1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
24 #include <unordered_map>
25 #include "dwarf2/index-cache.h"
26 #include "dwarf2/section.h"
27 #include "filename-seen-cache.h"
28 #include "gdb_obstack.h"
29 #include "gdbsupport/hash_enum.h"
32 /* Hold 'maintenance (set|show) dwarf' commands. */
33 extern struct cmd_list_element
*set_dwarf_cmdlist
;
34 extern struct cmd_list_element
*show_dwarf_cmdlist
;
38 int nr_uniq_abbrev_tables
;
40 int nr_symtab_sharers
;
41 int nr_stmt_less_type_units
;
42 int nr_all_type_units_reallocs
;
45 struct dwarf2_debug_sections
;
46 struct dwarf2_per_cu_data
;
48 struct mapped_debug_names
;
49 struct signatured_type
;
51 /* One item on the queue of compilation units to read in full symbols
53 struct dwarf2_queue_item
55 dwarf2_queue_item (dwarf2_per_cu_data
*cu
, enum language lang
)
57 pretend_language (lang
)
61 ~dwarf2_queue_item ();
63 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_item
);
65 struct dwarf2_per_cu_data
*per_cu
;
66 enum language pretend_language
;
69 /* Some DWARF data can be shared across objfiles who share the same BFD,
70 this data is stored in this object.
72 Two dwarf2_per_objfile objects representing objfiles sharing the same BFD
73 will point to the same instance of dwarf2_per_bfd, unless the BFD requires
78 /* Construct a dwarf2_per_bfd for OBFD. NAMES points to the
79 dwarf2 section names, or is NULL if the standard ELF names are
80 used. CAN_COPY is true for formats where symbol
81 interposition is possible and so symbol values must follow copy
83 dwarf2_per_bfd (bfd
*obfd
, const dwarf2_debug_sections
*names
, bool can_copy
);
87 DISABLE_COPY_AND_ASSIGN (dwarf2_per_bfd
);
89 /* Return the CU/TU given its index.
91 This is intended for loops like:
93 for (i = 0; i < (dwarf2_per_bfd->n_comp_units
94 + dwarf2_per_bfd->n_type_units); ++i)
96 dwarf2_per_cu_data *per_cu = dwarf2_per_bfd->get_cutu (i);
101 dwarf2_per_cu_data
*get_cutu (int index
);
103 /* Return the CU given its index.
104 This differs from get_cutu in that it's for when you know INDEX refers to a
106 dwarf2_per_cu_data
*get_cu (int index
);
108 /* Return the TU given its index.
109 This differs from get_cutu in that it's for when you know INDEX refers to a
111 signatured_type
*get_tu (int index
);
113 /* Free all cached compilation units. */
114 void free_cached_comp_units ();
116 /* A convenience function to allocate a dwarf2_per_cu_data. The
117 returned object has its "index" field set properly. The object
118 is allocated on the dwarf2_per_bfd obstack. */
119 dwarf2_per_cu_data
*allocate_per_cu ();
121 /* A convenience function to allocate a signatured_type. The
122 returned object has its "index" field set properly. The object
123 is allocated on the dwarf2_per_bfd obstack. */
124 signatured_type
*allocate_signatured_type ();
127 /* This function is mapped across the sections and remembers the
128 offset and size of each of the debugging sections we are
130 void locate_sections (bfd
*abfd
, asection
*sectp
,
131 const dwarf2_debug_sections
&names
);
134 /* Objects that can be shared across objfiles are stored in this
135 obstack or on the psymtab obstack, while objects that are
136 objfile-specific are stored on the objfile obstack. */
137 auto_obstack obstack
;
139 dwarf2_section_info info
{};
140 dwarf2_section_info abbrev
{};
141 dwarf2_section_info line
{};
142 dwarf2_section_info loc
{};
143 dwarf2_section_info loclists
{};
144 dwarf2_section_info macinfo
{};
145 dwarf2_section_info macro
{};
146 dwarf2_section_info str
{};
147 dwarf2_section_info str_offsets
{};
148 dwarf2_section_info line_str
{};
149 dwarf2_section_info ranges
{};
150 dwarf2_section_info rnglists
{};
151 dwarf2_section_info addr
{};
152 dwarf2_section_info frame
{};
153 dwarf2_section_info eh_frame
{};
154 dwarf2_section_info gdb_index
{};
155 dwarf2_section_info debug_names
{};
156 dwarf2_section_info debug_aranges
{};
158 std::vector
<dwarf2_section_info
> types
;
160 /* Table of all the compilation units. This is used to locate
161 the target compilation unit of a particular reference. */
162 std::vector
<dwarf2_per_cu_data
*> all_comp_units
;
164 /* The .debug_types-related CUs (TUs). */
165 std::vector
<signatured_type
*> all_type_units
;
167 /* Table of struct type_unit_group objects.
168 The hash key is the DW_AT_stmt_list value. */
169 htab_up type_unit_groups
;
171 /* A table mapping .debug_types signatures to its signatured_type entry.
172 This is NULL if the .debug_types section hasn't been read in yet. */
173 htab_up signatured_types
;
175 /* Type unit statistics, to see how well the scaling improvements
177 struct tu_stats tu_stats
{};
179 /* A chain of compilation units that are currently read in, so that
180 they can be freed later. */
181 dwarf2_per_cu_data
*read_in_chain
= NULL
;
183 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
184 This is NULL if the table hasn't been allocated yet. */
187 /* True if we've checked for whether there is a DWP file. */
188 bool dwp_checked
= false;
190 /* The DWP file if there is one, or NULL. */
191 std::unique_ptr
<struct dwp_file
> dwp_file
;
193 /* The shared '.dwz' file, if one exists. This is used when the
194 original data was compressed using 'dwz -m'. */
195 std::unique_ptr
<struct dwz_file
> dwz_file
;
197 /* Whether copy relocations are supported by this object format. */
200 /* A flag indicating whether this objfile has a section loaded at a
202 bool has_section_at_zero
= false;
204 /* True if we are using the mapped index,
205 or we are faking it for OBJF_READNOW's sake. */
206 bool using_index
= false;
208 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
209 std::unique_ptr
<mapped_index
> index_table
;
211 /* The mapped index, or NULL if .debug_names is missing or not being used. */
212 std::unique_ptr
<mapped_debug_names
> debug_names_table
;
214 /* When using index_table, this keeps track of all quick_file_names entries.
215 TUs typically share line table entries with a CU, so we maintain a
216 separate table of all line table entries to support the sharing.
217 Note that while there can be way more TUs than CUs, we've already
218 sorted all the TUs into "type unit groups", grouped by their
219 DW_AT_stmt_list value. Therefore the only sharing done here is with a
220 CU and its associated TU group if there is one. */
221 htab_up quick_file_names_table
;
223 /* Set during partial symbol reading, to prevent queueing of full
225 bool reading_partial_symbols
= false;
227 /* Table mapping type DIEs to their struct type *.
228 This is NULL if not allocated yet.
229 The mapping is done via (CU/TU + DIE offset) -> type. */
230 htab_up die_type_hash
;
232 /* The CUs we recently read. */
233 std::vector
<dwarf2_per_cu_data
*> just_read_cus
;
235 /* Table containing line_header indexed by offset and offset_in_dwz. */
236 htab_up line_header_hash
;
238 /* Table containing all filenames. This is an optional because the
239 table is lazily constructed on first access. */
240 gdb::optional
<filename_seen_cache
> filenames_cache
;
242 /* If we loaded the index from an external file, this contains the
243 resources associated to the open file, memory mapping, etc. */
244 std::unique_ptr
<index_cache_resource
> index_cache_res
;
246 /* Mapping from abstract origin DIE to concrete DIEs that reference it as
247 DW_AT_abstract_origin. */
248 std::unordered_map
<sect_offset
, std::vector
<sect_offset
>,
249 gdb::hash_enum
<sect_offset
>>
250 abstract_to_concrete
;
252 /* CUs that are queued to be read. */
253 std::queue
<dwarf2_queue_item
> queue
;
257 /* The total number of per_cu and signatured_type objects that have
258 been created so far for this reader. */
259 size_t m_num_psymtabs
= 0;
262 /* Collection of data recorded per objfile.
263 This hangs off of dwarf2_objfile_data_key.
265 Some DWARF data cannot (currently) be shared across objfiles. Such
266 data is stored in this object. */
268 struct dwarf2_per_objfile
270 dwarf2_per_objfile (struct objfile
*objfile
, dwarf2_per_bfd
*per_bfd
)
271 : objfile (objfile
), per_bfd (per_bfd
)
274 /* Return pointer to string at .debug_line_str offset as read from BUF.
275 BUF is assumed to be in a compilation unit described by CU_HEADER.
276 Return *BYTES_READ_PTR count of bytes read from BUF. */
277 const char *read_line_string (const gdb_byte
*buf
,
278 const struct comp_unit_head
*cu_header
,
279 unsigned int *bytes_read_ptr
);
282 struct objfile
*objfile
;
284 /* Pointer to the data that is (possibly) shared between this objfile and
285 other objfiles backed by the same BFD. */
286 struct dwarf2_per_bfd
*per_bfd
;
289 /* Get the dwarf2_per_objfile associated to OBJFILE. */
291 dwarf2_per_objfile
*get_dwarf2_per_objfile (struct objfile
*objfile
);
293 /* A partial symtab specialized for DWARF. */
294 struct dwarf2_psymtab
: public standard_psymtab
296 dwarf2_psymtab (const char *filename
, struct objfile
*objfile
,
297 dwarf2_per_cu_data
*per_cu
)
298 : standard_psymtab (filename
, objfile
, 0),
303 void read_symtab (struct objfile
*) override
;
304 void expand_psymtab (struct objfile
*) override
;
306 struct dwarf2_per_cu_data
*per_cu_data
;
309 /* Persistent data held for a compilation unit, even when not
310 processing it. We put a pointer to this structure in the
313 struct dwarf2_per_cu_data
315 /* The start offset and length of this compilation unit.
316 NOTE: Unlike comp_unit_head.length, this length includes
318 If the DIE refers to a DWO file, this is always of the original die,
320 sect_offset sect_off
;
323 /* DWARF standard version this data has been read from (such as 4 or 5). */
326 /* Flag indicating this compilation unit will be read in before
327 any of the current compilation units are processed. */
328 unsigned int queued
: 1;
330 /* This flag will be set when reading partial DIEs if we need to load
331 absolutely all DIEs for this compilation unit, instead of just the ones
332 we think are interesting. It gets set if we look for a DIE in the
333 hash table and don't find it. */
334 unsigned int load_all_dies
: 1;
336 /* Non-zero if this CU is from .debug_types.
337 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
339 unsigned int is_debug_types
: 1;
341 /* Non-zero if this CU is from the .dwz file. */
342 unsigned int is_dwz
: 1;
344 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
345 This flag is only valid if is_debug_types is true.
346 We can't read a CU directly from a DWO file: There are required
347 attributes in the stub. */
348 unsigned int reading_dwo_directly
: 1;
350 /* Non-zero if the TU has been read.
351 This is used to assist the "Stay in DWO Optimization" for Fission:
352 When reading a DWO, it's faster to read TUs from the DWO instead of
353 fetching them from random other DWOs (due to comdat folding).
354 If the TU has already been read, the optimization is unnecessary
355 (and unwise - we don't want to change where gdb thinks the TU lives
357 This flag is only valid if is_debug_types is true. */
358 unsigned int tu_read
: 1;
360 /* Our index in the unshared "symtabs" vector. */
363 /* The section this CU/TU lives in.
364 If the DIE refers to a DWO file, this is always the original die,
366 struct dwarf2_section_info
*section
;
368 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
369 of the CU cache it gets reset to NULL again. This is left as NULL for
370 dummy CUs (a CU header, but nothing else). */
371 struct dwarf2_cu
*cu
;
373 /* The unit type of this CU. */
374 enum dwarf_unit_type unit_type
;
376 /* The language of this CU. */
379 /* The corresponding dwarf2_per_objfile. */
380 struct dwarf2_per_objfile
*dwarf2_per_objfile
;
382 /* When dwarf2_per_bfd::using_index is true, the 'quick' field
383 is active. Otherwise, the 'psymtab' field is active. */
386 /* The partial symbol table associated with this compilation unit,
387 or NULL for unread partial units. */
388 dwarf2_psymtab
*psymtab
;
390 /* Data needed by the "quick" functions. */
391 struct dwarf2_per_cu_quick_data
*quick
;
394 /* The CUs we import using DW_TAG_imported_unit. This is filled in
395 while reading psymtabs, used to compute the psymtab dependencies,
396 and then cleared. Then it is filled in again while reading full
397 symbols, and only deleted when the objfile is destroyed.
399 This is also used to work around a difference between the way gold
400 generates .gdb_index version <=7 and the way gdb does. Arguably this
401 is a gold bug. For symbols coming from TUs, gold records in the index
402 the CU that includes the TU instead of the TU itself. This breaks
403 dw2_lookup_symbol: It assumes that if the index says symbol X lives
404 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
405 will find X. Alas TUs live in their own symtab, so after expanding CU Y
406 we need to look in TU Z to find X. Fortunately, this is akin to
407 DW_TAG_imported_unit, so we just use the same mechanism: For
408 .gdb_index version <=7 this also records the TUs that the CU referred
409 to. Concurrently with this change gdb was modified to emit version 8
410 indices so we only pay a price for gold generated indices.
411 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
413 This currently needs to be a public member due to how
414 dwarf2_per_cu_data is allocated and used. Ideally in future things
415 could be refactored to make this private. Until then please try to
416 avoid direct access to this member, and instead use the helper
418 std::vector
<dwarf2_per_cu_data
*> *imported_symtabs
;
420 /* Return true of IMPORTED_SYMTABS is empty or not yet allocated. */
421 bool imported_symtabs_empty () const
423 return (imported_symtabs
== nullptr || imported_symtabs
->empty ());
426 /* Push P to the back of IMPORTED_SYMTABS, allocated IMPORTED_SYMTABS
427 first if required. */
428 void imported_symtabs_push (dwarf2_per_cu_data
*p
)
430 if (imported_symtabs
== nullptr)
431 imported_symtabs
= new std::vector
<dwarf2_per_cu_data
*>;
432 imported_symtabs
->push_back (p
);
435 /* Return the size of IMPORTED_SYMTABS if it is allocated, otherwise
437 size_t imported_symtabs_size () const
439 if (imported_symtabs
== nullptr)
441 return imported_symtabs
->size ();
444 /* Delete IMPORTED_SYMTABS and set the pointer back to nullptr. */
445 void imported_symtabs_free ()
447 delete imported_symtabs
;
448 imported_symtabs
= nullptr;
451 /* Return the OBJFILE associated with this compilation unit. If
452 this compilation unit came from a separate debuginfo file, then
453 the master objfile is returned. */
454 struct objfile
*objfile () const;
456 /* Return the address size given in the compilation unit header for
458 int addr_size () const;
460 /* Return the offset size given in the compilation unit header for
462 int offset_size () const;
464 /* Return the DW_FORM_ref_addr size given in the compilation unit
465 header for this CU. */
466 int ref_addr_size () const;
468 /* Return the text offset of the CU. The returned offset comes from
469 this CU's objfile. If this objfile came from a separate
470 debuginfo file, then the offset may be different from the
471 corresponding offset in the parent objfile. */
472 CORE_ADDR
text_offset () const;
474 /* Return a type that is a generic pointer type, the size of which
475 matches the address size given in the compilation unit header for
477 struct type
*addr_type () const;
479 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
480 UNSIGNED_P controls if the integer is unsigned or not. */
481 struct type
*int_type (int size_in_bytes
, bool unsigned_p
) const;
483 /* Find an integer type the same size as the address size given in
484 the compilation unit header for this CU. UNSIGNED_P controls if
485 the integer is unsigned or not. */
486 struct type
*addr_sized_int_type (bool unsigned_p
) const;
488 /* Return DWARF version number of this CU. */
489 short version () const
491 return dwarf_version
;
494 /* A type unit group has a per_cu object that is recognized by
495 having no section. */
496 bool type_unit_group_p () const
498 return section
== nullptr;
502 /* Entry in the signatured_types hash table. */
504 struct signatured_type
506 /* The "per_cu" object of this type.
507 This struct is used iff per_cu.is_debug_types.
508 N.B.: This is the first member so that it's easy to convert pointers
510 struct dwarf2_per_cu_data per_cu
;
512 /* The type's signature. */
515 /* Offset in the TU of the type's DIE, as read from the TU header.
516 If this TU is a DWO stub and the definition lives in a DWO file
517 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
518 cu_offset type_offset_in_tu
;
520 /* Offset in the section of the type's DIE.
521 If the definition lives in a DWO file, this is the offset in the
522 .debug_types.dwo section.
523 The value is zero until the actual value is known.
524 Zero is otherwise not a valid section offset. */
525 sect_offset type_offset_in_section
;
527 /* Type units are grouped by their DW_AT_stmt_list entry so that they
528 can share them. This points to the containing symtab. */
529 struct type_unit_group
*type_unit_group
;
532 The first time we encounter this type we fully read it in and install it
533 in the symbol tables. Subsequent times we only need the type. */
536 /* Containing DWO unit.
537 This field is valid iff per_cu.reading_dwo_directly. */
538 struct dwo_unit
*dwo_unit
;
541 /* Open the separate '.dwz' debug file, if needed. Return NULL if
542 there is no .gnu_debugaltlink section in the file. Error if there
543 is such a section but the file cannot be found. */
545 extern struct dwz_file
*dwarf2_get_dwz_file
546 (struct dwarf2_per_objfile
*dwarf2_per_objfile
);
548 /* Return the type of the DIE at DIE_OFFSET in the CU named by
551 struct type
*dwarf2_get_die_type (cu_offset die_offset
,
552 struct dwarf2_per_cu_data
*per_cu
);
554 /* Given an index in .debug_addr, fetch the value.
555 NOTE: This can be called during dwarf expression evaluation,
556 long after the debug information has been read, and thus per_cu->cu
557 may no longer exist. */
559 CORE_ADDR
dwarf2_read_addr_index (dwarf2_per_cu_data
*per_cu
,
560 unsigned int addr_index
);
562 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
563 Returned value is intended for DW_OP_call*. Returned
564 dwarf2_locexpr_baton->data has lifetime of
565 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
567 struct dwarf2_locexpr_baton dwarf2_fetch_die_loc_sect_off
568 (sect_offset sect_off
, dwarf2_per_cu_data
*per_cu
,
569 CORE_ADDR (*get_frame_pc
) (void *baton
),
570 void *baton
, bool resolve_abstract_p
= false);
572 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
575 struct dwarf2_locexpr_baton dwarf2_fetch_die_loc_cu_off
576 (cu_offset offset_in_cu
, dwarf2_per_cu_data
*per_cu
,
577 CORE_ADDR (*get_frame_pc
) (void *baton
),
580 /* If the DIE at SECT_OFF in PER_CU has a DW_AT_const_value, return a
581 pointer to the constant bytes and set LEN to the length of the
582 data. If memory is needed, allocate it on OBSTACK. If the DIE
583 does not have a DW_AT_const_value, return NULL. */
585 extern const gdb_byte
*dwarf2_fetch_constant_bytes
586 (sect_offset sect_off
, dwarf2_per_cu_data
*per_cu
, obstack
*obstack
,
589 /* Return the type of the die at SECT_OFF in PER_CU. Return NULL if no
590 valid type for this die is found. */
592 struct type
*dwarf2_fetch_die_type_sect_off
593 (sect_offset sect_off
, dwarf2_per_cu_data
*per_cu
);
595 /* When non-zero, dump line number entries as they are read in. */
596 extern unsigned int dwarf_line_debug
;
598 #endif /* DWARF2READ_H */