3 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
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 3 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
24 #include "libiberty.h"
25 #include "search_list.h"
30 #include "safe-ctype.h"
35 static int core_num_syms
;
36 static asymbol
**core_syms
;
37 asection
*core_text_sect
;
40 static int min_insn_size
;
43 /* For mapping symbols to specific .o files during file ordering. */
44 struct function_map
*symbol_map
;
45 unsigned int symbol_map_count
;
47 static void read_function_mappings (const char *);
48 static int core_sym_class (asymbol
*);
49 static bfd_boolean get_src_info
50 (bfd_vma
, const char **, const char **, int *);
52 extern void i386_find_call (Sym
*, bfd_vma
, bfd_vma
);
53 extern void alpha_find_call (Sym
*, bfd_vma
, bfd_vma
);
54 extern void vax_find_call (Sym
*, bfd_vma
, bfd_vma
);
55 extern void tahoe_find_call (Sym
*, bfd_vma
, bfd_vma
);
56 extern void sparc_find_call (Sym
*, bfd_vma
, bfd_vma
);
57 extern void mips_find_call (Sym
*, bfd_vma
, bfd_vma
);
60 parse_error (const char *filename
)
62 fprintf (stderr
, _("%s: unable to parse mapping file %s.\n"), whoami
, filename
);
67 read_function_mappings (const char *filename
)
69 FILE *file
= fopen (filename
, "r");
75 fprintf (stderr
, _("%s: could not open %s.\n"), whoami
, filename
);
79 /* First parse the mapping file so we know how big we need to
80 make our tables. We also do some sanity checks at this
86 matches
= fscanf (file
, "%[^\n:]", dummy
);
88 parse_error (filename
);
90 /* Just skip messages about files with no symbols. */
91 if (!strncmp (dummy
, "No symbols in ", 14))
93 matches
= fscanf (file
, "\n");
95 parse_error (filename
);
99 /* Don't care what else is on this line at this point. */
100 matches
= fscanf (file
, "%[^\n]\n", dummy
);
102 parse_error (filename
);
106 /* Now we know how big we need to make our table. */
107 symbol_map
= ((struct function_map
*)
108 xmalloc (count
* sizeof (struct function_map
)));
110 /* Rewind the input file so we can read it again. */
113 /* Read each entry and put it into the table. */
120 matches
= fscanf (file
, "%[^\n:]", dummy
);
122 parse_error (filename
);
124 /* Just skip messages about files with no symbols. */
125 if (!strncmp (dummy
, "No symbols in ", 14))
127 matches
= fscanf (file
, "\n");
129 parse_error (filename
);
133 /* dummy has the filename, go ahead and copy it. */
134 symbol_map
[count
].file_name
= xmalloc (strlen (dummy
) + 1);
135 strcpy (symbol_map
[count
].file_name
, dummy
);
137 /* Now we need the function name. */
138 matches
= fscanf (file
, "%[^\n]\n", dummy
);
140 parse_error (filename
);
141 tmp
= strrchr (dummy
, ' ') + 1;
142 symbol_map
[count
].function_name
= xmalloc (strlen (tmp
) + 1);
143 strcpy (symbol_map
[count
].function_name
, tmp
);
147 /* Record the size of the map table for future reference. */
148 symbol_map_count
= count
;
153 core_init (const char *aout_name
)
159 core_bfd
= bfd_openr (aout_name
, 0);
167 if (!bfd_check_format (core_bfd
, bfd_object
))
169 fprintf (stderr
, _("%s: %s: not in executable format\n"), whoami
, aout_name
);
173 /* Get core's text section. */
174 core_text_sect
= bfd_get_section_by_name (core_bfd
, ".text");
177 core_text_sect
= bfd_get_section_by_name (core_bfd
, "$CODE$");
180 fprintf (stderr
, _("%s: can't find .text section in %s\n"),
186 /* Read core's symbol table. */
188 /* This will probably give us more than we need, but that's ok. */
189 core_sym_bytes
= bfd_get_symtab_upper_bound (core_bfd
);
190 if (core_sym_bytes
< 0)
192 fprintf (stderr
, "%s: %s: %s\n", whoami
, aout_name
,
193 bfd_errmsg (bfd_get_error ()));
197 core_syms
= (asymbol
**) xmalloc (core_sym_bytes
);
198 core_num_syms
= bfd_canonicalize_symtab (core_bfd
, core_syms
);
200 if (core_num_syms
< 0)
202 fprintf (stderr
, "%s: %s: %s\n", whoami
, aout_name
,
203 bfd_errmsg (bfd_get_error ()));
207 synth_count
= bfd_get_synthetic_symtab (core_bfd
, core_num_syms
, core_syms
,
208 0, NULL
, &synthsyms
);
215 new_size
= (core_num_syms
+ synth_count
+ 1) * sizeof (*core_syms
);
216 core_syms
= xrealloc (core_syms
, new_size
);
217 symp
= core_syms
+ core_num_syms
;
218 core_num_syms
+= synth_count
;
219 for (i
= 0; i
< synth_count
; i
++)
220 *symp
++ = synthsyms
+ i
;
227 switch (bfd_get_arch (core_bfd
))
242 if (function_mapping_file
)
243 read_function_mappings (function_mapping_file
);
246 /* Read in the text space of an a.out file. */
249 core_get_text_space (bfd
*cbfd
)
251 core_text_space
= malloc (bfd_get_section_size (core_text_sect
));
253 if (!core_text_space
)
255 fprintf (stderr
, _("%s: ran out room for %lu bytes of text space\n"),
256 whoami
, (unsigned long) bfd_get_section_size (core_text_sect
));
260 if (!bfd_get_section_contents (cbfd
, core_text_sect
, core_text_space
,
261 0, bfd_get_section_size (core_text_sect
)))
263 bfd_perror ("bfd_get_section_contents");
264 free (core_text_space
);
268 if (!core_text_space
)
269 fprintf (stderr
, _("%s: can't do -c\n"), whoami
);
274 find_call (Sym
*parent
, bfd_vma p_lowpc
, bfd_vma p_highpc
)
276 if (core_text_space
== 0)
279 hist_clip_symbol_address (&p_lowpc
, &p_highpc
);
281 switch (bfd_get_arch (core_bfd
))
284 i386_find_call (parent
, p_lowpc
, p_highpc
);
288 alpha_find_call (parent
, p_lowpc
, p_highpc
);
292 vax_find_call (parent
, p_lowpc
, p_highpc
);
296 sparc_find_call (parent
, p_lowpc
, p_highpc
);
300 tahoe_find_call (parent
, p_lowpc
, p_highpc
);
304 mips_find_call (parent
, p_lowpc
, p_highpc
);
308 fprintf (stderr
, _("%s: -c not supported on architecture %s\n"),
309 whoami
, bfd_printable_name(core_bfd
));
311 /* Don't give the error more than once. */
312 ignore_direct_calls
= FALSE
;
316 /* Return class of symbol SYM. The returned class can be any of:
317 0 -> symbol is not interesting to us
318 'T' -> symbol is a global name
319 't' -> symbol is a local (static) name. */
322 core_sym_class (asymbol
*sym
)
329 if (sym
->section
== NULL
|| (sym
->flags
& BSF_DEBUGGING
) != 0)
332 /* Must be a text symbol, and static text symbols
333 don't qualify if ignore_static_funcs set. */
334 if (ignore_static_funcs
&& (sym
->flags
& BSF_LOCAL
))
336 DBG (AOUTDEBUG
, printf ("[core_sym_class] %s: not a function\n",
341 bfd_get_symbol_info (core_bfd
, sym
, &syminfo
);
345 return i
; /* It's a global symbol. */
348 /* Treat weak symbols as text symbols. FIXME: a weak symbol may
349 also be a data symbol. */
354 /* Not a static text symbol. */
355 DBG (AOUTDEBUG
, printf ("[core_sym_class] %s is of class %c\n",
360 /* Do some more filtering on static function-names. */
361 if (ignore_static_funcs
)
364 /* Can't zero-length name or funny characters in name, where
365 `funny' includes: `.' (.o file names) and `$' (Pascal labels). */
366 if (!sym
->name
|| sym
->name
[0] == '\0')
369 for (name
= sym
->name
; *name
; ++name
)
374 /* Do not discard nested subprograms (those
375 which end with .NNN, where N are digits). */
377 for (name
++; *name
; name
++)
378 if (! ISDIGIT (*name
))
382 /* On systems where the C compiler adds an underscore to all
383 names, static names without underscores seem usually to be
384 labels in hand written assembler in the library. We don't want
385 these names. This is certainly necessary on a Sparc running
386 SunOS 4.1 (try profiling a program that does a lot of
387 division). I don't know whether it has harmful side effects on
388 other systems. Perhaps it should be made configurable. */
389 sym_prefix
= bfd_get_symbol_leading_char (core_bfd
);
391 if ((sym_prefix
&& sym_prefix
!= sym
->name
[0])
392 /* GCC may add special symbols to help gdb figure out the file
393 language. We want to ignore these, since sometimes they mask
394 the real function. (dj@ctron) */
395 || !strncmp (sym
->name
, "__gnu_compiled", 14)
396 || !strncmp (sym
->name
, "___gnu_compiled", 15))
401 /* If the object file supports marking of function symbols, then
402 we can zap anything that doesn't have BSF_FUNCTION set. */
403 if (ignore_non_functions
&& (sym
->flags
& BSF_FUNCTION
) == 0)
406 return 't'; /* It's a static text symbol. */
409 /* Get whatever source info we can get regarding address ADDR. */
412 get_src_info (bfd_vma addr
, const char **filename
, const char **name
, int *line_num
)
414 const char *fname
= 0, *func_name
= 0;
417 if (bfd_find_nearest_line (core_bfd
, core_text_sect
, core_syms
,
418 addr
- core_text_sect
->vma
,
419 &fname
, &func_name
, (unsigned int *) &l
)
420 && fname
&& func_name
&& l
)
422 DBG (AOUTDEBUG
, printf ("[get_src_info] 0x%lx -> %s:%d (%s)\n",
423 (unsigned long) addr
, fname
, l
, func_name
));
431 DBG (AOUTDEBUG
, printf ("[get_src_info] no info for 0x%lx (%s:%d,%s)\n",
432 (unsigned long) addr
,
433 fname
? fname
: "<unknown>", l
,
434 func_name
? func_name
: "<unknown>"));
439 /* Read in symbol table from core.
440 One symbol per function is entered. */
443 core_create_function_syms ()
445 bfd_vma min_vma
= ~(bfd_vma
) 0;
451 /* Pass 1 - determine upper bound on number of function names. */
454 for (i
= 0; i
< core_num_syms
; ++i
)
456 if (!core_sym_class (core_syms
[i
]))
459 /* This should be replaced with a binary search or hashed
462 Don't create a symtab entry for a function that has
463 a mapping to a file, unless it's the first function
466 for (j
= 0; j
< symbol_map_count
; j
++)
467 if (!strcmp (core_syms
[i
]->name
, symbol_map
[j
].function_name
))
469 if (j
> 0 && ! strcmp (symbol_map
[j
].file_name
,
470 symbol_map
[j
- 1].file_name
))
481 fprintf (stderr
, _("%s: file `%s' has no symbols\n"), whoami
, a_out_name
);
485 /* The "+ 2" is for the sentinels. */
486 symtab
.base
= (Sym
*) xmalloc ((symtab
.len
+ 2) * sizeof (Sym
));
488 /* Pass 2 - create symbols. */
489 symtab
.limit
= symtab
.base
;
491 for (i
= 0; i
< core_num_syms
; ++i
)
495 class = core_sym_class (core_syms
[i
]);
500 printf ("[core_create_function_syms] rejecting: 0x%lx %s\n",
501 (unsigned long) core_syms
[i
]->value
,
502 core_syms
[i
]->name
));
506 /* This should be replaced with a binary search or hashed
511 for (j
= 0; j
< symbol_map_count
; j
++)
512 if (!strcmp (core_syms
[i
]->name
, symbol_map
[j
].function_name
))
514 if (j
> 0 && ! strcmp (symbol_map
[j
].file_name
,
515 symbol_map
[j
- 1].file_name
))
525 sym_init (symtab
.limit
);
527 /* Symbol offsets are always section-relative. */
528 sym_sec
= core_syms
[i
]->section
;
529 symtab
.limit
->addr
= core_syms
[i
]->value
;
531 symtab
.limit
->addr
+= bfd_get_section_vma (sym_sec
->owner
, sym_sec
);
534 && !strcmp (core_syms
[i
]->name
, symbol_map
[found
].function_name
))
536 symtab
.limit
->name
= symbol_map
[found
].file_name
;
537 symtab
.limit
->mapped
= 1;
541 symtab
.limit
->name
= core_syms
[i
]->name
;
542 symtab
.limit
->mapped
= 0;
545 /* Lookup filename and line number, if we can. */
547 const char *filename
, *func_name
;
549 if (get_src_info (symtab
.limit
->addr
, &filename
, &func_name
,
550 &symtab
.limit
->line_num
))
552 symtab
.limit
->file
= source_file_lookup_path (filename
);
554 /* FIXME: Checking __osf__ here does not work with a cross
557 /* Suppress symbols that are not function names. This is
558 useful to suppress code-labels and aliases.
560 This is known to be useful under DEC's OSF/1. Under SunOS 4.x,
561 labels do not appear in the symbol table info, so this isn't
564 if (strcmp (symtab
.limit
->name
, func_name
) != 0)
566 /* The symbol's address maps to a different name, so
567 it can't be a function-entry point. This happens
568 for labels, for example. */
570 printf ("[core_create_function_syms: rej %s (maps to %s)\n",
571 symtab
.limit
->name
, func_name
));
578 symtab
.limit
->is_func
= TRUE
;
579 symtab
.limit
->is_bb_head
= TRUE
;
582 symtab
.limit
->is_static
= TRUE
;
584 /* Keep track of the minimum and maximum vma addresses used by all
585 symbols. When computing the max_vma, use the ending address of the
586 section containing the symbol, if available. */
587 min_vma
= MIN (symtab
.limit
->addr
, min_vma
);
589 max_vma
= MAX (bfd_get_section_vma (sym_sec
->owner
, sym_sec
)
590 + bfd_section_size (sym_sec
->owner
, sym_sec
) - 1,
593 max_vma
= MAX (symtab
.limit
->addr
, max_vma
);
595 DBG (AOUTDEBUG
, printf ("[core_create_function_syms] %ld %s 0x%lx\n",
596 (long) (symtab
.limit
- symtab
.base
),
598 (unsigned long) symtab
.limit
->addr
));
602 /* Create sentinels. */
603 sym_init (symtab
.limit
);
604 symtab
.limit
->name
= "<locore>";
605 symtab
.limit
->addr
= 0;
606 symtab
.limit
->end_addr
= min_vma
- 1;
609 sym_init (symtab
.limit
);
610 symtab
.limit
->name
= "<hicore>";
611 symtab
.limit
->addr
= max_vma
+ 1;
612 symtab
.limit
->end_addr
= ~(bfd_vma
) 0;
615 symtab
.len
= symtab
.limit
- symtab
.base
;
616 symtab_finalize (&symtab
);
619 /* Read in symbol table from core.
620 One symbol per line of source code is entered. */
623 core_create_line_syms ()
625 char *prev_name
, *prev_filename
;
626 unsigned int prev_name_len
, prev_filename_len
;
627 bfd_vma vma
, min_vma
= ~(bfd_vma
) 0, max_vma
= 0;
628 Sym
*prev
, dummy
, *sentinel
, *sym
;
629 const char *filename
;
634 /* Create symbols for functions as usual. This is necessary in
635 cases where parts of a program were not compiled with -g. For
636 those parts we still want to get info at the function level. */
637 core_create_function_syms ();
639 /* Pass 1: count the number of symbols. */
641 /* To find all line information, walk through all possible
642 text-space addresses (one by one!) and get the debugging
643 info for each address. When the debugging info changes,
644 it is time to create a new symbol.
646 Of course, this is rather slow and it would be better if
647 BFD would provide an iterator for enumerating all line infos. */
648 prev_name_len
= PATH_MAX
;
649 prev_filename_len
= PATH_MAX
;
650 prev_name
= xmalloc (prev_name_len
);
651 prev_filename
= xmalloc (prev_filename_len
);
655 vma_high
= core_text_sect
->vma
+ bfd_get_section_size (core_text_sect
);
656 for (vma
= core_text_sect
->vma
; vma
< vma_high
; vma
+= min_insn_size
)
660 if (!get_src_info (vma
, &filename
, &dummy
.name
, &dummy
.line_num
)
661 || (prev_line_num
== dummy
.line_num
663 && strcmp (prev_name
, dummy
.name
) == 0
664 && strcmp (prev_filename
, filename
) == 0))
668 prev_line_num
= dummy
.line_num
;
670 len
= strlen (dummy
.name
);
671 if (len
>= prev_name_len
)
673 prev_name_len
= len
+ 1024;
675 prev_name
= xmalloc (prev_name_len
);
678 strcpy (prev_name
, dummy
.name
);
679 len
= strlen (filename
);
681 if (len
>= prev_filename_len
)
683 prev_filename_len
= len
+ 1024;
684 free (prev_filename
);
685 prev_filename
= xmalloc (prev_filename_len
);
688 strcpy (prev_filename
, filename
);
690 min_vma
= MIN (vma
, min_vma
);
691 max_vma
= MAX (vma
, max_vma
);
695 free (prev_filename
);
697 /* Make room for function symbols, too. */
698 ltab
.len
+= symtab
.len
;
699 ltab
.base
= (Sym
*) xmalloc (ltab
.len
* sizeof (Sym
));
700 ltab
.limit
= ltab
.base
;
702 /* Pass 2 - create symbols. */
704 /* We now set is_static as we go along, rather than by running
705 through the symbol table at the end.
707 The old way called symtab_finalize before the is_static pass,
708 causing a problem since symtab_finalize uses is_static as part of
709 its address conflict resolution algorithm. Since global symbols
710 were prefered over static symbols, and all line symbols were
711 global at that point, static function names that conflicted with
712 their own line numbers (static, but labeled as global) were
713 rejected in favor of the line num.
715 This was not the desired functionality. We always want to keep
716 our function symbols and discard any conflicting line symbols.
717 Perhaps symtab_finalize should be modified to make this
718 distinction as well, but the current fix works and the code is a
722 for (vma
= core_text_sect
->vma
; vma
< vma_high
; vma
+= min_insn_size
)
724 sym_init (ltab
.limit
);
726 if (!get_src_info (vma
, &filename
, <ab
.limit
->name
, <ab
.limit
->line_num
)
727 || (prev
&& prev
->line_num
== ltab
.limit
->line_num
728 && strcmp (prev
->name
, ltab
.limit
->name
) == 0
729 && strcmp (prev
->file
->name
, filename
) == 0))
732 /* Make name pointer a malloc'ed string. */
733 ltab
.limit
->name
= xstrdup (ltab
.limit
->name
);
734 ltab
.limit
->file
= source_file_lookup_path (filename
);
736 ltab
.limit
->addr
= vma
;
738 /* Set is_static based on the enclosing function, using either:
739 1) the previous symbol, if it's from the same function, or
740 2) a symtab lookup. */
741 if (prev
&& ltab
.limit
->file
== prev
->file
&&
742 strcmp (ltab
.limit
->name
, prev
->name
) == 0)
744 ltab
.limit
->is_static
= prev
->is_static
;
748 sym
= sym_lookup(&symtab
, ltab
.limit
->addr
);
749 ltab
.limit
->is_static
= sym
->is_static
;
754 DBG (AOUTDEBUG
, printf ("[core_create_line_syms] %lu %s 0x%lx\n",
755 (unsigned long) (ltab
.limit
- ltab
.base
),
757 (unsigned long) ltab
.limit
->addr
));
761 /* Update sentinels. */
762 sentinel
= sym_lookup (&symtab
, (bfd_vma
) 0);
765 && strcmp (sentinel
->name
, "<locore>") == 0
766 && min_vma
<= sentinel
->end_addr
)
767 sentinel
->end_addr
= min_vma
- 1;
769 sentinel
= sym_lookup (&symtab
, ~(bfd_vma
) 0);
772 && strcmp (sentinel
->name
, "<hicore>") == 0
773 && max_vma
>= sentinel
->addr
)
774 sentinel
->addr
= max_vma
+ 1;
776 /* Copy in function symbols. */
777 memcpy (ltab
.limit
, symtab
.base
, symtab
.len
* sizeof (Sym
));
778 ltab
.limit
+= symtab
.len
;
780 if ((unsigned int) (ltab
.limit
- ltab
.base
) != ltab
.len
)
783 _("%s: somebody miscounted: ltab.len=%d instead of %ld\n"),
784 whoami
, ltab
.len
, (long) (ltab
.limit
- ltab
.base
));
788 /* Finalize ltab and make it symbol table. */
789 symtab_finalize (<ab
);