[binutils-gdb] gdb: make expanded_symbols_functions hold compunit symtabs

Simon Marchi via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=dc1ddeda3322aa50da50831a4028ead18ac50401

commit dc1ddeda3322aa50da50831a4028ead18ac50401
Author: Simon Marchi <[email protected]>
Date:   Fri Feb 27 22:51:54 2026 -0500

    gdb: make expanded_symbols_functions hold compunit symtabs
    
    Change the expanded_symbols_functions quick functions type to hold and
    use a list of compunit symtab to search.
    
    Currently, an expanded_symbols_functions instance will search all the
    compunits in the objfile.  This is not efficient if an
    expanded_symbols_functions instance exists alongside another quick
    functions object in an objfile, as the compunits belonging to that other
    object will be unnecessarily searched.
    
    And at worst, I think it could be a source of subtle bugs.  For
    instance, if the order of quick functions determine the order in which
    we want the search to happen (the comment in elf_symfile_read suggests
    this is the case), then having expanded_symbols_functions search the
    compunits from other quick functions objects would not respect that
    ordering.
    
    Update the expanded_symbols_functions constructor to accept a vector of
    compunits and store this vector in a field.  Update
    expanded_symbols_functions methods to use that vector instead of the
    objfile's compunits.
    
    Right now the sole user of expanded_symbols_functions is JIT.  Update it
    to keep a vector of compunits as they are finalized, and pass this
    vector to the expanded_symbols_functions object.
    
    Change-Id: Idf8de18b25fd3f71766166d6f420184af3c26b7e
    Approved-By: Tom Tromey <[email protected]>

Diff:
---
 gdb/expanded-symbol.c | 16 ++++++++--------
 gdb/expanded-symbol.h |  8 ++++++++
 gdb/jit.c             | 16 ++++++++++++----
 3 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/gdb/expanded-symbol.c b/gdb/expanded-symbol.c
index 050608795f9..d885fe33fa8 100644
--- a/gdb/expanded-symbol.c
+++ b/gdb/expanded-symbol.c
@@ -28,10 +28,10 @@
 symtab *
 expanded_symbols_functions::find_last_source_symtab (objfile *objfile)
 {
-  if (objfile->compunit_symtabs.empty ())
+  if (m_compunit_symtabs.empty ())
     return nullptr;
   else
-    return objfile->compunit_symtabs.back ().primary_filetab ();
+    return m_compunit_symtabs.back ()->primary_filetab ();
 }
 
 /* See expanded-symbol.h.  */
@@ -61,15 +61,15 @@ expanded_symbols_functions::search
   /* This invariant is documented in quick-functions.h.  */
   gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
 
-  for (compunit_symtab &cu : objfile->compunits ())
+  for (compunit_symtab *cu : m_compunit_symtabs)
     {
-      if (lang_matcher != nullptr && !lang_matcher (cu.language ()))
+      if (lang_matcher != nullptr && !lang_matcher (cu->language ()))
 	continue;
 
       if (file_matcher != nullptr)
 	{
 	  bool matched = false;
-	  for (auto st : cu.filetabs ())
+	  for (auto st : cu->filetabs ())
 	    {
 	      if (file_matcher (st->filename (), false))
 		{
@@ -92,7 +92,7 @@ expanded_symbols_functions::search
 	 consult lookup_name and symbol_matcher (if any).  This should be
 	 okay since i) all symtabs are already expanded and ii) listeners
 	 iterate over matching symbols themselves.  */
-      if (listener != nullptr && !listener (&cu))
+      if (listener != nullptr && !listener (cu))
 	return false;
     }
   return true;
@@ -104,9 +104,9 @@ symbol *
 expanded_symbols_functions::find_symbol_by_address (objfile *objfile,
 						    CORE_ADDR address)
 {
-  for (compunit_symtab &symtab : objfile->compunits ())
+  for (compunit_symtab *symtab : m_compunit_symtabs)
     {
-      symbol *sym = symtab.symbol_at_address (address);
+      symbol *sym = symtab->symbol_at_address (address);
       if (sym != nullptr)
 	return sym;
     }
diff --git a/gdb/expanded-symbol.h b/gdb/expanded-symbol.h
index c088d74f7e5..885390ccb12 100644
--- a/gdb/expanded-symbol.h
+++ b/gdb/expanded-symbol.h
@@ -29,6 +29,11 @@
 
 struct expanded_symbols_functions : public quick_symbol_functions
 {
+  explicit expanded_symbols_functions
+    (std::vector<compunit_symtab *> compunit_symtabs)
+    : m_compunit_symtabs (std::move (compunit_symtabs))
+  {}
+
   bool has_symbols (objfile *objfile) override
   {
     return true;
@@ -86,6 +91,9 @@ struct expanded_symbols_functions : public quick_symbol_functions
 			     bool need_fullname) override
   {
   }
+
+private:
+  std::vector<compunit_symtab *> m_compunit_symtabs;
 };
 
 
diff --git a/gdb/jit.c b/gdb/jit.c
index 21e8667a7af..5fa3869316c 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -513,9 +513,11 @@ jit_symtab_close_impl (struct gdb_symbol_callbacks *cb,
      ABI).  */
 }
 
-/* Transform STAB to a proper symtab, and add it it OBJFILE.  */
+/* Transform STAB to a proper symtab, and add it it OBJFILE.
 
-static void
+   Return the created symtab.  */
+
+static compunit_symtab *
 finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
 {
   CORE_ADDR begin, end;
@@ -653,6 +655,8 @@ finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
 
   /* Move just built blockvector over to CUST.  */
   cust->set_blockvector (std::move (bv));
+
+  return cust;
 }
 
 /* Called when closing a gdb_objfile.  Converts OBJ to a proper
@@ -673,10 +677,14 @@ jit_object_close_impl (struct gdb_symbol_callbacks *cb,
   objfile->section_offsets.push_back (0);
   objfile->sect_index_text = 0;
   objfile->per_bfd->gdbarch = priv_data->gdbarch;
-  objfile->qf.emplace_front (new expanded_symbols_functions);
+
+  std::vector<compunit_symtab *> compunit_symtabs;
 
   for (gdb_symtab &symtab : obj->symtabs)
-    finalize_symtab (&symtab, objfile);
+    compunit_symtabs.emplace_back (finalize_symtab (&symtab, objfile));
+
+  objfile->qf.emplace_front (std::make_unique<expanded_symbols_functions>
+			     (std::move (compunit_symtabs)));
 
   add_objfile_entry (objfile, priv_data->entry_addr,
 		     priv_data->entry.symfile_addr,
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.