]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/dwarf2/read.c
gdb: add type::name / type::set_name
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
index f291d8f237991a56f6b4e755418cc4c1cb3deebb..2c81a4eed65bf87e52d8dffadbc2737093c1d291 100644 (file)
 #include "dwarf2/read.h"
 #include "dwarf2/abbrev.h"
 #include "dwarf2/attribute.h"
+#include "dwarf2/comp-unit.h"
 #include "dwarf2/index-cache.h"
 #include "dwarf2/index-common.h"
 #include "dwarf2/leb.h"
 #include "dwarf2/line-header.h"
+#include "dwarf2/dwz.h"
+#include "dwarf2/macro.h"
+#include "dwarf2/die.h"
+#include "dwarf2/stringify.h"
 #include "bfd.h"
 #include "elf-bfd.h"
 #include "symtab.h"
@@ -46,7 +51,6 @@
 #include "demangle.h"
 #include "gdb-demangle.h"
 #include "filenames.h" /* for DOSish file names */
-#include "macrotab.h"
 #include "language.h"
 #include "complaints.h"
 #include "dwarf2/expr.h"
@@ -81,6 +85,8 @@
 #include "gdbsupport/selftest.h"
 #include "rust-lang.h"
 #include "gdbsupport/pathstuff.h"
+#include "count-one-bits.h"
+#include "debuginfod-support.h"
 
 /* When == 1, print basic high level tracing messages.
    When > 1, be more verbose.
@@ -108,6 +114,12 @@ static int dwarf2_loclist_index;
 static int dwarf2_locexpr_block_index;
 static int dwarf2_loclist_block_index;
 
+/* Size of .debug_loclists section header for 32-bit DWARF format.  */
+#define LOCLIST_HEADER_SIZE32 12
+
+/* Size of .debug_loclists section header for 64-bit DWARF format.  */
+#define LOCLIST_HEADER_SIZE64 20
+
 /* An index into a (C++) symbol name component in a symbol name as
    recorded in the mapped_index's symbol table.  For each C++ symbol
    in the symbol table, we record one entry for the start of each
@@ -340,41 +352,28 @@ dwop_section_names =
 
 /* local data types */
 
-/* The data in a compilation unit header, after target2host
-   translation, looks like this.  */
-struct comp_unit_head
+/* The location list section (.debug_loclists) begins with a header,
+   which contains the following information.  */
+struct loclist_header
 {
+  /* A 4-byte or 12-byte length containing the length of the
+     set of entries for this compilation unit, not including the
+     length field itself.  */
   unsigned int length;
-  short version;
-  unsigned char addr_size;
-  unsigned char signed_addr_p;
-  sect_offset abbrev_sect_off;
-
-  /* Size of file offsets; either 4 or 8.  */
-  unsigned int offset_size;
-
-  /* Size of the length field; either 4 or 12.  */
-  unsigned int initial_length_size;
-
-  enum dwarf_unit_type unit_type;
-
-  /* Offset to the first byte of this compilation unit header in the
-     .debug_info section, for resolving relative reference dies.  */
-  sect_offset sect_off;
 
-  /* Offset to first die in this cu from the start of the cu.
-     This will be the first byte following the compilation unit header.  */
-  cu_offset first_die_cu_offset;
+  /* A 2-byte version identifier.  */
+  short version;
 
+  /* A 1-byte unsigned integer containing the size in bytes of an address on
+     the target system.  */
+  unsigned char addr_size;
 
-  /* 64-bit signature of this unit. For type units, it denotes the signature of
-     the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
-     Also used in DWARF 5, to denote the dwo id when the unit type is
-     DW_UT_skeleton or DW_UT_split_compile.  */
-  ULONGEST signature;
+  /* A 1-byte unsigned integer containing the size in bytes of a segment selector
+     on the target system.  */
+  unsigned char segment_collector_size;
 
-  /* For types, offset in the type's DIE of the type defined by this TU.  */
-  cu_offset type_cu_offset_in_tu;
+  /* A 4-byte count of the number of offsets that follow the header.  */
+  unsigned int offset_entry_count;
 };
 
 /* Type used for delaying computation of method physnames.
@@ -425,10 +424,7 @@ struct dwarf2_cu
   struct comp_unit_head header {};
 
   /* Base address of this compilation unit.  */
-  CORE_ADDR base_address = 0;
-
-  /* Non-zero if base_address has been set.  */
-  int base_known = 0;
+  gdb::optional<CORE_ADDR> base_address;
 
   /* The language we are debugging.  */
   enum language language = language_unknown;
@@ -527,6 +523,9 @@ public:
      whether the DW_AT_ranges attribute came from the skeleton or DWO.  */
   ULONGEST ranges_base = 0;
 
+  /* The DW_AT_loclists_base attribute if present.  */
+  ULONGEST loclist_base = 0;
+
   /* When reading debug info generated by older versions of rustc, we
      have to rewrite some union types to be struct types with a
      variant part.  This rewriting must be done after the CU is fully
@@ -608,9 +607,7 @@ struct type_unit_group
   /* dwarf2read.c's main "handle" on a TU symtab.
      To simplify things we create an artificial CU that "includes" all the
      type units using this stmt_list so that the rest of the code still has
-     a "per_cu" handle on the symtab.
-     This PER_CU is recognized by having no section.  */
-#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
+     a "per_cu" handle on the symtab.  */
   struct dwarf2_per_cu_data per_cu;
 
   /* The TUs that share this DW_AT_stmt_list entry.
@@ -626,10 +623,6 @@ struct type_unit_group
   /* The data used to construct the hash key.  */
   struct stmt_list_hash hash;
 
-  /* The number of symtabs from the line header.
-     The value here must match line_header.num_file_names.  */
-  unsigned int num_symtabs;
-
   /* The symbol tables for this TU (obtained from the files listed in
      DW_AT_stmt_list).
      WARNING: The order of entries here must match the order of entries
@@ -917,27 +910,28 @@ public:
 
   cutu_reader (struct dwarf2_per_cu_data *this_cu,
               struct abbrev_table *abbrev_table,
-              int use_existing_cu, int keep,
+              int use_existing_cu,
               bool skip_partial);
 
   explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
                        struct dwarf2_cu *parent_cu = nullptr,
                        struct dwo_file *dwo_file = nullptr);
 
-  ~cutu_reader ();
-
   DISABLE_COPY_AND_ASSIGN (cutu_reader);
 
   const gdb_byte *info_ptr = nullptr;
   struct die_info *comp_unit_die = nullptr;
   bool dummy_p = false;
 
+  /* Release the new CU, putting it on the chain.  This cannot be done
+     for dummy CUs.  */
+  void keep ();
+
 private:
   void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
-                                 int use_existing_cu, int keep);
+                                 int use_existing_cu);
 
   struct dwarf2_per_cu_data *m_this_cu;
-  int m_keep = 0;
   std::unique_ptr<dwarf2_cu> m_new_cu;
 
   /* The ordinary abbreviation table.  */
@@ -1083,73 +1077,57 @@ struct partial_die_info : public allocate_on_obstack
     }
   };
 
-/* This data structure holds a complete die structure.  */
-struct die_info
-  {
-    /* DWARF-2 tag for this DIE.  */
-    ENUM_BITFIELD(dwarf_tag) tag : 16;
-
-    /* Number of attributes */
-    unsigned char num_attrs;
-
-    /* True if we're presently building the full type name for the
-       type derived from this DIE.  */
-    unsigned char building_fullname : 1;
-
-    /* True if this die is in process.  PR 16581.  */
-    unsigned char in_process : 1;
-
-    /* True if this DIE has children.  */
-    unsigned char has_children : 1;
-
-    /* Abbrev number */
-    unsigned int abbrev;
-
-    /* Offset in .debug_info or .debug_types section.  */
-    sect_offset sect_off;
-
-    /* The dies in a compilation unit form an n-ary tree.  PARENT
-       points to this die's parent; CHILD points to the first child of
-       this node; and all the children of a given node are chained
-       together via their SIBLING fields.  */
-    struct die_info *child;    /* Its first child, if any.  */
-    struct die_info *sibling;  /* Its next sibling, if any.  */
-    struct die_info *parent;   /* Its parent, if any.  */
-
-    /* An array of attributes, with NUM_ATTRS elements.  There may be
-       zero, but it's not common and zero-sized arrays are not
-       sufficiently portable C.  */
-    struct attribute attrs[1];
-  };
-
 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
    but this would require a corresponding change in unpack_field_as_long
    and friends.  */
 static int bits_per_byte = 8;
 
-/* When reading a variant or variant part, we track a bit more
-   information about the field, and store it in an object of this
-   type.  */
+struct variant_part_builder;
+
+/* When reading a variant, we track a bit more information about the
+   field, and store it in an object of this type.  */
 
 struct variant_field
 {
-  /* If we see a DW_TAG_variant, then this will be the discriminant
-     value.  */
-  ULONGEST discriminant_value;
+  int first_field = -1;
+  int last_field = -1;
+
+  /* A variant can contain other variant parts.  */
+  std::vector<variant_part_builder> variant_parts;
+
   /* If we see a DW_TAG_variant, then this will be set if this is the
      default branch.  */
-  bool default_branch;
-  /* While reading a DW_TAG_variant_part, this will be set if this
-     field is the discriminant.  */
-  bool is_discriminant;
+  bool default_branch = false;
+  /* If we see a DW_AT_discr_value, then this will be the discriminant
+     value.  */
+  ULONGEST discriminant_value = 0;
+  /* If we see a DW_AT_discr_list, then this is a pointer to the list
+     data.  */
+  struct dwarf_block *discr_list_data = nullptr;
+};
+
+/* This represents a DW_TAG_variant_part.  */
+
+struct variant_part_builder
+{
+  /* The offset of the discriminant field.  */
+  sect_offset discriminant_offset {};
+
+  /* Variants that are direct children of this variant part.  */
+  std::vector<variant_field> variants;
+
+  /* True if we're currently reading a variant.  */
+  bool processing_variant = false;
 };
 
 struct nextfield
 {
   int accessibility = 0;
   int virtuality = 0;
-  /* Extra information to describe a variant or variant part.  */
-  struct variant_field variant {};
+  /* Variant parts need to find the discriminant, which is a DIE
+     reference.  We track the section offset of each field to make
+     this link.  */
+  sect_offset offset;
   struct field field {};
 };
 
@@ -1168,9 +1146,6 @@ struct field_info
     std::vector<struct nextfield> fields;
     std::vector<struct nextfield> baseclasses;
 
-    /* Number of fields (including baseclasses).  */
-    int nfields = 0;
-
     /* Set if the accessibility of one of the fields is not public.  */
     int non_public_fields = 0;
 
@@ -1186,6 +1161,19 @@ struct field_info
     /* Nested types defined by this class and the number of elements in this
        list.  */
     std::vector<struct decl_field> nested_types_list;
+
+    /* If non-null, this is the variant part we are currently
+       reading.  */
+    variant_part_builder *current_variant_part = nullptr;
+    /* This holds all the top-level variant parts attached to the type
+       we're reading.  */
+    std::vector<variant_part_builder> variant_parts;
+
+    /* Return the total number of fields (including baseclasses).  */
+    int nfields () const
+    {
+      return fields.size () + baseclasses.size ();
+    }
   };
 
 /* Loaded secondary compilation units are kept in memory until they
@@ -1274,39 +1262,16 @@ static void read_attribute_reprocess (const struct die_reader_specs *reader,
 
 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
 
-static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
-                              unsigned int *);
-
-static LONGEST read_checked_initial_length_and_offset
-  (bfd *, const gdb_byte *, const struct comp_unit_head *,
-   unsigned int *, unsigned int *);
-
-static LONGEST read_offset (bfd *, const gdb_byte *,
-                           const struct comp_unit_head *,
-                           unsigned int *);
-
 static sect_offset read_abbrev_offset
   (struct dwarf2_per_objfile *dwarf2_per_objfile,
    struct dwarf2_section_info *, sect_offset);
 
-static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
-
-static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
-
 static const char *read_indirect_string
   (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
    const struct comp_unit_head *, unsigned int *);
 
-static const char *read_indirect_line_string
-  (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
-   const struct comp_unit_head *, unsigned int *);
-
 static const char *read_indirect_string_at_offset
-  (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
-   LONGEST str_offset);
-
-static const char *read_indirect_string_from_dwz
-  (struct objfile *objfile, struct dwz_file *, LONGEST);
+  (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
 
 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
                                              const gdb_byte *,
@@ -1323,9 +1288,6 @@ static void set_cu_language (unsigned int, struct dwarf2_cu *);
 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
                                      struct dwarf2_cu *);
 
-static struct attribute *dwarf2_attr_no_follow (struct die_info *,
-                                               unsigned int);
-
 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
                                        struct dwarf2_cu *cu);
 
@@ -1404,6 +1366,9 @@ static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
                               struct dwarf2_cu *, dwarf2_psymtab *);
 
+/* Return the .debug_loclists section to use for cu.  */
+static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
+
 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
    values.  Keep the items ordered with increasing constraints compliance.  */
 enum pc_bounds_kind
@@ -1470,7 +1435,8 @@ static const char *namespace_name (struct die_info *die,
 
 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
 
-static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
+static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
+                                bool * = nullptr);
 
 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
                                                       struct dwarf2_cu *);
@@ -1494,7 +1460,7 @@ static const gdb_byte *read_full_die (const struct die_reader_specs *,
 static void process_die (struct die_info *, struct dwarf2_cu *);
 
 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
-                                            struct obstack *);
+                                            struct objfile *);
 
 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
 
