Remove building with NOCRYPTO option
[minix.git] / external / bsd / elftoolchain / dist / libelf / libelf_open.c
blobe3c908d98a7d83a46e6412c487cdc814cada7645
1 /* $NetBSD: libelf_open.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
3 /*-
4 * Copyright (c) 2006,2008-2011 Joseph Koshy
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #if HAVE_NBTOOL_CONFIG_H
30 # include "nbtool_config.h"
31 #endif
33 #include <sys/types.h>
34 #include <sys/stat.h>
36 #include <assert.h>
37 #include <errno.h>
38 #include <libelf.h>
39 #include <stdlib.h>
40 #include <unistd.h>
42 #include "_libelf.h"
44 #if ELFTC_HAVE_MMAP
45 #include <sys/mman.h>
46 #endif
48 __RCSID("$NetBSD: libelf_open.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
49 ELFTC_VCSID("Id: libelf_open.c 2932 2013-03-30 01:26:04Z jkoshy ");
51 #define _LIBELF_INITSIZE (64*1024)
54 * Read from a device file, pipe or socket.
56 static void *
57 _libelf_read_special_file(int fd, size_t *fsz)
59 ssize_t readsz;
60 size_t bufsz, datasz;
61 unsigned char *buf, *t;
63 datasz = 0;
64 readsz = 0;
65 bufsz = _LIBELF_INITSIZE;
66 if ((buf = malloc(bufsz)) == NULL)
67 goto resourceerror;
70 * Read data from the file descriptor till we reach EOF, or
71 * till an error is encountered.
73 do {
74 /* Check if we need to expand the data buffer. */
75 if (datasz == bufsz) {
76 bufsz *= 2;
77 if ((t = realloc(buf, bufsz)) == NULL)
78 goto resourceerror;
79 buf = t;
82 do {
83 readsz = bufsz - datasz;
84 t = buf + datasz;
85 if ((readsz = read(fd, t, readsz)) <= 0)
86 break;
87 datasz += readsz;
88 } while (datasz < bufsz);
90 } while (readsz > 0);
92 if (readsz < 0) {
93 LIBELF_SET_ERROR(IO, errno);
94 goto error;
97 assert(readsz == 0);
100 * Free up extra buffer space.
102 if (bufsz > datasz) {
103 if (datasz > 0) {
104 if ((t = realloc(buf, datasz)) == NULL)
105 goto resourceerror;
106 buf = t;
107 } else { /* Zero bytes read. */
108 LIBELF_SET_ERROR(ARGUMENT, 0);
109 free(buf);
110 buf = NULL;
114 *fsz = datasz;
115 return (buf);
117 resourceerror:
118 LIBELF_SET_ERROR(RESOURCE, 0);
119 error:
120 if (buf != NULL)
121 free(buf);
122 return (NULL);
126 * Read the contents of the file referenced by the file descriptor
127 * 'fd'.
130 Elf *
131 _libelf_open_object(int fd, Elf_Cmd c, int reporterror)
133 Elf *e;
134 void *m;
135 mode_t mode;
136 size_t fsize;
137 struct stat sb;
138 unsigned int flags;
140 assert(c == ELF_C_READ || c == ELF_C_RDWR || c == ELF_C_WRITE);
142 if (fstat(fd, &sb) < 0) {
143 LIBELF_SET_ERROR(IO, errno);
144 return (NULL);
147 mode = sb.st_mode;
148 fsize = (size_t) sb.st_size;
151 * Reject unsupported file types.
153 if (!S_ISREG(mode) && !S_ISCHR(mode) && !S_ISFIFO(mode) &&
154 !S_ISSOCK(mode)) {
155 LIBELF_SET_ERROR(ARGUMENT, 0);
156 return (NULL);
160 * For ELF_C_WRITE mode, allocate and return a descriptor.
162 if (c == ELF_C_WRITE) {
163 if ((e = _libelf_allocate_elf()) != NULL) {
164 _libelf_init_elf(e, ELF_K_ELF);
165 e->e_byteorder = _libelf_host_byteorder();
166 e->e_fd = fd;
167 e->e_cmd = c;
168 if (!S_ISREG(mode))
169 e->e_flags |= LIBELF_F_SPECIAL_FILE;
172 return (e);
177 * ELF_C_READ and ELF_C_RDWR mode.
179 m = NULL;
180 flags = 0;
181 if (S_ISREG(mode)) {
184 * Reject zero length files.
186 if (fsize == 0) {
187 LIBELF_SET_ERROR(ARGUMENT, 0);
188 return (NULL);
191 #if ELFTC_HAVE_MMAP
193 * Always map regular files in with 'PROT_READ'
194 * permissions.
196 * For objects opened in ELF_C_RDWR mode, when
197 * elf_update(3) is called, we remove this mapping,
198 * write file data out using write(2), and map the new
199 * contents back.
201 m = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, (off_t) 0);
203 if (m == MAP_FAILED)
204 m = NULL;
205 else
206 flags = LIBELF_F_RAWFILE_MMAP;
207 #endif
210 * Fallback to a read() if the call to mmap() failed,
211 * or if mmap() is not available.
213 if (m == NULL) {
214 if ((m = malloc(fsize)) == NULL) {
215 LIBELF_SET_ERROR(RESOURCE, 0);
216 return (NULL);
219 if (read(fd, m, fsize) != (ssize_t) fsize) {
220 LIBELF_SET_ERROR(IO, errno);
221 free(m);
222 return (NULL);
225 flags = LIBELF_F_RAWFILE_MALLOC;
227 } else if ((m = _libelf_read_special_file(fd, &fsize)) != NULL)
228 flags = LIBELF_F_RAWFILE_MALLOC | LIBELF_F_SPECIAL_FILE;
229 else
230 return (NULL);
232 if ((e = _libelf_memory(m, fsize, reporterror)) == NULL) {
233 assert((flags & LIBELF_F_RAWFILE_MALLOC) ||
234 (flags & LIBELF_F_RAWFILE_MMAP));
235 if (flags & LIBELF_F_RAWFILE_MALLOC)
236 free(m);
237 #if ELFTC_HAVE_MMAP
238 else
239 (void) munmap(m, fsize);
240 #endif
241 return (NULL);
244 /* ar(1) archives aren't supported in RDWR mode. */
245 if (c == ELF_C_RDWR && e->e_kind == ELF_K_AR) {
246 (void) elf_end(e);
247 LIBELF_SET_ERROR(ARGUMENT, 0);
248 return (NULL);
251 e->e_flags |= flags;
252 e->e_fd = fd;
253 e->e_cmd = c;
255 return (e);