]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Allocate dwarf2_per_cu_data with 'new'
authorTom Tromey <tom@tromey.com>
Fri, 30 Apr 2021 20:07:58 +0000 (14:07 -0600)
committerTom Tromey <tom@tromey.com>
Fri, 30 Apr 2021 20:07:58 +0000 (14:07 -0600)
In a patch series I am working on, I'd like to have a non-POD member
in dwarf2_per_cu_data.  This currently can't be done because
dwarf2_per_cu_data is allocated on an obstack and initialized with
memset.

This patch changes the DWARF reader to allocate objects of this type
with 'new'.  The various "subclasses" of this type (signatured_type in
particular) are now changed to derive from dwarf2_per_cu_data, and
also use 'new' for allocation.

Regression tested on x86-64 Fedora 32.

gdb/ChangeLog
2021-04-30  Tom Tromey  <tom@tromey.com>

* dwarf2/read.h (struct dwarf2_per_bfd) <allocate_per_cu,
allocate_signatured_type>: Change return type.
<all_comp_units, all_type_units>: Hold unique pointers.
(struct dwarf2_per_cu_data): Add constructor and initializers.
(struct signatured_type): Derive from dwarf2_per_cu_data.
* dwarf2/read.c (type_unit_group): Derive from
dwarf2_per_cu_data.
(dwarf2_per_bfd::get_cutu, dwarf2_per_bfd::get_cu)
(dwarf2_per_bfd::get_tu)
(dwarf2_per_bfd::allocate_signatured_type)
(dwarf2_per_bfd::allocate_signatured_type)
(create_cu_from_index_list, create_cus_from_index_list)
(create_signatured_type_table_from_index)
(create_signatured_type_table_from_debug_names)
(create_addrmap_from_aranges)
(dwarf2_base_index_functions::find_last_source_symtab)
(dw_expand_symtabs_matching_file_matcher)
(dwarf2_gdb_index::expand_symtabs_matching)
(dwarf2_base_index_functions::map_symbol_filenames)
(create_cus_from_debug_names_list)
(dw2_debug_names_iterator::next)
(dwarf2_debug_names_index::expand_symtabs_matching)
(create_debug_type_hash_table, add_type_unit)
(fill_in_sig_entry_from_dwo_entry, lookup_dwo_signatured_type):
Update.
(allocate_type_unit_groups_table): Use delete.
(create_type_unit_group): Change return type.  Use new.
(get_type_unit_group, build_type_psymtabs_1)
(build_type_psymtab_dependencies)
(process_skeletonless_type_unit, set_partial_user)
(dwarf2_build_psymtabs_hard, read_comp_units_from_section)
(create_cus_hash_table, queue_and_load_dwo_tu, follow_die_sig_1)
(read_signatured_type): Update.
(dwarf2_find_containing_comp_unit): Change type of
'all_comp_units'.
(run_test): Update.
(dwarf2_per_bfd::allocate_per_cu)
(dwarf2_per_bfd::allocate_signatured_type): Change return type.
Use new.
(add_signatured_type_cu_to_table): Update.
* dwarf2/index-write.c (write_one_signatured_type)
(check_dwarf64_offsets, psyms_seen_size, write_gdbindex)
(write_debug_names): Update.

gdb/ChangeLog
gdb/dwarf2/index-write.c
gdb/dwarf2/read.c
gdb/dwarf2/read.h

index 16c0ef746d994d3e888fc4eb8ac20881770b23f9..e51ef3dcbaceec0a4d18a336b83f393c3a95582e 100644 (file)
@@ -1,3 +1,49 @@
+2021-04-30  Tom Tromey  <tom@tromey.com>
+
+       * dwarf2/read.h (struct dwarf2_per_bfd) <allocate_per_cu,
+       allocate_signatured_type>: Change return type.
+       <all_comp_units, all_type_units>: Hold unique pointers.
+       (struct dwarf2_per_cu_data): Add constructor and initializers.
+       (struct signatured_type): Derive from dwarf2_per_cu_data.
+       * dwarf2/read.c (type_unit_group): Derive from
+       dwarf2_per_cu_data.
+       (dwarf2_per_bfd::get_cutu, dwarf2_per_bfd::get_cu)
+       (dwarf2_per_bfd::get_tu)
+       (dwarf2_per_bfd::allocate_signatured_type)
+       (dwarf2_per_bfd::allocate_signatured_type)
+       (create_cu_from_index_list, create_cus_from_index_list)
+       (create_signatured_type_table_from_index)
+       (create_signatured_type_table_from_debug_names)
+       (create_addrmap_from_aranges)
+       (dwarf2_base_index_functions::find_last_source_symtab)
+       (dw_expand_symtabs_matching_file_matcher)
+       (dwarf2_gdb_index::expand_symtabs_matching)
+       (dwarf2_base_index_functions::map_symbol_filenames)
+       (create_cus_from_debug_names_list)
+       (dw2_debug_names_iterator::next)
+       (dwarf2_debug_names_index::expand_symtabs_matching)
+       (create_debug_type_hash_table, add_type_unit)
+       (fill_in_sig_entry_from_dwo_entry, lookup_dwo_signatured_type):
+       Update.
+       (allocate_type_unit_groups_table): Use delete.
+       (create_type_unit_group): Change return type.  Use new.
+       (get_type_unit_group, build_type_psymtabs_1)
+       (build_type_psymtab_dependencies)
+       (process_skeletonless_type_unit, set_partial_user)
+       (dwarf2_build_psymtabs_hard, read_comp_units_from_section)
+       (create_cus_hash_table, queue_and_load_dwo_tu, follow_die_sig_1)
+       (read_signatured_type): Update.
+       (dwarf2_find_containing_comp_unit): Change type of
+       'all_comp_units'.
+       (run_test): Update.
+       (dwarf2_per_bfd::allocate_per_cu)
+       (dwarf2_per_bfd::allocate_signatured_type): Change return type.
+       Use new.
+       (add_signatured_type_cu_to_table): Update.
+       * dwarf2/index-write.c (write_one_signatured_type)
+       (check_dwarf64_offsets, psyms_seen_size, write_gdbindex)
+       (write_debug_names): Update.
+
 2021-04-30  Tom Tromey  <tromey@adacore.com>
 
        * nat/windows-nat.h (get_image_name): Don't declare.
