]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/dwarf: add unit_lists structure to index writer
authorSimon Marchi <simon.marchi@polymtl.ca>
Sat, 17 Jan 2026 06:02:35 +0000 (01:02 -0500)
committerSimon Marchi <simon.marchi@polymtl.ca>
Tue, 20 Jan 2026 20:57:55 +0000 (15:57 -0500)
I think it makes the code more readable than the pair of vector.  Also,
I'm considering adding a third list (foreigh type units), which will be
easier with the structure.

Change-Id: I38ec4ddf8f786a2ba10c5b371cfe04c2baaa7da9
Approved-By: Tom Tromey <tom@tromey.com>
gdb/dwarf2/index-write.c

index c4bd424d43406dd570323458d85d77958a8f1278..c4e75898f4c0b1de51f1e6618d192851e1946c3b 100644 (file)
@@ -1314,21 +1314,30 @@ write_shortcuts_table (cooked_index *table, data_buf &shortcuts,
   shortcuts.append_offset (main_name_offset);
 }
 
+/* Lists of units, split by kind.  Each list is sorted by section offset.  */
+
+struct unit_lists
+{
+  /* Compilation units.  */
+  std::vector<const dwarf2_per_cu *> comp;
+
+  /* Type units.  */
+  std::vector<const signatured_type *> type;
+};
+
 /* Get sorted (by section offset) lists of comp units and type units.  */
 
-static std::pair<std::vector<const dwarf2_per_cu *>,
-                std::vector<const signatured_type *>>
+static unit_lists
 get_unit_lists (const dwarf2_per_bfd &per_bfd)
 {
-  std::vector<const dwarf2_per_cu *> comp_units;
-  std::vector<const signatured_type *> type_units;
+  unit_lists lists;
 
   for (const auto &unit : per_bfd.all_units)
     if (const signatured_type *sig_type = unit->as_signatured_type ();
        sig_type != nullptr)
-      type_units.emplace_back (sig_type);
+      lists.type.emplace_back (sig_type);
     else
-      comp_units.emplace_back (unit.get ());
+      lists.comp.emplace_back (unit.get ());
 
   auto by_sect_off = [] (const dwarf2_per_cu *lhs, const dwarf2_per_cu *rhs)
                       { return lhs->sect_off () < rhs->sect_off (); };
@@ -1342,10 +1351,10 @@ get_unit_lists (const dwarf2_per_bfd &per_bfd)
 
      However, it helps make sure that GDB produce a stable and predictable
      output, which is nice.  */
-  std::sort (comp_units.begin (), comp_units.end (), by_sect_off);
-  std::sort (type_units.begin (), type_units.end (), by_sect_off);
+  std::sort (lists.comp.begin (), lists.comp.end (), by_sect_off);
+  std::sort (lists.type.begin (), lists.type.end (), by_sect_off);
 
-  return {std::move (comp_units), std::move (type_units)};
+  return lists;
 }
 
 /* Write contents of a .gdb_index section for OBJFILE into OUT_FILE.
@@ -1365,14 +1374,14 @@ write_gdbindex (dwarf2_per_bfd *per_bfd, cooked_index *table,
   cu_index_map cu_index_htab;
   cu_index_htab.reserve (per_bfd->all_units.size ());
 
-  auto [comp_units, type_units] = get_unit_lists (*per_bfd);
+  unit_lists units = get_unit_lists (*per_bfd);
   int counter = 0;
 
   /* Write comp units.  */
   data_buf objfile_cu_list;
   data_buf dwz_cu_list;
 
-  for (const dwarf2_per_cu *per_cu : comp_units)
+  for (const dwarf2_per_cu *per_cu : units.comp)
     {
       const auto insertpair = cu_index_htab.emplace (per_cu, counter);
       gdb_assert (insertpair.second);
@@ -1390,7 +1399,7 @@ write_gdbindex (dwarf2_per_bfd *per_bfd, cooked_index *table,
   /* Write type units.  */
   data_buf types_cu_list;
 
-  for (const signatured_type *sig_type : type_units)
+  for (const signatured_type *sig_type : units.type)
     {
       const auto insertpair = cu_index_htab.emplace (sig_type, counter);
       gdb_assert (insertpair.second);
@@ -1449,12 +1458,12 @@ write_debug_names (dwarf2_per_bfd *per_bfd, cooked_index *table,
   const enum bfd_endian dwarf5_byte_order
     = bfd_big_endian (per_bfd->obfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
 
-  auto [comp_units, type_units] = get_unit_lists (*per_bfd);
+  unit_lists units = get_unit_lists (*per_bfd);
   debug_names nametable (per_bfd, dwarf5_is_dwarf64, dwarf5_byte_order);
   data_buf comp_unit_list;
   int comp_unit_counter = 0;
 
-  for (const auto per_cu : comp_units)
+  for (const auto per_cu : units.comp)
     {
       nametable.add_cu (per_cu, comp_unit_counter);
       comp_unit_list.append_uint (nametable.dwarf5_offset_size (),
@@ -1466,7 +1475,7 @@ write_debug_names (dwarf2_per_bfd *per_bfd, cooked_index *table,
   data_buf type_unit_list;
   int type_unit_counter = 0;
 
-  for (const auto per_cu : type_units)
+  for (const auto per_cu : units.type)
     {
       nametable.add_cu (per_cu, type_unit_counter);
       type_unit_list.append_uint (nametable.dwarf5_offset_size (),