@@ -1508,20 +1474,6 @@ static const char *dwarf2_physname (const char *name, struct die_info *die,
 static struct die_info *dwarf2_extension (struct die_info *die,
                                          struct dwarf2_cu **);
 
-static const char *dwarf_tag_name (unsigned int);
-
-static const char *dwarf_attr_name (unsigned int);
-
-static const char *dwarf_unit_type_name (int unit_type);
-
-static const char *dwarf_form_name (unsigned int);
-
-static const char *dwarf_bool_name (unsigned int);
-
-static const char *dwarf_type_encoding_name (unsigned int);
-
-static struct die_info *sibling_die (struct die_info *);
-
 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
 
 static void dump_die_for_error (struct die_info *);
@@ -1534,10 +1486,6 @@ static void dump_die_1 (struct ui_file *, int level, int max_level,
 static void store_in_ref_table (struct die_info *,
                                struct dwarf2_cu *);
 
-static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
-
-static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
-
 static struct die_info *follow_die_ref_or_sig (struct die_info *,
                                               const struct attribute *,
                                               struct dwarf2_cu **);
@@ -1697,19 +1645,9 @@ struct file_and_directory
 static file_and_directory find_file_and_directory (struct die_info *die,
                                                   struct dwarf2_cu *cu);
 
-/* Expected enum dwarf_unit_type for read_comp_unit_head.  */
-enum class rcuh_kind { COMPILE, TYPE };
-
-static const gdb_byte *read_and_check_comp_unit_head
-  (struct dwarf2_per_objfile* dwarf2_per_objfile,
-   struct comp_unit_head *header,
-   struct dwarf2_section_info *section,
-   struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
-   rcuh_kind section_kind);
-
-static htab_up allocate_signatured_type_table (struct objfile *objfile);
+static htab_up allocate_signatured_type_table ();
 
-static htab_up allocate_dwo_unit_table (struct objfile *objfile);
+static htab_up allocate_dwo_unit_table ();
 
 static struct dwo_unit *lookup_dwo_unit_in_dwp
   (struct dwarf2_per_objfile *dwarf2_per_objfile,
@@ -1739,12 +1677,6 @@ static void free_line_header_voidp (void *arg);
 \f
 /* Various complaints about symbol reading that don't abort the process.  */
 
-static void
-dwarf2_statement_list_fits_in_line_number_section_complaint (void)
-{
-  complaint (_("statement list doesn't fit in .debug_line section"));
-}
-
 static void
 dwarf2_debug_line_missing_file_complaint (void)
 {
@@ -1772,23 +1704,6 @@ dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
             arg1, arg2, arg3);
 }
 
-static void
-dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
-{
-  complaint (_("debug info runs off end of %s section"
-              " [in module %s]"),
-            section->get_name (),
-            section->get_file_name ());
-}
-
-static void
-dwarf2_macro_malformed_definition_complaint (const char *arg1)
-{
-  complaint (_("macro debug info contains a "
-              "malformed macro definition:\n`%s'"),
-            arg1);
-}
-
 static void
 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
 {
@@ -2210,6 +2125,29 @@ dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
   if (dwz_bfd == NULL)
     dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
 
+  if (dwz_bfd == nullptr)
+    {
+      gdb::unique_xmalloc_ptr<char> alt_filename;
+      const char *origname = dwarf2_per_objfile->objfile->original_name;
+
+      scoped_fd fd (debuginfod_debuginfo_query (buildid,
+                                               buildid_len,
+                                               origname,
+                                               &alt_filename));
+
+      if (fd.get () >= 0)
+       {
+         /* File successfully retrieved from server.  */
+         dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
+
+         if (dwz_bfd == nullptr)
+           warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
+                    alt_filename.get ());
+         else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
+           dwz_bfd.reset (nullptr);
+       }
+    }
+
   if (dwz_bfd == NULL)
     error (_("could not find '.gnu_debugaltlink' file for %s"),
           objfile_name (dwarf2_per_objfile->objfile));
@@ -2379,7 +2317,7 @@ dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
 
   /* Skip type_unit_groups, reading the type units they contain
      is handled elsewhere.  */
-  if (IS_TYPE_UNIT_GROUP (per_cu))
+  if (per_cu->type_unit_group_p ())
     return;
 
   /* The destructor of dwarf2_queue_guard frees any entries left on
@@ -2556,7 +2494,7 @@ create_signatured_type_table_from_index
   gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
   dwarf2_per_objfile->all_type_units.reserve (elements / 3);
 
-  htab_up sig_types_hash = allocate_signatured_type_table (objfile);
+  htab_up sig_types_hash = allocate_signatured_type_table ();
 
   for (offset_type i = 0; i < elements; i += 3)
     {
@@ -2612,7 +2550,7 @@ create_signatured_type_table_from_debug_names
   gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
   dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
 
-  htab_up sig_types_hash = allocate_signatured_type_table (objfile);
+  htab_up sig_types_hash = allocate_signatured_type_table ();
 
   for (uint32_t i = 0; i < map.tu_count; ++i)
     {
@@ -2660,7 +2598,7 @@ create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
                           struct mapped_index *index)
 {
   struct objfile *objfile = dwarf2_per_objfile->objfile;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   const gdb_byte *iter, *end;
   struct addrmap *mutable_map;
   CORE_ADDR baseaddr;
@@ -2717,7 +2655,7 @@ create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
 {
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   bfd *abfd = objfile->obfd;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   const CORE_ADDR baseaddr = objfile->text_section_offset ();
 
   auto_obstack temp_obstack;
@@ -2947,8 +2885,7 @@ find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
    Returns true if all went well, false otherwise.  */
 
 static bool
-read_gdb_index_from_buffer (struct objfile *objfile,
-                           const char *filename,
+read_gdb_index_from_buffer (const char *filename,
                            bool deprecated_ok,
                            gdb::array_view<const gdb_byte> buffer,
                            struct mapped_index *map,
@@ -3076,7 +3013,7 @@ dwarf2_read_gdb_index
     return 0;
 
   std::unique_ptr<struct mapped_index> map (new struct mapped_index);
-  if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
+  if (!read_gdb_index_from_buffer (objfile_name (objfile),
                                   use_deprecated_index_sections,
                                   main_index_contents, map.get (), &cu_list,
                                   &cu_list_elements, &types_list,
@@ -3102,8 +3039,7 @@ dwarf2_read_gdb_index
       if (dwz_index_content.empty ())
        return 0;
 
-      if (!read_gdb_index_from_buffer (objfile,
-                                      bfd_get_filename (dwz->dwz_bfd.get ()),
+      if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
                                       1, dwz_index_content, &dwz_map,
                                       &dwz_list, &dwz_list_elements,
                                       &dwz_types_ignore,
@@ -3235,7 +3171,7 @@ dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
   /* This should never be called for TUs.  */
   gdb_assert (! this_cu->is_debug_types);
   /* Nor type unit groups.  */
-  gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
+  gdb_assert (! this_cu->type_unit_group_p ());
 
   if (this_cu->v.quick->file_names != NULL)
     return this_cu->v.quick->file_names;
@@ -3721,9 +3657,35 @@ dw2_map_matching_symbols
    gdb::function_view<symbol_found_callback_ftype> callback,
    symbol_compare_ftype *ordered_compare)
 {
-  /* Currently unimplemented; used for Ada.  The function can be called if the
-     current language is Ada for a non-Ada objfile using GNU index.  As Ada
-     does not look for non-Ada symbols this function should just return.  */
+  /* Used for Ada.  */
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
+
+  if (dwarf2_per_objfile->index_table != nullptr)
+    {
+      /* Ada currently doesn't support .gdb_index (see PR24713).  We can get
+        here though if the current language is Ada for a non-Ada objfile
+        using GNU index.  As Ada does not look for non-Ada symbols this
+        function should just return.  */
+      return;
+    }
+
+  /* We have -readnow: no .gdb_index, but no partial symtabs either.  So,
+     inline psym_map_matching_symbols here, assuming all partial symtabs have
+     been read in.  */
+  const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
+
+  for (compunit_symtab *cust : objfile->compunits ())
+    {
+      const struct block *block;
+
+      if (cust == NULL)
+       continue;
+      block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
+      if (!iterate_over_symbols_terminated (block, name,
+                                           domain, callback))
+       return;
+    }
 }
 
 /* Starting from a search name, return the string that finds the upper
@@ -3810,7 +3772,7 @@ mapped_index_base::find_name_components_bounds
     = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
 
   const char *lang_name
-    = lookup_name_without_params.language_lookup_name (lang).c_str ();
+    = lookup_name_without_params.language_lookup_name (lang);
 
   /* Comparison function object for lower_bound that matches against a
      given symbol name.  */
@@ -3980,11 +3942,11 @@ dw2_expand_symtabs_matching_symbol
   struct name_and_matcher
   {
     symbol_name_matcher_ftype *matcher;
-    const std::string &name;
+    const char *name;
 
     bool operator== (const name_and_matcher &other) const
     {
-      return matcher == other.matcher && name == other.name;
+      return matcher == other.matcher && strcmp (name, other.name) == 0;
     }
   };
 
@@ -4672,7 +4634,7 @@ static void
 dw2_expand_symtabs_matching
   (struct objfile *objfile,
    gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
-   const lookup_name_info &lookup_name,
+   const lookup_name_info *lookup_name,
    gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
    gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
    enum search_domain kind)
@@ -4686,9 +4648,21 @@ dw2_expand_symtabs_matching
 
   dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
 
+  if (symbol_matcher == NULL && lookup_name == NULL)
+    {
+      for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
+       {
+         QUIT;
+
+         dw2_expand_symtabs_matching_one (per_cu, file_matcher,
+                                          expansion_notify);
+       }
+      return;
+    }
+
   mapped_index &index = *dwarf2_per_objfile->index_table;
 
-  dw2_expand_symtabs_matching_symbol (index, lookup_name,
+  dw2_expand_symtabs_matching_symbol (index, *lookup_name,
                                      symbol_matcher,
                                      kind, [&] (offset_type idx)
     {
@@ -4747,7 +4721,7 @@ dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
 
   if (warn_if_readin && data->v.quick->compunit_symtab)
     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
-            paddress (get_objfile_arch (objfile), pc));
+            paddress (objfile->arch (), pc));
 
   result
     = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
@@ -4837,6 +4811,7 @@ const struct quick_symbol_functions dwarf2_gdb_index_functions =
   dw2_forget_cached_source_info,
   dw2_map_symtabs_matching_filename,
   dw2_lookup_symbol,
+  NULL,
   dw2_print_stats,
   dw2_dump,
   dw2_expand_symtabs_for_function,
@@ -4876,7 +4851,7 @@ read_debug_names_from_section (struct objfile *objfile,
 
   section->read (objfile);
 
-  map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
+  map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
 
   const gdb_byte *addr = section->buffer;
 
@@ -5047,6 +5022,26 @@ create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
                                  dwarf2_section_info &section,
                                  bool is_dwz)
 {
+  if (!map.augmentation_is_gdb)
+    {
+    for (uint32_t i = 0; i < map.cu_count; ++i)
+      {
+       sect_offset sect_off
+         = (sect_offset) (extract_unsigned_integer
+                          (map.cu_table_reordered + i * map.offset_size,
+                           map.offset_size,
+                           map.dwarf5_byte_order));
+       /* We don't know the length of the CU, because the CU list in a
+          .debug_names index can be incomplete, so we can't use the start of
+          the next CU as end of this CU.  We create the CUs here with length 0,
+          and in cutu_reader::cutu_reader we'll fill in the actual length.  */
+       dwarf2_per_cu_data *per_cu
+         = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
+                                      sect_off, 0);
+       dwarf2_per_objfile->all_comp_units.push_back (per_cu);
+      }
+    }
+
   sect_offset sect_off_prev;
   for (uint32_t i = 0; i <= map.cu_count; ++i)
     {
@@ -5218,8 +5213,8 @@ mapped_debug_names::namei_to_name (uint32_t namei) const
                                 + namei * offset_size),
                                offset_size,
                                dwarf5_byte_order);
-  return read_indirect_string_at_offset
-    (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
+  return read_indirect_string_at_offset (dwarf2_per_objfile,
+                                        namei_string_offs);
 }
 
 /* Find a slot in .debug_names for the object named NAME.  If NAME is
@@ -5377,6 +5372,18 @@ dw2_debug_names_iterator::next ()
          ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
          m_addr += bytes_read;
          break;
+       case DW_FORM_ref4:
+         ull = read_4_bytes (abfd, m_addr);
+         m_addr += 4;
+         break;
+       case DW_FORM_ref8:
+         ull = read_8_bytes (abfd, m_addr);
+         m_addr += 8;
+         break;
+       case DW_FORM_ref_sig8:
+         ull = read_8_bytes (abfd, m_addr);
+         m_addr += 8;
+         break;
        default:
          complaint (_("Unsupported .debug_names form %s [in module %s]"),
                     dwarf_form_name (attr.form),
@@ -5409,6 +5416,12 @@ dw2_debug_names_iterator::next ()
            }
          per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
          break;
+       case DW_IDX_die_offset:
+         /* In a per-CU index (as opposed to a per-module index), index
+            entries without CU attribute implicitly refer to the single CU.  */
+         if (per_cu == NULL)
+           per_cu = dwarf2_per_objfile->get_cu (0);
+         break;
        case DW_IDX_GNU_internal:
          if (!m_map.augmentation_is_gdb)
            break;
@@ -5680,7 +5693,7 @@ static void
 dw2_debug_names_expand_symtabs_matching
   (struct objfile *objfile,
    gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
-   const lookup_name_info &lookup_name,
+   const lookup_name_info *lookup_name,
    gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
    gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
    enum search_domain kind)
@@ -5694,9 +5707,21 @@ dw2_debug_names_expand_symtabs_matching
 
   dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
 
+  if (symbol_matcher == NULL && lookup_name == NULL)
+    {
+      for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
+       {
+         QUIT;
+
+         dw2_expand_symtabs_matching_one (per_cu, file_matcher,
+                                          expansion_notify);
+       }
+      return;
+    }
+
   mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
 
-  dw2_expand_symtabs_matching_symbol (map, lookup_name,
+  dw2_expand_symtabs_matching_symbol (map, *lookup_name,
                                      symbol_matcher,
                                      kind, [&] (offset_type namei)
     {
@@ -5719,6 +5744,7 @@ const struct quick_symbol_functions dwarf2_debug_names_functions =
   dw2_forget_cached_source_info,
   dw2_map_symtabs_matching_filename,
   dw2_debug_names_lookup_symbol,
+  NULL,
   dw2_print_stats,
   dw2_debug_names_dump,
   dw2_debug_names_expand_symtabs_for_function,
@@ -5880,25 +5906,6 @@ dwarf2_build_psymtabs (struct objfile *objfile)
     }
 }
 
-/* Return the total length of the CU described by HEADER.  */
-
-static unsigned int
-get_cu_length (const struct comp_unit_head *header)
-{
-  return header->initial_length_size + header->length;
-}
-
-/* Return TRUE if SECT_OFF is within CU_HEADER.  */
-
-static inline bool
-offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
-{
-  sect_offset bottom = cu_header->sect_off;
-  sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
-
-  return sect_off >= bottom && sect_off < top;
-}
-
 /* Find the base address of the compilation unit for range lists and
    location lists.  It will normally be specified by DW_AT_low_pc.
    In DWARF-3 draft 4, the base address could be overridden by
@@ -5910,138 +5917,17 @@ dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
 {
   struct attribute *attr;
 
-  cu->base_known = 0;
-  cu->base_address = 0;
+  cu->base_address.reset ();
 
   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
   if (attr != nullptr)
-    {
-      cu->base_address = attr->value_as_address ();
-      cu->base_known = 1;
-    }
+    cu->base_address = attr->value_as_address ();
   else
     {
       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
       if (attr != nullptr)
-       {
-         cu->base_address = attr->value_as_address ();
-         cu->base_known = 1;
-       }
-    }
-}
-
-/* Read in the comp unit header information from the debug_info at info_ptr.
-   Use rcuh_kind::COMPILE as the default type if not known by the caller.
-   NOTE: This leaves members offset, first_die_offset to be filled in
-   by the caller.  */
-
-static const gdb_byte *
-read_comp_unit_head (struct comp_unit_head *cu_header,
-                    const gdb_byte *info_ptr,
-                    struct dwarf2_section_info *section,
-                    rcuh_kind section_kind)
-{
-  int signed_addr;
-  unsigned int bytes_read;
-  const char *filename = section->get_file_name ();
-  bfd *abfd = section->get_bfd_owner ();
-
-  cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
-  cu_header->initial_length_size = bytes_read;
-  cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
-  info_ptr += bytes_read;
-  cu_header->version = read_2_bytes (abfd, info_ptr);
-  if (cu_header->version < 2 || cu_header->version > 5)
-    error (_("Dwarf Error: wrong version in compilation unit header "
-          "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
-          cu_header->version, filename);
-  info_ptr += 2;
-  if (cu_header->version < 5)
-    switch (section_kind)
-      {
-      case rcuh_kind::COMPILE:
-       cu_header->unit_type = DW_UT_compile;
-       break;
-      case rcuh_kind::TYPE:
-       cu_header->unit_type = DW_UT_type;
-       break;
-      default:
-       internal_error (__FILE__, __LINE__,
-                       _("read_comp_unit_head: invalid section_kind"));
-      }
-  else
-    {
-      cu_header->unit_type = static_cast<enum dwarf_unit_type>
-                                                (read_1_byte (abfd, info_ptr));
-      info_ptr += 1;
-      switch (cu_header->unit_type)
-       {
-       case DW_UT_compile:
-       case DW_UT_partial:
-       case DW_UT_skeleton:
-       case DW_UT_split_compile:
-         if (section_kind != rcuh_kind::COMPILE)
-           error (_("Dwarf Error: wrong unit_type in compilation unit header "
-                  "(is %s, should be %s) [in module %s]"),
-                  dwarf_unit_type_name (cu_header->unit_type),
-                  dwarf_unit_type_name (DW_UT_type), filename);
-         break;
-       case DW_UT_type:
-       case DW_UT_split_type:
-         section_kind = rcuh_kind::TYPE;
-         break;
-       default:
-         error (_("Dwarf Error: wrong unit_type in compilation unit header "
-                "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
-                "[in module %s]"), cu_header->unit_type,
-                dwarf_unit_type_name (DW_UT_compile),
-                dwarf_unit_type_name (DW_UT_skeleton),
-                dwarf_unit_type_name (DW_UT_split_compile),
-                dwarf_unit_type_name (DW_UT_type),
-                dwarf_unit_type_name (DW_UT_split_type), filename);
-       }
-
-      cu_header->addr_size = read_1_byte (abfd, info_ptr);
-      info_ptr += 1;
-    }
-  cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
-                                                         cu_header,
-                                                         &bytes_read);
-  info_ptr += bytes_read;
-  if (cu_header->version < 5)
-    {
-      cu_header->addr_size = read_1_byte (abfd, info_ptr);
-      info_ptr += 1;
-    }
-  signed_addr = bfd_get_sign_extend_vma (abfd);
-  if (signed_addr < 0)
-    internal_error (__FILE__, __LINE__,
-                   _("read_comp_unit_head: dwarf from non elf file"));
-  cu_header->signed_addr_p = signed_addr;
-
-  bool header_has_signature = section_kind == rcuh_kind::TYPE
-    || cu_header->unit_type == DW_UT_skeleton
-    || cu_header->unit_type == DW_UT_split_compile;
-
-  if (header_has_signature)
-    {
-      cu_header->signature = read_8_bytes (abfd, info_ptr);
-      info_ptr += 8;
-    }
-
-  if (section_kind == rcuh_kind::TYPE)
-    {
-      LONGEST type_offset;
-      type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
-      info_ptr += bytes_read;
-      cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
-      if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
-       error (_("Dwarf Error: Too big type_offset in compilation unit "
-              "header (is %s) [in module %s]"), plongest (type_offset),
-              filename);
+       cu->base_address = attr->value_as_address ();
     }
-
-  return info_ptr;
 }
 
 /* Helper function that returns the proper abbrev section for
@@ -6061,62 +5947,6 @@ get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
   return abbrev;
 }
 
-/* Subroutine of read_and_check_comp_unit_head and
-   read_and_check_type_unit_head to simplify them.
-   Perform various error checking on the header.  */
-
-static void
-error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
-                           struct comp_unit_head *header,
-                           struct dwarf2_section_info *section,
-                           struct dwarf2_section_info *abbrev_section)
-{
-  const char *filename = section->get_file_name ();
-
-  if (to_underlying (header->abbrev_sect_off)
-      >= abbrev_section->get_size (dwarf2_per_objfile->objfile))
-    error (_("Dwarf Error: bad offset (%s) in compilation unit header "
-          "(offset %s + 6) [in module %s]"),
-          sect_offset_str (header->abbrev_sect_off),
-          sect_offset_str (header->sect_off),
-          filename);
-
-  /* Cast to ULONGEST to use 64-bit arithmetic when possible to
-     avoid potential 32-bit overflow.  */
-  if (((ULONGEST) header->sect_off + get_cu_length (header))
-      > section->size)
-    error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
-          "(offset %s + 0) [in module %s]"),
-          header->length, sect_offset_str (header->sect_off),
-          filename);
-}
-
-/* Read in a CU/TU header and perform some basic error checking.
-   The contents of the header are stored in HEADER.
-   The result is a pointer to the start of the first DIE.  */
-
-static const gdb_byte *
-read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
-                              struct comp_unit_head *header,
-                              struct dwarf2_section_info *section,
-                              struct dwarf2_section_info *abbrev_section,
-                              const gdb_byte *info_ptr,
-                              rcuh_kind section_kind)
-{
-  const gdb_byte *beg_of_comp_unit = info_ptr;
-
-  header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
-
-  info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
-
-  header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
-
-  error_check_comp_unit_head (dwarf2_per_objfile, header, section,
-                             abbrev_section);
-
-  return info_ptr;
-}
-
 /* Fetch the abbreviation table offset from a comp or type unit header.  */
 
 static sect_offset
@@ -6146,6 +5976,58 @@ read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
   return (sect_offset) read_offset (abfd, info_ptr, offset_size);
 }
 
+/* A partial symtab that is used only for include files.  */
+struct dwarf2_include_psymtab : public partial_symtab
+{
+  dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
+    : partial_symtab (filename, objfile)
+  {
+  }
+
+  void read_symtab (struct objfile *objfile) override
+  {
+    /* It's an include file, no symbols to read for it.
+       Everything is in the includer symtab.  */
+
+    /* The expansion of a dwarf2_include_psymtab is just a trigger for
+       expansion of the includer psymtab.  We use the dependencies[0] field to
+       model the includer.  But if we go the regular route of calling
+       expand_psymtab here, and having expand_psymtab call expand_dependencies
+       to expand the includer, we'll only use expand_psymtab on the includer
+       (making it a non-toplevel psymtab), while if we expand the includer via
+       another path, we'll use read_symtab (making it a toplevel psymtab).
+       So, don't pretend a dwarf2_include_psymtab is an actual toplevel
+       psymtab, and trigger read_symtab on the includer here directly.  */
+    includer ()->read_symtab (objfile);
+  }
+
+  void expand_psymtab (struct objfile *objfile) override
+  {
+    /* This is not called by read_symtab, and should not be called by any
+       expand_dependencies.  */
+    gdb_assert (false);
+  }
+
+  bool readin_p () const override
+  {
+    return includer ()->readin_p ();
+  }
+
+  struct compunit_symtab *get_compunit_symtab () const override
+  {
+    return nullptr;
+  }
+
+private:
+  partial_symtab *includer () const
+  {
+    /* An include psymtab has exactly one dependency: the psymtab that
+       includes it.  */
+    gdb_assert (this->number_of_dependencies == 1);
+    return this->dependencies[0];
+  }
+};
+
 /* Allocate a new partial symtab for file named NAME and mark this new
    partial symtab as being an include of PST.  */
 
@@ -6153,7 +6035,7 @@ static void
 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
                                struct objfile *objfile)
 {
-  dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
+  dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
 
   if (!IS_ABSOLUTE_PATH (subpst->filename))
     {
@@ -6164,11 +6046,6 @@ dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
   subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
   subpst->dependencies[0] = pst;
   subpst->number_of_dependencies = 1;
-
-  /* No private part is necessary for include psymtabs.  This property
-     can be used to differentiate between such include psymtabs and
-     the regular ones.  */
-  subpst->per_cu_data = nullptr;
 }
 
 /* Read the Line Number Program data and extract the list of files
@@ -6219,7 +6096,7 @@ eq_signatured_type (const void *item_lhs, const void *item_rhs)
 /* Allocate a hash table for signatured types.  */
 
 static htab_up
-allocate_signatured_type_table (struct objfile *objfile)
+allocate_signatured_type_table ()
 {
   return htab_up (htab_create_alloc (41,
                                     hash_signatured_type,
@@ -6300,7 +6177,7 @@ create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
       ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
                                           abbrev_section, ptr, section_kind);
 
-      length = get_cu_length (&header);
+      length = header.get_length ();
 
       /* Skip dummy type units.  */
       if (ptr >= info_ptr + length
@@ -6314,9 +6191,9 @@ create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
       if (types_htab == NULL)
        {
          if (dwo_file)
-           types_htab = allocate_dwo_unit_table (objfile);
+           types_htab = allocate_dwo_unit_table ();
          else
-           types_htab = allocate_signatured_type_table (objfile);
+           types_htab = allocate_signatured_type_table ();
        }
 
       if (dwo_file)
@@ -6527,7 +6404,6 @@ lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
 {
   struct dwarf2_per_objfile *dwarf2_per_objfile
     = cu->per_cu->dwarf2_per_objfile;
-  struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct dwo_file *dwo_file;
   struct dwo_unit find_dwo_entry, *dwo_entry;
   struct signatured_type find_sig_entry, *sig_entry;
@@ -6538,10 +6414,7 @@ lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
   /* If TU skeletons have been removed then we may not have read in any
      TUs yet.  */
   if (dwarf2_per_objfile->signatured_types == NULL)
-    {
-      dwarf2_per_objfile->signatured_types
-       = allocate_signatured_type_table (objfile);
-    }
+    dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
 
   /* We only ever need to read in one copy of a signatured type.
      Use the global signatured_types array to do our own comdat-folding
@@ -6598,7 +6471,6 @@ lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
 {
   struct dwarf2_per_objfile *dwarf2_per_objfile
     = cu->per_cu->dwarf2_per_objfile;
-  struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
   struct dwo_unit *dwo_entry;
   struct signatured_type find_sig_entry, *sig_entry;
@@ -6610,10 +6482,7 @@ lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
   /* If TU skeletons have been removed then we may not have read in any
      TUs yet.  */
   if (dwarf2_per_objfile->signatured_types == NULL)
-    {
-      dwarf2_per_objfile->signatured_types
-       = allocate_signatured_type_table (objfile);
-    }
+    dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
 
   find_sig_entry.signature = sig;
   slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
@@ -6673,34 +6542,6 @@ lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
     }
 }
 
-/* Return the address base of the compile unit, which, if exists, is stored
-   either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base.  */
-static gdb::optional<ULONGEST>
-lookup_addr_base (struct die_info *comp_unit_die)
-{
-  struct attribute *attr;
-  attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
-  if (attr == nullptr)
-    attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
-  if (attr == nullptr)
-    return gdb::optional<ULONGEST> ();
-  return DW_UNSND (attr);
-}
-
-/* Return range lists base of the compile unit, which, if exists, is stored
-   either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base.  */
-static ULONGEST
-lookup_ranges_base (struct die_info *comp_unit_die)
-{
-  struct attribute *attr;
-  attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
-  if (attr == nullptr)
-    attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
-  if (attr == nullptr)
-    return 0;
-  return DW_UNSND (attr);
-}
-
 /* Low level DIE reading support.  */
 
 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
@@ -6789,12 +6630,12 @@ read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
       ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
       comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
 
-      cu->addr_base = lookup_addr_base (stub_comp_unit_die);
+      cu->addr_base = stub_comp_unit_die->addr_base ();
 
       /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
         here (if needed). We need the value before we can process
         DW_AT_ranges.  */
-      cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
+      cu->ranges_base = stub_comp_unit_die->ranges_base ();
     }
   else if (stub_comp_dir != NULL)
     {
@@ -6836,7 +6677,7 @@ read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
       gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
       /* For DWOs coming from DWP files, we don't know the CU length
         nor the type's offset in the TU until now.  */
-      dwo_unit->length = get_cu_length (&cu->header);
+      dwo_unit->length = cu->header.get_length ();
       dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
 
       /* Establish the type offset that can be used to lookup the type.
@@ -6853,7 +6694,7 @@ read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
       gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
       /* For DWOs coming from DWP files, we don't know the CU length
         until now.  */
-      dwo_unit->length = get_cu_length (&cu->header);
+      dwo_unit->length = cu->header.get_length ();
     }
 
   *result_dwo_abbrev_table
@@ -6971,10 +6812,9 @@ lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
 
 void
 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
-                                       int use_existing_cu, int keep)
+                                       int use_existing_cu)
 {
   struct signatured_type *sig_type;
-  struct die_reader_specs reader;
 
   /* Verify we can do the following downcast, and that we have the
      data we need.  */
@@ -7002,7 +6842,7 @@ cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
   if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
                              NULL /* stub_comp_unit_die */,
                              sig_type->dwo_unit->dwo_file->comp_dir,
-                             &reader, &info_ptr,
+                             this, &info_ptr,
                              &comp_unit_die,
                              &m_dwo_abbrev_table) == 0)
     {
@@ -7019,19 +6859,14 @@ cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
    This is an optimization for when we already have the abbrev table.
 
    If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
-   Otherwise, a new CU is allocated with xmalloc.
-
-   If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
-   read_in_chain.  Otherwise the dwarf2_cu data is freed at the
-   end.  */
+   Otherwise, a new CU is allocated with xmalloc.  */
 
 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
                          struct abbrev_table *abbrev_table,
-                         int use_existing_cu, int keep,
+                         int use_existing_cu,
                          bool skip_partial)
   : die_reader_specs {},
-    m_this_cu (this_cu),
-    m_keep (keep)
+    m_this_cu (this_cu)
 {
   struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
@@ -7051,9 +6886,6 @@ cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
                        this_cu->is_debug_types ? "type" : "comp",
                        sect_offset_str (this_cu->sect_off));
 
-  if (use_existing_cu)
-    gdb_assert (keep);
-
   /* If we're reading a TU directly from a DWO file, including a virtual DWO
      file (instead of going through the stub), short-circuit all of this.  */
   if (this_cu->reading_dwo_directly)
@@ -7061,7 +6893,7 @@ cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
       /* Narrow down the scope of possibilities to have to understand.  */
       gdb_assert (this_cu->is_debug_types);
       gdb_assert (abbrev_table == NULL);
-      init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
+      init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
       return;
     }
 
@@ -7117,7 +6949,7 @@ cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
 
          /* LENGTH has not been set yet for type units if we're
             using .gdb_index.  */
-         this_cu->length = get_cu_length (&cu->header);
+         this_cu->length = cu->header.get_length ();
 
          /* Establish the type offset that can be used to lookup the type.  */
          sig_type->type_offset_in_section =
@@ -7134,7 +6966,10 @@ cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
                                                    rcuh_kind::COMPILE);
 
          gdb_assert (this_cu->sect_off == cu->header.sect_off);
-         gdb_assert (this_cu->length == get_cu_length (&cu->header));
+         if (this_cu->length == 0)
+           this_cu->length = cu->header.get_length ();
+         else
+           gdb_assert (this_cu->length == cu->header.get_length ());
          this_cu->dwarf_version = cu->header.version;
        }
     }
@@ -7217,10 +7052,12 @@ cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
     }
 }
 
-cutu_reader::~cutu_reader ()
+void
+cutu_reader::keep ()
 {
   /* Done, clean up.  */
-  if (m_new_cu != NULL && m_keep && !dummy_p)
+  gdb_assert (!dummy_p);
+  if (m_new_cu != NULL)
     {
       struct dwarf2_per_objfile *dwarf2_per_objfile
        = m_this_cu->dwarf2_per_objfile;
@@ -7290,7 +7127,7 @@ cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
       m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
       m_new_cu->addr_base = parent_cu->addr_base;
     }
-  this_cu->length = get_cu_length (&m_new_cu->header);
+  this_cu->length = m_new_cu->header.get_length ();
 
   /* Skip dummy compilation units.  */
   if (info_ptr >= begin_info_ptr + this_cu->length
@@ -7339,7 +7176,7 @@ eq_type_unit_group (const void *item_lhs, const void *item_rhs)
 /* Allocate a hash table for type unit groups.  */
 
 static htab_up
-allocate_type_unit_groups_table (struct objfile *objfile)
+allocate_type_unit_groups_table ()
 {
   return htab_up (htab_create_alloc (3,
                                     hash_type_unit_group,
@@ -7413,10 +7250,7 @@ get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
   struct type_unit_group type_unit_group_for_lookup;
 
   if (dwarf2_per_objfile->type_unit_groups == NULL)
-    {
-      dwarf2_per_objfile->type_unit_groups =
-       allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
-    }
+    dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
 
   /* Do we need to create a new group, or can we use an existing one?  */
 
@@ -7471,12 +7305,11 @@ create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
   struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
   dwarf2_psymtab *pst;
 
-  pst = new dwarf2_psymtab (name, objfile, 0);
+  pst = new dwarf2_psymtab (name, objfile, per_cu);
 
   pst->psymtabs_addrmap_supported = true;
 
   /* This is the glue that links PST into GDB's symbol API.  */
-  pst->per_cu_data = per_cu;
   per_cu->v.psymtab = pst;
 
   return pst;
@@ -7488,12 +7321,11 @@ static void
 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
                                  const gdb_byte *info_ptr,
                                  struct die_info *comp_unit_die,
-                                 int want_partial_unit,
                                  enum language pretend_language)
 {
   struct dwarf2_cu *cu = reader->cu;
   struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
   CORE_ADDR baseaddr;
   CORE_ADDR best_lowpc = 0, best_highpc = 0;
@@ -7501,17 +7333,23 @@ process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
   enum pc_bounds_kind cu_bounds_kind;
   const char *filename;
 
-  if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
-    return;
-
   gdb_assert (! per_cu->is_debug_types);
 
   prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
 
   /* Allocate a new partial symbol table structure.  */
+  gdb::unique_xmalloc_ptr<char> debug_filename;
+  static const char artificial[] = "<artificial>";
   filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
   if (filename == NULL)
     filename = "";
+  else if (strcmp (filename, artificial) == 0)
+    {
+      debug_filename.reset (concat (artificial, "@",
+                                   sect_offset_str (per_cu->sect_off),
+                                   (char *) NULL));
+      filename = debug_filename.get ();
+    }
 
   pst = create_partial_symtab (per_cu, filename);
 
@@ -7617,7 +7455,7 @@ process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
 
 static void
 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
-                          int want_partial_unit,
+                          bool want_partial_unit,
                           enum language pretend_language)
 {
   /* If this compilation unit was already read in, free the
@@ -7628,7 +7466,19 @@ process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
   if (this_cu->cu != NULL)
     free_one_cached_comp_unit (this_cu);
 
-  cutu_reader reader (this_cu, NULL, 0, 0, false);
+  cutu_reader reader (this_cu, NULL, 0, false);
+
+  switch (reader.comp_unit_die->tag)
+    {
+    case DW_TAG_compile_unit:
+      this_cu->unit_type = DW_UT_compile;
+      break;
+    case DW_TAG_partial_unit:
+      this_cu->unit_type = DW_UT_partial;
+      break;
+    default:
+      abort ();
+    }
 
   if (reader.dummy_p)
     {
@@ -7637,12 +7487,14 @@ process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
   else if (this_cu->is_debug_types)
     build_type_psymtabs_reader (&reader, reader.info_ptr,
                                reader.comp_unit_die);
-  else
+  else if (want_partial_unit
+          || reader.comp_unit_die->tag != DW_TAG_partial_unit)
     process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
                                      reader.comp_unit_die,
-                                     want_partial_unit,
                                      pretend_language);
 
+  this_cu->lang = this_cu->cu->language;
+
   /* Age out any secondary CUs.  */
   age_cached_comp_units (this_cu->dwarf2_per_objfile);
 }
@@ -7672,7 +7524,7 @@ build_type_psymtabs_reader (const struct die_reader_specs *reader,
   if (! type_unit_die->has_children)
     return;
 
-  attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
+  attr = type_unit_die->attr (DW_AT_stmt_list);
   tu_group = get_type_unit_group (cu, attr);
 
   if (tu_group->tus == nullptr)
@@ -7797,7 +7649,7 @@ build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
        }
 
       cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
-                         0, 0, false);
+                         0, false);
       if (!reader.dummy_p)
        build_type_psymtabs_reader (&reader, reader.info_ptr,
                                    reader.comp_unit_die);
@@ -7841,7 +7693,7 @@ build_type_psymtab_dependencies (void **slot, void *info)
   int i;
 
   gdb_assert (len > 0);
-  gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
+  gdb_assert (per_cu->type_unit_group_p ());
 
   pst->number_of_dependencies = len;
   pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
@@ -7885,10 +7737,7 @@ process_skeletonless_type_unit (void **slot, void *info)
   /* If this TU doesn't exist in the global table, add it and read it in.  */
 
   if (dwarf2_per_objfile->signatured_types == NULL)
-    {
-      dwarf2_per_objfile->signatured_types
-       = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
-    }
+    dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
 
   find_entry.signature = dwo_unit->signature;
   slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
@@ -7905,7 +7754,7 @@ process_skeletonless_type_unit (void **slot, void *info)
   *slot = entry;
 
   /* This does the job that build_type_psymtabs_1 would have done.  */
-  cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
+  cutu_reader reader (&entry->per_cu, NULL, 0, false);
   if (!reader.dummy_p)
     build_type_psymtabs_reader (&reader, reader.info_ptr,
                                reader.comp_unit_die);
@@ -7979,7 +7828,9 @@ dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
                          objfile_name (objfile));
     }
 
-  dwarf2_per_objfile->reading_partial_symbols = 1;
+  scoped_restore restore_reading_psyms
+    = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
+                          true);
 
   dwarf2_per_objfile->info.read (objfile);
 
@@ -8000,7 +7851,12 @@ dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
                           addrmap_create_mutable (&temp_obstack));
 
   for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
-    process_psymtab_comp_unit (per_cu, 0, language_minimal);
+    {
+      if (per_cu->v.psymtab != NULL)
+       /* In case a forward DW_TAG_imported_unit has read the CU already.  */
+       continue;
+      process_psymtab_comp_unit (per_cu, false, language_minimal);
+    }
 
   /* This has to wait until we read the CUs, we need the list of DWOs.  */
   process_skeletonless_type_units (dwarf2_per_objfile);
@@ -8034,7 +7890,7 @@ dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
 static void
 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
 {
-  cutu_reader reader (this_cu, NULL, 1, 1, false);
+  cutu_reader reader (this_cu, NULL, 1, false);
 
   if (!reader.dummy_p)
     {
@@ -8046,6 +7902,8 @@ load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
         If not, there's no more debug_info for this comp unit.  */
       if (reader.comp_unit_die->has_children)
        load_partial_dies (&reader, reader.info_ptr, 0);
+
+      reader.keep ();
     }
 }
 
@@ -8165,7 +8023,8 @@ scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
            case DW_TAG_variable:
            case DW_TAG_typedef:
            case DW_TAG_union_type:
-             if (!pdi->is_declaration)
+             if (!pdi->is_declaration
+                 || (pdi->tag == DW_TAG_variable && pdi->is_external))
                {
                  add_partial_symbol (pdi, cu);
                }
@@ -8217,7 +8076,7 @@ scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
 
                /* Go read the partial unit, if needed.  */
                if (per_cu->v.psymtab == NULL)
-                 process_psymtab_comp_unit (per_cu, 1, cu->language);
+                 process_psymtab_comp_unit (per_cu, true, cu->language);
 
                cu->per_cu->imported_symtabs_push (per_cu);
              }
@@ -8386,7 +8245,7 @@ add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
   struct dwarf2_per_objfile *dwarf2_per_objfile
     = cu->per_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   CORE_ADDR addr = 0;
   const char *actual_name = NULL;
   CORE_ADDR baseaddr;
@@ -8401,6 +8260,15 @@ add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
   if (actual_name == NULL)
     actual_name = pdi->name;
 
+  partial_symbol psymbol;
+  memset (&psymbol, 0, sizeof (psymbol));
+  psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
+  psymbol.ginfo.section = -1;
+
+  /* The code below indicates that the psymbol should be installed by
+     setting this.  */
+  gdb::optional<psymbol_placement> where;
+
   switch (pdi->tag)
     {
     case DW_TAG_inlined_subroutine:
@@ -8417,34 +8285,25 @@ add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
              But in Ada and Fortran, we want to be able to access nested
              procedures globally.  So all Ada and Fortran subprograms are
              stored in the global scope.  */
-         add_psymbol_to_list (actual_name,
-                              built_actual_name != NULL,
-                              VAR_DOMAIN, LOC_BLOCK,
-                              SECT_OFF_TEXT (objfile),
-                              psymbol_placement::GLOBAL,
-                              addr,
-                              cu->language, objfile);
+         where = psymbol_placement::GLOBAL;
        }
       else
-       {
-         add_psymbol_to_list (actual_name,
-                              built_actual_name != NULL,
-                              VAR_DOMAIN, LOC_BLOCK,
-                              SECT_OFF_TEXT (objfile),
-                              psymbol_placement::STATIC,
-                              addr, cu->language, objfile);
-       }
+       where = psymbol_placement::STATIC;
+
+      psymbol.domain = VAR_DOMAIN;
+      psymbol.aclass = LOC_BLOCK;
+      psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
+      psymbol.ginfo.value.address = addr;
 
       if (pdi->main_subprogram && actual_name != NULL)
        set_objfile_main_name (objfile, actual_name, cu->language);
       break;
     case DW_TAG_constant:
-      add_psymbol_to_list (actual_name,
-                          built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
-                          -1, (pdi->is_external
-                               ? psymbol_placement::GLOBAL
-                               : psymbol_placement::STATIC),
-                          0, cu->language, objfile);
+      psymbol.domain = VAR_DOMAIN;
+      psymbol.aclass = LOC_STATIC;
+      where = (pdi->is_external
+              ? psymbol_placement::GLOBAL
+              : psymbol_placement::STATIC);
       break;
     case DW_TAG_variable:
       if (pdi->d.locdesc)
@@ -8475,12 +8334,13 @@ add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
             table building.  */
 
          if (pdi->d.locdesc || pdi->has_type)
