2 * Copyright (c) 2006,2008-2011 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/types.h>
42 ELFTC_VCSID("$Id: libelf_open.c 3007 2014-03-22 08:10:14Z jkoshy $");
44 #define _LIBELF_INITSIZE (64*1024)
47 * Read from a device file, pipe or socket.
50 _libelf_read_special_file(int fd, size_t *fsz)
54 unsigned char *buf, *t;
58 bufsz = _LIBELF_INITSIZE;
59 if ((buf = malloc(bufsz)) == NULL)
63 * Read data from the file descriptor till we reach EOF, or
64 * till an error is encountered.
67 /* Check if we need to expand the data buffer. */
68 if (datasz == bufsz) {
70 if ((t = realloc(buf, bufsz)) == NULL)
76 assert(bufsz - datasz > 0);
78 if ((readsz = read(fd, t, bufsz - datasz)) <= 0)
80 datasz += (size_t) readsz;
81 } while (datasz < bufsz);
86 LIBELF_SET_ERROR(IO, errno);
93 * Free up extra buffer space.
97 if ((t = realloc(buf, datasz)) == NULL)
100 } else { /* Zero bytes read. */
101 LIBELF_SET_ERROR(ARGUMENT, 0);
111 LIBELF_SET_ERROR(RESOURCE, 0);
119 * Read the contents of the file referenced by the file descriptor
124 _libelf_open_object(int fd, Elf_Cmd c, int reporterror)
133 assert(c == ELF_C_READ || c == ELF_C_RDWR || c == ELF_C_WRITE);
135 if (fstat(fd, &sb) < 0) {
136 LIBELF_SET_ERROR(IO, errno);
141 fsize = (size_t) sb.st_size;
144 * Reject unsupported file types.
146 if (!S_ISREG(mode) && !S_ISCHR(mode) && !S_ISFIFO(mode) &&
148 LIBELF_SET_ERROR(ARGUMENT, 0);
153 * For ELF_C_WRITE mode, allocate and return a descriptor.
155 if (c == ELF_C_WRITE) {
156 if ((e = _libelf_allocate_elf()) != NULL) {
157 _libelf_init_elf(e, ELF_K_ELF);
158 e->e_byteorder = LIBELF_PRIVATE(byteorder);
162 e->e_flags |= LIBELF_F_SPECIAL_FILE;
170 * ELF_C_READ and ELF_C_RDWR mode.
177 * Reject zero length files.
180 LIBELF_SET_ERROR(ARGUMENT, 0);
186 * Always map regular files in with 'PROT_READ'
189 * For objects opened in ELF_C_RDWR mode, when
190 * elf_update(3) is called, we remove this mapping,
191 * write file data out using write(2), and map the new
194 m = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, (off_t) 0);
199 flags = LIBELF_F_RAWFILE_MMAP;
203 * Fallback to a read() if the call to mmap() failed,
204 * or if mmap() is not available.
207 if ((m = malloc(fsize)) == NULL) {
208 LIBELF_SET_ERROR(RESOURCE, 0);
212 if (read(fd, m, fsize) != (ssize_t) fsize) {
213 LIBELF_SET_ERROR(IO, errno);
218 flags = LIBELF_F_RAWFILE_MALLOC;
220 } else if ((m = _libelf_read_special_file(fd, &fsize)) != NULL)
221 flags = LIBELF_F_RAWFILE_MALLOC | LIBELF_F_SPECIAL_FILE;
225 if ((e = _libelf_memory(m, fsize, reporterror)) == NULL) {
226 assert((flags & LIBELF_F_RAWFILE_MALLOC) ||
227 (flags & LIBELF_F_RAWFILE_MMAP));
228 if (flags & LIBELF_F_RAWFILE_MALLOC)
232 (void) munmap(m, fsize);
237 /* ar(1) archives aren't supported in RDWR mode. */
238 if (c == ELF_C_RDWR && e->e_kind == ELF_K_AR) {
240 LIBELF_SET_ERROR(ARGUMENT, 0);