]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/db_gcc_aout.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
28 * Copyright (c) 1990, 1991, 1992, 1993 Open Software Foundation, Inc.
30 * Permission is hereby granted to use, copy, modify and freely distribute
31 * the software in this file and its documentation for any purpose without
32 * fee, provided that the above copyright notice appears in all copies and
33 * that both the copyright notice and this permission notice appear in
34 * supporting documentation. Further, provided that the name of Open
35 * Software Foundation, Inc. ("OSF") not be used in advertising or
36 * publicity pertaining to distribution of the software without prior
37 * written permission from OSF. OSF makes no representations about the
38 * suitability of this software for any purpose. It is provided "as is"
39 * without express or implied warranty.
44 * Revision 1.1.1.1 1998/09/22 21:05:36 wsanchez
45 * Import of Mac OS X kernel (~semeria)
47 * Revision 1.1.1.1 1998/03/07 02:25:37 wsanchez
48 * Import of OSF Mach kernel (~mburg)
50 * Revision 1.2.2.3 1994/01/28 17:23:00 chasb
52 * [1994/01/27 19:40:16 chasb]
54 * Revision 1.2.2.2 1993/06/09 02:27:36 gm
55 * Added to OSF/1 R1.3 from NMK15.0.
56 * [1993/06/02 21:04:03 jeffc]
58 * Revision 1.2 1993/04/19 16:13:10 devrcs
59 * pick up file_io.h from bootstrap directory
60 * [1993/02/27 15:01:09 david]
62 * Added new arguments and a missing one to db_add_symbol_table
66 * Added gcc symbol table handling based on db_aout.c (Revsion 2.4)
69 * Revision 1.1 1992/09/30 02:02:23 robert
76 * Revision 2.1 91/07/31 13:13:51 jeffreyh
79 * 31-Jul-91 Jeffrey Heller (tak) at Open Software Foundation
80 * Added gcc symbol table handling based on db_aout.c (Revsion 2.4)
85 * Mach Operating System
86 * Copyright (c) 1991,1990 Carnegie Mellon University
87 * All Rights Reserved.
89 * Permission to use, copy, modify and distribute this software and its
90 * documentation is hereby granted, provided that both the copyright
91 * notice and this permission notice appear in all copies of the
92 * software, derivative works or modified versions, and any portions
93 * thereof, and that both notices appear in supporting documentation.
95 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
96 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
97 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
99 * Carnegie Mellon requests users of this software to return to
101 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
102 * School of Computer Science
103 * Carnegie Mellon University
104 * Pittsburgh PA 15213-3890
106 * any improvements or extensions that they make and grant Carnegie Mellon
107 * the rights to redistribute these changes.
112 * Symbol table routines for a.out format files.
115 #include <mach/boolean.h>
116 #include <machine/db_machdep.h> /* data types */
117 #include <ddb/db_sym.h>
121 #include <ddb/nlist.h> /* a.out symbol table */
122 #include <i386/stab.h>
125 * An a.out symbol table as loaded into the kernel debugger:
127 * symtab -> size of symbol entries, in bytes
128 * sp -> first symbol entry
130 * ep -> last symbol entry + 1
131 * strtab == start of string table
132 * size of string table in bytes,
133 * including this word
138 * Find pointers to the start and end of the symbol entries,
139 * given a pointer to the start of the symbol table.
141 #define db_get_aout_symtab(symtab, sp, ep) \
142 (sp = (struct nlist *)((symtab) + 1), \
143 ep = (struct nlist *)((char *)sp + *(symtab)))
145 X_db_sym_init(symtab
, esymtab
, name
)
146 int * symtab
; /* pointer to start of symbol table */
147 char * esymtab
; /* pointer to end of string table,
148 for checking - rounded up to integer
152 register struct nlist
*sym_start
, *sym_end
;
153 register struct nlist
*sp
;
154 register char * strtab
;
157 db_get_aout_symtab(symtab
, sym_start
, sym_end
);
159 strtab
= (char *)sym_end
;
160 strlen
= *(int *)strtab
;
162 if (strtab
+ ((strlen
+ sizeof(int) - 1) & ~(sizeof(int)-1))
165 db_printf("[ %s symbol table not valid ]\n", name
);
169 db_printf("[ preserving %#x bytes of %s symbol table ]\n",
170 esymtab
- (char *)symtab
, name
);
172 for (sp
= sym_start
; sp
< sym_end
; sp
++) {
174 strx
= sp
->n_un
.n_strx
;
177 db_printf("Bad string table index (%#x)\n", strx
);
181 sp
->n_un
.n_name
= strtab
+ strx
;
185 db_add_symbol_table(sym_start
, sym_end
, name
, (char *)symtab
,
190 * check file name or not (check xxxx.x pattern)
193 X_db_is_filename(name
)
207 * special name comparison routine with a name in the symbol table entry
210 X_db_eq_name(sp
, name
)
214 register char *s1
, *s2
;
216 s1
= sp
->n_un
.n_name
;
218 if (*s1
== '_' && *s2
&& *s2
!= '_')
221 if (*s1
++ != *s2
++) {
223 * check .c .o file name comparison case
225 if (*s2
== 0 && sp
->n_un
.n_name
<= s1
- 2
226 && s1
[-2] == '.' && s1
[-1] == 'o')
232 * do special check for
234 * xxx.ttt for N_DATA and N_BSS
236 return(*s1
== 0 || (*s1
== ':' && sp
->n_type
== N_FUN
) ||
237 (*s1
== '.' && (sp
->n_type
== N_DATA
|| sp
->n_type
== N_BSS
)));
241 * search a symbol table with name and type
242 * fp(in,out): last found text file name symbol entry
245 X_db_search_name(sp
, ep
, name
, type
, fp
)
246 register struct nlist
*sp
;
252 struct nlist
*file_sp
= *fp
;
253 struct nlist
*found_sp
= 0;
255 for ( ; sp
< ep
; sp
++) {
256 if (sp
->n_type
== N_TEXT
&& X_db_is_filename(sp
->n_un
.n_name
))
259 if (sp
->n_type
== type
) {
260 if (X_db_eq_name(sp
, name
))
263 if (sp
->n_type
== N_SO
)
267 if (sp
->n_type
& N_STAB
)
269 if (sp
->n_un
.n_name
&& X_db_eq_name(sp
, name
)) {
271 * In case of qaulified search by a file,
272 * return it immediately with some check.
273 * Otherwise, search external one
276 if ((file_sp
== *fp
) || (sp
->n_type
& N_EXT
))
278 } else if (sp
->n_type
& N_EXT
)
288 * search a symbol with file, func and line qualification
291 X_db_qualified_search(stab
, file
, sym
, line
)
297 register struct nlist
*sp
= (struct nlist
*)stab
->start
;
298 struct nlist
*ep
= (struct nlist
*)stab
->end
;
299 struct nlist
*fp
= 0;
300 struct nlist
*found_sp
;
304 if (file
== 0 && sym
== 0)
307 if ((sp
= X_db_search_name(sp
, ep
, file
, N_TEXT
, &fp
)) == 0)
311 sp
= X_db_search_name(sp
, ep
, sym
, (line
> 0)? N_FUN
: 0, &fp
);
316 if (file
&& !X_db_eq_name(fp
, file
))
319 if (sp
->n_type
== N_FUN
) {
321 * qualfied by function name
322 * search backward because line number entries
323 * for the function are above it in this case.
325 func_top
= sp
->n_value
;
326 for (sp
--; sp
>= (struct nlist
*)stab
->start
; sp
--) {
327 if (sp
->n_type
!= N_SLINE
)
329 if (sp
->n_value
< func_top
)
331 if (sp
->n_desc
<= line
) {
332 if (found_sp
== 0 || found_sp
->n_desc
< sp
->n_desc
)
334 if (sp
->n_desc
== line
)
338 if (sp
->n_type
!= N_SLINE
|| sp
->n_value
< func_top
)
342 * qualified by only file name
343 * search forward in this case
346 for (sp
++; sp
< ep
; sp
++) {
347 if (sp
->n_type
== N_TEXT
348 && X_db_is_filename(sp
->n_un
.n_name
))
349 break; /* enter into another file */
350 if (sp
->n_type
== N_SOL
) {
351 in_file
= X_db_eq_name(sp
, file
);
354 if (!in_file
|| sp
->n_type
!= N_SLINE
)
356 if (sp
->n_desc
<= line
) {
357 if (found_sp
== 0 || found_sp
->n_desc
< sp
->n_desc
)
359 if (sp
->n_desc
== line
)
370 * lookup symbol by name
373 X_db_lookup(stab
, symstr
)
384 struct nlist
*found
= 0;
387 * disassemble component: [file_name:]symbol[:line_nubmer]
389 component
[0] = symstr
;
390 component
[1] = component
[2] = 0;
391 for (p
= symstr
, n
= 1; *p
; p
++) {
396 component
[n
++] = p
+1;
404 if (*p
>= '0' && *p
<= '9') {
407 for (line_number
= 0; *p
; p
++) {
408 if (*p
< '0' || *p
> '9')
410 line_number
= line_number
*10 + *p
- '0';
416 if (X_db_is_filename(component
[0])) {
417 file_name
= component
[0];
421 sym_name
= component
[0];
424 file_name
= component
[0];
425 sym_name
= component
[1];
427 found
= X_db_qualified_search(stab
, file_name
, sym_name
, line_number
);
431 component
[n
][-1] = ':';
432 return((db_sym_t
) found
);
436 X_db_search_symbol(symtab
, off
, strategy
, diffp
)
437 db_symtab_t
* symtab
;
440 db_strategy_t strategy
;
441 db_expr_t
*diffp
; /* in/out */
443 register unsigned int diff
= *diffp
;
444 register struct nlist
*symp
= 0;
445 register struct nlist
*sp
, *ep
;
447 sp
= (struct nlist
*)symtab
->start
;
448 ep
= (struct nlist
*)symtab
->end
;
450 for (; sp
< ep
; sp
++) {
451 if (sp
->n_un
.n_name
== 0)
453 if ((sp
->n_type
& N_STAB
) != 0)
455 if (off
>= sp
->n_value
) {
456 if (off
- sp
->n_value
< diff
) {
457 diff
= off
- sp
->n_value
;
459 if (diff
== 0 && (sp
->n_type
& N_EXT
))
462 else if (off
- sp
->n_value
== diff
) {
465 else if ((symp
->n_type
& N_EXT
) == 0 &&
466 (sp
->n_type
& N_EXT
) != 0)
467 symp
= sp
; /* pick the external symbol */
477 return ((db_sym_t
)symp
);
481 * Return the name and value for a symbol.
484 X_db_symbol_values(sym
, namep
, valuep
)
489 register struct nlist
*sp
;
491 sp
= (struct nlist
*)sym
;
493 *namep
= sp
->n_un
.n_name
;
495 *valuep
= sp
->n_value
;
498 #define X_DB_MAX_DIFF 8 /* maximum allowable diff at the end of line */
501 * search symbol by value
503 X_db_search_by_addr(stab
, addr
, file
, func
, line
, diff
)
505 register unsigned addr
;
511 register struct nlist
*sp
;
512 register struct nlist
*line_sp
, *func_sp
, *file_sp
, *line_func
;
513 register func_diff
, line_diff
;
514 boolean_t found_line
= FALSE
;
515 struct nlist
*ep
= (struct nlist
*)stab
->end
;
517 line_sp
= func_sp
= file_sp
= line_func
= 0;
520 for (sp
= (struct nlist
*)stab
->start
; sp
< ep
; sp
++) {
523 if (sp
->n_value
<= addr
) {
524 if (line_sp
== 0 || line_diff
>= addr
- sp
->n_value
) {
528 line_diff
= addr
- sp
->n_value
;
531 if (sp
->n_value
>= addr
&& line_sp
)
535 if ((found_line
|| (line_sp
&& line_diff
< X_DB_MAX_DIFF
))
540 if (X_db_is_filename(sp
->n_un
.n_name
)) {
541 if (sp
->n_value
> addr
)
543 if (file_sp
== 0 || file_sp
->n_value
< sp
->n_value
)
545 } else if (sp
->n_value
<= addr
&&
546 (func_sp
== 0 || func_diff
> addr
- sp
->n_value
)) {
548 func_diff
= addr
- sp
->n_value
;
552 if (sp
->n_value
<= addr
&&
553 (func_sp
== 0 || func_diff
>= addr
- sp
->n_value
)) {
555 func_diff
= addr
- sp
->n_value
;
556 if (func_diff
== 0 && file_sp
&& func_sp
)
565 if (line_func
== 0 || func_sp
== 0
566 || line_func
->n_value
!= func_sp
->n_value
)
570 *diff
= addr
- file_sp
->n_value
;
571 *file
= file_sp
->n_un
.n_name
;
574 *diff
= addr
- func_sp
->n_value
;
575 *func
= (func_sp
->n_un
.n_name
[0] == '_')?
576 func_sp
->n_un
.n_name
+ 1: func_sp
->n_un
.n_name
;
579 *diff
= addr
- line_sp
->n_value
;
580 *line
= line_sp
->n_desc
;
582 return(file_sp
|| func_sp
|| line_sp
);
587 X_db_line_at_pc(stab
, sym
, file
, line
, pc
)
598 found
= X_db_search_by_addr(stab
,(unsigned)pc
,file
,&func
,line
,&diff
);
599 return(found
&& func
&& *file
);
603 * Initialization routine for a.out files.
610 if (esym
> (char *)&end
) {
611 X_db_sym_init((int *)&end
, esym
, "mach");
616 * Read symbol table from file.
617 * (should be somewhere else)
619 #include <bootstrap/file_io.h>
620 #include <vm/vm_kern.h>
622 read_symtab_from_file(fp
, symtab_name
)
627 kern_return_t result
;
632 vm_size_t table_size
;
635 if (!get_symtab(fp
, &symoff
, &symsize
)) {
636 boot_printf("[ error %d reading %s file header ]\n",
637 result
, symtab_name
);
641 stroff
= symoff
+ symsize
;
642 result
= read_file(fp
, (vm_offset_t
)stroff
,
643 (vm_offset_t
)&strsize
, sizeof(strsize
), &resid
);
644 if (result
|| resid
) {
645 boot_printf("[ no valid symbol table present for %s ]\n",
650 table_size
= sizeof(int) + symsize
+ strsize
;
651 table_size
= (table_size
+ sizeof(int)-1) & ~(sizeof(int)-1);
653 result
= kmem_alloc_wired(kernel_map
, &symtab
, table_size
);
655 boot_printf("[ error %d allocating space for %s symbol table ]\n",
656 result
, symtab_name
);
660 *(int *)symtab
= symsize
;
662 result
= read_file(fp
, symoff
,
663 symtab
+ sizeof(int), symsize
, &resid
);
664 if (result
|| resid
) {
665 boot_printf("[ error %d reading %s symbol table ]\n",
666 result
, symtab_name
);
670 result
= read_file(fp
, stroff
,
671 symtab
+ sizeof(int) + symsize
, strsize
, &resid
);
672 if (result
|| resid
) {
673 boot_printf("[ error %d reading %s string table ]\n",
674 result
, symtab_name
);
678 X_db_sym_init((int *)symtab
,
679 (char *)(symtab
+ table_size
),
684 #endif /* DB_GCC_AOUT */