[PATCH v2 7/9] gdb: make expanded_symbols_functions hold compunit symtabs

simon.marchi
2026-02-17T20:03:03Z

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
---
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 050608795f93..d885fe33fa84 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 c088d74f7e59..885390ccb124 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 21e8667a7af6..5fa3869316c3 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,
--
2.53.0