17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5e78654d4Srie * Common Development and Distribution License (the "License"). 6e78654d4Srie * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate /* 23*842eec28SRod Evans * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277257d1b4Sraf /* 287257d1b4Sraf * Copyright (c) 1988 AT&T 297257d1b4Sraf * All Rights Reserved 307257d1b4Sraf */ 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <sys/mman.h> 337c478bd9Sstevel@tonic-gate #include <stdio.h> 347c478bd9Sstevel@tonic-gate #include <stdlib.h> 357c478bd9Sstevel@tonic-gate #include <unistd.h> 367c478bd9Sstevel@tonic-gate #include <libelf.h> 377c478bd9Sstevel@tonic-gate #include <errno.h> 387c478bd9Sstevel@tonic-gate #include "decl.h" 397c478bd9Sstevel@tonic-gate #include "msg.h" 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * File output 437c478bd9Sstevel@tonic-gate * These functions write output files. 447c478bd9Sstevel@tonic-gate * On SVR4 and newer systems use mmap(2). On older systems (or on 457c478bd9Sstevel@tonic-gate * file systems that don't support mmap), use write(2). 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate char * 507c478bd9Sstevel@tonic-gate _elf_outmap(int fd, size_t sz, unsigned int *pflag) 517c478bd9Sstevel@tonic-gate { 527c478bd9Sstevel@tonic-gate char *p; 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate /* 557c478bd9Sstevel@tonic-gate * Note: Some NFS implementations do not provide from enlarging a file 567c478bd9Sstevel@tonic-gate * via ftruncate(), thus this may fail with ENOSUP. In this case the 577c478bd9Sstevel@tonic-gate * fall through to the calloc() mechanism will occur. 587c478bd9Sstevel@tonic-gate */ 597c478bd9Sstevel@tonic-gate if ((!*pflag) && (ftruncate(fd, (off_t)sz) == 0) && 607c478bd9Sstevel@tonic-gate (p = mmap((char *)0, sz, PROT_READ+PROT_WRITE, 617c478bd9Sstevel@tonic-gate MAP_SHARED, fd, (off_t)0)) != (char *)-1) { 627c478bd9Sstevel@tonic-gate *pflag = 1; 637c478bd9Sstevel@tonic-gate return (p); 647c478bd9Sstevel@tonic-gate } 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate *pflag = 0; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate /* 697c478bd9Sstevel@tonic-gate * If mmap fails, try calloc. Some file systems don't mmap. Note, we 707c478bd9Sstevel@tonic-gate * use calloc rather than malloc, as ld(1) assumes that the backing 717c478bd9Sstevel@tonic-gate * storage it is working with is zero filled. 727c478bd9Sstevel@tonic-gate */ 737c478bd9Sstevel@tonic-gate if ((p = (char *)calloc(1, sz)) == 0) 747c478bd9Sstevel@tonic-gate _elf_seterr(EMEM_OUT, errno); 757c478bd9Sstevel@tonic-gate return (p); 767c478bd9Sstevel@tonic-gate } 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate size_t 807c478bd9Sstevel@tonic-gate _elf_outsync(int fd, char *p, size_t sz, unsigned int flag) 817c478bd9Sstevel@tonic-gate { 827c478bd9Sstevel@tonic-gate if (flag != 0) { 837c478bd9Sstevel@tonic-gate int err; 84*842eec28SRod Evans 85*842eec28SRod Evans if ((fd = msync(p, sz, MS_ASYNC)) == -1) 867c478bd9Sstevel@tonic-gate err = errno; 877c478bd9Sstevel@tonic-gate (void) munmap(p, sz); 887c478bd9Sstevel@tonic-gate if (fd == 0) 897c478bd9Sstevel@tonic-gate return (sz); 907c478bd9Sstevel@tonic-gate _elf_seterr(EIO_SYNC, err); 917c478bd9Sstevel@tonic-gate return (0); 927c478bd9Sstevel@tonic-gate } 93e78654d4Srie if ((lseek(fd, 0L, SEEK_SET) == 0) && 94*842eec28SRod Evans (write(fd, p, sz) == sz)) { 957c478bd9Sstevel@tonic-gate (void) free(p); 967c478bd9Sstevel@tonic-gate return (sz); 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate _elf_seterr(EIO_WRITE, errno); 997c478bd9Sstevel@tonic-gate return (0); 1007c478bd9Sstevel@tonic-gate } 101