3 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
23 #include "libiberty.h"
25 #include "search_list.h"
32 static int core_num_syms
;
33 static asymbol
**core_syms
;
34 asection
*core_text_sect
;
37 static int min_insn_size
;
40 /* For mapping symbols to specific .o files during file ordering. */
41 struct function_map
*symbol_map
;
42 unsigned int symbol_map_count
;
44 static void read_function_mappings (const char *);
45 static int core_sym_class (asymbol
*);
46 static bfd_boolean get_src_info
47 (bfd_vma
, const char **, const char **, int *);
49 extern void i386_find_call (Sym
*, bfd_vma
, bfd_vma
);
50 extern void alpha_find_call (Sym
*, bfd_vma
, bfd_vma
);
51 extern void vax_find_call (Sym
*, bfd_vma
, bfd_vma
);
52 extern void tahoe_find_call (Sym
*, bfd_vma
, bfd_vma
);
53 extern void sparc_find_call (Sym
*, bfd_vma
, bfd_vma
);
54 extern void mips_find_call (Sym
*, bfd_vma
, bfd_vma
);
57 read_function_mappings (const char *filename
)
59 FILE *file
= fopen (filename
, "r");
65 fprintf (stderr
, _("%s: could not open %s.\n"), whoami
, filename
);
69 /* First parse the mapping file so we know how big we need to
70 make our tables. We also do some sanity checks at this
76 matches
= fscanf (file
, "%[^\n:]", dummy
);
79 fprintf (stderr
, _("%s: unable to parse mapping file %s.\n"),
84 /* Just skip messages about files with no symbols. */
85 if (!strncmp (dummy
, "No symbols in ", 14))
91 /* Don't care what else is on this line at this point. */
92 fscanf (file
, "%[^\n]\n", dummy
);
96 /* Now we know how big we need to make our table. */
97 symbol_map
= ((struct function_map
*)
98 xmalloc (count
* sizeof (struct function_map
)));
100 /* Rewind the input file so we can read it again. */
103 /* Read each entry and put it into the table. */
110 matches
= fscanf (file
, "%[^\n:]", dummy
);
113 fprintf (stderr
, _("%s: unable to parse mapping file %s.\n"),
118 /* Just skip messages about files with no symbols. */
119 if (!strncmp (dummy
, "No symbols in ", 14))
125 /* dummy has the filename, go ahead and copy it. */
126 symbol_map
[count
].file_name
= xmalloc (strlen (dummy
) + 1);
127 strcpy (symbol_map
[count
].file_name
, dummy
);
129 /* Now we need the function name. */
130 fscanf (file
, "%[^\n]\n", dummy
);
131 tmp
= strrchr (dummy
, ' ') + 1;
132 symbol_map
[count
].function_name
= xmalloc (strlen (tmp
) + 1);
133 strcpy (symbol_map
[count
].function_name
, tmp
);
137 /* Record the size of the map table for future reference. */
138 symbol_map_count
= count
;
143 core_init (const char *aout_name
)
149 core_bfd
= bfd_openr (aout_name
, 0);
157 if (!bfd_check_format (core_bfd
, bfd_object
))
159 fprintf (stderr
, _("%s: %s: not in executable format\n"), whoami
, aout_name
);
163 /* Get core's text section. */
164 core_text_sect
= bfd_get_section_by_name (core_bfd
, ".text");
167 core_text_sect
= bfd_get_section_by_name (core_bfd
, "$CODE$");
170 fprintf (stderr
, _("%s: can't find .text section in %s\n"),
176 /* Read core's symbol table. */
178 /* This will probably give us more than we need, but that's ok. */
179 core_sym_bytes
= bfd_get_symtab_upper_bound (core_bfd
);
180 if (core_sym_bytes
< 0)
182 fprintf (stderr
, "%s: %s: %s\n", whoami
, aout_name
,
183 bfd_errmsg (bfd_get_error ()));
187 core_syms
= (asymbol
**) xmalloc (core_sym_bytes
);
188 core_num_syms
= bfd_canonicalize_symtab (core_bfd
, core_syms
);
190 if (core_num_syms
< 0)
192 fprintf (stderr
, "%s: %s: %s\n", whoami
, aout_name
,
193 bfd_errmsg (bfd_get_error ()));
197 synth_count
= bfd_get_synthetic_symtab (core_bfd
, core_num_syms
, core_syms
,
198 0, NULL
, &synthsyms
);
205 new_size
= (core_num_syms
+ synth_count
+ 1) * sizeof (*core_syms
);
206 core_syms
= xrealloc (core_syms
, new_size
);
207 symp
= core_syms
+ core_num_syms
;
208 core_num_syms
+= synth_count
;
209 for (i
= 0; i
< synth_count
; i
++)
210 *symp
++ = synthsyms
+ i
;
217 switch (bfd_get_arch (core_bfd
))
232 if (function_mapping_file
)
233 read_function_mappings (function_mapping_file
);
236 /* Read in the text space of an a.out file. */
239 core_get_text_space (bfd
*cbfd
)
241 core_text_space
= malloc (bfd_get_section_size (core_text_sect
));
243 if (!core_text_space
)
245 fprintf (stderr
, _("%s: ran out room for %lu bytes of text space\n"),
246 whoami
, (unsigned long) bfd_get_section_size (core_text_sect
));
250 if (!bfd_get_section_contents (cbfd
, core_text_sect
, core_text_space
,
251 0, bfd_get_section_size (core_text_sect
)))
253 bfd_perror ("bfd_get_section_contents");
254 free (core_text_space
);
258 if (!core_text_space
)
259 fprintf (stderr
, _("%s: can't do -c\n"), whoami
);
264 find_call (Sym
*parent
, bfd_vma p_lowpc
, bfd_vma p_highpc
)
266 if (core_text_space
== 0)
269 hist_clip_symbol_address (&p_lowpc
, &p_highpc
);
271 switch (bfd_get_arch (core_bfd
))
274 i386_find_call (parent
, p_lowpc
, p_highpc
);
278 alpha_find_call (parent
, p_lowpc
, p_highpc
);
282 vax_find_call (parent
, p_lowpc
, p_highpc
);
286 sparc_find_call (parent
, p_lowpc
, p_highpc
);
290 tahoe_find_call (parent
, p_lowpc
, p_highpc
);
294 mips_find_call (parent
, p_lowpc
, p_highpc
);
298 fprintf (stderr
, _("%s: -c not supported on architecture %s\n"),
299 whoami
, bfd_printable_name(core_bfd
));
301 /* Don't give the error more than once. */
302 ignore_direct_calls
= FALSE
;
306 /* Return class of symbol SYM. The returned class can be any of:
307 0 -> symbol is not interesting to us
308 'T' -> symbol is a global name
309 't' -> symbol is a local (static) name. */
312 core_sym_class (asymbol
*sym
)
319 if (sym
->section
== NULL
|| (sym
->flags
& BSF_DEBUGGING
) != 0)
322 /* Must be a text symbol, and static text symbols
323 don't qualify if ignore_static_funcs set. */
324 if (ignore_static_funcs
&& (sym
->flags
& BSF_LOCAL
))
326 DBG (AOUTDEBUG
, printf ("[core_sym_class] %s: not a function\n",
331 bfd_get_symbol_info (core_bfd
, sym
, &syminfo
);
335 return i
; /* It's a global symbol. */
338 /* Treat weak symbols as text symbols. FIXME: a weak symbol may
339 also be a data symbol. */
344 /* Not a static text symbol. */
345 DBG (AOUTDEBUG
, printf ("[core_sym_class] %s is of class %c\n",
350 /* Do some more filtering on static function-names. */
351 if (ignore_static_funcs
)
354 /* Can't zero-length name or funny characters in name, where
355 `funny' includes: `.' (.o file names) and `$' (Pascal labels). */
356 if (!sym
->name
|| sym
->name
[0] == '\0')
359 for (name
= sym
->name
; *name
; ++name
)
361 if (*name
== '.' || *name
== '$')
365 /* On systems where the C compiler adds an underscore to all
366 names, static names without underscores seem usually to be
367 labels in hand written assembler in the library. We don't want
368 these names. This is certainly necessary on a Sparc running
369 SunOS 4.1 (try profiling a program that does a lot of
370 division). I don't know whether it has harmful side effects on
371 other systems. Perhaps it should be made configurable. */
372 sym_prefix
= bfd_get_symbol_leading_char (core_bfd
);
374 if ((sym_prefix
&& sym_prefix
!= sym
->name
[0])
375 /* GCC may add special symbols to help gdb figure out the file
376 language. We want to ignore these, since sometimes they mask
377 the real function. (dj@ctron) */
378 || !strncmp (sym
->name
, "__gnu_compiled", 14)
379 || !strncmp (sym
->name
, "___gnu_compiled", 15))
384 /* If the object file supports marking of function symbols, then
385 we can zap anything that doesn't have BSF_FUNCTION set. */
386 if (ignore_non_functions
&& (sym
->flags
& BSF_FUNCTION
) == 0)
389 return 't'; /* It's a static text symbol. */
392 /* Get whatever source info we can get regarding address ADDR. */
395 get_src_info (bfd_vma addr
, const char **filename
, const char **name
, int *line_num
)
397 const char *fname
= 0, *func_name
= 0;
400 if (bfd_find_nearest_line (core_bfd
, core_text_sect
, core_syms
,
401 addr
- core_text_sect
->vma
,
402 &fname
, &func_name
, (unsigned int *) &l
)
403 && fname
&& func_name
&& l
)
405 DBG (AOUTDEBUG
, printf ("[get_src_info] 0x%lx -> %s:%d (%s)\n",
406 (unsigned long) addr
, fname
, l
, func_name
));
414 DBG (AOUTDEBUG
, printf ("[get_src_info] no info for 0x%lx (%s:%d,%s)\n",
415 (long) addr
, fname
? fname
: "<unknown>", l
,
416 func_name
? func_name
: "<unknown>"));
421 /* Read in symbol table from core.
422 One symbol per function is entered. */
425 core_create_function_syms ()
427 bfd_vma min_vma
= ~(bfd_vma
) 0;
433 /* Pass 1 - determine upper bound on number of function names. */
436 for (i
= 0; i
< core_num_syms
; ++i
)
438 if (!core_sym_class (core_syms
[i
]))
441 /* This should be replaced with a binary search or hashed
444 Don't create a symtab entry for a function that has
445 a mapping to a file, unless it's the first function
448 for (j
= 0; j
< symbol_map_count
; j
++)
449 if (!strcmp (core_syms
[i
]->name
, symbol_map
[j
].function_name
))
451 if (j
> 0 && ! strcmp (symbol_map
[j
].file_name
,
452 symbol_map
[j
- 1].file_name
))
463 fprintf (stderr
, _("%s: file `%s' has no symbols\n"), whoami
, a_out_name
);
467 /* The "+ 2" is for the sentinels. */
468 symtab
.base
= (Sym
*) xmalloc ((symtab
.len
+ 2) * sizeof (Sym
));
470 /* Pass 2 - create symbols. */
471 symtab
.limit
= symtab
.base
;
473 for (i
= 0; i
< core_num_syms
; ++i
)
477 class = core_sym_class (core_syms
[i
]);
482 printf ("[core_create_function_syms] rejecting: 0x%lx %s\n",
483 (unsigned long) core_syms
[i
]->value
,
484 core_syms
[i
]->name
));
488 /* This should be replaced with a binary search or hashed
493 for (j
= 0; j
< symbol_map_count
; j
++)
494 if (!strcmp (core_syms
[i
]->name
, symbol_map
[j
].function_name
))
496 if (j
> 0 && ! strcmp (symbol_map
[j
].file_name
,
497 symbol_map
[j
- 1].file_name
))
507 sym_init (symtab
.limit
);
509 /* Symbol offsets are always section-relative. */
510 sym_sec
= core_syms
[i
]->section
;
511 symtab
.limit
->addr
= core_syms
[i
]->value
;
513 symtab
.limit
->addr
+= bfd_get_section_vma (sym_sec
->owner
, sym_sec
);
516 && !strcmp (core_syms
[i
]->name
, symbol_map
[found
].function_name
))
518 symtab
.limit
->name
= symbol_map
[found
].file_name
;
519 symtab
.limit
->mapped
= 1;
523 symtab
.limit
->name
= core_syms
[i
]->name
;
524 symtab
.limit
->mapped
= 0;
527 /* Lookup filename and line number, if we can. */
529 const char *filename
, *func_name
;
531 if (get_src_info (symtab
.limit
->addr
, &filename
, &func_name
,
532 &symtab
.limit
->line_num
))
534 symtab
.limit
->file
= source_file_lookup_path (filename
);
536 /* FIXME: Checking __osf__ here does not work with a cross
539 /* Suppress symbols that are not function names. This is
540 useful to suppress code-labels and aliases.
542 This is known to be useful under DEC's OSF/1. Under SunOS 4.x,
543 labels do not appear in the symbol table info, so this isn't
546 if (strcmp (symtab
.limit
->name
, func_name
) != 0)
548 /* The symbol's address maps to a different name, so
549 it can't be a function-entry point. This happens
550 for labels, for example. */
552 printf ("[core_create_function_syms: rej %s (maps to %s)\n",
553 symtab
.limit
->name
, func_name
));
560 symtab
.limit
->is_func
= TRUE
;
561 symtab
.limit
->is_bb_head
= TRUE
;
564 symtab
.limit
->is_static
= TRUE
;
566 /* Keep track of the minimum and maximum vma addresses used by all
567 symbols. When computing the max_vma, use the ending address of the
568 section containing the symbol, if available. */
569 min_vma
= MIN (symtab
.limit
->addr
, min_vma
);
571 max_vma
= MAX (bfd_get_section_vma (sym_sec
->owner
, sym_sec
)
572 + bfd_section_size (sym_sec
->owner
, sym_sec
) - 1,
575 max_vma
= MAX (symtab
.limit
->addr
, max_vma
);
577 /* If we see "main" without an initial '_', we assume names
578 are *not* prefixed by '_'. */
579 if (symtab
.limit
->name
[0] == 'm' && discard_underscores
580 && strcmp (symtab
.limit
->name
, "main") == 0)
581 discard_underscores
= 0;
583 DBG (AOUTDEBUG
, printf ("[core_create_function_syms] %ld %s 0x%lx\n",
584 (long) (symtab
.limit
- symtab
.base
),
586 (unsigned long) symtab
.limit
->addr
));
590 /* Create sentinels. */
591 sym_init (symtab
.limit
);
592 symtab
.limit
->name
= "<locore>";
593 symtab
.limit
->addr
= 0;
594 symtab
.limit
->end_addr
= min_vma
- 1;
597 sym_init (symtab
.limit
);
598 symtab
.limit
->name
= "<hicore>";
599 symtab
.limit
->addr
= max_vma
+ 1;
600 symtab
.limit
->end_addr
= ~(bfd_vma
) 0;
603 symtab
.len
= symtab
.limit
- symtab
.base
;
604 symtab_finalize (&symtab
);
607 /* Read in symbol table from core.
608 One symbol per line of source code is entered. */
611 core_create_line_syms ()
613 char *prev_name
, *prev_filename
;
614 unsigned int prev_name_len
, prev_filename_len
;
615 bfd_vma vma
, min_vma
= ~(bfd_vma
) 0, max_vma
= 0;
616 Sym
*prev
, dummy
, *sentinel
, *sym
;
617 const char *filename
;
622 /* Create symbols for functions as usual. This is necessary in
623 cases where parts of a program were not compiled with -g. For
624 those parts we still want to get info at the function level. */
625 core_create_function_syms ();
627 /* Pass 1: count the number of symbols. */
629 /* To find all line information, walk through all possible
630 text-space addresses (one by one!) and get the debugging
631 info for each address. When the debugging info changes,
632 it is time to create a new symbol.
634 Of course, this is rather slow and it would be better if
635 BFD would provide an iterator for enumerating all line infos. */
636 prev_name_len
= PATH_MAX
;
637 prev_filename_len
= PATH_MAX
;
638 prev_name
= xmalloc (prev_name_len
);
639 prev_filename
= xmalloc (prev_filename_len
);
643 vma_high
= core_text_sect
->vma
+ bfd_get_section_size (core_text_sect
);
644 for (vma
= core_text_sect
->vma
; vma
< vma_high
; vma
+= min_insn_size
)
648 if (!get_src_info (vma
, &filename
, &dummy
.name
, &dummy
.line_num
)
649 || (prev_line_num
== dummy
.line_num
651 && strcmp (prev_name
, dummy
.name
) == 0
652 && strcmp (prev_filename
, filename
) == 0))
656 prev_line_num
= dummy
.line_num
;
658 len
= strlen (dummy
.name
);
659 if (len
>= prev_name_len
)
661 prev_name_len
= len
+ 1024;
663 prev_name
= xmalloc (prev_name_len
);
666 strcpy (prev_name
, dummy
.name
);
667 len
= strlen (filename
);
669 if (len
>= prev_filename_len
)
671 prev_filename_len
= len
+ 1024;
672 free (prev_filename
);
673 prev_filename
= xmalloc (prev_filename_len
);
676 strcpy (prev_filename
, filename
);
678 min_vma
= MIN (vma
, min_vma
);
679 max_vma
= MAX (vma
, max_vma
);
683 free (prev_filename
);
685 /* Make room for function symbols, too. */
686 ltab
.len
+= symtab
.len
;
687 ltab
.base
= (Sym
*) xmalloc (ltab
.len
* sizeof (Sym
));
688 ltab
.limit
= ltab
.base
;
690 /* Pass 2 - create symbols. */
692 /* We now set is_static as we go along, rather than by running
693 through the symbol table at the end.
695 The old way called symtab_finalize before the is_static pass,
696 causing a problem since symtab_finalize uses is_static as part of
697 its address conflict resolution algorithm. Since global symbols
698 were prefered over static symbols, and all line symbols were
699 global at that point, static function names that conflicted with
700 their own line numbers (static, but labeled as global) were
701 rejected in favor of the line num.
703 This was not the desired functionality. We always want to keep
704 our function symbols and discard any conflicting line symbols.
705 Perhaps symtab_finalize should be modified to make this
706 distinction as well, but the current fix works and the code is a
710 for (vma
= core_text_sect
->vma
; vma
< vma_high
; vma
+= min_insn_size
)
712 sym_init (ltab
.limit
);
714 if (!get_src_info (vma
, &filename
, <ab
.limit
->name
, <ab
.limit
->line_num
)
715 || (prev
&& prev
->line_num
== ltab
.limit
->line_num
716 && strcmp (prev
->name
, ltab
.limit
->name
) == 0
717 && strcmp (prev
->file
->name
, filename
) == 0))
720 /* Make name pointer a malloc'ed string. */
721 ltab
.limit
->name
= xstrdup (ltab
.limit
->name
);
722 ltab
.limit
->file
= source_file_lookup_path (filename
);
724 ltab
.limit
->addr
= vma
;
726 /* Set is_static based on the enclosing function, using either:
727 1) the previous symbol, if it's from the same function, or
728 2) a symtab lookup. */
729 if (prev
&& ltab
.limit
->file
== prev
->file
&&
730 strcmp (ltab
.limit
->name
, prev
->name
) == 0)
732 ltab
.limit
->is_static
= prev
->is_static
;
736 sym
= sym_lookup(&symtab
, ltab
.limit
->addr
);
737 ltab
.limit
->is_static
= sym
->is_static
;
742 /* If we see "main" without an initial '_', we assume names
743 are *not* prefixed by '_'. */
744 if (ltab
.limit
->name
[0] == 'm' && discard_underscores
745 && strcmp (ltab
.limit
->name
, "main") == 0)
746 discard_underscores
= 0;
748 DBG (AOUTDEBUG
, printf ("[core_create_line_syms] %lu %s 0x%lx\n",
749 (unsigned long) (ltab
.limit
- ltab
.base
),
751 (unsigned long) ltab
.limit
->addr
));
755 /* Update sentinels. */
756 sentinel
= sym_lookup (&symtab
, (bfd_vma
) 0);
759 && strcmp (sentinel
->name
, "<locore>") == 0
760 && min_vma
<= sentinel
->end_addr
)
761 sentinel
->end_addr
= min_vma
- 1;
763 sentinel
= sym_lookup (&symtab
, ~(bfd_vma
) 0);
766 && strcmp (sentinel
->name
, "<hicore>") == 0
767 && max_vma
>= sentinel
->addr
)
768 sentinel
->addr
= max_vma
+ 1;
770 /* Copy in function symbols. */
771 memcpy (ltab
.limit
, symtab
.base
, symtab
.len
* sizeof (Sym
));
772 ltab
.limit
+= symtab
.len
;
774 if ((unsigned int) (ltab
.limit
- ltab
.base
) != ltab
.len
)
777 _("%s: somebody miscounted: ltab.len=%d instead of %ld\n"),
778 whoami
, ltab
.len
, (long) (ltab
.limit
- ltab
.base
));
782 /* Finalize ltab and make it symbol table. */
783 symtab_finalize (<ab
);