xref: /illumos-gate/usr/src/cmd/sgs/libelf/common/output.c (revision 7dbbfe77)
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 
227257d1b4Sraf /*
23842eec28SRod Evans  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247257d1b4Sraf  * Use is subject to license terms.
257257d1b4Sraf  */
267257d1b4Sraf 
277c478bd9Sstevel@tonic-gate /*
28e78654d4Srie  *	Copyright (c) 1988 AT&T
29e78654d4Srie  *	  All Rights Reserved
307c478bd9Sstevel@tonic-gate  */
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 *
_elf_outmap(int fd,size_t sz,unsigned int * pflag)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
_elf_outsync(int fd,char * p,size_t sz,unsigned int flag)807c478bd9Sstevel@tonic-gate _elf_outsync(int fd, char *p, size_t sz, unsigned int flag)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	if (flag != 0) {
83*7dbbfe77SToomas Soome 		int	rv, err;
84842eec28SRod Evans 
85*7dbbfe77SToomas Soome 		err = ENOTSUP; /* msync should only return 0 or -1 */
86*7dbbfe77SToomas Soome 		rv = msync(p, sz, MS_ASYNC);
87*7dbbfe77SToomas Soome 		if (rv == -1)
887c478bd9Sstevel@tonic-gate 			err = errno;
897c478bd9Sstevel@tonic-gate 		(void) munmap(p, sz);
90*7dbbfe77SToomas Soome 		if (rv == 0)
917c478bd9Sstevel@tonic-gate 			return (sz);
927c478bd9Sstevel@tonic-gate 		_elf_seterr(EIO_SYNC, err);
937c478bd9Sstevel@tonic-gate 		return (0);
947c478bd9Sstevel@tonic-gate 	}
95e78654d4Srie 	if ((lseek(fd, 0L, SEEK_SET) == 0) &&
96842eec28SRod Evans 	    (write(fd, p, sz) == sz)) {
97e78654d4Srie 		(void) free(p);
98e78654d4Srie 		return (sz);
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 	_elf_seterr(EIO_WRITE, errno);
1017c478bd9Sstevel@tonic-gate 	return (0);
1027c478bd9Sstevel@tonic-gate }
103