-           add_psymbol_to_list (actual_name,
-                                built_actual_name != NULL,
-                                VAR_DOMAIN, LOC_STATIC,
-                                SECT_OFF_TEXT (objfile),
-                                psymbol_placement::GLOBAL,
-                                addr, cu->language, objfile);
+           {
+             psymbol.domain = VAR_DOMAIN;
+             psymbol.aclass = LOC_STATIC;
+             psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
+             psymbol.ginfo.value.address = addr;
+             where = psymbol_placement::GLOBAL;
+           }
        }
       else
        {
@@ -8491,42 +8351,37 @@ add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
          if (!has_loc && !pdi->has_const_value)
            return;
 
-         add_psymbol_to_list (actual_name,
-                              built_actual_name != NULL,
-                              VAR_DOMAIN, LOC_STATIC,
-                              SECT_OFF_TEXT (objfile),
-                              psymbol_placement::STATIC,
-                              has_loc ? addr : 0,
-                              cu->language, objfile);
+         psymbol.domain = VAR_DOMAIN;
+         psymbol.aclass = LOC_STATIC;
+         psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
+         if (has_loc)
+           psymbol.ginfo.value.address = addr;
+         where = psymbol_placement::STATIC;
        }
       break;
     case DW_TAG_typedef:
     case DW_TAG_base_type:
     case DW_TAG_subrange_type:
-      add_psymbol_to_list (actual_name,
-                          built_actual_name != NULL,
-                          VAR_DOMAIN, LOC_TYPEDEF, -1,
-                          psymbol_placement::STATIC,
-                          0, cu->language, objfile);
+      psymbol.domain = VAR_DOMAIN;
+      psymbol.aclass = LOC_TYPEDEF;
+      where = psymbol_placement::STATIC;
       break;
     case DW_TAG_imported_declaration:
     case DW_TAG_namespace:
-      add_psymbol_to_list (actual_name,
-                          built_actual_name != NULL,
-                          VAR_DOMAIN, LOC_TYPEDEF, -1,
-                          psymbol_placement::GLOBAL,
-                          0, cu->language, objfile);
+      psymbol.domain = VAR_DOMAIN;
+      psymbol.aclass = LOC_TYPEDEF;
+      where = psymbol_placement::GLOBAL;
       break;
     case DW_TAG_module:
       /* With Fortran 77 there might be a "BLOCK DATA" module
          available without any name.  If so, we skip the module as it
          doesn't bring any value.  */
       if (actual_name != nullptr)
-       add_psymbol_to_list (actual_name,
-                            built_actual_name != NULL,
-                            MODULE_DOMAIN, LOC_TYPEDEF, -1,
-                            psymbol_placement::GLOBAL,
-                            0, cu->language, objfile);
+       {
+         psymbol.domain = MODULE_DOMAIN;
+         psymbol.aclass = LOC_TYPEDEF;
+         where = psymbol_placement::GLOBAL;
+       }
       break;
     case DW_TAG_class_type:
     case DW_TAG_interface_type:
@@ -8543,27 +8398,37 @@ add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
 
       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
         static vs. global.  */
-      add_psymbol_to_list (actual_name,
-                          built_actual_name != NULL,
-                          STRUCT_DOMAIN, LOC_TYPEDEF, -1,
-                          cu->language == language_cplus
-                          ? psymbol_placement::GLOBAL
-                          : psymbol_placement::STATIC,
-                          0, cu->language, objfile);
-
+      psymbol.domain = STRUCT_DOMAIN;
+      psymbol.aclass = LOC_TYPEDEF;
+      where = (cu->language == language_cplus
+              ? psymbol_placement::GLOBAL
+              : psymbol_placement::STATIC);
       break;
     case DW_TAG_enumerator:
-      add_psymbol_to_list (actual_name,
-                          built_actual_name != NULL,
-                          VAR_DOMAIN, LOC_CONST, -1,
-                          cu->language == language_cplus
-                          ? psymbol_placement::GLOBAL
-                          : psymbol_placement::STATIC,
-                          0, cu->language, objfile);
+      psymbol.domain = VAR_DOMAIN;
+      psymbol.aclass = LOC_CONST;
+      where = (cu->language == language_cplus
+              ? psymbol_placement::GLOBAL
+              : psymbol_placement::STATIC);
       break;
     default:
       break;
     }
+
+  if (where.has_value ())
+    {
+      if (built_actual_name != nullptr)
+       actual_name = objfile->intern (actual_name);
+      if (pdi->linkage_name == nullptr || cu->language == language_ada)
+       psymbol.ginfo.set_linkage_name (actual_name);
+      else
+       {
+         psymbol.ginfo.set_demangled_name (actual_name,
+                                           &objfile->objfile_obstack);
+         psymbol.ginfo.set_linkage_name (pdi->linkage_name);
+       }
+      add_psymbol_to_list (psymbol, *where, objfile);
+    }
 }
 
 /* Read a partial die corresponding to a namespace; also, add a symbol
@@ -8629,7 +8494,7 @@ add_partial_subprogram (struct partial_die_info *pdi,
          if (set_addrmap)
            {
              struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
-             struct gdbarch *gdbarch = get_objfile_arch (objfile);
+             struct gdbarch *gdbarch = objfile->arch ();
              CORE_ADDR baseaddr;
              CORE_ADDR this_highpc;
              CORE_ADDR this_lowpc;
@@ -8790,13 +8655,13 @@ skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
            complaint (_("ignoring absolute DW_AT_sibling"));
          else
            {
-             sect_offset off = dwarf2_get_ref_die_offset (&attr);
+             sect_offset off = attr.get_ref_die_offset ();
              const gdb_byte *sibling_ptr = buffer + to_underlying (off);
 
              if (sibling_ptr < info_ptr)
                complaint (_("DW_AT_sibling points backwards"));
              else if (sibling_ptr > reader->buffer_end)
-               dwarf2_section_buffer_overflow_complaint (reader->die_section);
+               reader->die_section->overflow_complaint ();
              else
                return sibling_ptr;
            }
@@ -8882,6 +8747,7 @@ skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
        case DW_FORM_GNU_addr_index:
        case DW_FORM_GNU_str_index:
        case DW_FORM_rnglistx:
+       case DW_FORM_loclistx:
          info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
          break;
        case DW_FORM_indirect:
@@ -8951,8 +8817,6 @@ dwarf2_psymtab::read_symtab (struct objfile *objfile)
        = dpo_backlink->has_section_at_zero;
     }
 
-  dwarf2_per_objfile->reading_partial_symbols = 0;
-
   expand_psymtab (objfile);
 
   process_cu_includes (dwarf2_per_objfile);
@@ -9092,24 +8956,12 @@ process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
 void
 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
 {
-  struct dwarf2_per_cu_data *per_cu;
-
-  if (readin)
-    return;
-
-  read_dependencies (objfile);
-
-  per_cu = per_cu_data;
+  gdb_assert (!readin);
 
-  if (per_cu == NULL)
-    {
-      /* It's an include file, no symbols to read for it.
-         Everything is in the parent symtab.  */
-      readin = true;
-      return;
-    }
+  expand_dependencies (objfile);
 
-  dw2_do_instantiate_symtab (per_cu, false);
+  dw2_do_instantiate_symtab (per_cu_data, false);
+  gdb_assert (get_compunit_symtab () != nullptr);
 }
 
 /* Trivial hash function for die_info: the hash value of a DIE
@@ -9144,7 +8996,7 @@ load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
 {
   gdb_assert (! this_cu->is_debug_types);
 
-  cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
+  cutu_reader reader (this_cu, NULL, 1, skip_partial);
   if (reader.dummy_p)
     return;
 
@@ -9175,6 +9027,8 @@ load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
      Similarly, if we do not read the producer, we can not apply
      producer-specific interpretation.  */
   prepare_one_comp_unit (cu, cu->dies, pretend_language);
+
+  reader.keep ();
 }
 
 /* Add a DIE to the delayed physname list.  */
@@ -9313,13 +9167,12 @@ fixup_go_packaging (struct dwarf2_cu *cu)
   if (package_name != NULL)
     {
       struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
-      const char *saved_package_name
-       = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
+      const char *saved_package_name = objfile->intern (package_name.get ());
       struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
                                     saved_package_name);
       struct symbol *sym;
 
-      sym = allocate_symbol (objfile);
+      sym = new (&objfile->objfile_obstack) symbol;
       sym->set_language (language_go, &objfile->objfile_obstack);
       sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
       /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
@@ -9341,37 +9194,72 @@ rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
   return obconcat (obstack, p1, "::", p2, (char *) NULL);
 }
 
-/* A helper that allocates a struct discriminant_info to attach to a
-   union type.  */
+/* A helper that allocates a variant part to attach to a Rust enum
+   type.  OBSTACK is where the results should be allocated.  TYPE is
+   the type we're processing.  DISCRIMINANT_INDEX is the index of the
+   discriminant.  It must be the index of one of the fields of TYPE.
+   DEFAULT_INDEX is the index of the default field; or -1 if there is
+   no default.  RANGES is indexed by "effective" field number (the
+   field index, but omitting the discriminant and default fields) and
+   must hold the discriminant values used by the variants.  Note that
+   RANGES must have a lifetime at least as long as OBSTACK -- either
+   already allocated on it, or static.  */
 
-static struct discriminant_info *
-alloc_discriminant_info (struct type *type, int discriminant_index,
-                        int default_index)
-{
-  gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
-  gdb_assert (discriminant_index == -1
-             || (discriminant_index >= 0
-                 && discriminant_index < TYPE_NFIELDS (type)));
+static void
+alloc_rust_variant (struct obstack *obstack, struct type *type,
+                   int discriminant_index, int default_index,
+                   gdb::array_view<discriminant_range> ranges)
+{
+  /* When DISCRIMINANT_INDEX == -1, we have a univariant enum.  Those
+     must be handled by the caller.  */
+  gdb_assert (discriminant_index >= 0
+             && discriminant_index < TYPE_NFIELDS (type));
   gdb_assert (default_index == -1
              || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
 
-  TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
+  /* We have one variant for each non-discriminant field.  */
+  int n_variants = TYPE_NFIELDS (type) - 1;
 
-  struct discriminant_info *disc
-    = ((struct discriminant_info *)
-       TYPE_ZALLOC (type,
-                   offsetof (struct discriminant_info, discriminants)
-                   + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
-  disc->default_index = default_index;
-  disc->discriminant_index = discriminant_index;
+  variant *variants = new (obstack) variant[n_variants];
+  int var_idx = 0;
+  int range_idx = 0;
+  for (int i = 0; i < TYPE_NFIELDS (type); ++i)
+    {
+      if (i == discriminant_index)
+       continue;
 
-  struct dynamic_prop prop;
-  prop.kind = PROP_UNDEFINED;
-  prop.data.baton = disc;
+      variants[var_idx].first_field = i;
+      variants[var_idx].last_field = i + 1;
+
+      /* The default field does not need a range, but other fields do.
+        We skipped the discriminant above.  */
+      if (i != default_index)
+       {
+         variants[var_idx].discriminants = ranges.slice (range_idx, 1);
+         ++range_idx;
+       }
+
+      ++var_idx;
+    }
+
+  gdb_assert (range_idx == ranges.size ());
+  gdb_assert (var_idx == n_variants);
+
+  variant_part *part = new (obstack) variant_part;
+  part->discriminant_index = discriminant_index;
+  part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
+                                                     discriminant_index));
+  part->variants = gdb::array_view<variant> (variants, n_variants);
+
+  void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
+  gdb::array_view<variant_part> *prop_value
+    = new (storage) gdb::array_view<variant_part> (part, 1);
 
-  add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
+  struct dynamic_prop prop;
+  prop.kind = PROP_VARIANT_PARTS;
+  prop.data.variant_parts = prop_value;
 
-  return disc;
+  type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
 }
 
 /* Some versions of rustc emitted enums in an unusual way.
@@ -9397,7 +9285,7 @@ alloc_discriminant_info (struct type *type, int discriminant_index,
 static void
 quirk_rust_enum (struct type *type, struct objfile *objfile)
 {
-  gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
+  gdb_assert (type->code () == TYPE_CODE_UNION);
 
   /* We don't need to deal with empty enums.  */
   if (TYPE_NFIELDS (type) == 0)
@@ -9435,55 +9323,44 @@ quirk_rust_enum (struct type *type, struct objfile *objfile)
          field_type = TYPE_FIELD_TYPE (field_type, index);
        }
 
-      /* Make a union to hold the variants.  */
-      struct type *union_type = alloc_type (objfile);
-      TYPE_CODE (union_type) = TYPE_CODE_UNION;
-      TYPE_NFIELDS (union_type) = 3;
-      TYPE_FIELDS (union_type)
+      /* Smash this type to be a structure type.  We have to do this
+        because the type has already been recorded.  */
+      type->set_code (TYPE_CODE_STRUCT);
+      TYPE_NFIELDS (type) = 3;
+      /* Save the field we care about.  */
+      struct field saved_field = TYPE_FIELD (type, 0);
+      TYPE_FIELDS (type)
        = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
-      TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
-      set_type_align (union_type, TYPE_RAW_ALIGN (type));
 
-      /* Put the discriminant must at index 0.  */
-      TYPE_FIELD_TYPE (union_type, 0) = field_type;
-      TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
-      TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
-      SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
+      /* Put the discriminant at index 0.  */
+      TYPE_FIELD_TYPE (type, 0) = field_type;
+      TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
+      TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
+      SET_FIELD_BITPOS (TYPE_FIELD (type, 0), bit_offset);
 
       /* The order of fields doesn't really matter, so put the real
         field at index 1 and the data-less field at index 2.  */
-      struct discriminant_info *disc
-       = alloc_discriminant_info (union_type, 0, 1);
-      TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
-      TYPE_FIELD_NAME (union_type, 1)
-       = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
-      TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
-       = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
-                             TYPE_FIELD_NAME (union_type, 1));
+      TYPE_FIELD (type, 1) = saved_field;
+      TYPE_FIELD_NAME (type, 1)
+       = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, 1)));
+      TYPE_FIELD_TYPE (type, 1)->set_name
+       (rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
+                            TYPE_FIELD_NAME (type, 1)));
 
       const char *dataless_name
        = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
                              name);
       struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
                                              dataless_name);
-      TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
+      TYPE_FIELD_TYPE (type, 2) = dataless_type;
       /* NAME points into the original discriminant name, which
         already has the correct lifetime.  */
-      TYPE_FIELD_NAME (union_type, 2) = name;
-      SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
-      disc->discriminants[2] = 0;
-
-      /* Smash this type to be a structure type.  We have to do this
-        because the type has already been recorded.  */
-      TYPE_CODE (type) = TYPE_CODE_STRUCT;
-      TYPE_NFIELDS (type) = 1;
-      TYPE_FIELDS (type)
-       = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
+      TYPE_FIELD_NAME (type, 2) = name;
+      SET_FIELD_BITPOS (TYPE_FIELD (type, 2), 0);
 
-      /* Install the variant part.  */
-      TYPE_FIELD_TYPE (type, 0) = union_type;
-      SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
-      TYPE_FIELD_NAME (type, 0) = "<<variants>>";
+      /* Indicate that this is a variant type.  */
+      static discriminant_range ranges[1] = { { 0, 0 } };
+      alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
     }
   /* A union with a single anonymous field is probably an old-style
      univariant enum.  */
@@ -9491,33 +9368,15 @@ quirk_rust_enum (struct type *type, struct objfile *objfile)
     {
       /* Smash this type to be a structure type.  We have to do this
         because the type has already been recorded.  */
-      TYPE_CODE (type) = TYPE_CODE_STRUCT;
-
-      /* Make a union to hold the variants.  */
-      struct type *union_type = alloc_type (objfile);
-      TYPE_CODE (union_type) = TYPE_CODE_UNION;
-      TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
-      TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
-      set_type_align (union_type, TYPE_RAW_ALIGN (type));
-      TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
+      type->set_code (TYPE_CODE_STRUCT);
 
-      struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
+      struct type *field_type = TYPE_FIELD_TYPE (type, 0);
       const char *variant_name
        = rust_last_path_segment (TYPE_NAME (field_type));
-      TYPE_FIELD_NAME (union_type, 0) = variant_name;
-      TYPE_NAME (field_type)
-       = rust_fully_qualify (&objfile->objfile_obstack,
-                             TYPE_NAME (type), variant_name);
-
-      /* Install the union in the outer struct type.  */
-      TYPE_NFIELDS (type) = 1;
-      TYPE_FIELDS (type)
-       = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
-      TYPE_FIELD_TYPE (type, 0) = union_type;
-      TYPE_FIELD_NAME (type, 0) = "<<variants>>";
-      SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
-
-      alloc_discriminant_info (union_type, -1, 0);
+      TYPE_FIELD_NAME (type, 0) = variant_name;
+      field_type->set_name
+       (rust_fully_qualify (&objfile->objfile_obstack,
+                            TYPE_NAME (type), variant_name));
     }
   else
     {
@@ -9526,7 +9385,7 @@ quirk_rust_enum (struct type *type, struct objfile *objfile)
        {
          disr_type = TYPE_FIELD_TYPE (type, i);
 
-         if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
+         if (disr_type->code () != TYPE_CODE_STRUCT)
            {
              /* All fields of a true enum will be structs.  */
              return;
@@ -9556,35 +9415,22 @@ quirk_rust_enum (struct type *type, struct objfile *objfile)
 
       /* Smash this type to be a structure type.  We have to do this
         because the type has already been recorded.  */
-      TYPE_CODE (type) = TYPE_CODE_STRUCT;
+      type->set_code (TYPE_CODE_STRUCT);
 
-      /* Make a union to hold the variants.  */
+      /* Make space for the discriminant field.  */
       struct field *disr_field = &TYPE_FIELD (disr_type, 0);
-      struct type *union_type = alloc_type (objfile);
-      TYPE_CODE (union_type) = TYPE_CODE_UNION;
-      TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
-      TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
-      set_type_align (union_type, TYPE_RAW_ALIGN (type));
-      TYPE_FIELDS (union_type)
-       = (struct field *) TYPE_ZALLOC (union_type,
-                                       (TYPE_NFIELDS (union_type)
-                                        * sizeof (struct field)));
-
-      memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
+      field *new_fields
+       = (struct field *) TYPE_ZALLOC (type, (TYPE_NFIELDS (type)
+                                              * sizeof (struct field)));
+      memcpy (new_fields + 1, TYPE_FIELDS (type),
              TYPE_NFIELDS (type) * sizeof (struct field));
+      TYPE_FIELDS (type) = new_fields;
+      TYPE_NFIELDS (type) = TYPE_NFIELDS (type) + 1;
 
       /* Install the discriminant at index 0 in the union.  */
-      TYPE_FIELD (union_type, 0) = *disr_field;
-      TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
-      TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
-
-      /* Install the union in the outer struct type.  */
-      TYPE_FIELD_TYPE (type, 0) = union_type;
-      TYPE_FIELD_NAME (type, 0) = "<<variants>>";
-      TYPE_NFIELDS (type) = 1;
-
-      /* Set the size and offset of the union type.  */
-      SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
+      TYPE_FIELD (type, 0) = *disr_field;
+      TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
+      TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
 
       /* We need a way to find the correct discriminant given a
         variant name.  For convenience we build a map here.  */
@@ -9600,9 +9446,13 @@ quirk_rust_enum (struct type *type, struct objfile *objfile)
            }
        }
 
-      int n_fields = TYPE_NFIELDS (union_type);
-      struct discriminant_info *disc
-       = alloc_discriminant_info (union_type, 0, -1);
+      int n_fields = TYPE_NFIELDS (type);
+      /* We don't need a range entry for the discriminant, but we do
+        need one for every other field, as there is no default
+        variant.  */
+      discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
+                                             discriminant_range,
+                                             n_fields - 1);
       /* Skip the discriminant here.  */
       for (int i = 1; i < n_fields; ++i)
        {
@@ -9610,25 +9460,32 @@ quirk_rust_enum (struct type *type, struct objfile *objfile)
             That name can be used to look up the correct
             discriminant.  */
          const char *variant_name
-           = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
-                                                                 i)));
+           = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, i)));
 
          auto iter = discriminant_map.find (variant_name);
          if (iter != discriminant_map.end ())
-           disc->discriminants[i] = iter->second;
+           {
+             ranges[i].low = iter->second;
+             ranges[i].high = iter->second;
+           }
 
          /* Remove the discriminant field, if it exists.  */
-         struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
+         struct type *sub_type = TYPE_FIELD_TYPE (type, i);
          if (TYPE_NFIELDS (sub_type) > 0)
            {
              --TYPE_NFIELDS (sub_type);
              ++TYPE_FIELDS (sub_type);
            }
-         TYPE_FIELD_NAME (union_type, i) = variant_name;
-         TYPE_NAME (sub_type)
-           rust_fully_qualify (&objfile->objfile_obstack,
-                                 TYPE_NAME (type), variant_name);
+         TYPE_FIELD_NAME (type, i) = variant_name;
+         sub_type->set_name
+           (rust_fully_qualify (&objfile->objfile_obstack,
+                                TYPE_NAME (type), variant_name));
        }
+
+      /* Indicate that this is a variant type.  */
+      alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
+                         gdb::array_view<discriminant_range> (ranges,
+                                                              n_fields - 1));
     }
 }
 
@@ -9777,7 +9634,7 @@ process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
   struct dwarf2_cu *cu = per_cu->cu;
   struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   CORE_ADDR lowpc, highpc;
   struct compunit_symtab *cust;
   CORE_ADDR baseaddr;
@@ -9969,12 +9826,20 @@ process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
   attr = dwarf2_attr (die, DW_AT_import, cu);
   if (attr != NULL)
     {
-      sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
+      sect_offset sect_off = attr->get_ref_die_offset ();
       bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
       dwarf2_per_cu_data *per_cu
        = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
                                            cu->per_cu->dwarf2_per_objfile);
 
+      /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
+        into another compilation unit, at root level.  Regard this as a hint,
+        and ignore it.  */
+      if (die->parent && die->parent->parent == NULL
+         && per_cu->unit_type == DW_UT_compile
+         && per_cu->lang == language_cplus)
+       return;
+
       /* If necessary, add it to the queue and load its DIEs.  */
       if (maybe_queue_comp_unit (cu, per_cu, cu->language))
        load_full_comp_unit (per_cu, false, cu->language);
@@ -10214,6 +10079,12 @@ dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
   if (linkage_name == NULL)
     linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
 
+  /* rustc emits invalid values for DW_AT_linkage_name.  Ignore these.
+     See https://github.com/rust-lang/rust/issues/32925.  */
+  if (cu->language == language_rust && linkage_name != NULL
+      && strchr (linkage_name, '{') != NULL)
+    linkage_name = NULL;
+
   return linkage_name;
 }
 
@@ -10428,7 +10299,7 @@ dwarf2_compute_name (const char *name,
                     the two cases.  */
                  if (TYPE_NFIELDS (type) > 0
                      && TYPE_FIELD_ARTIFICIAL (type, 0)
-                     && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
+                     && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
                      && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
                                                                        0))))
                    buf.puts (" const");
@@ -10440,14 +10311,13 @@ dwarf2_compute_name (const char *name,
          if (cu->language == language_cplus)
            canonical_name
              = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
-                                         &objfile->per_bfd->storage_obstack);
+                                         objfile);
 
          /* If we only computed INTERMEDIATE_NAME, or if
             INTERMEDIATE_NAME is already canonical, then we need to
-            copy it to the appropriate obstack.  */
+            intern it.  */
          if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
-           name = obstack_strdup (&objfile->per_bfd->storage_obstack,
-                                  intermediate_name);
+           name = objfile->intern (intermediate_name);
          else
            name = canonical_name;
        }
@@ -10489,13 +10359,8 @@ dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
   if (!die_needs_namespace (die, cu))
     return dwarf2_compute_name (name, die, cu, 1);
 
-  mangled = dw2_linkage_name (die, cu);
-
-  /* rustc emits invalid values for DW_AT_linkage_name.  Ignore these.
-     See https://github.com/rust-lang/rust/issues/32925.  */
-  if (cu->language == language_rust && mangled != NULL
-      && strchr (mangled, '{') != NULL)
-    mangled = NULL;
+  if (cu->language != language_rust)
+    mangled = dw2_linkage_name (die, cu);
 
   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
      has computed.  */
@@ -10567,7 +10432,7 @@ dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
     retval = canon;
 
   if (need_copy)
-    retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
+    retval = objfile->intern (retval);
 
   return retval;
 }
@@ -10615,10 +10480,10 @@ read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
       if (attr != NULL)
        {
          struct type *type;
-         sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
+         sect_offset sect_off = attr->get_ref_die_offset ();
 
          type = get_die_type_at_offset (sect_off, cu->per_cu);
-         if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
+         if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
            {
              /* This declaration is a global namespace alias.  Add
                 a symbol for it whose type is the aliased namespace.  */
@@ -10741,7 +10606,7 @@ read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
 
   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
     for (child_die = die->child; child_die && child_die->tag;
-        child_die = sibling_die (child_die))
+        child_die = child_die->sibling)
       {
        /* DWARF-4: A Fortran use statement with a “rename list” may be
           represented by an imported module entry with an import attribute
@@ -10975,7 +10840,7 @@ read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
   struct dwarf2_per_objfile *dwarf2_per_objfile
     = cu->per_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   CORE_ADDR lowpc = ((CORE_ADDR) -1);
   CORE_ADDR highpc = ((CORE_ADDR) 0);
   struct attribute *attr;
@@ -11019,7 +10884,7 @@ read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
       while (child_die && child_die->tag)
        {
          process_die (child_die, cu);
-         child_die = sibling_die (child_die);
+         child_die = child_die->sibling;
        }
     }
 
@@ -11097,6 +10962,7 @@ dwarf2_cu::setup_type_unit_groups (struct die_info *die)
                            COMPUNIT_DIRNAME (cust),
                            compunit_language (cust),
                            0, cust));
+         list_in_scope = get_builder ()->get_file_symbols ();
        }
       return;
     }
@@ -11113,9 +10979,9 @@ dwarf2_cu::setup_type_unit_groups (struct die_info *die)
         process_full_type_unit still needs to know if this is the first
         time.  */
 
-      tu_group->num_symtabs = line_header->file_names_size ();
-      tu_group->symtabs = XNEWVEC (struct symtab *,
-                                  line_header->file_names_size ());
+      tu_group->symtabs
+       = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
+                    struct symtab *, line_header->file_names_size ());
 
       auto &file_names = line_header->file_names ();
       for (i = 0; i < file_names.size (); ++i)
@@ -11148,6 +11014,7 @@ dwarf2_cu::setup_type_unit_groups (struct die_info *die)
                        COMPUNIT_DIRNAME (cust),
                        compunit_language (cust),
                        0, cust));
+      list_in_scope = get_builder ()->get_file_symbols ();
 
       auto &file_names = line_header->file_names ();
       for (i = 0; i < file_names.size (); ++i)
@@ -11187,7 +11054,7 @@ read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
       while (child_die && child_die->tag)
        {
          process_die (child_die, cu);
-         child_die = sibling_die (child_die);
+         child_die = child_die->sibling;
        }
     }
 }
@@ -11229,7 +11096,7 @@ eq_dwo_file (const void *item_lhs, const void *item_rhs)
 /* Allocate a hash table for DWO files.  */
 
 static htab_up
-allocate_dwo_file_hash_table (struct objfile *objfile)
+allocate_dwo_file_hash_table ()
 {
   auto delete_dwo_file = [] (void *item)
     {
@@ -11256,8 +11123,7 @@ lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
   void **slot;
 
   if (dwarf2_per_objfile->dwo_files == NULL)
-    dwarf2_per_objfile->dwo_files
-      = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
+    dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
 
   find_entry.dwo_name = dwo_name;
   find_entry.comp_dir = comp_dir;
@@ -11293,7 +11159,7 @@ eq_dwo_unit (const void *item_lhs, const void *item_rhs)
    There is one of these tables for each of CUs,TUs for each DWO file.  */
 
 static htab_up
-allocate_dwo_unit_table (struct objfile *objfile)
+allocate_dwo_unit_table ()
 {
   /* Start out with a pretty small number.
      Generally DWO files contain only one CU and maybe some TUs.  */
@@ -11387,7 +11253,7 @@ create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
        continue;
 
       if (cus_htab == NULL)
-       cus_htab = allocate_dwo_unit_table (objfile);
+       cus_htab = allocate_dwo_unit_table ();
 
       dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
       *dwo_unit = read_unit;
@@ -11894,8 +11760,7 @@ create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
                              virtual_dwo_name.c_str ());
        }
       dwo_file = new struct dwo_file;
-      dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
-                                          virtual_dwo_name);
+      dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
       dwo_file->comp_dir = comp_dir;
       dwo_file->sections.abbrev = sections.abbrev;
       dwo_file->sections.line = sections.line;
