Warn if the .pdata section is not a multiple of 20 bytes
[deliverable/binutils-gdb.git] / gprof / core.c
CommitLineData
5489fcc3
KR
1#include "libiberty.h"
2#include "gprof.h"
3#include "core.h"
4#include "symtab.h"
5
6bfd *core_bfd;
7int core_num_syms;
8asymbol **core_syms;
9asection *core_text_sect;
10PTR core_text_space;
11
64c50fc5
JL
12/* For mapping symbols to specific .o files during file ordering. */
13struct function_map {
14 char *function_name;
15 char *file_name;
16};
17
18struct function_map *symbol_map;
19int symbol_map_count;
20
21static void
22DEFUN (read_function_mappings, (filename), const char *filename)
23{
24 FILE *file = fopen (filename, "r");
25 char dummy[1024];
26 int count = 0;
27
28 if (!file)
29 {
30 fprintf (stderr, "%s: could not open %s.\n", whoami, filename);
31 done (1);
32 }
33
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
36 time. */
37 while (!feof (file))
38 {
39 int matches;
40
41 matches = fscanf (file, "%[^\n:]", dummy);
42 if (!matches)
43 {
44 fprintf (stderr, "%s: unable to parse mapping file %s.\n",
45 whoami, filename);
46 done (1);
47 }
48
49 /* Just skip messages about files with no symbols. */
50 if (!strncmp (dummy, "No symbols in ", 14))
51 {
52 fscanf (file, "\n");
53 continue;
54 }
55
56 /* Don't care what else is on this line at this point. */
57 fscanf (file, "%[^\n]\n", dummy);
58 count++;
59 }
60
61 /* Now we know how big we need to make our table. */
62 symbol_map = xmalloc (count * sizeof (struct function_map));
63
64 /* Rewind the input file so we can read it again. */
65 rewind (file);
66
67 /* Read each entry and put it into the table. */
68 count = 0;
69 while (!feof (file))
70 {
71 int matches;
72 char *tmp;
73
74 matches = fscanf (file, "%[^\n:]", dummy);
75 if (!matches)
76 {
77 fprintf (stderr, "%s: unable to parse mapping file %s.\n",
78 whoami, filename);
79 done (1);
80 }
81
82 /* Just skip messages about files with no symbols. */
83 if (!strncmp (dummy, "No symbols in ", 14))
84 {
85 fscanf (file, "\n");
86 continue;
87 }
88
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);
92
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);
98 count++;
99 }
100
101 /* Record the size of the map table for future reference. */
102 symbol_map_count = count;
103}
5489fcc3
KR
104
105void
12516a37 106DEFUN (core_init, (a_out_name), const char *a_out_name)
5489fcc3 107{
12516a37
KR
108 core_bfd = bfd_openr (a_out_name, 0);
109
110 if (!core_bfd)
111 {
112 perror (a_out_name);
113 done (1);
03c35bcb 114 }
12516a37
KR
115
116 if (!bfd_check_format (core_bfd, bfd_object))
117 {
118 fprintf (stderr, "%s: %s: not in a.out format\n", whoami, a_out_name);
119 done (1);
03c35bcb 120 }
12516a37
KR
121
122 /* get core's text section: */
123 core_text_sect = bfd_get_section_by_name (core_bfd, ".text");
124 if (!core_text_sect)
125 {
126 core_text_sect = bfd_get_section_by_name (core_bfd, "$CODE$");
127 if (!core_text_sect)
128 {
129 fprintf (stderr, "%s: can't find .text section in %s\n",
130 whoami, a_out_name);
131 done (1);
03c35bcb
KR
132 }
133 }
12516a37
KR
134
135 /* read core's symbol table: */
136
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)
140 {
141 fprintf (stderr, "%s: %s: %s\n", whoami, a_out_name,
142 bfd_errmsg (bfd_get_error ()));
143 done (1);
03c35bcb 144 }
12516a37
KR
145
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)
149 {
150 fprintf (stderr, "%s: %s: %s\n", whoami, a_out_name,
151 bfd_errmsg (bfd_get_error ()));
152 done (1);
03c35bcb 153 }
64c50fc5
JL
154
155 if (function_mapping_file)
156 read_function_mappings (function_mapping_file);
03c35bcb 157}
5489fcc3
KR
158
159
160/*
161 * Read in the text space of an a.out file
162 */
163void
12516a37 164DEFUN (core_get_text_space, (core_bfd), bfd * core_bfd)
5489fcc3 165{
12516a37
KR
166 core_text_space = (PTR) malloc (core_text_sect->_raw_size);
167
168 if (!core_text_space)
5489fcc3 169 {
12516a37
KR
170 fprintf (stderr, "%s: ran out room for %ld bytes of text space\n",
171 whoami, core_text_sect->_raw_size);
172 done (1);
03c35bcb 173 }
12516a37
KR
174 if (!bfd_get_section_contents (core_bfd, core_text_sect, core_text_space,
175 0, core_text_sect->_raw_size))
176 {
177 bfd_perror ("bfd_get_section_contents");
178 free (core_text_space);
179 core_text_space = 0;
03c35bcb 180 }
12516a37
KR
181 if (!core_text_space)
182 {
183 fprintf (stderr, "%s: can't do -c\n", whoami);
03c35bcb
KR
184 }
185}
5489fcc3
KR
186
187
188/*
189 * Return class of symbol SYM. The returned class can be any of:
12516a37
KR
190 * 0 -> symbol is not interesting to us
191 * 'T' -> symbol is a global name
192 * 't' -> symbol is a local (static) name
5489fcc3
KR
193 */
194static int
12516a37 195DEFUN (core_sym_class, (sym), asymbol * sym)
5489fcc3 196{
12516a37
KR
197 symbol_info syminfo;
198 const char *name;
199 char sym_prefix;
200 int i;
201
202 /*
203 * Must be a text symbol, and static text symbols don't qualify if
204 * ignore_static_funcs set.
205 */
206 if (!sym->section)
207 {
208 return 0;
03c35bcb 209 }
12516a37
KR
210
211 if (ignore_static_funcs && (sym->flags & BSF_LOCAL))
212 {
213 DBG (AOUTDEBUG, printf ("[core_sym_class] %s: not a function\n",
5489fcc3 214 sym->name));
12516a37 215 return 0;
03c35bcb 216 }
5489fcc3 217
12516a37
KR
218 bfd_get_symbol_info (core_bfd, sym, &syminfo);
219 i = syminfo.type;
5489fcc3 220
12516a37
KR
221 if (i == 'T')
222 {
223 return i; /* it's a global symbol */
03c35bcb 224 }
5489fcc3 225
12516a37
KR
226 if (i != 't')
227 {
228 /* not a static text symbol */
229 DBG (AOUTDEBUG, printf ("[core_sym_class] %s is of class %c\n",
5489fcc3 230 sym->name, i));
12516a37 231 return 0;
03c35bcb 232 }
12516a37
KR
233
234 /* do some more filtering on static function-names: */
235
236 if (ignore_static_funcs)
237 {
238 return 0;
03c35bcb 239 }
12516a37
KR
240 /*
241 * Can't zero-length name or funny characters in name, where
242 * `funny' includes: `.' (.o file names) and `$' (Pascal labels).
243 */
244 if (!sym->name || sym->name[0] == '\0')
5489fcc3 245 {
12516a37 246 return 0;
03c35bcb 247 }
12516a37
KR
248
249 for (name = sym->name; *name; ++name)
250 {
251 if (*name == '.' || *name == '$')
252 {
253 return 0;
03c35bcb
KR
254 }
255 }
12516a37
KR
256 /*
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.
264 */
265 sym_prefix = bfd_get_symbol_leading_char (core_bfd);
266 if (sym_prefix && sym_prefix != sym->name[0]
267 /*
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)
271 */
272 || !strncmp (sym->name, "__gnu_compiled", 14)
273 || !strncmp (sym->name, "___gnu_compiled", 15))
274 {
275 return 0;
03c35bcb 276 }
32843f94
JL
277
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)
281 return 0;
282
12516a37 283 return 't'; /* it's a static text symbol */
03c35bcb 284}
5489fcc3
KR
285
286
287/*
288 * Get whatever source info we can get regarding address ADDR:
289 */
290static bool
12516a37
KR
291DEFUN (get_src_info, (addr, filename, name, line_num),
292 bfd_vma addr AND const char **filename AND const char **name
293 AND int *line_num)
5489fcc3 294{
12516a37
KR
295 const char *fname = 0, *func_name = 0;
296 int l = 0;
5489fcc3 297
12516a37
KR
298 if (bfd_find_nearest_line (core_bfd, core_text_sect, core_syms,
299 addr - core_text_sect->vma,
643f17d2 300 &fname, &func_name, (unsigned int *) &l)
12516a37 301 && fname && func_name && l)
5489fcc3 302 {
12516a37 303 DBG (AOUTDEBUG, printf ("[get_src_info] 0x%lx -> %s:%d (%s)\n",
5489fcc3 304 addr, fname, l, func_name));
12516a37
KR
305 *filename = fname;
306 *name = func_name;
307 *line_num = l;
308 return TRUE;
309 }
310 else
311 {
312 DBG (AOUTDEBUG, printf ("[get_src_info] no info for 0x%lx (%s:%d,%s)\n",
5489fcc3
KR
313 (long) addr, fname ? fname : "<unknown>", l,
314 func_name ? func_name : "<unknown>"));
12516a37 315 return FALSE;
03c35bcb
KR
316 }
317}
5489fcc3
KR
318
319
320/*
321 * Read in symbol table from core. One symbol per function is
322 * entered.
323 */
324void
12516a37 325DEFUN (core_create_function_syms, (core_bfd), bfd * core_bfd)
5489fcc3 326{
12516a37
KR
327 bfd_vma min_vma = ~0, max_vma = 0;
328 const char *filename, *func_name;
329 int class;
64c50fc5 330 long i, j, found, skip;
12516a37
KR
331
332 /* pass 1 - determine upper bound on number of function names: */
333 symtab.len = 0;
334 for (i = 0; i < core_num_syms; ++i)
335 {
336 if (!core_sym_class (core_syms[i]))
337 {
338 continue;
03c35bcb 339 }
64c50fc5
JL
340
341 /* This should be replaced with a binary search or hashed
342 search. Gross.
343
344 Don't create a symtab entry for a function that has
345 a mapping to a file, unless it's the first function
346 in the file. */
347 skip = 0;
348 for (j = 0; j < symbol_map_count; j++)
349 if (!strcmp (core_syms[i]->name, symbol_map[j].function_name))
350 {
351 if (j > 0 && ! strcmp (symbol_map [j].file_name,
352 symbol_map [j - 1].file_name))
353 skip = 1;
354 break;
355 }
356 if (!skip)
357 ++symtab.len;
03c35bcb 358 }
12516a37
KR
359
360 if (symtab.len == 0)
361 {
362 fprintf (stderr, "%s: file `%s' has no symbols\n", whoami, a_out_name);
363 done (1);
03c35bcb 364 }
12516a37
KR
365
366 /* the "+ 2" is for the sentinels: */
367 symtab.base = (Sym *) xmalloc ((symtab.len + 2) * sizeof (Sym));
368
369 /* pass 2 - create symbols: */
370
371 symtab.limit = symtab.base;
372 for (i = 0; i < core_num_syms; ++i)
373 {
374 class = core_sym_class (core_syms[i]);
375 if (!class)
376 {
377 DBG (AOUTDEBUG,
378 printf ("[core_create_function_syms] rejecting: 0x%lx %s\n",
5489fcc3 379 core_syms[i]->value, core_syms[i]->name));
12516a37 380 continue;
03c35bcb 381 }
64c50fc5
JL
382 /* This should be replaced with a binary search or hashed
383 search. Gross. */
384
385 skip = 0;
386 found = 0;
387 for (j = 0; j < symbol_map_count; j++)
388 if (!strcmp (core_syms[i]->name, symbol_map[j].function_name))
389 {
390 if (j > 0 && ! strcmp (symbol_map [j].file_name,
391 symbol_map [j - 1].file_name))
392 skip = 1;
393 else
394 found = j;
395 break;
396 }
397
398 if (skip)
399 continue;
5489fcc3 400
12516a37 401 sym_init (symtab.limit);
5489fcc3 402
12516a37 403 /* symbol offsets are always section-relative: */
5489fcc3 404
12516a37 405 symtab.limit->addr = core_syms[i]->value + core_syms[i]->section->vma;
64c50fc5
JL
406 if (symbol_map_count
407 && !strcmp (core_syms[i]->name, symbol_map[found].function_name))
408 {
409 symtab.limit->name = symbol_map[found].file_name;
410 symtab.limit->mapped = 1;
411 }
412 else
413 {
414 symtab.limit->name = core_syms[i]->name;
415 symtab.limit->mapped = 0;
416 }
5489fcc3
KR
417
418#ifdef __osf__
12516a37
KR
419 /*
420 * Suppress symbols that are not function names. This is
421 * useful to suppress code-labels and aliases.
422 *
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
425 * necessary.
426 */
427 if (get_src_info (symtab.limit->addr, &filename, &func_name,
428 &symtab.limit->line_num))
5489fcc3 429 {
12516a37
KR
430 symtab.limit->file = source_file_lookup_path (filename);
431
432 if (strcmp (symtab.limit->name, func_name) != 0)
433 {
434 /*
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.
438 */
439 DBG (AOUTDEBUG,
440 printf ("[core_create_function_syms: rej %s (maps to %s)\n",
441 symtab.limit->name, func_name));
442 continue;
03c35bcb
KR
443 }
444 }
5489fcc3
KR
445#endif
446
12516a37
KR
447 symtab.limit->is_func = TRUE;
448 symtab.limit->is_bb_head = TRUE;
449 if (class == 't')
450 {
451 symtab.limit->is_static = TRUE;
03c35bcb 452 }
12516a37
KR
453
454 min_vma = MIN (symtab.limit->addr, min_vma);
455 max_vma = MAX (symtab.limit->addr, max_vma);
456
457 /*
458 * If we see "main" without an initial '_', we assume names
459 * are *not* prefixed by '_'.
460 */
461 if (symtab.limit->name[0] == 'm' && discard_underscores
462 && strcmp (symtab.limit->name, "main") == 0)
5489fcc3 463 {
12516a37 464 discard_underscores = 0;
03c35bcb 465 }
5489fcc3 466
12516a37
KR
467 DBG (AOUTDEBUG, printf ("[core_create_function_syms] %ld %s 0x%lx\n",
468 (long) (symtab.limit - symtab.base),
5489fcc3 469 symtab.limit->name, symtab.limit->addr));
12516a37 470 ++symtab.limit;
03c35bcb 471 }
5489fcc3 472
12516a37 473 /* create sentinels: */
5489fcc3 474
12516a37
KR
475 sym_init (symtab.limit);
476 symtab.limit->name = "<locore>";
477 symtab.limit->addr = 0;
478 symtab.limit->end_addr = min_vma - 1;
479 ++symtab.limit;
5489fcc3 480
12516a37
KR
481 sym_init (symtab.limit);
482 symtab.limit->name = "<hicore>";
483 symtab.limit->addr = max_vma + 1;
484 symtab.limit->end_addr = ~0;
485 ++symtab.limit;
5489fcc3 486
12516a37
KR
487 symtab.len = symtab.limit - symtab.base;
488 symtab_finalize (&symtab);
03c35bcb 489}
5489fcc3
KR
490
491
492/*
493 * Read in symbol table from core. One symbol per line of source code
494 * is entered.
495 */
496void
12516a37 497DEFUN (core_create_line_syms, (core_bfd), bfd * core_bfd)
5489fcc3 498{
12516a37
KR
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;
505 Sym_Table ltab;
506 /*
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:
510 */
511 core_create_function_syms (core_bfd);
512
513 /* pass 1 - counter number of symbols: */
514
515 /*
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.
520 *
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
525 * lines is.
526 */
527 prev_name[0] = '\0';
528 ltab.len = 0;
529 min_dist = core_text_sect->_raw_size;
530 prev_offset = -min_dist;
531 prev_filename[0] = '\0';
532 prev_line_num = 0;
533 for (offset = 0; offset < core_text_sect->_raw_size; ++offset)
534 {
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))
5489fcc3 540 {
12516a37 541 continue;
03c35bcb 542 }
12516a37
KR
543
544 ++ltab.len;
545 prev_line_num = dummy.line_num;
546 strcpy (prev_name, dummy.name);
547 strcpy (prev_filename, filename);
548
549 if (offset - prev_offset < min_dist)
5489fcc3 550 {
12516a37 551 min_dist = offset - prev_offset;
03c35bcb 552 }
12516a37
KR
553 prev_offset = offset;
554
555 min_vma = MIN (vma, min_vma);
556 max_vma = MAX (vma, max_vma);
03c35bcb 557 }
12516a37
KR
558
559 DBG (AOUTDEBUG, printf ("[core_create_line_syms] min_dist=%lx\n", min_dist));
560
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;
565
566 /* pass 2 - create symbols: */
567
568 prev = 0;
569 for (offset = 0; offset < core_text_sect->_raw_size; offset += min_dist)
570 {
571 sym_init (ltab.limit);
572 if (!get_src_info (core_text_sect->vma + offset, &filename,
573 &ltab.limit->name, &ltab.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))
5489fcc3 577 {
12516a37 578 continue;
03c35bcb 579 }
12516a37
KR
580
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);
584
585 ltab.limit->addr = core_text_sect->vma + offset;
586 prev = ltab.limit;
587
588 /*
589 * If we see "main" without an initial '_', we assume names
590 * are *not* prefixed by '_'.
591 */
592 if (ltab.limit->name[0] == 'm' && discard_underscores
593 && strcmp (ltab.limit->name, "main") == 0)
594 {
595 discard_underscores = 0;
03c35bcb 596 }
5489fcc3 597
12516a37 598 DBG (AOUTDEBUG, printf ("[core_create_line_syms] %d %s 0x%lx\n",
5489fcc3
KR
599 ltab.len, ltab.limit->name,
600 ltab.limit->addr));
12516a37 601 ++ltab.limit;
03c35bcb 602 }
12516a37
KR
603
604 /* update sentinels: */
605
606 sentinel = sym_lookup (&symtab, 0);
607 if (strcmp (sentinel->name, "<locore>") == 0
608 && min_vma <= sentinel->end_addr)
609 {
610 sentinel->end_addr = min_vma - 1;
03c35bcb 611 }
12516a37
KR
612
613 sentinel = sym_lookup (&symtab, ~0);
614 if (strcmp (sentinel->name, "<hicore>") == 0 && max_vma >= sentinel->addr)
615 {
616 sentinel->addr = max_vma + 1;
03c35bcb 617 }
5489fcc3 618
12516a37
KR
619 /* copy in function symbols: */
620 memcpy (ltab.limit, symtab.base, symtab.len * sizeof (Sym));
621 ltab.limit += symtab.len;
5489fcc3 622
12516a37 623 if (ltab.limit - ltab.base != ltab.len)
5489fcc3 624 {
12516a37
KR
625 fprintf (stderr,
626 "%s: somebody miscounted: ltab.len=%ld instead of %d\n",
627 whoami, (long) (ltab.limit - ltab.base), ltab.len);
628 done (1);
03c35bcb 629 }
12516a37
KR
630
631 /* finalize ltab and make it symbol table: */
632
633 symtab_finalize (&ltab);
634 free (symtab.base);
635 symtab = ltab;
636
637 /* now go through all core symbols and set is_static accordingly: */
638
639 for (i = 0; i < core_num_syms; ++i)
640 {
641 if (core_sym_class (core_syms[i]) == 't')
642 {
643 sym = sym_lookup (&symtab, core_syms[i]->value
644 + core_syms[i]->section->vma);
645 do
646 {
647 sym++->is_static = TRUE;
648 }
649 while (sym->file == sym[-1].file &&
650 strcmp (sym->name, sym[-1].name) == 0);
03c35bcb
KR
651 }
652 }
12516a37 653
03c35bcb 654}
This page took 0.121942 seconds and 4 git commands to generate.