* gprof.c (long_options): Add "--function-ordering" and
[platform/upstream/binutils.git] / gprof / core.c
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 /* For mapping symbols to specific .o files during file ordering.  */
13 struct function_map {
14   char *function_name;
15   char *file_name;
16 };
17
18 struct function_map *symbol_map;
19 int symbol_map_count;
20
21 static void
22 DEFUN (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 }
104
105 void
106 DEFUN (core_init, (a_out_name), const char *a_out_name)
107 {
108   core_bfd = bfd_openr (a_out_name, 0);
109
110   if (!core_bfd)
111     {
112       perror (a_out_name);
113       done (1);
114     }
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);
120     }
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);
132         }
133     }
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);
144     }
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);
153     }
154
155   if (function_mapping_file)
156     read_function_mappings (function_mapping_file);
157 }
158
159
160 /*
161  * Read in the text space of an a.out file
162  */
163 void
164 DEFUN (core_get_text_space, (core_bfd), bfd * core_bfd)
165 {
166   core_text_space = (PTR) malloc (core_text_sect->_raw_size);
167
168   if (!core_text_space)
169     {
170       fprintf (stderr, "%s: ran out room for %ld bytes of text space\n",
171                whoami, core_text_sect->_raw_size);
172       done (1);
173     }
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;
180     }
181   if (!core_text_space)
182     {
183       fprintf (stderr, "%s: can't do -c\n", whoami);
184     }
185 }
186
187
188 /*
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
193  */
194 static int
195 DEFUN (core_sym_class, (sym), asymbol * sym)
196 {
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;
209     }
210
211   if (ignore_static_funcs && (sym->flags & BSF_LOCAL))
212     {
213       DBG (AOUTDEBUG, printf ("[core_sym_class] %s: not a function\n",
214                               sym->name));
215       return 0;
216     }
217
218   bfd_get_symbol_info (core_bfd, sym, &syminfo);
219   i = syminfo.type;
220
221   if (i == 'T')
222     {
223       return i;                 /* it's a global symbol */
224     }
225
226   if (i != 't')
227     {
228       /* not a static text symbol */
229       DBG (AOUTDEBUG, printf ("[core_sym_class] %s is of class %c\n",
230                               sym->name, i));
231       return 0;
232     }
233
234   /* do some more filtering on static function-names: */
235
236   if (ignore_static_funcs)
237     {
238       return 0;
239     }
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')
245     {
246       return 0;
247     }
248
249   for (name = sym->name; *name; ++name)
250     {
251       if (*name == '.' || *name == '$')
252         {
253           return 0;
254         }
255     }
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;
276     }
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
283   return 't';                   /* it's a static text symbol */
284 }
285
286
287 /*
288  * Get whatever source info we can get regarding address ADDR:
289  */
290 static bool
291 DEFUN (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)
294 {
295   const char *fname = 0, *func_name = 0;
296   int l = 0;
297
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)
302     {
303       DBG (AOUTDEBUG, printf ("[get_src_info] 0x%lx -> %s:%d (%s)\n",
304                               addr, fname, l, func_name));
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",
313                               (long) addr, fname ? fname : "<unknown>", l,
314                               func_name ? func_name : "<unknown>"));
315       return FALSE;
316     }
317 }
318
319
320 /*
321  * Read in symbol table from core.  One symbol per function is
322  * entered.
323  */
324 void
325 DEFUN (core_create_function_syms, (core_bfd), bfd * core_bfd)
326 {
327   bfd_vma min_vma = ~0, max_vma = 0;
328   const char *filename, *func_name;
329   int class;
330   long i, j, found, skip;
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;
339         }
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;
358     }
359
360   if (symtab.len == 0)
361     {
362       fprintf (stderr, "%s: file `%s' has no symbols\n", whoami, a_out_name);
363       done (1);
364     }
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",
379                        core_syms[i]->value, core_syms[i]->name));
380           continue;
381         }
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;
400
401       sym_init (symtab.limit);
402
403       /* symbol offsets are always section-relative: */
404
405       symtab.limit->addr = core_syms[i]->value + core_syms[i]->section->vma;
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         }
417
418 #ifdef __osf__
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))
429         {
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;
443             }
444         }
445 #endif
446
447       symtab.limit->is_func = TRUE;
448       symtab.limit->is_bb_head = TRUE;
449       if (class == 't')
450         {
451           symtab.limit->is_static = TRUE;
452         }
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)
463         {
464           discard_underscores = 0;
465         }
466
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));
470       ++symtab.limit;
471     }
472
473   /* create sentinels: */
474
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;
480
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;
486
487   symtab.len = symtab.limit - symtab.base;
488   symtab_finalize (&symtab);
489 }
490
491
492 /*
493  * Read in symbol table from core.  One symbol per line of source code
494  * is entered.
495  */
496 void
497 DEFUN (core_create_line_syms, (core_bfd), bfd * core_bfd)
498 {
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))
540         {
541           continue;
542         }
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)
550         {
551           min_dist = offset - prev_offset;
552         }
553       prev_offset = offset;
554
555       min_vma = MIN (vma, min_vma);
556       max_vma = MAX (vma, max_vma);
557     }
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))
577         {
578           continue;
579         }
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;
596         }
597
598       DBG (AOUTDEBUG, printf ("[core_create_line_syms] %d %s 0x%lx\n",
599                               ltab.len, ltab.limit->name,
600                               ltab.limit->addr));
601       ++ltab.limit;
602     }
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;
611     }
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;
617     }
618
619   /* copy in function symbols: */
620   memcpy (ltab.limit, symtab.base, symtab.len * sizeof (Sym));
621   ltab.limit += symtab.len;
622
623   if (ltab.limit - ltab.base != ltab.len)
624     {
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);
629     }
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);
651         }
652     }
653
654 }