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