1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2022 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/comp-unit-head.h"
26 #include "dwarf2/cooked-index.h"
27 #include "dwarf2/file-and-dir.h"
28 #include "dwarf2/index-cache.h"
29 #include "dwarf2/mapped-index.h"
30 #include "dwarf2/section.h"
31 #include "filename-seen-cache.h"
32 #include "gdbsupport/gdb_obstack.h"
33 #include "gdbsupport/hash_enum.h"
34 #include "gdbsupport/function-view.h"
36 /* Hold 'maintenance (set|show) dwarf' commands. */
37 extern struct cmd_list_element
*set_dwarf_cmdlist
;
38 extern struct cmd_list_element
*show_dwarf_cmdlist
;
42 int nr_uniq_abbrev_tables
;
44 int nr_symtab_sharers
;
45 int nr_stmt_less_type_units
;
46 int nr_all_type_units_reallocs
;
51 struct dwarf2_debug_sections
;
52 struct dwarf2_per_bfd
;
53 struct dwarf2_per_cu_data
;
55 struct mapped_debug_names
;
56 struct signatured_type
;
57 struct type_unit_group
;
59 /* One item on the queue of compilation units to read in full symbols
61 struct dwarf2_queue_item
63 dwarf2_queue_item (dwarf2_per_cu_data
*cu
, dwarf2_per_objfile
*per_objfile
,
66 per_objfile (per_objfile
),
67 pretend_language (lang
)
71 ~dwarf2_queue_item ();
73 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_item
);
75 dwarf2_per_cu_data
*per_cu
;
76 dwarf2_per_objfile
*per_objfile
;
77 enum language pretend_language
;
80 /* A deleter for dwarf2_per_cu_data that knows to downcast to
81 signatured_type as appropriate. This approach lets us avoid a
82 virtual destructor, which saves a bit of space. */
84 struct dwarf2_per_cu_data_deleter
86 void operator() (dwarf2_per_cu_data
*data
);
89 /* A specialization of unique_ptr for dwarf2_per_cu_data and
91 typedef std::unique_ptr
<dwarf2_per_cu_data
, dwarf2_per_cu_data_deleter
>
92 dwarf2_per_cu_data_up
;
94 /* Persistent data held for a compilation unit, even when not
95 processing it. We put a pointer to this structure in the
98 struct dwarf2_per_cu_data
100 dwarf2_per_cu_data ()
102 is_debug_types (false),
104 reading_dwo_directly (false),
106 m_header_read_in (false),
107 addresses_seen (false),
111 lang (language_unknown
),
116 /* The start offset and length of this compilation unit.
117 NOTE: Unlike comp_unit_head.length, this length includes
119 If the DIE refers to a DWO file, this is always of the original die,
121 sect_offset sect_off
{};
122 unsigned int length
= 0;
124 /* DWARF standard version this data has been read from (such as 4 or 5). */
125 unsigned char dwarf_version
= 0;
127 /* Flag indicating this compilation unit will be read in before
128 any of the current compilation units are processed. */
129 unsigned int queued
: 1;
131 /* Non-zero if this CU is from .debug_types.
132 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
134 unsigned int is_debug_types
: 1;
136 /* Non-zero if this CU is from the .dwz file. */
137 unsigned int is_dwz
: 1;
139 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
140 This flag is only valid if is_debug_types is true.
141 We can't read a CU directly from a DWO file: There are required
142 attributes in the stub. */
143 unsigned int reading_dwo_directly
: 1;
145 /* Non-zero if the TU has been read.
146 This is used to assist the "Stay in DWO Optimization" for Fission:
147 When reading a DWO, it's faster to read TUs from the DWO instead of
148 fetching them from random other DWOs (due to comdat folding).
149 If the TU has already been read, the optimization is unnecessary
150 (and unwise - we don't want to change where gdb thinks the TU lives
152 This flag is only valid if is_debug_types is true. */
153 unsigned int tu_read
: 1;
155 /* True if HEADER has been read in.
157 Don't access this field directly. It should be private, but we can't make
158 it private at the moment. */
159 mutable bool m_header_read_in
: 1;
161 /* If addresses have been read for this CU (usually from
162 .debug_aranges), then this flag is set. */
163 bool addresses_seen
: 1;
165 /* A temporary mark bit used when iterating over all CUs in
166 expand_symtabs_matching. */
167 unsigned int mark
: 1;
169 /* True if we've tried to read the file table. There will be no
170 point in trying to read it again next time. */
173 /* The unit type of this CU. */
174 ENUM_BITFIELD (dwarf_unit_type
) unit_type
: 8;
176 /* The language of this CU. */
177 ENUM_BITFIELD (language
) lang
: LANGUAGE_BITS
;
179 /* True if this CU has been scanned by the indexer; false if
181 std::atomic
<bool> scanned
;
183 /* Our index in the unshared "symtabs" vector. */
186 /* The section this CU/TU lives in.
187 If the DIE refers to a DWO file, this is always the original die,
189 struct dwarf2_section_info
*section
= nullptr;
191 /* Backlink to the owner of this. */
192 dwarf2_per_bfd
*per_bfd
= nullptr;
194 /* DWARF header of this CU. Note that dwarf2_cu reads its own version of the
195 header, which may differ from this one, since it may pass rcuh_kind::TYPE
196 to read_comp_unit_head, whereas for dwarf2_per_cu_data we always pass
199 Don't access this field directly, use the get_header method instead. It
200 should be private, but we can't make it private at the moment. */
201 mutable comp_unit_head m_header
{};
203 /* The file and directory for this CU. This is cached so that we
204 don't need to re-examine the DWO in some situations. This may be
205 nullptr, depending on the CU; for example a partial unit won't
207 std::unique_ptr
<file_and_directory
> fnd
;
209 /* The file table. This can be NULL if there was no file table
210 or it's currently not read in.
211 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
212 struct quick_file_names
*file_names
= nullptr;
214 /* The CUs we import using DW_TAG_imported_unit. This is filled in
215 while reading psymtabs, used to compute the psymtab dependencies,
216 and then cleared. Then it is filled in again while reading full
217 symbols, and only deleted when the objfile is destroyed.
219 This is also used to work around a difference between the way gold
220 generates .gdb_index version <=7 and the way gdb does. Arguably this
221 is a gold bug. For symbols coming from TUs, gold records in the index
222 the CU that includes the TU instead of the TU itself. This breaks
223 dw2_lookup_symbol: It assumes that if the index says symbol X lives
224 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
225 will find X. Alas TUs live in their own symtab, so after expanding CU Y
226 we need to look in TU Z to find X. Fortunately, this is akin to
227 DW_TAG_imported_unit, so we just use the same mechanism: For
228 .gdb_index version <=7 this also records the TUs that the CU referred
229 to. Concurrently with this change gdb was modified to emit version 8
230 indices so we only pay a price for gold generated indices.
231 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
233 This currently needs to be a public member due to how
234 dwarf2_per_cu_data is allocated and used. Ideally in future things
235 could be refactored to make this private. Until then please try to
236 avoid direct access to this member, and instead use the helper
238 std::vector
<dwarf2_per_cu_data
*> *imported_symtabs
= nullptr;
240 /* Return true of IMPORTED_SYMTABS is empty or not yet allocated. */
241 bool imported_symtabs_empty () const
243 return (imported_symtabs
== nullptr || imported_symtabs
->empty ());
246 /* Push P to the back of IMPORTED_SYMTABS, allocated IMPORTED_SYMTABS
247 first if required. */
248 void imported_symtabs_push (dwarf2_per_cu_data
*p
)
250 if (imported_symtabs
== nullptr)
251 imported_symtabs
= new std::vector
<dwarf2_per_cu_data
*>;
252 imported_symtabs
->push_back (p
);
255 /* Return the size of IMPORTED_SYMTABS if it is allocated, otherwise
257 size_t imported_symtabs_size () const
259 if (imported_symtabs
== nullptr)
261 return imported_symtabs
->size ();
264 /* Delete IMPORTED_SYMTABS and set the pointer back to nullptr. */
265 void imported_symtabs_free ()
267 delete imported_symtabs
;
268 imported_symtabs
= nullptr;
271 /* Get the header of this per_cu, reading it if necessary. */
272 const comp_unit_head
*get_header () const;
274 /* Return the address size given in the compilation unit header for
276 int addr_size () const;
278 /* Return the offset size given in the compilation unit header for
280 int offset_size () const;
282 /* Return the DW_FORM_ref_addr size given in the compilation unit
283 header for this CU. */
284 int ref_addr_size () const;
286 /* Return DWARF version number of this CU. */
287 short version () const
289 return dwarf_version
;
292 /* Free any cached file names. */
293 void free_cached_file_names ();
296 /* Entry in the signatured_types hash table. */
298 struct signatured_type
: public dwarf2_per_cu_data
300 signatured_type (ULONGEST signature
)
301 : signature (signature
)
304 /* The type's signature. */
307 /* Offset in the TU of the type's DIE, as read from the TU header.
308 If this TU is a DWO stub and the definition lives in a DWO file
309 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
310 cu_offset type_offset_in_tu
{};
312 /* Offset in the section of the type's DIE.
313 If the definition lives in a DWO file, this is the offset in the
314 .debug_types.dwo section.
315 The value is zero until the actual value is known.
316 Zero is otherwise not a valid section offset. */
317 sect_offset type_offset_in_section
{};
319 /* Type units are grouped by their DW_AT_stmt_list entry so that they
320 can share them. This points to the containing symtab. */
321 struct type_unit_group
*type_unit_group
= nullptr;
323 /* Containing DWO unit.
324 This field is valid iff per_cu.reading_dwo_directly. */
325 struct dwo_unit
*dwo_unit
= nullptr;
328 using signatured_type_up
= std::unique_ptr
<signatured_type
>;
330 /* Some DWARF data can be shared across objfiles who share the same BFD,
331 this data is stored in this object.
333 Two dwarf2_per_objfile objects representing objfiles sharing the same BFD
334 will point to the same instance of dwarf2_per_bfd, unless the BFD requires
337 struct dwarf2_per_bfd
339 /* Construct a dwarf2_per_bfd for OBFD. NAMES points to the
340 dwarf2 section names, or is NULL if the standard ELF names are
341 used. CAN_COPY is true for formats where symbol
342 interposition is possible and so symbol values must follow copy
344 dwarf2_per_bfd (bfd
*obfd
, const dwarf2_debug_sections
*names
, bool can_copy
);
348 DISABLE_COPY_AND_ASSIGN (dwarf2_per_bfd
);
350 /* Return the CU given its index. */
351 dwarf2_per_cu_data
*get_cu (int index
) const
353 return this->all_comp_units
[index
].get ();
356 /* A convenience function to allocate a dwarf2_per_cu_data. The
357 returned object has its "index" field set properly. The object
358 is allocated on the dwarf2_per_bfd obstack. */
359 dwarf2_per_cu_data_up
allocate_per_cu ();
361 /* A convenience function to allocate a signatured_type. The
362 returned object has its "index" field set properly. The object
363 is allocated on the dwarf2_per_bfd obstack. */
364 signatured_type_up
allocate_signatured_type (ULONGEST signature
);
366 /* Map all the DWARF section data needed when scanning
368 void map_info_sections (struct objfile
*objfile
);
371 /* This function is mapped across the sections and remembers the
372 offset and size of each of the debugging sections we are
374 void locate_sections (bfd
*abfd
, asection
*sectp
,
375 const dwarf2_debug_sections
&names
);
378 /* The corresponding BFD. */
381 /* Objects that can be shared across objfiles may be stored in this
382 obstack, while objects that are objfile-specific are stored on
383 the objfile obstack. */
384 auto_obstack obstack
;
386 dwarf2_section_info info
{};
387 dwarf2_section_info abbrev
{};
388 dwarf2_section_info line
{};
389 dwarf2_section_info loc
{};
390 dwarf2_section_info loclists
{};
391 dwarf2_section_info macinfo
{};
392 dwarf2_section_info macro
{};
393 dwarf2_section_info str
{};
394 dwarf2_section_info str_offsets
{};
395 dwarf2_section_info line_str
{};
396 dwarf2_section_info ranges
{};
397 dwarf2_section_info rnglists
{};
398 dwarf2_section_info addr
{};
399 dwarf2_section_info frame
{};
400 dwarf2_section_info eh_frame
{};
401 dwarf2_section_info gdb_index
{};
402 dwarf2_section_info debug_names
{};
403 dwarf2_section_info debug_aranges
{};
405 std::vector
<dwarf2_section_info
> types
;
407 /* Table of all the compilation units. This is used to locate
408 the target compilation unit of a particular reference. */
409 std::vector
<dwarf2_per_cu_data_up
> all_comp_units
;
411 /* Table of struct type_unit_group objects.
412 The hash key is the DW_AT_stmt_list value. */
413 htab_up type_unit_groups
;
415 /* A table mapping .debug_types signatures to its signatured_type entry.
416 This is NULL if the .debug_types section hasn't been read in yet. */
417 htab_up signatured_types
;
419 /* Type unit statistics, to see how well the scaling improvements
421 struct tu_stats tu_stats
{};
423 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
424 This is NULL if the table hasn't been allocated yet. */
427 /* True if we've checked for whether there is a DWP file. */
428 bool dwp_checked
= false;
430 /* The DWP file if there is one, or NULL. */
431 std::unique_ptr
<struct dwp_file
> dwp_file
;
433 /* The shared '.dwz' file, if one exists. This is used when the
434 original data was compressed using 'dwz -m'. */
435 std::unique_ptr
<struct dwz_file
> dwz_file
;
437 /* Whether copy relocations are supported by this object format. */
440 /* A flag indicating whether this objfile has a section loaded at a
442 bool has_section_at_zero
= false;
444 /* The mapped index, or NULL in the readnow case. */
445 std::unique_ptr
<dwarf_scanner_base
> index_table
;
447 /* When using index_table, this keeps track of all quick_file_names entries.
448 TUs typically share line table entries with a CU, so we maintain a
449 separate table of all line table entries to support the sharing.
450 Note that while there can be way more TUs than CUs, we've already
451 sorted all the TUs into "type unit groups", grouped by their
452 DW_AT_stmt_list value. Therefore the only sharing done here is with a
453 CU and its associated TU group if there is one. */
454 htab_up quick_file_names_table
;
456 /* The CUs we recently read. */
457 std::vector
<dwarf2_per_cu_data
*> just_read_cus
;
459 /* If we loaded the index from an external file, this contains the
460 resources associated to the open file, memory mapping, etc. */
461 std::unique_ptr
<index_cache_resource
> index_cache_res
;
463 /* Mapping from abstract origin DIE to concrete DIEs that reference it as
464 DW_AT_abstract_origin. */
465 std::unordered_map
<sect_offset
, std::vector
<sect_offset
>,
466 gdb::hash_enum
<sect_offset
>>
467 abstract_to_concrete
;
469 /* CUs that are queued to be read. */
470 gdb::optional
<std::queue
<dwarf2_queue_item
>> queue
;
472 /* The address map that is used by the DWARF index code. */
473 struct addrmap
*index_addrmap
= nullptr;
476 /* This is the per-objfile data associated with a type_unit_group. */
478 struct type_unit_group_unshareable
480 /* The compunit symtab.
481 Type units in a group needn't all be defined in the same source file,
482 so we create an essentially anonymous symtab as the compunit symtab. */
483 struct compunit_symtab
*compunit_symtab
= nullptr;
485 /* The number of symtabs from the line header.
486 The value here must match line_header.num_file_names. */
487 unsigned int num_symtabs
= 0;
489 /* The symbol tables for this TU (obtained from the files listed in
491 WARNING: The order of entries here must match the order of entries
492 in the line header. After the first TU using this type_unit_group, the
493 line header for the subsequent TUs is recreated from this. This is done
494 because we need to use the same symtabs for each TU using the same
495 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
496 there's no guarantee the line header doesn't have duplicate entries. */
497 struct symtab
**symtabs
= nullptr;
500 /* Collection of data recorded per objfile.
501 This hangs off of dwarf2_objfile_data_key.
503 Some DWARF data cannot (currently) be shared across objfiles. Such
504 data is stored in this object. */
506 struct dwarf2_per_objfile
508 dwarf2_per_objfile (struct objfile
*objfile
, dwarf2_per_bfd
*per_bfd
)
509 : objfile (objfile
), per_bfd (per_bfd
)
512 ~dwarf2_per_objfile ();
514 /* Return pointer to string at .debug_line_str offset as read from BUF.
515 BUF is assumed to be in a compilation unit described by CU_HEADER.
516 Return *BYTES_READ_PTR count of bytes read from BUF. */
517 const char *read_line_string (const gdb_byte
*buf
,
518 const struct comp_unit_head
*cu_header
,
519 unsigned int *bytes_read_ptr
);
521 /* Return pointer to string at .debug_line_str offset as read from BUF.
522 The offset_size is OFFSET_SIZE. */
523 const char *read_line_string (const gdb_byte
*buf
,
524 unsigned int offset_size
);
526 /* Return true if the symtab corresponding to PER_CU has been set,
528 bool symtab_set_p (const dwarf2_per_cu_data
*per_cu
) const;
530 /* Return the compunit_symtab associated to PER_CU, if it has been created. */
531 compunit_symtab
*get_symtab (const dwarf2_per_cu_data
*per_cu
) const;
533 /* Set the compunit_symtab associated to PER_CU. */
534 void set_symtab (const dwarf2_per_cu_data
*per_cu
, compunit_symtab
*symtab
);
536 /* Get the type_unit_group_unshareable corresponding to TU_GROUP. If one
537 does not exist, create it. */
538 type_unit_group_unshareable
*get_type_unit_group_unshareable
539 (type_unit_group
*tu_group
);
541 struct type
*get_type_for_signatured_type (signatured_type
*sig_type
) const;
543 void set_type_for_signatured_type (signatured_type
*sig_type
,
546 /* Get the dwarf2_cu matching PER_CU for this objfile. */
547 dwarf2_cu
*get_cu (dwarf2_per_cu_data
*per_cu
);
549 /* Set the dwarf2_cu matching PER_CU for this objfile. */
550 void set_cu (dwarf2_per_cu_data
*per_cu
, dwarf2_cu
*cu
);
552 /* Remove/free the dwarf2_cu matching PER_CU for this objfile. */
553 void remove_cu (dwarf2_per_cu_data
*per_cu
);
555 /* Free all cached compilation units. */
556 void remove_all_cus ();
558 /* Increase the age counter on each CU compilation unit and free
559 any that are too old. */
560 void age_comp_units ();
563 struct objfile
*objfile
;
565 /* Pointer to the data that is (possibly) shared between this objfile and
566 other objfiles backed by the same BFD. */
567 struct dwarf2_per_bfd
*per_bfd
;
569 /* Table mapping type DIEs to their struct type *.
570 This is nullptr if not allocated yet.
571 The mapping is done via (CU/TU + DIE offset) -> type. */
572 htab_up die_type_hash
;
574 /* Table containing line_header indexed by offset and offset_in_dwz. */
575 htab_up line_header_hash
;
577 /* The CU containing the m_builder in scope. */
578 dwarf2_cu
*sym_cu
= nullptr;
581 /* Hold the corresponding compunit_symtab for each CU or TU. This
582 is indexed by dwarf2_per_cu_data::index. A NULL value means
583 that the CU/TU has not been expanded yet. */
584 std::vector
<compunit_symtab
*> m_symtabs
;
586 /* Map from a type unit group to the corresponding unshared
588 typedef std::unique_ptr
<type_unit_group_unshareable
>
589 type_unit_group_unshareable_up
;
591 std::unordered_map
<type_unit_group
*, type_unit_group_unshareable_up
>
594 /* Map from signatured types to the corresponding struct type. */
595 std::unordered_map
<signatured_type
*, struct type
*> m_type_map
;
597 /* Map from the objfile-independent dwarf2_per_cu_data instances to the
598 corresponding objfile-dependent dwarf2_cu instances. */
599 std::unordered_map
<dwarf2_per_cu_data
*, dwarf2_cu
*> m_dwarf2_cus
;
602 /* Get the dwarf2_per_objfile associated to OBJFILE. */
604 dwarf2_per_objfile
*get_dwarf2_per_objfile (struct objfile
*objfile
);
606 /* Return the type of the DIE at DIE_OFFSET in the CU named by
609 struct type
*dwarf2_get_die_type (cu_offset die_offset
,
610 dwarf2_per_cu_data
*per_cu
,
611 dwarf2_per_objfile
*per_objfile
);
613 /* Given an index in .debug_addr, fetch the value.
614 NOTE: This can be called during dwarf expression evaluation,
615 long after the debug information has been read, and thus per_cu->cu
616 may no longer exist. */
618 CORE_ADDR
dwarf2_read_addr_index (dwarf2_per_cu_data
*per_cu
,
619 dwarf2_per_objfile
*per_objfile
,
620 unsigned int addr_index
);
622 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
623 Returned value is intended for DW_OP_call*. Returned
624 dwarf2_locexpr_baton->data has lifetime of
625 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
627 struct dwarf2_locexpr_baton dwarf2_fetch_die_loc_sect_off
628 (sect_offset sect_off
, dwarf2_per_cu_data
*per_cu
,
629 dwarf2_per_objfile
*per_objfile
,
630 gdb::function_view
<CORE_ADDR ()> get_frame_pc
,
631 bool resolve_abstract_p
= false);
633 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
636 struct dwarf2_locexpr_baton dwarf2_fetch_die_loc_cu_off
637 (cu_offset offset_in_cu
, dwarf2_per_cu_data
*per_cu
,
638 dwarf2_per_objfile
*per_objfile
,
639 gdb::function_view
<CORE_ADDR ()> get_frame_pc
);
641 /* If the DIE at SECT_OFF in PER_CU has a DW_AT_const_value, return a
642 pointer to the constant bytes and set LEN to the length of the
643 data. If memory is needed, allocate it on OBSTACK. If the DIE
644 does not have a DW_AT_const_value, return NULL. */
646 extern const gdb_byte
*dwarf2_fetch_constant_bytes
647 (sect_offset sect_off
, dwarf2_per_cu_data
*per_cu
,
648 dwarf2_per_objfile
*per_objfile
, obstack
*obstack
,
651 /* Return the type of the die at SECT_OFF in PER_CU. Return NULL if no
652 valid type for this die is found. If VAR_NAME is non-null, and if
653 the DIE in question is a variable declaration (definitions are
654 excluded), then *VAR_NAME is set to the variable's name. */
656 struct type
*dwarf2_fetch_die_type_sect_off
657 (sect_offset sect_off
, dwarf2_per_cu_data
*per_cu
,
658 dwarf2_per_objfile
*per_objfile
,
659 const char **var_name
= nullptr);
661 /* When non-zero, dump line number entries as they are read in. */
662 extern unsigned int dwarf_line_debug
;
664 /* Dwarf2 sections that can be accessed by dwarf2_get_section_info. */
665 enum dwarf2_section_enum
{
670 extern void dwarf2_get_section_info (struct objfile
*,
671 enum dwarf2_section_enum
,
672 asection
**, const gdb_byte
**,
675 #endif /* DWARF2READ_H */