xref: /titanic_51/usr/src/cmd/sgs/libelf/common/output.c (revision 24db46411fd54f70c35b94bb952eb7ba040e43b4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  *	Copyright (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 1.3	*/
31 
32 #include "syn.h"
33 #include <sys/mman.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <libelf.h>
38 #include <errno.h>
39 #include "decl.h"
40 #include "msg.h"
41 
42 /*
43  * File output
44  *	These functions write output files.
45  *	On SVR4 and newer systems use mmap(2).  On older systems (or on
46  *	file systems that don't support mmap), use write(2).
47  */
48 
49 
50 char *
51 _elf_outmap(int fd, size_t sz, unsigned int *pflag)
52 {
53 	char	*p;
54 
55 	/*
56 	 * Note: Some NFS implementations do not provide from enlarging a file
57 	 * via ftruncate(), thus this may fail with ENOSUP.  In this case the
58 	 * fall through to the calloc() mechanism will occur.
59 	 */
60 	if ((!*pflag) && (ftruncate(fd, (off_t)sz) == 0) &&
61 	    (p = mmap((char *)0, sz, PROT_READ+PROT_WRITE,
62 	    MAP_SHARED, fd, (off_t)0)) != (char *)-1) {
63 		*pflag = 1;
64 		return (p);
65 	}
66 
67 	*pflag = 0;
68 
69 	/*
70 	 * If mmap fails, try calloc.  Some file systems don't mmap.  Note, we
71 	 * use calloc rather than malloc, as ld(1) assumes that the backing
72 	 * storage it is working with is zero filled.
73 	 */
74 	if ((p = (char *)calloc(1, sz)) == 0)
75 		_elf_seterr(EMEM_OUT, errno);
76 	return (p);
77 }
78 
79 
80 size_t
81 _elf_outsync(int fd, char *p, size_t sz, unsigned int flag)
82 {
83 	if (flag != 0) {
84 		int	err;
85 		/*
86 		 * The value of MS_SYNC changed from zero to non-zero
87 		 * in SunOS 5.7.  This double call covers both cases.
88 		 */
89 		if ((fd = msync(p, sz, MS_SYNC)) == -1)
90 			fd = msync(p, sz, 0);
91 		if (fd == -1)
92 			err = errno;
93 		(void) munmap(p, sz);
94 		if (fd == 0)
95 			return (sz);
96 		_elf_seterr(EIO_SYNC, err);
97 		return (0);
98 	}
99 	if ((lseek(fd, 0L, SEEK_SET) == 0) &&
100 	    (write(fd, p, sz) == sz) && (fsync(fd) == 0)) {
101 		(void) free(p);
102 		return (sz);
103 	}
104 	_elf_seterr(EIO_WRITE, errno);
105 	return (0);
106 }
107