libelf_open.c revision 1.2.8.2
1/*	$NetBSD: libelf_open.c,v 1.2.8.2 2014/08/19 23:46:45 tls Exp $	*/
2
3/*-
4 * Copyright (c) 2006,2008-2011 Joseph Koshy
5 * All rights reserved.
6 *
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.
15 *
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.
27 */
28
29#if HAVE_NBTOOL_CONFIG_H
30# include "nbtool_config.h"
31#endif
32
33#include <sys/types.h>
34#include <sys/stat.h>
35
36#include <assert.h>
37#include <errno.h>
38#include <libelf.h>
39#include <stdlib.h>
40#include <unistd.h>
41
42#include "_libelf.h"
43
44#if	ELFTC_HAVE_MMAP
45#include <sys/mman.h>
46#endif
47
48__RCSID("$NetBSD: libelf_open.c,v 1.2.8.2 2014/08/19 23:46:45 tls Exp $");
49ELFTC_VCSID("Id: libelf_open.c 2932 2013-03-30 01:26:04Z jkoshy ");
50
51#define	_LIBELF_INITSIZE	(64*1024)
52
53/*
54 * Read from a device file, pipe or socket.
55 */
56static void *
57_libelf_read_special_file(int fd, size_t *fsz)
58{
59	ssize_t readsz;
60	size_t bufsz, datasz;
61	unsigned char *buf, *t;
62
63	datasz = 0;
64	readsz = 0;
65	bufsz = _LIBELF_INITSIZE;
66	if ((buf = malloc(bufsz)) == NULL)
67		goto resourceerror;
68
69	/*
70	 * Read data from the file descriptor till we reach EOF, or
71	 * till an error is encountered.
72	 */
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;
80		}
81
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);
89
90	} while (readsz > 0);
91
92	if (readsz < 0) {
93		LIBELF_SET_ERROR(IO, errno);
94		goto error;
95	}
96
97	assert(readsz == 0);
98
99	/*
100	 * Free up extra buffer space.
101	 */
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;
111		}
112	}
113
114	*fsz = datasz;
115	return (buf);
116
117resourceerror:
118	LIBELF_SET_ERROR(RESOURCE, 0);
119error:
120	if (buf != NULL)
121		free(buf);
122	return (NULL);
123}
124
125/*
126 * Read the contents of the file referenced by the file descriptor
127 * 'fd'.
128 */
129
130Elf *
131_libelf_open_object(int fd, Elf_Cmd c, int reporterror)
132{
133	Elf *e;
134	void *m;
135	mode_t mode;
136	size_t fsize;
137	struct stat sb;
138	unsigned int flags;
139
140	assert(c == ELF_C_READ || c == ELF_C_RDWR || c == ELF_C_WRITE);
141
142	if (fstat(fd, &sb) < 0) {
143		LIBELF_SET_ERROR(IO, errno);
144		return (NULL);
145	}
146
147	mode = sb.st_mode;
148	fsize = (size_t) sb.st_size;
149
150	/*
151	 * Reject unsupported file types.
152	 */
153	if (!S_ISREG(mode) && !S_ISCHR(mode) && !S_ISFIFO(mode) &&
154	    !S_ISSOCK(mode)) {
155		LIBELF_SET_ERROR(ARGUMENT, 0);
156		return (NULL);
157	}
158
159	/*
160	 * For ELF_C_WRITE mode, allocate and return a descriptor.
161	 */
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;
170		}
171
172		return (e);
173	}
174
175
176	/*
177	 * ELF_C_READ and ELF_C_RDWR mode.
178	 */
179	m = NULL;
180	flags = 0;
181	if (S_ISREG(mode)) {
182
183		/*
184		 * Reject zero length files.
185		 */
186		if (fsize == 0) {
187			LIBELF_SET_ERROR(ARGUMENT, 0);
188			return (NULL);
189		}
190
191#if	ELFTC_HAVE_MMAP
192		/*
193		 * Always map regular files in with 'PROT_READ'
194		 * permissions.
195		 *
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.
200		 */
201		m = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, (off_t) 0);
202
203		if (m == MAP_FAILED)
204			m = NULL;
205		else
206			flags = LIBELF_F_RAWFILE_MMAP;
207#endif
208
209		/*
210		 * Fallback to a read() if the call to mmap() failed,
211		 * or if mmap() is not available.
212		 */
213		if (m == NULL) {
214			if ((m = malloc(fsize)) == NULL) {
215				LIBELF_SET_ERROR(RESOURCE, 0);
216				return (NULL);
217			}
218
219			if (read(fd, m, fsize) != (ssize_t) fsize) {
220				LIBELF_SET_ERROR(IO, errno);
221				free(m);
222				return (NULL);
223			}
224
225			flags = LIBELF_F_RAWFILE_MALLOC;
226		}
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);
231
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);
242	}
243
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);
249	}
250
251	e->e_flags |= flags;
252	e->e_fd = fd;
253	e->e_cmd = c;
254
255	return (e);
256}
257