Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@polymtl.ca>
Subject: [PATCH 09/11] gdb: add objfile -> solib backlink
Date: Tue,  9 Dec 2025 14:32:13 -0500	[thread overview]
Message-ID: <20251209193610.296085-10-simon.marchi@efficios.com> (raw)
In-Reply-To: <20251209193610.296085-1-simon.marchi@efficios.com>

From: Simon Marchi <simon.marchi@polymtl.ca>

The following "multiple solib_ops" patch will require accessing the
solib(s) related to an objfile fairly often.  To make that easier, add
an objfile to solib backlink.

However, I learned that there may be more than one solib linked to one
objfile, so this backlink is actually a vector.

Rename find_solib_for_objfile to find_one_solib_for_objfile to reflect
that this function returns just one of possible multiple solibs for the
given objfile (if that ever matters).  To be clear, this doesn't change
its behavior, this is what it was doing before this patch.

Change-Id: I427d19407fdbc274b1790e0cae3004ee5fdbea72
---
 gdb/objfiles.h   | 23 +++++++++++++++++++++++
 gdb/solib-svr4.c | 16 ++++++++--------
 gdb/solib.c      | 13 +++++++++----
 3 files changed, 40 insertions(+), 12 deletions(-)

diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index f65f3bde930d..9e4a800dda9e 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -469,6 +469,25 @@ struct objfile : intrusive_list_node<objfile>
   /* Return the program space associated with this objfile.  */
   program_space *pspace () { return m_pspace; }
 
+  /* Return the list of struct solib associated to this objfile.  */
+  std::vector<solib *> &solibs () { return m_solibs; }
+
+  /* Add SOLIB to the list of solibs associated to this objfile.  */
+  void add_solib (solib &solib)
+  {
+    gdb_assert (std::find (m_solibs.begin (), m_solibs.end (), &solib)
+		== m_solibs.end ());
+    m_solibs.emplace_back (&solib);
+  }
+
+  /* Remove SOLIB from the list of solibs associated to this objfile.  */
+  void remove_solib (solib &solib)
+  {
+    auto it = std::find (m_solibs.begin (), m_solibs.end (), &solib);
+    gdb_assert (it != m_solibs.end ());
+    m_solibs.erase (it);
+  }
+
   using compunit_symtab_iterator
     = owning_intrusive_list<compunit_symtab>::iterator;
   using compunit_symtab_range = iterator_range<compunit_symtab_iterator>;
@@ -715,6 +734,10 @@ struct objfile : intrusive_list_node<objfile>
 
   program_space *m_pspace;
 
+  /* The solibs associated to this objfile.  This is a 1 objfile to N solibs
+     relationship (see solib.c for details).  */
+  std::vector<solib *> m_solibs;
+
 public:
   /* The object file's BFD.  Can be null if the objfile contains only
      minimal symbols (e.g. the run time common symbols for SunOS4) or
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 7fd067ff38ab..f0f13c16bb03 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -3595,10 +3595,10 @@ lp64_svr4_solib_ops::fetch_link_map_offsets () const
 }
 \f
 
-/* Return the DSO matching OBJFILE or nullptr if none can be found.  */
+/* Return one solib matching OBJFILE or nullptr if none can be found.  */
 
-static const solib *
-find_solib_for_objfile (struct objfile *objfile)
+static solib *
+find_one_solib_for_objfile (struct objfile *objfile)
 {
   if (objfile == nullptr)
     return nullptr;
@@ -3608,9 +3608,9 @@ find_solib_for_objfile (struct objfile *objfile)
   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;
+  if (const auto &solibs = objfile->solibs ();
+      !solibs.empty ())
+    return solibs.front ();
 
   return nullptr;
 }
@@ -3679,7 +3679,7 @@ svr4_solib_ops::iterate_over_objfiles_in_search_order
      r_debug object, defaulting to the initial namespace.  */
   svr4_info *info = get_svr4_info (current_program_space);
   CORE_ADDR default_debug_base = this->default_debug_base (info);
-  const solib *curr_solib = find_solib_for_objfile (current_objfile);
+  const solib *curr_solib = find_one_solib_for_objfile (current_objfile);
   CORE_ADDR debug_base = find_debug_base_for_solib (curr_solib);
   if (debug_base == 0)
     debug_base = default_debug_base;
@@ -3694,7 +3694,7 @@ svr4_solib_ops::iterate_over_objfiles_in_search_order
 	 If we fail, e.g. for manually added symbol files or for the main
 	 executable, we assume that they were added to the initial
 	 namespace.  */
