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