xref: /netbsd-src/lib/libexecinfo/symtab.c (revision 6c2fafb153b1953f161019ebdcce56474c587ea6)
1*6c2fafb1Srin /*	$NetBSD: symtab.c,v 1.10 2023/08/23 12:24:59 rin Exp $	*/
283d64dcfSchristos 
383d64dcfSchristos /*-
483d64dcfSchristos  * Copyright (c) 2012 The NetBSD Foundation, Inc.
583d64dcfSchristos  * All rights reserved.
683d64dcfSchristos  *
783d64dcfSchristos  * This code is derived from software contributed to The NetBSD Foundation
883d64dcfSchristos  * by Christos Zoulas.
983d64dcfSchristos  *
1083d64dcfSchristos  * Redistribution and use in source and binary forms, with or without
1183d64dcfSchristos  * modification, are permitted provided that the following conditions
1283d64dcfSchristos  * are met:
1383d64dcfSchristos  * 1. Redistributions of source code must retain the above copyright
1483d64dcfSchristos  *    notice, this list of conditions and the following disclaimer.
1583d64dcfSchristos  * 2. Redistributions in binary form must reproduce the above copyright
1683d64dcfSchristos  *    notice, this list of conditions and the following disclaimer in the
1783d64dcfSchristos  *    documentation and/or other materials provided with the distribution.
1883d64dcfSchristos  *
1983d64dcfSchristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2083d64dcfSchristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2183d64dcfSchristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2283d64dcfSchristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2383d64dcfSchristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2483d64dcfSchristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2583d64dcfSchristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2683d64dcfSchristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2783d64dcfSchristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2883d64dcfSchristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2983d64dcfSchristos  * POSSIBILITY OF SUCH DAMAGE.
3083d64dcfSchristos  */
3183d64dcfSchristos #include <sys/cdefs.h>
32*6c2fafb1Srin __RCSID("$NetBSD: symtab.c,v 1.10 2023/08/23 12:24:59 rin Exp $");
3383d64dcfSchristos 
3483d64dcfSchristos #include <stdlib.h>
3583d64dcfSchristos #include <stdio.h>
3683d64dcfSchristos #include <string.h>
37efbe1fcbSchristos #include <stdint.h>
385d9808ecSchristos #include <stdbool.h>
3983d64dcfSchristos #include <err.h>
4083d64dcfSchristos #include <dlfcn.h>
4183d64dcfSchristos 
4283d64dcfSchristos #include <libelf.h>
4383d64dcfSchristos #include <gelf.h>
4483d64dcfSchristos #ifndef ELF_ST_BIND
4583d64dcfSchristos #define ELF_ST_BIND(x)          ((x) >> 4)
4683d64dcfSchristos #endif
4783d64dcfSchristos #ifndef ELF_ST_TYPE
4883d64dcfSchristos #define ELF_ST_TYPE(x)          (((unsigned int)x) & 0xf)
4983d64dcfSchristos #endif
5083d64dcfSchristos 
5144889fb9Sskrll #include "symbol.h"
5283d64dcfSchristos #include "symtab.h"
5383d64dcfSchristos 
5428227ae2Sskrll #ifdef SYMTAB_DEBUG
5528227ae2Sskrll #define DPRINTF(fmt, ...)	fprintf(stderr, "%s: " fmt "\n", __func__, __VA_ARGS__)
5628227ae2Sskrll #else
5728227ae2Sskrll #define DPRINTF(fmt, ...)
5828227ae2Sskrll #endif
5928227ae2Sskrll 
6083d64dcfSchristos struct symbol {
6183d64dcfSchristos 	char *st_name;
6283d64dcfSchristos 	uintptr_t st_value;
6383d64dcfSchristos 	uintptr_t st_info;
6483d64dcfSchristos };
6583d64dcfSchristos 
6683d64dcfSchristos struct symtab {
6783d64dcfSchristos 	size_t nsymbols;
6883d64dcfSchristos 	struct symbol *symbols;
695d9808ecSchristos 	bool ispie;
7083d64dcfSchristos };
7183d64dcfSchristos 
7283d64dcfSchristos static int
address_compare(const void * a,const void * b)7383d64dcfSchristos address_compare(const void *a, const void *b)
7483d64dcfSchristos {
7583d64dcfSchristos 	const struct symbol *sa = a;
7683d64dcfSchristos 	const struct symbol *sb = b;
7783d64dcfSchristos 	return (int)(intmax_t)(sa->st_value - sb->st_value);
7883d64dcfSchristos }
7983d64dcfSchristos 
8083d64dcfSchristos void
symtab_destroy(symtab_t * s)8183d64dcfSchristos symtab_destroy(symtab_t *s)
8283d64dcfSchristos {
8383d64dcfSchristos 	if (s == NULL)
8483d64dcfSchristos 		return;
8583d64dcfSchristos 	for (size_t i = 0; i < s->nsymbols; i++)
8683d64dcfSchristos 		free(s->symbols[i].st_name);
8783d64dcfSchristos 	free(s->symbols);
8883d64dcfSchristos 	free(s);
8983d64dcfSchristos }
9083d64dcfSchristos 
9183d64dcfSchristos symtab_t *
symtab_create(int fd,int bind,int type)9283d64dcfSchristos symtab_create(int fd, int bind, int type)
9383d64dcfSchristos {
9483d64dcfSchristos 	Elf *elf;
9583d64dcfSchristos 	symtab_t *st;
9683d64dcfSchristos 	Elf_Scn *scn = NULL;
975d9808ecSchristos 	GElf_Ehdr ehdr;
9883d64dcfSchristos 
9983d64dcfSchristos 	if (elf_version(EV_CURRENT) == EV_NONE) {
10083d64dcfSchristos 		warnx("Elf Library is out of date.");
10183d64dcfSchristos 		return NULL;
10283d64dcfSchristos 	}
10383d64dcfSchristos 
10483d64dcfSchristos 	elf = elf_begin(fd, ELF_C_READ, NULL);
10583d64dcfSchristos 	if (elf == NULL) {
10683d64dcfSchristos 		warnx("Error opening elf file: %s", elf_errmsg(elf_errno()));
10783d64dcfSchristos 		return NULL;
10883d64dcfSchristos 	}
10983d64dcfSchristos 	st = calloc(1, sizeof(*st));
11083d64dcfSchristos 	if (st == NULL) {
11183d64dcfSchristos 		warnx("Error allocating symbol table");
11283d64dcfSchristos 		elf_end(elf);
11383d64dcfSchristos 		return NULL;
11483d64dcfSchristos 	}
1155d9808ecSchristos 	if (gelf_getehdr(elf, &ehdr) == NULL) {
1165d9808ecSchristos 		warnx("Error getting ELF Ehdr");
1175d9808ecSchristos 		elf_end(elf);
1185d9808ecSchristos 		return NULL;
1195d9808ecSchristos 	}
1205d9808ecSchristos 
1215d9808ecSchristos 	st->ispie = ehdr.e_type == ET_DYN;
12283d64dcfSchristos 
12383d64dcfSchristos 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
12483d64dcfSchristos 		GElf_Shdr shdr;
12583d64dcfSchristos 		Elf_Data *edata;
12683d64dcfSchristos 		size_t ns;
12783d64dcfSchristos 		struct symbol *s;
12883d64dcfSchristos 
12983d64dcfSchristos 		gelf_getshdr(scn, &shdr);
13083d64dcfSchristos 		if(shdr.sh_type != SHT_SYMTAB)
13183d64dcfSchristos 			continue;
13283d64dcfSchristos 
13383d64dcfSchristos 		edata = elf_getdata(scn, NULL);
13483d64dcfSchristos 		ns = shdr.sh_size / shdr.sh_entsize;
13583d64dcfSchristos 		s = calloc(ns, sizeof(*s));
13683d64dcfSchristos 		if (s == NULL) {
13783d64dcfSchristos 			warn("Cannot allocate %zu symbols", ns);
13883d64dcfSchristos 			goto out;
13983d64dcfSchristos 		}
14083d64dcfSchristos 		st->symbols = s;
14183d64dcfSchristos 
14283d64dcfSchristos 		for (size_t i = 0; i < ns; i++) {
14383d64dcfSchristos 			GElf_Sym sym;
14483d64dcfSchristos 			gelf_getsym(edata, (int)i, &sym);
14583d64dcfSchristos 
14628227ae2Sskrll 			DPRINTF("%s@%#jx=%d,%d",
14747c9654dSchristos 			    elf_strptr(elf, shdr.sh_link, sym.st_name),
14847c9654dSchristos 			    (uintmax_t)sym.st_value, ELF_ST_BIND(sym.st_info),
14947c9654dSchristos 			    ELF_ST_TYPE(sym.st_info));
15047c9654dSchristos 
15183d64dcfSchristos 			if (bind != -1 &&
15283d64dcfSchristos 			    (unsigned)bind != ELF_ST_BIND(sym.st_info))
15383d64dcfSchristos 				continue;
15483d64dcfSchristos 
15583d64dcfSchristos 			if (type != -1 &&
15683d64dcfSchristos 			    (unsigned)type != ELF_ST_TYPE(sym.st_info))
15783d64dcfSchristos 				continue;
15883d64dcfSchristos 
15983d64dcfSchristos 			s->st_value = sym.st_value;
16083d64dcfSchristos 			s->st_info = sym.st_info;
16183d64dcfSchristos 			s->st_name = strdup(
16283d64dcfSchristos 			    elf_strptr(elf, shdr.sh_link, sym.st_name));
1633ff21a6fSchristos 			if (s->st_name == NULL) {
1643ff21a6fSchristos 				warn("Cannot allocate symbol");
16583d64dcfSchristos 				goto out;
1663ff21a6fSchristos 			}
16783d64dcfSchristos 			s++;
16883d64dcfSchristos 		}
16983d64dcfSchristos 		st->nsymbols = s - st->symbols;
17083d64dcfSchristos 		if (st->nsymbols == 0) {
17183d64dcfSchristos 			warnx("No symbols found");
17283d64dcfSchristos 			goto out;
17383d64dcfSchristos 		}
17483d64dcfSchristos 		qsort(st->symbols, st->nsymbols, sizeof(*st->symbols),
17583d64dcfSchristos 		    address_compare);
17683d64dcfSchristos 		elf_end(elf);
17783d64dcfSchristos 		return st;
17883d64dcfSchristos 	}
17983d64dcfSchristos out:
18083d64dcfSchristos 	symtab_destroy(st);
18183d64dcfSchristos 	elf_end(elf);
18283d64dcfSchristos 	return NULL;
18383d64dcfSchristos }
18483d64dcfSchristos 
18583d64dcfSchristos 
18683d64dcfSchristos int
symtab_find(const symtab_t * st,const void * p,Dl_info * dli)18783d64dcfSchristos symtab_find(const symtab_t *st, const void *p, Dl_info *dli)
18883d64dcfSchristos {
18983d64dcfSchristos 	struct symbol *s = st->symbols;
19083d64dcfSchristos 	size_t ns = st->nsymbols;
19183d64dcfSchristos 	size_t hi = ns;
19283d64dcfSchristos 	size_t lo = 0;
19383d64dcfSchristos 	size_t mid = ns / 2;
1945d9808ecSchristos 	uintptr_t fbase = st->ispie ? (uintptr_t)dli->dli_fbase : 0;
1955d9808ecSchristos 	uintptr_t dd, sd, me = (uintptr_t)p - fbase;
19644889fb9Sskrll 	uintptr_t sa = SYMBOL_CANONICALIZE(dli->dli_saddr);
19744889fb9Sskrll 	uintptr_t ad = sa - fbase;
19883d64dcfSchristos 
19944889fb9Sskrll 	DPRINTF("[fbase=%#jx, saddr=%p, sa=%#jx, me=%#jx ad=%#jx]",
20044889fb9Sskrll 	    (uintmax_t)fbase, dli->dli_saddr, (uintmax_t)sa,
20128227ae2Sskrll 	    (uintmax_t)me, (uintmax_t)ad);
20228227ae2Sskrll 
20383d64dcfSchristos 	for (;;) {
20483d64dcfSchristos 		if (s[mid].st_value < me)
20583d64dcfSchristos 			lo = mid;
20683d64dcfSchristos 		else if (s[mid].st_value > me)
20783d64dcfSchristos 			hi = mid;
20883d64dcfSchristos 		else
20983d64dcfSchristos 			break;
21083d64dcfSchristos 		if (hi - lo == 1) {
21183d64dcfSchristos 			mid = lo;
21283d64dcfSchristos 			break;
21383d64dcfSchristos 		}
21483d64dcfSchristos 		mid = (hi + lo) / 2;
21583d64dcfSchristos 	}
21647c9654dSchristos 	dd = me - ad;
21783d64dcfSchristos 	sd = me - s[mid].st_value;
21883d64dcfSchristos 	if (dd > sd) {
21983d64dcfSchristos 		dli->dli_saddr = (void *)s[mid].st_value;
22083d64dcfSchristos 		dli->dli_sname = s[mid].st_name;
22128227ae2Sskrll 		DPRINTF("me=%#jx -> [%#jx, %s]", (uintmax_t)me, (uintmax_t)sd,
22228227ae2Sskrll 		    dli->dli_sname);
22328227ae2Sskrll 	} else {
22428227ae2Sskrll 		DPRINTF("%#jx -> [%#jx, ***]", (uintmax_t)me, (uintmax_t)sd);
22583d64dcfSchristos 	}
22628227ae2Sskrll 
22783d64dcfSchristos 	return 1;
22883d64dcfSchristos }
229