From: Guinevere Larsen <guinevere@redhat.com>
To: gdb-patches@sourceware.org
Cc: Guinevere Larsen <guinevere@redhat.com>
Subject: [PATCH v7 2/5] gdb: make lookup_minimal_symbol_linkage work with linker namespaces
Date: Mon, 1 Jun 2026 16:22:48 -0300 [thread overview]
Message-ID: <20260601192251.60958-3-guinevere@redhat.com> (raw)
In-Reply-To: <20260601192251.60958-1-guinevere@redhat.com>
When a symbol found by GDB may have copy relocation, GDB calculates the
address of the symbol by checking all the objfiles in the current
program space for another instance of the same symbol, and returns the
address of the first instance found.
This works well when linker namespaces are not involved, but fails when
a symbol is present in more than one namespace. That's because copy
relocations respect linker namespace boundaries, and so if we're trying
to find the symbol in namespace N, the symbol from a different namespace
M may be returned, if the SO containing the symbol was loaded in M
before N.
To make the search work correctly, lookup_minimal_symbol_linkage would
need to also respect linker namespace boundaries. However, to avoid
leaking solib knowledge to minsyms, and because of how little
information is passed to the function, this commit instead makes the
function take a vector of objfiles to search, in place of the program
space. This makes it so symbol::get_maybe_copied_address (and the
equivalent of minimal_symbol) need to request the objfiles for the
correct namespace, since they have enough context to figure out the
namespace. Creating the vector is left as a function in solib.c
Since minimal_symbol::get_maybe_copied_address only searches main_file
objfiles, if we can guarantee that only one is loaded for each program
space, we could avoid the work of constructing an std::vector that will
be mostly ignored. However, I couldn't convince myself that that is the
case, so I decided to keep the behavior exactly as is.
Ideally, lookup_minimal_symbol_linkage would receive an iterator instead
of a vector, but that would either require that we either make a generic
iterator to manage program_space::objfiles_range iterator and a vector
iterator, or we find a way to filter the objfiles based on the linker
namespace, but this has quadratic complexity, over the linear complexity
of creating a new vector, to avoid even worse performance.
Finally, as some minor refactoring, find_solib_for_objfile is moved to
solib.c to simplify looking for which namespace contains the objfile
where we found the symbol the user was looking for. As Andrew noted,
this function is flawed, but as the bug was already here and this
refactor only exposes it further, so the commit adds comment explaining
the issue.
---
gdb/dwarf2/ada-imported.c | 8 +++-
gdb/minsyms.c | 15 ++++---
gdb/minsyms.h | 13 +++---
gdb/solib-svr4.c | 20 ----------
gdb/solib.c | 84 +++++++++++++++++++++++++++++++++++++++
gdb/solib.h | 25 ++++++++++++
gdb/symtab.c | 13 +++++-
7 files changed, 143 insertions(+), 35 deletions(-)
diff --git a/gdb/dwarf2/ada-imported.c b/gdb/dwarf2/ada-imported.c
index 7cdaffef886..31782803c73 100644
--- a/gdb/dwarf2/ada-imported.c
+++ b/gdb/dwarf2/ada-imported.c
@@ -36,9 +36,13 @@ static struct value *
ada_imported_read_variable (struct symbol *symbol, const frame_info_ptr &frame)
{
const char *name = get_imported_name (symbol);
+
+ std::vector<objfile *> objfiles_to_search;
+ for (objfile &objf : symbol->objfile ()->pspace ()->objfiles ())
+ objfiles_to_search.push_back (&objf);
+
bound_minimal_symbol minsym
- = lookup_minimal_symbol_linkage (symbol->objfile ()->pspace (), name,
- true, false);
+ = lookup_minimal_symbol_linkage (objfiles_to_search, name, true, false);
if (minsym.minsym == nullptr)
error (_("could not find imported name %s"), name);
return value_at (symbol->type (), minsym.value_address ());
diff --git a/gdb/minsyms.c b/gdb/minsyms.c
index 4eafc789047..b3538a9284a 100644
--- a/gdb/minsyms.c
+++ b/gdb/minsyms.c
@@ -54,6 +54,7 @@
#include "gdbsupport/cxx-thread.h"
#include "gdbsupport/parallel-for.h"
#include "inferior.h"
+#include "solib.h"
/* Return true if MINSYM is a cold clone symbol.
Recognize f.i. these symbols (mangled/demangled):
@@ -588,19 +589,21 @@ lookup_minimal_symbol_linkage (const char *name, struct objfile *objf,
/* See minsyms.h. */
bound_minimal_symbol
-lookup_minimal_symbol_linkage (program_space *pspace, const char *name,
- bool match_static_type, bool only_main)
+lookup_minimal_symbol_linkage (gdb::array_view<objfile *> objfiles_to_search,
+ const char *name, bool match_static_type,
+ bool only_main)
{
- for (objfile &objfile : pspace->objfiles ())
+ for (objfile *objfile : objfiles_to_search)
{
- if (objfile.separate_debug_objfile_backlink != nullptr)
+ if (objfile == nullptr
+ || objfile->separate_debug_objfile_backlink != nullptr)
continue;
- if (only_main && (objfile.flags & OBJF_MAINLINE) == 0)
+ if (only_main && (objfile->flags & OBJF_MAINLINE) == 0)
continue;
bound_minimal_symbol minsym
- = lookup_minimal_symbol_linkage (name, &objfile, match_static_type);
+ = lookup_minimal_symbol_linkage (name, objfile, match_static_type);
if (minsym.minsym != nullptr)
return minsym;
}
diff --git a/gdb/minsyms.h b/gdb/minsyms.h
index c44d5b20aec..03d05b6e328 100644
--- a/gdb/minsyms.h
+++ b/gdb/minsyms.h
@@ -215,13 +215,16 @@ extern bound_minimal_symbol lookup_minimal_symbol_linkage
(const char *name, struct objfile *objf, bool match_static_type)
ATTRIBUTE_NONNULL (1) ATTRIBUTE_NONNULL (2);
-/* A variant of lookup_minimal_symbol_linkage that iterates over all
- objfiles of PSPACE. If ONLY_MAIN is true, then only an objfile with
- OBJF_MAINLINE will be considered. */
+/* A variant of lookup_minimal_symbol_linkage that iterates over the
+ objfiles in OBJFILES_TO_SEARCH. If ONLY_MAIN is true, then only an
+ objfile with OBJF_MAINLINE will be considered. This function should
+ receive a program_space::objfile_ranges instead, but we can't include
+ progspace.h here, nor can we do forward declarations of nested types,
+ so std::vector will waste a bit of memory to work around that issue. */
extern bound_minimal_symbol lookup_minimal_symbol_linkage
- (program_space *pspace, const char *name, bool match_static_type,
- bool only_main) ATTRIBUTE_NONNULL (1);
+ (gdb::array_view<objfile *> objfiles_to_search, const char *name,
+ bool match_static_type, bool only_main);
/* Look through all the current minimal symbol tables and find the
first minimal symbol that matches NAME and PC. If OBJF is non-NULL,
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 8e3de4d3ea1..394b1237096 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -3592,26 +3592,6 @@ lp64_svr4_solib_ops::fetch_link_map_offsets () const
}
\f
-/* Return the DSO matching OBJFILE or nullptr if none can be found. */
-
-static const solib *
-find_solib_for_objfile (struct objfile *objfile)
-{
- if (objfile == nullptr)
- return nullptr;
-
- /* If OBJFILE is a separate debug object file, look for the original
- object file. */
- if (objfile->separate_debug_objfile_backlink != nullptr)
- objfile = objfile->separate_debug_objfile_backlink;
-
- for (const solib &so : current_program_space->solibs ())
- if (so.objfile == objfile)
- return &so;
-
- return nullptr;
-}
-
/* Return the address of the r_debug object for the namespace containing
SOLIB or zero if it cannot be found. This may happen when symbol files
are added manually, for example, or with the main executable.
diff --git a/gdb/solib.c b/gdb/solib.c
index dbb69ef7678..ca68b5c93c1 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -1795,6 +1795,90 @@ solib_linker_namespace_count (program_space *pspace)
/* See solib.h. */
+const solib *
+find_solib_for_objfile (struct objfile *objfile)
+{
+ if (objfile == nullptr)
+ return nullptr;
+
+ /* If OBJFILE is a separate debug object file, look for the original
+ object file. */
+ if (objfile->separate_debug_objfile_backlink != nullptr)
+ objfile = objfile->separate_debug_objfile_backlink;
+
+ for (const solib &so : objfile->pspace ()->solibs ())
+ if (so.objfile == objfile)
+ return &so;
+
+ return nullptr;
+}
+
+/* See solib.h. */
+
+std::vector<objfile *>
+get_objfiles_in_linker_namespace (int nsid, program_space *pspace)
+{
+ std::vector<objfile *> objfiles_in_ns;
+ const solib_ops *ops = pspace->solib_ops ();
+
+ gdb_assert (ops->supports_namespaces ());
+
+ /* If we're looking at the default namespace, we also need
+ to add the mainline objfiles by hand, since there is no
+ solib associated with those files. We add them first
+ because if we're searching for copy relocations, they
+ should be in the main file, so we'll find it faster. */
+ if (nsid == 0)
+ for (objfile &objf : pspace->objfiles ())
+ if ((objf.flags & OBJF_MAINLINE) != 0)
+ objfiles_in_ns.push_back (&objf);
+
+ std::vector<const solib *> solibs = ops->get_solibs_in_ns (nsid);
+ /* Reserve for efficiency. */
+ objfiles_in_ns.reserve (solibs.size () + objfiles_in_ns.size ());
+ for (const solib *so : solibs)
+ objfiles_in_ns.push_back (so->objfile);
+
+ return objfiles_in_ns;
+}
+
+/* See solib.h. */
+
+std::vector<objfile *>
+get_objfiles_in_linker_namespace (objfile *objfile)
+{
+ program_space *pspace = objfile->pspace ();
+ const solib_ops *ops = pspace->solib_ops ();
+ const solib *so = find_solib_for_objfile (objfile);
+
+ /* If the inferior hasn't started yet, the solib_ops won't be
+ set for the program space. Since namespaces only make sense
+ when the inferior has already created some, we can just skip
+ it here. */
+ if (ops != nullptr && ops->supports_namespaces ()
+ /* If we're searching for a symbol from the linker, we'll reach here
+ before having any namespaces. Return all objfiles since the
+ boundaries haven't been setup yet. */
+ && ops->num_active_namespaces () > 0
+ /* When trying to load libthread_db, we can search for a symbol in an
+ objfile with no associated solib. In that case, again, we should
+ return all objfiles. */
+ && so != nullptr)
+ {
+ return get_objfiles_in_linker_namespace (ops->find_solib_ns (*so),
+ pspace);
+ }
+
+ /* If any of the previous conditions isn't satisfied, we return
+ the full list of objfiles in the inferior. */
+ std::vector<struct objfile *> found_objfiles;
+ for (struct objfile &objf : objfile->pspace ()->objfiles ())
+ found_objfiles.push_back (&objf);
+
+ return found_objfiles;
+}
+
+/* See solib.h. */
int
get_current_linker_namespace ()
{
diff --git a/gdb/solib.h b/gdb/solib.h
index e52eef53df8..493a2086f24 100644
--- a/gdb/solib.h
+++ b/gdb/solib.h
@@ -20,6 +20,9 @@
#ifndef GDB_SOLIB_H
#define GDB_SOLIB_H
+/* Forward decl's for prototypes */
+struct objfile;
+
#include "gdb_bfd.h"
#include "gdbsupport/function-view.h"
#include "gdbsupport/intrusive_list.h"
@@ -416,4 +419,26 @@ extern int solib_linker_namespace_count (program_space *pspace);
int get_current_linker_namespace ();
+/* Return a vector with pointers of all objfiles in the namespace
+ NSID. This version assumes that the inferior supports namespaces. */
+
+std::vector<objfile *> get_objfiles_in_linker_namespace
+ (int nsid, program_space *pspace);
+
+/* Return a vector with pointers of all objfiles in the same namespace
+ as OBJFILE. If the inferior doesn't support namespaces, return all
+ objfiles in the program_space. */
+
+std::vector<objfile *> get_objfiles_in_linker_namespace (objfile *objfile);
+
+/* Return the DSO matching OBJFILE or nullptr if none can be found.
+ NOTE: This function has the flawed assumption that the objfile <-> solib
+ relationship as a 1 to 1, but it is in fact 1 to many. For now, this
+ function is only used to find an objfile's namespace, meaning the bug
+ will only return a possibly incorrect value when the objfile belongs
+ to something present in multiple namespaces, which is only the linker
+ namespace as far as I know. Keep this in mind, if using this function. */
+
+const solib *find_solib_for_objfile (struct objfile *objfile);
+
#endif /* GDB_SOLIB_H */
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 3c10e1fd750..95287ab42f7 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -74,6 +74,7 @@
#include "gdbsupport/common-utils.h"
#include <optional>
#include "gdbsupport/unordered_set.h"
+#include "solib.h"
/* Forward declarations for local functions. */
@@ -6598,8 +6599,12 @@ symbol::get_maybe_copied_address () const
gdb_assert (this->loc_class () == LOC_STATIC);
const char *linkage_name = this->linkage_name ();
+
+ std::vector<struct objfile *> objfiles_to_search
+ (get_objfiles_in_linker_namespace (this->objfile ()));
+
bound_minimal_symbol minsym
- = lookup_minimal_symbol_linkage (this->objfile ()->pspace (), linkage_name,
+ = lookup_minimal_symbol_linkage (objfiles_to_search, linkage_name,
false, false);
if (minsym.minsym != nullptr)
return minsym.value_address ();
@@ -6638,8 +6643,12 @@ minimal_symbol::get_maybe_copied_address (objfile *objf) const
gdb_assert ((objf->flags & OBJF_MAINLINE) == 0);
const char *linkage_name = this->linkage_name ();
+
+ std::vector<struct objfile *> objfiles_to_search
+ (get_objfiles_in_linker_namespace (objf));
+
bound_minimal_symbol found
- = lookup_minimal_symbol_linkage (objf->pspace (), linkage_name,
+ = lookup_minimal_symbol_linkage (objfiles_to_search, linkage_name,
false, true);
if (found.minsym != nullptr)
return found.value_address ();
--
2.54.0
next prev parent reply other threads:[~2026-06-01 19:25 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-01 19:22 [PATCH v7 0/5] Introduce syntax for linker-namespace specific symbols Guinevere Larsen
2026-06-01 19:22 ` [PATCH v7 1/5] gdb: Create helper function for the current namespace Guinevere Larsen
2026-06-01 19:22 ` Guinevere Larsen [this message]
2026-06-01 19:22 ` [PATCH v7 3/5] gdb: Make the parser recognize the [[N]] syntax for variables Guinevere Larsen
2026-06-01 19:22 ` [PATCH v7 4/5] gdb: extend the [[N]]::foo syntax for files Guinevere Larsen
2026-06-01 19:22 ` [PATCH v7 5/5] gdb: prefer symtabs for the current linker namespace Guinevere Larsen
2026-06-03 18:26 ` Guinevere Larsen
2026-06-03 19:16 ` [PATCH v7 5/5] Fixed " Guinevere Larsen
2026-06-22 20:35 ` [PING][PATCH v7 0/5] Introduce syntax for linker-namespace specific symbols Guinevere Larsen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260601192251.60958-3-guinevere@redhat.com \
--to=guinevere@redhat.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox