xref: /dragonfly/lib/libexecinfo/symtab.c (revision 77bd33e17033047707557baf11c4799f1d4a3110)
1 /*        $NetBSD: symtab.c,v 1.2 2013/08/29 15:01:57 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 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdint.h>
36 #include <err.h>
37 #include <dlfcn.h>
38 
39 #if 1
40 #include "private_libelf.h"
41 #else
42 #include <libelf.h>
43 #include <gelf.h>
44 #endif
45 #ifndef ELF_ST_BIND
46 #define ELF_ST_BIND(x)          ((x) >> 4)
47 #endif
48 #ifndef ELF_ST_TYPE
49 #define ELF_ST_TYPE(x)          (((unsigned int)x) & 0xf)
50 #endif
51 
52 
53 #include "symtab.h"
54 
55 struct symbol {
56           char *st_name;
57           uintptr_t st_value;
58           uintptr_t st_info;
59 };
60 
61 struct symtab {
62           size_t nsymbols;
63           struct symbol *symbols;
64 };
65 
66 static int
address_compare(const void * a,const void * b)67 address_compare(const void *a, const void *b)
68 {
69           const struct symbol *sa = a;
70           const struct symbol *sb = b;
71           return (int)(intmax_t)(sa->st_value - sb->st_value);
72 }
73 
74 void
symtab_destroy(symtab_t * s)75 symtab_destroy(symtab_t *s)
76 {
77           if (s == NULL)
78                     return;
79           for (size_t i = 0; i < s->nsymbols; i++)
80                     free(s->symbols[i].st_name);
81           free(s->symbols);
82           free(s);
83 }
84 
85 symtab_t *
symtab_create(int fd,int bind,int type)86 symtab_create(int fd, int bind, int type)
87 {
88           Elf *elf;
89           symtab_t *st;
90           Elf_Scn *scn = NULL;
91 
92           if (elf_version(EV_CURRENT) == EV_NONE) {
93                     warnx("Elf Library is out of date.");
94                     return NULL;
95           }
96 
97           elf = elf_begin(fd, ELF_C_READ, NULL);
98           if (elf == NULL) {
99                     warnx("Error opening elf file: %s", elf_errmsg(elf_errno()));
100                     return NULL;
101           }
102           st = calloc(1, sizeof(*st));
103           if (st == NULL) {
104                     warnx("Error allocating symbol table");
105                     elf_end(elf);
106                     return NULL;
107           }
108 
109           while ((scn = elf_nextscn(elf, scn)) != NULL) {
110                     GElf_Shdr shdr;
111                     Elf_Data *edata;
112                     size_t ns;
113                     struct symbol *s;
114 
115                     if (gelf_getshdr(scn, &shdr) == NULL)
116                               goto out; /* XXX prevent use of uninitialized */
117                     if(shdr.sh_type != SHT_SYMTAB)
118                               continue;
119 
120                     edata = elf_getdata(scn, NULL);
121                     ns = shdr.sh_size / shdr.sh_entsize;
122                     s = calloc(ns, sizeof(*s));
123                     if (s == NULL) {
124                               warn("Cannot allocate %zu symbols", ns);
125                               goto out;
126                     }
127                     st->symbols = s;
128 
129                     for (size_t i = 0; i < ns; i++) {
130                               GElf_Sym sym;
131                               if (gelf_getsym(edata, (int)i, &sym) == NULL)
132                                         goto out; /* XXX prevent uninitialized */
133 
134                               if (bind != -1 &&
135                                   (unsigned)bind != ELF_ST_BIND(sym.st_info))
136                                         continue;
137 
138                               if (type != -1 &&
139                                   (unsigned)type != ELF_ST_TYPE(sym.st_info))
140                                         continue;
141 
142                               s->st_value = sym.st_value;
143                               s->st_info = sym.st_info;
144                               s->st_name = strdup(
145                                   elf_strptr(elf, shdr.sh_link, sym.st_name));
146                               if (s->st_name == NULL)
147                                         goto out;
148                               s++;
149                 }
150                     st->nsymbols = s - st->symbols;
151                     if (st->nsymbols == 0) {
152                               warnx("No symbols found");
153                               goto out;
154                     }
155                     qsort(st->symbols, st->nsymbols, sizeof(*st->symbols),
156                         address_compare);
157                     elf_end(elf);
158                     return st;
159           }
160 out:
161           symtab_destroy(st);
162           elf_end(elf);
163           return NULL;
164 }
165 
166 
167 int
symtab_find(const symtab_t * st,const void * p,Dl_info * dli)168 symtab_find(const symtab_t *st, const void *p, Dl_info *dli)
169 {
170           struct symbol *s = st->symbols;
171           size_t ns = st->nsymbols;
172           size_t hi = ns;
173           size_t lo = 0;
174           size_t mid = ns / 2;
175           uintptr_t dd, sd, me = (uintptr_t)p;
176 
177           for (;;) {
178                     if (s[mid].st_value < me)
179                               lo = mid;
180                     else if (s[mid].st_value > me)
181                               hi = mid;
182                     else
183                               break;
184                     if (hi - lo == 1) {
185                               mid = lo;
186                               break;
187                     }
188                     mid = (hi + lo) / 2;
189           }
190           dd = me - (uintptr_t)dli->dli_saddr;
191           sd = me - s[mid].st_value;
192           if (dd > sd) {
193                     dli->dli_saddr = (void *)s[mid].st_value;
194                     dli->dli_sname = s[mid].st_name;
195           }
196           return 1;
197 }
198