Turn start_psymtab_common into a constructor
[deliverable/binutils-gdb.git] / gdb / dwarf2read.h
CommitLineData
cd4fb1b2
SM
1/* DWARF 2 debugging format support for GDB.
2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
cd4fb1b2
SM
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#ifndef DWARF2READ_H
21#define DWARF2READ_H
22
e4a62c65 23#include <unordered_map>
87d6a7aa 24#include "dwarf-index-cache.h"
cd4fb1b2
SM
25#include "filename-seen-cache.h"
26#include "gdb_obstack.h"
268a13a5 27#include "gdbsupport/hash_enum.h"
cd4fb1b2 28
3c3bb058
AB
29/* Hold 'maintenance (set|show) dwarf' commands. */
30extern struct cmd_list_element *set_dwarf_cmdlist;
31extern struct cmd_list_element *show_dwarf_cmdlist;
32
e86f08d2
CB
33extern bool dwarf_always_disassemble;
34
cd4fb1b2
SM
35/* A descriptor for dwarf sections.
36
37 S.ASECTION, SIZE are typically initialized when the objfile is first
38 scanned. BUFFER, READIN are filled in later when the section is read.
39 If the section contained compressed data then SIZE is updated to record
40 the uncompressed size of the section.
41
42 DWP file format V2 introduces a wrinkle that is easiest to handle by
43 creating the concept of virtual sections contained within a real section.
44 In DWP V2 the sections of the input DWO files are concatenated together
45 into one section, but section offsets are kept relative to the original
46 input section.
47 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
48 the real section this "virtual" section is contained in, and BUFFER,SIZE
49 describe the virtual section. */
50
51struct dwarf2_section_info
52{
53 union
54 {
55 /* If this is a real section, the bfd section. */
56 asection *section;
57 /* If this is a virtual section, pointer to the containing ("real")
58 section. */
59 struct dwarf2_section_info *containing_section;
60 } s;
61 /* Pointer to section data, only valid if readin. */
62 const gdb_byte *buffer;
63 /* The size of the section, real or virtual. */
64 bfd_size_type size;
65 /* If this is a virtual section, the offset in the real section.
66 Only valid if is_virtual. */
67 bfd_size_type virtual_offset;
68 /* True if we have tried to read this section. */
dc4ccb6f 69 bool readin;
cd4fb1b2
SM
70 /* True if this is a virtual section, False otherwise.
71 This specifies which of s.section and s.containing_section to use. */
dc4ccb6f 72 bool is_virtual;
cd4fb1b2
SM
73};
74
cd4fb1b2
SM
75/* Read the contents of the section INFO.
76 OBJFILE is the main object file, but not necessarily the file where
77 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
78 of the DWO file.
79 If the section is compressed, uncompress it before returning. */
80
81void dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info);
82
83struct tu_stats
84{
85 int nr_uniq_abbrev_tables;
86 int nr_symtabs;
87 int nr_symtab_sharers;
88 int nr_stmt_less_type_units;
89 int nr_all_type_units_reallocs;
90};
91
92struct dwarf2_debug_sections;
93struct mapped_index;
94struct mapped_debug_names;
ff4c9fec 95struct signatured_type;
e4a62c65
TV
96struct die_info;
97typedef struct die_info *die_info_ptr;
cd4fb1b2
SM
98
99/* Collection of data recorded per objfile.
100 This hangs off of dwarf2_objfile_data_key. */
101
5bfd760d 102struct dwarf2_per_objfile
cd4fb1b2
SM
103{
104 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
105 dwarf2 section names, or is NULL if the standard ELF names are
4b610737
TT
106 used. CAN_COPY is true for formats where symbol
107 interposition is possible and so symbol values must follow copy
108 relocation rules. */
cd4fb1b2 109 dwarf2_per_objfile (struct objfile *objfile,
4b610737
TT
110 const dwarf2_debug_sections *names,
111 bool can_copy);
cd4fb1b2
SM
112
113 ~dwarf2_per_objfile ();
114
115 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
116
ff4c9fec
SM
117 /* Return the CU/TU given its index.
118
119 This is intended for loops like:
120
121 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
122 + dwarf2_per_objfile->n_type_units); ++i)
123 {
124 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
125
126 ...;
127 }
128 */
129 dwarf2_per_cu_data *get_cutu (int index);
130
131 /* Return the CU given its index.
132 This differs from get_cutu in that it's for when you know INDEX refers to a
133 CU. */
134 dwarf2_per_cu_data *get_cu (int index);
135
136 /* Return the TU given its index.
137 This differs from get_cutu in that it's for when you know INDEX refers to a
138 TU. */
139 signatured_type *get_tu (int index);
140
cd4fb1b2
SM
141 /* Free all cached compilation units. */
142 void free_cached_comp_units ();
143private:
144 /* This function is mapped across the sections and remembers the
145 offset and size of each of the debugging sections we are
146 interested in. */
147 void locate_sections (bfd *abfd, asection *sectp,
148 const dwarf2_debug_sections &names);
149
150public:
151 dwarf2_section_info info {};
152 dwarf2_section_info abbrev {};
153 dwarf2_section_info line {};
154 dwarf2_section_info loc {};
155 dwarf2_section_info loclists {};
156 dwarf2_section_info macinfo {};
157 dwarf2_section_info macro {};
158 dwarf2_section_info str {};
18a8505e 159 dwarf2_section_info str_offsets {};
cd4fb1b2
SM
160 dwarf2_section_info line_str {};
161 dwarf2_section_info ranges {};
162 dwarf2_section_info rnglists {};
163 dwarf2_section_info addr {};
164 dwarf2_section_info frame {};
165 dwarf2_section_info eh_frame {};
166 dwarf2_section_info gdb_index {};
167 dwarf2_section_info debug_names {};
168 dwarf2_section_info debug_aranges {};
169
fd5866f6 170 std::vector<dwarf2_section_info> types;
cd4fb1b2
SM
171
172 /* Back link. */
173 struct objfile *objfile = NULL;
174
175 /* Table of all the compilation units. This is used to locate
176 the target compilation unit of a particular reference. */
b76e467d 177 std::vector<dwarf2_per_cu_data *> all_comp_units;
cd4fb1b2 178
b2bdb8cf
SM
179 /* The .debug_types-related CUs (TUs). */
180 std::vector<signatured_type *> all_type_units;
cd4fb1b2
SM
181
182 /* Table of struct type_unit_group objects.
183 The hash key is the DW_AT_stmt_list value. */
184 htab_t type_unit_groups {};
185
186 /* A table mapping .debug_types signatures to its signatured_type entry.
187 This is NULL if the .debug_types section hasn't been read in yet. */
188 htab_t signatured_types {};
189
190 /* Type unit statistics, to see how well the scaling improvements
191 are doing. */
192 struct tu_stats tu_stats {};
193
194 /* A chain of compilation units that are currently read in, so that
195 they can be freed later. */
196 dwarf2_per_cu_data *read_in_chain = NULL;
197
198 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
199 This is NULL if the table hasn't been allocated yet. */
51ac9db5 200 htab_up dwo_files;
cd4fb1b2
SM
201
202 /* True if we've checked for whether there is a DWP file. */
203 bool dwp_checked = false;
204
205 /* The DWP file if there is one, or NULL. */
400174b1 206 std::unique_ptr<struct dwp_file> dwp_file;
cd4fb1b2
SM
207
208 /* The shared '.dwz' file, if one exists. This is used when the
209 original data was compressed using 'dwz -m'. */
7ff8cb8c 210 std::unique_ptr<struct dwz_file> dwz_file;
cd4fb1b2 211
4b610737
TT
212 /* Whether copy relocations are supported by this object format. */
213 bool can_copy;
214
cd4fb1b2
SM
215 /* A flag indicating whether this objfile has a section loaded at a
216 VMA of 0. */
217 bool has_section_at_zero = false;
218
219 /* True if we are using the mapped index,
220 or we are faking it for OBJF_READNOW's sake. */
221 bool using_index = false;
222
223 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
3063847f 224 std::unique_ptr<mapped_index> index_table;
cd4fb1b2
SM
225
226 /* The mapped index, or NULL if .debug_names is missing or not being used. */
227 std::unique_ptr<mapped_debug_names> debug_names_table;
228
229 /* When using index_table, this keeps track of all quick_file_names entries.
230 TUs typically share line table entries with a CU, so we maintain a
231 separate table of all line table entries to support the sharing.
232 Note that while there can be way more TUs than CUs, we've already
233 sorted all the TUs into "type unit groups", grouped by their
234 DW_AT_stmt_list value. Therefore the only sharing done here is with a
235 CU and its associated TU group if there is one. */
236 htab_t quick_file_names_table {};
237
238 /* Set during partial symbol reading, to prevent queueing of full
239 symbols. */
240 bool reading_partial_symbols = false;
241
242 /* Table mapping type DIEs to their struct type *.
243 This is NULL if not allocated yet.
244 The mapping is done via (CU/TU + DIE offset) -> type. */
245 htab_t die_type_hash {};
246
247 /* The CUs we recently read. */
c5d0225d 248 std::vector<dwarf2_per_cu_data *> just_read_cus;
cd4fb1b2
SM
249
250 /* Table containing line_header indexed by offset and offset_in_dwz. */
251 htab_t line_header_hash {};
252
253 /* Table containing all filenames. This is an optional because the
254 table is lazily constructed on first access. */
255 gdb::optional<filename_seen_cache> filenames_cache;
87d6a7aa
SM
256
257 /* If we loaded the index from an external file, this contains the
258 resources associated to the open file, memory mapping, etc. */
259 std::unique_ptr<index_cache_resource> index_cache_res;
e4a62c65
TV
260
261 /* Mapping from abstract origin DIE to concrete DIEs that reference it as
262 DW_AT_abstract_origin. */
fad03f6e
TT
263 std::unordered_map<sect_offset, std::vector<sect_offset>,
264 gdb::hash_enum<sect_offset>>
e4a62c65 265 abstract_to_concrete;
cd4fb1b2
SM
266};
267
268/* Get the dwarf2_per_objfile associated to OBJFILE. */
269
270dwarf2_per_objfile *get_dwarf2_per_objfile (struct objfile *objfile);
271
272/* Persistent data held for a compilation unit, even when not
273 processing it. We put a pointer to this structure in the
274 read_symtab_private field of the psymtab. */
275
276struct dwarf2_per_cu_data
277{
278 /* The start offset and length of this compilation unit.
279 NOTE: Unlike comp_unit_head.length, this length includes
280 initial_length_size.
281 If the DIE refers to a DWO file, this is always of the original die,
282 not the DWO file. */
283 sect_offset sect_off;
284 unsigned int length;
285
286 /* DWARF standard version this data has been read from (such as 4 or 5). */
287 short dwarf_version;
288
289 /* Flag indicating this compilation unit will be read in before
290 any of the current compilation units are processed. */
291 unsigned int queued : 1;
292
293 /* This flag will be set when reading partial DIEs if we need to load
294 absolutely all DIEs for this compilation unit, instead of just the ones
295 we think are interesting. It gets set if we look for a DIE in the
296 hash table and don't find it. */
297 unsigned int load_all_dies : 1;
298
299 /* Non-zero if this CU is from .debug_types.
300 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
301 this is non-zero. */
302 unsigned int is_debug_types : 1;
303
304 /* Non-zero if this CU is from the .dwz file. */
305 unsigned int is_dwz : 1;
306
307 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
308 This flag is only valid if is_debug_types is true.
309 We can't read a CU directly from a DWO file: There are required
310 attributes in the stub. */
311 unsigned int reading_dwo_directly : 1;
312
313 /* Non-zero if the TU has been read.
314 This is used to assist the "Stay in DWO Optimization" for Fission:
315 When reading a DWO, it's faster to read TUs from the DWO instead of
316 fetching them from random other DWOs (due to comdat folding).
317 If the TU has already been read, the optimization is unnecessary
318 (and unwise - we don't want to change where gdb thinks the TU lives
319 "midflight").
320 This flag is only valid if is_debug_types is true. */
321 unsigned int tu_read : 1;
322
323 /* The section this CU/TU lives in.
324 If the DIE refers to a DWO file, this is always the original die,
325 not the DWO file. */
326 struct dwarf2_section_info *section;
327
328 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
329 of the CU cache it gets reset to NULL again. This is left as NULL for
330 dummy CUs (a CU header, but nothing else). */
331 struct dwarf2_cu *cu;
332
333 /* The corresponding dwarf2_per_objfile. */
334 struct dwarf2_per_objfile *dwarf2_per_objfile;
335
336 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
337 is active. Otherwise, the 'psymtab' field is active. */
338 union
339 {
340 /* The partial symbol table associated with this compilation unit,
341 or NULL for unread partial units. */
342 struct partial_symtab *psymtab;
343
344 /* Data needed by the "quick" functions. */
345 struct dwarf2_per_cu_quick_data *quick;
346 } v;
347
ae640021
AB
348 /* Return true of IMPORTED_SYMTABS is empty or not yet allocated. */
349 bool imported_symtabs_empty () const
350 {
351 return (imported_symtabs == nullptr || imported_symtabs->empty ());
352 }
353
354 /* Push P to the back of IMPORTED_SYMTABS, allocated IMPORTED_SYMTABS
355 first if required. */
356 void imported_symtabs_push (dwarf2_per_cu_data *p)
357 {
358 if (imported_symtabs == nullptr)
359 imported_symtabs = new std::vector <dwarf2_per_cu_data *>;
360 imported_symtabs->push_back (p);
361 }
362
363 /* Return the size of IMPORTED_SYMTABS if it is allocated, otherwise
364 return 0. */
365 size_t imported_symtabs_size () const
366 {
367 if (imported_symtabs == nullptr)
368 return 0;
369 return imported_symtabs->size ();
370 }
371
372 /* Delete IMPORTED_SYMTABS and set the pointer back to nullptr. */
373 void imported_symtabs_free ()
374 {
375 delete imported_symtabs;
376 imported_symtabs = nullptr;
377 }
378
cd4fb1b2
SM
379 /* The CUs we import using DW_TAG_imported_unit. This is filled in
380 while reading psymtabs, used to compute the psymtab dependencies,
381 and then cleared. Then it is filled in again while reading full
382 symbols, and only deleted when the objfile is destroyed.
383
384 This is also used to work around a difference between the way gold
385 generates .gdb_index version <=7 and the way gdb does. Arguably this
386 is a gold bug. For symbols coming from TUs, gold records in the index
387 the CU that includes the TU instead of the TU itself. This breaks
388 dw2_lookup_symbol: It assumes that if the index says symbol X lives
389 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
390 will find X. Alas TUs live in their own symtab, so after expanding CU Y
391 we need to look in TU Z to find X. Fortunately, this is akin to
392 DW_TAG_imported_unit, so we just use the same mechanism: For
393 .gdb_index version <=7 this also records the TUs that the CU referred
394 to. Concurrently with this change gdb was modified to emit version 8
395 indices so we only pay a price for gold generated indices.
ae640021
AB
396 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
397
398 This currently needs to be a public member due to how
399 dwarf2_per_cu_data is allocated and used. Ideally in future things
400 could be refactored to make this private. Until then please try to
401 avoid direct access to this member, and instead use the helper
402 functions above. */
403 std::vector <dwarf2_per_cu_data *> *imported_symtabs;
cd4fb1b2
SM
404};
405
406/* Entry in the signatured_types hash table. */
407
408struct signatured_type
409{
410 /* The "per_cu" object of this type.
411 This struct is used iff per_cu.is_debug_types.
412 N.B.: This is the first member so that it's easy to convert pointers
413 between them. */
414 struct dwarf2_per_cu_data per_cu;
415
416 /* The type's signature. */
417 ULONGEST signature;
418
419 /* Offset in the TU of the type's DIE, as read from the TU header.
420 If this TU is a DWO stub and the definition lives in a DWO file
421 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
422 cu_offset type_offset_in_tu;
423
424 /* Offset in the section of the type's DIE.
425 If the definition lives in a DWO file, this is the offset in the
426 .debug_types.dwo section.
427 The value is zero until the actual value is known.
428 Zero is otherwise not a valid section offset. */
429 sect_offset type_offset_in_section;
430
431 /* Type units are grouped by their DW_AT_stmt_list entry so that they
432 can share them. This points to the containing symtab. */
433 struct type_unit_group *type_unit_group;
434
435 /* The type.
436 The first time we encounter this type we fully read it in and install it
437 in the symbol tables. Subsequent times we only need the type. */
438 struct type *type;
439
440 /* Containing DWO unit.
441 This field is valid iff per_cu.reading_dwo_directly. */
442 struct dwo_unit *dwo_unit;
443};
444
587ee17b
TT
445ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
446
c4973306
SM
447/* This represents a '.dwz' file. */
448
449struct dwz_file
450{
451 dwz_file (gdb_bfd_ref_ptr &&bfd)
452 : dwz_bfd (std::move (bfd))
453 {
454 }
455
456 const char *filename () const
457 {
00f93c44 458 return bfd_get_filename (this->dwz_bfd.get ());
c4973306
SM
459 }
460
461 /* A dwz file can only contain a few sections. */
462 struct dwarf2_section_info abbrev {};
463 struct dwarf2_section_info info {};
464 struct dwarf2_section_info str {};
465 struct dwarf2_section_info line {};
466 struct dwarf2_section_info macro {};
467 struct dwarf2_section_info gdb_index {};
468 struct dwarf2_section_info debug_names {};
469
470 /* The dwz's BFD. */
471 gdb_bfd_ref_ptr dwz_bfd;
472
473 /* If we loaded the index from an external file, this contains the
474 resources associated to the open file, memory mapping, etc. */
475 std::unique_ptr<index_cache_resource> index_cache_res;
476};
477
478/* Open the separate '.dwz' debug file, if needed. Return NULL if
479 there is no .gnu_debugaltlink section in the file. Error if there
480 is such a section but the file cannot be found. */
481
482extern struct dwz_file *dwarf2_get_dwz_file
483 (struct dwarf2_per_objfile *dwarf2_per_objfile);
484
cd4fb1b2 485#endif /* DWARF2READ_H */
This page took 0.195524 seconds and 4 git commands to generate.