@@ -12090,8 +11955,7 @@ create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
                              virtual_dwo_name.c_str ());
        }
       dwo_file = new struct dwo_file;
-      dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
-                                          virtual_dwo_name);
+      dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
       dwo_file->comp_dir = comp_dir;
       dwo_file->sections.abbrev =
        create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
@@ -12355,6 +12219,11 @@ dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
       dwo_sections->loc.s.section = sectp;
       dwo_sections->loc.size = bfd_section_size (sectp);
     }
+  else if (section_is_p (sectp->name, &names->loclists_dwo))
+    {
+      dwo_sections->loclists.s.section = sectp;
+      dwo_sections->loclists.size = bfd_section_size (sectp);
+    }
   else if (section_is_p (sectp->name, &names->macinfo_dwo))
     {
       dwo_sections->macinfo.s.section = sectp;
@@ -12544,7 +12413,7 @@ eq_dwp_loaded_cutus (const void *a, const void *b)
 /* Allocate a hash table for dwp_file loaded CUs/TUs.  */
 
 static htab_up
-allocate_dwp_loaded_cutus_table (struct objfile *objfile)
+allocate_dwp_loaded_cutus_table ()
 {
   return htab_up (htab_create_alloc (3,
                                     hash_dwp_loaded_cutus,
@@ -12677,8 +12546,8 @@ open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
                           dwarf2_locate_v2_dwp_sections,
                           dwp_file.get ());
 
-  dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
-  dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
+  dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
+  dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
 
   if (dwarf_read_debug)
     {
@@ -12963,7 +12832,7 @@ inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
 
   for (child_die = die->child;
        child_die && child_die->tag;
-       child_die = sibling_die (child_die))
+       child_die = child_die->sibling)
     {
       struct die_info *child_origin_die;
       struct dwarf2_cu *child_origin_cu;
@@ -13040,7 +12909,7 @@ inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
          if (!origin_child_die->in_process)
            process_die (origin_child_die, origin_cu);
        }
-      origin_child_die = sibling_die (origin_child_die);
+      origin_child_die = origin_child_die->sibling;
     }
   origin_cu->list_in_scope = origin_previous_list_in_scope;
 
@@ -13052,7 +12921,7 @@ static void
 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
 {
   struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   struct context_stack *newobj;
   CORE_ADDR lowpc;
   CORE_ADDR highpc;
@@ -13109,12 +12978,12 @@ read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
 
   /* If we have any template arguments, then we must allocate a
      different sort of symbol.  */
-  for (child_die = die->child; child_die; child_die = sibling_die (child_die))
+  for (child_die = die->child; child_die; child_die = child_die->sibling)
     {
       if (child_die->tag == DW_TAG_template_type_param
          || child_die->tag == DW_TAG_template_value_param)
        {
-         templ_func = allocate_template_symbol (objfile);
+         templ_func = new (&objfile->objfile_obstack) template_symbol;
          templ_func->subclass = SYMBOL_TEMPLATE;
          break;
        }
@@ -13162,7 +13031,7 @@ read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
            }
          else
            process_die (child_die, cu);
-         child_die = sibling_die (child_die);
+         child_die = child_die->sibling;
        }
     }
 
@@ -13184,7 +13053,7 @@ read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
            {
              if (child_die->tag == DW_TAG_imported_module)
                process_die (child_die, spec_cu);
-             child_die = sibling_die (child_die);
+             child_die = child_die->sibling;
            }
 
          /* In some cases, GCC generates specification DIEs that
@@ -13253,7 +13122,7 @@ static void
 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
 {
   struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   CORE_ADDR lowpc, highpc;
   struct die_info *child_die;
   CORE_ADDR baseaddr;
@@ -13273,8 +13142,17 @@ read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
         GCC does no longer produces such DWARF since GCC r224161.  */
       for (child_die = die->child;
           child_die != NULL && child_die->tag;
-          child_die = sibling_die (child_die))
-       process_die (child_die, cu);
+          child_die = child_die->sibling)
+       {
+         /* We might already be processing this DIE.  This can happen
+            in an unusual circumstance -- where a subroutine A
+            appears lexically in another subroutine B, but A actually
+            inlines B.  The recursion is broken here, rather than in
+            inherit_abstract_dies, because it seems better to simply
+            drop concrete children here.  */
+         if (!child_die->in_process)
+           process_die (child_die, cu);
+       }
       return;
     case PC_BOUNDS_INVALID:
       return;
@@ -13289,7 +13167,7 @@ read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
       while (child_die && child_die->tag)
        {
          process_die (child_die, cu);
-         child_die = sibling_die (child_die);
+         child_die = child_die->sibling;
        }
     }
   inherit_abstract_dies (die, cu);
@@ -13324,7 +13202,7 @@ static void
 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
 {
   struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   CORE_ADDR pc, baseaddr;
   struct attribute *attr;
   struct call_site *call_site, call_site_local;
@@ -13370,7 +13248,7 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
 
   nparams = 0;
   for (child_die = die->child; child_die && child_die->tag;
-       child_die = sibling_die (child_die))
+       child_die = child_die->sibling)
     {
       if (child_die->tag != DW_TAG_call_site_parameter
           && child_die->tag != DW_TAG_GNU_call_site_parameter)
@@ -13430,7 +13308,7 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
            func_type = get_die_type (func_die, cu);
          if (func_type != NULL)
            {
-             gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
+             gdb_assert (func_type->code () == TYPE_CODE_FUNC);
 
              /* Enlist this call site to the function.  */
              call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
@@ -13515,7 +13393,7 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
 
   for (child_die = die->child;
        child_die && child_die->tag;
-       child_die = sibling_die (child_die))
+       child_die = child_die->sibling)
     {
       struct call_site_parameter *parameter;
       struct attribute *loc, *origin;
@@ -13546,9 +13424,8 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
        {
          parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
 
-         sect_offset sect_off
-           = (sect_offset) dwarf2_get_ref_die_offset (origin);
-         if (!offset_in_cu_p (&cu->header, sect_off))
+         sect_offset sect_off = origin->get_ref_die_offset ();
+         if (!cu->header.offset_in_cu_p (sect_off))
            {
              /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
                 binding can be done only inside one CU.  Such referenced DIE
@@ -13669,8 +13546,7 @@ read_variable (struct die_info *die, struct dwarf2_cu *cu)
        {
          struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
 
-         storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
-         initialize_objfile_symbol (storage);
+         storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
          storage->concrete_type = containing_type;
          storage->subclass = SYMBOL_RUST_VTABLE;
        }
@@ -13711,13 +13587,11 @@ dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   bfd *obfd = objfile->obfd;
   /* Base address selection entry.  */
-  CORE_ADDR base;
-  int found_base;
+  gdb::optional<CORE_ADDR> base;
   const gdb_byte *buffer;
   CORE_ADDR baseaddr;
   bool overflow = false;
 
-  found_base = cu->base_known;
   base = cu->base_address;
 
   dwarf2_per_objfile->rnglists.read (objfile);
@@ -13755,8 +13629,7 @@ dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
              overflow = true;
              break;
            }
-         base = read_address (obfd, buffer, cu, &bytes_read);
-         found_base = 1;
+         base = cu->header.read_address (obfd, buffer, &bytes_read);
          buffer += bytes_read;
          break;
        case DW_RLE_start_length:
@@ -13765,7 +13638,8 @@ dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
              overflow = true;
              break;
            }
-         range_beginning = read_address (obfd, buffer, cu, &bytes_read);
+         range_beginning = cu->header.read_address (obfd, buffer,
+                                                    &bytes_read);
          buffer += bytes_read;
          range_end = (range_beginning
                       + read_unsigned_leb128 (obfd, buffer, &bytes_read));
@@ -13798,9 +13672,10 @@ dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
              overflow = true;
              break;
            }
-         range_beginning = read_address (obfd, buffer, cu, &bytes_read);
+         range_beginning = cu->header.read_address (obfd, buffer,
+                                                    &bytes_read);
          buffer += bytes_read;
-         range_end = read_address (obfd, buffer, cu, &bytes_read);
+         range_end = cu->header.read_address (obfd, buffer, &bytes_read);
          buffer += bytes_read;
          break;
        default:
@@ -13812,7 +13687,7 @@ dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
       if (rlet == DW_RLE_base_address)
        continue;
 
-      if (!found_base)
+      if (!base.has_value ())
        {
          /* We have no valid base address for the ranges
             data.  */
@@ -13831,8 +13706,8 @@ dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
       if (range_beginning == range_end)
        continue;
 
-      range_beginning += base;
-      range_end += base;
+      range_beginning += *base;
+      range_end += *base;
 
       /* A not-uncommon case of bad debug info.
         Don't pollute the addrmap with bad data.  */
@@ -13876,8 +13751,7 @@ dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
   unsigned int addr_size = cu_header->addr_size;
   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
   /* Base address selection entry.  */
-  CORE_ADDR base;
-  int found_base;
+  gdb::optional<CORE_ADDR> base;
   unsigned int dummy;
   const gdb_byte *buffer;
   CORE_ADDR baseaddr;
@@ -13885,7 +13759,6 @@ dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
   if (cu_header->version >= 5)
     return dwarf2_rnglists_process (offset, cu, callback);
 
-  found_base = cu->base_known;
   base = cu->base_address;
 
   dwarf2_per_objfile->ranges.read (objfile);
@@ -13903,9 +13776,9 @@ dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
     {
       CORE_ADDR range_beginning, range_end;
 
-      range_beginning = read_address (obfd, buffer, cu, &dummy);
+      range_beginning = cu->header.read_address (obfd, buffer, &dummy);
       buffer += addr_size;
-      range_end = read_address (obfd, buffer, cu, &dummy);
+      range_end = cu->header.read_address (obfd, buffer, &dummy);
       buffer += addr_size;
       offset += 2 * addr_size;
 
@@ -13922,11 +13795,10 @@ dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
          /* If we found the largest possible address, then we already
             have the base address in range_end.  */
          base = range_end;
-         found_base = 1;
          continue;
        }
 
-      if (!found_base)
+      if (!base.has_value ())
        {
          /* We have no valid base address for the ranges
             data.  */
@@ -13945,8 +13817,8 @@ dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
       if (range_beginning == range_end)
        continue;
 
-      range_beginning += base;
-      range_end += base;
+      range_beginning += *base;
+      range_end += *base;
 
       /* A not-uncommon case of bad debug info.
         Don't pollute the addrmap with bad data.  */
@@ -13974,7 +13846,7 @@ dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
                    dwarf2_psymtab *ranges_pst)
 {
   struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   const CORE_ADDR baseaddr = objfile->text_section_offset ();
   int low_set = 0;
   CORE_ADDR low = 0;
@@ -14146,7 +14018,7 @@ dwarf2_get_subprogram_pc_bounds (struct die_info *die,
       if (child->tag == DW_TAG_subprogram
           || child->tag == DW_TAG_lexical_block)
         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
-      child = sibling_die (child);
+      child = child->sibling;
     }
 }
 
@@ -14202,7 +14074,7 @@ get_scope_pc_bounds (struct die_info *die,
            break;
          }
 
-         child = sibling_die (child);
+         child = child->sibling;
        }
     }
 
@@ -14218,7 +14090,7 @@ dwarf2_record_block_ranges (struct die_info *die, struct block *block,
                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
 {
   struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   struct attribute *attr;
   struct attribute *attr_high;
 
@@ -14385,7 +14257,7 @@ handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
         so if we see it, we can assume that a constant form is really
         a constant and not a section offset.  */
       if (attr->form_is_constant ())
-       *offset = dwarf2_get_attr_constant_value (attr, 0);
+       *offset = attr->constant_value (0);
       else if (attr->form_is_section_offset ())
        dwarf2_complex_location_expr_complaint ();
       else if (attr->form_is_block ())
@@ -14399,6 +14271,53 @@ handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
   return 0;
 }
 
+/* Look for DW_AT_data_member_location and store the results in FIELD.  */
+
+static void
+handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
+                            struct field *field)
+{
+  struct attribute *attr;
+
+  attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
+  if (attr != NULL)
+    {
+      if (attr->form_is_constant ())
+       {
+         LONGEST offset = attr->constant_value (0);
+         SET_FIELD_BITPOS (*field, offset * bits_per_byte);
+       }
+      else if (attr->form_is_section_offset ())
+       dwarf2_complex_location_expr_complaint ();
+      else if (attr->form_is_block ())
+       {
+         bool handled;
+         CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
+         if (handled)
+           SET_FIELD_BITPOS (*field, offset * bits_per_byte);
+         else
+           {
+             struct objfile *objfile
+               = cu->per_cu->dwarf2_per_objfile->objfile;
+             struct dwarf2_locexpr_baton *dlbaton
+               = XOBNEW (&objfile->objfile_obstack,
+                         struct dwarf2_locexpr_baton);
+             dlbaton->data = DW_BLOCK (attr)->data;
+             dlbaton->size = DW_BLOCK (attr)->size;
+             /* When using this baton, we want to compute the address
+                of the field, not the value.  This is why
+                is_reference is set to false here.  */
+             dlbaton->is_reference = false;
+             dlbaton->per_cu = cu->per_cu;
+
+             SET_FIELD_DWARF_BLOCK (*field, dlbaton);
+           }
+       }
+      else
+       dwarf2_complex_location_expr_complaint ();
+    }
+}
+
 /* Add an aggregate field to the field list.  */
 
 static void
@@ -14406,7 +14325,7 @@ dwarf2_add_field (struct field_info *fip, struct die_info *die,
                  struct dwarf2_cu *cu)
 {
   struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   struct nextfield *new_field;
   struct attribute *attr;
   struct field *fp;
@@ -14423,7 +14342,7 @@ dwarf2_add_field (struct field_info *fip, struct die_info *die,
       new_field = &fip->fields.back ();
     }
 
-  fip->nfields++;
+  new_field->offset = die->sect_off;
 
   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
   if (attr != nullptr)
@@ -14443,8 +14362,6 @@ dwarf2_add_field (struct field_info *fip, struct die_info *die,
 
   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
     {
-      LONGEST offset;
-
       /* Data member other than a C++ static data member.  */
 
       /* Get type of field.  */
@@ -14464,8 +14381,7 @@ dwarf2_add_field (struct field_info *fip, struct die_info *die,
        }
 
       /* Get bit offset of field.  */
-      if (handle_data_member_location (die, cu, &offset))
-       SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
+      handle_data_member_location (die, cu, fp);
       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
       if (attr != nullptr)
        {
@@ -14514,7 +14430,7 @@ dwarf2_add_field (struct field_info *fip, struct die_info *die,
       attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
       if (attr != NULL)
        SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
-                               + dwarf2_get_attr_constant_value (attr, 0)));
+                               + attr->constant_value (0)));
 
       /* Get name of field.  */
       fieldname = dwarf2_name (die, cu);
@@ -14574,44 +14490,12 @@ dwarf2_add_field (struct field_info *fip, struct die_info *die,
     }
   else if (die->tag == DW_TAG_inheritance)
     {
-      LONGEST offset;
-
       /* C++ base class field.  */
-      if (handle_data_member_location (die, cu, &offset))
-       SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
+      handle_data_member_location (die, cu, fp);
       FIELD_BITSIZE (*fp) = 0;
       FIELD_TYPE (*fp) = die_type (die, cu);
       FIELD_NAME (*fp) = TYPE_NAME (fp->type);
     }
-  else if (die->tag == DW_TAG_variant_part)
-    {
-      /* process_structure_scope will treat this DIE as a union.  */
-      process_structure_scope (die, cu);
-
-      /* The variant part is relative to the start of the enclosing
-        structure.  */
-      SET_FIELD_BITPOS (*fp, 0);
-      fp->type = get_die_type (die, cu);
-      fp->artificial = 1;
-      fp->name = "<<variant>>";
-
-      /* Normally a DW_TAG_variant_part won't have a size, but our
-        representation requires one, so set it to the maximum of the
-        child sizes, being sure to account for the offset at which
-        each child is seen.  */
-      if (TYPE_LENGTH (fp->type) == 0)
-       {
-         unsigned max = 0;
-         for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
-           {
-             unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
-                             + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
-             if (len > max)
-               max = len;
-           }
-         TYPE_LENGTH (fp->type) = max;
-       }
-    }
   else
     gdb_assert_not_reached ("missing case in dwarf2_add_field");
 }
@@ -14678,13 +14562,209 @@ dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
     fip->nested_types_list.push_back (fp);
 }
 
+/* A convenience typedef that's used when finding the discriminant
+   field for a variant part.  */
+typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
+  offset_map_type;
+
+/* Compute the discriminant range for a given variant.  OBSTACK is
+   where the results will be stored.  VARIANT is the variant to
+   process.  IS_UNSIGNED indicates whether the discriminant is signed
+   or unsigned.  */
+
+static const gdb::array_view<discriminant_range>
+convert_variant_range (struct obstack *obstack, const variant_field &variant,
+                      bool is_unsigned)
+{
+  std::vector<discriminant_range> ranges;
+
+  if (variant.default_branch)
+    return {};
+
+  if (variant.discr_list_data == nullptr)
+    {
+      discriminant_range r
+       = {variant.discriminant_value, variant.discriminant_value};
+      ranges.push_back (r);
+    }
+  else
+    {
+      gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
+                                           variant.discr_list_data->size);
+      while (!data.empty ())
+       {
+         if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
+           {
+             complaint (_("invalid discriminant marker: %d"), data[0]);
+             break;
+           }
+         bool is_range = data[0] == DW_DSC_range;
+         data = data.slice (1);
+
+         ULONGEST low, high;
+         unsigned int bytes_read;
+
+         if (data.empty ())
+           {
+             complaint (_("DW_AT_discr_list missing low value"));
+             break;
+           }
+         if (is_unsigned)
+           low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
+         else
+           low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
+                                                &bytes_read);
+         data = data.slice (bytes_read);
+
+         if (is_range)
+           {
+             if (data.empty ())
+               {
+                 complaint (_("DW_AT_discr_list missing high value"));
+                 break;
+               }
+             if (is_unsigned)
+               high = read_unsigned_leb128 (nullptr, data.data (),
+                                            &bytes_read);
+             else
+               high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
+                                                    &bytes_read);
+             data = data.slice (bytes_read);
+           }
+         else
+           high = low;
+
+         ranges.push_back ({ low, high });
+       }
+    }
+
+  discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
+                                         ranges.size ());
+  std::copy (ranges.begin (), ranges.end (), result);
+  return gdb::array_view<discriminant_range> (result, ranges.size ());
+}
+
+static const gdb::array_view<variant_part> create_variant_parts
+  (struct obstack *obstack,
+   const offset_map_type &offset_map,
+   struct field_info *fi,
+   const std::vector<variant_part_builder> &variant_parts);
+
+/* Fill in a "struct variant" for a given variant field.  RESULT is
+   the variant to fill in.  OBSTACK is where any needed allocations
+   will be done.  OFFSET_MAP holds the mapping from section offsets to
+   fields for the type.  FI describes the fields of the type we're
+   processing.  FIELD is the variant field we're converting.  */
+
+static void
+create_one_variant (variant &result, struct obstack *obstack,
+                   const offset_map_type &offset_map,
+                   struct field_info *fi, const variant_field &field)
+{
+  result.discriminants = convert_variant_range (obstack, field, false);
+  result.first_field = field.first_field + fi->baseclasses.size ();
+  result.last_field = field.last_field + fi->baseclasses.size ();
+  result.parts = create_variant_parts (obstack, offset_map, fi,
+                                      field.variant_parts);
+}
+
+/* Fill in a "struct variant_part" for a given variant part.  RESULT
+   is the variant part to fill in.  OBSTACK is where any needed
+   allocations will be done.  OFFSET_MAP holds the mapping from
+   section offsets to fields for the type.  FI describes the fields of
+   the type we're processing.  BUILDER is the variant part to be
+   converted.  */
+
+static void
+create_one_variant_part (variant_part &result,
+                        struct obstack *obstack,
+                        const offset_map_type &offset_map,
+                        struct field_info *fi,
+                        const variant_part_builder &builder)
+{
+  auto iter = offset_map.find (builder.discriminant_offset);
+  if (iter == offset_map.end ())
+    {
+      result.discriminant_index = -1;
+      /* Doesn't matter.  */
+      result.is_unsigned = false;
+    }
+  else
+    {
+      result.discriminant_index = iter->second;
+      result.is_unsigned
+       = TYPE_UNSIGNED (FIELD_TYPE
+                        (fi->fields[result.discriminant_index].field));
+    }
+
+  size_t n = builder.variants.size ();
+  variant *output = new (obstack) variant[n];
+  for (size_t i = 0; i < n; ++i)
+    create_one_variant (output[i], obstack, offset_map, fi,
+                       builder.variants[i]);
+
+  result.variants = gdb::array_view<variant> (output, n);
+}
+
+/* Create a vector of variant parts that can be attached to a type.
+   OBSTACK is where any needed allocations will be done.  OFFSET_MAP
+   holds the mapping from section offsets to fields for the type.  FI
+   describes the fields of the type we're processing.  VARIANT_PARTS
+   is the vector to convert.  */
+
+static const gdb::array_view<variant_part>
+create_variant_parts (struct obstack *obstack,
+                     const offset_map_type &offset_map,
+                     struct field_info *fi,
+                     const std::vector<variant_part_builder> &variant_parts)
+{
+  if (variant_parts.empty ())
+    return {};
+
+  size_t n = variant_parts.size ();
+  variant_part *result = new (obstack) variant_part[n];
+  for (size_t i = 0; i < n; ++i)
+    create_one_variant_part (result[i], obstack, offset_map, fi,
+                            variant_parts[i]);
+
+  return gdb::array_view<variant_part> (result, n);
+}
+
+/* Compute the variant part vector for FIP, attaching it to TYPE when
+   done.  */
+
+static void
+add_variant_property (struct field_info *fip, struct type *type,
+                     struct dwarf2_cu *cu)
+{
+  /* Map section offsets of fields to their field index.  Note the
+     field index here does not take the number of baseclasses into
+     account.  */
+  offset_map_type offset_map;
+  for (int i = 0; i < fip->fields.size (); ++i)
+    offset_map[fip->fields[i].offset] = i;
+
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
+  gdb::array_view<variant_part> parts
+    = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
+                           fip->variant_parts);
+
+  struct dynamic_prop prop;
+  prop.kind = PROP_VARIANT_PARTS;
+  prop.data.variant_parts
+    = ((gdb::array_view<variant_part> *)
+       obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
+
+  type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
+}
+
 /* Create the vector of fields, and attach it to the type.  */
 
 static void
 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
                              struct dwarf2_cu *cu)
 {
-  int nfields = fip->nfields;
+  int nfields = fip->nfields ();
 
   /* Record the field count, allocate space for the array of fields,
      and create blank accessibility bitfields if necessary.  */
@@ -14723,22 +14803,8 @@ dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
       TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
     }
 
-  if (TYPE_FLAG_DISCRIMINATED_UNION (type))
-    {
-      struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
-
-      for (int index = 0; index < nfields; ++index)
-       {
-         struct nextfield &field = fip->fields[index];
-
-         if (field.variant.is_discriminant)
-           di->discriminant_index = index;
-         else if (field.variant.default_branch)
-           di->default_index = index;
-         else
-           di->discriminants[index] = field.variant.discriminant_value;
-       }
-    }
+  if (!fip->variant_parts.empty ())
+    add_variant_property (fip, type, cu);
 
   /* Copy the saved-up fields into the field vector.  */
   for (int i = 0; i < nfields; ++i)
@@ -14892,7 +14958,7 @@ dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
 
   fnp->type = alloc_type (objfile);
   this_type = read_type_die (die, cu);
