[PATCH 1/2] Remove dwarf2_per_bfd::all_type_units

Tom Tromey tom@tromey.com
Sun Apr 18 18:24:01 GMT 2021


I don't think there is any deep reason to separate CUs and TUs in
dwarf2_per_bfd.  This patch removes all_type_units and unifies these
two containers.  Some minor tweaks are needed to the index writers,
because both forms of index keep CUs and TUs separate;

Regression tested on x86-63 Fedora 32.

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

	* dwarf2/read.h (struct tu_stats) <nr_tus>: New member.
	(struct dwarf2_per_bfd) <get_cutu, get_tu>: Remove
	<get_cu>: Now inline.
	<all_type_units>: Remove.
	* dwarf2/read.c (dwarf2_per_bfd::~dwarf2_per_bfd): Update.
	(dwarf2_per_bfd::get_cutu, dwarf2_per_bfd::get_cu)
	(dwarf2_per_bfd::get_tu): Remove.
	(dwarf2_per_bfd::allocate_signatured_type): Update nr_tus.
	(create_signatured_type_table_from_index)
	(create_signatured_type_table_from_debug_names)
	(dw2_symtab_iter_next, dwarf2_base_index_functions::print_stats)
	(dwarf2_base_index_functions::expand_all_symtabs)
	(dw2_expand_marked_cus, dw_expand_symtabs_matching_file_matcher)
	(dwarf2_base_index_functions::map_symbol_filenames)
	(dw2_debug_names_iterator::next, dwarf2_initialize_objfile)
	(add_signatured_type_cu_to_table, create_all_type_units)
	(add_type_unit, build_type_psymtabs_1, print_tu_stats)
	(create_all_comp_units): Update.
	* dwarf2/index-write.c (check_dwarf64_offsets, write_gdbindex)
	(write_debug_names): Update.
---
 gdb/ChangeLog            |  23 +++++++
 gdb/dwarf2/index-write.c |  29 +++++----
 gdb/dwarf2/read.c        | 128 +++++++++++++--------------------------
 gdb/dwarf2/read.h        |  32 ++--------
 4 files changed, 89 insertions(+), 123 deletions(-)

diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c
index b000cd5b8fd..8cdfac79805 100644
--- a/gdb/dwarf2/index-write.c
+++ b/gdb/dwarf2/index-write.c
@@ -1318,12 +1318,6 @@ check_dwarf64_offsets (dwarf2_per_objfile *per_objfile)
 	  >= (static_cast<uint64_t> (1) << 32))
 	return true;
     }
-  for (const auto &sigtype : per_objfile->per_bfd->all_type_units)
-    {
-      if (to_underlying (sigtype->sect_off)
-	  >= (static_cast<uint64_t> (1) << 32))
-	return true;
-    }
   return false;
 }
 
@@ -1438,10 +1432,14 @@ write_gdbindex (dwarf2_per_objfile *per_objfile, FILE *out_file,
 
   std::unordered_set<partial_symbol *> psyms_seen
     (psyms_seen_size (per_objfile));
+  int counter = 0;
   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].get ();
+      if (per_cu->is_debug_types)
+	continue;
+
       partial_symtab *psymtab = per_cu->v.psymtab;
 
       if (psymtab != NULL)
@@ -1450,7 +1448,7 @@ write_gdbindex (dwarf2_per_objfile *per_objfile, FILE *out_file,
 	    recursively_write_psymbols (objfile, psymtab, &symtab,
 					psyms_seen, i);
 
-	  const auto insertpair = cu_index_htab.emplace (psymtab, i);
+	  const auto insertpair = cu_index_htab.emplace (psymtab, counter);
 	  gdb_assert (insertpair.second);
 	}
 
@@ -1461,6 +1459,7 @@ write_gdbindex (dwarf2_per_objfile *per_objfile, FILE *out_file,
       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);
+      ++counter;
     }
 
   /* Dump the address map.  */
