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/>. */
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"
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
;
35 extern bool dwarf_always_disassemble
;
39 int nr_uniq_abbrev_tables
;
41 int nr_symtab_sharers
;
42 int nr_stmt_less_type_units
;
43 int nr_all_type_units_reallocs
;
46 struct dwarf2_debug_sections
;
48 struct mapped_debug_names
;
49 struct signatured_type
;
51 typedef struct die_info
*die_info_ptr
;
53 /* Collection of data recorded per objfile.
54 This hangs off of dwarf2_objfile_data_key. */
56 struct dwarf2_per_objfile
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
63 dwarf2_per_objfile (struct objfile
*objfile
,
64 const dwarf2_debug_sections
*names
,
67 ~dwarf2_per_objfile ();
69 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile
);
71 /* Return the CU/TU given its index.
73 This is intended for loops like:
75 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
76 + dwarf2_per_objfile->n_type_units); ++i)
78 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
83 dwarf2_per_cu_data
*get_cutu (int index
);
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
88 dwarf2_per_cu_data
*get_cu (int index
);
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
93 signatured_type
*get_tu (int index
);
95 /* Free all cached compilation units. */
96 void free_cached_comp_units ();
98 /* This function is mapped across the sections and remembers the
99 offset and size of each of the debugging sections we are
101 void locate_sections (bfd
*abfd
, asection
*sectp
,
102 const dwarf2_debug_sections
&names
);
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
{};
124 std::vector
<dwarf2_section_info
> types
;
127 struct objfile
*objfile
= NULL
;
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
;
133 /* The .debug_types-related CUs (TUs). */
134 std::vector
<signatured_type
*> all_type_units
;
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
{};
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
{};
144 /* Type unit statistics, to see how well the scaling improvements
146 struct tu_stats tu_stats
{};
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
;
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. */
156 /* True if we've checked for whether there is a DWP file. */
157 bool dwp_checked
= false;
159 /* The DWP file if there is one, or NULL. */
160 std::unique_ptr
<struct dwp_file
> dwp_file
;
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
;
166 /* Whether copy relocations are supported by this object format. */
169 /* A flag indicating whether this objfile has a section loaded at a
171 bool has_section_at_zero
= false;
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;
177 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
178 std::unique_ptr
<mapped_index
> index_table
;
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
;
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
{};
192 /* Set during partial symbol reading, to prevent queueing of full
194 bool reading_partial_symbols
= false;
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
{};
201 /* The CUs we recently read. */
202 std::vector
<dwarf2_per_cu_data
*> just_read_cus
;
204 /* Table containing line_header indexed by offset and offset_in_dwz. */
205 htab_t line_header_hash
{};
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
;
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
;
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
;
222 /* Get the dwarf2_per_objfile associated to OBJFILE. */
224 dwarf2_per_objfile
*get_dwarf2_per_objfile (struct objfile
*objfile
);
226 /* A partial symtab specialized for DWARF. */
227 struct dwarf2_psymtab
: public standard_psymtab
229 dwarf2_psymtab (const char *filename
, struct objfile
*objfile
)
230 : standard_psymtab (filename
, objfile
)
234 dwarf2_psymtab (const char *filename
, struct objfile
*objfile
,
236 : standard_psymtab (filename
, objfile
, addr
)
240 void read_symtab (struct objfile
*) override
;
241 void expand_psymtab (struct objfile
*) override
;
243 struct dwarf2_per_cu_data
*per_cu_data
;
246 /* Persistent data held for a compilation unit, even when not
247 processing it. We put a pointer to this structure in the
250 struct dwarf2_per_cu_data
252 /* The start offset and length of this compilation unit.
253 NOTE: Unlike comp_unit_head.length, this length includes
255 If the DIE refers to a DWO file, this is always of the original die,
257 sect_offset sect_off
;
260 /* DWARF standard version this data has been read from (such as 4 or 5). */
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;
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;
273 /* Non-zero if this CU is from .debug_types.
274 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
276 unsigned int is_debug_types
: 1;
278 /* Non-zero if this CU is from the .dwz file. */
279 unsigned int is_dwz
: 1;
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;
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
294 This flag is only valid if is_debug_types is true. */
295 unsigned int tu_read
: 1;
297 /* The section this CU/TU lives in.
298 If the DIE refers to a DWO file, this is always the original die,
300 struct dwarf2_section_info
*section
;
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
;
307 /* The corresponding dwarf2_per_objfile. */
308 struct dwarf2_per_objfile
*dwarf2_per_objfile
;
310 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
311 is active. Otherwise, the 'psymtab' field is active. */
314 /* The partial symbol table associated with this compilation unit,
315 or NULL for unread partial units. */
316 dwarf2_psymtab
*psymtab
;
318 /* Data needed by the "quick" functions. */
319 struct dwarf2_per_cu_quick_data
*quick
;
322 /* Return true of IMPORTED_SYMTABS is empty or not yet allocated. */
323 bool imported_symtabs_empty () const
325 return (imported_symtabs
== nullptr || imported_symtabs
->empty ());
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
)
332 if (imported_symtabs
== nullptr)
333 imported_symtabs
= new std::vector
<dwarf2_per_cu_data
*>;
334 imported_symtabs
->push_back (p
);
337 /* Return the size of IMPORTED_SYMTABS if it is allocated, otherwise
339 size_t imported_symtabs_size () const
341 if (imported_symtabs
== nullptr)
343 return imported_symtabs
->size ();
346 /* Delete IMPORTED_SYMTABS and set the pointer back to nullptr. */
347 void imported_symtabs_free ()
349 delete imported_symtabs
;
350 imported_symtabs
= nullptr;
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.
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.
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
377 std::vector
<dwarf2_per_cu_data
*> *imported_symtabs
;
380 /* Entry in the signatured_types hash table. */
382 struct signatured_type
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
388 struct dwarf2_per_cu_data per_cu
;
390 /* The type's signature. */
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
;
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
;
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
;
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. */
414 /* Containing DWO unit.
415 This field is valid iff per_cu.reading_dwo_directly. */
416 struct dwo_unit
*dwo_unit
;
419 /* This represents a '.dwz' file. */
423 dwz_file (gdb_bfd_ref_ptr
&&bfd
)
424 : dwz_bfd (std::move (bfd
))
428 const char *filename () const
430 return bfd_get_filename (this->dwz_bfd
.get ());
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
{};
443 gdb_bfd_ref_ptr dwz_bfd
;
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
;
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. */
454 extern struct dwz_file
*dwarf2_get_dwz_file
455 (struct dwarf2_per_objfile
*dwarf2_per_objfile
);
457 #endif /* DWARF2READ_H */