2 /* $NetBSD: db_elf.c,v 1.4 1998/05/03 18:49:54 thorpej Exp $ */
5 * Copyright (c) 1997 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 * NASA Ames Research Center.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #if defined(__ELF__) && defined(__alpha__)
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/systm.h>
50 #include <machine/db_machdep.h>
52 #include <ddb/db_sym.h>
53 #include <ddb/db_output.h>
55 #include <machine/elf.h>
57 static char *db_elf_find_strtab __P((db_symtab_t *));
59 #define STAB_TO_SYMSTART(stab) ((Elf_Sym *)((stab)->start))
60 #define STAB_TO_SYMEND(stab) ((Elf_Sym *)((stab)->end))
61 #define STAB_TO_EHDR(stab) ((Elf_Ehdr *)((stab)->private))
62 #define STAB_TO_SHDR(stab, e) ((Elf_Shdr *)((stab)->private + (e)->e_shoff))
64 void X_db_sym_init(void *symtab, void *esymtab, char *name);
67 * Find the symbol table and strings; tell ddb about them.
70 X_db_sym_init(symtab, esymtab, name)
71 void *symtab; /* pointer to start of symbol table */
72 void *esymtab; /* pointer to end of string table,
73 for checking - rounded up to integer
79 Elf_Sym *symp, *symtab_start, *symtab_end;
80 char *strtab_start, *strtab_end;
83 if (ALIGNED_POINTER(symtab, long) == 0) {
84 printf("DDB: bad symbol table start address %p\n", symtab);
88 symtab_start = symtab_end = NULL;
89 strtab_start = strtab_end = NULL;
92 * The format of the symbols loaded by the boot program is:
95 * first section header
99 * first symbol or string table section
102 * last symbol or string table section
106 * Validate the Elf header.
108 elf = (Elf_Ehdr *)symtab;
109 if (elf->e_ident[EI_MAG0] != ELFMAG0
110 || elf->e_ident[EI_MAG1] != ELFMAG1
111 || elf->e_ident[EI_MAG2] != ELFMAG2
112 || elf->e_ident[EI_MAG3] != ELFMAG3)
115 if (!ELF_MACHINE_OK(elf->e_machine))
119 * We need to avoid the section header string table (small string
120 * table which names the sections). We do this by assuming that
121 * the following two conditions will be true:
123 * (1) .shstrtab will be smaller than one page.
124 * (2) .strtab will be larger than one page.
126 * When we encounter what we think is the .shstrtab, we change
127 * its section type Elf_sht_null so that it will be ignored
130 shp = (Elf_Shdr *)((char*)symtab + elf->e_shoff);
131 for (i = 0; i < elf->e_shnum; i++) {
132 if (shp[i].sh_addr || i == elf->e_shstrndx)
134 switch (shp[i].sh_type) {
136 if (shp[i].sh_size < PAGE_SIZE) {
137 shp[i].sh_type = SHT_NULL;
140 if (strtab_start != NULL)
141 goto multiple_strtab;
142 strtab_start = (char *)symtab + shp[i].sh_offset;
143 strtab_end = (char *)symtab + shp[i].sh_offset +
148 if (symtab_start != NULL)
149 goto multiple_symtab;
150 symtab_start = (Elf_Sym *)((char*)symtab + shp[i].sh_offset);
151 symtab_end = (Elf_Sym *)((char*)symtab + shp[i].sh_offset +
156 /* Ignore all other sections. */
162 * Now, sanity check the symbols against the string table.
164 if (symtab_start == NULL || strtab_start == NULL ||
165 ALIGNED_POINTER(symtab_start, long) == 0 ||
166 ALIGNED_POINTER(strtab_start, long) == 0)
168 for (symp = symtab_start; symp < symtab_end; symp++)
169 if (symp->st_name + strtab_start > strtab_end)
173 * Link the symbol table into the debugger.
175 db_add_symbol_table((char *)symtab_start,
176 (char *)symtab_end, name, (char *)symtab);
177 printf("[ preserving %lu bytes of %s symbol table ]\n",
178 (u_long)roundup(((char*)esymtab - (char*)symtab), sizeof(u_long)), name);
182 printf("[ %s symbol table not valid ]\n", name);
186 printf("[ %s has multiple string tables ]\n", name);
190 printf("[ %s has multiple symbol tables ]\n", name);
195 * Internal helper function - return a pointer to the string table
196 * for the current symbol table.
199 db_elf_find_strtab(stab)
202 Elf_Ehdr *elf = STAB_TO_EHDR(stab);
203 Elf_Shdr *shp = STAB_TO_SHDR(stab, elf);
206 for (i = 0; i < elf->e_shnum; i++) {
207 if (shp[i].sh_type == SHT_STRTAB
208 && !shp[i].sh_addr && i != elf->e_shstrndx)
209 return (stab->private + shp[i].sh_offset);
216 * Lookup the symbol with the given name.
219 X_db_lookup(stab, symstr)
223 Elf_Sym *symp, *symtab_start, *symtab_end;
226 symtab_start = STAB_TO_SYMSTART(stab);
227 symtab_end = STAB_TO_SYMEND(stab);
229 strtab = db_elf_find_strtab(stab);
231 return ((db_sym_t)0);
233 for (symp = symtab_start; symp < symtab_end; symp++) {
234 if (symp->st_name != 0 &&
235 db_eqname(strtab + symp->st_name, symstr, 0))
236 return ((db_sym_t)symp);
239 return ((db_sym_t)0);
243 * Search for the symbol with the given address (matching within the
244 * provided threshold).
247 X_db_search_symbol(symtab, off, strategy, diffp)
250 db_strategy_t strategy;
251 db_expr_t *diffp; /* in/out */
253 Elf_Sym *rsymp, *symp, *symtab_start, *symtab_end;
254 db_expr_t diff = *diffp;
256 symtab_start = STAB_TO_SYMSTART(symtab);
257 symtab_end = STAB_TO_SYMEND(symtab);
261 for (symp = symtab_start; symp < symtab_end; symp++) {
262 if (symp->st_name == 0)
264 if (ELF_ST_TYPE(symp->st_info) != STT_OBJECT &&
265 ELF_ST_TYPE(symp->st_info) != STT_FUNC)
268 if (off >= symp->st_value) {
269 if ((off - symp->st_value) < diff) {
270 diff = off - symp->st_value;
273 if (strategy == DB_STGY_PROC &&
274 ELF_ST_TYPE(symp->st_info) ==
276 ELF_ST_BIND(symp->st_info) !=
279 if (strategy == DB_STGY_ANY &&
280 ELF_ST_BIND(symp->st_info) !=
284 } else if ((off - symp->st_value) == diff) {
287 else if (ELF_ST_BIND(rsymp->st_info) ==
289 ELF_ST_BIND(symp->st_info) !=
291 /* pick the external symbol */
303 return ((db_sym_t)rsymp);
307 * Return the name and value for a symbol.
310 X_db_symbol_values(symtab, sym, namep, valuep)
316 Elf_Sym *symp = (Elf_Sym *)sym;
320 strtab = db_elf_find_strtab(symtab);
324 *namep = strtab + symp->st_name;
328 *valuep = symp->st_value;
332 * Return the file and line number of the current program counter
333 * if we can find the appropriate debugging symbol.
336 X_db_line_at_pc(symtab, cursym, filename, linenum, off)
345 * XXX We don't support this (yet).
351 * Returns the number of arguments to a function and their
352 * names if we can find the appropriate debugging symbol.
355 X_db_sym_numargs(symtab, cursym, nargp, argnamep)
363 * XXX We don't support this (yet).
369 * Initialization routine for Elf files.
371 extern void *ksym_start, *ksym_end;
377 if (ksym_end > ksym_start)
378 X_db_sym_init(ksym_start, ksym_end, "kernel");