]>
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_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
33 * Copyright (c) 1990, 1991, 1992, 1993 Open Software Foundation, Inc.
35 * Permission is hereby granted to use, copy, modify and freely distribute
36 * the software in this file and its documentation for any purpose without
37 * fee, provided that the above copyright notice appears in all copies and
38 * that both the copyright notice and this permission notice appear in
39 * supporting documentation. Further, provided that the name of Open
40 * Software Foundation, Inc. ("OSF") not be used in advertising or
41 * publicity pertaining to distribution of the software without prior
42 * written permission from OSF. OSF makes no representations about the
43 * suitability of this software for any purpose. It is provided "as is"
44 * without express or implied warranty.
49 * Revision 1.1.1.1 1998/09/22 21:05:36 wsanchez
50 * Import of Mac OS X kernel (~semeria)
52 * Revision 1.1.1.1 1998/03/07 02:25:37 wsanchez
53 * Import of OSF Mach kernel (~mburg)
55 * Revision 1.2.2.3 1994/01/28 17:23:00 chasb
57 * [1994/01/27 19:40:16 chasb]
59 * Revision 1.2.2.2 1993/06/09 02:27:36 gm
60 * Added to OSF/1 R1.3 from NMK15.0.
61 * [1993/06/02 21:04:03 jeffc]
63 * Revision 1.2 1993/04/19 16:13:10 devrcs
64 * pick up file_io.h from bootstrap directory
65 * [1993/02/27 15:01:09 david]
67 * Added new arguments and a missing one to db_add_symbol_table
71 * Added gcc symbol table handling based on db_aout.c (Revsion 2.4)
74 * Revision 1.1 1992/09/30 02:02:23 robert
81 * Revision 2.1 91/07/31 13:13:51 jeffreyh
84 * 31-Jul-91 Jeffrey Heller (tak) at Open Software Foundation
85 * Added gcc symbol table handling based on db_aout.c (Revsion 2.4)
90 * Mach Operating System
91 * Copyright (c) 1991,1990 Carnegie Mellon University
92 * All Rights Reserved.
94 * Permission to use, copy, modify and distribute this software and its
95 * documentation is hereby granted, provided that both the copyright
96 * notice and this permission notice appear in all copies of the
97 * software, derivative works or modified versions, and any portions
98 * thereof, and that both notices appear in supporting documentation.
100 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
101 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
102 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
104 * Carnegie Mellon requests users of this software to return to
106 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
107 * School of Computer Science
108 * Carnegie Mellon University
109 * Pittsburgh PA 15213-3890
111 * any improvements or extensions that they make and grant Carnegie Mellon
112 * the rights to redistribute these changes.
117 * Symbol table routines for a.out format files.
120 #include <mach/boolean.h>
121 #include <machine/db_machdep.h> /* data types */
122 #include <ddb/db_sym.h>
126 #include <ddb/nlist.h> /* a.out symbol table */
127 #include <i386/stab.h>
130 * An a.out symbol table as loaded into the kernel debugger:
132 * symtab -> size of symbol entries, in bytes
133 * sp -> first symbol entry
135 * ep -> last symbol entry + 1
136 * strtab == start of string table
137 * size of string table in bytes,
138 * including this word
143 * Find pointers to the start and end of the symbol entries,
144 * given a pointer to the start of the symbol table.
146 #define db_get_aout_symtab(symtab, sp, ep) \
147 (sp = (struct nlist *)((symtab) + 1), \
148 ep = (struct nlist *)((char *)sp + *(symtab)))
150 X_db_sym_init(symtab
, esymtab
, name
)
151 int * symtab
; /* pointer to start of symbol table */
152 char * esymtab
; /* pointer to end of string table,
153 for checking - rounded up to integer
157 register struct nlist
*sym_start
, *sym_end
;
158 register struct nlist
*sp
;
159 register char * strtab
;
162 db_get_aout_symtab(symtab
, sym_start
, sym_end
);
164 strtab
= (char *)sym_end
;
165 strlen
= *(int *)strtab
;
167 if (strtab
+ ((strlen
+ sizeof(int) - 1) & ~(sizeof(int)-1))
170 db_printf("[ %s symbol table not valid ]\n", name
);
174 db_printf("[ preserving %#x bytes of %s symbol table ]\n",
175 esymtab
- (char *)symtab
, name
);
177 for (sp
= sym_start
; sp
< sym_end
; sp
++) {
179 strx
= sp
->n_un
.n_strx
;
182 db_printf("Bad string table index (%#x)\n", strx
);
186 sp
->n_un
.n_name
= strtab
+ strx
;
190 db_add_symbol_table(sym_start
, sym_end
, name
, (char *)symtab
,
195 * check file name or not (check xxxx.x pattern)
198 X_db_is_filename(name
)
212 * special name comparison routine with a name in the symbol table entry
215 X_db_eq_name(sp
, name
)
219 register char *s1
, *s2
;
221 s1
= sp
->n_un
.n_name
;
223 if (*s1
== '_' && *s2
&& *s2
!= '_')
226 if (*s1
++ != *s2
++) {
228 * check .c .o file name comparison case
230 if (*s2
== 0 && sp
->n_un
.n_name
<= s1
- 2
231 && s1
[-2] == '.' && s1
[-1] == 'o')
237 * do special check for
239 * xxx.ttt for N_DATA and N_BSS
241 return(*s1
== 0 || (*s1
== ':' && sp
->n_type
== N_FUN
) ||
242 (*s1
== '.' && (sp
->n_type
== N_DATA
|| sp
->n_type
== N_BSS
)));
246 * search a symbol table with name and type
247 * fp(in,out): last found text file name symbol entry
250 X_db_search_name(sp
, ep
, name
, type
, fp
)
251 register struct nlist
*sp
;
257 struct nlist
*file_sp
= *fp
;
258 struct nlist
*found_sp
= 0;
260 for ( ; sp
< ep
; sp
++) {
261 if (sp
->n_type
== N_TEXT
&& X_db_is_filename(sp
->n_un
.n_name
))
264 if (sp
->n_type
== type
) {
265 if (X_db_eq_name(sp
, name
))
268 if (sp
->n_type
== N_SO
)
272 if (sp
->n_type
& N_STAB
)
274 if (sp
->n_un
.n_name
&& X_db_eq_name(sp
, name
)) {
276 * In case of qaulified search by a file,
277 * return it immediately with some check.
278 * Otherwise, search external one
281 if ((file_sp
== *fp
) || (sp
->n_type
& N_EXT
))
283 } else if (sp
->n_type
& N_EXT
)
293 * search a symbol with file, func and line qualification
296 X_db_qualified_search(stab
, file
, sym
, line
)
302 register struct nlist
*sp
= (struct nlist
*)stab
->start
;
303 struct nlist
*ep
= (struct nlist
*)stab
->end
;
304 struct nlist
*fp
= 0;
305 struct nlist
*found_sp
;
309 if (file
== 0 && sym
== 0)
312 if ((sp
= X_db_search_name(sp
, ep
, file
, N_TEXT
, &fp
)) == 0)
316 sp
= X_db_search_name(sp
, ep
, sym
, (line
> 0)? N_FUN
: 0, &fp
);
321 if (file
&& !X_db_eq_name(fp
, file
))
324 if (sp
->n_type
== N_FUN
) {
326 * qualfied by function name
327 * search backward because line number entries
328 * for the function are above it in this case.
330 func_top
= sp
->n_value
;
331 for (sp
--; sp
>= (struct nlist
*)stab
->start
; sp
--) {
332 if (sp
->n_type
!= N_SLINE
)
334 if (sp
->n_value
< func_top
)
336 if (sp
->n_desc
<= line
) {
337 if (found_sp
== 0 || found_sp
->n_desc
< sp
->n_desc
)
339 if (sp
->n_desc
== line
)
343 if (sp
->n_type
!= N_SLINE
|| sp
->n_value
< func_top
)
347 * qualified by only file name
348 * search forward in this case
351 for (sp
++; sp
< ep
; sp
++) {
352 if (sp
->n_type
== N_TEXT
353 && X_db_is_filename(sp
->n_un
.n_name
))
354 break; /* enter into another file */
355 if (sp
->n_type
== N_SOL
) {
356 in_file
= X_db_eq_name(sp
, file
);
359 if (!in_file
|| sp
->n_type
!= N_SLINE
)
361 if (sp
->n_desc
<= line
) {
362 if (found_sp
== 0 || found_sp
->n_desc
< sp
->n_desc
)
364 if (sp
->n_desc
== line
)
375 * lookup symbol by name
378 X_db_lookup(stab
, symstr
)
389 struct nlist
*found
= 0;
392 * disassemble component: [file_name:]symbol[:line_nubmer]
394 component
[0] = symstr
;
395 component
[1] = component
[2] = 0;
396 for (p
= symstr
, n
= 1; *p
; p
++) {
401 component
[n
++] = p
+1;
409 if (*p
>= '0' && *p
<= '9') {
412 for (line_number
= 0; *p
; p
++) {
413 if (*p
< '0' || *p
> '9')
415 line_number
= line_number
*10 + *p
- '0';
421 if (X_db_is_filename(component
[0])) {
422 file_name
= component
[0];
426 sym_name
= component
[0];
429 file_name
= component
[0];
430 sym_name
= component
[1];
432 found
= X_db_qualified_search(stab
, file_name
, sym_name
, line_number
);
436 component
[n
][-1] = ':';
437 return((db_sym_t
) found
);
441 X_db_search_symbol(symtab
, off
, strategy
, diffp
)
442 db_symtab_t
* symtab
;
445 db_strategy_t strategy
;
446 db_expr_t
*diffp
; /* in/out */
448 register unsigned int diff
= *diffp
;
449 register struct nlist
*symp
= 0;
450 register struct nlist
*sp
, *ep
;
452 sp
= (struct nlist
*)symtab
->start
;
453 ep
= (struct nlist
*)symtab
->end
;
455 for (; sp
< ep
; sp
++) {
456 if (sp
->n_un
.n_name
== 0)
458 if ((sp
->n_type
& N_STAB
) != 0)
460 if (off
>= sp
->n_value
) {
461 if (off
- sp
->n_value
< diff
) {
462 diff
= off
- sp
->n_value
;
464 if (diff
== 0 && (sp
->n_type
& N_EXT
))
467 else if (off
- sp
->n_value
== diff
) {
470 else if ((symp
->n_type
& N_EXT
) == 0 &&
471 (sp
->n_type
& N_EXT
) != 0)
472 symp
= sp
; /* pick the external symbol */
482 return ((db_sym_t
)symp
);
486 * Return the name and value for a symbol.
489 X_db_symbol_values(sym
, namep
, valuep
)
494 register struct nlist
*sp
;
496 sp
= (struct nlist
*)sym
;
498 *namep
= sp
->n_un
.n_name
;
500 *valuep
= sp
->n_value
;
503 #define X_DB_MAX_DIFF 8 /* maximum allowable diff at the end of line */
506 * search symbol by value
508 X_db_search_by_addr(stab
, addr
, file
, func
, line
, diff
)
510 register unsigned addr
;
516 register struct nlist
*sp
;
517 register struct nlist
*line_sp
, *func_sp
, *file_sp
, *line_func
;
518 register func_diff
, line_diff
;
519 boolean_t found_line
= FALSE
;
520 struct nlist
*ep
= (struct nlist
*)stab
->end
;
522 line_sp
= func_sp
= file_sp
= line_func
= 0;
525 for (sp
= (struct nlist
*)stab
->start
; sp
< ep
; sp
++) {
528 if (sp
->n_value
<= addr
) {
529 if (line_sp
== 0 || line_diff
>= addr
- sp
->n_value
) {
533 line_diff
= addr
- sp
->n_value
;
536 if (sp
->n_value
>= addr
&& line_sp
)
540 if ((found_line
|| (line_sp
&& line_diff
< X_DB_MAX_DIFF
))
545 if (X_db_is_filename(sp
->n_un
.n_name
)) {
546 if (sp
->n_value
> addr
)
548 if (file_sp
== 0 || file_sp
->n_value
< sp
->n_value
)
550 } else if (sp
->n_value
<= addr
&&
551 (func_sp
== 0 || func_diff
> addr
- sp
->n_value
)) {
553 func_diff
= addr
- sp
->n_value
;
557 if (sp
->n_value
<= addr
&&
558 (func_sp
== 0 || func_diff
>= addr
- sp
->n_value
)) {
560 func_diff
= addr
- sp
->n_value
;
561 if (func_diff
== 0 && file_sp
&& func_sp
)
570 if (line_func
== 0 || func_sp
== 0
571 || line_func
->n_value
!= func_sp
->n_value
)
575 *diff
= addr
- file_sp
->n_value
;
576 *file
= file_sp
->n_un
.n_name
;
579 *diff
= addr
- func_sp
->n_value
;
580 *func
= (func_sp
->n_un
.n_name
[0] == '_')?
581 func_sp
->n_un
.n_name
+ 1: func_sp
->n_un
.n_name
;
584 *diff
= addr
- line_sp
->n_value
;
585 *line
= line_sp
->n_desc
;
587 return(file_sp
|| func_sp
|| line_sp
);
592 X_db_line_at_pc(stab
, sym
, file
, line
, pc
)
603 found
= X_db_search_by_addr(stab
,(unsigned)pc
,file
,&func
,line
,&diff
);
604 return(found
&& func
&& *file
);
608 * Initialization routine for a.out files.
615 if (esym
> (char *)&end
) {
616 X_db_sym_init((int *)&end
, esym
, "mach");
621 * Read symbol table from file.
622 * (should be somewhere else)
624 #include <bootstrap/file_io.h>
625 #include <vm/vm_kern.h>
627 read_symtab_from_file(fp
, symtab_name
)
632 kern_return_t result
;
637 vm_size_t table_size
;
640 if (!get_symtab(fp
, &symoff
, &symsize
)) {
641 boot_printf("[ error %d reading %s file header ]\n",
642 result
, symtab_name
);
646 stroff
= symoff
+ symsize
;
647 result
= read_file(fp
, (vm_offset_t
)stroff
,
648 (vm_offset_t
)&strsize
, sizeof(strsize
), &resid
);
649 if (result
|| resid
) {
650 boot_printf("[ no valid symbol table present for %s ]\n",
655 table_size
= sizeof(int) + symsize
+ strsize
;
656 table_size
= (table_size
+ sizeof(int)-1) & ~(sizeof(int)-1);
658 result
= kmem_alloc_wired(kernel_map
, &symtab
, table_size
);
660 boot_printf("[ error %d allocating space for %s symbol table ]\n",
661 result
, symtab_name
);
665 *(int *)symtab
= symsize
;
667 result
= read_file(fp
, symoff
,
668 symtab
+ sizeof(int), symsize
, &resid
);
669 if (result
|| resid
) {
670 boot_printf("[ error %d reading %s symbol table ]\n",
671 result
, symtab_name
);
675 result
= read_file(fp
, stroff
,
676 symtab
+ sizeof(int) + symsize
, strsize
, &resid
);
677 if (result
|| resid
) {
678 boot_printf("[ error %d reading %s string table ]\n",
679 result
, symtab_name
);
683 X_db_sym_init((int *)symtab
,
684 (char *)(symtab
+ table_size
),
689 #endif /* DB_GCC_AOUT */