-  if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
+  if (this_type && this_type->code () == TYPE_CODE_FUNC)
     {
       int nparams = TYPE_NFIELDS (this_type);
 
@@ -15090,7 +15156,7 @@ quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
   struct type *pfn_type, *self_type, *new_type;
 
   /* Check for a structure with no name and two children.  */
-  if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
+  if (type->code () != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
     return;
 
   /* Check for __pfn and __delta members.  */
@@ -15103,15 +15169,15 @@ quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
   /* Find the type of the method.  */
   pfn_type = TYPE_FIELD_TYPE (type, 0);
   if (pfn_type == NULL
-      || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
-      || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
+      || pfn_type->code () != TYPE_CODE_PTR
+      || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
     return;
 
   /* Look for the "this" argument.  */
   pfn_type = TYPE_TARGET_TYPE (pfn_type);
   if (TYPE_NFIELDS (pfn_type) == 0
       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
-      || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
+      || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
     return;
 
   self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
@@ -15263,7 +15329,7 @@ read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
   /* If the definition of this type lives in .debug_types, read that type.
      Don't follow DW_AT_specification though, that will take us back up
      the chain and we want to go down.  */
-  attr = dwarf2_attr_no_follow (die, DW_AT_signature);
+  attr = die->attr (DW_AT_signature);
   if (attr != nullptr)
     {
       type = get_DW_AT_signature_type (die, attr, cu);
@@ -15290,32 +15356,27 @@ read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
          if (get_die_type (die, cu) != NULL)
            return get_die_type (die, cu);
 
-         TYPE_NAME (type) = full_name;
+         type->set_name (full_name);
        }
       else
        {
          /* The name is already allocated along with this objfile, so
             we don't need to duplicate it for the type.  */
-         TYPE_NAME (type) = name;
+         type->set_name (name);
        }
     }
 
   if (die->tag == DW_TAG_structure_type)
     {
-      TYPE_CODE (type) = TYPE_CODE_STRUCT;
+      type->set_code (TYPE_CODE_STRUCT);
     }
   else if (die->tag == DW_TAG_union_type)
     {
-      TYPE_CODE (type) = TYPE_CODE_UNION;
-    }
-  else if (die->tag == DW_TAG_variant_part)
-    {
-      TYPE_CODE (type) = TYPE_CODE_UNION;
-      TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
+      type->set_code (TYPE_CODE_UNION);
     }
   else
     {
-      TYPE_CODE (type) = TYPE_CODE_STRUCT;
+      type->set_code (TYPE_CODE_STRUCT);
     }
 
   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
@@ -15340,14 +15401,10 @@ read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
         TYPE_LENGTH (type) = DW_UNSND (attr);
       else
        {
-         /* For the moment, dynamic type sizes are not supported
-            by GDB's struct type.  The actual size is determined
-            on-demand when resolving the type of a given object,
-            so set the type's length to zero for now.  Otherwise,
-            we record an expression as the length, and that expression
-            could lead to a very large value, which could eventually
-            lead to us trying to allocate that much memory when creating
-            a value of that type.  */
+         struct dynamic_prop prop;
+         if (attr_to_dynamic_prop (attr, die, cu, &prop,
+                                   cu->per_cu->addr_type ()))
+           type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
           TYPE_LENGTH (type) = 0;
        }
     }
@@ -15386,89 +15443,181 @@ read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
   return type;
 }
 
-/* A helper for process_structure_scope that handles a single member
-   DIE.  */
+static void handle_struct_member_die
+  (struct die_info *child_die,
+   struct type *type,
+   struct field_info *fi,
+   std::vector<struct symbol *> *template_args,
+   struct dwarf2_cu *cu);
+
+/* A helper for handle_struct_member_die that handles
+   DW_TAG_variant_part.  */
 
 static void
-handle_struct_member_die (struct die_info *child_die, struct type *type,
-                         struct field_info *fi,
-                         std::vector<struct symbol *> *template_args,
-                         struct dwarf2_cu *cu)
+handle_variant_part (struct die_info *die, struct type *type,
+                    struct field_info *fi,
+                    std::vector<struct symbol *> *template_args,
+                    struct dwarf2_cu *cu)
 {
-  if (child_die->tag == DW_TAG_member
-      || child_die->tag == DW_TAG_variable
-      || child_die->tag == DW_TAG_variant_part)
+  variant_part_builder *new_part;
+  if (fi->current_variant_part == nullptr)
     {
-      /* NOTE: carlton/2002-11-05: A C++ static data member
-        should be a DW_TAG_member that is a declaration, but
-        all versions of G++ as of this writing (so through at
-        least 3.2.1) incorrectly generate DW_TAG_variable
-        tags for them instead.  */
-      dwarf2_add_field (fi, child_die, cu);
+      fi->variant_parts.emplace_back ();
+      new_part = &fi->variant_parts.back ();
     }
-  else if (child_die->tag == DW_TAG_subprogram)
+  else if (!fi->current_variant_part->processing_variant)
     {
-      /* Rust doesn't have member functions in the C++ sense.
-        However, it does emit ordinary functions as children
-        of a struct DIE.  */
-      if (cu->language == language_rust)
-       read_func_scope (child_die, cu);
-      else
-       {
-         /* C++ member function.  */
-         dwarf2_add_member_fn (fi, child_die, type, cu);
-       }
+      complaint (_("nested DW_TAG_variant_part seen "
+                  "- DIE at %s [in module %s]"),
+                sect_offset_str (die->sect_off),
+                objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
+      return;
     }
-  else if (child_die->tag == DW_TAG_inheritance)
+  else
     {
-      /* C++ base class field.  */
-      dwarf2_add_field (fi, child_die, cu);
+      variant_field &current = fi->current_variant_part->variants.back ();
+      current.variant_parts.emplace_back ();
+      new_part = &current.variant_parts.back ();
     }
-  else if (type_can_define_types (child_die))
-    dwarf2_add_type_defn (fi, child_die, cu);
-  else if (child_die->tag == DW_TAG_template_type_param
-          || child_die->tag == DW_TAG_template_value_param)
-    {
-      struct symbol *arg = new_symbol (child_die, NULL, cu);
 
-      if (arg != NULL)
-       template_args->push_back (arg);
+  /* When we recurse, we want callees to add to this new variant
+     part.  */
+  scoped_restore save_current_variant_part
+    = make_scoped_restore (&fi->current_variant_part, new_part);
+
+  struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
+  if (discr == NULL)
+    {
+      /* It's a univariant form, an extension we support.  */
     }
-  else if (child_die->tag == DW_TAG_variant)
+  else if (discr->form_is_ref ())
     {
-      /* In a variant we want to get the discriminant and also add a
-        field for our sole member child.  */
-      struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
+      struct dwarf2_cu *target_cu = cu;
+      struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
 
-      for (die_info *variant_child = child_die->child;
-          variant_child != NULL;
-          variant_child = sibling_die (variant_child))
-       {
-         if (variant_child->tag == DW_TAG_member)
-           {
-             handle_struct_member_die (variant_child, type, fi,
-                                       template_args, cu);
-             /* Only handle the one.  */
-             break;
-           }
-       }
+      new_part->discriminant_offset = target_die->sect_off;
+    }
+  else
+    {
+      complaint (_("DW_AT_discr does not have DIE reference form"
+                  " - DIE at %s [in module %s]"),
+                sect_offset_str (die->sect_off),
+                objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
+    }
 
-      /* We don't handle this but we might as well report it if we see
-        it.  */
-      if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
-         complaint (_("DW_AT_discr_list is not supported yet"
-                      " - DIE at %s [in module %s]"),
-                    sect_offset_str (child_die->sect_off),
-                    objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
+  for (die_info *child_die = die->child;
+       child_die != NULL;
+       child_die = child_die->sibling)
+    handle_struct_member_die (child_die, type, fi, template_args, cu);
+}
+
+/* A helper for handle_struct_member_die that handles
+   DW_TAG_variant.  */
+
+static void
+handle_variant (struct die_info *die, struct type *type,
+               struct field_info *fi,
+               std::vector<struct symbol *> *template_args,
+               struct dwarf2_cu *cu)
+{
+  if (fi->current_variant_part == nullptr)
+    {
+      complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
+                  "- DIE at %s [in module %s]"),
+                sect_offset_str (die->sect_off),
+                objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
+      return;
+    }
+  if (fi->current_variant_part->processing_variant)
+    {
+      complaint (_("nested DW_TAG_variant seen "
+                  "- DIE at %s [in module %s]"),
+                sect_offset_str (die->sect_off),
+                objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
+      return;
+    }
+
+  scoped_restore save_processing_variant
+    = make_scoped_restore (&fi->current_variant_part->processing_variant,
+                          true);
+
+  fi->current_variant_part->variants.emplace_back ();
+  variant_field &variant = fi->current_variant_part->variants.back ();
+  variant.first_field = fi->fields.size ();
+
+  /* In a variant we want to get the discriminant and also add a
+     field for our sole member child.  */
+  struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
+  if (discr == nullptr)
+    {
+      discr = dwarf2_attr (die, DW_AT_discr_list, cu);
+      if (discr == nullptr || DW_BLOCK (discr)->size == 0)
+       variant.default_branch = true;
+      else
+       variant.discr_list_data = DW_BLOCK (discr);
+    }
+  else
+    variant.discriminant_value = DW_UNSND (discr);
+
+  for (die_info *variant_child = die->child;
+       variant_child != NULL;
+       variant_child = variant_child->sibling)
+    handle_struct_member_die (variant_child, type, fi, template_args, cu);
+
+  variant.last_field = fi->fields.size ();
+}
+
+/* A helper for process_structure_scope that handles a single member
+   DIE.  */
 
-      /* The first field was just added, so we can stash the
-        discriminant there.  */
-      gdb_assert (!fi->fields.empty ());
-      if (discr == NULL)
-       fi->fields.back ().variant.default_branch = true;
+static void
+handle_struct_member_die (struct die_info *child_die, struct type *type,
+                         struct field_info *fi,
+                         std::vector<struct symbol *> *template_args,
+                         struct dwarf2_cu *cu)
+{
+  if (child_die->tag == DW_TAG_member
+      || child_die->tag == DW_TAG_variable)
+    {
+      /* NOTE: carlton/2002-11-05: A C++ static data member
+        should be a DW_TAG_member that is a declaration, but
+        all versions of G++ as of this writing (so through at
+        least 3.2.1) incorrectly generate DW_TAG_variable
+        tags for them instead.  */
+      dwarf2_add_field (fi, child_die, cu);
+    }
+  else if (child_die->tag == DW_TAG_subprogram)
+    {
+      /* Rust doesn't have member functions in the C++ sense.
+        However, it does emit ordinary functions as children
+        of a struct DIE.  */
+      if (cu->language == language_rust)
+       read_func_scope (child_die, cu);
       else
-       fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
+       {
+         /* C++ member function.  */
+         dwarf2_add_member_fn (fi, child_die, type, cu);
+       }
+    }
+  else if (child_die->tag == DW_TAG_inheritance)
+    {
+      /* C++ base class field.  */
+      dwarf2_add_field (fi, child_die, cu);
+    }
+  else if (type_can_define_types (child_die))
+    dwarf2_add_type_defn (fi, child_die, cu);
+  else if (child_die->tag == DW_TAG_template_type_param
+          || child_die->tag == DW_TAG_template_value_param)
+    {
+      struct symbol *arg = new_symbol (child_die, NULL, cu);
+
+      if (arg != NULL)
+       template_args->push_back (arg);
     }
+  else if (child_die->tag == DW_TAG_variant_part)
+    handle_variant_part (child_die, type, fi, template_args, cu);
+  else if (child_die->tag == DW_TAG_variant)
+    handle_variant (child_die, type, fi, template_args, cu);
 }
 
 /* Finish creating a structure or union type, including filling in
@@ -15485,39 +15634,7 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
   if (type == NULL)
     type = read_structure_type (die, cu);
 
-  /* When reading a DW_TAG_variant_part, we need to notice when we
-     read the discriminant member, so we can record it later in the
-     discriminant_info.  */
-  bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
-  sect_offset discr_offset {};
   bool has_template_parameters = false;
-
-  if (is_variant_part)
-    {
-      struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
-      if (discr == NULL)
-       {
-         /* Maybe it's a univariant form, an extension we support.
-            In this case arrange not to check the offset.  */
-         is_variant_part = false;
-       }
-      else if (discr->form_is_ref ())
-       {
-         struct dwarf2_cu *target_cu = cu;
-         struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
-
-         discr_offset = target_die->sect_off;
-       }
-      else
-       {
-         complaint (_("DW_AT_discr does not have DIE reference form"
-                      " - DIE at %s [in module %s]"),
-                    sect_offset_str (die->sect_off),
-                    objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
-         is_variant_part = false;
-       }
-    }
-
   if (die->child != NULL && ! die_is_declaration (die, cu))
     {
       struct field_info fi;
@@ -15528,11 +15645,7 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
       while (child_die && child_die->tag)
        {
          handle_struct_member_die (child_die, type, &fi, &template_args, cu);
-
-         if (is_variant_part && discr_offset == child_die->sect_off)
-           fi.fields.back ().variant.is_discriminant = true;
-
-         child_die = sibling_die (child_die);
+         child_die = child_die->sibling;
        }
 
       /* Attach template arguments to type.  */
@@ -15552,7 +15665,7 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
        }
 
       /* Attach fields and member functions to the type.  */
-      if (fi.nfields)
+      if (fi.nfields () > 0)
        dwarf2_attach_fields_to_type (&fi, type, cu);
       if (!fi.fnfieldlists.empty ())
        {
@@ -15680,14 +15793,15 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
       else
        process_die (child_die, cu);
 
-      child_die = sibling_die (child_die);
+      child_die = child_die->sibling;
     }
 
   /* Do not consider external references.  According to the DWARF standard,
      these DIEs are identified by the fact that they have no byte_size
      attribute, and a declaration attribute.  */
   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
-      || !die_is_declaration (die, cu))
+      || !die_is_declaration (die, cu)
+      || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
     {
       struct symbol *sym = new_symbol (die, type, cu);
 
@@ -15725,8 +15839,9 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
     }
 }
 
-/* Assuming DIE is an enumeration type, and TYPE is its associated type,
-   update TYPE using some information only available in DIE's children.  */
+/* Assuming DIE is an enumeration type, and TYPE is its associated
+   type, update TYPE using some information only available in DIE's
+   children.  In particular, the fields are computed.  */
 
 static void
 update_enumeration_type_from_children (struct die_info *die,
@@ -15736,13 +15851,13 @@ update_enumeration_type_from_children (struct die_info *die,
   struct die_info *child_die;
   int unsigned_enum = 1;
   int flag_enum = 1;
-  ULONGEST mask = 0;
 
   auto_obstack obstack;
+  std::vector<struct field> fields;
 
   for (child_die = die->child;
        child_die != NULL && child_die->tag;
-       child_die = sibling_die (child_die))
+       child_die = child_die->sibling)
     {
       struct attribute *attr;
       LONGEST value;
@@ -15768,15 +15883,25 @@ update_enumeration_type_from_children (struct die_info *die,
          unsigned_enum = 0;
          flag_enum = 0;
        }
-      else if ((mask & value) != 0)
-       flag_enum = 0;
       else
-       mask |= value;
+       {
+         if (count_one_bits_ll (value) >= 2)
+           flag_enum = 0;
+       }
 
-      /* If we already know that the enum type is neither unsigned, nor
-        a flag type, no need to look at the rest of the enumerates.  */
-      if (!unsigned_enum && !flag_enum)
-       break;
+      fields.emplace_back ();
+      struct field &field = fields.back ();
+      FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
+      SET_FIELD_ENUMVAL (field, value);
+    }
+
+  if (!fields.empty ())
+    {
+      TYPE_NFIELDS (type) = fields.size ();
+      TYPE_FIELDS (type) = (struct field *)
+       TYPE_ALLOC (type, sizeof (struct field) * fields.size ());
+      memcpy (TYPE_FIELDS (type), fields.data (),
+             sizeof (struct field) * fields.size ());
     }
 
   if (unsigned_enum)
@@ -15799,7 +15924,7 @@ read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
   /* If the definition of this type lives in .debug_types, read that type.
      Don't follow DW_AT_specification though, that will take us back up
      the chain and we want to go down.  */
-  attr = dwarf2_attr_no_follow (die, DW_AT_signature);
+  attr = die->attr (DW_AT_signature);
   if (attr != nullptr)
     {
       type = get_DW_AT_signature_type (die, attr, cu);
@@ -15811,10 +15936,10 @@ read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
 
   type = alloc_type (objfile);
 
-  TYPE_CODE (type) = TYPE_CODE_ENUM;
+  type->set_code (TYPE_CODE_ENUM);
   name = dwarf2_full_name (NULL, die, cu);
   if (name != NULL)
-    TYPE_NAME (type) = name;
+    type->set_name (name);
 
   attr = dwarf2_attr (die, DW_AT_type, cu);
   if (attr != NULL)
@@ -15844,11 +15969,6 @@ read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
   if (die_is_declaration (die, cu))
     TYPE_STUB (type) = 1;
 
-  /* Finish the creation of this type by using the enum's children.
-     We must call this even when the underlying type has been provided
-     so that we can determine if we're looking at a "flag" enum.  */
-  update_enumeration_type_from_children (die, type, cu);
-
   /* If this type has an underlying type that is not a stub, then we
      may use its attributes.  We always use the "unsigned" attribute
      in this situation, because ordinarily we guess whether the type
@@ -15858,17 +15978,27 @@ read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
      the underlying type if needed.  */
   if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
     {
-      TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
+      struct type *underlying_type = TYPE_TARGET_TYPE (type);
+      underlying_type = check_typedef (underlying_type);
+      TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
       if (TYPE_LENGTH (type) == 0)
-       TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
+       TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
       if (TYPE_RAW_ALIGN (type) == 0
-         && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
-       set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
+         && TYPE_RAW_ALIGN (underlying_type) != 0)
+       set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
     }
 
   TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
 
-  return set_die_type (die, type, cu);
+  set_die_type (die, type, cu);
+
+  /* Finish the creation of this type by using the enum's children.
+     Note that, as usual, this must come after set_die_type to avoid
+     infinite recursion when trying to compute the names of the
+     enumerators.  */
+  update_enumeration_type_from_children (die, type, cu);
+
+  return type;
 }
 
 /* Given a pointer to a die which begins an enumeration, process all
@@ -15889,8 +16019,6 @@ process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
   if (die->child != NULL)
     {
       struct die_info *child_die;
-      struct symbol *sym;
-      std::vector<struct field> fields;
       const char *name;
 
       child_die = die->child;
@@ -15904,29 +16032,10 @@ process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
            {
              name = dwarf2_name (child_die, cu);
              if (name)
-               {
-                 sym = new_symbol (child_die, this_type, cu);
-
-                 fields.emplace_back ();
-                 struct field &field = fields.back ();
-
-                 FIELD_NAME (field) = sym->linkage_name ();
-                 FIELD_TYPE (field) = NULL;
-                 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
-                 FIELD_BITSIZE (field) = 0;
-               }
+               new_symbol (child_die, this_type, cu);
            }
 
-         child_die = sibling_die (child_die);
-       }
-
-      if (!fields.empty ())
-       {
-         TYPE_NFIELDS (this_type) = fields.size ();
-         TYPE_FIELDS (this_type) = (struct field *)
-           TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
-         memcpy (TYPE_FIELDS (this_type), fields.data (),
-                 sizeof (struct field) * fields.size ());
+         child_die = child_die->sibling;
        }
     }
 
@@ -16028,7 +16137,7 @@ read_array_type (struct die_info *die, struct dwarf2_cu *cu)
              range_types.push_back (child_type);
             }
        }
-      child_die = sibling_die (child_die);
+      child_die = child_die->sibling;
     }
 
   /* Dwarf2 dimensions are output from left to right, create the
@@ -16077,7 +16186,7 @@ read_array_type (struct die_info *die, struct dwarf2_cu *cu)
 
   name = dwarf2_name (die, cu);
   if (name)
-    TYPE_NAME (type) = name;
+    type->set_name (name);
 
   maybe_set_alignment (cu, die, type);
 
@@ -16172,7 +16281,7 @@ mark_common_block_symbol_computed (struct symbol *sym,
   struct dwarf2_locexpr_baton *baton;
   gdb_byte *ptr;
   unsigned int cu_off;
-  enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
+  enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
   LONGEST offset = 0;
 
   gdb_assert (common_loc && member_loc);
@@ -16188,7 +16297,7 @@ mark_common_block_symbol_computed (struct symbol *sym,
 
   if (member_loc->form_is_constant ())
     {
-      offset = dwarf2_get_attr_constant_value (member_loc, 0);
+      offset = member_loc->constant_value (0);
       baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
     }
   else
@@ -16265,7 +16374,7 @@ read_common_block (struct die_info *die, struct dwarf2_cu *cu)
 
       for (child_die = die->child;
           child_die && child_die->tag;
-          child_die = sibling_die (child_die))
+          child_die = child_die->sibling)
        ++n_entries;
 
       size = (sizeof (struct common_block)
@@ -16278,7 +16387,7 @@ read_common_block (struct die_info *die, struct dwarf2_cu *cu)
 
       for (child_die = die->child;
           child_die && child_die->tag;
-          child_die = sibling_die (child_die))
+          child_die = child_die->sibling)
        {
          /* Create the symbol in the DW_TAG_common_block block in the current
             symbol scope.  */
@@ -16400,7 +16509,7 @@ read_namespace (struct die_info *die, struct dwarf2_cu *cu)
       while (child_die && child_die->tag)
        {
          process_die (child_die, cu);
-         child_die = sibling_die (child_die);
+         child_die = child_die->sibling;
        }
     }
 }
@@ -16436,7 +16545,7 @@ read_module (struct die_info *die, struct dwarf2_cu *cu)
   while (child_die && child_die->tag)
     {
       process_die (child_die, cu);
-      child_die = sibling_die (child_die);
+      child_die = child_die->sibling;
     }
 }
 
@@ -16480,7 +16589,7 @@ static struct type *
 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
 {
   struct gdbarch *gdbarch
-    = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
+    = cu->per_cu->dwarf2_per_objfile->objfile->arch ();
   struct comp_unit_head *cu_header = &cu->header;
   struct type *type;
   struct attribute *attr_byte_size;
@@ -16569,9 +16678,9 @@ read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
   if (type)
     return type;
 
-  if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
+  if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
     type = lookup_methodptr_type (to_type);
-  else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
+  else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
     {
       struct type *new_type
        = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
@@ -16636,7 +16745,7 @@ add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
   base_type = copy_type (base_type);
   inner_array = base_type;
 
-  while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
+  while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
     {
       TYPE_TARGET_TYPE (inner_array) =
        copy_type (TYPE_TARGET_TYPE (inner_array));
@@ -16665,7 +16774,7 @@ read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
 
   /* In case the const qualifier is applied to an array type, the element type
      is so qualified, not the array type (section 6.7.3 of C99).  */
-  if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
+  if (base_type->code () == TYPE_CODE_ARRAY)
     return add_array_cv_type (die, cu, base_type, 1, 0);
 
   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
@@ -16687,7 +16796,7 @@ read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
   /* In case the volatile qualifier is applied to an array type, the
      element type is so qualified, not the array type (section 6.7.3
      of C99).  */
-  if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
+  if (base_type->code () == TYPE_CODE_ARRAY)
     return add_array_cv_type (die, cu, base_type, 0, 1);
 
   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
@@ -16739,7 +16848,7 @@ static struct type *
 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
 {
   struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   struct type *type, *range_type, *index_type, *char_type;
   struct attribute *attr;
   struct dynamic_prop prop;
@@ -16780,7 +16889,7 @@ read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
        {
          /* Pass 0 as the default as we know this attribute is constant
             and the default value will not be returned.  */
-         LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
+         LONGEST sz = len->constant_value (0);
          prop_type = cu->per_cu->int_type (sz, true);
        }
       else
@@ -16802,12 +16911,12 @@ read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
         indirection.  There's no need to create a dynamic property in this
         case.  Pass 0 for the default value as we know it will not be
         returned in this case.  */
-      length = dwarf2_get_attr_constant_value (attr, 0);
+      length = attr->constant_value (0);
     }
   else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
     {
       /* We don't currently support non-constant byte sizes for strings.  */
-      length = dwarf2_get_attr_constant_value (attr, 1);
+      length = attr->constant_value (1);
     }
   else
     {
@@ -16935,7 +17044,7 @@ read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
            nparams++;
          else if (child_die->tag == DW_TAG_unspecified_parameters)
            TYPE_VARARGS (ftype) = 1;
-         child_die = sibling_die (child_die);
+         child_die = child_die->sibling;
        }
 
       /* Allocate storage for parameters and fill them in.  */
@@ -17006,7 +17115,7 @@ read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
              TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
              iparams++;
            }
-         child_die = sibling_die (child_die);
+         child_die = child_die->sibling;
        }
     }
 
@@ -17036,6 +17145,16 @@ read_typedef (struct die_info *die, struct dwarf2_cu *cu)
                 sect_offset_str (die->sect_off), objfile_name (objfile));
       TYPE_TARGET_TYPE (this_type) = NULL;
     }
+  if (name == NULL)
+    {
+      /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
+        anonymous typedefs, which is, strictly speaking, invalid DWARF.
+        Handle these by just returning the target type, rather than
+        constructing an anonymous typedef type and trying to handle this
+        elsewhere.  */
+      set_die_type (die, target_type, cu);
+      return target_type;
+    }
   return this_type;
 }
 
@@ -17047,7 +17166,7 @@ static struct type *
 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
                        const char *name_hint, enum bfd_endian byte_order)
 {
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   const struct floatformat **format;
   struct type *type;
 
@@ -17095,7 +17214,7 @@ dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
                                 int bits, const char *name_hint,
                                 enum bfd_endian byte_order)
 {
-  gdbarch *gdbarch = get_objfile_arch (objfile);
+  gdbarch *gdbarch = objfile->arch ();
   struct type *tt = nullptr;
 
   /* Try to find a suitable floating point builtin type of size BITS.
@@ -17168,7 +17287,7 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu)
   if (!name)
     complaint (_("DW_AT_name missing from DW_TAG_base_type"));
 
-  arch = get_objfile_arch (objfile);
+  arch = objfile->arch ();
   enum bfd_endian byte_order = gdbarch_byte_order (arch);
 
   attr = dwarf2_attr (die, DW_AT_endianity, cu);
@@ -17203,7 +17322,19 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu)
       case DW_ATE_complex_float:
        type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
                                                byte_order);
-       type = init_complex_type (objfile, name, type);
+       if (type->code () == TYPE_CODE_ERROR)
+         {
+           if (name == nullptr)
+             {
+               struct obstack *obstack
+                 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
+               name = obconcat (obstack, "_Complex ", TYPE_NAME (type),
+                                nullptr);
+             }
+           type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
+         }
+       else
+         type = init_complex_type (name, type);
        break;
       case DW_ATE_decimal_float:
        type = init_decfloat_type (objfile, bits, name);
@@ -17376,7 +17507,7 @@ attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
     }
   else if (attr->form_is_constant ())
     {
-      prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
+      prop->data.const_val = attr->constant_value (0);
       prop->kind = PROP_CONST;
     }
   else
@@ -17446,7 +17577,7 @@ read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
      GCC produces an empty range DIE.
      FIXME: muller/2010-05-28: Possible references to object for low bound,
      high bound or count are not yet handled by this code.  */
-  if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
+  if (index_type->code () == TYPE_CODE_VOID)
     index_type = cu->per_cu->addr_sized_int_type (false);
 
   return index_type;
@@ -17554,7 +17685,7 @@ read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
   LONGEST bias = 0;
   struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
   if (bias_attr != nullptr && bias_attr->form_is_constant ())
-    bias = dwarf2_get_attr_constant_value (bias_attr, 0);
+    bias = bias_attr->constant_value (0);
 
   /* Normally, the DWARF producers are expected to use a signed
      constant form (Eg. DW_FORM_sdata) to express negative bounds.
@@ -17626,7 +17757,7 @@ read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
 
   name = dwarf2_name (die, cu);
   if (name)
-    TYPE_NAME (range_type) = name;
+    range_type->set_name (name);
 
   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
   if (attr != nullptr)
@@ -17649,7 +17780,7 @@ read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
 
   type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
                    NULL);
-  TYPE_NAME (type) = dwarf2_name (die, cu);
+  type->set_name (dwarf2_name (die, cu));
 
   /* In Ada, an unspecified type is typically used when the description
      of the type is deferred to a different unit.  When encountering
@@ -17815,11 +17946,15 @@ read_full_die_1 (const struct die_reader_specs *reader,
         indexes_that_need_reprocess.push_back (i);
     }
 
-  struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
+  struct attribute *attr = die->attr (DW_AT_str_offsets_base);
   if (attr != nullptr)
     cu->str_offsets_base = DW_UNSND (attr);
 
-  auto maybe_addr_base = lookup_addr_base(die);
+  attr = die->attr (DW_AT_loclists_base);
+  if (attr != nullptr)
+    cu->loclist_base = DW_UNSND (attr);
+
+  auto maybe_addr_base = die->addr_base ();
   if (maybe_addr_base.has_value ())
     cu->addr_base = *maybe_addr_base;
   for (int index : indexes_that_need_reprocess)
@@ -18174,18 +18309,17 @@ partial_die_info::read (const struct die_reader_specs *reader,
   int has_high_pc_attr = 0;
   int high_pc_relative = 0;
 
-  std::vector<struct attribute> attr_vec (abbrev.num_attrs);
   for (i = 0; i < abbrev.num_attrs; ++i)
     {
+      attribute attr;
       bool need_reprocess;
-      info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
+      info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
                                 info_ptr, &need_reprocess);
       /* String and address offsets that need to do the reprocessing have
          already been read at this point, so there is no need to wait until
         the loop terminates to do the reprocessing.  */
       if (need_reprocess)
-       read_attribute_reprocess (reader, &attr_vec[i]);
-      attribute &attr = attr_vec[i];
+       read_attribute_reprocess (reader, &attr);
       /* Store the data if it is of an attribute we want to keep in a
          partial symbol table.  */
       switch (attr.name)
@@ -18209,8 +18343,7 @@ partial_die_info::read (const struct die_reader_specs *reader,
                struct objfile *objfile = dwarf2_per_objfile->objfile;
 
                name
-                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
-                                             &objfile->per_bfd->storage_obstack);
+                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
              }
              break;
            }
