xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/elf_scn.c (revision 5ac3bc719ce6e70593039505b491894133237d12)
1*5ac3bc71Schristos /*	$NetBSD: elf_scn.c,v 1.5 2024/03/03 17:37:33 christos Exp $	*/
2e81373b4Schristos 
39dd9d0cfSchristos /*-
49dd9d0cfSchristos  * Copyright (c) 2006,2008-2010 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 
339dd9d0cfSchristos #include <sys/cdefs.h>
349dd9d0cfSchristos #include <sys/queue.h>
359dd9d0cfSchristos 
369dd9d0cfSchristos #include <assert.h>
379dd9d0cfSchristos #include <errno.h>
389dd9d0cfSchristos #include <gelf.h>
399dd9d0cfSchristos #include <libelf.h>
409dd9d0cfSchristos #include <stddef.h>
4142bd3019Schristos #include <stdint.h>
429dd9d0cfSchristos #include <stdlib.h>
439dd9d0cfSchristos 
449dd9d0cfSchristos #include "_libelf.h"
459dd9d0cfSchristos 
46*5ac3bc71Schristos __RCSID("$NetBSD: elf_scn.c,v 1.5 2024/03/03 17:37:33 christos Exp $");
479dd9d0cfSchristos 
489dd9d0cfSchristos /*
499dd9d0cfSchristos  * Load an ELF section table and create a list of Elf_Scn structures.
509dd9d0cfSchristos  */
519dd9d0cfSchristos int
_libelf_load_section_headers(Elf * e,void * ehdr)529dd9d0cfSchristos _libelf_load_section_headers(Elf *e, void *ehdr)
539dd9d0cfSchristos {
5442bd3019Schristos 	Elf_Scn *scn;
559dd9d0cfSchristos 	uint64_t shoff;
569dd9d0cfSchristos 	Elf32_Ehdr *eh32;
579dd9d0cfSchristos 	Elf64_Ehdr *eh64;
5842bd3019Schristos 	int ec, swapbytes;
5942bd3019Schristos 	unsigned char *src;
6042bd3019Schristos 	size_t fsz, i, shnum;
61*5ac3bc71Schristos 	_libelf_translator_function *xlator;
629dd9d0cfSchristos 
639dd9d0cfSchristos 	assert(e != NULL);
649dd9d0cfSchristos 	assert(ehdr != NULL);
659dd9d0cfSchristos 	assert((e->e_flags & LIBELF_F_SHDRS_LOADED) == 0);
669dd9d0cfSchristos 
679dd9d0cfSchristos #define	CHECK_EHDR(E,EH)	do {				\
68*5ac3bc71Schristos 		uintmax_t rawsize = (uintmax_t) e->e_rawsize;	\
69*5ac3bc71Schristos 		if (shoff > (uintmax_t) e->e_rawsize ||		\
7042bd3019Schristos 		    fsz != (EH)->e_shentsize ||			\
7142bd3019Schristos 		    shnum > SIZE_MAX / fsz ||			\
72*5ac3bc71Schristos 		    fsz * shnum > rawsize - shoff) {		\
739dd9d0cfSchristos 			LIBELF_SET_ERROR(HEADER, 0);		\
749dd9d0cfSchristos 			return (0);				\
759dd9d0cfSchristos 		}						\
76*5ac3bc71Schristos 	} while (/* CONSTCOND */ 0)
779dd9d0cfSchristos 
789dd9d0cfSchristos 	ec = e->e_class;
799dd9d0cfSchristos 	fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1);
809dd9d0cfSchristos 	assert(fsz > 0);
819dd9d0cfSchristos 
829dd9d0cfSchristos 	shnum = e->e_u.e_elf.e_nscn;
839dd9d0cfSchristos 
849dd9d0cfSchristos 	if (ec == ELFCLASS32) {
859dd9d0cfSchristos 		eh32 = (Elf32_Ehdr *) ehdr;
869dd9d0cfSchristos 		shoff = (uint64_t) eh32->e_shoff;
879dd9d0cfSchristos 		CHECK_EHDR(e, eh32);
889dd9d0cfSchristos 	} else {
899dd9d0cfSchristos 		eh64 = (Elf64_Ehdr *) ehdr;
909dd9d0cfSchristos 		shoff = eh64->e_shoff;
919dd9d0cfSchristos 		CHECK_EHDR(e, eh64);
929dd9d0cfSchristos 	}
939dd9d0cfSchristos 
94*5ac3bc71Schristos 	xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec,
95*5ac3bc71Schristos 	    _libelf_elfmachine(e));
969dd9d0cfSchristos 
97*5ac3bc71Schristos 	swapbytes = e->e_byteorder != LIBELF_PRIVATE(byteorder);
98e81373b4Schristos 	if (shoff > SSIZE_MAX) {
99e81373b4Schristos 		LIBELF_SET_ERROR(HEADER, 0);
100e81373b4Schristos 		return (0);
101e81373b4Schristos 	}
102e81373b4Schristos 	src = e->e_rawfile + (ssize_t)shoff;
1039dd9d0cfSchristos 
1049dd9d0cfSchristos 	/*
1059dd9d0cfSchristos 	 * If the file is using extended numbering then section #0
1069dd9d0cfSchristos 	 * would have already been read in.
1079dd9d0cfSchristos 	 */
1089dd9d0cfSchristos 
1099dd9d0cfSchristos 	i = 0;
1109dd9d0cfSchristos 	if (!STAILQ_EMPTY(&e->e_u.e_elf.e_scn)) {
1119dd9d0cfSchristos 		assert(STAILQ_FIRST(&e->e_u.e_elf.e_scn) ==
1129dd9d0cfSchristos 		    STAILQ_LAST(&e->e_u.e_elf.e_scn, _Elf_Scn, s_next));
1139dd9d0cfSchristos 
1149dd9d0cfSchristos 		i = 1;
1159dd9d0cfSchristos 		src += fsz;
1169dd9d0cfSchristos 	}
1179dd9d0cfSchristos 
1189dd9d0cfSchristos 	for (; i < shnum; i++, src += fsz) {
1199dd9d0cfSchristos 		if ((scn = _libelf_allocate_scn(e, i)) == NULL)
1209dd9d0cfSchristos 			return (0);
1219dd9d0cfSchristos 
12242bd3019Schristos 		(*xlator)((void *) &scn->s_shdr, sizeof(scn->s_shdr),
12342bd3019Schristos 		    src, (size_t) 1, swapbytes);
1249dd9d0cfSchristos 
1259dd9d0cfSchristos 		if (ec == ELFCLASS32) {
1269dd9d0cfSchristos 			scn->s_offset = scn->s_rawoff =
1279dd9d0cfSchristos 			    scn->s_shdr.s_shdr32.sh_offset;
1289dd9d0cfSchristos 			scn->s_size = scn->s_shdr.s_shdr32.sh_size;
1299dd9d0cfSchristos 		} else {
1309dd9d0cfSchristos 			scn->s_offset = scn->s_rawoff =
1319dd9d0cfSchristos 			    scn->s_shdr.s_shdr64.sh_offset;
1329dd9d0cfSchristos 			scn->s_size = scn->s_shdr.s_shdr64.sh_size;
1339dd9d0cfSchristos 		}
1349dd9d0cfSchristos 	}
1359dd9d0cfSchristos 
1369dd9d0cfSchristos 	e->e_flags |= LIBELF_F_SHDRS_LOADED;
1379dd9d0cfSchristos 
1389dd9d0cfSchristos 	return (1);
1399dd9d0cfSchristos }
1409dd9d0cfSchristos 
1419dd9d0cfSchristos 
1429dd9d0cfSchristos Elf_Scn *
elf_getscn(Elf * e,size_t index)1439dd9d0cfSchristos elf_getscn(Elf *e, size_t index)
1449dd9d0cfSchristos {
1459dd9d0cfSchristos 	int ec;
1469dd9d0cfSchristos 	void *ehdr;
1479dd9d0cfSchristos 	Elf_Scn *s;
1489dd9d0cfSchristos 
1499dd9d0cfSchristos 	if (e == NULL || e->e_kind != ELF_K_ELF ||
1509dd9d0cfSchristos 	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
1519dd9d0cfSchristos 		LIBELF_SET_ERROR(ARGUMENT, 0);
1529dd9d0cfSchristos 		return (NULL);
1539dd9d0cfSchristos 	}
1549dd9d0cfSchristos 
1559dd9d0cfSchristos 	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
1569dd9d0cfSchristos 		return (NULL);
1579dd9d0cfSchristos 
1589dd9d0cfSchristos 	if (e->e_cmd != ELF_C_WRITE &&
1599dd9d0cfSchristos 	    (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
1609dd9d0cfSchristos 	    _libelf_load_section_headers(e, ehdr) == 0)
1619dd9d0cfSchristos 		return (NULL);
1629dd9d0cfSchristos 
1639dd9d0cfSchristos 	STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next)
1649dd9d0cfSchristos 		if (s->s_ndx == index)
1659dd9d0cfSchristos 			return (s);
1669dd9d0cfSchristos 
1679dd9d0cfSchristos 	LIBELF_SET_ERROR(ARGUMENT, 0);
1689dd9d0cfSchristos 	return (NULL);
1699dd9d0cfSchristos }
1709dd9d0cfSchristos 
1719dd9d0cfSchristos size_t
elf_ndxscn(Elf_Scn * s)1729dd9d0cfSchristos elf_ndxscn(Elf_Scn *s)
1739dd9d0cfSchristos {
1749dd9d0cfSchristos 	if (s == NULL) {
1759dd9d0cfSchristos 		LIBELF_SET_ERROR(ARGUMENT, 0);
1769dd9d0cfSchristos 		return (SHN_UNDEF);
1779dd9d0cfSchristos 	}
1789dd9d0cfSchristos 	return (s->s_ndx);
1799dd9d0cfSchristos }
1809dd9d0cfSchristos 
1819dd9d0cfSchristos Elf_Scn *
elf_newscn(Elf * e)1829dd9d0cfSchristos elf_newscn(Elf *e)
1839dd9d0cfSchristos {
1849dd9d0cfSchristos 	int ec;
1859dd9d0cfSchristos 	void *ehdr;
1869dd9d0cfSchristos 	Elf_Scn *scn;
1879dd9d0cfSchristos 
1889dd9d0cfSchristos 	if (e == NULL || e->e_kind != ELF_K_ELF) {
1899dd9d0cfSchristos 		LIBELF_SET_ERROR(ARGUMENT, 0);
1909dd9d0cfSchristos 		return (NULL);
1919dd9d0cfSchristos 	}
1929dd9d0cfSchristos 
1939dd9d0cfSchristos 	if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) {
1949dd9d0cfSchristos 		LIBELF_SET_ERROR(CLASS, 0);
1959dd9d0cfSchristos 		return (NULL);
1969dd9d0cfSchristos 	}
1979dd9d0cfSchristos 
1989dd9d0cfSchristos 	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
1999dd9d0cfSchristos 		return (NULL);
2009dd9d0cfSchristos 
2019dd9d0cfSchristos 	/*
2029dd9d0cfSchristos 	 * The application may be asking for a new section descriptor
2039dd9d0cfSchristos 	 * on an ELF object opened with ELF_C_RDWR or ELF_C_READ.  We
2049dd9d0cfSchristos 	 * need to bring in the existing section information before
2059dd9d0cfSchristos 	 * appending a new one to the list.
2069dd9d0cfSchristos 	 *
2079dd9d0cfSchristos 	 * Per the ELF(3) API, an application is allowed to open a
2089dd9d0cfSchristos 	 * file using ELF_C_READ, mess with its internal structure and
2099dd9d0cfSchristos 	 * use elf_update(...,ELF_C_NULL) to compute its new layout.
2109dd9d0cfSchristos 	 */
2119dd9d0cfSchristos 	if (e->e_cmd != ELF_C_WRITE &&
2129dd9d0cfSchristos 	    (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
2139dd9d0cfSchristos 	    _libelf_load_section_headers(e, ehdr) == 0)
2149dd9d0cfSchristos 		return (NULL);
2159dd9d0cfSchristos 
2169dd9d0cfSchristos 	if (STAILQ_EMPTY(&e->e_u.e_elf.e_scn)) {
2179dd9d0cfSchristos 		assert(e->e_u.e_elf.e_nscn == 0);
2189dd9d0cfSchristos 		if ((scn = _libelf_allocate_scn(e, (size_t) SHN_UNDEF)) ==
2199dd9d0cfSchristos 		    NULL)
2209dd9d0cfSchristos 			return (NULL);
2219dd9d0cfSchristos 		e->e_u.e_elf.e_nscn++;
2229dd9d0cfSchristos 	}
2239dd9d0cfSchristos 
2249dd9d0cfSchristos 	assert(e->e_u.e_elf.e_nscn > 0);
2259dd9d0cfSchristos 
2269dd9d0cfSchristos 	if ((scn = _libelf_allocate_scn(e, e->e_u.e_elf.e_nscn)) == NULL)
2279dd9d0cfSchristos 		return (NULL);
2289dd9d0cfSchristos 
2299dd9d0cfSchristos 	e->e_u.e_elf.e_nscn++;
2309dd9d0cfSchristos 
2319dd9d0cfSchristos 	(void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY);
2329dd9d0cfSchristos 
2339dd9d0cfSchristos 	return (scn);
2349dd9d0cfSchristos }
2359dd9d0cfSchristos 
2369dd9d0cfSchristos Elf_Scn *
elf_nextscn(Elf * e,Elf_Scn * s)2379dd9d0cfSchristos elf_nextscn(Elf *e, Elf_Scn *s)
2389dd9d0cfSchristos {
2399dd9d0cfSchristos 	if (e == NULL || (e->e_kind != ELF_K_ELF) ||
2409dd9d0cfSchristos 	    (s && s->s_elf != e)) {
2419dd9d0cfSchristos 		LIBELF_SET_ERROR(ARGUMENT, 0);
2429dd9d0cfSchristos 		return (NULL);
2439dd9d0cfSchristos 	}
2449dd9d0cfSchristos 
2459dd9d0cfSchristos 	return (s == NULL ? elf_getscn(e, (size_t) 1) :
2469dd9d0cfSchristos 	    STAILQ_NEXT(s, s_next));
2479dd9d0cfSchristos }
248