-      const solib *solib = find_solib_for_objfile (&objfile);
+      const solib *solib = find_one_solib_for_objfile (&objfile);
       CORE_ADDR solib_base = find_debug_base_for_solib (solib);
       if (solib_base == 0)
 	solib_base = default_debug_base;
diff --git a/gdb/solib.c b/gdb/solib.c
index 9f6cc26b63b4..33dd4b2b1a32 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -662,6 +662,7 @@ solib_read_symbols (solib &so, symfile_add_flags flags)
 	  so.objfile->addr_low = so.addr_low;
 	}
 
+      so.objfile->add_solib (so);
       so.symbols_loaded = true;
     }
   catch (const gdb_exception_error &e)
@@ -732,10 +733,14 @@ remove_solib (program_space *pspace,
   notify_solib_unloaded (pspace, *solib_it, still_in_use, false);
 
   /* Unless the user loaded it explicitly, free SO's objfile.  */
-  if (solib_it->objfile != nullptr
-      && !(solib_it->objfile->flags & OBJF_USERLOADED)
-      && !still_in_use)
-    solib_it->objfile->unlink ();
+  if (solib_it->objfile != nullptr)
+    {
+      /* Remove the objfile -> solib backlink.  */
+      solib_it->objfile->remove_solib (*solib_it);
+
+      if (!(solib_it->objfile->flags & OBJF_USERLOADED) && !still_in_use)
+	solib_it->objfile->unlink ();
+    }
 
   pspace->deleted_solibs.push_back (solib_it->name);
 
-- 
2.52.0


  parent reply	other threads:[~2025-12-09 19:38 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-09 19:32 [PATCH 00/11] Multiple solib_ops in a program_space Simon Marchi
2025-12-09 19:32 ` [PATCH 01/11] gdb/solib-rocm: assert that host ops isn't rocm_solib_ops Simon Marchi
2026-05-07 17:56   ` [PATCH 1/11] " Lancelot SIX
2026-05-08  1:37     ` Simon Marchi
2026-05-08 10:36       ` Lancelot SIX
2026-06-08 18:36         ` Simon Marchi
2025-12-09 19:32 ` [PATCH 02/11] gdb/solib: use early return in solib_read_symbols Simon Marchi
2026-04-28 16:16   ` Tom Tromey
2026-05-08  1:44     ` Simon Marchi
2025-12-09 19:32 ` [PATCH 03/11] gdb/solib-rocm: pass reference to cache to rocm_code_object_stream_file Simon Marchi
2026-05-07 18:20   ` [PATCH 3/11] " Lancelot SIX
2026-05-08  1:48     ` Simon Marchi
2025-12-09 19:32 ` [PATCH 04/11] gdb/solib-rocm: add cached_fd to manage cached fd lifetime Simon Marchi
2026-05-07 20:17   ` [PATCH 4/11] " Lancelot SIX
2026-05-08  1:52     ` Simon Marchi
2025-12-09 19:32 ` [PATCH 05/11] gdb: de-constify some methods of solib_ops Simon Marchi
2025-12-09 19:32 ` [PATCH 06/11] gdb/solib-rocm: move per-inferior data to rocm_solib_ops Simon Marchi
2026-04-28 16:19   ` Tom Tromey
2026-05-08  2:02     ` Simon Marchi
2025-12-09 19:32 ` [PATCH 07/11] gdb/solib-rocm: save inferior in rocm_solib_ops Simon Marchi
     [not found]   ` <87a4unm59w.fsf@tromey.com>
2026-05-08  2:10     ` Simon Marchi
2025-12-09 19:32 ` [PATCH 08/11] gdb/solib: add remove_solib function Simon Marchi
2026-04-28 16:23   ` Tom Tromey
2025-12-09 19:32 ` Simon Marchi [this message]
2026-04-28 16:28   ` [PATCH 09/11] gdb: add objfile -> solib backlink Tom Tromey
2026-05-08  2:25     ` Simon Marchi
2025-12-09 19:32 ` [PATCH 10/11] gdb: change default objfile iteration order to start with current objfile Simon Marchi
2026-04-28 16:42   ` Tom Tromey
2026-05-08  2:40     ` Simon Marchi
2025-12-09 19:32 ` [PATCH 11/11] gdb: multiple solib_ops per program space Simon Marchi
2026-04-28 17:27   ` Tom Tromey
2026-05-08  2:52     ` Simon Marchi
2026-04-28 16:00 ` [PATCH 00/11] Multiple solib_ops in a program_space Simon Marchi
2026-04-28 17:28   ` Tom Tromey
2026-05-08  2:40     ` Simon Marchi

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=20251209193610.296085-10-simon.marchi@efficios.com \
    --to=simon.marchi@efficios.com \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@polymtl.ca \
    /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