psympriv.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /* Private partial symbol table definitions.
  2. Copyright (C) 2009-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. #ifndef PSYMPRIV_H
  15. #define PSYMPRIV_H
  16. #include "psymtab.h"
  17. #include "objfiles.h"
  18. #include "gdbsupport/gdb_string_view.h"
  19. /* A partial_symbol records the name, domain, and address class of
  20. symbols whose types we have not parsed yet. For functions, it also
  21. contains their memory address, so we can find them from a PC value.
  22. Each partial_symbol sits in a partial_symtab, all of which are chained
  23. on a partial symtab list and which points to the corresponding
  24. normal symtab once the partial_symtab has been referenced. */
  25. /* This structure is space critical. See space comments at the top of
  26. symtab.h. */
  27. struct partial_symbol
  28. {
  29. /* Return the section for this partial symbol, or nullptr if no
  30. section has been set. */
  31. struct obj_section *obj_section (struct objfile *objfile) const
  32. {
  33. return ginfo.obj_section (objfile);
  34. }
  35. /* Return the unrelocated address of this partial symbol. */
  36. CORE_ADDR unrelocated_address () const
  37. {
  38. return ginfo.value.address;
  39. }
  40. /* Return the address of this partial symbol, relocated according to
  41. the offsets provided in OBJFILE. */
  42. CORE_ADDR address (const struct objfile *objfile) const
  43. {
  44. return (ginfo.value.address
  45. + objfile->section_offsets[ginfo.section_index ()]);
  46. }
  47. /* Set the address of this partial symbol. The address must be
  48. unrelocated. */
  49. void set_unrelocated_address (CORE_ADDR addr)
  50. {
  51. ginfo.value.address = addr;
  52. }
  53. /* Note that partial_symbol does not derive from general_symbol_info
  54. due to the bcache. See add_psymbol_to_bcache. */
  55. struct general_symbol_info ginfo;
  56. /* Name space code. */
  57. ENUM_BITFIELD(domain_enum_tag) domain : SYMBOL_DOMAIN_BITS;
  58. /* Address class (for info_symbols). Note that we don't allow
  59. synthetic "aclass" values here at present, simply because there's
  60. no need. */
  61. ENUM_BITFIELD(address_class) aclass : SYMBOL_ACLASS_BITS;
  62. };
  63. /* A convenience enum to give names to some constants used when
  64. searching psymtabs. This is internal to psymtab and should not be
  65. used elsewhere. */
  66. enum psymtab_search_status
  67. {
  68. PST_NOT_SEARCHED,
  69. PST_SEARCHED_AND_FOUND,
  70. PST_SEARCHED_AND_NOT_FOUND
  71. };
  72. /* Specify whether a partial psymbol should be allocated on the global
  73. list or the static list. */
  74. enum class psymbol_placement
  75. {
  76. STATIC,
  77. GLOBAL
  78. };
  79. /* Each source file that has not been fully read in is represented by
  80. a partial_symtab. This contains the information on where in the
  81. executable the debugging symbols for a specific file are, and a
  82. list of names of global symbols which are located in this file.
  83. They are all chained on partial symtab lists.
  84. Even after the source file has been read into a symtab, the
  85. partial_symtab remains around. */
  86. struct partial_symtab
  87. {
  88. /* Allocate a new partial symbol table.
  89. FILENAME (which must be non-NULL) is the filename of this partial
  90. symbol table; it is copied into the appropriate storage. The
  91. partial symtab will also be installed using
  92. psymtab_storage::install. */
  93. partial_symtab (const char *filename,
  94. psymtab_storage *partial_symtabs,
  95. objfile_per_bfd_storage *objfile_per_bfd)
  96. ATTRIBUTE_NONNULL (2) ATTRIBUTE_NONNULL (3);
  97. /* Like the above, but also sets the initial text low and text high
  98. from the ADDR argument, and sets the global- and
  99. static-offsets. */
  100. partial_symtab (const char *filename,
  101. psymtab_storage *partial_symtabs,
  102. objfile_per_bfd_storage *objfile_per_bfd,
  103. CORE_ADDR addr)
  104. ATTRIBUTE_NONNULL (2) ATTRIBUTE_NONNULL (3);
  105. virtual ~partial_symtab ()
  106. {
  107. }
  108. /* Psymtab expansion is done in two steps:
  109. - a call to read_symtab
  110. - while that call is in progress, calls to expand_psymtab can be made,
  111. both for this psymtab, and its dependencies.
  112. This makes a distinction between a toplevel psymtab (for which both
  113. read_symtab and expand_psymtab will be called) and a non-toplevel
  114. psymtab (for which only expand_psymtab will be called). The
  115. distinction can be used f.i. to do things before and after all
  116. dependencies of a top-level psymtab have been expanded.
  117. Read the full symbol table corresponding to this partial symbol
  118. table. Typically calls expand_psymtab. */
  119. virtual void read_symtab (struct objfile *) = 0;
  120. /* Expand the full symbol table for this partial symbol table. Typically
  121. calls expand_dependencies. */
  122. virtual void expand_psymtab (struct objfile *) = 0;
  123. /* Ensure that all the dependencies are read in. Calls
  124. expand_psymtab for each non-shared dependency. */
  125. void expand_dependencies (struct objfile *);
  126. /* Return true if the symtab corresponding to this psymtab has been
  127. read in in the context of this objfile. */
  128. virtual bool readin_p (struct objfile *) const = 0;
  129. /* Return a pointer to the compunit allocated for this source file
  130. in the context of this objfile.
  131. Return nullptr if the compunit was not read in or if there was no
  132. symtab. */
  133. virtual struct compunit_symtab *get_compunit_symtab
  134. (struct objfile *) const = 0;
  135. /* Return the raw low text address of this partial_symtab. */
  136. CORE_ADDR raw_text_low () const
  137. {
  138. return m_text_low;
  139. }
  140. /* Return the raw high text address of this partial_symtab. */
  141. CORE_ADDR raw_text_high () const
  142. {
  143. return m_text_high;
  144. }
  145. /* Return the relocated low text address of this partial_symtab. */
  146. CORE_ADDR text_low (struct objfile *objfile) const
  147. {
  148. return m_text_low + objfile->text_section_offset ();
  149. }
  150. /* Return the relocated high text address of this partial_symtab. */
  151. CORE_ADDR text_high (struct objfile *objfile) const
  152. {
  153. return m_text_high + objfile->text_section_offset ();
  154. }
  155. /* Set the low text address of this partial_symtab. */
  156. void set_text_low (CORE_ADDR addr)
  157. {
  158. m_text_low = addr;
  159. text_low_valid = 1;
  160. }
  161. /* Set the hight text address of this partial_symtab. */
  162. void set_text_high (CORE_ADDR addr)
  163. {
  164. m_text_high = addr;
  165. text_high_valid = 1;
  166. }
  167. /* Return true if this symtab is empty -- meaning that it contains
  168. no symbols. It may still have dependencies. */
  169. bool empty () const
  170. {
  171. return global_psymbols.empty () && static_psymbols.empty ();
  172. }
  173. /* Add a symbol to this partial symbol table of OBJFILE.
  174. If COPY_NAME is true, make a copy of NAME, otherwise use the passed
  175. reference.
  176. THECLASS is the type of symbol.
  177. SECTION is the index of the section of OBJFILE in which the symbol is found.
  178. WHERE determines whether the symbol goes in the list of static or global
  179. partial symbols.
  180. COREADDR is the address of the symbol. For partial symbols that don't have
  181. an address, zero is passed.
  182. LANGUAGE is the language from which the symbol originates. This will
  183. influence, amongst other things, how the symbol name is demangled. */
  184. void add_psymbol (gdb::string_view name,
  185. bool copy_name, domain_enum domain,
  186. enum address_class theclass,
  187. short section,
  188. psymbol_placement where,
  189. CORE_ADDR coreaddr,
  190. enum language language,
  191. psymtab_storage *partial_symtabs,
  192. struct objfile *objfile);
  193. /* Add a symbol to this partial symbol table of OBJFILE. The psymbol
  194. must be fully constructed, and the names must be set and intern'd
  195. as appropriate. */
  196. void add_psymbol (const partial_symbol &psym,
  197. psymbol_placement where,
  198. psymtab_storage *partial_symtabs,
  199. struct objfile *objfile);
  200. /* Indicate that this partial symtab is complete. */
  201. void end ();
  202. /* Chain of all existing partial symtabs. */
  203. struct partial_symtab *next = nullptr;
  204. /* Name of the source file which this partial_symtab defines,
  205. or if the psymtab is anonymous then a descriptive name for
  206. debugging purposes, or "". It must not be NULL. */
  207. const char *filename = nullptr;
  208. /* Full path of the source file. NULL if not known. */
  209. char *fullname = nullptr;
  210. /* Directory in which it was compiled, or NULL if we don't know. */
  211. const char *dirname = nullptr;
  212. /* Range of text addresses covered by this file; texthigh is the
  213. beginning of the next section. Do not use if PSYMTABS_ADDRMAP_SUPPORTED
  214. is set. Do not refer directly to these fields. Instead, use the
  215. accessors. The validity of these fields is determined by the
  216. text_low_valid and text_high_valid fields; these are located later
  217. in this structure for better packing. */
  218. CORE_ADDR m_text_low = 0;
  219. CORE_ADDR m_text_high = 0;
  220. /* If NULL, this is an ordinary partial symbol table.
  221. If non-NULL, this holds a single includer of this partial symbol
  222. table, and this partial symbol table is a shared one.
  223. A shared psymtab is one that is referenced by multiple other
  224. psymtabs, and which conceptually has its contents directly
  225. included in those.
  226. Shared psymtabs have special semantics. When a search finds a
  227. symbol in a shared table, we instead return one of the non-shared
  228. tables that include this one.
  229. A shared psymtabs can be referred to by other shared ones.
  230. The psymtabs that refer to a shared psymtab will list the shared
  231. psymtab in their 'dependencies' array.
  232. In DWARF terms, a shared psymtab is a DW_TAG_partial_unit; but
  233. of course using a name based on that would be too confusing, so
  234. "shared" was chosen instead.
  235. Only a single user is needed because, when expanding a shared
  236. psymtab, we only need to expand its "canonical" non-shared user.
  237. The choice of which one should be canonical is left to the
  238. debuginfo reader; it can be arbitrary. */
  239. struct partial_symtab *user = nullptr;
  240. /* Array of pointers to all of the partial_symtab's which this one
  241. depends on. Since this array can only be set to previous or
  242. the current (?) psymtab, this dependency tree is guaranteed not
  243. to have any loops. "depends on" means that symbols must be read
  244. for the dependencies before being read for this psymtab; this is
  245. for type references in stabs, where if foo.c includes foo.h, declarations
  246. in foo.h may use type numbers defined in foo.c. For other debugging
  247. formats there may be no need to use dependencies. */
  248. struct partial_symtab **dependencies = nullptr;
  249. int number_of_dependencies = 0;
  250. /* Global symbol list. This list will be sorted after readin to
  251. improve access. Binary search will be the usual method of
  252. finding a symbol within it. */
  253. std::vector<partial_symbol *> global_psymbols;
  254. /* Static symbol list. This list will *not* be sorted after readin;
  255. to find a symbol in it, exhaustive search must be used. This is
  256. reasonable because searches through this list will eventually
  257. lead to either the read in of a files symbols for real (assumed
  258. to take a *lot* of time; check) or an error (and we don't care
  259. how long errors take). */
  260. std::vector<partial_symbol *> static_psymbols;
  261. /* True iff objfile->psymtabs_addrmap is properly populated for this
  262. partial_symtab. For discontiguous overlapping psymtabs is the only usable
  263. info in PSYMTABS_ADDRMAP. */
  264. bool psymtabs_addrmap_supported = false;
  265. /* True if the name of this partial symtab is not a source file name. */
  266. bool anonymous = false;
  267. /* A flag that is temporarily used when searching psymtabs. */
  268. ENUM_BITFIELD (psymtab_search_status) searched_flag : 2;
  269. /* Validity of the m_text_low and m_text_high fields. */
  270. unsigned int text_low_valid : 1;
  271. unsigned int text_high_valid : 1;
  272. };
  273. /* A partial symtab that tracks the "readin" and "compunit_symtab"
  274. information in the ordinary way -- by storing it directly in this
  275. object. */
  276. struct standard_psymtab : public partial_symtab
  277. {
  278. standard_psymtab (const char *filename,
  279. psymtab_storage *partial_symtabs,
  280. objfile_per_bfd_storage *objfile_per_bfd)
  281. : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
  282. {
  283. }
  284. standard_psymtab (const char *filename,
  285. psymtab_storage *partial_symtabs,
  286. objfile_per_bfd_storage *objfile_per_bfd,
  287. CORE_ADDR addr)
  288. : partial_symtab (filename, partial_symtabs, objfile_per_bfd, addr)
  289. {
  290. }
  291. bool readin_p (struct objfile *) const override
  292. {
  293. return readin;
  294. }
  295. struct compunit_symtab *get_compunit_symtab (struct objfile *) const override
  296. {
  297. return compunit_symtab;
  298. }
  299. /* True if the symtab corresponding to this psymtab has been
  300. readin. */
  301. bool readin = false;
  302. /* Pointer to compunit eventually allocated for this source file, 0 if
  303. !readin or if we haven't looked for the symtab after it was readin. */
  304. struct compunit_symtab *compunit_symtab = nullptr;
  305. };
  306. /* A partial_symtab that works in the historical db way. This should
  307. not be used in new code, but exists to transition the somewhat
  308. unmaintained "legacy" debug formats. */
  309. struct legacy_psymtab : public standard_psymtab
  310. {
  311. legacy_psymtab (const char *filename,
  312. psymtab_storage *partial_symtabs,
  313. objfile_per_bfd_storage *objfile_per_bfd)
  314. : standard_psymtab (filename, partial_symtabs, objfile_per_bfd)
  315. {
  316. }
  317. legacy_psymtab (const char *filename,
  318. psymtab_storage *partial_symtabs,
  319. objfile_per_bfd_storage *objfile_per_bfd,
  320. CORE_ADDR addr)
  321. : standard_psymtab (filename, partial_symtabs, objfile_per_bfd, addr)
  322. {
  323. }
  324. void read_symtab (struct objfile *objf) override
  325. {
  326. if (legacy_read_symtab)
  327. (*legacy_read_symtab) (this, objf);
  328. }
  329. void expand_psymtab (struct objfile *objf) override
  330. {
  331. (*legacy_expand_psymtab) (this, objf);
  332. }
  333. /* Pointer to function which will read in the symtab corresponding to
  334. this psymtab. */
  335. void (*legacy_read_symtab) (legacy_psymtab *, struct objfile *) = nullptr;
  336. /* Pointer to function which will actually expand this psymtab into
  337. a full symtab. */
  338. void (*legacy_expand_psymtab) (legacy_psymtab *, struct objfile *) = nullptr;
  339. /* Information that lets read_symtab() locate the part of the symbol table
  340. that this psymtab corresponds to. This information is private to the
  341. format-dependent symbol reading routines. For further detail examine
  342. the various symbol reading modules. */
  343. void *read_symtab_private = nullptr;
  344. };
  345. /* Used when recording partial symbol tables. On destruction,
  346. discards any partial symbol tables that have been built. However,
  347. the tables can be kept by calling the "keep" method. */
  348. class psymtab_discarder
  349. {
  350. public:
  351. psymtab_discarder (psymtab_storage *partial_symtabs)
  352. : m_partial_symtabs (partial_symtabs),
  353. m_psymtab (partial_symtabs->psymtabs)
  354. {
  355. }
  356. ~psymtab_discarder ()
  357. {
  358. if (m_partial_symtabs != nullptr)
  359. m_partial_symtabs->discard_psymtabs_to (m_psymtab);
  360. }
  361. /* Keep any partial symbol tables that were built. */
  362. void keep ()
  363. {
  364. m_partial_symtabs = nullptr;
  365. }
  366. private:
  367. /* The partial symbol storage object. */
  368. psymtab_storage *m_partial_symtabs;
  369. /* How far back to free. */
  370. struct partial_symtab *m_psymtab;
  371. };
  372. /* An implementation of quick_symbol_functions, specialized for
  373. partial symbols. */
  374. struct psymbol_functions : public quick_symbol_functions
  375. {
  376. explicit psymbol_functions (const std::shared_ptr<psymtab_storage> &storage)
  377. : m_partial_symtabs (storage)
  378. {
  379. }
  380. psymbol_functions ()
  381. : m_partial_symtabs (new psymtab_storage)
  382. {
  383. }
  384. bool has_symbols (struct objfile *objfile) override;
  385. bool has_unexpanded_symtabs (struct objfile *objfile) override;
  386. struct symtab *find_last_source_symtab (struct objfile *objfile) override;
  387. void forget_cached_source_info (struct objfile *objfile) override;
  388. enum language lookup_global_symbol_language (struct objfile *objfile,
  389. const char *name,
  390. domain_enum domain,
  391. bool *symbol_found_p) override;
  392. void print_stats (struct objfile *objfile, bool print_bcache) override;
  393. void dump (struct objfile *objfile) override;
  394. void expand_all_symtabs (struct objfile *objfile) override;
  395. void expand_matching_symbols
  396. (struct objfile *,
  397. const lookup_name_info &lookup_name,
  398. domain_enum domain,
  399. int global,
  400. symbol_compare_ftype *ordered_compare) override;
  401. bool expand_symtabs_matching
  402. (struct objfile *objfile,
  403. gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
  404. const lookup_name_info *lookup_name,
  405. gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
  406. gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
  407. block_search_flags search_flags,
  408. domain_enum domain,
  409. enum search_domain kind) override;
  410. struct compunit_symtab *find_pc_sect_compunit_symtab
  411. (struct objfile *objfile, struct bound_minimal_symbol msymbol,
  412. CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
  413. struct compunit_symtab *find_compunit_symtab_by_address
  414. (struct objfile *objfile, CORE_ADDR address) override;
  415. void map_symbol_filenames (struct objfile *objfile,
  416. gdb::function_view<symbol_filename_ftype> fun,
  417. bool need_fullname) override;
  418. void relocated () override
  419. {
  420. m_psymbol_map.clear ();
  421. }
  422. /* Ensure the partial symbols for OBJFILE have been loaded. Return
  423. a range adapter for the psymtabs. */
  424. psymtab_storage::partial_symtab_range require_partial_symbols
  425. (struct objfile *objfile);
  426. /* Return the partial symbol storage associated with this
  427. object. */
  428. const std::shared_ptr<psymtab_storage> &get_partial_symtabs () const
  429. {
  430. return m_partial_symtabs;
  431. }
  432. /* Replace the partial symbol table storage in this object with
  433. SYMS. */
  434. void set_partial_symtabs (const std::shared_ptr<psymtab_storage> &syms)
  435. {
  436. m_partial_symtabs = syms;
  437. }
  438. /* Find which partial symtab contains PC and SECTION. Return NULL if
  439. none. We return the psymtab that contains a symbol whose address
  440. exactly matches PC, or, if we cannot find an exact match, the
  441. psymtab that contains a symbol whose address is closest to PC. */
  442. struct partial_symtab *find_pc_sect_psymtab
  443. (struct objfile *objfile,
  444. CORE_ADDR pc,
  445. struct obj_section *section,
  446. struct bound_minimal_symbol msymbol);
  447. private:
  448. /* Count the number of partial symbols in *THIS. */
  449. int count_psyms ();
  450. void fill_psymbol_map (struct objfile *objfile,
  451. struct partial_symtab *psymtab,
  452. std::set<CORE_ADDR> *seen_addrs,
  453. const std::vector<partial_symbol *> &symbols);
  454. /* Storage for the partial symbols. */
  455. std::shared_ptr<psymtab_storage> m_partial_symtabs;
  456. /* Map symbol addresses to the partial symtab that defines the
  457. object at that address. */
  458. std::vector<std::pair<CORE_ADDR, partial_symtab *>> m_psymbol_map;
  459. };
  460. #endif /* PSYMPRIV_H */