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