xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/libelf_extended.c (revision 5ac3bc719ce6e70593039505b491894133237d12)
1*5ac3bc71Schristos /*	$NetBSD: libelf_extended.c,v 1.5 2024/03/03 17:37:34 christos Exp $	*/
2e81373b4Schristos 
39dd9d0cfSchristos /*-
49dd9d0cfSchristos  * Copyright (c) 2006,2008 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 
359dd9d0cfSchristos #include <assert.h>
369dd9d0cfSchristos #include <libelf.h>
379dd9d0cfSchristos 
389dd9d0cfSchristos #include "_libelf.h"
399dd9d0cfSchristos 
40*5ac3bc71Schristos __RCSID("$NetBSD: libelf_extended.c,v 1.5 2024/03/03 17:37:34 christos Exp $");
41*5ac3bc71Schristos ELFTC_VCSID("Id: libelf_extended.c 3977 2022-05-01 06:45:34Z jkoshy");
429dd9d0cfSchristos 
439dd9d0cfSchristos /*
449dd9d0cfSchristos  * Retrieve section #0, allocating a new section if needed.
459dd9d0cfSchristos  */
469dd9d0cfSchristos static Elf_Scn *
_libelf_getscn0(Elf * e)479dd9d0cfSchristos _libelf_getscn0(Elf *e)
489dd9d0cfSchristos {
499dd9d0cfSchristos 	Elf_Scn *s;
509dd9d0cfSchristos 
519dd9d0cfSchristos 	if ((s = STAILQ_FIRST(&e->e_u.e_elf.e_scn)) != NULL)
529dd9d0cfSchristos 		return (s);
539dd9d0cfSchristos 
549dd9d0cfSchristos 	return (_libelf_allocate_scn(e, (size_t) SHN_UNDEF));
559dd9d0cfSchristos }
569dd9d0cfSchristos 
579dd9d0cfSchristos int
_libelf_setshnum(Elf * e,void * eh,int ec,size_t shnum)589dd9d0cfSchristos _libelf_setshnum(Elf *e, void *eh, int ec, size_t shnum)
599dd9d0cfSchristos {
609dd9d0cfSchristos 	Elf_Scn *scn;
619dd9d0cfSchristos 
629dd9d0cfSchristos 	if (shnum >= SHN_LORESERVE) {
639dd9d0cfSchristos 		if ((scn = _libelf_getscn0(e)) == NULL)
649dd9d0cfSchristos 			return (0);
659dd9d0cfSchristos 
669dd9d0cfSchristos 		assert(scn->s_ndx == SHN_UNDEF);
679dd9d0cfSchristos 
689dd9d0cfSchristos 		if (ec == ELFCLASS32)
69*5ac3bc71Schristos 			scn->s_shdr.s_shdr32.sh_size = (Elf32_Word) shnum;
709dd9d0cfSchristos 		else
719dd9d0cfSchristos 			scn->s_shdr.s_shdr64.sh_size = shnum;
729dd9d0cfSchristos 
739dd9d0cfSchristos 		(void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY);
749dd9d0cfSchristos 
759dd9d0cfSchristos 		shnum = 0;
769dd9d0cfSchristos 	}
779dd9d0cfSchristos 
789dd9d0cfSchristos 	if (ec == ELFCLASS32)
7942bd3019Schristos 		((Elf32_Ehdr *) eh)->e_shnum = shnum & 0xFFFFU;
809dd9d0cfSchristos 	else
8142bd3019Schristos 		((Elf64_Ehdr *) eh)->e_shnum = shnum & 0xFFFFU;
829dd9d0cfSchristos 
839dd9d0cfSchristos 
849dd9d0cfSchristos 	return (1);
859dd9d0cfSchristos }
869dd9d0cfSchristos 
879dd9d0cfSchristos int
_libelf_setshstrndx(Elf * e,void * eh,int ec,size_t shstrndx)889dd9d0cfSchristos _libelf_setshstrndx(Elf *e, void *eh, int ec, size_t shstrndx)
899dd9d0cfSchristos {
909dd9d0cfSchristos 	Elf_Scn *scn;
919dd9d0cfSchristos 
929dd9d0cfSchristos 	if (shstrndx >= SHN_LORESERVE) {
939dd9d0cfSchristos 		if ((scn = _libelf_getscn0(e)) == NULL)
949dd9d0cfSchristos 			return (0);
959dd9d0cfSchristos 
969dd9d0cfSchristos 		assert(scn->s_ndx == SHN_UNDEF);
979dd9d0cfSchristos 
989dd9d0cfSchristos 		if (ec == ELFCLASS32)
99*5ac3bc71Schristos 			scn->s_shdr.s_shdr32.sh_link = (Elf32_Word) shstrndx;
1009dd9d0cfSchristos 		else
101*5ac3bc71Schristos 			scn->s_shdr.s_shdr64.sh_link = (Elf64_Word) shstrndx;
1029dd9d0cfSchristos 
1039dd9d0cfSchristos 		(void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY);
1049dd9d0cfSchristos 
1059dd9d0cfSchristos 		shstrndx = SHN_XINDEX;
1069dd9d0cfSchristos 	}
1079dd9d0cfSchristos 
1089dd9d0cfSchristos 	if (ec == ELFCLASS32)
10942bd3019Schristos 		((Elf32_Ehdr *) eh)->e_shstrndx = shstrndx & 0xFFFFU;
1109dd9d0cfSchristos 	else
11142bd3019Schristos 		((Elf64_Ehdr *) eh)->e_shstrndx = shstrndx & 0xFFFFU;
1129dd9d0cfSchristos 
1139dd9d0cfSchristos 	return (1);
1149dd9d0cfSchristos }
1159dd9d0cfSchristos 
1169dd9d0cfSchristos int
_libelf_setphnum(Elf * e,void * eh,int ec,size_t phnum)1179dd9d0cfSchristos _libelf_setphnum(Elf *e, void *eh, int ec, size_t phnum)
1189dd9d0cfSchristos {
1199dd9d0cfSchristos 	Elf_Scn *scn;
1209dd9d0cfSchristos 
1219dd9d0cfSchristos 	if (phnum >= PN_XNUM) {
1229dd9d0cfSchristos 		if ((scn = _libelf_getscn0(e)) == NULL)
1239dd9d0cfSchristos 			return (0);
1249dd9d0cfSchristos 
1259dd9d0cfSchristos 		assert(scn->s_ndx == SHN_UNDEF);
1269dd9d0cfSchristos 
1279dd9d0cfSchristos 		if (ec == ELFCLASS32)
128*5ac3bc71Schristos 			scn->s_shdr.s_shdr32.sh_info = (Elf32_Word) phnum;
1299dd9d0cfSchristos 		else
130*5ac3bc71Schristos 			scn->s_shdr.s_shdr64.sh_info = (Elf64_Word) phnum;
1319dd9d0cfSchristos 
1329dd9d0cfSchristos 		(void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY);
1339dd9d0cfSchristos 
1349dd9d0cfSchristos 		phnum = PN_XNUM;
1359dd9d0cfSchristos 	}
1369dd9d0cfSchristos 
1379dd9d0cfSchristos 	if (ec == ELFCLASS32)
13842bd3019Schristos 		((Elf32_Ehdr *) eh)->e_phnum = phnum & 0xFFFFU;
1399dd9d0cfSchristos 	else
14042bd3019Schristos 		((Elf64_Ehdr *) eh)->e_phnum = phnum & 0xFFFFU;
1419dd9d0cfSchristos 
1429dd9d0cfSchristos 	return (1);
1439dd9d0cfSchristos }
144