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"
31 static int core_num_syms
;
32 static asymbol
**core_syms
;
33 asection
*core_text_sect
;
36 static int min_insn_size
;
39 /* For mapping symbols to specific .o files during file ordering. */
40 struct function_map
*symbol_map
;
41 unsigned int symbol_map_count
;
43 static void read_function_mappings (const char *);
44 static int core_sym_class (asymbol
*);
45 static bfd_boolean get_src_info
46 (bfd_vma
, const char **, const char **, int *);
48 extern void i386_find_call (Sym
*, bfd_vma
, bfd_vma
);
49 extern void alpha_find_call (Sym
*, bfd_vma
, bfd_vma
);
50 extern void vax_find_call (Sym
*, bfd_vma
, bfd_vma
);
51 extern void tahoe_find_call (Sym
*, bfd_vma
, bfd_vma
);
52 extern void sparc_find_call (Sym
*, bfd_vma
, bfd_vma
);
53 extern void mips_find_call (Sym
*, bfd_vma
, bfd_vma
);
56 read_function_mappings (const char *filename
)
58 FILE *file
= fopen (filename
, "r");
64 fprintf (stderr
, _("%s: could not open %s.\n"), whoami
, filename
);
68 /* First parse the mapping file so we know how big we need to
69 make our tables. We also do some sanity checks at this
75 matches
= fscanf (file
, "%[^\n:]", dummy
);
78 fprintf (stderr
, _("%s: unable to parse mapping file %s.\n"),
83 /* Just skip messages about files with no symbols. */
84 if (!strncmp (dummy
, "No symbols in ", 14))
90 /* Don't care what else is on this line at this point. */
91 fscanf (file
, "%[^\n]\n", dummy
);
95 /* Now we know how big we need to make our table. */
96 symbol_map
= ((struct function_map
*)
97 xmalloc (count
* sizeof (struct function_map
)));
99 /* Rewind the input file so we can read it again. */
102 /* Read each entry and put it into the table. */
109 matches
= fscanf (file
, "%[^\n:]", dummy
);
112 fprintf (stderr
, _("%s: unable to parse mapping file %s.\n"),
117 /* Just skip messages about files with no symbols. */
118 if (!strncmp (dummy
, "No symbols in ", 14))
124 /* dummy has the filename, go ahead and copy it. */
125 symbol_map
[count
].file_name
= xmalloc (strlen (dummy
) + 1);
126 strcpy (symbol_map
[count
].file_name
, dummy
);
128 /* Now we need the function name. */
129 fscanf (file
, "%[^\n]\n", dummy
);
130 tmp
= strrchr (dummy
, ' ') + 1;
131 symbol_map
[count
].function_name
= xmalloc (strlen (tmp
) + 1);
132 strcpy (symbol_map
[count
].function_name
, tmp
);
136 /* Record the size of the map table for future reference. */
137 symbol_map_count
= count
;
142 core_init (const char *aout_name
)
148 core_bfd
= bfd_openr (aout_name
, 0);
156 if (!bfd_check_format (core_bfd
, bfd_object
))
158 fprintf (stderr
, _("%s: %s: not in executable format\n"), whoami
, aout_name
);
162 /* Get core's text section. */
163 core_text_sect
= bfd_get_section_by_name (core_bfd
, ".text");
166 core_text_sect
= bfd_get_section_by_name (core_bfd
, "$CODE$");
169 fprintf (stderr
, _("%s: can't find .text section in %s\n"),
175 /* Read core's symbol table. */
177 /* This will probably give us more than we need, but that's ok. */
178 core_sym_bytes
= bfd_get_symtab_upper_bound (core_bfd
);
179 if (core_sym_bytes
< 0)
181 fprintf (stderr
, "%s: %s: %s\n", whoami
, aout_name
,
182 bfd_errmsg (bfd_get_error ()));
186 core_syms
= (asymbol
**) xmalloc (core_sym_bytes
);
187 core_num_syms
= bfd_canonicalize_symtab (core_bfd
, core_syms
);
189 if (core_num_syms
< 0)
191 fprintf (stderr
, "%s: %s: %s\n", whoami
, aout_name
,
192 bfd_errmsg (bfd_get_error ()));
196 synth_count
= bfd_get_synthetic_symtab (core_bfd
, core_num_syms
, core_syms
,
197 0, NULL
, &synthsyms
);
204 new_size
= (core_num_syms
+ synth_count
+ 1) * sizeof (*core_syms
);
205 core_syms
= xrealloc (core_syms
, new_size
);
206 symp
= core_syms
+ core_num_syms
;
207 core_num_syms
+= synth_count
;
208 for (i
= 0; i
< synth_count
; i
++)
209 *symp
++ = synthsyms
+ i
;
216 switch (bfd_get_arch (core_bfd
))
231 if (function_mapping_file
)
232 read_function_mappings (function_mapping_file
);
235 /* Read in the text space of an a.out file. */
238 core_get_text_space (bfd
*cbfd
)
240 core_text_space
= malloc (bfd_get_section_size (core_text_sect
));
242 if (!core_text_space
)
244 fprintf (stderr
, _("%s: ran out room for %lu bytes of text space\n"),
245 whoami
, (unsigned long) bfd_get_section_size (core_text_sect
));
249 if (!bfd_get_section_contents (cbfd
, core_text_sect
, core_text_space
,
250 0, bfd_get_section_size (core_text_sect
)))
252 bfd_perror ("bfd_get_section_contents");
253 free (core_text_space
);
257 if (!core_text_space
)
258 fprintf (stderr
, _("%s: can't do -c\n"), whoami
);
263 find_call (Sym
*parent
, bfd_vma p_lowpc
, bfd_vma p_highpc
)
265 switch (bfd_get_arch (core_bfd
))
268 i386_find_call (parent
, p_lowpc
, p_highpc
);
272 alpha_find_call (parent
, p_lowpc
, p_highpc
);
276 vax_find_call (parent
, p_lowpc
, p_highpc
);
280 sparc_find_call (parent
, p_lowpc
, p_highpc
);
284 tahoe_find_call (parent
, p_lowpc
, p_highpc
);
288 mips_find_call (parent
, p_lowpc
, p_highpc
);
292 fprintf (stderr
, _("%s: -c not supported on architecture %s\n"),
293 whoami
, bfd_printable_name(core_bfd
));
295 /* Don't give the error more than once. */
296 ignore_direct_calls
= FALSE
;
300 /* Return class of symbol SYM. The returned class can be any of:
301 0 -> symbol is not interesting to us
302 'T' -> symbol is a global name
303 't' -> symbol is a local (static) name. */
306 core_sym_class (asymbol
*sym
)
313 if (sym
->section
== NULL
|| (sym
->flags
& BSF_DEBUGGING
) != 0)
316 /* Must be a text symbol, and static text symbols
317 don't qualify if ignore_static_funcs set. */
318 if (ignore_static_funcs
&& (sym
->flags
& BSF_LOCAL
))
320 DBG (AOUTDEBUG
, printf ("[core_sym_class] %s: not a function\n",
325 bfd_get_symbol_info (core_bfd
, sym
, &syminfo
);
329 return i
; /* It's a global symbol. */
332 /* Treat weak symbols as text symbols. FIXME: a weak symbol may
333 also be a data symbol. */
338 /* Not a static text symbol. */
339 DBG (AOUTDEBUG
, printf ("[core_sym_class] %s is of class %c\n",
344 /* Do some more filtering on static function-names. */
345 if (ignore_static_funcs
)
348 /* Can't zero-length name or funny characters in name, where
349 `funny' includes: `.' (.o file names) and `$' (Pascal labels). */
350 if (!sym
->name
|| sym
->name
[0] == '\0')
353 for (name
= sym
->name
; *name
; ++name
)
355 if (*name
== '.' || *name
== '$')
359 /* On systems where the C compiler adds an underscore to all
360 names, static names without underscores seem usually to be
361 labels in hand written assembler in the library. We don't want
362 these names. This is certainly necessary on a Sparc running
363 SunOS 4.1 (try profiling a program that does a lot of
364 division). I don't know whether it has harmful side effects on
365 other systems. Perhaps it should be made configurable. */
366 sym_prefix
= bfd_get_symbol_leading_char (core_bfd
);
368 if ((sym_prefix
&& sym_prefix
!= sym
->name
[0])
369 /* GCC may add special symbols to help gdb figure out the file
370 language. We want to ignore these, since sometimes they mask
371 the real function. (dj@ctron) */
372 || !strncmp (sym
->name
, "__gnu_compiled", 14)
373 || !strncmp (sym
->name
, "___gnu_compiled", 15))
378 /* If the object file supports marking of function symbols, then
379 we can zap anything that doesn't have BSF_FUNCTION set. */
380 if (ignore_non_functions
&& (sym
->flags
& BSF_FUNCTION
) == 0)
383 return 't'; /* It's a static text symbol. */
386 /* Get whatever source info we can get regarding address ADDR. */
389 get_src_info (bfd_vma addr
, const char **filename
, const char **name
, int *line_num
)
391 const char *fname
= 0, *func_name
= 0;
394 if (bfd_find_nearest_line (core_bfd
, core_text_sect
, core_syms
,
395 addr
- core_text_sect
->vma
,
396 &fname
, &func_name
, (unsigned int *) &l
)
397 && fname
&& func_name
&& l
)
399 DBG (AOUTDEBUG
, printf ("[get_src_info] 0x%lx -> %s:%d (%s)\n",
400 (unsigned long) addr
, fname
, l
, func_name
));
408 DBG (AOUTDEBUG
, printf ("[get_src_info] no info for 0x%lx (%s:%d,%s)\n",
409 (long) addr
, fname
? fname
: "<unknown>", l
,
410 func_name
? func_name
: "<unknown>"));
415 /* Read in symbol table from core.
416 One symbol per function is entered. */
419 core_create_function_syms ()
421 bfd_vma min_vma
= ~(bfd_vma
) 0;
427 /* Pass 1 - determine upper bound on number of function names. */
430 for (i
= 0; i
< core_num_syms
; ++i
)
432 if (!core_sym_class (core_syms
[i
]))
435 /* This should be replaced with a binary search or hashed
438 Don't create a symtab entry for a function that has
439 a mapping to a file, unless it's the first function
442 for (j
= 0; j
< symbol_map_count
; j
++)
443 if (!strcmp (core_syms
[i
]->name
, symbol_map
[j
].function_name
))
445 if (j
> 0 && ! strcmp (symbol_map
[j
].file_name
,
446 symbol_map
[j
- 1].file_name
))
457 fprintf (stderr
, _("%s: file `%s' has no symbols\n"), whoami
, a_out_name
);
461 /* The "+ 2" is for the sentinels. */
462 symtab
.base
= (Sym
*) xmalloc ((symtab
.len
+ 2) * sizeof (Sym
));
464 /* Pass 2 - create symbols. */
465 symtab
.limit
= symtab
.base
;
467 for (i
= 0; i
< core_num_syms
; ++i
)
471 class = core_sym_class (core_syms
[i
]);
476 printf ("[core_create_function_syms] rejecting: 0x%lx %s\n",
477 (unsigned long) core_syms
[i
]->value
,
478 core_syms
[i
]->name
));
482 /* This should be replaced with a binary search or hashed
487 for (j
= 0; j
< symbol_map_count
; j
++)
488 if (!strcmp (core_syms
[i
]->name
, symbol_map
[j
].function_name
))
490 if (j
> 0 && ! strcmp (symbol_map
[j
].file_name
,
491 symbol_map
[j
- 1].file_name
))
501 sym_init (symtab
.limit
);
503 /* Symbol offsets are always section-relative. */
504 sym_sec
= core_syms
[i
]->section
;
505 symtab
.limit
->addr
= core_syms
[i
]->value
;
507 symtab
.limit
->addr
+= bfd_get_section_vma (sym_sec
->owner
, sym_sec
);
510 && !strcmp (core_syms
[i
]->name
, symbol_map
[found
].function_name
))
512 symtab
.limit
->name
= symbol_map
[found
].file_name
;
513 symtab
.limit
->mapped
= 1;
517 symtab
.limit
->name
= core_syms
[i
]->name
;
518 symtab
.limit
->mapped
= 0;
521 /* Lookup filename and line number, if we can. */
523 const char *filename
, *func_name
;
525 if (get_src_info (symtab
.limit
->addr
, &filename
, &func_name
,
526 &symtab
.limit
->line_num
))
528 symtab
.limit
->file
= source_file_lookup_path (filename
);
530 /* FIXME: Checking __osf__ here does not work with a cross
533 /* Suppress symbols that are not function names. This is
534 useful to suppress code-labels and aliases.
536 This is known to be useful under DEC's OSF/1. Under SunOS 4.x,
537 labels do not appear in the symbol table info, so this isn't
540 if (strcmp (symtab
.limit
->name
, func_name
) != 0)
542 /* The symbol's address maps to a different name, so
543 it can't be a function-entry point. This happens
544 for labels, for example. */
546 printf ("[core_create_function_syms: rej %s (maps to %s)\n",
547 symtab
.limit
->name
, func_name
));
554 symtab
.limit
->is_func
= TRUE
;
555 symtab
.limit
->is_bb_head
= TRUE
;
558 symtab
.limit
->is_static
= TRUE
;
560 /* Keep track of the minimum and maximum vma addresses used by all
561 symbols. When computing the max_vma, use the ending address of the
562 section containing the symbol, if available. */
563 min_vma
= MIN (symtab
.limit
->addr
, min_vma
);
565 max_vma
= MAX (bfd_get_section_vma (sym_sec
->owner
, sym_sec
)
566 + bfd_section_size (sym_sec
->owner
, sym_sec
) - 1,
569 max_vma
= MAX (symtab
.limit
->addr
, max_vma
);
571 /* If we see "main" without an initial '_', we assume names
572 are *not* prefixed by '_'. */
573 if (symtab
.limit
->name
[0] == 'm' && discard_underscores
574 && strcmp (symtab
.limit
->name
, "main") == 0)
575 discard_underscores
= 0;
577 DBG (AOUTDEBUG
, printf ("[core_create_function_syms] %ld %s 0x%lx\n",
578 (long) (symtab
.limit
- symtab
.base
),
580 (unsigned long) symtab
.limit
->addr
));
584 /* Create sentinels. */
585 sym_init (symtab
.limit
);
586 symtab
.limit
->name
= "<locore>";
587 symtab
.limit
->addr
= 0;
588 symtab
.limit
->end_addr
= min_vma
- 1;
591 sym_init (symtab
.limit
);
592 symtab
.limit
->name
= "<hicore>";
593 symtab
.limit
->addr
= max_vma
+ 1;
594 symtab
.limit
->end_addr
= ~(bfd_vma
) 0;
597 symtab
.len
= symtab
.limit
- symtab
.base
;
598 symtab_finalize (&symtab
);
601 /* Read in symbol table from core.
602 One symbol per line of source code is entered. */
605 core_create_line_syms ()
607 char *prev_name
, *prev_filename
;
608 unsigned int prev_name_len
, prev_filename_len
;
609 bfd_vma vma
, min_vma
= ~(bfd_vma
) 0, max_vma
= 0;
610 Sym
*prev
, dummy
, *sentinel
, *sym
;
611 const char *filename
;
616 /* Create symbols for functions as usual. This is necessary in
617 cases where parts of a program were not compiled with -g. For
618 those parts we still want to get info at the function level. */
619 core_create_function_syms ();
621 /* Pass 1: count the number of symbols. */
623 /* To find all line information, walk through all possible
624 text-space addresses (one by one!) and get the debugging
625 info for each address. When the debugging info changes,
626 it is time to create a new symbol.
628 Of course, this is rather slow and it would be better if
629 BFD would provide an iterator for enumerating all line infos. */
630 prev_name_len
= PATH_MAX
;
631 prev_filename_len
= PATH_MAX
;
632 prev_name
= xmalloc (prev_name_len
);
633 prev_filename
= xmalloc (prev_filename_len
);
637 vma_high
= core_text_sect
->vma
+ bfd_get_section_size (core_text_sect
);
638 for (vma
= core_text_sect
->vma
; vma
< vma_high
; vma
+= min_insn_size
)
642 if (!get_src_info (vma
, &filename
, &dummy
.name
, &dummy
.line_num
)
643 || (prev_line_num
== dummy
.line_num
645 && strcmp (prev_name
, dummy
.name
) == 0
646 && strcmp (prev_filename
, filename
) == 0))
650 prev_line_num
= dummy
.line_num
;
652 len
= strlen (dummy
.name
);
653 if (len
>= prev_name_len
)
655 prev_name_len
= len
+ 1024;
657 prev_name
= xmalloc (prev_name_len
);
660 strcpy (prev_name
, dummy
.name
);
661 len
= strlen (filename
);
663 if (len
>= prev_filename_len
)
665 prev_filename_len
= len
+ 1024;
666 free (prev_filename
);
667 prev_filename
= xmalloc (prev_filename_len
);
670 strcpy (prev_filename
, filename
);
672 min_vma
= MIN (vma
, min_vma
);
673 max_vma
= MAX (vma
, max_vma
);
677 free (prev_filename
);
679 /* Make room for function symbols, too. */
680 ltab
.len
+= symtab
.len
;
681 ltab
.base
= (Sym
*) xmalloc (ltab
.len
* sizeof (Sym
));
682 ltab
.limit
= ltab
.base
;
684 /* Pass 2 - create symbols. */
686 /* We now set is_static as we go along, rather than by running
687 through the symbol table at the end.
689 The old way called symtab_finalize before the is_static pass,
690 causing a problem since symtab_finalize uses is_static as part of
691 its address conflict resolution algorithm. Since global symbols
692 were prefered over static symbols, and all line symbols were
693 global at that point, static function names that conflicted with
694 their own line numbers (static, but labeled as global) were
695 rejected in favor of the line num.
697 This was not the desired functionality. We always want to keep
698 our function symbols and discard any conflicting line symbols.
699 Perhaps symtab_finalize should be modified to make this
700 distinction as well, but the current fix works and the code is a
704 for (vma
= core_text_sect
->vma
; vma
< vma_high
; vma
+= min_insn_size
)
706 sym_init (ltab
.limit
);
708 if (!get_src_info (vma
, &filename
, <ab
.limit
->name
, <ab
.limit
->line_num
)
709 || (prev
&& prev
->line_num
== ltab
.limit
->line_num
710 && strcmp (prev
->name
, ltab
.limit
->name
) == 0
711 && strcmp (prev
->file
->name
, filename
) == 0))
714 /* Make name pointer a malloc'ed string. */
715 ltab
.limit
->name
= xstrdup (ltab
.limit
->name
);
716 ltab
.limit
->file
= source_file_lookup_path (filename
);
718 ltab
.limit
->addr
= vma
;
720 /* Set is_static based on the enclosing function, using either:
721 1) the previous symbol, if it's from the same function, or
722 2) a symtab lookup. */
723 if (prev
&& ltab
.limit
->file
== prev
->file
&&
724 strcmp (ltab
.limit
->name
, prev
->name
) == 0)
726 ltab
.limit
->is_static
= prev
->is_static
;
730 sym
= sym_lookup(&symtab
, ltab
.limit
->addr
);
731 ltab
.limit
->is_static
= sym
->is_static
;
736 /* If we see "main" without an initial '_', we assume names
737 are *not* prefixed by '_'. */
738 if (ltab
.limit
->name
[0] == 'm' && discard_underscores
739 && strcmp (ltab
.limit
->name
, "main") == 0)
740 discard_underscores
= 0;
742 DBG (AOUTDEBUG
, printf ("[core_create_line_syms] %lu %s 0x%lx\n",
743 (unsigned long) (ltab
.limit
- ltab
.base
),
745 (unsigned long) ltab
.limit
->addr
));
749 /* Update sentinels. */
750 sentinel
= sym_lookup (&symtab
, (bfd_vma
) 0);
753 && strcmp (sentinel
->name
, "<locore>") == 0
754 && min_vma
<= sentinel
->end_addr
)
755 sentinel
->end_addr
= min_vma
- 1;
757 sentinel
= sym_lookup (&symtab
, ~(bfd_vma
) 0);
760 && strcmp (sentinel
->name
, "<hicore>") == 0
761 && max_vma
>= sentinel
->addr
)
762 sentinel
->addr
= max_vma
+ 1;
764 /* Copy in function symbols. */
765 memcpy (ltab
.limit
, symtab
.base
, symtab
.len
* sizeof (Sym
));
766 ltab
.limit
+= symtab
.len
;
768 if ((unsigned int) (ltab
.limit
- ltab
.base
) != ltab
.len
)
771 _("%s: somebody miscounted: ltab.len=%d instead of %ld\n"),
772 whoami
, ltab
.len
, (long) (ltab
.limit
- ltab
.base
));
776 /* Finalize ltab and make it symbol table. */
777 symtab_finalize (<ab
);