2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 * Author: David B. Golub, Carnegie Mellon University
57 * Symbol table routines for a.out format files.
59 #include <mach/boolean.h>
60 #include <mach/std_types.h>
61 #include <machine/db_machdep.h> /* data types */
62 #include <string.h> /* For strcpy(), strcmp() */
63 #include <ddb/db_aout.h>
64 #include <ddb/db_output.h> /* For db_printf() */
65 #include <ddb/db_sym.h>
69 #include <ddb/nlist.h> /* a.out symbol table */
72 #define private static
74 private int aout_db_order_symbols(char *, char *);
75 private int aout_db_compare_symbols(char *, char *);
76 private boolean_t
aout_db_is_filename(char *);
77 private boolean_t
aout_db_eq_name(struct nlist
*, char *, int);
80 * An a.out symbol table as loaded into the kernel debugger:
82 * symtab -> size of symbol entries, in bytes
83 * sp -> first symbol entry
85 * ep -> last symbol entry + 1
86 * strtab == start of string table
87 * size of string table in bytes,
93 * Find pointers to the start and end of the symbol entries,
94 * given a pointer to the start of the symbol table.
96 #define db_get_aout_symtab(symtab, sp, ep) \
97 (sp = (struct nlist *)(((vm_offset_t *)(symtab)) + 1), \
98 ep = (struct nlist *)((char *)sp + *((int *)(symtab))))
100 char *db_sorting_sym_end
;
103 aout_db_order_symbols(
107 struct nlist
*sym1
= (struct nlist
*) s1
;
108 struct nlist
*sym2
= (struct nlist
*) s2
;
110 if (sym1
->n_value
!= sym2
->n_value
)
111 return (sym1
->n_value
- sym2
->n_value
);
113 return (sym1
->n_un
.n_name
- sym2
->n_un
.n_name
);
118 aout_db_compare_symbols(
122 return (((struct nlist
*) sym1
)->n_value
-
123 ((struct nlist
*) sym2
)->n_value
);
126 int db_sorting_limit
= 50000;
130 char * symtab
, /* pointer to start of symbol table */
131 char * esymtab
, /* pointer to end of string table,
132 for checking - may be rounded up to
135 char * task_addr
) /* use for this task only */
137 struct nlist
*sym_start
, *sym_end
, *dbsym_start
, *dbsym_end
;
139 char *strtab
, *dbstrtab
;
141 char *estrtab
, *dbestrtab
;
142 unsigned long minsym
= ~0;
143 unsigned long maxsym
= 0;
146 extern boolean_t
getsymtab(char *,
147 vm_offset_t
*, int *,
148 vm_offset_t
*, vm_size_t
*);
152 if (!getsymtab(symtab
,
153 (vm_offset_t
*)&sym_start
, &nsyms
,
154 (vm_offset_t
*)&strtab
, (vm_size_t
*)&strlen
)) {
157 sym_end
= sym_start
+ nsyms
;
158 estrtab
= strtab
+ strlen
;
161 * We haven't actually started up VM yet, so we can just steal some pages to
162 * make a working copy of the symbols and strings
165 dbsym_start
= (struct nlist
*)pmap_steal_memory(((unsigned int)sym_end
- (unsigned int)sym_start
+ 4096) & -4096); /* Get space for symbols */
166 dbstrtab
= (char *)pmap_steal_memory(((unsigned int)estrtab
- (unsigned int)strtab
+ 4096) & -4096); /* Get space for strings */
168 bcopy((char *)sym_start
, (char *)dbsym_start
, (unsigned int)sym_end
- (unsigned int)sym_start
); /* Copy symbols */
169 bcopy(strtab
, dbstrtab
, (unsigned int)estrtab
- (unsigned int)strtab
); /* Copy strings */
171 dbsym_end
= dbsym_start
+ nsyms
;
172 dbestrtab
= dbstrtab
+ strlen
;
174 sorting
= ((dbsym_end
- dbsym_start
) < db_sorting_limit
);
176 for (sp
= dbsym_start
; sp
< dbsym_end
; sp
++) {
178 strx
= sp
->n_un
.n_strx
;
184 sp
->n_un
.n_name
= dbstrtab
+ strx
;
186 if (sp
->n_type
!= N_ABS
) {
187 if (sp
->n_value
> 0 && sp
->n_value
< minsym
)
188 minsym
= sp
->n_value
;
189 if (sp
->n_value
> maxsym
)
190 maxsym
= sp
->n_value
;
198 db_qsort((char *) dbsym_start
, dbsym_end
- dbsym_start
,
199 sizeof (struct nlist
), aout_db_order_symbols
);
204 if (db_add_symbol_table(SYMTAB_AOUT
,
214 /* Successfully added symbol table */
216 pmap_protect(kernel_pmap
,
217 (vm_offset_t
) dbsym_start
, (vm_offset_t
) dbsym_end
,
218 VM_PROT_READ
|VM_PROT_WRITE
);
219 pmap_protect(kernel_pmap
,
220 (vm_offset_t
) dbstrtab
, (vm_offset_t
) dbestrtab
,
221 VM_PROT_READ
|VM_PROT_WRITE
);
228 * This KLUDGE offsets the n_values of a copied symbol table
230 void db_clone_offsetXXX(char *, long);
232 db_clone_offsetXXX(char * symtab
, long offset
)
234 register struct nlist
*sym_start
, *sym_end
, *sp
;
236 db_get_aout_symtab((int *)symtab
, sym_start
, sym_end
);
238 for (sp
= sym_start
; sp
< sym_end
; sp
++)
239 if (sp
->n_type
!= N_ABS
)
240 sp
->n_value
+= offset
;
245 * check file name or not (check xxxx.x pattern)
248 aout_db_is_filename(char *name
)
261 * special name comparison routine with a name in the symbol table entry
269 register char *s1
, *s2
;
271 s1
= sp
->n_un
.n_name
;
273 #ifndef __NO_UNDERSCORES__
274 if (*s1
== '_' && *s2
&& *s2
!= '_')
276 #endif /* __NO_UNDERSCORES__ */
278 if (*s1
++ != *s2
++) {
280 * check .c .o file name comparison case
282 if (*s2
== 0 && sp
->n_un
.n_name
<= s1
- 2
283 && s1
[-2] == '.' && s1
[-1] == 'o')
291 * do special check for
293 * xxx.ttt for N_DATA and N_BSS
295 return(*s1
== 0 || (*s1
== ':' && sp
->n_type
== N_FUN
) ||
296 (*s1
== '.' && (sp
->n_type
== N_DATA
|| sp
->n_type
== N_BSS
)));
300 * search a symbol table with name and type
301 * fp(in,out): last found text file name symbol entry
303 private struct nlist
*
312 struct nlist
*file_sp
= *fp
;
313 struct nlist
*found_sp
= 0;
315 for ( ; sp
< ep
; sp
++) {
318 if (sp
->n_type
== N_TEXT
&& aout_db_is_filename(sp
->n_un
.n_name
))
321 if (sp
->n_type
== type
) {
322 /* dwm_debug: b26 name, mk6 added last param */
323 if (aout_db_eq_name(sp
, name
, 0))
326 if (sp
->n_type
== N_SO
)
330 if (sp
->n_type
& N_STAB
)
332 if (sp
->n_un
.n_name
&& aout_db_eq_name(sp
, name
, incomplete
)) {
334 * In case of qaulified search by a file,
335 * return it immediately with some check.
336 * Otherwise, search external one
339 if ((file_sp
== *fp
) || (sp
->n_type
& N_EXT
))
341 } else if ((sp
->n_type
& N_EXT
) ||
342 (incomplete
&& !aout_db_is_filename(sp
->n_un
.n_name
)))
352 * Print sorted possible completions for a symbol.
353 * Use n_other field to mark completion symbols in order
357 aout_db_qualified_print_completion(
365 struct nlist
*fp
= 0;
374 sp
= aout_db_search_name((struct nlist
*)stab
->start
,
375 (struct nlist
*)stab
->end
,
377 if (sp
== (struct nlist
*)0)
380 symlen
= strlen(sym
);
383 if (strncmp(cur
->n_un
.n_name
, sym
, symlen
) == 0)
388 cur
= aout_db_search_name(cur
+ 1, (struct nlist
*)stab
->end
,
399 if (strcmp(&cur
->n_un
.n_name
[cur
->n_other
- 1],
400 &new->n_un
.n_name
[new->n_other
- 1]) < 0)
407 if ((new->n_type
& N_EXT
) == 0) {
408 for (cur
= new - 1; cur
> (struct nlist
*)stab
->start
; cur
--) {
409 if (cur
->n_type
== N_SO
||
410 (stab
->sorted
&& cur
->n_value
< new->n_value
))
413 cur
->n_type
== N_SLINE
&&
414 cur
->n_value
== new->n_value
)
417 cur
->n_type
== N_FUN
&&
418 cur
->n_value
== new->n_value
)
422 if (cur
->n_type
== N_SO
)
423 fname
= cur
->n_un
.n_name
;
427 if (line
== 0 || func
== 0)
429 cur
< (struct nlist
*)stab
->end
; cur
++) {
430 if (cur
->n_type
== N_SO
||
431 (stab
->sorted
&& cur
->n_value
> new->n_value
))
434 cur
->n_type
== N_SLINE
&&
435 cur
->n_value
== new->n_value
) {
441 cur
->n_type
== N_FUN
&&
442 cur
->n_value
== new->n_value
) {
450 for (cur
= new - 1; cur
> (struct nlist
*)stab
->start
; cur
--) {
451 if (cur
->n_type
== N_SO
||
452 (stab
->sorted
&& cur
->n_value
< new->n_value
))
455 cur
->n_type
== N_FUN
&&
456 cur
->n_value
== new->n_value
)
461 cur
< (struct nlist
*)stab
->end
; cur
++) {
462 if (cur
->n_type
== N_SO
||
463 (stab
->sorted
&& cur
->n_value
> new->n_value
))
465 if (cur
->n_type
== N_FUN
&&
466 cur
->n_value
== new->n_value
) {
473 db_sym_print_completion(stab
, &new->n_un
.n_name
[new->n_other
- 1],
482 } else if (new == sp1
)
492 * search a (possibly incomplete) symbol with file, func and line qualification
495 aout_db_qualified_search(
504 register struct nlist
*sp
= (struct nlist
*)stab
->start
;
505 struct nlist
*ep
= (struct nlist
*)stab
->end
;
506 struct nlist
*fp
= 0;
507 struct nlist
*found_sp
;
508 unsigned long func_top
;
514 if (file
== 0 && sym
== 0)
517 if ((sp
= aout_db_search_name(sp
, ep
, file
, N_TEXT
, &fp
, 0)) == 0)
522 sp
= aout_db_search_name(sp
, ep
, sym
, (line
> 0)? N_FUN
: 0, &fp
,
523 (ret
== (db_sym_t
*)0));
529 if (strncmp(sp
->n_un
.n_name
, sym
, strlen(sym
)) == 0)
532 p
= &sp
->n_un
.n_name
[1];
534 if (*name
== (char *)0) {
538 for (i
= 0; i
< *len
; i
++)
539 if ((*name
)[i
] != p
[i
]) {
550 if (file
&& !aout_db_eq_name(fp
, file
, 0))
553 if (sp
->n_type
== N_FUN
) {
555 * qualfied by function name
556 * search backward because line number entries
557 * for the function are above it in this case.
559 func_top
= sp
->n_value
;
561 /* symbols with the same value may have been mixed up */
564 } while (sp
->n_value
== func_top
);
566 for (sp
--; sp
>= (struct nlist
*)stab
->start
; sp
--) {
567 if (sp
->n_type
!= N_SLINE
)
569 if (sp
->n_value
< func_top
)
571 if (sp
->n_desc
<= line
) {
572 if (found_sp
== 0 || found_sp
->n_desc
< sp
->n_desc
)
574 if (sp
->n_desc
== line
)
578 if (sp
->n_type
!= N_SLINE
|| sp
->n_value
< func_top
)
582 * qualified by only file name
583 * search forward in this case
587 /* symbols with the same value may have been mixed up */
588 func_top
= sp
->n_value
;
591 } while (sp
->n_value
== func_top
);
593 for (sp
++; sp
< ep
; sp
++) {
594 if (sp
->n_type
== N_TEXT
595 && aout_db_is_filename(sp
->n_un
.n_name
))
596 break; /* enter into another file */
597 if (sp
->n_type
== N_SOL
) {
598 in_file
= aout_db_eq_name(sp
, file
, 0);
601 if (!in_file
|| sp
->n_type
!= N_SLINE
)
603 if (sp
->n_desc
<= line
) {
604 if (found_sp
== 0 || found_sp
->n_desc
< sp
->n_desc
)
606 if (sp
->n_desc
== line
)
613 *ret
= (db_sym_t
) sp
;
618 * lookup symbol by name
625 return(db_sym_parse_and_lookup(aout_db_qualified_search
, stab
, symstr
));
629 * lookup (possibly incomplete) symbol by name
632 aout_db_lookup_incomplete(
639 return(db_sym_parse_and_lookup_incomplete(aout_db_qualified_search
,
640 stab
, symstr
, name
, len
, toadd
));
644 * Display possible completion for the symbol
647 aout_db_print_completion(stab
, symstr
)
652 return(db_sym_parse_and_print_completion(aout_db_qualified_print_completion
,
657 aout_db_search_symbol(
660 db_strategy_t strategy
,
661 db_expr_t
*diffp
) /* in/out */
663 register unsigned long diff
= *diffp
;
664 register struct nlist
*symp
= 0;
665 struct nlist
*sp
, *ep
, *cp
;
666 boolean_t first_pass
= FALSE
;
668 sp
= (struct nlist
*)symtab
->start
;
669 ep
= (struct nlist
*)symtab
->end
;
671 if (symtab
->sorted
) {
674 target
.n_value
= off
;
675 target
.n_un
.n_name
= (char *) 0;
676 target
.n_other
= (char) 0;
677 db_qsort_limit_search((char *) &target
, (char **) &sp
, (char **) &ep
,
678 sizeof (struct nlist
), aout_db_compare_symbols
);
683 for (cp
= ep
-1; cp
>= sp
; cp
--) {
684 if (cp
->n_un
.n_name
== 0)
686 if ((cp
->n_type
& N_STAB
) != 0)
688 if (strategy
== DB_STGY_XTRN
&& (cp
->n_type
& N_EXT
) == 0)
690 if (off
>= cp
->n_value
) {
691 if (off
- cp
->n_value
< diff
) {
692 diff
= off
- cp
->n_value
;
694 if (diff
== 0 && (cp
->n_type
& N_EXT
))
697 else if (off
- cp
->n_value
== diff
) {
700 else if ((symp
->n_type
& N_EXT
) == 0 &&
701 (cp
->n_type
& N_EXT
) != 0)
702 symp
= cp
; /* pick the external symbol */
709 sp
= (struct nlist
*) symtab
->start
;
717 return ((db_sym_t
)symp
);
721 * Return the name and value for a symbol.
724 aout_db_symbol_values(
729 register struct nlist
*sp
;
731 sp
= (struct nlist
*)sym
;
733 *namep
= sp
->n_un
.n_name
;
735 *valuep
= sp
->n_value
;
738 #define X_DB_MAX_DIFF 8 /* maximum allowable diff at the end of line */
739 extern int db_search_maxoff
; /* maximum acceptable offset */
742 * search symbol by value
745 aout_db_search_by_addr(
754 struct nlist
*sp
, *cp
;
755 register struct nlist
*line_sp
, *func_sp
, *file_sp
, *line_func
;
756 unsigned long func_diff
, line_diff
;
757 boolean_t found_line
= FALSE
;
758 struct nlist
*ep
= (struct nlist
*)stab
->end
;
759 boolean_t first_pass
= FALSE
;
763 * Added init of these two... not sure if it's correct, but
764 * can't be worse than random values.... -- jfriedl@omron.co.jp
766 func_diff
= line_diff
= /*HUGE*/0x0fffffff;
768 line_sp
= func_sp
= file_sp
= line_func
= 0;
773 sp
= (struct nlist
*)stab
->start
;
777 target
.n_value
= addr
;
778 target
.n_un
.n_name
= (char *) 0;
779 target
.n_other
= (char) 0;
780 db_qsort_limit_search((char *) &target
, (char **) &sp
,
781 (char **) &ep
, sizeof (struct nlist
),
782 aout_db_compare_symbols
);
786 for (cp
= sp
; cp
< ep
; cp
++) {
789 if (cp
->n_value
<= addr
) {
790 if (line_sp
== 0 || line_diff
>= addr
- cp
->n_value
) {
794 line_diff
= addr
- cp
->n_value
;
797 if (cp
->n_value
>= addr
&& line_sp
)
801 if ((found_line
|| (line_sp
&& line_diff
< X_DB_MAX_DIFF
))
806 if (cp
->n_value
> addr
)
808 if (file_sp
== 0 || file_sp
->n_value
<= cp
->n_value
)
812 if (aout_db_is_filename(cp
->n_un
.n_name
)) {
813 if (cp
->n_value
> addr
)
815 if (file_sp
== 0 || file_sp
->n_value
<= cp
->n_value
)
817 } else if (cp
->n_value
<= addr
&&
818 (func_sp
== 0 || func_diff
> addr
- cp
->n_value
)) {
820 func_diff
= addr
- cp
->n_value
;
824 if (cp
->n_value
<= addr
&&
825 (func_sp
== 0 || func_diff
>= addr
- cp
->n_value
)) {
827 func_diff
= addr
- cp
->n_value
;
828 if (func_diff
== 0 && file_sp
&& func_sp
&& line_sp
== 0)
833 if ((cp
->n_value
> addr
) &&
834 (cp
->n_value
- addr
> db_search_maxoff
))
841 if (first_pass
&& (!file_sp
|| !line_sp
|| !func_sp
)) {
844 sp
= (struct nlist
*)stab
->start
;
845 for (; cp
>= sp
; cp
--) {
852 if ((found_line
|| (line_sp
&& line_diff
< X_DB_MAX_DIFF
))
861 if (aout_db_is_filename(cp
->n_un
.n_name
)) {
864 } else if (func_sp
== 0) {
866 func_diff
= addr
- cp
->n_value
;
872 func_diff
= addr
- cp
->n_value
;
873 if (func_diff
== 0 && file_sp
&& func_sp
878 if (line_sp
&& file_sp
&&
879 addr
- cp
->n_value
> db_search_maxoff
)
888 * XXX - barbou@gr.osf.org
889 * I don't know if that code is useful to something, but it makes the -gline
890 * option of gcc useless.
893 if (line_func
== 0 || func_sp
== 0
894 || line_func
->n_value
!= func_sp
->n_value
)
898 if (line_sp
&& !found_line
) {
904 *diff
= addr
- file_sp
->n_value
;
905 *file
= file_sp
->n_un
.n_name
;
908 *diff
= addr
- line_sp
->n_value
;
909 *line
= line_sp
->n_desc
;
912 *diff
= addr
- func_sp
->n_value
;
913 *func
= (func_sp
->n_un
.n_name
[0] == '_')?
914 func_sp
->n_un
.n_name
+ 1: func_sp
->n_un
.n_name
;
915 if (line_func
&& (line_func
->n_desc
& 0x4000))
916 *args
= line_func
->n_desc
& 0x3ff;
918 return((db_sym_t
) func_sp
);
922 * Find filename and lineno within, given the current pc.
937 found
= (aout_db_search_by_addr(stab
, (unsigned)pc
, file
, &func
, line
,
940 return(found
&& func
&& *file
);
944 * Initialization routine for a.out files.
949 extern struct mach_header _mh_execute_header
;
951 aout_db_sym_init((char *) &_mh_execute_header
,
952 (char *)0, "mach", (char *)0);
955 #endif /* DB_NO_AOUT */