index e27e1e8cf7dd31335d4fa36ec4ac5131d6495ac4..b000cd5b8fddc34a9684a328f2a7424e39d89c74 100644 (file)
@@ -615,7 +615,7 @@ 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;
-  partial_symtab *psymtab = entry->per_cu.v.psymtab;
+  partial_symtab *psymtab = entry->v.psymtab;
 
   if (psymtab == nullptr)
     {
@@ -633,7 +633,7 @@ write_one_signatured_type (void **slot, void *d)
                  1);
 
   info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
-                               to_underlying (entry->per_cu.sect_off));
+                               to_underlying (entry->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);
@@ -1259,7 +1259,7 @@ private:
   write_one_signatured_type (struct signatured_type *entry,
                             struct signatured_type_index_data *info)
   {
-    partial_symtab *psymtab = entry->per_cu.v.psymtab;
+    partial_symtab *psymtab = entry->v.psymtab;
 
     write_psymbols (info->psyms_seen, psymtab->global_psymbols,
                    info->cu_index, false, unit_kind::tu);
@@ -1267,7 +1267,7 @@ private:
                    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));
+                                 to_underlying (entry->sect_off));
 
     ++info->cu_index;
   }
@@ -1312,16 +1312,16 @@ private:
 static bool
 check_dwarf64_offsets (dwarf2_per_objfile *per_objfile)
 {
-  for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
+  for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
     {
-      if (to_underlying (per_cu->sect_off) >= (static_cast<uint64_t> (1) << 32))
+      if (to_underlying (per_cu->sect_off)
+         >= (static_cast<uint64_t> (1) << 32))
        return true;
     }
-  for (const signatured_type *sigtype : per_objfile->per_bfd->all_type_units)
+  for (const auto &sigtype : per_objfile->per_bfd->all_type_units)
     {
-      const dwarf2_per_cu_data &per_cu = sigtype->per_cu;
-
-      if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
+      if (to_underlying (sigtype->sect_off)
+         >= (static_cast<uint64_t> (1) << 32))
        return true;
     }
   return false;
@@ -1337,7 +1337,7 @@ static size_t
 psyms_seen_size (dwarf2_per_objfile *per_objfile)
 {
   size_t psyms_count = 0;
-  for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
+  for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
     {
       partial_symtab *psymtab = per_cu->v.psymtab;
 
@@ -1440,7 +1440,8 @@ write_gdbindex (dwarf2_per_objfile *per_objfile, FILE *out_file,
     (psyms_seen_size (per_objfile));
   for (int i = 0; i < per_objfile->per_bfd->all_comp_units.size (); ++i)
     {
-      dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->all_comp_units[i];
+      dwarf2_per_cu_data *per_cu
+       = per_objfile->per_bfd->all_comp_units[i].get ();
       partial_symtab *psymtab = per_cu->v.psymtab;
 
       if (psymtab != NULL)
@@ -1521,7 +1522,8 @@ write_debug_names (dwarf2_per_objfile *per_objfile,
     psyms_seen (psyms_seen_size (per_objfile));
   for (int i = 0; i < per_objfile->per_bfd->all_comp_units.size (); ++i)
     {
-      const dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->all_comp_units[i];
+      const dwarf2_per_cu_data *per_cu
+       = per_objfile->per_bfd->all_comp_units[i].get ();
       partial_symtab *psymtab = per_cu->v.psymtab;
 
       /* CU of a shared file from 'dwz -m' may be unused by this main
index 306971ef1b75aea0fc965d890a1de742183ec8d1..6eb6caf10576f3ee347702b82bfeb11fd928162a 100644 (file)
@@ -720,21 +720,15 @@ struct stmt_list_hash
    that can be shared across objfiles.  The non-shareable parts are in
    type_unit_group_unshareable.  */
 
-struct type_unit_group
+struct type_unit_group : public dwarf2_per_cu_data
 {
-  /* dwarf2read.c's main "handle" on a TU symtab.
-     To simplify things we create an artificial CU that "includes" all the
-     type units using this stmt_list so that the rest of the code still has
-     a "per_cu" handle on the symtab.  */
-  struct dwarf2_per_cu_data per_cu;
-
   /* The TUs that share this DW_AT_stmt_list entry.
      This is added to while parsing type units to build partial symtabs,
      and is deleted afterwards and not used again.  */
-  std::vector<signatured_type *> *tus;
+  std::vector<signatured_type *> *tus = nullptr;
 
   /* The data used to construct the hash key.  */
-  struct stmt_list_hash hash;
+  struct stmt_list_hash hash {};
 };
 
 /* These sections are what may appear in a (real or virtual) DWO file.  */
@@ -1903,11 +1897,11 @@ dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
 
 dwarf2_per_bfd::~dwarf2_per_bfd ()
 {
-  for (dwarf2_per_cu_data *per_cu : all_comp_units)
+  for (auto &per_cu : all_comp_units)
     per_cu->imported_symtabs_free ();
 
-  for (signatured_type *sig_type : all_type_units)
-    sig_type->per_cu.imported_symtabs_free ();
+  for (auto &sig_type : all_type_units)
+    sig_type->imported_symtabs_free ();
 
   /* Everything else should be on this->obstack.  */
 }
@@ -2540,10 +2534,10 @@ dwarf2_per_bfd::get_cutu (int index)
     {
       index -= this->all_comp_units.size ();
       gdb_assert (index < this->all_type_units.size ());
-      return &this->all_type_units[index]->per_cu;
+      return this->all_type_units[index].get ();
     }
 
-  return this->all_comp_units[index];
+  return this->all_comp_units[index].get ();
 }
 
 /* See declaration.  */
@@ -2553,7 +2547,7 @@ dwarf2_per_bfd::get_cu (int index)
 {
   gdb_assert (index >= 0 && index < this->all_comp_units.size ());
 
-  return this->all_comp_units[index];
+  return this->all_comp_units[index].get ();
 }
 
 /* See declaration.  */
@@ -2563,15 +2557,15 @@ dwarf2_per_bfd::get_tu (int index)
 {
   gdb_assert (index >= 0 && index < this->all_type_units.size ());
 
-  return this->all_type_units[index];
+  return this->all_type_units[index].get ();
 }
 
 /* See read.h.  */
 
-dwarf2_per_cu_data *
+std::unique_ptr<dwarf2_per_cu_data>
 dwarf2_per_bfd::allocate_per_cu ()
 {
-  dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
+  std::unique_ptr<dwarf2_per_cu_data> result (new dwarf2_per_cu_data);
   result->per_bfd = this;
   result->index = m_num_psymtabs++;
   return result;
@@ -2579,25 +2573,25 @@ dwarf2_per_bfd::allocate_per_cu ()
 
 /* See read.h.  */
 
-signatured_type *
+std::unique_ptr<signatured_type>
 dwarf2_per_bfd::allocate_signatured_type ()
 {
-  signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
-  result->per_cu.per_bfd = this;
-  result->per_cu.index = m_num_psymtabs++;
+  std::unique_ptr<signatured_type> result (new signatured_type);
+  result->per_bfd = this;
+  result->index = m_num_psymtabs++;
   return result;
 }
 
 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
    obstack, and constructed with the specified field values.  */
 
-static dwarf2_per_cu_data *
+static std::unique_ptr<dwarf2_per_cu_data>
 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
                           struct dwarf2_section_info *section,
                           int is_dwz,
                           sect_offset sect_off, ULONGEST length)
 {
-  dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
+  std::unique_ptr<dwarf2_per_cu_data> the_cu = per_bfd->allocate_per_cu ();
   the_cu->sect_off = sect_off;
   the_cu->length = length;
   the_cu->section = section;
@@ -2625,10 +2619,10 @@ create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
       ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
       cu_list += 2 * 8;
 
-      dwarf2_per_cu_data *per_cu
+      std::unique_ptr<dwarf2_per_cu_data> per_cu
        = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
                                     length);
-      per_bfd->all_comp_units.push_back (per_cu);
+      per_bfd->all_comp_units.push_back (std::move (per_cu));
     }
 }
 
@@ -2668,7 +2662,7 @@ create_signatured_type_table_from_index
 
   for (offset_type i = 0; i < elements; i += 3)
     {
-      struct signatured_type *sig_type;
+      std::unique_ptr<signatured_type> sig_type;
       ULONGEST signature;
       void **slot;
       cu_offset type_offset_in_tu;
@@ -2685,17 +2679,17 @@ create_signatured_type_table_from_index
       sig_type = per_bfd->allocate_signatured_type ();
       sig_type->signature = signature;
       sig_type->type_offset_in_tu = type_offset_in_tu;
-      sig_type->per_cu.is_debug_types = 1;
-      sig_type->per_cu.section = section;
-      sig_type->per_cu.sect_off = sect_off;
-      sig_type->per_cu.v.quick
+      sig_type->is_debug_types = 1;
+      sig_type->section = section;
+      sig_type->sect_off = sect_off;
+      sig_type->v.quick
        = OBSTACK_ZALLOC (&per_bfd->obstack,
                          struct dwarf2_per_cu_quick_data);
 
-      slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
-      *slot = sig_type;
+      slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
+      *slot = sig_type.get ();
 
-      per_bfd->all_type_units.push_back (sig_type);
+      per_bfd->all_type_units.push_back (std::move (sig_type));
     }
 
   per_bfd->signatured_types = std::move (sig_types_hash);
@@ -2722,7 +2716,7 @@ create_signatured_type_table_from_debug_names
 
   for (uint32_t i = 0; i < map.tu_count; ++i)
     {
-      struct signatured_type *sig_type;
+      std::unique_ptr<signatured_type> sig_type;
       void **slot;
 
       sect_offset sect_off
@@ -2740,17 +2734,17 @@ create_signatured_type_table_from_debug_names
       sig_type = per_objfile->per_bfd->allocate_signatured_type ();
       sig_type->signature = cu_header.signature;
       sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
-      sig_type->per_cu.is_debug_types = 1;
-      sig_type->per_cu.section = section;
-      sig_type->per_cu.sect_off = sect_off;
-      sig_type->per_cu.v.quick
+      sig_type->is_debug_types = 1;
+      sig_type->section = section;
+      sig_type->sect_off = sect_off;
+      sig_type->v.quick
        = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
                          struct dwarf2_per_cu_quick_data);
 
-      slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
-      *slot = sig_type;
+      slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
+      *slot = sig_type.get ();
 
-      per_objfile->per_bfd->all_type_units.push_back (sig_type);
+      per_objfile->per_bfd->all_type_units.push_back (std::move (sig_type));
     }
 
   per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
@@ -2833,10 +2827,11 @@ create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
                     dwarf2_per_cu_data *,
                     gdb::hash_enum<sect_offset>>
     debug_info_offset_to_per_cu;
-  for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
+  for (const auto &per_cu : per_bfd->all_comp_units)
     {
       const auto insertpair
-       = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
+       = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
+                                              per_cu.get ());
       if (!insertpair.second)
        {
          warning (_("Section .debug_aranges in %s has duplicate "
@@ -3310,7 +3305,8 @@ struct symtab *
 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
 {
   dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
-  dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
+  dwarf2_per_cu_data *dwarf_cu
+    = per_objfile->per_bfd->all_comp_units.back ().get ();
   compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
 
   if (cust == NULL)
@@ -4541,17 +4537,18 @@ 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 (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
+  for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
     {
       QUIT;
 
       per_cu->v.quick->mark = 0;
 
       /* We only need to look at symtabs not already expanded.  */
-      if (per_objfile->symtab_set_p (per_cu))
+      if (per_objfile->symtab_set_p (per_cu.get ()))
        continue;
 
-      quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
+      quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
+                                                       per_objfile);
       if (file_data == NULL)
        continue;
 
@@ -4617,11 +4614,11 @@ dwarf2_gdb_index::expand_symtabs_matching
 
   if (symbol_matcher == NULL && lookup_name == NULL)
     {
-      for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
+      for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
        {
          QUIT;
 
-         if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
+         if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
                                                file_matcher,
                                                expansion_notify))
            return false;
@@ -4724,22 +4721,23 @@ dwarf2_base_index_functions::map_symbol_filenames
      reuse the file names data from a currently unexpanded CU, in this
      case we don't want to report the files from the unexpanded CU.  */
 
-  for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
+  for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
     {
-      if (per_objfile->symtab_set_p (per_cu))
+      if (per_objfile->symtab_set_p (per_cu.get ()))
        {
          if (per_cu->v.quick->file_names != nullptr)
            qfn_cache.insert (per_cu->v.quick->file_names);
        }
     }
 
-  for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
+  for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
     {
       /* We only need to look at symtabs not already expanded.  */
-      if (per_objfile->symtab_set_p (per_cu))
+      if (per_objfile->symtab_set_p (per_cu.get ()))
        continue;
 
-      quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
+      quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
+                                                       per_objfile);
       if (file_data == nullptr
          || qfn_cache.find (file_data) != qfn_cache.end ())
        continue;
@@ -4979,10 +4977,10 @@ create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
             of the next CU as end of this CU.  We create the CUs here with
             length 0, and in cutu_reader::cutu_reader we'll fill in the
             actual length.  */
-         dwarf2_per_cu_data *per_cu
+         std::unique_ptr<dwarf2_per_cu_data> per_cu
            = create_cu_from_index_list (per_bfd, &section, is_dwz,
                                         sect_off, 0);
-         per_bfd->all_comp_units.push_back (per_cu);
+         per_bfd->all_comp_units.push_back (std::move (per_cu));
        }
       return;
     }
@@ -5004,10 +5002,10 @@ create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
       if (i >= 1)
        {
          const ULONGEST length = sect_off_next - sect_off_prev;
-         dwarf2_per_cu_data *per_cu
+         std::unique_ptr<dwarf2_per_cu_data> per_cu
            = create_cu_from_index_list (per_bfd, &section, is_dwz,
                                         sect_off_prev, length);
-         per_bfd->all_comp_units.push_back (per_cu);
+         per_bfd->all_comp_units.push_back (std::move (per_cu));
        }
       sect_off_prev = sect_off_next;
     }
@@ -5367,7 +5365,7 @@ dw2_debug_names_iterator::next ()
                         objfile_name (objfile));
              continue;
            }
-         per_cu = &per_bfd->get_tu (ull)->per_cu;
+         per_cu = per_bfd->get_tu (ull);
          break;
        case DW_IDX_die_offset:
          /* In a per-CU index (as opposed to a per-module index), index
@@ -5584,11 +5582,11 @@ dwarf2_debug_names_index::expand_symtabs_matching
 
   if (symbol_matcher == NULL && lookup_name == NULL)
     {
-      for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
+      for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
        {
          QUIT;
 
-         if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
+         if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
                                                file_matcher,
                                                expansion_notify))
            return false;
@@ -6056,10 +6054,10 @@ static int
 add_signatured_type_cu_to_table (void **slot, void *datum)
 {
   struct signatured_type *sigt = (struct signatured_type *) *slot;
-  std::vector<signatured_type *> *all_type_units
-    = (std::vector<signatured_type *> *) datum;
+  std::vector<std::unique_ptr<signatured_type>> *all_type_units
+    = (std::vector<std::unique_ptr<signatured_type>> *) datum;
 
-  all_type_units->push_back (sigt);
+  all_type_units->emplace_back (sigt);
 
   return 1;
 }
@@ -6103,7 +6101,7 @@ create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
   end_ptr = info_ptr + section->size;
   while (info_ptr < end_ptr)
     {
-      struct signatured_type *sig_type;
+      std::unique_ptr<signatured_type> sig_type;
       struct dwo_unit *dwo_tu;
       void **slot;
       const gdb_byte *ptr = info_ptr;
@@ -6144,7 +6142,6 @@ create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
 
       if (dwo_file)
        {
-         sig_type = NULL;
          dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
          dwo_tu->dwo_file = dwo_file;
          dwo_tu->signature = header.signature;
@@ -6161,14 +6158,16 @@ create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
          sig_type = per_objfile->per_bfd->allocate_signatured_type ();
          sig_type->signature = header.signature;
          sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
-         sig_type->per_cu.is_debug_types = 1;
-         sig_type->per_cu.section = section;
-         sig_type->per_cu.sect_off = sect_off;
-         sig_type->per_cu.length = length;
+         sig_type->is_debug_types = 1;
+         sig_type->section = section;
+         sig_type->sect_off = sect_off;
+         sig_type->length = length;
        }
 
       slot = htab_find_slot (types_htab.get (),
-                            dwo_file ? (void*) dwo_tu : (void *) sig_type,
+                            (dwo_file
+                             ? (void *) dwo_tu
+                             : (void *) sig_type.get ()),
                             INSERT);
       gdb_assert (slot != NULL);
       if (*slot != NULL)
@@ -6187,7 +6186,7 @@ create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
              const struct signatured_type *dup_tu
                = (const struct signatured_type *) *slot;
 
-             dup_sect_off = dup_tu->per_cu.sect_off;
+             dup_sect_off = dup_tu->sect_off;
            }
 
          complaint (_("debug type entry at offset %s is duplicate to"
@@ -6195,7 +6194,7 @@ create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
                     sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
                     hex_string (header.signature));
        }
-      *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
+      *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type.release ();
 
       dwarf_read_debug_printf_v ("  offset %s, signature %s",
                                 sect_offset_str (sect_off),
@@ -6269,16 +6268,18 @@ add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
       == per_objfile->per_bfd->all_type_units.capacity ())
     ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
 
-  signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
+  std::unique_ptr<signatured_type> sig_type_holder
+    = per_objfile->per_bfd->allocate_signatured_type ();
+  signatured_type *sig_type = sig_type_holder.get ();
 
   per_objfile->resize_symtabs ();
 
-  per_objfile->per_bfd->all_type_units.push_back (sig_type);
+  per_objfile->per_bfd->all_type_units.push_back (std::move (sig_type_holder));
   sig_type->signature = sig;
-  sig_type->per_cu.is_debug_types = 1;
+  sig_type->is_debug_types = 1;
   if (per_objfile->per_bfd->using_index)
     {
-      sig_type->per_cu.v.quick =
+      sig_type->v.quick =
        OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
                        struct dwarf2_per_cu_quick_data);
     }
@@ -6305,25 +6306,25 @@ fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
   dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
 
   /* Make sure we're not clobbering something we don't expect to.  */
-  gdb_assert (! sig_entry->per_cu.queued);
-  gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
+  gdb_assert (! sig_entry->queued);
+  gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
   if (per_bfd->using_index)
     {
-      gdb_assert (sig_entry->per_cu.v.quick != NULL);
-      gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
+      gdb_assert (sig_entry->v.quick != NULL);
+      gdb_assert (!per_objfile->symtab_set_p (sig_entry));
     }
   else
-      gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
+      gdb_assert (sig_entry->v.psymtab == NULL);
   gdb_assert (sig_entry->signature == dwo_entry->signature);
   gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
   gdb_assert (sig_entry->type_unit_group == NULL);
   gdb_assert (sig_entry->dwo_unit == NULL);
 
-  sig_entry->per_cu.section = dwo_entry->section;
-  sig_entry->per_cu.sect_off = dwo_entry->sect_off;
-  sig_entry->per_cu.length = dwo_entry->length;
-  sig_entry->per_cu.reading_dwo_directly = 1;
-  sig_entry->per_cu.per_bfd = per_bfd;
+  sig_entry->section = dwo_entry->section;
+  sig_entry->sect_off = dwo_entry->sect_off;
+  sig_entry->length = dwo_entry->length;
+  sig_entry->reading_dwo_directly = 1;
+  sig_entry->per_bfd = per_bfd;
   sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
   sig_entry->dwo_unit = dwo_entry;
 }
@@ -6376,7 +6377,7 @@ lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
   /* Have we already tried to read this TU?
      Note: sig_entry can be NULL if the skeleton TU was removed (thus it
      needn't exist in the global table yet).  */
-  if (sig_entry != NULL && sig_entry->per_cu.tu_read)
+  if (sig_entry != NULL && sig_entry->tu_read)
     return sig_entry;
 
   /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
@@ -6397,7 +6398,7 @@ lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
     sig_entry = add_type_unit (per_objfile, sig, slot);
 
   fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
-  sig_entry->per_cu.tu_read = 1;
+  sig_entry->tu_read = 1;
   return sig_entry;
 }
 
@@ -7116,7 +7117,13 @@ allocate_type_unit_groups_table ()
   return htab_up (htab_create_alloc (3,
                                     hash_type_unit_group,
                                     eq_type_unit_group,
-                                    NULL, xcalloc, xfree));
+                                    [] (void *arg)
+                                    {
+                                      type_unit_group *grp
+                                        = (type_unit_group *) arg;
+                                      delete grp;
+                                    },
+                                    xcalloc, xfree));
 }
 
 /* Type units that don't have DW_AT_stmt_list are grouped into their own
@@ -7128,22 +7135,19 @@ allocate_type_unit_groups_table ()
 /* Helper routine for get_type_unit_group.
    Create the type_unit_group object used to hold one or more TUs.  */
 
