xref: /titanic_51/usr/src/cmd/sgs/libelf/common/output.c (revision b6917abefc343244b784f0cc34bc65b01469c3bf)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  *	Copyright (c) 1988 AT&T
29  *	  All Rights Reserved
30  */
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #include <sys/mman.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <libelf.h>
39 #include <errno.h>
40 #include "decl.h"
41 #include "msg.h"
42 
43 /*
44  * File output
45  *	These functions write output files.
46  *	On SVR4 and newer systems use mmap(2).  On older systems (or on
47  *	file systems that don't support mmap), use write(2).
48  */
49 
50 
51 char *
52 _elf_outmap(int fd, size_t sz, unsigned int *pflag)
53 {
54 	char	*p;
55 
56 	/*
57 	 * Note: Some NFS implementations do not provide from enlarging a file
58 	 * via ftruncate(), thus this may fail with ENOSUP.  In this case the
59 	 * fall through to the calloc() mechanism will occur.
60 	 */
61 	if ((!*pflag) && (ftruncate(fd, (off_t)sz) == 0) &&
62 	    (p = mmap((char *)0, sz, PROT_READ+PROT_WRITE,
63 	    MAP_SHARED, fd, (off_t)0)) != (char *)-1) {
64 		*pflag = 1;
65 		return (p);
66 	}
67 
68 	*pflag = 0;
69 
70 	/*
71 	 * If mmap fails, try calloc.  Some file systems don't mmap.  Note, we
72 	 * use calloc rather than malloc, as ld(1) assumes that the backing
73 	 * storage it is working with is zero filled.
74 	 */
75 	if ((p = (char *)calloc(1, sz)) == 0)
76 		_elf_seterr(EMEM_OUT, errno);
77 	return (p);
78 }
79 
80 
81 size_t
82 _elf_outsync(int fd, char *p, size_t sz, unsigned int flag)
83 {
84 	if (flag != 0) {
85 		int	err;
86 		/*
87 		 * The value of MS_SYNC changed from zero to non-zero
88 		 * in SunOS 5.7.  This double call covers both cases.
89 		 */
90 		if ((fd = msync(p, sz, MS_SYNC)) == -1)
91 			fd = msync(p, sz, 0);
92 		if (fd == -1)
93 			err = errno;
94 		(void) munmap(p, sz);
95 		if (fd == 0)
96 			return (sz);
97 		_elf_seterr(EIO_SYNC, err);
98 		return (0);
99 	}
100 	if ((lseek(fd, 0L, SEEK_SET) == 0) &&
101 	    (write(fd, p, sz) == sz) && (fsync(fd) == 0)) {
102 		(void) free(p);
103 		return (sz);
104 	}
105 	_elf_seterr(EIO_WRITE, errno);
106 	return (0);
107 }
108