* config-ml.in (multi-do): LDFLAGS must include multilib
[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
0f579087
ILT
6#ifndef MIN_INSN_SIZE
7/* If not defined in MACHINE_H, assume smallest instruction is 1 byte
8 long. THis is safe but may be needlessly slow on machines where
9 all instructions are longer. */
10#define MIN_INSN_SIZE 1
11#endif
12
5489fcc3
KR
13bfd *core_bfd;
14int core_num_syms;
15asymbol **core_syms;
16asection *core_text_sect;
17PTR core_text_space;
18
64c50fc5
JL
19/* For mapping symbols to specific .o files during file ordering. */
20struct function_map {
21 char *function_name;
22 char *file_name;
23};
24
25struct function_map *symbol_map;
26int symbol_map_count;
27
28static void
29DEFUN (read_function_mappings, (filename), const char *filename)
30{
31 FILE *file = fopen (filename, "r");
32 char dummy[1024];
33 int count = 0;
34
35 if (!file)
36 {
37 fprintf (stderr, "%s: could not open %s.\n", whoami, filename);
38 done (1);
39 }
40
41 /* First parse the mapping file so we know how big we need to
42 make our tables. We also do some sanity checks at this
43 time. */
44 while (!feof (file))
45 {
46 int matches;
47
48 matches = fscanf (file, "%[^\n:]", dummy);
49 if (!matches)
50 {
51 fprintf (stderr, "%s: unable to parse mapping file %s.\n",
52 whoami, filename);
53 done (1);
54 }
55
56 /* Just skip messages about files with no symbols. */
57 if (!strncmp (dummy, "No symbols in ", 14))
58 {
59 fscanf (file, "\n");
60 continue;
61 }
62
63 /* Don't care what else is on this line at this point. */
64 fscanf (file, "%[^\n]\n", dummy);
65 count++;
66 }
67
68 /* Now we know how big we need to make our table. */
1c34a108
ILT
69 symbol_map = ((struct function_map *)
70 xmalloc (count * sizeof (struct function_map)));
64c50fc5
JL
71
72 /* Rewind the input file so we can read it again. */
73 rewind (file);
74
75 /* Read each entry and put it into the table. */
76 count = 0;
77 while (!feof (file))
78 {
79 int matches;
80 char *tmp;
81
82 matches = fscanf (file, "%[^\n:]", dummy);
83 if (!matches)
84 {
85 fprintf (stderr, "%s: unable to parse mapping file %s.\n",
86 whoami, filename);
87 done (1);
88 }
89
90 /* Just skip messages about files with no symbols. */
91 if (!strncmp (dummy, "No symbols in ", 14))
92 {
93 fscanf (file, "\n");
94 continue;
95 }
96
97 /* dummy has the filename, go ahead and copy it. */
98 symbol_map[count].file_name = xmalloc (strlen (dummy) + 1);
99 strcpy (symbol_map[count].file_name, dummy);
100
101 /* Now we need the function name. */
102 fscanf (file, "%[^\n]\n", dummy);
103 tmp = strrchr (dummy, ' ') + 1;
104 symbol_map[count].function_name = xmalloc (strlen (tmp) + 1);
105 strcpy (symbol_map[count].function_name, tmp);
106 count++;
107 }
108
109 /* Record the size of the map table for future reference. */
110 symbol_map_count = count;
111}
5489fcc3
KR
112
113void
12516a37 114DEFUN (core_init, (a_out_name), const char *a_out_name)
5489fcc3 115{
12516a37
KR
116 core_bfd = bfd_openr (a_out_name, 0);
117
118 if (!core_bfd)
119 {
120 perror (a_out_name);
121 done (1);
03c35bcb 122 }
12516a37
KR
123
124 if (!bfd_check_format (core_bfd, bfd_object))
125 {
126 fprintf (stderr, "%s: %s: not in a.out format\n", whoami, a_out_name);
127 done (1);
03c35bcb 128 }
12516a37
KR
129
130 /* get core's text section: */
131 core_text_sect = bfd_get_section_by_name (core_bfd, ".text");
132 if (!core_text_sect)
133 {
134 core_text_sect = bfd_get_section_by_name (core_bfd, "$CODE$");
135 if (!core_text_sect)
136 {
137 fprintf (stderr, "%s: can't find .text section in %s\n",
138 whoami, a_out_name);
139 done (1);
03c35bcb
KR
140 }
141 }
12516a37
KR
142
143 /* read core's symbol table: */
144
145 /* this will probably give us more than we need, but that's ok: */
146 core_num_syms = bfd_get_symtab_upper_bound (core_bfd);
147 if (core_num_syms < 0)
148 {
149 fprintf (stderr, "%s: %s: %s\n", whoami, a_out_name,
150 bfd_errmsg (bfd_get_error ()));
151 done (1);
03c35bcb 152 }
12516a37
KR
153
154 core_syms = (asymbol **) xmalloc (core_num_syms);
155 core_num_syms = bfd_canonicalize_symtab (core_bfd, core_syms);
156 if (core_num_syms < 0)
157 {
158 fprintf (stderr, "%s: %s: %s\n", whoami, a_out_name,
159 bfd_errmsg (bfd_get_error ()));
160 done (1);
03c35bcb 161 }
64c50fc5
JL
162
163 if (function_mapping_file)
164 read_function_mappings (function_mapping_file);
03c35bcb 165}
5489fcc3
KR
166
167
168/*
169 * Read in the text space of an a.out file
170 */
171void
12516a37 172DEFUN (core_get_text_space, (core_bfd), bfd * core_bfd)
5489fcc3 173{
12516a37
KR
174 core_text_space = (PTR) malloc (core_text_sect->_raw_size);
175
176 if (!core_text_space)
5489fcc3 177 {
12516a37
KR
178 fprintf (stderr, "%s: ran out room for %ld bytes of text space\n",
179 whoami, core_text_sect->_raw_size);
180 done (1);
03c35bcb 181 }
12516a37
KR
182 if (!bfd_get_section_contents (core_bfd, core_text_sect, core_text_space,
183 0, core_text_sect->_raw_size))
184 {
185 bfd_perror ("bfd_get_section_contents");
186 free (core_text_space);
187 core_text_space = 0;
03c35bcb 188 }
12516a37
KR
189 if (!core_text_space)
190 {
191 fprintf (stderr, "%s: can't do -c\n", whoami);
03c35bcb
KR
192 }
193}
5489fcc3
KR
194
195
196/*
197 * Return class of symbol SYM. The returned class can be any of:
12516a37
KR
198 * 0 -> symbol is not interesting to us
199 * 'T' -> symbol is a global name
200 * 't' -> symbol is a local (static) name
5489fcc3
KR
201 */
202static int
12516a37 203DEFUN (core_sym_class, (sym), asymbol * sym)
5489fcc3 204{
12516a37
KR
205 symbol_info syminfo;
206 const char *name;
207 char sym_prefix;
208 int i;
209
0f579087 210 if (sym->section == NULL || (sym->flags & BSF_DEBUGGING) != 0)
12516a37
KR
211 {
212 return 0;
03c35bcb 213 }
12516a37 214
0f579087
ILT
215 /*
216 * Must be a text symbol, and static text symbols don't qualify if
217 * ignore_static_funcs set.
218 */
12516a37
KR
219 if (ignore_static_funcs && (sym->flags & BSF_LOCAL))
220 {
221 DBG (AOUTDEBUG, printf ("[core_sym_class] %s: not a function\n",
5489fcc3 222 sym->name));
12516a37 223 return 0;
03c35bcb 224 }
5489fcc3 225
12516a37
KR
226 bfd_get_symbol_info (core_bfd, sym, &syminfo);
227 i = syminfo.type;
5489fcc3 228
12516a37
KR
229 if (i == 'T')
230 {
231 return i; /* it's a global symbol */
03c35bcb 232 }
5489fcc3 233
a3da1edc
ILT
234 if (i == 'W')
235 {
236 /* Treat weak symbols as text symbols. FIXME: a weak symbol may
237 also be a data symbol. */
238 return 'T';
239 }
240
12516a37
KR
241 if (i != 't')
242 {
243 /* not a static text symbol */
244 DBG (AOUTDEBUG, printf ("[core_sym_class] %s is of class %c\n",
5489fcc3 245 sym->name, i));
12516a37 246 return 0;
03c35bcb 247 }
12516a37
KR
248
249 /* do some more filtering on static function-names: */
250
251 if (ignore_static_funcs)
252 {
253 return 0;
03c35bcb 254 }
12516a37
KR
255 /*
256 * Can't zero-length name or funny characters in name, where
257 * `funny' includes: `.' (.o file names) and `$' (Pascal labels).
258 */
259 if (!sym->name || sym->name[0] == '\0')
5489fcc3 260 {
12516a37 261 return 0;
03c35bcb 262 }
12516a37
KR
263
264 for (name = sym->name; *name; ++name)
265 {
266 if (*name == '.' || *name == '$')
267 {
268 return 0;
03c35bcb
KR
269 }
270 }
12516a37
KR
271 /*
272 * On systems where the C compiler adds an underscore to all
273 * names, static names without underscores seem usually to be
274 * labels in hand written assembler in the library. We don't want
275 * these names. This is certainly necessary on a Sparc running
276 * SunOS 4.1 (try profiling a program that does a lot of
277 * division). I don't know whether it has harmful side effects on
278 * other systems. Perhaps it should be made configurable.
279 */
280 sym_prefix = bfd_get_symbol_leading_char (core_bfd);
df928c8f 281 if ((sym_prefix && sym_prefix != sym->name[0])
12516a37
KR
282 /*
283 * GCC may add special symbols to help gdb figure out the file
284 * language. We want to ignore these, since sometimes they mask
285 * the real function. (dj@ctron)
286 */
287 || !strncmp (sym->name, "__gnu_compiled", 14)
288 || !strncmp (sym->name, "___gnu_compiled", 15))
289 {
290 return 0;
03c35bcb 291 }
32843f94
JL
292
293 /* If the object file supports marking of function symbols, then we can
294 zap anything that doesn't have BSF_FUNCTION set. */
295 if (ignore_non_functions && (sym->flags & BSF_FUNCTION) == 0)
296 return 0;
297
12516a37 298 return 't'; /* it's a static text symbol */
03c35bcb 299}
5489fcc3
KR
300
301
302/*
303 * Get whatever source info we can get regarding address ADDR:
304 */
305static bool
12516a37
KR
306DEFUN (get_src_info, (addr, filename, name, line_num),
307 bfd_vma addr AND const char **filename AND const char **name
308 AND int *line_num)
5489fcc3 309{
12516a37
KR
310 const char *fname = 0, *func_name = 0;
311 int l = 0;
5489fcc3 312
12516a37
KR
313 if (bfd_find_nearest_line (core_bfd, core_text_sect, core_syms,
314 addr - core_text_sect->vma,
643f17d2 315 &fname, &func_name, (unsigned int *) &l)
12516a37 316 && fname && func_name && l)
5489fcc3 317 {
12516a37 318 DBG (AOUTDEBUG, printf ("[get_src_info] 0x%lx -> %s:%d (%s)\n",
5489fcc3 319 addr, fname, l, func_name));
12516a37
KR
320 *filename = fname;
321 *name = func_name;
322 *line_num = l;
323 return TRUE;
324 }
325 else
326 {
327 DBG (AOUTDEBUG, printf ("[get_src_info] no info for 0x%lx (%s:%d,%s)\n",
5489fcc3
KR
328 (long) addr, fname ? fname : "<unknown>", l,
329 func_name ? func_name : "<unknown>"));
12516a37 330 return FALSE;
03c35bcb
KR
331 }
332}
5489fcc3
KR
333
334
335/*
336 * Read in symbol table from core. One symbol per function is
337 * entered.
338 */
339void
12516a37 340DEFUN (core_create_function_syms, (core_bfd), bfd * core_bfd)
5489fcc3 341{
12516a37 342 bfd_vma min_vma = ~0, max_vma = 0;
12516a37 343 int class;
64c50fc5 344 long i, j, found, skip;
12516a37
KR
345
346 /* pass 1 - determine upper bound on number of function names: */
347 symtab.len = 0;
348 for (i = 0; i < core_num_syms; ++i)
349 {
350 if (!core_sym_class (core_syms[i]))
351 {
352 continue;
03c35bcb 353 }
64c50fc5
JL
354
355 /* This should be replaced with a binary search or hashed
356 search. Gross.
357
358 Don't create a symtab entry for a function that has
359 a mapping to a file, unless it's the first function
360 in the file. */
361 skip = 0;
362 for (j = 0; j < symbol_map_count; j++)
363 if (!strcmp (core_syms[i]->name, symbol_map[j].function_name))
364 {
365 if (j > 0 && ! strcmp (symbol_map [j].file_name,
366 symbol_map [j - 1].file_name))
367 skip = 1;
368 break;
369 }
370 if (!skip)
371 ++symtab.len;
03c35bcb 372 }
12516a37
KR
373
374 if (symtab.len == 0)
375 {
376 fprintf (stderr, "%s: file `%s' has no symbols\n", whoami, a_out_name);
377 done (1);
03c35bcb 378 }
12516a37
KR
379
380 /* the "+ 2" is for the sentinels: */
381 symtab.base = (Sym *) xmalloc ((symtab.len + 2) * sizeof (Sym));
382
383 /* pass 2 - create symbols: */
384
385 symtab.limit = symtab.base;
386 for (i = 0; i < core_num_syms; ++i)
387 {
388 class = core_sym_class (core_syms[i]);
389 if (!class)
390 {
391 DBG (AOUTDEBUG,
392 printf ("[core_create_function_syms] rejecting: 0x%lx %s\n",
5489fcc3 393 core_syms[i]->value, core_syms[i]->name));
12516a37 394 continue;
03c35bcb 395 }
64c50fc5
JL
396 /* This should be replaced with a binary search or hashed
397 search. Gross. */
398
399 skip = 0;
400 found = 0;
401 for (j = 0; j < symbol_map_count; j++)
402 if (!strcmp (core_syms[i]->name, symbol_map[j].function_name))
403 {
404 if (j > 0 && ! strcmp (symbol_map [j].file_name,
405 symbol_map [j - 1].file_name))
406 skip = 1;
407 else
408 found = j;
409 break;
410 }
411
412 if (skip)
413 continue;
5489fcc3 414
12516a37 415 sym_init (symtab.limit);
5489fcc3 416
12516a37 417 /* symbol offsets are always section-relative: */
5489fcc3 418
12516a37 419 symtab.limit->addr = core_syms[i]->value + core_syms[i]->section->vma;
64c50fc5
JL
420 if (symbol_map_count
421 && !strcmp (core_syms[i]->name, symbol_map[found].function_name))
422 {
423 symtab.limit->name = symbol_map[found].file_name;
424 symtab.limit->mapped = 1;
425 }
426 else
427 {
428 symtab.limit->name = core_syms[i]->name;
429 symtab.limit->mapped = 0;
430 }
5489fcc3
KR
431
432#ifdef __osf__
12516a37
KR
433 /*
434 * Suppress symbols that are not function names. This is
435 * useful to suppress code-labels and aliases.
436 *
437 * This is known to be useful under DEC's OSF/1. Under SunOS 4.x,
438 * labels do not appear in the symbol table info, so this isn't
439 * necessary.
440 */
df928c8f
ILT
441 {
442 const char *filename, *func_name;
443
444 if (get_src_info (symtab.limit->addr, &filename, &func_name,
445 &symtab.limit->line_num))
446 {
447 symtab.limit->file = source_file_lookup_path (filename);
448
449 if (strcmp (symtab.limit->name, func_name) != 0)
450 {
451 /*
452 * The symbol's address maps to a different name, so
453 * it can't be a function-entry point. This happens
454 * for labels, for example.
455 */
456 DBG (AOUTDEBUG,
457 printf ("[core_create_function_syms: rej %s (maps to %s)\n",
458 symtab.limit->name, func_name));
459 continue;
460 }
461 }
462 }
5489fcc3
KR
463#endif
464
12516a37
KR
465 symtab.limit->is_func = TRUE;
466 symtab.limit->is_bb_head = TRUE;
467 if (class == 't')
468 {
469 symtab.limit->is_static = TRUE;
03c35bcb 470 }
12516a37
KR
471
472 min_vma = MIN (symtab.limit->addr, min_vma);
473 max_vma = MAX (symtab.limit->addr, max_vma);
474
475 /*
476 * If we see "main" without an initial '_', we assume names
477 * are *not* prefixed by '_'.
478 */
479 if (symtab.limit->name[0] == 'm' && discard_underscores
480 && strcmp (symtab.limit->name, "main") == 0)
5489fcc3 481 {
12516a37 482 discard_underscores = 0;
03c35bcb 483 }
5489fcc3 484
12516a37
KR
485 DBG (AOUTDEBUG, printf ("[core_create_function_syms] %ld %s 0x%lx\n",
486 (long) (symtab.limit - symtab.base),
5489fcc3 487 symtab.limit->name, symtab.limit->addr));
12516a37 488 ++symtab.limit;
03c35bcb 489 }
5489fcc3 490
12516a37 491 /* create sentinels: */
5489fcc3 492
12516a37
KR
493 sym_init (symtab.limit);
494 symtab.limit->name = "<locore>";
495 symtab.limit->addr = 0;
496 symtab.limit->end_addr = min_vma - 1;
497 ++symtab.limit;
5489fcc3 498
12516a37
KR
499 sym_init (symtab.limit);
500 symtab.limit->name = "<hicore>";
501 symtab.limit->addr = max_vma + 1;
502 symtab.limit->end_addr = ~0;
503 ++symtab.limit;
5489fcc3 504
12516a37
KR
505 symtab.len = symtab.limit - symtab.base;
506 symtab_finalize (&symtab);
03c35bcb 507}
5489fcc3
KR
508
509
510/*
511 * Read in symbol table from core. One symbol per line of source code
512 * is entered.
513 */
514void
12516a37 515DEFUN (core_create_line_syms, (core_bfd), bfd * core_bfd)
5489fcc3 516{
12516a37
KR
517 char prev_name[PATH_MAX], prev_filename[PATH_MAX];
518 bfd_vma vma, min_vma = ~0, max_vma = 0;
519 bfd_vma offset, prev_offset, min_dist;
520 Sym *prev, dummy, *sentinel, *sym;
521 const char *filename;
522 int prev_line_num, i;
523 Sym_Table ltab;
524 /*
525 * Create symbols for functions as usual. This is necessary in
526 * cases where parts of a program were not compiled with -g. For
527 * those parts we still want to get info at the function level:
528 */
529 core_create_function_syms (core_bfd);
530
531 /* pass 1 - counter number of symbols: */
532
533 /*
534 * To find all line information, walk through all possible
535 * text-space addresses (one by one!) and get the debugging
536 * info for each address. When the debugging info changes,
537 * it is time to create a new symbol.
538 *
539 * Of course, this is rather slow and it would be better if
540 * bfd would provide an iterator for enumerating all line
541 * infos, but for now, we try to speed up the second pass
542 * by determining what the minimum code distance between two
543 * lines is.
544 */
545 prev_name[0] = '\0';
546 ltab.len = 0;
547 min_dist = core_text_sect->_raw_size;
548 prev_offset = -min_dist;
549 prev_filename[0] = '\0';
550 prev_line_num = 0;
0f579087 551 for (offset = 0; offset < core_text_sect->_raw_size; offset += MIN_INSN_SIZE)
12516a37
KR
552 {
553 vma = core_text_sect->vma + offset;
554 if (!get_src_info (vma, &filename, &dummy.name, &dummy.line_num)
555 || (prev_line_num == dummy.line_num &&
556 strcmp (prev_name, dummy.name) == 0
557 && strcmp (prev_filename, filename) == 0))
5489fcc3 558 {
12516a37 559 continue;
03c35bcb 560 }
12516a37
KR
561
562 ++ltab.len;
563 prev_line_num = dummy.line_num;
564 strcpy (prev_name, dummy.name);
565 strcpy (prev_filename, filename);
566
567 if (offset - prev_offset < min_dist)
5489fcc3 568 {
12516a37 569 min_dist = offset - prev_offset;
03c35bcb 570 }
12516a37
KR
571 prev_offset = offset;
572
573 min_vma = MIN (vma, min_vma);
574 max_vma = MAX (vma, max_vma);
03c35bcb 575 }
12516a37
KR
576
577 DBG (AOUTDEBUG, printf ("[core_create_line_syms] min_dist=%lx\n", min_dist));
578
579 /* make room for function symbols, too: */
580 ltab.len += symtab.len;
581 ltab.base = (Sym *) xmalloc (ltab.len * sizeof (Sym));
582 ltab.limit = ltab.base;
583
584 /* pass 2 - create symbols: */
585
586 prev = 0;
587 for (offset = 0; offset < core_text_sect->_raw_size; offset += min_dist)
588 {
589 sym_init (ltab.limit);
590 if (!get_src_info (core_text_sect->vma + offset, &filename,
591 &ltab.limit->name, &ltab.limit->line_num)
592 || (prev && prev->line_num == ltab.limit->line_num
593 && strcmp (prev->name, ltab.limit->name) == 0
594 && strcmp (prev->file->name, filename) == 0))
5489fcc3 595 {
12516a37 596 continue;
03c35bcb 597 }
12516a37
KR
598
599 /* make name pointer a malloc'ed string: */
d75ea6de 600 ltab.limit->name = xstrdup (ltab.limit->name);
12516a37
KR
601 ltab.limit->file = source_file_lookup_path (filename);
602
603 ltab.limit->addr = core_text_sect->vma + offset;
604 prev = ltab.limit;
605
606 /*
607 * If we see "main" without an initial '_', we assume names
608 * are *not* prefixed by '_'.
609 */
610 if (ltab.limit->name[0] == 'm' && discard_underscores
611 && strcmp (ltab.limit->name, "main") == 0)
612 {
613 discard_underscores = 0;
03c35bcb 614 }
5489fcc3 615
12516a37 616 DBG (AOUTDEBUG, printf ("[core_create_line_syms] %d %s 0x%lx\n",
5489fcc3
KR
617 ltab.len, ltab.limit->name,
618 ltab.limit->addr));
12516a37 619 ++ltab.limit;
03c35bcb 620 }
12516a37
KR
621
622 /* update sentinels: */
623
624 sentinel = sym_lookup (&symtab, 0);
625 if (strcmp (sentinel->name, "<locore>") == 0
626 && min_vma <= sentinel->end_addr)
627 {
628 sentinel->end_addr = min_vma - 1;
03c35bcb 629 }
12516a37
KR
630
631 sentinel = sym_lookup (&symtab, ~0);
632 if (strcmp (sentinel->name, "<hicore>") == 0 && max_vma >= sentinel->addr)
633 {
634 sentinel->addr = max_vma + 1;
03c35bcb 635 }
5489fcc3 636
12516a37
KR
637 /* copy in function symbols: */
638 memcpy (ltab.limit, symtab.base, symtab.len * sizeof (Sym));
639 ltab.limit += symtab.len;
5489fcc3 640
12516a37 641 if (ltab.limit - ltab.base != ltab.len)
5489fcc3 642 {
12516a37
KR
643 fprintf (stderr,
644 "%s: somebody miscounted: ltab.len=%ld instead of %d\n",
645 whoami, (long) (ltab.limit - ltab.base), ltab.len);
646 done (1);
03c35bcb 647 }
12516a37
KR
648
649 /* finalize ltab and make it symbol table: */
650
651 symtab_finalize (&ltab);
652 free (symtab.base);
653 symtab = ltab;
654
655 /* now go through all core symbols and set is_static accordingly: */
656
657 for (i = 0; i < core_num_syms; ++i)
658 {
659 if (core_sym_class (core_syms[i]) == 't')
660 {
661 sym = sym_lookup (&symtab, core_syms[i]->value
662 + core_syms[i]->section->vma);
663 do
664 {
665 sym++->is_static = TRUE;
666 }
667 while (sym->file == sym[-1].file &&
668 strcmp (sym->name, sym[-1].name) == 0);
03c35bcb
KR
669 }
670 }
12516a37 671
03c35bcb 672}
This page took 0.126015 seconds and 4 git commands to generate.