-static struct type_unit_group *
+static std::unique_ptr<type_unit_group>
 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
 {
   dwarf2_per_objfile *per_objfile = cu->per_objfile;
   dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
-  struct dwarf2_per_cu_data *per_cu;
-  struct type_unit_group *tu_group;
 
-  tu_group = OBSTACK_ZALLOC (&per_bfd->obstack, type_unit_group);
-  per_cu = &tu_group->per_cu;
-  per_cu->per_bfd = per_bfd;
+  std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
+  tu_group->per_bfd = per_bfd;
 
   if (per_bfd->using_index)
     {
-      per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
-                                       struct dwarf2_per_cu_quick_data);
+      tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
+                                         struct dwarf2_per_cu_quick_data);
     }
   else
     {
@@ -7158,7 +7162,8 @@ create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
       else
        name = string_printf ("<type_units_at_0x%x>", line_offset);
 
-      pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
+      pst = create_partial_symtab (tu_group.get (), per_objfile,
+                                  name.c_str ());
       pst->anonymous = true;
     }
 
@@ -7208,19 +7213,17 @@ get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
   type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
   slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
                         &type_unit_group_for_lookup, INSERT);
-  if (*slot != NULL)
-    {
-      tu_group = (struct type_unit_group *) *slot;
-      gdb_assert (tu_group != NULL);
-    }
-  else
+  if (*slot == nullptr)
     {
       sect_offset line_offset_struct = (sect_offset) line_offset;
-      tu_group = create_type_unit_group (cu, line_offset_struct);
-      *slot = tu_group;
+      std::unique_ptr<type_unit_group> grp
+       = create_type_unit_group (cu, line_offset_struct);
+      *slot = grp.release ();
       ++tu_stats->nr_symtabs;
     }
 
