1 /* $NetBSD: symtab.c,v 1.5 2016/04/20 14:00:16 christos Exp $ */
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: symtab.c,v 1.5 2016/04/20 14:00:16 christos Exp $");
45 #define ELF_ST_BIND(x) ((x) >> 4)
48 #define ELF_ST_TYPE(x) (((unsigned int)x) & 0xf)
61 struct symbol *symbols;
66 address_compare(const void *a, const void *b)
68 const struct symbol *sa = a;
69 const struct symbol *sb = b;
70 return (int)(intmax_t)(sa->st_value - sb->st_value);
74 symtab_destroy(symtab_t *s)
78 for (size_t i = 0; i < s->nsymbols; i++)
79 free(s->symbols[i].st_name);
85 symtab_create(int fd, int bind, int type)
92 if (elf_version(EV_CURRENT) == EV_NONE) {
93 warnx("Elf Library is out of date.");
97 elf = elf_begin(fd, ELF_C_READ, NULL);
99 warnx("Error opening elf file: %s", elf_errmsg(elf_errno()));
102 st = calloc(1, sizeof(*st));
104 warnx("Error allocating symbol table");
108 if (gelf_getehdr(elf, &ehdr) == NULL) {
109 warnx("Error getting ELF Ehdr");
114 st->ispie = ehdr.e_type == ET_DYN;
116 while ((scn = elf_nextscn(elf, scn)) != NULL) {
122 gelf_getshdr(scn, &shdr);
123 if(shdr.sh_type != SHT_SYMTAB)
126 edata = elf_getdata(scn, NULL);
127 ns = shdr.sh_size / shdr.sh_entsize;
128 s = calloc(ns, sizeof(*s));
130 warn("Cannot allocate %zu symbols", ns);
135 for (size_t i = 0; i < ns; i++) {
137 gelf_getsym(edata, (int)i, &sym);
140 fprintf(stderr, "%s: %s@%#jx=%d,%d\n", __func__,
141 elf_strptr(elf, shdr.sh_link, sym.st_name),
142 (uintmax_t)sym.st_value, ELF_ST_BIND(sym.st_info),
143 ELF_ST_TYPE(sym.st_info));
147 (unsigned)bind != ELF_ST_BIND(sym.st_info))
151 (unsigned)type != ELF_ST_TYPE(sym.st_info))
154 s->st_value = sym.st_value;
155 s->st_info = sym.st_info;
157 elf_strptr(elf, shdr.sh_link, sym.st_name));
158 if (s->st_name == NULL)
162 st->nsymbols = s - st->symbols;
163 if (st->nsymbols == 0) {
164 warnx("No symbols found");
167 qsort(st->symbols, st->nsymbols, sizeof(*st->symbols),
180 symtab_find(const symtab_t *st, const void *p, Dl_info *dli)
182 struct symbol *s = st->symbols;
183 size_t ns = st->nsymbols;
187 uintptr_t fbase = st->ispie ? (uintptr_t)dli->dli_fbase : 0;
188 uintptr_t dd, sd, me = (uintptr_t)p - fbase;
189 uintptr_t ad = (uintptr_t)dli->dli_saddr - fbase;
192 fprintf(stderr, "%s: [fbase=%#jx, saddr=%p, me=%#jx ad=%#jx]\n",
193 __func__, (uintmax_t)fbase, dli->dli_saddr, (uintmax_t)me,
197 if (s[mid].st_value < me)
199 else if (s[mid].st_value > me)
210 sd = me - s[mid].st_value;
212 dli->dli_saddr = (void *)s[mid].st_value;
213 dli->dli_sname = s[mid].st_name;
215 fprintf(stderr, "%s: me=%#jx -> [%#jx, %s]\n", __func__,
216 (uintmax_t)me, (uintmax_t)sd, dli->dli_sname);
221 fprintf(stderr, "%s: %#jx -> [%#jx, ***]\n", __func__,
222 (uintmax_t)me, (uintmax_t)sd);