block.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /* Block-related functions for the GNU debugger, GDB.
  2. Copyright (C) 2003-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 "block.h"
  16. #include "symtab.h"
  17. #include "symfile.h"
  18. #include "gdbsupport/gdb_obstack.h"
  19. #include "cp-support.h"
  20. #include "addrmap.h"
  21. #include "gdbtypes.h"
  22. #include "objfiles.h"
  23. /* This is used by struct block to store namespace-related info for
  24. C++ files, namely using declarations and the current namespace in
  25. scope. */
  26. struct block_namespace_info : public allocate_on_obstack
  27. {
  28. const char *scope = nullptr;
  29. struct using_direct *using_decl = nullptr;
  30. };
  31. static void block_initialize_namespace (struct block *block,
  32. struct obstack *obstack);
  33. /* See block.h. */
  34. struct objfile *
  35. block_objfile (const struct block *block)
  36. {
  37. const struct global_block *global_block;
  38. if (BLOCK_FUNCTION (block) != NULL)
  39. return symbol_objfile (BLOCK_FUNCTION (block));
  40. global_block = (struct global_block *) block_global_block (block);
  41. return global_block->compunit_symtab->objfile ();
  42. }
  43. /* See block. */
  44. struct gdbarch *
  45. block_gdbarch (const struct block *block)
  46. {
  47. if (BLOCK_FUNCTION (block) != NULL)
  48. return symbol_arch (BLOCK_FUNCTION (block));
  49. return block_objfile (block)->arch ();
  50. }
  51. /* See block.h. */
  52. bool
  53. contained_in (const struct block *a, const struct block *b,
  54. bool allow_nested)
  55. {
  56. if (!a || !b)
  57. return false;
  58. do
  59. {
  60. if (a == b)
  61. return true;
  62. /* If A is a function block, then A cannot be contained in B,
  63. except if A was inlined. */
  64. if (!allow_nested && BLOCK_FUNCTION (a) != NULL && !block_inlined_p (a))
  65. return false;
  66. a = BLOCK_SUPERBLOCK (a);
  67. }
  68. while (a != NULL);
  69. return false;
  70. }
  71. /* Return the symbol for the function which contains a specified
  72. lexical block, described by a struct block BL. The return value
  73. will not be an inlined function; the containing function will be
  74. returned instead. */
  75. struct symbol *
  76. block_linkage_function (const struct block *bl)
  77. {
  78. while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl))
  79. && BLOCK_SUPERBLOCK (bl) != NULL)
  80. bl = BLOCK_SUPERBLOCK (bl);
  81. return BLOCK_FUNCTION (bl);
  82. }
  83. /* Return the symbol for the function which contains a specified
  84. block, described by a struct block BL. The return value will be
  85. the closest enclosing function, which might be an inline
  86. function. */
  87. struct symbol *
  88. block_containing_function (const struct block *bl)
  89. {
  90. while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
  91. bl = BLOCK_SUPERBLOCK (bl);
  92. return BLOCK_FUNCTION (bl);
  93. }
  94. /* Return one if BL represents an inlined function. */
  95. int
  96. block_inlined_p (const struct block *bl)
  97. {
  98. return BLOCK_FUNCTION (bl) != NULL && BLOCK_FUNCTION (bl)->is_inlined ();
  99. }
  100. /* A helper function that checks whether PC is in the blockvector BL.
  101. It returns the containing block if there is one, or else NULL. */
  102. static const struct block *
  103. find_block_in_blockvector (const struct blockvector *bl, CORE_ADDR pc)
  104. {
  105. const struct block *b;
  106. int bot, top, half;
  107. /* If we have an addrmap mapping code addresses to blocks, then use
  108. that. */
  109. if (BLOCKVECTOR_MAP (bl))
  110. return (const struct block *) addrmap_find (BLOCKVECTOR_MAP (bl), pc);
  111. /* Otherwise, use binary search to find the last block that starts
  112. before PC.
  113. Note: GLOBAL_BLOCK is block 0, STATIC_BLOCK is block 1.
  114. They both have the same START,END values.
  115. Historically this code would choose STATIC_BLOCK over GLOBAL_BLOCK but the
  116. fact that this choice was made was subtle, now we make it explicit. */
  117. gdb_assert (BLOCKVECTOR_NBLOCKS (bl) >= 2);
  118. bot = STATIC_BLOCK;
  119. top = BLOCKVECTOR_NBLOCKS (bl);
  120. while (top - bot > 1)
  121. {
  122. half = (top - bot + 1) >> 1;
  123. b = BLOCKVECTOR_BLOCK (bl, bot + half);
  124. if (BLOCK_START (b) <= pc)
  125. bot += half;
  126. else
  127. top = bot + half;
  128. }
  129. /* Now search backward for a block that ends after PC. */
  130. while (bot >= STATIC_BLOCK)
  131. {
  132. b = BLOCKVECTOR_BLOCK (bl, bot);
  133. if (!(BLOCK_START (b) <= pc))
  134. return NULL;
  135. if (BLOCK_END (b) > pc)
  136. return b;
  137. bot--;
  138. }
  139. return NULL;
  140. }
  141. /* Return the blockvector immediately containing the innermost lexical
  142. block containing the specified pc value and section, or 0 if there
  143. is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
  144. don't pass this information back to the caller. */
  145. const struct blockvector *
  146. blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
  147. const struct block **pblock,
  148. struct compunit_symtab *cust)
  149. {
  150. const struct blockvector *bl;
  151. const struct block *b;
  152. if (cust == NULL)
  153. {
  154. /* First search all symtabs for one whose file contains our pc */
  155. cust = find_pc_sect_compunit_symtab (pc, section);
  156. if (cust == NULL)
  157. return 0;
  158. }
  159. bl = cust->blockvector ();
  160. /* Then search that symtab for the smallest block that wins. */
  161. b = find_block_in_blockvector (bl, pc);
  162. if (b == NULL)
  163. return NULL;
  164. if (pblock)
  165. *pblock = b;
  166. return bl;
  167. }
  168. /* Return true if the blockvector BV contains PC, false otherwise. */
  169. int
  170. blockvector_contains_pc (const struct blockvector *bv, CORE_ADDR pc)
  171. {
  172. return find_block_in_blockvector (bv, pc) != NULL;
  173. }
  174. /* Return call_site for specified PC in GDBARCH. PC must match exactly, it
  175. must be the next instruction after call (or after tail call jump). Throw
  176. NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
  177. struct call_site *
  178. call_site_for_pc (struct gdbarch *gdbarch, CORE_ADDR pc)
  179. {
  180. struct compunit_symtab *cust;
  181. call_site *cs = nullptr;
  182. /* -1 as tail call PC can be already after the compilation unit range. */
  183. cust = find_pc_compunit_symtab (pc - 1);
  184. if (cust != nullptr)
  185. cs = cust->find_call_site (pc);
  186. if (cs == nullptr)
  187. {
  188. struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (pc);
  189. /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
  190. the call target. */
  191. throw_error (NO_ENTRY_VALUE_ERROR,
  192. _("DW_OP_entry_value resolving cannot find "
  193. "DW_TAG_call_site %s in %s"),
  194. paddress (gdbarch, pc),
  195. (msym.minsym == NULL ? "???"
  196. : msym.minsym->print_name ()));
  197. }
  198. return cs;
  199. }
  200. /* Return the blockvector immediately containing the innermost lexical block
  201. containing the specified pc value, or 0 if there is none.
  202. Backward compatibility, no section. */
  203. const struct blockvector *
  204. blockvector_for_pc (CORE_ADDR pc, const struct block **pblock)
  205. {
  206. return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
  207. pblock, NULL);
  208. }
  209. /* Return the innermost lexical block containing the specified pc value
  210. in the specified section, or 0 if there is none. */
  211. const struct block *
  212. block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
  213. {
  214. const struct blockvector *bl;
  215. const struct block *b;
  216. bl = blockvector_for_pc_sect (pc, section, &b, NULL);
  217. if (bl)
  218. return b;
  219. return 0;
  220. }
  221. /* Return the innermost lexical block containing the specified pc value,
  222. or 0 if there is none. Backward compatibility, no section. */
  223. const struct block *
  224. block_for_pc (CORE_ADDR pc)
  225. {
  226. return block_for_pc_sect (pc, find_pc_mapped_section (pc));
  227. }
  228. /* Now come some functions designed to deal with C++ namespace issues.
  229. The accessors are safe to use even in the non-C++ case. */
  230. /* This returns the namespace that BLOCK is enclosed in, or "" if it
  231. isn't enclosed in a namespace at all. This travels the chain of
  232. superblocks looking for a scope, if necessary. */
  233. const char *
  234. block_scope (const struct block *block)
  235. {
  236. for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
  237. {
  238. if (BLOCK_NAMESPACE (block) != NULL
  239. && BLOCK_NAMESPACE (block)->scope != NULL)
  240. return BLOCK_NAMESPACE (block)->scope;
  241. }
  242. return "";
  243. }
  244. /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
  245. OBSTACK. (It won't make a copy of SCOPE, however, so that already
  246. has to be allocated correctly.) */
  247. void
  248. block_set_scope (struct block *block, const char *scope,
  249. struct obstack *obstack)
  250. {
  251. block_initialize_namespace (block, obstack);
  252. BLOCK_NAMESPACE (block)->scope = scope;
  253. }
  254. /* This returns the using directives list associated with BLOCK, if
  255. any. */
  256. struct using_direct *
  257. block_using (const struct block *block)
  258. {
  259. if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
  260. return NULL;
  261. else
  262. return BLOCK_NAMESPACE (block)->using_decl;
  263. }
  264. /* Set BLOCK's using member to USING; if needed, allocate memory via
  265. OBSTACK. (It won't make a copy of USING, however, so that already
  266. has to be allocated correctly.) */
  267. void
  268. block_set_using (struct block *block,
  269. struct using_direct *using_decl,
  270. struct obstack *obstack)
  271. {
  272. block_initialize_namespace (block, obstack);
  273. BLOCK_NAMESPACE (block)->using_decl = using_decl;
  274. }
  275. /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
  276. initialize its members to zero. */
  277. static void
  278. block_initialize_namespace (struct block *block, struct obstack *obstack)
  279. {
  280. if (BLOCK_NAMESPACE (block) == NULL)
  281. BLOCK_NAMESPACE (block) = new (obstack) struct block_namespace_info ();
  282. }
  283. /* Return the static block associated to BLOCK. Return NULL if block
  284. is NULL or if block is a global block. */
  285. const struct block *
  286. block_static_block (const struct block *block)
  287. {
  288. if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
  289. return NULL;
  290. while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
  291. block = BLOCK_SUPERBLOCK (block);
  292. return block;
  293. }
  294. /* Return the static block associated to BLOCK. Return NULL if block
  295. is NULL. */
  296. const struct block *
  297. block_global_block (const struct block *block)
  298. {
  299. if (block == NULL)
  300. return NULL;
  301. while (BLOCK_SUPERBLOCK (block) != NULL)
  302. block = BLOCK_SUPERBLOCK (block);
  303. return block;
  304. }
  305. /* Allocate a block on OBSTACK, and initialize its elements to
  306. zero/NULL. This is useful for creating "dummy" blocks that don't
  307. correspond to actual source files.
  308. Warning: it sets the block's BLOCK_MULTIDICT to NULL, which isn't a
  309. valid value. If you really don't want the block to have a
  310. dictionary, then you should subsequently set its BLOCK_MULTIDICT to
  311. dict_create_linear (obstack, NULL). */
  312. struct block *
  313. allocate_block (struct obstack *obstack)
  314. {
  315. struct block *bl = OBSTACK_ZALLOC (obstack, struct block);
  316. return bl;
  317. }
  318. /* Allocate a global block. */
  319. struct block *
  320. allocate_global_block (struct obstack *obstack)
  321. {
  322. struct global_block *bl = OBSTACK_ZALLOC (obstack, struct global_block);
  323. return &bl->block;
  324. }
  325. /* Set the compunit of the global block. */
  326. void
  327. set_block_compunit_symtab (struct block *block, struct compunit_symtab *cu)
  328. {
  329. struct global_block *gb;
  330. gdb_assert (BLOCK_SUPERBLOCK (block) == NULL);
  331. gb = (struct global_block *) block;
  332. gdb_assert (gb->compunit_symtab == NULL);
  333. gb->compunit_symtab = cu;
  334. }
  335. /* See block.h. */
  336. struct dynamic_prop *
  337. block_static_link (const struct block *block)
  338. {
  339. struct objfile *objfile = block_objfile (block);
  340. /* Only objfile-owned blocks that materialize top function scopes can have
  341. static links. */
  342. if (objfile == NULL || BLOCK_FUNCTION (block) == NULL)
  343. return NULL;
  344. return (struct dynamic_prop *) objfile_lookup_static_link (objfile, block);
  345. }
  346. /* Return the compunit of the global block. */
  347. static struct compunit_symtab *
  348. get_block_compunit_symtab (const struct block *block)
  349. {
  350. struct global_block *gb;
  351. gdb_assert (BLOCK_SUPERBLOCK (block) == NULL);
  352. gb = (struct global_block *) block;
  353. gdb_assert (gb->compunit_symtab != NULL);
  354. return gb->compunit_symtab;
  355. }
  356. /* Initialize a block iterator, either to iterate over a single block,
  357. or, for static and global blocks, all the included symtabs as
  358. well. */
  359. static void
  360. initialize_block_iterator (const struct block *block,
  361. struct block_iterator *iter)
  362. {
  363. enum block_enum which;
  364. struct compunit_symtab *cu;
  365. iter->idx = -1;
  366. if (BLOCK_SUPERBLOCK (block) == NULL)
  367. {
  368. which = GLOBAL_BLOCK;
  369. cu = get_block_compunit_symtab (block);
  370. }
  371. else if (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL)
  372. {
  373. which = STATIC_BLOCK;
  374. cu = get_block_compunit_symtab (BLOCK_SUPERBLOCK (block));
  375. }
  376. else
  377. {
  378. iter->d.block = block;
  379. /* A signal value meaning that we're iterating over a single
  380. block. */
  381. iter->which = FIRST_LOCAL_BLOCK;
  382. return;
  383. }
  384. /* If this is an included symtab, find the canonical includer and
  385. use it instead. */
  386. while (cu->user != NULL)
  387. cu = cu->user;
  388. /* Putting this check here simplifies the logic of the iterator
  389. functions. If there are no included symtabs, we only need to
  390. search a single block, so we might as well just do that
  391. directly. */
  392. if (cu->includes == NULL)
  393. {
  394. iter->d.block = block;
  395. /* A signal value meaning that we're iterating over a single
  396. block. */
  397. iter->which = FIRST_LOCAL_BLOCK;
  398. }
  399. else
  400. {
  401. iter->d.compunit_symtab = cu;
  402. iter->which = which;
  403. }
  404. }
  405. /* A helper function that finds the current compunit over whose static
  406. or global block we should iterate. */
  407. static struct compunit_symtab *
  408. find_iterator_compunit_symtab (struct block_iterator *iterator)
  409. {
  410. if (iterator->idx == -1)
  411. return iterator->d.compunit_symtab;
  412. return iterator->d.compunit_symtab->includes[iterator->idx];
  413. }
  414. /* Perform a single step for a plain block iterator, iterating across
  415. symbol tables as needed. Returns the next symbol, or NULL when
  416. iteration is complete. */
  417. static struct symbol *
  418. block_iterator_step (struct block_iterator *iterator, int first)
  419. {
  420. struct symbol *sym;
  421. gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
  422. while (1)
  423. {
  424. if (first)
  425. {
  426. struct compunit_symtab *cust
  427. = find_iterator_compunit_symtab (iterator);
  428. const struct block *block;
  429. /* Iteration is complete. */
  430. if (cust == NULL)
  431. return NULL;
  432. block = BLOCKVECTOR_BLOCK (cust->blockvector (),
  433. iterator->which);
  434. sym = mdict_iterator_first (BLOCK_MULTIDICT (block),
  435. &iterator->mdict_iter);
  436. }
  437. else
  438. sym = mdict_iterator_next (&iterator->mdict_iter);
  439. if (sym != NULL)
  440. return sym;
  441. /* We have finished iterating the appropriate block of one
  442. symtab. Now advance to the next symtab and begin iteration
  443. there. */
  444. ++iterator->idx;
  445. first = 1;
  446. }
  447. }
  448. /* See block.h. */
  449. struct symbol *
  450. block_iterator_first (const struct block *block,
  451. struct block_iterator *iterator)
  452. {
  453. initialize_block_iterator (block, iterator);
  454. if (iterator->which == FIRST_LOCAL_BLOCK)
  455. return mdict_iterator_first (block->multidict, &iterator->mdict_iter);
  456. return block_iterator_step (iterator, 1);
  457. }
  458. /* See block.h. */
  459. struct symbol *
  460. block_iterator_next (struct block_iterator *iterator)
  461. {
  462. if (iterator->which == FIRST_LOCAL_BLOCK)
  463. return mdict_iterator_next (&iterator->mdict_iter);
  464. return block_iterator_step (iterator, 0);
  465. }
  466. /* Perform a single step for a "match" block iterator, iterating
  467. across symbol tables as needed. Returns the next symbol, or NULL
  468. when iteration is complete. */
  469. static struct symbol *
  470. block_iter_match_step (struct block_iterator *iterator,
  471. const lookup_name_info &name,
  472. int first)
  473. {
  474. struct symbol *sym;
  475. gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
  476. while (1)
  477. {
  478. if (first)
  479. {
  480. struct compunit_symtab *cust
  481. = find_iterator_compunit_symtab (iterator);
  482. const struct block *block;
  483. /* Iteration is complete. */
  484. if (cust == NULL)
  485. return NULL;
  486. block = BLOCKVECTOR_BLOCK (cust->blockvector (),
  487. iterator->which);
  488. sym = mdict_iter_match_first (BLOCK_MULTIDICT (block), name,
  489. &iterator->mdict_iter);
  490. }
  491. else
  492. sym = mdict_iter_match_next (name, &iterator->mdict_iter);
  493. if (sym != NULL)
  494. return sym;
  495. /* We have finished iterating the appropriate block of one
  496. symtab. Now advance to the next symtab and begin iteration
  497. there. */
  498. ++iterator->idx;
  499. first = 1;
  500. }
  501. }
  502. /* See block.h. */
  503. struct symbol *
  504. block_iter_match_first (const struct block *block,
  505. const lookup_name_info &name,
  506. struct block_iterator *iterator)
  507. {
  508. initialize_block_iterator (block, iterator);
  509. if (iterator->which == FIRST_LOCAL_BLOCK)
  510. return mdict_iter_match_first (block->multidict, name,
  511. &iterator->mdict_iter);
  512. return block_iter_match_step (iterator, name, 1);
  513. }
  514. /* See block.h. */
  515. struct symbol *
  516. block_iter_match_next (const lookup_name_info &name,
  517. struct block_iterator *iterator)
  518. {
  519. if (iterator->which == FIRST_LOCAL_BLOCK)
  520. return mdict_iter_match_next (name, &iterator->mdict_iter);
  521. return block_iter_match_step (iterator, name, 0);
  522. }
  523. /* See block.h. */
  524. bool
  525. best_symbol (struct symbol *a, const domain_enum domain)
  526. {
  527. return (a->domain () == domain
  528. && a->aclass () != LOC_UNRESOLVED);
  529. }
  530. /* See block.h. */
  531. struct symbol *
  532. better_symbol (struct symbol *a, struct symbol *b, const domain_enum domain)
  533. {
  534. if (a == NULL)
  535. return b;
  536. if (b == NULL)
  537. return a;
  538. if (a->domain () == domain && b->domain () != domain)
  539. return a;
  540. if (b->domain () == domain && a->domain () != domain)
  541. return b;
  542. if (a->aclass () != LOC_UNRESOLVED && b->aclass () == LOC_UNRESOLVED)
  543. return a;
  544. if (b->aclass () != LOC_UNRESOLVED && a->aclass () == LOC_UNRESOLVED)
  545. return b;
  546. return a;
  547. }
  548. /* See block.h.
  549. Note that if NAME is the demangled form of a C++ symbol, we will fail
  550. to find a match during the binary search of the non-encoded names, but
  551. for now we don't worry about the slight inefficiency of looking for
  552. a match we'll never find, since it will go pretty quick. Once the
  553. binary search terminates, we drop through and do a straight linear
  554. search on the symbols. Each symbol which is marked as being a ObjC/C++
  555. symbol (language_cplus or language_objc set) has both the encoded and
  556. non-encoded names tested for a match. */
  557. struct symbol *
  558. block_lookup_symbol (const struct block *block, const char *name,
  559. symbol_name_match_type match_type,
  560. const domain_enum domain)
  561. {
  562. struct block_iterator iter;
  563. struct symbol *sym;
  564. lookup_name_info lookup_name (name, match_type);
  565. if (!BLOCK_FUNCTION (block))
  566. {
  567. struct symbol *other = NULL;
  568. ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym)
  569. {
  570. /* See comment related to PR gcc/debug/91507 in
  571. block_lookup_symbol_primary. */
  572. if (best_symbol (sym, domain))
  573. return sym;
  574. /* This is a bit of a hack, but symbol_matches_domain might ignore
  575. STRUCT vs VAR domain symbols. So if a matching symbol is found,
  576. make sure there is no "better" matching symbol, i.e., one with
  577. exactly the same domain. PR 16253. */
  578. if (symbol_matches_domain (sym->language (),
  579. sym->domain (), domain))
  580. other = better_symbol (other, sym, domain);
  581. }
  582. return other;
  583. }
  584. else
  585. {
  586. /* Note that parameter symbols do not always show up last in the
  587. list; this loop makes sure to take anything else other than
  588. parameter symbols first; it only uses parameter symbols as a
  589. last resort. Note that this only takes up extra computation
  590. time on a match.
  591. It's hard to define types in the parameter list (at least in
  592. C/C++) so we don't do the same PR 16253 hack here that is done
  593. for the !BLOCK_FUNCTION case. */
  594. struct symbol *sym_found = NULL;
  595. ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym)
  596. {
  597. if (symbol_matches_domain (sym->language (),
  598. sym->domain (), domain))
  599. {
  600. sym_found = sym;
  601. if (!sym->is_argument ())
  602. {
  603. break;
  604. }
  605. }
  606. }
  607. return (sym_found); /* Will be NULL if not found. */
  608. }
  609. }
  610. /* See block.h. */
  611. struct symbol *
  612. block_lookup_symbol_primary (const struct block *block, const char *name,
  613. const domain_enum domain)
  614. {
  615. struct symbol *sym, *other;
  616. struct mdict_iterator mdict_iter;
  617. lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
  618. /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */
  619. gdb_assert (BLOCK_SUPERBLOCK (block) == NULL
  620. || BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL);
  621. other = NULL;
  622. for (sym
  623. = mdict_iter_match_first (block->multidict, lookup_name, &mdict_iter);
  624. sym != NULL;
  625. sym = mdict_iter_match_next (lookup_name, &mdict_iter))
  626. {
  627. /* With the fix for PR gcc/debug/91507, we get for:
  628. ...
  629. extern char *zzz[];
  630. char *zzz[ ] = {
  631. "abc",
  632. "cde"
  633. };
  634. ...
  635. DWARF which will result in two entries in the symbol table, a decl
  636. with type char *[] and a def with type char *[2].
  637. If we return the decl here, we don't get the value of zzz:
  638. ...
  639. $ gdb a.spec.out -batch -ex "p zzz"
  640. $1 = 0x601030 <zzz>
  641. ...
  642. because we're returning the symbol without location information, and
  643. because the fallback that uses the address from the minimal symbols
  644. doesn't work either because the type of the decl does not specify a
  645. size.
  646. To fix this, we prefer def over decl in best_symbol and
  647. better_symbol.
  648. In absence of the gcc fix, both def and decl have type char *[], so
  649. the only option to make this work is improve the fallback to use the
  650. size of the minimal symbol. Filed as PR exp/24989. */
  651. if (best_symbol (sym, domain))
  652. return sym;
  653. /* This is a bit of a hack, but symbol_matches_domain might ignore
  654. STRUCT vs VAR domain symbols. So if a matching symbol is found,
  655. make sure there is no "better" matching symbol, i.e., one with
  656. exactly the same domain. PR 16253. */
  657. if (symbol_matches_domain (sym->language (), sym->domain (), domain))
  658. other = better_symbol (other, sym, domain);
  659. }
  660. return other;
  661. }
  662. /* See block.h. */
  663. struct symbol *
  664. block_find_symbol (const struct block *block, const char *name,
  665. const domain_enum domain,
  666. block_symbol_matcher_ftype *matcher, void *data)
  667. {
  668. struct block_iterator iter;
  669. struct symbol *sym;
  670. lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
  671. /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */
  672. gdb_assert (BLOCK_SUPERBLOCK (block) == NULL
  673. || BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL);
  674. ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym)
  675. {
  676. /* MATCHER is deliberately called second here so that it never sees
  677. a non-domain-matching symbol. */
  678. if (symbol_matches_domain (sym->language (), sym->domain (), domain)
  679. && matcher (sym, data))
  680. return sym;
  681. }
  682. return NULL;
  683. }
  684. /* See block.h. */
  685. int
  686. block_find_non_opaque_type (struct symbol *sym, void *data)
  687. {
  688. return !TYPE_IS_OPAQUE (sym->type ());
  689. }
  690. /* See block.h. */
  691. int
  692. block_find_non_opaque_type_preferred (struct symbol *sym, void *data)
  693. {
  694. struct symbol **best = (struct symbol **) data;
  695. if (!TYPE_IS_OPAQUE (sym->type ()))
  696. return 1;
  697. *best = sym;
  698. return 0;
  699. }
  700. /* See block.h. */
  701. struct blockranges *
  702. make_blockranges (struct objfile *objfile,
  703. const std::vector<blockrange> &rangevec)
  704. {
  705. struct blockranges *blr;
  706. size_t n = rangevec.size();
  707. blr = (struct blockranges *)
  708. obstack_alloc (&objfile->objfile_obstack,
  709. sizeof (struct blockranges)
  710. + (n - 1) * sizeof (struct blockrange));
  711. blr->nranges = n;
  712. for (int i = 0; i < n; i++)
  713. blr->range[i] = rangevec[i];
  714. return blr;
  715. }