buildsym.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* Build 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. #if !defined (BUILDSYM_H)
  15. #define BUILDSYM_H 1
  16. #include "gdbsupport/gdb_obstack.h"
  17. struct objfile;
  18. struct symbol;
  19. struct addrmap;
  20. struct compunit_symtab;
  21. enum language;
  22. /* This module provides definitions used for creating and adding to
  23. the symbol table. These routines are called from various symbol-
  24. file-reading routines.
  25. They originated in dbxread.c of gdb-4.2, and were split out to
  26. make xcoffread.c more maintainable by sharing code. */
  27. struct block;
  28. struct pending_block;
  29. struct dynamic_prop;
  30. /* The list of sub-source-files within the current individual
  31. compilation. Each file gets its own symtab with its own linetable
  32. and associated info, but they all share one blockvector. */
  33. struct subfile
  34. {
  35. struct subfile *next;
  36. /* Space for this is malloc'd. */
  37. char *name;
  38. /* Space for this is malloc'd. */
  39. struct linetable *line_vector;
  40. int line_vector_length;
  41. enum language language;
  42. struct symtab *symtab;
  43. };
  44. /* Record the symbols defined for each context in a list. We don't
  45. create a struct block for the context until we know how long to
  46. make it. */
  47. #define PENDINGSIZE 100
  48. struct pending
  49. {
  50. struct pending *next;
  51. int nsyms;
  52. struct symbol *symbol[PENDINGSIZE];
  53. };
  54. /* Stack representing unclosed lexical contexts (that will become
  55. blocks, eventually). */
  56. struct context_stack
  57. {
  58. /* Outer locals at the time we entered */
  59. struct pending *locals;
  60. /* Pending using directives at the time we entered. */
  61. struct using_direct *local_using_directives;
  62. /* Pointer into blocklist as of entry */
  63. struct pending_block *old_blocks;
  64. /* Name of function, if any, defining context */
  65. struct symbol *name;
  66. /* Expression that computes the frame base of the lexically enclosing
  67. function, if any. NULL otherwise. */
  68. struct dynamic_prop *static_link;
  69. /* PC where this context starts */
  70. CORE_ADDR start_addr;
  71. /* Temp slot for exception handling. */
  72. CORE_ADDR end_addr;
  73. /* For error-checking matching push/pop */
  74. int depth;
  75. };
  76. /* Flags associated with a linetable entry. */
  77. enum linetable_entry_flag : unsigned
  78. {
  79. /* Indicates this PC is a good location to place a breakpoint at LINE. */
  80. LEF_IS_STMT = 1 << 1,
  81. /* Indicates this PC is a good location to place a breakpoint at the first
  82. instruction past a function prologue. */
  83. LEF_PROLOGUE_END = 1 << 2,
  84. };
  85. DEF_ENUM_FLAGS_TYPE (enum linetable_entry_flag, linetable_entry_flags);
  86. /* Buildsym's counterpart to struct compunit_symtab. */
  87. struct buildsym_compunit
  88. {
  89. /* Start recording information about a primary source file (IOW, not an
  90. included source file).
  91. COMP_DIR is the directory in which the compilation unit was compiled
  92. (or NULL if not known). */
  93. buildsym_compunit (struct objfile *objfile_, const char *name,
  94. const char *comp_dir_, enum language language_,
  95. CORE_ADDR last_addr);
  96. /* Reopen an existing compunit_symtab so that additional symbols can
  97. be added to it. Arguments are as for the main constructor. CUST
  98. is the expandable compunit_symtab to be reopened. */
  99. buildsym_compunit (struct objfile *objfile_, const char *name,
  100. const char *comp_dir_, enum language language_,
  101. CORE_ADDR last_addr, struct compunit_symtab *cust)
  102. : m_objfile (objfile_),
  103. m_last_source_file (name == nullptr ? nullptr : xstrdup (name)),
  104. m_comp_dir (comp_dir_ == nullptr ? nullptr : xstrdup (comp_dir_)),
  105. m_compunit_symtab (cust),
  106. m_language (language_),
  107. m_last_source_start_addr (last_addr)
  108. {
  109. }
  110. ~buildsym_compunit ();
  111. DISABLE_COPY_AND_ASSIGN (buildsym_compunit);
  112. void set_last_source_file (const char *name)
  113. {
  114. char *new_name = name == NULL ? NULL : xstrdup (name);
  115. m_last_source_file.reset (new_name);
  116. }
  117. const char *get_last_source_file ()
  118. {
  119. return m_last_source_file.get ();
  120. }
  121. struct macro_table *get_macro_table ();
  122. struct macro_table *release_macros ()
  123. {
  124. struct macro_table *result = m_pending_macros;
  125. m_pending_macros = nullptr;
  126. return result;
  127. }
  128. /* This function is called to discard any pending blocks. */
  129. void free_pending_blocks ()
  130. {
  131. m_pending_block_obstack.clear ();
  132. m_pending_blocks = nullptr;
  133. }
  134. struct block *finish_block (struct symbol *symbol,
  135. struct pending_block *old_blocks,
  136. const struct dynamic_prop *static_link,
  137. CORE_ADDR start, CORE_ADDR end);
  138. void record_block_range (struct block *block,
  139. CORE_ADDR start, CORE_ADDR end_inclusive);
  140. void start_subfile (const char *name);
  141. void patch_subfile_names (struct subfile *subfile, const char *name);
  142. void push_subfile ();
  143. const char *pop_subfile ();
  144. void record_line (struct subfile *subfile, int line, CORE_ADDR pc,
  145. linetable_entry_flags flags);
  146. struct compunit_symtab *get_compunit_symtab ()
  147. {
  148. return m_compunit_symtab;
  149. }
  150. void set_last_source_start_addr (CORE_ADDR addr)
  151. {
  152. m_last_source_start_addr = addr;
  153. }
  154. CORE_ADDR get_last_source_start_addr ()
  155. {
  156. return m_last_source_start_addr;
  157. }
  158. struct using_direct **get_local_using_directives ()
  159. {
  160. return &m_local_using_directives;
  161. }
  162. void set_local_using_directives (struct using_direct *new_local)
  163. {
  164. m_local_using_directives = new_local;
  165. }
  166. struct using_direct **get_global_using_directives ()
  167. {
  168. return &m_global_using_directives;
  169. }
  170. bool outermost_context_p () const
  171. {
  172. return m_context_stack.empty ();
  173. }
  174. struct context_stack *get_current_context_stack ()
  175. {
  176. if (m_context_stack.empty ())
  177. return nullptr;
  178. return &m_context_stack.back ();
  179. }
  180. int get_context_stack_depth () const
  181. {
  182. return m_context_stack.size ();
  183. }
  184. struct subfile *get_current_subfile ()
  185. {
  186. return m_current_subfile;
  187. }
  188. struct pending **get_local_symbols ()
  189. {
  190. return &m_local_symbols;
  191. }
  192. struct pending **get_file_symbols ()
  193. {
  194. return &m_file_symbols;
  195. }
  196. struct pending **get_global_symbols ()
  197. {
  198. return &m_global_symbols;
  199. }
  200. void record_debugformat (const char *format)
  201. {
  202. m_debugformat = format;
  203. }
  204. void record_producer (const char *producer)
  205. {
  206. m_producer = producer;
  207. }
  208. struct context_stack *push_context (int desc, CORE_ADDR valu);
  209. struct context_stack pop_context ();
  210. struct block *end_compunit_symtab_get_static_block
  211. (CORE_ADDR end_addr, int expandable, int required);
  212. struct compunit_symtab *end_compunit_symtab_from_static_block
  213. (struct block *static_block, int section, int expandable);
  214. struct compunit_symtab *end_compunit_symtab (CORE_ADDR end_addr, int section);
  215. struct compunit_symtab *end_expandable_symtab (CORE_ADDR end_addr,
  216. int section);
  217. void augment_type_symtab ();
  218. private:
  219. void record_pending_block (struct block *block, struct pending_block *opblock);
  220. struct block *finish_block_internal (struct symbol *symbol,
  221. struct pending **listhead,
  222. struct pending_block *old_blocks,
  223. const struct dynamic_prop *static_link,
  224. CORE_ADDR start, CORE_ADDR end,
  225. int is_global, int expandable);
  226. struct blockvector *make_blockvector ();
  227. void watch_main_source_file_lossage ();
  228. struct compunit_symtab *end_compunit_symtab_with_blockvector
  229. (struct block *static_block, int section, int expandable);
  230. /* The objfile we're reading debug info from. */
  231. struct objfile *m_objfile;
  232. /* List of subfiles (source files).
  233. Files are added to the front of the list.
  234. This is important mostly for the language determination hacks we use,
  235. which iterate over previously added files. */
  236. struct subfile *m_subfiles = nullptr;
  237. /* The subfile of the main source file. */
  238. struct subfile *m_main_subfile = nullptr;
  239. /* Name of source file whose symbol data we are now processing. This
  240. comes from a symbol of type N_SO for stabs. For DWARF it comes
  241. from the DW_AT_name attribute of a DW_TAG_compile_unit DIE. */
  242. gdb::unique_xmalloc_ptr<char> m_last_source_file;
  243. /* E.g., DW_AT_comp_dir if DWARF. Space for this is malloc'd. */
  244. gdb::unique_xmalloc_ptr<char> m_comp_dir;
  245. /* Space for this is not malloc'd, and is assumed to have at least
  246. the same lifetime as objfile. */
  247. const char *m_producer = nullptr;
  248. /* Space for this is not malloc'd, and is assumed to have at least
  249. the same lifetime as objfile. */
  250. const char *m_debugformat = nullptr;
  251. /* The compunit we are building. */
  252. struct compunit_symtab *m_compunit_symtab = nullptr;
  253. /* Language of this compunit_symtab. */
  254. enum language m_language;
  255. /* The macro table for the compilation unit whose symbols we're
  256. currently reading. */
  257. struct macro_table *m_pending_macros = nullptr;
  258. /* True if symtab has line number info. This prevents an otherwise
  259. empty symtab from being tossed. */
  260. bool m_have_line_numbers = false;
  261. /* Core address of start of text of current source file. This too
  262. comes from the N_SO symbol. For Dwarf it typically comes from the
  263. DW_AT_low_pc attribute of a DW_TAG_compile_unit DIE. */
  264. CORE_ADDR m_last_source_start_addr;
  265. /* Stack of subfile names. */
  266. std::vector<const char *> m_subfile_stack;
  267. /* The "using" directives local to lexical context. */
  268. struct using_direct *m_local_using_directives = nullptr;
  269. /* Global "using" directives. */
  270. struct using_direct *m_global_using_directives = nullptr;
  271. /* The stack of contexts that are pushed by push_context and popped
  272. by pop_context. */
  273. std::vector<struct context_stack> m_context_stack;
  274. struct subfile *m_current_subfile = nullptr;
  275. /* The mutable address map for the compilation unit whose symbols
  276. we're currently reading. The symtabs' shared blockvector will
  277. point to a fixed copy of this. */
  278. struct addrmap *m_pending_addrmap = nullptr;
  279. /* The obstack on which we allocate pending_addrmap.
  280. If pending_addrmap is NULL, this is uninitialized; otherwise, it is
  281. initialized (and holds pending_addrmap). */
  282. auto_obstack m_pending_addrmap_obstack;
  283. /* True if we recorded any ranges in the addrmap that are different
  284. from those in the blockvector already. We set this to false when
  285. we start processing a symfile, and if it's still false at the
  286. end, then we just toss the addrmap. */
  287. bool m_pending_addrmap_interesting = false;
  288. /* An obstack used for allocating pending blocks. */
  289. auto_obstack m_pending_block_obstack;
  290. /* Pointer to the head of a linked list of symbol blocks which have
  291. already been finalized (lexical contexts already closed) and which
  292. are just waiting to be built into a blockvector when finalizing the
  293. associated symtab. */
  294. struct pending_block *m_pending_blocks = nullptr;
  295. /* Pending static symbols and types at the top level. */
  296. struct pending *m_file_symbols = nullptr;
  297. /* Pending global functions and variables. */
  298. struct pending *m_global_symbols = nullptr;
  299. /* Pending symbols that are local to the lexical context. */
  300. struct pending *m_local_symbols = nullptr;
  301. };
  302. extern void add_symbol_to_list (struct symbol *symbol,
  303. struct pending **listhead);
  304. extern struct symbol *find_symbol_in_list (struct pending *list,
  305. char *name, int length);
  306. #endif /* defined (BUILDSYM_H) */