Remove TYPE_TAG_NAME
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
index 78f1aab0ce72e7a8d14653cf4f124f37446dcb3c..b8e681b79a031b32811b46f3144679c06dc1e489 100644 (file)
@@ -29,6 +29,8 @@
    E.g., load_partial_dies, read_partial_die.  */
 
 #include "defs.h"
+#include "dwarf2read.h"
+#include "dwarf-index-common.h"
 #include "bfd.h"
 #include "elf-bfd.h"
 #include "symtab.h"
@@ -86,9 +88,8 @@
 #include <cmath>
 #include <set>
 #include <forward_list>
-
-typedef struct symbol *symbolp;
-DEF_VEC_P (symbolp);
+#include "rust-lang.h"
+#include "common/pathstuff.h"
 
 /* When == 1, print basic high level tracing messages.
    When > 1, be more verbose.
@@ -116,99 +117,6 @@ static int dwarf2_loclist_index;
 static int dwarf2_locexpr_block_index;
 static int dwarf2_loclist_block_index;
 
-/* A descriptor for dwarf sections.
-
-   S.ASECTION, SIZE are typically initialized when the objfile is first
-   scanned.  BUFFER, READIN are filled in later when the section is read.
-   If the section contained compressed data then SIZE is updated to record
-   the uncompressed size of the section.
-
-   DWP file format V2 introduces a wrinkle that is easiest to handle by
-   creating the concept of virtual sections contained within a real section.
-   In DWP V2 the sections of the input DWO files are concatenated together
-   into one section, but section offsets are kept relative to the original
-   input section.
-   If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
-   the real section this "virtual" section is contained in, and BUFFER,SIZE
-   describe the virtual section.  */
-
-struct dwarf2_section_info
-{
-  union
-  {
-    /* If this is a real section, the bfd section.  */
-    asection *section;
-    /* If this is a virtual section, pointer to the containing ("real")
-       section.  */
-    struct dwarf2_section_info *containing_section;
-  } s;
-  /* Pointer to section data, only valid if readin.  */
-  const gdb_byte *buffer;
-  /* The size of the section, real or virtual.  */
-  bfd_size_type size;
-  /* If this is a virtual section, the offset in the real section.
-     Only valid if is_virtual.  */
-  bfd_size_type virtual_offset;
-  /* True if we have tried to read this section.  */
-  char readin;
-  /* True if this is a virtual section, False otherwise.
-     This specifies which of s.section and s.containing_section to use.  */
-  char is_virtual;
-};
-
-typedef struct dwarf2_section_info dwarf2_section_info_def;
-DEF_VEC_O (dwarf2_section_info_def);
-
-/* All offsets in the index are of this type.  It must be
-   architecture-independent.  */
-typedef uint32_t offset_type;
-
-DEF_VEC_I (offset_type);
-
-/* Ensure only legit values are used.  */
-#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
-  do { \
-    gdb_assert ((unsigned int) (value) <= 1); \
-    GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
-  } while (0)
-
-/* Ensure only legit values are used.  */
-#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
-  do { \
-    gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
-                && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
-    GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
-  } while (0)
-
-/* Ensure we don't use more than the alloted nuber of bits for the CU.  */
-#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
-  do { \
-    gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
-    GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
-  } while (0)
-
-#if WORDS_BIGENDIAN
-
-/* Convert VALUE between big- and little-endian.  */
-
-static offset_type
-byte_swap (offset_type value)
-{
-  offset_type result;
-
-  result = (value & 0xff) << 24;
-  result |= (value & 0xff00) << 8;
-  result |= (value & 0xff0000) >> 8;
-  result |= (value & 0xff000000) >> 24;
-  return result;
-}
-
-#define MAYBE_SWAP(V)  byte_swap (V)
-
-#else
-#define MAYBE_SWAP(V) static_cast<offset_type> (V)
-#endif /* WORDS_BIGENDIAN */
-
 /* 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
@@ -239,6 +147,9 @@ struct name_component
 
 struct mapped_index_base
 {
+  mapped_index_base () = default;
+  DISABLE_COPY_AND_ASSIGN (mapped_index_base);
+
   /* The name_component table (a sorted vector).  See name_component's
      description above.  */
   std::vector<name_component> name_components;
@@ -287,10 +198,7 @@ struct mapped_index final : public mapped_index_base
   };
 
   /* Index data format version.  */
-  int version;
-
-  /* The total length of the buffer.  */
-  off_t total_size;
+  int version = 0;
 
   /* The address table data.  */
   gdb::array_view<const gdb_byte> address_table;
@@ -299,7 +207,7 @@ struct mapped_index final : public mapped_index_base
   gdb::array_view<symbol_table_slot> symbol_table;
 
   /* A pointer to the constant pool.  */
-  const char *constant_pool;
+  const char *constant_pool = nullptr;
 
   bool symbol_name_slot_invalid (offset_type idx) const override
   {
@@ -320,6 +228,11 @@ struct mapped_index final : public mapped_index_base
    Uninitialized map has CU_COUNT 0.  */
 struct mapped_debug_names final : public mapped_index_base
 {
+  mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
+  : dwarf2_per_objfile (dwarf2_per_objfile_)
+  {}
+
+  struct dwarf2_per_objfile *dwarf2_per_objfile;
   bfd_endian dwarf5_byte_order;
   bool dwarf5_is_dwarf64;
   bool augmentation_is_gdb;
@@ -363,158 +276,24 @@ struct mapped_debug_names final : public mapped_index_base
   { return this->name_count; }
 };
 
-typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
-DEF_VEC_P (dwarf2_per_cu_ptr);
+/* See dwarf2read.h.  */
 
-struct tu_stats
+dwarf2_per_objfile *
+get_dwarf2_per_objfile (struct objfile *objfile)
 {
-  int nr_uniq_abbrev_tables;
-  int nr_symtabs;
-  int nr_symtab_sharers;
-  int nr_stmt_less_type_units;
-  int nr_all_type_units_reallocs;
-};
+  return ((struct dwarf2_per_objfile *)
+         objfile_data (objfile, dwarf2_objfile_data_key));
+}
 
-/* Collection of data recorded per objfile.
-   This hangs off of dwarf2_objfile_data_key.  */
+/* Set the dwarf2_per_objfile associated to OBJFILE.  */
 
-struct dwarf2_per_objfile
+void
+set_dwarf2_per_objfile (struct objfile *objfile,
+                       struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
-  /* Construct a dwarf2_per_objfile for OBJFILE.  NAMES points to the
-     dwarf2 section names, or is NULL if the standard ELF names are
-     used.  */
-  dwarf2_per_objfile (struct objfile *objfile,
-                     const dwarf2_debug_sections *names);
-
-  ~dwarf2_per_objfile ();
-
-  DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
-
-  /* Free all cached compilation units.  */
-  void free_cached_comp_units ();
-private:
-  /* This function is mapped across the sections and remembers the
-     offset and size of each of the debugging sections we are
-     interested in.  */
-  void locate_sections (bfd *abfd, asection *sectp,
-                       const dwarf2_debug_sections &names);
-
-public:
-  dwarf2_section_info info {};
-  dwarf2_section_info abbrev {};
-  dwarf2_section_info line {};
-  dwarf2_section_info loc {};
-  dwarf2_section_info loclists {};
-  dwarf2_section_info macinfo {};
-  dwarf2_section_info macro {};
-  dwarf2_section_info str {};
-  dwarf2_section_info line_str {};
-  dwarf2_section_info ranges {};
-  dwarf2_section_info rnglists {};
-  dwarf2_section_info addr {};
-  dwarf2_section_info frame {};
-  dwarf2_section_info eh_frame {};
-  dwarf2_section_info gdb_index {};
-  dwarf2_section_info debug_names {};
-  dwarf2_section_info debug_aranges {};
-
-  VEC (dwarf2_section_info_def) *types = NULL;
-
-  /* Back link.  */
-  struct objfile *objfile = NULL;
-
-  /* Table of all the compilation units.  This is used to locate
-     the target compilation unit of a particular reference.  */
-  struct dwarf2_per_cu_data **all_comp_units = NULL;
-
-  /* The number of compilation units in ALL_COMP_UNITS.  */
-  int n_comp_units = 0;
-
-  /* The number of .debug_types-related CUs.  */
-  int n_type_units = 0;
-
-  /* The number of elements allocated in all_type_units.
-     If there are skeleton-less TUs, we add them to all_type_units lazily.  */
-  int n_allocated_type_units = 0;
-
-  /* The .debug_types-related CUs (TUs).
-     This is stored in malloc space because we may realloc it.  */
-  struct signatured_type **all_type_units = NULL;
-
-  /* Table of struct type_unit_group objects.
-     The hash key is the DW_AT_stmt_list value.  */
-  htab_t type_unit_groups {};
-
-  /* A table mapping .debug_types signatures to its signatured_type entry.
-     This is NULL if the .debug_types section hasn't been read in yet.  */
-  htab_t signatured_types {};
-
-  /* Type unit statistics, to see how well the scaling improvements
-     are doing.  */
-  struct tu_stats tu_stats {};
-
-  /* A chain of compilation units that are currently read in, so that
-     they can be freed later.  */
-  dwarf2_per_cu_data *read_in_chain = NULL;
-
-  /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
-     This is NULL if the table hasn't been allocated yet.  */
-  htab_t dwo_files {};
-
-  /* True if we've checked for whether there is a DWP file.  */
-  bool dwp_checked = false;
-
-  /* The DWP file if there is one, or NULL.  */
-  struct dwp_file *dwp_file = NULL;
-
-  /* The shared '.dwz' file, if one exists.  This is used when the
-     original data was compressed using 'dwz -m'.  */
-  struct dwz_file *dwz_file = NULL;
-
-  /* A flag indicating whether this objfile has a section loaded at a
-     VMA of 0.  */
-  bool has_section_at_zero = false;
-
-  /* True if we are using the mapped index,
-     or we are faking it for OBJF_READNOW's sake.  */
-  bool using_index = false;
-
-  /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
-  mapped_index *index_table = NULL;
-
-  /* The mapped index, or NULL if .debug_names is missing or not being used.  */
-  std::unique_ptr<mapped_debug_names> debug_names_table;
-
-  /* When using index_table, this keeps track of all quick_file_names entries.
-     TUs typically share line table entries with a CU, so we maintain a
-     separate table of all line table entries to support the sharing.
-     Note that while there can be way more TUs than CUs, we've already
-     sorted all the TUs into "type unit groups", grouped by their
-     DW_AT_stmt_list value.  Therefore the only sharing done here is with a
-     CU and its associated TU group if there is one.  */
-  htab_t quick_file_names_table {};
-
-  /* Set during partial symbol reading, to prevent queueing of full
-     symbols.  */
-  bool reading_partial_symbols = false;
-
-  /* Table mapping type DIEs to their struct type *.
-     This is NULL if not allocated yet.
-     The mapping is done via (CU/TU + DIE offset) -> type.  */
-  htab_t die_type_hash {};
-
-  /* The CUs we recently read.  */
-  VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
-
-  /* Table containing line_header indexed by offset and offset_in_dwz.  */
-  htab_t line_header_hash {};
-
-  /* Table containing all filenames.  This is an optional because the
-     table is lazily constructed on first access.  */
-  gdb::optional<filename_seen_cache> filenames_cache;
-};
-
-static struct dwarf2_per_objfile *dwarf2_per_objfile;
+  gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
+  set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
+}
 
 /* Default names of the debugging sections.  */
 
@@ -633,29 +412,28 @@ struct delayed_method_info
   struct die_info *die;
 };
 
-typedef struct delayed_method_info delayed_method_info;
-DEF_VEC_O (delayed_method_info);
-
 /* Internal state when decoding a particular compilation unit.  */
 struct dwarf2_cu
 {
-  /* The dwarf2_per_objfile containing this compilation unit.  */
-  struct dwarf2_per_objfile *dwarf2_per_objfile;
+  explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
+  ~dwarf2_cu ();
+
+  DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
 
   /* The header of the compilation unit.  */
-  struct comp_unit_head header;
+  struct comp_unit_head header {};
 
   /* Base address of this compilation unit.  */
-  CORE_ADDR base_address;
+  CORE_ADDR base_address = 0;
 
   /* Non-zero if base_address has been set.  */
-  int base_known;
+  int base_known = 0;
 
   /* The language we are debugging.  */
-  enum language language;
-  const struct language_defn *language_defn;
+  enum language language = language_unknown;
+  const struct language_defn *language_defn = nullptr;
 
-  const char *producer;
+  const char *producer = nullptr;
 
   /* The generic symbol table building routines have separate lists for
      file scope symbols and all all other scopes (local scopes).  So
@@ -666,60 +444,55 @@ struct dwarf2_cu
      first local scope, and all other local scopes as nested local
      scopes, and worked fine.  Check to see if we really need to
      distinguish these in buildsym.c.  */
-  struct pending **list_in_scope;
-
-  /* The abbrev table for this CU.
-     Normally this points to the abbrev table in the objfile.
-     But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file.  */
-  struct abbrev_table *abbrev_table;
+  struct pending **list_in_scope = nullptr;
 
   /* Hash table holding all the loaded partial DIEs
      with partial_die->offset.SECT_OFF as hash.  */
-  htab_t partial_dies;
+  htab_t partial_dies = nullptr;
 
   /* Storage for things with the same lifetime as this read-in compilation
      unit, including partial DIEs.  */
-  struct obstack comp_unit_obstack;
+  auto_obstack comp_unit_obstack;
 
   /* When multiple dwarf2_cu structures are living in memory, this field
      chains them all together, so that they can be released efficiently.
      We will probably also want a generation counter so that most-recently-used
      compilation units are cached...  */
-  struct dwarf2_per_cu_data *read_in_chain;
+  struct dwarf2_per_cu_data *read_in_chain = nullptr;
 
   /* Backlink to our per_cu entry.  */
   struct dwarf2_per_cu_data *per_cu;
 
   /* How many compilation units ago was this CU last referenced?  */
-  int last_used;
+  int last_used = 0;
 
   /* A hash table of DIE cu_offset for following references with
      die_info->offset.sect_off as hash.  */
-  htab_t die_hash;
+  htab_t die_hash = nullptr;
 
   /* Full DIEs if read in.  */
-  struct die_info *dies;
+  struct die_info *dies = nullptr;
 
   /* A set of pointers to dwarf2_per_cu_data objects for compilation
      units referenced by this one.  Only set during full symbol processing;
      partial symbol tables do not have dependencies.  */
-  htab_t dependencies;
+  htab_t dependencies = nullptr;
 
   /* Header data from the line table, during full symbol processing.  */
-  struct line_header *line_header;
+  struct line_header *line_header = nullptr;
   /* Non-NULL if LINE_HEADER is owned by this DWARF_CU.  Otherwise,
      it's owned by dwarf2_per_objfile::line_header_hash.  If non-NULL,
      this is the DW_TAG_compile_unit die for this CU.  We'll hold on
      to the line header as long as this DIE is being processed.  See
      process_die_scope.  */
-  die_info *line_header_die_owner;
+  die_info *line_header_die_owner = nullptr;
 
   /* A list of methods which need to have physnames computed
      after all type information has been read.  */
-  VEC (delayed_method_info) *method_list;
+  std::vector<delayed_method_info> method_list;
 
   /* To be copied to symtab->call_site_htab.  */
-  htab_t call_site_htab;
+  htab_t call_site_htab = nullptr;
 
   /* Non-NULL if this CU came from a DWO file.
      There is an invariant here that is important to remember:
@@ -730,12 +503,12 @@ struct dwarf2_cu
      is moot), or there is and either we're not going to read it (in which
      case this is NULL) or there is and we are reading it (in which case this
      is non-NULL).  */
-  struct dwo_unit *dwo_unit;
+  struct dwo_unit *dwo_unit = nullptr;
 
   /* The DW_AT_addr_base attribute if present, zero otherwise
      (zero is a valid value though).
      Note this value comes from the Fission stub CU/TU's DIE.  */
-  ULONGEST addr_base;
+  ULONGEST addr_base = 0;
 
   /* The DW_AT_ranges_base attribute if present, zero otherwise
      (zero is a valid value though).
@@ -747,7 +520,15 @@ struct dwarf2_cu
      DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
      DW_AT_ranges_base *would* have to be applied, and we'd have to care
      whether the DW_AT_ranges attribute came from the skeleton or DWO.  */
-  ULONGEST ranges_base;
+  ULONGEST ranges_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
+     read in, because otherwise at the point of rewriting some struct
+     type might not have been fully processed.  So, we keep a list of
+     all such types here and process them after expansion.  */
+  std::vector<struct type *> rust_unions;
 
   /* Mark used when releasing cached dies.  */
   unsigned int mark : 1;
@@ -772,147 +553,10 @@ struct dwarf2_cu
      this information, but later versions do.  */
 
   unsigned int processing_has_namespace_info : 1;
-};
-
-/* Persistent data held for a compilation unit, even when not
-   processing it.  We put a pointer to this structure in the
-   read_symtab_private field of the psymtab.  */
-
-struct dwarf2_per_cu_data
-{
-  /* The start offset and length of this compilation unit.
-     NOTE: Unlike comp_unit_head.length, this length includes
-     initial_length_size.
-     If the DIE refers to a DWO file, this is always of the original die,
-     not the DWO file.  */
-  sect_offset sect_off;
-  unsigned int length;
-
-  /* DWARF standard version this data has been read from (such as 4 or 5).  */
-  short dwarf_version;
-
-  /* Flag indicating this compilation unit will be read in before
-     any of the current compilation units are processed.  */
-  unsigned int queued : 1;
-
-  /* This flag will be set when reading partial DIEs if we need to load
-     absolutely all DIEs for this compilation unit, instead of just the ones
-     we think are interesting.  It gets set if we look for a DIE in the
-     hash table and don't find it.  */
-  unsigned int load_all_dies : 1;
-
-  /* Non-zero if this CU is from .debug_types.
-     Struct dwarf2_per_cu_data is contained in struct signatured_type iff
-     this is non-zero.  */
-  unsigned int is_debug_types : 1;
-
-  /* Non-zero if this CU is from the .dwz file.  */
-  unsigned int is_dwz : 1;
-
-  /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
-     This flag is only valid if is_debug_types is true.
-     We can't read a CU directly from a DWO file: There are required
-     attributes in the stub.  */
-  unsigned int reading_dwo_directly : 1;
-
-  /* Non-zero if the TU has been read.
-     This is used to assist the "Stay in DWO Optimization" for Fission:
-     When reading a DWO, it's faster to read TUs from the DWO instead of
-     fetching them from random other DWOs (due to comdat folding).
-     If the TU has already been read, the optimization is unnecessary
-     (and unwise - we don't want to change where gdb thinks the TU lives
-     "midflight").
-     This flag is only valid if is_debug_types is true.  */
-  unsigned int tu_read : 1;
-
-  /* The section this CU/TU lives in.
-     If the DIE refers to a DWO file, this is always the original die,
-     not the DWO file.  */
-  struct dwarf2_section_info *section;
-
-  /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
-     of the CU cache it gets reset to NULL again.  This is left as NULL for
-     dummy CUs (a CU header, but nothing else).  */
-  struct dwarf2_cu *cu;
-
-  /* The corresponding dwarf2_per_objfile.  */
-  struct dwarf2_per_objfile *dwarf2_per_objfile;
 
-  /* When dwarf2_per_objfile->using_index is true, the 'quick' field
-     is active.  Otherwise, the 'psymtab' field is active.  */
-  union
-  {
-    /* The partial symbol table associated with this compilation unit,
-       or NULL for unread partial units.  */
-    struct partial_symtab *psymtab;
-
-    /* Data needed by the "quick" functions.  */
-    struct dwarf2_per_cu_quick_data *quick;
-  } v;
-
-  /* The CUs we import using DW_TAG_imported_unit.  This is filled in
-     while reading psymtabs, used to compute the psymtab dependencies,
-     and then cleared.  Then it is filled in again while reading full
-     symbols, and only deleted when the objfile is destroyed.
-
-     This is also used to work around a difference between the way gold
-     generates .gdb_index version <=7 and the way gdb does.  Arguably this
-     is a gold bug.  For symbols coming from TUs, gold records in the index
-     the CU that includes the TU instead of the TU itself.  This breaks
-     dw2_lookup_symbol: It assumes that if the index says symbol X lives
-     in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
-     will find X.  Alas TUs live in their own symtab, so after expanding CU Y
-     we need to look in TU Z to find X.  Fortunately, this is akin to
-     DW_TAG_imported_unit, so we just use the same mechanism: For
-     .gdb_index version <=7 this also records the TUs that the CU referred
-     to.  Concurrently with this change gdb was modified to emit version 8
-     indices so we only pay a price for gold generated indices.
-     http://sourceware.org/bugzilla/show_bug.cgi?id=15021.  */
-  VEC (dwarf2_per_cu_ptr) *imported_symtabs;
-};
-
-/* Entry in the signatured_types hash table.  */
-
-struct signatured_type
-{
-  /* The "per_cu" object of this type.
-     This struct is used iff per_cu.is_debug_types.
-     N.B.: This is the first member so that it's easy to convert pointers
-     between them.  */
-  struct dwarf2_per_cu_data per_cu;
-
-  /* The type's signature.  */
-  ULONGEST signature;
-
-  /* Offset in the TU of the type's DIE, as read from the TU header.
-     If this TU is a DWO stub and the definition lives in a DWO file
-     (specified by DW_AT_GNU_dwo_name), this value is unusable.  */
-  cu_offset type_offset_in_tu;
-
-  /* Offset in the section of the type's DIE.
-     If the definition lives in a DWO file, this is the offset in the
-     .debug_types.dwo section.
-     The value is zero until the actual value is known.
-     Zero is otherwise not a valid section offset.  */
-  sect_offset type_offset_in_section;
-
-  /* Type units are grouped by their DW_AT_stmt_list entry so that they
-     can share them.  This points to the containing symtab.  */
-  struct type_unit_group *type_unit_group;
-
-  /* The type.
-     The first time we encounter this type we fully read it in and install it
-     in the symbol tables.  Subsequent times we only need the type.  */
-  struct type *type;
-
-  /* Containing DWO unit.
-     This field is valid iff per_cu.reading_dwo_directly.  */
-  struct dwo_unit *dwo_unit;
+  struct partial_die_info *find_partial_die (sect_offset sect_off);
 };
 
-typedef struct signatured_type *sig_type_ptr;
-DEF_VEC_P (sig_type_ptr);
-
 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
    This includes type_unit_group and quick_file_names.  */
 
@@ -1166,49 +810,60 @@ struct dwp_hash_table
 
 struct dwp_file
 {
+  dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
+    : name (name_),
+      dbfd (std::move (abfd))
+  {
+  }
+
   /* Name of the file.  */
   const char *name;
 
   /* File format version.  */
-  int version;
+  int version = 0;
 
   /* The bfd.  */
-  bfd *dbfd;
+  gdb_bfd_ref_ptr dbfd;
 
   /* Section info for this file.  */
-  struct dwp_sections sections;
+  struct dwp_sections sections {};
 
   /* Table of CUs in the file.  */
-  const struct dwp_hash_table *cus;
+  const struct dwp_hash_table *cus = nullptr;
 
   /* Table of TUs in the file.  */
-  const struct dwp_hash_table *tus;
+  const struct dwp_hash_table *tus = nullptr;
 
   /* Tables of loaded CUs/TUs.  Each entry is a struct dwo_unit *.  */
-  htab_t loaded_cus;
-  htab_t loaded_tus;
+  htab_t loaded_cus {};
+  htab_t loaded_tus {};
 
   /* Table to map ELF section numbers to their sections.
      This is only needed for the DWP V1 file format.  */
-  unsigned int num_sections;
-  asection **elf_sections;
+  unsigned int num_sections = 0;
+  asection **elf_sections = nullptr;
 };
 
 /* This represents a '.dwz' file.  */
 
 struct dwz_file
 {
+  dwz_file (gdb_bfd_ref_ptr &&bfd)
+    : dwz_bfd (std::move (bfd))
+  {
+  }
+
   /* A dwz file can only contain a few sections.  */
-  struct dwarf2_section_info abbrev;
-  struct dwarf2_section_info info;
-  struct dwarf2_section_info str;
-  struct dwarf2_section_info line;
-  struct dwarf2_section_info macro;
-  struct dwarf2_section_info gdb_index;
-  struct dwarf2_section_info debug_names;
+  struct dwarf2_section_info abbrev {};
+  struct dwarf2_section_info info {};
+  struct dwarf2_section_info str {};
+  struct dwarf2_section_info line {};
+  struct dwarf2_section_info macro {};
+  struct dwarf2_section_info gdb_index {};
+  struct dwarf2_section_info debug_names {};
 
   /* The dwz's BFD.  */
-  bfd *dwz_bfd;
+  gdb_bfd_ref_ptr dwz_bfd;
 };
 
 /* Struct used to pass misc. parameters to read_die_and_children, et
@@ -1239,6 +894,9 @@ struct die_reader_specs
 
   /* The value of the DW_AT_comp_dir attribute.  */
   const char *comp_dir;
+
+  /* The abbreviation table to use when reading the DIEs.  */
+  struct abbrev_table *abbrev_table;
 };
 
 /* Type of function passed to init_cutu_and_read_dies, et.al.  */
@@ -1384,16 +1042,34 @@ file_entry::include_dir (const line_header *lh) const
 
 /* When we construct a partial symbol table entry we only
    need this much information.  */
-struct partial_die_info
+struct partial_die_info : public allocate_on_obstack
   {
+    partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
+
+    /* Disable assign but still keep copy ctor, which is needed
+       load_partial_dies.   */
+    partial_die_info& operator=(const partial_die_info& rhs) = delete;
+
+    /* Adjust the partial die before generating a symbol for it.  This
+       function may set the is_external flag or change the DIE's
+       name.  */
+    void fixup (struct dwarf2_cu *cu);
+
+    /* Read a minimal amount of information into the minimal die
+       structure.  */
+    const gdb_byte *read (const struct die_reader_specs *reader,
+                         const struct abbrev_info &abbrev,
+                         const gdb_byte *info_ptr);
+
     /* Offset of this DIE.  */
-    sect_offset sect_off;
+    const sect_offset sect_off;
 
     /* DWARF-2 tag for this DIE.  */
-    ENUM_BITFIELD(dwarf_tag) tag : 16;
+    const ENUM_BITFIELD(dwarf_tag) tag : 16;
 
     /* Assorted flags describing the data found in this DIE.  */
-    unsigned int has_children : 1;
+    const unsigned int has_children : 1;
+
     unsigned int is_external : 1;
     unsigned int is_declaration : 1;
     unsigned int has_type : 1;
@@ -1417,7 +1093,7 @@ struct partial_die_info
     /* Flag set if any of the DIE's children are template arguments.  */
     unsigned int has_template_arguments : 1;
 
-    /* Flag set if fixup_partial_die has been called on this die.  */
+    /* Flag set if fixup has been called on this die.  */
     unsigned int fixup_called : 1;
 
     /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt.  */
@@ -1428,15 +1104,15 @@ struct partial_die_info
 
     /* The name of this DIE.  Normally the value of DW_AT_name, but
        sometimes a default name for unnamed DIEs.  */
-    const char *name;
+    const char *name = nullptr;
 
     /* The linkage name, if present.  */
-    const char *linkage_name;
+    const char *linkage_name = nullptr;
 
     /* The scope to prepend to our children.  This is generally
        allocated on the comp_unit_obstack, so will disappear
        when this compilation unit leaves the cache.  */
-    const char *scope;
+    const char *scope = nullptr;
 
     /* Some data associated with the partial DIE.  The tag determines
        which field is live.  */
@@ -1446,26 +1122,58 @@ struct partial_die_info
       struct dwarf_block *locdesc;
       /* The offset of an import, for DW_TAG_imported_unit.  */
       sect_offset sect_off;
-    } d;
+    } d {};
 
     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
-    CORE_ADDR lowpc;
-    CORE_ADDR highpc;
+    CORE_ADDR lowpc = 0;
+    CORE_ADDR highpc = 0;
 
     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
        DW_AT_sibling, if any.  */
-    /* NOTE: This member isn't strictly necessary, read_partial_die could
-       return DW_AT_sibling values to its caller load_partial_dies.  */
-    const gdb_byte *sibling;
+    /* NOTE: This member isn't strictly necessary, partial_die_info::read
+       could return DW_AT_sibling values to its caller load_partial_dies.  */
+    const gdb_byte *sibling = nullptr;
 
     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
        DW_AT_specification (or DW_AT_abstract_origin or
        DW_AT_extension).  */
-    sect_offset spec_offset;
+    sect_offset spec_offset {};
 
     /* Pointers to this DIE's parent, first child, and next sibling,
        if any.  */
-    struct partial_die_info *die_parent, *die_child, *die_sibling;
+    struct partial_die_info *die_parent = nullptr;
+    struct partial_die_info *die_child = nullptr;
+    struct partial_die_info *die_sibling = nullptr;
+
+    friend struct partial_die_info *
+    dwarf2_cu::find_partial_die (sect_offset sect_off);
+
+  private:
+    /* Only need to do look up in dwarf2_cu::find_partial_die.  */
+    partial_die_info (sect_offset sect_off)
+      : partial_die_info (sect_off, DW_TAG_padding, 0)
+    {
+    }
+
+    partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
+                     int has_children_)
+      : sect_off (sect_off_), tag (tag_), has_children (has_children_)
+    {
+      is_external = 0;
+      is_declaration = 0;
+      has_type = 0;
+      has_specification = 0;
+      has_pc_info = 0;
+      may_be_inlined = 0;
+      main_subprogram = 0;
+      scope_set = 0;
+      has_byte_size = 0;
+      has_const_value = 0;
+      has_template_arguments = 0;
+      fixup_called = 0;
+      is_dwz = 0;
+      spec_is_dwz = 0;
+    }
   };
 
 /* This data structure holds the information of an abbrev.  */
@@ -1495,20 +1203,47 @@ struct attr_abbrev
 
 struct abbrev_table
 {
+  explicit abbrev_table (sect_offset off)
+    : sect_off (off)
+  {
+    m_abbrevs =
+      XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
+    memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
+  }
+
+  DISABLE_COPY_AND_ASSIGN (abbrev_table);
+
+  /* Allocate space for a struct abbrev_info object in
+     ABBREV_TABLE.  */
+  struct abbrev_info *alloc_abbrev ();
+
+  /* Add an abbreviation to the table.  */
+  void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
+
+  /* Look up an abbrev in the table.
+     Returns NULL if the abbrev is not found.  */
+
+  struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
+
+
   /* Where the abbrev table came from.
      This is used as a sanity check when the table is used.  */
-  sect_offset sect_off;
+  const sect_offset sect_off;
 
   /* Storage for the abbrev table.  */
-  struct obstack abbrev_obstack;
+  auto_obstack abbrev_obstack;
+
+private:
 
   /* Hash table of abbrevs.
      This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
      It could be statically allocated, but the previous code didn't so we
      don't either.  */
-  struct abbrev_info **abbrevs;
+  struct abbrev_info **m_abbrevs;
 };
 
+typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
+
 /* Attributes have a name and a value.  */
 struct attribute
   {
@@ -1601,31 +1336,36 @@ struct dwarf_block
    and friends.  */
 static int bits_per_byte = 8;
 
-struct nextfield
-{
-  struct nextfield *next;
-  int accessibility;
-  int virtuality;
-  struct field field;
-};
+/* 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 nextfnfield
+struct variant_field
 {
-  struct nextfnfield *next;
-  struct fn_field fnfield;
+  /* If we see a DW_TAG_variant, then this will be the discriminant
+     value.  */
+  ULONGEST discriminant_value;
+  /* 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;
 };
 
-struct fnfieldlist
+struct nextfield
 {
-  const char *name;
-  int length;
-  struct nextfnfield *head;
+  int accessibility = 0;
+  int virtuality = 0;
+  /* Extra information to describe a variant or variant part.  */
+  struct variant_field variant {};
+  struct field field {};
 };
 
-struct decl_field_list
+struct fnfieldlist
 {
-  struct decl_field field;
-  struct decl_field_list *next;
+  const char *name = nullptr;
+  std::vector<struct fn_field> fnfields;
 };
 
 /* The routines that read and process dies for a C struct or C++ class
@@ -1634,34 +1374,27 @@ struct decl_field_list
 struct field_info
   {
     /* List of data member and baseclasses fields.  */
-    struct nextfield *fields, *baseclasses;
+    std::vector<struct nextfield> fields;
+    std::vector<struct nextfield> baseclasses;
 
     /* Number of fields (including baseclasses).  */
-    int nfields;
-
-    /* Number of baseclasses.  */
-    int nbaseclasses;
+    int nfields = 0;
 
     /* Set if the accesibility of one of the fields is not public.  */
-    int non_public_fields;
+    int non_public_fields = 0;
 
     /* Member function fieldlist array, contains name of possibly overloaded
        member function, number of overloaded member functions and a pointer
        to the head of the member function field chain.  */
-    struct fnfieldlist *fnfieldlists;
-
-    /* Number of entries in the fnfieldlists array.  */
-    int nfnfields;
+    std::vector<struct fnfieldlist> fnfieldlists;
 
     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
-    struct decl_field_list *typedef_field_list;
-    unsigned typedef_field_list_count;
+    std::vector<struct decl_field> typedef_field_list;
 
     /* Nested types defined by this class and the number of elements in this
        list.  */
-    struct decl_field_list *nested_types_list;
-    unsigned nested_types_list_count;
+    std::vector<struct decl_field> nested_types_list;
   };
 
 /* One item on the queue of compilation units to read in full symbols
@@ -1708,7 +1441,8 @@ static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
                                        struct die_info *type_unit_die,
                                        int has_children, void *data);
 
-static void dwarf2_build_psymtabs_hard (struct objfile *);
+static void dwarf2_build_psymtabs_hard
+  (struct dwarf2_per_objfile *dwarf2_per_objfile);
 
 static void scan_partial_symbols (struct partial_die_info *,
                                  CORE_ADDR *, CORE_ADDR *,
@@ -1737,38 +1471,18 @@ static void dwarf2_read_symtab (struct partial_symtab *,
 
 static void psymtab_to_symtab_1 (struct partial_symtab *);
 
-static struct abbrev_info *abbrev_table_lookup_abbrev
-  (const struct abbrev_table *, unsigned int);
-
-static struct abbrev_table *abbrev_table_read_table
-  (struct dwarf2_section_info *, sect_offset);
-
-static void abbrev_table_free (struct abbrev_table *);
-
-static void abbrev_table_free_cleanup (void *);
-
-static void dwarf2_read_abbrevs (struct dwarf2_cu *,
-                                struct dwarf2_section_info *);
-
-static void dwarf2_free_abbrev_table (void *);
+static abbrev_table_up abbrev_table_read_table
+  (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
+   sect_offset);
 
 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
 
 static struct partial_die_info *load_partial_dies
   (const struct die_reader_specs *, const gdb_byte *, int);
 
-static const gdb_byte *read_partial_die (const struct die_reader_specs *,
-                                        struct partial_die_info *,
-                                        struct abbrev_info *,
-                                        unsigned int,
-                                        const gdb_byte *);
-
 static struct partial_die_info *find_partial_die (sect_offset, int,
                                                  struct dwarf2_cu *);
 
-static void fixup_partial_die (struct partial_die_info *,
-                              struct dwarf2_cu *);
-
 static const gdb_byte *read_attribute (const struct die_reader_specs *,
                                       struct attribute *, struct attr_abbrev *,
                                       const gdb_byte *);
@@ -1798,25 +1512,28 @@ static LONGEST read_offset (bfd *, const gdb_byte *,
 
 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
 
-static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
-                                      sect_offset);
+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 (bfd *, const gdb_byte *,
-                                        const struct comp_unit_head *,
-                                        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 (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 (bfd *abfd,
-                                                  LONGEST str_offset);
+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 dwz_file *, LONGEST);
+static const char *read_indirect_string_from_dwz
+  (struct objfile *objfile, struct dwz_file *, LONGEST);
 
 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
 
@@ -1860,10 +1577,7 @@ static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
                                                    CORE_ADDR);
 
 static struct symbol *new_symbol (struct die_info *, struct type *,
-                                 struct dwarf2_cu *);
-
-static struct symbol *new_symbol_full (struct die_info *, struct type *,
-                                      struct dwarf2_cu *, struct symbol *);
+                                 struct dwarf2_cu *, struct symbol * = NULL);
 
 static void dwarf2_const_value (const struct attribute *, struct symbol *,
                                struct dwarf2_cu *);
@@ -2106,38 +1820,30 @@ static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
                                     const gdb_byte *info_ptr,
                                     struct abbrev_info *abbrev);
 
-static void free_stack_comp_unit (void *);
-
 static hashval_t partial_die_hash (const void *item);
 
 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
 
 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
-  (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
-
-static void init_one_comp_unit (struct dwarf2_cu *cu,
-                               struct dwarf2_per_cu_data *per_cu);
+  (sect_offset sect_off, unsigned int offset_in_dwz,
+   struct dwarf2_per_objfile *dwarf2_per_objfile);
 
 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
                                   struct die_info *comp_unit_die,
                                   enum language pretend_language);
 
-static void free_heap_comp_unit (void *);
-
-static void free_cached_comp_units (void *);
-
-static void age_cached_comp_units (void);
+static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
 
 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
 
 static struct type *set_die_type (struct die_info *, struct type *,
                                  struct dwarf2_cu *);
 
-static void create_all_comp_units (struct objfile *);
+static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
 
-static int create_all_type_units (struct objfile *);
+static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
 
-static void load_full_comp_unit (struct dwarf2_per_cu_data *,
+static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
                                 enum language);
 
 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
@@ -2158,12 +1864,47 @@ static struct type *get_die_type_at_offset (sect_offset,
 
 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
 
-static void dwarf2_release_queue (void *dummy);
-
 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
                             enum language pretend_language);
 
-static void process_queue (void);
+static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
+
+/* Class, the destructor of which frees all allocated queue entries.  This
+   will only have work to do if an error was thrown while processing the
+   dwarf.  If no error was thrown then the queue entries should have all
+   been processed, and freed, as we went along.  */
+
+class dwarf2_queue_guard
+{
+public:
+  dwarf2_queue_guard () = default;
+
+  /* Free any entries remaining on the queue.  There should only be
+     entries left if we hit an error while processing the dwarf.  */
+  ~dwarf2_queue_guard ()
+  {
+    struct dwarf2_queue_item *item, *last;
+
+    item = dwarf2_queue;
+    while (item)
+      {
+       /* Anything still marked queued is likely to be in an
+          inconsistent state, so discard it.  */
+       if (item->per_cu->queued)
+         {
+           if (item->per_cu->cu != NULL)
+             free_one_cached_comp_unit (item->per_cu);
+           item->per_cu->queued = 0;
+         }
+
+       last = item;
+       item = item->next;
+       xfree (last);
+      }
+
+    dwarf2_queue = dwarf2_queue_tail = NULL;
+  }
+};
 
 /* The return type of find_file_and_directory.  Note, the enclosed
    string pointers are only valid while this object is valid.  */
@@ -2194,14 +1935,15 @@ static char *file_full_name (int file, struct line_header *lh,
 enum class rcuh_kind { COMPILE, TYPE };
 
 static const gdb_byte *read_and_check_comp_unit_head
-  (struct comp_unit_head *header,
+  (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 void init_cutu_and_read_dies
   (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
-   int use_existing_cu, int keep,
+   int use_existing_cu, int keep, bool skip_partial,
    die_reader_func_ftype *die_reader_func, void *data);
 
 static void init_cutu_and_read_dies_simple
@@ -2213,10 +1955,12 @@ static htab_t allocate_signatured_type_table (struct objfile *objfile);
 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
 
 static struct dwo_unit *lookup_dwo_unit_in_dwp
-  (struct dwp_file *dwp_file, const char *comp_dir,
+  (struct dwarf2_per_objfile *dwarf2_per_objfile,
+   struct dwp_file *dwp_file, const char *comp_dir,
    ULONGEST signature, int is_debug_types);
 
-static struct dwp_file *get_dwp_file (void);
+static struct dwp_file *get_dwp_file
+  (struct dwarf2_per_objfile *dwarf2_per_objfile);
 
 static struct dwo_unit *lookup_dwo_comp_unit
   (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
@@ -2226,9 +1970,23 @@ static struct dwo_unit *lookup_dwo_type_unit
 
 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
 
-static void free_dwo_file_cleanup (void *);
+static void free_dwo_file (struct dwo_file *);
+
+/* A unique_ptr helper to free a dwo_file.  */
+
+struct dwo_file_deleter
+{
+  void operator() (struct dwo_file *df) const
+  {
+    free_dwo_file (df);
+  }
+};
+
+/* A unique pointer to a dwo_file.  */
 
-static void process_cu_includes (void);
+typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
+
+static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
 
 static void check_producer (struct dwarf2_cu *cu);
 
@@ -2239,45 +1997,40 @@ static void free_line_header_voidp (void *arg);
 static void
 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
 {
-  complaint (&symfile_complaints,
-            _("statement list doesn't fit in .debug_line section"));
+  complaint (_("statement list doesn't fit in .debug_line section"));
 }
 
 static void
 dwarf2_debug_line_missing_file_complaint (void)
 {
-  complaint (&symfile_complaints,
-            _(".debug_line section has line data without a file"));
+  complaint (_(".debug_line section has line data without a file"));
 }
 
 static void
 dwarf2_debug_line_missing_end_sequence_complaint (void)
 {
-  complaint (&symfile_complaints,
-            _(".debug_line section has line "
+  complaint (_(".debug_line section has line "
               "program sequence without an end"));
 }
 
 static void
 dwarf2_complex_location_expr_complaint (void)
 {
-  complaint (&symfile_complaints, _("location expression too complex"));
+  complaint (_("location expression too complex"));
 }
 
 static void
 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
                                              int arg3)
 {
-  complaint (&symfile_complaints,
-            _("const value length mismatch for '%s', got %d, expected %d"),
+  complaint (_("const value length mismatch for '%s', got %d, expected %d"),
             arg1, arg2, arg3);
 }
 
 static void
 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
 {
-  complaint (&symfile_complaints,
-            _("debug info runs off end of %s section"
+  complaint (_("debug info runs off end of %s section"
               " [in module %s]"),
             get_section_name (section),
             get_section_file_name (section));
@@ -2286,8 +2039,7 @@ dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
 static void
 dwarf2_macro_malformed_definition_complaint (const char *arg1)
 {
-  complaint (&symfile_complaints,
-            _("macro debug info contains a "
+  complaint (_("macro debug info contains a "
               "malformed macro definition:\n`%s'"),
             arg1);
 }
@@ -2295,8 +2047,7 @@ dwarf2_macro_malformed_definition_complaint (const char *arg1)
 static void
 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
 {
-  complaint (&symfile_complaints,
-            _("invalid attribute class or form for '%s' in '%s'"),
+  complaint (_("invalid attribute class or form for '%s' in '%s'"),
             arg1, arg2);
 }
 
@@ -2362,11 +2113,6 @@ attr_value_as_address (struct attribute *attr)
   return addr;
 }
 
-/* The suffix for an index file.  */
-#define INDEX4_SUFFIX ".gdb-index"
-#define INDEX5_SUFFIX ".debug_names"
-#define DEBUG_STR_SUFFIX ".debug_str"
-
 /* See declaration.  */
 
 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
@@ -2382,6 +2128,8 @@ dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
     locate_sections (obfd, sec, *names);
 }
 
+static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
+
 dwarf2_per_objfile::~dwarf2_per_objfile ()
 {
   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
@@ -2393,6 +2141,17 @@ dwarf2_per_objfile::~dwarf2_per_objfile ()
   if (line_header_hash)
     htab_delete (line_header_hash);
 
+  for (dwarf2_per_cu_data *per_cu : all_comp_units)
+    VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
+
+  for (signatured_type *sig_type : all_type_units)
+    VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
+
+  VEC_free (dwarf2_section_info_def, types);
+
+  if (dwo_files != NULL)
+    free_dwo_files (dwo_files, objfile);
+
   /* Everything else should be on the objfile obstack.  */
 }
 
@@ -2407,12 +2166,36 @@ dwarf2_per_objfile::free_cached_comp_units ()
     {
       dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
 
-      free_heap_comp_unit (per_cu->cu);
+      delete per_cu->cu;
       *last_chain = next_cu;
       per_cu = next_cu;
     }
 }
 
+/* A helper class that calls free_cached_comp_units on
+   destruction.  */
+
+class free_cached_comp_units
+{
+public:
+
+  explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
+    : m_per_objfile (per_objfile)
+  {
+  }
+
+  ~free_cached_comp_units ()
+  {
+    m_per_objfile->free_cached_comp_units ();
+  }
+
+  DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
+
+private:
+
+  dwarf2_per_objfile *m_per_objfile;
+};
+
 /* Try to locate the sections we need for DWARF 2 debugging
    information and return true if we have enough to do something.
    NAMES points to the dwarf2 section names, or is NULL if the standard
@@ -2425,16 +2208,16 @@ dwarf2_has_info (struct objfile *objfile,
   if (objfile->flags & OBJF_READNEVER)
     return 0;
 
-  dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
-                       objfile_data (objfile, dwarf2_objfile_data_key));
-  if (!dwarf2_per_objfile)
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
+
+  if (dwarf2_per_objfile == NULL)
     {
       /* Initialize per-objfile state.  */
-      struct dwarf2_per_objfile *data
-       = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
-
-      dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
-      set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
+      dwarf2_per_objfile
+       = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
+                                                                    names);
+      set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
     }
   return (!dwarf2_per_objfile->info.is_virtual
          && dwarf2_per_objfile->info.s.section != NULL
@@ -2664,14 +2447,10 @@ dwarf2_section_empty_p (const struct dwarf2_section_info *section)
   return section->s.section == NULL || section->size == 0;
 }
 
-/* Read the contents of the section INFO.
-   OBJFILE is the main object file, but not necessarily the file where
-   the section comes from.  E.g., for DWO files the bfd of INFO is the bfd
-   of the DWO file.
-   If the section is compressed, uncompress it before returning.  */
+/* See dwarf2read.h.  */
 
-static void
-dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
+void
+dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
 {
   asection *sectp;
   bfd *abfd;
@@ -2855,16 +2634,15 @@ locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
    is such a section but the file cannot be found.  */
 
 static struct dwz_file *
-dwarf2_get_dwz_file (void)
+dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
   const char *filename;
-  struct dwz_file *result;
   bfd_size_type buildid_len_arg;
   size_t buildid_len;
   bfd_byte *buildid;
 
   if (dwarf2_per_objfile->dwz_file != NULL)
-    return dwarf2_per_objfile->dwz_file;
+    return dwarf2_per_objfile->dwz_file.get ();
 
   bfd_set_error (bfd_error_no_error);
   gdb::unique_xmalloc_ptr<char> data
@@ -2910,15 +2688,16 @@ dwarf2_get_dwz_file (void)
     error (_("could not find '.gnu_debugaltlink' file for %s"),
           objfile_name (dwarf2_per_objfile->objfile));
 
-  result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
-                          struct dwz_file);
-  result->dwz_bfd = dwz_bfd.release ();
+  std::unique_ptr<struct dwz_file> result
+    (new struct dwz_file (std::move (dwz_bfd)));
 
-  bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
+  bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
+                        result.get ());
 
-  gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
-  dwarf2_per_objfile->dwz_file = result;
-  return result;
+  gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
+                           result->dwz_bfd.get ());
+  dwarf2_per_objfile->dwz_file = std::move (result);
+  return dwarf2_per_objfile->dwz_file.get ();
 }
 \f
 /* DWARF quick_symbols_functions support.  */
@@ -3052,12 +2831,12 @@ create_quick_file_names_table (unsigned int nr_initial_entries)
    processing PER_CU->CU.  dw2_setup must have been already called.  */
 
 static void
-load_cu (struct dwarf2_per_cu_data *per_cu)
+load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
 {
   if (per_cu->is_debug_types)
     load_full_type_unit (per_cu);
   else
-    load_full_comp_unit (per_cu, language_minimal);
+    load_full_comp_unit (per_cu, skip_partial, language_minimal);
 
   if (per_cu->cu == NULL)
     return;  /* Dummy CU.  */
@@ -3068,23 +2847,26 @@ load_cu (struct dwarf2_per_cu_data *per_cu)
 /* Read in the symbols for PER_CU.  */
 
 static void
-dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
+dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
 {
-  struct cleanup *back_to;
+  struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
 
   /* Skip type_unit_groups, reading the type units they contain
      is handled elsewhere.  */
   if (IS_TYPE_UNIT_GROUP (per_cu))
     return;
 
-  back_to = make_cleanup (dwarf2_release_queue, NULL);
+  /* The destructor of dwarf2_queue_guard frees any entries left on
+     the queue.  After this point we're guaranteed to leave this function
+     with the dwarf queue empty.  */
+  dwarf2_queue_guard q_guard;
 
   if (dwarf2_per_objfile->using_index
       ? per_cu->v.quick->compunit_symtab == NULL
       : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
     {
       queue_comp_unit (per_cu, language_minimal);
-      load_cu (per_cu);
+      load_cu (per_cu, skip_partial);
 
       /* If we just loaded a CU from a DWO, and we're working with an index
         that may badly handle TUs, load all the TUs in that DWO as well.
@@ -3095,17 +2877,15 @@ dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
          && dwarf2_per_objfile->index_table != NULL
          && dwarf2_per_objfile->index_table->version <= 7
          /* DWP files aren't supported yet.  */
-         && get_dwp_file () == NULL)
+         && get_dwp_file (dwarf2_per_objfile) == NULL)
        queue_and_load_all_dwo_tus (per_cu);
     }
 
-  process_queue ();
+  process_queue (dwarf2_per_objfile);
 
   /* Age the cache, releasing compilation units that have not
      been used recently.  */
-  age_cached_comp_units ();
-
-  do_cleanups (back_to);
+  age_cached_comp_units (dwarf2_per_objfile);
 }
 
 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
@@ -3113,57 +2893,55 @@ dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
    table.  */
 
 static struct compunit_symtab *
-dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
+dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
+
   gdb_assert (dwarf2_per_objfile->using_index);
   if (!per_cu->v.quick->compunit_symtab)
     {
-      struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
+      free_cached_comp_units freer (dwarf2_per_objfile);
       scoped_restore decrementer = increment_reading_symtab ();
-      dw2_do_instantiate_symtab (per_cu);
-      process_cu_includes ();
-      do_cleanups (back_to);
+      dw2_do_instantiate_symtab (per_cu, skip_partial);
+      process_cu_includes (dwarf2_per_objfile);
     }
 
   return per_cu->v.quick->compunit_symtab;
 }
 
-/* Return the CU/TU given its index.
+/* See declaration.  */
 
-   This is intended for loops like:
+dwarf2_per_cu_data *
+dwarf2_per_objfile::get_cutu (int index)
+{
+  if (index >= this->all_comp_units.size ())
+    {
+      index -= this->all_comp_units.size ();
+      gdb_assert (index < this->all_type_units.size ());
+      return &this->all_type_units[index]->per_cu;
+    }
 
-   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
-                   + dwarf2_per_objfile->n_type_units); ++i)
-     {
-       struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
+  return this->all_comp_units[index];
+}
 
-       ...;
-     }
-*/
+/* See declaration.  */
 
-static struct dwarf2_per_cu_data *
-dw2_get_cutu (int index)
+dwarf2_per_cu_data *
+dwarf2_per_objfile::get_cu (int index)
 {
-  if (index >= dwarf2_per_objfile->n_comp_units)
-    {
-      index -= dwarf2_per_objfile->n_comp_units;
-      gdb_assert (index < dwarf2_per_objfile->n_type_units);
-      return &dwarf2_per_objfile->all_type_units[index]->per_cu;
-    }
+  gdb_assert (index >= 0 && index < this->all_comp_units.size ());
 
-  return dwarf2_per_objfile->all_comp_units[index];
+  return this->all_comp_units[index];
 }
 
-/* Return the CU given its index.
-   This differs from dw2_get_cutu in that it's for when you know INDEX
-   refers to a CU.  */
+/* See declaration.  */
 
-static struct dwarf2_per_cu_data *
-dw2_get_cu (int index)
+signatured_type *
+dwarf2_per_objfile::get_tu (int index)
 {
-  gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
+  gdb_assert (index >= 0 && index < this->all_type_units.size ());
 
-  return dwarf2_per_objfile->all_comp_units[index];
+  return this->all_type_units[index];
 }
 
 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
@@ -3171,11 +2949,12 @@ dw2_get_cu (int index)
    values.  */
 
 static dwarf2_per_cu_data *
-create_cu_from_index_list (struct objfile *objfile,
+create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
                           struct dwarf2_section_info *section,
                           int is_dwz,
                           sect_offset sect_off, ULONGEST length)
 {
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
   dwarf2_per_cu_data *the_cu
     = OBSTACK_ZALLOC (&objfile->objfile_obstack,
                      struct dwarf2_per_cu_data);
@@ -3193,15 +2972,12 @@ create_cu_from_index_list (struct objfile *objfile,
    CUs.  */
 
 static void
-create_cus_from_index_list (struct objfile *objfile,
+create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
                            const gdb_byte *cu_list, offset_type n_elements,
                            struct dwarf2_section_info *section,
-                           int is_dwz,
-                           int base_offset)
+                           int is_dwz)
 {
-  offset_type i;
-
-  for (i = 0; i < n_elements; i += 2)
+  for (offset_type i = 0; i < n_elements; i += 2)
     {
       gdb_static_assert (sizeof (ULONGEST) >= 8);
 
@@ -3210,8 +2986,10 @@ create_cus_from_index_list (struct objfile *objfile,
       ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
       cu_list += 2 * 8;
 
-      dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
-       = create_cu_from_index_list (objfile, section, is_dwz, sect_off, length);
+      dwarf2_per_cu_data *per_cu
+       = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
+                                    sect_off, length);
+      dwarf2_per_objfile->all_comp_units.push_back (per_cu);
     }
 }
 
@@ -3219,48 +2997,42 @@ create_cus_from_index_list (struct objfile *objfile,
    the CU objects for this objfile.  */
 
 static void
-create_cus_from_index (struct objfile *objfile,
+create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
                       const gdb_byte *cu_list, offset_type cu_list_elements,
                       const gdb_byte *dwz_list, offset_type dwz_elements)
 {
-  struct dwz_file *dwz;
-
-  dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
-  dwarf2_per_objfile->all_comp_units =
-    XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
-              dwarf2_per_objfile->n_comp_units);
+  gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
+  dwarf2_per_objfile->all_comp_units.reserve
+    ((cu_list_elements + dwz_elements) / 2);
 
-  create_cus_from_index_list (objfile, cu_list, cu_list_elements,
-                             &dwarf2_per_objfile->info, 0, 0);
+  create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
+                             &dwarf2_per_objfile->info, 0);
 
   if (dwz_elements == 0)
     return;
 
-  dwz = dwarf2_get_dwz_file ();
-  create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
-                             cu_list_elements / 2);
+  dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
+  create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
+                             &dwz->info, 1);
 }
 
 /* Create the signatured type hash table from the index.  */
 
 static void
-create_signatured_type_table_from_index (struct objfile *objfile,
-                                        struct dwarf2_section_info *section,
-                                        const gdb_byte *bytes,
-                                        offset_type elements)
+create_signatured_type_table_from_index
+  (struct dwarf2_per_objfile *dwarf2_per_objfile,
+   struct dwarf2_section_info *section,
+   const gdb_byte *bytes,
+   offset_type elements)
 {
-  offset_type i;
-  htab_t sig_types_hash;
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
 
-  dwarf2_per_objfile->n_type_units
-    = dwarf2_per_objfile->n_allocated_type_units
-    = elements / 3;
-  dwarf2_per_objfile->all_type_units =
-    XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
+  gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
+  dwarf2_per_objfile->all_type_units.reserve (elements / 3);
 
-  sig_types_hash = allocate_signatured_type_table (objfile);
+  htab_t sig_types_hash = allocate_signatured_type_table (objfile);
 
-  for (i = 0; i < elements; i += 3)
+  for (offset_type i = 0; i < elements; i += 3)
     {
       struct signatured_type *sig_type;
       ULONGEST signature;
@@ -3291,7 +3063,7 @@ create_signatured_type_table_from_index (struct objfile *objfile,
       slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
       *slot = sig_type;
 
-      dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
+      dwarf2_per_objfile->all_type_units.push_back (sig_type);
     }
 
   dwarf2_per_objfile->signatured_types = sig_types_hash;
@@ -3301,28 +3073,25 @@ create_signatured_type_table_from_index (struct objfile *objfile,
 
 static void
 create_signatured_type_table_from_debug_names
-  (struct objfile *objfile,
+  (struct dwarf2_per_objfile *dwarf2_per_objfile,
    const mapped_debug_names &map,
    struct dwarf2_section_info *section,
    struct dwarf2_section_info *abbrev_section)
 {
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
+
   dwarf2_read_section (objfile, section);
   dwarf2_read_section (objfile, abbrev_section);
 
-  dwarf2_per_objfile->n_type_units
-    = dwarf2_per_objfile->n_allocated_type_units
-    = map.tu_count;
-  dwarf2_per_objfile->all_type_units
-    = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
+  gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
+  dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
 
   htab_t sig_types_hash = allocate_signatured_type_table (objfile);
 
   for (uint32_t i = 0; i < map.tu_count; ++i)
     {
       struct signatured_type *sig_type;
-      ULONGEST signature;
       void **slot;
-      cu_offset type_offset_in_tu;
 
       sect_offset sect_off
        = (sect_offset) (extract_unsigned_integer
@@ -3331,7 +3100,8 @@ create_signatured_type_table_from_debug_names
                          map.dwarf5_byte_order));
 
       comp_unit_head cu_header;
-      read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
+      read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
+                                    abbrev_section,
                                     section->buffer + to_underlying (sect_off),
                                     rcuh_kind::TYPE);
 
@@ -3350,7 +3120,7 @@ create_signatured_type_table_from_debug_names
       slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
       *slot = sig_type;
 
-      dwarf2_per_objfile->all_type_units[i] = sig_type;
+      dwarf2_per_objfile->all_type_units.push_back (sig_type);
     }
 
   dwarf2_per_objfile->signatured_types = sig_types_hash;
@@ -3360,8 +3130,10 @@ create_signatured_type_table_from_debug_names
    populate the objfile's psymtabs_addrmap.  */
 
 static void
-create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
+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);
   const gdb_byte *iter, *end;
   struct addrmap *mutable_map;
@@ -3388,23 +3160,22 @@ create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
 
       if (lo > hi)
        {
-         complaint (&symfile_complaints,
-                    _(".gdb_index address table has invalid range (%s - %s)"),
+         complaint (_(".gdb_index address table has invalid range (%s - %s)"),
                     hex_string (lo), hex_string (hi));
          continue;
        }
 
-      if (cu_index >= dwarf2_per_objfile->n_comp_units)
+      if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
        {
-         complaint (&symfile_complaints,
-                    _(".gdb_index address table has invalid CU number %u"),
+         complaint (_(".gdb_index address table has invalid CU number %u"),
                     (unsigned) cu_index);
          continue;
        }
 
       lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
       hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
-      addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
+      addrmap_set_empty (mutable_map, lo, hi - 1,
+                        dwarf2_per_objfile->get_cu (cu_index));
     }
 
   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
@@ -3415,9 +3186,10 @@ create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
    populate the objfile's psymtabs_addrmap.  */
 
 static void
-create_addrmap_from_aranges (struct objfile *objfile,
+create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
                             struct dwarf2_section_info *section)
 {
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
   bfd *abfd = objfile->obfd;
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
   const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
@@ -3430,16 +3202,15 @@ create_addrmap_from_aranges (struct objfile *objfile,
                     dwarf2_per_cu_data *,
                     gdb::hash_enum<sect_offset>>
     debug_info_offset_to_per_cu;
-  for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
+  for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
     {
-      dwarf2_per_cu_data *per_cu = dw2_get_cutu (cui);
       const auto insertpair
        = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
       if (!insertpair.second)
        {
          warning (_("Section .debug_aranges in %s has duplicate "
-                    "debug_info_offset %u, ignoring .debug_aranges."),
-                  objfile_name (objfile), to_underlying (per_cu->sect_off));
+                    "debug_info_offset %s, ignoring .debug_aranges."),
+                  objfile_name (objfile), sect_offset_str (per_cu->sect_off));
          return;
        }
     }
@@ -3570,31 +3341,6 @@ create_addrmap_from_aranges (struct objfile *objfile,
                                                    &objfile->objfile_obstack);
 }
 
-/* The hash function for strings in the mapped index.  This is the same as
-   SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
-   implementation.  This is necessary because the hash function is tied to the
-   format of the mapped index file.  The hash values do not have to match with
-   SYMBOL_HASH_NEXT.
-   
-   Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
-
-static hashval_t
-mapped_index_string_hash (int index_version, const void *p)
-{
-  const unsigned char *str = (const unsigned char *) p;
-  hashval_t r = 0;
-  unsigned char c;
-
-  while ((c = *str++) != 0)
-    {
-      if (index_version >= 5)
-       c = tolower (c);
-      r = r * 67 + c - 113;
-    }
-
-  return r;
-}
-
 /* Find a slot in the mapped index INDEX for the object named NAME.
    If NAME is found, set *VEC_OUT to point to the CU vector in the
    constant pool and return true.  If NAME cannot be found, return
@@ -3659,7 +3405,7 @@ find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
 
 /* A helper function that reads the .gdb_index from SECTION and fills
    in MAP.  FILENAME is the name of the file containing the section;
-   it is used for error reporting.  DEPRECATED_OK is nonzero if it is
+   it is used for error reporting.  DEPRECATED_OK is true if it is
    ok to use deprecated sections.
 
    CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
@@ -3668,10 +3414,10 @@ find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
 
    Returns 1 if all went well, 0 otherwise.  */
 
-static int
+static bool
 read_index_from_section (struct objfile *objfile,
                         const char *filename,
-                        int deprecated_ok,
+                        bool deprecated_ok,
                         struct dwarf2_section_info *section,
                         struct mapped_index *map,
                         const gdb_byte **cu_list,
@@ -3748,7 +3494,6 @@ to use the section anyway."),
     return 0;
 
   map->version = version;
-  map->total_size = section->size;
 
   metadata = (offset_type *) (addr + sizeof (offset_type));
 
@@ -3787,27 +3532,28 @@ to use the section anyway."),
    elements of all the CUs and return 1.  Otherwise, return 0.  */
 
 static int
-dwarf2_read_index (struct objfile *objfile)
+dwarf2_read_index (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
-  struct mapped_index local_map, *map;
   const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
   offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
   struct dwz_file *dwz;
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
 
+  std::unique_ptr<struct mapped_index> map (new struct mapped_index);
   if (!read_index_from_section (objfile, objfile_name (objfile),
                                use_deprecated_index_sections,
-                               &dwarf2_per_objfile->gdb_index, &local_map,
+                               &dwarf2_per_objfile->gdb_index, map.get (),
                                &cu_list, &cu_list_elements,
                                &types_list, &types_list_elements))
     return 0;
 
   /* Don't use the index if it's empty.  */
-  if (local_map.symbol_table.empty ())
+  if (map->symbol_table.empty ())
     return 0;
 
   /* If there is a .dwz file, read it so we can get its CU list as
      well.  */
-  dwz = dwarf2_get_dwz_file ();
+  dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
   if (dwz != NULL)
     {
       struct mapped_index dwz_map;
@@ -3827,8 +3573,8 @@ dwarf2_read_index (struct objfile *objfile)
        }
     }
 
-  create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
-                        dwz_list_elements);
+  create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
+                        dwz_list, dwz_list_elements);
 
   if (types_list_elements)
     {
@@ -3842,35 +3588,20 @@ dwarf2_read_index (struct objfile *objfile)
       section = VEC_index (dwarf2_section_info_def,
                           dwarf2_per_objfile->types, 0);
 
-      create_signatured_type_table_from_index (objfile, section, types_list,
-                                              types_list_elements);
+      create_signatured_type_table_from_index (dwarf2_per_objfile, section,
+                                              types_list, types_list_elements);
     }
 
-  create_addrmap_from_index (objfile, &local_map);
+  create_addrmap_from_index (dwarf2_per_objfile, map.get ());
 
-  map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
-  map = new (map) mapped_index ();
-  *map = local_map;
-
-  dwarf2_per_objfile->index_table = map;
+  dwarf2_per_objfile->index_table = std::move (map);
   dwarf2_per_objfile->using_index = 1;
   dwarf2_per_objfile->quick_file_names_table =
-    create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
+    create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
 
   return 1;
 }
 
-/* A helper for the "quick" functions which sets the global
-   dwarf2_per_objfile according to OBJFILE.  */
-
-static void
-dw2_setup (struct objfile *objfile)
-{
-  dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
-                       objfile_data (objfile, dwarf2_objfile_data_key));
-  gdb_assert (dwarf2_per_objfile);
-}
-
 /* die_reader_func for dw2_get_file_names.  */
 
 static void
@@ -3881,7 +3612,9 @@ dw2_get_file_names_reader (const struct die_reader_specs *reader,
                           void *data)
 {
   struct dwarf2_cu *cu = reader->cu;
-  struct dwarf2_per_cu_data *this_cu = cu->per_cu;  
+  struct dwarf2_per_cu_data *this_cu = cu->per_cu;
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct dwarf2_per_cu_data *lh_cu;
   struct attribute *attr;
@@ -3994,14 +3727,14 @@ dw2_get_real_path (struct objfile *objfile,
 static struct symtab *
 dw2_find_last_source_symtab (struct objfile *objfile)
 {
-  struct compunit_symtab *cust;
-  int index;
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
+  dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
+  compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
 
-  dw2_setup (objfile);
-  index = dwarf2_per_objfile->n_comp_units - 1;
-  cust = dw2_instantiate_symtab (dw2_get_cutu (index));
   if (cust == NULL)
     return NULL;
+
   return compunit_primary_filetab (cust);
 }
 
@@ -4029,7 +3762,8 @@ dw2_free_cached_file_names (void **slot, void *info)
 static void
 dw2_forget_cached_source_info (struct objfile *objfile)
 {
-  dw2_setup (objfile);
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
 
   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
                          dw2_free_cached_file_names, NULL);
@@ -4052,7 +3786,7 @@ dw2_map_expand_apply (struct objfile *objfile,
 
   /* This may expand more than one symtab, and we want to iterate over
      all of them.  */
-  dw2_instantiate_symtab (per_cu);
+  dw2_instantiate_symtab (per_cu, false);
 
   return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
                                    last_made, callback);
@@ -4065,29 +3799,24 @@ dw2_map_symtabs_matching_filename
   (struct objfile *objfile, const char *name, const char *real_path,
    gdb::function_view<bool (symtab *)> callback)
 {
-  int i;
   const char *name_basename = lbasename (name);
-
-  dw2_setup (objfile);
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
 
   /* The rule is CUs specify all the files, including those used by
      any TU, so there's no need to scan TUs here.  */
 
-  for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
+  for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
     {
-      int j;
-      struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
-      struct quick_file_names *file_data;
-
       /* We only need to look at symtabs not already expanded.  */
       if (per_cu->v.quick->compunit_symtab)
        continue;
 
-      file_data = dw2_get_file_names (per_cu);
+      quick_file_names *file_data = dw2_get_file_names (per_cu);
       if (file_data == NULL)
        continue;
 
-      for (j = 0; j < file_data->num_file_names; ++j)
+      for (int j = 0; j < file_data->num_file_names; ++j)
        {
          const char *this_name = file_data->file_names[j];
          const char *this_real_name;
@@ -4138,8 +3867,8 @@ dw2_map_symtabs_matching_filename
 
 struct dw2_symtab_iterator
 {
-  /* The internalized form of .gdb_index.  */
-  struct mapped_index *index;
+  /* The dwarf2_per_objfile owning the CUs we are iterating on.  */
+  struct dwarf2_per_objfile *dwarf2_per_objfile;
   /* If non-zero, only look for symbols that match BLOCK_INDEX.  */
   int want_specific_block;
   /* One of GLOBAL_BLOCK or STATIC_BLOCK.
@@ -4167,20 +3896,23 @@ struct dw2_symtab_iterator
 
 static void
 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
-                     struct mapped_index *index,
+                     struct dwarf2_per_objfile *dwarf2_per_objfile,
                      int want_specific_block,
                      int block_index,
                      domain_enum domain,
                      const char *name)
 {
-  iter->index = index;
+  iter->dwarf2_per_objfile = dwarf2_per_objfile;
   iter->want_specific_block = want_specific_block;
   iter->block_index = block_index;
   iter->domain = domain;
   iter->next = 0;
   iter->global_seen = 0;
 
-  if (find_slot_in_mapped_hash (index, name, &iter->vec))
+  mapped_index *index = dwarf2_per_objfile->index_table.get ();
+
+  /* index is NULL if OBJF_READNOW.  */
+  if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
     iter->length = MAYBE_SWAP (*iter->vec);
   else
     {
@@ -4194,12 +3926,13 @@ dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
 static struct dwarf2_per_cu_data *
 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
+
   for ( ; iter->next < iter->length; ++iter->next)
     {
       offset_type cu_index_and_attrs =
        MAYBE_SWAP (iter->vec[iter->next + 1]);
       offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
-      struct dwarf2_per_cu_data *per_cu;
       int want_static = iter->block_index != GLOBAL_BLOCK;
       /* This value is only valid for index versions >= 7.  */
       int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
@@ -4210,21 +3943,20 @@ dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
         and indices >= 7 may elide them for certain symbols
         (gold does this).  */
       int attrs_valid =
-       (iter->index->version >= 7
+       (dwarf2_per_objfile->index_table->version >= 7
         && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
 
       /* Don't crash on bad data.  */
-      if (cu_index >= (dwarf2_per_objfile->n_comp_units
-                      + dwarf2_per_objfile->n_type_units))
+      if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
+                      + dwarf2_per_objfile->all_type_units.size ()))
        {
-         complaint (&symfile_complaints,
-                    _(".gdb_index entry has bad CU index"
+         complaint (_(".gdb_index entry has bad CU index"
                       " [in module %s]"),
                     objfile_name (dwarf2_per_objfile->objfile));
          continue;
        }
 
-      per_cu = dw2_get_cutu (cu_index);
+      dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
 
       /* Skip if already read in.  */
       if (per_cu->v.quick->compunit_symtab)
@@ -4280,46 +4012,39 @@ dw2_lookup_symbol (struct objfile *objfile, int block_index,
                   const char *name, domain_enum domain)
 {
   struct compunit_symtab *stab_best = NULL;
-  struct mapped_index *index;
-
-  dw2_setup (objfile);
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
 
   lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
 
-  index = dwarf2_per_objfile->index_table;
-
-  /* index is NULL if OBJF_READNOW.  */
-  if (index)
-    {
-      struct dw2_symtab_iterator iter;
-      struct dwarf2_per_cu_data *per_cu;
+  struct dw2_symtab_iterator iter;
+  struct dwarf2_per_cu_data *per_cu;
 
-      dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
+  dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
 
-      while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
-       {
-         struct symbol *sym, *with_opaque = NULL;
-         struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
-         const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
-         struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
+  while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
+    {
+      struct symbol *sym, *with_opaque = NULL;
+      struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
+      const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
+      struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
 
-         sym = block_find_symbol (block, name, domain,
-                                  block_find_non_opaque_type_preferred,
-                                  &with_opaque);
+      sym = block_find_symbol (block, name, domain,
+                              block_find_non_opaque_type_preferred,
+                              &with_opaque);
 
-         /* Some caution must be observed with overloaded functions
-            and methods, since the index will not contain any overload
-            information (but NAME might contain it).  */
+      /* Some caution must be observed with overloaded functions
+        and methods, since the index will not contain any overload
+        information (but NAME might contain it).  */
 
-         if (sym != NULL
-             && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
-           return stab;
-         if (with_opaque != NULL
-             && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
-           stab_best = stab;
+      if (sym != NULL
+         && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
+       return stab;
+      if (with_opaque != NULL
+         && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
+       stab_best = stab;
 
-         /* Keep looking through other CUs.  */
-       }
+      /* Keep looking through other CUs.  */
     }
 
   return stab_best;
@@ -4328,14 +4053,15 @@ dw2_lookup_symbol (struct objfile *objfile, int block_index,
 static void
 dw2_print_stats (struct objfile *objfile)
 {
-  int i, total, count;
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
+  int total = (dwarf2_per_objfile->all_comp_units.size ()
+              + dwarf2_per_objfile->all_type_units.size ());
+  int count = 0;
 
-  dw2_setup (objfile);
-  total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
-  count = 0;
-  for (i = 0; i < total; ++i)
+  for (int i = 0; i < total; ++i)
     {
-      struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
+      dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
 
       if (!per_cu->v.quick->compunit_symtab)
        ++count;
@@ -4352,7 +4078,9 @@ dw2_print_stats (struct objfile *objfile)
 static void
 dw2_dump (struct objfile *objfile)
 {
-  dw2_setup (objfile);
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
+
   gdb_assert (dwarf2_per_objfile->using_index);
   printf_filtered (".gdb_index:");
   if (dwarf2_per_objfile->index_table != NULL)
@@ -4377,40 +4105,39 @@ static void
 dw2_expand_symtabs_for_function (struct objfile *objfile,
                                 const char *func_name)
 {
-  struct mapped_index *index;
-
-  dw2_setup (objfile);
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
 
-  index = dwarf2_per_objfile->index_table;
+  struct dw2_symtab_iterator iter;
+  struct dwarf2_per_cu_data *per_cu;
 
-  /* index is NULL if OBJF_READNOW.  */
-  if (index)
-    {
-      struct dw2_symtab_iterator iter;
-      struct dwarf2_per_cu_data *per_cu;
+  /* Note: It doesn't matter what we pass for block_index here.  */
+  dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
+                       func_name);
 
-      /* Note: It doesn't matter what we pass for block_index here.  */
-      dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
-                           func_name);
+  while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
+    dw2_instantiate_symtab (per_cu, false);
 
-      while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
-       dw2_instantiate_symtab (per_cu);
-    }
 }
 
 static void
 dw2_expand_all_symtabs (struct objfile *objfile)
 {
-  int i;
-
-  dw2_setup (objfile);
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
+  int total_units = (dwarf2_per_objfile->all_comp_units.size ()
+                    + dwarf2_per_objfile->all_type_units.size ());
 
-  for (i = 0; i < (dwarf2_per_objfile->n_comp_units
-                  + dwarf2_per_objfile->n_type_units); ++i)
+  for (int i = 0; i < total_units; ++i)
     {
-      struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
+      dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
 
-      dw2_instantiate_symtab (per_cu);
+      /* We don't want to directly expand a partial CU, because if we
+        read it with the wrong language, then assertion failures can
+        be triggered later on.  See PR symtab/23010.  So, tell
+        dw2_instantiate_symtab to skip partial CUs -- any important
+        partial CU will be read via DW_TAG_imported_unit anyway.  */
+      dw2_instantiate_symtab (per_cu, true);
     }
 }
 
@@ -4418,36 +4145,31 @@ static void
 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
                                  const char *fullname)
 {
-  int i;
-
-  dw2_setup (objfile);
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
 
   /* We don't need to consider type units here.
      This is only called for examining code, e.g. expand_line_sal.
      There can be an order of magnitude (or more) more type units
      than comp units, and we avoid them if we can.  */
 
-  for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
+  for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
     {
-      int j;
-      struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
-      struct quick_file_names *file_data;
-
       /* We only need to look at symtabs not already expanded.  */
       if (per_cu->v.quick->compunit_symtab)
        continue;
 
-      file_data = dw2_get_file_names (per_cu);
+      quick_file_names *file_data = dw2_get_file_names (per_cu);
       if (file_data == NULL)
        continue;
 
-      for (j = 0; j < file_data->num_file_names; ++j)
+      for (int j = 0; j < file_data->num_file_names; ++j)
        {
          const char *this_fullname = file_data->file_names[j];
 
          if (filename_cmp (this_fullname, fullname) == 0)
            {
-             dw2_instantiate_symtab (per_cu);
+             dw2_instantiate_symtab (per_cu, false);
              break;
            }
        }
@@ -4527,23 +4249,21 @@ gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
   for (int i = 0; i < nr_languages; i++)
     {
       const language_defn *lang = language_def ((enum language) i);
-      if (lang->la_get_symbol_name_matcher != NULL)
-       {
-         symbol_name_matcher_ftype *name_matcher
-           = lang->la_get_symbol_name_matcher (m_lookup_name);
+      symbol_name_matcher_ftype *name_matcher
+       = get_symbol_name_matcher (lang, m_lookup_name);
 
-         /* Don't insert the same comparison routine more than once.
-            Note that we do this linear walk instead of a cheaper
-            sorted insert, or use a std::set or something like that,
-            because relative order of function addresses is not
-            stable.  This is not a problem in practice because the
-            number of supported languages is low, and the cost here
-            is tiny compared to the number of searches we'll do
-            afterwards using this object.  */
-         if (std::find (matchers.begin (), matchers.end (), name_matcher)
-             == matchers.end ())
-           matchers.push_back (name_matcher);
-       }
+      /* Don't insert the same comparison routine more than once.
+        Note that we do this linear walk instead of a seemingly
+        cheaper sorted insert, or use a std::set or something like
+        that, because relative order of function addresses is not
+        stable.  This is not a problem in practice because the number
+        of supported languages is low, and the cost here is tiny
+        compared to the number of searches we'll do afterwards using
+        this object.  */
+      if (name_matcher != default_symbol_name_matcher
+         && (std::find (matchers.begin (), matchers.end (), name_matcher)
+             == matchers.end ()))
+       matchers.push_back (name_matcher);
     }
 }
 
@@ -4850,13 +4570,13 @@ public:
   DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
 
   /* Return the number of names in the symbol table.  */
-  virtual size_t symbol_name_count () const
+  size_t symbol_name_count () const override
   {
     return m_symbol_table.size ();
   }
 
   /* Get the name of the symbol at IDX in the symbol table.  */
-  virtual const char *symbol_name_at (offset_type idx) const
+  const char *symbol_name_at (offset_type idx) const override
   {
     return m_symbol_table[idx];
   }
@@ -5273,7 +4993,7 @@ dw2_expand_symtabs_matching_one
       bool symtab_was_null
        = (per_cu->v.quick->compunit_symtab == NULL);
 
-      dw2_instantiate_symtab (per_cu);
+      dw2_instantiate_symtab (per_cu, false);
 
       if (expansion_notify != NULL
          && symtab_was_null
@@ -5288,21 +5008,20 @@ dw2_expand_symtabs_matching_one
 
 static void
 dw2_expand_marked_cus
-  (mapped_index &index, offset_type idx,
-   struct objfile *objfile,
+  (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
    gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
    gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
    search_domain kind)
 {
   offset_type *vec, vec_len, vec_idx;
   bool global_seen = false;
+  mapped_index &index = *dwarf2_per_objfile->index_table;
 
   vec = (offset_type *) (index.constant_pool
                         + MAYBE_SWAP (index.symbol_table[idx].vec));
   vec_len = MAYBE_SWAP (vec[0]);
   for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
     {
-      struct dwarf2_per_cu_data *per_cu;
       offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
       /* This value is only valid for index versions >= 7.  */
       int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
@@ -5349,16 +5068,16 @@ dw2_expand_marked_cus
        }
 
       /* Don't crash on bad data.  */
-      if (cu_index >= (dwarf2_per_objfile->n_comp_units
-                      + dwarf2_per_objfile->n_type_units))
+      if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
+                      + dwarf2_per_objfile->all_type_units.size ()))
        {
-         complaint (&symfile_complaints,
-                    _(".gdb_index entry has bad CU index"
-                      " [in module %s]"), objfile_name (objfile));
+         complaint (_(".gdb_index entry has bad CU index"
+                      " [in module %s]"),
+                      objfile_name (dwarf2_per_objfile->objfile));
          continue;
        }
 
-      per_cu = dw2_get_cutu (cu_index);
+      dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
       dw2_expand_symtabs_matching_one (per_cu, file_matcher,
                                       expansion_notify);
     }
@@ -5370,7 +5089,8 @@ dw2_expand_marked_cus
 
 static void
 dw_expand_symtabs_matching_file_matcher
-  (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
+  (struct dwarf2_per_objfile *dwarf2_per_objfile,
+   gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
 {
   if (file_matcher == NULL)
     return;
@@ -5387,13 +5107,8 @@ dw_expand_symtabs_matching_file_matcher
   /* The rule is CUs specify all the files, including those used by
      any TU, so there's no need to scan TUs here.  */
 
-  for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
+  for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
     {
-      int j;
-      struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
-      struct quick_file_names *file_data;
-      void **slot;
-
       QUIT;
 
       per_cu->v.quick->mark = 0;
@@ -5402,7 +5117,7 @@ dw_expand_symtabs_matching_file_matcher
       if (per_cu->v.quick->compunit_symtab)
        continue;
 
-      file_data = dw2_get_file_names (per_cu);
+      quick_file_names *file_data = dw2_get_file_names (per_cu);
       if (file_data == NULL)
        continue;
 
@@ -5414,7 +5129,7 @@ dw_expand_symtabs_matching_file_matcher
          continue;
        }
 
-      for (j = 0; j < file_data->num_file_names; ++j)
+      for (int j = 0; j < file_data->num_file_names; ++j)
        {
          const char *this_real_name;
 
@@ -5439,10 +5154,10 @@ dw_expand_symtabs_matching_file_matcher
            }
        }
 
-      slot = htab_find_slot (per_cu->v.quick->mark
-                            ? visited_found.get ()
-                            : visited_not_found.get (),
-                            file_data, INSERT);
+      void **slot = htab_find_slot (per_cu->v.quick->mark
+                                   ? visited_found.get ()
+                                   : visited_not_found.get (),
+                                   file_data, INSERT);
       *slot = file_data;
     }
 }
@@ -5456,13 +5171,14 @@ dw2_expand_symtabs_matching
    gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
    enum search_domain kind)
 {
-  dw2_setup (objfile);
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
 
   /* index_table is NULL if OBJF_READNOW.  */
   if (!dwarf2_per_objfile->index_table)
     return;
 
-  dw_expand_symtabs_matching_file_matcher (file_matcher);
+  dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
 
   mapped_index &index = *dwarf2_per_objfile->index_table;
 
@@ -5470,7 +5186,7 @@ dw2_expand_symtabs_matching
                                      symbol_matcher,
                                      kind, [&] (offset_type idx)
     {
-      dw2_expand_marked_cus (index, idx, objfile, file_matcher,
+      dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
                             expansion_notify, kind);
     });
 }
@@ -5513,8 +5229,6 @@ dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
   struct dwarf2_per_cu_data *data;
   struct compunit_symtab *result;
 
-  dw2_setup (objfile);
-
   if (!objfile->psymtabs_addrmap)
     return NULL;
 
@@ -5528,7 +5242,8 @@ dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
             paddress (get_objfile_arch (objfile), pc));
 
   result
-    = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
+    = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
+                                                                       false),
                                                pc);
   gdb_assert (result != NULL);
   return result;
@@ -5538,7 +5253,8 @@ static void
 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
                          void *data, int need_fullname)
 {
-  dw2_setup (objfile);
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
 
   if (!dwarf2_per_objfile->filenames_cache)
     {
@@ -5552,10 +5268,8 @@ dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
         by any TU, so there's no need to scan TUs here.  We can
         ignore file names coming from already-expanded CUs.  */
 
-      for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
+      for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
        {
-         struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
-
          if (per_cu->v.quick->compunit_symtab)
            {
              void **slot = htab_find_slot (visited.get (),
@@ -5566,21 +5280,17 @@ dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
            }
        }
 
-      for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
+      for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
        {
-         struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
-         struct quick_file_names *file_data;
-         void **slot;
-
          /* We only need to look at symtabs not already expanded.  */
          if (per_cu->v.quick->compunit_symtab)
            continue;
 
-         file_data = dw2_get_file_names (per_cu);
+         quick_file_names *file_data = dw2_get_file_names (per_cu);
          if (file_data == NULL)
            continue;
 
-         slot = htab_find_slot (visited.get (), file_data, INSERT);
+         void **slot = htab_find_slot (visited.get (), file_data, INSERT);
          if (*slot)
            {
              /* Already visited.  */
@@ -5825,10 +5535,10 @@ read_debug_names_from_section (struct objfile *objfile,
    list.  */
 
 static void
-create_cus_from_debug_names_list (struct objfile *objfile,
+create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
                                  const mapped_debug_names &map,
                                  dwarf2_section_info &section,
-                                 bool is_dwz, int base_offset)
+                                 bool is_dwz)
 {
   sect_offset sect_off_prev;
   for (uint32_t i = 0; i <= map.cu_count; ++i)
@@ -5847,61 +5557,61 @@ create_cus_from_debug_names_list (struct objfile *objfile,
       if (i >= 1)
        {
          const ULONGEST length = sect_off_next - sect_off_prev;
-         dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
-           = create_cu_from_index_list (objfile, &section, is_dwz,
+         dwarf2_per_cu_data *per_cu
+           = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
                                         sect_off_prev, length);
+         dwarf2_per_objfile->all_comp_units.push_back (per_cu);
        }
       sect_off_prev = sect_off_next;
     }
 }
 
 /* Read the CU list from the mapped index, and use it to create all
-   the CU objects for this objfile.  */
+   the CU objects for this dwarf2_per_objfile.  */
 
 static void
-create_cus_from_debug_names (struct objfile *objfile,
+create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
                             const mapped_debug_names &map,
                             const mapped_debug_names &dwz_map)
 {
+  gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
+  dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
 
-  dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
-  dwarf2_per_objfile->all_comp_units
-    = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
-                dwarf2_per_objfile->n_comp_units);
-
-  create_cus_from_debug_names_list (objfile, map, dwarf2_per_objfile->info,
-                                   false /* is_dwz */,
-                                   0 /* base_offset */);
+  create_cus_from_debug_names_list (dwarf2_per_objfile, map,
+                                   dwarf2_per_objfile->info,
+                                   false /* is_dwz */);
 
   if (dwz_map.cu_count == 0)
     return;
 
-  dwz_file *dwz = dwarf2_get_dwz_file ();
-  create_cus_from_debug_names_list (objfile, dwz_map, dwz->info,
-                                   true /* is_dwz */,
-                                   map.cu_count /* base_offset */);
+  dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
+  create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
+                                   true /* is_dwz */);
 }
 
 /* Read .debug_names.  If everything went ok, initialize the "quick"
    elements of all the CUs and return true.  Otherwise, return false.  */
 
 static bool
-dwarf2_read_debug_names (struct objfile *objfile)
+dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
-  mapped_debug_names local_map, dwz_map;
+  std::unique_ptr<mapped_debug_names> map
+    (new mapped_debug_names (dwarf2_per_objfile));
+  mapped_debug_names dwz_map (dwarf2_per_objfile);
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
 
   if (!read_debug_names_from_section (objfile, objfile_name (objfile),
                                      &dwarf2_per_objfile->debug_names,
-                                     local_map))
+                                     *map))
     return false;
 
   /* Don't use the index if it's empty.  */
-  if (local_map.name_count == 0)
+  if (map->name_count == 0)
     return false;
 
   /* If there is a .dwz file, read it so we can get its CU list as
      well.  */
-  dwz_file *dwz = dwarf2_get_dwz_file ();
+  dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
   if (dwz != NULL)
     {
       if (!read_debug_names_from_section (objfile,
@@ -5914,9 +5624,9 @@ dwarf2_read_debug_names (struct objfile *objfile)
        }
     }
 
-  create_cus_from_debug_names (objfile, local_map, dwz_map);
+  create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
 
-  if (local_map.tu_count != 0)
+  if (map->tu_count != 0)
     {
       /* We can only handle a single .debug_types when we have an
         index.  */
@@ -5927,36 +5637,20 @@ dwarf2_read_debug_names (struct objfile *objfile)
                                                dwarf2_per_objfile->types, 0);
 
       create_signatured_type_table_from_debug_names
-       (objfile, local_map, section, &dwarf2_per_objfile->abbrev);
+       (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
     }
 
-  create_addrmap_from_aranges (objfile, &dwarf2_per_objfile->debug_aranges);
+  create_addrmap_from_aranges (dwarf2_per_objfile,
+                              &dwarf2_per_objfile->debug_aranges);
 
-  dwarf2_per_objfile->debug_names_table.reset (new mapped_debug_names);
-  *dwarf2_per_objfile->debug_names_table = std::move (local_map);
+  dwarf2_per_objfile->debug_names_table = std::move (map);
   dwarf2_per_objfile->using_index = 1;
   dwarf2_per_objfile->quick_file_names_table =
-    create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
+    create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
 
   return true;
 }
 
-/* Symbol name hashing function as specified by DWARF-5.  */
-
-static uint32_t
-dwarf5_djb_hash (const char *str_)
-{
-  const unsigned char *str = (const unsigned char *) str_;
-
-  /* Note: tolower here ignores UTF-8, which isn't fully compliant.
-     See http://dwarfstd.org/ShowIssue.php?issue=161027.1.  */
-
-  uint32_t hash = 5381;
-  while (int c = *str++)
-    hash = hash * 33 + tolower (c);
-  return hash;
-}
-
 /* Type used to manage iterating over all CUs looking for a symbol for
    .debug_names.  */
 
@@ -6019,7 +5713,7 @@ mapped_debug_names::namei_to_name (uint32_t namei) const
                                offset_size,
                                dwarf5_byte_order);
   return read_indirect_string_at_offset
-    (dwarf2_per_objfile->objfile->obfd, namei_string_offs);
+    (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
 }
 
 /* Find a slot in .debug_names for the object named NAME.  If NAME is
@@ -6064,11 +5758,10 @@ dw2_debug_names_iterator::find_vec_in_debug_names
   --namei;
   if (namei >= map.name_count)
     {
-      complaint (&symfile_complaints,
-                _("Wrong .debug_names with name index %u but name_count=%u "
+      complaint (_("Wrong .debug_names with name index %u but name_count=%u "
                   "[in module %s]"),
                 namei, map.name_count,
-                objfile_name (dwarf2_per_objfile->objfile));
+                objfile_name (map.dwarf2_per_objfile->objfile));
       return NULL;
     }
 
@@ -6088,8 +5781,7 @@ dw2_debug_names_iterator::find_vec_in_debug_names
 #if 0 /* An expensive sanity check.  */
          if (namei_full_hash != dwarf5_djb_hash (namei_string))
            {
-             complaint (&symfile_complaints,
-                        _("Wrong .debug_names hash for string at index %u "
+             complaint (_("Wrong .debug_names hash for string at index %u "
                           "[in module %s]"),
                         namei, objfile_name (dwarf2_per_objfile->objfile));
              return NULL;
@@ -6118,11 +5810,10 @@ dw2_debug_names_iterator::find_vec_in_debug_names
 {
   if (namei >= map.name_count)
     {
-      complaint (&symfile_complaints,
-                _("Wrong .debug_names with name index %u but name_count=%u "
+      complaint (_("Wrong .debug_names with name index %u but name_count=%u "
                   "[in module %s]"),
                 namei, map.name_count,
-                objfile_name (dwarf2_per_objfile->objfile));
+                objfile_name (map.dwarf2_per_objfile->objfile));
       return NULL;
     }
 
@@ -6141,7 +5832,9 @@ dw2_debug_names_iterator::next ()
   if (m_addr == NULL)
     return NULL;
 
-  bfd *const abfd = dwarf2_per_objfile->objfile->obfd;
+  struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
+  bfd *const abfd = objfile->obfd;
 
  again:
 
@@ -6154,10 +5847,9 @@ dw2_debug_names_iterator::next ()
   const auto indexval_it = m_map.abbrev_map.find (abbrev);
   if (indexval_it == m_map.abbrev_map.cend ())
     {
-      complaint (&symfile_complaints,
-                _("Wrong .debug_names undefined abbrev code %s "
+      complaint (_("Wrong .debug_names undefined abbrev code %s "
                   "[in module %s]"),
-                pulongest (abbrev), objfile_name (dwarf2_per_objfile->objfile));
+                pulongest (abbrev), objfile_name (objfile));
       return NULL;
     }
   const mapped_debug_names::index_val &indexval = indexval_it->second;
@@ -6180,39 +5872,36 @@ dw2_debug_names_iterator::next ()
          m_addr += bytes_read;
          break;
        default:
-         complaint (&symfile_complaints,
-                    _("Unsupported .debug_names form %s [in module %s]"),
+         complaint (_("Unsupported .debug_names form %s [in module %s]"),
                     dwarf_form_name (attr.form),
-                    objfile_name (dwarf2_per_objfile->objfile));
+                    objfile_name (objfile));
          return NULL;
        }
       switch (attr.dw_idx)
        {
        case DW_IDX_compile_unit:
          /* Don't crash on bad data.  */
-         if (ull >= dwarf2_per_objfile->n_comp_units)
+         if (ull >= dwarf2_per_objfile->all_comp_units.size ())
            {
-             complaint (&symfile_complaints,
-                        _(".debug_names entry has bad CU index %s"
+             complaint (_(".debug_names entry has bad CU index %s"
                           " [in module %s]"),
                         pulongest (ull),
                         objfile_name (dwarf2_per_objfile->objfile));
              continue;
            }
-         per_cu = dw2_get_cutu (ull);
+         per_cu = dwarf2_per_objfile->get_cutu (ull);
          break;
        case DW_IDX_type_unit:
          /* Don't crash on bad data.  */
-         if (ull >= dwarf2_per_objfile->n_type_units)
+         if (ull >= dwarf2_per_objfile->all_type_units.size ())
            {
-             complaint (&symfile_complaints,
-                        _(".debug_names entry has bad TU index %s"
+             complaint (_(".debug_names entry has bad TU index %s"
                           " [in module %s]"),
                         pulongest (ull),
                         objfile_name (dwarf2_per_objfile->objfile));
              continue;
            }
-         per_cu = dw2_get_cutu (dwarf2_per_objfile->n_comp_units + ull);
+         per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
          break;
        case DW_IDX_GNU_internal:
          if (!m_map.augmentation_is_gdb)
@@ -6326,7 +6015,8 @@ dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
                               const char *name, domain_enum domain)
 {
   const block_enum block_index = static_cast<block_enum> (block_index_int);
-  dw2_setup (objfile);
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
 
   const auto &mapp = dwarf2_per_objfile->debug_names_table;
   if (!mapp)
@@ -6344,7 +6034,7 @@ dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
   while ((per_cu = iter.next ()) != NULL)
     {
       struct symbol *sym, *with_opaque = NULL;
-      struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
+      struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
       const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
       struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
 
@@ -6376,7 +6066,9 @@ dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
 static void
 dw2_debug_names_dump (struct objfile *objfile)
 {
-  dw2_setup (objfile);
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
+
   gdb_assert (dwarf2_per_objfile->using_index);
   printf_filtered (".debug_names:");
   if (dwarf2_per_objfile->debug_names_table)
@@ -6390,7 +6082,8 @@ static void
 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
                                             const char *func_name)
 {
-  dw2_setup (objfile);
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
 
   /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW.  */
   if (dwarf2_per_objfile->debug_names_table)
@@ -6403,7 +6096,7 @@ dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
 
       struct dwarf2_per_cu_data *per_cu;
       while ((per_cu = iter.next ()) != NULL)
-       dw2_instantiate_symtab (per_cu);
+       dw2_instantiate_symtab (per_cu, false);
     }
 }
 
@@ -6416,13 +6109,14 @@ dw2_debug_names_expand_symtabs_matching
    gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
    enum search_domain kind)
 {
-  dw2_setup (objfile);
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
 
   /* debug_names_table is NULL if OBJF_READNOW.  */
   if (!dwarf2_per_objfile->debug_names_table)
     return;
 
-  dw_expand_symtabs_matching_file_matcher (file_matcher);
+  dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
 
   mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
 
@@ -6466,24 +6160,26 @@ const struct quick_symbol_functions dwarf2_debug_names_functions =
 bool
 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
+
   /* If we're about to read full symbols, don't bother with the
      indices.  In this case we also don't care if some other debug
      format is making psymtabs, because they are all about to be
      expanded anyway.  */
   if ((objfile->flags & OBJF_READNOW))
     {
-      int i;
-
       dwarf2_per_objfile->using_index = 1;
-      create_all_comp_units (objfile);
-      create_all_type_units (objfile);
-      dwarf2_per_objfile->quick_file_names_table =
-       create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
+      create_all_comp_units (dwarf2_per_objfile);
+      create_all_type_units (dwarf2_per_objfile);
+      dwarf2_per_objfile->quick_file_names_table
+       = create_quick_file_names_table
+           (dwarf2_per_objfile->all_comp_units.size ());
 
-      for (i = 0; i < (dwarf2_per_objfile->n_comp_units
-                      + dwarf2_per_objfile->n_type_units); ++i)
+      for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
+                          + dwarf2_per_objfile->all_type_units.size ()); ++i)
        {
-         struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
+         dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
 
          per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
                                            struct dwarf2_per_cu_quick_data);
@@ -6496,13 +6192,13 @@ dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
       return true;
     }
 
-  if (dwarf2_read_debug_names (objfile))
+  if (dwarf2_read_debug_names (dwarf2_per_objfile))
     {
       *index_kind = dw_index_kind::DEBUG_NAMES;
       return true;
     }
 
-  if (dwarf2_read_index (objfile))
+  if (dwarf2_read_index (dwarf2_per_objfile))
     {
       *index_kind = dw_index_kind::GDB_INDEX;
       return true;
@@ -6518,6 +6214,8 @@ dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
 void
 dwarf2_build_psymtabs (struct objfile *objfile)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
 
   if (objfile->global_psymbols.capacity () == 0
       && objfile->static_psymbols.capacity () == 0)
@@ -6529,7 +6227,7 @@ dwarf2_build_psymtabs (struct objfile *objfile)
         objfile's obstack is still uselessly kept around.  However,
         freeing it seems unsafe.  */
       psymtab_discarder psymtabs (objfile);
-      dwarf2_build_psymtabs_hard (objfile);
+      dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
       psymtabs.keep ();
     }
   CATCH (except, RETURN_MASK_ERROR)
@@ -6690,9 +6388,10 @@ static struct dwarf2_section_info *
 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
 {
   struct dwarf2_section_info *abbrev;
+  struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
 
   if (this_cu->is_dwz)
-    abbrev = &dwarf2_get_dwz_file ()->abbrev;
+    abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
   else
     abbrev = &dwarf2_per_objfile->abbrev;
 
@@ -6704,7 +6403,8 @@ get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
    Perform various error checking on the header.  */
 
 static void
-error_check_comp_unit_head (struct comp_unit_head *header,
+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)
 {
@@ -6717,10 +6417,10 @@ error_check_comp_unit_head (struct comp_unit_head *header,
 
   if (to_underlying (header->abbrev_sect_off)
       >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
-    error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
-          "(offset 0x%x + 6) [in module %s]"),
-          to_underlying (header->abbrev_sect_off),
-          to_underlying (header->sect_off),
+    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
@@ -6728,8 +6428,8 @@ error_check_comp_unit_head (struct comp_unit_head *header,
   if (((ULONGEST) header->sect_off + get_cu_length (header))
       > section->size)
     error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
-          "(offset 0x%x + 0) [in module %s]"),
-          header->length, to_underlying (header->sect_off),
+          "(offset %s + 0) [in module %s]"),
+          header->length, sect_offset_str (header->sect_off),
           filename);
 }
 
@@ -6738,7 +6438,8 @@ error_check_comp_unit_head (struct comp_unit_head *header,
    The result is a pointer to the start of the first DIE.  */
 
 static const gdb_byte *
-read_and_check_comp_unit_head (struct comp_unit_head *header,
+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,
@@ -6752,7 +6453,8 @@ read_and_check_comp_unit_head (struct comp_unit_head *header,
 
   header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
 
-  error_check_comp_unit_head (header, section, abbrev_section);
+  error_check_comp_unit_head (dwarf2_per_objfile, header, section,
+                             abbrev_section);
 
   return info_ptr;
 }
@@ -6760,7 +6462,8 @@ read_and_check_comp_unit_head (struct comp_unit_head *header,
 /* Fetch the abbreviation table offset from a comp or type unit header.  */
 
 static sect_offset
-read_abbrev_offset (struct dwarf2_section_info *section,
+read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                   struct dwarf2_section_info *section,
                    sect_offset sect_off)
 {
   bfd *abfd = get_section_bfd_owner (section);
@@ -6883,10 +6586,10 @@ static int
 add_signatured_type_cu_to_table (void **slot, void *datum)
 {
   struct signatured_type *sigt = (struct signatured_type *) *slot;
-  struct signatured_type ***datap = (struct signatured_type ***) datum;
+  std::vector<signatured_type *> *all_type_units
+    = (std::vector<signatured_type *> *) datum;
 
-  **datap = sigt;
-  ++*datap;
+  all_type_units->push_back (sigt);
 
   return 1;
 }
@@ -6896,7 +6599,8 @@ add_signatured_type_cu_to_table (void **slot, void *datum)
    therefore DW_UT_type.  */
 
 static void
-create_debug_type_hash_table (struct dwo_file *dwo_file,
+create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                             struct dwo_file *dwo_file,
                              dwarf2_section_info *section, htab_t &types_htab,
                              rcuh_kind section_kind)
 {
@@ -6947,7 +6651,7 @@ create_debug_type_hash_table (struct dwo_file *dwo_file,
       /* We need to read the type's signature in order to build the hash
         table, but we don't need anything else just yet.  */
 
-      ptr = read_and_check_comp_unit_head (&header, section,
+      ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
                                           abbrev_section, ptr, section_kind);
 
       length = get_cu_length (&header);
@@ -7020,17 +6724,16 @@ create_debug_type_hash_table (struct dwo_file *dwo_file,
              dup_sect_off = dup_tu->per_cu.sect_off;
            }
 
-         complaint (&symfile_complaints,
-                    _("debug type entry at offset 0x%x is duplicate to"
-                      " the entry at offset 0x%x, signature %s"),
-                    to_underlying (sect_off), to_underlying (dup_sect_off),
+         complaint (_("debug type entry at offset %s is duplicate to"
+                      " the entry at offset %s, signature %s"),
+                    sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
                     hex_string (header.signature));
        }
       *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
 
       if (dwarf_read_debug > 1)
-       fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature %s\n",
-                           to_underlying (sect_off),
+       fprintf_unfiltered (gdb_stdlog, "  offset %s, signature %s\n",
+                           sect_offset_str (sect_off),
                            hex_string (header.signature));
 
       info_ptr += length;
@@ -7047,7 +6750,8 @@ create_debug_type_hash_table (struct dwo_file *dwo_file,
    Note: This function processes DWO files only, not DWP files.  */
 
 static void
-create_debug_types_hash_table (struct dwo_file *dwo_file,
+create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                              struct dwo_file *dwo_file,
                               VEC (dwarf2_section_info_def) *types,
                               htab_t &types_htab)
 {
@@ -7060,8 +6764,8 @@ create_debug_types_hash_table (struct dwo_file *dwo_file,
   for (ix = 0;
        VEC_iterate (dwarf2_section_info_def, types, ix, section);
        ++ix)
-    create_debug_type_hash_table (dwo_file, section, types_htab,
-                                 rcuh_kind::TYPE);
+    create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
+                                 types_htab, rcuh_kind::TYPE);
 }
 
 /* Create the hash table of all entries in the .debug_types section,
@@ -7070,14 +6774,15 @@ create_debug_types_hash_table (struct dwo_file *dwo_file,
    otherwise non-zero. */
 
 static int
-create_all_type_units (struct objfile *objfile)
+create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
   htab_t types_htab = NULL;
-  struct signatured_type **iter;
 
-  create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
+  create_debug_type_hash_table (dwarf2_per_objfile, NULL,
+                               &dwarf2_per_objfile->info, types_htab,
                                rcuh_kind::COMPILE);
-  create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
+  create_debug_types_hash_table (dwarf2_per_objfile, NULL,
+                                dwarf2_per_objfile->types, types_htab);
   if (types_htab == NULL)
     {
       dwarf2_per_objfile->signatured_types = NULL;
@@ -7086,15 +6791,11 @@ create_all_type_units (struct objfile *objfile)
 
   dwarf2_per_objfile->signatured_types = types_htab;
 
-  dwarf2_per_objfile->n_type_units
-    = dwarf2_per_objfile->n_allocated_type_units
-    = htab_elements (types_htab);
-  dwarf2_per_objfile->all_type_units =
-    XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
-  iter = &dwarf2_per_objfile->all_type_units[0];
-  htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
-  gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
-             == dwarf2_per_objfile->n_type_units);
+  gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
+  dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
+
+  htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
+                         &dwarf2_per_objfile->all_type_units);
 
   return 1;
 }
@@ -7104,30 +6805,19 @@ create_all_type_units (struct objfile *objfile)
    Otherwise we find one.  */
 
 static struct signatured_type *
-add_type_unit (ULONGEST sig, void **slot)
+add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
+              void **slot)
 {
   struct objfile *objfile = dwarf2_per_objfile->objfile;
-  int n_type_units = dwarf2_per_objfile->n_type_units;
-  struct signatured_type *sig_type;
 
-  gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
-  ++n_type_units;
-  if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
-    {
-      if (dwarf2_per_objfile->n_allocated_type_units == 0)
-       dwarf2_per_objfile->n_allocated_type_units = 1;
-      dwarf2_per_objfile->n_allocated_type_units *= 2;
-      dwarf2_per_objfile->all_type_units
-       = XRESIZEVEC (struct signatured_type *,
-                     dwarf2_per_objfile->all_type_units,
-                     dwarf2_per_objfile->n_allocated_type_units);
-      ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
-    }
-  dwarf2_per_objfile->n_type_units = n_type_units;
+  if (dwarf2_per_objfile->all_type_units.size ()
+      == dwarf2_per_objfile->all_type_units.capacity ())
+    ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
+
+  signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
+                                             struct signatured_type);
 
-  sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
-                            struct signatured_type);
-  dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
+  dwarf2_per_objfile->all_type_units.push_back (sig_type);
   sig_type->signature = sig;
   sig_type->per_cu.is_debug_types = 1;
   if (dwarf2_per_objfile->using_index)
@@ -7152,7 +6842,7 @@ add_type_unit (ULONGEST sig, void **slot)
    Fill in SIG_ENTRY with DWO_ENTRY.  */
 
 static void
-fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
+fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
                                  struct signatured_type *sig_entry,
                                  struct dwo_unit *dwo_entry)
 {
@@ -7195,6 +6885,8 @@ fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
 static struct signatured_type *
 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;
@@ -7248,9 +6940,9 @@ lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
 
   /* If the global table doesn't have an entry for this TU, add one.  */
   if (sig_entry == NULL)
-    sig_entry = add_type_unit (sig, slot);
+    sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
 
-  fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
+  fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
   sig_entry->per_cu.tu_read = 1;
   return sig_entry;
 }
@@ -7263,8 +6955,10 @@ lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
 static struct signatured_type *
 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 ();
+  struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
   struct dwo_unit *dwo_entry;
   struct signatured_type find_sig_entry, *sig_entry;
   void **slot;
@@ -7293,13 +6987,13 @@ lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
 
   if (dwp_file->tus == NULL)
     return NULL;
-  dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
+  dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
                                      sig, 1 /* is_debug_types */);
   if (dwo_entry == NULL)
     return NULL;
 
-  sig_entry = add_type_unit (sig, slot);
-  fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
+  sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
+  fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
 
   return sig_entry;
 }
@@ -7311,12 +7005,15 @@ lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
 static struct signatured_type *
 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
+
   if (cu->dwo_unit
       && dwarf2_per_objfile->using_index)
     {
       /* We're in a DWO/DWP file, and we're using .gdb_index.
         These cases require special processing.  */
-      if (get_dwp_file () == NULL)
+      if (get_dwp_file (dwarf2_per_objfile) == NULL)
        return lookup_dwo_signatured_type (cu, sig);
       else
        return lookup_dwp_signatured_type (cu, sig);
@@ -7342,7 +7039,8 @@ static void
 init_cu_die_reader (struct die_reader_specs *reader,
                    struct dwarf2_cu *cu,
                    struct dwarf2_section_info *section,
-                   struct dwo_file *dwo_file)
+                   struct dwo_file *dwo_file,
+                   struct abbrev_table *abbrev_table)
 {
   gdb_assert (section->readin && section->buffer != NULL);
   reader->abfd = get_section_bfd_owner (section);
@@ -7352,6 +7050,7 @@ init_cu_die_reader (struct die_reader_specs *reader,
   reader->buffer = section->buffer;
   reader->buffer_end = section->buffer + section->size;
   reader->comp_dir = NULL;
+  reader->abbrev_table = abbrev_table;
 }
 
 /* Subroutine of init_cutu_and_read_dies to simplify it.
@@ -7367,24 +7066,26 @@ init_cu_die_reader (struct die_reader_specs *reader,
    STUB_COMP_DIR may be non-NULL.
    *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
    are filled in with the info of the DIE from the DWO file.
-   ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
-   provided an abbrev table to use.
+   *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
+   from the dwo.  Since *RESULT_READER references this abbrev table, it must be
+   kept around for at least as long as *RESULT_READER.
+
    The result is non-zero if a valid (non-dummy) DIE was found.  */
 
 static int
 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
                        struct dwo_unit *dwo_unit,
-                       int abbrev_table_provided,
                        struct die_info *stub_comp_unit_die,
                        const char *stub_comp_dir,
                        struct die_reader_specs *result_reader,
                        const gdb_byte **result_info_ptr,
                        struct die_info **result_comp_unit_die,
-                       int *result_has_children)
+                       int *result_has_children,
+                       abbrev_table_up *result_dwo_abbrev_table)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct dwarf2_cu *cu = this_cu->cu;
-  struct dwarf2_section_info *section;
   bfd *abfd;
   const gdb_byte *begin_info_ptr, *info_ptr;
   struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
@@ -7447,29 +7148,29 @@ read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
 
   /* Set up for reading the DWO CU/TU.  */
   cu->dwo_unit = dwo_unit;
-  section = dwo_unit->section;
+  dwarf2_section_info *section = dwo_unit->section;
   dwarf2_read_section (objfile, section);
   abfd = get_section_bfd_owner (section);
   begin_info_ptr = info_ptr = (section->buffer
                               + to_underlying (dwo_unit->sect_off));
   dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
-  init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
 
   if (this_cu->is_debug_types)
     {
       struct signatured_type *sig_type = (struct signatured_type *) this_cu;
 
-      info_ptr = read_and_check_comp_unit_head (&cu->header, section,
+      info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
+                                               &cu->header, section,
                                                dwo_abbrev_section,
                                                info_ptr, rcuh_kind::TYPE);
       /* This is not an assert because it can be caused by bad debug info.  */
       if (sig_type->signature != cu->header.signature)
        {
          error (_("Dwarf Error: signature mismatch %s vs %s while reading"
-                  " TU at offset 0x%x [in module %s]"),
+                  " TU at offset %s [in module %s]"),
                 hex_string (sig_type->signature),
                 hex_string (cu->header.signature),
-                to_underlying (dwo_unit->sect_off),
+                sect_offset_str (dwo_unit->sect_off),
                 bfd_get_filename (abfd));
        }
       gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
@@ -7485,7 +7186,8 @@ read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
     }
   else
     {
-      info_ptr = read_and_check_comp_unit_head (&cu->header, section,
+      info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
+                                               &cu->header, section,
                                                dwo_abbrev_section,
                                                info_ptr, rcuh_kind::COMPILE);
       gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
@@ -7494,22 +7196,11 @@ read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
       dwo_unit->length = get_cu_length (&cu->header);
     }
 
-  /* Replace the CU's original abbrev table with the DWO's.
-     Reminder: We can't read the abbrev table until we've read the header.  */
-  if (abbrev_table_provided)
-    {
-      /* Don't free the provided abbrev table, the caller of
-        init_cutu_and_read_dies owns it.  */
-      dwarf2_read_abbrevs (cu, dwo_abbrev_section);
-      /* Ensure the DWO abbrev table gets freed.  */
-      make_cleanup (dwarf2_free_abbrev_table, cu);
-    }
-  else
-    {
-      dwarf2_free_abbrev_table (cu);
-      dwarf2_read_abbrevs (cu, dwo_abbrev_section);
-      /* Leave any existing abbrev table cleanup as is.  */
-    }
+  *result_dwo_abbrev_table
+    = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
+                              cu->header.abbrev_sect_off);
+  init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
+                     result_dwo_abbrev_table->get ());
 
   /* Read in the die, but leave space to copy over the attributes
      from the stub.  This has the benefit of simplifying the rest of
@@ -7612,12 +7303,7 @@ lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
 
 /* Subroutine of init_cutu_and_read_dies to simplify it.
    See it for a description of the parameters.
-   Read a TU directly from a DWO file, bypassing the stub.
-
-   Note: This function could be a little bit simpler if we shared cleanups
-   with our caller, init_cutu_and_read_dies.  That's generally a fragile thing
-   to do, so we keep this function self-contained.  Or we could move this
-   into our caller, but it's complex enough already.  */
+   Read a TU directly from a DWO file, bypassing the stub.  */
 
 static void
 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
@@ -7625,13 +7311,13 @@ init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
                           die_reader_func_ftype *die_reader_func,
                           void *data)
 {
-  struct dwarf2_cu *cu;
+  std::unique_ptr<dwarf2_cu> new_cu;
   struct signatured_type *sig_type;
-  struct cleanup *cleanups, *free_cu_cleanup = NULL;
   struct die_reader_specs reader;
   const gdb_byte *info_ptr;
   struct die_info *comp_unit_die;
   int has_children;
+  struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
 
   /* Verify we can do the following downcast, and that we have the
      data we need.  */
@@ -7639,12 +7325,9 @@ init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
   sig_type = (struct signatured_type *) this_cu;
   gdb_assert (sig_type->dwo_unit != NULL);
 
-  cleanups = make_cleanup (null_cleanup, NULL);
-
   if (use_existing_cu && this_cu->cu != NULL)
     {
       gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
-      cu = this_cu->cu;
       /* There's no need to do the rereading_dwo_cu handling that
         init_cutu_and_read_dies does since we don't read the stub.  */
     }
@@ -7652,25 +7335,25 @@ init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
     {
       /* If !use_existing_cu, this_cu->cu must be NULL.  */
       gdb_assert (this_cu->cu == NULL);
-      cu = XNEW (struct dwarf2_cu);
-      init_one_comp_unit (cu, this_cu);
-      /* If an error occurs while loading, release our storage.  */
-      free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
+      new_cu.reset (new dwarf2_cu (this_cu));
     }
 
   /* A future optimization, if needed, would be to use an existing
      abbrev table.  When reading DWOs with skeletonless TUs, all the TUs
      could share abbrev tables.  */
 
+  /* The abbreviation table used by READER, this must live at least as long as
+     READER.  */
+  abbrev_table_up dwo_abbrev_table;
+
   if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
-                             0 /* abbrev_table_provided */,
                              NULL /* stub_comp_unit_die */,
                              sig_type->dwo_unit->dwo_file->comp_dir,
                              &reader, &info_ptr,
-                             &comp_unit_die, &has_children) == 0)
+                             &comp_unit_die, &has_children,
+                             &dwo_abbrev_table) == 0)
     {
       /* Dummy die.  */
-      do_cleanups (cleanups);
       return;
     }
 
@@ -7681,27 +7364,14 @@ init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
      but the alternative is making the latter more complex.
      This function is only for the special case of using DWO files directly:
      no point in overly complicating the general case just to handle this.  */
-  if (free_cu_cleanup != NULL)
+  if (new_cu != NULL && keep)
     {
-      if (keep)
-       {
-         /* We've successfully allocated this compilation unit.  Let our
-            caller clean it up when finished with it.  */
-         discard_cleanups (free_cu_cleanup);
-
-         /* We can only discard free_cu_cleanup and all subsequent cleanups.
-            So we have to manually free the abbrev table.  */
-         dwarf2_free_abbrev_table (cu);
-
-         /* Link this CU into read_in_chain.  */
-         this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
-         dwarf2_per_objfile->read_in_chain = this_cu;
-       }
-      else
-       do_cleanups (free_cu_cleanup);
+      /* Link this CU into read_in_chain.  */
+      this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
+      dwarf2_per_objfile->read_in_chain = this_cu;
+      /* The chain owns it now.  */
+      new_cu.release ();
     }
-
-  do_cleanups (cleanups);
 }
 
 /* Initialize a CU (or TU) and read its DIEs.
@@ -7724,9 +7394,11 @@ static void
 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
                         struct abbrev_table *abbrev_table,
                         int use_existing_cu, int keep,
+                        bool skip_partial,
                         die_reader_func_ftype *die_reader_func,
                         void *data)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct dwarf2_section_info *section = this_cu->section;
   bfd *abfd = get_section_bfd_owner (section);
@@ -7736,7 +7408,6 @@ init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
   struct die_info *comp_unit_die;
   int has_children;
   struct attribute *attr;
-  struct cleanup *cleanups, *free_cu_cleanup = NULL;
   struct signatured_type *sig_type = NULL;
   struct dwarf2_section_info *abbrev_section;
   /* Non-zero if CU currently points to a DWO file and we need to
@@ -7745,9 +7416,9 @@ init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
   int rereading_dwo_cu = 0;
 
   if (dwarf_die_debug)
-    fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
+    fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
                        this_cu->is_debug_types ? "type" : "comp",
-                       to_underlying (this_cu->sect_off));
+                       sect_offset_str (this_cu->sect_off));
 
   if (use_existing_cu)
     gdb_assert (keep);
@@ -7764,8 +7435,6 @@ init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
       return;
     }
 
-  cleanups = make_cleanup (null_cleanup, NULL);
-
   /* This is cheap if the section is already read in.  */
   dwarf2_read_section (objfile, section);
 
@@ -7773,6 +7442,7 @@ init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
 
   abbrev_section = get_abbrev_section_for_cu (this_cu);
 
+  std::unique_ptr<dwarf2_cu> new_cu;
   if (use_existing_cu && this_cu->cu != NULL)
     {
       cu = this_cu->cu;
@@ -7789,10 +7459,8 @@ init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
     {
       /* If !use_existing_cu, this_cu->cu must be NULL.  */
       gdb_assert (this_cu->cu == NULL);
-      cu = XNEW (struct dwarf2_cu);
-      init_one_comp_unit (cu, this_cu);
-      /* If an error occurs while loading, release our storage.  */
-      free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
+      new_cu.reset (new dwarf2_cu (this_cu));
+      cu = new_cu.get ();
     }
 
   /* Get the header.  */
@@ -7805,7 +7473,8 @@ init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
     {
       if (this_cu->is_debug_types)
        {
-         info_ptr = read_and_check_comp_unit_head (&cu->header, section,
+         info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
+                                                   &cu->header, section,
                                                    abbrev_section, info_ptr,
                                                    rcuh_kind::TYPE);
 
@@ -7829,7 +7498,8 @@ init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
        }
       else
        {
-         info_ptr = read_and_check_comp_unit_head (&cu->header, section,
+         info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
+                                                   &cu->header, section,
                                                    abbrev_section,
                                                    info_ptr,
                                                    rcuh_kind::COMPILE);
@@ -7843,42 +7513,39 @@ init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
   /* Skip dummy compilation units.  */
   if (info_ptr >= begin_info_ptr + this_cu->length
       || peek_abbrev_code (abfd, info_ptr) == 0)
-    {
-      do_cleanups (cleanups);
-      return;
-    }
+    return;
 
   /* If we don't have them yet, read the abbrevs for this compilation unit.
      And if we need to read them now, make sure they're freed when we're
-     done.  Note that it's important that if the CU had an abbrev table
-     on entry we don't free it when we're done: Somewhere up the call stack
-     it may be in use.  */
+     done (own the table through ABBREV_TABLE_HOLDER).  */
+  abbrev_table_up abbrev_table_holder;
   if (abbrev_table != NULL)
+    gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
+  else
     {
-      gdb_assert (cu->abbrev_table == NULL);
-      gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
-      cu->abbrev_table = abbrev_table;
-    }
-  else if (cu->abbrev_table == NULL)
-    {
-      dwarf2_read_abbrevs (cu, abbrev_section);
-      make_cleanup (dwarf2_free_abbrev_table, cu);
-    }
-  else if (rereading_dwo_cu)
-    {
-      dwarf2_free_abbrev_table (cu);
-      dwarf2_read_abbrevs (cu, abbrev_section);
+      abbrev_table_holder
+       = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
+                                  cu->header.abbrev_sect_off);
+      abbrev_table = abbrev_table_holder.get ();
     }
 
   /* Read the top level CU/TU die.  */
-  init_cu_die_reader (&reader, cu, section, NULL);
+  init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
 
+  if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
+    return;
+
   /* If we are in a DWO stub, process it and then read in the "real" CU/TU
-     from the DWO file.
+     from the DWO file.  read_cutu_die_from_dwo will allocate the abbreviation
+     table from the DWO file and pass the ownership over to us.  It will be
+     referenced from READER, so we must make sure to free it after we're done
+     with READER.
+
      Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
      DWO CU, that this test will fail (the attribute will not be present).  */
   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
+  abbrev_table_up dwo_abbrev_table;
   if (attr)
     {
       struct dwo_unit *dwo_unit;
@@ -7886,22 +7553,21 @@ init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
 
       if (has_children)
        {
-         complaint (&symfile_complaints,
-                    _("compilation unit with DW_AT_GNU_dwo_name"
-                      " has children (offset 0x%x) [in module %s]"),
-                    to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
+         complaint (_("compilation unit with DW_AT_GNU_dwo_name"
+                      " has children (offset %s) [in module %s]"),
+                    sect_offset_str (this_cu->sect_off),
+                    bfd_get_filename (abfd));
        }
       dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
       if (dwo_unit != NULL)
        {
          if (read_cutu_die_from_dwo (this_cu, dwo_unit,
-                                     abbrev_table != NULL,
                                      comp_unit_die, NULL,
                                      &reader, &info_ptr,
-                                     &dwo_comp_unit_die, &has_children) == 0)
+                                     &dwo_comp_unit_die, &has_children,
+                                     &dwo_abbrev_table) == 0)
            {
              /* Dummy die.  */
-             do_cleanups (cleanups);
              return;
            }
          comp_unit_die = dwo_comp_unit_die;
@@ -7920,27 +7586,14 @@ init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
 
   /* Done, clean up.  */
-  if (free_cu_cleanup != NULL)
+  if (new_cu != NULL && keep)
     {
-      if (keep)
-       {
-         /* We've successfully allocated this compilation unit.  Let our
-            caller clean it up when finished with it.  */
-         discard_cleanups (free_cu_cleanup);
-
-         /* We can only discard free_cu_cleanup and all subsequent cleanups.
-            So we have to manually free the abbrev table.  */
-         dwarf2_free_abbrev_table (cu);
-
-         /* Link this CU into read_in_chain.  */
-         this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
-         dwarf2_per_objfile->read_in_chain = this_cu;
-       }
-      else
-       do_cleanups (free_cu_cleanup);
+      /* Link this CU into read_in_chain.  */
+      this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
+      dwarf2_per_objfile->read_in_chain = this_cu;
+      /* The chain owns it now.  */
+      new_cu.release ();
     }
-
-  do_cleanups (cleanups);
 }
 
 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
@@ -7965,21 +7618,20 @@ init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
                                   die_reader_func_ftype *die_reader_func,
                                   void *data)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct dwarf2_section_info *section = this_cu->section;
   bfd *abfd = get_section_bfd_owner (section);
   struct dwarf2_section_info *abbrev_section;
-  struct dwarf2_cu cu;
   const gdb_byte *begin_info_ptr, *info_ptr;
   struct die_reader_specs reader;
-  struct cleanup *cleanups;
   struct die_info *comp_unit_die;
   int has_children;
 
   if (dwarf_die_debug)
-    fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
+    fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
                        this_cu->is_debug_types ? "type" : "comp",
-                       to_underlying (this_cu->sect_off));
+                       sect_offset_str (this_cu->sect_off));
 
   gdb_assert (this_cu->cu == NULL);
 
@@ -7990,12 +7642,11 @@ init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
   /* This is cheap if the section is already read in.  */
   dwarf2_read_section (objfile, section);
 
-  init_one_comp_unit (&cu, this_cu);
-
-  cleanups = make_cleanup (free_stack_comp_unit, &cu);
+  struct dwarf2_cu cu (this_cu);
 
   begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
-  info_ptr = read_and_check_comp_unit_head (&cu.header, section,
+  info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
+                                           &cu.header, section,
                                            abbrev_section, info_ptr,
                                            (this_cu->is_debug_types
                                             ? rcuh_kind::TYPE
@@ -8006,20 +7657,16 @@ init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
   /* Skip dummy compilation units.  */
   if (info_ptr >= begin_info_ptr + this_cu->length
       || peek_abbrev_code (abfd, info_ptr) == 0)
-    {
-      do_cleanups (cleanups);
-      return;
-    }
+    return;
 
-  dwarf2_read_abbrevs (&cu, abbrev_section);
-  make_cleanup (dwarf2_free_abbrev_table, &cu);
+  abbrev_table_up abbrev_table
+    = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
+                              cu.header.abbrev_sect_off);
 
-  init_cu_die_reader (&reader, &cu, section, dwo_file);
+  init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
 
   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
-
-  do_cleanups (cleanups);
 }
 
 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
@@ -8068,13 +7715,13 @@ eq_type_unit_group (const void *item_lhs, const void *item_rhs)
 /* Allocate a hash table for type unit groups.  */
 
 static htab_t
-allocate_type_unit_groups_table (void)
+allocate_type_unit_groups_table (struct objfile *objfile)
 {
   return htab_create_alloc_ex (3,
                               hash_type_unit_group,
                               eq_type_unit_group,
                               NULL,
-                              &dwarf2_per_objfile->objfile->objfile_obstack,
+                              &objfile->objfile_obstack,
                               hashtab_obstack_allocate,
                               dummy_obstack_deallocate);
 }
@@ -8091,6 +7738,8 @@ allocate_type_unit_groups_table (void)
 static struct type_unit_group *
 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct dwarf2_per_cu_data *per_cu;
   struct type_unit_group *tu_group;
@@ -8098,7 +7747,7 @@ create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
   tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
                             struct type_unit_group);
   per_cu = &tu_group->per_cu;
-  per_cu->dwarf2_per_objfile = cu->dwarf2_per_objfile;
+  per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
 
   if (dwarf2_per_objfile->using_index)
     {
@@ -8136,6 +7785,8 @@ create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
 static struct type_unit_group *
 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
   struct type_unit_group *tu_group;
   void **slot;
@@ -8145,7 +7796,7 @@ get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
   if (dwarf2_per_objfile->type_unit_groups == NULL)
     {
       dwarf2_per_objfile->type_unit_groups =
-       allocate_type_unit_groups_table ();
+       allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
     }
 
   /* Do we need to create a new group, or can we use an existing one?  */
@@ -8240,7 +7891,7 @@ process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
                                  void *data)
 {
   struct dwarf2_cu *cu = reader->cu;
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
   CORE_ADDR baseaddr;
@@ -8351,10 +8002,10 @@ process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
       struct gdbarch *gdbarch = get_objfile_arch (objfile);
 
       fprintf_unfiltered (gdb_stdlog,
-                         "Psymtab for %s unit @0x%x: %s - %s"
+                         "Psymtab for %s unit @%s: %s - %s"
                          ", %d global, %d static syms\n",
                          per_cu->is_debug_types ? "type" : "comp",
-                         to_underlying (per_cu->sect_off),
+                         sect_offset_str (per_cu->sect_off),
                          paddress (gdbarch, pst->textlow),
                          paddress (gdbarch, pst->texthigh),
                          pst->n_global_syms, pst->n_static_syms);
@@ -8378,19 +8029,19 @@ process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
     free_one_cached_comp_unit (this_cu);
 
   if (this_cu->is_debug_types)
-    init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
-                            NULL);
+    init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
+                            build_type_psymtabs_reader, NULL);
   else
     {
       process_psymtab_comp_unit_data info;
       info.want_partial_unit = want_partial_unit;
       info.pretend_language = pretend_language;
-      init_cutu_and_read_dies (this_cu, NULL, 0, 0,
+      init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
                               process_psymtab_comp_unit_reader, &info);
     }
 
   /* Age out any secondary CUs.  */
-  age_cached_comp_units ();
+  age_cached_comp_units (this_cu->dwarf2_per_objfile);
 }
 
 /* Reader function for build_type_psymtabs.  */
@@ -8402,6 +8053,8 @@ build_type_psymtabs_reader (const struct die_reader_specs *reader,
                            int has_children,
                            void *data)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = reader->cu->per_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct dwarf2_cu *cu = reader->cu;
   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
@@ -8442,23 +8095,21 @@ build_type_psymtabs_reader (const struct die_reader_specs *reader,
 
 struct tu_abbrev_offset
 {
-  struct signatured_type *sig_type;
+  tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
+  : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
+  {}
+
+  signatured_type *sig_type;
   sect_offset abbrev_offset;
 };
 
-/* Helper routine for build_type_psymtabs_1, passed to qsort.  */
+/* Helper routine for build_type_psymtabs_1, passed to std::sort.  */
 
-static int
-sort_tu_by_abbrev_offset (const void *ap, const void *bp)
+static bool
+sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
+                         const struct tu_abbrev_offset &b)
 {
-  const struct tu_abbrev_offset * const *a
-    = (const struct tu_abbrev_offset * const*) ap;
-  const struct tu_abbrev_offset * const *b
-    = (const struct tu_abbrev_offset * const*) bp;
-  sect_offset aoff = (*a)->abbrev_offset;
-  sect_offset boff = (*b)->abbrev_offset;
-
-  return (aoff > boff) - (aoff < boff);
+  return a.abbrev_offset < b.abbrev_offset;
 }
 
 /* Efficiently read all the type units.
@@ -8480,19 +8131,16 @@ sort_tu_by_abbrev_offset (const void *ap, const void *bp)
    dwarf2_per_objfile->type_unit_groups.  */
 
 static void
-build_type_psymtabs_1 (void)
+build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
-  struct cleanup *cleanups;
-  struct abbrev_table *abbrev_table;
+  abbrev_table_up abbrev_table;
   sect_offset abbrev_offset;
-  struct tu_abbrev_offset *sorted_by_abbrev;
-  int i;
 
   /* It's up to the caller to not call us multiple times.  */
   gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
 
-  if (dwarf2_per_objfile->n_type_units == 0)
+  if (dwarf2_per_objfile->all_type_units.empty ())
     return;
 
   /* TUs typically share abbrev tables, and there can be way more TUs than
@@ -8519,64 +8167,49 @@ build_type_psymtabs_1 (void)
 
   /* Sort in a separate table to maintain the order of all_type_units
      for .gdb_index: TU indices directly index all_type_units.  */
-  sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
-                             dwarf2_per_objfile->n_type_units);
-  for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
-    {
-      struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
+  std::vector<tu_abbrev_offset> sorted_by_abbrev;
+  sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
 
-      sorted_by_abbrev[i].sig_type = sig_type;
-      sorted_by_abbrev[i].abbrev_offset =
-       read_abbrev_offset (sig_type->per_cu.section,
-                           sig_type->per_cu.sect_off);
-    }
-  cleanups = make_cleanup (xfree, sorted_by_abbrev);
-  qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
-        sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
+  for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
+    sorted_by_abbrev.emplace_back
+      (sig_type, read_abbrev_offset (dwarf2_per_objfile,
+                                    sig_type->per_cu.section,
+                                    sig_type->per_cu.sect_off));
+
+  std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
+            sort_tu_by_abbrev_offset);
 
   abbrev_offset = (sect_offset) ~(unsigned) 0;
-  abbrev_table = NULL;
-  make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
 
-  for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
+  for (const tu_abbrev_offset &tu : sorted_by_abbrev)
     {
-      const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
-
       /* Switch to the next abbrev table if necessary.  */
       if (abbrev_table == NULL
-         || tu->abbrev_offset != abbrev_offset)
+         || tu.abbrev_offset != abbrev_offset)
        {
-         if (abbrev_table != NULL)
-           {
-             abbrev_table_free (abbrev_table);
-             /* Reset to NULL in case abbrev_table_read_table throws
-                an error: abbrev_table_free_cleanup will get called.  */
-             abbrev_table = NULL;
-           }
-         abbrev_offset = tu->abbrev_offset;
+         abbrev_offset = tu.abbrev_offset;
          abbrev_table =
-           abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
+           abbrev_table_read_table (dwarf2_per_objfile,
+                                    &dwarf2_per_objfile->abbrev,
                                     abbrev_offset);
          ++tu_stats->nr_uniq_abbrev_tables;
        }
 
-      init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
-                              build_type_psymtabs_reader, NULL);
+      init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
+                              0, 0, false, build_type_psymtabs_reader, NULL);
     }
-
-  do_cleanups (cleanups);
 }
 
 /* Print collected type unit statistics.  */
 
 static void
-print_tu_stats (void)
+print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
 
   fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
-  fprintf_unfiltered (gdb_stdlog, "  %d TUs\n",
-                     dwarf2_per_objfile->n_type_units);
+  fprintf_unfiltered (gdb_stdlog, "  %zu TUs\n",
+                     dwarf2_per_objfile->all_type_units.size ());
   fprintf_unfiltered (gdb_stdlog, "  %d uniq abbrev tables\n",
                      tu_stats->nr_uniq_abbrev_tables);
   fprintf_unfiltered (gdb_stdlog, "  %d symtabs from stmt_list entries\n",
@@ -8594,6 +8227,8 @@ print_tu_stats (void)
 static int
 build_type_psymtab_dependencies (void **slot, void *info)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = (struct dwarf2_per_objfile *) info;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
   struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
@@ -8626,12 +8261,12 @@ build_type_psymtab_dependencies (void **slot, void *info)
    Build partial symbol tables for the .debug_types comp-units.  */
 
 static void
-build_type_psymtabs (struct objfile *objfile)
+build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
-  if (! create_all_type_units (objfile))
+  if (! create_all_type_units (dwarf2_per_objfile))
     return;
 
-  build_type_psymtabs_1 ();
+  build_type_psymtabs_1 (dwarf2_per_objfile);
 }
 
 /* Traversal function for process_skeletonless_type_unit.
@@ -8641,7 +8276,8 @@ static int
 process_skeletonless_type_unit (void **slot, void *info)
 {
   struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
-  struct objfile *objfile = (struct objfile *) info;
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = (struct dwarf2_per_objfile *) info;
   struct signatured_type find_entry, *entry;
 
   /* If this TU doesn't exist in the global table, add it and read it in.  */
@@ -8649,7 +8285,7 @@ process_skeletonless_type_unit (void **slot, void *info)
   if (dwarf2_per_objfile->signatured_types == NULL)
     {
       dwarf2_per_objfile->signatured_types
-       = allocate_signatured_type_table (objfile);
+       = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
     }
 
   find_entry.signature = dwo_unit->signature;
@@ -8662,12 +8298,12 @@ process_skeletonless_type_unit (void **slot, void *info)
 
   /* This does the job that create_all_type_units would have done for
      this TU.  */
-  entry = add_type_unit (dwo_unit->signature, slot);
-  fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
+  entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
+  fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
   *slot = entry;
 
   /* This does the job that build_type_psymtabs_1 would have done.  */
-  init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
+  init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
                           build_type_psymtabs_reader, NULL);
 
   return 1;
@@ -8694,35 +8330,31 @@ process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
    Note: This can't be done until we know what all the DWO files are.  */
 
 static void
-process_skeletonless_type_units (struct objfile *objfile)
+process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
   /* Skeletonless TUs in DWP files without .gdb_index is not supported yet.  */
-  if (get_dwp_file () == NULL
+  if (get_dwp_file (dwarf2_per_objfile) == NULL
       && dwarf2_per_objfile->dwo_files != NULL)
     {
       htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
                              process_dwo_file_for_skeletonless_type_units,
-                             objfile);
+                             dwarf2_per_objfile);
     }
 }
 
-/* Compute the 'user' field for each psymtab in OBJFILE.  */
+/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE.  */
 
 static void
-set_partial_user (struct objfile *objfile)
+set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
-  int i;
-
-  for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
+  for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
     {
-      struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
       struct partial_symtab *pst = per_cu->v.psymtab;
-      int j;
 
       if (pst == NULL)
        continue;
 
-      for (j = 0; j < pst->number_of_dependencies; ++j)
+      for (int j = 0; j < pst->number_of_dependencies; ++j)
        {
          /* Set the 'user' field only if it is not already set.  */
          if (pst->dependencies[j]->user == NULL)
@@ -8735,10 +8367,9 @@ set_partial_user (struct objfile *objfile)
    .debug_info and .debug_abbrev sections.  */
 
 static void
-dwarf2_build_psymtabs_hard (struct objfile *objfile)
+dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
-  struct cleanup *back_to;
-  int i;
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
 
   if (dwarf_read_debug)
     {
@@ -8752,11 +8383,11 @@ dwarf2_build_psymtabs_hard (struct objfile *objfile)
 
   /* Any cached compilation units will be linked by the per-objfile
      read_in_chain.  Make sure to free them when we're done.  */
-  back_to = make_cleanup (free_cached_comp_units, NULL);
+  free_cached_comp_units freer (dwarf2_per_objfile);
 
-  build_type_psymtabs (objfile);
+  build_type_psymtabs (dwarf2_per_objfile);
 
-  create_all_comp_units (objfile);
+  create_all_comp_units (dwarf2_per_objfile);
 
   /* Create a temporary address map on a temporary obstack.  We later
      copy this to the final obstack.  */
@@ -8766,35 +8397,29 @@ dwarf2_build_psymtabs_hard (struct objfile *objfile)
     = make_scoped_restore (&objfile->psymtabs_addrmap,
                           addrmap_create_mutable (&temp_obstack));
 
-  for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
-    {
-      struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
-
-      process_psymtab_comp_unit (per_cu, 0, language_minimal);
-    }
+  for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
+    process_psymtab_comp_unit (per_cu, 0, language_minimal);
 
   /* This has to wait until we read the CUs, we need the list of DWOs.  */
-  process_skeletonless_type_units (objfile);
+  process_skeletonless_type_units (dwarf2_per_objfile);
 
   /* Now that all TUs have been processed we can fill in the dependencies.  */
   if (dwarf2_per_objfile->type_unit_groups != NULL)
     {
       htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
-                             build_type_psymtab_dependencies, NULL);
+                             build_type_psymtab_dependencies, dwarf2_per_objfile);
     }
 
   if (dwarf_read_debug)
-    print_tu_stats ();
+    print_tu_stats (dwarf2_per_objfile);
 
-  set_partial_user (objfile);
+  set_partial_user (dwarf2_per_objfile);
 
   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
                                                    &objfile->objfile_obstack);
   /* At this point we want to keep the address map.  */
   save_psymtabs_addrmap.release ();
 
-  do_cleanups (back_to);
-
   if (dwarf_read_debug)
     fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
                        objfile_name (objfile));
@@ -8826,20 +8451,18 @@ load_partial_comp_unit_reader (const struct die_reader_specs *reader,
 static void
 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
 {
-  init_cutu_and_read_dies (this_cu, NULL, 1, 1,
+  init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
                           load_partial_comp_unit_reader, NULL);
 }
 
 static void
-read_comp_units_from_section (struct objfile *objfile,
+read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
                              struct dwarf2_section_info *section,
                              struct dwarf2_section_info *abbrev_section,
-                             unsigned int is_dwz,
-                             int *n_allocated,
-                             int *n_comp_units,
-                             struct dwarf2_per_cu_data ***all_comp_units)
+                             unsigned int is_dwz)
 {
   const gdb_byte *info_ptr;
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
 
   if (dwarf_read_debug)
     fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
@@ -8857,8 +8480,9 @@ read_comp_units_from_section (struct objfile *objfile,
       sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
 
       comp_unit_head cu_header;
-      read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
-                                    info_ptr, rcuh_kind::COMPILE);
+      read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
+                                    abbrev_section, info_ptr,
+                                    rcuh_kind::COMPILE);
 
       /* Save the compilation unit for later lookup.  */
       if (cu_header.unit_type != DW_UT_type)
@@ -8883,14 +8507,7 @@ read_comp_units_from_section (struct objfile *objfile,
       this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
       this_cu->section = section;
 
-      if (*n_comp_units == *n_allocated)
-       {
-         *n_allocated *= 2;
-         *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
-                                       *all_comp_units, *n_allocated);
-       }
-      (*all_comp_units)[*n_comp_units] = this_cu;
-      ++*n_comp_units;
+      dwarf2_per_objfile->all_comp_units.push_back (this_cu);
 
       info_ptr = info_ptr + this_cu->length;
     }
@@ -8900,34 +8517,16 @@ read_comp_units_from_section (struct objfile *objfile,
    This is only done for -readnow and building partial symtabs.  */
 
 static void
-create_all_comp_units (struct objfile *objfile)
+create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
-  int n_allocated;
-  int n_comp_units;
-  struct dwarf2_per_cu_data **all_comp_units;
-  struct dwz_file *dwz;
-
-  n_comp_units = 0;
-  n_allocated = 10;
-  all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
+  gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
+  read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
+                               &dwarf2_per_objfile->abbrev, 0);
 
-  read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
-                               &dwarf2_per_objfile->abbrev, 0,
-                               &n_allocated, &n_comp_units, &all_comp_units);
-
-  dwz = dwarf2_get_dwz_file ();
+  dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
   if (dwz != NULL)
-    read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
-                                 &n_allocated, &n_comp_units,
-                                 &all_comp_units);
-
-  dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
-                                                 struct dwarf2_per_cu_data *,
-                                                 n_comp_units);
-  memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
-         n_comp_units * sizeof (struct dwarf2_per_cu_data *));
-  xfree (all_comp_units);
-  dwarf2_per_objfile->n_comp_units = n_comp_units;
+    read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
+                                 1);
 }
 
 /* Process all loaded DIEs for compilation unit CU, starting at
@@ -8951,7 +8550,7 @@ scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
 
   while (pdi != NULL)
     {
-      fixup_partial_die (pdi, cu);
+      pdi->fixup (cu);
 
       /* Anonymous namespaces or modules have no name but have interesting
         children, so we need to look at them.  Ditto for anonymous
@@ -8959,11 +8558,13 @@ scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
 
       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
          || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
-         || pdi->tag == DW_TAG_imported_unit)
+         || pdi->tag == DW_TAG_imported_unit
+         || pdi->tag == DW_TAG_inlined_subroutine)
        {
          switch (pdi->tag)
            {
            case DW_TAG_subprogram:
+           case DW_TAG_inlined_subroutine:
              add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
              break;
            case DW_TAG_constant:
@@ -8982,7 +8583,8 @@ scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
                {
                  add_partial_symbol (pdi, cu);
                }
-             if (cu->language == language_rust && pdi->has_children)
+             if ((cu->language == language_rust
+                  || cu->language == language_cplus) && pdi->has_children)
                scan_partial_symbols (pdi->die_child, lowpc, highpc,
                                      set_addrmap, cu);
              break;
@@ -9011,12 +8613,12 @@ scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
                  {
                    error (_("Dwarf Error: DW_TAG_imported_unit is not"
                             " supported in type units [in module %s]"),
-                          objfile_name (cu->dwarf2_per_objfile->objfile));
+                          objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
                  }
 
                per_cu = dwarf2_find_containing_comp_unit
                           (pdi->d.sect_off, pdi->is_dwz,
-                           cu->dwarf2_per_objfile->objfile);
+                           cu->per_cu->dwarf2_per_objfile);
 
                /* Go read the partial unit, if needed.  */
                if (per_cu->v.psymtab == NULL)
@@ -9085,7 +8687,7 @@ partial_die_parent_scope (struct partial_die_info *pdi,
   if (parent->scope_set)
     return parent->scope;
 
-  fixup_partial_die (parent, cu);
+  parent->fixup (cu);
 
   grandparent_scope = partial_die_parent_scope (parent, cu);
 
@@ -9125,9 +8727,8 @@ partial_die_parent_scope (struct partial_die_info *pdi,
       /* FIXME drow/2004-04-01: What should we be doing with
         function-local names?  For partial symbols, we should probably be
         ignoring them.  */
-      complaint (&symfile_complaints,
-                _("unhandled containing DIE tag %d for DIE at %d"),
-                parent->tag, to_underlying (pdi->sect_off));
+      complaint (_("unhandled containing DIE tag %d for DIE at %s"),
+                parent->tag, sect_offset_str (pdi->sect_off));
       parent->scope = grandparent_scope;
     }
 
@@ -9150,7 +8751,7 @@ partial_die_full_name (struct partial_die_info *pdi,
      types here will be reused if full symbols are loaded later.  */
   if (pdi->has_template_arguments)
     {
-      fixup_partial_die (pdi, cu);
+      pdi->fixup (cu);
 
       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
        {
@@ -9178,7 +8779,9 @@ partial_die_full_name (struct partial_die_info *pdi,
 static void
 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  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);
   CORE_ADDR addr = 0;
   const char *actual_name = NULL;
@@ -9196,6 +8799,7 @@ add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
 
   switch (pdi->tag)
     {
+    case DW_TAG_inlined_subroutine:
     case DW_TAG_subprogram:
       addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
       if (pdi->is_external || cu->language == language_ada)
@@ -9393,12 +8997,12 @@ add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
     scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
 }
 
-/* Read a partial die corresponding to a subprogram and create a partial
-   symbol for that subprogram.  When the CU language allows it, this
-   routine also defines a partial symbol for each nested subprogram
-   that this subprogram contains.  If SET_ADDRMAP is true, record the
-   covered ranges in the addrmap.  Set *LOWPC and *HIGHPC to the lowest
-   and highest PC values found in PDI.
+/* Read a partial die corresponding to a subprogram or an inlined
+   subprogram and create a partial symbol for that subprogram.
+   When the CU language allows it, this routine also defines a partial
+   symbol for each nested subprogram that this subprogram contains.
+   If SET_ADDRMAP is true, record the covered ranges in the addrmap.
+   Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
 
    PDI may also be a lexical block, in which case we simply search
    recursively for subprograms defined inside that lexical block.
@@ -9410,7 +9014,7 @@ add_partial_subprogram (struct partial_die_info *pdi,
                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
                        int set_addrmap, struct dwarf2_cu *cu)
 {
-  if (pdi->tag == DW_TAG_subprogram)
+  if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
     {
       if (pdi->has_pc_info)
         {
@@ -9420,7 +9024,7 @@ add_partial_subprogram (struct partial_die_info *pdi,
             *highpc = pdi->highpc;
          if (set_addrmap)
            {
-             struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+             struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
              struct gdbarch *gdbarch = get_objfile_arch (objfile);
              CORE_ADDR baseaddr;
              CORE_ADDR highpc;
@@ -9456,8 +9060,9 @@ add_partial_subprogram (struct partial_die_info *pdi,
       pdi = pdi->die_child;
       while (pdi != NULL)
        {
-         fixup_partial_die (pdi, cu);
+         pdi->fixup (cu);
          if (pdi->tag == DW_TAG_subprogram
+             || pdi->tag == DW_TAG_inlined_subroutine
              || pdi->tag == DW_TAG_lexical_block)
            add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
          pdi = pdi->die_sibling;
@@ -9480,7 +9085,7 @@ add_partial_enumeration (struct partial_die_info *enum_pdi,
   while (pdi)
     {
       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
-       complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
+       complaint (_("malformed enumerator DIE ignored"));
       else
        add_partial_symbol (pdi, cu);
       pdi = pdi->die_sibling;
@@ -9497,31 +9102,32 @@ peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
 }
 
-/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
+/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
+   READER::CU.  Use READER::ABBREV_TABLE to lookup any abbreviation.
+
    Return the corresponding abbrev, or NULL if the number is zero (indicating
    an empty DIE).  In either case *BYTES_READ will be set to the length of
    the initial number.  */
 
 static struct abbrev_info *
-peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
-                struct dwarf2_cu *cu)
+peek_die_abbrev (const die_reader_specs &reader,
+                const gdb_byte *info_ptr, unsigned int *bytes_read)
 {
-  bfd *abfd = cu->dwarf2_per_objfile->objfile->obfd;
-  unsigned int abbrev_number;
-  struct abbrev_info *abbrev;
-
-  abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
+  dwarf2_cu *cu = reader.cu;
+  bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
+  unsigned int abbrev_number
+    = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
 
   if (abbrev_number == 0)
     return NULL;
 
-  abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
+  abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
   if (!abbrev)
     {
       error (_("Dwarf Error: Could not find abbrev number %d in %s"
-              " at offset 0x%x [in module %s]"),
+              " at offset %s [in module %s]"),
             abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
-            to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
+            sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
     }
 
   return abbrev;
@@ -9534,13 +9140,11 @@ peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
 static const gdb_byte *
 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
 {
-  struct dwarf2_cu *cu = reader->cu;
-  struct abbrev_info *abbrev;
-  unsigned int bytes_read;
-
   while (1)
     {
-      abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
+      unsigned int bytes_read;
+      abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
+
       if (abbrev == NULL)
        return info_ptr + bytes_read;
       else
@@ -9573,16 +9177,14 @@ skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
        {
          read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
          if (attr.form == DW_FORM_ref_addr)
-           complaint (&symfile_complaints,
-                      _("ignoring absolute DW_AT_sibling"));
+           complaint (_("ignoring absolute DW_AT_sibling"));
          else
            {
              sect_offset off = dwarf2_get_ref_die_offset (&attr);
              const gdb_byte *sibling_ptr = buffer + to_underlying (off);
 
              if (sibling_ptr < info_ptr)
-               complaint (&symfile_complaints,
-                          _("DW_AT_sibling points backwards"));
+               complaint (_("DW_AT_sibling points backwards"));
              else if (sibling_ptr > reader->buffer_end)
                dwarf2_section_buffer_overflow_complaint (reader->die_section);
              else
@@ -9714,6 +9316,9 @@ static void
 dwarf2_read_symtab (struct partial_symtab *self,
                    struct objfile *objfile)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
+
   if (self->readin)
     {
       warning (_("bug: psymtab for %s is already read in."),
@@ -9728,11 +9333,6 @@ dwarf2_read_symtab (struct partial_symtab *self,
          gdb_flush (gdb_stdout);
        }
 
-      /* Restore our global data.  */
-      dwarf2_per_objfile
-       = (struct dwarf2_per_objfile *) objfile_data (objfile,
-                                                     dwarf2_objfile_data_key);
-
       /* If this psymtab is constructed from a debug-only objfile, the
         has_section_at_zero flag will not necessarily be correct.  We
         can get the correct value for this flag by looking at the data
@@ -9740,9 +9340,7 @@ dwarf2_read_symtab (struct partial_symtab *self,
       if (objfile->separate_debug_objfile_backlink)
        {
          struct dwarf2_per_objfile *dpo_backlink
-           = ((struct dwarf2_per_objfile *)
-              objfile_data (objfile->separate_debug_objfile_backlink,
-                            dwarf2_objfile_data_key));
+           = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
 
          dwarf2_per_objfile->has_section_at_zero
            = dpo_backlink->has_section_at_zero;
@@ -9757,7 +9355,7 @@ dwarf2_read_symtab (struct partial_symtab *self,
        printf_filtered (_("done.\n"));
     }
 
-  process_cu_includes ();
+  process_cu_includes (dwarf2_per_objfile);
 }
 \f
 /* Reading in full CUs.  */
@@ -9801,7 +9399,7 @@ maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
   /* We may arrive here during partial symbol reading, if we need full
      DIEs to process an unusual case (e.g. template arguments).  Do
      not queue PER_CU, just tell our caller to load its DIEs.  */
-  if (dwarf2_per_objfile->reading_partial_symbols)
+  if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
     {
       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
        return 1;
@@ -9834,7 +9432,7 @@ maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
 /* Process the queue.  */
 
 static void
-process_queue (void)
+process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
   struct dwarf2_queue_item *item, *next_item;
 
@@ -9864,17 +9462,17 @@ process_queue (void)
              struct signatured_type *sig_type =
                (struct signatured_type *) per_cu;
 
-             sprintf (buf, "TU %s at offset 0x%x",
+             sprintf (buf, "TU %s at offset %s",
                       hex_string (sig_type->signature),
-                      to_underlying (per_cu->sect_off));
+                      sect_offset_str (per_cu->sect_off));
              /* There can be 100s of TUs.
                 Only print them in verbose mode.  */
              debug_print_threshold = 2;
            }
          else
            {
-             sprintf (buf, "CU at offset 0x%x",
-                      to_underlying (per_cu->sect_off));
+             sprintf (buf, "CU at offset %s",
+                      sect_offset_str (per_cu->sect_off));
              debug_print_threshold = 1;
            }
 
@@ -9904,35 +9502,6 @@ process_queue (void)
     }
 }
 
-/* Free all allocated queue entries.  This function only releases anything if
-   an error was thrown; if the queue was processed then it would have been
-   freed as we went along.  */
-
-static void
-dwarf2_release_queue (void *dummy)
-{
-  struct dwarf2_queue_item *item, *last;
-
-  item = dwarf2_queue;
-  while (item)
-    {
-      /* Anything still marked queued is likely to be in an
-        inconsistent state, so discard it.  */
-      if (item->per_cu->queued)
-       {
-         if (item->per_cu->cu != NULL)
-           free_one_cached_comp_unit (item->per_cu);
-         item->per_cu->queued = 0;
-       }
-
-      last = item;
-      item = item->next;
-      xfree (last);
-    }
-
-  dwarf2_queue = dwarf2_queue_tail = NULL;
-}
-
 /* Read in full symbols for PST, and anything it depends on.  */
 
 static void
@@ -9973,7 +9542,7 @@ psymtab_to_symtab_1 (struct partial_symtab *pst)
       return;
     }
 
-  dw2_do_instantiate_symtab (per_cu);
+  dw2_do_instantiate_symtab (per_cu, false);
 }
 
 /* Trivial hash function for die_info: the hash value of a DIE
@@ -10042,11 +9611,12 @@ load_full_comp_unit_reader (const struct die_reader_specs *reader,
 
 static void
 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
+                    bool skip_partial,
                     enum language pretend_language)
 {
   gdb_assert (! this_cu->is_debug_types);
 
-  init_cutu_and_read_dies (this_cu, NULL, 1, 1,
+  init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
                           load_full_comp_unit_reader, &pretend_language);
 }
 
@@ -10063,20 +9633,7 @@ add_to_method_list (struct type *type, int fnfield_index, int index,
   mi.index = index;
   mi.name = name;
   mi.die = die;
-  VEC_safe_push (delayed_method_info, cu->method_list, &mi);
-}
-
-/* A cleanup for freeing the delayed method list.  */
-
-static void
-free_delayed_list (void *ptr)
-{
-  struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
-  if (cu->method_list != NULL)
-    {
-      VEC_free (delayed_method_info, cu->method_list);
-      cu->method_list = NULL;
-    }
+  cu->method_list.push_back (mi);
 }
 
 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
@@ -10105,21 +9662,18 @@ check_modifier (const char *physname, size_t &len, const char (&mod)[N])
 static void
 compute_delayed_physnames (struct dwarf2_cu *cu)
 {
-  int i;
-  struct delayed_method_info *mi;
-
   /* Only C++ delays computing physnames.  */
-  if (VEC_empty (delayed_method_info, cu->method_list))
+  if (cu->method_list.empty ())
     return;
   gdb_assert (cu->language == language_cplus);
 
-  for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
+  for (const delayed_method_info &mi : cu->method_list)
     {
       const char *physname;
       struct fn_fieldlist *fn_flp
-       = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
-      physname = dwarf2_physname (mi->name, mi->die, cu);
-      TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
+       = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
+      physname = dwarf2_physname (mi.name, mi.die, cu);
+      TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
        = physname ? physname : "";
 
       /* Since there's no tag to indicate whether a method is a
@@ -10134,14 +9688,17 @@ compute_delayed_physnames (struct dwarf2_cu *cu)
              if (physname[len] == ')') /* shortcut */
                break;
              else if (check_modifier (physname, len, " const"))
-               TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
+               TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
              else if (check_modifier (physname, len, " volatile"))
-               TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
+               TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
              else
                break;
            }
        }
     }
+
+  /* The list is no longer needed.  */
+  cu->method_list.clear ();
 }
 
 /* Go objects should be embedded in a DW_TAG_module DIE,
@@ -10179,10 +9736,10 @@ fixup_go_packaging (struct dwarf2_cu *cu)
                package_name = this_package_name;
              else
                {
-                 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+                 struct objfile *objfile
+                   = cu->per_cu->dwarf2_per_objfile->objfile;
                  if (strcmp (package_name, this_package_name) != 0)
-                   complaint (&symfile_complaints,
-                              _("Symtab %s has objects from two different Go packages: %s and %s"),
+                   complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
                               (symbol_symtab (sym) != NULL
                                ? symtab_to_filename_for_display
                                    (symbol_symtab (sym))
@@ -10196,7 +9753,7 @@ fixup_go_packaging (struct dwarf2_cu *cu)
 
   if (package_name != NULL)
     {
-      struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+      struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
       const char *saved_package_name
        = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
                                        package_name,
@@ -10205,8 +9762,6 @@ fixup_go_packaging (struct dwarf2_cu *cu)
                                     saved_package_name);
       struct symbol *sym;
 
-      TYPE_TAG_NAME (type) = TYPE_NAME (type);
-
       sym = allocate_symbol (objfile);
       SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
       SYMBOL_SET_NAMES (sym, saved_package_name,
@@ -10223,54 +9778,366 @@ fixup_go_packaging (struct dwarf2_cu *cu)
     }
 }
 
-/* Return the symtab for PER_CU.  This works properly regardless of
-   whether we're using the index or psymtabs.  */
+/* Allocate a fully-qualified name consisting of the two parts on the
+   obstack.  */
 
-static struct compunit_symtab *
-get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
+static const char *
+rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
 {
-  return (dwarf2_per_objfile->using_index
-         ? per_cu->v.quick->compunit_symtab
-         : per_cu->v.psymtab->compunit_symtab);
+  return obconcat (obstack, p1, "::", p2, (char *) NULL);
 }
 
-/* A helper function for computing the list of all symbol tables
-   included by PER_CU.  */
+/* A helper that allocates a struct discriminant_info to attach to a
+   union type.  */
+
+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)));
+  gdb_assert (default_index == -1
+             || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
+
+  TYPE_FLAG_DISCRIMINATED_UNION (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;
+
+  struct dynamic_prop prop;
+  prop.kind = PROP_UNDEFINED;
+  prop.data.baton = disc;
+
+  add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
+
+  return disc;
+}
+
+/* Some versions of rustc emitted enums in an unusual way.
+
+   Ordinary enums were emitted as unions.  The first element of each
+   structure in the union was named "RUST$ENUM$DISR".  This element
+   held the discriminant.
+
+   These versions of Rust also implemented the "non-zero"
+   optimization.  When the enum had two values, and one is empty and
+   the other holds a pointer that cannot be zero, the pointer is used
+   as the discriminant, with a zero value meaning the empty variant.
+   Here, the union's first member is of the form
+   RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
+   where the fieldnos are the indices of the fields that should be
+   traversed in order to find the field (which may be several fields deep)
+   and the variantname is the name of the variant of the case when the
+   field is zero.
+
+   This function recognizes whether TYPE is of one of these forms,
+   and, if so, smashes it to be a variant type.  */
 
 static void
-recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
-                               htab_t all_children, htab_t all_type_symtabs,
-                               struct dwarf2_per_cu_data *per_cu,
-                               struct compunit_symtab *immediate_parent)
+quirk_rust_enum (struct type *type, struct objfile *objfile)
 {
-  void **slot;
-  int ix;
-  struct compunit_symtab *cust;
-  struct dwarf2_per_cu_data *iter;
+  gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
 
-  slot = htab_find_slot (all_children, per_cu, INSERT);
-  if (*slot != NULL)
-    {
-      /* This inclusion and its children have been processed.  */
-      return;
+  /* We don't need to deal with empty enums.  */
+  if (TYPE_NFIELDS (type) == 0)
+    return;
+
+#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
+  if (TYPE_NFIELDS (type) == 1
+      && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
+    {
+      const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
+
+      /* Decode the field name to find the offset of the
+        discriminant.  */
+      ULONGEST bit_offset = 0;
+      struct type *field_type = TYPE_FIELD_TYPE (type, 0);
+      while (name[0] >= '0' && name[0] <= '9')
+       {
+         char *tail;
+         unsigned long index = strtoul (name, &tail, 10);
+         name = tail;
+         if (*name != '$'
+             || index >= TYPE_NFIELDS (field_type)
+             || (TYPE_FIELD_LOC_KIND (field_type, index)
+                 != FIELD_LOC_KIND_BITPOS))
+           {
+             complaint (_("Could not parse Rust enum encoding string \"%s\""
+                          "[in module %s]"),
+                        TYPE_FIELD_NAME (type, 0),
+                        objfile_name (objfile));
+             return;
+           }
+         ++name;
+
+         bit_offset += TYPE_FIELD_BITPOS (field_type, index);
+         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)
+       = (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);
+
+      /* 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));
+
+      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;
+      /* 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));
+
+      /* 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>>";
     }
+  else if (TYPE_NFIELDS (type) == 1)
+    {
+      /* We assume that a union with a single field is a univariant
+        enum.  */
+      /* 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;
 
-  *slot = per_cu;
-  /* Only add a CU if it has a symbol table.  */
-  cust = get_compunit_symtab (per_cu);
-  if (cust != NULL)
+      /* 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);
+
+      struct type *field_type = TYPE_FIELD_TYPE (union_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);
+    }
+  else
     {
-      /* If this is a type unit only add its symbol table if we haven't
-        seen it yet (type unit per_cu's can share symtabs).  */
-      if (per_cu->is_debug_types)
+      struct type *disr_type = nullptr;
+      for (int i = 0; i < TYPE_NFIELDS (type); ++i)
        {
-         slot = htab_find_slot (all_type_symtabs, cust, INSERT);
-         if (*slot == NULL)
+         disr_type = TYPE_FIELD_TYPE (type, i);
+
+         if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
            {
-             *slot = cust;
-             VEC_safe_push (compunit_symtab_ptr, *result, cust);
-             if (cust->user == NULL)
-               cust->user = immediate_parent;
+             /* All fields of a true enum will be structs.  */
+             return;
+           }
+         else if (TYPE_NFIELDS (disr_type) == 0)
+           {
+             /* Could be data-less variant, so keep going.  */
+             disr_type = nullptr;
+           }
+         else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
+                          "RUST$ENUM$DISR") != 0)
+           {
+             /* Not a Rust enum.  */
+             return;
+           }
+         else
+           {
+             /* Found one.  */
+             break;
+           }
+       }
+
+      /* If we got here without a discriminant, then it's probably
+        just a union.  */
+      if (disr_type == nullptr)
+       return;
+
+      /* 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 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),
+             TYPE_NFIELDS (type) * sizeof (struct field));
+
+      /* 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);
+
+      /* We need a way to find the correct discriminant given a
+        variant name.  For convenience we build a map here.  */
+      struct type *enum_type = FIELD_TYPE (*disr_field);
+      std::unordered_map<std::string, ULONGEST> discriminant_map;
+      for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
+       {
+         if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
+           {
+             const char *name
+               = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
+             discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
+           }
+       }
+
+      int n_fields = TYPE_NFIELDS (union_type);
+      struct discriminant_info *disc
+       = alloc_discriminant_info (union_type, 0, -1);
+      /* Skip the discriminant here.  */
+      for (int i = 1; i < n_fields; ++i)
+       {
+         /* Find the final word in the name of this variant's type.
+            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)));
+
+         auto iter = discriminant_map.find (variant_name);
+         if (iter != discriminant_map.end ())
+           disc->discriminants[i] = iter->second;
+
+         /* Remove the discriminant field, if it exists.  */
+         struct type *sub_type = TYPE_FIELD_TYPE (union_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);
+       }
+    }
+}
+
+/* Rewrite some Rust unions to be structures with variants parts.  */
+
+static void
+rust_union_quirks (struct dwarf2_cu *cu)
+{
+  gdb_assert (cu->language == language_rust);
+  for (type *type_ : cu->rust_unions)
+    quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
+  /* We don't need this any more.  */
+  cu->rust_unions.clear ();
+}
+
+/* Return the symtab for PER_CU.  This works properly regardless of
+   whether we're using the index or psymtabs.  */
+
+static struct compunit_symtab *
+get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
+{
+  return (per_cu->dwarf2_per_objfile->using_index
+         ? per_cu->v.quick->compunit_symtab
+         : per_cu->v.psymtab->compunit_symtab);
+}
+
+/* A helper function for computing the list of all symbol tables
+   included by PER_CU.  */
+
+static void
+recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
+                               htab_t all_children, htab_t all_type_symtabs,
+                               struct dwarf2_per_cu_data *per_cu,
+                               struct compunit_symtab *immediate_parent)
+{
+  void **slot;
+  int ix;
+  struct compunit_symtab *cust;
+  struct dwarf2_per_cu_data *iter;
+
+  slot = htab_find_slot (all_children, per_cu, INSERT);
+  if (*slot != NULL)
+    {
+      /* This inclusion and its children have been processed.  */
+      return;
+    }
+
+  *slot = per_cu;
+  /* Only add a CU if it has a symbol table.  */
+  cust = get_compunit_symtab (per_cu);
+  if (cust != NULL)
+    {
+      /* If this is a type unit only add its symbol table if we haven't
+        seen it yet (type unit per_cu's can share symtabs).  */
+      if (per_cu->is_debug_types)
+       {
+         slot = htab_find_slot (all_type_symtabs, cust, INSERT);
+         if (*slot == NULL)
+           {
+             *slot = cust;
+             VEC_safe_push (compunit_symtab_ptr, *result, cust);
+             if (cust->user == NULL)
+               cust->user = immediate_parent;
            }
        }
       else
@@ -10329,7 +10196,7 @@ compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
       /* Now we have a transitive closure of all the included symtabs.  */
       len = VEC_length (compunit_symtab_ptr, result_symtabs);
       cust->includes
-       = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
+       = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
                     struct compunit_symtab *, len + 1);
       for (ix = 0;
           VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
@@ -10348,7 +10215,7 @@ compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
    read.  */
 
 static void
-process_cu_includes (void)
+process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
   int ix;
   struct dwarf2_per_cu_data *iter;
@@ -10373,11 +10240,11 @@ process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
                        enum language pretend_language)
 {
   struct dwarf2_cu *cu = per_cu->cu;
-  struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
+  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);
   CORE_ADDR lowpc, highpc;
   struct compunit_symtab *cust;
-  struct cleanup *delayed_list_cleanup;
   CORE_ADDR baseaddr;
   struct block *static_block;
   CORE_ADDR addr;
@@ -10386,7 +10253,9 @@ process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
 
   buildsym_init ();
   scoped_free_pendings free_pending;
-  delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
+
+  /* Clear the list here in case something was left over.  */
+  cu->method_list.clear ();
 
   cu->list_in_scope = &file_symbols;
 
@@ -10404,7 +10273,9 @@ process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
      should be complete, and it should now be safe to compute all of the
      physnames.  */
   compute_delayed_physnames (cu);
-  do_cleanups (delayed_list_cleanup);
+
+  if (cu->language == language_rust)
+    rust_union_quirks (cu);
 
   /* Some compilers don't define a DW_AT_high_pc attribute for the
      compilation unit.  If the DW_AT_high_pc is missing, synthesize
@@ -10477,9 +10348,9 @@ process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
                        enum language pretend_language)
 {
   struct dwarf2_cu *cu = per_cu->cu;
-  struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
+  struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct compunit_symtab *cust;
-  struct cleanup *delayed_list_cleanup;
   struct signatured_type *sig_type;
 
   gdb_assert (per_cu->is_debug_types);
@@ -10487,7 +10358,9 @@ process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
 
   buildsym_init ();
   scoped_free_pendings free_pending;
-  delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
+
+  /* Clear the list here in case something was left over.  */
+  cu->method_list.clear ();
 
   cu->list_in_scope = &file_symbols;
 
@@ -10505,7 +10378,9 @@ process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
      should be complete, and it should now be safe to compute all of the
      physnames.  */
   compute_delayed_physnames (cu);
-  do_cleanups (delayed_list_cleanup);
+
+  if (cu->language == language_rust)
+    rust_union_quirks (cu);
 
   /* TUs share symbol tables.
      If this is the first TU to use this symtab, complete the construction
@@ -10555,7 +10430,7 @@ process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
     {
       error (_("Dwarf Error: DW_TAG_imported_unit is not"
               " supported in type units [in module %s]"),
-            objfile_name (cu->dwarf2_per_objfile->objfile));
+            objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
     }
 
   attr = dwarf2_attr (die, DW_AT_import, cu);
@@ -10565,11 +10440,11 @@ process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
       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->dwarf2_per_objfile->objfile);
+                                           cu->per_cu->dwarf2_per_objfile);
 
       /* 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, cu->language);
+       load_full_comp_unit (per_cu, false, cu->language);
 
       VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
                     per_cu);
@@ -10686,12 +10561,13 @@ process_die (struct die_info *die, struct dwarf2_cu *cu)
       cu->processing_has_namespace_info = 1;
       if (read_namespace_alias (die, cu))
        break;
-      /* The declaration is not a global namespace alias: fall through.  */
+      /* The declaration is not a global namespace alias.  */
+      /* Fall through.  */
     case DW_TAG_imported_module:
       cu->processing_has_namespace_info = 1;
       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
                                 || cu->language != language_fortran))
-       complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
+       complaint (_("Tag '%s' has unexpected children"),
                   dwarf_tag_name (die->tag));
       read_import_statement (die, cu);
       break;
@@ -10822,7 +10698,7 @@ dwarf2_compute_name (const char *name,
                     struct die_info *die, struct dwarf2_cu *cu,
                     int physname)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
 
   if (name == NULL)
     name = dwarf2_name (die, cu);
@@ -10831,7 +10707,7 @@ dwarf2_compute_name (const char *name,
      but otherwise compute it by typename_concat inside GDB.
      FIXME: Actually this is not really true, or at least not always true.
      It's all very confusing.  SYMBOL_SET_NAMES doesn't try to demangle
-     Fortran names because there is no mangling standard.  So new_symbol_full
+     Fortran names because there is no mangling standard.  So new_symbol
      will set the demangled name to the result of dwarf2_full_name, and it is
      the demangled name that GDB uses if it exists.  */
   if (cu->language == language_ada
@@ -10925,8 +10801,7 @@ dwarf2_compute_name (const char *name,
                  attr = dwarf2_attr (child, DW_AT_type, cu);
                  if (attr == NULL)
                    {
-                     complaint (&symfile_complaints,
-                                _("template parameter missing DW_AT_type"));
+                     complaint (_("template parameter missing DW_AT_type"));
                      buf.puts ("UNKNOWN_TYPE");
                      continue;
                    }
@@ -10934,15 +10809,15 @@ dwarf2_compute_name (const char *name,
 
                  if (child->tag == DW_TAG_template_type_param)
                    {
-                     c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
+                     c_print_type (type, "", &buf, -1, 0, cu->language,
+                                   &type_print_raw_options);
                      continue;
                    }
 
                  attr = dwarf2_attr (child, DW_AT_const_value, cu);
                  if (attr == NULL)
                    {
-                     complaint (&symfile_complaints,
-                                _("template parameter missing "
+                     complaint (_("template parameter missing "
                                   "DW_AT_const_value"));
                      buf.puts ("UNKNOWN_VALUE");
                      continue;
@@ -10980,7 +10855,6 @@ dwarf2_compute_name (const char *name,
                      opts.raw = 1;
                      value_print (v, &buf, &opts);
                      release_value (v);
-                     value_free (v);
                    }
                }
 
@@ -11071,7 +10945,7 @@ dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
 static const char *
 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   const char *retval, *mangled = NULL, *canon = NULL;
   int need_copy = 1;
 
@@ -11093,23 +10967,27 @@ dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
   gdb::unique_xmalloc_ptr<char> demangled;
   if (mangled != NULL)
     {
-      /* Use DMGL_RET_DROP for C++ template functions to suppress their return
-        type.  It is easier for GDB users to search for such functions as
-        `name(params)' than `long name(params)'.  In such case the minimal
-        symbol names do not match the full symbol names but for template
-        functions there is never a need to look up their definition from their
-        declaration so the only disadvantage remains the minimal symbol
-        variant `long name(params)' does not have the proper inferior type.
-        */
 
-      if (cu->language == language_go)
+      if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
+       {
+         /* Do nothing (do not demangle the symbol name).  */
+       }
+      else if (cu->language == language_go)
        {
-         /* This is a lie, but we already lie to the caller new_symbol_full.
-            new_symbol_full assumes we return the mangled name.
+         /* This is a lie, but we already lie to the caller new_symbol.
+            new_symbol assumes we return the mangled name.
             This just undoes that lie until things are cleaned up.  */
        }
       else
        {
+         /* Use DMGL_RET_DROP for C++ template functions to suppress
+            their return type.  It is easier for GDB users to search
+            for such functions as `name(params)' than `long name(params)'.
+            In such case the minimal symbol names do not match the full
+            symbol names but for template functions there is never a need
+            to look up their definition from their declaration so
+            the only disadvantage remains the minimal symbol variant
+            `long name(params)' does not have the proper inferior type.  */
          demangled.reset (gdb_demangle (mangled,
                                         (DMGL_PARAMS | DMGL_ANSI
                                          | DMGL_RET_DROP)));
@@ -11133,10 +11011,9 @@ dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
             compute DW_AT_linkage_name incorrectly.  But in such case
             GDB would need to be bug-to-bug compatible.  */
 
-         complaint (&symfile_complaints,
-                    _("Computed physname <%s> does not match demangled <%s> "
-                      "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
-                    physname, canon, mangled, to_underlying (die->sect_off),
+         complaint (_("Computed physname <%s> does not match demangled <%s> "
+                      "(from linkage <%s>) - DIE at %s [in module %s]"),
+                    physname, canon, mangled, sect_offset_str (die->sect_off),
                     objfile_name (objfile));
 
          /* Prefer DW_AT_linkage_name (in the CANON form) - when it
@@ -11197,9 +11074,8 @@ read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
 
       if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
        {
-         complaint (&symfile_complaints,
-                    _("DIE at 0x%x has too many recursively imported "
-                      "declarations"), to_underlying (d->sect_off));
+         complaint (_("DIE at %s has too many recursively imported "
+                      "declarations"), sect_offset_str (d->sect_off));
          return 0;
        }
 
@@ -11245,7 +11121,7 @@ using_directives (enum language language)
 static void
 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct attribute *import_attr;
   struct die_info *imported_die, *child_die;
   struct dwarf2_cu *imported_cu;
@@ -11260,7 +11136,7 @@ read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
   import_attr = dwarf2_attr (die, DW_AT_import, cu);
   if (import_attr == NULL)
     {
-      complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
+      complaint (_("Tag '%s' has no DW_AT_import"),
                 dwarf_tag_name (die->tag));
       return;
     }
@@ -11340,17 +11216,17 @@ read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
 
        if (child_die->tag != DW_TAG_imported_declaration)
          {
-           complaint (&symfile_complaints,
-                      _("child DW_TAG_imported_declaration expected "
-                        "- DIE at 0x%x [in module %s]"),
-                      to_underlying (child_die->sect_off), objfile_name (objfile));
+           complaint (_("child DW_TAG_imported_declaration expected "
+                        "- DIE at %s [in module %s]"),
+                      sect_offset_str (child_die->sect_off),
+                      objfile_name (objfile));
            continue;
          }
 
        import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
        if (import_attr == NULL)
          {
-           complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
+           complaint (_("Tag '%s' has no DW_AT_import"),
                       dwarf_tag_name (child_die->tag));
            continue;
          }
@@ -11361,10 +11237,10 @@ read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
        imported_name = dwarf2_name (imported_die, imported_cu);
        if (imported_name == NULL)
          {
-           complaint (&symfile_complaints,
-                      _("child DW_TAG_imported_declaration has unknown "
-                        "imported name - DIE at 0x%x [in module %s]"),
-                      to_underlying (child_die->sect_off), objfile_name (objfile));
+           complaint (_("child DW_TAG_imported_declaration has unknown "
+                        "imported name - DIE at %s [in module %s]"),
+                      sect_offset_str (child_die->sect_off),
+                      objfile_name (objfile));
            continue;
          }
 
@@ -11452,6 +11328,8 @@ static void
 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
                        const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct attribute *attr;
   struct line_header line_header_local;
@@ -11550,6 +11428,8 @@ handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
 static void
 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);
   CORE_ADDR lowpc = ((CORE_ADDR) -1);
@@ -11610,8 +11490,7 @@ read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
   if (attr && cu->line_header)
     {
       if (dwarf2_attr (die, DW_AT_macro_info, cu))
-       complaint (&symfile_complaints,
-                  _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
+       complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
 
       dwarf_decode_macros (cu, DW_UNSND (attr), 1);
     }
@@ -11801,10 +11680,8 @@ eq_dwo_file (const void *item_lhs, const void *item_rhs)
 /* Allocate a hash table for DWO files.  */
 
 static htab_t
-allocate_dwo_file_hash_table (void)
+allocate_dwo_file_hash_table (struct objfile *objfile)
 {
-  struct objfile *objfile = dwarf2_per_objfile->objfile;
-
   return htab_create_alloc_ex (41,
                               hash_dwo_file,
                               eq_dwo_file,
@@ -11817,13 +11694,16 @@ allocate_dwo_file_hash_table (void)
 /* Lookup DWO file DWO_NAME.  */
 
 static void **
-lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
+lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                     const char *dwo_name,
+                     const char *comp_dir)
 {
   struct dwo_file find_entry;
   void **slot;
 
   if (dwarf2_per_objfile->dwo_files == NULL)
-    dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
+    dwarf2_per_objfile->dwo_files
+      = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
 
   memset (&find_entry, 0, sizeof (find_entry));
   find_entry.dwo_name = dwo_name;
@@ -11900,10 +11780,9 @@ create_dwo_cu_reader (const struct die_reader_specs *reader,
   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
   if (attr == NULL)
     {
-      complaint (&symfile_complaints,
-                _("Dwarf Error: debug entry at offset 0x%x is missing"
+      complaint (_("Dwarf Error: debug entry at offset %s is missing"
                   " its dwo_id [in module %s]"),
-                to_underlying (sect_off), dwo_file->dwo_name);
+                sect_offset_str (sect_off), dwo_file->dwo_name);
       return;
     }
 
@@ -11914,8 +11793,8 @@ create_dwo_cu_reader (const struct die_reader_specs *reader,
   dwo_unit->length = cu->per_cu->length;
 
   if (dwarf_read_debug)
-    fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, dwo_id %s\n",
-                       to_underlying (sect_off),
+    fprintf_unfiltered (gdb_stdlog, "  offset %s, dwo_id %s\n",
+                       sect_offset_str (sect_off),
                        hex_string (dwo_unit->signature));
 }
 
@@ -11923,7 +11802,8 @@ create_dwo_cu_reader (const struct die_reader_specs *reader,
    Note: This function processes DWO files only, not DWP files.  */
 
 static void
-create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
+create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                      struct dwo_file &dwo_file, dwarf2_section_info &section,
                       htab_t &cus_htab)
 {
   struct objfile *objfile = dwarf2_per_objfile->objfile;
@@ -11980,10 +11860,9 @@ create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
          const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
          sect_offset dup_sect_off = dup_cu->sect_off;
 
-         complaint (&symfile_complaints,
-                    _("debug cu entry at offset 0x%x is duplicate to"
-                      " the entry at offset 0x%x, signature %s"),
-                    to_underlying (sect_off), to_underlying (dup_sect_off),
+         complaint (_("debug cu entry at offset %s is duplicate to"
+                      " the entry at offset %s, signature %s"),
+                    sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
                     hex_string (dwo_unit->signature));
        }
       *slot = (void *)dwo_unit;
@@ -12128,10 +12007,11 @@ create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
    Note: This function processes DWP files only, not DWO files.  */
 
 static struct dwp_hash_table *
-create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
+create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                      struct dwp_file *dwp_file, int is_debug_types)
 {
   struct objfile *objfile = dwarf2_per_objfile->objfile;
-  bfd *dbfd = dwp_file->dbfd;
+  bfd *dbfd = dwp_file->dbfd.get ();
   const gdb_byte *index_ptr, *index_end;
   struct dwarf2_section_info *index;
   uint32_t version, nr_columns, nr_units, nr_slots;
@@ -12190,8 +12070,7 @@ create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
       if (nr_slots != 0 || nr_units != 0
          || (version == 2 && nr_columns != 0))
        {
-         complaint (&symfile_complaints,
-                    _("Empty DWP but nr_slots,nr_units,nr_columns not"
+         complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
                       " all zero [in modules %s]"),
                     dwp_file->name);
        }
@@ -12365,7 +12244,8 @@ locate_v1_virtual_dwo_sections (asection *sectp,
    This is for DWP version 1 files.  */
 
 static struct dwo_unit *
-create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
+create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                          struct dwp_file *dwp_file,
                           uint32_t unit_index,
                           const char *comp_dir,
                           ULONGEST signature, int is_debug_types)
@@ -12373,7 +12253,7 @@ create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   const struct dwp_hash_table *dwp_htab =
     is_debug_types ? dwp_file->tus : dwp_file->cus;
-  bfd *dbfd = dwp_file->dbfd;
+  bfd *dbfd = dwp_file->dbfd.get ();
   const char *kind = is_debug_types ? "TU" : "CU";
   struct dwo_file *dwo_file;
   struct dwo_unit *dwo_unit;
@@ -12463,7 +12343,9 @@ create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
                   get_section_id (&sections.loc),
                   get_section_id (&sections.str_offsets));
   /* Can we use an existing virtual DWO file?  */
-  dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
+  dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
+                                       virtual_dwo_name.c_str (),
+                                       comp_dir);
   /* Create one if necessary.  */
   if (*dwo_file_slot == NULL)
     {
@@ -12522,7 +12404,8 @@ create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
    of just that piece.  */
 
 static struct dwarf2_section_info
-create_dwp_v2_section (struct dwarf2_section_info *section,
+create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                      struct dwarf2_section_info *section,
                       bfd_size_type offset, bfd_size_type size)
 {
   struct dwarf2_section_info result;
@@ -12563,7 +12446,8 @@ create_dwp_v2_section (struct dwarf2_section_info *section,
    This is for DWP version 2 files.  */
 
 static struct dwo_unit *
-create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
+create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                          struct dwp_file *dwp_file,
                           uint32_t unit_index,
                           const char *comp_dir,
                           ULONGEST signature, int is_debug_types)
@@ -12571,7 +12455,7 @@ create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   const struct dwp_hash_table *dwp_htab =
     is_debug_types ? dwp_file->tus : dwp_file->cus;
-  bfd *dbfd = dwp_file->dbfd;
+  bfd *dbfd = dwp_file->dbfd.get ();
   const char *kind = is_debug_types ? "TU" : "CU";
   struct dwo_file *dwo_file;
   struct dwo_unit *dwo_unit;
@@ -12657,7 +12541,9 @@ create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
                   (long) (sections.str_offsets_size
                           ? sections.str_offsets_offset : 0));
   /* Can we use an existing virtual DWO file?  */
-  dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
+  dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
+                                       virtual_dwo_name.c_str (),
+                                       comp_dir);
   /* Create one if necessary.  */
   if (*dwo_file_slot == NULL)
     {
@@ -12673,22 +12559,23 @@ create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
                                        virtual_dwo_name.size ());
       dwo_file->comp_dir = comp_dir;
       dwo_file->sections.abbrev =
-       create_dwp_v2_section (&dwp_file->sections.abbrev,
+       create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
                               sections.abbrev_offset, sections.abbrev_size);
       dwo_file->sections.line =
-       create_dwp_v2_section (&dwp_file->sections.line,
+       create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
                               sections.line_offset, sections.line_size);
       dwo_file->sections.loc =
-       create_dwp_v2_section (&dwp_file->sections.loc,
+       create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
                               sections.loc_offset, sections.loc_size);
       dwo_file->sections.macinfo =
-       create_dwp_v2_section (&dwp_file->sections.macinfo,
+       create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
                               sections.macinfo_offset, sections.macinfo_size);
       dwo_file->sections.macro =
-       create_dwp_v2_section (&dwp_file->sections.macro,
+       create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
                               sections.macro_offset, sections.macro_size);
       dwo_file->sections.str_offsets =
-       create_dwp_v2_section (&dwp_file->sections.str_offsets,
+       create_dwp_v2_section (dwarf2_per_objfile,
+                              &dwp_file->sections.str_offsets,
                               sections.str_offsets_offset,
                               sections.str_offsets_size);
       /* The "str" section is global to the entire DWP file.  */
@@ -12717,7 +12604,8 @@ create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
   dwo_unit->signature = signature;
   dwo_unit->section =
     XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
-  *dwo_unit->section = create_dwp_v2_section (is_debug_types
+  *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
+                                             is_debug_types
                                              ? &dwp_file->sections.types
                                              : &dwp_file->sections.info,
                                              sections.info_or_types_offset,
@@ -12731,12 +12619,13 @@ create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
    Returns NULL if the signature isn't found.  */
 
 static struct dwo_unit *
-lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
+lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                       struct dwp_file *dwp_file, const char *comp_dir,
                        ULONGEST signature, int is_debug_types)
 {
   const struct dwp_hash_table *dwp_htab =
     is_debug_types ? dwp_file->tus : dwp_file->cus;
-  bfd *dbfd = dwp_file->dbfd;
+  bfd *dbfd = dwp_file->dbfd.get ();
   uint32_t mask = dwp_htab->nr_slots - 1;
   uint32_t hash = signature & mask;
   uint32_t hash2 = ((signature >> 32) & mask) | 1;
@@ -12769,13 +12658,15 @@ lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
 
          if (dwp_file->version == 1)
            {
-             *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
+             *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
+                                                dwp_file, unit_index,
                                                 comp_dir, signature,
                                                 is_debug_types);
            }
          else
            {
-             *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
+             *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
+                                                dwp_file, unit_index,
                                                 comp_dir, signature,
                                                 is_debug_types);
            }
@@ -12804,38 +12695,44 @@ lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
    NOTE: This function is derived from symfile_bfd_open.  */
 
 static gdb_bfd_ref_ptr
-try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
+try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                   const char *file_name, int is_dwp, int search_cwd)
 {
-  int desc, flags;
-  char *absolute_name;
+  int desc;
   /* Blech.  OPF_TRY_CWD_FIRST also disables searching the path list if
      FILE_NAME contains a '/'.  So we can't use it.  Instead prepend "."
      to debug_file_directory.  */
-  char *search_path;
+  const char *search_path;
   static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
 
+  gdb::unique_xmalloc_ptr<char> search_path_holder;
   if (search_cwd)
     {
       if (*debug_file_directory != '\0')
-       search_path = concat (".", dirname_separator_string,
-                             debug_file_directory, (char *) NULL);
+       {
+         search_path_holder.reset (concat (".", dirname_separator_string,
+                                           debug_file_directory,
+                                           (char *) NULL));
+         search_path = search_path_holder.get ();
+       }
       else
-       search_path = xstrdup (".");
+       search_path = ".";
     }
   else
-    search_path = xstrdup (debug_file_directory);
+    search_path = debug_file_directory;
 
-  flags = OPF_RETURN_REALPATH;
+  openp_flags flags = OPF_RETURN_REALPATH;
   if (is_dwp)
     flags |= OPF_SEARCH_IN_PATH;
+
+  gdb::unique_xmalloc_ptr<char> absolute_name;
   desc = openp (search_path, flags, file_name,
                O_RDONLY | O_BINARY, &absolute_name);
-  xfree (search_path);
   if (desc < 0)
     return NULL;
 
-  gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
-  xfree (absolute_name);
+  gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
+                                        gnutarget, desc));
   if (sym_bfd == NULL)
     return NULL;
   bfd_set_cacheable (sym_bfd.get (), 1);
@@ -12860,10 +12757,12 @@ try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
    same as symfile_bfd_open.  */
 
 static gdb_bfd_ref_ptr
-open_dwo_file (const char *file_name, const char *comp_dir)
+open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
+              const char *file_name, const char *comp_dir)
 {
   if (IS_ABSOLUTE_PATH (file_name))
-    return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
+    return try_open_dwop_file (dwarf2_per_objfile, file_name,
+                              0 /*is_dwp*/, 0 /*search_cwd*/);
 
   /* Before trying the search path, try DWO_NAME in COMP_DIR.  */
 
@@ -12874,7 +12773,9 @@ open_dwo_file (const char *file_name, const char *comp_dir)
 
       /* NOTE: If comp_dir is a relative path, this will also try the
         search path, which seems useful.  */
-      gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
+      gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
+                                               path_to_try,
+                                               0 /*is_dwp*/,
                                                1 /*search_cwd*/));
       xfree (path_to_try);
       if (abfd != NULL)
@@ -12887,7 +12788,8 @@ open_dwo_file (const char *file_name, const char *comp_dir)
   if (*debug_file_directory == '\0')
     return NULL;
 
-  return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
+  return try_open_dwop_file (dwarf2_per_objfile, file_name,
+                            0 /*is_dwp*/, 1 /*search_cwd*/);
 }
 
 /* This function is mapped across the sections and remembers the offset and
@@ -12959,38 +12861,38 @@ static struct dwo_file *
 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
                        const char *dwo_name, const char *comp_dir)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
-  struct dwo_file *dwo_file;
-  struct cleanup *cleanups;
 
-  gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
+  gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
   if (dbfd == NULL)
     {
       if (dwarf_read_debug)
        fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
       return NULL;
     }
-  dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
+
+  /* We use a unique pointer here, despite the obstack allocation,
+     because a dwo_file needs some cleanup if it is abandoned.  */
+  dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
+                                       struct dwo_file));
   dwo_file->dwo_name = dwo_name;
   dwo_file->comp_dir = comp_dir;
   dwo_file->dbfd = dbfd.release ();
 
-  cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
-
   bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
                         &dwo_file->sections);
 
-  create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
-
-  create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
-                                dwo_file->tus);
+  create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
+                        dwo_file->cus);
 
-  discard_cleanups (cleanups);
+  create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
+                                dwo_file->sections.types, dwo_file->tus);
 
   if (dwarf_read_debug)
     fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
 
-  return dwo_file;
+  return dwo_file.release ();
 }
 
 /* This function is mapped across the sections and remembers the offset and
@@ -13131,9 +13033,11 @@ allocate_dwp_loaded_cutus_table (struct objfile *objfile)
    same as symfile_bfd_open.  */
 
 static gdb_bfd_ref_ptr
-open_dwp_file (const char *file_name)
+open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
+              const char *file_name)
 {
-  gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
+  gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
+                                           1 /*is_dwp*/,
                                            1 /*search_cwd*/));
   if (abfd != NULL)
     return abfd;
@@ -13151,7 +13055,8 @@ open_dwp_file (const char *file_name)
       /* Don't implicitly search the current directory here.
         If the user wants to search "." to handle this case,
         it must be added to debug-file-directory.  */
-      return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
+      return try_open_dwop_file (dwarf2_per_objfile,
+                                lbasename (file_name), 1 /*is_dwp*/,
                                 0 /*search_cwd*/);
     }
 
@@ -13162,11 +13067,10 @@ open_dwp_file (const char *file_name)
    By convention the name of the DWP file is ${objfile}.dwp.
    The result is NULL if it can't be found.  */
 
-static struct dwp_file *
-open_and_init_dwp_file (void)
+static std::unique_ptr<struct dwp_file>
+open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
   struct objfile *objfile = dwarf2_per_objfile->objfile;
-  struct dwp_file *dwp_file;
 
   /* Try to find first .dwp for the binary file before any symbolic links
      resolving.  */
@@ -13186,25 +13090,26 @@ open_and_init_dwp_file (void)
 
   dwp_name += ".dwp";
 
-  gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
+  gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
   if (dbfd == NULL
       && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
     {
       /* Try to find .dwp for the binary file after gdb_realpath resolving.  */
       dwp_name = objfile_name (objfile);
       dwp_name += ".dwp";
-      dbfd = open_dwp_file (dwp_name.c_str ());
+      dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
     }
 
   if (dbfd == NULL)
     {
       if (dwarf_read_debug)
        fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
-      return NULL;
+      return std::unique_ptr<dwp_file> ();
     }
-  dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
-  dwp_file->name = bfd_get_filename (dbfd.get ());
-  dwp_file->dbfd = dbfd.release ();
+
+  const char *name = bfd_get_filename (dbfd.get ());
+  std::unique_ptr<struct dwp_file> dwp_file
+    (new struct dwp_file (name, std::move (dbfd)));
 
   /* +1: section 0 is unused */
   dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
@@ -13212,12 +13117,15 @@ open_and_init_dwp_file (void)
     OBSTACK_CALLOC (&objfile->objfile_obstack,
                    dwp_file->num_sections, asection *);
 
-  bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
-                        dwp_file);
+  bfd_map_over_sections (dwp_file->dbfd.get (),
+                        dwarf2_locate_common_dwp_sections,
+                        dwp_file.get ());
 
-  dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
+  dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
+                                        0);
 
-  dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
+  dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
+                                        1);
 
   /* The DWP file version is stored in the hash table.  Oh well.  */
   if (dwp_file->cus && dwp_file->tus
@@ -13240,8 +13148,9 @@ open_and_init_dwp_file (void)
     dwp_file->version = 2;
 
   if (dwp_file->version == 2)
-    bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
-                          dwp_file);
+    bfd_map_over_sections (dwp_file->dbfd.get (),
+                          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);
@@ -13261,14 +13170,15 @@ open_and_init_dwp_file (void)
 /* Wrapper around open_and_init_dwp_file, only open it once.  */
 
 static struct dwp_file *
-get_dwp_file (void)
+get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
   if (! dwarf2_per_objfile->dwp_checked)
     {
-      dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
+      dwarf2_per_objfile->dwp_file
+       = open_and_init_dwp_file (dwarf2_per_objfile);
       dwarf2_per_objfile->dwp_checked = 1;
     }
-  return dwarf2_per_objfile->dwp_file;
+  return dwarf2_per_objfile->dwp_file.get ();
 }
 
 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
@@ -13292,6 +13202,7 @@ lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
                 const char *dwo_name, const char *comp_dir,
                 ULONGEST signature, int is_debug_types)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   const char *kind = is_debug_types ? "TU" : "CU";
   void **dwo_file_slot;
@@ -13303,7 +13214,7 @@ lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
      look for the original DWO file.  It makes gdb behave differently
      depending on whether one is debugging in the build tree.  */
 
-  dwp_file = get_dwp_file ();
+  dwp_file = get_dwp_file (dwarf2_per_objfile);
   if (dwp_file != NULL)
     {
       const struct dwp_hash_table *dwp_htab =
@@ -13312,7 +13223,7 @@ lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
       if (dwp_htab != NULL)
        {
          struct dwo_unit *dwo_cutu =
-           lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
+           lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
                                    signature, is_debug_types);
 
          if (dwo_cutu != NULL)
@@ -13332,7 +13243,8 @@ lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
     {
       /* No DWP file, look for the DWO file.  */
 
-      dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
+      dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
+                                           dwo_name, comp_dir);
       if (*dwo_file_slot == NULL)
        {
          /* Read in the file and build a table of the CUs/TUs it contains.  */
@@ -13398,12 +13310,12 @@ lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
       dwp_text = string_printf (" [in DWP file %s]",
                                lbasename (dwp_file->name));
 
-    warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
+    warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
               " [in module %s]"),
             kind, dwo_name, hex_string (signature),
             dwp_text.c_str (),
             this_unit->is_debug_types ? "TU" : "CU",
-            to_underlying (this_unit->sect_off), objfile_name (objfile));
+            sect_offset_str (this_unit->sect_off), objfile_name (objfile));
   }
   return NULL;
 }
@@ -13467,7 +13379,7 @@ queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
   struct dwo_file *dwo_file;
 
   gdb_assert (!per_cu->is_debug_types);
-  gdb_assert (get_dwp_file () == NULL);
+  gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
   gdb_assert (per_cu->cu != NULL);
 
   dwo_unit = per_cu->cu->dwo_unit;
@@ -13479,39 +13391,25 @@ queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
 }
 
 /* Free all resources associated with DWO_FILE.
-   Close the DWO file and munmap the sections.
-   All memory should be on the objfile obstack.  */
+   Close the DWO file and munmap the sections.  */
 
 static void
-free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
+free_dwo_file (struct dwo_file *dwo_file)
 {
-
   /* Note: dbfd is NULL for virtual DWO files.  */
   gdb_bfd_unref (dwo_file->dbfd);
 
   VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
 }
 
-/* Wrapper for free_dwo_file for use in cleanups.  */
-
-static void
-free_dwo_file_cleanup (void *arg)
-{
-  struct dwo_file *dwo_file = (struct dwo_file *) arg;
-  struct objfile *objfile = dwarf2_per_objfile->objfile;
-
-  free_dwo_file (dwo_file, objfile);
-}
-
 /* Traversal function for free_dwo_files.  */
 
 static int
 free_dwo_file_from_slot (void **slot, void *info)
 {
   struct dwo_file *dwo_file = (struct dwo_file *) *slot;
-  struct objfile *objfile = (struct objfile *) info;
 
-  free_dwo_file (dwo_file, objfile);
+  free_dwo_file (dwo_file);
 
   return 1;
 }
@@ -13562,10 +13460,9 @@ inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
   if (die->tag != origin_die->tag
       && !(die->tag == DW_TAG_inlined_subroutine
           && origin_die->tag == DW_TAG_subprogram))
-    complaint (&symfile_complaints,
-              _("DIE 0x%x and its abstract origin 0x%x have different tags"),
-              to_underlying (die->sect_off),
-              to_underlying (origin_die->sect_off));
+    complaint (_("DIE %s and its abstract origin %s have different tags"),
+              sect_offset_str (die->sect_off),
+              sect_offset_str (origin_die->sect_off));
 
   std::vector<sect_offset> offsets;
 
@@ -13609,17 +13506,15 @@ inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
          if (child_die->tag != child_origin_die->tag
              && !(child_die->tag == DW_TAG_inlined_subroutine
                   && child_origin_die->tag == DW_TAG_subprogram))
-           complaint (&symfile_complaints,
-                      _("Child DIE 0x%x and its abstract origin 0x%x have "
+           complaint (_("Child DIE %s and its abstract origin %s have "
                         "different tags"),
-                      to_underlying (child_die->sect_off),
-                      to_underlying (child_origin_die->sect_off));
+                      sect_offset_str (child_die->sect_off),
+                      sect_offset_str (child_origin_die->sect_off));
          if (child_origin_die->parent != origin_die)
-           complaint (&symfile_complaints,
-                      _("Child DIE 0x%x and its abstract origin 0x%x have "
+           complaint (_("Child DIE %s and its abstract origin %s have "
                         "different parents"),
-                      to_underlying (child_die->sect_off),
-                      to_underlying (child_origin_die->sect_off));
+                      sect_offset_str (child_die->sect_off),
+                      sect_offset_str (child_origin_die->sect_off));
          else
            offsets.push_back (child_origin_die->sect_off);
        }
@@ -13628,10 +13523,9 @@ inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
   sect_offset *offsets_end = offsets.data () + offsets.size ();
   for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
     if (offsetp[-1] == *offsetp)
-      complaint (&symfile_complaints,
-                _("Multiple children of DIE 0x%x refer "
-                  "to DIE 0x%x as their abstract origin"),
-                to_underlying (die->sect_off), to_underlying (*offsetp));
+      complaint (_("Multiple children of DIE %s refer "
+                  "to DIE %s as their abstract origin"),
+                sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
 
   offsetp = offsets.data ();
   origin_child_die = origin_die->child;
@@ -13659,7 +13553,7 @@ inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
 static void
 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
   struct context_stack *newobj;
   CORE_ADDR lowpc;
@@ -13695,9 +13589,8 @@ read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
      illegal according to the DWARF standard.  */
   if (name == NULL)
     {
-      complaint (&symfile_complaints,
-                _("missing name for subprogram DIE at %d"),
-                to_underlying (die->sect_off));
+      complaint (_("missing name for subprogram DIE at %s"),
+                sect_offset_str (die->sect_off));
       return;
     }
 
@@ -13707,10 +13600,9 @@ read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
     {
       attr = dwarf2_attr (die, DW_AT_external, cu);
       if (!attr || !DW_UNSND (attr))
-       complaint (&symfile_complaints,
-                  _("cannot get low and high bounds "
-                    "for subprogram DIE at %d"),
-                  to_underlying (die->sect_off));
+       complaint (_("cannot get low and high bounds "
+                    "for subprogram DIE at %s"),
+                  sect_offset_str (die->sect_off));
       return;
     }
 
@@ -13731,8 +13623,8 @@ read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
     }
 
   newobj = push_context (0, lowpc);
-  newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
-                              (struct symbol *) templ_func);
+  newobj->name = new_symbol (die, read_type_die (die, cu), cu,
+                            (struct symbol *) templ_func);
 
   /* If there is a location expression for DW_AT_frame_base, record
      it.  */
@@ -13850,7 +13742,7 @@ read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
 static void
 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
   struct context_stack *newobj;
   CORE_ADDR lowpc, highpc;
@@ -13921,7 +13813,7 @@ read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
 static void
 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
   CORE_ADDR pc, baseaddr;
   struct attribute *attr;
@@ -13941,10 +13833,9 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
     }
   if (!attr)
     {
-      complaint (&symfile_complaints,
-                _("missing DW_AT_call_return_pc for DW_TAG_call_site "
-                  "DIE 0x%x [in module %s]"),
-                to_underlying (die->sect_off), objfile_name (objfile));
+      complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
+                  "DIE %s [in module %s]"),
+                sect_offset_str (die->sect_off), objfile_name (objfile));
       return;
     }
   pc = attr_value_as_address (attr) + baseaddr;
@@ -13958,10 +13849,9 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
   if (*slot != NULL)
     {
-      complaint (&symfile_complaints,
-                _("Duplicate PC %s for DW_TAG_call_site "
-                  "DIE 0x%x [in module %s]"),
-                paddress (gdbarch, pc), to_underlying (die->sect_off),
+      complaint (_("Duplicate PC %s for DW_TAG_call_site "
+                  "DIE %s [in module %s]"),
+                paddress (gdbarch, pc), sect_offset_str (die->sect_off),
                 objfile_name (objfile));
       return;
     }
@@ -13975,10 +13865,9 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
       if (child_die->tag != DW_TAG_call_site_parameter
           && child_die->tag != DW_TAG_GNU_call_site_parameter)
        {
-         complaint (&symfile_complaints,
-                    _("Tag %d is not DW_TAG_call_site_parameter in "
-                      "DW_TAG_call_site child DIE 0x%x [in module %s]"),
-                    child_die->tag, to_underlying (child_die->sect_off),
+         complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
+                      "DW_TAG_call_site child DIE %s [in module %s]"),
+                    child_die->tag, sect_offset_str (child_die->sect_off),
                     objfile_name (objfile));
          continue;
        }
@@ -14038,10 +13927,9 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
              TYPE_TAIL_CALL_LIST (func_type) = call_site;
            }
          else
-           complaint (&symfile_complaints,
-                      _("Cannot find function owning DW_TAG_call_site "
-                        "DIE 0x%x [in module %s]"),
-                      to_underlying (die->sect_off), objfile_name (objfile));
+           complaint (_("Cannot find function owning DW_TAG_call_site "
+                        "DIE %s [in module %s]"),
+                      sect_offset_str (die->sect_off), objfile_name (objfile));
        }
     }
 
@@ -14075,7 +13963,7 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
       struct die_info *target_die;
 
       target_die = follow_die_ref (die, attr, &target_cu);
-      gdb_assert (target_cu->dwarf2_per_objfile->objfile == objfile);
+      gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
       if (die_is_declaration (target_die, target_cu))
        {
          const char *target_physname;
@@ -14085,10 +13973,9 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
          if (target_physname == NULL)
            target_physname = dwarf2_physname (NULL, target_die, target_cu);
          if (target_physname == NULL)
-           complaint (&symfile_complaints,
-                      _("DW_AT_call_target target DIE has invalid "
-                        "physname, for referencing DIE 0x%x [in module %s]"),
-                      to_underlying (die->sect_off), objfile_name (objfile));
+           complaint (_("DW_AT_call_target target DIE has invalid "
+                        "physname, for referencing DIE %s [in module %s]"),
+                      sect_offset_str (die->sect_off), objfile_name (objfile));
          else
            SET_FIELD_PHYSNAME (call_site->target, target_physname);
        }
@@ -14099,10 +13986,9 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
          /* DW_AT_entry_pc should be preferred.  */
          if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
              <= PC_BOUNDS_INVALID)
-           complaint (&symfile_complaints,
-                      _("DW_AT_call_target target DIE has invalid "
-                        "low pc, for referencing DIE 0x%x [in module %s]"),
-                      to_underlying (die->sect_off), objfile_name (objfile));
+           complaint (_("DW_AT_call_target target DIE has invalid "
+                        "low pc, for referencing DIE %s [in module %s]"),
+                      sect_offset_str (die->sect_off), objfile_name (objfile));
          else
            {
              lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
@@ -14111,10 +13997,9 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
        }
     }
   else
-    complaint (&symfile_complaints,
-              _("DW_TAG_call_site DW_AT_call_target is neither "
-                "block nor reference, for DIE 0x%x [in module %s]"),
-              to_underlying (die->sect_off), objfile_name (objfile));
+    complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
+                "block nor reference, for DIE %s [in module %s]"),
+              sect_offset_str (die->sect_off), objfile_name (objfile));
 
   call_site->per_cu = cu->per_cu;
 
@@ -14158,10 +14043,9 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
              /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
                 binding can be done only inside one CU.  Such referenced DIE
                 therefore cannot be even moved to DW_TAG_partial_unit.  */
-             complaint (&symfile_complaints,
-                        _("DW_AT_call_parameter offset is not in CU for "
-                          "DW_TAG_call_site child DIE 0x%x [in module %s]"),
-                        to_underlying (child_die->sect_off),
+             complaint (_("DW_AT_call_parameter offset is not in CU for "
+                          "DW_TAG_call_site child DIE %s [in module %s]"),
+                        sect_offset_str (child_die->sect_off),
                         objfile_name (objfile));
              continue;
            }
@@ -14170,10 +14054,9 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
        }
       else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
        {
-         complaint (&symfile_complaints,
-                    _("No DW_FORM_block* DW_AT_location for "
-                      "DW_TAG_call_site child DIE 0x%x [in module %s]"),
-                    to_underlying (child_die->sect_off), objfile_name (objfile));
+         complaint (_("No DW_FORM_block* DW_AT_location for "
+                      "DW_TAG_call_site child DIE %s [in module %s]"),
+                    sect_offset_str (child_die->sect_off), objfile_name (objfile));
          continue;
        }
       else
@@ -14188,12 +14071,11 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
            parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
          else
            {
-             complaint (&symfile_complaints,
-                        _("Only single DW_OP_reg or DW_OP_fbreg is supported "
+             complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
                           "for DW_FORM_block* DW_AT_location is supported for "
-                          "DW_TAG_call_site child DIE 0x%x "
+                          "DW_TAG_call_site child DIE %s "
                           "[in module %s]"),
-                        to_underlying (child_die->sect_off),
+                        sect_offset_str (child_die->sect_off),
                         objfile_name (objfile));
              continue;
            }
@@ -14204,10 +14086,9 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
        attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
       if (!attr_form_is_block (attr))
        {
-         complaint (&symfile_complaints,
-                    _("No DW_FORM_block* DW_AT_call_value for "
-                      "DW_TAG_call_site child DIE 0x%x [in module %s]"),
-                    to_underlying (child_die->sect_off),
+         complaint (_("No DW_FORM_block* DW_AT_call_value for "
+                      "DW_TAG_call_site child DIE %s [in module %s]"),
+                    sect_offset_str (child_die->sect_off),
                     objfile_name (objfile));
          continue;
        }
@@ -14225,10 +14106,9 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
       if (attr)
        {
          if (!attr_form_is_block (attr))
-           complaint (&symfile_complaints,
-                      _("No DW_FORM_block* DW_AT_call_data_value for "
-                        "DW_TAG_call_site child DIE 0x%x [in module %s]"),
-                      to_underlying (child_die->sect_off),
+           complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
+                        "DW_TAG_call_site child DIE %s [in module %s]"),
+                      sect_offset_str (child_die->sect_off),
                       objfile_name (objfile));
          else
            {
@@ -14277,7 +14157,7 @@ read_variable (struct die_info *die, struct dwarf2_cu *cu)
 
       if (containing_type != NULL)
        {
-         struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+         struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
 
          storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
                                    struct rust_vtable_symbol);
@@ -14287,7 +14167,7 @@ read_variable (struct die_info *die, struct dwarf2_cu *cu)
        }
     }
 
-  new_symbol_full (die, NULL, cu, storage);
+  new_symbol (die, NULL, cu, storage);
 }
 
 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
@@ -14302,7 +14182,9 @@ static bool
 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
                         Callback &&callback)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
   bfd *obfd = objfile->obfd;
   /* Base address selection entry.  */
   CORE_ADDR base;
@@ -14317,8 +14199,7 @@ dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
   dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
   if (offset >= dwarf2_per_objfile->rnglists.size)
     {
-      complaint (&symfile_complaints,
-                _("Offset %d out of bounds for DW_AT_ranges attribute"),
+      complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
                 offset);
       return false;
     }
@@ -14399,8 +14280,7 @@ dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
          buffer += bytes_read;
          break;
        default:
-         complaint (&symfile_complaints,
-                    _("Invalid .debug_rnglists data (no base address)"));
+         complaint (_("Invalid .debug_rnglists data (no base address)"));
          return false;
        }
       if (rlet == DW_RLE_end_of_list || overflow)
@@ -14412,16 +14292,14 @@ dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
        {
          /* We have no valid base address for the ranges
             data.  */
-         complaint (&symfile_complaints,
-                    _("Invalid .debug_rnglists data (no base address)"));
+         complaint (_("Invalid .debug_rnglists data (no base address)"));
          return false;
        }
 
       if (range_beginning > range_end)
        {
          /* Inverted range entries are invalid.  */
-         complaint (&symfile_complaints,
-                    _("Invalid .debug_rnglists data (inverted range)"));
+         complaint (_("Invalid .debug_rnglists data (inverted range)"));
          return false;
        }
 
@@ -14437,8 +14315,7 @@ dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
       if (range_beginning + baseaddr == 0
          && !dwarf2_per_objfile->has_section_at_zero)
        {
-         complaint (&symfile_complaints,
-                    _(".debug_rnglists entry has start address of zero"
+         complaint (_(".debug_rnglists entry has start address of zero"
                       " [in module %s]"), objfile_name (objfile));
          continue;
        }
@@ -14448,8 +14325,7 @@ dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
 
   if (overflow)
     {
-      complaint (&symfile_complaints,
-                _("Offset %d is not terminated "
+      complaint (_("Offset %d is not terminated "
                   "for DW_AT_ranges attribute"),
                 offset);
       return false;
@@ -14468,7 +14344,9 @@ static int
 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
                       Callback &&callback)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+      = cu->per_cu->dwarf2_per_objfile;
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct comp_unit_head *cu_header = &cu->header;
   bfd *obfd = objfile->obfd;
   unsigned int addr_size = cu_header->addr_size;
@@ -14489,8 +14367,7 @@ dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
   if (offset >= dwarf2_per_objfile->ranges.size)
     {
-      complaint (&symfile_complaints,
-                _("Offset %d out of bounds for DW_AT_ranges attribute"),
+      complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
                 offset);
       return 0;
     }
@@ -14529,16 +14406,14 @@ dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
        {
          /* We have no valid base address for the ranges
             data.  */
-         complaint (&symfile_complaints,
-                    _("Invalid .debug_ranges data (no base address)"));
+         complaint (_("Invalid .debug_ranges data (no base address)"));
          return 0;
        }
 
       if (range_beginning > range_end)
        {
          /* Inverted range entries are invalid.  */
-         complaint (&symfile_complaints,
-                    _("Invalid .debug_ranges data (inverted range)"));
+         complaint (_("Invalid .debug_ranges data (inverted range)"));
          return 0;
        }
 
@@ -14554,8 +14429,7 @@ dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
       if (range_beginning + baseaddr == 0
          && !dwarf2_per_objfile->has_section_at_zero)
        {
-         complaint (&symfile_complaints,
-                    _(".debug_ranges entry has start address of zero"
+         complaint (_(".debug_ranges entry has start address of zero"
                       " [in module %s]"), objfile_name (objfile));
          continue;
        }
@@ -14575,7 +14449,7 @@ dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
                    CORE_ADDR *high_return, struct dwarf2_cu *cu,
                    struct partial_symtab *ranges_pst)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
   const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
                                       SECT_OFF_TEXT (objfile));
@@ -14642,6 +14516,8 @@ dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
                      CORE_ADDR *highpc, struct dwarf2_cu *cu,
                      struct partial_symtab *pst)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
   struct attribute *attr;
   struct attribute *attr_high;
   CORE_ADDR low = 0;
@@ -14691,7 +14567,7 @@ dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
        return PC_BOUNDS_NOT_PRESENT;
     }
 
-  /* read_partial_die has also the strict LOW < HIGH requirement.  */
+  /* partial_die_info::read has also the strict LOW < HIGH requirement.  */
   if (high <= low)
     return PC_BOUNDS_INVALID;
 
@@ -14816,7 +14692,7 @@ static void
 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
   struct attribute *attr;
   struct attribute *attr_high;
@@ -14851,24 +14727,6 @@ dwarf2_record_block_ranges (struct die_info *die, struct block *block,
          address range list in the .debug_ranges section.  */
       unsigned long offset = (DW_UNSND (attr)
                              + (need_ranges_base ? cu->ranges_base : 0));
-      const gdb_byte *buffer;
-
-      /* For some target architectures, but not others, the
-         read_address function sign-extends the addresses it returns.
-         To recognize base address selection entries, we need a
-         mask.  */
-      unsigned int addr_size = cu->header.addr_size;
-      CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
-
-      /* The base address, to which the next pair is relative.  Note
-         that this 'base' is a DWARF concept: most entries in a range
-         list are relative, to reduce the number of relocs against the
-         debugging information.  This is separate from this function's
-         'baseaddr' argument, which GDB uses to relocate debugging
-         information from a shared library based on the address at
-         which the library was loaded.  */
-      CORE_ADDR base = cu->base_address;
-      int base_known = cu->base_known;
 
       dwarf2_ranges_process (offset, cu,
        [&] (CORE_ADDR start, CORE_ADDR end)
@@ -15000,28 +14858,24 @@ static void
 dwarf2_add_field (struct field_info *fip, struct die_info *die,
                  struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
   struct nextfield *new_field;
   struct attribute *attr;
   struct field *fp;
   const char *fieldname = "";
 
-  /* Allocate a new field list entry and link it in.  */
-  new_field = XNEW (struct nextfield);
-  make_cleanup (xfree, new_field);
-  memset (new_field, 0, sizeof (struct nextfield));
-
   if (die->tag == DW_TAG_inheritance)
     {
-      new_field->next = fip->baseclasses;
-      fip->baseclasses = new_field;
+      fip->baseclasses.emplace_back ();
+      new_field = &fip->baseclasses.back ();
     }
   else
     {
-      new_field->next = fip->fields;
-      fip->fields = new_field;
+      fip->fields.emplace_back ();
+      new_field = &fip->fields.back ();
     }
+
   fip->nfields++;
 
   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
@@ -15181,8 +15035,21 @@ dwarf2_add_field (struct field_info *fip, struct die_info *die,
       FIELD_BITSIZE (*fp) = 0;
       FIELD_TYPE (*fp) = die_type (die, cu);
       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
-      fip->nbaseclasses++;
     }
+  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>>";
+    }
+  else
+    gdb_assert_not_reached ("missing case in dwarf2_add_field");
 }
 
 /* Can the type given by DIE define another type?  */
@@ -15210,20 +15077,14 @@ static void
 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
                      struct dwarf2_cu *cu)
 {
-  struct decl_field_list *new_field;
-  struct decl_field *fp;
-
-  /* Allocate a new field list entry and link it in.  */
-  new_field = XCNEW (struct decl_field_list);
-  make_cleanup (xfree, new_field);
+  struct decl_field fp;
+  memset (&fp, 0, sizeof (fp));
 
   gdb_assert (type_can_define_types (die));
 
-  fp = &new_field->field;
-
   /* Get name of field.  NULL is okay here, meaning an anonymous type.  */
-  fp->name = dwarf2_name (die, cu);
-  fp->type = read_type_die (die, cu);
+  fp.name = dwarf2_name (die, cu);
+  fp.type = read_type_die (die, cu);
 
   /* Save accessibility.  */
   enum dwarf_access_attribute accessibility;
@@ -15238,28 +15099,19 @@ dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
       /* The assumed value if neither private nor protected.  */
       break;
     case DW_ACCESS_private:
-      fp->is_private = 1;
+      fp.is_private = 1;
       break;
     case DW_ACCESS_protected:
-      fp->is_protected = 1;
+      fp.is_protected = 1;
       break;
     default:
-      complaint (&symfile_complaints,
-                _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
+      complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
     }
 
   if (die->tag == DW_TAG_typedef)
-    {
-      new_field->next = fip->typedef_field_list;
-      fip->typedef_field_list = new_field;
-      fip->typedef_field_list_count++;
-    }
+    fip->typedef_field_list.push_back (fp);
   else
-    {
-      new_field->next = fip->nested_types_list;
-      fip->nested_types_list = new_field;
-      fip->nested_types_list_count++;
-    }
+    fip->nested_types_list.push_back (fp);
 }
 
 /* Create the vector of fields, and attach it to the type.  */
@@ -15274,8 +15126,7 @@ dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
      and create blank accessibility bitfields if necessary.  */
   TYPE_NFIELDS (type) = nfields;
   TYPE_FIELDS (type) = (struct field *)
-    TYPE_ALLOC (type, sizeof (struct field) * nfields);
-  memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
+    TYPE_ZALLOC (type, sizeof (struct field) * nfields);
 
   if (fip->non_public_fields && cu->language != language_ada)
     {
@@ -15296,47 +15147,53 @@ dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
 
   /* If the type has baseclasses, allocate and clear a bit vector for
      TYPE_FIELD_VIRTUAL_BITS.  */
-  if (fip->nbaseclasses && cu->language != language_ada)
+  if (!fip->baseclasses.empty () && cu->language != language_ada)
     {
-      int num_bytes = B_BYTES (fip->nbaseclasses);
+      int num_bytes = B_BYTES (fip->baseclasses.size ());
       unsigned char *pointer;
 
       ALLOCATE_CPLUS_STRUCT_TYPE (type);
       pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
-      B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
-      TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
+      B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
+      TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
     }
 
-  /* Copy the saved-up fields into the field vector.  Start from the head of
-     the list, adding to the tail of the field array, so that they end up in
-     the same order in the array in which they were added to the list.  */
-  while (nfields-- > 0)
+  if (TYPE_FLAG_DISCRIMINATED_UNION (type))
     {
-      struct nextfield *fieldp;
+      struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
 
-      if (fip->fields)
-       {
-         fieldp = fip->fields;
-         fip->fields = fieldp->next;
-       }
-      else
+      for (int index = 0; index < nfields; ++index)
        {
-         fieldp = fip->baseclasses;
-         fip->baseclasses = fieldp->next;
+         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;
        }
+    }
+
+  /* Copy the saved-up fields into the field vector.  */
+  for (int i = 0; i < nfields; ++i)
+    {
+      struct nextfield &field
+       = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
+          : fip->fields[i - fip->baseclasses.size ()]);
 
-      TYPE_FIELD (type, nfields) = fieldp->field;
-      switch (fieldp->accessibility)
+      TYPE_FIELD (type, i) = field.field;
+      switch (field.accessibility)
        {
        case DW_ACCESS_private:
          if (cu->language != language_ada)
-           SET_TYPE_FIELD_PRIVATE (type, nfields);
+           SET_TYPE_FIELD_PRIVATE (type, i);
          break;
 
        case DW_ACCESS_protected:
          if (cu->language != language_ada)
-           SET_TYPE_FIELD_PROTECTED (type, nfields);
+           SET_TYPE_FIELD_PROTECTED (type, i);
          break;
 
        case DW_ACCESS_public:
@@ -15345,20 +15202,20 @@ dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
        default:
          /* Unknown accessibility.  Complain and treat it as public.  */
          {
-           complaint (&symfile_complaints, _("unsupported accessibility %d"),
-                      fieldp->accessibility);
+           complaint (_("unsupported accessibility %d"),
+                      field.accessibility);
          }
          break;
        }
-      if (nfields < fip->nbaseclasses)
+      if (i < fip->baseclasses.size ())
        {
-         switch (fieldp->virtuality)
+         switch (field.virtuality)
            {
            case DW_VIRTUALITY_virtual:
            case DW_VIRTUALITY_pure_virtual:
              if (cu->language == language_ada)
                error (_("unexpected virtuality in component of Ada type"));
-             SET_TYPE_FIELD_VIRTUAL (type, nfields);
+             SET_TYPE_FIELD_VIRTUAL (type, i);
              break;
            }
        }
@@ -15399,13 +15256,12 @@ static void
 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
                      struct type *type, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct attribute *attr;
-  struct fnfieldlist *flp;
   int i;
+  struct fnfieldlist *flp = nullptr;
   struct fn_field *fnp;
   const char *fieldname;
-  struct nextfnfield *new_fnfield;
   struct type *this_type;
   enum dwarf_access_attribute accessibility;
 
@@ -15418,51 +15274,33 @@ dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
     return;
 
   /* Look up member function name in fieldlist.  */
-  for (i = 0; i < fip->nfnfields; i++)
+  for (i = 0; i < fip->fnfieldlists.size (); i++)
     {
       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
-       break;
+       {
+         flp = &fip->fnfieldlists[i];
+         break;
+       }
     }
 
-  /* Create new list element if necessary.  */
-  if (i < fip->nfnfields)
-    flp = &fip->fnfieldlists[i];
-  else
+  /* Create a new fnfieldlist if necessary.  */
+  if (flp == nullptr)
     {
-      if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
-       {
-         fip->fnfieldlists = (struct fnfieldlist *)
-           xrealloc (fip->fnfieldlists,
-                     (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
-                     * sizeof (struct fnfieldlist));
-         if (fip->nfnfields == 0)
-           make_cleanup (free_current_contents, &fip->fnfieldlists);
-       }
-      flp = &fip->fnfieldlists[fip->nfnfields];
+      fip->fnfieldlists.emplace_back ();
+      flp = &fip->fnfieldlists.back ();
       flp->name = fieldname;
-      flp->length = 0;
-      flp->head = NULL;
-      i = fip->nfnfields++;
+      i = fip->fnfieldlists.size () - 1;
     }
 
-  /* Create a new member function field and chain it to the field list
-     entry.  */
-  new_fnfield = XNEW (struct nextfnfield);
-  make_cleanup (xfree, new_fnfield);
-  memset (new_fnfield, 0, sizeof (struct nextfnfield));
-  new_fnfield->next = flp->head;
-  flp->head = new_fnfield;
-  flp->length++;
-
-  /* Fill in the member function field info.  */
-  fnp = &new_fnfield->fnfield;
+  /* Create a new member function field and add it to the vector of
+     fnfieldlists.  */
+  flp->fnfields.emplace_back ();
+  fnp = &flp->fnfields.back ();
 
   /* Delay processing of the physname until later.  */
   if (cu->language == language_cplus)
-    {
-      add_to_method_list (type, i, flp->length - 1, fieldname,
-                         die, cu);
-    }
+    add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
+                       die, cu);
   else
     {
       const char *physname = dwarf2_physname (fieldname, die, cu);
@@ -15493,7 +15331,7 @@ dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
        fnp->voffset = VOFFSET_STATIC;
     }
   else
-    complaint (&symfile_complaints, _("member function type missing for '%s'"),
+    complaint (_("member function type missing for '%s'"),
               dwarf2_full_name (fieldname, die, cu));
 
   /* Get fcontext from DW_AT_containing_type if present.  */
@@ -15565,10 +15403,9 @@ dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
              if (TYPE_NFIELDS (this_type) == 0
                  || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
                {
-                 complaint (&symfile_complaints,
-                            _("cannot determine context for virtual member "
-                              "function \"%s\" (offset %d)"),
-                            fieldname, to_underlying (die->sect_off));
+                 complaint (_("cannot determine context for virtual member "
+                              "function \"%s\" (offset %s)"),
+                            fieldname, sect_offset_str (die->sect_off));
                }
              else
                {
@@ -15593,10 +15430,9 @@ dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
       if (attr && DW_UNSND (attr))
        {
          /* GCC does this, as of 2008-08-25; PR debug/37237.  */
-         complaint (&symfile_complaints,
-                    _("Member function \"%s\" (offset %d) is virtual "
+         complaint (_("Member function \"%s\" (offset %s) is virtual "
                       "but the vtable offset is not specified"),
-                    fieldname, to_underlying (die->sect_off));
+                    fieldname, sect_offset_str (die->sect_off));
          ALLOCATE_CPLUS_STRUCT_TYPE (type);
          TYPE_CPLUS_DYNAMIC (type) = 1;
        }
@@ -15609,31 +15445,29 @@ static void
 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
                                 struct dwarf2_cu *cu)
 {
-  struct fnfieldlist *flp;
-  int i;
-
   if (cu->language == language_ada)
     error (_("unexpected member functions in Ada type"));
 
   ALLOCATE_CPLUS_STRUCT_TYPE (type);
   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
-    TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
+    TYPE_ALLOC (type,
+               sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
 
-  for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
+  for (int i = 0; i < fip->fnfieldlists.size (); i++)
     {
-      struct nextfnfield *nfp = flp->head;
+      struct fnfieldlist &nf = fip->fnfieldlists[i];
       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
-      int k;
 
-      TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
-      TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
+      TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
+      TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
       fn_flp->fn_fields = (struct fn_field *)
-       TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
-      for (k = flp->length; (k--, nfp); nfp = nfp->next)
-       fn_flp->fn_fields[k] = nfp->fnfield;
+       TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
+
+      for (int k = 0; k < nf.fnfields.size (); ++k)
+       fn_flp->fn_fields[k] = nf.fnfields[k];
     }
 
-  TYPE_NFN_FIELDS (type) = fip->nfnfields;
+  TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
 }
 
 /* Returns non-zero if NAME is the name of a vtable member in CU's
@@ -15695,6 +15529,77 @@ quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
   smash_to_methodptr_type (type, new_type);
 }
 
+/* If the DIE has a DW_AT_alignment attribute, return its value, doing
+   appropriate error checking and issuing complaints if there is a
+   problem.  */
+
+static ULONGEST
+get_alignment (struct dwarf2_cu *cu, struct die_info *die)
+{
+  struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
+
+  if (attr == nullptr)
+    return 0;
+
+  if (!attr_form_is_constant (attr))
+    {
+      complaint (_("DW_AT_alignment must have constant form"
+                  " - DIE at %s [in module %s]"),
+                sect_offset_str (die->sect_off),
+                objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
+      return 0;
+    }
+
+  ULONGEST align;
+  if (attr->form == DW_FORM_sdata)
+    {
+      LONGEST val = DW_SND (attr);
+      if (val < 0)
+       {
+         complaint (_("DW_AT_alignment value must not be negative"
+                      " - DIE at %s [in module %s]"),
+                    sect_offset_str (die->sect_off),
+                    objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
+         return 0;
+       }
+      align = val;
+    }
+  else
+    align = DW_UNSND (attr);
+
+  if (align == 0)
+    {
+      complaint (_("DW_AT_alignment value must not be zero"
+                  " - DIE at %s [in module %s]"),
+                sect_offset_str (die->sect_off),
+                objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
+      return 0;
+    }
+  if ((align & (align - 1)) != 0)
+    {
+      complaint (_("DW_AT_alignment value must be a power of 2"
+                  " - DIE at %s [in module %s]"),
+                sect_offset_str (die->sect_off),
+                objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
+      return 0;
+    }
+
+  return align;
+}
+
+/* If the DIE has a DW_AT_alignment attribute, use its value to set
+   the alignment for TYPE.  */
+
+static void
+maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
+                    struct type *type)
+{
+  if (!set_type_align (type, get_alignment (cu, die)))
+    complaint (_("DW_AT_alignment value too large"
+                " - DIE at %s [in module %s]"),
+              sect_offset_str (die->sect_off),
+              objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
+}
 
 /* Called when we find the DIE that starts a structure or union scope
    (definition) to create a type for the structure or union.  Fill in
@@ -15711,7 +15616,7 @@ quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
 static struct type *
 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct type *type;
   struct attribute *attr;
   const char *name;
@@ -15746,18 +15651,13 @@ 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_TAG_NAME (type) = full_name;
-         if (die->tag == DW_TAG_structure_type
-             || die->tag == DW_TAG_class_type)
-           TYPE_NAME (type) = TYPE_TAG_NAME (type);
+         TYPE_NAME (type) = full_name;
        }
       else
        {
          /* The name is already allocated along with this objfile, so
             we don't need to duplicate it for the type.  */
-         TYPE_TAG_NAME (type) = name;
-         if (die->tag == DW_TAG_class_type)
-           TYPE_NAME (type) = TYPE_TAG_NAME (type);
+         TYPE_NAME (type) = name;
        }
     }
 
@@ -15769,6 +15669,11 @@ read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
     {
       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;
+    }
   else
     {
       TYPE_CODE (type) = TYPE_CODE_STRUCT;
@@ -15800,6 +15705,8 @@ read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
       TYPE_LENGTH (type) = 0;
     }
 
+  maybe_set_alignment (cu, die, type);
+
   if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
     {
       /* ICC<14 does not output the required DW_AT_declaration on
@@ -15828,13 +15735,98 @@ 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)
+{
+  if (child_die->tag == DW_TAG_member
+      || child_die->tag == DW_TAG_variable
+      || child_die->tag == DW_TAG_variant_part)
+    {
+      /* 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
+       {
+         /* 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)
+    {
+      /* 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);
+
+      for (struct 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;
+           }
+       }
+
+      /* 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));
+
+      /* 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;
+      else
+       fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
+    }
+}
+
 /* Finish creating a structure or union type, including filling in
    its members and creating a symbol for it.  */
 
 static void
 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct die_info *child_die;
   struct type *type;
 
@@ -15842,56 +15834,51 @@ 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;
+
+  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 (attr_form_is_ref (discr))
+       {
+         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;
       std::vector<struct symbol *> template_args;
-      struct cleanup *back_to = make_cleanup (null_cleanup, 0);
-
-      memset (&fi, 0, sizeof (struct field_info));
 
       child_die = die->child;
 
       while (child_die && child_die->tag)
        {
-         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
-               {
-                 /* 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);
+         handle_struct_member_die (child_die, type, &fi, &template_args, cu);
 
-             if (arg != NULL)
-               template_args.push_back (arg);
-           }
+         if (is_variant_part && discr_offset == child_die->sect_off)
+           fi.fields.back ().variant.is_discriminant = true;
 
          child_die = sibling_die (child_die);
        }
@@ -15914,7 +15901,7 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
       /* Attach fields and member functions to the type.  */
       if (fi.nfields)
        dwarf2_attach_fields_to_type (&fi, type, cu);
-      if (fi.nfnfields)
+      if (!fi.fnfieldlists.empty ())
        {
          dwarf2_attach_fn_fields_to_type (&fi, type, cu);
 
@@ -15948,11 +15935,9 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
 
                  /* Complain if virtual function table field not found.  */
                  if (i < TYPE_N_BASECLASSES (t))
-                   complaint (&symfile_complaints,
-                              _("virtual function table pointer "
+                   complaint (_("virtual function table pointer "
                                 "not found when defining class '%s'"),
-                              TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
-                              "");
+                              TYPE_NAME (type) ? TYPE_NAME (type) : "");
                }
              else
                {
@@ -15984,56 +15969,41 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
 
       /* Copy fi.typedef_field_list linked list elements content into the
         allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
-      if (fi.typedef_field_list)
+      if (!fi.typedef_field_list.empty ())
        {
-         int i = fi.typedef_field_list_count;
+         int count = fi.typedef_field_list.size ();
 
          ALLOCATE_CPLUS_STRUCT_TYPE (type);
          TYPE_TYPEDEF_FIELD_ARRAY (type)
            = ((struct decl_field *)
-              TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
-         TYPE_TYPEDEF_FIELD_COUNT (type) = i;
-
-         /* Reverse the list order to keep the debug info elements order.  */
-         while (--i >= 0)
-           {
-             struct decl_field *dest, *src;
+              TYPE_ALLOC (type,
+                          sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
+         TYPE_TYPEDEF_FIELD_COUNT (type) = count;
 
-             dest = &TYPE_TYPEDEF_FIELD (type, i);
-             src = &fi.typedef_field_list->field;
-             fi.typedef_field_list = fi.typedef_field_list->next;
-             *dest = *src;
-           }
+         for (int i = 0; i < fi.typedef_field_list.size (); ++i)
+           TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
        }
 
       /* Copy fi.nested_types_list linked list elements content into the
         allocated array TYPE_NESTED_TYPES_ARRAY (type).  */
-      if (fi.nested_types_list != NULL && cu->language != language_ada)
+      if (!fi.nested_types_list.empty () && cu->language != language_ada)
        {
-         int i = fi.nested_types_list_count;
+         int count = fi.nested_types_list.size ();
 
          ALLOCATE_CPLUS_STRUCT_TYPE (type);
          TYPE_NESTED_TYPES_ARRAY (type)
            = ((struct decl_field *)
-              TYPE_ALLOC (type, sizeof (struct decl_field) * i));
-         TYPE_NESTED_TYPES_COUNT (type) = i;
-
-         /* Reverse the list order to keep the debug info elements order.  */
-         while (--i >= 0)
-           {
-             struct decl_field *dest, *src;
+              TYPE_ALLOC (type, sizeof (struct decl_field) * count));
+         TYPE_NESTED_TYPES_COUNT (type) = count;
 
-             dest = &TYPE_NESTED_TYPES_FIELD (type, i);
-             src = &fi.nested_types_list->field;
-             fi.nested_types_list = fi.nested_types_list->next;
-             *dest = *src;
-           }
+         for (int i = 0; i < fi.nested_types_list.size (); ++i)
+           TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
        }
-
-      do_cleanups (back_to);
     }
 
   quirk_gcc_member_function_pointer (type, objfile);
+  if (cu->language == language_rust && die->tag == DW_TAG_union_type)
+    cu->rust_unions.push_back (type);
 
   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
      snapshots) has been known to create a die giving a declaration
@@ -16134,7 +16104,7 @@ update_enumeration_type_from_children (struct die_info *die,
 static struct type *
 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct type *type;
   struct attribute *attr;
   const char *name;
@@ -16157,7 +16127,7 @@ read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
   TYPE_CODE (type) = TYPE_CODE_ENUM;
   name = dwarf2_full_name (NULL, die, cu);
   if (name != NULL)
-    TYPE_TAG_NAME (type) = name;
+    TYPE_NAME (type) = name;
 
   attr = dwarf2_attr (die, DW_AT_type, cu);
   if (attr != NULL)
@@ -16177,6 +16147,8 @@ read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
       TYPE_LENGTH (type) = 0;
     }
 
+  maybe_set_alignment (cu, die, type);
+
   /* The enumeration DIE can be incomplete.  In Ada, any type can be
      declared as private in the package spec, and then defined only
      inside the package body.  Such types are known as Taft Amendment
@@ -16202,6 +16174,9 @@ read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
       TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
       if (TYPE_LENGTH (type) == 0)
        TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (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_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
@@ -16306,7 +16281,7 @@ process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
 static struct type *
 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct die_info *child_die;
   struct type *type;
   struct type *element_type, *range_type, *index_type;
@@ -16332,11 +16307,10 @@ read_array_type (struct die_info *die, struct dwarf2_cu *cu)
       stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
       if (!stride_ok)
        {
-         complaint (&symfile_complaints,
-                    _("unable to read array DW_AT_byte_stride "
-                      " - DIE at 0x%x [in module %s]"),
-                    to_underlying (die->sect_off),
-                    objfile_name (cu->dwarf2_per_objfile->objfile));
+         complaint (_("unable to read array DW_AT_byte_stride "
+                      " - DIE at %s [in module %s]"),
+                    sect_offset_str (die->sect_off),
+                    objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
          /* Ignore this attribute.  We will likely not be able to print
             arrays of this type correctly, but there is little we can do
             to help if we cannot read the attribute's value.  */
@@ -16417,8 +16391,7 @@ read_array_type (struct die_info *die, struct dwarf2_cu *cu)
       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
        TYPE_LENGTH (type) = DW_UNSND (attr);
       else
-       complaint (&symfile_complaints,
-                  _("DW_AT_byte_size for array type smaller "
+       complaint (_("DW_AT_byte_size for array type smaller "
                     "than the total size of elements"));
     }
 
@@ -16426,6 +16399,8 @@ read_array_type (struct die_info *die, struct dwarf2_cu *cu)
   if (name)
     TYPE_NAME (type) = name;
 
+  maybe_set_alignment (cu, die, type);
+
   /* Install the type in the die.  */
   set_die_type (die, type, cu);
 
@@ -16490,6 +16465,8 @@ read_set_type (struct die_info *die, struct dwarf2_cu *cu)
   if (attr)
     TYPE_LENGTH (set_type) = DW_UNSND (attr);
 
+  maybe_set_alignment (cu, die, set_type);
+
   return set_die_type (die, set_type, cu);
 }
 
@@ -16509,6 +16486,8 @@ mark_common_block_symbol_computed (struct symbol *sym,
                                   struct attribute *member_loc,
                                   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 dwarf2_locexpr_baton *baton;
   gdb_byte *ptr;
@@ -16598,7 +16577,7 @@ read_common_block (struct die_info *die, struct dwarf2_cu *cu)
 
   if (die->child != NULL)
     {
-      struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+      struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
       struct die_info *child_die;
       size_t n_entries = 0, size;
       struct common_block *common_block;
@@ -16638,12 +16617,11 @@ read_common_block (struct die_info *die, struct dwarf2_cu *cu)
                     not specified by DWARF.  It seems to have been
                     emitted by gfortran at least as recently as:
                     http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057.  */
-                 complaint (&symfile_complaints,
-                            _("Variable in common block has "
+                 complaint (_("Variable in common block has "
                               "DW_AT_data_member_location "
-                              "- DIE at 0x%x [in module %s]"),
-                            to_underlying (child_die->sect_off),
-                            objfile_name (cu->dwarf2_per_objfile->objfile));
+                              "- DIE at %s [in module %s]"),
+                              sect_offset_str (child_die->sect_off),
+                            objfile_name (objfile));
 
                  if (attr_form_is_section_offset (member_loc))
                    dwarf2_complex_location_expr_complaint ();
@@ -16670,7 +16648,7 @@ read_common_block (struct die_info *die, struct dwarf2_cu *cu)
 static struct type *
 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   const char *previous_prefix, *name;
   int is_anonymous;
   struct type *type;
@@ -16700,7 +16678,6 @@ read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
 
   /* Create the type.  */
   type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
-  TYPE_TAG_NAME (type) = TYPE_NAME (type);
 
   return set_die_type (die, type, cu);
 }
@@ -16710,7 +16687,7 @@ read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
 static void
 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   int is_anonymous;
 
   /* Add a symbol associated to this if we haven't seen the namespace
@@ -16755,20 +16732,16 @@ read_namespace (struct die_info *die, struct dwarf2_cu *cu)
 static struct type *
 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   const char *module_name;
   struct type *type;
 
   module_name = dwarf2_name (die, cu);
   if (!module_name)
-    complaint (&symfile_complaints,
-              _("DW_TAG_module has no name, offset 0x%x"),
-               to_underlying (die->sect_off));
+    complaint (_("DW_TAG_module has no name, offset %s"),
+               sect_offset_str (die->sect_off));
   type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
 
-  /* determine_prefix uses TYPE_TAG_NAME.  */
-  TYPE_TAG_NAME (type) = TYPE_NAME (type);
-
   return set_die_type (die, type, cu);
 }
 
@@ -16829,7 +16802,8 @@ namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
 static struct type *
 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct gdbarch *gdbarch = get_objfile_arch (cu->dwarf2_per_objfile->objfile);
+  struct gdbarch *gdbarch
+    = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
   struct comp_unit_head *cu_header = &cu->header;
   struct type *type;
   struct attribute *attr_byte_size;
@@ -16858,10 +16832,15 @@ read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
   else
     addr_class = DW_ADDR_none;
 
-  /* If the pointer size or address class is different than the
-     default, create a type variant marked as such and set the
-     length accordingly.  */
-  if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
+  ULONGEST alignment = get_alignment (cu, die);
+
+  /* If the pointer size, alignment, or address class is different
+     than the default, create a type variant marked as such and set
+     the length accordingly.  */
+  if (TYPE_LENGTH (type) != byte_size
+      || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
+         && alignment != TYPE_RAW_ALIGN (type))
+      || addr_class != DW_ADDR_none)
     {
       if (gdbarch_address_class_type_flags_p (gdbarch))
        {
@@ -16875,8 +16854,14 @@ read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
        }
       else if (TYPE_LENGTH (type) != byte_size)
        {
-         complaint (&symfile_complaints,
-                    _("invalid pointer size %d"), byte_size);
+         complaint (_("invalid pointer size %d"), byte_size);
+       }
+      else if (TYPE_RAW_ALIGN (type) != alignment)
+       {
+         complaint (_("Invalid DW_AT_alignment"
+                      " - DIE at %s [in module %s]"),
+                    sect_offset_str (die->sect_off),
+                    objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
        }
       else
        {
@@ -16885,6 +16870,7 @@ read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
     }
 
   TYPE_LENGTH (type) = byte_size;
+  set_type_align (type, alignment);
   return set_die_type (die, type, cu);
 }
 
@@ -16910,7 +16896,8 @@ read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
     type = lookup_methodptr_type (to_type);
   else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
     {
-      struct type *new_type = alloc_type (cu->dwarf2_per_objfile->objfile);
+      struct type *new_type
+       = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
 
       smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
                            TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
@@ -16953,6 +16940,7 @@ read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
     {
       TYPE_LENGTH (type) = cu_header->addr_size;
     }
+  maybe_set_alignment (cu, die, type);
   return set_die_type (die, type, cu);
 }
 
@@ -17073,7 +17061,7 @@ read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
 static struct type *
 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
   struct type *type, *range_type, *index_type, *char_type;
   struct attribute *attr;
@@ -17151,7 +17139,7 @@ prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
 static struct type *
 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct type *type;           /* Type that this function returns.  */
   struct type *ftype;          /* Function that returns above type.  */
   struct attribute *attr;
@@ -17288,7 +17276,7 @@ read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
 static struct type *
 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   const char *name = NULL;
   struct type *this_type, *target_type;
 
@@ -17303,10 +17291,9 @@ read_typedef (struct die_info *die, struct dwarf2_cu *cu)
     {
       /* Self-referential typedefs are, it seems, not allowed by the DWARF
         spec and cause infinite loops in GDB.  */
-      complaint (&symfile_complaints,
-                _("Self-referential DW_TAG_typedef "
-                  "- DIE at 0x%x [in module %s]"),
-                to_underlying (die->sect_off), objfile_name (objfile));
+      complaint (_("Self-referential DW_TAG_typedef "
+                  "- DIE at %s [in module %s]"),
+                sect_offset_str (die->sect_off), objfile_name (objfile));
       TYPE_TARGET_TYPE (this_type) = NULL;
     }
   return this_type;
@@ -17339,7 +17326,7 @@ dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
 static struct type *
 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct type *type;
   struct attribute *attr;
   int encoding = 0, bits = 0;
@@ -17358,8 +17345,7 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu)
   name = dwarf2_name (die, cu);
   if (!name)
     {
-      complaint (&symfile_complaints,
-                _("DW_AT_name missing from DW_TAG_base_type"));
+      complaint (_("DW_AT_name missing from DW_TAG_base_type"));
     }
 
   switch (encoding)
@@ -17420,8 +17406,7 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu)
            type = builtin_type (arch)->builtin_char32;
          else
            {
-             complaint (&symfile_complaints,
-                        _("unsupported DW_ATE_UTF bit size: '%d'"),
+             complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
                         bits);
              type = init_integer_type (objfile, bits, 1, name);
            }
@@ -17430,7 +17415,7 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu)
        break;
 
       default:
-       complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
+       complaint (_("unsupported DW_AT_encoding: '%s'"),
                   dwarf_type_encoding_name (encoding));
        type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
        break;
@@ -17439,6 +17424,8 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu)
   if (name && strcmp (name, "char") == 0)
     TYPE_NOSIGN (type) = 1;
 
+  maybe_set_alignment (cu, die, type);
+
   return set_die_type (die, type, cu);
 }
 
@@ -17451,7 +17438,8 @@ attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
                      struct dwarf2_cu *cu, struct dynamic_prop *prop)
 {
   struct dwarf2_property_baton *baton;
-  struct obstack *obstack = &cu->dwarf2_per_objfile->objfile->objfile_obstack;
+  struct obstack *obstack
+    = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
 
   if (attr == NULL || prop == NULL)
     return 0;
@@ -17610,10 +17598,10 @@ read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
   if (attr)
     attr_to_dynamic_prop (attr, die, cu, &low);
   else if (!low_default_is_valid)
-    complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
-                                     "- DIE at 0x%x [in module %s]"),
-              to_underlying (die->sect_off),
-              objfile_name (cu->dwarf2_per_objfile->objfile));
+    complaint (_("Missing DW_AT_lower_bound "
+                                     "- DIE at %s [in module %s]"),
+              sect_offset_str (die->sect_off),
+              objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
 
   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
   if (!attr_to_dynamic_prop (attr, die, cu, &high))
@@ -17643,7 +17631,7 @@ read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
      high bound or count are not yet handled by this code.  */
   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
     {
-      struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+      struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
       struct gdbarch *gdbarch = get_objfile_arch (objfile);
       int addr_size = gdbarch_addr_bit (gdbarch) /8;
       struct type *int_type = objfile_type (objfile)->builtin_int;
@@ -17700,6 +17688,8 @@ read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
   if (attr)
     TYPE_LENGTH (range_type) = DW_UNSND (attr);
 
+  maybe_set_alignment (cu, die, range_type);
+
   set_die_type (die, range_type, cu);
 
   /* set_die_type should be already done.  */
@@ -17713,7 +17703,8 @@ read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
 {
   struct type *type;
 
-  type = init_type (cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
+  type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
+                   NULL);
   TYPE_NAME (type) = dwarf2_name (die, cu);
 
   /* In Ada, an unspecified type is typically used when the description
@@ -17855,7 +17846,7 @@ read_full_die_1 (const struct die_reader_specs *reader,
       return info_ptr;
     }
 
-  abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
+  abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
   if (!abbrev)
     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
           abbrev_number,
@@ -17916,12 +17907,12 @@ read_full_die (const struct die_reader_specs *reader,
 
 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE.  */
 
-static struct abbrev_info *
-abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
+struct abbrev_info *
+abbrev_table::alloc_abbrev ()
 {
   struct abbrev_info *abbrev;
 
-  abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
+  abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
   memset (abbrev, 0, sizeof (struct abbrev_info));
 
   return abbrev;
@@ -17929,30 +17920,28 @@ abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
 
 /* Add an abbreviation to the table.  */
 
-static void
-abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
-                        unsigned int abbrev_number,
-                        struct abbrev_info *abbrev)
+void
+abbrev_table::add_abbrev (unsigned int abbrev_number,
+                         struct abbrev_info *abbrev)
 {
   unsigned int hash_number;
 
   hash_number = abbrev_number % ABBREV_HASH_SIZE;
-  abbrev->next = abbrev_table->abbrevs[hash_number];
-  abbrev_table->abbrevs[hash_number] = abbrev;
+  abbrev->next = m_abbrevs[hash_number];
+  m_abbrevs[hash_number] = abbrev;
 }
 
 /* Look up an abbrev in the table.
    Returns NULL if the abbrev is not found.  */
 
-static struct abbrev_info *
-abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
-                           unsigned int abbrev_number)
+struct abbrev_info *
+abbrev_table::lookup_abbrev (unsigned int abbrev_number)
 {
   unsigned int hash_number;
   struct abbrev_info *abbrev;
 
   hash_number = abbrev_number % ABBREV_HASH_SIZE;
-  abbrev = abbrev_table->abbrevs[hash_number];
+  abbrev = m_abbrevs[hash_number];
 
   while (abbrev)
     {
@@ -17965,13 +17954,13 @@ abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
 
 /* Read in an abbrev table.  */
 
-static struct abbrev_table *
-abbrev_table_read_table (struct dwarf2_section_info *section,
+static abbrev_table_up
+abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                        struct dwarf2_section_info *section,
                         sect_offset sect_off)
 {
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   bfd *abfd = get_section_bfd_owner (section);
-  struct abbrev_table *abbrev_table;
   const gdb_byte *abbrev_ptr;
   struct abbrev_info *cur_abbrev;
   unsigned int abbrev_number, bytes_read, abbrev_name;
@@ -17979,14 +17968,7 @@ abbrev_table_read_table (struct dwarf2_section_info *section,
   struct attr_abbrev *cur_attrs;
   unsigned int allocated_attrs;
 
-  abbrev_table = XNEW (struct abbrev_table);
-  abbrev_table->sect_off = sect_off;
-  obstack_init (&abbrev_table->abbrev_obstack);
-  abbrev_table->abbrevs =
-    XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
-              ABBREV_HASH_SIZE);
-  memset (abbrev_table->abbrevs, 0,
-         ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
+  abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
 
   dwarf2_read_section (objfile, section);
   abbrev_ptr = section->buffer + to_underlying (sect_off);
@@ -17999,7 +17981,7 @@ abbrev_table_read_table (struct dwarf2_section_info *section,
   /* Loop until we reach an abbrev number of 0.  */
   while (abbrev_number)
     {
-      cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
+      cur_abbrev = abbrev_table->alloc_abbrev ();
 
       /* read in abbrev header */
       cur_abbrev->number = abbrev_number;
@@ -18054,7 +18036,7 @@ abbrev_table_read_table (struct dwarf2_section_info *section,
       memcpy (cur_abbrev->attrs, cur_attrs,
              cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
 
-      abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
+      abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
 
       /* Get next abbreviation.
          Under Irix6 the abbreviations for a compilation unit are not
@@ -18067,7 +18049,7 @@ abbrev_table_read_table (struct dwarf2_section_info *section,
        break;
       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
       abbrev_ptr += bytes_read;
-      if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
+      if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
        break;
     }
 
@@ -18075,54 +18057,6 @@ abbrev_table_read_table (struct dwarf2_section_info *section,
   return abbrev_table;
 }
 
-/* Free the resources held by ABBREV_TABLE.  */
-
-static void
-abbrev_table_free (struct abbrev_table *abbrev_table)
-{
-  obstack_free (&abbrev_table->abbrev_obstack, NULL);
-  xfree (abbrev_table);
-}
-
-/* Same as abbrev_table_free but as a cleanup.
-   We pass in a pointer to the pointer to the table so that we can
-   set the pointer to NULL when we're done.  It also simplifies
-   build_type_psymtabs_1.  */
-
-static void
-abbrev_table_free_cleanup (void *table_ptr)
-{
-  struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
-
-  if (*abbrev_table_ptr != NULL)
-    abbrev_table_free (*abbrev_table_ptr);
-  *abbrev_table_ptr = NULL;
-}
-
-/* Read the abbrev table for CU from ABBREV_SECTION.  */
-
-static void
-dwarf2_read_abbrevs (struct dwarf2_cu *cu,
-                    struct dwarf2_section_info *abbrev_section)
-{
-  cu->abbrev_table =
-    abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
-}
-
-/* Release the memory used by the abbrev table for a compilation unit.  */
-
-static void
-dwarf2_free_abbrev_table (void *ptr_to_cu)
-{
-  struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
-
-  if (cu->abbrev_table != NULL)
-    abbrev_table_free (cu->abbrev_table);
-  /* Set this to NULL so that we SEGV if we try to read it later,
-     and also because free_comp_unit verifies this is NULL.  */
-  cu->abbrev_table = NULL;
-}
-\f
 /* Returns nonzero if TAG represents a type that we might generate a partial
    symbol for.  */
 
@@ -18162,10 +18096,8 @@ load_partial_dies (const struct die_reader_specs *reader,
                   const gdb_byte *info_ptr, int building_psymtab)
 {
   struct dwarf2_cu *cu = reader->cu;
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
-  struct partial_die_info *part_die;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
-  struct abbrev_info *abbrev;
   unsigned int bytes_read;
   unsigned int load_all = 0;
   int nesting_level = 1;
@@ -18186,25 +18118,16 @@ load_partial_dies (const struct die_reader_specs *reader,
                            hashtab_obstack_allocate,
                            dummy_obstack_deallocate);
 
-  part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
-
   while (1)
     {
-      abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
+      abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
 
       /* A NULL abbrev means the end of a series of children.  */
       if (abbrev == NULL)
        {
          if (--nesting_level == 0)
-           {
-             /* PART_DIE was probably the last thing allocated on the
-                comp_unit_obstack, so we could call obstack_free
-                here.  We don't do that because the waste is small,
-                and will be cleaned up when we're done with this
-                compilation unit.  This way, we're also more robust
-                against other users of the comp_unit_obstack.  */
-             return first_die;
-           }
+           return first_die;
+
          info_ptr += bytes_read;
          last_die = parent_die;
          parent_die = parent_die->die_parent;
@@ -18248,6 +18171,7 @@ load_partial_dies (const struct die_reader_specs *reader,
          && abbrev->tag != DW_TAG_constant
          && abbrev->tag != DW_TAG_enumerator
          && abbrev->tag != DW_TAG_subprogram
+         && abbrev->tag != DW_TAG_inlined_subroutine
          && abbrev->tag != DW_TAG_lexical_block
          && abbrev->tag != DW_TAG_variable
          && abbrev->tag != DW_TAG_namespace
@@ -18261,8 +18185,10 @@ load_partial_dies (const struct die_reader_specs *reader,
          continue;
        }
 
-      info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
-                                  info_ptr);
+      struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
+                                  abbrev);
+
+      info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
 
       /* This two-pass algorithm for processing partial symbols has a
         high cost in cache pressure.  Thus, handle some simple cases
@@ -18281,18 +18207,18 @@ load_partial_dies (const struct die_reader_specs *reader,
         of them, for a language without namespaces), can be processed
         directly.  */
       if (parent_die == NULL
-         && part_die->has_specification == 0
-         && part_die->is_declaration == 0
-         && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
-             || part_die->tag == DW_TAG_base_type
-             || part_die->tag == DW_TAG_subrange_type))
-       {
-         if (building_psymtab && part_die->name != NULL)
-           add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
+         && pdi.has_specification == 0
+         && pdi.is_declaration == 0
+         && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
+             || pdi.tag == DW_TAG_base_type
+             || pdi.tag == DW_TAG_subrange_type))
+       {
+         if (building_psymtab && pdi.name != NULL)
+           add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
                                 VAR_DOMAIN, LOC_TYPEDEF,
                                 &objfile->static_psymbols,
                                 0, cu->language, objfile);
-         info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
+         info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
          continue;
        }
 
@@ -18304,37 +18230,38 @@ load_partial_dies (const struct die_reader_specs *reader,
         it could not find the child DIEs referenced later, this is checked
         above.  In correct DWARF DW_TAG_typedef should have no children.  */
 
-      if (part_die->tag == DW_TAG_typedef && part_die->has_children)
-       complaint (&symfile_complaints,
-                  _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
-                    "- DIE at 0x%x [in module %s]"),
-                  to_underlying (part_die->sect_off), objfile_name (objfile));
+      if (pdi.tag == DW_TAG_typedef && pdi.has_children)
+       complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
+                    "- DIE at %s [in module %s]"),
+                  sect_offset_str (pdi.sect_off), objfile_name (objfile));
 
       /* If we're at the second level, and we're an enumerator, and
         our parent has no specification (meaning possibly lives in a
         namespace elsewhere), then we can add the partial symbol now
         instead of queueing it.  */
-      if (part_die->tag == DW_TAG_enumerator
+      if (pdi.tag == DW_TAG_enumerator
          && parent_die != NULL
          && parent_die->die_parent == NULL
          && parent_die->tag == DW_TAG_enumeration_type
          && parent_die->has_specification == 0)
        {
-         if (part_die->name == NULL)
-           complaint (&symfile_complaints,
-                      _("malformed enumerator DIE ignored"));
+         if (pdi.name == NULL)
+           complaint (_("malformed enumerator DIE ignored"));
          else if (building_psymtab)
-           add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
+           add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
                                 VAR_DOMAIN, LOC_CONST,
                                 cu->language == language_cplus
                                 ? &objfile->global_psymbols
                                 : &objfile->static_psymbols,
                                 0, cu->language, objfile);
 
-         info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
+         info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
          continue;
        }
 
+      struct partial_die_info *part_die
+       = new (&cu->comp_unit_obstack) partial_die_info (pdi);
+
       /* We'll save this DIE so link it in.  */
       part_die->die_parent = parent_die;
       part_die->die_sibling = NULL;
@@ -18386,8 +18313,6 @@ load_partial_dies (const struct die_reader_specs *reader,
          *slot = part_die;
        }
 
-      part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
-
       /* For some DIEs we want to follow their children (if any).  For C
         we have no reason to follow the children of structures; for other
         languages we have to, so that we can get at method physnames
@@ -18430,45 +18355,39 @@ load_partial_dies (const struct die_reader_specs *reader,
     }
 }
 
-/* Read a minimal amount of information into the minimal die structure.  */
+partial_die_info::partial_die_info (sect_offset sect_off_,
+                                   struct abbrev_info *abbrev)
+  : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
+{
+}
 
-static const gdb_byte *
-read_partial_die (const struct die_reader_specs *reader,
-                 struct partial_die_info *part_die,
-                 struct abbrev_info *abbrev, unsigned int abbrev_len,
-                 const gdb_byte *info_ptr)
+/* Read a minimal amount of information into the minimal die structure.
+   INFO_PTR should point just after the initial uleb128 of a DIE.  */
+
+const gdb_byte *
+partial_die_info::read (const struct die_reader_specs *reader,
+                       const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
 {
   struct dwarf2_cu *cu = reader->cu;
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
-  const gdb_byte *buffer = reader->buffer;
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
   unsigned int i;
-  struct attribute attr;
   int has_low_pc_attr = 0;
   int has_high_pc_attr = 0;
   int high_pc_relative = 0;
 
-  memset (part_die, 0, sizeof (struct partial_die_info));
-
-  part_die->sect_off = (sect_offset) (info_ptr - buffer);
-
-  info_ptr += abbrev_len;
-
-  if (abbrev == NULL)
-    return info_ptr;
-
-  part_die->tag = abbrev->tag;
-  part_die->has_children = abbrev->has_children;
-
-  for (i = 0; i < abbrev->num_attrs; ++i)
+  for (i = 0; i < abbrev.num_attrs; ++i)
     {
-      info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
+      struct attribute attr;
+
+      info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
 
       /* Store the data if it is of an attribute we want to keep in a
          partial symbol table.  */
       switch (attr.name)
        {
        case DW_AT_name:
-         switch (part_die->tag)
+         switch (tag)
            {
            case DW_TAG_compile_unit:
            case DW_TAG_partial_unit:
@@ -18479,12 +18398,16 @@ read_partial_die (const struct die_reader_specs *reader,
            case DW_TAG_enumerator:
              /* These tags always have simple identifiers already; no need
                 to canonicalize them.  */
-             part_die->name = DW_STRING (&attr);
+             name = DW_STRING (&attr);
              break;
            default:
-             part_die->name
-               = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
-                                           &objfile->per_bfd->storage_obstack);
+             {
+               struct objfile *objfile = dwarf2_per_objfile->objfile;
+
+               name
+                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
+                                             &objfile->per_bfd->storage_obstack);
+             }
              break;
            }
          break;
@@ -18494,16 +18417,16 @@ read_partial_die (const struct die_reader_specs *reader,
             assume they will be the same, and we only store the last
             one we see.  */
          if (cu->language == language_ada)
-           part_die->name = DW_STRING (&attr);
-         part_die->linkage_name = DW_STRING (&attr);
+           name = DW_STRING (&attr);
+         linkage_name = DW_STRING (&attr);
          break;
        case DW_AT_low_pc:
          has_low_pc_attr = 1;
-         part_die->lowpc = attr_value_as_address (&attr);
+         lowpc = attr_value_as_address (&attr);
          break;
        case DW_AT_high_pc:
          has_high_pc_attr = 1;
-         part_die->highpc = attr_value_as_address (&attr);
+         highpc = attr_value_as_address (&attr);
          if (cu->header.version >= 4 && attr_form_is_constant (&attr))
                high_pc_relative = 1;
          break;
@@ -18511,7 +18434,7 @@ read_partial_die (const struct die_reader_specs *reader,
           /* Support the .debug_loc offsets.  */
           if (attr_form_is_block (&attr))
             {
-              part_die->d.locdesc = DW_BLOCK (&attr);
+              d.locdesc = DW_BLOCK (&attr);
             }
           else if (attr_form_is_section_offset (&attr))
             {
@@ -18524,47 +18447,46 @@ read_partial_die (const struct die_reader_specs *reader,
             }
          break;
        case DW_AT_external:
-         part_die->is_external = DW_UNSND (&attr);
+         is_external = DW_UNSND (&attr);
          break;
        case DW_AT_declaration:
-         part_die->is_declaration = DW_UNSND (&attr);
+         is_declaration = DW_UNSND (&attr);
          break;
        case DW_AT_type:
-         part_die->has_type = 1;
+         has_type = 1;
          break;
        case DW_AT_abstract_origin:
        case DW_AT_specification:
        case DW_AT_extension:
-         part_die->has_specification = 1;
-         part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
-         part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
+         has_specification = 1;
+         spec_offset = dwarf2_get_ref_die_offset (&attr);
+         spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
                                   || cu->per_cu->is_dwz);
          break;
        case DW_AT_sibling:
          /* Ignore absolute siblings, they might point outside of
             the current compile unit.  */
          if (attr.form == DW_FORM_ref_addr)
-           complaint (&symfile_complaints,
-                      _("ignoring absolute DW_AT_sibling"));
+           complaint (_("ignoring absolute DW_AT_sibling"));
          else
            {
+             const gdb_byte *buffer = reader->buffer;
              sect_offset off = dwarf2_get_ref_die_offset (&attr);
              const gdb_byte *sibling_ptr = buffer + to_underlying (off);
 
              if (sibling_ptr < info_ptr)
-               complaint (&symfile_complaints,
-                          _("DW_AT_sibling points backwards"));
+               complaint (_("DW_AT_sibling points backwards"));
              else if (sibling_ptr > reader->buffer_end)
                dwarf2_section_buffer_overflow_complaint (reader->die_section);
              else
-               part_die->sibling = sibling_ptr;
+               sibling = sibling_ptr;
            }
          break;
         case DW_AT_byte_size:
-          part_die->has_byte_size = 1;
+          has_byte_size = 1;
           break;
         case DW_AT_const_value:
-          part_die->has_const_value = 1;
+          has_const_value = 1;
           break;
        case DW_AT_calling_convention:
          /* DWARF doesn't provide a way to identify a program's source-level
@@ -18583,25 +18505,25 @@ read_partial_die (const struct die_reader_specs *reader,
             compatibility.  */
          if (DW_UNSND (&attr) == DW_CC_program
              && cu->language == language_fortran)
-           part_die->main_subprogram = 1;
+           main_subprogram = 1;
          break;
        case DW_AT_inline:
          if (DW_UNSND (&attr) == DW_INL_inlined
              || DW_UNSND (&attr) == DW_INL_declared_inlined)
-           part_die->may_be_inlined = 1;
+           may_be_inlined = 1;
          break;
 
        case DW_AT_import:
-         if (part_die->tag == DW_TAG_imported_unit)
+         if (tag == DW_TAG_imported_unit)
            {
-             part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
-             part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
+             d.sect_off = dwarf2_get_ref_die_offset (&attr);
+             is_dwz = (attr.form == DW_FORM_GNU_ref_alt
                                  || cu->per_cu->is_dwz);
            }
          break;
 
        case DW_AT_main_subprogram:
-         part_die->main_subprogram = DW_UNSND (&attr);
+         main_subprogram = DW_UNSND (&attr);
          break;
 
        default:
@@ -18610,7 +18532,7 @@ read_partial_die (const struct die_reader_specs *reader,
     }
 
   if (high_pc_relative)
-    part_die->highpc += part_die->lowpc;
+    highpc += lowpc;
 
   if (has_low_pc_attr && has_high_pc_attr)
     {
@@ -18622,31 +18544,32 @@ read_partial_die (const struct die_reader_specs *reader,
         labels are not in the output, so the relocs get a value of 0.
         If this is a discarded function, mark the pc bounds as invalid,
         so that GDB will ignore it.  */
-      if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
+      if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
        {
+         struct objfile *objfile = dwarf2_per_objfile->objfile;
          struct gdbarch *gdbarch = get_objfile_arch (objfile);
 
-         complaint (&symfile_complaints,
-                    _("DW_AT_low_pc %s is zero "
-                      "for DIE at 0x%x [in module %s]"),
-                    paddress (gdbarch, part_die->lowpc),
-                    to_underlying (part_die->sect_off), objfile_name (objfile));
+         complaint (_("DW_AT_low_pc %s is zero "
+                      "for DIE at %s [in module %s]"),
+                    paddress (gdbarch, lowpc),
+                    sect_offset_str (sect_off),
+                    objfile_name (objfile));
        }
       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
-      else if (part_die->lowpc >= part_die->highpc)
+      else if (lowpc >= highpc)
        {
+         struct objfile *objfile = dwarf2_per_objfile->objfile;
          struct gdbarch *gdbarch = get_objfile_arch (objfile);
 
-         complaint (&symfile_complaints,
-                    _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
-                      "for DIE at 0x%x [in module %s]"),
-                    paddress (gdbarch, part_die->lowpc),
-                    paddress (gdbarch, part_die->highpc),
-                    to_underlying (part_die->sect_off),
+         complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
+                      "for DIE at %s [in module %s]"),
+                    paddress (gdbarch, lowpc),
+                    paddress (gdbarch, highpc),
+                    sect_offset_str (sect_off),
                     objfile_name (objfile));
        }
       else
-       part_die->has_pc_info = 1;
+       has_pc_info = 1;
     }
 
   return info_ptr;
@@ -18654,15 +18577,14 @@ read_partial_die (const struct die_reader_specs *reader,
 
 /* Find a cached partial DIE at OFFSET in CU.  */
 
-static struct partial_die_info *
-find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
+struct partial_die_info *
+dwarf2_cu::find_partial_die (sect_offset sect_off)
 {
   struct partial_die_info *lookup_die = NULL;
-  struct partial_die_info part_die;
+  struct partial_die_info part_die (sect_off);
 
-  part_die.sect_off = sect_off;
   lookup_die = ((struct partial_die_info *)
-               htab_find_with_hash (cu->partial_dies, &part_die,
+               htab_find_with_hash (partial_dies, &part_die,
                                     to_underlying (sect_off)));
 
   return lookup_die;
@@ -18676,14 +18598,16 @@ find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
 static struct partial_die_info *
 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct dwarf2_per_cu_data *per_cu = NULL;
   struct partial_die_info *pd = NULL;
 
   if (offset_in_dwz == cu->per_cu->is_dwz
       && offset_in_cu_p (&cu->header, sect_off))
     {
-      pd = find_partial_die_in_comp_unit (sect_off, cu);
+      pd = cu->find_partial_die (sect_off);
       if (pd != NULL)
        return pd;
       /* We missed recording what we needed.
@@ -18695,19 +18619,19 @@ find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
       /* TUs don't reference other CUs/TUs (except via type signatures).  */
       if (cu->per_cu->is_debug_types)
        {
-         error (_("Dwarf Error: Type Unit at offset 0x%x contains"
-                  " external reference to offset 0x%x [in module %s].\n"),
-                to_underlying (cu->header.sect_off), to_underlying (sect_off),
+         error (_("Dwarf Error: Type Unit at offset %s contains"
+                  " external reference to offset %s [in module %s].\n"),
+                sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
                 bfd_get_filename (objfile->obfd));
        }
       per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
-                                                objfile);
+                                                dwarf2_per_objfile);
 
       if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
        load_partial_comp_unit (per_cu);
 
       per_cu->cu->last_used = 0;
-      pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
+      pd = per_cu->cu->find_partial_die (sect_off);
     }
 
   /* If we didn't find it, and not all dies have been loaded,
@@ -18725,14 +18649,14 @@ find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
         set.  */
       load_partial_comp_unit (per_cu);
 
-      pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
+      pd = per_cu->cu->find_partial_die (sect_off);
     }
 
   if (pd == NULL)
     internal_error (__FILE__, __LINE__,
-                   _("could not find partial DIE 0x%x "
+                   _("could not find partial DIE %s "
                      "in cache [from module %s]\n"),
-                   to_underlying (sect_off), bfd_get_filename (objfile->obfd));
+                   sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
   return pd;
 }
 
@@ -18777,7 +18701,7 @@ guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
                                                 child_pdi->linkage_name);
          if (actual_class_name != NULL)
            {
-             struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+             struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
              struct_pdi->name
                = ((const char *)
                   obstack_copy0 (&objfile->per_bfd->storage_obstack,
@@ -18790,70 +18714,66 @@ guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
     }
 }
 
-/* Adjust PART_DIE before generating a symbol for it.  This function
-   may set the is_external flag or change the DIE's name.  */
-
-static void
-fixup_partial_die (struct partial_die_info *part_die,
-                  struct dwarf2_cu *cu)
+void
+partial_die_info::fixup (struct dwarf2_cu *cu)
 {
   /* Once we've fixed up a die, there's no point in doing so again.
      This also avoids a memory leak if we were to call
      guess_partial_die_structure_name multiple times.  */
-  if (part_die->fixup_called)
+  if (fixup_called)
     return;
 
   /* If we found a reference attribute and the DIE has no name, try
      to find a name in the referred to DIE.  */
 
-  if (part_die->name == NULL && part_die->has_specification)
+  if (name == NULL && has_specification)
     {
       struct partial_die_info *spec_die;
 
-      spec_die = find_partial_die (part_die->spec_offset,
-                                  part_die->spec_is_dwz, cu);
+      spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
 
-      fixup_partial_die (spec_die, cu);
+      spec_die->fixup (cu);
 
       if (spec_die->name)
        {
-         part_die->name = spec_die->name;
+         name = spec_die->name;
 
          /* Copy DW_AT_external attribute if it is set.  */
          if (spec_die->is_external)
-           part_die->is_external = spec_die->is_external;
+           is_external = spec_die->is_external;
        }
     }
 
   /* Set default names for some unnamed DIEs.  */
 
-  if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
-    part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
+  if (name == NULL && tag == DW_TAG_namespace)
+    name = CP_ANONYMOUS_NAMESPACE_STR;
 
   /* If there is no parent die to provide a namespace, and there are
      children, see if we can determine the namespace from their linkage
      name.  */
   if (cu->language == language_cplus
-      && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
-      && part_die->die_parent == NULL
-      && part_die->has_children
-      && (part_die->tag == DW_TAG_class_type
-         || part_die->tag == DW_TAG_structure_type
-         || part_die->tag == DW_TAG_union_type))
-    guess_partial_die_structure_name (part_die, cu);
+      && !VEC_empty (dwarf2_section_info_def,
+                    cu->per_cu->dwarf2_per_objfile->types)
+      && die_parent == NULL
+      && has_children
+      && (tag == DW_TAG_class_type
+         || tag == DW_TAG_structure_type
+         || tag == DW_TAG_union_type))
+    guess_partial_die_structure_name (this, cu);
 
   /* GCC might emit a nameless struct or union that has a linkage
      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
-  if (part_die->name == NULL
-      && (part_die->tag == DW_TAG_class_type
-         || part_die->tag == DW_TAG_interface_type
-         || part_die->tag == DW_TAG_structure_type
-         || part_die->tag == DW_TAG_union_type)
-      && part_die->linkage_name != NULL)
+  if (name == NULL
+      && (tag == DW_TAG_class_type
+         || tag == DW_TAG_interface_type
+         || tag == DW_TAG_structure_type
+         || tag == DW_TAG_union_type)
+      && linkage_name != NULL)
     {
       char *demangled;
 
-      demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
+      demangled = gdb_demangle (linkage_name, DMGL_TYPES);
       if (demangled)
        {
          const char *base;
@@ -18866,8 +18786,8 @@ fixup_partial_die (struct partial_die_info *part_die,
          else
            base = demangled;
 
-         struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
-         part_die->name
+         struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
+         name
            = ((const char *)
               obstack_copy0 (&objfile->per_bfd->storage_obstack,
                              base, strlen (base)));
@@ -18875,7 +18795,7 @@ fixup_partial_die (struct partial_die_info *part_die,
        }
     }
 
-  part_die->fixup_called = 1;
+  fixup_called = 1;
 }
 
 /* Read an attribute value described by an attribute form.  */
@@ -18886,7 +18806,9 @@ read_attribute_value (const struct die_reader_specs *reader,
                      LONGEST implicit_const, const gdb_byte *info_ptr)
 {
   struct dwarf2_cu *cu = reader->cu;
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  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;
@@ -18960,7 +18882,8 @@ read_attribute_value (const struct die_reader_specs *reader,
     case DW_FORM_strp:
       if (!cu->per_cu->is_dwz)
        {
-         DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
+         DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
+                                                  abfd, info_ptr, cu_header,
                                                   &bytes_read);
          DW_STRING_IS_CANONICAL (attr) = 0;
          info_ptr += bytes_read;
@@ -18970,7 +18893,8 @@ 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 (abfd, info_ptr,
+         DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
+                                                       abfd, info_ptr,
                                                        cu_header, &bytes_read);
          DW_STRING_IS_CANONICAL (attr) = 0;
          info_ptr += bytes_read;
@@ -18979,11 +18903,12 @@ read_attribute_value (const struct die_reader_specs *reader,
       /* FALLTHROUGH */
     case DW_FORM_GNU_strp_alt:
       {
-       struct dwz_file *dwz = dwarf2_get_dwz_file ();
+       struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
        LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
                                          &bytes_read);
 
-       DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
+       DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
+                                                         dwz, str_offset);
        DW_STRING_IS_CANONICAL (attr) = 0;
        info_ptr += bytes_read;
       }
@@ -19118,8 +19043,7 @@ read_attribute_value (const struct die_reader_specs *reader,
       && DW_UNSND (attr) >= 0xffffffff)
     {
       complaint
-        (&symfile_complaints,
-         _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
+        (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
          hex_string (DW_UNSND (attr)));
       DW_UNSND (attr) = 0;
     }
@@ -19317,8 +19241,7 @@ read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
              || cu_header->initial_length_size == 12);
 
   if (cu_header->initial_length_size != *bytes_read)
-    complaint (&symfile_complaints,
-              _("intermixed 32-bit and 64-bit DWARF sections"));
+    complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
 
   *offset_size = (*bytes_read == 4) ? 4 : 8;
   return length;
@@ -19393,12 +19316,13 @@ read_direct_string (bfd *abfd, const gdb_byte *buf,
    reporting strings FORM_NAME and SECT_NAME.  */
 
 static const char *
-read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
+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)
 {
-  dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
+  dwarf2_read_section (objfile, sect);
   if (sect->buffer == NULL)
     error (_("%s used without %s section [in module %s]"),
           form_name, sect_name, bfd_get_filename (abfd));
@@ -19414,9 +19338,11 @@ read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
 /* Return pointer to string at .debug_str offset STR_OFFSET.  */
 
 static const char *
-read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
+read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                               bfd *abfd, LONGEST str_offset)
 {
-  return read_indirect_string_at_offset_from (abfd, 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");
 }
@@ -19424,9 +19350,11 @@ read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
 /* Return pointer to string at .debug_line_str offset STR_OFFSET.  */
 
 static const char *
-read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
+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 (abfd, 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");
@@ -19438,9 +19366,10 @@ read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
    return a pointer to the string.  */
 
 static const char *
-read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
+read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
+                              LONGEST str_offset)
 {
-  dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
+  dwarf2_read_section (objfile, &dwz->str);
 
   if (dwz->str.buffer == NULL)
     error (_("DW_FORM_GNU_strp_alt used without .debug_str "
@@ -19461,13 +19390,14 @@ read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
    Return *BYTES_READ_PTR count of bytes read from BUF.  */
 
 static const char *
-read_indirect_string (bfd *abfd, const gdb_byte *buf,
+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 (abfd, str_offset);
+  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.
@@ -19475,13 +19405,15 @@ read_indirect_string (bfd *abfd, const gdb_byte *buf,
    Return *BYTES_READ_PTR count of bytes read from BUF.  */
 
 static const char *
-read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
+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 (abfd, str_offset);
+  return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
+                                             str_offset);
 }
 
 ULONGEST
@@ -19546,7 +19478,8 @@ read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
    ADDR_SIZE is the size of addresses from the CU header.  */
 
 static CORE_ADDR
-read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
+read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                  unsigned int addr_index, ULONGEST addr_base, int addr_size)
 {
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   bfd *abfd = objfile->obfd;
@@ -19573,7 +19506,8 @@ read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
 static CORE_ADDR
 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
 {
-  return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
+  return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
+                           cu->addr_base, cu->header.addr_size);
 }
 
 /* Given a pointer to an leb128 value, fetch the value from .debug_addr.  */
@@ -19582,7 +19516,7 @@ static CORE_ADDR
 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
                             unsigned int *bytes_read)
 {
-  bfd *abfd = cu->dwarf2_per_objfile->objfile->obfd;
+  bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
   unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
 
   return read_addr_index (cu, addr_index);
@@ -19623,14 +19557,11 @@ CORE_ADDR
 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
                        unsigned int addr_index)
 {
-  struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
+  struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
   struct dwarf2_cu *cu = per_cu->cu;
   ULONGEST addr_base;
   int addr_size;
 
-  /* This is intended to be called from outside this file.  */
-  dw2_setup (objfile);
-
   /* We need addr_base and addr_size.
      If we don't have PER_CU->cu, we have to get it.
      Nasty, but the alternative is storing the needed info in PER_CU,
@@ -19658,13 +19589,14 @@ dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
 
       /* Note: We can't use init_cutu_and_read_dies_simple here,
         we need addr_base.  */
-      init_cutu_and_read_dies (per_cu, NULL, 0, 0,
+      init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
                               dwarf2_read_addr_index_reader, &aidata);
       addr_base = aidata.addr_base;
       addr_size = aidata.addr_size;
     }
 
-  return read_addr_index_1 (addr_index, addr_base, addr_size);
+  return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
+                           addr_size);
 }
 
 /* Given a DW_FORM_GNU_str_index, fetch the string.
@@ -19673,10 +19605,12 @@ dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
 static const char *
 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
 {
+  struct dwarf2_cu *cu = reader->cu;
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   const char *objf_name = objfile_name (objfile);
   bfd *abfd = objfile->obfd;
-  struct dwarf2_cu *cu = reader->cu;
   struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
   struct dwarf2_section_info *str_offsets_section =
     &reader->dwo_file->sections.str_offsets;
@@ -19688,16 +19622,16 @@ read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
   dwarf2_read_section (objfile, str_offsets_section);
   if (str_section->buffer == NULL)
     error (_("%s used without .debug_str.dwo section"
-            " in CU at offset 0x%x [in module %s]"),
-          form_name, to_underlying (cu->header.sect_off), objf_name);
+            " in CU at offset %s [in module %s]"),
+          form_name, sect_offset_str (cu->header.sect_off), objf_name);
   if (str_offsets_section->buffer == NULL)
     error (_("%s used without .debug_str_offsets.dwo section"
-            " in CU at offset 0x%x [in module %s]"),
-          form_name, to_underlying (cu->header.sect_off), objf_name);
+            " in CU at offset %s [in module %s]"),
+          form_name, sect_offset_str (cu->header.sect_off), objf_name);
   if (str_index * cu->header.offset_size >= str_offsets_section->size)
     error (_("%s pointing outside of .debug_str_offsets.dwo"
-            " section in CU at offset 0x%x [in module %s]"),
-          form_name, to_underlying (cu->header.sect_off), objf_name);
+            " section in CU at offset %s [in module %s]"),
+          form_name, sect_offset_str (cu->header.sect_off), objf_name);
   info_ptr = (str_offsets_section->buffer
              + str_index * cu->header.offset_size);
   if (cu->header.offset_size == 4)
@@ -19706,8 +19640,8 @@ read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
     str_offset = bfd_get_64 (abfd, info_ptr);
   if (str_offset >= str_section->size)
     error (_("Offset from %s pointing outside of"
-            " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
-          form_name, to_underlying (cu->header.sect_off), objf_name);
+            " .debug_str.dwo section in CU at offset %s [in module %s]"),
+          form_name, sect_offset_str (cu->header.sect_off), objf_name);
   return (const char *) (str_section->buffer + str_offset);
 }
 
@@ -19852,11 +19786,10 @@ dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *c
          || attr->form == DW_FORM_GNU_strp_alt)
        str = DW_STRING (attr);
       else
-        complaint (&symfile_complaints,
-                  _("string type expected for attribute %s for "
-                    "DIE at 0x%x in module %s"),
-                  dwarf_attr_name (name), to_underlying (die->sect_off),
-                  objfile_name (cu->dwarf2_per_objfile->objfile));
+        complaint (_("string type expected for attribute %s for "
+                    "DIE at %s in module %s"),
+                  dwarf_attr_name (name), sect_offset_str (die->sect_off),
+                  objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
     }
 
   return str;
@@ -19948,6 +19881,8 @@ static struct dwarf2_section_info *
 get_debug_line_section (struct dwarf2_cu *cu)
 {
   struct dwarf2_section_info *section;
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
 
   /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
      DWO file.  */
@@ -19955,7 +19890,7 @@ get_debug_line_section (struct dwarf2_cu *cu)
     section = &cu->dwo_unit->dwo_file->sections.line;
   else if (cu->per_cu->is_dwz)
     {
-      struct dwz_file *dwz = dwarf2_get_dwz_file ();
+      struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
 
       section = &dwz->line;
     }
@@ -19971,7 +19906,8 @@ get_debug_line_section (struct dwarf2_cu *cu)
    format.  */
 
 static void
-read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
+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,
@@ -20023,7 +19959,8 @@ read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
              break;
 
            case DW_FORM_line_strp:
-             string.emplace (read_indirect_line_string (abfd, buf,
+             string.emplace (read_indirect_line_string (dwarf2_per_objfile,
+                                                        abfd, buf,
                                                         cu_header,
                                                         &bytes_read));
              buf += bytes_read;
@@ -20081,8 +20018,7 @@ read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
            case DW_LNCT_MD5:
              break;
            default:
-             complaint (&symfile_complaints,
-                        _("Unknown format content type %s"),
+             complaint (_("Unknown format content type %s"),
                         pulongest (content_type));
            }
        }
@@ -20112,15 +20048,17 @@ dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
   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;
 
   section = get_debug_line_section (cu);
   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
   if (section->buffer == NULL)
     {
       if (cu->dwo_unit && cu->per_cu->is_debug_types)
-       complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
+       complaint (_("missing .debug_line.dwo section"));
       else
-       complaint (&symfile_complaints, _("missing .debug_line section"));
+       complaint (_("missing .debug_line section"));
       return 0;
     }
 
@@ -20160,8 +20098,7 @@ dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
     {
       /* This is a version we don't understand.  The format could have
         changed in ways we don't handle properly so just punt.  */
-      complaint (&symfile_complaints,
-                _("unsupported version in .debug_line section"));
+      complaint (_("unsupported version in .debug_line section"));
       return NULL;
     }
   if (lh->version >= 5)
@@ -20176,8 +20113,7 @@ dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
       line_ptr += 1;
       if (segment_selector_size != 0)
        {
-         complaint (&symfile_complaints,
-                    _("unsupported segment selector size %u "
+         complaint (_("unsupported segment selector size %u "
                       "in .debug_line section"),
                     segment_selector_size);
          return NULL;
@@ -20198,8 +20134,7 @@ dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
   if (lh->maximum_ops_per_instruction == 0)
     {
       lh->maximum_ops_per_instruction = 1;
-      complaint (&symfile_complaints,
-                _("invalid maximum_ops_per_instruction "
+      complaint (_("invalid maximum_ops_per_instruction "
                   "in `.debug_line' section"));
     }
 
@@ -20223,7 +20158,8 @@ dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
   if (lh->version >= 5)
     {
       /* Read directory table.  */
-      read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
+      read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
+                             &cu->header,
                              [] (struct line_header *lh, const char *name,
                                  dir_index d_index, unsigned int mod_time,
                                  unsigned int length)
@@ -20232,7 +20168,8 @@ dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
        });
 
       /* Read file name table.  */
-      read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
+      read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
+                             &cu->header,
                              [] (struct line_header *lh, const char *name,
                                  dir_index d_index, unsigned int mod_time,
                                  unsigned int length)
@@ -20271,8 +20208,7 @@ dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
   lh->statement_program_start = line_ptr;
 
   if (line_ptr > (section->buffer + section->size))
-    complaint (&symfile_complaints,
-              _("line number info header doesn't "
+    complaint (_("line number info header doesn't "
                 "fit in `.debug_line' section"));
 
   return lh;
@@ -20282,25 +20218,24 @@ dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
    Return the file name of the psymtab for included file FILE_INDEX
    in line header LH of PST.
    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
-   If space for the result is malloc'd, it will be freed by a cleanup.
-   Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
-
-   The function creates dangling cleanup registration.  */
+   If space for the result is malloc'd, *NAME_HOLDER will be set.
+   Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
 
 static const char *
 psymtab_include_file_name (const struct line_header *lh, int file_index,
                           const struct partial_symtab *pst,
-                          const char *comp_dir)
+                          const char *comp_dir,
+                          gdb::unique_xmalloc_ptr<char> *name_holder)
 {
   const file_entry &fe = lh->file_names[file_index];
   const char *include_name = fe.name;
   const char *include_name_to_compare = include_name;
   const char *pst_filename;
-  char *copied_name = NULL;
   int file_is_pst;
 
   const char *dir_name = fe.include_dir (lh);
 
+  gdb::unique_xmalloc_ptr<char> hold_compare;
   if (!IS_ABSOLUTE_PATH (include_name)
       && (dir_name != NULL || comp_dir != NULL))
     {
@@ -20327,36 +20262,30 @@ psymtab_include_file_name (const struct line_header *lh, int file_index,
 
       if (dir_name != NULL)
        {
-         char *tem = concat (dir_name, SLASH_STRING,
-                             include_name, (char *)NULL);
-
-         make_cleanup (xfree, tem);
-         include_name = tem;
+         name_holder->reset (concat (dir_name, SLASH_STRING,
+                                     include_name, (char *) NULL));
+         include_name = name_holder->get ();
          include_name_to_compare = include_name;
        }
       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
        {
-         char *tem = concat (comp_dir, SLASH_STRING,
-                             include_name, (char *)NULL);
-
-         make_cleanup (xfree, tem);
-         include_name_to_compare = tem;
+         hold_compare.reset (concat (comp_dir, SLASH_STRING,
+                                     include_name, (char *) NULL));
+         include_name_to_compare = hold_compare.get ();
        }
     }
 
   pst_filename = pst->filename;
+  gdb::unique_xmalloc_ptr<char> copied_name;
   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
     {
-      copied_name = concat (pst->dirname, SLASH_STRING,
-                           pst_filename, (char *)NULL);
-      pst_filename = copied_name;
+      copied_name.reset (concat (pst->dirname, SLASH_STRING,
+                                pst_filename, (char *) NULL));
+      pst_filename = copied_name.get ();
     }
 
   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
 
-  if (copied_name != NULL)
-    xfree (copied_name);
-
   if (file_is_pst)
     return NULL;
   return include_name;
@@ -20742,11 +20671,10 @@ lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
       /* This line table is for a function which has been
         GCd by the linker.  Ignore it.  PR gdb/12528 */
 
-      struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+      struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
       long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
 
-      complaint (&symfile_complaints,
-                _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
+      complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
                 line_offset, objfile_name (objfile));
       m_record_line_callback = noop_record_line;
       /* Note: record_line_callback is left as noop_record_line until
@@ -20768,7 +20696,7 @@ dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
   unsigned int bytes_read, extended_len;
   unsigned char op_code, extended_op;
   CORE_ADDR baseaddr;
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   bfd *abfd = objfile->obfd;
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
   /* True if we're recording line info (as opposed to building partial
@@ -20873,8 +20801,7 @@ dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
                  }
                  break;
                default:
-                 complaint (&symfile_complaints,
-                            _("mangled .debug_line section"));
+                 complaint (_("mangled .debug_line section"));
                  return;
                }
              /* Make sure that we parsed the extended op correctly.  If e.g.
@@ -20882,8 +20809,7 @@ dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
                 we may have read the wrong number of bytes.  */
              if (line_ptr != extended_end)
                {
-                 complaint (&symfile_complaints,
-                            _("mangled .debug_line section"));
+                 complaint (_("mangled .debug_line section"));
                  return;
                }
              break;
@@ -20999,7 +20925,7 @@ dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
                    struct dwarf2_cu *cu, struct partial_symtab *pst,
                    CORE_ADDR lowpc, int decode_mapping)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   const int decode_for_pst_p = (pst != NULL);
 
   if (decode_mapping)
@@ -21014,8 +20940,10 @@ dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
       for (file_index = 0; file_index < lh->file_names.size (); file_index++)
         if (lh->file_names[file_index].included_p == 1)
           {
+           gdb::unique_xmalloc_ptr<char> name_holder;
            const char *include_name =
-             psymtab_include_file_name (lh, file_index, pst, comp_dir);
+             psymtab_include_file_name (lh, file_index, pst, comp_dir,
+                                        &name_holder);
            if (include_name != NULL)
               dwarf2_create_include_psymtab (include_name, pst, objfile);
           }
@@ -21099,8 +21027,8 @@ dwarf2_start_symtab (struct dwarf2_cu *cu,
                     const char *name, const char *comp_dir, CORE_ADDR low_pc)
 {
   struct compunit_symtab *cust
-    = start_symtab (cu->dwarf2_per_objfile->objfile, name, comp_dir, low_pc,
-                   cu->language);
+    = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
+                   low_pc, cu->language);
 
   record_debugformat ("DWARF 2");
   record_producer (cu->producer);
@@ -21117,7 +21045,7 @@ static void
 var_decode_location (struct attribute *attr, struct symbol *sym,
                     struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct comp_unit_head *cu_header = &cu->header;
 
   /* NOTE drow/2003-01-30: There used to be a comment and some special
@@ -21187,10 +21115,12 @@ var_decode_location (struct attribute *attr, struct symbol *sym,
    NULL, allocate a new symbol on the objfile's obstack.  */
 
 static struct symbol *
-new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
-                struct symbol *space)
+new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
+           struct symbol *space)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  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 symbol *sym = NULL;
   const char *name;
@@ -21258,8 +21188,7 @@ new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
            fe = NULL;
 
          if (fe == NULL)
-           complaint (&symfile_complaints,
-                      _("file index out of range"));
+           complaint (_("file index out of range"));
          else
            symbol_set_symtab (sym, fe->symtab);
        }
@@ -21544,7 +21473,7 @@ new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
             trash data, but since we must specifically ignore things
             we don't recognize, there is nothing else we should do at
             this point.  */
-         complaint (&symfile_complaints, _("unsupported tag: '%s'"),
+         complaint (_("unsupported tag: '%s'"),
                     dwarf_tag_name (die->tag));
          break;
        }
@@ -21568,14 +21497,6 @@ new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
   return (sym);
 }
 
-/* A wrapper for new_symbol_full that always allocates a new symbol.  */
-
-static struct symbol *
-new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
-{
-  return new_symbol_full (die, type, cu, NULL);
-}
-
 /* Given an attr with a DW_FORM_dataN value in host byte order,
    zero-extend it as appropriate for the symbol's type.  The DWARF
    standard (v4) is not entirely clear about the meaning of using
@@ -21588,7 +21509,7 @@ static gdb_byte *
 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
                         struct dwarf2_cu *cu, LONGEST *value, int bits)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
                                BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
   LONGEST l = DW_UNSND (attr);
@@ -21623,7 +21544,7 @@ dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
                         LONGEST *value, const gdb_byte **bytes,
                         struct dwarf2_locexpr_baton **baton)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   struct comp_unit_head *cu_header = &cu->header;
   struct dwarf_block *blk;
   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
@@ -21710,8 +21631,7 @@ dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
       break;
 
     default:
-      complaint (&symfile_complaints,
-                _("unsupported const value attribute form: '%s'"),
+      complaint (_("unsupported const value attribute form: '%s'"),
                 dwarf_form_name (attr->form));
       *value = 0;
       break;
@@ -21725,7 +21645,7 @@ static void
 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
                    struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   LONGEST value;
   const gdb_byte *bytes;
   struct dwarf2_locexpr_baton *baton;
@@ -21762,8 +21682,9 @@ die_type (struct die_info *die, struct dwarf2_cu *cu)
   type_attr = dwarf2_attr (die, DW_AT_type, cu);
   if (!type_attr)
     {
+      struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
       /* A missing DW_AT_type represents a void type.  */
-      return objfile_type (cu->dwarf2_per_objfile->objfile)->builtin_void;
+      return objfile_type (objfile)->builtin_void;
     }
 
   return lookup_die_type (die, type_attr, cu);
@@ -21776,15 +21697,9 @@ die_type (struct die_info *die, struct dwarf2_cu *cu)
 static int
 need_gnat_info (struct dwarf2_cu *cu)
 {
-  /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
-     of GNAT produces this auxiliary information, without any indication
-     that it is produced.  Part of enhancing the FSF version of GNAT
-     to produce that information will be to put in place an indicator
-     that we can use in order to determine whether the descriptive type
-     info is available or not.  One suggestion that has been made is
-     to use a new attribute, attached to the CU die.  For now, assume
-     that the descriptive type info is not available.  */
-  return 0;
+  /* Assume that the Ada compiler was GNAT, which always produces
+     the auxiliary information.  */
+  return (cu->language == language_ada);
 }
 
 /* Return the auxiliary type of the die in question using its
@@ -21826,11 +21741,12 @@ static struct type *
 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
 {
   struct attribute *type_attr;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
 
   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
   if (!type_attr)
     error (_("Dwarf Error: Problem turning containing type into gdb type "
-            "[in module %s]"), objfile_name (cu->dwarf2_per_objfile->objfile));
+            "[in module %s]"), objfile_name (objfile));
 
   return lookup_die_type (die, type_attr, cu);
 }
@@ -21840,13 +21756,15 @@ die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
 static struct type *
 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   char *message, *saved;
 
-  message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
+  message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
                        objfile_name (objfile),
-                       to_underlying (cu->header.sect_off),
-                       to_underlying (die->sect_off));
+                       sect_offset_str (cu->header.sect_off),
+                       sect_offset_str (die->sect_off));
   saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
                                  message, strlen (message));
   xfree (message);
@@ -21863,7 +21781,9 @@ static struct type *
 lookup_die_type (struct die_info *die, const struct attribute *attr,
                 struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct type *this_type;
 
   gdb_assert (attr->name == DW_AT_type
@@ -21877,7 +21797,8 @@ lookup_die_type (struct die_info *die, const struct attribute *attr,
       struct dwarf2_per_cu_data *per_cu;
       sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
 
-      per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, objfile);
+      per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
+                                                dwarf2_per_objfile);
       this_type = get_die_type_at_offset (sect_off, per_cu);
     }
   else if (attr_form_is_ref (attr))
@@ -21894,10 +21815,9 @@ lookup_die_type (struct die_info *die, const struct attribute *attr,
     }
   else
     {
-      complaint (&symfile_complaints,
-                _("Dwarf Error: Bad type attribute %s in DIE"
-                  " at 0x%x [in module %s]"),
-                dwarf_attr_name (attr->name), to_underlying (die->sect_off),
+      complaint (_("Dwarf Error: Bad type attribute %s in DIE"
+                  " at %s [in module %s]"),
+                dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
                 objfile_name (objfile));
       return build_error_marker_type (cu, die);
     }
@@ -22023,8 +21943,7 @@ read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
       this_type = read_tag_atomic_type (die, cu);
       break;
     default:
-      complaint (&symfile_complaints,
-                _("unexpected tag in read_type_die: '%s'"),
+      complaint (_("unexpected tag in read_type_die: '%s'"),
                 dwarf_tag_name (die->tag));
       break;
     }
@@ -22046,7 +21965,7 @@ guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
   struct die_info *spec_die;
   struct dwarf2_cu *spec_cu;
   struct die_info *child;
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
 
   spec_cu = cu;
   spec_die = die_specification (die, &spec_cu);
@@ -22130,7 +22049,7 @@ anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
     return "";
 
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
                                 DW_STRING (attr),
                                 &base[-1] - DW_STRING (attr));
@@ -22154,6 +22073,8 @@ anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
 static const char *
 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
   struct die_info *parent, *spec_die;
   struct dwarf2_cu *spec_cu;
   struct type *parent_type;
@@ -22236,8 +22157,7 @@ determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
         doesn't allow it), and break the loop here.  */
       name = dwarf2_name (die, cu);
       parent_name = dwarf2_name (parent, cu);
-      complaint (&symfile_complaints,
-                _("template param type '%s' defined within parent '%s'"),
+      complaint (_("template param type '%s' defined within parent '%s'"),
                 name ? name : "<unknown>",
                 parent_name ? parent_name : "<unknown>");
       return "";
@@ -22251,18 +22171,18 @@ determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
           DW_TAG_namespace DIEs with a name of "::" for the global namespace.
           Work around this problem here.  */
        if (cu->language == language_cplus
-           && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
+           && strcmp (TYPE_NAME (parent_type), "::") == 0)
          return "";
        /* We give a name to even anonymous namespaces.  */
-       return TYPE_TAG_NAME (parent_type);
+       return TYPE_NAME (parent_type);
       case DW_TAG_class_type:
       case DW_TAG_interface_type:
       case DW_TAG_structure_type:
       case DW_TAG_union_type:
       case DW_TAG_module:
        parent_type = read_type_die (parent, cu);
-       if (TYPE_TAG_NAME (parent_type) != NULL)
-         return TYPE_TAG_NAME (parent_type);
+       if (TYPE_NAME (parent_type) != NULL)
+         return TYPE_NAME (parent_type);
        else
          /* An anonymous structure is only allowed non-static data
             members; no typedefs, no member functions, et cetera.
@@ -22287,8 +22207,8 @@ determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
        parent_type = read_type_die (parent, cu);
        if (TYPE_DECLARED_CLASS (parent_type))
          {
-           if (TYPE_TAG_NAME (parent_type) != NULL)
-             return TYPE_TAG_NAME (parent_type);
+           if (TYPE_NAME (parent_type) != NULL)
+             return TYPE_NAME (parent_type);
            return "";
          }
        /* Fall through.  */
@@ -22399,7 +22319,7 @@ static const char *
 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
 {
   struct attribute *attr;
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
 
   attr = dwarf2_attr (die, DW_AT_name, cu);
   if ((!attr || !DW_STRING (attr))
@@ -22585,15 +22505,15 @@ dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
   unsigned int i;
 
   print_spaces (indent, f);
-  fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
+  fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
                      dwarf_tag_name (die->tag), die->abbrev,
-                     to_underlying (die->sect_off));
+                     sect_offset_str (die->sect_off));
 
   if (die->parent != NULL)
     {
       print_spaces (indent, f);
-      fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
-                         to_underlying (die->parent->sect_off));
+      fprintf_unfiltered (f, "  parent at offset: %s\n",
+                         sect_offset_str (die->parent->sect_off));
     }
 
   print_spaces (indent, f);
@@ -22772,8 +22692,7 @@ dwarf2_get_ref_die_offset (const struct attribute *attr)
   if (attr_form_is_ref (attr))
     return (sect_offset) DW_UNSND (attr);
 
-  complaint (&symfile_complaints,
-            _("unsupported die ref attribute form: '%s'"),
+  complaint (_("unsupported die ref attribute form: '%s'"),
             dwarf_form_name (attr->form));
   return {};
 }
@@ -22795,8 +22714,7 @@ dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
   else
     {
       /* For DW_FORM_data16 see attr_form_is_constant.  */
-      complaint (&symfile_complaints,
-                _("Attribute value is not a constant (%s)"),
+      complaint (_("Attribute value is not a constant (%s)"),
                  dwarf_form_name (attr->form));
       return default_value;
     }
@@ -22820,7 +22738,7 @@ follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
     {
       dump_die_for_error (src_die);
       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
-            objfile_name ((*ref_cu)->dwarf2_per_objfile->objfile));
+            objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
     }
 
   return die;
@@ -22837,7 +22755,8 @@ follow_die_offset (sect_offset sect_off, int offset_in_dwz,
 {
   struct die_info temp_die;
   struct dwarf2_cu *target_cu, *cu = *ref_cu;
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
 
   gdb_assert (cu->per_cu != NULL);
 
@@ -22857,11 +22776,11 @@ follow_die_offset (sect_offset sect_off, int offset_in_dwz,
       struct dwarf2_per_cu_data *per_cu;
 
       per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
-                                                objfile);
+                                                dwarf2_per_objfile);
 
       /* 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, cu->language);
+       load_full_comp_unit (per_cu, false, cu->language);
 
       target_cu = per_cu->cu;
     }
@@ -22869,7 +22788,7 @@ follow_die_offset (sect_offset sect_off, int offset_in_dwz,
     {
       /* We're loading full DIEs during partial symbol reading.  */
       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
-      load_full_comp_unit (cu->per_cu, language_minimal);
+      load_full_comp_unit (cu->per_cu, false, language_minimal);
     }
 
   *ref_cu = target_cu;
@@ -22896,10 +22815,10 @@ follow_die_ref (struct die_info *src_die, const struct attribute *attr,
                            || cu->per_cu->is_dwz),
                           ref_cu);
   if (!die)
-    error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
-          "at 0x%x [in module %s]"),
-          to_underlying (sect_off), to_underlying (src_die->sect_off),
-          objfile_name (cu->dwarf2_per_objfile->objfile));
+    error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
+          "at %s [in module %s]"),
+          sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
+          objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
 
   return die;
 }
@@ -22919,25 +22838,24 @@ dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
   struct die_info *die;
   struct attribute *attr;
   struct dwarf2_locexpr_baton retval;
-  struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
-
-  dw2_setup (objfile);
+  struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
 
   if (per_cu->cu == NULL)
-    load_cu (per_cu);
+    load_cu (per_cu, false);
   cu = per_cu->cu;
   if (cu == NULL)
     {
       /* We shouldn't get here for a dummy CU, but don't crash on the user.
         Instead just throw an error, not much else we can do.  */
-      error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
-            to_underlying (sect_off), objfile_name (objfile));
+      error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
+            sect_offset_str (sect_off), objfile_name (objfile));
     }
 
   die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
   if (!die)
-    error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
-          to_underlying (sect_off), objfile_name (objfile));
+    error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
+          sect_offset_str (sect_off), objfile_name (objfile));
 
   attr = dwarf2_attr (die, DW_AT_location, cu);
   if (!attr)
@@ -22963,16 +22881,16 @@ dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
   else
     {
       if (!attr_form_is_block (attr))
-       error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
+       error (_("Dwarf Error: DIE at %s referenced in module %s "
                 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
-              to_underlying (sect_off), objfile_name (objfile));
+              sect_offset_str (sect_off), objfile_name (objfile));
 
       retval.data = DW_BLOCK (attr)->data;
       retval.size = DW_BLOCK (attr)->size;
     }
   retval.per_cu = cu->per_cu;
 
-  age_cached_comp_units ();
+  age_cached_comp_units (dwarf2_per_objfile);
 
   return retval;
 }
@@ -23030,24 +22948,21 @@ dwarf2_fetch_constant_bytes (sect_offset sect_off,
   enum bfd_endian byte_order;
   struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
 
-  dw2_setup (objfile);
-
   if (per_cu->cu == NULL)
-    load_cu (per_cu);
+    load_cu (per_cu, false);
   cu = per_cu->cu;
   if (cu == NULL)
     {
       /* We shouldn't get here for a dummy CU, but don't crash on the user.
         Instead just throw an error, not much else we can do.  */
-      error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
-            to_underlying (sect_off), objfile_name (objfile));
+      error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
+            sect_offset_str (sect_off), objfile_name (objfile));
     }
 
   die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
   if (!die)
-    error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
-          to_underlying (sect_off), objfile_name (objfile));
-
+    error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
+          sect_offset_str (sect_off), objfile_name (objfile));
 
   attr = dwarf2_attr (die, DW_AT_const_value, cu);
   if (attr == NULL)
@@ -23136,8 +23051,7 @@ dwarf2_fetch_constant_bytes (sect_offset sect_off,
       break;
 
     default:
-      complaint (&symfile_complaints,
-                _("unsupported const value attribute form: '%s'"),
+      complaint (_("unsupported const value attribute form: '%s'"),
                 dwarf_form_name (attr->form));
       break;
     }
@@ -23155,10 +23069,8 @@ dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
   struct dwarf2_cu *cu;
   struct die_info *die;
 
-  dw2_setup (per_cu->dwarf2_per_objfile->objfile);
-
   if (per_cu->cu == NULL)
-    load_cu (per_cu);
+    load_cu (per_cu, false);
   cu = per_cu->cu;
   if (!cu)
     return NULL;
@@ -23177,8 +23089,6 @@ struct type *
 dwarf2_get_die_type (cu_offset die_offset,
                     struct dwarf2_per_cu_data *per_cu)
 {
-  dw2_setup (per_cu->dwarf2_per_objfile->objfile);
-
   sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
   return get_die_type_at_offset (die_offset_sect, per_cu);
 }
@@ -23213,6 +23123,9 @@ follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
                                                 to_underlying (temp_die.sect_off));
   if (die)
     {
+      struct dwarf2_per_objfile *dwarf2_per_objfile
+       = (*ref_cu)->per_cu->dwarf2_per_objfile;
+
       /* For .gdb_index version 7 keep track of included TUs.
         http://sourceware.org/bugzilla/show_bug.cgi?id=15021.  */
       if (dwarf2_per_objfile->index_table != NULL
@@ -23252,9 +23165,9 @@ follow_die_sig (struct die_info *src_die, const struct attribute *attr,
   if (sig_type == NULL)
     {
       error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
-               " from DIE at 0x%x [in module %s]"),
-             hex_string (signature), to_underlying (src_die->sect_off),
-            objfile_name ((*ref_cu)->dwarf2_per_objfile->objfile));
+               " from DIE at %s [in module %s]"),
+             hex_string (signature), sect_offset_str (src_die->sect_off),
+            objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
     }
 
   die = follow_die_sig_1 (src_die, sig_type, ref_cu);
@@ -23262,9 +23175,9 @@ follow_die_sig (struct die_info *src_die, const struct attribute *attr,
     {
       dump_die_for_error (src_die);
       error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
-              " from DIE at 0x%x [in module %s]"),
-            hex_string (signature), to_underlying (src_die->sect_off),
-            objfile_name ((*ref_cu)->dwarf2_per_objfile->objfile));
+              " from DIE at %s [in module %s]"),
+            hex_string (signature), sect_offset_str (src_die->sect_off),
+            objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
     }
 
   return die;
@@ -23277,6 +23190,8 @@ static struct type *
 get_signatured_type (struct die_info *die, ULONGEST signature,
                     struct dwarf2_cu *cu)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
   struct signatured_type *sig_type;
   struct dwarf2_cu *type_cu;
   struct die_info *type_die;
@@ -23287,10 +23202,9 @@ get_signatured_type (struct die_info *die, ULONGEST signature,
      the debug info.  */
   if (sig_type == NULL)
     {
-      complaint (&symfile_complaints,
-                _("Dwarf Error: Cannot find signatured DIE %s referenced"
-                  " from DIE at 0x%x [in module %s]"),
-                hex_string (signature), to_underlying (die->sect_off),
+      complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
+                  " from DIE at %s [in module %s]"),
+                hex_string (signature), sect_offset_str (die->sect_off),
                 objfile_name (dwarf2_per_objfile->objfile));
       return build_error_marker_type (cu, die);
     }
@@ -23309,20 +23223,18 @@ get_signatured_type (struct die_info *die, ULONGEST signature,
       type = read_type_die (type_die, type_cu);
       if (type == NULL)
        {
-         complaint (&symfile_complaints,
-                    _("Dwarf Error: Cannot build signatured type %s"
-                      " referenced from DIE at 0x%x [in module %s]"),
-                    hex_string (signature), to_underlying (die->sect_off),
+         complaint (_("Dwarf Error: Cannot build signatured type %s"
+                      " referenced from DIE at %s [in module %s]"),
+                    hex_string (signature), sect_offset_str (die->sect_off),
                     objfile_name (dwarf2_per_objfile->objfile));
          type = build_error_marker_type (cu, die);
        }
     }
   else
     {
-      complaint (&symfile_complaints,
-                _("Dwarf Error: Problem reading signatured DIE %s referenced"
-                  " from DIE at 0x%x [in module %s]"),
-                hex_string (signature), to_underlying (die->sect_off),
+      complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
+                  " from DIE at %s [in module %s]"),
+                hex_string (signature), sect_offset_str (die->sect_off),
                 objfile_name (dwarf2_per_objfile->objfile));
       type = build_error_marker_type (cu, die);
     }
@@ -23352,10 +23264,12 @@ get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
     }
   else
     {
-      complaint (&symfile_complaints,
-                _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
-                  " at 0x%x [in module %s]"),
-                dwarf_form_name (attr->form), to_underlying (die->sect_off),
+      struct dwarf2_per_objfile *dwarf2_per_objfile
+       = cu->per_cu->dwarf2_per_objfile;
+
+      complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
+                  " at %s [in module %s]"),
+                dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
                 objfile_name (dwarf2_per_objfile->objfile));
       return build_error_marker_type (cu, die);
     }
@@ -23433,7 +23347,7 @@ read_signatured_type (struct signatured_type *sig_type)
   gdb_assert (per_cu->is_debug_types);
   gdb_assert (per_cu->cu == NULL);
 
-  init_cutu_and_read_dies (per_cu, NULL, 0, 1,
+  init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
                           read_signatured_type_reader, NULL);
   sig_type->per_cu.tu_read = 1;
 }
@@ -23462,7 +23376,7 @@ read_signatured_type (struct signatured_type *sig_type)
 static CORE_ADDR
 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
 {
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   size_t i;
   size_t size = blk->size;
   const gdb_byte *data = blk->data;
@@ -23673,10 +23587,10 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
            const char *name = get_DW_OP_name (op);
 
            if (name)
-             complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
+             complaint (_("unsupported stack op: '%s'"),
                         name);
            else
-             complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
+             complaint (_("unsupported stack op: '%02x'"),
                         op);
          }
 
@@ -23687,15 +23601,13 @@ 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 (&symfile_complaints,
-                    _("location description stack overflow"));
+         complaint (_("location description stack overflow"));
          return 0;
        }
 
       if (stacki <= 0)
        {
-         complaint (&symfile_complaints,
-                    _("location description stack underflow"));
+         complaint (_("location description stack underflow"));
          return 0;
        }
     }
@@ -23758,8 +23670,7 @@ file_file_name (int file, struct line_header *lh)
       xsnprintf (fake_name, sizeof (fake_name),
                 "<bad macro file number %d>", file);
 
-      complaint (&symfile_complaints,
-                 _("bad file number in macro information (%d)"),
+      complaint (_("bad file number in macro information (%d)"),
                  file);
 
       return xstrdup (fake_name);
@@ -23821,8 +23732,7 @@ consume_improper_spaces (const char *p, const char *body)
 {
   if (*p == ' ')
     {
-      complaint (&symfile_complaints,
-                _("macro definition contains spaces "
+      complaint (_("macro definition contains spaces "
                   "in formal argument list:\n`%s'"),
                 body);
 
@@ -24051,8 +23961,7 @@ skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
 
     default:
       {
-       complaint (&symfile_complaints,
-                  _("invalid form 0x%x in `%s'"),
+       complaint (_("invalid form 0x%x in `%s'"),
                   form, get_section_name (section));
        return NULL;
       }
@@ -24079,8 +23988,7 @@ skip_unknown_opcode (unsigned int opcode,
 
   if (opcode_definitions[opcode] == NULL)
     {
-      complaint (&symfile_complaints,
-                _("unrecognized DW_MACFINO opcode 0x%x"),
+      complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
                 opcode);
       return NULL;
     }
@@ -24125,8 +24033,7 @@ dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
       version = read_2_bytes (abfd, mac_ptr);
       if (version != 4 && version != 5)
        {
-         complaint (&symfile_complaints,
-                    _("unrecognized version `%d' in .debug_macro section"),
+         complaint (_("unrecognized version `%d' in .debug_macro section"),
                     version);
          return NULL;
        }
@@ -24169,7 +24076,8 @@ dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
    including DW_MACRO_import.  */
 
 static void
-dwarf_decode_macro_bytes (bfd *abfd,
+dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
+                         bfd *abfd,
                          const gdb_byte *mac_ptr, const gdb_byte *mac_end,
                          struct macro_source_file *current_file,
                          struct line_header *lh,
@@ -24255,12 +24163,15 @@ dwarf_decode_macro_bytes (bfd *abfd,
                    || macinfo_type == DW_MACRO_undef_sup
                    || section_is_dwz)
                  {
-                   struct dwz_file *dwz = dwarf2_get_dwz_file ();
+                   struct dwz_file *dwz
+                     = dwarf2_get_dwz_file (dwarf2_per_objfile);
 
-                   body = read_indirect_string_from_dwz (dwz, str_offset);
+                   body = read_indirect_string_from_dwz (objfile,
+                                                         dwz, str_offset);
                  }
                else
-                 body = read_indirect_string_at_offset (abfd, str_offset);
+                 body = read_indirect_string_at_offset (dwarf2_per_objfile,
+                                                        abfd, str_offset);
              }
 
            is_define = (macinfo_type == DW_MACRO_define
@@ -24269,8 +24180,7 @@ dwarf_decode_macro_bytes (bfd *abfd,
             if (! current_file)
              {
                /* DWARF violation as no main source is present.  */
-               complaint (&symfile_complaints,
-                          _("debug info with no main source gives macro %s "
+               complaint (_("debug info with no main source gives macro %s "
                             "on line %d: %s"),
                           is_define ? _("definition") : _("undefinition"),
                           line, body);
@@ -24278,8 +24188,7 @@ dwarf_decode_macro_bytes (bfd *abfd,
              }
            if ((line == 0 && !at_commandline)
                || (line != 0 && at_commandline))
-             complaint (&symfile_complaints,
-                        _("debug info gives %s macro %s with %s line %d: %s"),
+             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);
@@ -24308,8 +24217,7 @@ dwarf_decode_macro_bytes (bfd *abfd,
 
            if ((line == 0 && !at_commandline)
                || (line != 0 && at_commandline))
-             complaint (&symfile_complaints,
-                        _("debug info gives source %d included "
+             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);
@@ -24327,8 +24235,7 @@ dwarf_decode_macro_bytes (bfd *abfd,
 
         case DW_MACRO_end_file:
           if (! current_file)
-           complaint (&symfile_complaints,
-                      _("macro debug info has an unmatched "
+           complaint (_("macro debug info has an unmatched "
                         "`close_file' directive"));
           else
             {
@@ -24355,8 +24262,7 @@ dwarf_decode_macro_bytes (bfd *abfd,
                    = (enum dwarf_macro_record_type) read_1_byte (abfd,
                                                                  mac_ptr);
                   if (next_type != 0)
-                   complaint (&symfile_complaints,
-                              _("no terminating 0-type entry for "
+                   complaint (_("no terminating 0-type entry for "
                                 "macros in `.debug_macinfo' section"));
 
                   return;
@@ -24380,7 +24286,7 @@ dwarf_decode_macro_bytes (bfd *abfd,
 
            if (macinfo_type == DW_MACRO_import_sup)
              {
-               struct dwz_file *dwz = dwarf2_get_dwz_file ();
+               struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
 
                dwarf2_read_section (objfile, &dwz->macro);
 
@@ -24397,15 +24303,15 @@ dwarf_decode_macro_bytes (bfd *abfd,
              {
                /* This has actually happened; see
                   http://sourceware.org/bugzilla/show_bug.cgi?id=13568.  */
-               complaint (&symfile_complaints,
-                          _("recursive DW_MACRO_import in "
+               complaint (_("recursive DW_MACRO_import in "
                             ".debug_macro section"));
              }
            else
              {
                *slot = (void *) new_mac_ptr;
 
-               dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
+               dwarf_decode_macro_bytes (dwarf2_per_objfile,
+                                         include_bfd, new_mac_ptr,
                                          include_mac_end, current_file, lh,
                                          section, section_is_gnu, is_dwz,
                                          offset_size, include_hash);
@@ -24448,6 +24354,8 @@ static void
 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
                      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;
@@ -24490,7 +24398,7 @@ dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
   dwarf2_read_section (objfile, section);
   if (section->buffer == NULL)
     {
-      complaint (&symfile_complaints, _("missing %s section"), section_name);
+      complaint (_("missing %s section"), section_name);
       return;
     }
   abfd = get_section_bfd_owner (section);
@@ -24630,7 +24538,8 @@ dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
   mac_ptr = section->buffer + offset;
   slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
   *slot = (void *) mac_ptr;
-  dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
+  dwarf_decode_macro_bytes (dwarf2_per_objfile,
+                           abfd, mac_ptr, mac_end,
                            current_file, lh, section,
                            section_is_gnu, 0, offset_size,
                            include_hash.get ());
@@ -24729,6 +24638,9 @@ attr_form_is_ref (const struct attribute *attr)
 static struct dwarf2_section_info *
 cu_debug_loc_section (struct dwarf2_cu *cu)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
+
   if (cu->dwo_unit)
     {
       struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
@@ -24746,6 +24658,8 @@ fill_in_loclist_baton (struct dwarf2_cu *cu,
                       struct dwarf2_loclist_baton *baton,
                       const struct attribute *attr)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
   struct dwarf2_section_info *section = cu_debug_loc_section (cu);
 
   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
@@ -24764,6 +24678,8 @@ static void
 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
                             struct dwarf2_cu *cu, int is_block)
 {
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = cu->per_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct dwarf2_section_info *section = cu_debug_loc_section (cu);
 
@@ -24780,8 +24696,7 @@ dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
       fill_in_loclist_baton (cu, baton, attr);
 
       if (cu->base_known == 0)
-       complaint (&symfile_complaints,
-                  _("Location list used without "
+       complaint (_("Location list used without "
                     "specifying the CU base address."));
 
       SYMBOL_ACLASS_INDEX (sym) = (is_block
@@ -24929,14 +24844,14 @@ dwarf2_version (struct dwarf2_per_cu_data *per_cu)
 static struct dwarf2_per_cu_data *
 dwarf2_find_containing_comp_unit (sect_offset sect_off,
                                  unsigned int offset_in_dwz,
-                                 struct objfile *objfile)
+                                 struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
   struct dwarf2_per_cu_data *this_cu;
   int low, high;
   const sect_offset *cu_off;
 
   low = 0;
-  high = dwarf2_per_objfile->n_comp_units - 1;
+  high = dwarf2_per_objfile->all_comp_units.size () - 1;
   while (high > low)
     {
       struct dwarf2_per_cu_data *mid_cu;
@@ -24957,8 +24872,9 @@ dwarf2_find_containing_comp_unit (sect_offset sect_off,
     {
       if (low == 0 || this_cu->is_dwz != offset_in_dwz)
        error (_("Dwarf Error: could not find partial DIE containing "
-              "offset 0x%x [in module %s]"),
-              to_underlying (sect_off), bfd_get_filename (objfile->obfd));
+              "offset %s [in module %s]"),
+              sect_offset_str (sect_off),
+              bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
 
       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
                  <= sect_off);
@@ -24967,9 +24883,9 @@ dwarf2_find_containing_comp_unit (sect_offset sect_off,
   else
     {
       this_cu = dwarf2_per_objfile->all_comp_units[low];
-      if (low == dwarf2_per_objfile->n_comp_units - 1
+      if (low == dwarf2_per_objfile->all_comp_units.size () - 1
          && sect_off >= this_cu->sect_off + this_cu->length)
-       error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
+       error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
       gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
       return this_cu;
     }
@@ -24977,14 +24893,24 @@ dwarf2_find_containing_comp_unit (sect_offset sect_off,
 
 /* Initialize dwarf2_cu CU, owned by PER_CU.  */
 
-static void
-init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
+dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
+  : per_cu (per_cu_),
+    mark (0),
+    has_loclist (0),
+    checked_producer (0),
+    producer_is_gxx_lt_4_6 (0),
+    producer_is_gcc_lt_4_3 (0),
+    producer_is_icc_lt_14 (0),
+    processing_has_namespace_info (0)
+{
+  per_cu->cu = this;
+}
+
+/* Destroy a dwarf2_cu.  */
+
+dwarf2_cu::~dwarf2_cu ()
 {
-  memset (cu, 0, sizeof (*cu));
-  per_cu->cu = cu;
-  cu->per_cu = per_cu;
-  cu->dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
-  obstack_init (&cu->comp_unit_obstack);
+  per_cu->cu = NULL;
 }
 
 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
@@ -25008,56 +24934,11 @@ prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
   cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
 }
 
-/* Release one cached compilation unit, CU.  We unlink it from the tree
-   of compilation units, but we don't remove it from the read_in_chain;
-   the caller is responsible for that.
-   NOTE: DATA is a void * because this function is also used as a
-   cleanup routine.  */
-
-static void
-free_heap_comp_unit (void *data)
-{
-  struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
-
-  gdb_assert (cu->per_cu != NULL);
-  cu->per_cu->cu = NULL;
-  cu->per_cu = NULL;
-
-  obstack_free (&cu->comp_unit_obstack, NULL);
-
-  xfree (cu);
-}
-
-/* This cleanup function is passed the address of a dwarf2_cu on the stack
-   when we're finished with it.  We can't free the pointer itself, but be
-   sure to unlink it from the cache.  Also release any associated storage.  */
-
-static void
-free_stack_comp_unit (void *data)
-{
-  struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
-
-  gdb_assert (cu->per_cu != NULL);
-  cu->per_cu->cu = NULL;
-  cu->per_cu = NULL;
-
-  obstack_free (&cu->comp_unit_obstack, NULL);
-  cu->partial_dies = NULL;
-}
-
-/* Free all cached compilation units.  */
-
-static void
-free_cached_comp_units (void *data)
-{
-  dwarf2_per_objfile->free_cached_comp_units ();
-}
-
 /* Increase the age counter on each cached compilation unit, and free
    any that are too old.  */
 
 static void
-age_cached_comp_units (void)
+age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
 {
   struct dwarf2_per_cu_data *per_cu, **last_chain;
 
@@ -25081,7 +24962,7 @@ age_cached_comp_units (void)
 
       if (!per_cu->cu->mark)
        {
-         free_heap_comp_unit (per_cu->cu);
+         delete per_cu->cu;
          *last_chain = next_cu;
        }
       else
@@ -25097,6 +24978,8 @@ static void
 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
 {
   struct dwarf2_per_cu_data *per_cu, **last_chain;
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = target_per_cu->dwarf2_per_objfile;
 
   per_cu = dwarf2_per_objfile->read_in_chain;
   last_chain = &dwarf2_per_objfile->read_in_chain;
@@ -25108,7 +24991,7 @@ free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
 
       if (per_cu == target_per_cu)
        {
-         free_heap_comp_unit (per_cu->cu);
+         delete per_cu->cu;
          per_cu->cu = NULL;
          *last_chain = next_cu;
          break;
@@ -25125,14 +25008,10 @@ free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
 void
 dwarf2_free_objfile (struct objfile *objfile)
 {
-  dwarf2_per_objfile
-    = (struct dwarf2_per_objfile *) objfile_data (objfile,
-                                                 dwarf2_objfile_data_key);
-
-  if (dwarf2_per_objfile == NULL)
-    return;
+  struct dwarf2_per_objfile *dwarf2_per_objfile
+    = get_dwarf2_per_objfile (objfile);
 
-  dwarf2_per_objfile->~dwarf2_per_objfile ();
+  delete dwarf2_per_objfile;
 }
 
 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
@@ -25202,8 +25081,10 @@ per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
 static struct type *
 set_die_type (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 dwarf2_per_cu_offset_and_type **slot, ofs;
-  struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct attribute *attr;
   struct dynamic_prop prop;
 
@@ -25228,14 +25109,13 @@ set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
   if (attr_form_is_block (attr))
     {
       if (attr_to_dynamic_prop (attr, die, cu, &prop))
-        add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
+        add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
     }
   else if (attr != NULL)
     {
-      complaint (&symfile_complaints,
-                _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
+      complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
                 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
-                to_underlying (die->sect_off));
+                sect_offset_str (die->sect_off));
     }
 
   /* Read DW_AT_associated and set in type.  */
@@ -25243,20 +25123,19 @@ set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
   if (attr_form_is_block (attr))
     {
       if (attr_to_dynamic_prop (attr, die, cu, &prop))
-        add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
+        add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
     }
   else if (attr != NULL)
     {
-      complaint (&symfile_complaints,
-                _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
+      complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
                 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
-                to_underlying (die->sect_off));
+                sect_offset_str (die->sect_off));
     }
 
   /* Read DW_AT_data_location and set in type.  */
   attr = dwarf2_attr (die, DW_AT_data_location, cu);
   if (attr_to_dynamic_prop (attr, die, cu, &prop))
-    add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
+    add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
 
   if (dwarf2_per_objfile->die_type_hash == NULL)
     {
@@ -25276,9 +25155,8 @@ set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
   slot = (struct dwarf2_per_cu_offset_and_type **)
     htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
   if (*slot)
-    complaint (&symfile_complaints,
-              _("A problem internal to GDB: DIE 0x%x has type already set"),
-              to_underlying (die->sect_off));
+    complaint (_("A problem internal to GDB: DIE %s has type already set"),
+              sect_offset_str (die->sect_off));
   *slot = XOBNEW (&objfile->objfile_obstack,
                  struct dwarf2_per_cu_offset_and_type);
   **slot = ofs;
@@ -25293,6 +25171,7 @@ get_die_type_at_offset (sect_offset sect_off,
                        struct dwarf2_per_cu_data *per_cu)
 {
   struct dwarf2_per_cu_offset_and_type *slot, ofs;
+  struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
 
   if (dwarf2_per_objfile->die_type_hash == NULL)
     return NULL;
@@ -25428,1734 +25307,105 @@ show_dwarf_cmd (const char *args, int from_tty)
   cmd_show_list (show_dwarf_cmdlist, from_tty, "");
 }
 
-/* Free data associated with OBJFILE, if necessary.  */
-
-static void
-dwarf2_per_objfile_free (struct objfile *objfile, void *d)
-{
-  struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
-  int ix;
-
-  /* Make sure we don't accidentally use dwarf2_per_objfile while
-     cleaning up.  */
-  dwarf2_per_objfile = NULL;
-
-  for (ix = 0; ix < data->n_comp_units; ++ix)
-   VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
-
-  for (ix = 0; ix < data->n_type_units; ++ix)
-    VEC_free (dwarf2_per_cu_ptr,
-             data->all_type_units[ix]->per_cu.imported_symtabs);
-  xfree (data->all_type_units);
-
-  VEC_free (dwarf2_section_info_def, data->types);
-
-  if (data->dwo_files)
-    free_dwo_files (data->dwo_files, objfile);
-  if (data->dwp_file)
-    gdb_bfd_unref (data->dwp_file->dbfd);
-
-  if (data->dwz_file && data->dwz_file->dwz_bfd)
-    gdb_bfd_unref (data->dwz_file->dwz_bfd);
-
-  if (data->index_table != NULL)
-    data->index_table->~mapped_index ();
-}
-
-\f
-/* The "save gdb-index" command.  */
-
-/* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
-   error checking.  */
+int dwarf_always_disassemble;
 
 static void
-file_write (FILE *file, const void *data, size_t size)
+show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
+                              struct cmd_list_element *c, const char *value)
 {
-  if (fwrite (data, 1, size, file) != size)
-    error (_("couldn't data write to file"));
+  fprintf_filtered (file,
+                   _("Whether to always disassemble "
+                     "DWARF expressions is %s.\n"),
+                   value);
 }
 
-/* Write the contents of VEC to FILE, with error checking.  */
-
-template<typename Elem, typename Alloc>
 static void
-file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
+show_check_physname (struct ui_file *file, int from_tty,
+                    struct cmd_list_element *c, const char *value)
 {
-  file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
+  fprintf_filtered (file,
+                   _("Whether to check \"physname\" is %s.\n"),
+                   value);
 }
 
-/* In-memory buffer to prepare data to be written later to a file.  */
-class data_buf
+void
+_initialize_dwarf2_read (void)
 {
-public:
-  /* Copy DATA to the end of the buffer.  */
-  template<typename T>
-  void append_data (const T &data)
-  {
-    std::copy (reinterpret_cast<const gdb_byte *> (&data),
-              reinterpret_cast<const gdb_byte *> (&data + 1),
-              grow (sizeof (data)));
-  }
 
-  /* Copy CSTR (a zero-terminated string) to the end of buffer.  The
-     terminating zero is appended too.  */
-  void append_cstr0 (const char *cstr)
-  {
-    const size_t size = strlen (cstr) + 1;
-    std::copy (cstr, cstr + size, grow (size));
-  }
-
-  /* Store INPUT as ULEB128 to the end of buffer.  */
-  void append_unsigned_leb128 (ULONGEST input)
-  {
-    for (;;)
-      {
-       gdb_byte output = input & 0x7f;
-       input >>= 7;
-       if (input)
-         output |= 0x80;
-       append_data (output);
-       if (input == 0)
-         break;
-      }
-  }
+  dwarf2_objfile_data_key = register_objfile_data ();
 
-  /* Accept a host-format integer in VAL and append it to the buffer
-     as a target-format integer which is LEN bytes long.  */
-  void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
-  {
-    ::store_unsigned_integer (grow (len), len, byte_order, val);
-  }
+  add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
+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);
 
-  /* Return the size of the buffer.  */
-  size_t size () const
-  {
-    return m_vec.size ();
-  }
+  add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
+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);
 
-  /* Return true iff the buffer is empty.  */
-  bool empty () const
-  {
-    return m_vec.empty ();
-  }
+  add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
+                           &dwarf_max_cache_age, _("\
+Set the upper bound on the age of cached DWARF compilation units."), _("\
+Show the upper bound on the age of cached DWARF compilation units."), _("\
+A higher limit means that cached compilation units will be stored\n\
+in memory longer, and more total memory will be used.  Zero disables\n\
+caching, which can slow down startup."),
+                           NULL,
+                           show_dwarf_max_cache_age,
+                           &set_dwarf_cmdlist,
+                           &show_dwarf_cmdlist);
 
-  /* Write the buffer to FILE.  */
-  void file_write (FILE *file) const
-  {
-    ::file_write (file, m_vec);
-  }
+  add_setshow_boolean_cmd ("always-disassemble", class_obscure,
+                          &dwarf_always_disassemble, _("\
+Set whether `info address' always disassembles DWARF expressions."), _("\
+Show whether `info address' always disassembles DWARF expressions."), _("\
+When enabled, DWARF expressions are always printed in an assembly-like\n\
+syntax.  When disabled, expressions will be printed in a more\n\
+conversational style, when possible."),
+                          NULL,
+                          show_dwarf_always_disassemble,
+                          &set_dwarf_cmdlist,
+                          &show_dwarf_cmdlist);
 
-private:
-  /* Grow SIZE bytes at the end of the buffer.  Returns a pointer to
-     the start of the new block.  */
-  gdb_byte *grow (size_t size)
-  {
-    m_vec.resize (m_vec.size () + size);
-    return &*m_vec.end () - size;
-  }
+  add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
+Set debugging of the DWARF reader."), _("\
+Show debugging of the DWARF reader."), _("\
+When enabled (non-zero), debugging messages are printed during DWARF\n\
+reading and symtab expansion.  A value of 1 (one) provides basic\n\
+information.  A value greater than 1 provides more verbose information."),
+                           NULL,
+                           NULL,
+                           &setdebuglist, &showdebuglist);
 
-  gdb::byte_vector m_vec;
-};
+  add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
+Set debugging of the DWARF DIE reader."), _("\
+Show debugging of the DWARF DIE reader."), _("\
+When enabled (non-zero), DIEs are dumped after they are read in.\n\
+The value is the maximum depth to print."),
+                            NULL,
+                            NULL,
+                            &setdebuglist, &showdebuglist);
 
-/* An entry in the symbol table.  */
-struct symtab_index_entry
-{
-  /* The name of the symbol.  */
-  const char *name;
-  /* The offset of the name in the constant pool.  */
-  offset_type index_offset;
-  /* A sorted vector of the indices of all the CUs that hold an object
-     of this name.  */
-  std::vector<offset_type> cu_indices;
-};
+  add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
+Set debugging of the dwarf line reader."), _("\
+Show debugging of the dwarf line reader."), _("\
+When enabled (non-zero), line number entries are dumped as they are read in.\n\
+A value of 1 (one) provides basic information.\n\
+A value greater than 1 provides more verbose information."),
+                            NULL,
+                            NULL,
+                            &setdebuglist, &showdebuglist);
 
-/* The symbol table.  This is a power-of-2-sized hash table.  */
-struct mapped_symtab
-{
-  mapped_symtab ()
-  {
-    data.resize (1024);
-  }
-
-  offset_type n_elements = 0;
-  std::vector<symtab_index_entry> data;
-};
-
-/* Find a slot in SYMTAB for the symbol NAME.  Returns a reference to
-   the slot.
-   
-   Function is used only during write_hash_table so no index format backward
-   compatibility is needed.  */
-
-static symtab_index_entry &
-find_slot (struct mapped_symtab *symtab, const char *name)
-{
-  offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
-
-  index = hash & (symtab->data.size () - 1);
-  step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
-
-  for (;;)
-    {
-      if (symtab->data[index].name == NULL
-         || strcmp (name, symtab->data[index].name) == 0)
-       return symtab->data[index];
-      index = (index + step) & (symtab->data.size () - 1);
-    }
-}
-
-/* Expand SYMTAB's hash table.  */
-
-static void
-hash_expand (struct mapped_symtab *symtab)
-{
-  auto old_entries = std::move (symtab->data);
-
-  symtab->data.clear ();
-  symtab->data.resize (old_entries.size () * 2);
-
-  for (auto &it : old_entries)
-    if (it.name != NULL)
-      {
-       auto &ref = find_slot (symtab, it.name);
-       ref = std::move (it);
-      }
-}
-
-/* Add an entry to SYMTAB.  NAME is the name of the symbol.
-   CU_INDEX is the index of the CU in which the symbol appears.
-   IS_STATIC is one if the symbol is static, otherwise zero (global).  */
-
-static void
-add_index_entry (struct mapped_symtab *symtab, const char *name,
-                int is_static, gdb_index_symbol_kind kind,
-                offset_type cu_index)
-{
-  offset_type cu_index_and_attrs;
-
-  ++symtab->n_elements;
-  if (4 * symtab->n_elements / 3 >= symtab->data.size ())
-    hash_expand (symtab);
-
-  symtab_index_entry &slot = find_slot (symtab, name);
-  if (slot.name == NULL)
-    {
-      slot.name = name;
-      /* index_offset is set later.  */
-    }
-
-  cu_index_and_attrs = 0;
-  DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
-  DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
-  DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
-
-  /* We don't want to record an index value twice as we want to avoid the
-     duplication.
-     We process all global symbols and then all static symbols
-     (which would allow us to avoid the duplication by only having to check
-     the last entry pushed), but a symbol could have multiple kinds in one CU.
-     To keep things simple we don't worry about the duplication here and
-     sort and uniqufy the list after we've processed all symbols.  */
-  slot.cu_indices.push_back (cu_index_and_attrs);
-}
-
-/* Sort and remove duplicates of all symbols' cu_indices lists.  */
-
-static void
-uniquify_cu_indices (struct mapped_symtab *symtab)
-{
-  for (auto &entry : symtab->data)
-    {
-      if (entry.name != NULL && !entry.cu_indices.empty ())
-       {
-         auto &cu_indices = entry.cu_indices;
-         std::sort (cu_indices.begin (), cu_indices.end ());
-         auto from = std::unique (cu_indices.begin (), cu_indices.end ());
-         cu_indices.erase (from, cu_indices.end ());
-       }
-    }
-}
-
-/* A form of 'const char *' suitable for container keys.  Only the
-   pointer is stored.  The strings themselves are compared, not the
-   pointers.  */
-class c_str_view
-{
-public:
-  c_str_view (const char *cstr)
-    : m_cstr (cstr)
-  {}
-
-  bool operator== (const c_str_view &other) const
-  {
-    return strcmp (m_cstr, other.m_cstr) == 0;
-  }
-
-  /* Return the underlying C string.  Note, the returned string is
-     only a reference with lifetime of this object.  */
-  const char *c_str () const
-  {
-    return m_cstr;
-  }
-
-private:
-  friend class c_str_view_hasher;
-  const char *const m_cstr;
-};
-
-/* A std::unordered_map::hasher for c_str_view that uses the right
-   hash function for strings in a mapped index.  */
-class c_str_view_hasher
-{
-public:
-  size_t operator () (const c_str_view &x) const
-  {
-    return mapped_index_string_hash (INT_MAX, x.m_cstr);
-  }
-};
-
-/* A std::unordered_map::hasher for std::vector<>.  */
-template<typename T>
-class vector_hasher
-{
-public:
-  size_t operator () (const std::vector<T> &key) const
-  {
-    return iterative_hash (key.data (),
-                          sizeof (key.front ()) * key.size (), 0);
-  }
-};
-
-/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
-   constant pool entries going into the data buffer CPOOL.  */
-
-static void
-write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
-{
-  {
-    /* Elements are sorted vectors of the indices of all the CUs that
-       hold an object of this name.  */
-    std::unordered_map<std::vector<offset_type>, offset_type,
-                      vector_hasher<offset_type>>
-      symbol_hash_table;
-
-    /* We add all the index vectors to the constant pool first, to
-       ensure alignment is ok.  */
-    for (symtab_index_entry &entry : symtab->data)
-      {
-       if (entry.name == NULL)
-         continue;
-       gdb_assert (entry.index_offset == 0);
-
-       /* Finding before inserting is faster than always trying to
-          insert, because inserting always allocates a node, does the
-          lookup, and then destroys the new node if another node
-          already had the same key.  C++17 try_emplace will avoid
-          this.  */
-       const auto found
-         = symbol_hash_table.find (entry.cu_indices);
-       if (found != symbol_hash_table.end ())
-         {
-           entry.index_offset = found->second;
-           continue;
-         }
-
-       symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
-       entry.index_offset = cpool.size ();
-       cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
-       for (const auto index : entry.cu_indices)
-         cpool.append_data (MAYBE_SWAP (index));
-      }
-  }
-
-  /* Now write out the hash table.  */
-  std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
-  for (const auto &entry : symtab->data)
-    {
-      offset_type str_off, vec_off;
-
-      if (entry.name != NULL)
-       {
-         const auto insertpair = str_table.emplace (entry.name, cpool.size ());
-         if (insertpair.second)
-           cpool.append_cstr0 (entry.name);
-         str_off = insertpair.first->second;
-         vec_off = entry.index_offset;
-       }
-      else
-       {
-         /* While 0 is a valid constant pool index, it is not valid
-            to have 0 for both offsets.  */
-         str_off = 0;
-         vec_off = 0;
-       }
-
-      output.append_data (MAYBE_SWAP (str_off));
-      output.append_data (MAYBE_SWAP (vec_off));
-    }
-}
-
-typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
-
-/* Helper struct for building the address table.  */
-struct addrmap_index_data
-{
-  addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
-    : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
-  {}
-
-  struct objfile *objfile;
-  data_buf &addr_vec;
-  psym_index_map &cu_index_htab;
-
-  /* Non-zero if the previous_* fields are valid.
-     We can't write an entry until we see the next entry (since it is only then
-     that we know the end of the entry).  */
-  int previous_valid;
-  /* Index of the CU in the table of all CUs in the index file.  */
-  unsigned int previous_cu_index;
-  /* Start address of the CU.  */
-  CORE_ADDR previous_cu_start;
-};
-
-/* Write an address entry to ADDR_VEC.  */
-
-static void
-add_address_entry (struct objfile *objfile, data_buf &addr_vec,
-                  CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
-{
-  CORE_ADDR baseaddr;
-
-  baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
-
-  addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
-  addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
-  addr_vec.append_data (MAYBE_SWAP (cu_index));
-}
-
-/* Worker function for traversing an addrmap to build the address table.  */
-
-static int
-add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
-{
-  struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
-  struct partial_symtab *pst = (struct partial_symtab *) obj;
-
-  if (data->previous_valid)
-    add_address_entry (data->objfile, data->addr_vec,
-                      data->previous_cu_start, start_addr,
-                      data->previous_cu_index);
-
-  data->previous_cu_start = start_addr;
-  if (pst != NULL)
-    {
-      const auto it = data->cu_index_htab.find (pst);
-      gdb_assert (it != data->cu_index_htab.cend ());
-      data->previous_cu_index = it->second;
-      data->previous_valid = 1;
-    }
-  else
-    data->previous_valid = 0;
-
-  return 0;
-}
-
-/* Write OBJFILE's address map to ADDR_VEC.
-   CU_INDEX_HTAB is used to map addrmap entries to their CU indices
-   in the index file.  */
-
-static void
-write_address_map (struct objfile *objfile, data_buf &addr_vec,
-                  psym_index_map &cu_index_htab)
-{
-  struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
-
-  /* When writing the address table, we have to cope with the fact that
-     the addrmap iterator only provides the start of a region; we have to
-     wait until the next invocation to get the start of the next region.  */
-
-  addrmap_index_data.objfile = objfile;
-  addrmap_index_data.previous_valid = 0;
-
-  addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
-                  &addrmap_index_data);
-
-  /* It's highly unlikely the last entry (end address = 0xff...ff)
-     is valid, but we should still handle it.
-     The end address is recorded as the start of the next region, but that
-     doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
-     anyway.  */
-  if (addrmap_index_data.previous_valid)
-    add_address_entry (objfile, addr_vec,
-                      addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
-                      addrmap_index_data.previous_cu_index);
-}
-
-/* Return the symbol kind of PSYM.  */
-
-static gdb_index_symbol_kind
-symbol_kind (struct partial_symbol *psym)
-{
-  domain_enum domain = PSYMBOL_DOMAIN (psym);
-  enum address_class aclass = PSYMBOL_CLASS (psym);
-
-  switch (domain)
-    {
-    case VAR_DOMAIN:
-      switch (aclass)
-       {
-       case LOC_BLOCK:
-         return GDB_INDEX_SYMBOL_KIND_FUNCTION;
-       case LOC_TYPEDEF:
-         return GDB_INDEX_SYMBOL_KIND_TYPE;
-       case LOC_COMPUTED:
-       case LOC_CONST_BYTES:
-       case LOC_OPTIMIZED_OUT:
-       case LOC_STATIC:
-         return GDB_INDEX_SYMBOL_KIND_VARIABLE;
-       case LOC_CONST:
-         /* Note: It's currently impossible to recognize psyms as enum values
-            short of reading the type info.  For now punt.  */
-         return GDB_INDEX_SYMBOL_KIND_VARIABLE;
-       default:
-         /* There are other LOC_FOO values that one might want to classify
-            as variables, but dwarf2read.c doesn't currently use them.  */
-         return GDB_INDEX_SYMBOL_KIND_OTHER;
-       }
-    case STRUCT_DOMAIN:
-      return GDB_INDEX_SYMBOL_KIND_TYPE;
-    default:
-      return GDB_INDEX_SYMBOL_KIND_OTHER;
-    }
-}
-
-/* Add a list of partial symbols to SYMTAB.  */
-
-static void
-write_psymbols (struct mapped_symtab *symtab,
-               std::unordered_set<partial_symbol *> &psyms_seen,
-               struct partial_symbol **psymp,
-               int count,
-               offset_type cu_index,
-               int is_static)
-{
-  for (; count-- > 0; ++psymp)
-    {
-      struct partial_symbol *psym = *psymp;
-
-      if (SYMBOL_LANGUAGE (psym) == language_ada)
-       error (_("Ada is not currently supported by the index"));
-
-      /* Only add a given psymbol once.  */
-      if (psyms_seen.insert (psym).second)
-       {
-         gdb_index_symbol_kind kind = symbol_kind (psym);
-
-         add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
-                          is_static, kind, cu_index);
-       }
-    }
-}
-
-/* A helper struct used when iterating over debug_types.  */
-struct signatured_type_index_data
-{
-  signatured_type_index_data (data_buf &types_list_,
-                              std::unordered_set<partial_symbol *> &psyms_seen_)
-    : types_list (types_list_), psyms_seen (psyms_seen_)
-  {}
-
-  struct objfile *objfile;
-  struct mapped_symtab *symtab;
-  data_buf &types_list;
-  std::unordered_set<partial_symbol *> &psyms_seen;
-  int cu_index;
-};
-
-/* A helper function that writes a single signatured_type to an
-   obstack.  */
-
-static int
-write_one_signatured_type (void **slot, void *d)
-{
-  struct signatured_type_index_data *info
-    = (struct signatured_type_index_data *) d;
-  struct signatured_type *entry = (struct signatured_type *) *slot;
-  struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
-
-  write_psymbols (info->symtab,
-                 info->psyms_seen,
-                 &info->objfile->global_psymbols[psymtab->globals_offset],
-                 psymtab->n_global_syms, info->cu_index,
-                 0);
-  write_psymbols (info->symtab,
-                 info->psyms_seen,
-                 &info->objfile->static_psymbols[psymtab->statics_offset],
-                 psymtab->n_static_syms, info->cu_index,
-                 1);
-
-  info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
-                               to_underlying (entry->per_cu.sect_off));
-  info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
-                               to_underlying (entry->type_offset_in_tu));
-  info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
-
-  ++info->cu_index;
-
-  return 1;
-}
-
-/* Recurse into all "included" dependencies and count their symbols as
-   if they appeared in this psymtab.  */
-
-static void
-recursively_count_psymbols (struct partial_symtab *psymtab,
-                           size_t &psyms_seen)
-{
-  for (int i = 0; i < psymtab->number_of_dependencies; ++i)
-    if (psymtab->dependencies[i]->user != NULL)
-      recursively_count_psymbols (psymtab->dependencies[i],
-                                 psyms_seen);
-
-  psyms_seen += psymtab->n_global_syms;
-  psyms_seen += psymtab->n_static_syms;
-}
-
-/* Recurse into all "included" dependencies and write their symbols as
-   if they appeared in this psymtab.  */
-
-static void
-recursively_write_psymbols (struct objfile *objfile,
-                           struct partial_symtab *psymtab,
-                           struct mapped_symtab *symtab,
-                           std::unordered_set<partial_symbol *> &psyms_seen,
-                           offset_type cu_index)
-{
-  int i;
-
-  for (i = 0; i < psymtab->number_of_dependencies; ++i)
-    if (psymtab->dependencies[i]->user != NULL)
-      recursively_write_psymbols (objfile, psymtab->dependencies[i],
-                                 symtab, psyms_seen, cu_index);
-
-  write_psymbols (symtab,
-                 psyms_seen,
-                 &objfile->global_psymbols[psymtab->globals_offset],
-                 psymtab->n_global_syms, cu_index,
-                 0);
-  write_psymbols (symtab,
-                 psyms_seen,
-                 &objfile->static_psymbols[psymtab->statics_offset],
-                 psymtab->n_static_syms, cu_index,
-                 1);
-}
-
-/* DWARF-5 .debug_names builder.  */
-class debug_names
-{
-public:
-  debug_names (bool is_dwarf64, bfd_endian dwarf5_byte_order)
-    : m_dwarf5_byte_order (dwarf5_byte_order),
-      m_dwarf32 (dwarf5_byte_order),
-      m_dwarf64 (dwarf5_byte_order),
-      m_dwarf (is_dwarf64
-              ? static_cast<dwarf &> (m_dwarf64)
-              : static_cast<dwarf &> (m_dwarf32)),
-      m_name_table_string_offs (m_dwarf.name_table_string_offs),
-      m_name_table_entry_offs (m_dwarf.name_table_entry_offs)
-  {}
-
-  int dwarf5_offset_size () const
-  {
-    const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
-    return dwarf5_is_dwarf64 ? 8 : 4;
-  }
-
-  /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit?  */
-  enum class unit_kind { cu, tu };
-
-  /* Insert one symbol.  */
-  void insert (const partial_symbol *psym, int cu_index, bool is_static,
-              unit_kind kind)
-  {
-    const int dwarf_tag = psymbol_tag (psym);
-    if (dwarf_tag == 0)
-      return;
-    const char *const name = SYMBOL_SEARCH_NAME (psym);
-    const auto insertpair
-      = m_name_to_value_set.emplace (c_str_view (name),
-                                    std::set<symbol_value> ());
-    std::set<symbol_value> &value_set = insertpair.first->second;
-    value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
-  }
-
-  /* Build all the tables.  All symbols must be already inserted.
-     This function does not call file_write, caller has to do it
-     afterwards.  */
-  void build ()
-  {
-    /* Verify the build method has not be called twice.  */
-    gdb_assert (m_abbrev_table.empty ());
-    const size_t name_count = m_name_to_value_set.size ();
-    m_bucket_table.resize
-      (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
-    m_hash_table.reserve (name_count);
-    m_name_table_string_offs.reserve (name_count);
-    m_name_table_entry_offs.reserve (name_count);
-
-    /* Map each hash of symbol to its name and value.  */
-    struct hash_it_pair
-    {
-      uint32_t hash;
-      decltype (m_name_to_value_set)::const_iterator it;
-    };
-    std::vector<std::forward_list<hash_it_pair>> bucket_hash;
-    bucket_hash.resize (m_bucket_table.size ());
-    for (decltype (m_name_to_value_set)::const_iterator it
-          = m_name_to_value_set.cbegin ();
-        it != m_name_to_value_set.cend ();
-        ++it)
-      {
-       const char *const name = it->first.c_str ();
-       const uint32_t hash = dwarf5_djb_hash (name);
-       hash_it_pair hashitpair;
-       hashitpair.hash = hash;
-       hashitpair.it = it;
-       auto &slot = bucket_hash[hash % bucket_hash.size()];
-       slot.push_front (std::move (hashitpair));
-      }
-    for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
-      {
-       const std::forward_list<hash_it_pair> &hashitlist
-         = bucket_hash[bucket_ix];
-       if (hashitlist.empty ())
-         continue;
-       uint32_t &bucket_slot = m_bucket_table[bucket_ix];
-       /* The hashes array is indexed starting at 1.  */
-       store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
-                               sizeof (bucket_slot), m_dwarf5_byte_order,
-                               m_hash_table.size () + 1);
-       for (const hash_it_pair &hashitpair : hashitlist)
-         {
-           m_hash_table.push_back (0);
-           store_unsigned_integer (reinterpret_cast<gdb_byte *>
-                                                       (&m_hash_table.back ()),
-                                   sizeof (m_hash_table.back ()),
-                                   m_dwarf5_byte_order, hashitpair.hash);
-           const c_str_view &name = hashitpair.it->first;
-           const std::set<symbol_value> &value_set = hashitpair.it->second;
-           m_name_table_string_offs.push_back_reorder
-             (m_debugstrlookup.lookup (name.c_str ()));
-           m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
-           gdb_assert (!value_set.empty ());
-           for (const symbol_value &value : value_set)
-             {
-               int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
-                                                       value.is_static,
-                                                       value.kind)];
-               if (idx == 0)
-                 {
-                   idx = m_idx_next++;
-                   m_abbrev_table.append_unsigned_leb128 (idx);
-                   m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
-                   m_abbrev_table.append_unsigned_leb128
-                             (value.kind == unit_kind::cu ? DW_IDX_compile_unit
-                                                          : DW_IDX_type_unit);
-                   m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
-                   m_abbrev_table.append_unsigned_leb128 (value.is_static
-                                                          ? DW_IDX_GNU_internal
-                                                          : DW_IDX_GNU_external);
-                   m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
-
-                   /* Terminate attributes list.  */
-                   m_abbrev_table.append_unsigned_leb128 (0);
-                   m_abbrev_table.append_unsigned_leb128 (0);
-                 }
-
-               m_entry_pool.append_unsigned_leb128 (idx);
-               m_entry_pool.append_unsigned_leb128 (value.cu_index);
-             }
-
-           /* Terminate the list of CUs.  */
-           m_entry_pool.append_unsigned_leb128 (0);
-         }
-      }
-    gdb_assert (m_hash_table.size () == name_count);
-
-    /* Terminate tags list.  */
-    m_abbrev_table.append_unsigned_leb128 (0);
-  }
-
-  /* Return .debug_names bucket count.  This must be called only after
-     calling the build method.  */
-  uint32_t bucket_count () const
-  {
-    /* Verify the build method has been already called.  */
-    gdb_assert (!m_abbrev_table.empty ());
-    const uint32_t retval = m_bucket_table.size ();
-
-    /* Check for overflow.  */
-    gdb_assert (retval == m_bucket_table.size ());
-    return retval;
-  }
-
-  /* Return .debug_names names count.  This must be called only after
-     calling the build method.  */
-  uint32_t name_count () const
-  {
-    /* Verify the build method has been already called.  */
-    gdb_assert (!m_abbrev_table.empty ());
-    const uint32_t retval = m_hash_table.size ();
-
-    /* Check for overflow.  */
-    gdb_assert (retval == m_hash_table.size ());
-    return retval;
-  }
-
-  /* Return number of bytes of .debug_names abbreviation table.  This
-     must be called only after calling the build method.  */
-  uint32_t abbrev_table_bytes () const
-  {
-    gdb_assert (!m_abbrev_table.empty ());
-    return m_abbrev_table.size ();
-  }
-
-  /* Recurse into all "included" dependencies and store their symbols
-     as if they appeared in this psymtab.  */
-  void recursively_write_psymbols
-    (struct objfile *objfile,
-     struct partial_symtab *psymtab,
-     std::unordered_set<partial_symbol *> &psyms_seen,
-     int cu_index)
-  {
-    for (int i = 0; i < psymtab->number_of_dependencies; ++i)
-      if (psymtab->dependencies[i]->user != NULL)
-       recursively_write_psymbols (objfile, psymtab->dependencies[i],
-                                   psyms_seen, cu_index);
-
-    write_psymbols (psyms_seen,
-                   &objfile->global_psymbols[psymtab->globals_offset],
-                   psymtab->n_global_syms, cu_index, false, unit_kind::cu);
-    write_psymbols (psyms_seen,
-                   &objfile->static_psymbols[psymtab->statics_offset],
-                   psymtab->n_static_syms, cu_index, true, unit_kind::cu);
-  }
-
-  /* Return number of bytes the .debug_names section will have.  This
-     must be called only after calling the build method.  */
-  size_t bytes () const
-  {
-    /* Verify the build method has been already called.  */
-    gdb_assert (!m_abbrev_table.empty ());
-    size_t expected_bytes = 0;
-    expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
-    expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
-    expected_bytes += m_name_table_string_offs.bytes ();
-    expected_bytes += m_name_table_entry_offs.bytes ();
-    expected_bytes += m_abbrev_table.size ();
-    expected_bytes += m_entry_pool.size ();
-    return expected_bytes;
-  }
-
-  /* Write .debug_names to FILE_NAMES and .debug_str addition to
-     FILE_STR.  This must be called only after calling the build
-     method.  */
-  void file_write (FILE *file_names, FILE *file_str) const
-  {
-    /* Verify the build method has been already called.  */
-    gdb_assert (!m_abbrev_table.empty ());
-    ::file_write (file_names, m_bucket_table);
-    ::file_write (file_names, m_hash_table);
-    m_name_table_string_offs.file_write (file_names);
-    m_name_table_entry_offs.file_write (file_names);
-    m_abbrev_table.file_write (file_names);
-    m_entry_pool.file_write (file_names);
-    m_debugstrlookup.file_write (file_str);
-  }
-
-  /* A helper user data for write_one_signatured_type.  */
-  class write_one_signatured_type_data
-  {
-  public:
-    write_one_signatured_type_data (debug_names &nametable_,
-                                    signatured_type_index_data &&info_)
-    : nametable (nametable_), info (std::move (info_))
-    {}
-    debug_names &nametable;
-    struct signatured_type_index_data info;
-  };
-
-  /* A helper function to pass write_one_signatured_type to
-     htab_traverse_noresize.  */
-  static int
-  write_one_signatured_type (void **slot, void *d)
-  {
-    write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
-    struct signatured_type_index_data *info = &data->info;
-    struct signatured_type *entry = (struct signatured_type *) *slot;
-
-    data->nametable.write_one_signatured_type (entry, info);
-
-    return 1;
-  }
-
-private:
-
-  /* Storage for symbol names mapping them to their .debug_str section
-     offsets.  */
-  class debug_str_lookup
-  {
-  public:
-
-    /* Object costructor to be called for current DWARF2_PER_OBJFILE.
-       All .debug_str section strings are automatically stored.  */
-    debug_str_lookup ()
-      : m_abfd (dwarf2_per_objfile->objfile->obfd)
-    {
-      dwarf2_read_section (dwarf2_per_objfile->objfile,
-                          &dwarf2_per_objfile->str);
-      if (dwarf2_per_objfile->str.buffer == NULL)
-       return;
-      for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
-          data < (dwarf2_per_objfile->str.buffer
-                  + dwarf2_per_objfile->str.size);)
-       {
-         const char *const s = reinterpret_cast<const char *> (data);
-         const auto insertpair
-           = m_str_table.emplace (c_str_view (s),
-                                  data - dwarf2_per_objfile->str.buffer);
-         if (!insertpair.second)
-           complaint (&symfile_complaints,
-                      _("Duplicate string \"%s\" in "
-                        ".debug_str section [in module %s]"),
-                      s, bfd_get_filename (m_abfd));
-         data += strlen (s) + 1;
-       }
-    }
-
-    /* Return offset of symbol name S in the .debug_str section.  Add
-       such symbol to the section's end if it does not exist there
-       yet.  */
-    size_t lookup (const char *s)
-    {
-      const auto it = m_str_table.find (c_str_view (s));
-      if (it != m_str_table.end ())
-       return it->second;
-      const size_t offset = (dwarf2_per_objfile->str.size
-                            + m_str_add_buf.size ());
-      m_str_table.emplace (c_str_view (s), offset);
-      m_str_add_buf.append_cstr0 (s);
-      return offset;
-    }
-
-    /* Append the end of the .debug_str section to FILE.  */
-    void file_write (FILE *file) const
-    {
-      m_str_add_buf.file_write (file);
-    }
-
-  private:
-    std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
-    bfd *const m_abfd;
-
-    /* Data to add at the end of .debug_str for new needed symbol names.  */
-    data_buf m_str_add_buf;
-  };
-
-  /* Container to map used DWARF tags to their .debug_names abbreviation
-     tags.  */
-  class index_key
-  {
-  public:
-    index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
-      : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
-    {
-    }
-
-    bool
-    operator== (const index_key &other) const
-    {
-      return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
-             && kind == other.kind);
-    }
-
-    const int dwarf_tag;
-    const bool is_static;
-    const unit_kind kind;
-  };
-
-  /* Provide std::unordered_map::hasher for index_key.  */
-  class index_key_hasher
-  {
-  public:
-    size_t
-    operator () (const index_key &key) const
-    {
-      return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
-    }
-  };
-
-  /* Parameters of one symbol entry.  */
-  class symbol_value
-  {
-  public:
-    const int dwarf_tag, cu_index;
-    const bool is_static;
-    const unit_kind kind;
-
-    symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
-                 unit_kind kind_)
-      : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
-        kind (kind_)
-    {}
-
-    bool
-    operator< (const symbol_value &other) const
-    {
-#define X(n) \
-  do \
-    { \
-      if (n < other.n) \
-       return true; \
-      if (n > other.n) \
-       return false; \
-    } \
-  while (0)
-      X (dwarf_tag);
-      X (is_static);
-      X (kind);
-      X (cu_index);
-#undef X
-      return false;
-    }
-  };
-
-  /* Abstract base class to unify DWARF-32 and DWARF-64 name table
-     output.  */
-  class offset_vec
-  {
-  protected:
-    const bfd_endian dwarf5_byte_order;
-  public:
-    explicit offset_vec (bfd_endian dwarf5_byte_order_)
-      : dwarf5_byte_order (dwarf5_byte_order_)
-    {}
-
-    /* Call std::vector::reserve for NELEM elements.  */
-    virtual void reserve (size_t nelem) = 0;
-
-    /* Call std::vector::push_back with store_unsigned_integer byte
-       reordering for ELEM.  */
-    virtual void push_back_reorder (size_t elem) = 0;
-
-    /* Return expected output size in bytes.  */
-    virtual size_t bytes () const = 0;
-
-    /* Write name table to FILE.  */
-    virtual void file_write (FILE *file) const = 0;
-  };
-
-  /* Template to unify DWARF-32 and DWARF-64 output.  */
-  template<typename OffsetSize>
-  class offset_vec_tmpl : public offset_vec
-  {
-  public:
-    explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
-      : offset_vec (dwarf5_byte_order_)
-    {}
-
-    /* Implement offset_vec::reserve.  */
-    void reserve (size_t nelem) override
-    {
-      m_vec.reserve (nelem);
-    }
-
-    /* Implement offset_vec::push_back_reorder.  */
-    void push_back_reorder (size_t elem) override
-    {
-      m_vec.push_back (elem);
-      /* Check for overflow.  */
-      gdb_assert (m_vec.back () == elem);
-      store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
-                             sizeof (m_vec.back ()), dwarf5_byte_order, elem);
-    }
-
-    /* Implement offset_vec::bytes.  */
-    size_t bytes () const override
-    {
-      return m_vec.size () * sizeof (m_vec[0]);
-    }
-
-    /* Implement offset_vec::file_write.  */
-    void file_write (FILE *file) const override
-    {
-      ::file_write (file, m_vec);
-    }
-
-  private:
-    std::vector<OffsetSize> m_vec;
-  };
-
-  /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
-     respecting name table width.  */
-  class dwarf
-  {
-  public:
-    offset_vec &name_table_string_offs, &name_table_entry_offs;
-
-    dwarf (offset_vec &name_table_string_offs_,
-          offset_vec &name_table_entry_offs_)
-      : name_table_string_offs (name_table_string_offs_),
-       name_table_entry_offs (name_table_entry_offs_)
-    {
-    }
-  };
-
-  /* Template to unify DWARF-32 and DWARF-64 .debug_names output
-     respecting name table width.  */
-  template<typename OffsetSize>
-  class dwarf_tmpl : public dwarf
-  {
-  public:
-    explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
-      : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
-       m_name_table_string_offs (dwarf5_byte_order_),
-       m_name_table_entry_offs (dwarf5_byte_order_)
-    {}
-
-  private:
-    offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
-    offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
-  };
-
-  /* Try to reconstruct original DWARF tag for given partial_symbol.
-     This function is not DWARF-5 compliant but it is sufficient for
-     GDB as a DWARF-5 index consumer.  */
-  static int psymbol_tag (const struct partial_symbol *psym)
-  {
-    domain_enum domain = PSYMBOL_DOMAIN (psym);
-    enum address_class aclass = PSYMBOL_CLASS (psym);
-
-    switch (domain)
-      {
-      case VAR_DOMAIN:
-       switch (aclass)
-         {
-         case LOC_BLOCK:
-           return DW_TAG_subprogram;
-         case LOC_TYPEDEF:
-           return DW_TAG_typedef;
-         case LOC_COMPUTED:
-         case LOC_CONST_BYTES:
-         case LOC_OPTIMIZED_OUT:
-         case LOC_STATIC:
-           return DW_TAG_variable;
-         case LOC_CONST:
-           /* Note: It's currently impossible to recognize psyms as enum values
-              short of reading the type info.  For now punt.  */
-           return DW_TAG_variable;
-         default:
-           /* There are other LOC_FOO values that one might want to classify
-              as variables, but dwarf2read.c doesn't currently use them.  */
-           return DW_TAG_variable;
-         }
-      case STRUCT_DOMAIN:
-       return DW_TAG_structure_type;
-      default:
-       return 0;
-      }
-  }
-
-  /* Call insert for all partial symbols and mark them in PSYMS_SEEN.  */
-  void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
-                      struct partial_symbol **psymp, int count, int cu_index,
-                      bool is_static, unit_kind kind)
-  {
-    for (; count-- > 0; ++psymp)
-      {
-       struct partial_symbol *psym = *psymp;
-
-       if (SYMBOL_LANGUAGE (psym) == language_ada)
-         error (_("Ada is not currently supported by the index"));
-
-       /* Only add a given psymbol once.  */
-       if (psyms_seen.insert (psym).second)
-         insert (psym, cu_index, is_static, kind);
-      }
-  }
-
-  /* A helper function that writes a single signatured_type
-     to a debug_names.  */
-  void
-  write_one_signatured_type (struct signatured_type *entry,
-                            struct signatured_type_index_data *info)
-  {
-    struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
-
-    write_psymbols (info->psyms_seen,
-                   &info->objfile->global_psymbols[psymtab->globals_offset],
-                   psymtab->n_global_syms, info->cu_index, false,
-                   unit_kind::tu);
-    write_psymbols (info->psyms_seen,
-                   &info->objfile->static_psymbols[psymtab->statics_offset],
-                   psymtab->n_static_syms, info->cu_index, true,
-                   unit_kind::tu);
-
-    info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
-                                 to_underlying (entry->per_cu.sect_off));
-
-    ++info->cu_index;
-  }
-
-  /* Store value of each symbol.  */
-  std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
-    m_name_to_value_set;
-
-  /* Tables of DWARF-5 .debug_names.  They are in object file byte
-     order.  */
-  std::vector<uint32_t> m_bucket_table;
-  std::vector<uint32_t> m_hash_table;
-
-  const bfd_endian m_dwarf5_byte_order;
-  dwarf_tmpl<uint32_t> m_dwarf32;
-  dwarf_tmpl<uint64_t> m_dwarf64;
-  dwarf &m_dwarf;
-  offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
-  debug_str_lookup m_debugstrlookup;
-
-  /* Map each used .debug_names abbreviation tag parameter to its
-     index value.  */
-  std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
-
-  /* Next unused .debug_names abbreviation tag for
-     m_indexkey_to_idx.  */
-  int m_idx_next = 1;
-
-  /* .debug_names abbreviation table.  */
-  data_buf m_abbrev_table;
-
-  /* .debug_names entry pool.  */
-  data_buf m_entry_pool;
-};
-
-/* Return iff any of the needed offsets does not fit into 32-bit
-   .debug_names section.  */
-
-static bool
-check_dwarf64_offsets ()
-{
-  for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
-    {
-      const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
-
-      if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
-       return true;
-    }
-  for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
-    {
-      const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
-      const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
-
-      if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
-       return true;
-    }
-  return false;
-}
-
-/* The psyms_seen set is potentially going to be largish (~40k
-   elements when indexing a -g3 build of GDB itself).  Estimate the
-   number of elements in order to avoid too many rehashes, which
-   require rebuilding buckets and thus many trips to
-   malloc/free.  */
-
-static size_t
-psyms_seen_size ()
-{
-  size_t psyms_count = 0;
-  for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
-    {
-      struct dwarf2_per_cu_data *per_cu
-       = dwarf2_per_objfile->all_comp_units[i];
-      struct partial_symtab *psymtab = per_cu->v.psymtab;
-
-      if (psymtab != NULL && psymtab->user == NULL)
-       recursively_count_psymbols (psymtab, psyms_count);
-    }
-  /* Generating an index for gdb itself shows a ratio of
-     TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5.  4 seems like a good bet.  */
-  return psyms_count / 4;
-}
-
-/* Write new .gdb_index section for OBJFILE into OUT_FILE.
-   Return how many bytes were expected to be written into OUT_FILE.  */
-
-static size_t
-write_gdbindex (struct objfile *objfile, FILE *out_file)
-{
-  mapped_symtab symtab;
-  data_buf cu_list;
-
-  /* While we're scanning CU's create a table that maps a psymtab pointer
-     (which is what addrmap records) to its index (which is what is recorded
-     in the index file).  This will later be needed to write the address
-     table.  */
-  psym_index_map cu_index_htab;
-  cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
-
-  /* The CU list is already sorted, so we don't need to do additional
-     work here.  Also, the debug_types entries do not appear in
-     all_comp_units, but only in their own hash table.  */
-
-  std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
-  for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
-    {
-      struct dwarf2_per_cu_data *per_cu
-       = dwarf2_per_objfile->all_comp_units[i];
-      struct partial_symtab *psymtab = per_cu->v.psymtab;
-
-      /* CU of a shared file from 'dwz -m' may be unused by this main file.
-        It may be referenced from a local scope but in such case it does not
-        need to be present in .gdb_index.  */
-      if (psymtab == NULL)
-       continue;
-
-      if (psymtab->user == NULL)
-       recursively_write_psymbols (objfile, psymtab, &symtab,
-                                   psyms_seen, i);
-
-      const auto insertpair = cu_index_htab.emplace (psymtab, i);
-      gdb_assert (insertpair.second);
-
-      cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
-                          to_underlying (per_cu->sect_off));
-      cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
-    }
-
-  /* Dump the address map.  */
-  data_buf addr_vec;
-  write_address_map (objfile, addr_vec, cu_index_htab);
-
-  /* Write out the .debug_type entries, if any.  */
-  data_buf types_cu_list;
-  if (dwarf2_per_objfile->signatured_types)
-    {
-      signatured_type_index_data sig_data (types_cu_list,
-                                          psyms_seen);
-
-      sig_data.objfile = objfile;
-      sig_data.symtab = &symtab;
-      sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
-      htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
-                             write_one_signatured_type, &sig_data);
-    }
-
-  /* Now that we've processed all symbols we can shrink their cu_indices
-     lists.  */
-  uniquify_cu_indices (&symtab);
-
-  data_buf symtab_vec, constant_pool;
-  write_hash_table (&symtab, symtab_vec, constant_pool);
-
-  data_buf contents;
-  const offset_type size_of_contents = 6 * sizeof (offset_type);
-  offset_type total_len = size_of_contents;
-
-  /* The version number.  */
-  contents.append_data (MAYBE_SWAP (8));
-
-  /* The offset of the CU list from the start of the file.  */
-  contents.append_data (MAYBE_SWAP (total_len));
-  total_len += cu_list.size ();
-
-  /* The offset of the types CU list from the start of the file.  */
-  contents.append_data (MAYBE_SWAP (total_len));
-  total_len += types_cu_list.size ();
-
-  /* The offset of the address table from the start of the file.  */
-  contents.append_data (MAYBE_SWAP (total_len));
-  total_len += addr_vec.size ();
-
-  /* The offset of the symbol table from the start of the file.  */
-  contents.append_data (MAYBE_SWAP (total_len));
-  total_len += symtab_vec.size ();
-
-  /* The offset of the constant pool from the start of the file.  */
-  contents.append_data (MAYBE_SWAP (total_len));
-  total_len += constant_pool.size ();
-
-  gdb_assert (contents.size () == size_of_contents);
-
-  contents.file_write (out_file);
-  cu_list.file_write (out_file);
-  types_cu_list.file_write (out_file);
-  addr_vec.file_write (out_file);
-  symtab_vec.file_write (out_file);
-  constant_pool.file_write (out_file);
-
-  return total_len;
-}
-
-/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension.  */
-static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
-
-/* Write a new .debug_names section for OBJFILE into OUT_FILE, write
-   needed addition to .debug_str section to OUT_FILE_STR.  Return how
-   many bytes were expected to be written into OUT_FILE.  */
-
-static size_t
-write_debug_names (struct objfile *objfile, FILE *out_file, FILE *out_file_str)
-{
-  const bool dwarf5_is_dwarf64 = check_dwarf64_offsets ();
-  const enum bfd_endian dwarf5_byte_order
-    = gdbarch_byte_order (get_objfile_arch (objfile));
-
-  /* The CU list is already sorted, so we don't need to do additional
-     work here.  Also, the debug_types entries do not appear in
-     all_comp_units, but only in their own hash table.  */
-  data_buf cu_list;
-  debug_names nametable (dwarf5_is_dwarf64, dwarf5_byte_order);
-  std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
-  for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
-    {
-      const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
-      partial_symtab *psymtab = per_cu->v.psymtab;
-
-      /* CU of a shared file from 'dwz -m' may be unused by this main
-        file.  It may be referenced from a local scope but in such
-        case it does not need to be present in .debug_names.  */
-      if (psymtab == NULL)
-       continue;
-
-      if (psymtab->user == NULL)
-       nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
-
-      cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
-                          to_underlying (per_cu->sect_off));
-    }
-
-  /* Write out the .debug_type entries, if any.  */
-  data_buf types_cu_list;
-  if (dwarf2_per_objfile->signatured_types)
-    {
-      debug_names::write_one_signatured_type_data sig_data (nametable,
-                       signatured_type_index_data (types_cu_list, psyms_seen));
-
-      sig_data.info.objfile = objfile;
-      /* It is used only for gdb_index.  */
-      sig_data.info.symtab = nullptr;
-      sig_data.info.cu_index = 0;
-      htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
-                             debug_names::write_one_signatured_type,
-                             &sig_data);
-    }
-
-  nametable.build ();
-
-  /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC.  */
-
-  const offset_type bytes_of_header
-    = ((dwarf5_is_dwarf64 ? 12 : 4)
-       + 2 + 2 + 7 * 4
-       + sizeof (dwarf5_gdb_augmentation));
-  size_t expected_bytes = 0;
-  expected_bytes += bytes_of_header;
-  expected_bytes += cu_list.size ();
-  expected_bytes += types_cu_list.size ();
-  expected_bytes += nametable.bytes ();
-  data_buf header;
-
-  if (!dwarf5_is_dwarf64)
-    {
-      const uint64_t size64 = expected_bytes - 4;
-      gdb_assert (size64 < 0xfffffff0);
-      header.append_uint (4, dwarf5_byte_order, size64);
-    }
-  else
-    {
-      header.append_uint (4, dwarf5_byte_order, 0xffffffff);
-      header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
-    }
-
-  /* The version number.  */
-  header.append_uint (2, dwarf5_byte_order, 5);
-
-  /* Padding.  */
-  header.append_uint (2, dwarf5_byte_order, 0);
-
-  /* comp_unit_count - The number of CUs in the CU list.  */
-  header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
-
-  /* local_type_unit_count - The number of TUs in the local TU
-     list.  */
-  header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
-
-  /* foreign_type_unit_count - The number of TUs in the foreign TU
-     list.  */
-  header.append_uint (4, dwarf5_byte_order, 0);
-
-  /* bucket_count - The number of hash buckets in the hash lookup
-     table.  */
-  header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
-
-  /* name_count - The number of unique names in the index.  */
-  header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
-
-  /* abbrev_table_size - The size in bytes of the abbreviations
-     table.  */
-  header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
-
-  /* augmentation_string_size - The size in bytes of the augmentation
-     string.  This value is rounded up to a multiple of 4.  */
-  static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
-  header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
-  header.append_data (dwarf5_gdb_augmentation);
-
-  gdb_assert (header.size () == bytes_of_header);
-
-  header.file_write (out_file);
-  cu_list.file_write (out_file);
-  types_cu_list.file_write (out_file);
-  nametable.file_write (out_file, out_file_str);
-
-  return expected_bytes;
-}
-
-/* Assert that FILE's size is EXPECTED_SIZE.  Assumes file's seek
-   position is at the end of the file.  */
-
-static void
-assert_file_size (FILE *file, const char *filename, size_t expected_size)
-{
-  const auto file_size = ftell (file);
-  if (file_size == -1)
-    error (_("Can't get `%s' size"), filename);
-  gdb_assert (file_size == expected_size);
-}
-
-/* Create an index file for OBJFILE in the directory DIR.  */
-
-static void
-write_psymtabs_to_index (struct objfile *objfile, const char *dir,
-                        dw_index_kind index_kind)
-{
-  if (dwarf2_per_objfile->using_index)
-    error (_("Cannot use an index to create the index"));
-
-  if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
-    error (_("Cannot make an index when the file has multiple .debug_types sections"));
-
-  if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
-    return;
-
-  struct stat st;
-  if (stat (objfile_name (objfile), &st) < 0)
-    perror_with_name (objfile_name (objfile));
-
-  std::string filename (std::string (dir) + SLASH_STRING
-                       + lbasename (objfile_name (objfile))
-                       + (index_kind == dw_index_kind::DEBUG_NAMES
-                          ? INDEX5_SUFFIX : INDEX4_SUFFIX));
-
-  FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
-  if (!out_file)
-    error (_("Can't open `%s' for writing"), filename.c_str ());
-
-  /* Order matters here; we want FILE to be closed before FILENAME is
-     unlinked, because on MS-Windows one cannot delete a file that is
-     still open.  (Don't call anything here that might throw until
-     file_closer is created.)  */
-  gdb::unlinker unlink_file (filename.c_str ());
-  gdb_file_up close_out_file (out_file);
-
-  if (index_kind == dw_index_kind::DEBUG_NAMES)
-    {
-      std::string filename_str (std::string (dir) + SLASH_STRING
-                               + lbasename (objfile_name (objfile))
-                               + DEBUG_STR_SUFFIX);
-      FILE *out_file_str
-       = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
-      if (!out_file_str)
-       error (_("Can't open `%s' for writing"), filename_str.c_str ());
-      gdb::unlinker unlink_file_str (filename_str.c_str ());
-      gdb_file_up close_out_file_str (out_file_str);
-
-      const size_t total_len
-       = write_debug_names (objfile, out_file, out_file_str);
-      assert_file_size (out_file, filename.c_str (), total_len);
-
-      /* We want to keep the file .debug_str file too.  */
-      unlink_file_str.keep ();
-    }
-  else
-    {
-      const size_t total_len
-       = write_gdbindex (objfile, out_file);
-      assert_file_size (out_file, filename.c_str (), total_len);
-    }
-
-  /* We want to keep the file.  */
-  unlink_file.keep ();
-}
-
-/* Implementation of the `save gdb-index' command.
-   
-   Note that the .gdb_index file format used by this command is
-   documented in the GDB manual.  Any changes here must be documented
-   there.  */
-
-static void
-save_gdb_index_command (const char *arg, int from_tty)
-{
-  struct objfile *objfile;
-  const char dwarf5space[] = "-dwarf-5 ";
-  dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
-
-  if (!arg)
-    arg = "";
-
-  arg = skip_spaces (arg);
-  if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
-    {
-      index_kind = dw_index_kind::DEBUG_NAMES;
-      arg += strlen (dwarf5space);
-      arg = skip_spaces (arg);
-    }
-
-  if (!*arg)
-    error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
-
-  ALL_OBJFILES (objfile)
-  {
-    struct stat st;
-
-    /* If the objfile does not correspond to an actual file, skip it.  */
-    if (stat (objfile_name (objfile), &st) < 0)
-      continue;
-
-    dwarf2_per_objfile
-      = (struct dwarf2_per_objfile *) objfile_data (objfile,
-                                                   dwarf2_objfile_data_key);
-    if (dwarf2_per_objfile)
-      {
-
-       TRY
-         {
-           write_psymtabs_to_index (objfile, arg, index_kind);
-         }
-       CATCH (except, RETURN_MASK_ERROR)
-         {
-           exception_fprintf (gdb_stderr, except,
-                              _("Error while writing index for `%s': "),
-                              objfile_name (objfile));
-         }
-       END_CATCH
-      }
-  }
-}
-
-\f
-
-int dwarf_always_disassemble;
-
-static void
-show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
-                              struct cmd_list_element *c, const char *value)
-{
-  fprintf_filtered (file,
-                   _("Whether to always disassemble "
-                     "DWARF expressions is %s.\n"),
-                   value);
-}
-
-static void
-show_check_physname (struct ui_file *file, int from_tty,
-                    struct cmd_list_element *c, const char *value)
-{
-  fprintf_filtered (file,
-                   _("Whether to check \"physname\" is %s.\n"),
-                   value);
-}
-
-void
-_initialize_dwarf2_read (void)
-{
-  struct cmd_list_element *c;
-
-  dwarf2_objfile_data_key
-    = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
-
-  add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
-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);
-
-  add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
-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);
-
-  add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
-                           &dwarf_max_cache_age, _("\
-Set the upper bound on the age of cached DWARF compilation units."), _("\
-Show the upper bound on the age of cached DWARF compilation units."), _("\
-A higher limit means that cached compilation units will be stored\n\
-in memory longer, and more total memory will be used.  Zero disables\n\
-caching, which can slow down startup."),
-                           NULL,
-                           show_dwarf_max_cache_age,
-                           &set_dwarf_cmdlist,
-                           &show_dwarf_cmdlist);
-
-  add_setshow_boolean_cmd ("always-disassemble", class_obscure,
-                          &dwarf_always_disassemble, _("\
-Set whether `info address' always disassembles DWARF expressions."), _("\
-Show whether `info address' always disassembles DWARF expressions."), _("\
-When enabled, DWARF expressions are always printed in an assembly-like\n\
-syntax.  When disabled, expressions will be printed in a more\n\
-conversational style, when possible."),
-                          NULL,
-                          show_dwarf_always_disassemble,
-                          &set_dwarf_cmdlist,
-                          &show_dwarf_cmdlist);
-
-  add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
-Set debugging of the DWARF reader."), _("\
-Show debugging of the DWARF reader."), _("\
-When enabled (non-zero), debugging messages are printed during DWARF\n\
-reading and symtab expansion.  A value of 1 (one) provides basic\n\
-information.  A value greater than 1 provides more verbose information."),
-                           NULL,
-                           NULL,
-                           &setdebuglist, &showdebuglist);
-
-  add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
-Set debugging of the DWARF DIE reader."), _("\
-Show debugging of the DWARF DIE reader."), _("\
-When enabled (non-zero), DIEs are dumped after they are read in.\n\
-The value is the maximum depth to print."),
-                            NULL,
-                            NULL,
-                            &setdebuglist, &showdebuglist);
-
-  add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
-Set debugging of the dwarf line reader."), _("\
-Show debugging of the dwarf line reader."), _("\
-When enabled (non-zero), line number entries are dumped as they are read in.\n\
-A value of 1 (one) provides basic information.\n\
-A value greater than 1 provides more verbose information."),
-                            NULL,
-                            NULL,
-                            &setdebuglist, &showdebuglist);
-
-  add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
-Set cross-checking of \"physname\" code against demangler."), _("\
-Show cross-checking of \"physname\" code against demangler."), _("\
-When enabled, GDB's internal \"physname\" code is checked against\n\
-the demangler."),
-                          NULL, show_check_physname,
-                          &setdebuglist, &showdebuglist);
+  add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
+Set cross-checking of \"physname\" code against demangler."), _("\
+Show cross-checking of \"physname\" code against demangler."), _("\
+When enabled, GDB's internal \"physname\" code is checked against\n\
+the demangler."),
+                          NULL, show_check_physname,
+                          &setdebuglist, &showdebuglist);
 
   add_setshow_boolean_cmd ("use-deprecated-index-sections",
                           no_class, &use_deprecated_index_sections, _("\
@@ -27169,17 +25419,6 @@ Warning: This option must be enabled before gdb reads the file."),
                           NULL,
                           &setlist, &showlist);
 
-  c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
-              _("\
-Save a gdb-index file.\n\
-Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
-\n\
-No options create one file with .gdb-index extension for pre-DWARF-5\n\
-compatible .gdb_index section.  With -dwarf-5 creates two files with\n\
-extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
-              &save_cmdlist);
-  set_cmd_completer (c, filename_completer);
-
   dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
                                                        &dwarf2_locexpr_funcs);
   dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
This page took 0.182815 seconds and 4 git commands to generate.