[07/28] Restore "ingestion" of .debug_str when writing .debug_names

Message ID 20250311-search-in-psyms-v1-7-d73d9be20983@tromey.com
State New
Headers
Series Search symbols via quick API |

Commit Message

Tom Tromey March 11, 2025, 2:12 p.m. UTC
  When I rewrote the .debug_names writer (commit 91a42a61), I changed
the writer to not import .debug_str into the debug_str_lookup object.

However, a later patch in this series needed this again.  The issue
here was that if a name occurs in the DWARF, and is also allocated,
then there is a race, where the created index depends on which DIE is
read first.  This can cause index-file.exp failures.

This patch restores the old approach, avoiding this problem.  I also
applied a couple of small cleanups to the class.  And, I removed the
old complaint from the "ingestion" function, as this was not
necessary.
---
 gdb/dwarf2/index-write.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)
  

Patch

diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c
index a3204b01699a9141e353f25d5c43faa47596a716..1a26bd857bb0d7253f872c776a436bd77145ba57 100644
--- a/gdb/dwarf2/index-write.c
+++ b/gdb/dwarf2/index-write.c
@@ -763,7 +763,7 @@  class debug_names
 		   });
 
 	m_name_table_string_offs.push_back_reorder
-	  (m_debugstrlookup.lookup (name.c_str ())); /* ??? */
+	  (m_debugstrlookup.lookup (name.c_str ()));
 	m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
 
 	for (const cooked_index_entry *entry : these_entries)
@@ -928,11 +928,21 @@  class debug_names
   {
   public:
 
-    /* Object constructor to be called for current DWARF2_PER_BFD.  */
-    debug_str_lookup (dwarf2_per_bfd *per_bfd)
-      : m_abfd (per_bfd->obfd),
-	m_per_bfd (per_bfd)
+    /* Object constructor to be called for current DWARF2_PER_BFD.
+       All .debug_str section strings are automatically stored.  */
+    explicit debug_str_lookup (dwarf2_per_bfd *per_bfd)
+      : m_per_bfd (per_bfd)
     {
+      gdb_assert (per_bfd->str.readin);
+      const gdb_byte *data = per_bfd->str.buffer;
+      if (data == nullptr)
+	return;
+      while (data < per_bfd->str.buffer + per_bfd->str.size)
+	{
+	  const char *const s = reinterpret_cast<const char *> (data);
+	  m_str_table.emplace (c_str_view (s), data - per_bfd->str.buffer);
+	  data += strlen (s) + 1;
+	}
     }
 
     /* Return offset of symbol name S in the .debug_str section.  Add
@@ -940,13 +950,6 @@  class debug_names
        yet.  */
     size_t lookup (const char *s)
     {
-      /* Most strings will have come from the string table
-	 already.  */
-      const gdb_byte *b = (const gdb_byte *) s;
-      if (b >= m_per_bfd->str.buffer
-	  && b < m_per_bfd->str.buffer + m_per_bfd->str.size)
-	return b - m_per_bfd->str.buffer;
-
       const auto it = m_str_table.find (c_str_view (s));
       if (it != m_str_table.end ())
 	return it->second;
@@ -965,7 +968,6 @@  class debug_names
 
   private:
     gdb::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
-    bfd *const m_abfd;
     dwarf2_per_bfd *m_per_bfd;
 
     /* Data to add at the end of .debug_str for new needed symbol names.  */