symtab.c revision 1.7 1 /* $NetBSD: symtab.c,v 1.7 2022/06/23 09:53:49 skrll 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.7 2022/06/23 09:53:49 skrll Exp $");
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdint.h>
38 #include <stdbool.h>
39 #include <err.h>
40 #include <dlfcn.h>
41
42 #include <libelf.h>
43 #include <gelf.h>
44 #ifndef ELF_ST_BIND
45 #define ELF_ST_BIND(x) ((x) >> 4)
46 #endif
47 #ifndef ELF_ST_TYPE
48 #define ELF_ST_TYPE(x) (((unsigned int)x) & 0xf)
49 #endif
50
51 #include "symtab.h"
52
53 #ifdef SYMTAB_DEBUG
54 #define DPRINTF(fmt, ...) fprintf(stderr, "%s: " fmt "\n", __func__, __VA_ARGS__)
55 #else
56 #define DPRINTF(fmt, ...)
57 #endif
58
59 struct symbol {
60 char *st_name;
61 uintptr_t st_value;
62 uintptr_t st_info;
63 };
64
65 struct symtab {
66 size_t nsymbols;
67 struct symbol *symbols;
68 bool ispie;
69 };
70
71 static int
72 address_compare(const void *a, const void *b)
73 {
74 const struct symbol *sa = a;
75 const struct symbol *sb = b;
76 return (int)(intmax_t)(sa->st_value - sb->st_value);
77 }
78
79 void
80 symtab_destroy(symtab_t *s)
81 {
82 if (s == NULL)
83 return;
84 for (size_t i = 0; i < s->nsymbols; i++)
85 free(s->symbols[i].st_name);
86 free(s->symbols);
87 free(s);
88 }
89
90 symtab_t *
91 symtab_create(int fd, int bind, int type)
92 {
93 Elf *elf;
94 symtab_t *st;
95 Elf_Scn *scn = NULL;
96 GElf_Ehdr ehdr;
97
98 if (elf_version(EV_CURRENT) == EV_NONE) {
99 warnx("Elf Library is out of date.");
100 return NULL;
101 }
102
103 elf = elf_begin(fd, ELF_C_READ, NULL);
104 if (elf == NULL) {
105 warnx("Error opening elf file: %s", elf_errmsg(elf_errno()));
106 return NULL;
107 }
108 st = calloc(1, sizeof(*st));
109 if (st == NULL) {
110 warnx("Error allocating symbol table");
111 elf_end(elf);
112 return NULL;
113 }
114 if (gelf_getehdr(elf, &ehdr) == NULL) {
115 warnx("Error getting ELF Ehdr");
116 elf_end(elf);
117 return NULL;
118 }
119
120 st->ispie = ehdr.e_type == ET_DYN;
121
122 while ((scn = elf_nextscn(elf, scn)) != NULL) {
123 GElf_Shdr shdr;
124 Elf_Data *edata;
125 size_t ns;
126 struct symbol *s;
127
128 gelf_getshdr(scn, &shdr);
129 if(shdr.sh_type != SHT_SYMTAB)
130 continue;
131
132 edata = elf_getdata(scn, NULL);
133 ns = shdr.sh_size / shdr.sh_entsize;
134 s = calloc(ns, sizeof(*s));
135 if (s == NULL) {
136 warn("Cannot allocate %zu symbols", ns);
137 goto out;
138 }
139 st->symbols = s;
140
141 for (size_t i = 0; i < ns; i++) {
142 GElf_Sym sym;
143 gelf_getsym(edata, (int)i, &sym);
144
145 DPRINTF("%s@%#jx=%d,%d",
146 elf_strptr(elf, shdr.sh_link, sym.st_name),
147 (uintmax_t)sym.st_value, ELF_ST_BIND(sym.st_info),
148 ELF_ST_TYPE(sym.st_info));
149
150 if (bind != -1 &&
151 (unsigned)bind != ELF_ST_BIND(sym.st_info))
152 continue;
153
154 if (type != -1 &&
155 (unsigned)type != ELF_ST_TYPE(sym.st_info))
156 continue;
157
158 s->st_value = sym.st_value;
159 s->st_info = sym.st_info;
160 s->st_name = strdup(
161 elf_strptr(elf, shdr.sh_link, sym.st_name));
162 if (s->st_name == NULL) {
163 warn("Cannot allocate symbol");
164 goto out;
165 }
166 s++;
167 }
168 st->nsymbols = s - st->symbols;
169 if (st->nsymbols == 0) {
170 warnx("No symbols found");
171 goto out;
172 }
173 qsort(st->symbols, st->nsymbols, sizeof(*st->symbols),
174 address_compare);
175 elf_end(elf);
176 return st;
177 }
178 out:
179 symtab_destroy(st);
180 elf_end(elf);
181 return NULL;
182 }
183
184
185 int
186 symtab_find(const symtab_t *st, const void *p, Dl_info *dli)
187 {
188 struct symbol *s = st->symbols;
189 size_t ns = st->nsymbols;
190 size_t hi = ns;
191 size_t lo = 0;
192 size_t mid = ns / 2;
193 uintptr_t fbase = st->ispie ? (uintptr_t)dli->dli_fbase : 0;
194 uintptr_t dd, sd, me = (uintptr_t)p - fbase;
195 uintptr_t ad = (uintptr_t)dli->dli_saddr - fbase;
196
197 DPRINTF("[fbase=%#jx, saddr=%p, sa=%#jx, me=%#jx ad=%#jx]",
198 (uintmax_t)fbase, dli->dli_saddr,
199 (uintmax_t)me, (uintmax_t)ad);
200
201 for (;;) {
202 if (s[mid].st_value < me)
203 lo = mid;
204 else if (s[mid].st_value > me)
205 hi = mid;
206 else
207 break;
208 if (hi - lo == 1) {
209 mid = lo;
210 break;
211 }
212 mid = (hi + lo) / 2;
213 }
214 dd = me - ad;
215 sd = me - s[mid].st_value;
216 if (dd > sd) {
217 dli->dli_saddr = (void *)s[mid].st_value;
218 dli->dli_sname = s[mid].st_name;
219 DPRINTF("me=%#jx -> [%#jx, %s]", (uintmax_t)me, (uintmax_t)sd,
220 dli->dli_sname);
221 } else {
222 DPRINTF("%#jx -> [%#jx, ***]", (uintmax_t)me, (uintmax_t)sd);
223 }
224
225 return 1;
226 }
227