@@ -1476,7 +1475,8 @@ write_gdbindex (dwarf2_per_objfile *per_objfile, FILE *out_file,
 
       sig_data.objfile = objfile;
       sig_data.symtab = &symtab;
-      sig_data.cu_index = per_objfile->per_bfd->all_comp_units.size ();
+      sig_data.cu_index = (per_objfile->per_bfd->all_comp_units.size ()
+			   - per_objfile->per_bfd->tu_stats.nr_tus);
       htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
 			      write_one_signatured_type, &sig_data);
     }
@@ -1520,10 +1520,14 @@ write_debug_names (dwarf2_per_objfile *per_objfile,
   debug_names nametable (per_objfile, dwarf5_is_dwarf64, dwarf5_byte_order);
   std::unordered_set<partial_symbol *>
     psyms_seen (psyms_seen_size (per_objfile));
+  int counter = 0;
   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].get ();
+      if (per_cu->is_debug_types)
+	continue;
+
       partial_symtab *psymtab = per_cu->v.psymtab;
 
       /* CU of a shared file from 'dwz -m' may be unused by this main
@@ -1533,10 +1537,12 @@ write_debug_names (dwarf2_per_objfile *per_objfile,
 	continue;
 
       if (psymtab->user == NULL)
-	nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
+	nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen,
+					      counter);
 
       cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
 			   to_underlying (per_cu->sect_off));
+      ++counter;
     }
 
   /* Write out the .debug_type entries, if any.  */
@@ -1590,12 +1596,13 @@ write_debug_names (dwarf2_per_objfile *per_objfile,
 
   /* comp_unit_count - The number of CUs in the CU list.  */
   header.append_uint (4, dwarf5_byte_order,
-		      per_objfile->per_bfd->all_comp_units.size ());
+		      per_objfile->per_bfd->all_comp_units.size ()
+		      - per_objfile->per_bfd->tu_stats.nr_tus);
 
   /* local_type_unit_count - The number of TUs in the local TU
      list.  */
   header.append_uint (4, dwarf5_byte_order,
-		      per_objfile->per_bfd->all_type_units.size ());
+		      per_objfile->per_bfd->tu_stats.nr_tus);
 
   /* foreign_type_unit_count - The number of TUs in the foreign TU
      list.  */
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 15f0a43003f..e28856c2fd7 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -1899,9 +1899,6 @@ dwarf2_per_bfd::~dwarf2_per_bfd ()
   for (auto &per_cu : all_comp_units)
     per_cu->imported_symtabs_free ();
 
-  for (auto &sig_type : all_type_units)
-    sig_type->imported_symtabs_free ();
-
   /* Everything else should be on this->obstack.  */
 }
 
@@ -2524,41 +2521,6 @@ dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
   return per_objfile->get_symtab (per_cu);
 }
 
-/* See declaration.  */
-
-dwarf2_per_cu_data *
-dwarf2_per_bfd::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].get ();
-    }
-
-  return this->all_comp_units[index].get ();
-}
-
-/* See declaration.  */
-
-dwarf2_per_cu_data *
-dwarf2_per_bfd::get_cu (int index)
-{
-  gdb_assert (index >= 0 && index < this->all_comp_units.size ());
-
-  return this->all_comp_units[index].get ();
-}
-
-/* See declaration.  */
-
-signatured_type *
-dwarf2_per_bfd::get_tu (int index)
-{
-  gdb_assert (index >= 0 && index < this->all_type_units.size ());
-
-  return this->all_type_units[index].get ();
-}
-
 /* See read.h.  */
 
 std::unique_ptr<dwarf2_per_cu_data>
@@ -2578,6 +2540,7 @@ dwarf2_per_bfd::allocate_signatured_type ()
   std::unique_ptr<signatured_type> result (new signatured_type);
   result->per_bfd = this;
   result->index = m_num_psymtabs++;
+  tu_stats.nr_tus++;
   return result;
 }
 
