xref: /minix/lib/libexecinfo/symtab.c (revision 84d9c625)
1 /*	$NetBSD: symtab.c,v 1.3 2013/09/03 08:44:45 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: symtab.c,v 1.3 2013/09/03 08:44:45 christos Exp $");
33 
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdint.h>
38 #include <err.h>
39 #include <dlfcn.h>
40 
41 #include <libelf.h>
42 #include <gelf.h>
43 #ifndef ELF_ST_BIND
44 #define ELF_ST_BIND(x)          ((x) >> 4)
45 #endif
46 #ifndef ELF_ST_TYPE
47 #define ELF_ST_TYPE(x)          (((unsigned int)x) & 0xf)
48 #endif
49 
50 
51 #include "symtab.h"
52 
53 struct symbol {
54 	char *st_name;
55 	uintptr_t st_value;
56 	uintptr_t st_info;
57 };
58 
59 struct symtab {
60 	size_t nsymbols;
61 	struct symbol *symbols;
62 };
63 
64 static int
address_compare(const void * a,const void * b)65 address_compare(const void *a, const void *b)
66 {
67 	const struct symbol *sa = a;
68 	const struct symbol *sb = b;
69 	return (int)(intmax_t)(sa->st_value - sb->st_value);
70 }
71 
72 void
symtab_destroy(symtab_t * s)73 symtab_destroy(symtab_t *s)
74 {
75 	if (s == NULL)
76 		return;
77 	for (size_t i = 0; i < s->nsymbols; i++)
78 		free(s->symbols[i].st_name);
79 	free(s->symbols);
80 	free(s);
81 }
82 
83 symtab_t *
symtab_create(int fd,int bind,int type)84 symtab_create(int fd, int bind, int type)
85 {
86 	Elf *elf;
87 	symtab_t *st;
88 	Elf_Scn *scn = NULL;
89 
90 	if (elf_version(EV_CURRENT) == EV_NONE) {
91 		warnx("Elf Library is out of date.");
92 		return NULL;
93 	}
94 
95 	elf = elf_begin(fd, ELF_C_READ, NULL);
96 	if (elf == NULL) {
97 		warnx("Error opening elf file: %s", elf_errmsg(elf_errno()));
98 		return NULL;
99 	}
100 	st = calloc(1, sizeof(*st));
101 	if (st == NULL) {
102 		warnx("Error allocating symbol table");
103 		elf_end(elf);
104 		return NULL;
105 	}
106 
107 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
108 		GElf_Shdr shdr;
109 		Elf_Data *edata;
110 		size_t ns;
111 		struct symbol *s;
112 
113 		gelf_getshdr(scn, &shdr);
114 		if(shdr.sh_type != SHT_SYMTAB)
115 			continue;
116 
117 		edata = elf_getdata(scn, NULL);
118 		ns = shdr.sh_size / shdr.sh_entsize;
119 		s = calloc(ns, sizeof(*s));
120 		if (s == NULL) {
121 			warn("Cannot allocate %zu symbols", ns);
122 			goto out;
123 		}
124 		st->symbols = s;
125 
126 		for (size_t i = 0; i < ns; i++) {
127 			GElf_Sym sym;
128                         gelf_getsym(edata, (int)i, &sym);
129 
130 			if (bind != -1 &&
131 			    (unsigned)bind != ELF_ST_BIND(sym.st_info))
132 				continue;
133 
134 			if (type != -1 &&
135 			    (unsigned)type != ELF_ST_TYPE(sym.st_info))
136 				continue;
137 
138 			s->st_value = sym.st_value;
139 			s->st_info = sym.st_info;
140 			s->st_name = strdup(
141 			    elf_strptr(elf, shdr.sh_link, sym.st_name));
142 			if (s->st_name == NULL) {
143 				warn("Cannot allocate symbol");
144 				goto out;
145 			}
146 			s++;
147                 }
148 		st->nsymbols = s - st->symbols;
149 		if (st->nsymbols == 0) {
150 			warnx("No symbols found");
151 			goto out;
152 		}
153 		qsort(st->symbols, st->nsymbols, sizeof(*st->symbols),
154 		    address_compare);
155 		elf_end(elf);
156 		return st;
157 	}
158 out:
159 	symtab_destroy(st);
160 	elf_end(elf);
161 	return NULL;
162 }
163 
164 
165 int
symtab_find(const symtab_t * st,const void * p,Dl_info * dli)166 symtab_find(const symtab_t *st, const void *p, Dl_info *dli)
167 {
168 	struct symbol *s = st->symbols;
169 	size_t ns = st->nsymbols;
170 	size_t hi = ns;
171 	size_t lo = 0;
172 	size_t mid = ns / 2;
173 	uintptr_t dd, sd, me = (uintptr_t)p;
174 
175 	for (;;) {
176 		if (s[mid].st_value < me)
177 			lo = mid;
178 		else if (s[mid].st_value > me)
179 			hi = mid;
180 		else
181 			break;
182 		if (hi - lo == 1) {
183 			mid = lo;
184 			break;
185 		}
186 		mid = (hi + lo) / 2;
187 	}
188 	dd = me - (uintptr_t)dli->dli_saddr;
189 	sd = me - s[mid].st_value;
190 	if (dd > sd) {
191 		dli->dli_saddr = (void *)s[mid].st_value;
192 		dli->dli_sname = s[mid].st_name;
193 	}
194 	return 1;
195 }
196