corefile.c 25 KB

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