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