xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/gelf_shdr.c (revision 5ac3bc719ce6e70593039505b491894133237d12)
1*5ac3bc71Schristos /*	$NetBSD: gelf_shdr.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 <gelf.h>
379dd9d0cfSchristos #include <libelf.h>
389dd9d0cfSchristos #include <limits.h>
3942bd3019Schristos #include <stdint.h>
409dd9d0cfSchristos 
419dd9d0cfSchristos #include "_libelf.h"
429dd9d0cfSchristos 
43*5ac3bc71Schristos __RCSID("$NetBSD: gelf_shdr.c,v 1.5 2024/03/03 17:37:34 christos Exp $");
44*5ac3bc71Schristos ELFTC_VCSID("Id: gelf_shdr.c 3977 2022-05-01 06:45:34Z jkoshy");
459dd9d0cfSchristos 
469dd9d0cfSchristos Elf32_Shdr *
elf32_getshdr(Elf_Scn * s)479dd9d0cfSchristos elf32_getshdr(Elf_Scn *s)
489dd9d0cfSchristos {
499dd9d0cfSchristos 	return (_libelf_getshdr(s, ELFCLASS32));
509dd9d0cfSchristos }
519dd9d0cfSchristos 
529dd9d0cfSchristos Elf64_Shdr *
elf64_getshdr(Elf_Scn * s)539dd9d0cfSchristos elf64_getshdr(Elf_Scn *s)
549dd9d0cfSchristos {
559dd9d0cfSchristos 	return (_libelf_getshdr(s, ELFCLASS64));
569dd9d0cfSchristos }
579dd9d0cfSchristos 
589dd9d0cfSchristos GElf_Shdr *
gelf_getshdr(Elf_Scn * s,GElf_Shdr * d)599dd9d0cfSchristos gelf_getshdr(Elf_Scn *s, GElf_Shdr *d)
609dd9d0cfSchristos {
619dd9d0cfSchristos 	int ec;
629dd9d0cfSchristos 	void *sh;
639dd9d0cfSchristos 	Elf32_Shdr *sh32;
649dd9d0cfSchristos 	Elf64_Shdr *sh64;
659dd9d0cfSchristos 
669dd9d0cfSchristos 	if (d == NULL) {
679dd9d0cfSchristos 		LIBELF_SET_ERROR(ARGUMENT, 0);
689dd9d0cfSchristos 		return (NULL);
699dd9d0cfSchristos 	}
709dd9d0cfSchristos 
719dd9d0cfSchristos 	if ((sh = _libelf_getshdr(s, ELFCLASSNONE)) == NULL)
729dd9d0cfSchristos 		return (NULL);
739dd9d0cfSchristos 
749dd9d0cfSchristos 	ec = s->s_elf->e_class;
759dd9d0cfSchristos 	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
769dd9d0cfSchristos 
779dd9d0cfSchristos 	if (ec == ELFCLASS32) {
789dd9d0cfSchristos 		sh32 = (Elf32_Shdr *) sh;
799dd9d0cfSchristos 
809dd9d0cfSchristos 		d->sh_name      = sh32->sh_name;
819dd9d0cfSchristos 		d->sh_type      = sh32->sh_type;
829dd9d0cfSchristos 		d->sh_flags     = (Elf64_Xword) sh32->sh_flags;
839dd9d0cfSchristos 		d->sh_addr      = (Elf64_Addr) sh32->sh_addr;
849dd9d0cfSchristos 		d->sh_offset    = (Elf64_Off) sh32->sh_offset;
859dd9d0cfSchristos 		d->sh_size      = (Elf64_Xword) sh32->sh_size;
869dd9d0cfSchristos 		d->sh_link      = sh32->sh_link;
879dd9d0cfSchristos 		d->sh_info      = sh32->sh_info;
889dd9d0cfSchristos 		d->sh_addralign = (Elf64_Xword) sh32->sh_addralign;
899dd9d0cfSchristos 		d->sh_entsize   = (Elf64_Xword) sh32->sh_entsize;
909dd9d0cfSchristos 	} else {
919dd9d0cfSchristos 		sh64 = (Elf64_Shdr *) sh;
929dd9d0cfSchristos 		*d = *sh64;
939dd9d0cfSchristos 	}
949dd9d0cfSchristos 
959dd9d0cfSchristos 	return (d);
969dd9d0cfSchristos }
979dd9d0cfSchristos 
989dd9d0cfSchristos int
gelf_update_shdr(Elf_Scn * scn,GElf_Shdr * s)999dd9d0cfSchristos gelf_update_shdr(Elf_Scn *scn, GElf_Shdr *s)
1009dd9d0cfSchristos {
1019dd9d0cfSchristos 	int ec;
1029dd9d0cfSchristos 	Elf *e;
1039dd9d0cfSchristos 	Elf32_Shdr *sh32;
1049dd9d0cfSchristos 
1059dd9d0cfSchristos 
1069dd9d0cfSchristos 	if (s == NULL || scn == NULL || (e = scn->s_elf) == NULL ||
1079dd9d0cfSchristos 	    e->e_kind != ELF_K_ELF ||
1089dd9d0cfSchristos 	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
1099dd9d0cfSchristos 		LIBELF_SET_ERROR(ARGUMENT, 0);
1109dd9d0cfSchristos 		return (0);
1119dd9d0cfSchristos 	}
1129dd9d0cfSchristos 
1139dd9d0cfSchristos 	if (e->e_cmd == ELF_C_READ) {
1149dd9d0cfSchristos 		LIBELF_SET_ERROR(MODE, 0);
1159dd9d0cfSchristos 		return (0);
1169dd9d0cfSchristos 	}
1179dd9d0cfSchristos 
1189dd9d0cfSchristos 	(void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY);
1199dd9d0cfSchristos 
1209dd9d0cfSchristos 	if (ec == ELFCLASS64) {
1219dd9d0cfSchristos 		scn->s_shdr.s_shdr64 = *s;
1229dd9d0cfSchristos 		return (1);
1239dd9d0cfSchristos 	}
1249dd9d0cfSchristos 
1259dd9d0cfSchristos 	sh32 = &scn->s_shdr.s_shdr32;
1269dd9d0cfSchristos 
1279dd9d0cfSchristos 	sh32->sh_name	 =  s->sh_name;
1289dd9d0cfSchristos 	sh32->sh_type	 =  s->sh_type;
1299dd9d0cfSchristos 	LIBELF_COPY_U32(sh32, s, sh_flags);
1309dd9d0cfSchristos 	LIBELF_COPY_U32(sh32, s, sh_addr);
1319dd9d0cfSchristos 	LIBELF_COPY_U32(sh32, s, sh_offset);
1329dd9d0cfSchristos 	LIBELF_COPY_U32(sh32, s, sh_size);
1339dd9d0cfSchristos 	sh32->sh_link	 =  s->sh_link;
1349dd9d0cfSchristos 	sh32->sh_info	 =  s->sh_info;
1359dd9d0cfSchristos 	LIBELF_COPY_U32(sh32, s, sh_addralign);
1369dd9d0cfSchristos 	LIBELF_COPY_U32(sh32, s, sh_entsize);
1379dd9d0cfSchristos 
1389dd9d0cfSchristos 	return (1);
1399dd9d0cfSchristos }
140