Commit | Line | Data |
---|---|---|
5489fcc3 KR |
1 | #include "libiberty.h" |
2 | #include "gprof.h" | |
3 | #include "core.h" | |
4 | #include "symtab.h" | |
5 | ||
6 | bfd *core_bfd; | |
7 | int core_num_syms; | |
8 | asymbol **core_syms; | |
9 | asection *core_text_sect; | |
10 | PTR core_text_space; | |
11 | ||
12 | ||
13 | void | |
12516a37 | 14 | DEFUN (core_init, (a_out_name), const char *a_out_name) |
5489fcc3 | 15 | { |
12516a37 KR |
16 | core_bfd = bfd_openr (a_out_name, 0); |
17 | ||
18 | if (!core_bfd) | |
19 | { | |
20 | perror (a_out_name); | |
21 | done (1); | |
03c35bcb | 22 | } |
12516a37 KR |
23 | |
24 | if (!bfd_check_format (core_bfd, bfd_object)) | |
25 | { | |
26 | fprintf (stderr, "%s: %s: not in a.out format\n", whoami, a_out_name); | |
27 | done (1); | |
03c35bcb | 28 | } |
12516a37 KR |
29 | |
30 | /* get core's text section: */ | |
31 | core_text_sect = bfd_get_section_by_name (core_bfd, ".text"); | |
32 | if (!core_text_sect) | |
33 | { | |
34 | core_text_sect = bfd_get_section_by_name (core_bfd, "$CODE$"); | |
35 | if (!core_text_sect) | |
36 | { | |
37 | fprintf (stderr, "%s: can't find .text section in %s\n", | |
38 | whoami, a_out_name); | |
39 | done (1); | |
03c35bcb KR |
40 | } |
41 | } | |
12516a37 KR |
42 | |
43 | /* read core's symbol table: */ | |
44 | ||
45 | /* this will probably give us more than we need, but that's ok: */ | |
46 | core_num_syms = bfd_get_symtab_upper_bound (core_bfd); | |
47 | if (core_num_syms < 0) | |
48 | { | |
49 | fprintf (stderr, "%s: %s: %s\n", whoami, a_out_name, | |
50 | bfd_errmsg (bfd_get_error ())); | |
51 | done (1); | |
03c35bcb | 52 | } |
12516a37 KR |
53 | |
54 | core_syms = (asymbol **) xmalloc (core_num_syms); | |
55 | core_num_syms = bfd_canonicalize_symtab (core_bfd, core_syms); | |
56 | if (core_num_syms < 0) | |
57 | { | |
58 | fprintf (stderr, "%s: %s: %s\n", whoami, a_out_name, | |
59 | bfd_errmsg (bfd_get_error ())); | |
60 | done (1); | |
03c35bcb KR |
61 | } |
62 | } | |
5489fcc3 KR |
63 | |
64 | ||
65 | /* | |
66 | * Read in the text space of an a.out file | |
67 | */ | |
68 | void | |
12516a37 | 69 | DEFUN (core_get_text_space, (core_bfd), bfd * core_bfd) |
5489fcc3 | 70 | { |
12516a37 KR |
71 | core_text_space = (PTR) malloc (core_text_sect->_raw_size); |
72 | ||
73 | if (!core_text_space) | |
5489fcc3 | 74 | { |
12516a37 KR |
75 | fprintf (stderr, "%s: ran out room for %ld bytes of text space\n", |
76 | whoami, core_text_sect->_raw_size); | |
77 | done (1); | |
03c35bcb | 78 | } |
12516a37 KR |
79 | if (!bfd_get_section_contents (core_bfd, core_text_sect, core_text_space, |
80 | 0, core_text_sect->_raw_size)) | |
81 | { | |
82 | bfd_perror ("bfd_get_section_contents"); | |
83 | free (core_text_space); | |
84 | core_text_space = 0; | |
03c35bcb | 85 | } |
12516a37 KR |
86 | if (!core_text_space) |
87 | { | |
88 | fprintf (stderr, "%s: can't do -c\n", whoami); | |
03c35bcb KR |
89 | } |
90 | } | |
5489fcc3 KR |
91 | |
92 | ||
93 | /* | |
94 | * Return class of symbol SYM. The returned class can be any of: | |
12516a37 KR |
95 | * 0 -> symbol is not interesting to us |
96 | * 'T' -> symbol is a global name | |
97 | * 't' -> symbol is a local (static) name | |
5489fcc3 KR |
98 | */ |
99 | static int | |
12516a37 | 100 | DEFUN (core_sym_class, (sym), asymbol * sym) |
5489fcc3 | 101 | { |
12516a37 KR |
102 | symbol_info syminfo; |
103 | const char *name; | |
104 | char sym_prefix; | |
105 | int i; | |
106 | ||
107 | /* | |
108 | * Must be a text symbol, and static text symbols don't qualify if | |
109 | * ignore_static_funcs set. | |
110 | */ | |
111 | if (!sym->section) | |
112 | { | |
113 | return 0; | |
03c35bcb | 114 | } |
12516a37 KR |
115 | |
116 | if (ignore_static_funcs && (sym->flags & BSF_LOCAL)) | |
117 | { | |
118 | DBG (AOUTDEBUG, printf ("[core_sym_class] %s: not a function\n", | |
5489fcc3 | 119 | sym->name)); |
12516a37 | 120 | return 0; |
03c35bcb | 121 | } |
5489fcc3 | 122 | |
12516a37 KR |
123 | bfd_get_symbol_info (core_bfd, sym, &syminfo); |
124 | i = syminfo.type; | |
5489fcc3 | 125 | |
12516a37 KR |
126 | if (i == 'T') |
127 | { | |
128 | return i; /* it's a global symbol */ | |
03c35bcb | 129 | } |
5489fcc3 | 130 | |
12516a37 KR |
131 | if (i != 't') |
132 | { | |
133 | /* not a static text symbol */ | |
134 | DBG (AOUTDEBUG, printf ("[core_sym_class] %s is of class %c\n", | |
5489fcc3 | 135 | sym->name, i)); |
12516a37 | 136 | return 0; |
03c35bcb | 137 | } |
12516a37 KR |
138 | |
139 | /* do some more filtering on static function-names: */ | |
140 | ||
141 | if (ignore_static_funcs) | |
142 | { | |
143 | return 0; | |
03c35bcb | 144 | } |
12516a37 KR |
145 | /* |
146 | * Can't zero-length name or funny characters in name, where | |
147 | * `funny' includes: `.' (.o file names) and `$' (Pascal labels). | |
148 | */ | |
149 | if (!sym->name || sym->name[0] == '\0') | |
5489fcc3 | 150 | { |
12516a37 | 151 | return 0; |
03c35bcb | 152 | } |
12516a37 KR |
153 | |
154 | for (name = sym->name; *name; ++name) | |
155 | { | |
156 | if (*name == '.' || *name == '$') | |
157 | { | |
158 | return 0; | |
03c35bcb KR |
159 | } |
160 | } | |
12516a37 KR |
161 | /* |
162 | * On systems where the C compiler adds an underscore to all | |
163 | * names, static names without underscores seem usually to be | |
164 | * labels in hand written assembler in the library. We don't want | |
165 | * these names. This is certainly necessary on a Sparc running | |
166 | * SunOS 4.1 (try profiling a program that does a lot of | |
167 | * division). I don't know whether it has harmful side effects on | |
168 | * other systems. Perhaps it should be made configurable. | |
169 | */ | |
170 | sym_prefix = bfd_get_symbol_leading_char (core_bfd); | |
171 | if (sym_prefix && sym_prefix != sym->name[0] | |
172 | /* | |
173 | * GCC may add special symbols to help gdb figure out the file | |
174 | * language. We want to ignore these, since sometimes they mask | |
175 | * the real function. (dj@ctron) | |
176 | */ | |
177 | || !strncmp (sym->name, "__gnu_compiled", 14) | |
178 | || !strncmp (sym->name, "___gnu_compiled", 15)) | |
179 | { | |
180 | return 0; | |
03c35bcb | 181 | } |
32843f94 JL |
182 | |
183 | /* If the object file supports marking of function symbols, then we can | |
184 | zap anything that doesn't have BSF_FUNCTION set. */ | |
185 | if (ignore_non_functions && (sym->flags & BSF_FUNCTION) == 0) | |
186 | return 0; | |
187 | ||
12516a37 | 188 | return 't'; /* it's a static text symbol */ |
03c35bcb | 189 | } |
5489fcc3 KR |
190 | |
191 | ||
192 | /* | |
193 | * Get whatever source info we can get regarding address ADDR: | |
194 | */ | |
195 | static bool | |
12516a37 KR |
196 | DEFUN (get_src_info, (addr, filename, name, line_num), |
197 | bfd_vma addr AND const char **filename AND const char **name | |
198 | AND int *line_num) | |
5489fcc3 | 199 | { |
12516a37 KR |
200 | const char *fname = 0, *func_name = 0; |
201 | int l = 0; | |
5489fcc3 | 202 | |
12516a37 KR |
203 | if (bfd_find_nearest_line (core_bfd, core_text_sect, core_syms, |
204 | addr - core_text_sect->vma, | |
643f17d2 | 205 | &fname, &func_name, (unsigned int *) &l) |
12516a37 | 206 | && fname && func_name && l) |
5489fcc3 | 207 | { |
12516a37 | 208 | DBG (AOUTDEBUG, printf ("[get_src_info] 0x%lx -> %s:%d (%s)\n", |
5489fcc3 | 209 | addr, fname, l, func_name)); |
12516a37 KR |
210 | *filename = fname; |
211 | *name = func_name; | |
212 | *line_num = l; | |
213 | return TRUE; | |
214 | } | |
215 | else | |
216 | { | |
217 | DBG (AOUTDEBUG, printf ("[get_src_info] no info for 0x%lx (%s:%d,%s)\n", | |
5489fcc3 KR |
218 | (long) addr, fname ? fname : "<unknown>", l, |
219 | func_name ? func_name : "<unknown>")); | |
12516a37 | 220 | return FALSE; |
03c35bcb KR |
221 | } |
222 | } | |
5489fcc3 KR |
223 | |
224 | ||
225 | /* | |
226 | * Read in symbol table from core. One symbol per function is | |
227 | * entered. | |
228 | */ | |
229 | void | |
12516a37 | 230 | DEFUN (core_create_function_syms, (core_bfd), bfd * core_bfd) |
5489fcc3 | 231 | { |
12516a37 KR |
232 | bfd_vma min_vma = ~0, max_vma = 0; |
233 | const char *filename, *func_name; | |
234 | int class; | |
235 | long i; | |
236 | ||
237 | /* pass 1 - determine upper bound on number of function names: */ | |
238 | symtab.len = 0; | |
239 | for (i = 0; i < core_num_syms; ++i) | |
240 | { | |
241 | if (!core_sym_class (core_syms[i])) | |
242 | { | |
243 | continue; | |
03c35bcb | 244 | } |
12516a37 | 245 | ++symtab.len; |
03c35bcb | 246 | } |
12516a37 KR |
247 | |
248 | if (symtab.len == 0) | |
249 | { | |
250 | fprintf (stderr, "%s: file `%s' has no symbols\n", whoami, a_out_name); | |
251 | done (1); | |
03c35bcb | 252 | } |
12516a37 KR |
253 | |
254 | /* the "+ 2" is for the sentinels: */ | |
255 | symtab.base = (Sym *) xmalloc ((symtab.len + 2) * sizeof (Sym)); | |
256 | ||
257 | /* pass 2 - create symbols: */ | |
258 | ||
259 | symtab.limit = symtab.base; | |
260 | for (i = 0; i < core_num_syms; ++i) | |
261 | { | |
262 | class = core_sym_class (core_syms[i]); | |
263 | if (!class) | |
264 | { | |
265 | DBG (AOUTDEBUG, | |
266 | printf ("[core_create_function_syms] rejecting: 0x%lx %s\n", | |
5489fcc3 | 267 | core_syms[i]->value, core_syms[i]->name)); |
12516a37 | 268 | continue; |
03c35bcb | 269 | } |
5489fcc3 | 270 | |
12516a37 | 271 | sym_init (symtab.limit); |
5489fcc3 | 272 | |
12516a37 | 273 | /* symbol offsets are always section-relative: */ |
5489fcc3 | 274 | |
12516a37 KR |
275 | symtab.limit->addr = core_syms[i]->value + core_syms[i]->section->vma; |
276 | symtab.limit->name = core_syms[i]->name; | |
5489fcc3 KR |
277 | |
278 | #ifdef __osf__ | |
12516a37 KR |
279 | /* |
280 | * Suppress symbols that are not function names. This is | |
281 | * useful to suppress code-labels and aliases. | |
282 | * | |
283 | * This is known to be useful under DEC's OSF/1. Under SunOS 4.x, | |
284 | * labels do not appear in the symbol table info, so this isn't | |
285 | * necessary. | |
286 | */ | |
287 | if (get_src_info (symtab.limit->addr, &filename, &func_name, | |
288 | &symtab.limit->line_num)) | |
5489fcc3 | 289 | { |
12516a37 KR |
290 | symtab.limit->file = source_file_lookup_path (filename); |
291 | ||
292 | if (strcmp (symtab.limit->name, func_name) != 0) | |
293 | { | |
294 | /* | |
295 | * The symbol's address maps to a different name, so | |
296 | * it can't be a function-entry point. This happens | |
297 | * for labels, for example. | |
298 | */ | |
299 | DBG (AOUTDEBUG, | |
300 | printf ("[core_create_function_syms: rej %s (maps to %s)\n", | |
301 | symtab.limit->name, func_name)); | |
302 | continue; | |
03c35bcb KR |
303 | } |
304 | } | |
5489fcc3 KR |
305 | #endif |
306 | ||
12516a37 KR |
307 | symtab.limit->is_func = TRUE; |
308 | symtab.limit->is_bb_head = TRUE; | |
309 | if (class == 't') | |
310 | { | |
311 | symtab.limit->is_static = TRUE; | |
03c35bcb | 312 | } |
12516a37 KR |
313 | |
314 | min_vma = MIN (symtab.limit->addr, min_vma); | |
315 | max_vma = MAX (symtab.limit->addr, max_vma); | |
316 | ||
317 | /* | |
318 | * If we see "main" without an initial '_', we assume names | |
319 | * are *not* prefixed by '_'. | |
320 | */ | |
321 | if (symtab.limit->name[0] == 'm' && discard_underscores | |
322 | && strcmp (symtab.limit->name, "main") == 0) | |
5489fcc3 | 323 | { |
12516a37 | 324 | discard_underscores = 0; |
03c35bcb | 325 | } |
5489fcc3 | 326 | |
12516a37 KR |
327 | DBG (AOUTDEBUG, printf ("[core_create_function_syms] %ld %s 0x%lx\n", |
328 | (long) (symtab.limit - symtab.base), | |
5489fcc3 | 329 | symtab.limit->name, symtab.limit->addr)); |
12516a37 | 330 | ++symtab.limit; |
03c35bcb | 331 | } |
5489fcc3 | 332 | |
12516a37 | 333 | /* create sentinels: */ |
5489fcc3 | 334 | |
12516a37 KR |
335 | sym_init (symtab.limit); |
336 | symtab.limit->name = "<locore>"; | |
337 | symtab.limit->addr = 0; | |
338 | symtab.limit->end_addr = min_vma - 1; | |
339 | ++symtab.limit; | |
5489fcc3 | 340 | |
12516a37 KR |
341 | sym_init (symtab.limit); |
342 | symtab.limit->name = "<hicore>"; | |
343 | symtab.limit->addr = max_vma + 1; | |
344 | symtab.limit->end_addr = ~0; | |
345 | ++symtab.limit; | |
5489fcc3 | 346 | |
12516a37 KR |
347 | symtab.len = symtab.limit - symtab.base; |
348 | symtab_finalize (&symtab); | |
03c35bcb | 349 | } |
5489fcc3 KR |
350 | |
351 | ||
352 | /* | |
353 | * Read in symbol table from core. One symbol per line of source code | |
354 | * is entered. | |
355 | */ | |
356 | void | |
12516a37 | 357 | DEFUN (core_create_line_syms, (core_bfd), bfd * core_bfd) |
5489fcc3 | 358 | { |
12516a37 KR |
359 | char prev_name[PATH_MAX], prev_filename[PATH_MAX]; |
360 | bfd_vma vma, min_vma = ~0, max_vma = 0; | |
361 | bfd_vma offset, prev_offset, min_dist; | |
362 | Sym *prev, dummy, *sentinel, *sym; | |
363 | const char *filename; | |
364 | int prev_line_num, i; | |
365 | Sym_Table ltab; | |
366 | /* | |
367 | * Create symbols for functions as usual. This is necessary in | |
368 | * cases where parts of a program were not compiled with -g. For | |
369 | * those parts we still want to get info at the function level: | |
370 | */ | |
371 | core_create_function_syms (core_bfd); | |
372 | ||
373 | /* pass 1 - counter number of symbols: */ | |
374 | ||
375 | /* | |
376 | * To find all line information, walk through all possible | |
377 | * text-space addresses (one by one!) and get the debugging | |
378 | * info for each address. When the debugging info changes, | |
379 | * it is time to create a new symbol. | |
380 | * | |
381 | * Of course, this is rather slow and it would be better if | |
382 | * bfd would provide an iterator for enumerating all line | |
383 | * infos, but for now, we try to speed up the second pass | |
384 | * by determining what the minimum code distance between two | |
385 | * lines is. | |
386 | */ | |
387 | prev_name[0] = '\0'; | |
388 | ltab.len = 0; | |
389 | min_dist = core_text_sect->_raw_size; | |
390 | prev_offset = -min_dist; | |
391 | prev_filename[0] = '\0'; | |
392 | prev_line_num = 0; | |
393 | for (offset = 0; offset < core_text_sect->_raw_size; ++offset) | |
394 | { | |
395 | vma = core_text_sect->vma + offset; | |
396 | if (!get_src_info (vma, &filename, &dummy.name, &dummy.line_num) | |
397 | || (prev_line_num == dummy.line_num && | |
398 | strcmp (prev_name, dummy.name) == 0 | |
399 | && strcmp (prev_filename, filename) == 0)) | |
5489fcc3 | 400 | { |
12516a37 | 401 | continue; |
03c35bcb | 402 | } |
12516a37 KR |
403 | |
404 | ++ltab.len; | |
405 | prev_line_num = dummy.line_num; | |
406 | strcpy (prev_name, dummy.name); | |
407 | strcpy (prev_filename, filename); | |
408 | ||
409 | if (offset - prev_offset < min_dist) | |
5489fcc3 | 410 | { |
12516a37 | 411 | min_dist = offset - prev_offset; |
03c35bcb | 412 | } |
12516a37 KR |
413 | prev_offset = offset; |
414 | ||
415 | min_vma = MIN (vma, min_vma); | |
416 | max_vma = MAX (vma, max_vma); | |
03c35bcb | 417 | } |
12516a37 KR |
418 | |
419 | DBG (AOUTDEBUG, printf ("[core_create_line_syms] min_dist=%lx\n", min_dist)); | |
420 | ||
421 | /* make room for function symbols, too: */ | |
422 | ltab.len += symtab.len; | |
423 | ltab.base = (Sym *) xmalloc (ltab.len * sizeof (Sym)); | |
424 | ltab.limit = ltab.base; | |
425 | ||
426 | /* pass 2 - create symbols: */ | |
427 | ||
428 | prev = 0; | |
429 | for (offset = 0; offset < core_text_sect->_raw_size; offset += min_dist) | |
430 | { | |
431 | sym_init (ltab.limit); | |
432 | if (!get_src_info (core_text_sect->vma + offset, &filename, | |
433 | <ab.limit->name, <ab.limit->line_num) | |
434 | || (prev && prev->line_num == ltab.limit->line_num | |
435 | && strcmp (prev->name, ltab.limit->name) == 0 | |
436 | && strcmp (prev->file->name, filename) == 0)) | |
5489fcc3 | 437 | { |
12516a37 | 438 | continue; |
03c35bcb | 439 | } |
12516a37 KR |
440 | |
441 | /* make name pointer a malloc'ed string: */ | |
442 | ltab.limit->name = strdup (ltab.limit->name); | |
443 | ltab.limit->file = source_file_lookup_path (filename); | |
444 | ||
445 | ltab.limit->addr = core_text_sect->vma + offset; | |
446 | prev = ltab.limit; | |
447 | ||
448 | /* | |
449 | * If we see "main" without an initial '_', we assume names | |
450 | * are *not* prefixed by '_'. | |
451 | */ | |
452 | if (ltab.limit->name[0] == 'm' && discard_underscores | |
453 | && strcmp (ltab.limit->name, "main") == 0) | |
454 | { | |
455 | discard_underscores = 0; | |
03c35bcb | 456 | } |
5489fcc3 | 457 | |
12516a37 | 458 | DBG (AOUTDEBUG, printf ("[core_create_line_syms] %d %s 0x%lx\n", |
5489fcc3 KR |
459 | ltab.len, ltab.limit->name, |
460 | ltab.limit->addr)); | |
12516a37 | 461 | ++ltab.limit; |
03c35bcb | 462 | } |
12516a37 KR |
463 | |
464 | /* update sentinels: */ | |
465 | ||
466 | sentinel = sym_lookup (&symtab, 0); | |
467 | if (strcmp (sentinel->name, "<locore>") == 0 | |
468 | && min_vma <= sentinel->end_addr) | |
469 | { | |
470 | sentinel->end_addr = min_vma - 1; | |
03c35bcb | 471 | } |
12516a37 KR |
472 | |
473 | sentinel = sym_lookup (&symtab, ~0); | |
474 | if (strcmp (sentinel->name, "<hicore>") == 0 && max_vma >= sentinel->addr) | |
475 | { | |
476 | sentinel->addr = max_vma + 1; | |
03c35bcb | 477 | } |
5489fcc3 | 478 | |
12516a37 KR |
479 | /* copy in function symbols: */ |
480 | memcpy (ltab.limit, symtab.base, symtab.len * sizeof (Sym)); | |
481 | ltab.limit += symtab.len; | |
5489fcc3 | 482 | |
12516a37 | 483 | if (ltab.limit - ltab.base != ltab.len) |
5489fcc3 | 484 | { |
12516a37 KR |
485 | fprintf (stderr, |
486 | "%s: somebody miscounted: ltab.len=%ld instead of %d\n", | |
487 | whoami, (long) (ltab.limit - ltab.base), ltab.len); | |
488 | done (1); | |
03c35bcb | 489 | } |
12516a37 KR |
490 | |
491 | /* finalize ltab and make it symbol table: */ | |
492 | ||
493 | symtab_finalize (<ab); | |
494 | free (symtab.base); | |
495 | symtab = ltab; | |
496 | ||
497 | /* now go through all core symbols and set is_static accordingly: */ | |
498 | ||
499 | for (i = 0; i < core_num_syms; ++i) | |
500 | { | |
501 | if (core_sym_class (core_syms[i]) == 't') | |
502 | { | |
503 | sym = sym_lookup (&symtab, core_syms[i]->value | |
504 | + core_syms[i]->section->vma); | |
505 | do | |
506 | { | |
507 | sym++->is_static = TRUE; | |
508 | } | |
509 | while (sym->file == sym[-1].file && | |
510 | strcmp (sym->name, sym[-1].name) == 0); | |
03c35bcb KR |
511 | } |
512 | } | |
12516a37 | 513 | |
03c35bcb | 514 | } |