Introduce DWARF abbrev cache
[binutils-gdb.git] / gdb / buildsym.h
1 /* Build symbol tables in GDB's internal format.
2 Copyright (C) 1986-2022 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 "gdbsupport/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 enum language language;
55 struct symtab *symtab;
56 };
57
58 /* Record the symbols defined for each context in a list. We don't
59 create a struct block for the context until we know how long to
60 make it. */
61
62 #define PENDINGSIZE 100
63
64 struct pending
65 {
66 struct pending *next;
67 int nsyms;
68 struct symbol *symbol[PENDINGSIZE];
69 };
70
71 /* Stack representing unclosed lexical contexts (that will become
72 blocks, eventually). */
73
74 struct context_stack
75 {
76 /* Outer locals at the time we entered */
77
78 struct pending *locals;
79
80 /* Pending using directives at the time we entered. */
81
82 struct using_direct *local_using_directives;
83
84 /* Pointer into blocklist as of entry */
85
86 struct pending_block *old_blocks;
87
88 /* Name of function, if any, defining context */
89
90 struct symbol *name;
91
92 /* Expression that computes the frame base of the lexically enclosing
93 function, if any. NULL otherwise. */
94
95 struct dynamic_prop *static_link;
96
97 /* PC where this context starts */
98
99 CORE_ADDR start_addr;
100
101 /* Temp slot for exception handling. */
102
103 CORE_ADDR end_addr;
104
105 /* For error-checking matching push/pop */
106
107 int depth;
108
109 };
110
111 /* Flags associated with a linetable entry. */
112
113 enum linetable_entry_flag : unsigned
114 {
115 /* Indicates this PC is a good location to place a breakpoint at LINE. */
116 LEF_IS_STMT = 1 << 1,
117
118 /* Indicates this PC is a good location to place a breakpoint at the first
119 instruction past a function prologue. */
120 LEF_PROLOGUE_END = 1 << 2,
121 };
122 DEF_ENUM_FLAGS_TYPE (enum linetable_entry_flag, linetable_entry_flags);
123
124
125 /* Buildsym's counterpart to struct compunit_symtab. */
126
127 struct buildsym_compunit
128 {
129 /* Start recording information about a primary source file (IOW, not an
130 included source file).
131 COMP_DIR is the directory in which the compilation unit was compiled
132 (or NULL if not known). */
133
134 buildsym_compunit (struct objfile *objfile_, const char *name,
135 const char *comp_dir_, enum language language_,
136 CORE_ADDR last_addr);
137
138 /* Reopen an existing compunit_symtab so that additional symbols can
139 be added to it. Arguments are as for the main constructor. CUST
140 is the expandable compunit_symtab to be reopened. */
141
142 buildsym_compunit (struct objfile *objfile_, const char *name,
143 const char *comp_dir_, enum language language_,
144 CORE_ADDR last_addr, struct compunit_symtab *cust)
145 : m_objfile (objfile_),
146 m_last_source_file (name == nullptr ? nullptr : xstrdup (name)),
147 m_comp_dir (comp_dir_ == nullptr ? nullptr : xstrdup (comp_dir_)),
148 m_compunit_symtab (cust),
149 m_language (language_),
150 m_last_source_start_addr (last_addr)
151 {
152 }
153
154 ~buildsym_compunit ();
155
156 DISABLE_COPY_AND_ASSIGN (buildsym_compunit);
157
158 void set_last_source_file (const char *name)
159 {
160 char *new_name = name == NULL ? NULL : xstrdup (name);
161 m_last_source_file.reset (new_name);
162 }
163
164 const char *get_last_source_file ()
165 {
166 return m_last_source_file.get ();
167 }
168
169 struct macro_table *get_macro_table ();
170
171 struct macro_table *release_macros ()
172 {
173 struct macro_table *result = m_pending_macros;
174 m_pending_macros = nullptr;
175 return result;
176 }
177
178 /* This function is called to discard any pending blocks. */
179
180 void free_pending_blocks ()
181 {
182 m_pending_block_obstack.clear ();
183 m_pending_blocks = nullptr;
184 }
185
186 struct block *finish_block (struct symbol *symbol,
187 struct pending_block *old_blocks,
188 const struct dynamic_prop *static_link,
189 CORE_ADDR start, CORE_ADDR end);
190
191 void record_block_range (struct block *block,
192 CORE_ADDR start, CORE_ADDR end_inclusive);
193
194 void start_subfile (const char *name);
195
196 void patch_subfile_names (struct subfile *subfile, const char *name);
197
198 void push_subfile ();
199
200 const char *pop_subfile ();
201
202 void record_line (struct subfile *subfile, int line, CORE_ADDR pc,
203 linetable_entry_flags flags);
204
205 struct compunit_symtab *get_compunit_symtab ()
206 {
207 return m_compunit_symtab;
208 }
209
210 void set_last_source_start_addr (CORE_ADDR addr)
211 {
212 m_last_source_start_addr = addr;
213 }
214
215 CORE_ADDR get_last_source_start_addr ()
216 {
217 return m_last_source_start_addr;
218 }
219
220 struct using_direct **get_local_using_directives ()
221 {
222 return &m_local_using_directives;
223 }
224
225 void set_local_using_directives (struct using_direct *new_local)
226 {
227 m_local_using_directives = new_local;
228 }
229
230 struct using_direct **get_global_using_directives ()
231 {
232 return &m_global_using_directives;
233 }
234
235 bool outermost_context_p () const
236 {
237 return m_context_stack.empty ();
238 }
239
240 struct context_stack *get_current_context_stack ()
241 {
242 if (m_context_stack.empty ())
243 return nullptr;
244 return &m_context_stack.back ();
245 }
246
247 int get_context_stack_depth () const
248 {
249 return m_context_stack.size ();
250 }
251
252 struct subfile *get_current_subfile ()
253 {
254 return m_current_subfile;
255 }
256
257 struct pending **get_local_symbols ()
258 {
259 return &m_local_symbols;
260 }
261
262 struct pending **get_file_symbols ()
263 {
264 return &m_file_symbols;
265 }
266
267 struct pending **get_global_symbols ()
268 {
269 return &m_global_symbols;
270 }
271
272 void record_debugformat (const char *format)
273 {
274 m_debugformat = format;
275 }
276
277 void record_producer (const char *producer)
278 {
279 m_producer = producer;
280 }
281
282 struct context_stack *push_context (int desc, CORE_ADDR valu);
283
284 struct context_stack pop_context ();
285
286 struct block *end_compunit_symtab_get_static_block
287 (CORE_ADDR end_addr, int expandable, int required);
288
289 struct compunit_symtab *end_compunit_symtab_from_static_block
290 (struct block *static_block, int section, int expandable);
291
292 struct compunit_symtab *end_compunit_symtab (CORE_ADDR end_addr, int section);
293
294 struct compunit_symtab *end_expandable_symtab (CORE_ADDR end_addr,
295 int section);
296
297 void augment_type_symtab ();
298
299 private:
300
301 void record_pending_block (struct block *block, struct pending_block *opblock);
302
303 struct block *finish_block_internal (struct symbol *symbol,
304 struct pending **listhead,
305 struct pending_block *old_blocks,
306 const struct dynamic_prop *static_link,
307 CORE_ADDR start, CORE_ADDR end,
308 int is_global, int expandable);
309
310 struct blockvector *make_blockvector ();
311
312 void watch_main_source_file_lossage ();
313
314 struct compunit_symtab *end_compunit_symtab_with_blockvector
315 (struct block *static_block, int section, int expandable);
316
317 /* The objfile we're reading debug info from. */
318 struct objfile *m_objfile;
319
320 /* List of subfiles (source files).
321 Files are added to the front of the list.
322 This is important mostly for the language determination hacks we use,
323 which iterate over previously added files. */
324 struct subfile *m_subfiles = nullptr;
325
326 /* The subfile of the main source file. */
327 struct subfile *m_main_subfile = nullptr;
328
329 /* Name of source file whose symbol data we are now processing. This
330 comes from a symbol of type N_SO for stabs. For DWARF it comes
331 from the DW_AT_name attribute of a DW_TAG_compile_unit DIE. */
332 gdb::unique_xmalloc_ptr<char> m_last_source_file;
333
334 /* E.g., DW_AT_comp_dir if DWARF. Space for this is malloc'd. */
335 gdb::unique_xmalloc_ptr<char> m_comp_dir;
336
337 /* Space for this is not malloc'd, and is assumed to have at least
338 the same lifetime as objfile. */
339 const char *m_producer = nullptr;
340
341 /* Space for this is not malloc'd, and is assumed to have at least
342 the same lifetime as objfile. */
343 const char *m_debugformat = nullptr;
344
345 /* The compunit we are building. */
346 struct compunit_symtab *m_compunit_symtab = nullptr;
347
348 /* Language of this compunit_symtab. */
349 enum language m_language;
350
351 /* The macro table for the compilation unit whose symbols we're
352 currently reading. */
353 struct macro_table *m_pending_macros = nullptr;
354
355 /* True if symtab has line number info. This prevents an otherwise
356 empty symtab from being tossed. */
357 bool m_have_line_numbers = false;
358
359 /* Core address of start of text of current source file. This too
360 comes from the N_SO symbol. For Dwarf it typically comes from the
361 DW_AT_low_pc attribute of a DW_TAG_compile_unit DIE. */
362 CORE_ADDR m_last_source_start_addr;
363
364 /* Stack of subfile names. */
365 std::vector<const char *> m_subfile_stack;
366
367 /* The "using" directives local to lexical context. */
368 struct using_direct *m_local_using_directives = nullptr;
369
370 /* Global "using" directives. */
371 struct using_direct *m_global_using_directives = nullptr;
372
373 /* The stack of contexts that are pushed by push_context and popped
374 by pop_context. */
375 std::vector<struct context_stack> m_context_stack;
376
377 struct subfile *m_current_subfile = nullptr;
378
379 /* The mutable address map for the compilation unit whose symbols
380 we're currently reading. The symtabs' shared blockvector will
381 point to a fixed copy of this. */
382 struct addrmap *m_pending_addrmap = nullptr;
383
384 /* The obstack on which we allocate pending_addrmap.
385 If pending_addrmap is NULL, this is uninitialized; otherwise, it is
386 initialized (and holds pending_addrmap). */
387 auto_obstack m_pending_addrmap_obstack;
388
389 /* True if we recorded any ranges in the addrmap that are different
390 from those in the blockvector already. We set this to false when
391 we start processing a symfile, and if it's still false at the
392 end, then we just toss the addrmap. */
393 bool m_pending_addrmap_interesting = false;
394
395 /* An obstack used for allocating pending blocks. */
396 auto_obstack m_pending_block_obstack;
397
398 /* Pointer to the head of a linked list of symbol blocks which have
399 already been finalized (lexical contexts already closed) and which
400 are just waiting to be built into a blockvector when finalizing the
401 associated symtab. */
402 struct pending_block *m_pending_blocks = nullptr;
403
404 /* Pending static symbols and types at the top level. */
405 struct pending *m_file_symbols = nullptr;
406
407 /* Pending global functions and variables. */
408 struct pending *m_global_symbols = nullptr;
409
410 /* Pending symbols that are local to the lexical context. */
411 struct pending *m_local_symbols = nullptr;
412 };
413
414 \f
415
416 extern void add_symbol_to_list (struct symbol *symbol,
417 struct pending **listhead);
418
419 extern struct symbol *find_symbol_in_list (struct pending *list,
420 char *name, int length);
421
422 #endif /* defined (BUILDSYM_H) */