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