1999f82afSJohn Marino /* $NetBSD: symtab.c,v 1.2 2013/08/29 15:01:57 christos Exp $ */
2999f82afSJohn Marino
3999f82afSJohn Marino /*-
4999f82afSJohn Marino * Copyright (c) 2012 The NetBSD Foundation, Inc.
5999f82afSJohn Marino * All rights reserved.
6999f82afSJohn Marino *
7999f82afSJohn Marino * This code is derived from software contributed to The NetBSD Foundation
8999f82afSJohn Marino * by Christos Zoulas.
9999f82afSJohn Marino *
10999f82afSJohn Marino * Redistribution and use in source and binary forms, with or without
11999f82afSJohn Marino * modification, are permitted provided that the following conditions
12999f82afSJohn Marino * are met:
13999f82afSJohn Marino * 1. Redistributions of source code must retain the above copyright
14999f82afSJohn Marino * notice, this list of conditions and the following disclaimer.
15999f82afSJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
16999f82afSJohn Marino * notice, this list of conditions and the following disclaimer in the
17999f82afSJohn Marino * documentation and/or other materials provided with the distribution.
18999f82afSJohn Marino *
19999f82afSJohn Marino * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20999f82afSJohn Marino * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21999f82afSJohn Marino * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22999f82afSJohn Marino * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23999f82afSJohn Marino * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24999f82afSJohn Marino * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25999f82afSJohn Marino * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26999f82afSJohn Marino * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27999f82afSJohn Marino * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28999f82afSJohn Marino * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29999f82afSJohn Marino * POSSIBILITY OF SUCH DAMAGE.
30999f82afSJohn Marino */
31999f82afSJohn Marino #include <sys/cdefs.h>
32999f82afSJohn Marino #include <stdlib.h>
33999f82afSJohn Marino #include <stdio.h>
34999f82afSJohn Marino #include <string.h>
35999f82afSJohn Marino #include <stdint.h>
36999f82afSJohn Marino #include <err.h>
37999f82afSJohn Marino #include <dlfcn.h>
38999f82afSJohn Marino
39*77bd33e1Szrj #if 1
40*77bd33e1Szrj #include "private_libelf.h"
41*77bd33e1Szrj #else
42999f82afSJohn Marino #include <libelf.h>
43999f82afSJohn Marino #include <gelf.h>
44*77bd33e1Szrj #endif
45999f82afSJohn Marino #ifndef ELF_ST_BIND
46999f82afSJohn Marino #define ELF_ST_BIND(x) ((x) >> 4)
47999f82afSJohn Marino #endif
48999f82afSJohn Marino #ifndef ELF_ST_TYPE
49999f82afSJohn Marino #define ELF_ST_TYPE(x) (((unsigned int)x) & 0xf)
50999f82afSJohn Marino #endif
51999f82afSJohn Marino
52999f82afSJohn Marino
53999f82afSJohn Marino #include "symtab.h"
54999f82afSJohn Marino
55999f82afSJohn Marino struct symbol {
56999f82afSJohn Marino char *st_name;
57999f82afSJohn Marino uintptr_t st_value;
58999f82afSJohn Marino uintptr_t st_info;
59999f82afSJohn Marino };
60999f82afSJohn Marino
61999f82afSJohn Marino struct symtab {
62999f82afSJohn Marino size_t nsymbols;
63999f82afSJohn Marino struct symbol *symbols;
64999f82afSJohn Marino };
65999f82afSJohn Marino
66999f82afSJohn Marino static int
address_compare(const void * a,const void * b)67999f82afSJohn Marino address_compare(const void *a, const void *b)
68999f82afSJohn Marino {
69999f82afSJohn Marino const struct symbol *sa = a;
70999f82afSJohn Marino const struct symbol *sb = b;
71999f82afSJohn Marino return (int)(intmax_t)(sa->st_value - sb->st_value);
72999f82afSJohn Marino }
73999f82afSJohn Marino
74999f82afSJohn Marino void
symtab_destroy(symtab_t * s)75999f82afSJohn Marino symtab_destroy(symtab_t *s)
76999f82afSJohn Marino {
77999f82afSJohn Marino if (s == NULL)
78999f82afSJohn Marino return;
79999f82afSJohn Marino for (size_t i = 0; i < s->nsymbols; i++)
80999f82afSJohn Marino free(s->symbols[i].st_name);
81999f82afSJohn Marino free(s->symbols);
82999f82afSJohn Marino free(s);
83999f82afSJohn Marino }
84999f82afSJohn Marino
85999f82afSJohn Marino symtab_t *
symtab_create(int fd,int bind,int type)86999f82afSJohn Marino symtab_create(int fd, int bind, int type)
87999f82afSJohn Marino {
88999f82afSJohn Marino Elf *elf;
89999f82afSJohn Marino symtab_t *st;
90999f82afSJohn Marino Elf_Scn *scn = NULL;
91999f82afSJohn Marino
92999f82afSJohn Marino if (elf_version(EV_CURRENT) == EV_NONE) {
93999f82afSJohn Marino warnx("Elf Library is out of date.");
94999f82afSJohn Marino return NULL;
95999f82afSJohn Marino }
96999f82afSJohn Marino
97999f82afSJohn Marino elf = elf_begin(fd, ELF_C_READ, NULL);
98999f82afSJohn Marino if (elf == NULL) {
99999f82afSJohn Marino warnx("Error opening elf file: %s", elf_errmsg(elf_errno()));
100999f82afSJohn Marino return NULL;
101999f82afSJohn Marino }
102999f82afSJohn Marino st = calloc(1, sizeof(*st));
103999f82afSJohn Marino if (st == NULL) {
104999f82afSJohn Marino warnx("Error allocating symbol table");
105999f82afSJohn Marino elf_end(elf);
106999f82afSJohn Marino return NULL;
107999f82afSJohn Marino }
108999f82afSJohn Marino
109999f82afSJohn Marino while ((scn = elf_nextscn(elf, scn)) != NULL) {
110999f82afSJohn Marino GElf_Shdr shdr;
111999f82afSJohn Marino Elf_Data *edata;
112999f82afSJohn Marino size_t ns;
113999f82afSJohn Marino struct symbol *s;
114999f82afSJohn Marino
11561bebfaaSzrj if (gelf_getshdr(scn, &shdr) == NULL)
11661bebfaaSzrj goto out; /* XXX prevent use of uninitialized */
117999f82afSJohn Marino if(shdr.sh_type != SHT_SYMTAB)
118999f82afSJohn Marino continue;
119999f82afSJohn Marino
120999f82afSJohn Marino edata = elf_getdata(scn, NULL);
121999f82afSJohn Marino ns = shdr.sh_size / shdr.sh_entsize;
122999f82afSJohn Marino s = calloc(ns, sizeof(*s));
123999f82afSJohn Marino if (s == NULL) {
124999f82afSJohn Marino warn("Cannot allocate %zu symbols", ns);
125999f82afSJohn Marino goto out;
126999f82afSJohn Marino }
127999f82afSJohn Marino st->symbols = s;
128999f82afSJohn Marino
129999f82afSJohn Marino for (size_t i = 0; i < ns; i++) {
130999f82afSJohn Marino GElf_Sym sym;
13161bebfaaSzrj if (gelf_getsym(edata, (int)i, &sym) == NULL)
13261bebfaaSzrj goto out; /* XXX prevent uninitialized */
133999f82afSJohn Marino
134999f82afSJohn Marino if (bind != -1 &&
135999f82afSJohn Marino (unsigned)bind != ELF_ST_BIND(sym.st_info))
136999f82afSJohn Marino continue;
137999f82afSJohn Marino
138999f82afSJohn Marino if (type != -1 &&
139999f82afSJohn Marino (unsigned)type != ELF_ST_TYPE(sym.st_info))
140999f82afSJohn Marino continue;
141999f82afSJohn Marino
142999f82afSJohn Marino s->st_value = sym.st_value;
143999f82afSJohn Marino s->st_info = sym.st_info;
144999f82afSJohn Marino s->st_name = strdup(
145999f82afSJohn Marino elf_strptr(elf, shdr.sh_link, sym.st_name));
146999f82afSJohn Marino if (s->st_name == NULL)
147999f82afSJohn Marino goto out;
148999f82afSJohn Marino s++;
149999f82afSJohn Marino }
150999f82afSJohn Marino st->nsymbols = s - st->symbols;
151999f82afSJohn Marino if (st->nsymbols == 0) {
152999f82afSJohn Marino warnx("No symbols found");
153999f82afSJohn Marino goto out;
154999f82afSJohn Marino }
155999f82afSJohn Marino qsort(st->symbols, st->nsymbols, sizeof(*st->symbols),
156999f82afSJohn Marino address_compare);
157999f82afSJohn Marino elf_end(elf);
158999f82afSJohn Marino return st;
159999f82afSJohn Marino }
160999f82afSJohn Marino out:
161999f82afSJohn Marino symtab_destroy(st);
162999f82afSJohn Marino elf_end(elf);
163999f82afSJohn Marino return NULL;
164999f82afSJohn Marino }
165999f82afSJohn Marino
166999f82afSJohn Marino
167999f82afSJohn Marino int
symtab_find(const symtab_t * st,const void * p,Dl_info * dli)168999f82afSJohn Marino symtab_find(const symtab_t *st, const void *p, Dl_info *dli)
169999f82afSJohn Marino {
170999f82afSJohn Marino struct symbol *s = st->symbols;
171999f82afSJohn Marino size_t ns = st->nsymbols;
172999f82afSJohn Marino size_t hi = ns;
173999f82afSJohn Marino size_t lo = 0;
174999f82afSJohn Marino size_t mid = ns / 2;
175999f82afSJohn Marino uintptr_t dd, sd, me = (uintptr_t)p;
176999f82afSJohn Marino
177999f82afSJohn Marino for (;;) {
178999f82afSJohn Marino if (s[mid].st_value < me)
179999f82afSJohn Marino lo = mid;
180999f82afSJohn Marino else if (s[mid].st_value > me)
181999f82afSJohn Marino hi = mid;
182999f82afSJohn Marino else
183999f82afSJohn Marino break;
184999f82afSJohn Marino if (hi - lo == 1) {
185999f82afSJohn Marino mid = lo;
186999f82afSJohn Marino break;
187999f82afSJohn Marino }
188999f82afSJohn Marino mid = (hi + lo) / 2;
189999f82afSJohn Marino }
190999f82afSJohn Marino dd = me - (uintptr_t)dli->dli_saddr;
191999f82afSJohn Marino sd = me - s[mid].st_value;
192999f82afSJohn Marino if (dd > sd) {
193999f82afSJohn Marino dli->dli_saddr = (void *)s[mid].st_value;
194999f82afSJohn Marino dli->dli_sname = s[mid].st_name;
195999f82afSJohn Marino }
196999f82afSJohn Marino return 1;
197999f82afSJohn Marino }
198