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