2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
26 * $FreeBSD: src/sys/ddb/db_aout.c,v 1.27 1999/08/28 00:41:05 peter Exp $
27 * $DragonFly: src/sys/ddb/db_aout.c,v 1.5 2005/06/10 20:16:31 swildner Exp $
31 * Author: David B. Golub, Carnegie Mellon University
35 * Symbol table routines for a.out format files.
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <machine/bootinfo.h>
46 #include <ddb/db_sym.h>
48 #define _AOUT_INCLUDE_
53 * An a.out symbol table as loaded into the kernel debugger:
55 * symtab -> size of symbol entries, in bytes
56 * sp -> first symbol entry
58 * ep -> last symbol entry + 1
59 * strtab == start of string table
60 * size of string table in bytes,
65 static void X_db_sym_init (int *symtab, char *esymtab, char *name);
67 * Find pointers to the start and end of the symbol entries,
68 * given a pointer to the start of the symbol table.
70 #define db_get_aout_symtab(symtab, sp, ep) \
71 (sp = (struct nlist *)((symtab) + 1), \
72 ep = (struct nlist *)((char *)sp + *(symtab)))
75 X_db_sym_init(symtab, esymtab, name)
76 int * symtab; /* pointer to start of symbol table */
77 char * esymtab; /* pointer to end of string table,
78 for checking - rounded up to integer
82 struct nlist *sym_start, *sym_end;
88 printf ("DDB: no symbols\n");
92 db_get_aout_symtab(symtab, sym_start, sym_end);
94 strtab = (char *)sym_end;
95 strlen = *(int *)strtab;
97 if (strtab + ((strlen + sizeof(int) - 1) & ~(sizeof(int)-1))
100 db_printf("[ %s symbol table not valid ]\n", name);
104 db_printf("[ preserving %#x bytes of %s symbol table ]\n",
105 esymtab - (char *)symtab, name);
107 for (sp = sym_start; sp < sym_end; sp++) {
109 strx = sp->n_un.n_strx;
112 db_printf("Bad string table index (%#x)\n", strx);
116 sp->n_un.n_name = strtab + strx;
120 db_add_symbol_table((char *)sym_start, (char *)sym_end, name,
125 X_db_lookup(stab, symstr)
129 struct nlist *sp, *ep;
131 sp = (struct nlist *)stab->start;
132 ep = (struct nlist *)stab->end;
134 for (; sp < ep; sp++) {
135 if (sp->n_un.n_name == 0)
137 if ((sp->n_type & N_STAB) == 0 &&
138 sp->n_un.n_name != 0 &&
139 db_eqname(sp->n_un.n_name, symstr, '_'))
141 return ((db_sym_t)sp);
144 return ((db_sym_t)0);
148 X_db_search_symbol(symtab, off, strategy, diffp)
149 db_symtab_t * symtab;
152 db_strategy_t strategy;
153 db_expr_t *diffp; /* in/out */
155 unsigned int diff = *diffp;
156 struct nlist *symp = 0;
157 struct nlist *sp, *ep;
159 sp = (struct nlist *)symtab->start;
160 ep = (struct nlist *)symtab->end;
162 for (; sp < ep; sp++) {
163 if (sp->n_un.n_name == 0)
165 if ((sp->n_type & N_STAB) != 0 || (sp->n_type & N_TYPE) == N_FN)
167 if (off >= sp->n_value) {
168 if (off - sp->n_value < diff) {
169 diff = off - sp->n_value;
172 if (strategy == DB_STGY_PROC &&
173 sp->n_type == (N_TEXT|N_EXT))
175 if (strategy == DB_STGY_ANY &&
176 (sp->n_type & N_EXT))
180 else if (off - sp->n_value == diff) {
183 else if ((symp->n_type & N_EXT) == 0 &&
184 (sp->n_type & N_EXT) != 0)
185 symp = sp; /* pick the external symbol */
195 return ((db_sym_t)symp);
199 * Return the name and value for a symbol.
202 X_db_symbol_values(symtab, sym, namep, valuep)
208 const struct nlist *sp;
210 sp = (const struct nlist *)sym;
212 *namep = sp->n_un.n_name;
214 *valuep = sp->n_value;
219 X_db_line_at_pc(symtab, cursym, filename, linenum, off)
220 db_symtab_t * symtab;
226 struct nlist *sp, *ep;
227 unsigned long sodiff = -1UL, lndiff = -1UL, ln = 0;
230 sp = (struct nlist *)symtab->start;
231 ep = (struct nlist *)symtab->end;
234 * XXX - this used to remove "gcc_compiled.", but that is obsolete. We
235 * now remove unwanted names using symorder.
237 #define NEWSRC(str) 0
239 for (; sp < ep; sp++) {
242 * Prevent bogus linenumbers in case module not compiled
243 * with debugging options
246 if (sp->n_value <= off && (off - sp->n_value) <= sodiff &&
247 NEWSRC(sp->n_un.n_name)) {
249 if ((sp->n_type & N_TYPE) == N_FN || NEWSRC(sp->n_un.n_name)) {
250 sodiff = lndiff = -1UL;
255 if (sp->n_type == N_SO && *sp->n_un.n_name != '/') {
256 if (sp->n_value <= off && (off - sp->n_value) < sodiff) {
257 sodiff = off - sp->n_value;
258 fname = sp->n_un.n_name;
263 if (sp->n_type != N_SLINE)
266 if (sp->n_value > off)
269 if (off - sp->n_value < lndiff) {
270 lndiff = off - sp->n_value;
275 if (fname != NULL && ln != 0) {
285 X_db_sym_numargs(symtab, cursym, nargp, argnamep)
286 db_symtab_t * symtab;
291 struct nlist *sp, *ep;
293 int maxnarg = *nargp, nargs = 0;
298 addr = ((const struct nlist *)cursym)->n_value;
299 sp = (struct nlist *)symtab->start;
300 ep = (struct nlist *)symtab->end;
302 for (; sp < ep; sp++) {
303 if (sp->n_type == N_FUN && sp->n_value == addr) {
304 while (++sp < ep && sp->n_type == N_PSYM) {
305 if (nargs >= maxnarg)
308 *argnamep++ = sp->n_un.n_name?sp->n_un.n_name:"???";
310 /* XXX - remove trailers */
311 char *cp = *(argnamep-1);
312 while (*cp != '\0' && *cp != ':') cp++;
313 if (*cp == ':') *cp = '\0';
324 * Initialization routine for a.out files.
330 if (bootinfo.bi_esymtab != bootinfo.bi_symtab)
331 X_db_sym_init((int *)bootinfo.bi_symtab,
332 (char *)((bootinfo.bi_esymtab + sizeof(int) - 1)
333 & ~(sizeof(int) - 1)),
340 * Read symbol table from file.
341 * (should be somewhere else)
343 #include <boot_ufs/file_io.h>
344 #include <vm/vm_kern.h>
346 read_symtab_from_file(fp, symtab_name)
351 kern_return_t result;
356 vm_size_t table_size;
359 if (!get_symtab(fp, &symoff, &symsize)) {
360 boot_printf("[ error %d reading %s file header ]\n",
361 result, symtab_name);
365 stroff = symoff + symsize;
366 result = read_file(fp, (vm_offset_t)stroff,
367 (vm_offset_t)&strsize, sizeof(strsize), &resid);
368 if (result || resid) {
369 boot_printf("[ no valid symbol table present for %s ]\n",
374 table_size = sizeof(int) + symsize + strsize;
375 table_size = (table_size + sizeof(int)-1) & ~(sizeof(int)-1);
377 symtab = kmem_alloc_wired(kernel_map, table_size);
379 *(int *)symtab = symsize;
381 result = read_file(fp, symoff,
382 symtab + sizeof(int), symsize, &resid);
383 if (result || resid) {
384 boot_printf("[ error %d reading %s symbol table ]\n",
385 result, symtab_name);
389 result = read_file(fp, stroff,
390 symtab + sizeof(int) + symsize, strsize, &resid);
391 if (result || resid) {
392 boot_printf("[ error %d reading %s string table ]\n",
393 result, symtab_name);
397 X_db_sym_init((int *)symtab,
398 (char *)(symtab + table_size),