]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.h
Move read_addrmap_from_aranges to new file
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.h
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2023 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 <queue>
24 #include <unordered_map>
25 #include "dwarf2/comp-unit-head.h"
26 #include "dwarf2/file-and-dir.h"
27 #include "dwarf2/index-cache.h"
28 #include "dwarf2/mapped-index.h"
29 #include "dwarf2/section.h"
30 #include "dwarf2/cu.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"
35 #include "gdbsupport/packed.h"
36
37 /* Hold 'maintenance (set|show) dwarf' commands. */
38 extern struct cmd_list_element *set_dwarf_cmdlist;
39 extern struct cmd_list_element *show_dwarf_cmdlist;
40
41 struct tu_stats
42 {
43 int nr_uniq_abbrev_tables;
44 int nr_symtabs;
45 int nr_symtab_sharers;
46 int nr_stmt_less_type_units;
47 int nr_all_type_units_reallocs;
48 int nr_tus;
49 };
50
51 struct dwarf2_cu;
52 struct dwarf2_debug_sections;
53 struct dwarf2_per_bfd;
54 struct dwarf2_per_cu_data;
55 struct mapped_index;
56 struct mapped_debug_names;
57 struct signatured_type;
58 struct type_unit_group;
59
60 /* One item on the queue of compilation units to read in full symbols
61 for. */
62 struct dwarf2_queue_item
63 {
64 dwarf2_queue_item (dwarf2_per_cu_data *cu, dwarf2_per_objfile *per_objfile,
65 enum language lang)
66 : per_cu (cu),
67 per_objfile (per_objfile),
68 pretend_language (lang)
69 {
70 }
71
72 ~dwarf2_queue_item ();
73
74 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_item);
75
76 dwarf2_per_cu_data *per_cu;
77 dwarf2_per_objfile *per_objfile;
78 enum language pretend_language;
79 };
80
81 /* A deleter for dwarf2_per_cu_data that knows to downcast to
82 signatured_type as appropriate. This approach lets us avoid a
83 virtual destructor, which saves a bit of space. */
84
85 struct dwarf2_per_cu_data_deleter
86 {
87 void operator() (dwarf2_per_cu_data *data);
88 };
89
90 /* A specialization of unique_ptr for dwarf2_per_cu_data and
91 subclasses. */
92 typedef std::unique_ptr<dwarf2_per_cu_data, dwarf2_per_cu_data_deleter>
93 dwarf2_per_cu_data_up;
94
95 /* Persistent data held for a compilation unit, even when not
96 processing it. We put a pointer to this structure in the
97 psymtab. */
98
99 struct dwarf2_per_cu_data
100 {
101 dwarf2_per_cu_data ()
102 : is_debug_types (false),
103 is_dwz (false),
104 reading_dwo_directly (false),
105 tu_read (false),
106 queued (false),
107 m_header_read_in (false),
108 mark (false),
109 files_read (false),
110 scanned (false)
111 {
112 }
113
114 /* The start offset and length of this compilation unit.
115 NOTE: Unlike comp_unit_head.length, this length includes
116 initial_length_size.
117 If the DIE refers to a DWO file, this is always of the original die,
118 not the DWO file. */
119 sect_offset sect_off {};
120
121 private:
122 unsigned int m_length = 0;
123
124 /* DWARF standard version this data has been read from (such as 4 or 5). */
125 unsigned char m_dwarf_version = 0;
126
127 public:
128 /* Non-zero if this CU is from .debug_types.
129 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
130 this is non-zero. */
131 unsigned int is_debug_types : 1;
132
133 /* Non-zero if this CU is from the .dwz file. */
134 unsigned int is_dwz : 1;
135
136 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
137 This flag is only valid if is_debug_types is true.
138 We can't read a CU directly from a DWO file: There are required
139 attributes in the stub. */
140 unsigned int reading_dwo_directly : 1;
141
142 /* Non-zero if the TU has been read.
143 This is used to assist the "Stay in DWO Optimization" for Fission:
144 When reading a DWO, it's faster to read TUs from the DWO instead of
145 fetching them from random other DWOs (due to comdat folding).
146 If the TU has already been read, the optimization is unnecessary
147 (and unwise - we don't want to change where gdb thinks the TU lives
148 "midflight").
149 This flag is only valid if is_debug_types is true. */
150 unsigned int tu_read : 1;
151
152 /* Wrap the following in struct packed instead of bitfields to avoid
153 data races when the bitfields end up on the same memory location
154 (per C++ memory model). */
155
156 /* If addresses have been read for this CU (usually from
157 .debug_aranges), then this flag is set. */
158 packed<bool, 1> addresses_seen = false;
159
160 /* Flag indicating this compilation unit will be read in before
161 any of the current compilation units are processed. */
162 packed<bool, 1> queued;
163
164 /* True if HEADER has been read in.
165
166 Don't access this field directly. It should be private, but we can't make
167 it private at the moment. */
168 mutable packed<bool, 1> m_header_read_in;
169
170 /* A temporary mark bit used when iterating over all CUs in
171 expand_symtabs_matching. */
172 packed<unsigned int, 1> mark;
173
174 /* True if we've tried to read the file table. There will be no
175 point in trying to read it again next time. */
176 packed<bool, 1> files_read;
177
178 private:
179 /* The unit type of this CU. */
180 std::atomic<packed<dwarf_unit_type, 1>> m_unit_type {(dwarf_unit_type)0};
181
182 /* The language of this CU. */
183 std::atomic<packed<language, LANGUAGE_BYTES>> m_lang {language_unknown};
184
185 /* The original DW_LANG_* value of the CU, as provided to us by
186 DW_AT_language. It is interesting to keep this value around in cases where
187 we can't use the values from the language enum, as the mapping to them is
188 lossy, and, while that is usually fine, things like the index have an
189 understandable bias towards not exposing internal GDB structures to the
190 outside world, and so prefer to use DWARF constants in their stead. */
191 std::atomic<packed<dwarf_source_language, 2>> m_dw_lang
192 { (dwarf_source_language) 0 };
193
194 public:
195 /* True if this CU has been scanned by the indexer; false if
196 not. */
197 std::atomic<bool> scanned;
198
199 /* Our index in the unshared "symtabs" vector. */
200 unsigned index = 0;
201
202 /* The section this CU/TU lives in.
203 If the DIE refers to a DWO file, this is always the original die,
204 not the DWO file. */
205 struct dwarf2_section_info *section = nullptr;
206
207 /* Backlink to the owner of this. */
208 dwarf2_per_bfd *per_bfd = nullptr;
209
210 /* DWARF header of this CU. Note that dwarf2_cu reads its own version of the
211 header, which may differ from this one, since it may pass rcuh_kind::TYPE
212 to read_comp_unit_head, whereas for dwarf2_per_cu_data we always pass
213 rcuh_kind::COMPILE.
214
215 Don't access this field directly, use the get_header method instead. It
216 should be private, but we can't make it private at the moment. */
217 mutable comp_unit_head m_header;
218
219 /* The file and directory for this CU. This is cached so that we
220 don't need to re-examine the DWO in some situations. This may be
221 nullptr, depending on the CU; for example a partial unit won't
222 have one. */
223 std::unique_ptr<file_and_directory> fnd;
224
225 /* The file table. This can be NULL if there was no file table
226 or it's currently not read in.
227 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
228 struct quick_file_names *file_names = nullptr;
229
230 /* The CUs we import using DW_TAG_imported_unit. This is filled in
231 while reading psymtabs, used to compute the psymtab dependencies,
232 and then cleared. Then it is filled in again while reading full
233 symbols, and only deleted when the objfile is destroyed.
234
235 This is also used to work around a difference between the way gold
236 generates .gdb_index version <=7 and the way gdb does. Arguably this
237 is a gold bug. For symbols coming from TUs, gold records in the index
238 the CU that includes the TU instead of the TU itself. This breaks
239 dw2_lookup_symbol: It assumes that if the index says symbol X lives
240 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
241 will find X. Alas TUs live in their own symtab, so after expanding CU Y
242 we need to look in TU Z to find X. Fortunately, this is akin to
243 DW_TAG_imported_unit, so we just use the same mechanism: For
244 .gdb_index version <=7 this also records the TUs that the CU referred
245 to. Concurrently with this change gdb was modified to emit version 8
246 indices so we only pay a price for gold generated indices.
247 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
248
249 This currently needs to be a public member due to how
250 dwarf2_per_cu_data is allocated and used. Ideally in future things
251 could be refactored to make this private. Until then please try to
252 avoid direct access to this member, and instead use the helper
253 functions above. */
254 std::vector <dwarf2_per_cu_data *> *imported_symtabs = nullptr;
255
256 /* Return true of IMPORTED_SYMTABS is empty or not yet allocated. */
257 bool imported_symtabs_empty () const
258 {
259 return (imported_symtabs == nullptr || imported_symtabs->empty ());
260 }
261
262 /* Push P to the back of IMPORTED_SYMTABS, allocated IMPORTED_SYMTABS
263 first if required. */
264 void imported_symtabs_push (dwarf2_per_cu_data *p)
265 {
266 if (imported_symtabs == nullptr)
267 imported_symtabs = new std::vector <dwarf2_per_cu_data *>;
268 imported_symtabs->push_back (p);
269 }
270
271 /* Return the size of IMPORTED_SYMTABS if it is allocated, otherwise
272 return 0. */
273 size_t imported_symtabs_size () const
274 {
275 if (imported_symtabs == nullptr)
276 return 0;
277 return imported_symtabs->size ();
278 }
279
280 /* Delete IMPORTED_SYMTABS and set the pointer back to nullptr. */
281 void imported_symtabs_free ()
282 {
283 delete imported_symtabs;
284 imported_symtabs = nullptr;
285 }
286
287 /* Get the header of this per_cu, reading it if necessary. */
288 const comp_unit_head *get_header () const;
289
290 /* Return the address size given in the compilation unit header for
291 this CU. */
292 int addr_size () const;
293
294 /* Return the offset size given in the compilation unit header for
295 this CU. */
296 int offset_size () const;
297
298 /* Return the DW_FORM_ref_addr size given in the compilation unit
299 header for this CU. */
300 int ref_addr_size () const;
301
302 /* Return length of this CU. */
303 unsigned int length () const
304 {
305 /* Make sure it's set already. */
306 gdb_assert (m_length != 0);
307 return m_length;
308 }
309
310 void set_length (unsigned int length, bool strict_p = true)
311 {
312 if (m_length == 0)
313 /* Set if not set already. */
314 m_length = length;
315 else if (strict_p)
316 /* If already set, verify that it's the same value. */
317 gdb_assert (m_length == length);
318 }
319
320 /* Return DWARF version number of this CU. */
321 short version () const
322 {
323 /* Make sure it's set already. */
324 gdb_assert (m_dwarf_version != 0);
325 return m_dwarf_version;
326 }
327
328 void set_version (short version)
329 {
330 if (m_dwarf_version == 0)
331 /* Set if not set already. */
332 m_dwarf_version = version;
333 else
334 /* If already set, verify that it's the same value. */
335 gdb_assert (m_dwarf_version == version);
336 }
337
338 dwarf_unit_type unit_type (bool strict_p = true) const
339 {
340 dwarf_unit_type ut = m_unit_type.load ();
341 if (strict_p)
342 gdb_assert (ut != 0);
343 return ut;
344 }
345
346 void set_unit_type (dwarf_unit_type unit_type)
347 {
348 /* Set if not set already. */
349 packed<dwarf_unit_type, 1> nope = (dwarf_unit_type)0;
350 if (m_unit_type.compare_exchange_strong (nope, unit_type))
351 return;
352
353 /* If already set, verify that it's the same value. */
354 nope = unit_type;
355 if (m_unit_type.compare_exchange_strong (nope, unit_type))
356 return;
357 gdb_assert_not_reached ();
358 }
359
360 enum language lang (bool strict_p = true) const
361 {
362 enum language l = m_lang.load ();
363 if (strict_p)
364 gdb_assert (l != language_unknown);
365 return l;
366 }
367
368 /* Return the language of this CU, as a DWARF DW_LANG_* value. This
369 may be 0 in some situations. */
370 dwarf_source_language dw_lang () const
371 { return m_dw_lang.load (); }
372
373 /* Set the language of this CU. LANG is the language in gdb terms,
374 and DW_LANG is the language as a DW_LANG_* value. These may
375 differ, as DW_LANG can be 0 for included units, whereas in this
376 situation LANG would be set by the importing CU. */
377 void set_lang (enum language lang, dwarf_source_language dw_lang);
378
379 /* Free any cached file names. */
380 void free_cached_file_names ();
381 };
382
383 /* Entry in the signatured_types hash table. */
384
385 struct signatured_type : public dwarf2_per_cu_data
386 {
387 signatured_type (ULONGEST signature)
388 : signature (signature)
389 {}
390
391 /* The type's signature. */
392 ULONGEST signature;
393
394 /* Offset in the TU of the type's DIE, as read from the TU header.
395 If this TU is a DWO stub and the definition lives in a DWO file
396 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
397 cu_offset type_offset_in_tu {};
398
399 /* Offset in the section of the type's DIE.
400 If the definition lives in a DWO file, this is the offset in the
401 .debug_types.dwo section.
402 The value is zero until the actual value is known.
403 Zero is otherwise not a valid section offset. */
404 sect_offset type_offset_in_section {};
405
406 /* Type units are grouped by their DW_AT_stmt_list entry so that they
407 can share them. This points to the containing symtab. */
408 struct type_unit_group *type_unit_group = nullptr;
409
410 /* Containing DWO unit.
411 This field is valid iff per_cu.reading_dwo_directly. */
412 struct dwo_unit *dwo_unit = nullptr;
413 };
414
415 using signatured_type_up = std::unique_ptr<signatured_type>;
416
417 /* Some DWARF data can be shared across objfiles who share the same BFD,
418 this data is stored in this object.
419
420 Two dwarf2_per_objfile objects representing objfiles sharing the same BFD
421 will point to the same instance of dwarf2_per_bfd, unless the BFD requires
422 relocation. */
423
424 struct dwarf2_per_bfd
425 {
426 /* Construct a dwarf2_per_bfd for OBFD. NAMES points to the
427 dwarf2 section names, or is NULL if the standard ELF names are
428 used. CAN_COPY is true for formats where symbol
429 interposition is possible and so symbol values must follow copy
430 relocation rules. */
431 dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names, bool can_copy);
432
433 ~dwarf2_per_bfd ();
434
435 DISABLE_COPY_AND_ASSIGN (dwarf2_per_bfd);
436
437 /* Return the CU given its index. */
438 dwarf2_per_cu_data *get_cu (int index) const
439 {
440 return this->all_units[index].get ();
441 }
442
443 /* A convenience function to allocate a dwarf2_per_cu_data. The
444 returned object has its "index" field set properly. The object
445 is allocated on the dwarf2_per_bfd obstack. */
446 dwarf2_per_cu_data_up allocate_per_cu ();
447
448 /* A convenience function to allocate a signatured_type. The
449 returned object has its "index" field set properly. The object
450 is allocated on the dwarf2_per_bfd obstack. */
451 signatured_type_up allocate_signatured_type (ULONGEST signature);
452
453 /* Map all the DWARF section data needed when scanning
454 .debug_info. */
455 void map_info_sections (struct objfile *objfile);
456
457 private:
458 /* This function is mapped across the sections and remembers the
459 offset and size of each of the debugging sections we are
460 interested in. */
461 void locate_sections (bfd *abfd, asection *sectp,
462 const dwarf2_debug_sections &names);
463
464 public:
465 /* The corresponding BFD. */
466 bfd *obfd;
467
468 /* Objects that can be shared across objfiles may be stored in this
469 obstack, while objects that are objfile-specific are stored on
470 the objfile obstack. */
471 auto_obstack obstack;
472
473 dwarf2_section_info info {};
474 dwarf2_section_info abbrev {};
475 dwarf2_section_info line {};
476 dwarf2_section_info loc {};
477 dwarf2_section_info loclists {};
478 dwarf2_section_info macinfo {};
479 dwarf2_section_info macro {};
480 dwarf2_section_info str {};
481 dwarf2_section_info str_offsets {};
482 dwarf2_section_info line_str {};
483 dwarf2_section_info ranges {};
484 dwarf2_section_info rnglists {};
485 dwarf2_section_info addr {};
486 dwarf2_section_info frame {};
487 dwarf2_section_info eh_frame {};
488 dwarf2_section_info gdb_index {};
489 dwarf2_section_info debug_names {};
490 dwarf2_section_info debug_aranges {};
491
492 std::vector<dwarf2_section_info> types;
493
494 /* Table of all the compilation units. This is used to locate
495 the target compilation unit of a particular reference. */
496 std::vector<dwarf2_per_cu_data_up> all_units;
497
498 /* The all_units vector contains both CUs and TUs. Provide views on the
499 vector that are limited to either the CU part or the TU part. */
500 gdb::array_view<dwarf2_per_cu_data_up> all_comp_units;
501 gdb::array_view<dwarf2_per_cu_data_up> all_type_units;
502
503 /* Table of struct type_unit_group objects.
504 The hash key is the DW_AT_stmt_list value. */
505 htab_up type_unit_groups;
506
507 /* A table mapping .debug_types signatures to its signatured_type entry.
508 This is NULL if the .debug_types section hasn't been read in yet. */
509 htab_up signatured_types;
510
511 /* Type unit statistics, to see how well the scaling improvements
512 are doing. */
513 struct tu_stats tu_stats {};
514
515 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
516 This is NULL if the table hasn't been allocated yet. */
517 htab_up dwo_files;
518
519 /* True if we've checked for whether there is a DWP file. */
520 bool dwp_checked = false;
521
522 /* The DWP file if there is one, or NULL. */
523 std::unique_ptr<struct dwp_file> dwp_file;
524
525 /* The shared '.dwz' file, if one exists. This is used when the
526 original data was compressed using 'dwz -m'. */
527 std::unique_ptr<struct dwz_file> dwz_file;
528
529 /* Whether copy relocations are supported by this object format. */
530 bool can_copy;
531
532 /* A flag indicating whether this objfile has a section loaded at a
533 VMA of 0. */
534 bool has_section_at_zero = false;
535
536 /* The mapped index, or NULL in the readnow case. */
537 std::unique_ptr<dwarf_scanner_base> index_table;
538
539 /* When using index_table, this keeps track of all quick_file_names entries.
540 TUs typically share line table entries with a CU, so we maintain a
541 separate table of all line table entries to support the sharing.
542 Note that while there can be way more TUs than CUs, we've already
543 sorted all the TUs into "type unit groups", grouped by their
544 DW_AT_stmt_list value. Therefore the only sharing done here is with a
545 CU and its associated TU group if there is one. */
546 htab_up quick_file_names_table;
547
548 /* The CUs we recently read. */
549 std::vector<dwarf2_per_cu_data *> just_read_cus;
550
551 /* If we loaded the index from an external file, this contains the
552 resources associated to the open file, memory mapping, etc. */
553 std::unique_ptr<index_cache_resource> index_cache_res;
554
555 /* Mapping from abstract origin DIE to concrete DIEs that reference it as
556 DW_AT_abstract_origin. */
557 std::unordered_map<sect_offset, std::vector<sect_offset>,
558 gdb::hash_enum<sect_offset>>
559 abstract_to_concrete;
560
561 /* The address map that is used by the DWARF index code. */
562 struct addrmap *index_addrmap = nullptr;
563 };
564
565 /* An iterator for all_units that is based on index. This
566 approach makes it possible to iterate over all_units safely,
567 when some caller in the loop may add new units. */
568
569 class all_units_iterator
570 {
571 public:
572
573 all_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
574 : m_per_bfd (per_bfd),
575 m_index (start ? 0 : per_bfd->all_units.size ())
576 {
577 }
578
579 all_units_iterator &operator++ ()
580 {
581 ++m_index;
582 return *this;
583 }
584
585 dwarf2_per_cu_data *operator* () const
586 {
587 return m_per_bfd->get_cu (m_index);
588 }
589
590 bool operator== (const all_units_iterator &other) const
591 {
592 return m_index == other.m_index;
593 }
594
595
596 bool operator!= (const all_units_iterator &other) const
597 {
598 return m_index != other.m_index;
599 }
600
601 private:
602
603 dwarf2_per_bfd *m_per_bfd;
604 size_t m_index;
605 };
606
607 /* A range adapter for the all_units_iterator. */
608 class all_units_range
609 {
610 public:
611
612 all_units_range (dwarf2_per_bfd *per_bfd)
613 : m_per_bfd (per_bfd)
614 {
615 }
616
617 all_units_iterator begin ()
618 {
619 return all_units_iterator (m_per_bfd, true);
620 }
621
622 all_units_iterator end ()
623 {
624 return all_units_iterator (m_per_bfd, false);
625 }
626
627 private:
628
629 dwarf2_per_bfd *m_per_bfd;
630 };
631
632 /* This is the per-objfile data associated with a type_unit_group. */
633
634 struct type_unit_group_unshareable
635 {
636 /* The compunit symtab.
637 Type units in a group needn't all be defined in the same source file,
638 so we create an essentially anonymous symtab as the compunit symtab. */
639 struct compunit_symtab *compunit_symtab = nullptr;
640
641 /* The number of symtabs from the line header.
642 The value here must match line_header.num_file_names. */
643 unsigned int num_symtabs = 0;
644
645 /* The symbol tables for this TU (obtained from the files listed in
646 DW_AT_stmt_list).
647 WARNING: The order of entries here must match the order of entries
648 in the line header. After the first TU using this type_unit_group, the
649 line header for the subsequent TUs is recreated from this. This is done
650 because we need to use the same symtabs for each TU using the same
651 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
652 there's no guarantee the line header doesn't have duplicate entries. */
653 struct symtab **symtabs = nullptr;
654 };
655
656 /* Collection of data recorded per objfile.
657 This hangs off of dwarf2_objfile_data_key.
658
659 Some DWARF data cannot (currently) be shared across objfiles. Such
660 data is stored in this object. */
661
662 struct dwarf2_per_objfile
663 {
664 dwarf2_per_objfile (struct objfile *objfile, dwarf2_per_bfd *per_bfd)
665 : objfile (objfile), per_bfd (per_bfd)
666 {}
667
668 ~dwarf2_per_objfile ();
669
670 /* Return pointer to string at .debug_line_str offset as read from BUF.
671 BUF is assumed to be in a compilation unit described by CU_HEADER.
672 Return *BYTES_READ_PTR count of bytes read from BUF. */
673 const char *read_line_string (const gdb_byte *buf,
674 const struct comp_unit_head *cu_header,
675 unsigned int *bytes_read_ptr);
676
677 /* Return pointer to string at .debug_line_str offset as read from BUF.
678 The offset_size is OFFSET_SIZE. */
679 const char *read_line_string (const gdb_byte *buf,
680 unsigned int offset_size);
681
682 /* Return true if the symtab corresponding to PER_CU has been set,
683 false otherwise. */
684 bool symtab_set_p (const dwarf2_per_cu_data *per_cu) const;
685
686 /* Return the compunit_symtab associated to PER_CU, if it has been created. */
687 compunit_symtab *get_symtab (const dwarf2_per_cu_data *per_cu) const;
688
689 /* Set the compunit_symtab associated to PER_CU. */
690 void set_symtab (const dwarf2_per_cu_data *per_cu, compunit_symtab *symtab);
691
692 /* Get the type_unit_group_unshareable corresponding to TU_GROUP. If one
693 does not exist, create it. */
694 type_unit_group_unshareable *get_type_unit_group_unshareable
695 (type_unit_group *tu_group);
696
697 struct type *get_type_for_signatured_type (signatured_type *sig_type) const;
698
699 void set_type_for_signatured_type (signatured_type *sig_type,
700 struct type *type);
701
702 /* Get the dwarf2_cu matching PER_CU for this objfile. */
703 dwarf2_cu *get_cu (dwarf2_per_cu_data *per_cu);
704
705 /* Set the dwarf2_cu matching PER_CU for this objfile. */
706 void set_cu (dwarf2_per_cu_data *per_cu, std::unique_ptr<dwarf2_cu> cu);
707
708 /* Remove/free the dwarf2_cu matching PER_CU for this objfile. */
709 void remove_cu (dwarf2_per_cu_data *per_cu);
710
711 /* Free all cached compilation units. */
712 void remove_all_cus ();
713
714 /* Increase the age counter on each CU compilation unit and free
715 any that are too old. */
716 void age_comp_units ();
717
718 /* Apply any needed adjustments to ADDR, returning an adjusted but
719 still unrelocated address. */
720 unrelocated_addr adjust (unrelocated_addr addr);
721
722 /* Apply any needed adjustments to ADDR and then relocate the
723 address according to the objfile's section offsets, returning a
724 relocated address. */
725 CORE_ADDR relocate (unrelocated_addr addr);
726
727 /* Back link. */
728 struct objfile *objfile;
729
730 /* Pointer to the data that is (possibly) shared between this objfile and
731 other objfiles backed by the same BFD. */
732 struct dwarf2_per_bfd *per_bfd;
733
734 /* Table mapping type DIEs to their struct type *.
735 This is nullptr if not allocated yet.
736 The mapping is done via (CU/TU + DIE offset) -> type. */
737 htab_up die_type_hash;
738
739 /* Table containing line_header indexed by offset and offset_in_dwz. */
740 htab_up line_header_hash;
741
742 /* The CU containing the m_builder in scope. */
743 dwarf2_cu *sym_cu = nullptr;
744
745 /* CUs that are queued to be read. */
746 gdb::optional<std::queue<dwarf2_queue_item>> queue;
747
748 private:
749 /* Hold the corresponding compunit_symtab for each CU or TU. This
750 is indexed by dwarf2_per_cu_data::index. A NULL value means
751 that the CU/TU has not been expanded yet. */
752 std::vector<compunit_symtab *> m_symtabs;
753
754 /* Map from a type unit group to the corresponding unshared
755 structure. */
756 typedef std::unique_ptr<type_unit_group_unshareable>
757 type_unit_group_unshareable_up;
758
759 std::unordered_map<type_unit_group *, type_unit_group_unshareable_up>
760 m_type_units;
761
762 /* Map from signatured types to the corresponding struct type. */
763 std::unordered_map<signatured_type *, struct type *> m_type_map;
764
765 /* Map from the objfile-independent dwarf2_per_cu_data instances to the
766 corresponding objfile-dependent dwarf2_cu instances. */
767 std::unordered_map<dwarf2_per_cu_data *,
768 std::unique_ptr<dwarf2_cu>> m_dwarf2_cus;
769 };
770
771 /* Converts DWARF language names to GDB language names. */
772
773 enum language dwarf_lang_to_enum_language (unsigned int lang);
774
775 /* Get the dwarf2_per_objfile associated to OBJFILE. */
776
777 dwarf2_per_objfile *get_dwarf2_per_objfile (struct objfile *objfile);
778
779 /* Return the type of the DIE at DIE_OFFSET in the CU named by
780 PER_CU. */
781
782 struct type *dwarf2_get_die_type (cu_offset die_offset,
783 dwarf2_per_cu_data *per_cu,
784 dwarf2_per_objfile *per_objfile);
785
786 /* Given an index in .debug_addr, fetch the value.
787 NOTE: This can be called during dwarf expression evaluation,
788 long after the debug information has been read, and thus per_cu->cu
789 may no longer exist. */
790
791 unrelocated_addr dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
792 dwarf2_per_objfile *per_objfile,
793 unsigned int addr_index);
794
795 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
796 Returned value is intended for DW_OP_call*. Returned
797 dwarf2_locexpr_baton->data has lifetime of
798 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
799
800 struct dwarf2_locexpr_baton dwarf2_fetch_die_loc_sect_off
801 (sect_offset sect_off, dwarf2_per_cu_data *per_cu,
802 dwarf2_per_objfile *per_objfile,
803 gdb::function_view<CORE_ADDR ()> get_frame_pc,
804 bool resolve_abstract_p = false);
805
806 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
807 offset. */
808
809 struct dwarf2_locexpr_baton dwarf2_fetch_die_loc_cu_off
810 (cu_offset offset_in_cu, dwarf2_per_cu_data *per_cu,
811 dwarf2_per_objfile *per_objfile,
812 gdb::function_view<CORE_ADDR ()> get_frame_pc);
813
814 /* If the DIE at SECT_OFF in PER_CU has a DW_AT_const_value, return a
815 pointer to the constant bytes and set LEN to the length of the
816 data. If memory is needed, allocate it on OBSTACK. If the DIE
817 does not have a DW_AT_const_value, return NULL. */
818
819 extern const gdb_byte *dwarf2_fetch_constant_bytes
820 (sect_offset sect_off, dwarf2_per_cu_data *per_cu,
821 dwarf2_per_objfile *per_objfile, obstack *obstack,
822 LONGEST *len);
823
824 /* Return the type of the die at SECT_OFF in PER_CU. Return NULL if no
825 valid type for this die is found. If VAR_NAME is non-null, and if
826 the DIE in question is a variable declaration (definitions are
827 excluded), then *VAR_NAME is set to the variable's name. */
828
829 struct type *dwarf2_fetch_die_type_sect_off
830 (sect_offset sect_off, dwarf2_per_cu_data *per_cu,
831 dwarf2_per_objfile *per_objfile,
832 const char **var_name = nullptr);
833
834 /* When non-zero, dump line number entries as they are read in. */
835 extern unsigned int dwarf_line_debug;
836
837 /* Dwarf2 sections that can be accessed by dwarf2_get_section_info. */
838 enum dwarf2_section_enum {
839 DWARF2_DEBUG_FRAME,
840 DWARF2_EH_FRAME
841 };
842
843 extern void dwarf2_get_section_info (struct objfile *,
844 enum dwarf2_section_enum,
845 asection **, const gdb_byte **,
846 bfd_size_type *);
847
848 /* Return true if the producer of the inferior is clang. */
849 extern bool producer_is_clang (struct dwarf2_cu *cu);
850
851 /* Interface for DWARF indexing methods. */
852
853 struct dwarf2_base_index_functions : public quick_symbol_functions
854 {
855 bool has_symbols (struct objfile *objfile) override;
856
857 bool has_unexpanded_symtabs (struct objfile *objfile) override;
858
859 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
860
861 void forget_cached_source_info (struct objfile *objfile) override;
862
863 enum language lookup_global_symbol_language (struct objfile *objfile,
864 const char *name,
865 domain_enum domain,
866 bool *symbol_found_p) override
867 {
868 *symbol_found_p = false;
869 return language_unknown;
870 }
871
872 void print_stats (struct objfile *objfile, bool print_bcache) override;
873
874 void expand_all_symtabs (struct objfile *objfile) override;
875
876 /* A helper function that finds the per-cu object from an "adjusted"
877 PC -- a PC with the base text offset removed. */
878 virtual dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
879 CORE_ADDR adjusted_pc);
880
881 struct compunit_symtab *find_pc_sect_compunit_symtab
882 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
883 CORE_ADDR pc, struct obj_section *section, int warn_if_readin)
884 override final;
885
886 struct compunit_symtab *find_compunit_symtab_by_address
887 (struct objfile *objfile, CORE_ADDR address) override
888 {
889 return nullptr;
890 }
891
892 void map_symbol_filenames (struct objfile *objfile,
893 gdb::function_view<symbol_filename_ftype> fun,
894 bool need_fullname) override;
895 };
896
897 /* If FILE_MATCHER is NULL or if PER_CU has
898 dwarf2_per_cu_quick_data::MARK set (see
899 dw_expand_symtabs_matching_file_matcher), expand the CU and call
900 EXPANSION_NOTIFY on it. */
901
902 extern bool dw2_expand_symtabs_matching_one
903 (dwarf2_per_cu_data *per_cu,
904 dwarf2_per_objfile *per_objfile,
905 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
906 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
907
908 /* Helper for dw2_expand_symtabs_matching that works with a
909 mapped_index_base instead of the containing objfile. This is split
910 to a separate function in order to be able to unit test the
911 name_components matching using a mock mapped_index_base. For each
912 symbol name that matches, calls MATCH_CALLBACK, passing it the
913 symbol's index in the mapped_index_base symbol table. */
914
915 extern bool dw2_expand_symtabs_matching_symbol
916 (mapped_index_base &index,
917 const lookup_name_info &lookup_name_in,
918 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
919 gdb::function_view<bool (offset_type)> match_callback,
920 dwarf2_per_objfile *per_objfile);
921
922 /* If FILE_MATCHER is non-NULL, set all the
923 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
924 that match FILE_MATCHER. */
925
926 extern void dw_expand_symtabs_matching_file_matcher
927 (dwarf2_per_objfile *per_objfile,
928 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher);
929
930 /* Return pointer to string at .debug_str offset STR_OFFSET. */
931
932 extern const char *read_indirect_string_at_offset
933 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
934
935 /* Allocate a hash table for signatured types. */
936
937 extern htab_up allocate_signatured_type_table ();
938
939 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
940 obstack, and constructed with the specified field values. */
941
942 extern dwarf2_per_cu_data_up create_cu_from_index_list
943 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
944 int is_dwz, sect_offset sect_off, ULONGEST length);
945
946 /* Initialize the views on all_units. */
947
948 extern void finalize_all_units (dwarf2_per_bfd *per_bfd);
949
950 /* Create a quick_file_names hash table. */
951
952 extern htab_up create_quick_file_names_table (unsigned int nr_initial_entries);
953
954 #endif /* DWARF2READ_H */