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