@@ -18220,7 +18353,12 @@ partial_die_info::read (const struct die_reader_specs *reader,
          /* Note that both forms of linkage name might appear.  We
             assume they will be the same, and we only store the last
             one we see.  */
-         linkage_name = DW_STRING (&attr);
+         linkage_name = attr.value_as_string ();
+         /* rustc emits invalid values for DW_AT_linkage_name.  Ignore these.
+            See https://github.com/rust-lang/rust/issues/32925.  */
+         if (cu->language == language_rust && linkage_name != NULL
+             && strchr (linkage_name, '{') != NULL)
+           linkage_name = NULL;
          break;
        case DW_AT_low_pc:
          has_low_pc_attr = 1;
@@ -18261,7 +18399,7 @@ partial_die_info::read (const struct die_reader_specs *reader,
        case DW_AT_specification:
        case DW_AT_extension:
          has_specification = 1;
-         spec_offset = dwarf2_get_ref_die_offset (&attr);
+         spec_offset = attr.get_ref_die_offset ();
          spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
                                   || cu->per_cu->is_dwz);
          break;
@@ -18273,13 +18411,13 @@ partial_die_info::read (const struct die_reader_specs *reader,
          else
            {
              const gdb_byte *buffer = reader->buffer;
-             sect_offset off = dwarf2_get_ref_die_offset (&attr);
+             sect_offset off = attr.get_ref_die_offset ();
              const gdb_byte *sibling_ptr = buffer + to_underlying (off);
 
              if (sibling_ptr < info_ptr)
                complaint (_("DW_AT_sibling points backwards"));
              else if (sibling_ptr > reader->buffer_end)
-               dwarf2_section_buffer_overflow_complaint (reader->die_section);
+               reader->die_section->overflow_complaint ();
              else
                sibling = sibling_ptr;
            }
@@ -18318,7 +18456,7 @@ partial_die_info::read (const struct die_reader_specs *reader,
        case DW_AT_import:
          if (tag == DW_TAG_imported_unit)
            {
-             d.sect_off = dwarf2_get_ref_die_offset (&attr);
+             d.sect_off = attr.get_ref_die_offset ();
              is_dwz = (attr.form == DW_FORM_GNU_ref_alt
                                  || cu->per_cu->is_dwz);
            }
@@ -18376,7 +18514,7 @@ partial_die_info::read (const struct die_reader_specs *reader,
       if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
        {
          struct objfile *objfile = dwarf2_per_objfile->objfile;
-         struct gdbarch *gdbarch = get_objfile_arch (objfile);
+         struct gdbarch *gdbarch = objfile->arch ();
 
          complaint (_("DW_AT_low_pc %s is zero "
                       "for DIE at %s [in module %s]"),
@@ -18388,7 +18526,7 @@ partial_die_info::read (const struct die_reader_specs *reader,
       else if (lowpc >= highpc)
        {
          struct objfile *objfile = dwarf2_per_objfile->objfile;
-         struct gdbarch *gdbarch = get_objfile_arch (objfile);
+         struct gdbarch *gdbarch = objfile->arch ();
 
          complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
                       "for DIE at %s [in module %s]"),
@@ -18434,7 +18572,7 @@ find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
   struct partial_die_info *pd = NULL;
 
   if (offset_in_dwz == cu->per_cu->is_dwz
-      && offset_in_cu_p (&cu->header, sect_off))
+      && cu->header.offset_in_cu_p (sect_off))
     {
       pd = cu->find_partial_die (sect_off);
       if (pd != NULL)
@@ -18535,15 +18673,32 @@ guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
          if (actual_class_name != NULL)
            {
              struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
-             struct_pdi->name
-               = obstack_strdup (&objfile->per_bfd->storage_obstack,
-                                 actual_class_name.get ());
+             struct_pdi->name = objfile->intern (actual_class_name.get ());
            }
          break;
        }
     }
 }
 
+/* Return true if a DIE with TAG may have the DW_AT_const_value
+   attribute.  */
+
+static bool
+can_have_DW_AT_const_value_p (enum dwarf_tag tag)
+{
+  switch (tag)
+    {
+    case DW_TAG_constant:
+    case DW_TAG_enumerator:
+    case DW_TAG_formal_parameter:
+    case DW_TAG_template_value_param:
+    case DW_TAG_variable:
+      return true;
+    }
+
+  return false;
+}
+
 void
 partial_die_info::fixup (struct dwarf2_cu *cu)
 {
@@ -18576,6 +18731,24 @@ partial_die_info::fixup (struct dwarf2_cu *cu)
        }
     }
 
+  if (!has_const_value && has_specification
+      && can_have_DW_AT_const_value_p (tag))
+    {
+      struct partial_die_info *spec_die;
+
+      auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
+      spec_die = res.pdi;
+      cu = res.cu;
+
+      spec_die->fixup (cu);
+
+      if (spec_die->has_const_value)
+       {
+         /* Copy DW_AT_const_value attribute if it is set.  */
+         has_const_value = spec_die->has_const_value;
+       }
+    }
+
   /* Set default names for some unnamed DIEs.  */
 
   if (name == NULL && tag == DW_TAG_namespace)
@@ -18617,19 +18790,98 @@ partial_die_info::fixup (struct dwarf2_cu *cu)
            base = demangled.get ();
 
          struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
-         name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
+         name = objfile->intern (base);
        }
     }
 
   fixup_called = 1;
 }
 
+/* Read the .debug_loclists header contents from the given SECTION in the
+   HEADER.  */
+static void
+read_loclist_header (struct loclist_header *header,
+                     struct dwarf2_section_info *section)
+{
+  unsigned int bytes_read;
+  bfd *abfd = section->get_bfd_owner ();
+  const gdb_byte *info_ptr = section->buffer;
+  header->length = read_initial_length (abfd, info_ptr, &bytes_read);
+  info_ptr += bytes_read;
+  header->version = read_2_bytes (abfd, info_ptr);
+  info_ptr += 2;
+  header->addr_size = read_1_byte (abfd, info_ptr);
+  info_ptr += 1;
+  header->segment_collector_size = read_1_byte (abfd, info_ptr);
+  info_ptr += 1;
+  header->offset_entry_count = read_4_bytes (abfd, info_ptr);
+}
+
+/* Return the DW_AT_loclists_base value for the CU.  */
+static ULONGEST
+lookup_loclist_base (struct dwarf2_cu *cu)
+{
+  /* For the .dwo unit, the loclist_base points to the first offset following
+     the header. The header consists of the following entities-
+     1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
+        bit format)
+     2. version (2 bytes)
+     3. address size (1 byte)
+     4. segment selector size (1 byte)
+     5. offset entry count (4 bytes)
+     These sizes are derived as per the DWARFv5 standard.  */
+  if (cu->dwo_unit != nullptr)
+    {
+      if (cu->header.initial_length_size == 4)
+        return LOCLIST_HEADER_SIZE32;
+      return LOCLIST_HEADER_SIZE64;
+    }
+  return cu->loclist_base;
+}
+
+/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
+   array of offsets in the .debug_loclists section.  */
+static CORE_ADDR
+read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
+{
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
+  bfd *abfd = objfile->obfd;
+  ULONGEST loclist_base = lookup_loclist_base (cu);
+  struct dwarf2_section_info *section = cu_debug_loc_section (cu);
+
+  section->read (objfile);
+  if (section->buffer == NULL)
+    complaint (_("DW_FORM_loclistx used without .debug_loclists "
+               "section [in module %s]"), objfile_name (objfile));
+  struct loclist_header header;
+  read_loclist_header (&header, section);
+  if (loclist_index >= header.offset_entry_count)
+    complaint (_("DW_FORM_loclistx pointing outside of "
+               ".debug_loclists offset array [in module %s]"),
+               objfile_name (objfile));
+  if (loclist_base + loclist_index * cu->header.offset_size
+       >= section->size)
+    complaint (_("DW_FORM_loclistx pointing outside of "
+               ".debug_loclists section [in module %s]"),
+               objfile_name (objfile));
+  const gdb_byte *info_ptr
+    = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
+
+  if (cu->header.offset_size == 4)
+    return bfd_get_32 (abfd, info_ptr) + loclist_base;
+  else
+    return bfd_get_64 (abfd, info_ptr) + loclist_base;
+}
+
 /* Process the attributes that had to be skipped in the first round. These
    attributes are the ones that need str_offsets_base or addr_base attributes.
    They could not have been processed in the first round, because at the time
    the values of str_offsets_base or addr_base may not have been known.  */
-void read_attribute_reprocess (const struct die_reader_specs *reader,
-                              struct attribute *attr)
+static void
+read_attribute_reprocess (const struct die_reader_specs *reader,
+                         struct attribute *attr)
 {
   struct dwarf2_cu *cu = reader->cu;
   switch (attr->form)
@@ -18638,6 +18890,9 @@ void read_attribute_reprocess (const struct die_reader_specs *reader,
       case DW_FORM_GNU_addr_index:
         DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
         break;
+      case DW_FORM_loclistx:
+        DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
+        break;
       case DW_FORM_strx:
       case DW_FORM_strx1:
       case DW_FORM_strx2:
@@ -18675,7 +18930,6 @@ read_attribute_value (const struct die_reader_specs *reader,
   struct dwarf2_per_objfile *dwarf2_per_objfile
     = cu->per_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
   bfd *abfd = reader->abfd;
   struct comp_unit_head *cu_header = &cu->header;
   unsigned int bytes_read;
@@ -18687,20 +18941,24 @@ read_attribute_value (const struct die_reader_specs *reader,
     {
     case DW_FORM_ref_addr:
       if (cu->header.version == 2)
-       DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
+       DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
+                                                  &bytes_read);
       else
-       DW_UNSND (attr) = read_offset (abfd, info_ptr,
-                                      &cu->header, &bytes_read);
+       DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
+                                                 &bytes_read);
       info_ptr += bytes_read;
       break;
     case DW_FORM_GNU_ref_alt:
-      DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
+      DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
       info_ptr += bytes_read;
       break;
     case DW_FORM_addr:
-      DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
-      DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
-      info_ptr += bytes_read;
+      {
+       struct gdbarch *gdbarch = objfile->arch ();
+       DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
+       DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
+       info_ptr += bytes_read;
+      }
       break;
     case DW_FORM_block2:
       blk = dwarf_alloc_block (cu);
@@ -18738,9 +18996,16 @@ read_attribute_value (const struct die_reader_specs *reader,
       DW_BLOCK (attr) = blk;
       break;
     case DW_FORM_sec_offset:
-      DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
+      DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
       info_ptr += bytes_read;
       break;
+    case DW_FORM_loclistx:
+      {
+        *need_reprocess = true;
+        DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
+        info_ptr += bytes_read;
+      }
+      break;
     case DW_FORM_string:
       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
       DW_STRING_IS_CANONICAL (attr) = 0;
@@ -18760,9 +19025,9 @@ read_attribute_value (const struct die_reader_specs *reader,
     case DW_FORM_line_strp:
       if (!cu->per_cu->is_dwz)
        {
-         DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
-                                                       abfd, info_ptr,
-                                                       cu_header, &bytes_read);
+         DW_STRING (attr)
+           = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
+                                                   &bytes_read);
          DW_STRING_IS_CANONICAL (attr) = 0;
          info_ptr += bytes_read;
          break;
@@ -18771,11 +19036,10 @@ read_attribute_value (const struct die_reader_specs *reader,
     case DW_FORM_GNU_strp_alt:
       {
        struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
-       LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
-                                         &bytes_read);
+       LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
+                                                    &bytes_read);
 
-       DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
-                                                         dwz, str_offset);
+       DW_STRING (attr) = dwz->read_string (objfile, str_offset);
        DW_STRING_IS_CANONICAL (attr) = 0;
        info_ptr += bytes_read;
       }
@@ -18945,227 +19209,42 @@ read_attribute (const struct die_reader_specs *reader,
                               need_reprocess);
 }
 
-static CORE_ADDR
-read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
-             unsigned int *bytes_read)
-{
-  struct comp_unit_head *cu_header = &cu->header;
-  CORE_ADDR retval = 0;
-
-  if (cu_header->signed_addr_p)
-    {
-      switch (cu_header->addr_size)
-       {
-       case 2:
-         retval = bfd_get_signed_16 (abfd, buf);
-         break;
-       case 4:
-         retval = bfd_get_signed_32 (abfd, buf);
-         break;
-       case 8:
-         retval = bfd_get_signed_64 (abfd, buf);
-         break;
-       default:
-         internal_error (__FILE__, __LINE__,
-                         _("read_address: bad switch, signed [in module %s]"),
-                         bfd_get_filename (abfd));
-       }
-    }
-  else
-    {
-      switch (cu_header->addr_size)
-       {
-       case 2:
-         retval = bfd_get_16 (abfd, buf);
-         break;
-       case 4:
-         retval = bfd_get_32 (abfd, buf);
-         break;
-       case 8:
-         retval = bfd_get_64 (abfd, buf);
-         break;
-       default:
-         internal_error (__FILE__, __LINE__,
-                         _("read_address: bad switch, "
-                           "unsigned [in module %s]"),
-                         bfd_get_filename (abfd));
-       }
-    }
+/* Return pointer to string at .debug_str offset STR_OFFSET.  */
 
-  *bytes_read = cu_header->addr_size;
-  return retval;
+static const char *
+read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                               LONGEST str_offset)
+{
+  return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
+                                             str_offset, "DW_FORM_strp");
 }
 
-/* Cover function for read_initial_length.
-   Returns the length of the object at BUF, and stores the size of the
-   initial length in *BYTES_READ and stores the size that offsets will be in
-   *OFFSET_SIZE.
-   If the initial length size is not equivalent to that specified in
-   CU_HEADER then issue a complaint.
-   This is useful when reading non-comp-unit headers.  */
+/* Return pointer to string at .debug_str offset as read from BUF.
+   BUF is assumed to be in a compilation unit described by CU_HEADER.
+   Return *BYTES_READ_PTR count of bytes read from BUF.  */
 
-static LONGEST
-read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
-                                       const struct comp_unit_head *cu_header,
-                                       unsigned int *bytes_read,
-                                       unsigned int *offset_size)
+static const char *
+read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
+                     const gdb_byte *buf,
+                     const struct comp_unit_head *cu_header,
+                     unsigned int *bytes_read_ptr)
 {
-  LONGEST length = read_initial_length (abfd, buf, bytes_read);
-
-  gdb_assert (cu_header->initial_length_size == 4
-             || cu_header->initial_length_size == 8
-             || cu_header->initial_length_size == 12);
+  LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
 
-  if (cu_header->initial_length_size != *bytes_read)
-    complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
-
-  *offset_size = (*bytes_read == 4) ? 4 : 8;
-  return length;
+  return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
 }
 
-/* Read an offset from the data stream.  The size of the offset is
-   given by cu_header->offset_size.  */
+/* See read.h.  */
 
-static LONGEST
-read_offset (bfd *abfd, const gdb_byte *buf,
-            const struct comp_unit_head *cu_header,
-             unsigned int *bytes_read)
+const char *
+dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
+                          const struct comp_unit_head *cu_header,
+                          unsigned int *bytes_read_ptr)
 {
-  LONGEST offset = read_offset (abfd, buf, cu_header->offset_size);
-
-  *bytes_read = cu_header->offset_size;
-  return offset;
-}
+  bfd *abfd = objfile->obfd;
+  LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
 
-static const gdb_byte *
-read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
-{
-  /* If the size of a host char is 8 bits, we can return a pointer
-     to the buffer, otherwise we have to copy the data to a buffer
-     allocated on the temporary obstack.  */
-  gdb_assert (HOST_CHAR_BIT == 8);
-  return buf;
-}
-
-static const char *
-read_direct_string (bfd *abfd, const gdb_byte *buf,
-                   unsigned int *bytes_read_ptr)
-{
-  /* If the size of a host char is 8 bits, we can return a pointer
-     to the string, otherwise we have to copy the string to a buffer
-     allocated on the temporary obstack.  */
-  gdb_assert (HOST_CHAR_BIT == 8);
-  if (*buf == '\0')
-    {
-      *bytes_read_ptr = 1;
-      return NULL;
-    }
-  *bytes_read_ptr = strlen ((const char *) buf) + 1;
-  return (const char *) buf;
-}
-
-/* Return pointer to string at section SECT offset STR_OFFSET with error
-   reporting strings FORM_NAME and SECT_NAME.  */
-
-static const char *
-read_indirect_string_at_offset_from (struct objfile *objfile,
-                                    bfd *abfd, LONGEST str_offset,
-                                    struct dwarf2_section_info *sect,
-                                    const char *form_name,
-                                    const char *sect_name)
-{
-  sect->read (objfile);
-  if (sect->buffer == NULL)
-    error (_("%s used without %s section [in module %s]"),
-          form_name, sect_name, bfd_get_filename (abfd));
-  if (str_offset >= sect->size)
-    error (_("%s pointing outside of %s section [in module %s]"),
-          form_name, sect_name, bfd_get_filename (abfd));
-  gdb_assert (HOST_CHAR_BIT == 8);
-  if (sect->buffer[str_offset] == '\0')
-    return NULL;
-  return (const char *) (sect->buffer + str_offset);
-}
-
-/* Return pointer to string at .debug_str offset STR_OFFSET.  */
-
-static const char *
-read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
-                               bfd *abfd, LONGEST str_offset)
-{
-  return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
-                                             abfd, str_offset,
-                                             &dwarf2_per_objfile->str,
-                                             "DW_FORM_strp", ".debug_str");
-}
-
-/* Return pointer to string at .debug_line_str offset STR_OFFSET.  */
-
-static const char *
-read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
-                                    bfd *abfd, LONGEST str_offset)
-{
-  return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
-                                             abfd, str_offset,
-                                             &dwarf2_per_objfile->line_str,
-                                             "DW_FORM_line_strp",
-                                             ".debug_line_str");
-}
-
-/* Read a string at offset STR_OFFSET in the .debug_str section from
-   the .dwz file DWZ.  Throw an error if the offset is too large.  If
-   the string consists of a single NUL byte, return NULL; otherwise
-   return a pointer to the string.  */
-
-static const char *
-read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
-                              LONGEST str_offset)
-{
-  dwz->str.read (objfile);
-
-  if (dwz->str.buffer == NULL)
-    error (_("DW_FORM_GNU_strp_alt used without .debug_str "
-            "section [in module %s]"),
-          bfd_get_filename (dwz->dwz_bfd.get ()));
-  if (str_offset >= dwz->str.size)
-    error (_("DW_FORM_GNU_strp_alt pointing outside of "
-            ".debug_str section [in module %s]"),
-          bfd_get_filename (dwz->dwz_bfd.get ()));
-  gdb_assert (HOST_CHAR_BIT == 8);
-  if (dwz->str.buffer[str_offset] == '\0')
-    return NULL;
-  return (const char *) (dwz->str.buffer + str_offset);
-}
-
-/* Return pointer to string at .debug_str offset as read from BUF.
-   BUF is assumed to be in a compilation unit described by CU_HEADER.
-   Return *BYTES_READ_PTR count of bytes read from BUF.  */
-
-static const char *
-read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
-                     const gdb_byte *buf,
-                     const struct comp_unit_head *cu_header,
-                     unsigned int *bytes_read_ptr)
-{
-  LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
-
-  return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
-}
-
-/* Return pointer to string at .debug_line_str offset as read from BUF.
-   BUF is assumed to be in a compilation unit described by CU_HEADER.
-   Return *BYTES_READ_PTR count of bytes read from BUF.  */
-
-static const char *
-read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
-                          bfd *abfd, const gdb_byte *buf,
-                          const struct comp_unit_head *cu_header,
-                          unsigned int *bytes_read_ptr)
-{
-  LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
-
-  return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
-                                             str_offset);
+  return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
 }
 
 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
@@ -19220,14 +19299,10 @@ read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
   return read_addr_index (cu, addr_index);
 }
 
-/* Given an index in .debug_addr, fetch the value.
-   NOTE: This can be called during dwarf expression evaluation,
-   long after the debug information has been read, and thus per_cu->cu
-   may no longer exist.  */
+/* See read.h.  */
 
 CORE_ADDR
-dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
-                       unsigned int addr_index)
+dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
 {
   struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
   struct dwarf2_cu *cu = per_cu->cu;
@@ -19257,7 +19332,7 @@ dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
     }
   else
     {
-      cutu_reader reader (per_cu, NULL, 0, 0, false);
+      cutu_reader reader (per_cu, NULL, 0, false);
       addr_base = reader.cu->addr_base;
       addr_size = reader.cu->header.addr_size;
     }
@@ -19450,24 +19525,6 @@ dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
   return NULL;
 }
 
-/* Return the named attribute or NULL if not there,
-   but do not follow DW_AT_specification, etc.
-   This is for use in contexts where we're reading .debug_types dies.
-   Following DW_AT_specification, DW_AT_abstract_origin will take us
-   back up the chain, and we want to go down.  */
-
-static struct attribute *
-dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
-{
-  unsigned int i;
-
-  for (i = 0; i < die->num_attrs; ++i)
-    if (die->attrs[i].name == name)
-      return &die->attrs[i];
-
-  return NULL;
-}
-
 /* Return the string associated with a string-typed attribute, or NULL if it
    is either not found or is of an incorrect type.  */
 
@@ -19481,17 +19538,8 @@ dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *c
 
   if (attr != NULL)
     {
-      if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
-         || attr->form == DW_FORM_string
-         || attr->form == DW_FORM_strx
-         || attr->form == DW_FORM_strx1
-         || attr->form == DW_FORM_strx2
-         || attr->form == DW_FORM_strx3
-         || attr->form == DW_FORM_strx4
-         || attr->form == DW_FORM_GNU_str_index
-         || attr->form == DW_FORM_GNU_strp_alt)
-       str = DW_STRING (attr);
-      else
+      str = attr->value_as_string ();
+      if (str == nullptr)
         complaint (_("string type expected for attribute %s for "
                     "DIE at %s in module %s"),
                   dwarf_attr_name (name), sect_offset_str (die->sect_off),
@@ -19594,140 +19642,6 @@ get_debug_line_section (struct dwarf2_cu *cu)
   return section;
 }
 
-/* Read directory or file name entry format, starting with byte of
-   format count entries, ULEB128 pairs of entry formats, ULEB128 of
-   entries count and the entries themselves in the described entry
-   format.  */
-
-static void
-read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
-                       bfd *abfd, const gdb_byte **bufp,
-                       struct line_header *lh,
-                       const struct comp_unit_head *cu_header,
-                       void (*callback) (struct line_header *lh,
-                                         const char *name,
-                                         dir_index d_index,
-                                         unsigned int mod_time,
-                                         unsigned int length))
-{
-  gdb_byte format_count, formati;
-  ULONGEST data_count, datai;
-  const gdb_byte *buf = *bufp;
-  const gdb_byte *format_header_data;
-  unsigned int bytes_read;
-
-  format_count = read_1_byte (abfd, buf);
-  buf += 1;
-  format_header_data = buf;
-  for (formati = 0; formati < format_count; formati++)
-    {
-      read_unsigned_leb128 (abfd, buf, &bytes_read);
-      buf += bytes_read;
-      read_unsigned_leb128 (abfd, buf, &bytes_read);
-      buf += bytes_read;
-    }
-
-  data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
-  buf += bytes_read;
-  for (datai = 0; datai < data_count; datai++)
-    {
-      const gdb_byte *format = format_header_data;
-      struct file_entry fe;
-
-      for (formati = 0; formati < format_count; formati++)
-       {
-         ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
-         format += bytes_read;
-
-         ULONGEST form  = read_unsigned_leb128 (abfd, format, &bytes_read);
-         format += bytes_read;
-
-         gdb::optional<const char *> string;
-         gdb::optional<unsigned int> uint;
-
-         switch (form)
-           {
-           case DW_FORM_string:
-             string.emplace (read_direct_string (abfd, buf, &bytes_read));
-             buf += bytes_read;
-             break;
-
-           case DW_FORM_line_strp:
-             string.emplace (read_indirect_line_string (dwarf2_per_objfile,
-                                                        abfd, buf,
-                                                        cu_header,
-                                                        &bytes_read));
-             buf += bytes_read;
-             break;
-
-           case DW_FORM_data1:
-             uint.emplace (read_1_byte (abfd, buf));
-             buf += 1;
-             break;
-
-           case DW_FORM_data2:
-             uint.emplace (read_2_bytes (abfd, buf));
-             buf += 2;
-             break;
-
-           case DW_FORM_data4:
-             uint.emplace (read_4_bytes (abfd, buf));
-             buf += 4;
-             break;
-
-           case DW_FORM_data8:
-             uint.emplace (read_8_bytes (abfd, buf));
-             buf += 8;
-             break;
-
-           case DW_FORM_data16:
-             /*  This is used for MD5, but file_entry does not record MD5s. */
-             buf += 16;
-             break;
-
-           case DW_FORM_udata:
-             uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
-             buf += bytes_read;
-             break;
-
-           case DW_FORM_block:
-             /* It is valid only for DW_LNCT_timestamp which is ignored by
-                current GDB.  */
-             break;
-           }
-
-         switch (content_type)
-           {
-           case DW_LNCT_path:
-             if (string.has_value ())
-               fe.name = *string;
-             break;
-           case DW_LNCT_directory_index:
-             if (uint.has_value ())
-               fe.d_index = (dir_index) *uint;
-             break;
-           case DW_LNCT_timestamp:
-             if (uint.has_value ())
-               fe.mod_time = *uint;
-             break;
-           case DW_LNCT_size:
-             if (uint.has_value ())
-               fe.length = *uint;
-             break;
-           case DW_LNCT_MD5:
-             break;
-           default:
-             complaint (_("Unknown format content type %s"),
-                        pulongest (content_type));
-           }
-       }
-
-      callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
-    }
-
-  *bufp = buf;
-}
-
 /* Read the statement program header starting at OFFSET in
    .debug_line, or .debug_line.dwo.  Return a pointer
    to a struct line_header, allocated using xmalloc.
@@ -19741,12 +19655,7 @@ read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
 static line_header_up
 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
 {
-  const gdb_byte *line_ptr;
-  unsigned int bytes_read, offset_size;
-  int i;
-  const char *cur_dir, *cur_file;
   struct dwarf2_section_info *section;
-  bfd *abfd;
   struct dwarf2_per_objfile *dwarf2_per_objfile
     = cu->per_cu->dwarf2_per_objfile;
 
@@ -19761,159 +19670,9 @@ dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
       return 0;
     }
 
-  /* We can't do this until we know the section is non-empty.
-     Only then do we know we have such a section.  */
-  abfd = section->get_bfd_owner ();
-
-  /* Make sure that at least there's room for the total_length field.
-     That could be 12 bytes long, but we're just going to fudge that.  */
-  if (to_underlying (sect_off) + 4 >= section->size)
-    {
-      dwarf2_statement_list_fits_in_line_number_section_complaint ();
-      return 0;
-    }
-
-  line_header_up lh (new line_header ());
-
-  lh->sect_off = sect_off;
-  lh->offset_in_dwz = cu->per_cu->is_dwz;
-
-  line_ptr = section->buffer + to_underlying (sect_off);
-
-  /* Read in the header.  */
-  lh->total_length =
-    read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
-                                           &bytes_read, &offset_size);
-  line_ptr += bytes_read;
-
-  const gdb_byte *start_here = line_ptr;
-
-  if (line_ptr + lh->total_length > (section->buffer + section->size))
-    {
-      dwarf2_statement_list_fits_in_line_number_section_complaint ();
-      return 0;
-    }
-  lh->statement_program_end = start_here + lh->total_length;
-  lh->version = read_2_bytes (abfd, line_ptr);
-  line_ptr += 2;
-  if (lh->version > 5)
-    {
-      /* This is a version we don't understand.  The format could have
-        changed in ways we don't handle properly so just punt.  */
-      complaint (_("unsupported version in .debug_line section"));
-      return NULL;
-    }
-  if (lh->version >= 5)
-    {
-      gdb_byte segment_selector_size;
-
-      /* Skip address size.  */
-      read_1_byte (abfd, line_ptr);
-      line_ptr += 1;
-
-      segment_selector_size = read_1_byte (abfd, line_ptr);
-      line_ptr += 1;
-      if (segment_selector_size != 0)
-       {
-         complaint (_("unsupported segment selector size %u "
-                      "in .debug_line section"),
-                    segment_selector_size);
-         return NULL;
-       }
-    }
-  lh->header_length = read_offset (abfd, line_ptr, offset_size);
-  line_ptr += offset_size;
-  lh->statement_program_start = line_ptr + lh->header_length;
-  lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
-  line_ptr += 1;
-  if (lh->version >= 4)
-    {
-      lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
-      line_ptr += 1;
-    }
-  else
-    lh->maximum_ops_per_instruction = 1;
-
-  if (lh->maximum_ops_per_instruction == 0)
-    {
-      lh->maximum_ops_per_instruction = 1;
-      complaint (_("invalid maximum_ops_per_instruction "
-                  "in `.debug_line' section"));
-    }
-
-  lh->default_is_stmt = read_1_byte (abfd, line_ptr);
-  line_ptr += 1;
-  lh->line_base = read_1_signed_byte (abfd, line_ptr);
-  line_ptr += 1;
-  lh->line_range = read_1_byte (abfd, line_ptr);
-  line_ptr += 1;
-  lh->opcode_base = read_1_byte (abfd, line_ptr);
-  line_ptr += 1;
-  lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
-
-  lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
-  for (i = 1; i < lh->opcode_base; ++i)
-    {
-      lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
-      line_ptr += 1;
-    }
-
-  if (lh->version >= 5)
-    {
-      /* Read directory table.  */
-      read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
-                             &cu->header,
-                             [] (struct line_header *header, const char *name,
-                                 dir_index d_index, unsigned int mod_time,
-                                 unsigned int length)
-       {
-         header->add_include_dir (name);
-       });
-
-      /* Read file name table.  */
-      read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
-                             &cu->header,
-                             [] (struct line_header *header, const char *name,
-                                 dir_index d_index, unsigned int mod_time,
-                                 unsigned int length)
-       {
-         header->add_file_name (name, d_index, mod_time, length);
-       });
-    }
-  else
-    {
-      /* Read directory table.  */
-      while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
-       {
-         line_ptr += bytes_read;
-         lh->add_include_dir (cur_dir);
-       }
-      line_ptr += bytes_read;
-
-      /* Read file name table.  */
-      while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
-       {
-         unsigned int mod_time, length;
-         dir_index d_index;
-
-         line_ptr += bytes_read;
-         d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
-         line_ptr += bytes_read;
-         mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
-         line_ptr += bytes_read;
-         length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
-         line_ptr += bytes_read;
-
-         lh->add_file_name (cur_file, d_index, mod_time, length);
-       }
-      line_ptr += bytes_read;
-    }
-
-  if (line_ptr > (section->buffer + section->size))
-    complaint (_("line number info header doesn't "
-                "fit in `.debug_line' section"));
-
-  return lh;
+  return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
+                                  dwarf2_per_objfile, section,
+                                  &cu->header);
 }
 
 /* Subroutine of dwarf_decode_lines to simplify it.
@@ -20145,16 +19904,16 @@ void
 lnp_state_machine::handle_special_opcode (unsigned char op_code)
 {
   unsigned char adj_opcode = op_code - m_line_header->opcode_base;
-  CORE_ADDR addr_adj = (((m_op_index
-                         + (adj_opcode / m_line_header->line_range))
+  unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
+  unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
+  CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
                         / m_line_header->maximum_ops_per_instruction)
                        * m_line_header->minimum_instruction_length);
   m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
-  m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
+  m_op_index = ((m_op_index + adj_opcode_d)
                % m_line_header->maximum_ops_per_instruction);
 
-  int line_delta = (m_line_header->line_base
-                   + (adj_opcode % m_line_header->line_range));
+  int line_delta = m_line_header->line_base + adj_opcode_r;
   advance_line (line_delta);
   record_line (false);
   m_discriminator = 0;
@@ -20247,7 +20006,7 @@ dwarf_record_line_p (struct dwarf2_cu *cu,
 
 static void
 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
-                    unsigned int line, CORE_ADDR address,
+                    unsigned int line, CORE_ADDR address, bool is_stmt,
                     struct dwarf2_cu *cu)
 {
   CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
@@ -20261,7 +20020,7 @@ dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
     }
 
   if (cu != nullptr)
-    cu->get_builder ()->record_line (subfile, line, addr);
+    cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
 }
 
 /* Subroutine of dwarf_decode_lines_1 to simplify it.
@@ -20284,7 +20043,7 @@ dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
                          paddress (gdbarch, address));
     }
 
-  dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
+  dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
 }
 
 void
@@ -20311,8 +20070,7 @@ lnp_state_machine::record_line (bool end_sequence)
   else if (m_op_index == 0 || end_sequence)
     {
       fe->included_p = 1;
-      if (m_record_lines_p
-         && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
+      if (m_record_lines_p)
        {
          if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
              || end_sequence)
@@ -20323,6 +20081,8 @@ lnp_state_machine::record_line (bool end_sequence)
 
          if (!end_sequence)
            {
+             bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
+
              if (dwarf_record_line_p (m_cu, m_line, m_last_line,
                                       m_line_has_non_zero_discriminator,
                                       m_last_subfile))
@@ -20330,7 +20090,7 @@ lnp_state_machine::record_line (bool end_sequence)
                  buildsym_compunit *builder = m_cu->get_builder ();
                  dwarf_record_line_1 (m_gdbarch,
                                       builder->get_current_subfile (),
-                                      m_line, m_address,
+                                      m_line, m_address, is_stmt,
                                       m_currently_recording_lines ? m_cu : nullptr);
                }
              m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
@@ -20401,7 +20161,7 @@ dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
   CORE_ADDR baseaddr;
   struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   bfd *abfd = objfile->obfd;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   /* True if we're recording line info (as opposed to building partial
      symtabs and just interested in finding include files mentioned by
      the line number program).  */
