buildsym.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. /* Support routines for building symbol tables in GDB's internal format.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  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, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "buildsym-legacy.h"
  16. #include "bfd.h"
  17. #include "gdbsupport/gdb_obstack.h"
  18. #include "symtab.h"
  19. #include "symfile.h"
  20. #include "objfiles.h"
  21. #include "gdbtypes.h"
  22. #include "complaints.h"
  23. #include "expression.h" /* For "enum exp_opcode" used by... */
  24. #include "filenames.h" /* For DOSish file names. */
  25. #include "macrotab.h"
  26. #include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */
  27. #include "block.h"
  28. #include "cp-support.h"
  29. #include "dictionary.h"
  30. #include "addrmap.h"
  31. #include <algorithm>
  32. /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
  33. questionable--see comment where we call them). */
  34. #include "stabsread.h"
  35. /* List of blocks already made (lexical contexts already closed).
  36. This is used at the end to make the blockvector. */
  37. struct pending_block
  38. {
  39. struct pending_block *next;
  40. struct block *block;
  41. };
  42. /* Initial sizes of data structures. These are realloc'd larger if
  43. needed, and realloc'd down to the size actually used, when
  44. completed. */
  45. #define INITIAL_LINE_VECTOR_LENGTH 1000
  46. buildsym_compunit::buildsym_compunit (struct objfile *objfile_,
  47. const char *name,
  48. const char *comp_dir_,
  49. enum language language_,
  50. CORE_ADDR last_addr)
  51. : m_objfile (objfile_),
  52. m_last_source_file (name == nullptr ? nullptr : xstrdup (name)),
  53. m_comp_dir (comp_dir_ == nullptr ? nullptr : xstrdup (comp_dir_)),
  54. m_language (language_),
  55. m_last_source_start_addr (last_addr)
  56. {
  57. /* Allocate the compunit symtab now. The caller needs it to allocate
  58. non-primary symtabs. It is also needed by get_macro_table. */
  59. m_compunit_symtab = allocate_compunit_symtab (m_objfile, name);
  60. /* Build the subfile for NAME (the main source file) so that we can record
  61. a pointer to it for later.
  62. IMPORTANT: Do not allocate a struct symtab for NAME here.
  63. It can happen that the debug info provides a different path to NAME than
  64. DIRNAME,NAME. We cope with this in watch_main_source_file_lossage but
  65. that only works if the main_subfile doesn't have a symtab yet. */
  66. start_subfile (name);
  67. /* Save this so that we don't have to go looking for it at the end
  68. of the subfiles list. */
  69. m_main_subfile = m_current_subfile;
  70. }
  71. buildsym_compunit::~buildsym_compunit ()
  72. {
  73. struct subfile *subfile, *nextsub;
  74. if (m_pending_macros != nullptr)
  75. free_macro_table (m_pending_macros);
  76. for (subfile = m_subfiles;
  77. subfile != NULL;
  78. subfile = nextsub)
  79. {
  80. nextsub = subfile->next;
  81. xfree (subfile->name);
  82. xfree (subfile->line_vector);
  83. xfree (subfile);
  84. }
  85. struct pending *next, *next1;
  86. for (next = m_file_symbols; next != NULL; next = next1)
  87. {
  88. next1 = next->next;
  89. xfree ((void *) next);
  90. }
  91. for (next = m_global_symbols; next != NULL; next = next1)
  92. {
  93. next1 = next->next;
  94. xfree ((void *) next);
  95. }
  96. }
  97. struct macro_table *
  98. buildsym_compunit::get_macro_table ()
  99. {
  100. if (m_pending_macros == nullptr)
  101. m_pending_macros = new_macro_table (&m_objfile->per_bfd->storage_obstack,
  102. &m_objfile->per_bfd->string_cache,
  103. m_compunit_symtab);
  104. return m_pending_macros;
  105. }
  106. /* Maintain the lists of symbols and blocks. */
  107. /* Add a symbol to one of the lists of symbols. */
  108. void
  109. add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
  110. {
  111. struct pending *link;
  112. /* If this is an alias for another symbol, don't add it. */
  113. if (symbol->linkage_name () && symbol->linkage_name ()[0] == '#')
  114. return;
  115. /* We keep PENDINGSIZE symbols in each link of the list. If we
  116. don't have a link with room in it, add a new link. */
  117. if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
  118. {
  119. link = XNEW (struct pending);
  120. link->next = *listhead;
  121. *listhead = link;
  122. link->nsyms = 0;
  123. }
  124. (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
  125. }
  126. /* Find a symbol named NAME on a LIST. NAME need not be
  127. '\0'-terminated; LENGTH is the length of the name. */
  128. struct symbol *
  129. find_symbol_in_list (struct pending *list, char *name, int length)
  130. {
  131. int j;
  132. const char *pp;
  133. while (list != NULL)
  134. {
  135. for (j = list->nsyms; --j >= 0;)
  136. {
  137. pp = list->symbol[j]->linkage_name ();
  138. if (*pp == *name && strncmp (pp, name, length) == 0
  139. && pp[length] == '\0')
  140. {
  141. return (list->symbol[j]);
  142. }
  143. }
  144. list = list->next;
  145. }
  146. return (NULL);
  147. }
  148. /* Record BLOCK on the list of all blocks in the file. Put it after
  149. OPBLOCK, or at the beginning if opblock is NULL. This puts the
  150. block in the list after all its subblocks. */
  151. void
  152. buildsym_compunit::record_pending_block (struct block *block,
  153. struct pending_block *opblock)
  154. {
  155. struct pending_block *pblock;
  156. pblock = XOBNEW (&m_pending_block_obstack, struct pending_block);
  157. pblock->block = block;
  158. if (opblock)
  159. {
  160. pblock->next = opblock->next;
  161. opblock->next = pblock;
  162. }
  163. else
  164. {
  165. pblock->next = m_pending_blocks;
  166. m_pending_blocks = pblock;
  167. }
  168. }
  169. /* Take one of the lists of symbols and make a block from it. Keep
  170. the order the symbols have in the list (reversed from the input
  171. file). Put the block on the list of pending blocks. */
  172. struct block *
  173. buildsym_compunit::finish_block_internal
  174. (struct symbol *symbol,
  175. struct pending **listhead,
  176. struct pending_block *old_blocks,
  177. const struct dynamic_prop *static_link,
  178. CORE_ADDR start, CORE_ADDR end,
  179. int is_global, int expandable)
  180. {
  181. struct gdbarch *gdbarch = m_objfile->arch ();
  182. struct pending *next, *next1;
  183. struct block *block;
  184. struct pending_block *pblock;
  185. struct pending_block *opblock;
  186. block = (is_global
  187. ? allocate_global_block (&m_objfile->objfile_obstack)
  188. : allocate_block (&m_objfile->objfile_obstack));
  189. if (symbol)
  190. {
  191. BLOCK_MULTIDICT (block)
  192. = mdict_create_linear (&m_objfile->objfile_obstack, *listhead);
  193. }
  194. else
  195. {
  196. if (expandable)
  197. {
  198. BLOCK_MULTIDICT (block) = mdict_create_hashed_expandable (m_language);
  199. mdict_add_pending (BLOCK_MULTIDICT (block), *listhead);
  200. }
  201. else
  202. {
  203. BLOCK_MULTIDICT (block) =
  204. mdict_create_hashed (&m_objfile->objfile_obstack, *listhead);
  205. }
  206. }
  207. BLOCK_START (block) = start;
  208. BLOCK_END (block) = end;
  209. /* Put the block in as the value of the symbol that names it. */
  210. if (symbol)
  211. {
  212. struct type *ftype = symbol->type ();
  213. struct mdict_iterator miter;
  214. SYMBOL_BLOCK_VALUE (symbol) = block;
  215. BLOCK_FUNCTION (block) = symbol;
  216. if (ftype->num_fields () <= 0)
  217. {
  218. /* No parameter type information is recorded with the
  219. function's type. Set that from the type of the
  220. parameter symbols. */
  221. int nparams = 0, iparams;
  222. struct symbol *sym;
  223. /* Here we want to directly access the dictionary, because
  224. we haven't fully initialized the block yet. */
  225. ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block), miter, sym)
  226. {
  227. if (sym->is_argument ())
  228. nparams++;
  229. }
  230. if (nparams > 0)
  231. {
  232. ftype->set_num_fields (nparams);
  233. ftype->set_fields
  234. ((struct field *)
  235. TYPE_ALLOC (ftype, nparams * sizeof (struct field)));
  236. iparams = 0;
  237. /* Here we want to directly access the dictionary, because
  238. we haven't fully initialized the block yet. */
  239. ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block), miter, sym)
  240. {
  241. if (iparams == nparams)
  242. break;
  243. if (sym->is_argument ())
  244. {
  245. ftype->field (iparams).set_type (sym->type ());
  246. TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
  247. iparams++;
  248. }
  249. }
  250. }
  251. }
  252. }
  253. else
  254. {
  255. BLOCK_FUNCTION (block) = NULL;
  256. }
  257. if (static_link != NULL)
  258. objfile_register_static_link (m_objfile, block, static_link);
  259. /* Now free the links of the list, and empty the list. */
  260. for (next = *listhead; next; next = next1)
  261. {
  262. next1 = next->next;
  263. xfree (next);
  264. }
  265. *listhead = NULL;
  266. /* Check to be sure that the blocks have an end address that is
  267. greater than starting address. */
  268. if (BLOCK_END (block) < BLOCK_START (block))
  269. {
  270. if (symbol)
  271. {
  272. complaint (_("block end address less than block "
  273. "start address in %s (patched it)"),
  274. symbol->print_name ());
  275. }
  276. else
  277. {
  278. complaint (_("block end address %s less than block "
  279. "start address %s (patched it)"),
  280. paddress (gdbarch, BLOCK_END (block)),
  281. paddress (gdbarch, BLOCK_START (block)));
  282. }
  283. /* Better than nothing. */
  284. BLOCK_END (block) = BLOCK_START (block);
  285. }
  286. /* Install this block as the superblock of all blocks made since the
  287. start of this scope that don't have superblocks yet. */
  288. opblock = NULL;
  289. for (pblock = m_pending_blocks;
  290. pblock && pblock != old_blocks;
  291. pblock = pblock->next)
  292. {
  293. if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
  294. {
  295. /* Check to be sure the blocks are nested as we receive
  296. them. If the compiler/assembler/linker work, this just
  297. burns a small amount of time.
  298. Skip blocks which correspond to a function; they're not
  299. physically nested inside this other blocks, only
  300. lexically nested. */
  301. if (BLOCK_FUNCTION (pblock->block) == NULL
  302. && (BLOCK_START (pblock->block) < BLOCK_START (block)
  303. || BLOCK_END (pblock->block) > BLOCK_END (block)))
  304. {
  305. if (symbol)
  306. {
  307. complaint (_("inner block not inside outer block in %s"),
  308. symbol->print_name ());
  309. }
  310. else
  311. {
  312. complaint (_("inner block (%s-%s) not "
  313. "inside outer block (%s-%s)"),
  314. paddress (gdbarch, BLOCK_START (pblock->block)),
  315. paddress (gdbarch, BLOCK_END (pblock->block)),
  316. paddress (gdbarch, BLOCK_START (block)),
  317. paddress (gdbarch, BLOCK_END (block)));
  318. }
  319. if (BLOCK_START (pblock->block) < BLOCK_START (block))
  320. BLOCK_START (pblock->block) = BLOCK_START (block);
  321. if (BLOCK_END (pblock->block) > BLOCK_END (block))
  322. BLOCK_END (pblock->block) = BLOCK_END (block);
  323. }
  324. BLOCK_SUPERBLOCK (pblock->block) = block;
  325. }
  326. opblock = pblock;
  327. }
  328. block_set_using (block,
  329. (is_global
  330. ? m_global_using_directives
  331. : m_local_using_directives),
  332. &m_objfile->objfile_obstack);
  333. if (is_global)
  334. m_global_using_directives = NULL;
  335. else
  336. m_local_using_directives = NULL;
  337. record_pending_block (block, opblock);
  338. return block;
  339. }
  340. struct block *
  341. buildsym_compunit::finish_block (struct symbol *symbol,
  342. struct pending_block *old_blocks,
  343. const struct dynamic_prop *static_link,
  344. CORE_ADDR start, CORE_ADDR end)
  345. {
  346. return finish_block_internal (symbol, &m_local_symbols,
  347. old_blocks, static_link, start, end, 0, 0);
  348. }
  349. /* Record that the range of addresses from START to END_INCLUSIVE
  350. (inclusive, like it says) belongs to BLOCK. BLOCK's start and end
  351. addresses must be set already. You must apply this function to all
  352. BLOCK's children before applying it to BLOCK.
  353. If a call to this function complicates the picture beyond that
  354. already provided by BLOCK_START and BLOCK_END, then we create an
  355. address map for the block. */
  356. void
  357. buildsym_compunit::record_block_range (struct block *block,
  358. CORE_ADDR start,
  359. CORE_ADDR end_inclusive)
  360. {
  361. /* If this is any different from the range recorded in the block's
  362. own BLOCK_START and BLOCK_END, then note that the address map has
  363. become interesting. Note that even if this block doesn't have
  364. any "interesting" ranges, some later block might, so we still
  365. need to record this block in the addrmap. */
  366. if (start != BLOCK_START (block)
  367. || end_inclusive + 1 != BLOCK_END (block))
  368. m_pending_addrmap_interesting = true;
  369. if (m_pending_addrmap == nullptr)
  370. m_pending_addrmap = addrmap_create_mutable (&m_pending_addrmap_obstack);
  371. addrmap_set_empty (m_pending_addrmap, start, end_inclusive, block);
  372. }
  373. struct blockvector *
  374. buildsym_compunit::make_blockvector ()
  375. {
  376. struct pending_block *next;
  377. struct blockvector *blockvector;
  378. int i;
  379. /* Count the length of the list of blocks. */
  380. for (next = m_pending_blocks, i = 0; next; next = next->next, i++)
  381. {
  382. }
  383. blockvector = (struct blockvector *)
  384. obstack_alloc (&m_objfile->objfile_obstack,
  385. (sizeof (struct blockvector)
  386. + (i - 1) * sizeof (struct block *)));
  387. /* Copy the blocks into the blockvector. This is done in reverse
  388. order, which happens to put the blocks into the proper order
  389. (ascending starting address). finish_block has hair to insert
  390. each block into the list after its subblocks in order to make
  391. sure this is true. */
  392. BLOCKVECTOR_NBLOCKS (blockvector) = i;
  393. for (next = m_pending_blocks; next; next = next->next)
  394. {
  395. BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
  396. }
  397. free_pending_blocks ();
  398. /* If we needed an address map for this symtab, record it in the
  399. blockvector. */
  400. if (m_pending_addrmap != nullptr && m_pending_addrmap_interesting)
  401. BLOCKVECTOR_MAP (blockvector)
  402. = addrmap_create_fixed (m_pending_addrmap, &m_objfile->objfile_obstack);
  403. else
  404. BLOCKVECTOR_MAP (blockvector) = 0;
  405. /* Some compilers output blocks in the wrong order, but we depend on
  406. their being in the right order so we can binary search. Check the
  407. order and moan about it.
  408. Note: Remember that the first two blocks are the global and static
  409. blocks. We could special case that fact and begin checking at block 2.
  410. To avoid making that assumption we do not. */
  411. if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
  412. {
  413. for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
  414. {
  415. if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
  416. > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
  417. {
  418. CORE_ADDR start
  419. = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));
  420. complaint (_("block at %s out of order"),
  421. hex_string ((LONGEST) start));
  422. }
  423. }
  424. }
  425. return (blockvector);
  426. }
  427. /* Start recording information about source code that came from an
  428. included (or otherwise merged-in) source file with a different
  429. name. NAME is the name of the file (cannot be NULL). */
  430. void
  431. buildsym_compunit::start_subfile (const char *name)
  432. {
  433. const char *subfile_dirname;
  434. struct subfile *subfile;
  435. subfile_dirname = m_comp_dir.get ();
  436. /* See if this subfile is already registered. */
  437. for (subfile = m_subfiles; subfile; subfile = subfile->next)
  438. {
  439. char *subfile_name;
  440. /* If NAME is an absolute path, and this subfile is not, then
  441. attempt to create an absolute path to compare. */
  442. if (IS_ABSOLUTE_PATH (name)
  443. && !IS_ABSOLUTE_PATH (subfile->name)
  444. && subfile_dirname != NULL)
  445. subfile_name = concat (subfile_dirname, SLASH_STRING,
  446. subfile->name, (char *) NULL);
  447. else
  448. subfile_name = subfile->name;
  449. if (FILENAME_CMP (subfile_name, name) == 0)
  450. {
  451. m_current_subfile = subfile;
  452. if (subfile_name != subfile->name)
  453. xfree (subfile_name);
  454. return;
  455. }
  456. if (subfile_name != subfile->name)
  457. xfree (subfile_name);
  458. }
  459. /* This subfile is not known. Add an entry for it. */
  460. subfile = XNEW (struct subfile);
  461. memset (subfile, 0, sizeof (struct subfile));
  462. subfile->next = m_subfiles;
  463. m_subfiles = subfile;
  464. m_current_subfile = subfile;
  465. subfile->name = xstrdup (name);
  466. /* Initialize line-number recording for this subfile. */
  467. subfile->line_vector = NULL;
  468. /* Default the source language to whatever can be deduced from the
  469. filename. If nothing can be deduced (such as for a C/C++ include
  470. file with a ".h" extension), then inherit whatever language the
  471. previous subfile had. This kludgery is necessary because there
  472. is no standard way in some object formats to record the source
  473. language. Also, when symtabs are allocated we try to deduce a
  474. language then as well, but it is too late for us to use that
  475. information while reading symbols, since symtabs aren't allocated
  476. until after all the symbols have been processed for a given
  477. source file. */
  478. subfile->language = deduce_language_from_filename (subfile->name);
  479. if (subfile->language == language_unknown
  480. && subfile->next != NULL)
  481. {
  482. subfile->language = subfile->next->language;
  483. }
  484. /* If the filename of this subfile ends in .C, then change the
  485. language of any pending subfiles from C to C++. We also accept
  486. any other C++ suffixes accepted by deduce_language_from_filename. */
  487. /* Likewise for f2c. */
  488. if (subfile->name)
  489. {
  490. struct subfile *s;
  491. enum language sublang = deduce_language_from_filename (subfile->name);
  492. if (sublang == language_cplus || sublang == language_fortran)
  493. for (s = m_subfiles; s != NULL; s = s->next)
  494. if (s->language == language_c)
  495. s->language = sublang;
  496. }
  497. /* And patch up this file if necessary. */
  498. if (subfile->language == language_c
  499. && subfile->next != NULL
  500. && (subfile->next->language == language_cplus
  501. || subfile->next->language == language_fortran))
  502. {
  503. subfile->language = subfile->next->language;
  504. }
  505. }
  506. /* For stabs readers, the first N_SO symbol is assumed to be the
  507. source file name, and the subfile struct is initialized using that
  508. assumption. If another N_SO symbol is later seen, immediately
  509. following the first one, then the first one is assumed to be the
  510. directory name and the second one is really the source file name.
  511. So we have to patch up the subfile struct by moving the old name
  512. value to dirname and remembering the new name. Some sanity
  513. checking is performed to ensure that the state of the subfile
  514. struct is reasonable and that the old name we are assuming to be a
  515. directory name actually is (by checking for a trailing '/'). */
  516. void
  517. buildsym_compunit::patch_subfile_names (struct subfile *subfile,
  518. const char *name)
  519. {
  520. if (subfile != NULL
  521. && m_comp_dir == NULL
  522. && subfile->name != NULL
  523. && IS_DIR_SEPARATOR (subfile->name[strlen (subfile->name) - 1]))
  524. {
  525. m_comp_dir.reset (subfile->name);
  526. subfile->name = xstrdup (name);
  527. set_last_source_file (name);
  528. /* Default the source language to whatever can be deduced from
  529. the filename. If nothing can be deduced (such as for a C/C++
  530. include file with a ".h" extension), then inherit whatever
  531. language the previous subfile had. This kludgery is
  532. necessary because there is no standard way in some object
  533. formats to record the source language. Also, when symtabs
  534. are allocated we try to deduce a language then as well, but
  535. it is too late for us to use that information while reading
  536. symbols, since symtabs aren't allocated until after all the
  537. symbols have been processed for a given source file. */
  538. subfile->language = deduce_language_from_filename (subfile->name);
  539. if (subfile->language == language_unknown
  540. && subfile->next != NULL)
  541. {
  542. subfile->language = subfile->next->language;
  543. }
  544. }
  545. }
  546. /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
  547. switching source files (different subfiles, as we call them) within
  548. one object file, but using a stack rather than in an arbitrary
  549. order. */
  550. void
  551. buildsym_compunit::push_subfile ()
  552. {
  553. gdb_assert (m_current_subfile != NULL);
  554. gdb_assert (m_current_subfile->name != NULL);
  555. m_subfile_stack.push_back (m_current_subfile->name);
  556. }
  557. const char *
  558. buildsym_compunit::pop_subfile ()
  559. {
  560. gdb_assert (!m_subfile_stack.empty ());
  561. const char *name = m_subfile_stack.back ();
  562. m_subfile_stack.pop_back ();
  563. return name;
  564. }
  565. /* Add a linetable entry for line number LINE and address PC to the
  566. line vector for SUBFILE. */
  567. void
  568. buildsym_compunit::record_line (struct subfile *subfile, int line,
  569. CORE_ADDR pc, linetable_entry_flags flags)
  570. {
  571. struct linetable_entry *e;
  572. /* Make sure line vector exists and is big enough. */
  573. if (!subfile->line_vector)
  574. {
  575. subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
  576. subfile->line_vector = (struct linetable *)
  577. xmalloc (sizeof (struct linetable)
  578. + subfile->line_vector_length * sizeof (struct linetable_entry));
  579. subfile->line_vector->nitems = 0;
  580. m_have_line_numbers = true;
  581. }
  582. if (subfile->line_vector->nitems >= subfile->line_vector_length)
  583. {
  584. subfile->line_vector_length *= 2;
  585. subfile->line_vector = (struct linetable *)
  586. xrealloc ((char *) subfile->line_vector,
  587. (sizeof (struct linetable)
  588. + (subfile->line_vector_length
  589. * sizeof (struct linetable_entry))));
  590. }
  591. /* Normally, we treat lines as unsorted. But the end of sequence
  592. marker is special. We sort line markers at the same PC by line
  593. number, so end of sequence markers (which have line == 0) appear
  594. first. This is right if the marker ends the previous function,
  595. and there is no padding before the next function. But it is
  596. wrong if the previous line was empty and we are now marking a
  597. switch to a different subfile. We must leave the end of sequence
  598. marker at the end of this group of lines, not sort the empty line
  599. to after the marker. The easiest way to accomplish this is to
  600. delete any empty lines from our table, if they are followed by
  601. end of sequence markers. All we lose is the ability to set
  602. breakpoints at some lines which contain no instructions
  603. anyway. */
  604. if (line == 0)
  605. {
  606. struct linetable_entry *last = nullptr;
  607. while (subfile->line_vector->nitems > 0)
  608. {
  609. last = subfile->line_vector->item + subfile->line_vector->nitems - 1;
  610. if (last->pc != pc)
  611. break;
  612. subfile->line_vector->nitems--;
  613. }
  614. /* Ignore an end-of-sequence marker marking an empty sequence. */
  615. if (last == nullptr || last->line == 0)
  616. return;
  617. }
  618. e = subfile->line_vector->item + subfile->line_vector->nitems++;
  619. e->line = line;
  620. e->is_stmt = (flags & LEF_IS_STMT) != 0;
  621. e->pc = pc;
  622. e->prologue_end = (flags & LEF_PROLOGUE_END) != 0;
  623. }
  624. /* Subroutine of end_compunit_symtab to simplify it. Look for a subfile that
  625. matches the main source file's basename. If there is only one, and
  626. if the main source file doesn't have any symbol or line number
  627. information, then copy this file's symtab and line_vector to the
  628. main source file's subfile and discard the other subfile. This can
  629. happen because of a compiler bug or from the user playing games
  630. with #line or from things like a distributed build system that
  631. manipulates the debug info. This can also happen from an innocent
  632. symlink in the paths, we don't canonicalize paths here. */
  633. void
  634. buildsym_compunit::watch_main_source_file_lossage ()
  635. {
  636. struct subfile *mainsub, *subfile;
  637. /* Get the main source file. */
  638. mainsub = m_main_subfile;
  639. /* If the main source file doesn't have any line number or symbol
  640. info, look for an alias in another subfile. */
  641. if (mainsub->line_vector == NULL
  642. && mainsub->symtab == NULL)
  643. {
  644. const char *mainbase = lbasename (mainsub->name);
  645. int nr_matches = 0;
  646. struct subfile *prevsub;
  647. struct subfile *mainsub_alias = NULL;
  648. struct subfile *prev_mainsub_alias = NULL;
  649. prevsub = NULL;
  650. for (subfile = m_subfiles;
  651. subfile != NULL;
  652. subfile = subfile->next)
  653. {
  654. if (subfile == mainsub)
  655. continue;
  656. if (filename_cmp (lbasename (subfile->name), mainbase) == 0)
  657. {
  658. ++nr_matches;
  659. mainsub_alias = subfile;
  660. prev_mainsub_alias = prevsub;
  661. }
  662. prevsub = subfile;
  663. }
  664. if (nr_matches == 1)
  665. {
  666. gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub);
  667. /* Found a match for the main source file.
  668. Copy its line_vector and symtab to the main subfile
  669. and then discard it. */
  670. mainsub->line_vector = mainsub_alias->line_vector;
  671. mainsub->line_vector_length = mainsub_alias->line_vector_length;
  672. mainsub->symtab = mainsub_alias->symtab;
  673. if (prev_mainsub_alias == NULL)
  674. m_subfiles = mainsub_alias->next;
  675. else
  676. prev_mainsub_alias->next = mainsub_alias->next;
  677. xfree (mainsub_alias->name);
  678. xfree (mainsub_alias);
  679. }
  680. }
  681. }
  682. /* Implementation of the first part of end_compunit_symtab. It allows modifying
  683. STATIC_BLOCK before it gets finalized by
  684. end_compunit_symtab_from_static_block. If the returned value is NULL there
  685. is no blockvector created for this symtab (you still must call
  686. end_compunit_symtab_from_static_block).
  687. END_ADDR is the same as for end_compunit_symtab: the address of the end of
  688. the file's text.
  689. If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
  690. expandable.
  691. If REQUIRED is non-zero, then a symtab is created even if it does
  692. not contain any symbols. */
  693. struct block *
  694. buildsym_compunit::end_compunit_symtab_get_static_block (CORE_ADDR end_addr,
  695. int expandable,
  696. int required)
  697. {
  698. /* Finish the lexical context of the last function in the file; pop
  699. the context stack. */
  700. if (!m_context_stack.empty ())
  701. {
  702. struct context_stack cstk = pop_context ();
  703. /* Make a block for the local symbols within. */
  704. finish_block (cstk.name, cstk.old_blocks, NULL,
  705. cstk.start_addr, end_addr);
  706. if (!m_context_stack.empty ())
  707. {
  708. /* This is said to happen with SCO. The old coffread.c
  709. code simply emptied the context stack, so we do the
  710. same. FIXME: Find out why it is happening. This is not
  711. believed to happen in most cases (even for coffread.c);
  712. it used to be an abort(). */
  713. complaint (_("Context stack not empty in end_compunit_symtab"));
  714. m_context_stack.clear ();
  715. }
  716. }
  717. /* Reordered executables may have out of order pending blocks; if
  718. OBJF_REORDERED is true, then sort the pending blocks. */
  719. if ((m_objfile->flags & OBJF_REORDERED) && m_pending_blocks)
  720. {
  721. struct pending_block *pb;
  722. std::vector<block *> barray;
  723. for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
  724. barray.push_back (pb->block);
  725. /* Sort blocks by start address in descending order. Blocks with the
  726. same start address must remain in the original order to preserve
  727. inline function caller/callee relationships. */
  728. std::stable_sort (barray.begin (), barray.end (),
  729. [] (const block *a, const block *b)
  730. {
  731. return BLOCK_START (a) > BLOCK_START (b);
  732. });
  733. int i = 0;
  734. for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
  735. pb->block = barray[i++];
  736. }
  737. /* Cleanup any undefined types that have been left hanging around
  738. (this needs to be done before the finish_blocks so that
  739. file_symbols is still good).
  740. Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
  741. specific, but harmless for other symbol readers, since on gdb
  742. startup or when finished reading stabs, the state is set so these
  743. are no-ops. FIXME: Is this handled right in case of QUIT? Can
  744. we make this cleaner? */
  745. cleanup_undefined_stabs_types (m_objfile);
  746. finish_global_stabs (m_objfile);
  747. if (!required
  748. && m_pending_blocks == NULL
  749. && m_file_symbols == NULL
  750. && m_global_symbols == NULL
  751. && !m_have_line_numbers
  752. && m_pending_macros == NULL
  753. && m_global_using_directives == NULL)
  754. {
  755. /* Ignore symtabs that have no functions with real debugging info. */
  756. return NULL;
  757. }
  758. else
  759. {
  760. /* Define the STATIC_BLOCK. */
  761. return finish_block_internal (NULL, get_file_symbols (), NULL, NULL,
  762. m_last_source_start_addr,
  763. end_addr, 0, expandable);
  764. }
  765. }
  766. /* Subroutine of end_compunit_symtab_from_static_block to simplify it.
  767. Handle the "have blockvector" case.
  768. See end_compunit_symtab_from_static_block for a description of the
  769. arguments. */
  770. struct compunit_symtab *
  771. buildsym_compunit::end_compunit_symtab_with_blockvector
  772. (struct block *static_block, int section, int expandable)
  773. {
  774. struct compunit_symtab *cu = m_compunit_symtab;
  775. struct blockvector *blockvector;
  776. struct subfile *subfile;
  777. CORE_ADDR end_addr;
  778. gdb_assert (static_block != NULL);
  779. gdb_assert (m_subfiles != NULL);
  780. end_addr = BLOCK_END (static_block);
  781. /* Create the GLOBAL_BLOCK and build the blockvector. */
  782. finish_block_internal (NULL, get_global_symbols (), NULL, NULL,
  783. m_last_source_start_addr, end_addr,
  784. 1, expandable);
  785. blockvector = make_blockvector ();
  786. /* Read the line table if it has to be read separately.
  787. This is only used by xcoffread.c. */
  788. if (m_objfile->sf->sym_read_linetable != NULL)
  789. m_objfile->sf->sym_read_linetable (m_objfile);
  790. /* Handle the case where the debug info specifies a different path
  791. for the main source file. It can cause us to lose track of its
  792. line number information. */
  793. watch_main_source_file_lossage ();
  794. /* Now create the symtab objects proper, if not already done,
  795. one for each subfile. */
  796. for (subfile = m_subfiles;
  797. subfile != NULL;
  798. subfile = subfile->next)
  799. {
  800. int linetablesize = 0;
  801. if (subfile->line_vector)
  802. {
  803. linetablesize = sizeof (struct linetable) +
  804. subfile->line_vector->nitems * sizeof (struct linetable_entry);
  805. const auto lte_is_less_than
  806. = [] (const linetable_entry &ln1,
  807. const linetable_entry &ln2) -> bool
  808. {
  809. if (ln1.pc == ln2.pc
  810. && ((ln1.line == 0) != (ln2.line == 0)))
  811. return ln1.line == 0;
  812. return (ln1.pc < ln2.pc);
  813. };
  814. /* Like the pending blocks, the line table may be scrambled in
  815. reordered executables. Sort it if OBJF_REORDERED is true. It
  816. is important to preserve the order of lines at the same
  817. address, as this maintains the inline function caller/callee
  818. relationships, this is why std::stable_sort is used. */
  819. if (m_objfile->flags & OBJF_REORDERED)
  820. std::stable_sort (subfile->line_vector->item,
  821. subfile->line_vector->item
  822. + subfile->line_vector->nitems,
  823. lte_is_less_than);
  824. }
  825. /* Allocate a symbol table if necessary. */
  826. if (subfile->symtab == NULL)
  827. subfile->symtab = allocate_symtab (cu, subfile->name);
  828. struct symtab *symtab = subfile->symtab;
  829. /* Fill in its components. */
  830. if (subfile->line_vector)
  831. {
  832. /* Reallocate the line table on the symbol obstack. */
  833. symtab->set_linetable
  834. ((struct linetable *)
  835. obstack_alloc (&m_objfile->objfile_obstack, linetablesize));
  836. memcpy (symtab->linetable (), subfile->line_vector, linetablesize);
  837. }
  838. else
  839. symtab->set_linetable (nullptr);
  840. /* Use whatever language we have been using for this
  841. subfile, not the one that was deduced in allocate_symtab
  842. from the filename. We already did our own deducing when
  843. we created the subfile, and we may have altered our
  844. opinion of what language it is from things we found in
  845. the symbols. */
  846. symtab->set_language (subfile->language);
  847. }
  848. /* Make sure the filetab of main_subfile is the primary filetab of the CU. */
  849. cu->set_primary_filetab (m_main_subfile->symtab);
  850. /* Fill out the compunit symtab. */
  851. if (m_comp_dir != NULL)
  852. {
  853. /* Reallocate the dirname on the symbol obstack. */
  854. const char *comp_dir = m_comp_dir.get ();
  855. cu->set_dirname (obstack_strdup (&m_objfile->objfile_obstack,
  856. comp_dir));
  857. }
  858. /* Save the debug format string (if any) in the symtab. */
  859. cu->set_debugformat (m_debugformat);
  860. /* Similarly for the producer. */
  861. cu->set_producer (m_producer);
  862. cu->set_blockvector (blockvector);
  863. {
  864. struct block *b = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
  865. set_block_compunit_symtab (b, cu);
  866. }
  867. cu->set_block_line_section (section);
  868. cu->set_macro_table (release_macros ());
  869. /* Default any symbols without a specified symtab to the primary symtab. */
  870. {
  871. int block_i;
  872. /* The main source file's symtab. */
  873. struct symtab *symtab = cu->primary_filetab ();
  874. for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
  875. {
  876. struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
  877. struct symbol *sym;
  878. struct mdict_iterator miter;
  879. /* Inlined functions may have symbols not in the global or
  880. static symbol lists. */
  881. if (BLOCK_FUNCTION (block) != NULL)
  882. if (symbol_symtab (BLOCK_FUNCTION (block)) == NULL)
  883. symbol_set_symtab (BLOCK_FUNCTION (block), symtab);
  884. /* Note that we only want to fix up symbols from the local
  885. blocks, not blocks coming from included symtabs. That is why
  886. we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS. */
  887. ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block), miter, sym)
  888. if (symbol_symtab (sym) == NULL)
  889. symbol_set_symtab (sym, symtab);
  890. }
  891. }
  892. add_compunit_symtab_to_objfile (cu);
  893. return cu;
  894. }
  895. /* Implementation of the second part of end_compunit_symtab. Pass STATIC_BLOCK
  896. as value returned by end_compunit_symtab_get_static_block.
  897. SECTION is the same as for end_compunit_symtab: the section number
  898. (in objfile->section_offsets) of the blockvector and linetable.
  899. If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
  900. expandable. */
  901. struct compunit_symtab *
  902. buildsym_compunit::end_compunit_symtab_from_static_block
  903. (struct block *static_block, int section, int expandable)
  904. {
  905. struct compunit_symtab *cu;
  906. if (static_block == NULL)
  907. {
  908. /* Handle the "no blockvector" case.
  909. When this happens there is nothing to record, so there's nothing
  910. to do: memory will be freed up later.
  911. Note: We won't be adding a compunit to the objfile's list of
  912. compunits, so there's nothing to unchain. However, since each symtab
  913. is added to the objfile's obstack we can't free that space.
  914. We could do better, but this is believed to be a sufficiently rare
  915. event. */
  916. cu = NULL;
  917. }
  918. else
  919. cu = end_compunit_symtab_with_blockvector (static_block, section, expandable);
  920. return cu;
  921. }
  922. /* Finish the symbol definitions for one main source file, close off
  923. all the lexical contexts for that file (creating struct block's for
  924. them), then make the struct symtab for that file and put it in the
  925. list of all such.
  926. END_ADDR is the address of the end of the file's text. SECTION is
  927. the section number (in objfile->section_offsets) of the blockvector
  928. and linetable.
  929. Note that it is possible for end_compunit_symtab() to return NULL. In
  930. particular, for the DWARF case at least, it will return NULL when
  931. it finds a compilation unit that has exactly one DIE, a
  932. TAG_compile_unit DIE. This can happen when we link in an object
  933. file that was compiled from an empty source file. Returning NULL
  934. is probably not the correct thing to do, because then gdb will
  935. never know about this empty file (FIXME).
  936. If you need to modify STATIC_BLOCK before it is finalized you should
  937. call end_compunit_symtab_get_static_block and
  938. end_compunit_symtab_from_static_block yourself. */
  939. struct compunit_symtab *
  940. buildsym_compunit::end_compunit_symtab (CORE_ADDR end_addr, int section)
  941. {
  942. struct block *static_block;
  943. static_block = end_compunit_symtab_get_static_block (end_addr, 0, 0);
  944. return end_compunit_symtab_from_static_block (static_block, section, 0);
  945. }
  946. /* Same as end_compunit_symtab except create a symtab that can be later added
  947. to. */
  948. struct compunit_symtab *
  949. buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr, int section)
  950. {
  951. struct block *static_block;
  952. static_block = end_compunit_symtab_get_static_block (end_addr, 1, 0);
  953. return end_compunit_symtab_from_static_block (static_block, section, 1);
  954. }
  955. /* Subroutine of augment_type_symtab to simplify it.
  956. Attach the main source file's symtab to all symbols in PENDING_LIST that
  957. don't have one. */
  958. static void
  959. set_missing_symtab (struct pending *pending_list,
  960. struct compunit_symtab *cu)
  961. {
  962. struct pending *pending;
  963. int i;
  964. for (pending = pending_list; pending != NULL; pending = pending->next)
  965. {
  966. for (i = 0; i < pending->nsyms; ++i)
  967. {
  968. if (symbol_symtab (pending->symbol[i]) == NULL)
  969. symbol_set_symtab (pending->symbol[i], cu->primary_filetab ());
  970. }
  971. }
  972. }
  973. /* Same as end_compunit_symtab, but for the case where we're adding more symbols
  974. to an existing symtab that is known to contain only type information.
  975. This is the case for DWARF4 Type Units. */
  976. void
  977. buildsym_compunit::augment_type_symtab ()
  978. {
  979. struct compunit_symtab *cust = m_compunit_symtab;
  980. const struct blockvector *blockvector = cust->blockvector ();
  981. if (!m_context_stack.empty ())
  982. complaint (_("Context stack not empty in augment_type_symtab"));
  983. if (m_pending_blocks != NULL)
  984. complaint (_("Blocks in a type symtab"));
  985. if (m_pending_macros != NULL)
  986. complaint (_("Macro in a type symtab"));
  987. if (m_have_line_numbers)
  988. complaint (_("Line numbers recorded in a type symtab"));
  989. if (m_file_symbols != NULL)
  990. {
  991. struct block *block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
  992. /* First mark any symbols without a specified symtab as belonging
  993. to the primary symtab. */
  994. set_missing_symtab (m_file_symbols, cust);
  995. mdict_add_pending (BLOCK_MULTIDICT (block), m_file_symbols);
  996. }
  997. if (m_global_symbols != NULL)
  998. {
  999. struct block *block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
  1000. /* First mark any symbols without a specified symtab as belonging
  1001. to the primary symtab. */
  1002. set_missing_symtab (m_global_symbols, cust);
  1003. mdict_add_pending (BLOCK_MULTIDICT (block),
  1004. m_global_symbols);
  1005. }
  1006. }
  1007. /* Push a context block. Args are an identifying nesting level
  1008. (checkable when you pop it), and the starting PC address of this
  1009. context. */
  1010. struct context_stack *
  1011. buildsym_compunit::push_context (int desc, CORE_ADDR valu)
  1012. {
  1013. m_context_stack.emplace_back ();
  1014. struct context_stack *newobj = &m_context_stack.back ();
  1015. newobj->depth = desc;
  1016. newobj->locals = m_local_symbols;
  1017. newobj->old_blocks = m_pending_blocks;
  1018. newobj->start_addr = valu;
  1019. newobj->local_using_directives = m_local_using_directives;
  1020. newobj->name = NULL;
  1021. m_local_symbols = NULL;
  1022. m_local_using_directives = NULL;
  1023. return newobj;
  1024. }
  1025. /* Pop a context block. Returns the address of the context block just
  1026. popped. */
  1027. struct context_stack
  1028. buildsym_compunit::pop_context ()
  1029. {
  1030. gdb_assert (!m_context_stack.empty ());
  1031. struct context_stack result = m_context_stack.back ();
  1032. m_context_stack.pop_back ();
  1033. return result;
  1034. }