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