@@ -20459,7 +20219,7 @@ dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
                case DW_LNE_set_address:
                  {
                    CORE_ADDR address
-                     = read_address (abfd, line_ptr, cu, &bytes_read);
+                     = cu->header.read_address (abfd, line_ptr, &bytes_read);
                    line_ptr += bytes_read;
 
                    state_machine.check_line_address (cu, line_ptr,
@@ -20780,10 +20540,10 @@ var_decode_location (struct attribute *attr, struct symbol *sym,
       unsigned int dummy;
 
       if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
-       SET_SYMBOL_VALUE_ADDRESS (sym,
-                                 read_address (objfile->obfd,
-                                               DW_BLOCK (attr)->data + 1,
-                                               cu, &dummy));
+       SET_SYMBOL_VALUE_ADDRESS
+         (sym, cu->header.read_address (objfile->obfd,
+                                        DW_BLOCK (attr)->data + 1,
+                                        &dummy));
       else
        SET_SYMBOL_VALUE_ADDRESS
          (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
@@ -20825,7 +20585,7 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
   struct dwarf2_per_objfile *dwarf2_per_objfile
     = cu->per_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   struct symbol *sym = NULL;
   const char *name;
   struct attribute *attr = NULL;
@@ -20840,27 +20600,31 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
   name = dwarf2_name (die, cu);
   if (name)
     {
-      const char *linkagename;
       int suppress_add = 0;
 
       if (space)
        sym = space;
       else
-       sym = allocate_symbol (objfile);
+       sym = new (&objfile->objfile_obstack) symbol;
       OBJSTAT (objfile, n_syms++);
 
       /* Cache this symbol's name and the name's demangled form (if any).  */
       sym->set_language (cu->language, &objfile->objfile_obstack);
-      linkagename = dwarf2_physname (name, die, cu);
-      sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
-
       /* Fortran does not have mangling standard and the mangling does differ
         between gfortran, iFort etc.  */
-      if (cu->language == language_fortran
-          && symbol_get_demangled_name (sym) == NULL)
-       symbol_set_demangled_name (sym,
-                                  dwarf2_full_name (name, die, cu),
-                                  NULL);
+      const char *physname
+       = (cu->language == language_fortran
+          ? dwarf2_full_name (name, die, cu)
+          : dwarf2_physname (name, die, cu));
+      const char *linkagename = dw2_linkage_name (die, cu);
+
+      if (linkagename == nullptr || cu->language == language_ada)
+       sym->set_linkage_name (physname);
+      else
+       {
+         sym->set_demangled_name (physname, &objfile->objfile_obstack);
+         sym->set_linkage_name (linkagename);
+       }
 
       /* Default assumptions.
          Use the passed type or decode it from the die.  */
@@ -20952,7 +20716,7 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
          /* Compilation with minimal debug info may result in
             variables with missing type entries.  Change the
             misleading `void' type to something sensible.  */
-         if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
+         if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
            SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
 
          attr = dwarf2_attr (die, DW_AT_const_value, cu);
@@ -21141,7 +20905,7 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
                       with this objfile, so we don't need to
                       duplicate it for the type.  */
                    if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
-                     TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
+                     SYMBOL_TYPE (sym)->set_name (sym->search_name ());
                  }
              }
          }
@@ -21517,7 +21281,7 @@ lookup_die_type (struct die_info *die, const struct attribute *attr,
   if (attr->form == DW_FORM_GNU_ref_alt)
     {
       struct dwarf2_per_cu_data *per_cu;
-      sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
+      sect_offset sect_off = attr->get_ref_die_offset ();
 
       per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
                                                 dwarf2_per_objfile);
@@ -21525,7 +21289,7 @@ lookup_die_type (struct die_info *die, const struct attribute *attr,
     }
   else if (attr->form_is_ref ())
     {
-      sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
+      sect_offset sect_off = attr->get_ref_die_offset ();
 
       this_type = get_die_type_at_offset (sect_off, cu->per_cu);
     }
@@ -22013,29 +21777,19 @@ typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
     }
 }
 
-/* Return sibling of die, NULL if no sibling.  */
-
-static struct die_info *
-sibling_die (struct die_info *die)
-{
-  return die->sibling;
-}
-
 /* Get name of a die, return NULL if not found.  */
 
 static const char *
 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
-                         struct obstack *obstack)
+                         struct objfile *objfile)
 {
   if (name && cu->language == language_cplus)
     {
-      std::string canon_name = cp_canonicalize_string (name);
+      gdb::unique_xmalloc_ptr<char> canon_name
+       = cp_canonicalize_string (name);
 
-      if (!canon_name.empty ())
-       {
-         if (canon_name != name)
-           name = obstack_strdup (obstack, canon_name);
-       }
+      if (canon_name != nullptr)
+       name = objfile->intern (canon_name.get ());
     }
 
   return name;
@@ -22103,23 +21857,20 @@ dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
            {
              gdb::unique_xmalloc_ptr<char> demangled
                (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
+             if (demangled == nullptr)
+               return nullptr;
 
-             const char *base;
-
-             /* FIXME: we already did this for the partial symbol... */
-             DW_STRING (attr)
-               = obstack_strdup (&objfile->per_bfd->storage_obstack,
-                                 demangled.get ());
+             DW_STRING (attr) = objfile->intern (demangled.get ());
              DW_STRING_IS_CANONICAL (attr) = 1;
-
-             /* Strip any leading namespaces/classes, keep only the base name.
-                DW_AT_name for named DIEs does not contain the prefixes.  */
-             base = strrchr (DW_STRING (attr), ':');
-             if (base && base > DW_STRING (attr) && base[-1] == ':')
-               return &base[1];
-             else
-               return DW_STRING (attr);
            }
+
+         /* Strip any leading namespaces/classes, keep only the base name.
+            DW_AT_name for named DIEs does not contain the prefixes.  */
+         const char *base = strrchr (DW_STRING (attr), ':');
+         if (base && base > DW_STRING (attr) && base[-1] == ':')
+           return &base[1];
+         else
+           return DW_STRING (attr);
        }
       break;
 
@@ -22129,9 +21880,8 @@ dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
 
   if (!DW_STRING_IS_CANONICAL (attr))
     {
-      DW_STRING (attr)
-       = dwarf2_canonicalize_name (DW_STRING (attr), cu,
-                                   &objfile->per_bfd->storage_obstack);
+      DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
+                                                  objfile);
       DW_STRING_IS_CANONICAL (attr) = 1;
     }
   return DW_STRING (attr);
@@ -22153,116 +21903,6 @@ dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
   return follow_die_ref (die, attr, ext_cu);
 }
 
-/* A convenience function that returns an "unknown" DWARF name,
-   including the value of V.  STR is the name of the entity being
-   printed, e.g., "TAG".  */
-
-static const char *
-dwarf_unknown (const char *str, unsigned v)
-{
-  char *cell = get_print_cell ();
-  xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
-  return cell;
-}
-
-/* Convert a DIE tag into its string name.  */
-
-static const char *
-dwarf_tag_name (unsigned tag)
-{
-  const char *name = get_DW_TAG_name (tag);
-
-  if (name == NULL)
-    return dwarf_unknown ("TAG", tag);
-
-  return name;
-}
-
-/* Convert a DWARF attribute code into its string name.  */
-
-static const char *
-dwarf_attr_name (unsigned attr)
-{
-  const char *name;
-
-#ifdef MIPS /* collides with DW_AT_HP_block_index */
-  if (attr == DW_AT_MIPS_fde)
-    return "DW_AT_MIPS_fde";
-#else
-  if (attr == DW_AT_HP_block_index)
-    return "DW_AT_HP_block_index";
-#endif
-
-  name = get_DW_AT_name (attr);
-
-  if (name == NULL)
-    return dwarf_unknown ("AT", attr);
-
-  return name;
-}
-
-/* Convert a unit type to corresponding DW_UT name.  */
-
-static const char *
-dwarf_unit_type_name (int unit_type) {
-  switch (unit_type)
-    {
-      case 0x01:
-       return "DW_UT_compile (0x01)";
-      case 0x02:
-       return "DW_UT_type (0x02)";
-      case 0x03:
-       return "DW_UT_partial (0x03)";
-      case 0x04:
-       return "DW_UT_skeleton (0x04)";
-      case 0x05:
-       return "DW_UT_split_compile (0x05)";
-      case 0x06:
-       return "DW_UT_split_type (0x06)";
-      case 0x80:
-       return "DW_UT_lo_user (0x80)";
-      case 0xff:
-       return "DW_UT_hi_user (0xff)";
-      default:
-       return nullptr;
-    }
-}
-
-/* Convert a DWARF value form code into its string name.  */
-
-static const char *
-dwarf_form_name (unsigned form)
-{
-  const char *name = get_DW_FORM_name (form);
-
-  if (name == NULL)
-    return dwarf_unknown ("FORM", form);
-
-  return name;
-}
-
-static const char *
-dwarf_bool_name (unsigned mybool)
-{
-  if (mybool)
-    return "TRUE";
-  else
-    return "FALSE";
-}
-
-/* Convert a DWARF type code into its string name.  */
-
-static const char *
-dwarf_type_encoding_name (unsigned enc)
-{
-  const char *name = get_DW_ATE_name (enc);
-
-  if (name == NULL)
-    return dwarf_unknown ("ATE", enc);
-
-  return name;
-}
-
 static void
 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
 {
@@ -22449,52 +22089,15 @@ store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
   *slot = die;
 }
 
-/* Return DIE offset of ATTR.  Return 0 with complaint if ATTR is not of the
-   required kind.  */
+/* Follow reference or signature attribute ATTR of SRC_DIE.
+   On entry *REF_CU is the CU of SRC_DIE.
+   On exit *REF_CU is the CU of the result.  */
 
-static sect_offset
-dwarf2_get_ref_die_offset (const struct attribute *attr)
+static struct die_info *
+follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
+                      struct dwarf2_cu **ref_cu)
 {
-  if (attr->form_is_ref ())
-    return (sect_offset) DW_UNSND (attr);
-
-  complaint (_("unsupported die ref attribute form: '%s'"),
-            dwarf_form_name (attr->form));
-  return {};
-}
-
-/* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
- * the value held by the attribute is not constant.  */
-
-static LONGEST
-dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
-{
-  if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
-    return DW_SND (attr);
-  else if (attr->form == DW_FORM_udata
-           || attr->form == DW_FORM_data1
-           || attr->form == DW_FORM_data2
-           || attr->form == DW_FORM_data4
-           || attr->form == DW_FORM_data8)
-    return DW_UNSND (attr);
-  else
-    {
-      /* For DW_FORM_data16 see attribute::form_is_constant.  */
-      complaint (_("Attribute value is not a constant (%s)"),
-                 dwarf_form_name (attr->form));
-      return default_value;
-    }
-}
-
-/* Follow reference or signature attribute ATTR of SRC_DIE.
-   On entry *REF_CU is the CU of SRC_DIE.
-   On exit *REF_CU is the CU of the result.  */
-
-static struct die_info *
-follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
-                      struct dwarf2_cu **ref_cu)
-{
-  struct die_info *die;
+  struct die_info *die;
 
   if (attr->form_is_ref ())
     die = follow_die_ref (src_die, attr, ref_cu);
@@ -22533,11 +22136,11 @@ follow_die_offset (sect_offset sect_off, int offset_in_dwz,
       /* .debug_types CUs cannot reference anything outside their CU.
         If they need to, they have to reference a signatured type via
         DW_FORM_ref_sig8.  */
-      if (!offset_in_cu_p (&cu->header, sect_off))
+      if (!cu->header.offset_in_cu_p (sect_off))
        return NULL;
     }
   else if (offset_in_dwz != cu->per_cu->is_dwz
-          || !offset_in_cu_p (&cu->header, sect_off))
+          || !cu->header.offset_in_cu_p (sect_off))
     {
       struct dwarf2_per_cu_data *per_cu;
 
@@ -22576,7 +22179,7 @@ static struct die_info *
 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
                struct dwarf2_cu **ref_cu)
 {
-  sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
+  sect_offset sect_off = attr->get_ref_die_offset ();
   struct dwarf2_cu *cu = *ref_cu;
   struct die_info *die;
 
@@ -22593,14 +22196,11 @@ follow_die_ref (struct die_info *src_die, const struct attribute *attr,
   return die;
 }
 
-/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
-   Returned value is intended for DW_OP_call*.  Returned
-   dwarf2_locexpr_baton->data has lifetime of
-   PER_CU->DWARF2_PER_OBJFILE->OBJFILE.  */
+/* See read.h.  */
 
 struct dwarf2_locexpr_baton
 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
-                              struct dwarf2_per_cu_data *per_cu,
+                              dwarf2_per_cu_data *per_cu,
                               CORE_ADDR (*get_frame_pc) (void *baton),
                               void *baton, bool resolve_abstract_p)
 {
@@ -22634,7 +22234,7 @@ dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
     {
       CORE_ADDR pc = (*get_frame_pc) (baton);
       CORE_ADDR baseaddr = objfile->text_section_offset ();
-      struct gdbarch *gdbarch = get_objfile_arch (objfile);
+      struct gdbarch *gdbarch = objfile->arch ();
 
       for (const auto &cand_off
             : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
@@ -22699,12 +22299,11 @@ dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
   return retval;
 }
 
-/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
-   offset.  */
+/* See read.h.  */
 
 struct dwarf2_locexpr_baton
 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
-                            struct dwarf2_per_cu_data *per_cu,
+                            dwarf2_per_cu_data *per_cu,
                             CORE_ADDR (*get_frame_pc) (void *baton),
                             void *baton)
 {
@@ -22732,15 +22331,12 @@ write_constant_as_bytes (struct obstack *obstack,
   return result;
 }
 
-/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
-   pointer to the constant bytes and set LEN to the length of the
-   data.  If memory is needed, allocate it on OBSTACK.  If the DIE
-   does not have a DW_AT_const_value, return NULL.  */
+/* See read.h.  */
 
 const gdb_byte *
 dwarf2_fetch_constant_bytes (sect_offset sect_off,
-                            struct dwarf2_per_cu_data *per_cu,
-                            struct obstack *obstack,
+                            dwarf2_per_cu_data *per_cu,
+                            obstack *obstack,
                             LONGEST *len)
 {
   struct dwarf2_cu *cu;
@@ -22865,12 +22461,11 @@ dwarf2_fetch_constant_bytes (sect_offset sect_off,
   return result;
 }
 
-/* Return the type of the die at OFFSET in PER_CU.  Return NULL if no
-   valid type for this die is found.  */
+/* See read.h.  */
 
 struct type *
 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
-                               struct dwarf2_per_cu_data *per_cu)
+                               dwarf2_per_cu_data *per_cu)
 {
   struct dwarf2_cu *cu;
   struct die_info *die;
@@ -22888,8 +22483,7 @@ dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
   return die_type (die, cu);
 }
 
-/* Return the type of the DIE at DIE_OFFSET in the CU named by
-   PER_CU.  */
+/* See read.h.  */
 
 struct type *
 dwarf2_get_die_type (cu_offset die_offset,
@@ -23090,7 +22684,7 @@ load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
   struct signatured_type *sig_type;
 
   /* Caller is responsible for ensuring type_unit_groups don't get here.  */
-  gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
+  gdb_assert (! per_cu->type_unit_group_p ());
 
   /* We have the per_cu, but we need the signatured_type.
      Fortunately this is an easy translation.  */
@@ -23116,7 +22710,7 @@ read_signatured_type (struct signatured_type *sig_type)
   gdb_assert (per_cu->is_debug_types);
   gdb_assert (per_cu->cu == NULL);
 
-  cutu_reader reader (per_cu, NULL, 0, 1, false);
+  cutu_reader reader (per_cu, NULL, 0, false);
 
   if (!reader.dummy_p)
     {
@@ -23147,6 +22741,8 @@ read_signatured_type (struct signatured_type *sig_type)
         correctly.  Similarly, if we do not read the producer, we can
         not apply producer-specific interpretation.  */
       prepare_one_comp_unit (cu, cu->dies, language_minimal);
+
+      reader.keep ();
     }
 
   sig_type->per_cu.tu_read = 1;
@@ -23154,27 +22750,13 @@ read_signatured_type (struct signatured_type *sig_type)
 
 /* Decode simple location descriptions.
    Given a pointer to a dwarf block that defines a location, compute
-   the location and return the value.
-
-   NOTE drow/2003-11-18: This function is called in two situations
-   now: for the address of static or global variables (partial symbols
-   only) and for offsets into structures which are expected to be
-   (more or less) constant.  The partial symbol case should go away,
-   and only the constant case should remain.  That will let this
-   function complain more accurately.  A few special modes are allowed
-   without complaint for global variables (for instance, global
-   register values and thread-local values).
-
-   A location description containing no operations indicates that the
-   object is optimized out.  The return value is 0 for that case.
-   FIXME drow/2003-11-16: No callers check for this case any more; soon all
-   callers will only want a very basic result and this can become a
-   complaint.
-
-   Note that stack[0] is unused except as a default error return.  */
+   the location and return the value.  If COMPUTED is non-null, it is
+   set to true to indicate that decoding was successful, and false
+   otherwise.  If COMPUTED is null, then this function may emit a
+   complaint.  */
 
 static CORE_ADDR
-decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
+decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
 {
   struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   size_t i;
@@ -23185,6 +22767,9 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
   unsigned int bytes_read, unsnd;
   gdb_byte op;
 
+  if (computed != nullptr)
+    *computed = false;
+
   i = 0;
   stacki = 0;
   stack[stacki] = 0;
@@ -23264,7 +22849,12 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
        case DW_OP_reg31:
          stack[++stacki] = op - DW_OP_reg0;
          if (i < size)
-           dwarf2_complex_location_expr_complaint ();
+           {
+             if (computed == nullptr)
+               dwarf2_complex_location_expr_complaint ();
+             else
+               return 0;
+           }
          break;
 
        case DW_OP_regx:
@@ -23272,12 +22862,17 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
          i += bytes_read;
          stack[++stacki] = unsnd;
          if (i < size)
-           dwarf2_complex_location_expr_complaint ();
+           {
+             if (computed == nullptr)
+               dwarf2_complex_location_expr_complaint ();
+             else
+               return 0;
+           }
          break;
 
        case DW_OP_addr:
-         stack[++stacki] = read_address (objfile->obfd, &data[i],
-                                         cu, &bytes_read);
+         stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
+                                                    &bytes_read);
          i += bytes_read;
          break;
 
@@ -23354,7 +22949,12 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
             global symbols, although the variable's address will be bogus
             in the psymtab.  */
          if (i < size)
-           dwarf2_complex_location_expr_complaint ();
+           {
+             if (computed == nullptr)
+               dwarf2_complex_location_expr_complaint ();
+             else
+               return 0;
+           }
          break;
 
         case DW_OP_GNU_push_tls_address:
@@ -23368,11 +22968,18 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
             non-zero to not look as a variable garbage collected by linker
             which have DW_OP_addr 0.  */
          if (i < size)
-           dwarf2_complex_location_expr_complaint ();
+           {
+             if (computed == nullptr)
+               dwarf2_complex_location_expr_complaint ();
+             else
+               return 0;
+           }
          stack[stacki]++;
           break;
 
        case DW_OP_GNU_uninit:
+         if (computed != nullptr)
+           return 0;
          break;
 
        case DW_OP_addrx:
@@ -23384,16 +22991,17 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
          break;
 
        default:
-         {
-           const char *name = get_DW_OP_name (op);
+         if (computed == nullptr)
+           {
+             const char *name = get_DW_OP_name (op);
 
-           if (name)
-             complaint (_("unsupported stack op: '%s'"),
-                        name);
-           else
-             complaint (_("unsupported stack op: '%02x'"),
-                        op);
-         }
+             if (name)
+               complaint (_("unsupported stack op: '%s'"),
+                          name);
+             else
+               complaint (_("unsupported stack op: '%02x'"),
+                          op);
+           }
 
          return (stack[stacki]);
        }
@@ -23402,16 +23010,21 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
          outside of the allocated space.  Also enforce minimum>0.  */
       if (stacki >= ARRAY_SIZE (stack) - 1)
        {
-         complaint (_("location description stack overflow"));
+         if (computed == nullptr)
+           complaint (_("location description stack overflow"));
          return 0;
        }
 
       if (stacki <= 0)
        {
-         complaint (_("location description stack underflow"));
+         if (computed == nullptr)
+           complaint (_("location description stack underflow"));
          return 0;
        }
     }
+
+  if (computed != nullptr)
+    *computed = true;
   return (stack[stacki]);
 }
 
@@ -23438,691 +23051,25 @@ dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
 }
 
 \f
-/* Macro support.  */
-
-static struct macro_source_file *
-macro_start_file (struct dwarf2_cu *cu,
-                 int file, int line,
-                  struct macro_source_file *current_file,
-                  struct line_header *lh)
-{
-  /* File name relative to the compilation directory of this source file.  */
-  gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
-
-  if (! current_file)
-    {
-      /* Note: We don't create a macro table for this compilation unit
-        at all until we actually get a filename.  */
-      struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
-
-      /* If we have no current file, then this must be the start_file
-        directive for the compilation unit's main source file.  */
-      current_file = macro_set_main (macro_table, file_name.get ());
-      macro_define_special (macro_table);
-    }
-  else
-    current_file = macro_include (current_file, line, file_name.get ());
-
-  return current_file;
-}
-
-static const char *
-consume_improper_spaces (const char *p, const char *body)
-{
-  if (*p == ' ')
-    {
-      complaint (_("macro definition contains spaces "
-                  "in formal argument list:\n`%s'"),
-                body);
-
-      while (*p == ' ')
-        p++;
-    }
-
-  return p;
-}
-
-
-static void
-parse_macro_definition (struct macro_source_file *file, int line,
-                        const char *body)
-{
-  const char *p;
-
-  /* The body string takes one of two forms.  For object-like macro
-     definitions, it should be:
-
-        <macro name> " " <definition>
-
-     For function-like macro definitions, it should be:
-
-        <macro name> "() " <definition>
-     or
-        <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
-
-     Spaces may appear only where explicitly indicated, and in the
-     <definition>.
-
-     The Dwarf 2 spec says that an object-like macro's name is always
-     followed by a space, but versions of GCC around March 2002 omit
-     the space when the macro's definition is the empty string.
-
-     The Dwarf 2 spec says that there should be no spaces between the
-     formal arguments in a function-like macro's formal argument list,
-     but versions of GCC around March 2002 include spaces after the
-     commas.  */
-
-
-  /* Find the extent of the macro name.  The macro name is terminated
-     by either a space or null character (for an object-like macro) or
-     an opening paren (for a function-like macro).  */
-  for (p = body; *p; p++)
-    if (*p == ' ' || *p == '(')
-      break;
-
-  if (*p == ' ' || *p == '\0')
-    {
-      /* It's an object-like macro.  */
-      int name_len = p - body;
-      std::string name (body, name_len);
-      const char *replacement;
-
-      if (*p == ' ')
-        replacement = body + name_len + 1;
-      else
-        {
-         dwarf2_macro_malformed_definition_complaint (body);
-          replacement = body + name_len;
-        }
-
-      macro_define_object (file, line, name.c_str (), replacement);
-    }
-  else if (*p == '(')
-    {
-      /* It's a function-like macro.  */
-      std::string name (body, p - body);
-      int argc = 0;
-      int argv_size = 1;
-      char **argv = XNEWVEC (char *, argv_size);
-
-      p++;
-
-      p = consume_improper_spaces (p, body);
-
-      /* Parse the formal argument list.  */
-      while (*p && *p != ')')
-        {
-          /* Find the extent of the current argument name.  */
-          const char *arg_start = p;
-
-          while (*p && *p != ',' && *p != ')' && *p != ' ')
-            p++;
-
-          if (! *p || p == arg_start)
-           dwarf2_macro_malformed_definition_complaint (body);
-          else
-            {
-              /* Make sure argv has room for the new argument.  */
-              if (argc >= argv_size)
-                {
-                  argv_size *= 2;
-                  argv = XRESIZEVEC (char *, argv, argv_size);
-                }
-
-              argv[argc++] = savestring (arg_start, p - arg_start);
-            }
-
-          p = consume_improper_spaces (p, body);
-
-          /* Consume the comma, if present.  */
-          if (*p == ',')
-            {
-              p++;
-
-              p = consume_improper_spaces (p, body);
-            }
-        }
-
-      if (*p == ')')
-        {
-          p++;
-
-          if (*p == ' ')
-            /* Perfectly formed definition, no complaints.  */
-            macro_define_function (file, line, name.c_str (),
-                                   argc, (const char **) argv,
-                                   p + 1);
-          else if (*p == '\0')
-            {
-              /* Complain, but do define it.  */
-             dwarf2_macro_malformed_definition_complaint (body);
-              macro_define_function (file, line, name.c_str (),
-                                     argc, (const char **) argv,
-                                     p);
-            }
-          else
-            /* Just complain.  */
-           dwarf2_macro_malformed_definition_complaint (body);
-        }
-      else
-        /* Just complain.  */
-       dwarf2_macro_malformed_definition_complaint (body);
-
-      {
-        int i;
-
-        for (i = 0; i < argc; i++)
-          xfree (argv[i]);
-      }
-      xfree (argv);
-    }
-  else
-    dwarf2_macro_malformed_definition_complaint (body);
-}
-
-/* Skip some bytes from BYTES according to the form given in FORM.
-   Returns the new pointer.  */
-
-static const gdb_byte *
-skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
-                enum dwarf_form form,
-                unsigned int offset_size,
-                struct dwarf2_section_info *section)
-{
-  unsigned int bytes_read;
-
-  switch (form)
-    {
-    case DW_FORM_data1:
-    case DW_FORM_flag:
-      ++bytes;
-      break;
-
-    case DW_FORM_data2:
-      bytes += 2;
-      break;
-
-    case DW_FORM_data4:
-      bytes += 4;
-      break;
-
-    case DW_FORM_data8:
-      bytes += 8;
-      break;
-
-    case DW_FORM_data16:
-      bytes += 16;
-      break;
-
-    case DW_FORM_string:
-      read_direct_string (abfd, bytes, &bytes_read);
-      bytes += bytes_read;
-      break;
-
-    case DW_FORM_sec_offset:
-    case DW_FORM_strp:
-    case DW_FORM_GNU_strp_alt:
-      bytes += offset_size;
-      break;
-
-    case DW_FORM_block:
-      bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
-      bytes += bytes_read;
-      break;
-
-    case DW_FORM_block1:
-      bytes += 1 + read_1_byte (abfd, bytes);
-      break;
-    case DW_FORM_block2:
-      bytes += 2 + read_2_bytes (abfd, bytes);
-      break;
-    case DW_FORM_block4:
-      bytes += 4 + read_4_bytes (abfd, bytes);
-      break;
-
-    case DW_FORM_addrx:
-    case DW_FORM_sdata:
-    case DW_FORM_strx:
-    case DW_FORM_udata:
-    case DW_FORM_GNU_addr_index:
-    case DW_FORM_GNU_str_index:
-      bytes = gdb_skip_leb128 (bytes, buffer_end);
-      if (bytes == NULL)
-       {
-         dwarf2_section_buffer_overflow_complaint (section);
-         return NULL;
-       }
-      break;
-
-    case DW_FORM_implicit_const:
-      break;
-
-    default:
-      {
-       complaint (_("invalid form 0x%x in `%s'"),
-                  form, section->get_name ());
-       return NULL;
-      }
-    }
-
-  return bytes;
-}
-
-/* A helper for dwarf_decode_macros that handles skipping an unknown
-   opcode.  Returns an updated pointer to the macro data buffer; or,
-   on error, issues a complaint and returns NULL.  */
-
-static const gdb_byte *
-skip_unknown_opcode (unsigned int opcode,
-                    const gdb_byte **opcode_definitions,
-                    const gdb_byte *mac_ptr, const gdb_byte *mac_end,
-                    bfd *abfd,
-                    unsigned int offset_size,
-                    struct dwarf2_section_info *section)
-{
-  unsigned int bytes_read, i;
-  unsigned long arg;
-  const gdb_byte *defn;
-
-  if (opcode_definitions[opcode] == NULL)
-    {
-      complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
-                opcode);
-      return NULL;
-    }
-
-  defn = opcode_definitions[opcode];
-  arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
-  defn += bytes_read;
-
-  for (i = 0; i < arg; ++i)
-    {
-      mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
-                                (enum dwarf_form) defn[i], offset_size,
-                                section);
-      if (mac_ptr == NULL)
-       {
-         /* skip_form_bytes already issued the complaint.  */
-         return NULL;
-       }
-    }
 