+  tu_group = (struct type_unit_group *) *slot;
+  gdb_assert (tu_group != nullptr);
   return tu_group;
 }
 \f
@@ -7560,10 +7563,10 @@ build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
   std::vector<tu_abbrev_offset> sorted_by_abbrev;
   sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
 
-  for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
+  for (const auto &sig_type : per_objfile->per_bfd->all_type_units)
     sorted_by_abbrev.emplace_back
-      (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
-                                    sig_type->per_cu.sect_off));
+      (sig_type.get (), read_abbrev_offset (per_objfile, sig_type->section,
+                                           sig_type->sect_off));
 
   std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
             sort_tu_by_abbrev_offset);
@@ -7583,7 +7586,7 @@ build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
          ++tu_stats->nr_uniq_abbrev_tables;
        }
 
-      cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
+      cutu_reader reader (tu.sig_type, per_objfile,
                          abbrev_table.get (), nullptr, false);
       if (!reader.dummy_p)
        build_type_psymtabs_reader (&reader, reader.info_ptr,
@@ -7621,21 +7624,20 @@ build_type_psymtab_dependencies (void **slot, void *info)
   dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
   dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
   struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
-  struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
-  dwarf2_psymtab *pst = per_cu->v.psymtab;
+  dwarf2_psymtab *pst = tu_group->v.psymtab;
   int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
   int i;
 
   gdb_assert (len > 0);
-  gdb_assert (per_cu->type_unit_group_p ());
+  gdb_assert (tu_group->type_unit_group_p ());
 
   pst->number_of_dependencies = len;
   pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
   for (i = 0; i < len; ++i)
     {
       struct signatured_type *iter = tu_group->tus->at (i);
-      gdb_assert (iter->per_cu.is_debug_types);
-      pst->dependencies[i] = iter->per_cu.v.psymtab;
+      gdb_assert (iter->is_debug_types);
+      pst->dependencies[i] = iter->v.psymtab;
       iter->type_unit_group = tu_group;
     }
 
@@ -7687,7 +7689,7 @@ process_skeletonless_type_unit (void **slot, void *info)
   *slot = entry;
 
   /* This does the job that build_type_psymtabs_1 would have done.  */
-  cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
+  cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
   if (!reader.dummy_p)
     build_type_psymtabs_reader (&reader, reader.info_ptr,
                                reader.comp_unit_die);
@@ -7731,7 +7733,7 @@ process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
 static void
 set_partial_user (dwarf2_per_objfile *per_objfile)
 {
-  for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
+  for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
     {
       dwarf2_psymtab *pst = per_cu->v.psymtab;
 
@@ -7780,12 +7782,12 @@ dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
     = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
                           addrmap_create_mutable (&temp_obstack));
 
-  for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
+  for (const auto &per_cu : per_bfd->all_comp_units)
     {
       if (per_cu->v.psymtab != NULL)
        /* In case a forward DW_TAG_imported_unit has read the CU already.  */
        continue;
-      process_psymtab_comp_unit (per_cu, per_objfile, false,
+      process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
                                 language_minimal);
     }
 
@@ -7858,7 +7860,7 @@ read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
 
   while (info_ptr < section->buffer + section->size)
     {
-      struct dwarf2_per_cu_data *this_cu;
+      std::unique_ptr<dwarf2_per_cu_data> this_cu;
 
       sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
 
@@ -7875,7 +7877,7 @@ read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
          auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
          sig_type->signature = cu_header.signature;
          sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
-         this_cu = &sig_type->per_cu;
+         this_cu = std::move (sig_type);
        }
       this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
       this_cu->sect_off = sect_off;
@@ -7883,9 +7885,8 @@ read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
       this_cu->is_dwz = is_dwz;
       this_cu->section = section;
 
-      per_objfile->per_bfd->all_comp_units.push_back (this_cu);
-
       info_ptr = info_ptr + this_cu->length;
+      per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
     }
 }
 
