3 Copyright 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 #include "libiberty.h"
24 #include "search_list.h"
32 asection
*core_text_sect
;
38 /* For mapping symbols to specific .o files during file ordering. */
39 struct function_map
*symbol_map
;
40 unsigned int symbol_map_count
;
42 static void read_function_mappings (const char *);
43 static int core_sym_class (asymbol
*);
44 static bfd_boolean get_src_info
45 (bfd_vma
, const char **, const char **, int *);
47 extern void i386_find_call (Sym
*, bfd_vma
, bfd_vma
);
48 extern void alpha_find_call (Sym
*, bfd_vma
, bfd_vma
);
49 extern void vax_find_call (Sym
*, bfd_vma
, bfd_vma
);
50 extern void tahoe_find_call (Sym
*, bfd_vma
, bfd_vma
);
51 extern void sparc_find_call (Sym
*, bfd_vma
, bfd_vma
);
52 extern void mips_find_call (Sym
*, bfd_vma
, bfd_vma
);
55 read_function_mappings (const char *filename
)
57 FILE *file
= fopen (filename
, "r");
63 fprintf (stderr
, _("%s: could not open %s.\n"), whoami
, filename
);
67 /* First parse the mapping file so we know how big we need to
68 make our tables. We also do some sanity checks at this
74 matches
= fscanf (file
, "%[^\n:]", dummy
);
77 fprintf (stderr
, _("%s: unable to parse mapping file %s.\n"),
82 /* Just skip messages about files with no symbols. */
83 if (!strncmp (dummy
, "No symbols in ", 14))
89 /* Don't care what else is on this line at this point. */
90 fscanf (file
, "%[^\n]\n", dummy
);
94 /* Now we know how big we need to make our table. */
95 symbol_map
= ((struct function_map
*)
96 xmalloc (count
* sizeof (struct function_map
)));
98 /* Rewind the input file so we can read it again. */
101 /* Read each entry and put it into the table. */
108 matches
= fscanf (file
, "%[^\n:]", dummy
);
111 fprintf (stderr
, _("%s: unable to parse mapping file %s.\n"),
116 /* Just skip messages about files with no symbols. */
117 if (!strncmp (dummy
, "No symbols in ", 14))
123 /* dummy has the filename, go ahead and copy it. */
124 symbol_map
[count
].file_name
= xmalloc (strlen (dummy
) + 1);
125 strcpy (symbol_map
[count
].file_name
, dummy
);
127 /* Now we need the function name. */
128 fscanf (file
, "%[^\n]\n", dummy
);
129 tmp
= strrchr (dummy
, ' ') + 1;
130 symbol_map
[count
].function_name
= xmalloc (strlen (tmp
) + 1);
131 strcpy (symbol_map
[count
].function_name
, tmp
);
135 /* Record the size of the map table for future reference. */
136 symbol_map_count
= count
;
141 core_init (const char *aout_name
)
144 core_bfd
= bfd_openr (aout_name
, 0);
152 if (!bfd_check_format (core_bfd
, bfd_object
))
154 fprintf (stderr
, _("%s: %s: not in a.out format\n"), whoami
, aout_name
);
158 /* Get core's text section. */
159 core_text_sect
= bfd_get_section_by_name (core_bfd
, ".text");
162 core_text_sect
= bfd_get_section_by_name (core_bfd
, "$CODE$");
165 fprintf (stderr
, _("%s: can't find .text section in %s\n"),
171 /* Read core's symbol table. */
173 /* This will probably give us more than we need, but that's ok. */
174 core_sym_bytes
= bfd_get_symtab_upper_bound (core_bfd
);
175 if (core_sym_bytes
< 0)
177 fprintf (stderr
, "%s: %s: %s\n", whoami
, aout_name
,
178 bfd_errmsg (bfd_get_error ()));
182 core_syms
= (asymbol
**) xmalloc (core_sym_bytes
);
183 core_num_syms
= bfd_canonicalize_symtab (core_bfd
, core_syms
);
185 if (core_num_syms
< 0)
187 fprintf (stderr
, "%s: %s: %s\n", whoami
, aout_name
,
188 bfd_errmsg (bfd_get_error ()));
195 switch (bfd_get_arch (core_bfd
))
210 if (function_mapping_file
)
211 read_function_mappings (function_mapping_file
);
214 /* Read in the text space of an a.out file. */
217 core_get_text_space (bfd
*cbfd
)
219 core_text_space
= (PTR
) malloc ((unsigned int) core_text_sect
->_raw_size
);
221 if (!core_text_space
)
223 fprintf (stderr
, _("%s: ran out room for %lu bytes of text space\n"),
224 whoami
, (unsigned long) core_text_sect
->_raw_size
);
228 if (!bfd_get_section_contents (cbfd
, core_text_sect
, core_text_space
,
229 (bfd_vma
) 0, core_text_sect
->_raw_size
))
231 bfd_perror ("bfd_get_section_contents");
232 free (core_text_space
);
236 if (!core_text_space
)
237 fprintf (stderr
, _("%s: can't do -c\n"), whoami
);
242 find_call (Sym
*parent
, bfd_vma p_lowpc
, bfd_vma p_highpc
)
244 switch (bfd_get_arch (core_bfd
))
247 i386_find_call (parent
, p_lowpc
, p_highpc
);
251 alpha_find_call (parent
, p_lowpc
, p_highpc
);
255 vax_find_call (parent
, p_lowpc
, p_highpc
);
259 sparc_find_call (parent
, p_lowpc
, p_highpc
);
263 tahoe_find_call (parent
, p_lowpc
, p_highpc
);
267 mips_find_call (parent
, p_lowpc
, p_highpc
);
271 fprintf (stderr
, _("%s: -c not supported on architecture %s\n"),
272 whoami
, bfd_printable_name(core_bfd
));
274 /* Don't give the error more than once. */
275 ignore_direct_calls
= FALSE
;
279 /* Return class of symbol SYM. The returned class can be any of:
280 0 -> symbol is not interesting to us
281 'T' -> symbol is a global name
282 't' -> symbol is a local (static) name. */
285 core_sym_class (asymbol
*sym
)
292 if (sym
->section
== NULL
|| (sym
->flags
& BSF_DEBUGGING
) != 0)
295 /* Must be a text symbol, and static text symbols
296 don't qualify if ignore_static_funcs set. */
297 if (ignore_static_funcs
&& (sym
->flags
& BSF_LOCAL
))
299 DBG (AOUTDEBUG
, printf ("[core_sym_class] %s: not a function\n",
304 bfd_get_symbol_info (core_bfd
, sym
, &syminfo
);
308 return i
; /* It's a global symbol. */
311 /* Treat weak symbols as text symbols. FIXME: a weak symbol may
312 also be a data symbol. */
317 /* Not a static text symbol. */
318 DBG (AOUTDEBUG
, printf ("[core_sym_class] %s is of class %c\n",
323 /* Do some more filtering on static function-names. */
324 if (ignore_static_funcs
)
327 /* Can't zero-length name or funny characters in name, where
328 `funny' includes: `.' (.o file names) and `$' (Pascal labels). */
329 if (!sym
->name
|| sym
->name
[0] == '\0')
332 for (name
= sym
->name
; *name
; ++name
)
334 if (*name
== '.' || *name
== '$')
338 /* On systems where the C compiler adds an underscore to all
339 names, static names without underscores seem usually to be
340 labels in hand written assembler in the library. We don't want
341 these names. This is certainly necessary on a Sparc running
342 SunOS 4.1 (try profiling a program that does a lot of
343 division). I don't know whether it has harmful side effects on
344 other systems. Perhaps it should be made configurable. */
345 sym_prefix
= bfd_get_symbol_leading_char (core_bfd
);
347 if ((sym_prefix
&& sym_prefix
!= sym
->name
[0])
348 /* GCC may add special symbols to help gdb figure out the file
349 language. We want to ignore these, since sometimes they mask
350 the real function. (dj@ctron) */
351 || !strncmp (sym
->name
, "__gnu_compiled", 14)
352 || !strncmp (sym
->name
, "___gnu_compiled", 15))
357 /* If the object file supports marking of function symbols, then
358 we can zap anything that doesn't have BSF_FUNCTION set. */
359 if (ignore_non_functions
&& (sym
->flags
& BSF_FUNCTION
) == 0)
362 return 't'; /* It's a static text symbol. */
365 /* Get whatever source info we can get regarding address ADDR. */
368 get_src_info (bfd_vma addr
, const char **filename
, const char **name
, int *line_num
)
370 const char *fname
= 0, *func_name
= 0;
373 if (bfd_find_nearest_line (core_bfd
, core_text_sect
, core_syms
,
374 addr
- core_text_sect
->vma
,
375 &fname
, &func_name
, (unsigned int *) &l
)
376 && fname
&& func_name
&& l
)
378 DBG (AOUTDEBUG
, printf ("[get_src_info] 0x%lx -> %s:%d (%s)\n",
379 (unsigned long) addr
, fname
, l
, func_name
));
387 DBG (AOUTDEBUG
, printf ("[get_src_info] no info for 0x%lx (%s:%d,%s)\n",
388 (long) addr
, fname
? fname
: "<unknown>", l
,
389 func_name
? func_name
: "<unknown>"));
394 /* Read in symbol table from core.
395 One symbol per function is entered. */
398 core_create_function_syms ()
400 bfd_vma min_vma
= ~(bfd_vma
) 0;
406 /* Pass 1 - determine upper bound on number of function names. */
409 for (i
= 0; i
< core_num_syms
; ++i
)
411 if (!core_sym_class (core_syms
[i
]))
414 /* This should be replaced with a binary search or hashed
417 Don't create a symtab entry for a function that has
418 a mapping to a file, unless it's the first function
421 for (j
= 0; j
< symbol_map_count
; j
++)
422 if (!strcmp (core_syms
[i
]->name
, symbol_map
[j
].function_name
))
424 if (j
> 0 && ! strcmp (symbol_map
[j
].file_name
,
425 symbol_map
[j
- 1].file_name
))
436 fprintf (stderr
, _("%s: file `%s' has no symbols\n"), whoami
, a_out_name
);
440 /* The "+ 2" is for the sentinels. */
441 symtab
.base
= (Sym
*) xmalloc ((symtab
.len
+ 2) * sizeof (Sym
));
443 /* Pass 2 - create symbols. */
444 symtab
.limit
= symtab
.base
;
446 for (i
= 0; i
< core_num_syms
; ++i
)
450 class = core_sym_class (core_syms
[i
]);
455 printf ("[core_create_function_syms] rejecting: 0x%lx %s\n",
456 (unsigned long) core_syms
[i
]->value
,
457 core_syms
[i
]->name
));
461 /* This should be replaced with a binary search or hashed
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
))
480 sym_init (symtab
.limit
);
482 /* Symbol offsets are always section-relative. */
483 sym_sec
= core_syms
[i
]->section
;
484 symtab
.limit
->addr
= core_syms
[i
]->value
;
486 symtab
.limit
->addr
+= bfd_get_section_vma (sym_sec
->owner
, sym_sec
);
489 && !strcmp (core_syms
[i
]->name
, symbol_map
[found
].function_name
))
491 symtab
.limit
->name
= symbol_map
[found
].file_name
;
492 symtab
.limit
->mapped
= 1;
496 symtab
.limit
->name
= core_syms
[i
]->name
;
497 symtab
.limit
->mapped
= 0;
500 /* Lookup filename and line number, if we can. */
502 const char *filename
, *func_name
;
504 if (get_src_info (symtab
.limit
->addr
, &filename
, &func_name
,
505 &symtab
.limit
->line_num
))
507 symtab
.limit
->file
= source_file_lookup_path (filename
);
509 /* FIXME: Checking __osf__ here does not work with a cross
512 /* Suppress symbols that are not function names. This is
513 useful to suppress code-labels and aliases.
515 This is known to be useful under DEC's OSF/1. Under SunOS 4.x,
516 labels do not appear in the symbol table info, so this isn't
519 if (strcmp (symtab
.limit
->name
, func_name
) != 0)
521 /* The symbol's address maps to a different name, so
522 it can't be a function-entry point. This happens
523 for labels, for example. */
525 printf ("[core_create_function_syms: rej %s (maps to %s)\n",
526 symtab
.limit
->name
, func_name
));
533 symtab
.limit
->is_func
= TRUE
;
534 symtab
.limit
->is_bb_head
= TRUE
;
537 symtab
.limit
->is_static
= TRUE
;
539 /* Keep track of the minimum and maximum vma addresses used by all
540 symbols. When computing the max_vma, use the ending address of the
541 section containing the symbol, if available. */
542 min_vma
= MIN (symtab
.limit
->addr
, min_vma
);
544 max_vma
= MAX (bfd_get_section_vma (sym_sec
->owner
, sym_sec
)
545 + bfd_section_size (sym_sec
->owner
, sym_sec
) - 1,
548 max_vma
= MAX (symtab
.limit
->addr
, max_vma
);
550 /* If we see "main" without an initial '_', we assume names
551 are *not* prefixed by '_'. */
552 if (symtab
.limit
->name
[0] == 'm' && discard_underscores
553 && strcmp (symtab
.limit
->name
, "main") == 0)
554 discard_underscores
= 0;
556 DBG (AOUTDEBUG
, printf ("[core_create_function_syms] %ld %s 0x%lx\n",
557 (long) (symtab
.limit
- symtab
.base
),
559 (unsigned long) symtab
.limit
->addr
));
563 /* Create sentinels. */
564 sym_init (symtab
.limit
);
565 symtab
.limit
->name
= "<locore>";
566 symtab
.limit
->addr
= 0;
567 symtab
.limit
->end_addr
= min_vma
- 1;
570 sym_init (symtab
.limit
);
571 symtab
.limit
->name
= "<hicore>";
572 symtab
.limit
->addr
= max_vma
+ 1;
573 symtab
.limit
->end_addr
= ~(bfd_vma
) 0;
576 symtab
.len
= symtab
.limit
- symtab
.base
;
577 symtab_finalize (&symtab
);
580 /* Read in symbol table from core.
581 One symbol per line of source code is entered. */
584 core_create_line_syms ()
586 char *prev_name
, *prev_filename
;
587 unsigned int prev_name_len
, prev_filename_len
;
588 bfd_vma vma
, min_vma
= ~(bfd_vma
) 0, max_vma
= 0;
589 Sym
*prev
, dummy
, *sentinel
, *sym
;
590 const char *filename
;
594 /* Create symbols for functions as usual. This is necessary in
595 cases where parts of a program were not compiled with -g. For
596 those parts we still want to get info at the function level. */
597 core_create_function_syms ();
599 /* Pass 1: count the number of symbols. */
601 /* To find all line information, walk through all possible
602 text-space addresses (one by one!) and get the debugging
603 info for each address. When the debugging info changes,
604 it is time to create a new symbol.
606 Of course, this is rather slow and it would be better if
607 BFD would provide an iterator for enumerating all line infos. */
608 prev_name_len
= PATH_MAX
;
609 prev_filename_len
= PATH_MAX
;
610 prev_name
= xmalloc (prev_name_len
);
611 prev_filename
= xmalloc (prev_filename_len
);
615 bfd_vma vma_high
= core_text_sect
->vma
+ core_text_sect
->_raw_size
;
616 for (vma
= core_text_sect
->vma
; vma
< vma_high
; vma
+= min_insn_size
)
620 if (!get_src_info (vma
, &filename
, &dummy
.name
, &dummy
.line_num
)
621 || (prev_line_num
== dummy
.line_num
623 && strcmp (prev_name
, dummy
.name
) == 0
624 && strcmp (prev_filename
, filename
) == 0))
628 prev_line_num
= dummy
.line_num
;
630 len
= strlen (dummy
.name
);
631 if (len
>= prev_name_len
)
633 prev_name_len
= len
+ 1024;
635 prev_name
= xmalloc (prev_name_len
);
638 strcpy (prev_name
, dummy
.name
);
639 len
= strlen (filename
);
641 if (len
>= prev_filename_len
)
643 prev_filename_len
= len
+ 1024;
644 free (prev_filename
);
645 prev_filename
= xmalloc (prev_filename_len
);
648 strcpy (prev_filename
, filename
);
650 min_vma
= MIN (vma
, min_vma
);
651 max_vma
= MAX (vma
, max_vma
);
655 free (prev_filename
);
657 /* Make room for function symbols, too. */
658 ltab
.len
+= symtab
.len
;
659 ltab
.base
= (Sym
*) xmalloc (ltab
.len
* sizeof (Sym
));
660 ltab
.limit
= ltab
.base
;
662 /* Pass 2 - create symbols. */
664 /* We now set is_static as we go along, rather than by running
665 through the symbol table at the end.
667 The old way called symtab_finalize before the is_static pass,
668 causing a problem since symtab_finalize uses is_static as part of
669 its address conflict resolution algorithm. Since global symbols
670 were prefered over static symbols, and all line symbols were
671 global at that point, static function names that conflicted with
672 their own line numbers (static, but labeled as global) were
673 rejected in favor of the line num.
675 This was not the desired functionality. We always want to keep
676 our function symbols and discard any conflicting line symbols.
677 Perhaps symtab_finalize should be modified to make this
678 distinction as well, but the current fix works and the code is a
682 for (vma
= core_text_sect
->vma
; vma
< vma_high
; vma
+= min_insn_size
)
684 sym_init (ltab
.limit
);
686 if (!get_src_info (vma
, &filename
, <ab
.limit
->name
, <ab
.limit
->line_num
)
687 || (prev
&& prev
->line_num
== ltab
.limit
->line_num
688 && strcmp (prev
->name
, ltab
.limit
->name
) == 0
689 && strcmp (prev
->file
->name
, filename
) == 0))
692 /* Make name pointer a malloc'ed string. */
693 ltab
.limit
->name
= xstrdup (ltab
.limit
->name
);
694 ltab
.limit
->file
= source_file_lookup_path (filename
);
696 ltab
.limit
->addr
= vma
;
698 /* Set is_static based on the enclosing function, using either:
699 1) the previous symbol, if it's from the same function, or
700 2) a symtab lookup. */
701 if (prev
&& ltab
.limit
->file
== prev
->file
&&
702 strcmp (ltab
.limit
->name
, prev
->name
) == 0)
704 ltab
.limit
->is_static
= prev
->is_static
;
708 sym
= sym_lookup(&symtab
, ltab
.limit
->addr
);
709 ltab
.limit
->is_static
= sym
->is_static
;
714 /* If we see "main" without an initial '_', we assume names
715 are *not* prefixed by '_'. */
716 if (ltab
.limit
->name
[0] == 'm' && discard_underscores
717 && strcmp (ltab
.limit
->name
, "main") == 0)
718 discard_underscores
= 0;
720 DBG (AOUTDEBUG
, printf ("[core_create_line_syms] %lu %s 0x%lx\n",
721 (unsigned long) (ltab
.limit
- ltab
.base
),
723 (unsigned long) ltab
.limit
->addr
));
727 /* Update sentinels. */
728 sentinel
= sym_lookup (&symtab
, (bfd_vma
) 0);
731 && strcmp (sentinel
->name
, "<locore>") == 0
732 && min_vma
<= sentinel
->end_addr
)
733 sentinel
->end_addr
= min_vma
- 1;
735 sentinel
= sym_lookup (&symtab
, ~(bfd_vma
) 0);
738 && strcmp (sentinel
->name
, "<hicore>") == 0
739 && max_vma
>= sentinel
->addr
)
740 sentinel
->addr
= max_vma
+ 1;
742 /* Copy in function symbols. */
743 memcpy (ltab
.limit
, symtab
.base
, symtab
.len
* sizeof (Sym
));
744 ltab
.limit
+= symtab
.len
;
746 if ((unsigned int) (ltab
.limit
- ltab
.base
) != ltab
.len
)
749 _("%s: somebody miscounted: ltab.len=%d instead of %ld\n"),
750 whoami
, ltab
.len
, (long) (ltab
.limit
- ltab
.base
));
754 /* Finalize ltab and make it symbol table. */
755 symtab_finalize (<ab
);