-  return mac_ptr;
-}
-
-/* A helper function which parses the header of a macro section.
-   If the macro section is the extended (for now called "GNU") type,
-   then this updates *OFFSET_SIZE.  Returns a pointer to just after
-   the header, or issues a complaint and returns NULL on error.  */
-
-static const gdb_byte *
-dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
-                         bfd *abfd,
-                         const gdb_byte *mac_ptr,
-                         unsigned int *offset_size,
-                         int section_is_gnu)
-{
-  memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
-
-  if (section_is_gnu)
-    {
-      unsigned int version, flags;
-
-      version = read_2_bytes (abfd, mac_ptr);
-      if (version != 4 && version != 5)
-       {
-         complaint (_("unrecognized version `%d' in .debug_macro section"),
-                    version);
-         return NULL;
-       }
-      mac_ptr += 2;
-
-      flags = read_1_byte (abfd, mac_ptr);
-      ++mac_ptr;
-      *offset_size = (flags & 1) ? 8 : 4;
-
-      if ((flags & 2) != 0)
-       /* We don't need the line table offset.  */
-       mac_ptr += *offset_size;
-
-      /* Vendor opcode descriptions.  */
-      if ((flags & 4) != 0)
-       {
-         unsigned int i, count;
-
-         count = read_1_byte (abfd, mac_ptr);
-         ++mac_ptr;
-         for (i = 0; i < count; ++i)
-           {
-             unsigned int opcode, bytes_read;
-             unsigned long arg;
-
-             opcode = read_1_byte (abfd, mac_ptr);
-             ++mac_ptr;
-             opcode_definitions[opcode] = mac_ptr;
-             arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
-             mac_ptr += bytes_read;
-             mac_ptr += arg;
-           }
-       }
-    }
-
-  return mac_ptr;
-}
-
-/* A helper for dwarf_decode_macros that handles the GNU extensions,
-   including DW_MACRO_import.  */
-
-static void
-dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
-                         bfd *abfd,
-                         const gdb_byte *mac_ptr, const gdb_byte *mac_end,
-                         struct macro_source_file *current_file,
-                         struct line_header *lh,
-                         struct dwarf2_section_info *section,
-                         int section_is_gnu, int section_is_dwz,
-                         unsigned int offset_size,
-                         htab_t include_hash)
-{
-  struct dwarf2_per_objfile *dwarf2_per_objfile
-    = cu->per_cu->dwarf2_per_objfile;
-  struct objfile *objfile = dwarf2_per_objfile->objfile;
-  enum dwarf_macro_record_type macinfo_type;
-  int at_commandline;
-  const gdb_byte *opcode_definitions[256];
-
-  mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
-                                     &offset_size, section_is_gnu);
-  if (mac_ptr == NULL)
-    {
-      /* We already issued a complaint.  */
-      return;
-    }
-
-  /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
-     GDB is still reading the definitions from command line.  First
-     DW_MACINFO_start_file will need to be ignored as it was already executed
-     to create CURRENT_FILE for the main source holding also the command line
-     definitions.  On first met DW_MACINFO_start_file this flag is reset to
-     normally execute all the remaining DW_MACINFO_start_file macinfos.  */
-
-  at_commandline = 1;
-
-  do
-    {
-      /* Do we at least have room for a macinfo type byte?  */
-      if (mac_ptr >= mac_end)
-       {
-         dwarf2_section_buffer_overflow_complaint (section);
-         break;
-       }
-
-      macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
-      mac_ptr++;
-
-      /* Note that we rely on the fact that the corresponding GNU and
-        DWARF constants are the same.  */
-      DIAGNOSTIC_PUSH
-      DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
-      switch (macinfo_type)
-       {
-         /* A zero macinfo type indicates the end of the macro
-            information.  */
-       case 0:
-         break;
-
-        case DW_MACRO_define:
-        case DW_MACRO_undef:
-       case DW_MACRO_define_strp:
-       case DW_MACRO_undef_strp:
-       case DW_MACRO_define_sup:
-       case DW_MACRO_undef_sup:
-          {
-            unsigned int bytes_read;
-            int line;
-            const char *body;
-           int is_define;
-
-           line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
-           mac_ptr += bytes_read;
-
-           if (macinfo_type == DW_MACRO_define
-               || macinfo_type == DW_MACRO_undef)
-             {
-               body = read_direct_string (abfd, mac_ptr, &bytes_read);
-               mac_ptr += bytes_read;
-             }
-           else
-             {
-               LONGEST str_offset;
-
-               str_offset = read_offset (abfd, mac_ptr, offset_size);
-               mac_ptr += offset_size;
-
-               if (macinfo_type == DW_MACRO_define_sup
-                   || macinfo_type == DW_MACRO_undef_sup
-                   || section_is_dwz)
-                 {
-                   struct dwz_file *dwz
-                     = dwarf2_get_dwz_file (dwarf2_per_objfile);
-
-                   body = read_indirect_string_from_dwz (objfile,
-                                                         dwz, str_offset);
-                 }
-               else
-                 body = read_indirect_string_at_offset (dwarf2_per_objfile,
-                                                        abfd, str_offset);
-             }
-
-           is_define = (macinfo_type == DW_MACRO_define
-                        || macinfo_type == DW_MACRO_define_strp
-                        || macinfo_type == DW_MACRO_define_sup);
-            if (! current_file)
-             {
-               /* DWARF violation as no main source is present.  */
-               complaint (_("debug info with no main source gives macro %s "
-                            "on line %d: %s"),
-                          is_define ? _("definition") : _("undefinition"),
-                          line, body);
-               break;
-             }
-           if ((line == 0 && !at_commandline)
-               || (line != 0 && at_commandline))
-             complaint (_("debug info gives %s macro %s with %s line %d: %s"),
-                        at_commandline ? _("command-line") : _("in-file"),
-                        is_define ? _("definition") : _("undefinition"),
-                        line == 0 ? _("zero") : _("non-zero"), line, body);
-
-           if (body == NULL)
-             {
-               /* Fedora's rpm-build's "debugedit" binary
-                  corrupted .debug_macro sections.
-
-                  For more info, see
-                  https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
-               complaint (_("debug info gives %s invalid macro %s "
-                            "without body (corrupted?) at line %d "
-                            "on file %s"),
-                          at_commandline ? _("command-line") : _("in-file"),
-                          is_define ? _("definition") : _("undefinition"),
-                          line, current_file->filename);
-             }
-           else if (is_define)
-             parse_macro_definition (current_file, line, body);
-           else
-             {
-               gdb_assert (macinfo_type == DW_MACRO_undef
-                           || macinfo_type == DW_MACRO_undef_strp
-                           || macinfo_type == DW_MACRO_undef_sup);
-               macro_undef (current_file, line, body);
-             }
-          }
-          break;
-
-        case DW_MACRO_start_file:
-          {
-            unsigned int bytes_read;
-            int line, file;
-
-            line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
-            mac_ptr += bytes_read;
-            file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
-            mac_ptr += bytes_read;
-
-           if ((line == 0 && !at_commandline)
-               || (line != 0 && at_commandline))
-             complaint (_("debug info gives source %d included "
-                          "from %s at %s line %d"),
-                        file, at_commandline ? _("command-line") : _("file"),
-                        line == 0 ? _("zero") : _("non-zero"), line);
-
-           if (at_commandline)
-             {
-               /* This DW_MACRO_start_file was executed in the
-                  pass one.  */
-               at_commandline = 0;
-             }
-           else
-             current_file = macro_start_file (cu, file, line, current_file,
-                                              lh);
-          }
-          break;
-
-        case DW_MACRO_end_file:
-          if (! current_file)
-           complaint (_("macro debug info has an unmatched "
-                        "`close_file' directive"));
-          else
-            {
-              current_file = current_file->included_by;
-              if (! current_file)
-                {
-                  enum dwarf_macro_record_type next_type;
-
-                  /* GCC circa March 2002 doesn't produce the zero
-                     type byte marking the end of the compilation
-                     unit.  Complain if it's not there, but exit no
-                     matter what.  */
-
-                  /* Do we at least have room for a macinfo type byte?  */
-                  if (mac_ptr >= mac_end)
-                    {
-                     dwarf2_section_buffer_overflow_complaint (section);
-                      return;
-                    }
-
-                  /* We don't increment mac_ptr here, so this is just
-                     a look-ahead.  */
-                  next_type
-                   = (enum dwarf_macro_record_type) read_1_byte (abfd,
-                                                                 mac_ptr);
-                  if (next_type != 0)
-                   complaint (_("no terminating 0-type entry for "
-                                "macros in `.debug_macinfo' section"));
-
-                  return;
-                }
-            }
-          break;
-
-       case DW_MACRO_import:
-       case DW_MACRO_import_sup:
-         {
-           LONGEST offset;
-           void **slot;
-           bfd *include_bfd = abfd;
-           struct dwarf2_section_info *include_section = section;
-           const gdb_byte *include_mac_end = mac_end;
-           int is_dwz = section_is_dwz;
-           const gdb_byte *new_mac_ptr;
-
-           offset = read_offset (abfd, mac_ptr, offset_size);
-           mac_ptr += offset_size;
-
-           if (macinfo_type == DW_MACRO_import_sup)
-             {
-               struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
-
-               dwz->macro.read (objfile);
-
-               include_section = &dwz->macro;
-               include_bfd = include_section->get_bfd_owner ();
-               include_mac_end = dwz->macro.buffer + dwz->macro.size;
-               is_dwz = 1;
-             }
-
-           new_mac_ptr = include_section->buffer + offset;
-           slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
-
-           if (*slot != NULL)
-             {
-               /* This has actually happened; see
-                  http://sourceware.org/bugzilla/show_bug.cgi?id=13568.  */
-               complaint (_("recursive DW_MACRO_import in "
-                            ".debug_macro section"));
-             }
-           else
-             {
-               *slot = (void *) new_mac_ptr;
-
-               dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
-                                         include_mac_end, current_file, lh,
-                                         section, section_is_gnu, is_dwz,
-                                         offset_size, include_hash);
-
-               htab_remove_elt (include_hash, (void *) new_mac_ptr);
-             }
-         }
-         break;
-
-        case DW_MACINFO_vendor_ext:
-         if (!section_is_gnu)
-           {
-             unsigned int bytes_read;
-
-             /* This reads the constant, but since we don't recognize
-                any vendor extensions, we ignore it.  */
-             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
-             mac_ptr += bytes_read;
-             read_direct_string (abfd, mac_ptr, &bytes_read);
-             mac_ptr += bytes_read;
-
-             /* We don't recognize any vendor extensions.  */
-             break;
-           }
-         /* FALLTHROUGH */
+/* Macro support.  */
 
-       default:
-         mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
-                                        mac_ptr, mac_end, abfd, offset_size,
-                                        section);
-         if (mac_ptr == NULL)
-           return;
-         break;
-        }
-      DIAGNOSTIC_POP
-    } while (macinfo_type != 0);
-}
+/* An overload of dwarf_decode_macros that finds the correct section
+   and ensures it is read in before calling the other overload.  */
 
 static void
 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
-                     int section_is_gnu)
+                    int section_is_gnu)
 {
   struct dwarf2_per_objfile *dwarf2_per_objfile
     = cu->per_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
-  struct line_header *lh = cu->line_header;
-  bfd *abfd;
-  const gdb_byte *mac_ptr, *mac_end;
-  struct macro_source_file *current_file = 0;
-  enum dwarf_macro_record_type macinfo_type;
+  const struct line_header *lh = cu->line_header;
   unsigned int offset_size = cu->header.offset_size;
-  const gdb_byte *opcode_definitions[256];
-  void **slot;
   struct dwarf2_section_info *section;
   const char *section_name;
 
-  if (cu->dwo_unit != NULL)
+  if (cu->dwo_unit != nullptr)
     {
       if (section_is_gnu)
        {
@@ -24150,152 +23097,16 @@ dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
     }
 
   section->read (objfile);
-  if (section->buffer == NULL)
+  if (section->buffer == nullptr)
     {
       complaint (_("missing %s section"), section_name);
       return;
     }
-  abfd = section->get_bfd_owner ();
-
-  /* First pass: Find the name of the base filename.
-     This filename is needed in order to process all macros whose definition
-     (or undefinition) comes from the command line.  These macros are defined
-     before the first DW_MACINFO_start_file entry, and yet still need to be
-     associated to the base file.
-
-     To determine the base file name, we scan the macro definitions until we
-     reach the first DW_MACINFO_start_file entry.  We then initialize
-     CURRENT_FILE accordingly so that any macro definition found before the
-     first DW_MACINFO_start_file can still be associated to the base file.  */
-
-  mac_ptr = section->buffer + offset;
-  mac_end = section->buffer + section->size;
-
-  mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
-                                     &offset_size, section_is_gnu);
-  if (mac_ptr == NULL)
-    {
-      /* We already issued a complaint.  */
-      return;
-    }
-
-  do
-    {
-      /* Do we at least have room for a macinfo type byte?  */
-      if (mac_ptr >= mac_end)
-        {
-         /* Complaint is printed during the second pass as GDB will probably
-            stop the first pass earlier upon finding
-            DW_MACINFO_start_file.  */
-         break;
-        }
-
-      macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
-      mac_ptr++;
-
-      /* Note that we rely on the fact that the corresponding GNU and
-        DWARF constants are the same.  */
-      DIAGNOSTIC_PUSH
-      DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
-      switch (macinfo_type)
-        {
-          /* A zero macinfo type indicates the end of the macro
-             information.  */
-        case 0:
-         break;
-
-       case DW_MACRO_define:
-       case DW_MACRO_undef:
-         /* Only skip the data by MAC_PTR.  */
-         {
-           unsigned int bytes_read;
-
-           read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
-           mac_ptr += bytes_read;
-           read_direct_string (abfd, mac_ptr, &bytes_read);
-           mac_ptr += bytes_read;
-         }
-         break;
-
-       case DW_MACRO_start_file:
-         {
-           unsigned int bytes_read;
-           int line, file;
-
-           line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
-           mac_ptr += bytes_read;
-           file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
-           mac_ptr += bytes_read;
-
-           current_file = macro_start_file (cu, file, line, current_file, lh);
-         }
-         break;
-
-       case DW_MACRO_end_file:
-         /* No data to skip by MAC_PTR.  */
-         break;
-
-       case DW_MACRO_define_strp:
-       case DW_MACRO_undef_strp:
-       case DW_MACRO_define_sup:
-       case DW_MACRO_undef_sup:
-         {
-           unsigned int bytes_read;
 
-           read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
-           mac_ptr += bytes_read;
-           mac_ptr += offset_size;
-         }
-         break;
-
-       case DW_MACRO_import:
-       case DW_MACRO_import_sup:
-         /* Note that, according to the spec, a transparent include
-            chain cannot call DW_MACRO_start_file.  So, we can just
-            skip this opcode.  */
-         mac_ptr += offset_size;
-         break;
+  buildsym_compunit *builder = cu->get_builder ();
 
-       case DW_MACINFO_vendor_ext:
-         /* Only skip the data by MAC_PTR.  */
-         if (!section_is_gnu)
-           {
-             unsigned int bytes_read;
-
-             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
-             mac_ptr += bytes_read;
-             read_direct_string (abfd, mac_ptr, &bytes_read);
-             mac_ptr += bytes_read;
-           }
-         /* FALLTHROUGH */
-
-       default:
-         mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
-                                        mac_ptr, mac_end, abfd, offset_size,
-                                        section);
-         if (mac_ptr == NULL)
-           return;
-         break;
-       }
-      DIAGNOSTIC_POP
-    } while (macinfo_type != 0 && current_file == NULL);
-
-  /* Second pass: Process all entries.
-
-     Use the AT_COMMAND_LINE flag to determine whether we are still processing
-     command-line macro definitions/undefinitions.  This flag is unset when we
-     reach the first DW_MACINFO_start_file entry.  */
-
-  htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
-                                          htab_eq_pointer,
-                                          NULL, xcalloc, xfree));
-  mac_ptr = section->buffer + offset;
-  slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
-  *slot = (void *) mac_ptr;
-  dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
-                           current_file, lh, section,
-                           section_is_gnu, 0, offset_size,
-                           include_hash.get ());
+  dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
+                      offset_size, offset, section_is_gnu);
 }
 
 /* Return the .debug_loc section to use for CU.
@@ -24336,7 +23147,10 @@ fill_in_loclist_baton (struct dwarf2_cu *cu,
      don't run off the edge of the section.  */
   baton->size = section->size - DW_UNSND (attr);
   baton->data = section->buffer + DW_UNSND (attr);
-  baton->base_address = cu->base_address;
+  if (cu->base_address.has_value ())
+    baton->base_address = *cu->base_address;
+  else
+    baton->base_address = 0;
   baton->from_dwo = cu->dwo_unit != NULL;
 }
 
@@ -24361,7 +23175,7 @@ dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
 
       fill_in_loclist_baton (cu, baton, attr);
 
-      if (cu->base_known == 0)
+      if (!cu->base_address.has_value ())
        complaint (_("Location list used without "
                     "specifying the CU base address."));
 
@@ -24508,34 +23322,53 @@ dwarf2_per_cu_data::addr_type () const
   return addr_type;
 }
 
-/* Locate the .debug_info compilation unit from CU's objfile which contains
-   the DIE at OFFSET.  Raises an error on failure.  */
+/* A helper function for dwarf2_find_containing_comp_unit that returns
+   the index of the result, and that searches a vector.  It will
+   return a result even if the offset in question does not actually
+   occur in any CU.  This is separate so that it can be unit
+   tested.  */
 
-static struct dwarf2_per_cu_data *
-dwarf2_find_containing_comp_unit (sect_offset sect_off,
-                                 unsigned int offset_in_dwz,
-                                 struct dwarf2_per_objfile *dwarf2_per_objfile)
+static int
+dwarf2_find_containing_comp_unit
+  (sect_offset sect_off,
+   unsigned int offset_in_dwz,
+   const std::vector<dwarf2_per_cu_data *> &all_comp_units)
 {
-  struct dwarf2_per_cu_data *this_cu;
   int low, high;
 
   low = 0;
-  high = dwarf2_per_objfile->all_comp_units.size () - 1;
+  high = all_comp_units.size () - 1;
   while (high > low)
     {
       struct dwarf2_per_cu_data *mid_cu;
       int mid = low + (high - low) / 2;
 
-      mid_cu = dwarf2_per_objfile->all_comp_units[mid];
+      mid_cu = all_comp_units[mid];
       if (mid_cu->is_dwz > offset_in_dwz
          || (mid_cu->is_dwz == offset_in_dwz
-             && mid_cu->sect_off + mid_cu->length >= sect_off))
+             && mid_cu->sect_off + mid_cu->length > sect_off))
        high = mid;
       else
        low = mid + 1;
     }
   gdb_assert (low == high);
-  this_cu = dwarf2_per_objfile->all_comp_units[low];
+  return low;
+}
+
+/* Locate the .debug_info compilation unit from CU's objfile which contains
+   the DIE at OFFSET.  Raises an error on failure.  */
+
+static struct dwarf2_per_cu_data *
+dwarf2_find_containing_comp_unit (sect_offset sect_off,
+                                 unsigned int offset_in_dwz,
+                                 struct dwarf2_per_objfile *dwarf2_per_objfile)
+{
+  int low
+    = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
+                                       dwarf2_per_objfile->all_comp_units);
+  struct dwarf2_per_cu_data *this_cu
+    = dwarf2_per_objfile->all_comp_units[low];
+
   if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
     {
       if (low == 0 || this_cu->is_dwz != offset_in_dwz)
@@ -24558,6 +23391,57 @@ dwarf2_find_containing_comp_unit (sect_offset sect_off,
     }
 }
 
+#if GDB_SELF_TEST
+
+namespace selftests {
+namespace find_containing_comp_unit {
+
+static void
+run_test ()
+{
+  struct dwarf2_per_cu_data one {};
+  struct dwarf2_per_cu_data two {};
+  struct dwarf2_per_cu_data three {};
+  struct dwarf2_per_cu_data four {};
+
+  one.length = 5;
+  two.sect_off = sect_offset (one.length);
+  two.length = 7;
+
+  three.length = 5;
+  three.is_dwz = 1;
+  four.sect_off = sect_offset (three.length);
+  four.length = 7;
+  four.is_dwz = 1;
+
+  std::vector<dwarf2_per_cu_data *> units;
+  units.push_back (&one);
+  units.push_back (&two);
+  units.push_back (&three);
+  units.push_back (&four);
+
+  int result;
+
+  result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
+  SELF_CHECK (units[result] == &one);
+  result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
+  SELF_CHECK (units[result] == &one);
+  result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
+  SELF_CHECK (units[result] == &two);
+
+  result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
+  SELF_CHECK (units[result] == &three);
+  result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
+  SELF_CHECK (units[result] == &three);
+  result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
+  SELF_CHECK (units[result] == &four);
+}
+
+}
+}
+
+#endif /* GDB_SELF_TEST */
+
 /* Initialize dwarf2_cu CU, owned by PER_CU.  */
 
 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
@@ -24754,11 +23638,11 @@ set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
      But this is not a problem, because the gnat-specific information
      is actually not needed for these types.  */
   if (need_gnat_info (cu)
-      && TYPE_CODE (type) != TYPE_CODE_FUNC
-      && TYPE_CODE (type) != TYPE_CODE_FLT
-      && TYPE_CODE (type) != TYPE_CODE_METHODPTR
-      && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
-      && TYPE_CODE (type) != TYPE_CODE_METHOD
+      && type->code () != TYPE_CODE_FUNC
+      && type->code () != TYPE_CODE_FLT
+      && type->code () != TYPE_CODE_METHODPTR
+      && type->code () != TYPE_CODE_MEMBERPTR
+      && type->code () != TYPE_CODE_METHOD
       && !HAVE_GNAT_AUX_INFO (type))
     INIT_GNAT_SPECIFIC (type);
 
@@ -24768,7 +23652,7 @@ set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
     {
       struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
       if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
-        add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
+        type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
     }
   else if (attr != NULL)
     {
@@ -24783,7 +23667,7 @@ set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
     {
       struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
       if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
-        add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
+        type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
     }
   else if (attr != NULL)
     {
@@ -24796,7 +23680,7 @@ set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
   attr = dwarf2_attr (die, DW_AT_data_location, cu);
   if (attr_to_dynamic_prop (attr, die, cu, &prop,
                            cu->per_cu->addr_type ()))
-    add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
+    type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
 
   if (dwarf2_per_objfile->die_type_hash == NULL)
     dwarf2_per_objfile->die_type_hash
@@ -24950,19 +23834,6 @@ partial_die_eq (const void *item_lhs, const void *item_rhs)
 struct cmd_list_element *set_dwarf_cmdlist;
 struct cmd_list_element *show_dwarf_cmdlist;
 
-static void
-set_dwarf_cmd (const char *args, int from_tty)
-{
-  help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
-            gdb_stdout);
-}
-
-static void
-show_dwarf_cmd (const char *args, int from_tty)
-{
-  cmd_show_list (show_dwarf_cmdlist, from_tty, "");
-}
-
 static void
 show_check_physname (struct ui_file *file, int from_tty,
                     struct cmd_list_element *c, const char *value)
@@ -24976,17 +23847,17 @@ void _initialize_dwarf2_read ();
 void
 _initialize_dwarf2_read ()
 {
-  add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
+  add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
 Set DWARF specific variables.\n\
 Configure DWARF variables such as the cache size."),
-                  &set_dwarf_cmdlist, "maintenance set dwarf ",
-                  0/*allow-unknown*/, &maintenance_set_cmdlist);
+                       &set_dwarf_cmdlist, "maintenance set dwarf ",
+                       0/*allow-unknown*/, &maintenance_set_cmdlist);
 
-  add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
+  add_show_prefix_cmd ("dwarf", class_maintenance, _("\
 Show DWARF specific variables.\n\
 Show DWARF variables such as the cache size."),
-                  &show_dwarf_cmdlist, "maintenance show dwarf ",
-                  0/*allow-unknown*/, &maintenance_show_cmdlist);
+                      &show_dwarf_cmdlist, "maintenance show dwarf ",
+                      0/*allow-unknown*/, &maintenance_show_cmdlist);
 
   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
                            &dwarf_max_cache_age, _("\
@@ -25062,5 +23933,7 @@ Warning: This option must be enabled before gdb reads the file."),
 #if GDB_SELF_TEST
   selftests::register_test ("dw2_expand_symtabs_matching",
                            selftests::dw2_expand_symtabs_matching::run_test);
+  selftests::register_test ("dwarf2_find_containing_comp_unit",
+                           selftests::find_containing_comp_unit::run_test);
 #endif
 }