@@ -2654,9 +2617,6 @@ create_signatured_type_table_from_index
   (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
    const gdb_byte *bytes, offset_type elements)
 {
-  gdb_assert (per_bfd->all_type_units.empty ());
-  per_bfd->all_type_units.reserve (elements / 3);
-
   htab_up sig_types_hash = allocate_signatured_type_table ();
 
   for (offset_type i = 0; i < elements; i += 3)
@@ -2688,7 +2648,7 @@ create_signatured_type_table_from_index
       slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
       *slot = sig_type.get ();
 
-      per_bfd->all_type_units.push_back (std::move (sig_type));
+      per_bfd->all_comp_units.push_back (std::move (sig_type));
     }
 
   per_bfd->signatured_types = std::move (sig_types_hash);
@@ -2708,9 +2668,6 @@ create_signatured_type_table_from_debug_names
   section->read (objfile);
   abbrev_section->read (objfile);
 
-  gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
-  per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
-
   htab_up sig_types_hash = allocate_signatured_type_table ();
 
   for (uint32_t i = 0; i < map.tu_count; ++i)
@@ -2743,7 +2700,7 @@ create_signatured_type_table_from_debug_names
       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 (std::move (sig_type));
+      per_objfile->per_bfd->all_comp_units.push_back (std::move (sig_type));
     }
 
   per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
@@ -3420,15 +3377,14 @@ dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
 	 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
 
       /* Don't crash on bad data.  */
-      if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
-		       + per_objfile->per_bfd->all_type_units.size ()))
+      if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
 	{
 	  complaint (_(".gdb_index entry has bad CU index"
 		       " [in module %s]"), objfile_name (per_objfile->objfile));
 	  continue;
 	}
 
-      dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
+      dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
 
       /* Skip if already read in.  */
       if (per_objfile->symtab_set_p (per_cu))
@@ -3502,13 +3458,12 @@ dwarf2_base_index_functions::print_stats (struct objfile *objfile,
     return;
 
   dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
-  int total = (per_objfile->per_bfd->all_comp_units.size ()
-	       + per_objfile->per_bfd->all_type_units.size ());
+  int total = per_objfile->per_bfd->all_comp_units.size ();
   int count = 0;
 
   for (int i = 0; i < total; ++i)
     {
-      dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
+      dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
 
       if (!per_objfile->symtab_set_p (per_cu))
 	++count;
@@ -3543,12 +3498,11 @@ void
 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
 {
   dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
-  int total_units = (per_objfile->per_bfd->all_comp_units.size ()
-		     + per_objfile->per_bfd->all_type_units.size ());
+  int total_units = per_objfile->per_bfd->all_comp_units.size ();
 
   for (int i = 0; i < total_units; ++i)
     {
-      dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
+      dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
 
       /* We don't want to directly expand a partial CU, because if we
 	 read it with the wrong language, then assertion failures can
@@ -4497,15 +4451,14 @@ dw2_expand_marked_cus
 	}
 
       /* Don't crash on bad data.  */
-      if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
-		       + per_objfile->per_bfd->all_type_units.size ()))
+      if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
 	{
 	  complaint (_(".gdb_index entry has bad CU index"
 		       " [in module %s]"), objfile_name (per_objfile->objfile));
 	  continue;
 	}
 
-      dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
+      dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
       if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
 					    expansion_notify))
 	return false;
@@ -4540,6 +4493,8 @@ dw_expand_symtabs_matching_file_matcher
     {
       QUIT;
 
+      if (per_cu->is_debug_types)
+	continue;
       per_cu->v.quick->mark = 0;
 
       /* We only need to look at symtabs not already expanded.  */
@@ -4736,6 +4691,8 @@ dwarf2_base_index_functions::map_symbol_filenames
 
       for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
 	{
+	  if (per_cu->is_debug_types)
+	    continue;
 	  /* We only need to look at symtabs not already expanded.  */
 	  if (per_objfile->symtab_set_p (per_cu.get ()))
 	    continue;
@@ -5365,11 +5322,11 @@ dw2_debug_names_iterator::next ()
 			 objfile_name (objfile));
 	      continue;
 	    }
