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