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