9 asection
*core_text_sect
;
12 /* For mapping symbols to specific .o files during file ordering. */
18 struct function_map
*symbol_map
;
22 DEFUN (read_function_mappings
, (filename
), const char *filename
)
24 FILE *file
= fopen (filename
, "r");
30 fprintf (stderr
, "%s: could not open %s.\n", whoami
, filename
);
34 /* First parse the mapping file so we know how big we need to
35 make our tables. We also do some sanity checks at this
41 matches
= fscanf (file
, "%[^\n:]", dummy
);
44 fprintf (stderr
, "%s: unable to parse mapping file %s.\n",
49 /* Just skip messages about files with no symbols. */
50 if (!strncmp (dummy
, "No symbols in ", 14))
56 /* Don't care what else is on this line at this point. */
57 fscanf (file
, "%[^\n]\n", dummy
);
61 /* Now we know how big we need to make our table. */
62 symbol_map
= xmalloc (count
* sizeof (struct function_map
));
64 /* Rewind the input file so we can read it again. */
67 /* Read each entry and put it into the table. */
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 /* dummy has the filename, go ahead and copy it. */
90 symbol_map
[count
].file_name
= xmalloc (strlen (dummy
) + 1);
91 strcpy (symbol_map
[count
].file_name
, dummy
);
93 /* Now we need the function name. */
94 fscanf (file
, "%[^\n]\n", dummy
);
95 tmp
= strrchr (dummy
, ' ') + 1;
96 symbol_map
[count
].function_name
= xmalloc (strlen (tmp
) + 1);
97 strcpy (symbol_map
[count
].function_name
, tmp
);
101 /* Record the size of the map table for future reference. */
102 symbol_map_count
= count
;
106 DEFUN (core_init
, (a_out_name
), const char *a_out_name
)
108 core_bfd
= bfd_openr (a_out_name
, 0);
116 if (!bfd_check_format (core_bfd
, bfd_object
))
118 fprintf (stderr
, "%s: %s: not in a.out format\n", whoami
, a_out_name
);
122 /* get core's text section: */
123 core_text_sect
= bfd_get_section_by_name (core_bfd
, ".text");
126 core_text_sect
= bfd_get_section_by_name (core_bfd
, "$CODE$");
129 fprintf (stderr
, "%s: can't find .text section in %s\n",
135 /* read core's symbol table: */
137 /* this will probably give us more than we need, but that's ok: */
138 core_num_syms
= bfd_get_symtab_upper_bound (core_bfd
);
139 if (core_num_syms
< 0)
141 fprintf (stderr
, "%s: %s: %s\n", whoami
, a_out_name
,
142 bfd_errmsg (bfd_get_error ()));
146 core_syms
= (asymbol
**) xmalloc (core_num_syms
);
147 core_num_syms
= bfd_canonicalize_symtab (core_bfd
, core_syms
);
148 if (core_num_syms
< 0)
150 fprintf (stderr
, "%s: %s: %s\n", whoami
, a_out_name
,
151 bfd_errmsg (bfd_get_error ()));
155 if (function_mapping_file
)
156 read_function_mappings (function_mapping_file
);
161 * Read in the text space of an a.out file
164 DEFUN (core_get_text_space
, (core_bfd
), bfd
* core_bfd
)
166 core_text_space
= (PTR
) malloc (core_text_sect
->_raw_size
);
168 if (!core_text_space
)
170 fprintf (stderr
, "%s: ran out room for %ld bytes of text space\n",
171 whoami
, core_text_sect
->_raw_size
);
174 if (!bfd_get_section_contents (core_bfd
, core_text_sect
, core_text_space
,
175 0, core_text_sect
->_raw_size
))
177 bfd_perror ("bfd_get_section_contents");
178 free (core_text_space
);
181 if (!core_text_space
)
183 fprintf (stderr
, "%s: can't do -c\n", whoami
);
189 * Return class of symbol SYM. The returned class can be any of:
190 * 0 -> symbol is not interesting to us
191 * 'T' -> symbol is a global name
192 * 't' -> symbol is a local (static) name
195 DEFUN (core_sym_class
, (sym
), asymbol
* sym
)
203 * Must be a text symbol, and static text symbols don't qualify if
204 * ignore_static_funcs set.
211 if (ignore_static_funcs
&& (sym
->flags
& BSF_LOCAL
))
213 DBG (AOUTDEBUG
, printf ("[core_sym_class] %s: not a function\n",
218 bfd_get_symbol_info (core_bfd
, sym
, &syminfo
);
223 return i
; /* it's a global symbol */
228 /* not a static text symbol */
229 DBG (AOUTDEBUG
, printf ("[core_sym_class] %s is of class %c\n",
234 /* do some more filtering on static function-names: */
236 if (ignore_static_funcs
)
241 * Can't zero-length name or funny characters in name, where
242 * `funny' includes: `.' (.o file names) and `$' (Pascal labels).
244 if (!sym
->name
|| sym
->name
[0] == '\0')
249 for (name
= sym
->name
; *name
; ++name
)
251 if (*name
== '.' || *name
== '$')
257 * On systems where the C compiler adds an underscore to all
258 * names, static names without underscores seem usually to be
259 * labels in hand written assembler in the library. We don't want
260 * these names. This is certainly necessary on a Sparc running
261 * SunOS 4.1 (try profiling a program that does a lot of
262 * division). I don't know whether it has harmful side effects on
263 * other systems. Perhaps it should be made configurable.
265 sym_prefix
= bfd_get_symbol_leading_char (core_bfd
);
266 if (sym_prefix
&& sym_prefix
!= sym
->name
[0]
268 * GCC may add special symbols to help gdb figure out the file
269 * language. We want to ignore these, since sometimes they mask
270 * the real function. (dj@ctron)
272 || !strncmp (sym
->name
, "__gnu_compiled", 14)
273 || !strncmp (sym
->name
, "___gnu_compiled", 15))
278 /* If the object file supports marking of function symbols, then we can
279 zap anything that doesn't have BSF_FUNCTION set. */
280 if (ignore_non_functions
&& (sym
->flags
& BSF_FUNCTION
) == 0)
283 return 't'; /* it's a static text symbol */
288 * Get whatever source info we can get regarding address ADDR:
291 DEFUN (get_src_info
, (addr
, filename
, name
, line_num
),
292 bfd_vma addr AND
const char **filename AND
const char **name
295 const char *fname
= 0, *func_name
= 0;
298 if (bfd_find_nearest_line (core_bfd
, core_text_sect
, core_syms
,
299 addr
- core_text_sect
->vma
,
300 &fname
, &func_name
, (unsigned int *) &l
)
301 && fname
&& func_name
&& l
)
303 DBG (AOUTDEBUG
, printf ("[get_src_info] 0x%lx -> %s:%d (%s)\n",
304 addr
, fname
, l
, func_name
));
312 DBG (AOUTDEBUG
, printf ("[get_src_info] no info for 0x%lx (%s:%d,%s)\n",
313 (long) addr
, fname
? fname
: "<unknown>", l
,
314 func_name
? func_name
: "<unknown>"));
321 * Read in symbol table from core. One symbol per function is
325 DEFUN (core_create_function_syms
, (core_bfd
), bfd
* core_bfd
)
327 bfd_vma min_vma
= ~0, max_vma
= 0;
328 const char *filename
, *func_name
;
330 long i
, j
, found
, skip
;
332 /* pass 1 - determine upper bound on number of function names: */
334 for (i
= 0; i
< core_num_syms
; ++i
)
336 if (!core_sym_class (core_syms
[i
]))
341 /* This should be replaced with a binary search or hashed
344 Don't create a symtab entry for a function that has
345 a mapping to a file, unless it's the first function
348 for (j
= 0; j
< symbol_map_count
; j
++)
349 if (!strcmp (core_syms
[i
]->name
, symbol_map
[j
].function_name
))
351 if (j
> 0 && ! strcmp (symbol_map
[j
].file_name
,
352 symbol_map
[j
- 1].file_name
))
362 fprintf (stderr
, "%s: file `%s' has no symbols\n", whoami
, a_out_name
);
366 /* the "+ 2" is for the sentinels: */
367 symtab
.base
= (Sym
*) xmalloc ((symtab
.len
+ 2) * sizeof (Sym
));
369 /* pass 2 - create symbols: */
371 symtab
.limit
= symtab
.base
;
372 for (i
= 0; i
< core_num_syms
; ++i
)
374 class = core_sym_class (core_syms
[i
]);
378 printf ("[core_create_function_syms] rejecting: 0x%lx %s\n",
379 core_syms
[i
]->value
, core_syms
[i
]->name
));
382 /* This should be replaced with a binary search or hashed
387 for (j
= 0; j
< symbol_map_count
; j
++)
388 if (!strcmp (core_syms
[i
]->name
, symbol_map
[j
].function_name
))
390 if (j
> 0 && ! strcmp (symbol_map
[j
].file_name
,
391 symbol_map
[j
- 1].file_name
))
401 sym_init (symtab
.limit
);
403 /* symbol offsets are always section-relative: */
405 symtab
.limit
->addr
= core_syms
[i
]->value
+ core_syms
[i
]->section
->vma
;
407 && !strcmp (core_syms
[i
]->name
, symbol_map
[found
].function_name
))
409 symtab
.limit
->name
= symbol_map
[found
].file_name
;
410 symtab
.limit
->mapped
= 1;
414 symtab
.limit
->name
= core_syms
[i
]->name
;
415 symtab
.limit
->mapped
= 0;
420 * Suppress symbols that are not function names. This is
421 * useful to suppress code-labels and aliases.
423 * This is known to be useful under DEC's OSF/1. Under SunOS 4.x,
424 * labels do not appear in the symbol table info, so this isn't
427 if (get_src_info (symtab
.limit
->addr
, &filename
, &func_name
,
428 &symtab
.limit
->line_num
))
430 symtab
.limit
->file
= source_file_lookup_path (filename
);
432 if (strcmp (symtab
.limit
->name
, func_name
) != 0)
435 * The symbol's address maps to a different name, so
436 * it can't be a function-entry point. This happens
437 * for labels, for example.
440 printf ("[core_create_function_syms: rej %s (maps to %s)\n",
441 symtab
.limit
->name
, func_name
));
447 symtab
.limit
->is_func
= TRUE
;
448 symtab
.limit
->is_bb_head
= TRUE
;
451 symtab
.limit
->is_static
= TRUE
;
454 min_vma
= MIN (symtab
.limit
->addr
, min_vma
);
455 max_vma
= MAX (symtab
.limit
->addr
, max_vma
);
458 * If we see "main" without an initial '_', we assume names
459 * are *not* prefixed by '_'.
461 if (symtab
.limit
->name
[0] == 'm' && discard_underscores
462 && strcmp (symtab
.limit
->name
, "main") == 0)
464 discard_underscores
= 0;
467 DBG (AOUTDEBUG
, printf ("[core_create_function_syms] %ld %s 0x%lx\n",
468 (long) (symtab
.limit
- symtab
.base
),
469 symtab
.limit
->name
, symtab
.limit
->addr
));
473 /* create sentinels: */
475 sym_init (symtab
.limit
);
476 symtab
.limit
->name
= "<locore>";
477 symtab
.limit
->addr
= 0;
478 symtab
.limit
->end_addr
= min_vma
- 1;
481 sym_init (symtab
.limit
);
482 symtab
.limit
->name
= "<hicore>";
483 symtab
.limit
->addr
= max_vma
+ 1;
484 symtab
.limit
->end_addr
= ~0;
487 symtab
.len
= symtab
.limit
- symtab
.base
;
488 symtab_finalize (&symtab
);
493 * Read in symbol table from core. One symbol per line of source code
497 DEFUN (core_create_line_syms
, (core_bfd
), bfd
* core_bfd
)
499 char prev_name
[PATH_MAX
], prev_filename
[PATH_MAX
];
500 bfd_vma vma
, min_vma
= ~0, max_vma
= 0;
501 bfd_vma offset
, prev_offset
, min_dist
;
502 Sym
*prev
, dummy
, *sentinel
, *sym
;
503 const char *filename
;
504 int prev_line_num
, i
;
507 * Create symbols for functions as usual. This is necessary in
508 * cases where parts of a program were not compiled with -g. For
509 * those parts we still want to get info at the function level:
511 core_create_function_syms (core_bfd
);
513 /* pass 1 - counter number of symbols: */
516 * To find all line information, walk through all possible
517 * text-space addresses (one by one!) and get the debugging
518 * info for each address. When the debugging info changes,
519 * it is time to create a new symbol.
521 * Of course, this is rather slow and it would be better if
522 * bfd would provide an iterator for enumerating all line
523 * infos, but for now, we try to speed up the second pass
524 * by determining what the minimum code distance between two
529 min_dist
= core_text_sect
->_raw_size
;
530 prev_offset
= -min_dist
;
531 prev_filename
[0] = '\0';
533 for (offset
= 0; offset
< core_text_sect
->_raw_size
; ++offset
)
535 vma
= core_text_sect
->vma
+ offset
;
536 if (!get_src_info (vma
, &filename
, &dummy
.name
, &dummy
.line_num
)
537 || (prev_line_num
== dummy
.line_num
&&
538 strcmp (prev_name
, dummy
.name
) == 0
539 && strcmp (prev_filename
, filename
) == 0))
545 prev_line_num
= dummy
.line_num
;
546 strcpy (prev_name
, dummy
.name
);
547 strcpy (prev_filename
, filename
);
549 if (offset
- prev_offset
< min_dist
)
551 min_dist
= offset
- prev_offset
;
553 prev_offset
= offset
;
555 min_vma
= MIN (vma
, min_vma
);
556 max_vma
= MAX (vma
, max_vma
);
559 DBG (AOUTDEBUG
, printf ("[core_create_line_syms] min_dist=%lx\n", min_dist
));
561 /* make room for function symbols, too: */
562 ltab
.len
+= symtab
.len
;
563 ltab
.base
= (Sym
*) xmalloc (ltab
.len
* sizeof (Sym
));
564 ltab
.limit
= ltab
.base
;
566 /* pass 2 - create symbols: */
569 for (offset
= 0; offset
< core_text_sect
->_raw_size
; offset
+= min_dist
)
571 sym_init (ltab
.limit
);
572 if (!get_src_info (core_text_sect
->vma
+ offset
, &filename
,
573 <ab
.limit
->name
, <ab
.limit
->line_num
)
574 || (prev
&& prev
->line_num
== ltab
.limit
->line_num
575 && strcmp (prev
->name
, ltab
.limit
->name
) == 0
576 && strcmp (prev
->file
->name
, filename
) == 0))
581 /* make name pointer a malloc'ed string: */
582 ltab
.limit
->name
= strdup (ltab
.limit
->name
);
583 ltab
.limit
->file
= source_file_lookup_path (filename
);
585 ltab
.limit
->addr
= core_text_sect
->vma
+ offset
;
589 * If we see "main" without an initial '_', we assume names
590 * are *not* prefixed by '_'.
592 if (ltab
.limit
->name
[0] == 'm' && discard_underscores
593 && strcmp (ltab
.limit
->name
, "main") == 0)
595 discard_underscores
= 0;
598 DBG (AOUTDEBUG
, printf ("[core_create_line_syms] %d %s 0x%lx\n",
599 ltab
.len
, ltab
.limit
->name
,
604 /* update sentinels: */
606 sentinel
= sym_lookup (&symtab
, 0);
607 if (strcmp (sentinel
->name
, "<locore>") == 0
608 && min_vma
<= sentinel
->end_addr
)
610 sentinel
->end_addr
= min_vma
- 1;
613 sentinel
= sym_lookup (&symtab
, ~0);
614 if (strcmp (sentinel
->name
, "<hicore>") == 0 && max_vma
>= sentinel
->addr
)
616 sentinel
->addr
= max_vma
+ 1;
619 /* copy in function symbols: */
620 memcpy (ltab
.limit
, symtab
.base
, symtab
.len
* sizeof (Sym
));
621 ltab
.limit
+= symtab
.len
;
623 if (ltab
.limit
- ltab
.base
!= ltab
.len
)
626 "%s: somebody miscounted: ltab.len=%ld instead of %d\n",
627 whoami
, (long) (ltab
.limit
- ltab
.base
), ltab
.len
);
631 /* finalize ltab and make it symbol table: */
633 symtab_finalize (<ab
);
637 /* now go through all core symbols and set is_static accordingly: */
639 for (i
= 0; i
< core_num_syms
; ++i
)
641 if (core_sym_class (core_syms
[i
]) == 't')
643 sym
= sym_lookup (&symtab
, core_syms
[i
]->value
644 + core_syms
[i
]->section
->vma
);
647 sym
++->is_static
= TRUE
;
649 while (sym
->file
== sym
[-1].file
&&
650 strcmp (sym
->name
, sym
[-1].name
) == 0);