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