ran "indent -gnu"; have not fixed block comment style
[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
13 void
14 DEFUN (core_init, (a_out_name), const char *a_out_name)
15 {
16   core_bfd = bfd_openr (a_out_name, 0);
17
18   if (!core_bfd)
19     {
20       perror (a_out_name);
21       done (1);
22     }                           /* if */
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);
28     }                           /* if */
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);
40         }                       /* if */
41     }                           /* if */
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);
52     }                           /* if */
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);
61     }                           /* if */
62 }                               /* core_init */
63
64
65 /*
66  * Read in the text space of an a.out file
67  */
68 void
69 DEFUN (core_get_text_space, (core_bfd), bfd * core_bfd)
70 {
71   core_text_space = (PTR) malloc (core_text_sect->_raw_size);
72
73   if (!core_text_space)
74     {
75       fprintf (stderr, "%s: ran out room for %ld bytes of text space\n",
76                whoami, core_text_sect->_raw_size);
77       done (1);
78     }                           /* if */
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;
85     }                           /* if */
86   if (!core_text_space)
87     {
88       fprintf (stderr, "%s: can't do -c\n", whoami);
89     }                           /* if */
90 }                               /* core_get_text_space */
91
92
93 /*
94  * Return class of symbol SYM.  The returned class can be any of:
95  *      0   -> symbol is not interesting to us
96  *      'T' -> symbol is a global name
97  *      't' -> symbol is a local (static) name
98  */
99 static int
100 DEFUN (core_sym_class, (sym), asymbol * sym)
101 {
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;
114     }                           /* if */
115
116   if (ignore_static_funcs && (sym->flags & BSF_LOCAL))
117     {
118       DBG (AOUTDEBUG, printf ("[core_sym_class] %s: not a function\n",
119                               sym->name));
120       return 0;
121     }                           /* if */
122
123   bfd_get_symbol_info (core_bfd, sym, &syminfo);
124   i = syminfo.type;
125
126   if (i == 'T')
127     {
128       return i;                 /* it's a global symbol */
129     }                           /* if */
130
131   if (i != 't')
132     {
133       /* not a static text symbol */
134       DBG (AOUTDEBUG, printf ("[core_sym_class] %s is of class %c\n",
135                               sym->name, i));
136       return 0;
137     }                           /* if */
138
139   /* do some more filtering on static function-names: */
140
141   if (ignore_static_funcs)
142     {
143       return 0;
144     }                           /* if */
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')
150     {
151       return 0;
152     }                           /* if */
153
154   for (name = sym->name; *name; ++name)
155     {
156       if (*name == '.' || *name == '$')
157         {
158           return 0;
159         }                       /* if */
160     }                           /* if */
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;
181     }                           /* if */
182   return 't';                   /* it's a static text symbol */
183 }                               /* core_sym_class */
184
185
186 /*
187  * Get whatever source info we can get regarding address ADDR:
188  */
189 static bool
190 DEFUN (get_src_info, (addr, filename, name, line_num),
191        bfd_vma addr AND const char **filename AND const char **name
192        AND int *line_num)
193 {
194   const char *fname = 0, *func_name = 0;
195   int l = 0;
196
197   if (bfd_find_nearest_line (core_bfd, core_text_sect, core_syms,
198                              addr - core_text_sect->vma,
199                              &fname, &func_name, &l)
200       && fname && func_name && l)
201     {
202       DBG (AOUTDEBUG, printf ("[get_src_info] 0x%lx -> %s:%d (%s)\n",
203                               addr, fname, l, func_name));
204       *filename = fname;
205       *name = func_name;
206       *line_num = l;
207       return TRUE;
208     }
209   else
210     {
211       DBG (AOUTDEBUG, printf ("[get_src_info] no info for 0x%lx (%s:%d,%s)\n",
212                               (long) addr, fname ? fname : "<unknown>", l,
213                               func_name ? func_name : "<unknown>"));
214       return FALSE;
215     }                           /* if */
216 }                               /* get_src_info */
217
218
219 /*
220  * Read in symbol table from core.  One symbol per function is
221  * entered.
222  */
223 void
224 DEFUN (core_create_function_syms, (core_bfd), bfd * core_bfd)
225 {
226   bfd_vma min_vma = ~0, max_vma = 0;
227   const char *filename, *func_name;
228   int class;
229   long i;
230
231   /* pass 1 - determine upper bound on number of function names: */
232   symtab.len = 0;
233   for (i = 0; i < core_num_syms; ++i)
234     {
235       if (!core_sym_class (core_syms[i]))
236         {
237           continue;
238         }                       /* if */
239       ++symtab.len;
240     }                           /* for */
241
242   if (symtab.len == 0)
243     {
244       fprintf (stderr, "%s: file `%s' has no symbols\n", whoami, a_out_name);
245       done (1);
246     }                           /* if */
247
248   /* the "+ 2" is for the sentinels: */
249   symtab.base = (Sym *) xmalloc ((symtab.len + 2) * sizeof (Sym));
250
251   /* pass 2 - create symbols: */
252
253   symtab.limit = symtab.base;
254   for (i = 0; i < core_num_syms; ++i)
255     {
256       class = core_sym_class (core_syms[i]);
257       if (!class)
258         {
259           DBG (AOUTDEBUG,
260                printf ("[core_create_function_syms] rejecting: 0x%lx %s\n",
261                        core_syms[i]->value, core_syms[i]->name));
262           continue;
263         }                       /* if */
264
265       sym_init (symtab.limit);
266
267       /* symbol offsets are always section-relative: */
268
269       symtab.limit->addr = core_syms[i]->value + core_syms[i]->section->vma;
270       symtab.limit->name = core_syms[i]->name;
271
272 #ifdef __osf__
273       /*
274        * Suppress symbols that are not function names.  This is
275        * useful to suppress code-labels and aliases.
276        *
277        * This is known to be useful under DEC's OSF/1.  Under SunOS 4.x,
278        * labels do not appear in the symbol table info, so this isn't
279        * necessary.
280        */
281       if (get_src_info (symtab.limit->addr, &filename, &func_name,
282                         &symtab.limit->line_num))
283         {
284           symtab.limit->file = source_file_lookup_path (filename);
285
286           if (strcmp (symtab.limit->name, func_name) != 0)
287             {
288               /*
289                * The symbol's address maps to a different name, so
290                * it can't be a function-entry point.  This happens
291                * for labels, for example.
292                */
293               DBG (AOUTDEBUG,
294                 printf ("[core_create_function_syms: rej %s (maps to %s)\n",
295                         symtab.limit->name, func_name));
296               continue;
297             }                   /* if */
298         }                       /* if */
299 #endif
300
301       symtab.limit->is_func = TRUE;
302       symtab.limit->is_bb_head = TRUE;
303       if (class == 't')
304         {
305           symtab.limit->is_static = TRUE;
306         }                       /* if */
307
308       min_vma = MIN (symtab.limit->addr, min_vma);
309       max_vma = MAX (symtab.limit->addr, max_vma);
310
311       /*
312        * If we see "main" without an initial '_', we assume names
313        * are *not* prefixed by '_'.
314        */
315       if (symtab.limit->name[0] == 'm' && discard_underscores
316           && strcmp (symtab.limit->name, "main") == 0)
317         {
318           discard_underscores = 0;
319         }                       /* if */
320
321       DBG (AOUTDEBUG, printf ("[core_create_function_syms] %ld %s 0x%lx\n",
322                               (long) (symtab.limit - symtab.base),
323                               symtab.limit->name, symtab.limit->addr));
324       ++symtab.limit;
325     }                           /* for */
326
327   /* create sentinels: */
328
329   sym_init (symtab.limit);
330   symtab.limit->name = "<locore>";
331   symtab.limit->addr = 0;
332   symtab.limit->end_addr = min_vma - 1;
333   ++symtab.limit;
334
335   sym_init (symtab.limit);
336   symtab.limit->name = "<hicore>";
337   symtab.limit->addr = max_vma + 1;
338   symtab.limit->end_addr = ~0;
339   ++symtab.limit;
340
341   symtab.len = symtab.limit - symtab.base;
342   symtab_finalize (&symtab);
343 }                               /* core_create_function_syms */
344
345
346 /*
347  * Read in symbol table from core.  One symbol per line of source code
348  * is entered.
349  */
350 void
351 DEFUN (core_create_line_syms, (core_bfd), bfd * core_bfd)
352 {
353   char prev_name[PATH_MAX], prev_filename[PATH_MAX];
354   bfd_vma vma, min_vma = ~0, max_vma = 0;
355   bfd_vma offset, prev_offset, min_dist;
356   Sym *prev, dummy, *sentinel, *sym;
357   const char *filename;
358   int prev_line_num, i;
359   Sym_Table ltab;
360   /*
361    * Create symbols for functions as usual.  This is necessary in
362    * cases where parts of a program were not compiled with -g.  For
363    * those parts we still want to get info at the function level:
364    */
365   core_create_function_syms (core_bfd);
366
367   /* pass 1 - counter number of symbols: */
368
369   /*
370    * To find all line information, walk through all possible
371    * text-space addresses (one by one!) and get the debugging
372    * info for each address.  When the debugging info changes,
373    * it is time to create a new symbol.
374    *
375    * Of course, this is rather slow and it would be better if
376    * bfd would provide an iterator for enumerating all line
377    * infos, but for now, we try to speed up the second pass
378    * by determining what the minimum code distance between two
379    * lines is.
380    */
381   prev_name[0] = '\0';
382   ltab.len = 0;
383   min_dist = core_text_sect->_raw_size;
384   prev_offset = -min_dist;
385   prev_filename[0] = '\0';
386   prev_line_num = 0;
387   for (offset = 0; offset < core_text_sect->_raw_size; ++offset)
388     {
389       vma = core_text_sect->vma + offset;
390       if (!get_src_info (vma, &filename, &dummy.name, &dummy.line_num)
391           || (prev_line_num == dummy.line_num &&
392               strcmp (prev_name, dummy.name) == 0
393               && strcmp (prev_filename, filename) == 0))
394         {
395           continue;
396         }                       /* if */
397
398       ++ltab.len;
399       prev_line_num = dummy.line_num;
400       strcpy (prev_name, dummy.name);
401       strcpy (prev_filename, filename);
402
403       if (offset - prev_offset < min_dist)
404         {
405           min_dist = offset - prev_offset;
406         }                       /* if */
407       prev_offset = offset;
408
409       min_vma = MIN (vma, min_vma);
410       max_vma = MAX (vma, max_vma);
411     }                           /* for */
412
413   DBG (AOUTDEBUG, printf ("[core_create_line_syms] min_dist=%lx\n", min_dist));
414
415   /* make room for function symbols, too: */
416   ltab.len += symtab.len;
417   ltab.base = (Sym *) xmalloc (ltab.len * sizeof (Sym));
418   ltab.limit = ltab.base;
419
420   /* pass 2 - create symbols: */
421
422   prev = 0;
423   for (offset = 0; offset < core_text_sect->_raw_size; offset += min_dist)
424     {
425       sym_init (ltab.limit);
426       if (!get_src_info (core_text_sect->vma + offset, &filename,
427                          &ltab.limit->name, &ltab.limit->line_num)
428           || (prev && prev->line_num == ltab.limit->line_num
429               && strcmp (prev->name, ltab.limit->name) == 0
430               && strcmp (prev->file->name, filename) == 0))
431         {
432           continue;
433         }                       /* if */
434
435       /* make name pointer a malloc'ed string: */
436       ltab.limit->name = strdup (ltab.limit->name);
437       ltab.limit->file = source_file_lookup_path (filename);
438
439       ltab.limit->addr = core_text_sect->vma + offset;
440       prev = ltab.limit;
441
442       /*
443        * If we see "main" without an initial '_', we assume names
444        * are *not* prefixed by '_'.
445        */
446       if (ltab.limit->name[0] == 'm' && discard_underscores
447           && strcmp (ltab.limit->name, "main") == 0)
448         {
449           discard_underscores = 0;
450         }                       /* if */
451
452       DBG (AOUTDEBUG, printf ("[core_create_line_syms] %d %s 0x%lx\n",
453                               ltab.len, ltab.limit->name,
454                               ltab.limit->addr));
455       ++ltab.limit;
456     }                           /* for */
457
458   /* update sentinels: */
459
460   sentinel = sym_lookup (&symtab, 0);
461   if (strcmp (sentinel->name, "<locore>") == 0
462       && min_vma <= sentinel->end_addr)
463     {
464       sentinel->end_addr = min_vma - 1;
465     }                           /* if */
466
467   sentinel = sym_lookup (&symtab, ~0);
468   if (strcmp (sentinel->name, "<hicore>") == 0 && max_vma >= sentinel->addr)
469     {
470       sentinel->addr = max_vma + 1;
471     }                           /* if */
472
473   /* copy in function symbols: */
474   memcpy (ltab.limit, symtab.base, symtab.len * sizeof (Sym));
475   ltab.limit += symtab.len;
476
477   if (ltab.limit - ltab.base != ltab.len)
478     {
479       fprintf (stderr,
480                "%s: somebody miscounted: ltab.len=%ld instead of %d\n",
481                whoami, (long) (ltab.limit - ltab.base), ltab.len);
482       done (1);
483     }                           /* if */
484
485   /* finalize ltab and make it symbol table: */
486
487   symtab_finalize (&ltab);
488   free (symtab.base);
489   symtab = ltab;
490
491   /* now go through all core symbols and set is_static accordingly: */
492
493   for (i = 0; i < core_num_syms; ++i)
494     {
495       if (core_sym_class (core_syms[i]) == 't')
496         {
497           sym = sym_lookup (&symtab, core_syms[i]->value
498                             + core_syms[i]->section->vma);
499           do
500             {
501               sym++->is_static = TRUE;
502             }
503           while (sym->file == sym[-1].file &&
504                  strcmp (sym->name, sym[-1].name) == 0);
505         }                       /* if */
506     }                           /* for */
507
508 }                               /* core_create_line_syms */
509
510 /*** end of core.c ***/