xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/libelf_open.c (revision 5ac3bc719ce6e70593039505b491894133237d12)
1*5ac3bc71Schristos /*	$NetBSD: libelf_open.c,v 1.5 2024/03/03 17:37:34 christos Exp $	*/
2e81373b4Schristos 
39dd9d0cfSchristos /*-
49dd9d0cfSchristos  * Copyright (c) 2006,2008-2011 Joseph Koshy
59dd9d0cfSchristos  * All rights reserved.
69dd9d0cfSchristos  *
79dd9d0cfSchristos  * Redistribution and use in source and binary forms, with or without
89dd9d0cfSchristos  * modification, are permitted provided that the following conditions
99dd9d0cfSchristos  * are met:
109dd9d0cfSchristos  * 1. Redistributions of source code must retain the above copyright
119dd9d0cfSchristos  *    notice, this list of conditions and the following disclaimer.
129dd9d0cfSchristos  * 2. Redistributions in binary form must reproduce the above copyright
139dd9d0cfSchristos  *    notice, this list of conditions and the following disclaimer in the
149dd9d0cfSchristos  *    documentation and/or other materials provided with the distribution.
159dd9d0cfSchristos  *
169dd9d0cfSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179dd9d0cfSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189dd9d0cfSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199dd9d0cfSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209dd9d0cfSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219dd9d0cfSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229dd9d0cfSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239dd9d0cfSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249dd9d0cfSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259dd9d0cfSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269dd9d0cfSchristos  * SUCH DAMAGE.
279dd9d0cfSchristos  */
289dd9d0cfSchristos 
29e81373b4Schristos #if HAVE_NBTOOL_CONFIG_H
30e81373b4Schristos # include "nbtool_config.h"
31e81373b4Schristos #endif
32e81373b4Schristos 
33*5ac3bc71Schristos #include <sys/cdefs.h>
349dd9d0cfSchristos #include <sys/types.h>
359dd9d0cfSchristos #include <sys/stat.h>
369dd9d0cfSchristos 
379dd9d0cfSchristos #include <assert.h>
389dd9d0cfSchristos #include <errno.h>
399dd9d0cfSchristos #include <libelf.h>
409dd9d0cfSchristos #include <stdlib.h>
419dd9d0cfSchristos #include <unistd.h>
429dd9d0cfSchristos 
439dd9d0cfSchristos #include "_libelf.h"
449dd9d0cfSchristos 
459dd9d0cfSchristos #if	ELFTC_HAVE_MMAP
469dd9d0cfSchristos #include <sys/mman.h>
479dd9d0cfSchristos #endif
489dd9d0cfSchristos 
49*5ac3bc71Schristos __RCSID("$NetBSD: libelf_open.c,v 1.5 2024/03/03 17:37:34 christos Exp $");
50*5ac3bc71Schristos ELFTC_VCSID("Id: libelf_open.c 3977 2022-05-01 06:45:34Z jkoshy");
519dd9d0cfSchristos 
529dd9d0cfSchristos #define	_LIBELF_INITSIZE	(64*1024)
539dd9d0cfSchristos 
549dd9d0cfSchristos /*
559dd9d0cfSchristos  * Read from a device file, pipe or socket.
569dd9d0cfSchristos  */
579dd9d0cfSchristos static void *
_libelf_read_special_file(int fd,size_t * fsz)589dd9d0cfSchristos _libelf_read_special_file(int fd, size_t *fsz)
599dd9d0cfSchristos {
609dd9d0cfSchristos 	ssize_t readsz;
619dd9d0cfSchristos 	size_t bufsz, datasz;
629dd9d0cfSchristos 	unsigned char *buf, *t;
639dd9d0cfSchristos 
649dd9d0cfSchristos 	datasz = 0;
659dd9d0cfSchristos 	readsz = 0;
669dd9d0cfSchristos 	bufsz = _LIBELF_INITSIZE;
679dd9d0cfSchristos 	if ((buf = malloc(bufsz)) == NULL)
689dd9d0cfSchristos 		goto resourceerror;
699dd9d0cfSchristos 
709dd9d0cfSchristos 	/*
719dd9d0cfSchristos 	 * Read data from the file descriptor till we reach EOF, or
729dd9d0cfSchristos 	 * till an error is encountered.
739dd9d0cfSchristos 	 */
749dd9d0cfSchristos 	do {
759dd9d0cfSchristos 		/* Check if we need to expand the data buffer. */
769dd9d0cfSchristos 		if (datasz == bufsz) {
779dd9d0cfSchristos 			bufsz *= 2;
789dd9d0cfSchristos 			if ((t = realloc(buf, bufsz)) == NULL)
799dd9d0cfSchristos 				goto resourceerror;
809dd9d0cfSchristos 			buf = t;
819dd9d0cfSchristos 		}
829dd9d0cfSchristos 
839dd9d0cfSchristos 		do {
8442bd3019Schristos 			assert(bufsz - datasz > 0);
859dd9d0cfSchristos 			t = buf + datasz;
8642bd3019Schristos 			if ((readsz = read(fd, t, bufsz - datasz)) <= 0)
879dd9d0cfSchristos 				break;
8842bd3019Schristos 			datasz += (size_t) readsz;
899dd9d0cfSchristos 		} while (datasz < bufsz);
909dd9d0cfSchristos 
919dd9d0cfSchristos 	} while (readsz > 0);
929dd9d0cfSchristos 
939dd9d0cfSchristos 	if (readsz < 0) {
949dd9d0cfSchristos 		LIBELF_SET_ERROR(IO, errno);
959dd9d0cfSchristos 		goto error;
969dd9d0cfSchristos 	}
979dd9d0cfSchristos 
989dd9d0cfSchristos 	assert(readsz == 0);
999dd9d0cfSchristos 
1009dd9d0cfSchristos 	/*
1019dd9d0cfSchristos 	 * Free up extra buffer space.
1029dd9d0cfSchristos 	 */
1039dd9d0cfSchristos 	if (bufsz > datasz) {
1049dd9d0cfSchristos 		if (datasz > 0) {
1059dd9d0cfSchristos 			if ((t = realloc(buf, datasz)) == NULL)
1069dd9d0cfSchristos 				goto resourceerror;
1079dd9d0cfSchristos 			buf = t;
1089dd9d0cfSchristos 		} else {	/* Zero bytes read. */
1099dd9d0cfSchristos 			LIBELF_SET_ERROR(ARGUMENT, 0);
1109dd9d0cfSchristos 			free(buf);
1119dd9d0cfSchristos 			buf = NULL;
1129dd9d0cfSchristos 		}
1139dd9d0cfSchristos 	}
1149dd9d0cfSchristos 
1159dd9d0cfSchristos 	*fsz = datasz;
1169dd9d0cfSchristos 	return (buf);
1179dd9d0cfSchristos 
1189dd9d0cfSchristos resourceerror:
1199dd9d0cfSchristos 	LIBELF_SET_ERROR(RESOURCE, 0);
1209dd9d0cfSchristos error:
1219dd9d0cfSchristos 	if (buf != NULL)
1229dd9d0cfSchristos 		free(buf);
1239dd9d0cfSchristos 	return (NULL);
1249dd9d0cfSchristos }
1259dd9d0cfSchristos 
1269dd9d0cfSchristos /*
1279dd9d0cfSchristos  * Read the contents of the file referenced by the file descriptor
1289dd9d0cfSchristos  * 'fd'.
1299dd9d0cfSchristos  */
1309dd9d0cfSchristos 
1319dd9d0cfSchristos Elf *
_libelf_open_object(int fd,Elf_Cmd c,int reporterror)1329dd9d0cfSchristos _libelf_open_object(int fd, Elf_Cmd c, int reporterror)
1339dd9d0cfSchristos {
1349dd9d0cfSchristos 	Elf *e;
1359dd9d0cfSchristos 	void *m;
1369dd9d0cfSchristos 	mode_t mode;
1379dd9d0cfSchristos 	size_t fsize;
1389dd9d0cfSchristos 	struct stat sb;
1399dd9d0cfSchristos 	unsigned int flags;
1409dd9d0cfSchristos 
1419dd9d0cfSchristos 	assert(c == ELF_C_READ || c == ELF_C_RDWR || c == ELF_C_WRITE);
1429dd9d0cfSchristos 
1439dd9d0cfSchristos 	if (fstat(fd, &sb) < 0) {
1449dd9d0cfSchristos 		LIBELF_SET_ERROR(IO, errno);
1459dd9d0cfSchristos 		return (NULL);
1469dd9d0cfSchristos 	}
1479dd9d0cfSchristos 
1489dd9d0cfSchristos 	mode = sb.st_mode;
1499dd9d0cfSchristos 	fsize = (size_t) sb.st_size;
1509dd9d0cfSchristos 
1519dd9d0cfSchristos 	/*
1529dd9d0cfSchristos 	 * Reject unsupported file types.
1539dd9d0cfSchristos 	 */
1549dd9d0cfSchristos 	if (!S_ISREG(mode) && !S_ISCHR(mode) && !S_ISFIFO(mode) &&
1559dd9d0cfSchristos 	    !S_ISSOCK(mode)) {
1569dd9d0cfSchristos 		LIBELF_SET_ERROR(ARGUMENT, 0);
1579dd9d0cfSchristos 		return (NULL);
1589dd9d0cfSchristos 	}
1599dd9d0cfSchristos 
1609dd9d0cfSchristos 	/*
1619dd9d0cfSchristos 	 * For ELF_C_WRITE mode, allocate and return a descriptor.
1629dd9d0cfSchristos 	 */
1639dd9d0cfSchristos 	if (c == ELF_C_WRITE) {
1649dd9d0cfSchristos 		if ((e = _libelf_allocate_elf()) != NULL) {
1659dd9d0cfSchristos 			_libelf_init_elf(e, ELF_K_ELF);
166*5ac3bc71Schristos 			e->e_byteorder = LIBELF_PRIVATE(byteorder);
1679dd9d0cfSchristos 			e->e_fd = fd;
1689dd9d0cfSchristos 			e->e_cmd = c;
1699dd9d0cfSchristos 			if (!S_ISREG(mode))
1709dd9d0cfSchristos 				e->e_flags |= LIBELF_F_SPECIAL_FILE;
1719dd9d0cfSchristos 		}
1729dd9d0cfSchristos 
1739dd9d0cfSchristos 		return (e);
1749dd9d0cfSchristos 	}
1759dd9d0cfSchristos 
1769dd9d0cfSchristos 
1779dd9d0cfSchristos 	/*
1789dd9d0cfSchristos 	 * ELF_C_READ and ELF_C_RDWR mode.
1799dd9d0cfSchristos 	 */
1809dd9d0cfSchristos 	m = NULL;
1819dd9d0cfSchristos 	flags = 0;
1829dd9d0cfSchristos 	if (S_ISREG(mode)) {
1839dd9d0cfSchristos 
1849dd9d0cfSchristos 		/*
1859dd9d0cfSchristos 		 * Reject zero length files.
1869dd9d0cfSchristos 		 */
1879dd9d0cfSchristos 		if (fsize == 0) {
1889dd9d0cfSchristos 			LIBELF_SET_ERROR(ARGUMENT, 0);
1899dd9d0cfSchristos 			return (NULL);
1909dd9d0cfSchristos 		}
1919dd9d0cfSchristos 
1929dd9d0cfSchristos #if	ELFTC_HAVE_MMAP
1939dd9d0cfSchristos 		/*
1949dd9d0cfSchristos 		 * Always map regular files in with 'PROT_READ'
1959dd9d0cfSchristos 		 * permissions.
1969dd9d0cfSchristos 		 *
1979dd9d0cfSchristos 		 * For objects opened in ELF_C_RDWR mode, when
1989dd9d0cfSchristos 		 * elf_update(3) is called, we remove this mapping,
1999dd9d0cfSchristos 		 * write file data out using write(2), and map the new
2009dd9d0cfSchristos 		 * contents back.
2019dd9d0cfSchristos 		 */
2029dd9d0cfSchristos 		m = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, (off_t) 0);
2039dd9d0cfSchristos 
2049dd9d0cfSchristos 		if (m == MAP_FAILED)
2059dd9d0cfSchristos 			m = NULL;
2069dd9d0cfSchristos 		else
2079dd9d0cfSchristos 			flags = LIBELF_F_RAWFILE_MMAP;
2089dd9d0cfSchristos #endif
2099dd9d0cfSchristos 
2109dd9d0cfSchristos 		/*
2119dd9d0cfSchristos 		 * Fallback to a read() if the call to mmap() failed,
2129dd9d0cfSchristos 		 * or if mmap() is not available.
2139dd9d0cfSchristos 		 */
2149dd9d0cfSchristos 		if (m == NULL) {
2159dd9d0cfSchristos 			if ((m = malloc(fsize)) == NULL) {
2169dd9d0cfSchristos 				LIBELF_SET_ERROR(RESOURCE, 0);
2179dd9d0cfSchristos 				return (NULL);
2189dd9d0cfSchristos 			}
2199dd9d0cfSchristos 
2209dd9d0cfSchristos 			if (read(fd, m, fsize) != (ssize_t) fsize) {
2219dd9d0cfSchristos 				LIBELF_SET_ERROR(IO, errno);
2229dd9d0cfSchristos 				free(m);
2239dd9d0cfSchristos 				return (NULL);
2249dd9d0cfSchristos 			}
2259dd9d0cfSchristos 
2269dd9d0cfSchristos 			flags = LIBELF_F_RAWFILE_MALLOC;
2279dd9d0cfSchristos 		}
2289dd9d0cfSchristos 	} else if ((m = _libelf_read_special_file(fd, &fsize)) != NULL)
2299dd9d0cfSchristos 		flags = LIBELF_F_RAWFILE_MALLOC | LIBELF_F_SPECIAL_FILE;
2309dd9d0cfSchristos 	else
2319dd9d0cfSchristos 		return (NULL);
2329dd9d0cfSchristos 
2339dd9d0cfSchristos 	if ((e = _libelf_memory(m, fsize, reporterror)) == NULL) {
2349dd9d0cfSchristos 		assert((flags & LIBELF_F_RAWFILE_MALLOC) ||
2359dd9d0cfSchristos 		    (flags & LIBELF_F_RAWFILE_MMAP));
2369dd9d0cfSchristos 		if (flags & LIBELF_F_RAWFILE_MALLOC)
2379dd9d0cfSchristos 			free(m);
2389dd9d0cfSchristos #if	ELFTC_HAVE_MMAP
2399dd9d0cfSchristos 		else
2409dd9d0cfSchristos 			(void) munmap(m, fsize);
2419dd9d0cfSchristos #endif
2429dd9d0cfSchristos 		return (NULL);
2439dd9d0cfSchristos 	}
2449dd9d0cfSchristos 
2459dd9d0cfSchristos 	/* ar(1) archives aren't supported in RDWR mode. */
2469dd9d0cfSchristos 	if (c == ELF_C_RDWR && e->e_kind == ELF_K_AR) {
2479dd9d0cfSchristos 		(void) elf_end(e);
2489dd9d0cfSchristos 		LIBELF_SET_ERROR(ARGUMENT, 0);
2499dd9d0cfSchristos 		return (NULL);
2509dd9d0cfSchristos 	}
2519dd9d0cfSchristos 
2529dd9d0cfSchristos 	e->e_flags |= flags;
2539dd9d0cfSchristos 	e->e_fd = fd;
2549dd9d0cfSchristos 	e->e_cmd = c;
2559dd9d0cfSchristos 
2569dd9d0cfSchristos 	return (e);
2579dd9d0cfSchristos }
258