@@ -11262,7 +11263,6 @@ create_cus_hash_table (dwarf2_per_objfile *per_objfile,
       void **slot;
       sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
 
-      memset (&per_cu, 0, sizeof (per_cu));
       per_cu.per_bfd = per_bfd;
       per_cu.is_debug_types = 0;
       per_cu.sect_off = sect_offset (info_ptr - section.buffer);
@@ -13090,14 +13090,13 @@ queue_and_load_dwo_tu (void **slot, void *info)
 
   if (sig_type != NULL)
     {
-      struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
-
       /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
         a real dependency of PER_CU on SIG_TYPE.  That is detected later
         while processing PER_CU.  */
-      if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
-       load_full_type_unit (sig_cu, cu->per_objfile);
-      cu->per_cu->imported_symtabs_push (sig_cu);
+      if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
+                                cu->language))
+       load_full_type_unit (sig_type, cu->per_objfile);
+      cu->per_cu->imported_symtabs_push (sig_type);
     }
 
   return 1;
@@ -23882,12 +23881,12 @@ follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
      Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
      it doesn't mean they are currently loaded.  Since we require them
      to be loaded, we must check for ourselves.  */
-  if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
+  if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
                             language_minimal)
-      || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
+      || per_objfile->get_cu (sig_type) == nullptr)
     read_signatured_type (sig_type, per_objfile);
 
