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