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@
29 * Mach Operating System
30 * Copyright (c) 1991,1990 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
56 * Author: David B. Golub, Carnegie Mellon University
60 * Symbol table routines for a.out format files.
62 #include <mach/boolean.h>
63 #include <mach/std_types.h>
64 #include <machine/db_machdep.h> /* data types */
65 #include <string.h> /* For strcpy(), strcmp() */
66 #include <ddb/db_aout.h>
67 #include <ddb/db_output.h> /* For db_printf() */
68 #include <ddb/db_sym.h>
72 #include <ddb/nlist.h> /* a.out symbol table */
75 #define private static
77 private int aout_db_order_symbols(char *, char *);
78 private int aout_db_compare_symbols(char *, char *);
79 private boolean_t
aout_db_is_filename(char *);
80 private boolean_t
aout_db_eq_name(struct nlist
*, char *, int);
83 * An a.out symbol table as loaded into the kernel debugger:
85 * symtab -> size of symbol entries, in bytes
86 * sp -> first symbol entry
88 * ep -> last symbol entry + 1
89 * strtab == start of string table
90 * size of string table in bytes,
96 * Find pointers to the start and end of the symbol entries,
97 * given a pointer to the start of the symbol table.
99 #define db_get_aout_symtab(symtab, sp, ep) \
100 (sp = (struct nlist *)(((vm_offset_t *)(symtab)) + 1), \
101 ep = (struct nlist *)((char *)sp + *((int *)(symtab))))
103 char *db_sorting_sym_end
;
106 aout_db_order_symbols(
110 struct nlist
*sym1
= (struct nlist
*) s1
;
111 struct nlist
*sym2
= (struct nlist
*) s2
;
113 if (sym1
->n_value
!= sym2
->n_value
)
114 return (sym1
->n_value
- sym2
->n_value
);
116 return (sym1
->n_un
.n_name
- sym2
->n_un
.n_name
);
121 aout_db_compare_symbols(
125 return (((struct nlist
*) sym1
)->n_value
-
126 ((struct nlist
*) sym2
)->n_value
);
129 int db_sorting_limit
= 50000;
133 char * symtab
, /* pointer to start of symbol table */
134 char * esymtab
, /* pointer to end of string table,
135 for checking - may be rounded up to
138 char * task_addr
) /* use for this task only */
140 struct nlist
*sym_start
, *sym_end
, *dbsym_start
, *dbsym_end
;
142 char *strtab
, *dbstrtab
;
144 char *estrtab
, *dbestrtab
;
145 unsigned long minsym
= ~0;
146 unsigned long maxsym
= 0;
149 extern boolean_t
getsymtab(char *,
150 vm_offset_t
*, int *,
151 vm_offset_t
*, vm_size_t
*);
155 if (!getsymtab(symtab
,
156 (vm_offset_t
*)&sym_start
, &nsyms
,
157 (vm_offset_t
*)&strtab
, (vm_size_t
*)&strlen
)) {
160 sym_end
= sym_start
+ nsyms
;
161 estrtab
= strtab
+ strlen
;
164 * We haven't actually started up VM yet, so we can just steal some pages to
165 * make a working copy of the symbols and strings
168 dbsym_start
= (struct nlist
*)pmap_steal_memory(((unsigned int)sym_end
- (unsigned int)sym_start
+ 4096) & -4096); /* Get space for symbols */
169 dbstrtab
= (char *)pmap_steal_memory(((unsigned int)estrtab
- (unsigned int)strtab
+ 4096) & -4096); /* Get space for strings */
171 bcopy((char *)sym_start
, (char *)dbsym_start
, (unsigned int)sym_end
- (unsigned int)sym_start
); /* Copy symbols */
172 bcopy(strtab
, dbstrtab
, (unsigned int)estrtab
- (unsigned int)strtab
); /* Copy strings */
174 dbsym_end
= dbsym_start
+ nsyms
;
175 dbestrtab
= dbstrtab
+ strlen
;
177 sorting
= ((dbsym_end
- dbsym_start
) < db_sorting_limit
);
179 for (sp
= dbsym_start
; sp
< dbsym_end
; sp
++) {
181 strx
= sp
->n_un
.n_strx
;
187 sp
->n_un
.n_name
= dbstrtab
+ strx
;
189 if (sp
->n_type
!= N_ABS
) {
190 if (sp
->n_value
> 0 && sp
->n_value
< minsym
)
191 minsym
= sp
->n_value
;
192 if (sp
->n_value
> maxsym
)
193 maxsym
= sp
->n_value
;
201 db_qsort((char *) dbsym_start
, dbsym_end
- dbsym_start
,
202 sizeof (struct nlist
), aout_db_order_symbols
);
207 if (db_add_symbol_table(SYMTAB_AOUT
,
217 /* Successfully added symbol table */
219 pmap_protect(kernel_pmap
,
220 (vm_offset_t
) dbsym_start
, (vm_offset_t
) dbsym_end
,
221 VM_PROT_READ
|VM_PROT_WRITE
);
222 pmap_protect(kernel_pmap
,
223 (vm_offset_t
) dbstrtab
, (vm_offset_t
) dbestrtab
,
224 VM_PROT_READ
|VM_PROT_WRITE
);
231 * This KLUDGE offsets the n_values of a copied symbol table
233 void db_clone_offsetXXX(char *, long);
235 db_clone_offsetXXX(char * symtab
, long offset
)
237 register struct nlist
*sym_start
, *sym_end
, *sp
;
239 db_get_aout_symtab((int *)symtab
, sym_start
, sym_end
);
241 for (sp
= sym_start
; sp
< sym_end
; sp
++)
242 if (sp
->n_type
!= N_ABS
)
243 sp
->n_value
+= offset
;
248 * check file name or not (check xxxx.x pattern)
251 aout_db_is_filename(char *name
)
264 * special name comparison routine with a name in the symbol table entry
272 register char *s1
, *s2
;
274 s1
= sp
->n_un
.n_name
;
276 #ifndef __NO_UNDERSCORES__
277 if (*s1
== '_' && *s2
&& *s2
!= '_')
279 #endif /* __NO_UNDERSCORES__ */
281 if (*s1
++ != *s2
++) {
283 * check .c .o file name comparison case
285 if (*s2
== 0 && sp
->n_un
.n_name
<= s1
- 2
286 && s1
[-2] == '.' && s1
[-1] == 'o')
294 * do special check for
296 * xxx.ttt for N_DATA and N_BSS
298 return(*s1
== 0 || (*s1
== ':' && sp
->n_type
== N_FUN
) ||
299 (*s1
== '.' && (sp
->n_type
== N_DATA
|| sp
->n_type
== N_BSS
)));
303 * search a symbol table with name and type
304 * fp(in,out): last found text file name symbol entry
306 private struct nlist
*
315 struct nlist
*file_sp
= *fp
;
316 struct nlist
*found_sp
= 0;
318 for ( ; sp
< ep
; sp
++) {
321 if (sp
->n_type
== N_TEXT
&& aout_db_is_filename(sp
->n_un
.n_name
))
324 if (sp
->n_type
== type
) {
325 /* dwm_debug: b26 name, mk6 added last param */
326 if (aout_db_eq_name(sp
, name
, 0))
329 if (sp
->n_type
== N_SO
)
333 if (sp
->n_type
& N_STAB
)
335 if (sp
->n_un
.n_name
&& aout_db_eq_name(sp
, name
, incomplete
)) {
337 * In case of qaulified search by a file,
338 * return it immediately with some check.
339 * Otherwise, search external one
342 if ((file_sp
== *fp
) || (sp
->n_type
& N_EXT
))
344 } else if ((sp
->n_type
& N_EXT
) ||
345 (incomplete
&& !aout_db_is_filename(sp
->n_un
.n_name
)))
355 * Print sorted possible completions for a symbol.
356 * Use n_other field to mark completion symbols in order
360 aout_db_qualified_print_completion(
368 struct nlist
*fp
= 0;
377 sp
= aout_db_search_name((struct nlist
*)stab
->start
,
378 (struct nlist
*)stab
->end
,
380 if (sp
== (struct nlist
*)0)
383 symlen
= strlen(sym
);
386 if (strncmp(cur
->n_un
.n_name
, sym
, symlen
) == 0)
391 cur
= aout_db_search_name(cur
+ 1, (struct nlist
*)stab
->end
,
402 if (strcmp(&cur
->n_un
.n_name
[cur
->n_other
- 1],
403 &new->n_un
.n_name
[new->n_other
- 1]) < 0)
410 if ((new->n_type
& N_EXT
) == 0) {
411 for (cur
= new - 1; cur
> (struct nlist
*)stab
->start
; cur
--) {
412 if (cur
->n_type
== N_SO
||
413 (stab
->sorted
&& cur
->n_value
< new->n_value
))
416 cur
->n_type
== N_SLINE
&&
417 cur
->n_value
== new->n_value
)
420 cur
->n_type
== N_FUN
&&
421 cur
->n_value
== new->n_value
)
425 if (cur
->n_type
== N_SO
)
426 fname
= cur
->n_un
.n_name
;
430 if (line
== 0 || func
== 0)
432 cur
< (struct nlist
*)stab
->end
; cur
++) {
433 if (cur
->n_type
== N_SO
||
434 (stab
->sorted
&& cur
->n_value
> new->n_value
))
437 cur
->n_type
== N_SLINE
&&
438 cur
->n_value
== new->n_value
) {
444 cur
->n_type
== N_FUN
&&
445 cur
->n_value
== new->n_value
) {
453 for (cur
= new - 1; cur
> (struct nlist
*)stab
->start
; cur
--) {
454 if (cur
->n_type
== N_SO
||
455 (stab
->sorted
&& cur
->n_value
< new->n_value
))
458 cur
->n_type
== N_FUN
&&
459 cur
->n_value
== new->n_value
)
464 cur
< (struct nlist
*)stab
->end
; cur
++) {
465 if (cur
->n_type
== N_SO
||
466 (stab
->sorted
&& cur
->n_value
> new->n_value
))
468 if (cur
->n_type
== N_FUN
&&
469 cur
->n_value
== new->n_value
) {
476 db_sym_print_completion(stab
, &new->n_un
.n_name
[new->n_other
- 1],
485 } else if (new == sp1
)
495 * search a (possibly incomplete) symbol with file, func and line qualification
498 aout_db_qualified_search(
507 register struct nlist
*sp
= (struct nlist
*)stab
->start
;
508 struct nlist
*ep
= (struct nlist
*)stab
->end
;
509 struct nlist
*fp
= 0;
510 struct nlist
*found_sp
;
511 unsigned long func_top
;
517 if (file
== 0 && sym
== 0)
520 if ((sp
= aout_db_search_name(sp
, ep
, file
, N_TEXT
, &fp
, 0)) == 0)
525 sp
= aout_db_search_name(sp
, ep
, sym
, (line
> 0)? N_FUN
: 0, &fp
,
526 (ret
== (db_sym_t
*)0));
532 if (strncmp(sp
->n_un
.n_name
, sym
, strlen(sym
)) == 0)
535 p
= &sp
->n_un
.n_name
[1];
537 if (*name
== (char *)0) {
541 for (i
= 0; i
< *len
; i
++)
542 if ((*name
)[i
] != p
[i
]) {
553 if (file
&& !aout_db_eq_name(fp
, file
, 0))
556 if (sp
->n_type
== N_FUN
) {
558 * qualfied by function name
559 * search backward because line number entries
560 * for the function are above it in this case.
562 func_top
= sp
->n_value
;
564 /* symbols with the same value may have been mixed up */
567 } while (sp
->n_value
== func_top
);
569 for (sp
--; sp
>= (struct nlist
*)stab
->start
; sp
--) {
570 if (sp
->n_type
!= N_SLINE
)
572 if (sp
->n_value
< func_top
)
574 if (sp
->n_desc
<= line
) {
575 if (found_sp
== 0 || found_sp
->n_desc
< sp
->n_desc
)
577 if (sp
->n_desc
== line
)
581 if (sp
->n_type
!= N_SLINE
|| sp
->n_value
< func_top
)
585 * qualified by only file name
586 * search forward in this case
590 /* symbols with the same value may have been mixed up */
591 func_top
= sp
->n_value
;
594 } while (sp
->n_value
== func_top
);
596 for (sp
++; sp
< ep
; sp
++) {
597 if (sp
->n_type
== N_TEXT
598 && aout_db_is_filename(sp
->n_un
.n_name
))
599 break; /* enter into another file */
600 if (sp
->n_type
== N_SOL
) {
601 in_file
= aout_db_eq_name(sp
, file
, 0);
604 if (!in_file
|| sp
->n_type
!= N_SLINE
)
606 if (sp
->n_desc
<= line
) {
607 if (found_sp
== 0 || found_sp
->n_desc
< sp
->n_desc
)
609 if (sp
->n_desc
== line
)
616 *ret
= (db_sym_t
) sp
;
621 * lookup symbol by name
628 return(db_sym_parse_and_lookup(aout_db_qualified_search
, stab
, symstr
));
632 * lookup (possibly incomplete) symbol by name
635 aout_db_lookup_incomplete(
642 return(db_sym_parse_and_lookup_incomplete(aout_db_qualified_search
,
643 stab
, symstr
, name
, len
, toadd
));
647 * Display possible completion for the symbol
650 aout_db_print_completion(stab
, symstr
)
655 return(db_sym_parse_and_print_completion(aout_db_qualified_print_completion
,
660 aout_db_search_symbol(
663 db_strategy_t strategy
,
664 db_expr_t
*diffp
) /* in/out */
666 register unsigned long diff
= *diffp
;
667 register struct nlist
*symp
= 0;
668 struct nlist
*sp
, *ep
, *cp
;
669 boolean_t first_pass
= FALSE
;
671 sp
= (struct nlist
*)symtab
->start
;
672 ep
= (struct nlist
*)symtab
->end
;
674 if (symtab
->sorted
) {
677 target
.n_value
= off
;
678 target
.n_un
.n_name
= (char *) 0;
679 target
.n_other
= (char) 0;
680 db_qsort_limit_search((char *) &target
, (char **) &sp
, (char **) &ep
,
681 sizeof (struct nlist
), aout_db_compare_symbols
);
686 for (cp
= ep
-1; cp
>= sp
; cp
--) {
687 if (cp
->n_un
.n_name
== 0)
689 if ((cp
->n_type
& N_STAB
) != 0)
691 if (strategy
== DB_STGY_XTRN
&& (cp
->n_type
& N_EXT
) == 0)
693 if (off
>= cp
->n_value
) {
694 if (off
- cp
->n_value
< diff
) {
695 diff
= off
- cp
->n_value
;
697 if (diff
== 0 && (cp
->n_type
& N_EXT
))
700 else if (off
- cp
->n_value
== diff
) {
703 else if ((symp
->n_type
& N_EXT
) == 0 &&
704 (cp
->n_type
& N_EXT
) != 0)
705 symp
= cp
; /* pick the external symbol */
712 sp
= (struct nlist
*) symtab
->start
;
720 return ((db_sym_t
)symp
);
724 * Return the name and value for a symbol.
727 aout_db_symbol_values(
732 register struct nlist
*sp
;
734 sp
= (struct nlist
*)sym
;
736 *namep
= sp
->n_un
.n_name
;
738 *valuep
= sp
->n_value
;
741 #define X_DB_MAX_DIFF 8 /* maximum allowable diff at the end of line */
742 extern int db_search_maxoff
; /* maximum acceptable offset */
745 * search symbol by value
748 aout_db_search_by_addr(
757 struct nlist
*sp
, *cp
;
758 register struct nlist
*line_sp
, *func_sp
, *file_sp
, *line_func
;
759 unsigned long func_diff
, line_diff
;
760 boolean_t found_line
= FALSE
;
761 struct nlist
*ep
= (struct nlist
*)stab
->end
;
762 boolean_t first_pass
= FALSE
;
766 * Added init of these two... not sure if it's correct, but
767 * can't be worse than random values.... -- jfriedl@omron.co.jp
769 func_diff
= line_diff
= /*HUGE*/0x0fffffff;
771 line_sp
= func_sp
= file_sp
= line_func
= 0;
776 sp
= (struct nlist
*)stab
->start
;
780 target
.n_value
= addr
;
781 target
.n_un
.n_name
= (char *) 0;
782 target
.n_other
= (char) 0;
783 db_qsort_limit_search((char *) &target
, (char **) &sp
,
784 (char **) &ep
, sizeof (struct nlist
),
785 aout_db_compare_symbols
);
789 for (cp
= sp
; cp
< ep
; cp
++) {
792 if (cp
->n_value
<= addr
) {
793 if (line_sp
== 0 || line_diff
>= addr
- cp
->n_value
) {
797 line_diff
= addr
- cp
->n_value
;
800 if (cp
->n_value
>= addr
&& line_sp
)
804 if ((found_line
|| (line_sp
&& line_diff
< X_DB_MAX_DIFF
))
809 if (cp
->n_value
> addr
)
811 if (file_sp
== 0 || file_sp
->n_value
<= cp
->n_value
)
815 if (aout_db_is_filename(cp
->n_un
.n_name
)) {
816 if (cp
->n_value
> addr
)
818 if (file_sp
== 0 || file_sp
->n_value
<= cp
->n_value
)
820 } else if (cp
->n_value
<= addr
&&
821 (func_sp
== 0 || func_diff
> addr
- cp
->n_value
)) {
823 func_diff
= addr
- cp
->n_value
;
827 if (cp
->n_value
<= addr
&&
828 (func_sp
== 0 || func_diff
>= addr
- cp
->n_value
)) {
830 func_diff
= addr
- cp
->n_value
;
831 if (func_diff
== 0 && file_sp
&& func_sp
&& line_sp
== 0)
836 if ((cp
->n_value
> addr
) &&
837 (cp
->n_value
- addr
> db_search_maxoff
))
844 if (first_pass
&& (!file_sp
|| !line_sp
|| !func_sp
)) {
847 sp
= (struct nlist
*)stab
->start
;
848 for (; cp
>= sp
; cp
--) {
855 if ((found_line
|| (line_sp
&& line_diff
< X_DB_MAX_DIFF
))
864 if (aout_db_is_filename(cp
->n_un
.n_name
)) {
867 } else if (func_sp
== 0) {
869 func_diff
= addr
- cp
->n_value
;
875 func_diff
= addr
- cp
->n_value
;
876 if (func_diff
== 0 && file_sp
&& func_sp
881 if (line_sp
&& file_sp
&&
882 addr
- cp
->n_value
> db_search_maxoff
)
891 * XXX - barbou@gr.osf.org
892 * I don't know if that code is useful to something, but it makes the -gline
893 * option of gcc useless.
896 if (line_func
== 0 || func_sp
== 0
897 || line_func
->n_value
!= func_sp
->n_value
)
901 if (line_sp
&& !found_line
) {
907 *diff
= addr
- file_sp
->n_value
;
908 *file
= file_sp
->n_un
.n_name
;
911 *diff
= addr
- line_sp
->n_value
;
912 *line
= line_sp
->n_desc
;
915 *diff
= addr
- func_sp
->n_value
;
916 *func
= (func_sp
->n_un
.n_name
[0] == '_')?
917 func_sp
->n_un
.n_name
+ 1: func_sp
->n_un
.n_name
;
918 if (line_func
&& (line_func
->n_desc
& 0x4000))
919 *args
= line_func
->n_desc
& 0x3ff;
921 return((db_sym_t
) func_sp
);
925 * Find filename and lineno within, given the current pc.
940 found
= (aout_db_search_by_addr(stab
, (unsigned)pc
, file
, &func
, line
,
943 return(found
&& func
&& *file
);
947 * Initialization routine for a.out files.
952 extern struct mach_header _mh_execute_header
;
954 aout_db_sym_init((char *) &_mh_execute_header
,
955 (char *)0, "mach", (char *)0);
958 #endif /* DB_NO_AOUT */