-  sig_cu = per_objfile->get_cu (&sig_type->per_cu);
+  sig_cu = per_objfile->get_cu (sig_type);
   gdb_assert (sig_cu != NULL);
   gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
   temp_die.sect_off = sig_type->type_offset_in_section;
@@ -24076,12 +24075,10 @@ static void
 read_signatured_type (signatured_type *sig_type,
                      dwarf2_per_objfile *per_objfile)
 {
-  struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
-
-  gdb_assert (per_cu->is_debug_types);
-  gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
+  gdb_assert (sig_type->is_debug_types);
+  gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
 
-  cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
+  cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
 
   if (!reader.dummy_p)
     {
@@ -24116,7 +24113,7 @@ read_signatured_type (signatured_type *sig_type,
       reader.keep ();
     }
 
-  sig_type->per_cu.tu_read = 1;
+  sig_type->tu_read = 1;
 }
 
 /* Decode simple location descriptions.
@@ -24710,7 +24707,7 @@ static int
 dwarf2_find_containing_comp_unit
   (sect_offset sect_off,
    unsigned int offset_in_dwz,
-   const std::vector<dwarf2_per_cu_data *> &all_comp_units)
+   const std::vector<std::unique_ptr<dwarf2_per_cu_data>> &all_comp_units)
 {
   int low, high;
 
@@ -24721,7 +24718,7 @@ dwarf2_find_containing_comp_unit
       struct dwarf2_per_cu_data *mid_cu;
       int mid = low + (high - low) / 2;
 
-      mid_cu = all_comp_units[mid];
+      mid_cu = all_comp_units[mid].get ();
       if (mid_cu->is_dwz > offset_in_dwz
          || (mid_cu->is_dwz == offset_in_dwz
              && mid_cu->sect_off + mid_cu->length > sect_off))
@@ -24743,7 +24740,8 @@ dwarf2_find_containing_comp_unit (sect_offset sect_off,
 {
   int low = dwarf2_find_containing_comp_unit
     (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
-  dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
+  dwarf2_per_cu_data *this_cu
+    = per_objfile->per_bfd->all_comp_units[low].get ();
 
   if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
     {
@@ -24755,7 +24753,7 @@ dwarf2_find_containing_comp_unit (sect_offset sect_off,
 
       gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
                  <= sect_off);
-      return per_objfile->per_bfd->all_comp_units[low-1];
+      return per_objfile->per_bfd->all_comp_units[low - 1].get ();
     }
   else
     {
@@ -24775,42 +24773,46 @@ namespace find_containing_comp_unit {
 static void
 run_test ()
 {
-  struct dwarf2_per_cu_data one {};
-  struct dwarf2_per_cu_data two {};
-  struct dwarf2_per_cu_data three {};
-  struct dwarf2_per_cu_data four {};
-
-  one.length = 5;
-  two.sect_off = sect_offset (one.length);
-  two.length = 7;
-
-  three.length = 5;
-  three.is_dwz = 1;
-  four.sect_off = sect_offset (three.length);
-  four.length = 7;
-  four.is_dwz = 1;
-
-  std::vector<dwarf2_per_cu_data *> units;
-  units.push_back (&one);
-  units.push_back (&two);
-  units.push_back (&three);
-  units.push_back (&four);
+  std::unique_ptr<dwarf2_per_cu_data> one (new dwarf2_per_cu_data);
+  dwarf2_per_cu_data *one_ptr = one.get ();
+  std::unique_ptr<dwarf2_per_cu_data> two (new dwarf2_per_cu_data);
+  dwarf2_per_cu_data *two_ptr = two.get ();
+  std::unique_ptr<dwarf2_per_cu_data> three (new dwarf2_per_cu_data);
+  dwarf2_per_cu_data *three_ptr = three.get ();
+  std::unique_ptr<dwarf2_per_cu_data> four (new dwarf2_per_cu_data);
+  dwarf2_per_cu_data *four_ptr = four.get ();
+
+  one->length = 5;
+  two->sect_off = sect_offset (one->length);
+  two->length = 7;
+
+  three->length = 5;
+  three->is_dwz = 1;
+  four->sect_off = sect_offset (three->length);
+  four->length = 7;
+  four->is_dwz = 1;
+
+  std::vector<std::unique_ptr<dwarf2_per_cu_data>> units;
+  units.push_back (std::move (one));
+  units.push_back (std::move (two));
+  units.push_back (std::move (three));
+  units.push_back (std::move (four));
 
   int result;
 
   result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
-  SELF_CHECK (units[result] == &one);
+  SELF_CHECK (units[result].get () == one_ptr);
   result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
-  SELF_CHECK (units[result] == &one);
+  SELF_CHECK (units[result].get () == one_ptr);
   result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
-  SELF_CHECK (units[result] == &two);
+  SELF_CHECK (units[result].get () == two_ptr);
 
   result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
-  SELF_CHECK (units[result] == &three);
+  SELF_CHECK (units[result].get () == three_ptr);
   result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
-  SELF_CHECK (units[result] == &three);
+  SELF_CHECK (units[result].get () == three_ptr);
   result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
-  SELF_CHECK (units[result] == &four);
+  SELF_CHECK (units[result].get () == four_ptr);
 }
 
 }
index dc25ff16f517d60687f682b1e89ea93ad55186f6..98ace89644f217e4ee9282dcc02cbcd82ab04200 100644 (file)
@@ -120,12 +120,12 @@ struct dwarf2_per_bfd
   /* A convenience function to allocate a dwarf2_per_cu_data.  The
      returned object has its "index" field set properly.  The object
      is allocated on the dwarf2_per_bfd obstack.  */
-  dwarf2_per_cu_data *allocate_per_cu ();
+  std::unique_ptr<dwarf2_per_cu_data> allocate_per_cu ();
 
   /* A convenience function to allocate a signatured_type.  The
      returned object has its "index" field set properly.  The object
      is allocated on the dwarf2_per_bfd obstack.  */
-  signatured_type *allocate_signatured_type ();
+  std::unique_ptr<signatured_type> allocate_signatured_type ();
 
   /* Return the number of partial symtabs allocated with allocate_per_cu
      and allocate_signatured_type so far.  */
@@ -171,10 +171,10 @@ public:
 
   /* Table of all the compilation units.  This is used to locate
      the target compilation unit of a particular reference.  */
-  std::vector<dwarf2_per_cu_data *> all_comp_units;
+  std::vector<std::unique_ptr<dwarf2_per_cu_data>> all_comp_units;
 
   /* The .debug_types-related CUs (TUs).  */
-  std::vector<signatured_type *> all_type_units;
+  std::vector<std::unique_ptr<signatured_type>> all_type_units;
 
   /* Table of struct type_unit_group objects.
      The hash key is the DW_AT_stmt_list value.  */
@@ -425,16 +425,29 @@ struct dwarf2_psymtab : public partial_symtab
 
 struct dwarf2_per_cu_data
 {
+  dwarf2_per_cu_data ()
+    : queued (false),
+      load_all_dies (false),
+      is_debug_types (false),
+      is_dwz (false),
+      reading_dwo_directly (false),
+      tu_read (false),
+      m_header_read_in (false),
+      unit_type {},
+      lang (language_unknown)
+  {
+  }
+
   /* 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;
+  sect_offset sect_off {};
+  unsigned int length = 0;
 
   /* DWARF standard version this data has been read from (such as 4 or 5).  */
-  unsigned char dwarf_version;
+  unsigned char dwarf_version = 0;
 
   /* Flag indicating this compilation unit will be read in before
      any of the current compilation units are processed.  */
@@ -483,15 +496,15 @@ struct dwarf2_per_cu_data
   ENUM_BITFIELD (language) lang : LANGUAGE_BITS;
 
   /* Our index in the unshared "symtabs" vector.  */
-  unsigned index;
+  unsigned index = 0;
 
   /* 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;
+  struct dwarf2_section_info *section = nullptr;
 
   /* Backlink to the owner of this.  */
-  dwarf2_per_bfd *per_bfd;
+  dwarf2_per_bfd *per_bfd = nullptr;
 
   /* DWARF header of this CU.  Note that dwarf2_cu reads its own version of the
      header, which may differ from this one, since it may pass rcuh_kind::TYPE
@@ -500,7 +513,7 @@ struct dwarf2_per_cu_data
 
      Don't access this field directly, use the get_header method instead.  It
      should be private, but we can't make it private at the moment.  */
-  mutable comp_unit_head m_header;
+  mutable comp_unit_head m_header {};
 
   /* When dwarf2_per_bfd::using_index is true, the 'quick' field
      is active.  Otherwise, the 'psymtab' field is active.  */
@@ -512,7 +525,7 @@ struct dwarf2_per_cu_data
 
     /* Data needed by the "quick" functions.  */
     struct dwarf2_per_cu_quick_data *quick;
-  } v;
+  } v {};
 
   /* The CUs we import using DW_TAG_imported_unit.  This is filled in
      while reading psymtabs, used to compute the psymtab dependencies,
@@ -538,7 +551,7 @@ struct dwarf2_per_cu_data
      could be refactored to make this private.  Until then please try to
      avoid direct access to this member, and instead use the helper
      functions above.  */
-  std::vector <dwarf2_per_cu_data *> *imported_symtabs;
+  std::vector <dwarf2_per_cu_data *> *imported_symtabs = nullptr;
 
   /* Return true of IMPORTED_SYMTABS is empty or not yet allocated.  */
   bool imported_symtabs_empty () const
@@ -602,36 +615,30 @@ struct dwarf2_per_cu_data
 
 /* Entry in the signatured_types hash table.  */
 
-struct signatured_type
+struct signatured_type : public dwarf2_per_cu_data
 {
-  /* 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;
+  ULONGEST signature = 0;
 
   /* 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;
+  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;
+  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;
+  struct type_unit_group *type_unit_group = nullptr;
 
   /* Containing DWO unit.
      This field is valid iff per_cu.reading_dwo_directly.  */
-  struct dwo_unit *dwo_unit;
+  struct dwo_unit *dwo_unit = nullptr;
 };
 
 /* Return the type of the DIE at DIE_OFFSET in the CU named by