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