db_elf.c revision 1.28 1 1.28 christos /* $NetBSD: db_elf.c,v 1.28 2017/11/04 22:17:55 christos Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.26 ad * Copyright (c) 1997, 2009 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.26 ad * NASA Ames Research Center, and by Andrew Doran.
10 1.1 thorpej *
11 1.1 thorpej * Redistribution and use in source and binary forms, with or without
12 1.1 thorpej * modification, are permitted provided that the following conditions
13 1.1 thorpej * are met:
14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer.
16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.1 thorpej * documentation and/or other materials provided with the distribution.
19 1.1 thorpej *
20 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.1 thorpej */
32 1.18 lukem
33 1.18 lukem #include <sys/cdefs.h>
34 1.28 christos __KERNEL_RCSID(0, "$NetBSD: db_elf.c,v 1.28 2017/11/04 22:17:55 christos Exp $");
35 1.1 thorpej
36 1.1 thorpej #include <sys/param.h>
37 1.20 simonb #include <sys/systm.h>
38 1.1 thorpej #include <sys/proc.h>
39 1.1 thorpej
40 1.1 thorpej
41 1.28 christos #include <machine/db_machdep.h>
42 1.26 ad #include <machine/pmap.h>
43 1.26 ad #include <machine/vmparam.h>
44 1.1 thorpej
45 1.1 thorpej #ifdef DB_ELF_SYMBOLS
46 1.1 thorpej
47 1.1 thorpej #ifndef DB_ELFSIZE
48 1.1 thorpej #error Must define DB_ELFSIZE!
49 1.1 thorpej #endif
50 1.1 thorpej
51 1.1 thorpej #define ELFSIZE DB_ELFSIZE
52 1.1 thorpej
53 1.28 christos #include <ddb/ddb.h>
54 1.1 thorpej #include <sys/exec_elf.h>
55 1.1 thorpej
56 1.20 simonb static char *db_elf_find_strtab(db_symtab_t *);
57 1.1 thorpej
58 1.1 thorpej #define STAB_TO_SYMSTART(stab) ((Elf_Sym *)((stab)->start))
59 1.1 thorpej #define STAB_TO_SYMEND(stab) ((Elf_Sym *)((stab)->end))
60 1.1 thorpej #define STAB_TO_EHDR(stab) ((Elf_Ehdr *)((stab)->private))
61 1.1 thorpej #define STAB_TO_SHDR(stab, e) ((Elf_Shdr *)((stab)->private + (e)->e_shoff))
62 1.1 thorpej
63 1.23 thorpej static bool db_elf_sym_init(int, void *, void *, const char *);
64 1.26 ad static db_sym_t db_elf_lookup(db_symtab_t *, const char *);
65 1.20 simonb static db_sym_t db_elf_search_symbol(db_symtab_t *, db_addr_t, db_strategy_t,
66 1.20 simonb db_expr_t *);
67 1.26 ad static void db_elf_symbol_values(db_symtab_t *, db_sym_t, const char **,
68 1.20 simonb db_expr_t *);
69 1.23 thorpej static bool db_elf_line_at_pc(db_symtab_t *, db_sym_t, char **, int *,
70 1.20 simonb db_expr_t);
71 1.23 thorpej static bool db_elf_sym_numargs(db_symtab_t *, db_sym_t, int *, char **);
72 1.20 simonb static void db_elf_forall(db_symtab_t *, db_forall_func_t db_forall_func,
73 1.20 simonb void *);
74 1.7 thorpej
75 1.14 jdolecek const db_symformat_t db_symformat_elf = {
76 1.7 thorpej "ELF",
77 1.7 thorpej db_elf_sym_init,
78 1.7 thorpej db_elf_lookup,
79 1.7 thorpej db_elf_search_symbol,
80 1.7 thorpej db_elf_symbol_values,
81 1.7 thorpej db_elf_line_at_pc,
82 1.7 thorpej db_elf_sym_numargs,
83 1.11 jhawk db_elf_forall
84 1.7 thorpej };
85 1.7 thorpej
86 1.26 ad static db_symtab_t db_symtabs;
87 1.26 ad
88 1.26 ad /*
89 1.26 ad * Add symbol table, with given name, to symbol tables.
90 1.26 ad */
91 1.26 ad static int
92 1.26 ad db_add_symbol_table(char *start, char *end, const char *name, char *ref)
93 1.26 ad {
94 1.26 ad
95 1.26 ad db_symtabs.start = start;
96 1.26 ad db_symtabs.end = end;
97 1.26 ad db_symtabs.name = name;
98 1.26 ad db_symtabs.private = ref;
99 1.26 ad
100 1.26 ad return(0);
101 1.26 ad }
102 1.26 ad
103 1.1 thorpej /*
104 1.1 thorpej * Find the symbol table and strings; tell ddb about them.
105 1.1 thorpej */
106 1.23 thorpej static bool
107 1.20 simonb db_elf_sym_init(
108 1.20 simonb int symsize, /* size of symbol table */
109 1.20 simonb void *symtab, /* pointer to start of symbol table */
110 1.20 simonb void *esymtab, /* pointer to end of string table,
111 1.1 thorpej for checking - rounded up to integer
112 1.1 thorpej boundary */
113 1.20 simonb const char *name
114 1.20 simonb )
115 1.1 thorpej {
116 1.1 thorpej Elf_Ehdr *elf;
117 1.1 thorpej Elf_Shdr *shp;
118 1.1 thorpej Elf_Sym *symp, *symtab_start, *symtab_end;
119 1.15 bjh21 char *strtab_start, *strtab_end;
120 1.15 bjh21 int i, j;
121 1.1 thorpej
122 1.1 thorpej if (ALIGNED_POINTER(symtab, long) == 0) {
123 1.7 thorpej printf("[ %s symbol table has bad start address %p ]\n",
124 1.7 thorpej name, symtab);
125 1.24 thorpej return (false);
126 1.1 thorpej }
127 1.1 thorpej
128 1.1 thorpej symtab_start = symtab_end = NULL;
129 1.1 thorpej strtab_start = strtab_end = NULL;
130 1.1 thorpej
131 1.1 thorpej /*
132 1.1 thorpej * The format of the symbols loaded by the boot program is:
133 1.1 thorpej *
134 1.1 thorpej * Elf exec header
135 1.1 thorpej * first section header
136 1.1 thorpej * . . .
137 1.1 thorpej * . . .
138 1.1 thorpej * last section header
139 1.1 thorpej * first symbol or string table section
140 1.1 thorpej * . . .
141 1.1 thorpej * . . .
142 1.1 thorpej * last symbol or string table section
143 1.1 thorpej */
144 1.1 thorpej
145 1.1 thorpej /*
146 1.1 thorpej * Validate the Elf header.
147 1.1 thorpej */
148 1.1 thorpej elf = (Elf_Ehdr *)symtab;
149 1.10 kleink if (memcmp(elf->e_ident, ELFMAG, SELFMAG) != 0 ||
150 1.10 kleink elf->e_ident[EI_CLASS] != ELFCLASS)
151 1.1 thorpej goto badheader;
152 1.1 thorpej
153 1.1 thorpej switch (elf->e_machine) {
154 1.1 thorpej
155 1.9 erh ELFDEFNNAME(MACHDEP_ID_CASES)
156 1.1 thorpej
157 1.1 thorpej default:
158 1.1 thorpej goto badheader;
159 1.1 thorpej }
160 1.1 thorpej
161 1.1 thorpej /*
162 1.15 bjh21 * Find the first (and, we hope, only) SHT_SYMTAB section in
163 1.15 bjh21 * the file, and the SHT_STRTAB section that goes with it.
164 1.1 thorpej */
165 1.16 bjh21 if (elf->e_shoff == 0)
166 1.16 bjh21 goto badheader;
167 1.8 augustss shp = (Elf_Shdr *)((char *)symtab + elf->e_shoff);
168 1.1 thorpej for (i = 0; i < elf->e_shnum; i++) {
169 1.15 bjh21 if (shp[i].sh_type == SHT_SYMTAB) {
170 1.16 bjh21 if (shp[i].sh_offset == 0)
171 1.16 bjh21 continue;
172 1.15 bjh21 /* Got the symbol table. */
173 1.20 simonb symtab_start = (Elf_Sym *)((char *)symtab +
174 1.8 augustss shp[i].sh_offset);
175 1.20 simonb symtab_end = (Elf_Sym *)((char *)symtab +
176 1.8 augustss shp[i].sh_offset + shp[i].sh_size);
177 1.15 bjh21 /* Find the string table to go with it. */
178 1.15 bjh21 j = shp[i].sh_link;
179 1.16 bjh21 if (shp[j].sh_offset == 0)
180 1.16 bjh21 continue;
181 1.15 bjh21 strtab_start = (char *)symtab + shp[j].sh_offset;
182 1.15 bjh21 strtab_end = (char *)symtab + shp[j].sh_offset +
183 1.15 bjh21 shp[j].sh_size;
184 1.15 bjh21 /* There should only be one symbol table. */
185 1.15 bjh21 break;
186 1.1 thorpej }
187 1.1 thorpej }
188 1.1 thorpej
189 1.1 thorpej /*
190 1.1 thorpej * Now, sanity check the symbols against the string table.
191 1.1 thorpej */
192 1.2 thorpej if (symtab_start == NULL || strtab_start == NULL ||
193 1.2 thorpej ALIGNED_POINTER(symtab_start, long) == 0 ||
194 1.2 thorpej ALIGNED_POINTER(strtab_start, long) == 0)
195 1.1 thorpej goto badheader;
196 1.1 thorpej for (symp = symtab_start; symp < symtab_end; symp++)
197 1.1 thorpej if (symp->st_name + strtab_start > strtab_end)
198 1.1 thorpej goto badheader;
199 1.1 thorpej
200 1.1 thorpej /*
201 1.1 thorpej * Link the symbol table into the debugger.
202 1.1 thorpej */
203 1.1 thorpej if (db_add_symbol_table((char *)symtab_start,
204 1.7 thorpej (char *)symtab_end, name, (char *)symtab) != -1) {
205 1.24 thorpej return (true);
206 1.7 thorpej }
207 1.7 thorpej
208 1.24 thorpej return (false);
209 1.1 thorpej
210 1.1 thorpej badheader:
211 1.7 thorpej printf("[ %s ELF symbol table not valid ]\n", name);
212 1.24 thorpej return (false);
213 1.1 thorpej }
214 1.1 thorpej
215 1.1 thorpej /*
216 1.1 thorpej * Internal helper function - return a pointer to the string table
217 1.1 thorpej * for the current symbol table.
218 1.1 thorpej */
219 1.1 thorpej static char *
220 1.20 simonb db_elf_find_strtab(db_symtab_t *stab)
221 1.1 thorpej {
222 1.1 thorpej Elf_Ehdr *elf = STAB_TO_EHDR(stab);
223 1.1 thorpej Elf_Shdr *shp = STAB_TO_SHDR(stab, elf);
224 1.1 thorpej int i;
225 1.1 thorpej
226 1.26 ad stab = &db_symtabs;
227 1.26 ad
228 1.17 christos /*
229 1.17 christos * We don't load ELF header for ELF modules.
230 1.17 christos * Find out if this is a loadable module. If so,
231 1.17 christos * string table comes right after symbol table.
232 1.17 christos */
233 1.17 christos if ((Elf_Sym *)elf == STAB_TO_SYMSTART(stab)) {
234 1.17 christos return ((char *)STAB_TO_SYMEND(stab));
235 1.17 christos }
236 1.1 thorpej for (i = 0; i < elf->e_shnum; i++) {
237 1.15 bjh21 if (shp[i].sh_type == SHT_SYMTAB)
238 1.15 bjh21 return ((char*)elf + shp[shp[i].sh_link].sh_offset);
239 1.1 thorpej }
240 1.1 thorpej
241 1.1 thorpej return (NULL);
242 1.1 thorpej }
243 1.1 thorpej
244 1.1 thorpej /*
245 1.1 thorpej * Lookup the symbol with the given name.
246 1.1 thorpej */
247 1.20 simonb static db_sym_t
248 1.26 ad db_elf_lookup(db_symtab_t *stab, const char *symstr)
249 1.1 thorpej {
250 1.1 thorpej Elf_Sym *symp, *symtab_start, *symtab_end;
251 1.1 thorpej char *strtab;
252 1.1 thorpej
253 1.26 ad stab = &db_symtabs;
254 1.26 ad
255 1.1 thorpej symtab_start = STAB_TO_SYMSTART(stab);
256 1.1 thorpej symtab_end = STAB_TO_SYMEND(stab);
257 1.1 thorpej
258 1.1 thorpej strtab = db_elf_find_strtab(stab);
259 1.1 thorpej if (strtab == NULL)
260 1.1 thorpej return ((db_sym_t)0);
261 1.1 thorpej
262 1.1 thorpej for (symp = symtab_start; symp < symtab_end; symp++) {
263 1.1 thorpej if (symp->st_name != 0 &&
264 1.1 thorpej db_eqname(strtab + symp->st_name, symstr, 0))
265 1.1 thorpej return ((db_sym_t)symp);
266 1.1 thorpej }
267 1.1 thorpej
268 1.1 thorpej return ((db_sym_t)0);
269 1.1 thorpej }
270 1.1 thorpej
271 1.1 thorpej /*
272 1.1 thorpej * Search for the symbol with the given address (matching within the
273 1.1 thorpej * provided threshold).
274 1.1 thorpej */
275 1.20 simonb static db_sym_t
276 1.20 simonb db_elf_search_symbol(db_symtab_t *symtab, db_addr_t off, db_strategy_t strategy,
277 1.20 simonb db_expr_t *diffp)
278 1.1 thorpej {
279 1.1 thorpej Elf_Sym *rsymp, *symp, *symtab_start, *symtab_end;
280 1.22 yamt db_addr_t diff = *diffp;
281 1.1 thorpej
282 1.26 ad symtab = &db_symtabs;
283 1.26 ad
284 1.1 thorpej symtab_start = STAB_TO_SYMSTART(symtab);
285 1.1 thorpej symtab_end = STAB_TO_SYMEND(symtab);
286 1.1 thorpej
287 1.1 thorpej rsymp = NULL;
288 1.1 thorpej
289 1.1 thorpej for (symp = symtab_start; symp < symtab_end; symp++) {
290 1.1 thorpej if (symp->st_name == 0)
291 1.1 thorpej continue;
292 1.26 ad
293 1.6 eeh #if 0
294 1.6 eeh /* This prevents me from seeing anythin in locore.s -- eeh */
295 1.26 ad if (ELF_ST_TYPE(symp->st_info) != STT_OBJECT &&
296 1.26 ad ELF_ST_TYPE(symp->st_info) != STT_FUNC)
297 1.1 thorpej continue;
298 1.6 eeh #endif
299 1.1 thorpej
300 1.1 thorpej if (off >= symp->st_value) {
301 1.22 yamt if (off - symp->st_value < diff) {
302 1.1 thorpej diff = off - symp->st_value;
303 1.1 thorpej rsymp = symp;
304 1.1 thorpej if (diff == 0) {
305 1.1 thorpej if (strategy == DB_STGY_PROC &&
306 1.10 kleink ELFDEFNNAME(ST_TYPE)(symp->st_info)
307 1.10 kleink == STT_FUNC &&
308 1.10 kleink ELFDEFNNAME(ST_BIND)(symp->st_info)
309 1.10 kleink != STB_LOCAL)
310 1.1 thorpej break;
311 1.1 thorpej if (strategy == DB_STGY_ANY &&
312 1.10 kleink ELFDEFNNAME(ST_BIND)(symp->st_info)
313 1.10 kleink != STB_LOCAL)
314 1.1 thorpej break;
315 1.1 thorpej }
316 1.22 yamt } else if (off - symp->st_value == diff) {
317 1.1 thorpej if (rsymp == NULL)
318 1.1 thorpej rsymp = symp;
319 1.10 kleink else if (ELFDEFNNAME(ST_BIND)(rsymp->st_info)
320 1.10 kleink == STB_LOCAL &&
321 1.10 kleink ELFDEFNNAME(ST_BIND)(symp->st_info)
322 1.10 kleink != STB_LOCAL) {
323 1.1 thorpej /* pick the external symbol */
324 1.1 thorpej rsymp = symp;
325 1.1 thorpej }
326 1.1 thorpej }
327 1.1 thorpej }
328 1.1 thorpej }
329 1.1 thorpej
330 1.1 thorpej if (rsymp == NULL)
331 1.1 thorpej *diffp = off;
332 1.1 thorpej else
333 1.1 thorpej *diffp = diff;
334 1.1 thorpej
335 1.1 thorpej return ((db_sym_t)rsymp);
336 1.1 thorpej }
337 1.1 thorpej
338 1.1 thorpej /*
339 1.1 thorpej * Return the name and value for a symbol.
340 1.1 thorpej */
341 1.20 simonb static void
342 1.26 ad db_elf_symbol_values(db_symtab_t *symtab, db_sym_t sym, const char **namep,
343 1.20 simonb db_expr_t *valuep)
344 1.1 thorpej {
345 1.1 thorpej Elf_Sym *symp = (Elf_Sym *)sym;
346 1.1 thorpej char *strtab;
347 1.1 thorpej
348 1.26 ad symtab = &db_symtabs;
349 1.26 ad
350 1.1 thorpej if (namep) {
351 1.1 thorpej strtab = db_elf_find_strtab(symtab);
352 1.1 thorpej if (strtab == NULL)
353 1.1 thorpej *namep = NULL;
354 1.1 thorpej else
355 1.1 thorpej *namep = strtab + symp->st_name;
356 1.1 thorpej }
357 1.1 thorpej
358 1.1 thorpej if (valuep)
359 1.1 thorpej *valuep = symp->st_value;
360 1.1 thorpej }
361 1.1 thorpej
362 1.1 thorpej /*
363 1.1 thorpej * Return the file and line number of the current program counter
364 1.1 thorpej * if we can find the appropriate debugging symbol.
365 1.1 thorpej */
366 1.23 thorpej static bool
367 1.27 dsl db_elf_line_at_pc(db_symtab_t *symtab, db_sym_t cursym, char **filename, int *linenum, db_expr_t off)
368 1.1 thorpej {
369 1.1 thorpej
370 1.1 thorpej /*
371 1.1 thorpej * XXX We don't support this (yet).
372 1.1 thorpej */
373 1.24 thorpej return (false);
374 1.1 thorpej }
375 1.1 thorpej
376 1.1 thorpej /*
377 1.1 thorpej * Returns the number of arguments to a function and their
378 1.1 thorpej * names if we can find the appropriate debugging symbol.
379 1.1 thorpej */
380 1.23 thorpej static bool
381 1.20 simonb db_elf_sym_numargs(db_symtab_t *symtab, db_sym_t cursym, int *nargp,
382 1.20 simonb char **argnamep)
383 1.1 thorpej {
384 1.1 thorpej
385 1.1 thorpej /*
386 1.1 thorpej * XXX We don't support this (yet).
387 1.1 thorpej */
388 1.24 thorpej return (false);
389 1.11 jhawk }
390 1.11 jhawk
391 1.20 simonb static void
392 1.20 simonb db_elf_forall(db_symtab_t *stab, db_forall_func_t db_forall_func, void *arg)
393 1.11 jhawk {
394 1.11 jhawk char *strtab;
395 1.11 jhawk static char suffix[2];
396 1.11 jhawk Elf_Sym *symp, *symtab_start, *symtab_end;
397 1.11 jhawk
398 1.26 ad stab = &db_symtabs;
399 1.26 ad
400 1.11 jhawk symtab_start = STAB_TO_SYMSTART(stab);
401 1.11 jhawk symtab_end = STAB_TO_SYMEND(stab);
402 1.11 jhawk
403 1.11 jhawk strtab = db_elf_find_strtab(stab);
404 1.11 jhawk if (strtab == NULL)
405 1.11 jhawk return;
406 1.11 jhawk
407 1.11 jhawk for (symp = symtab_start; symp < symtab_end; symp++)
408 1.11 jhawk if (symp->st_name != 0) {
409 1.11 jhawk suffix[1] = '\0';
410 1.11 jhawk switch (ELFDEFNNAME(ST_TYPE)(symp->st_info)) {
411 1.11 jhawk case STT_OBJECT:
412 1.11 jhawk suffix[0] = '+';
413 1.11 jhawk break;
414 1.11 jhawk case STT_FUNC:
415 1.11 jhawk suffix[0] = '*';
416 1.11 jhawk break;
417 1.11 jhawk case STT_SECTION:
418 1.11 jhawk suffix[0] = '&';
419 1.11 jhawk break;
420 1.11 jhawk case STT_FILE:
421 1.11 jhawk suffix[0] = '/';
422 1.11 jhawk break;
423 1.11 jhawk default:
424 1.11 jhawk suffix[0] = '\0';
425 1.11 jhawk }
426 1.11 jhawk (*db_forall_func)(stab, (db_sym_t)symp,
427 1.11 jhawk strtab + symp->st_name, suffix, 0, arg);
428 1.11 jhawk }
429 1.11 jhawk return;
430 1.1 thorpej }
431 1.1 thorpej #endif /* DB_ELF_SYMBOLS */
432