xref: /freebsd/contrib/libexecinfo/symtab.c (revision 499fe48de8938d4c7b0a91e20eb6c16db9d55633)
1*499fe48dSConrad Meyer /*	$NetBSD: symtab.c,v 1.5 2016/04/20 14:00:16 christos Exp $	*/
2287472b3SEd Maste 
3287472b3SEd Maste /*-
4287472b3SEd Maste  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5287472b3SEd Maste  * All rights reserved.
6287472b3SEd Maste  *
7287472b3SEd Maste  * This code is derived from software contributed to The NetBSD Foundation
8287472b3SEd Maste  * by Christos Zoulas.
9287472b3SEd Maste  *
10287472b3SEd Maste  * Redistribution and use in source and binary forms, with or without
11287472b3SEd Maste  * modification, are permitted provided that the following conditions
12287472b3SEd Maste  * are met:
13287472b3SEd Maste  * 1. Redistributions of source code must retain the above copyright
14287472b3SEd Maste  *    notice, this list of conditions and the following disclaimer.
15287472b3SEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
16287472b3SEd Maste  *    notice, this list of conditions and the following disclaimer in the
17287472b3SEd Maste  *    documentation and/or other materials provided with the distribution.
18287472b3SEd Maste  *
19287472b3SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20287472b3SEd Maste  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21287472b3SEd Maste  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22287472b3SEd Maste  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23287472b3SEd Maste  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24287472b3SEd Maste  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25287472b3SEd Maste  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26287472b3SEd Maste  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27287472b3SEd Maste  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28287472b3SEd Maste  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29287472b3SEd Maste  * POSSIBILITY OF SUCH DAMAGE.
30287472b3SEd Maste  */
31287472b3SEd Maste #include <sys/cdefs.h>
32*499fe48dSConrad Meyer __RCSID("$NetBSD: symtab.c,v 1.5 2016/04/20 14:00:16 christos Exp $");
33287472b3SEd Maste 
34287472b3SEd Maste #include <stdlib.h>
35287472b3SEd Maste #include <stdio.h>
36287472b3SEd Maste #include <string.h>
37f357c00bSEd Maste #include <stdint.h>
38*499fe48dSConrad Meyer #include <stdbool.h>
39287472b3SEd Maste #include <err.h>
40287472b3SEd Maste #include <dlfcn.h>
41287472b3SEd Maste 
42287472b3SEd Maste #include <libelf.h>
43287472b3SEd Maste #include <gelf.h>
44287472b3SEd Maste #ifndef ELF_ST_BIND
45287472b3SEd Maste #define ELF_ST_BIND(x)          ((x) >> 4)
46287472b3SEd Maste #endif
47287472b3SEd Maste #ifndef ELF_ST_TYPE
48287472b3SEd Maste #define ELF_ST_TYPE(x)          (((unsigned int)x) & 0xf)
49287472b3SEd Maste #endif
50287472b3SEd Maste 
51287472b3SEd Maste #include "symtab.h"
52287472b3SEd Maste 
53287472b3SEd Maste struct symbol {
54287472b3SEd Maste 	char *st_name;
55287472b3SEd Maste 	uintptr_t st_value;
56287472b3SEd Maste 	uintptr_t st_info;
57287472b3SEd Maste };
58287472b3SEd Maste 
59287472b3SEd Maste struct symtab {
60287472b3SEd Maste 	size_t nsymbols;
61287472b3SEd Maste 	struct symbol *symbols;
62*499fe48dSConrad Meyer 	bool ispie;
63287472b3SEd Maste };
64287472b3SEd Maste 
65287472b3SEd Maste static int
address_compare(const void * a,const void * b)66287472b3SEd Maste address_compare(const void *a, const void *b)
67287472b3SEd Maste {
68287472b3SEd Maste 	const struct symbol *sa = a;
69287472b3SEd Maste 	const struct symbol *sb = b;
70287472b3SEd Maste 	return (int)(intmax_t)(sa->st_value - sb->st_value);
71287472b3SEd Maste }
72287472b3SEd Maste 
73287472b3SEd Maste void
symtab_destroy(symtab_t * s)74287472b3SEd Maste symtab_destroy(symtab_t *s)
75287472b3SEd Maste {
76287472b3SEd Maste 	if (s == NULL)
77287472b3SEd Maste 		return;
78287472b3SEd Maste 	for (size_t i = 0; i < s->nsymbols; i++)
79287472b3SEd Maste 		free(s->symbols[i].st_name);
80287472b3SEd Maste 	free(s->symbols);
81287472b3SEd Maste 	free(s);
82287472b3SEd Maste }
83287472b3SEd Maste 
84287472b3SEd Maste symtab_t *
symtab_create(int fd,int bind,int type)85287472b3SEd Maste symtab_create(int fd, int bind, int type)
86287472b3SEd Maste {
87287472b3SEd Maste 	Elf *elf;
88287472b3SEd Maste 	symtab_t *st;
89287472b3SEd Maste 	Elf_Scn *scn = NULL;
90*499fe48dSConrad Meyer 	GElf_Ehdr ehdr;
91287472b3SEd Maste 
92287472b3SEd Maste 	if (elf_version(EV_CURRENT) == EV_NONE) {
93287472b3SEd Maste 		warnx("Elf Library is out of date.");
94287472b3SEd Maste 		return NULL;
95287472b3SEd Maste 	}
96287472b3SEd Maste 
97287472b3SEd Maste 	elf = elf_begin(fd, ELF_C_READ, NULL);
98287472b3SEd Maste 	if (elf == NULL) {
99287472b3SEd Maste 		warnx("Error opening elf file: %s", elf_errmsg(elf_errno()));
100287472b3SEd Maste 		return NULL;
101287472b3SEd Maste 	}
102287472b3SEd Maste 	st = calloc(1, sizeof(*st));
103287472b3SEd Maste 	if (st == NULL) {
104287472b3SEd Maste 		warnx("Error allocating symbol table");
105287472b3SEd Maste 		elf_end(elf);
106287472b3SEd Maste 		return NULL;
107287472b3SEd Maste 	}
108*499fe48dSConrad Meyer 	if (gelf_getehdr(elf, &ehdr) == NULL) {
109*499fe48dSConrad Meyer 		warnx("Error getting ELF Ehdr");
110*499fe48dSConrad Meyer 		elf_end(elf);
111*499fe48dSConrad Meyer 		return NULL;
112*499fe48dSConrad Meyer 	}
113*499fe48dSConrad Meyer 
114*499fe48dSConrad Meyer 	st->ispie = ehdr.e_type == ET_DYN;
115287472b3SEd Maste 
116287472b3SEd Maste 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
117287472b3SEd Maste 		GElf_Shdr shdr;
118287472b3SEd Maste 		Elf_Data *edata;
119287472b3SEd Maste 		size_t ns;
120287472b3SEd Maste 		struct symbol *s;
121287472b3SEd Maste 
122287472b3SEd Maste 		gelf_getshdr(scn, &shdr);
123287472b3SEd Maste 		if(shdr.sh_type != SHT_SYMTAB)
124287472b3SEd Maste 			continue;
125287472b3SEd Maste 
126287472b3SEd Maste 		edata = elf_getdata(scn, NULL);
127287472b3SEd Maste 		ns = shdr.sh_size / shdr.sh_entsize;
128287472b3SEd Maste 		s = calloc(ns, sizeof(*s));
129287472b3SEd Maste 		if (s == NULL) {
130287472b3SEd Maste 			warn("Cannot allocate %zu symbols", ns);
131287472b3SEd Maste 			goto out;
132287472b3SEd Maste 		}
133287472b3SEd Maste 		st->symbols = s;
134287472b3SEd Maste 
135287472b3SEd Maste 		for (size_t i = 0; i < ns; i++) {
136287472b3SEd Maste 			GElf_Sym sym;
137287472b3SEd Maste                         gelf_getsym(edata, (int)i, &sym);
138287472b3SEd Maste 
139*499fe48dSConrad Meyer #ifdef SYMTAB_DEBUG
140*499fe48dSConrad Meyer 			fprintf(stderr, "%s: %s@%#jx=%d,%d\n", __func__,
141*499fe48dSConrad Meyer 			    elf_strptr(elf, shdr.sh_link, sym.st_name),
142*499fe48dSConrad Meyer 			    (uintmax_t)sym.st_value, ELF_ST_BIND(sym.st_info),
143*499fe48dSConrad Meyer 			    ELF_ST_TYPE(sym.st_info));
144*499fe48dSConrad Meyer #endif
145*499fe48dSConrad Meyer 
146287472b3SEd Maste 			if (bind != -1 &&
147287472b3SEd Maste 			    (unsigned)bind != ELF_ST_BIND(sym.st_info))
148287472b3SEd Maste 				continue;
149287472b3SEd Maste 
150287472b3SEd Maste 			if (type != -1 &&
151287472b3SEd Maste 			    (unsigned)type != ELF_ST_TYPE(sym.st_info))
152287472b3SEd Maste 				continue;
153287472b3SEd Maste 
154287472b3SEd Maste 			s->st_value = sym.st_value;
155287472b3SEd Maste 			s->st_info = sym.st_info;
156287472b3SEd Maste 			s->st_name = strdup(
157287472b3SEd Maste 			    elf_strptr(elf, shdr.sh_link, sym.st_name));
158287472b3SEd Maste 			if (s->st_name == NULL)
159287472b3SEd Maste 				goto out;
160287472b3SEd Maste 			s++;
161287472b3SEd Maste                 }
162287472b3SEd Maste 		st->nsymbols = s - st->symbols;
163287472b3SEd Maste 		if (st->nsymbols == 0) {
164287472b3SEd Maste 			warnx("No symbols found");
165287472b3SEd Maste 			goto out;
166287472b3SEd Maste 		}
167287472b3SEd Maste 		qsort(st->symbols, st->nsymbols, sizeof(*st->symbols),
168287472b3SEd Maste 		    address_compare);
169287472b3SEd Maste 		elf_end(elf);
170287472b3SEd Maste 		return st;
171287472b3SEd Maste 	}
172287472b3SEd Maste out:
173287472b3SEd Maste 	symtab_destroy(st);
174287472b3SEd Maste 	elf_end(elf);
175287472b3SEd Maste 	return NULL;
176287472b3SEd Maste }
177287472b3SEd Maste 
178287472b3SEd Maste 
179287472b3SEd Maste int
symtab_find(const symtab_t * st,const void * p,Dl_info * dli)180287472b3SEd Maste symtab_find(const symtab_t *st, const void *p, Dl_info *dli)
181287472b3SEd Maste {
182287472b3SEd Maste 	struct symbol *s = st->symbols;
183287472b3SEd Maste 	size_t ns = st->nsymbols;
184287472b3SEd Maste 	size_t hi = ns;
185287472b3SEd Maste 	size_t lo = 0;
186287472b3SEd Maste 	size_t mid = ns / 2;
187*499fe48dSConrad Meyer 	uintptr_t fbase = st->ispie ? (uintptr_t)dli->dli_fbase : 0;
188*499fe48dSConrad Meyer 	uintptr_t dd, sd, me = (uintptr_t)p - fbase;
189*499fe48dSConrad Meyer 	uintptr_t ad = (uintptr_t)dli->dli_saddr - fbase;
190287472b3SEd Maste 
191*499fe48dSConrad Meyer #ifdef SYMTAB_DEBUG
192*499fe48dSConrad Meyer 	fprintf(stderr, "%s: [fbase=%#jx, saddr=%p, me=%#jx ad=%#jx]\n",
193*499fe48dSConrad Meyer 	    __func__, (uintmax_t)fbase, dli->dli_saddr, (uintmax_t)me,
194*499fe48dSConrad Meyer 	    (uintmax_t)ad);
195*499fe48dSConrad Meyer #endif
196287472b3SEd Maste 	for (;;) {
197287472b3SEd Maste 		if (s[mid].st_value < me)
198287472b3SEd Maste 			lo = mid;
199287472b3SEd Maste 		else if (s[mid].st_value > me)
200287472b3SEd Maste 			hi = mid;
201287472b3SEd Maste 		else
202287472b3SEd Maste 			break;
203287472b3SEd Maste 		if (hi - lo == 1) {
204287472b3SEd Maste 			mid = lo;
205287472b3SEd Maste 			break;
206287472b3SEd Maste 		}
207287472b3SEd Maste 		mid = (hi + lo) / 2;
208287472b3SEd Maste 	}
209*499fe48dSConrad Meyer 	dd = me - ad;
210287472b3SEd Maste 	sd = me - s[mid].st_value;
211287472b3SEd Maste 	if (dd > sd) {
212287472b3SEd Maste 		dli->dli_saddr = (void *)s[mid].st_value;
213287472b3SEd Maste 		dli->dli_sname = s[mid].st_name;
214*499fe48dSConrad Meyer #ifdef SYMTAB_DEBUG
215*499fe48dSConrad Meyer 		fprintf(stderr, "%s: me=%#jx -> [%#jx, %s]\n", __func__,
216*499fe48dSConrad Meyer 		    (uintmax_t)me, (uintmax_t)sd, dli->dli_sname);
217*499fe48dSConrad Meyer #endif
218287472b3SEd Maste 	}
219*499fe48dSConrad Meyer #ifdef SYMTAB_DEBUG
220*499fe48dSConrad Meyer 	else
221*499fe48dSConrad Meyer 		fprintf(stderr, "%s: %#jx -> [%#jx, ***]\n", __func__,
222*499fe48dSConrad Meyer 		    (uintmax_t)me, (uintmax_t)sd);
223*499fe48dSConrad Meyer #endif
224287472b3SEd Maste 	return 1;
225287472b3SEd Maste }
226