-	  per_cu = per_bfd->get_cutu (ull);
+	  per_cu = per_bfd->get_cu (ull);
 	  break;
 	case DW_IDX_type_unit:
 	  /* Don't crash on bad data.  */
-	  if (ull >= per_bfd->all_type_units.size ())
+	  if (ull >= per_bfd->tu_stats.nr_tus)
 	    {
 	      complaint (_(".debug_names entry has bad TU index %s"
 			   " [in module %s]"),
@@ -5377,7 +5334,7 @@ dw2_debug_names_iterator::next ()
 			 objfile_name (objfile));
 	      continue;
 	    }
-	  per_cu = per_bfd->get_tu (ull);
+	  per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
 	  break;
 	case DW_IDX_die_offset:
 	  /* In a per-CU index (as opposed to a per-module index), index
@@ -5718,10 +5675,9 @@ dwarf2_initialize_objfile (struct objfile *objfile)
 	= create_quick_file_names_table (per_bfd->all_comp_units.size ());
       per_objfile->resize_symtabs ();
 
-      for (int i = 0; i < (per_bfd->all_comp_units.size ()
-			   + per_bfd->all_type_units.size ()); ++i)
+      for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
 	{
-	  dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
+	  dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
 
 	  per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
 					    struct dwarf2_per_cu_quick_data);
@@ -6066,10 +6022,10 @@ static int
 add_signatured_type_cu_to_table (void **slot, void *datum)
 {
   struct signatured_type *sigt = (struct signatured_type *) *slot;
-  std::vector<std::unique_ptr<signatured_type>> *all_type_units
-    = (std::vector<std::unique_ptr<signatured_type>> *) datum;
+  std::vector<std::unique_ptr<dwarf2_per_cu_data>> *all_comp_units
+    = (std::vector<std::unique_ptr<dwarf2_per_cu_data>> *) datum;
 
-  all_type_units->emplace_back (sigt);
+  all_comp_units->emplace_back (sigt);
 
   return 1;
 }
@@ -6237,7 +6193,7 @@ create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
 }
 
 /* Create the hash table of all entries in the .debug_types section,
-   and initialize all_type_units.
+   and update all_comp_units.
    The result is zero if there is an error (e.g. missing .debug_types section),
    otherwise non-zero.	*/
 
@@ -6258,13 +6214,9 @@ create_all_type_units (dwarf2_per_objfile *per_objfile)
 
   per_objfile->per_bfd->signatured_types = std::move (types_htab);
 
-  gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
-  per_objfile->per_bfd->all_type_units.reserve
-    (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
-
   htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
 			  add_signatured_type_cu_to_table,
-			  &per_objfile->per_bfd->all_type_units);
+			  &per_objfile->per_bfd->all_comp_units);
 
   return 1;
 }
