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