xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/elf_strptr.c (revision 5ac3bc719ce6e70593039505b491894133237d12)
1*5ac3bc71Schristos /*	$NetBSD: elf_strptr.c,v 1.5 2024/03/03 17:37:33 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 
33*5ac3bc71Schristos #include <sys/cdefs.h>
349dd9d0cfSchristos #include <sys/param.h>
359dd9d0cfSchristos 
369dd9d0cfSchristos #include <assert.h>
379dd9d0cfSchristos #include <gelf.h>
389dd9d0cfSchristos 
399dd9d0cfSchristos #include "_libelf.h"
409dd9d0cfSchristos 
41*5ac3bc71Schristos __RCSID("$NetBSD: elf_strptr.c,v 1.5 2024/03/03 17:37:33 christos Exp $");
429dd9d0cfSchristos 
439dd9d0cfSchristos /*
449dd9d0cfSchristos  * Convert an ELF section#,offset pair to a string pointer.
459dd9d0cfSchristos  */
469dd9d0cfSchristos 
479dd9d0cfSchristos char *
elf_strptr(Elf * e,size_t scndx,size_t offset)489dd9d0cfSchristos elf_strptr(Elf *e, size_t scndx, size_t offset)
499dd9d0cfSchristos {
509dd9d0cfSchristos 	Elf_Scn *s;
519dd9d0cfSchristos 	Elf_Data *d;
529dd9d0cfSchristos 	GElf_Shdr shdr;
5342bd3019Schristos 	uint64_t alignment, count;
549dd9d0cfSchristos 
559dd9d0cfSchristos 	if (e == NULL || e->e_kind != ELF_K_ELF) {
569dd9d0cfSchristos 		LIBELF_SET_ERROR(ARGUMENT, 0);
579dd9d0cfSchristos 		return (NULL);
589dd9d0cfSchristos 	}
599dd9d0cfSchristos 
609dd9d0cfSchristos 	if ((s = elf_getscn(e, scndx)) == NULL ||
619dd9d0cfSchristos 	    gelf_getshdr(s, &shdr) == NULL)
629dd9d0cfSchristos 		return (NULL);
639dd9d0cfSchristos 
649dd9d0cfSchristos 	if (shdr.sh_type != SHT_STRTAB ||
659dd9d0cfSchristos 	    offset >= shdr.sh_size) {
669dd9d0cfSchristos 		LIBELF_SET_ERROR(ARGUMENT, 0);
679dd9d0cfSchristos 		return (NULL);
689dd9d0cfSchristos 	}
699dd9d0cfSchristos 
709dd9d0cfSchristos 	d = NULL;
719dd9d0cfSchristos 	if (e->e_flags & ELF_F_LAYOUT) {
729dd9d0cfSchristos 
739dd9d0cfSchristos 		/*
749dd9d0cfSchristos 		 * The application is taking responsibility for the
759dd9d0cfSchristos 		 * ELF object's layout, so we can directly translate
769dd9d0cfSchristos 		 * an offset to a `char *' address using the `d_off'
779dd9d0cfSchristos 		 * members of Elf_Data descriptors.
789dd9d0cfSchristos 		 */
799dd9d0cfSchristos 		while ((d = elf_getdata(s, d)) != NULL) {
809dd9d0cfSchristos 
819dd9d0cfSchristos 			if (d->d_buf == 0 || d->d_size == 0)
829dd9d0cfSchristos 				continue;
839dd9d0cfSchristos 
849dd9d0cfSchristos 			if (d->d_type != ELF_T_BYTE) {
859dd9d0cfSchristos 				LIBELF_SET_ERROR(DATA, 0);
869dd9d0cfSchristos 				return (NULL);
879dd9d0cfSchristos 			}
889dd9d0cfSchristos 
899dd9d0cfSchristos 			if (offset >= d->d_off &&
909dd9d0cfSchristos 			    offset < d->d_off + d->d_size)
919dd9d0cfSchristos 				return ((char *) d->d_buf + offset - d->d_off);
929dd9d0cfSchristos 		}
939dd9d0cfSchristos 	} else {
949dd9d0cfSchristos 		/*
959dd9d0cfSchristos 		 * Otherwise, the `d_off' members are not useable and
969dd9d0cfSchristos 		 * we need to compute offsets ourselves, taking into
979dd9d0cfSchristos 		 * account 'holes' in coverage of the section introduced
989dd9d0cfSchristos 		 * by alignment requirements.
999dd9d0cfSchristos 		 */
10042bd3019Schristos 		count = (uint64_t) 0;	/* cumulative count of bytes seen */
1019dd9d0cfSchristos 		while ((d = elf_getdata(s, d)) != NULL && count <= offset) {
1029dd9d0cfSchristos 
1039dd9d0cfSchristos 			if (d->d_buf == NULL || d->d_size == 0)
1049dd9d0cfSchristos 				continue;
1059dd9d0cfSchristos 
1069dd9d0cfSchristos 			if (d->d_type != ELF_T_BYTE) {
1079dd9d0cfSchristos 				LIBELF_SET_ERROR(DATA, 0);
1089dd9d0cfSchristos 				return (NULL);
1099dd9d0cfSchristos 			}
1109dd9d0cfSchristos 
1119dd9d0cfSchristos 			if ((alignment = d->d_align) > 1) {
1129dd9d0cfSchristos 				if ((alignment & (alignment - 1)) != 0) {
1139dd9d0cfSchristos 					LIBELF_SET_ERROR(DATA, 0);
1149dd9d0cfSchristos 					return (NULL);
1159dd9d0cfSchristos 				}
1169dd9d0cfSchristos 				count = roundup2(count, alignment);
1179dd9d0cfSchristos 			}
1189dd9d0cfSchristos 
1199dd9d0cfSchristos 			if (offset < count) {
1209dd9d0cfSchristos 				/* offset starts in the 'hole' */
1219dd9d0cfSchristos 				LIBELF_SET_ERROR(ARGUMENT, 0);
1229dd9d0cfSchristos 				return (NULL);
1239dd9d0cfSchristos 			}
1249dd9d0cfSchristos 
1259dd9d0cfSchristos 			if (offset < count + d->d_size) {
1269dd9d0cfSchristos 				if (d->d_buf != NULL)
1279dd9d0cfSchristos 					return ((char *) d->d_buf +
1289dd9d0cfSchristos 					    offset - count);
1299dd9d0cfSchristos 				LIBELF_SET_ERROR(DATA, 0);
1309dd9d0cfSchristos 				return (NULL);
1319dd9d0cfSchristos 			}
1329dd9d0cfSchristos 
1339dd9d0cfSchristos 			count += d->d_size;
1349dd9d0cfSchristos 		}
1359dd9d0cfSchristos 	}
1369dd9d0cfSchristos 
1379dd9d0cfSchristos 	LIBELF_SET_ERROR(ARGUMENT, 0);
1389dd9d0cfSchristos 	return (NULL);
1399dd9d0cfSchristos }
140