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 $
30 * Author: David B. Golub, Carnegie Mellon University
34 * Symbol table routines for a.out format files.
37 #if !defined(__ELF__) && !defined(__alpha__)
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <machine/bootinfo.h>
45 #include <ddb/db_sym.h>
47 #define _AOUT_INCLUDE_
52 * An a.out symbol table as loaded into the kernel debugger:
54 * symtab -> size of symbol entries, in bytes
55 * sp -> first symbol entry
57 * ep -> last symbol entry + 1
58 * strtab == start of string table
59 * size of string table in bytes,
64 static void X_db_sym_init __P((int *symtab, char *esymtab, char *name));
66 * Find pointers to the start and end of the symbol entries,
67 * given a pointer to the start of the symbol table.
69 #define db_get_aout_symtab(symtab, sp, ep) \
70 (sp = (struct nlist *)((symtab) + 1), \
71 ep = (struct nlist *)((char *)sp + *(symtab)))
74 X_db_sym_init(symtab, esymtab, name)
75 int * symtab; /* pointer to start of symbol table */
76 char * esymtab; /* pointer to end of string table,
77 for checking - rounded up to integer
81 register struct nlist *sym_start, *sym_end;
82 register struct nlist *sp;
83 register char * strtab;
87 printf ("DDB: no symbols\n");
91 db_get_aout_symtab(symtab, sym_start, sym_end);
93 strtab = (char *)sym_end;
94 strlen = *(int *)strtab;
96 if (strtab + ((strlen + sizeof(int) - 1) & ~(sizeof(int)-1))
99 db_printf("[ %s symbol table not valid ]\n", name);
103 db_printf("[ preserving %#x bytes of %s symbol table ]\n",
104 esymtab - (char *)symtab, name);
106 for (sp = sym_start; sp < sym_end; sp++) {
108 strx = sp->n_un.n_strx;
111 db_printf("Bad string table index (%#x)\n", strx);
115 sp->n_un.n_name = strtab + strx;
119 db_add_symbol_table((char *)sym_start, (char *)sym_end, name,
124 X_db_lookup(stab, symstr)
128 register struct nlist *sp, *ep;
130 sp = (struct nlist *)stab->start;
131 ep = (struct nlist *)stab->end;
133 for (; sp < ep; sp++) {
134 if (sp->n_un.n_name == 0)
136 if ((sp->n_type & N_STAB) == 0 &&
137 sp->n_un.n_name != 0 &&
138 db_eqname(sp->n_un.n_name, symstr, '_'))
140 return ((db_sym_t)sp);
143 return ((db_sym_t)0);
147 X_db_search_symbol(symtab, off, strategy, diffp)
148 db_symtab_t * symtab;
151 db_strategy_t strategy;
152 db_expr_t *diffp; /* in/out */
154 register unsigned int diff = *diffp;
155 register struct nlist *symp = 0;
156 register struct nlist *sp, *ep;
158 sp = (struct nlist *)symtab->start;
159 ep = (struct nlist *)symtab->end;
161 for (; sp < ep; sp++) {
162 if (sp->n_un.n_name == 0)
164 if ((sp->n_type & N_STAB) != 0 || (sp->n_type & N_TYPE) == N_FN)
166 if (off >= sp->n_value) {
167 if (off - sp->n_value < diff) {
168 diff = off - sp->n_value;
171 if (strategy == DB_STGY_PROC &&
172 sp->n_type == (N_TEXT|N_EXT))
174 if (strategy == DB_STGY_ANY &&
175 (sp->n_type & N_EXT))
179 else if (off - sp->n_value == diff) {
182 else if ((symp->n_type & N_EXT) == 0 &&
183 (sp->n_type & N_EXT) != 0)
184 symp = sp; /* pick the external symbol */
194 return ((db_sym_t)symp);
198 * Return the name and value for a symbol.
201 X_db_symbol_values(symtab, sym, namep, valuep)
207 register const struct nlist *sp;
209 sp = (const struct nlist *)sym;
211 *namep = sp->n_un.n_name;
213 *valuep = sp->n_value;
218 X_db_line_at_pc(symtab, cursym, filename, linenum, off)
219 db_symtab_t * symtab;
225 register struct nlist *sp, *ep;
226 unsigned long sodiff = -1UL, lndiff = -1UL, ln = 0;
229 sp = (struct nlist *)symtab->start;
230 ep = (struct nlist *)symtab->end;
233 * XXX - this used to remove "gcc_compiled.", but that is obsolete. We
234 * now remove unwanted names using symorder.
236 #define NEWSRC(str) 0
238 for (; sp < ep; sp++) {
241 * Prevent bogus linenumbers in case module not compiled
242 * with debugging options
245 if (sp->n_value <= off && (off - sp->n_value) <= sodiff &&
246 NEWSRC(sp->n_un.n_name)) {
248 if ((sp->n_type & N_TYPE) == N_FN || NEWSRC(sp->n_un.n_name)) {
249 sodiff = lndiff = -1UL;
254 if (sp->n_type == N_SO && *sp->n_un.n_name != '/') {
255 if (sp->n_value <= off && (off - sp->n_value) < sodiff) {
256 sodiff = off - sp->n_value;
257 fname = sp->n_un.n_name;
262 if (sp->n_type != N_SLINE)
265 if (sp->n_value > off)
268 if (off - sp->n_value < lndiff) {
269 lndiff = off - sp->n_value;
274 if (fname != NULL && ln != 0) {
284 X_db_sym_numargs(symtab, cursym, nargp, argnamep)
285 db_symtab_t * symtab;
290 register struct nlist *sp, *ep;
292 int maxnarg = *nargp, nargs = 0;
297 addr = ((const struct nlist *)cursym)->n_value;
298 sp = (struct nlist *)symtab->start;
299 ep = (struct nlist *)symtab->end;
301 for (; sp < ep; sp++) {
302 if (sp->n_type == N_FUN && sp->n_value == addr) {
303 while (++sp < ep && sp->n_type == N_PSYM) {
304 if (nargs >= maxnarg)
307 *argnamep++ = sp->n_un.n_name?sp->n_un.n_name:"???";
309 /* XXX - remove trailers */
310 char *cp = *(argnamep-1);
311 while (*cp != '\0' && *cp != ':') cp++;
312 if (*cp == ':') *cp = '\0';
323 * Initialization routine for a.out files.
329 if (bootinfo.bi_esymtab != bootinfo.bi_symtab)
330 X_db_sym_init((int *)bootinfo.bi_symtab,
331 (char *)((bootinfo.bi_esymtab + sizeof(int) - 1)
332 & ~(sizeof(int) - 1)),
339 * Read symbol table from file.
340 * (should be somewhere else)
342 #include <boot_ufs/file_io.h>
343 #include <vm/vm_kern.h>
345 read_symtab_from_file(fp, symtab_name)
350 kern_return_t result;
355 vm_size_t table_size;
358 if (!get_symtab(fp, &symoff, &symsize)) {
359 boot_printf("[ error %d reading %s file header ]\n",
360 result, symtab_name);
364 stroff = symoff + symsize;
365 result = read_file(fp, (vm_offset_t)stroff,
366 (vm_offset_t)&strsize, sizeof(strsize), &resid);
367 if (result || resid) {
368 boot_printf("[ no valid symbol table present for %s ]\n",
373 table_size = sizeof(int) + symsize + strsize;
374 table_size = (table_size + sizeof(int)-1) & ~(sizeof(int)-1);
376 symtab = kmem_alloc_wired(kernel_map, table_size);
378 *(int *)symtab = symsize;
380 result = read_file(fp, symoff,
381 symtab + sizeof(int), symsize, &resid);
382 if (result || resid) {
383 boot_printf("[ error %d reading %s symbol table ]\n",
384 result, symtab_name);
388 result = read_file(fp, stroff,
389 symtab + sizeof(int) + symsize, strsize, &resid);
390 if (result || resid) {
391 boot_printf("[ error %d reading %s string table ]\n",
392 result, symtab_name);
396 X_db_sym_init((int *)symtab,
397 (char *)(symtab + table_size),