1 /* Build symbol tables in GDB's internal format. 2 Copyright (C) 1986-2020 Free Software Foundation, Inc. 3 4 This file is part of GDB. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18 19 #if !defined (BUILDSYM_H) 20 #define BUILDSYM_H 1 21 22 #include "gdb_obstack.h" 23 24 struct objfile; 25 struct symbol; 26 struct addrmap; 27 struct compunit_symtab; 28 enum language; 29 30 /* This module provides definitions used for creating and adding to 31 the symbol table. These routines are called from various symbol- 32 file-reading routines. 33 34 They originated in dbxread.c of gdb-4.2, and were split out to 35 make xcoffread.c more maintainable by sharing code. */ 36 37 struct block; 38 struct pending_block; 39 40 struct dynamic_prop; 41 42 /* The list of sub-source-files within the current individual 43 compilation. Each file gets its own symtab with its own linetable 44 and associated info, but they all share one blockvector. */ 45 46 struct subfile 47 { 48 struct subfile *next; 49 /* Space for this is malloc'd. */ 50 char *name; 51 /* Space for this is malloc'd. */ 52 struct linetable *line_vector; 53 int line_vector_length; 54 /* The "containing" compunit. */ 55 struct buildsym_compunit *buildsym_compunit; 56 enum language language; 57 struct symtab *symtab; 58 }; 59 60 /* Record the symbols defined for each context in a list. We don't 61 create a struct block for the context until we know how long to 62 make it. */ 63 64 #define PENDINGSIZE 100 65 66 struct pending 67 { 68 struct pending *next; 69 int nsyms; 70 struct symbol *symbol[PENDINGSIZE]; 71 }; 72 73 /* Stack representing unclosed lexical contexts (that will become 74 blocks, eventually). */ 75 76 struct context_stack 77 { 78 /* Outer locals at the time we entered */ 79 80 struct pending *locals; 81 82 /* Pending using directives at the time we entered. */ 83 84 struct using_direct *local_using_directives; 85 86 /* Pointer into blocklist as of entry */ 87 88 struct pending_block *old_blocks; 89 90 /* Name of function, if any, defining context */ 91 92 struct symbol *name; 93 94 /* Expression that computes the frame base of the lexically enclosing 95 function, if any. NULL otherwise. */ 96 97 struct dynamic_prop *static_link; 98 99 /* PC where this context starts */ 100 101 CORE_ADDR start_addr; 102 103 /* Temp slot for exception handling. */ 104 105 CORE_ADDR end_addr; 106 107 /* For error-checking matching push/pop */ 108 109 int depth; 110 111 }; 112 113 /* Buildsym's counterpart to struct compunit_symtab. */ 114 115 struct buildsym_compunit 116 { 117 /* Start recording information about a primary source file (IOW, not an 118 included source file). 119 COMP_DIR is the directory in which the compilation unit was compiled 120 (or NULL if not known). */ 121 122 buildsym_compunit (struct objfile *objfile_, const char *name, 123 const char *comp_dir_, enum language language_, 124 CORE_ADDR last_addr); 125 126 /* Reopen an existing compunit_symtab so that additional symbols can 127 be added to it. Arguments are as for the main constructor. CUST 128 is the expandable compunit_symtab to be reopened. */ 129 130 buildsym_compunit (struct objfile *objfile_, const char *name, 131 const char *comp_dir_, enum language language_, 132 CORE_ADDR last_addr, struct compunit_symtab *cust) 133 : m_objfile (objfile_), 134 m_last_source_file (name == nullptr ? nullptr : xstrdup (name)), 135 m_comp_dir (comp_dir_ == nullptr ? nullptr : xstrdup (comp_dir_)), 136 m_compunit_symtab (cust), 137 m_language (language_), 138 m_last_source_start_addr (last_addr) 139 { 140 } 141 142 ~buildsym_compunit (); 143 144 DISABLE_COPY_AND_ASSIGN (buildsym_compunit); 145 146 void set_last_source_file (const char *name) 147 { 148 char *new_name = name == NULL ? NULL : xstrdup (name); 149 m_last_source_file.reset (new_name); 150 } 151 152 const char *get_last_source_file () 153 { 154 return m_last_source_file.get (); 155 } 156 157 struct macro_table *get_macro_table (); 158 159 struct macro_table *release_macros () 160 { 161 struct macro_table *result = m_pending_macros; 162 m_pending_macros = nullptr; 163 return result; 164 } 165 166 /* This function is called to discard any pending blocks. */ 167 168 void free_pending_blocks () 169 { 170 m_pending_block_obstack.clear (); 171 m_pending_blocks = nullptr; 172 } 173 174 struct block *finish_block (struct symbol *symbol, 175 struct pending_block *old_blocks, 176 const struct dynamic_prop *static_link, 177 CORE_ADDR start, CORE_ADDR end); 178 179 void record_block_range (struct block *block, 180 CORE_ADDR start, CORE_ADDR end_inclusive); 181 182 void start_subfile (const char *name); 183 184 void patch_subfile_names (struct subfile *subfile, const char *name); 185 186 void push_subfile (); 187 188 const char *pop_subfile (); 189 190 void record_line (struct subfile *subfile, int line, CORE_ADDR pc, 191 bool is_stmt); 192 193 struct compunit_symtab *get_compunit_symtab () 194 { 195 return m_compunit_symtab; 196 } 197 198 void set_last_source_start_addr (CORE_ADDR addr) 199 { 200 m_last_source_start_addr = addr; 201 } 202 203 CORE_ADDR get_last_source_start_addr () 204 { 205 return m_last_source_start_addr; 206 } 207 208 struct using_direct **get_local_using_directives () 209 { 210 return &m_local_using_directives; 211 } 212 213 void set_local_using_directives (struct using_direct *new_local) 214 { 215 m_local_using_directives = new_local; 216 } 217 218 struct using_direct **get_global_using_directives () 219 { 220 return &m_global_using_directives; 221 } 222 223 bool outermost_context_p () const 224 { 225 return m_context_stack.empty (); 226 } 227 228 struct context_stack *get_current_context_stack () 229 { 230 if (m_context_stack.empty ()) 231 return nullptr; 232 return &m_context_stack.back (); 233 } 234 235 int get_context_stack_depth () const 236 { 237 return m_context_stack.size (); 238 } 239 240 struct subfile *get_current_subfile () 241 { 242 return m_current_subfile; 243 } 244 245 struct pending **get_local_symbols () 246 { 247 return &m_local_symbols; 248 } 249 250 struct pending **get_file_symbols () 251 { 252 return &m_file_symbols; 253 } 254 255 struct pending **get_global_symbols () 256 { 257 return &m_global_symbols; 258 } 259 260 void record_debugformat (const char *format) 261 { 262 m_debugformat = format; 263 } 264 265 void record_producer (const char *producer) 266 { 267 m_producer = producer; 268 } 269 270 struct context_stack *push_context (int desc, CORE_ADDR valu); 271 272 struct context_stack pop_context (); 273 274 struct block *end_symtab_get_static_block (CORE_ADDR end_addr, 275 int expandable, int required); 276 277 struct compunit_symtab *end_symtab_from_static_block 278 (struct block *static_block, int section, int expandable); 279 280 struct compunit_symtab *end_symtab (CORE_ADDR end_addr, int section); 281 282 struct compunit_symtab *end_expandable_symtab (CORE_ADDR end_addr, 283 int section); 284 285 void augment_type_symtab (); 286 287 private: 288 289 void record_pending_block (struct block *block, struct pending_block *opblock); 290 291 struct block *finish_block_internal (struct symbol *symbol, 292 struct pending **listhead, 293 struct pending_block *old_blocks, 294 const struct dynamic_prop *static_link, 295 CORE_ADDR start, CORE_ADDR end, 296 int is_global, int expandable); 297 298 struct blockvector *make_blockvector (); 299 300 void watch_main_source_file_lossage (); 301 302 struct compunit_symtab *end_symtab_with_blockvector 303 (struct block *static_block, int section, int expandable); 304 305 /* The objfile we're reading debug info from. */ 306 struct objfile *m_objfile; 307 308 /* List of subfiles (source files). 309 Files are added to the front of the list. 310 This is important mostly for the language determination hacks we use, 311 which iterate over previously added files. */ 312 struct subfile *m_subfiles = nullptr; 313 314 /* The subfile of the main source file. */ 315 struct subfile *m_main_subfile = nullptr; 316 317 /* Name of source file whose symbol data we are now processing. This 318 comes from a symbol of type N_SO for stabs. For DWARF it comes 319 from the DW_AT_name attribute of a DW_TAG_compile_unit DIE. */ 320 gdb::unique_xmalloc_ptr<char> m_last_source_file; 321 322 /* E.g., DW_AT_comp_dir if DWARF. Space for this is malloc'd. */ 323 gdb::unique_xmalloc_ptr<char> m_comp_dir; 324 325 /* Space for this is not malloc'd, and is assumed to have at least 326 the same lifetime as objfile. */ 327 const char *m_producer = nullptr; 328 329 /* Space for this is not malloc'd, and is assumed to have at least 330 the same lifetime as objfile. */ 331 const char *m_debugformat = nullptr; 332 333 /* The compunit we are building. */ 334 struct compunit_symtab *m_compunit_symtab = nullptr; 335 336 /* Language of this compunit_symtab. */ 337 enum language m_language; 338 339 /* The macro table for the compilation unit whose symbols we're 340 currently reading. */ 341 struct macro_table *m_pending_macros = nullptr; 342 343 /* True if symtab has line number info. This prevents an otherwise 344 empty symtab from being tossed. */ 345 bool m_have_line_numbers = false; 346 347 /* Core address of start of text of current source file. This too 348 comes from the N_SO symbol. For Dwarf it typically comes from the 349 DW_AT_low_pc attribute of a DW_TAG_compile_unit DIE. */ 350 CORE_ADDR m_last_source_start_addr; 351 352 /* Stack of subfile names. */ 353 std::vector<const char *> m_subfile_stack; 354 355 /* The "using" directives local to lexical context. */ 356 struct using_direct *m_local_using_directives = nullptr; 357 358 /* Global "using" directives. */ 359 struct using_direct *m_global_using_directives = nullptr; 360 361 /* The stack of contexts that are pushed by push_context and popped 362 by pop_context. */ 363 std::vector<struct context_stack> m_context_stack; 364 365 struct subfile *m_current_subfile = nullptr; 366 367 /* The mutable address map for the compilation unit whose symbols 368 we're currently reading. The symtabs' shared blockvector will 369 point to a fixed copy of this. */ 370 struct addrmap *m_pending_addrmap = nullptr; 371 372 /* The obstack on which we allocate pending_addrmap. 373 If pending_addrmap is NULL, this is uninitialized; otherwise, it is 374 initialized (and holds pending_addrmap). */ 375 auto_obstack m_pending_addrmap_obstack; 376 377 /* True if we recorded any ranges in the addrmap that are different 378 from those in the blockvector already. We set this to false when 379 we start processing a symfile, and if it's still false at the 380 end, then we just toss the addrmap. */ 381 bool m_pending_addrmap_interesting = false; 382 383 /* An obstack used for allocating pending blocks. */ 384 auto_obstack m_pending_block_obstack; 385 386 /* Pointer to the head of a linked list of symbol blocks which have 387 already been finalized (lexical contexts already closed) and which 388 are just waiting to be built into a blockvector when finalizing the 389 associated symtab. */ 390 struct pending_block *m_pending_blocks = nullptr; 391 392 /* Pending static symbols and types at the top level. */ 393 struct pending *m_file_symbols = nullptr; 394 395 /* Pending global functions and variables. */ 396 struct pending *m_global_symbols = nullptr; 397 398 /* Pending symbols that are local to the lexical context. */ 399 struct pending *m_local_symbols = nullptr; 400 }; 401 402 403 404 extern void add_symbol_to_list (struct symbol *symbol, 405 struct pending **listhead); 406 407 extern struct symbol *find_symbol_in_list (struct pending *list, 408 char *name, int length); 409 410 #endif /* defined (BUILDSYM_H) */ 411