@@ -6276,8 +6228,8 @@ create_all_type_units (dwarf2_per_objfile *per_objfile)
 static struct signatured_type *
 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
 {
-  if (per_objfile->per_bfd->all_type_units.size ()
-      == per_objfile->per_bfd->all_type_units.capacity ())
+  if (per_objfile->per_bfd->all_comp_units.size ()
+      == per_objfile->per_bfd->all_comp_units.capacity ())
     ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
 
   std::unique_ptr<signatured_type> sig_type_holder
@@ -6286,7 +6238,7 @@ add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
 
   per_objfile->resize_symtabs ();
 
-  per_objfile->per_bfd->all_type_units.push_back (std::move (sig_type_holder));
+  per_objfile->per_bfd->all_comp_units.push_back (std::move (sig_type_holder));
   sig_type->signature = sig;
   sig_type->is_debug_types = 1;
   if (per_objfile->per_bfd->using_index)
@@ -7546,7 +7498,7 @@ build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
   /* It's up to the caller to not call us multiple times.  */
   gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
 
-  if (per_objfile->per_bfd->all_type_units.empty ())
+  if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
     return;
 
   /* TUs typically share abbrev tables, and there can be way more TUs than
@@ -7570,15 +7522,21 @@ build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
 
   dwarf_read_debug_printf ("Building type unit groups ...");
 
-  /* Sort in a separate table to maintain the order of all_type_units
+  /* Sort in a separate table to maintain the order of all_comp_units
      for .gdb_index: TU indices directly index all_type_units.  */
   std::vector<tu_abbrev_offset> sorted_by_abbrev;
-  sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
+  sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
 
-  for (const auto &sig_type : per_objfile->per_bfd->all_type_units)
-    sorted_by_abbrev.emplace_back
-      (sig_type.get (), read_abbrev_offset (per_objfile, sig_type->section,
-					    sig_type->sect_off));
+  for (const auto &cu : per_objfile->per_bfd->all_comp_units)
+    {
+      if (cu->is_debug_types)
+	{
+	  auto sig_type = static_cast<signatured_type *> (cu.get ());
+	  sorted_by_abbrev.emplace_back
+	    (sig_type, 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);
@@ -7614,8 +7572,7 @@ print_tu_stats (dwarf2_per_objfile *per_objfile)
   struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
 
   dwarf_read_debug_printf ("Type unit statistics:");
-  dwarf_read_debug_printf ("  %zu TUs",
-			   per_objfile->per_bfd->all_type_units.size ());
+  dwarf_read_debug_printf ("  %d TUs", tu_stats->nr_tus);
   dwarf_read_debug_printf ("  %d uniq abbrev tables",
 			   tu_stats->nr_uniq_abbrev_tables);
   dwarf_read_debug_printf ("  %d symtabs from stmt_list entries",
@@ -7908,7 +7865,6 @@ read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
 static void
 create_all_comp_units (dwarf2_per_objfile *per_objfile)
 {
-  gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
   read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
 				&per_objfile->per_bfd->abbrev, 0);
 
diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h
index 49997a1f6f6..294d5337fed 100644
--- a/gdb/dwarf2/read.h
+++ b/gdb/dwarf2/read.h
@@ -42,6 +42,7 @@ struct tu_stats
   int nr_symtab_sharers;
   int nr_stmt_less_type_units;
   int nr_all_type_units_reallocs;
+  int nr_tus;
 };
 
 struct dwarf2_cu;
@@ -93,29 +94,11 @@ struct dwarf2_per_bfd
 
   DISABLE_COPY_AND_ASSIGN (dwarf2_per_bfd);
 
-  /* Return the CU/TU given its index.
-
-     This is intended for loops like:
-
-     for (i = 0; i < (dwarf2_per_bfd->n_comp_units
-		      + dwarf2_per_bfd->n_type_units); ++i)
-       {
-	 dwarf2_per_cu_data *per_cu = dwarf2_per_bfd->get_cutu (i);
-
-	 ...;
-       }
-  */
-  dwarf2_per_cu_data *get_cutu (int index);
-
-  /* Return the CU given its index.
-     This differs from get_cutu in that it's for when you know INDEX refers to a
-     CU.  */
-  dwarf2_per_cu_data *get_cu (int index);
-
-  /* Return the TU given its index.
-     This differs from get_cutu in that it's for when you know INDEX refers to a
-     TU.  */
-  signatured_type *get_tu (int index);
+  /* Return the CU given its index.  */
+  dwarf2_per_cu_data *get_cu (int index) const
+  {
+    return this->all_comp_units[index].get ();
+  }
 
   /* A convenience function to allocate a dwarf2_per_cu_data.  The
      returned object has its "index" field set properly.  The object
@@ -173,9 +156,6 @@ struct dwarf2_per_bfd
      the target compilation unit of a particular reference.  */
   std::vector<std::unique_ptr<dwarf2_per_cu_data>> all_comp_units;
 
-  /* The .debug_types-related CUs (TUs).  */
-  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.  */
   htab_up type_unit_groups;
-- 
2.26.2



More information about the Gdb-patches mailing list