public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 00/24] C++ification of struct so_list
@ 2023-10-10 20:39 Simon Marchi
  2023-10-10 20:39 ` [PATCH 01/24] gdb: remove empty clear_solib functions Simon Marchi
                   ` (25 more replies)
  0 siblings, 26 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:39 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

This series modernizes the struct so_list area by C++ifying it a little
bit and replacing the manual linked list implementation with
intrusive_list.  It also contains a few other cleanups written along the
way.

Simon Marchi (24):
  gdb: remove empty clear_solib functions
  gdb: add program_space parameter to target_so_ops::clear_solib
  gdb: make interps_notify work with references
  gdb: replace some so_list parameters to use references
  gdbsupport: use "reference" and "pointer" type aliases in
    intrusive_list
  gdbsupport: make intrusive_list's disposer accept a reference
  gdb: make get_cbfd_soname_build_id static
  gdb: allocate so_list with new, deallocate with delete
  gdb: rename lm_info_base to lm_info
  gdb: remove target_so_ops::free_so
  gdb: use gdb::checked_static_cast when casting lm_info
  gdb: make solib-svr4 not use so_list internally
  gdb: make solib-rocm not use so_list internally
  gdb: remove lm_info_vector typedef
  gdb: make so_list::lm_info a unique_ptr
  gdb: make clear_so a method of struct so_list
  gdb: remove target_section_table typedef
  gdb: make so_list::sections not a pointer
  gdb: make so_list::abfd a gdb_bfd_ref_ptr
  gdb: make so_list::{so_original_name,so_name} std::strings
  gdb: link so_list using intrusive_list
  gdb: don't call so_list::clear in free_so
  gdb: remove free_so function
  gdb: rename struct so_list to so

 gdb/bfd-target.c                         |   6 +-
 gdb/break-catch-load.c                   |   4 +-
 gdb/breakpoint.c                         |   6 +-
 gdb/bsd-uthread.c                        |  18 +-
 gdb/corelow.c                            |   7 +-
 gdb/exec.c                               |  24 +-
 gdb/exec.h                               |   6 +-
 gdb/hppa-tdep.c                          |   2 +-
 gdb/hppa-tdep.h                          |   4 +-
 gdb/inferior.c                           |  10 +-
 gdb/interps.c                            |  10 +-
 gdb/interps.h                            |  10 +-
 gdb/maint.c                              |   2 +-
 gdb/mi/mi-cmd-file.c                     |   6 +-
 gdb/mi/mi-interp.c                       |  26 +-
 gdb/mi/mi-interp.h                       |   6 +-
 gdb/nto-tdep.c                           |   6 +-
 gdb/nto-tdep.h                           |   3 +-
 gdb/observable.h                         |   7 +-
 gdb/progspace.c                          |   4 +-
 gdb/progspace.h                          |  26 +-
 gdb/record-full.c                        |   2 +-
 gdb/remote.c                             |   3 +-
 gdb/solib-aix.c                          |  69 +---
 gdb/solib-darwin.c                       |  84 ++---
 gdb/solib-dsbt.c                         |  73 ++--
 gdb/solib-frv.c                          |  58 ++--
 gdb/solib-rocm.c                         | 122 +++----
 gdb/solib-svr4.c                         | 421 ++++++++---------------
 gdb/solib-svr4.h                         |   4 +-
 gdb/solib-target.c                       | 121 +++----
 gdb/solib.c                              | 309 ++++++++---------
 gdb/solib.h                              |  13 +-
 gdb/solist.h                             |  77 ++---
 gdb/symfile.c                            |   2 +-
 gdb/symfile.h                            |   2 +-
 gdb/target-debug.h                       |   4 +-
 gdb/target-delegates.c                   |  14 +-
 gdb/target-section.h                     |   6 +-
 gdb/target.c                             |  10 +-
 gdb/target.h                             |   6 +-
 gdb/unittests/intrusive_list-selftests.c |   4 +-
 gdbsupport/intrusive_list.h              |  30 +-
 43 files changed, 658 insertions(+), 969 deletions(-)


base-commit: 635b2dd919b8c58e164b77c396041935fca1d66a
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 01/24] gdb: remove empty clear_solib functions
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
@ 2023-10-10 20:39 ` Simon Marchi
  2023-10-10 20:39 ` [PATCH 02/24] gdb: add program_space parameter to target_so_ops::clear_solib Simon Marchi
                   ` (24 subsequent siblings)
  25 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:39 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Make the target_so_ops::clear_solib method optional, remove two empty
implementations.

Change-Id: Ifda297d50c74327d337091c58cdb5b3b60382591
---
 gdb/solib-aix.c    | 10 +---------
 gdb/solib-target.c |  8 +-------
 gdb/solib.c        |  6 ++++--
 3 files changed, 6 insertions(+), 18 deletions(-)

diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index fd6b1e7a4543..0b6ad83eed92 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -373,14 +373,6 @@ solib_aix_free_so (struct so_list *so)
   delete li;
 }
 
-/* Implement the "clear_solib" target_so_ops method.  */
-
-static void
-solib_aix_clear_solib (void)
-{
-  /* Nothing needed.  */
-}
-
 /* Compute and return the OBJFILE's section_offset array, using
    the associated loader info (INFO).  */
 
@@ -715,7 +707,7 @@ const struct target_so_ops solib_aix_so_ops =
   solib_aix_relocate_section_addresses,
   solib_aix_free_so,
   nullptr,
-  solib_aix_clear_solib,
+  nullptr,
   solib_aix_solib_create_inferior_hook,
   solib_aix_current_sos,
   solib_aix_open_symbol_file_object,
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index ca9478f2ec30..865235de2995 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -281,12 +281,6 @@ solib_target_solib_create_inferior_hook (int from_tty)
   /* Nothing needed.  */
 }
 
-static void
-solib_target_clear_solib (void)
-{
-  /* Nothing needed.  */
-}
-
 static void
 solib_target_free_so (struct so_list *so)
 {
@@ -440,7 +434,7 @@ const struct target_so_ops solib_target_so_ops =
   solib_target_relocate_section_addresses,
   solib_target_free_so,
   nullptr,
-  solib_target_clear_solib,
+  nullptr,
   solib_target_solib_create_inferior_hook,
   solib_target_current_sos,
   solib_target_open_symbol_file_object,
diff --git a/gdb/solib.c b/gdb/solib.c
index a2a8a031f343..e8211814283a 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -1240,7 +1240,8 @@ clear_solib (void)
       free_so (so);
     }
 
-  ops->clear_solib ();
+  if (ops->clear_solib != nullptr)
+    ops->clear_solib ();
 }
 
 /* Shared library startup support.  When GDB starts up the inferior,
@@ -1409,7 +1410,8 @@ reload_shared_libraries (const char *ignored, int from_tty,
     {
       /* Reset or free private data structures not associated with
 	 so_list entries.  */
-      ops->clear_solib ();
+      if (ops->clear_solib != nullptr)
+	ops->clear_solib ();
 
       /* Remove any previous solib event breakpoint.  This is usually
 	 done in common code, at breakpoint_init_inferior time, but
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 02/24] gdb: add program_space parameter to target_so_ops::clear_solib
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
  2023-10-10 20:39 ` [PATCH 01/24] gdb: remove empty clear_solib functions Simon Marchi
@ 2023-10-10 20:39 ` Simon Marchi
  2023-10-17 14:57   ` Pedro Alves
  2023-10-10 20:39 ` [PATCH 03/24] gdb: make interps_notify work with references Simon Marchi
                   ` (23 subsequent siblings)
  25 siblings, 1 reply; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:39 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

The clear_solib is implicitly meant to clear the resources associated to
the current program space (that's what the solib implementations that
actually support multi-program-space / multi-inferior do).  Make that
explicit by adding a program_space parameter and pass down
current_program_space in call sites.  The implementation of the
clear_solib callbacks is fairly simple, I don't think any of them rely
on global state other than accessing current_program_space.

Change-Id: I8d0cc4db7b4f8db8d7452879c0c62db03269bf46
---
 gdb/solib-darwin.c | 20 +++++++++-----------
 gdb/solib-dsbt.c   | 32 +++++++++++++-------------------
 gdb/solib-frv.c    |  2 +-
 gdb/solib-svr4.c   |  6 ++----
 gdb/solib.c        |  4 ++--
 gdb/solist.h       |  7 ++++---
 6 files changed, 31 insertions(+), 40 deletions(-)

diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index 98c81bb3bdaa..4796315c373a 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -78,16 +78,14 @@ static const registry<program_space>::key<darwin_info>
 /* Get the current darwin data.  If none is found yet, add it now.  This
    function always returns a valid object.  */
 
-static struct darwin_info *
-get_darwin_info (void)
+static darwin_info *
+get_darwin_info (program_space *pspace)
 {
-  struct darwin_info *info;
-
-  info = solib_darwin_pspace_data.get (current_program_space);
-  if (info != NULL)
+  darwin_info *info = solib_darwin_pspace_data.get (pspace);
+  if (info != nullptr)
     return info;
 
-  return solib_darwin_pspace_data.emplace (current_program_space);
+  return solib_darwin_pspace_data.emplace (pspace);
 }
 
 /* Return non-zero if the version in dyld_all_image is known.  */
@@ -225,7 +223,7 @@ darwin_current_sos (void)
   struct so_list *head = NULL;
   struct so_list *tail = NULL;
   int i;
-  struct darwin_info *info = get_darwin_info ();
+  darwin_info *info = get_darwin_info (current_program_space);
 
   /* Be sure image infos are loaded.  */
   darwin_load_image_infos (info);
@@ -506,7 +504,7 @@ darwin_solib_create_inferior_hook (int from_tty)
   if (!target_has_execution ())
     return;
 
-  struct darwin_info *info = get_darwin_info ();
+  darwin_info *info = get_darwin_info (current_program_space);
   CORE_ADDR load_addr;
 
   info->all_image_addr = 0;
@@ -601,9 +599,9 @@ darwin_solib_create_inferior_hook (int from_tty)
 }
 
 static void
-darwin_clear_solib (void)
+darwin_clear_solib (program_space *pspace)
 {
-  struct darwin_info *info = get_darwin_info ();
+  darwin_info *info = get_darwin_info (pspace);
 
   info->all_image_addr = 0;
   info->all_image.version = 0;
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index c29b2b81b113..16fa78a3d8b2 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -169,16 +169,14 @@ static const registry<program_space>::key<dsbt_info> solib_dsbt_pspace_data;
 /* Get the current dsbt data.  If none is found yet, add it now.  This
    function always returns a valid object.  */
 
-static struct dsbt_info *
-get_dsbt_info (void)
+static dsbt_info *
+get_dsbt_info (program_space *pspace)
 {
-  struct dsbt_info *info;
-
-  info = solib_dsbt_pspace_data.get (current_program_space);
-  if (info != NULL)
+  dsbt_info *info = solib_dsbt_pspace_data.get (pspace);
+  if (info != nullptr)
     return info;
 
-  return solib_dsbt_pspace_data.emplace (current_program_space);
+  return solib_dsbt_pspace_data.emplace (pspace);
 }
 
 
@@ -270,9 +268,6 @@ decode_loadmap (const gdb_byte *buf)
   return int_ldmbuf;
 }
 
-
-static struct dsbt_info *get_dsbt_info (void);
-
 /* Interrogate the Linux kernel to find out where the program was loaded.
    There are two load maps; one for the executable and one for the
    interpreter (only in the case of a dynamically linked executable).  */
@@ -280,7 +275,7 @@ static struct dsbt_info *get_dsbt_info (void);
 static void
 dsbt_get_initial_loadmaps (void)
 {
-  struct dsbt_info *info = get_dsbt_info ();
+  dsbt_info *info = get_dsbt_info (current_program_space);
   gdb::optional<gdb::byte_vector> buf
     = target_read_alloc (current_inferior ()->top_target (),
 			 TARGET_OBJECT_FDPIC, "exec");
@@ -442,7 +437,7 @@ lm_base (void)
   struct bound_minimal_symbol got_sym;
   CORE_ADDR addr;
   gdb_byte buf[TIC6X_PTR_SIZE];
-  struct dsbt_info *info = get_dsbt_info ();
+  dsbt_info *info = get_dsbt_info (current_program_space);
 
   /* One of our assumptions is that the main executable has been relocated.
      Bail out if this has not happened.  (Note that post_create_inferior
@@ -524,7 +519,7 @@ dsbt_current_sos (void)
   CORE_ADDR lm_addr;
   struct so_list *sos_head = NULL;
   struct so_list **sos_next_ptr = &sos_head;
-  struct dsbt_info *info = get_dsbt_info ();
+  dsbt_info *info = get_dsbt_info (current_program_space);
 
   /* Make sure that the main executable has been relocated.  This is
      required in order to find the address of the global offset table,
@@ -645,7 +640,7 @@ dsbt_current_sos (void)
 static int
 dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
 {
-  struct dsbt_info *info = get_dsbt_info ();
+  dsbt_info *info = get_dsbt_info (current_program_space);
 
   return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
 	  || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
@@ -675,7 +670,6 @@ static int
 enable_break (void)
 {
   asection *interp_sect;
-  struct dsbt_info *info;
 
   if (current_program_space->exec_bfd () == NULL)
     return 0;
@@ -683,7 +677,7 @@ enable_break (void)
   if (!target_has_execution ())
     return 0;
 
-  info = get_dsbt_info ();
+  dsbt_info *info = get_dsbt_info (current_program_space);
 
   info->interp_text_sect_low = 0;
   info->interp_text_sect_high = 0;
@@ -803,7 +797,7 @@ dsbt_relocate_main_executable (void)
 {
   struct int_elf32_dsbt_loadmap *ldm;
   int changed;
-  struct dsbt_info *info = get_dsbt_info ();
+  dsbt_info *info = get_dsbt_info (current_program_space);
 
   dsbt_get_initial_loadmaps ();
   ldm = info->exec_loadmap;
@@ -875,9 +869,9 @@ dsbt_solib_create_inferior_hook (int from_tty)
 }
 
 static void
-dsbt_clear_solib (void)
+dsbt_clear_solib (program_space *pspace)
 {
-  struct dsbt_info *info = get_dsbt_info ();
+  dsbt_info *info = get_dsbt_info (pspace);
 
   info->lm_base_cache = 0;
   info->main_lm_addr = 0;
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index d4e84a1b528d..c61ed7910ee2 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -806,7 +806,7 @@ frv_solib_create_inferior_hook (int from_tty)
 }
 
 static void
-frv_clear_solib (void)
+frv_clear_solib (program_space *pspace)
 {
   lm_base_cache = 0;
   enable_break2_done = 0;
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 7bc6ca45c373..1f8b8b34c00a 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -3178,11 +3178,9 @@ svr4_solib_create_inferior_hook (int from_tty)
 }
 
 static void
-svr4_clear_solib (void)
+svr4_clear_solib (program_space *pspace)
 {
-  struct svr4_info *info;
-
-  info = get_svr4_info (current_program_space);
+  svr4_info *info = get_svr4_info (pspace);
   info->debug_base = 0;
   info->debug_loader_offset_p = 0;
   info->debug_loader_offset = 0;
diff --git a/gdb/solib.c b/gdb/solib.c
index e8211814283a..54a4c3942b0c 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -1241,7 +1241,7 @@ clear_solib (void)
     }
 
   if (ops->clear_solib != nullptr)
-    ops->clear_solib ();
+    ops->clear_solib (current_program_space);
 }
 
 /* Shared library startup support.  When GDB starts up the inferior,
@@ -1411,7 +1411,7 @@ reload_shared_libraries (const char *ignored, int from_tty,
       /* Reset or free private data structures not associated with
 	 so_list entries.  */
       if (ops->clear_solib != nullptr)
-	ops->clear_solib ();
+	ops->clear_solib (current_program_space);
 
       /* Remove any previous solib event breakpoint.  This is usually
 	 done in common code, at breakpoint_init_inferior time, but
diff --git a/gdb/solist.h b/gdb/solist.h
index 0f764b264f74..b3a06b88c439 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -96,9 +96,10 @@ struct target_so_ops
      It is also called before free_so when SO is about to be freed.  */
   void (*clear_so) (struct so_list *so);
 
-  /* Reset or free private data structures not associated with
-     so_list entries.  */
-  void (*clear_solib) (void);
+  /* Free private data structures associated to PSPACE.  This method
+     should not free resources associated to individual so_list entries,
+     those are cleared by the clear_so method.  */
+  void (*clear_solib) (program_space *pspace);
 
   /* Target dependent code to run after child process fork.  */
   void (*solib_create_inferior_hook) (int from_tty);
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 03/24] gdb: make interps_notify work with references
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
  2023-10-10 20:39 ` [PATCH 01/24] gdb: remove empty clear_solib functions Simon Marchi
  2023-10-10 20:39 ` [PATCH 02/24] gdb: add program_space parameter to target_so_ops::clear_solib Simon Marchi
@ 2023-10-10 20:39 ` Simon Marchi
  2023-10-11  8:48   ` Lancelot SIX
  2023-10-10 20:39 ` [PATCH 04/24] gdb: replace some so_list parameters to use references Simon Marchi
                   ` (22 subsequent siblings)
  25 siblings, 1 reply; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:39 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

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

A subsequent patch changes the interp::on_solib_loaded and
interp::on_solib_unloaded methods to take references.  This highlighted
that interps_notify did not work with reference parameters.

Fix that by changing interps_notify's `args` arg to be a universal
reference (&&).  Change the type of the method to be auto-deduced as an
additional template parameter, otherwise the signature of the callback
function would never match:

      CXX    interps.o
    /home/simark/src/binutils-gdb/gdb/interps.c: In function ‘void interps_notify_signal_received(gdb_signal)’:
    /home/simark/src/binutils-gdb/gdb/interps.c:378:18: error: no matching function for call to ‘interps_notify(void (interp::*)(gdb_signal), gdb_signal&)’
      378 |   interps_notify (&interp::on_signal_received, sig);
          |   ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /home/simark/src/binutils-gdb/gdb/interps.c:363:1: note: candidate: ‘template<class ... Args> void interps_notify(void (interp::*)(Args ...), Args&& ...)’
      363 | interps_notify (void (interp::*method) (Args...), Args&&... args)
          | ^~~~~~~~~~~~~~
    /home/simark/src/binutils-gdb/gdb/interps.c:363:1: note:   template argument deduction/substitution failed:
    /home/simark/src/binutils-gdb/gdb/interps.c:378:18: note:   inconsistent parameter pack deduction with ‘gdb_signal’ and ‘gdb_signal&’
      378 |   interps_notify (&interp::on_signal_received, sig);
          |   ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Change-Id: I0cd9378e24ef039f30f8e14f054f8d7fb539c838
---
 gdb/interps.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/gdb/interps.c b/gdb/interps.c
index f91b2b62c1ba..62a30583e8c0 100644
--- a/gdb/interps.c
+++ b/gdb/interps.c
@@ -358,15 +358,15 @@ current_interpreter (void)
 /* Helper interps_notify_* functions.  Call METHOD on the top-level interpreter
    of all UIs.  */
 
-template <typename ...Args>
+template <typename MethodType, typename ...Args>
 void
-interps_notify (void (interp::*method) (Args...), Args... args)
+interps_notify (MethodType method, Args&&... args)
 {
   SWITCH_THRU_ALL_UIS ()
     {
       interp *tli = top_level_interpreter ();
       if (tli != nullptr)
-	(tli->*method) (args...);
+	(tli->*method) (std::forward<Args>(args)...);
     }
 }
 
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 04/24] gdb: replace some so_list parameters to use references
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (2 preceding siblings ...)
  2023-10-10 20:39 ` [PATCH 03/24] gdb: make interps_notify work with references Simon Marchi
@ 2023-10-10 20:39 ` Simon Marchi
  2023-10-19 11:07   ` [PATCH 4/24] " Lancelot SIX
  2023-10-10 20:40 ` [PATCH 05/24] gdbsupport: use "reference" and "pointer" type aliases in intrusive_list Simon Marchi
                   ` (21 subsequent siblings)
  25 siblings, 1 reply; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:39 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

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

A subsequent patch changes so_list to be linked using
intrusive_list.  Iterating an intrusive_list yields some references to
the list elements.  Convert some functions accepting so_list objects to
take references, to make things easier and more natural.  Add const
where possible and convenient.

Change-Id: Id5ab5339c3eb6432e809ad14782952d6a45806f3
---
 gdb/breakpoint.c     |   5 +-
 gdb/bsd-uthread.c    |  12 ++---
 gdb/exec.c           |   4 +-
 gdb/interps.c        |   4 +-
 gdb/interps.h        |   8 +--
 gdb/mi/mi-cmd-file.c |   2 +-
 gdb/mi/mi-interp.c   |  26 ++++-----
 gdb/mi/mi-interp.h   |   6 +--
 gdb/nto-tdep.c       |   6 +--
 gdb/nto-tdep.h       |   3 +-
 gdb/observable.h     |   5 +-
 gdb/progspace.h      |   4 +-
 gdb/solib-aix.c      |  11 ++--
 gdb/solib-darwin.c   |  23 ++++----
 gdb/solib-dsbt.c     |   9 ++--
 gdb/solib-frv.c      |   9 ++--
 gdb/solib-rocm.c     |   8 +--
 gdb/solib-svr4.c     |  35 ++++++------
 gdb/solib-target.c   |  49 +++++++++--------
 gdb/solib.c          | 126 +++++++++++++++++++++----------------------
 gdb/solib.h          |   4 +-
 gdb/solist.h         |  13 +++--
 gdb/target-section.h |   2 +-
 23 files changed, 183 insertions(+), 191 deletions(-)

diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 2edcbf6d4904..75691935553d 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -8018,7 +8018,8 @@ disable_breakpoints_in_shlibs (void)
    disabled ones can just stay disabled.  */
 
 static void
-disable_breakpoints_in_unloaded_shlib (program_space *pspace, so_list *solib)
+disable_breakpoints_in_unloaded_shlib (program_space *pspace,
+				       const so_list &solib)
 {
   bool disabled_shlib_breaks = false;
 
@@ -8051,7 +8052,7 @@ disable_breakpoints_in_unloaded_shlib (program_space *pspace, so_list *solib)
 	      target_terminal::ours_for_output ();
 	      warning (_("Temporarily disabling breakpoints "
 			 "for unloaded shared library \"%s\""),
-		       solib->so_name);
+		       solib.so_name);
 	    }
 	  disabled_shlib_breaks = true;
 	}
diff --git a/gdb/bsd-uthread.c b/gdb/bsd-uthread.c
index 8765fb77283d..6435a5291958 100644
--- a/gdb/bsd-uthread.c
+++ b/gdb/bsd-uthread.c
@@ -275,19 +275,19 @@ static const char * const bsd_uthread_solib_names[] =
 };
 
 static void
-bsd_uthread_solib_loaded (struct so_list *so)
+bsd_uthread_solib_loaded (so_list &so)
 {
   const char * const *names = bsd_uthread_solib_names;
 
   for (names = bsd_uthread_solib_names; *names; names++)
     {
-      if (startswith (so->so_original_name, *names))
+      if (startswith (so.so_original_name, *names))
 	{
 	  solib_read_symbols (so, 0);
 
-	  if (bsd_uthread_activate (so->objfile))
+	  if (bsd_uthread_activate (so.objfile))
 	    {
-	      bsd_uthread_solib_name = so->so_original_name;
+	      bsd_uthread_solib_name = so.so_original_name;
 	      return;
 	    }
 	}
@@ -295,12 +295,12 @@ bsd_uthread_solib_loaded (struct so_list *so)
 }
 
 static void
-bsd_uthread_solib_unloaded (program_space *pspace, so_list *so)
+bsd_uthread_solib_unloaded (program_space *pspace, const so_list &so)
 {
   if (!bsd_uthread_solib_name)
     return;
 
-  if (strcmp (so->so_original_name, bsd_uthread_solib_name) == 0)
+  if (strcmp (so.so_original_name, bsd_uthread_solib_name) == 0)
     bsd_uthread_deactivate ();
 }
 
diff --git a/gdb/exec.c b/gdb/exec.c
index a1396c2aa3d2..08c205662ea1 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -599,7 +599,7 @@ build_section_table (struct bfd *some_bfd)
    current set of target sections.  */
 
 void
-program_space::add_target_sections (void *owner,
+program_space::add_target_sections (const void *owner,
 				    const target_section_table &sections)
 {
   if (!sections.empty ())
@@ -651,7 +651,7 @@ program_space::add_target_sections (struct objfile *objfile)
    OWNER must be the same value passed to add_target_sections.  */
 
 void
-program_space::remove_target_sections (void *owner)
+program_space::remove_target_sections (const void *owner)
 {
   gdb_assert (owner != NULL);
 
diff --git a/gdb/interps.c b/gdb/interps.c
index 62a30583e8c0..544539b0c798 100644
--- a/gdb/interps.c
+++ b/gdb/interps.c
@@ -488,7 +488,7 @@ interps_notify_target_resumed (ptid_t ptid)
 /* See interps.h.  */
 
 void
-interps_notify_solib_loaded (so_list *so)
+interps_notify_solib_loaded (const so_list &so)
 {
   interps_notify (&interp::on_solib_loaded, so);
 }
@@ -496,7 +496,7 @@ interps_notify_solib_loaded (so_list *so)
 /* See interps.h.  */
 
 void
-interps_notify_solib_unloaded (so_list *so)
+interps_notify_solib_unloaded (const so_list &so)
 {
   interps_notify (&interp::on_solib_unloaded, so);
 }
diff --git a/gdb/interps.h b/gdb/interps.h
index 287df2c8c810..a90c5aa17b0a 100644
--- a/gdb/interps.h
+++ b/gdb/interps.h
@@ -147,10 +147,10 @@ class interp : public intrusive_list_node<interp>
   virtual void on_target_resumed (ptid_t ptid) {}
 
   /* Notify the interpreter that solib SO has been loaded.  */
-  virtual void on_solib_loaded (so_list *so) {}
+  virtual void on_solib_loaded (const so_list &so) {}
 
   /* Notify the interpreter that solib SO has been unloaded.  */
-  virtual void on_solib_unloaded (so_list *so) {}
+  virtual void on_solib_unloaded (const so_list &so) {}
 
   /* Notify the interpreter that a command it is executing is about to cause
      the inferior to proceed.  */
@@ -324,10 +324,10 @@ extern void interps_notify_record_changed (inferior *inf, int started,
 extern void interps_notify_target_resumed (ptid_t ptid);
 
 /* Notify all interpreters that solib SO has been loaded.  */
-extern void interps_notify_solib_loaded (so_list *so);
+extern void interps_notify_solib_loaded (const so_list &so);
 
 /* Notify all interpreters that solib SO has been unloaded.  */
-extern void interps_notify_solib_unloaded (so_list *so);
+extern void interps_notify_solib_unloaded (const so_list &so);
 
 /* Notify all interpreters that the selected traceframe changed.
 
diff --git a/gdb/mi/mi-cmd-file.c b/gdb/mi/mi-cmd-file.c
index 688bf493f195..822d3df40005 100644
--- a/gdb/mi/mi-cmd-file.c
+++ b/gdb/mi/mi-cmd-file.c
@@ -171,6 +171,6 @@ mi_cmd_file_list_shared_libraries (const char *command,
 	continue;
 
       ui_out_emit_tuple tuple_emitter (uiout, NULL);
-      mi_output_solib_attribs (uiout, so);
+      mi_output_solib_attribs (uiout, *so);
     }
 }
diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
index 0d3c8235c2c1..0fae5674201c 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -724,28 +724,28 @@ mi_interp::on_target_resumed (ptid_t ptid)
 /* See mi-interp.h.  */
 
 void
-mi_output_solib_attribs (ui_out *uiout, struct so_list *solib)
+mi_output_solib_attribs (ui_out *uiout, const so_list &solib)
 {
   gdbarch *gdbarch = current_inferior ()->arch ();
 
-  uiout->field_string ("id", solib->so_original_name);
-  uiout->field_string ("target-name", solib->so_original_name);
-  uiout->field_string ("host-name", solib->so_name);
-  uiout->field_signed ("symbols-loaded", solib->symbols_loaded);
+  uiout->field_string ("id", solib.so_original_name);
+  uiout->field_string ("target-name", solib.so_original_name);
+  uiout->field_string ("host-name", solib.so_name);
+  uiout->field_signed ("symbols-loaded", solib.symbols_loaded);
   if (!gdbarch_has_global_solist (current_inferior ()->arch ()))
       uiout->field_fmt ("thread-group", "i%d", current_inferior ()->num);
 
   ui_out_emit_list list_emitter (uiout, "ranges");
   ui_out_emit_tuple tuple_emitter (uiout, NULL);
-  if (solib->addr_high != 0)
+  if (solib.addr_high != 0)
     {
-      uiout->field_core_addr ("from", gdbarch, solib->addr_low);
-      uiout->field_core_addr ("to", gdbarch, solib->addr_high);
+      uiout->field_core_addr ("from", gdbarch, solib.addr_low);
+      uiout->field_core_addr ("to", gdbarch, solib.addr_high);
     }
 }
 
 void
-mi_interp::on_solib_loaded (so_list *solib)
+mi_interp::on_solib_loaded (const so_list &solib)
 {
   ui_out *uiout = this->interp_ui_out ();
 
@@ -762,7 +762,7 @@ mi_interp::on_solib_loaded (so_list *solib)
 }
 
 void
-mi_interp::on_solib_unloaded (so_list *solib)
+mi_interp::on_solib_unloaded (const so_list &solib)
 {
   ui_out *uiout = this->interp_ui_out ();
 
@@ -773,9 +773,9 @@ mi_interp::on_solib_unloaded (so_list *solib)
 
   ui_out_redirect_pop redir (uiout, this->event_channel);
 
-  uiout->field_string ("id", solib->so_original_name);
-  uiout->field_string ("target-name", solib->so_original_name);
-  uiout->field_string ("host-name", solib->so_name);
+  uiout->field_string ("id", solib.so_original_name);
+  uiout->field_string ("target-name", solib.so_original_name);
+  uiout->field_string ("host-name", solib.so_name);
   if (!gdbarch_has_global_solist (current_inferior ()->arch ()))
     uiout->field_fmt ("thread-group", "i%d", current_inferior ()->num);
 
diff --git a/gdb/mi/mi-interp.h b/gdb/mi/mi-interp.h
index f9af61f0a571..781a8dc6f466 100644
--- a/gdb/mi/mi-interp.h
+++ b/gdb/mi/mi-interp.h
@@ -60,8 +60,8 @@ class mi_interp final : public interp
   void on_record_changed (inferior *inf, int started, const char *method,
 			  const char *format) override;
   void on_target_resumed (ptid_t ptid) override;
-  void on_solib_loaded (so_list *so) override;
-  void on_solib_unloaded (so_list *so) override;
+  void on_solib_loaded (const so_list &so) override;
+  void on_solib_unloaded (const so_list &so) override;
   void on_about_to_proceed () override;
   void on_traceframe_changed (int tfnum, int tpnum) override;
   void on_tsv_created (const trace_state_variable *tsv) override;
@@ -108,7 +108,7 @@ class mi_interp final : public interp
 
 /* Output the shared object attributes to UIOUT.  */
 
-void mi_output_solib_attribs (ui_out *uiout, struct so_list *solib);
+void mi_output_solib_attribs (ui_out *uiout, const so_list &solib);
 
 /* Returns the INTERP's data cast as mi_interp if INTERP is an MI, and
    returns NULL otherwise.  */
diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index 9ece8b5fb39b..1d9c7340fcf7 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -245,9 +245,9 @@ nto_parse_redirection (char *pargv[], const char **pin, const char **pout,
 }
 
 static CORE_ADDR
-lm_addr (struct so_list *so)
+lm_addr (const so_list &so)
 {
-  lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
+  const lm_info_svr4 *li = (const lm_info_svr4 *) so.lm_info;
 
   return li->l_addr;
 }
@@ -283,7 +283,7 @@ find_load_phdr (bfd *abfd)
 }
 
 void
-nto_relocate_section_addresses (struct so_list *so, struct target_section *sec)
+nto_relocate_section_addresses (so_list &so, target_section *sec)
 {
   /* Neutrino treats the l_addr base address field in link.h as different than
      the base address in the System V ABI and so the offset needs to be
diff --git a/gdb/nto-tdep.h b/gdb/nto-tdep.h
index b352d5310c49..8b644b30c42a 100644
--- a/gdb/nto-tdep.h
+++ b/gdb/nto-tdep.h
@@ -166,8 +166,7 @@ void nto_init_solib_absolute_prefix (void);
 char **nto_parse_redirection (char *start_argv[], const char **in,
 			      const char **out, const char **err);
 
-void nto_relocate_section_addresses (struct so_list *,
-				     struct target_section *);
+void nto_relocate_section_addresses (so_list &, target_section *);
 
 int nto_map_arch_to_cputype (const char *);
 
diff --git a/gdb/observable.h b/gdb/observable.h
index acb05e9b535c..5ed6ca547ce0 100644
--- a/gdb/observable.h
+++ b/gdb/observable.h
@@ -99,13 +99,12 @@ extern observable<inferior */* parent_inf */, inferior */* child_inf */,
 /* The shared library specified by SOLIB has been loaded.  Note that
    when gdb calls this observer, the library's symbols probably
    haven't been loaded yet.  */
-extern observable<struct so_list */* solib */> solib_loaded;
+extern observable<so_list &/* solib */> solib_loaded;
 
 /* The shared library SOLIB has been unloaded from program space PSPACE.
    Note  when gdb calls this observer, the library's symbols have not
    been unloaded yet, and thus are still available.  */
-extern observable<struct program_space */* pspace */, struct so_list */* solib */>
-  solib_unloaded;
+extern observable<program_space *, const so_list &/* solib */> solib_unloaded;
 
 /* The symbol file specified by OBJFILE has been loaded.  */
 extern observable<struct objfile */* objfile */> new_objfile;
diff --git a/gdb/progspace.h b/gdb/progspace.h
index ee12d89c173c..0930d8583597 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -286,11 +286,11 @@ struct program_space
   bool empty ();
 
   /* Remove all target sections owned by OWNER.  */
-  void remove_target_sections (void *owner);
+  void remove_target_sections (const void *owner);
 
   /* Add the sections array defined by SECTIONS to the
      current set of target sections.  */
-  void add_target_sections (void *owner,
+  void add_target_sections (const void *owner,
 			    const target_section_table &sections);
 
   /* Add the sections of OBJFILE to the current set of target
diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index 0b6ad83eed92..cfcc04db1518 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -311,13 +311,12 @@ solib_aix_bss_data_overlap (bfd *abfd)
 /* Implement the "relocate_section_addresses" target_so_ops method.  */
 
 static void
-solib_aix_relocate_section_addresses (struct so_list *so,
-				      struct target_section *sec)
+solib_aix_relocate_section_addresses (so_list &so, target_section *sec)
 {
   struct bfd_section *bfd_sect = sec->the_bfd_section;
   bfd *abfd = bfd_sect->owner;
   const char *section_name = bfd_section_name (bfd_sect);
-  lm_info_aix *info = (lm_info_aix *) so->lm_info;
+  lm_info_aix *info = (lm_info_aix *) so.lm_info;
 
   if (strcmp (section_name, ".text") == 0)
     {
@@ -364,11 +363,11 @@ solib_aix_relocate_section_addresses (struct so_list *so,
 /* Implement the "free_so" target_so_ops method.  */
 
 static void
-solib_aix_free_so (struct so_list *so)
+solib_aix_free_so (so_list &so)
 {
-  lm_info_aix *li = (lm_info_aix *) so->lm_info;
+  lm_info_aix *li = (lm_info_aix *) so.lm_info;
 
-  solib_debug_printf ("%s", so->so_name);
+  solib_debug_printf ("%s", so.so_name);
 
   delete li;
 }
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index 4796315c373a..5720f997bef7 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -608,9 +608,9 @@ darwin_clear_solib (program_space *pspace)
 }
 
 static void
-darwin_free_so (struct so_list *so)
+darwin_free_so (so_list &so)
 {
-  lm_info_darwin *li = (lm_info_darwin *) so->lm_info;
+  lm_info_darwin *li = (lm_info_darwin *) so.lm_info;
 
   delete li;
 }
@@ -619,25 +619,24 @@ darwin_free_so (struct so_list *so)
    Relocate these VMAs according to solib info.  */
 
 static void
-darwin_relocate_section_addresses (struct so_list *so,
-				   struct target_section *sec)
+darwin_relocate_section_addresses (so_list &so, target_section *sec)
 {
-  lm_info_darwin *li = (lm_info_darwin *) so->lm_info;
+  lm_info_darwin *li = (lm_info_darwin *) so.lm_info;
 
   sec->addr += li->lm_addr;
   sec->endaddr += li->lm_addr;
 
   /* Best effort to set addr_high/addr_low.  This is used only by
      'info sharedlibary'.  */
-  if (so->addr_high == 0)
+  if (so.addr_high == 0)
     {
-      so->addr_low = sec->addr;
-      so->addr_high = sec->endaddr;
+      so.addr_low = sec->addr;
+      so.addr_high = sec->endaddr;
     }
-  if (sec->endaddr > so->addr_high)
-    so->addr_high = sec->endaddr;
-  if (sec->addr < so->addr_low)
-    so->addr_low = sec->addr;
+  if (sec->endaddr > so.addr_high)
+    so.addr_high = sec->endaddr;
+  if (sec->addr < so.addr_low)
+    so.addr_low = sec->addr;
 }
 \f
 static gdb_bfd_ref_ptr
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index 16fa78a3d8b2..60a211ee6e6e 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -881,19 +881,18 @@ dsbt_clear_solib (program_space *pspace)
 }
 
 static void
-dsbt_free_so (struct so_list *so)
+dsbt_free_so (so_list &so)
 {
-  lm_info_dsbt *li = (lm_info_dsbt *) so->lm_info;
+  lm_info_dsbt *li = (lm_info_dsbt *) so.lm_info;
 
   delete li;
 }
 
 static void
-dsbt_relocate_section_addresses (struct so_list *so,
-				 struct target_section *sec)
+dsbt_relocate_section_addresses (so_list &so, target_section *sec)
 {
   int seg;
-  lm_info_dsbt *li = (lm_info_dsbt *) so->lm_info;
+  lm_info_dsbt *li = (lm_info_dsbt *) so.lm_info;
   int_elf32_dsbt_loadmap *map = li->map;
 
   for (seg = 0; seg < map->nsegs; seg++)
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index c61ed7910ee2..c895eb20e434 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -817,19 +817,18 @@ frv_clear_solib (program_space *pspace)
 }
 
 static void
-frv_free_so (struct so_list *so)
+frv_free_so (so_list &so)
 {
-  lm_info_frv *li = (lm_info_frv *) so->lm_info;
+  lm_info_frv *li = (lm_info_frv *) so.lm_info;
 
   delete li;
 }
 
 static void
-frv_relocate_section_addresses (struct so_list *so,
-				 struct target_section *sec)
+frv_relocate_section_addresses (so_list &so, target_section *sec)
 {
   int seg;
-  lm_info_frv *li = (lm_info_frv *) so->lm_info;
+  lm_info_frv *li = (lm_info_frv *) so.lm_info;
   int_elf32_fdpic_loadmap *map = li->map;
 
   for (seg = 0; seg < map->nsegs; seg++)
diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
index b24d0e8fb235..65dd1c06271f 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -153,7 +153,7 @@ rocm_free_solib_list (struct solib_info *info)
     {
       struct so_list *next = info->solib_list->next;
 
-      free_so (info->solib_list);
+      free_so (*info->solib_list);
       info->solib_list = next;
     }
 
@@ -177,16 +177,16 @@ get_solib_info (inferior *inf)
 /* Relocate section addresses.  */
 
 static void
-rocm_solib_relocate_section_addresses (struct so_list *so,
+rocm_solib_relocate_section_addresses (so_list &so,
 				       struct target_section *sec)
 {
-  if (!is_amdgpu_arch (gdbarch_from_bfd (so->abfd)))
+  if (!is_amdgpu_arch (gdbarch_from_bfd (so.abfd)))
     {
       svr4_so_ops.relocate_section_addresses (so, sec);
       return;
     }
 
-  lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
+  lm_info_svr4 *li = (lm_info_svr4 *) so.lm_info;
   sec->addr = sec->addr + li->l_addr;
   sec->endaddr = sec->endaddr + li->l_addr;
 }
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 1f8b8b34c00a..b32848944cad 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -173,16 +173,16 @@ svr4_same_1 (const char *gdb_so_name, const char *inferior_so_name)
 }
 
 static int
-svr4_same (struct so_list *gdb, struct so_list *inferior)
+svr4_same (const so_list &gdb, const so_list &inferior)
 {
-  if (!svr4_same_1 (gdb->so_original_name, inferior->so_original_name))
+  if (!svr4_same_1 (gdb.so_original_name, inferior.so_original_name))
     return false;
 
   /* There may be different instances of the same library, in different
      namespaces.  Each instance, however, must have been loaded at a
      different address so its relocation offset would be different.  */
-  const lm_info_svr4 *lmg = (const lm_info_svr4 *) gdb->lm_info;
-  const lm_info_svr4 *lmi = (const lm_info_svr4 *) inferior->lm_info;
+  const lm_info_svr4 *lmg = (const lm_info_svr4 *) gdb.lm_info;
+  const lm_info_svr4 *lmi = (const lm_info_svr4 *) inferior.lm_info;
 
   return (lmg->l_addr_inferior == lmi->l_addr_inferior);
 }
@@ -229,9 +229,9 @@ has_lm_dynamic_from_link_map (void)
 }
 
 static CORE_ADDR
-lm_addr_check (const struct so_list *so, bfd *abfd)
+lm_addr_check (const so_list &so, bfd *abfd)
 {
-  lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
+  lm_info_svr4 *li = (lm_info_svr4 *) so.lm_info;
 
   if (!li->l_addr_p)
     {
@@ -306,7 +306,7 @@ lm_addr_check (const struct so_list *so, bfd *abfd)
 		gdb_printf (_("Using PIC (Position Independent Code) "
 			      "prelink displacement %s for \"%s\".\n"),
 			    paddress (current_inferior ()->arch (), l_addr),
-			    so->so_name);
+			    so.so_name);
 	    }
 	  else
 	    {
@@ -321,7 +321,7 @@ lm_addr_check (const struct so_list *so, bfd *abfd)
 
 	      warning (_(".dynamic section for \"%s\" "
 			 "is not at the expected address "
-			 "(wrong library or version mismatch?)"), so->so_name);
+			 "(wrong library or version mismatch?)"), so.so_name);
 	    }
 	}
 
@@ -979,9 +979,9 @@ svr4_free_objfile_observer (struct objfile *objfile)
 /* Implementation for target_so_ops.free_so.  */
 
 static void
-svr4_free_so (struct so_list *so)
+svr4_free_so (so_list &so)
 {
-  lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
+  lm_info_svr4 *li = (lm_info_svr4 *) so.lm_info;
 
   delete li;
 }
@@ -989,9 +989,9 @@ svr4_free_so (struct so_list *so)
 /* Implement target_so_ops.clear_so.  */
 
 static void
-svr4_clear_so (struct so_list *so)
+svr4_clear_so (const so_list &so)
 {
-  lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
+  lm_info_svr4 *li = (lm_info_svr4 *) so.lm_info;
 
   if (li != NULL)
     li->l_addr_p = 0;
@@ -1006,7 +1006,7 @@ svr4_free_library_list (so_list *list)
     {
       struct so_list *next = list->next;
 
-      free_so (list);
+      free_so (*list);
       list = next;
     }
 }
@@ -1581,7 +1581,7 @@ svr4_current_sos (void)
 	  if (address_in_mem_range (li->l_ld, &vsyscall_range))
 	    {
 	      *sop = so->next;
-	      free_so (so);
+	      free_so (*so);
 	      break;
 	    }
 
@@ -2471,7 +2471,7 @@ enable_break (struct svr4_info *info, int from_tty)
 	    {
 	      load_addr_found = 1;
 	      loader_found_in_list = 1;
-	      load_addr = lm_addr_check (so, tmp_bfd.get ());
+	      load_addr = lm_addr_check (*so, tmp_bfd.get ());
 	      break;
 	    }
 	}
@@ -3214,8 +3214,7 @@ svr4_truncate_ptr (CORE_ADDR addr)
 
 
 static void
-svr4_relocate_section_addresses (struct so_list *so,
-				 struct target_section *sec)
+svr4_relocate_section_addresses (so_list &so, target_section *sec)
 {
   bfd *abfd = sec->the_bfd_section->owner;
 
@@ -3396,7 +3395,7 @@ find_debug_base_for_solib (so_list *solib)
       so_list *solist = tuple.second;
 
       for (; solist != nullptr; solist = solist->next)
-	if (svr4_same (solib, solist))
+	if (svr4_same (*solib, *solist))
 	  return debug_base;
     }
 
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index 865235de2995..b8b6dd644018 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -282,9 +282,9 @@ solib_target_solib_create_inferior_hook (int from_tty)
 }
 
 static void
-solib_target_free_so (struct so_list *so)
+solib_target_free_so (so_list &so)
 {
-  lm_info_target *li = (lm_info_target *) so->lm_info;
+  lm_info_target *li = (lm_info_target *) so.lm_info;
 
   gdb_assert (li->name.empty ());
 
@@ -292,17 +292,16 @@ solib_target_free_so (struct so_list *so)
 }
 
 static void
-solib_target_relocate_section_addresses (struct so_list *so,
-					 struct target_section *sec)
+solib_target_relocate_section_addresses (so_list &so, target_section *sec)
 {
   CORE_ADDR offset;
-  lm_info_target *li = (lm_info_target *) so->lm_info;
+  lm_info_target *li = (lm_info_target *) so.lm_info;
 
   /* Build the offset table only once per object file.  We can not do
      it any earlier, since we need to open the file first.  */
   if (li->offsets.empty ())
     {
-      int num_sections = gdb_bfd_count_sections (so->abfd);
+      int num_sections = gdb_bfd_count_sections (so.abfd);
 
       li->offsets.assign (num_sections, 0);
 
@@ -312,7 +311,7 @@ solib_target_relocate_section_addresses (struct so_list *so,
 	  asection *sect;
 	  int num_alloc_sections = 0;
 
-	  for (i = 0, sect = so->abfd->sections;
+	  for (i = 0, sect = so.abfd->sections;
 	       sect != NULL;
 	       i++, sect = sect->next)
 	    if ((bfd_section_flags (sect) & SEC_ALLOC))
@@ -321,15 +320,15 @@ solib_target_relocate_section_addresses (struct so_list *so,
 	  if (num_alloc_sections != li->section_bases.size ())
 	    warning (_("\
 Could not relocate shared library \"%s\": wrong number of ALLOC sections"),
-		     so->so_name);
+		     so.so_name);
 	  else
 	    {
 	      int bases_index = 0;
 	      int found_range = 0;
 
-	      so->addr_low = ~(CORE_ADDR) 0;
-	      so->addr_high = 0;
-	      for (i = 0, sect = so->abfd->sections;
+	      so.addr_low = ~(CORE_ADDR) 0;
+	      so.addr_high = 0;
+	      for (i = 0, sect = so.abfd->sections;
 		   sect != NULL;
 		   i++, sect = sect->next)
 		{
@@ -342,40 +341,40 @@ Could not relocate shared library \"%s\": wrong number of ALLOC sections"),
 		      low = li->section_bases[i];
 		      high = low + bfd_section_size (sect) - 1;
 
-		      if (low < so->addr_low)
-			so->addr_low = low;
-		      if (high > so->addr_high)
-			so->addr_high = high;
-		      gdb_assert (so->addr_low <= so->addr_high);
+		      if (low < so.addr_low)
+			so.addr_low = low;
+		      if (high > so.addr_high)
+			so.addr_high = high;
+		      gdb_assert (so.addr_low <= so.addr_high);
 		      found_range = 1;
 		    }
 		  li->offsets[i] = li->section_bases[bases_index];
 		  bases_index++;
 		}
 	      if (!found_range)
-		so->addr_low = so->addr_high = 0;
-	      gdb_assert (so->addr_low <= so->addr_high);
+		so.addr_low = so.addr_high = 0;
+	      gdb_assert (so.addr_low <= so.addr_high);
 	    }
 	}
       else if (!li->segment_bases.empty ())
 	{
 	  symfile_segment_data_up data
-	    = get_symfile_segment_data (so->abfd);
+	    = get_symfile_segment_data (so.abfd);
 
 	  if (data == NULL)
 	    warning (_("\
-Could not relocate shared library \"%s\": no segments"), so->so_name);
+Could not relocate shared library \"%s\": no segments"), so.so_name);
 	  else
 	    {
 	      ULONGEST orig_delta;
 	      int i;
 
-	      if (!symfile_map_offsets_to_segments (so->abfd, data.get (),
+	      if (!symfile_map_offsets_to_segments (so.abfd, data.get (),
 						    li->offsets,
 						    li->segment_bases.size (),
 						    li->segment_bases.data ()))
 		warning (_("\
-Could not relocate shared library \"%s\": bad offsets"), so->so_name);
+Could not relocate shared library \"%s\": bad offsets"), so.so_name);
 
 	      /* Find the range of addresses to report for this library in
 		 "info sharedlibrary".  Report any consecutive segments
@@ -397,11 +396,11 @@ Could not relocate shared library \"%s\": bad offsets"), so->so_name);
 		    break;
 		}
 
-	      so->addr_low = li->segment_bases[0];
-	      so->addr_high = (data->segments[i - 1].base
+	      so.addr_low = li->segment_bases[0];
+	      so.addr_high = (data->segments[i - 1].base
 			       + data->segments[i - 1].size
 			       + orig_delta);
-	      gdb_assert (so->addr_low <= so->addr_high);
+	      gdb_assert (so.addr_low <= so.addr_high);
 	    }
 	}
     }
diff --git a/gdb/solib.c b/gdb/solib.c
index 54a4c3942b0c..a85ea5458bce 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -537,14 +537,14 @@ get_cbfd_soname_build_id (gdb_bfd_ref_ptr abfd, const char *soname)
    expansion stuff?).  */
 
 static int
-solib_map_sections (struct so_list *so)
+solib_map_sections (so_list &so)
 {
   const target_so_ops *ops = gdbarch_so_ops (current_inferior ()->arch ());
 
-  gdb::unique_xmalloc_ptr<char> filename (tilde_expand (so->so_name));
+  gdb::unique_xmalloc_ptr<char> filename (tilde_expand (so.so_name));
   gdb_bfd_ref_ptr abfd (ops->bfd_open (filename.get ()));
   gdb::unique_xmalloc_ptr<char> build_id_hexstr
-    = get_cbfd_soname_build_id (current_program_space->cbfd, so->so_name);
+    = get_cbfd_soname_build_id (current_program_space->cbfd, so.so_name);
 
   /* If we already know the build-id of this solib from a core file, verify
      it matches ABFD's build-id.  If there is a mismatch or the solib wasn't
@@ -564,7 +564,7 @@ solib_map_sections (struct so_list *so)
 	{
 	  scoped_fd fd = debuginfod_exec_query ((const unsigned char*)
 						build_id_hexstr.get (),
-						0, so->so_name, &filename);
+						0, so.so_name, &filename);
 
 	  if (fd.get () >= 0)
 	    abfd = ops->bfd_open (filename.get ());
@@ -578,7 +578,7 @@ solib_map_sections (struct so_list *so)
     return 0;
 
   /* Leave bfd open, core_xfer_memory and "info files" need it.  */
-  so->abfd = abfd.release ();
+  so.abfd = abfd.release ();
 
   /* Copy the full path name into so_name, allowing symbol_file_add
      to find it later.  This also affects the =library-loaded GDB/MI
@@ -586,15 +586,15 @@ solib_map_sections (struct so_list *so)
      the library's host-side path.  If we let the target dictate
      that objfile's path, and the target is different from the host,
      GDB/MI will not provide the correct host-side path.  */
-  if (strlen (bfd_get_filename (so->abfd)) >= SO_NAME_MAX_PATH_SIZE)
+  if (strlen (bfd_get_filename (so.abfd)) >= SO_NAME_MAX_PATH_SIZE)
     error (_("Shared library file name is too long."));
-  strcpy (so->so_name, bfd_get_filename (so->abfd));
+  strcpy (so.so_name, bfd_get_filename (so.abfd));
 
-  if (so->sections == nullptr)
-    so->sections = new target_section_table;
-  *so->sections = build_section_table (so->abfd);
+  if (so.sections == nullptr)
+    so.sections = new target_section_table;
+  *so.sections = build_section_table (so.abfd);
 
-  for (target_section &p : *so->sections)
+  for (target_section &p : *so.sections)
     {
       /* Relocate the section binding addresses as recorded in the shared
 	 object's file by the base address to which the object was actually
@@ -604,11 +604,11 @@ solib_map_sections (struct so_list *so)
       /* If the target didn't provide information about the address
 	 range of the shared object, assume we want the location of
 	 the .text section.  */
-      if (so->addr_low == 0 && so->addr_high == 0
+      if (so.addr_low == 0 && so.addr_high == 0
 	  && strcmp (p.the_bfd_section->name, ".text") == 0)
 	{
-	  so->addr_low = p.addr;
-	  so->addr_high = p.endaddr;
+	  so.addr_low = p.addr;
+	  so.addr_high = p.endaddr;
 	}
     }
 
@@ -616,7 +616,7 @@ solib_map_sections (struct so_list *so)
      section tables.  Do this immediately after mapping the object so
      that later nodes in the list can query this object, as is needed
      in solib-osf.c.  */
-  current_program_space->add_target_sections (so, *so->sections);
+  current_program_space->add_target_sections (&so, *so.sections);
 
   return 1;
 }
@@ -631,25 +631,25 @@ solib_map_sections (struct so_list *so)
    responsible for taking care of that.  */
 
 static void
-clear_so (struct so_list *so)
+clear_so (so_list &so)
 {
   const target_so_ops *ops = gdbarch_so_ops (current_inferior ()->arch ());
 
-  delete so->sections;
-  so->sections = NULL;
+  delete so.sections;
+  so.sections = NULL;
 
-  gdb_bfd_unref (so->abfd);
-  so->abfd = NULL;
+  gdb_bfd_unref (so.abfd);
+  so.abfd = NULL;
 
   /* Our caller closed the objfile, possibly via objfile_purge_solibs.  */
-  so->symbols_loaded = 0;
-  so->objfile = NULL;
+  so.symbols_loaded = 0;
+  so.objfile = NULL;
 
-  so->addr_low = so->addr_high = 0;
+  so.addr_low = so.addr_high = 0;
 
   /* Restore the target-supplied file name.  SO_NAME may be the path
      of the symbol file.  */
-  strcpy (so->so_name, so->so_original_name);
+  strcpy (so.so_name, so.so_original_name);
 
   /* Do the same for target-specific data.  */
   if (ops->clear_so != NULL)
@@ -668,14 +668,14 @@ clear_so (struct so_list *so)
    responsible for taking care of that.  */
 
 void
-free_so (struct so_list *so)
+free_so (so_list &so)
 {
   const target_so_ops *ops = gdbarch_so_ops (current_inferior ()->arch ());
 
   clear_so (so);
   ops->free_so (so);
 
-  xfree (so);
+  xfree (&so);
 }
 
 
@@ -683,13 +683,13 @@ free_so (struct so_list *so)
    be chatty about it.  Return true if any symbols were actually loaded.  */
 
 bool
-solib_read_symbols (struct so_list *so, symfile_add_flags flags)
+solib_read_symbols (so_list &so, symfile_add_flags flags)
 {
-  if (so->symbols_loaded)
+  if (so.symbols_loaded)
     {
       /* If needed, we've already warned in our caller.  */
     }
-  else if (so->abfd == NULL)
+  else if (so.abfd == NULL)
     {
       /* We've already warned about this library, when trying to open
 	 it.  */
@@ -702,35 +702,35 @@ solib_read_symbols (struct so_list *so, symfile_add_flags flags)
       try
 	{
 	  /* Have we already loaded this shared object?  */
-	  so->objfile = nullptr;
+	  so.objfile = nullptr;
 	  for (objfile *objfile : current_program_space->objfiles ())
 	    {
-	      if (filename_cmp (objfile_name (objfile), so->so_name) == 0
-		  && objfile->addr_low == so->addr_low)
+	      if (filename_cmp (objfile_name (objfile), so.so_name) == 0
+		  && objfile->addr_low == so.addr_low)
 		{
-		  so->objfile = objfile;
+		  so.objfile = objfile;
 		  break;
 		}
 	    }
-	  if (so->objfile == NULL)
+	  if (so.objfile == NULL)
 	    {
 	      section_addr_info sap
-		= build_section_addr_info_from_section_table (*so->sections);
+		= build_section_addr_info_from_section_table (*so.sections);
 	      gdb_bfd_ref_ptr tmp_bfd
-		(gdb_bfd_ref_ptr::new_reference (so->abfd));
-	      so->objfile = symbol_file_add_from_bfd (tmp_bfd, so->so_name,
+		(gdb_bfd_ref_ptr::new_reference (so.abfd));
+	      so.objfile = symbol_file_add_from_bfd (tmp_bfd, so.so_name,
 						      flags, &sap,
 						      OBJF_SHARED, NULL);
-	      so->objfile->addr_low = so->addr_low;
+	      so.objfile->addr_low = so.addr_low;
 	    }
 
-	  so->symbols_loaded = 1;
+	  so.symbols_loaded = 1;
 	}
       catch (const gdb_exception_error &e)
 	{
 	  exception_fprintf (gdb_stderr, e, _("Error while reading shared"
 					      " library symbols for %s:\n"),
-			     so->so_name);
+			     so.so_name);
 	}
 
       return true;
@@ -754,7 +754,7 @@ solib_used (const struct so_list *const known)
 /* Notify interpreters and observers that solib SO has been loaded.  */
 
 static void
-notify_solib_loaded (so_list *so)
+notify_solib_loaded (so_list &so)
 {
   interps_notify_solib_loaded (so);
   gdb::observers::solib_loaded.notify (so);
@@ -763,7 +763,7 @@ notify_solib_loaded (so_list *so)
 /* Notify interpreters and observers that solib SO has been unloaded.  */
 
 static void
-notify_solib_unloaded (program_space *pspace, so_list *so)
+notify_solib_unloaded (program_space *pspace, const so_list &so)
 {
   interps_notify_solib_unloaded (so);
   gdb::observers::solib_unloaded.notify (pspace, so);
@@ -840,7 +840,7 @@ update_solib_list (int from_tty)
 	{
 	  if (ops->same)
 	    {
-	      if (ops->same (gdb, i))
+	      if (ops->same (*gdb, *i))
 		break;
 	    }
 	  else
@@ -859,7 +859,7 @@ update_solib_list (int from_tty)
       if (i)
 	{
 	  *i_link = i->next;
-	  free_so (i);
+	  free_so (*i);
 	  gdb_link = &gdb->next;
 	  gdb = *gdb_link;
 	}
@@ -869,7 +869,7 @@ update_solib_list (int from_tty)
 	{
 	  /* Notify any observer that the shared object has been
 	     unloaded before we remove it from GDB's tables.  */
-	  notify_solib_unloaded (current_program_space, gdb);
+	  notify_solib_unloaded (current_program_space, *gdb);
 
 	  current_program_space->deleted_solibs.push_back (gdb->so_name);
 
@@ -881,10 +881,10 @@ update_solib_list (int from_tty)
 	    gdb->objfile->unlink ();
 
 	  /* Some targets' section tables might be referring to
-	     sections from so->abfd; remove them.  */
+	     sections from so.abfd; remove them.  */
 	  current_program_space->remove_target_sections (gdb);
 
-	  free_so (gdb);
+	  free_so (*gdb);
 	  gdb = *gdb_link;
 	}
     }
@@ -910,7 +910,7 @@ update_solib_list (int from_tty)
 	  try
 	    {
 	      /* Fill in the rest of the `struct so_list' node.  */
-	      if (!solib_map_sections (i))
+	      if (!solib_map_sections (*i))
 		{
 		  not_found++;
 		  if (not_found_filename == NULL)
@@ -927,7 +927,7 @@ update_solib_list (int from_tty)
 
 	  /* Notify any observer that the shared object has been
 	     loaded now that we've added it to GDB's tables.  */
-	  notify_solib_loaded (i);
+	  notify_solib_loaded (*i);
 	}
 
       /* If a library was not found, issue an appropriate warning
@@ -972,9 +972,9 @@ libpthread_name_p (const char *name)
 /* Return non-zero if SO is the libpthread shared library.  */
 
 static bool
-libpthread_solib_p (struct so_list *so)
+libpthread_solib_p (const so_list &so)
 {
-  return libpthread_name_p (so->so_name);
+  return libpthread_name_p (so.so_name);
 }
 
 /* Read in symbolic information for any shared objects whose names
@@ -1032,7 +1032,7 @@ solib_add (const char *pattern, int from_tty, int readsyms)
 	     need the library symbols to be loaded in order to provide
 	     thread support (x86-linux for instance).  */
 	  const int add_this_solib =
-	    (readsyms || libpthread_solib_p (gdb));
+	    (readsyms || libpthread_solib_p (*gdb));
 
 	  any_matches = true;
 	  if (add_this_solib)
@@ -1045,7 +1045,7 @@ solib_add (const char *pattern, int from_tty, int readsyms)
 		    gdb_printf (_("Symbols already loaded for %s\n"),
 				gdb->so_name);
 		}
-	      else if (solib_read_symbols (gdb, add_flags))
+	      else if (solib_read_symbols (*gdb, add_flags))
 		loaded_any_symbols = true;
 	    }
 	}
@@ -1172,13 +1172,13 @@ info_sharedlibrary_command (const char *pattern, int from_tty)
 /* See solib.h.  */
 
 bool
-solib_contains_address_p (const struct so_list *const solib,
+solib_contains_address_p (const so_list &solib,
 			  CORE_ADDR address)
 {
-  if (solib->sections == nullptr)
+  if (solib.sections == nullptr)
     return false;
 
-  for (target_section &p : *solib->sections)
+  for (target_section &p : *solib.sections)
     if (p.addr <= address && address < p.endaddr)
       return true;
 
@@ -1202,7 +1202,7 @@ solib_name_from_address (struct program_space *pspace, CORE_ADDR address)
   struct so_list *so = NULL;
 
   for (so = pspace->so_list; so; so = so->next)
-    if (solib_contains_address_p (so, address))
+    if (solib_contains_address_p (*so, address))
       return (so->so_name);
 
   return (0);
@@ -1235,9 +1235,9 @@ clear_solib (void)
       struct so_list *so = current_program_space->so_list;
 
       current_program_space->so_list = so->next;
-      notify_solib_unloaded (current_program_space, so);
+      notify_solib_unloaded (current_program_space, *so);
       current_program_space->remove_target_sections (so);
-      free_so (so);
+      free_so (*so);
     }
 
   if (ops->clear_solib != nullptr)
@@ -1358,7 +1358,7 @@ reload_shared_libraries_1 (int from_tty)
 	      && !solib_used (so))
 	    so->objfile->unlink ();
 	  current_program_space->remove_target_sections (so);
-	  clear_so (so);
+	  clear_so (*so);
 	}
 
       /* If this shared library is now associated with a new symbol
@@ -1371,7 +1371,7 @@ reload_shared_libraries_1 (int from_tty)
 
 	  try
 	    {
-	      solib_map_sections (so);
+	      solib_map_sections (*so);
 	    }
 
 	  catch (const gdb_exception_error &e)
@@ -1383,8 +1383,8 @@ reload_shared_libraries_1 (int from_tty)
 	    }
 
 	    if (!got_error
-		&& (auto_solib_add || was_loaded || libpthread_solib_p (so)))
-	      solib_read_symbols (so, add_flags);
+		&& (auto_solib_add || was_loaded || libpthread_solib_p (*so)))
+	      solib_read_symbols (*so, add_flags);
 	}
     }
 }
diff --git a/gdb/solib.h b/gdb/solib.h
index 77e023002edf..50b154b2b9b6 100644
--- a/gdb/solib.h
+++ b/gdb/solib.h
@@ -50,7 +50,7 @@ extern void clear_solib (void);
 /* Called to add symbols from a shared library to gdb's symbol table.  */
 
 extern void solib_add (const char *, int, int);
-extern bool solib_read_symbols (struct so_list *, symfile_add_flags);
+extern bool solib_read_symbols (so_list &, symfile_add_flags);
 
 /* Function to be called when the inferior starts up, to discover the
    names of shared libraries that are dynamically linked, the base
@@ -65,7 +65,7 @@ extern const char *solib_name_from_address (struct program_space *, CORE_ADDR);
 
 /* Return true if ADDR lies within SOLIB.  */
 
-extern bool solib_contains_address_p (const struct so_list *, CORE_ADDR);
+extern bool solib_contains_address_p (const so_list &, CORE_ADDR);
 
 /* Return whether the data starting at VADDR, size SIZE, must be kept
    in a core file for shared libraries loaded before "gcore" is used
diff --git a/gdb/solist.h b/gdb/solist.h
index b3a06b88c439..5d648900d29d 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -84,17 +84,16 @@ struct target_so_ops
 {
   /* Adjust the section binding addresses by the base address at
      which the object was actually mapped.  */
-  void (*relocate_section_addresses) (struct so_list *so,
-				      struct target_section *);
+  void (*relocate_section_addresses) (so_list &so, target_section *);
 
   /* Free the link map info and any other private data structures
      associated with a so_list entry.  */
-  void (*free_so) (struct so_list *so);
+  void (*free_so) (so_list &so);
 
   /* Reset private data structures associated with SO.
      This is called when SO is about to be reloaded.
      It is also called before free_so when SO is about to be freed.  */
-  void (*clear_so) (struct so_list *so);
+  void (*clear_so) (const so_list &so);
 
   /* Free private data structures associated to PSPACE.  This method
      should not free resources associated to individual so_list entries,
@@ -137,7 +136,7 @@ struct target_so_ops
      if they represent the same library.
      Falls back to using strcmp on so_original_name field when set
      to NULL.  */
-  int (*same) (struct so_list *gdb, struct so_list *inferior);
+  int (*same) (const so_list &gdb, const so_list &inferior);
 
   /* Return whether a region of memory must be kept in a core file
      for shared libraries loaded before "gcore" is used to be
@@ -164,14 +163,14 @@ struct target_so_ops
 using so_list_range = next_range<so_list>;
 
 /* Free the memory associated with a (so_list *).  */
-void free_so (struct so_list *so);
+void free_so (so_list &so);
 
 /* A deleter that calls free_so.  */
 struct so_deleter
 {
   void operator() (struct so_list *so) const
   {
-    free_so (so);
+    free_so (*so);
   }
 };
 
diff --git a/gdb/target-section.h b/gdb/target-section.h
index 9dc716b732bd..5d7c846a6e25 100644
--- a/gdb/target-section.h
+++ b/gdb/target-section.h
@@ -48,7 +48,7 @@ struct target_section
      and used by remove_target_sections.
      For example, for executables it is a pointer to exec_bfd and
      for shlibs it is the so_list pointer.  */
-  void *owner;
+  const void *owner;
 };
 
 /* Holds an array of target sections.  */
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 05/24] gdbsupport: use "reference" and "pointer" type aliases in intrusive_list
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (3 preceding siblings ...)
  2023-10-10 20:39 ` [PATCH 04/24] gdb: replace some so_list parameters to use references Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-10 20:40 ` [PATCH 06/24] gdbsupport: make intrusive_list's disposer accept a reference Simon Marchi
                   ` (20 subsequent siblings)
  25 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

It seems to me like the code should used the defined type aliases, for
consistency.

Change-Id: Ib52493ff18ad29464405275bc10a0c6704ed39e9
---
 gdbsupport/intrusive_list.h | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/gdbsupport/intrusive_list.h b/gdbsupport/intrusive_list.h
index 5c95f7019a82..5e9243867d27 100644
--- a/gdbsupport/intrusive_list.h
+++ b/gdbsupport/intrusive_list.h
@@ -86,7 +86,7 @@ struct intrusive_list_base_iterator
   using node_type = intrusive_list_node<T>;
 
   /* Create an iterator pointing to ELEM.  */
-  explicit intrusive_list_base_iterator (T *elem)
+  explicit intrusive_list_base_iterator (pointer elem)
     : m_elem (elem)
   {}
 
@@ -108,7 +108,7 @@ struct intrusive_list_base_iterator
   { return m_elem != other.m_elem; }
 
 protected:
-  static node_type *as_node (T *elem)
+  static node_type *as_node (pointer elem)
   { return AsNode::as_node (elem); }
 
   /* A past-end-the iterator points to the list's head.  */
@@ -347,9 +347,9 @@ class intrusive_list
       return this->push_back_non_empty (elem);
 
     intrusive_list_node<T> *elem_node = as_node (&elem);
-    T *pos_elem = &*pos;
+    pointer pos_elem = &*pos;
     intrusive_list_node<T> *pos_node = as_node (pos_elem);
-    T *prev_elem = pos_node->prev;
+    pointer prev_elem = pos_node->prev;
     intrusive_list_node<T> *prev_node = as_node (prev_elem);
 
     gdb_assert (elem_node->next == INTRUSIVE_LIST_UNLINKED_VALUE);
@@ -374,11 +374,11 @@ class intrusive_list
       }
 
     /* [A ... B] + [C ... D] */
-    T *b_elem = m_back;
+    pointer b_elem = m_back;
     node_type *b_node = as_node (b_elem);
-    T *c_elem = other.m_front;
+    pointer c_elem = other.m_front;
     node_type *c_node = as_node (c_elem);
-    T *d_elem = other.m_back;
+    pointer d_elem = other.m_back;
 
     b_node->next = c_elem;
     c_node->prev = b_elem;
@@ -402,7 +402,7 @@ class intrusive_list
 
 private:
   /* Push ELEM in the list, knowing the list is empty.  */
-  void push_empty (T &elem)
+  void push_empty (reference elem)
   {
     gdb_assert (this->empty ());
 
@@ -418,7 +418,7 @@ class intrusive_list
   }
 
   /* Push ELEM at the front of the list, knowing the list is not empty.  */
-  void push_front_non_empty (T &elem)
+  void push_front_non_empty (reference elem)
   {
     gdb_assert (!this->empty ());
 
@@ -435,7 +435,7 @@ class intrusive_list
   }
 
   /* Push ELEM at the back of the list, knowing the list is not empty.  */
-  void push_back_non_empty (T &elem)
+  void push_back_non_empty (reference elem)
   {
     gdb_assert (!this->empty ());
 
@@ -451,7 +451,7 @@ class intrusive_list
     m_back = &elem;
   }
 
-  void erase_element (T &elem)
+  void erase_element (reference elem)
   {
     intrusive_list_node<T> *elem_node = as_node (&elem);
 
@@ -585,13 +585,13 @@ class intrusive_list
   }
 
 private:
-  static node_type *as_node (T *elem)
+  static node_type *as_node (pointer elem)
   {
     return AsNode::as_node (elem);
   }
 
-  T *m_front = nullptr;
-  T *m_back = nullptr;
+  pointer m_front = nullptr;
+  pointer m_back = nullptr;
 };
 
 #endif /* GDBSUPPORT_INTRUSIVE_LIST_H */
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 06/24] gdbsupport: make intrusive_list's disposer accept a reference
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (4 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 05/24] gdbsupport: use "reference" and "pointer" type aliases in intrusive_list Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-12 19:05   ` Pedro Alves
  2023-10-10 20:40 ` [PATCH 07/24] gdb: make get_cbfd_soname_build_id static Simon Marchi
                   ` (19 subsequent siblings)
  25 siblings, 1 reply; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

All the API of intrusive_list works with references to item, I'm not
sure why the disposer used in intrusive_list::clear_and_dispose is
different and receives a pointer.  Change it to accept a reference
instead.  This helps simplify a bit a subsequent patch, and I don't see
any downside to it.

Change-Id: I380886cd4ddaf136fe532eb2744f9e69beb41283
---
 gdb/inferior.c                           | 10 +++++-----
 gdb/unittests/intrusive_list-selftests.c |  4 ++--
 gdbsupport/intrusive_list.h              |  2 +-
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/gdb/inferior.c b/gdb/inferior.c
index efe57cceae3e..4a55970ccba1 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -262,13 +262,13 @@ inferior::find_thread (ptid_t ptid)
 void
 inferior::clear_thread_list ()
 {
-  thread_list.clear_and_dispose ([=] (thread_info *thr)
+  thread_list.clear_and_dispose ([=] (thread_info &thr)
     {
       threads_debug_printf ("deleting thread %s",
-			    thr->ptid.to_string ().c_str ());
-      set_thread_exited (thr, {}, true /* silent */);
-      if (thr->deletable ())
-	delete thr;
+			    thr.ptid.to_string ().c_str ());
+      set_thread_exited (&thr, {}, true /* silent */);
+      if (thr.deletable ())
+	delete &thr;
     });
   ptid_thread_map.clear ();
 }
diff --git a/gdb/unittests/intrusive_list-selftests.c b/gdb/unittests/intrusive_list-selftests.c
index 1f85b3266830..d990c0878b35 100644
--- a/gdb/unittests/intrusive_list-selftests.c
+++ b/gdb/unittests/intrusive_list-selftests.c
@@ -692,9 +692,9 @@ struct intrusive_list_test
     list.push_back (b);
     list.push_back (c);
 
-    auto disposer = [&] (const item_type *item)
+    auto disposer = [&] (const item_type &item)
       {
-	disposer_seen.insert (item);
+	disposer_seen.insert (&item);
 	disposer_calls++;
       };
     list.clear_and_dispose (disposer);
diff --git a/gdbsupport/intrusive_list.h b/gdbsupport/intrusive_list.h
index 5e9243867d27..aea1922cabee 100644
--- a/gdbsupport/intrusive_list.h
+++ b/gdbsupport/intrusive_list.h
@@ -513,7 +513,7 @@ class intrusive_list
   {
     while (!this->empty ())
       {
-	pointer p = &front ();
+	reference p = front ();
 	pop_front ();
 	disposer (p);
       }
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 07/24] gdb: make get_cbfd_soname_build_id static
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (5 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 06/24] gdbsupport: make intrusive_list's disposer accept a reference Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-10 20:40 ` [PATCH 08/24] gdb: allocate so_list with new, deallocate with delete Simon Marchi
                   ` (18 subsequent siblings)
  25 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

It is only used in solib.c.

Change-Id: I43461d13d84d65c4f6913d4033678d8983b9910b
---
 gdb/solib.c | 6 ++++--
 gdb/solib.h | 7 -------
 2 files changed, 4 insertions(+), 9 deletions(-)

diff --git a/gdb/solib.c b/gdb/solib.c
index a85ea5458bce..32f9aa7397c0 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -503,9 +503,11 @@ set_cbfd_soname_build_id (gdb_bfd_ref_ptr abfd,
   (*mapptr)[soname] = build_id_to_string (build_id);
 }
 
-/* See solib.h.  */
+/* If SONAME had a build-id associated with it in ABFD's registry by a
+   previous call to set_cbfd_soname_build_id then return the build-id
+   as a NULL-terminated hex string.  */
 
-gdb::unique_xmalloc_ptr<char>
+static gdb::unique_xmalloc_ptr<char>
 get_cbfd_soname_build_id (gdb_bfd_ref_ptr abfd, const char *soname)
 {
   if (abfd.get () == nullptr || soname == nullptr)
diff --git a/gdb/solib.h b/gdb/solib.h
index 50b154b2b9b6..88ade882396d 100644
--- a/gdb/solib.h
+++ b/gdb/solib.h
@@ -143,11 +143,4 @@ extern void set_cbfd_soname_build_id (gdb_bfd_ref_ptr abfd,
 				      const char *soname,
 				      const bfd_build_id *build_id);
 
-/* If SONAME had a build-id associated with it in ABFD's registry by a
-   previous call to set_cbfd_soname_build_id then return the build-id
-   as a NULL-terminated hex string.  */
-
-extern gdb::unique_xmalloc_ptr<char> get_cbfd_soname_build_id
-  (gdb_bfd_ref_ptr abfd, const char *soname);
-
 #endif /* SOLIB_H */
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 08/24] gdb: allocate so_list with new, deallocate with delete
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (6 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 07/24] gdb: make get_cbfd_soname_build_id static Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-10 20:40 ` [PATCH 09/24] gdb: rename lm_info_base to lm_info Simon Marchi
                   ` (17 subsequent siblings)
  25 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Initialize all fields in the class declaration, change allocations to
use "new", change deallocations to use "delete".  This is needed by a
subsequent patches that use C++ stuff in so_list.

Change-Id: I4b140d9f1ec9ff809554a056f76e3eb2b9e23222
---
 gdb/solib-aix.c    |  2 +-
 gdb/solib-darwin.c |  9 +++++----
 gdb/solib-dsbt.c   |  3 +--
 gdb/solib-frv.c    |  3 +--
 gdb/solib-rocm.c   |  6 ++----
 gdb/solib-svr4.c   | 13 ++++---------
 gdb/solib-target.c |  4 ++--
 gdb/solib.c        |  2 +-
 gdb/solist.h       | 18 +++++++++---------
 9 files changed, 26 insertions(+), 34 deletions(-)

diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index cfcc04db1518..515d9d62704a 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -473,7 +473,7 @@ solib_aix_current_sos (void)
      to the main executable, not a shared library.  */
   for (ix = 1; ix < library_list->size (); ix++)
     {
-      struct so_list *new_solib = XCNEW (struct so_list);
+      so_list *new_solib = new so_list;
       std::string so_name;
 
       lm_info_aix &info = (*library_list)[ix];
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index 5720f997bef7..c2f250ae705c 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -273,7 +273,7 @@ darwin_current_sos (void)
 	break;
 
       /* Create and fill the new so_list element.  */
-      gdb::unique_xmalloc_ptr<struct so_list> newobj (XCNEW (struct so_list));
+      so_list *newobj = new so_list;
 
       lm_info_darwin *li = new lm_info_darwin;
       newobj->lm_info = li;
@@ -284,10 +284,11 @@ darwin_current_sos (void)
       li->lm_addr = load_addr;
 
       if (head == NULL)
-	head = newobj.get ();
+	head = newobj;
       else
-	tail->next = newobj.get ();
-      tail = newobj.release ();
+	tail->next = newobj;
+
+      tail = newobj;
     }
 
   return head;
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index 60a211ee6e6e..dd0c8e47d086 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -584,7 +584,6 @@ dsbt_current_sos (void)
       if (dsbt_index != 0)
 	{
 	  struct int_elf32_dsbt_loadmap *loadmap;
-	  struct so_list *sop;
 	  CORE_ADDR addr;
 
 	  loadmap = fetch_loadmap (map_addr);
@@ -595,7 +594,7 @@ dsbt_current_sos (void)
 	      break;
 	    }
 
-	  sop = XCNEW (struct so_list);
+	  so_list *sop = new so_list;
 	  lm_info_dsbt *li = new lm_info_dsbt;
 	  sop->lm_info = li;
 	  li->map = loadmap;
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index c895eb20e434..f823aca57fa0 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -363,7 +363,6 @@ frv_current_sos (void)
       if (got_addr != mgot)
 	{
 	  struct int_elf32_fdpic_loadmap *loadmap;
-	  struct so_list *sop;
 	  CORE_ADDR addr;
 
 	  /* Fetch the load map address.  */
@@ -378,7 +377,7 @@ frv_current_sos (void)
 	      break;
 	    }
 
-	  sop = XCNEW (struct so_list);
+	  so_list *sop = new so_list;
 	  lm_info_frv *li = new lm_info_frv;
 	  sop->lm_info = li;
 	  li->map = loadmap;
diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
index 65dd1c06271f..e46d272b3339 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -217,9 +217,7 @@ rocm_solib_copy_list (const so_list *src)
 
   while (src != nullptr)
     {
-      struct so_list *newobj;
-
-      newobj = XNEW (struct so_list);
+      so_list *newobj = new so_list;
       memcpy (newobj, src, sizeof (struct so_list));
 
       lm_info_svr4 *src_li = (lm_info_svr4 *) src->lm_info;
@@ -738,7 +736,7 @@ rocm_update_solib_list ()
       if (status != AMD_DBGAPI_STATUS_SUCCESS)
 	continue;
 
-      struct so_list *so = XCNEW (struct so_list);
+      so_list *so = new so_list;
       lm_info_svr4 *li = new lm_info_svr4;
       li->l_addr = l_addr;
       so->lm_info = li;
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index b32848944cad..6fae926d5e62 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -1021,9 +1021,7 @@ svr4_copy_library_list (struct so_list *src)
 
   while (src != NULL)
     {
-      struct so_list *newobj;
-
-      newobj = XNEW (struct so_list);
+      so_list *newobj = new so_list;
       memcpy (newobj, src, sizeof (struct so_list));
 
       lm_info_svr4 *src_li = (lm_info_svr4 *) src->lm_info;
@@ -1061,9 +1059,8 @@ library_list_start_library (struct gdb_xml_parser *parser,
     = (ULONGEST *) xml_find_attribute (attributes, "l_addr")->value.get ();
   ULONGEST *l_ldp
     = (ULONGEST *) xml_find_attribute (attributes, "l_ld")->value.get ();
-  struct so_list *new_elem;
 
-  new_elem = XCNEW (struct so_list);
+  so_list *new_elem = new so_list;
   lm_info_svr4 *li = new lm_info_svr4;
   new_elem->lm_info = li;
   li->lm_addr = *lmp;
@@ -1246,12 +1243,10 @@ svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
 static struct so_list *
 svr4_default_sos (svr4_info *info)
 {
-  struct so_list *newobj;
-
   if (!info->debug_loader_offset_p)
     return NULL;
 
-  newobj = XCNEW (struct so_list);
+  so_list *newobj = new so_list;
   lm_info_svr4 *li = new lm_info_svr4;
   newobj->lm_info = li;
 
@@ -1283,7 +1278,7 @@ svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
 
   for (; lm != 0; prev_lm = lm, lm = next_lm)
     {
-      so_list_up newobj (XCNEW (struct so_list));
+      so_list_up newobj (new so_list);
 
       lm_info_svr4 *li = lm_info_read (lm).release ();
       newobj->lm_info = li;
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index b8b6dd644018..d2459b5dc81b 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -230,7 +230,7 @@ solib_target_parse_libraries (const char *library)
 static struct so_list *
 solib_target_current_sos (void)
 {
-  struct so_list *new_solib, *start = NULL, *last = NULL;
+  so_list *start = NULL, *last = NULL;
 
   /* Fetch the list of shared libraries.  */
   gdb::optional<gdb::char_vector> library_document
@@ -249,7 +249,7 @@ solib_target_current_sos (void)
   /* Build a struct so_list for each entry on the list.  */
   for (auto &&info : library_list)
     {
-      new_solib = XCNEW (struct so_list);
+      so_list *new_solib = new so_list;
       strncpy (new_solib->so_name, info->name.c_str (),
 	       SO_NAME_MAX_PATH_SIZE - 1);
       new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
diff --git a/gdb/solib.c b/gdb/solib.c
index 32f9aa7397c0..816dacb31829 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -677,7 +677,7 @@ free_so (so_list &so)
   clear_so (so);
   ops->free_so (so);
 
-  xfree (&so);
+  delete &so;
 }
 
 
diff --git a/gdb/solist.h b/gdb/solist.h
index 5d648900d29d..8b80ed4a6cc7 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -37,23 +37,23 @@ struct so_list
      dynamic linker's tables in the inferior, and are initialized by
      current_sos.  */
 
-  struct so_list *next;	/* next structure in linked list */
+  struct so_list *next = nullptr;	/* next structure in linked list */
 
   /* A pointer to target specific link map information.  Often this
      will be a copy of struct link_map from the user process, but
      it need not be; it can be any collection of data needed to
      traverse the dynamic linker's data structures.  */
-  lm_info_base *lm_info;
+  lm_info_base *lm_info = nullptr;
 
   /* Shared object file name, exactly as it appears in the
      inferior's link map.  This may be a relative path, or something
      which needs to be looked up in LD_LIBRARY_PATH, etc.  We use it
      to tell which entries in the inferior's dynamic linker's link
      map we've already loaded.  */
-  char so_original_name[SO_NAME_MAX_PATH_SIZE];
+  char so_original_name[SO_NAME_MAX_PATH_SIZE] {};
 
   /* Shared object file name, expanded to something GDB can open.  */
-  char so_name[SO_NAME_MAX_PATH_SIZE];
+  char so_name[SO_NAME_MAX_PATH_SIZE] {};
 
   /* The following fields of the structure are built from
      information gathered from the shared object file itself, and
@@ -61,15 +61,15 @@ struct so_list
 
      current_sos must initialize these fields to 0.  */
 
-  bfd *abfd;
-  char symbols_loaded;	/* flag: symbols read in yet?  */
+  bfd *abfd = nullptr;
+  char symbols_loaded = 0;	/* flag: symbols read in yet?  */
 
   /* objfile with symbols for a loaded library.  Target memory is read from
      ABFD.  OBJFILE may be NULL either before symbols have been loaded, if
      the file cannot be found or after the command "nosharedlibrary".  */
-  struct objfile *objfile;
+  struct objfile *objfile = nullptr;
 
-  target_section_table *sections;
+  target_section_table *sections = nullptr;
 
   /* Record the range of addresses belonging to this shared library.
      There may not be just one (e.g. if two segments are relocated
@@ -77,7 +77,7 @@ struct so_list
      the MI command "-file-list-shared-libraries".  The latter has a format
      that supports outputting multiple segments once the related code
      supports them.  */
-  CORE_ADDR addr_low, addr_high;
+  CORE_ADDR addr_low = 0, addr_high = 0;
 };
 
 struct target_so_ops
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 09/24] gdb: rename lm_info_base to lm_info
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (7 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 08/24] gdb: allocate so_list with new, deallocate with delete Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-10 20:40 ` [PATCH 10/24] gdb: remove target_so_ops::free_so Simon Marchi
                   ` (16 subsequent siblings)
  25 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

The base class doesn't need to have "_base" in its name, all the
sub-classes have a specific suffix.

Change-Id: I87652105cfedd87898770a81f0eda343ff7f2bdb
---
 gdb/solib-aix.c    | 2 +-
 gdb/solib-darwin.c | 2 +-
 gdb/solib-dsbt.c   | 2 +-
 gdb/solib-frv.c    | 2 +-
 gdb/solib-svr4.h   | 2 +-
 gdb/solib-target.c | 2 +-
 gdb/solist.h       | 4 ++--
 7 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index 515d9d62704a..7e7333e20dd1 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -28,7 +28,7 @@
 
 /* Our private data in struct so_list.  */
 
-struct lm_info_aix : public lm_info_base
+struct lm_info_aix : public lm_info
 {
   /* The name of the file mapped by the loader.  Apart from the entry
      for the main executable, this is usually a shared library (which,
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index c2f250ae705c..2ca2d533a9b4 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -135,7 +135,7 @@ darwin_load_image_infos (struct darwin_info *info)
 
 /* Link map info to include in an allocated so_list entry.  */
 
-struct lm_info_darwin : public lm_info_base
+struct lm_info_darwin : public lm_info
 {
   /* The target location of lm.  */
   CORE_ADDR lm_addr = 0;
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index dd0c8e47d086..5458ef51571a 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -123,7 +123,7 @@ struct dbst_ext_link_map
 
 /* Link map info to include in an allocated so_list entry */
 
-struct lm_info_dsbt : public lm_info_base
+struct lm_info_dsbt : public lm_info
 {
   ~lm_info_dsbt ()
   {
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index f823aca57fa0..caf8a9a87701 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -196,7 +196,7 @@ struct ext_link_map
 
 /* Link map info to include in an allocated so_list entry.  */
 
-struct lm_info_frv : public lm_info_base
+struct lm_info_frv : public lm_info
 {
   ~lm_info_frv ()
   {
diff --git a/gdb/solib-svr4.h b/gdb/solib-svr4.h
index ff15603476ed..050c702f620b 100644
--- a/gdb/solib-svr4.h
+++ b/gdb/solib-svr4.h
@@ -29,7 +29,7 @@ extern const struct target_so_ops svr4_so_ops;
 
 /* Link map info to include in an allocated so_list entry.  */
 
-struct lm_info_svr4 : public lm_info_base
+struct lm_info_svr4 : public lm_info
 {
   /* Amount by which addresses in the binary should be relocated to
      match the inferior.  The direct inferior value is L_ADDR_INFERIOR.
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index d2459b5dc81b..8e6a440c4673 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -28,7 +28,7 @@
 #include "inferior.h"
 
 /* Private data for each loaded library.  */
-struct lm_info_target : public lm_info_base
+struct lm_info_target : public lm_info
 {
   /* The library's name.  The name is normally kept in the struct
      so_list; it is only here during XML parsing.  */
diff --git a/gdb/solist.h b/gdb/solist.h
index 8b80ed4a6cc7..51b1ceb0398e 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -27,7 +27,7 @@
 
 /* Base class for target-specific link map information.  */
 
-struct lm_info_base
+struct lm_info
 {
 };
 
@@ -43,7 +43,7 @@ struct so_list
      will be a copy of struct link_map from the user process, but
      it need not be; it can be any collection of data needed to
      traverse the dynamic linker's data structures.  */
-  lm_info_base *lm_info = nullptr;
+  struct lm_info *lm_info = nullptr;
 
   /* Shared object file name, exactly as it appears in the
      inferior's link map.  This may be a relative path, or something
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 10/24] gdb: remove target_so_ops::free_so
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (8 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 09/24] gdb: rename lm_info_base to lm_info Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-10 20:40 ` [PATCH 11/24] gdb: use gdb::checked_static_cast when casting lm_info Simon Marchi
                   ` (15 subsequent siblings)
  25 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

target_so_ops::free_so is responsible for freeing the specific lm_info
object.  All implementations basically just call delete.  Remove that
method, make the destructor of lm_info virtual, and call delete directly
from the free_so function.  Make the sub-classes final, just because
it's good practice.

Change-Id: Iee1fd4861c75034a9e41a656add8ed8dfd8964ee
---
 gdb/solib-aix.c    | 15 +--------------
 gdb/solib-darwin.c | 11 +----------
 gdb/solib-dsbt.c   | 11 +----------
 gdb/solib-frv.c    | 11 +----------
 gdb/solib-svr4.c   | 11 -----------
 gdb/solib-svr4.h   |  2 +-
 gdb/solib-target.c | 13 +------------
 gdb/solib.c        |  7 +++----
 gdb/solist.h       |  9 ++++-----
 9 files changed, 13 insertions(+), 77 deletions(-)

diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index 7e7333e20dd1..7ec6462a8de1 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -28,7 +28,7 @@
 
 /* Our private data in struct so_list.  */
 
-struct lm_info_aix : public lm_info
+struct lm_info_aix final : public lm_info
 {
   /* The name of the file mapped by the loader.  Apart from the entry
      for the main executable, this is usually a shared library (which,
@@ -360,18 +360,6 @@ solib_aix_relocate_section_addresses (so_list &so, target_section *sec)
     }
 }
 
-/* Implement the "free_so" target_so_ops method.  */
-
-static void
-solib_aix_free_so (so_list &so)
-{
-  lm_info_aix *li = (lm_info_aix *) so.lm_info;
-
-  solib_debug_printf ("%s", so.so_name);
-
-  delete li;
-}
-
 /* Compute and return the OBJFILE's section_offset array, using
    the associated loader info (INFO).  */
 
@@ -704,7 +692,6 @@ solib_aix_normal_stop_observer (struct bpstat *unused_1, int unused_2)
 const struct target_so_ops solib_aix_so_ops =
 {
   solib_aix_relocate_section_addresses,
-  solib_aix_free_so,
   nullptr,
   nullptr,
   solib_aix_solib_create_inferior_hook,
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index 2ca2d533a9b4..cb6ed3a70afc 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -135,7 +135,7 @@ darwin_load_image_infos (struct darwin_info *info)
 
 /* Link map info to include in an allocated so_list entry.  */
 
-struct lm_info_darwin : public lm_info
+struct lm_info_darwin final : public lm_info
 {
   /* The target location of lm.  */
   CORE_ADDR lm_addr = 0;
@@ -608,14 +608,6 @@ darwin_clear_solib (program_space *pspace)
   info->all_image.version = 0;
 }
 
-static void
-darwin_free_so (so_list &so)
-{
-  lm_info_darwin *li = (lm_info_darwin *) so.lm_info;
-
-  delete li;
-}
-
 /* The section table is built from bfd sections using bfd VMAs.
    Relocate these VMAs according to solib info.  */
 
@@ -673,7 +665,6 @@ darwin_bfd_open (const char *pathname)
 const struct target_so_ops darwin_so_ops =
 {
   darwin_relocate_section_addresses,
-  darwin_free_so,
   nullptr,
   darwin_clear_solib,
   darwin_solib_create_inferior_hook,
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index 5458ef51571a..af99afc33caa 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -123,7 +123,7 @@ struct dbst_ext_link_map
 
 /* Link map info to include in an allocated so_list entry */
 
-struct lm_info_dsbt : public lm_info
+struct lm_info_dsbt final : public lm_info
 {
   ~lm_info_dsbt ()
   {
@@ -879,14 +879,6 @@ dsbt_clear_solib (program_space *pspace)
   info->main_executable_lm_info = NULL;
 }
 
-static void
-dsbt_free_so (so_list &so)
-{
-  lm_info_dsbt *li = (lm_info_dsbt *) so.lm_info;
-
-  delete li;
-}
-
 static void
 dsbt_relocate_section_addresses (so_list &so, target_section *sec)
 {
@@ -917,7 +909,6 @@ show_dsbt_debug (struct ui_file *file, int from_tty,
 const struct target_so_ops dsbt_so_ops =
 {
   dsbt_relocate_section_addresses,
-  dsbt_free_so,
   nullptr,
   dsbt_clear_solib,
   dsbt_solib_create_inferior_hook,
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index caf8a9a87701..853d90a8c86c 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -196,7 +196,7 @@ struct ext_link_map
 
 /* Link map info to include in an allocated so_list entry.  */
 
-struct lm_info_frv : public lm_info
+struct lm_info_frv final : public lm_info
 {
   ~lm_info_frv ()
   {
@@ -815,14 +815,6 @@ frv_clear_solib (program_space *pspace)
   main_executable_lm_info = NULL;
 }
 
-static void
-frv_free_so (so_list &so)
-{
-  lm_info_frv *li = (lm_info_frv *) so.lm_info;
-
-  delete li;
-}
-
 static void
 frv_relocate_section_addresses (so_list &so, target_section *sec)
 {
@@ -1089,7 +1081,6 @@ frv_fetch_objfile_link_map (struct objfile *objfile)
 const struct target_so_ops frv_so_ops =
 {
   frv_relocate_section_addresses,
-  frv_free_so,
   nullptr,
   frv_clear_solib,
   frv_solib_create_inferior_hook,
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 6fae926d5e62..0bf8bb8446c7 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -976,16 +976,6 @@ svr4_free_objfile_observer (struct objfile *objfile)
   probes_table_remove_objfile_probes (objfile);
 }
 
-/* Implementation for target_so_ops.free_so.  */
-
-static void
-svr4_free_so (so_list &so)
-{
-  lm_info_svr4 *li = (lm_info_svr4 *) so.lm_info;
-
-  delete li;
-}
-
 /* Implement target_so_ops.clear_so.  */
 
 static void
@@ -3465,7 +3455,6 @@ svr4_iterate_over_objfiles_in_search_order
 const struct target_so_ops svr4_so_ops =
 {
   svr4_relocate_section_addresses,
-  svr4_free_so,
   svr4_clear_so,
   svr4_clear_solib,
   svr4_solib_create_inferior_hook,
diff --git a/gdb/solib-svr4.h b/gdb/solib-svr4.h
index 050c702f620b..1aff3b59da8c 100644
--- a/gdb/solib-svr4.h
+++ b/gdb/solib-svr4.h
@@ -29,7 +29,7 @@ extern const struct target_so_ops svr4_so_ops;
 
 /* Link map info to include in an allocated so_list entry.  */
 
-struct lm_info_svr4 : public lm_info
+struct lm_info_svr4 final : public lm_info
 {
   /* Amount by which addresses in the binary should be relocated to
      match the inferior.  The direct inferior value is L_ADDR_INFERIOR.
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index 8e6a440c4673..f79b59a36de0 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -28,7 +28,7 @@
 #include "inferior.h"
 
 /* Private data for each loaded library.  */
-struct lm_info_target : public lm_info
+struct lm_info_target final : public lm_info
 {
   /* The library's name.  The name is normally kept in the struct
      so_list; it is only here during XML parsing.  */
@@ -281,16 +281,6 @@ solib_target_solib_create_inferior_hook (int from_tty)
   /* Nothing needed.  */
 }
 
-static void
-solib_target_free_so (so_list &so)
-{
-  lm_info_target *li = (lm_info_target *) so.lm_info;
-
-  gdb_assert (li->name.empty ());
-
-  delete li;
-}
-
 static void
 solib_target_relocate_section_addresses (so_list &so, target_section *sec)
 {
@@ -431,7 +421,6 @@ solib_target_in_dynsym_resolve_code (CORE_ADDR pc)
 const struct target_so_ops solib_target_so_ops =
 {
   solib_target_relocate_section_addresses,
-  solib_target_free_so,
   nullptr,
   nullptr,
   solib_target_solib_create_inferior_hook,
diff --git a/gdb/solib.c b/gdb/solib.c
index 816dacb31829..4799fabe8213 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -658,6 +658,8 @@ clear_so (so_list &so)
     ops->clear_so (so);
 }
 
+lm_info::~lm_info () = default;
+
 /* Free the storage associated with the `struct so_list' object SO.
    If we have opened a BFD for SO, close it.
 
@@ -672,11 +674,8 @@ clear_so (so_list &so)
 void
 free_so (so_list &so)
 {
-  const target_so_ops *ops = gdbarch_so_ops (current_inferior ()->arch ());
-
   clear_so (so);
-  ops->free_so (so);
-
+  delete so.lm_info;
   delete &so;
 }
 
diff --git a/gdb/solist.h b/gdb/solist.h
index 51b1ceb0398e..12ea516a10cf 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -29,6 +29,9 @@
 
 struct lm_info
 {
+  lm_info () = default;
+  lm_info (const lm_info &) = default;
+  virtual ~lm_info () = 0;
 };
 
 struct so_list
@@ -86,13 +89,9 @@ struct target_so_ops
      which the object was actually mapped.  */
   void (*relocate_section_addresses) (so_list &so, target_section *);
 
-  /* Free the link map info and any other private data structures
-     associated with a so_list entry.  */
-  void (*free_so) (so_list &so);
-
   /* Reset private data structures associated with SO.
      This is called when SO is about to be reloaded.
-     It is also called before free_so when SO is about to be freed.  */
+     It is also called when SO is about to be freed.  */
   void (*clear_so) (const so_list &so);
 
   /* Free private data structures associated to PSPACE.  This method
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 11/24] gdb: use gdb::checked_static_cast when casting lm_info
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (9 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 10/24] gdb: remove target_so_ops::free_so Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-10 20:40 ` [PATCH 12/24] gdb: make solib-svr4 not use so_list internally Simon Marchi
                   ` (14 subsequent siblings)
  25 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Now that the lm_info class hierarchy has a virtual destructor and
therefore a vtable, use checked_static_cast instead of C-style cases to
ensure (when building in dev mode) that we're casting to the right kind
of lm_info.

Change-Id: I9a99b7d6aa9a44edbe76377d57a7008cfb75a744
---
 gdb/nto-tdep.c     |  2 +-
 gdb/solib-aix.c    |  2 +-
 gdb/solib-darwin.c |  2 +-
 gdb/solib-dsbt.c   |  2 +-
 gdb/solib-frv.c    |  8 ++++----
 gdb/solib-rocm.c   |  4 ++--
 gdb/solib-svr4.c   | 16 ++++++++--------
 gdb/solib-target.c |  2 +-
 8 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index 1d9c7340fcf7..67e3c7042754 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -247,7 +247,7 @@ nto_parse_redirection (char *pargv[], const char **pin, const char **pout,
 static CORE_ADDR
 lm_addr (const so_list &so)
 {
-  const lm_info_svr4 *li = (const lm_info_svr4 *) so.lm_info;
+  auto *li = gdb::checked_static_cast<const lm_info_svr4 *> (so.lm_info);
 
   return li->l_addr;
 }
diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index 7ec6462a8de1..9d232a03ae7b 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -316,7 +316,7 @@ solib_aix_relocate_section_addresses (so_list &so, target_section *sec)
   struct bfd_section *bfd_sect = sec->the_bfd_section;
   bfd *abfd = bfd_sect->owner;
   const char *section_name = bfd_section_name (bfd_sect);
-  lm_info_aix *info = (lm_info_aix *) so.lm_info;
+  auto *info = gdb::checked_static_cast<lm_info_aix *> (so.lm_info);
 
   if (strcmp (section_name, ".text") == 0)
     {
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index cb6ed3a70afc..c42082cfb368 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -614,7 +614,7 @@ darwin_clear_solib (program_space *pspace)
 static void
 darwin_relocate_section_addresses (so_list &so, target_section *sec)
 {
-  lm_info_darwin *li = (lm_info_darwin *) so.lm_info;
+  auto *li = gdb::checked_static_cast<lm_info_darwin *> (so.lm_info);
 
   sec->addr += li->lm_addr;
   sec->endaddr += li->lm_addr;
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index af99afc33caa..dca4080dee15 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -883,7 +883,7 @@ static void
 dsbt_relocate_section_addresses (so_list &so, target_section *sec)
 {
   int seg;
-  lm_info_dsbt *li = (lm_info_dsbt *) so.lm_info;
+  auto *li = gdb::checked_static_cast<lm_info_dsbt *> (so.lm_info);
   int_elf32_dsbt_loadmap *map = li->map;
 
   for (seg = 0; seg < map->nsegs; seg++)
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index 853d90a8c86c..47a81b88efe7 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -819,7 +819,7 @@ static void
 frv_relocate_section_addresses (so_list &so, target_section *sec)
 {
   int seg;
-  lm_info_frv *li = (lm_info_frv *) so.lm_info;
+  auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info);
   int_elf32_fdpic_loadmap *map = li->map;
 
   for (seg = 0; seg < map->nsegs; seg++)
@@ -860,7 +860,7 @@ frv_fdpic_find_global_pointer (CORE_ADDR addr)
   for (struct so_list *so : current_program_space->solibs ())
     {
       int seg;
-      lm_info_frv *li = (lm_info_frv *) so->lm_info;
+      auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info);
       int_elf32_fdpic_loadmap *map = li->map;
 
       for (seg = 0; seg < map->nsegs; seg++)
@@ -916,7 +916,7 @@ frv_fdpic_find_canonical_descriptor (CORE_ADDR entry_point)
     {
       for (struct so_list *so : current_program_space->solibs ())
 	{
-	  lm_info_frv *li = (lm_info_frv *) so->lm_info;
+	  auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info);
 
 	  addr = find_canonical_descriptor_in_load_object
 		   (entry_point, got_value, name, so->abfd, li);
@@ -1068,7 +1068,7 @@ frv_fetch_objfile_link_map (struct objfile *objfile)
      of shared libraries.  */
   for (struct so_list *so : current_program_space->solibs ())
     {
-      lm_info_frv *li = (lm_info_frv *) so->lm_info;
+      auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info);
 
       if (so->objfile == objfile)
 	return li->lm_addr;
diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
index e46d272b3339..403173ecc998 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -186,7 +186,7 @@ rocm_solib_relocate_section_addresses (so_list &so,
       return;
     }
 
-  lm_info_svr4 *li = (lm_info_svr4 *) so.lm_info;
+  auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info);
   sec->addr = sec->addr + li->l_addr;
   sec->endaddr = sec->endaddr + li->l_addr;
 }
@@ -220,7 +220,7 @@ rocm_solib_copy_list (const so_list *src)
       so_list *newobj = new so_list;
       memcpy (newobj, src, sizeof (struct so_list));
 
-      lm_info_svr4 *src_li = (lm_info_svr4 *) src->lm_info;
+      auto *src_li = gdb::checked_static_cast<lm_info_svr4 *> (src->lm_info);
       newobj->lm_info = new lm_info_svr4 (*src_li);
 
       newobj->next = nullptr;
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 0bf8bb8446c7..7d247fa455e9 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -181,8 +181,8 @@ svr4_same (const so_list &gdb, const so_list &inferior)
   /* There may be different instances of the same library, in different
      namespaces.  Each instance, however, must have been loaded at a
      different address so its relocation offset would be different.  */
-  const lm_info_svr4 *lmg = (const lm_info_svr4 *) gdb.lm_info;
-  const lm_info_svr4 *lmi = (const lm_info_svr4 *) inferior.lm_info;
+  auto *lmg = gdb::checked_static_cast<const lm_info_svr4 *> (gdb.lm_info);
+  auto *lmi = gdb::checked_static_cast<const lm_info_svr4 *> (inferior.lm_info);
 
   return (lmg->l_addr_inferior == lmi->l_addr_inferior);
 }
@@ -231,7 +231,7 @@ has_lm_dynamic_from_link_map (void)
 static CORE_ADDR
 lm_addr_check (const so_list &so, bfd *abfd)
 {
-  lm_info_svr4 *li = (lm_info_svr4 *) so.lm_info;
+  auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info);
 
   if (!li->l_addr_p)
     {
@@ -981,7 +981,7 @@ svr4_free_objfile_observer (struct objfile *objfile)
 static void
 svr4_clear_so (const so_list &so)
 {
-  lm_info_svr4 *li = (lm_info_svr4 *) so.lm_info;
+  auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info);
 
   if (li != NULL)
     li->l_addr_p = 0;
@@ -1014,7 +1014,7 @@ svr4_copy_library_list (struct so_list *src)
       so_list *newobj = new so_list;
       memcpy (newobj, src, sizeof (struct so_list));
 
-      lm_info_svr4 *src_li = (lm_info_svr4 *) src->lm_info;
+      auto *src_li = gdb::checked_static_cast<lm_info_svr4 *> (src->lm_info);
       newobj->lm_info = new lm_info_svr4 (*src_li);
 
       newobj->next = NULL;
@@ -1561,7 +1561,7 @@ svr4_current_sos (void)
 		[ 9] .dynamic DYNAMIC ffffffffff700580 000580 0000f0
 	  */
 
-	  lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
+	  auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so->lm_info);
 
 	  if (address_in_mem_range (li->l_ld, &vsyscall_range))
 	    {
@@ -1597,7 +1597,7 @@ svr4_fetch_objfile_link_map (struct objfile *objfile)
   for (struct so_list *so : current_program_space->solibs ())
     if (so->objfile == objfile)
       {
-	lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
+	auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so->lm_info);
 
 	return li->lm_addr;
       }
@@ -1880,7 +1880,7 @@ solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
       for (; solist->next != nullptr; solist = solist->next)
 	/* Nothing.  */;
 
-      lm_info_svr4 *li = (lm_info_svr4 *) solist->lm_info;
+      auto *li = gdb::checked_static_cast<lm_info_svr4 *> (solist->lm_info);
       prev_lm = li->lm_addr;
       pnext = &solist->next;
     }
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index f79b59a36de0..05b35103091e 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -285,7 +285,7 @@ static void
 solib_target_relocate_section_addresses (so_list &so, target_section *sec)
 {
   CORE_ADDR offset;
-  lm_info_target *li = (lm_info_target *) so.lm_info;
+  auto *li = gdb::checked_static_cast<lm_info_target *> (so.lm_info);
 
   /* Build the offset table only once per object file.  We can not do
      it any earlier, since we need to open the file first.  */
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 12/24] gdb: make solib-svr4 not use so_list internally
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (10 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 11/24] gdb: use gdb::checked_static_cast when casting lm_info Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-13 17:52   ` Lancelot SIX
  2023-10-19 11:08   ` Lancelot SIX
  2023-10-10 20:40 ` [PATCH 13/24] gdb: make solib-rocm " Simon Marchi
                   ` (13 subsequent siblings)
  25 siblings, 2 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

A subsequent patch makes use of non-trivial types in struct so_list.
This trips on the fact that svr4_copy_library_list uses memcpy to copy
so_list objects:

      so_list *newobj = new so_list;
      memcpy (newobj, src, sizeof (struct so_list));

solib-svr4 maintains lists of so_list objects in its own internal data
structures.  When requested to return a list of so_list objects (through
target_so_ops::current_sos), it duplicates the internal so_list lists,
using memcpy.  When changing so_list to make it non-trivial, we would
need to replace this use of memcpy somehow.  That would mean making
so_list copyable, with all the complexity that entails, just to satisfy
this internal usage of solib-svr4 (and solib-rocm, which does the same).

Change solib-svr4 to use its own data type for its internal lists.  The
use of so_list is a bit overkill anyway, as most fields of so_list are
irrelevant for this internal use.

 - Introduce svr4_so, which contains just an std::string for the name
   and a unique_ptr for the lm_info.
 - Change the internal so_list lists to be std::vector<svr4_so>.  Vector
   seems like a good choice for this, we don't need to insert/remove
   elements in the middle of these internal lists.
 - Remove svr4_free_library_list, free_solib_lists and ~svr4_info, as
   everything is managed automatically now.
 - Replace svr4_copy_library_list (which duplicated internal lists in
   order to return them to the core) with so_list_from_svr4_sos, which
   creates an so_list list from a vector of svr4_so.
 - Generalize svr4_same a bit, because find_debug_base_for_solib now
   needs to compare an so_list and an svr4_so to see if they are the
   same.

Change-Id: I6012e48e07aace2a8172b74b389f9547ce777877
---
 gdb/solib-svr4.c | 275 +++++++++++++++++------------------------------
 gdb/solib-svr4.h |   2 +
 2 files changed, 100 insertions(+), 177 deletions(-)

diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 7d247fa455e9..9266929d2895 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -50,7 +50,6 @@
 static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
 static int svr4_have_link_map_offsets (void);
 static void svr4_relocate_main_executable (void);
-static void svr4_free_library_list (so_list *solist);
 static void probes_table_remove_objfile_probes (struct objfile *objfile);
 static void svr4_iterate_over_objfiles_in_search_order
   (gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype cb,
@@ -172,26 +171,35 @@ svr4_same_1 (const char *gdb_so_name, const char *inferior_so_name)
   return 0;
 }
 
-static int
-svr4_same (const so_list &gdb, const so_list &inferior)
+static bool
+svr4_same (const char *gdb_name, const char *inferior_name,
+	   const lm_info_svr4 &gdb_lm_info,
+	   const lm_info_svr4 &inferior_lm_info)
 {
-  if (!svr4_same_1 (gdb.so_original_name, inferior.so_original_name))
+  if (!svr4_same_1 (gdb_name, inferior_name))
     return false;
 
   /* There may be different instances of the same library, in different
      namespaces.  Each instance, however, must have been loaded at a
      different address so its relocation offset would be different.  */
+  return gdb_lm_info.l_addr_inferior == inferior_lm_info.l_addr_inferior;
+}
+
+static int
+svr4_same (const so_list &gdb, const so_list &inferior)
+{
   auto *lmg = gdb::checked_static_cast<const lm_info_svr4 *> (gdb.lm_info);
   auto *lmi = gdb::checked_static_cast<const lm_info_svr4 *> (inferior.lm_info);
 
-  return (lmg->l_addr_inferior == lmi->l_addr_inferior);
+  return svr4_same (gdb.so_original_name, inferior.so_original_name,
+		    *lmg, *lmi);
 }
 
-static std::unique_ptr<lm_info_svr4>
+static lm_info_svr4_up
 lm_info_read (CORE_ADDR lm_addr)
 {
   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
-  std::unique_ptr<lm_info_svr4> lm_info;
+  lm_info_svr4_up lm_info;
 
   gdb::byte_vector lm (lmo->link_map_size);
 
@@ -203,7 +211,7 @@ lm_info_read (CORE_ADDR lm_addr)
       type *ptr_type
 	= builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
 
-      lm_info.reset (new lm_info_svr4);
+      lm_info = gdb::make_unique<lm_info_svr4> ();
       lm_info->lm_addr = lm_addr;
 
       lm_info->l_addr_inferior = extract_typed_address (&lm[lmo->l_addr_offset],
@@ -333,13 +341,20 @@ lm_addr_check (const so_list &so, bfd *abfd)
   return li->l_addr;
 }
 
+struct svr4_so
+{
+  svr4_so (const char *name, lm_info_svr4_up lm_info)
+    : name (name), lm_info (std::move (lm_info))
+  {}
+
+  std::string name;
+  lm_info_svr4_up lm_info;
+};
+
 /* Per pspace SVR4 specific data.  */
 
 struct svr4_info
 {
-  svr4_info () = default;
-  ~svr4_info ();
-
   /* Base of dynamic linker structures in default namespace.  */
   CORE_ADDR debug_base = 0;
 
@@ -385,7 +400,7 @@ struct svr4_info
 
      The special entry zero is reserved for a linear list to support
      gdbstubs that do not support namespaces.  */
-  std::map<CORE_ADDR, so_list *> solib_lists;
+  std::map<CORE_ADDR, std::vector<svr4_so>> solib_lists;
 };
 
 /* Per-program-space data key.  */
@@ -407,23 +422,6 @@ free_probes_table (struct svr4_info *info)
   info->probes_table.reset (nullptr);
 }
 
-/* Free the solib lists for all namespaces.  */
-
-static void
-free_solib_lists (svr4_info *info)
-{
-  for (const std::pair<CORE_ADDR, so_list *> tuple
-	 : info->solib_lists)
-    svr4_free_library_list (tuple.second);
-
-  info->solib_lists.clear ();
-}
-
-svr4_info::~svr4_info ()
-{
-  free_solib_lists (this);
-}
-
 /* Get the svr4 data for program space PSPACE.  If none is found yet, add it now.
    This function always returns a valid object.  */
 
@@ -955,7 +953,7 @@ struct svr4_library_list
 {
   /* The tail pointer of the current namespace.  This is internal to XML
      parsing.  */
-  so_list **tailp;
+  std::vector<svr4_so> *cur_list;
 
   /* Inferior address of struct link_map used for the main executable.  It is
      NULL if not known.  */
@@ -965,7 +963,7 @@ struct svr4_library_list
      not include any default sos.
 
      See comment on struct svr4_info.solib_lists.  */
-  std::map<CORE_ADDR, so_list *> solib_lists;
+  std::map<CORE_ADDR, std::vector<svr4_so>> solib_lists;
 };
 
 /* This module's 'free_objfile' observer.  */
@@ -987,41 +985,27 @@ svr4_clear_so (const so_list &so)
     li->l_addr_p = 0;
 }
 
-/* Free so_list built so far.  */
-
-static void
-svr4_free_library_list (so_list *list)
-{
-  while (list != NULL)
-    {
-      struct so_list *next = list->next;
+/* Create the so_list objects equivalent to the svr4_sos in SOS.  */
 
-      free_so (*list);
-      list = next;
-    }
-}
-
-/* Copy library list.  */
-
-static struct so_list *
-svr4_copy_library_list (struct so_list *src)
+static so_list *
+so_list_from_svr4_sos (const std::vector<svr4_so> &sos)
 {
   struct so_list *dst = NULL;
   struct so_list **link = &dst;
 
-  while (src != NULL)
+  for (const svr4_so &so : sos)
     {
       so_list *newobj = new so_list;
-      memcpy (newobj, src, sizeof (struct so_list));
 
-      auto *src_li = gdb::checked_static_cast<lm_info_svr4 *> (src->lm_info);
-      newobj->lm_info = new lm_info_svr4 (*src_li);
+      strncpy (newobj->so_name, so.name.c_str (),
+	       sizeof (newobj->so_name) - 1);                                                                                                                
+      newobj->so_name[sizeof (newobj->so_name) - 1] = 0;                                                                                                                            
+      strcpy (newobj->so_original_name, newobj->so_name);    
+      newobj->lm_info = new lm_info_svr4 (*so.lm_info);
 
       newobj->next = NULL;
       *link = newobj;
       link = &newobj->next;
-
-      src = src->next;
     }
 
   return dst;
@@ -1050,48 +1034,25 @@ library_list_start_library (struct gdb_xml_parser *parser,
   ULONGEST *l_ldp
     = (ULONGEST *) xml_find_attribute (attributes, "l_ld")->value.get ();
 
-  so_list *new_elem = new so_list;
-  lm_info_svr4 *li = new lm_info_svr4;
-  new_elem->lm_info = li;
+  lm_info_svr4_up li = gdb::make_unique<lm_info_svr4> ();
   li->lm_addr = *lmp;
   li->l_addr_inferior = *l_addrp;
   li->l_ld = *l_ldp;
 
-  strncpy (new_elem->so_name, name, sizeof (new_elem->so_name) - 1);
-  new_elem->so_name[sizeof (new_elem->so_name) - 1] = 0;
-  strcpy (new_elem->so_original_name, new_elem->so_name);
+  std::vector<svr4_so> *solist;
 
   /* Older versions did not supply lmid.  Put the element into the flat
      list of the special namespace zero in that case.  */
   gdb_xml_value *at_lmid = xml_find_attribute (attributes, "lmid");
   if (at_lmid == nullptr)
-    {
-      *list->tailp = new_elem;
-      list->tailp = &new_elem->next;
-    }
+    solist = list->cur_list;
   else
     {
       ULONGEST lmid = *(ULONGEST *) at_lmid->value.get ();
-
-      /* Ensure that the element is actually initialized.  */
-      if (list->solib_lists.find (lmid) == list->solib_lists.end ())
-	list->solib_lists[lmid] = nullptr;
-
-      so_list **psolist = &list->solib_lists[lmid];
-      so_list **pnext = psolist;
-
-      /* Walk to the end of the list if we have one.  */
-      so_list *solist = *psolist;
-      if (solist != nullptr)
-	{
-	  for (; solist->next != nullptr; solist = solist->next)
-	    /* Nothing.  */;
-
-	  pnext = &solist->next;
-	}
-
-      *pnext = new_elem;
+      solist = &list->solib_lists[lmid];
     }
+
+  solist->emplace_back (name, std::move (li));
 }
 
 /* Handle the start of a <library-list-svr4> element.  */
@@ -1117,9 +1078,7 @@ svr4_library_list_start_list (struct gdb_xml_parser *parser,
 
   /* Older gdbserver do not support namespaces.  We use the special
      namespace zero for a linear list of libraries.  */
-  so_list **solist = &list->solib_lists[0];
-  *solist = nullptr;
-  list->tailp = solist;
+  list->cur_list = &list->solib_lists[0];
 }
 
 /* The allowed elements and attributes for an XML library list.
@@ -1169,13 +1128,9 @@ static int
 svr4_parse_libraries (const char *document, struct svr4_library_list *list)
 {
   auto cleanup = make_scope_exit ([list] ()
-    {
-      for (const std::pair<CORE_ADDR, so_list *> tuple
-	     : list->solib_lists)
-	svr4_free_library_list (tuple.second);
-    });
+    {  list->solib_lists.clear (); });
 
-  list->tailp = nullptr;
+  list->cur_list = nullptr;
   list->main_lm = 0;
   list->solib_lists.clear ();
   if (gdb_xml_parse_quick (_("target library list"), "library-list-svr4.dtd",
@@ -1253,25 +1208,21 @@ svr4_default_sos (svr4_info *info)
 
 /* Read the whole inferior libraries chain starting at address LM.
    Expect the first entry in the chain's previous entry to be PREV_LM.
-   Add the entries to the tail referenced by LINK_PTR_PTR.  Ignore the
-   first entry if IGNORE_FIRST and set global MAIN_LM_ADDR according
-   to it.  Returns nonzero upon success.  If zero is returned the
-   entries stored to LINK_PTR_PTR are still valid although they may
+   Add the entries to SOS.  Ignore the first entry if IGNORE_FIRST and set
+   global MAIN_LM_ADDR according to it.  Returns nonzero upon success.  If zero
+   is returned the entries stored to LINK_PTR_PTR are still valid although they may
    represent only part of the inferior library list.  */
 
 static int
 svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
-		   struct so_list ***link_ptr_ptr, int ignore_first)
+		   std::vector<svr4_so> &sos, int ignore_first)
 {
   CORE_ADDR first_l_name = 0;
   CORE_ADDR next_lm;
 
   for (; lm != 0; prev_lm = lm, lm = next_lm)
     {
-      so_list_up newobj (new so_list);
-
-      lm_info_svr4 *li = lm_info_read (lm).release ();
-      newobj->lm_info = li;
+      lm_info_svr4_up li = lm_info_read (lm);
       if (li == NULL)
 	return 0;
 
@@ -1298,9 +1249,9 @@ svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
 	}
 
       /* Extract this shared object's name.  */
-      gdb::unique_xmalloc_ptr<char> buffer
+      gdb::unique_xmalloc_ptr<char> name
 	= target_read_string (li->l_name, SO_NAME_MAX_PATH_SIZE - 1);
-      if (buffer == nullptr)
+      if (name == nullptr)
 	{
 	  /* If this entry's l_name address matches that of the
 	     inferior executable, then this is not a normal shared
@@ -1311,19 +1262,12 @@ svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
 	  continue;
 	}
 
-      strncpy (newobj->so_name, buffer.get (), SO_NAME_MAX_PATH_SIZE - 1);
-      newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
-      strcpy (newobj->so_original_name, newobj->so_name);
-
       /* If this entry has no name, or its name matches the name
 	 for the main executable, don't include it in the list.  */
-      if (! newobj->so_name[0] || match_main (newobj->so_name))
+      if (*name == '\0' || match_main (name.get ()))
 	continue;
 
-      newobj->next = 0;
-      /* Don't free it now.  */
-      **link_ptr_ptr = newobj.release ();
-      *link_ptr_ptr = &(**link_ptr_ptr)->next;
+      sos.emplace_back (name.get (), std::move (li));
     }
 
   return 1;
@@ -1342,7 +1286,7 @@ svr4_current_sos_direct (struct svr4_info *info)
   struct svr4_library_list library_list;
 
   /* Remove any old libraries.  We're going to read them back in again.  */
-  free_solib_lists (info);
+  info->solib_lists.clear ();
 
   /* Fall back to manual examination of the target if the packet is not
      supported or gdbserver failed to find DT_DEBUG.  gdb.server/solib-list.exp
@@ -1362,10 +1306,10 @@ svr4_current_sos_direct (struct svr4_info *info)
       /* Remove an empty special zero namespace so we know that when there
 	 is one, it is actually used, and we have a flat list without
 	 namespace information.  */
-      if ((library_list.solib_lists.find (0)
-	   != library_list.solib_lists.end ())
-	  && (library_list.solib_lists[0] == nullptr))
-	library_list.solib_lists.erase (0);
+      auto it_0 = library_list.solib_lists.find (0);
+      if (it_0 != library_list.solib_lists.end ()
+	  && it_0->second.empty ())
+	library_list.solib_lists.erase (it_0);
 
       /* Replace the (empty) solib_lists in INFO with the one generated
 	 from the target.  We don't want to copy it on assignment and then
@@ -1391,9 +1335,7 @@ svr4_current_sos_direct (struct svr4_info *info)
     ignore_first = true;
 
   auto cleanup = make_scope_exit ([info] ()
-    {
-      free_solib_lists (info);
-    });
+    { info->solib_lists.clear (); });
 
   /* Collect the sos in each namespace.  */
   CORE_ADDR debug_base = info->debug_base;
@@ -1403,12 +1345,8 @@ svr4_current_sos_direct (struct svr4_info *info)
       /* Walk the inferior's link map list, and build our so_list list.  */
       lm = solib_svr4_r_map (debug_base);
       if (lm != 0)
-	{
-	  so_list **sos = &info->solib_lists[debug_base];
-	  *sos = nullptr;
-
-	  svr4_read_so_list (info, lm, 0, &sos, ignore_first);
-	}
+	svr4_read_so_list (info, lm, 0, info->solib_lists[debug_base],
+			   ignore_first);
     }
 
   /* On Solaris, the dynamic linker is not in the normal list of
@@ -1425,11 +1363,8 @@ svr4_current_sos_direct (struct svr4_info *info)
     {
       /* Add the dynamic linker's namespace unless we already did.  */
       if (info->solib_lists.find (debug_base) == info->solib_lists.end ())
-	{
-	  so_list **sos = &info->solib_lists[debug_base];
-	  *sos = nullptr;
-	  svr4_read_so_list (info, debug_base, 0, &sos, 0);
-	}
+	svr4_read_so_list (info, debug_base, 0, info->solib_lists[debug_base],
+			   0);
     }
 
   cleanup.release ();
@@ -1440,19 +1375,18 @@ svr4_current_sos_direct (struct svr4_info *info)
 static so_list *
 svr4_collect_probes_sos (svr4_info *info)
 {
-  so_list *sos = nullptr;
-  so_list **pnext = &sos;
+  so_list *res = nullptr;
+  so_list **pnext = &res;
 
-  for (const std::pair<CORE_ADDR, so_list *> tuple
-	 : info->solib_lists)
+  for (const auto &tuple : info->solib_lists)
     {
-      so_list *solist = tuple.second;
+      const std::vector<svr4_so> &sos = tuple.second;
 
       /* Allow the linker to report empty namespaces.  */
-      if (solist == nullptr)
+      if (sos.empty ())
 	continue;
 
-      *pnext = svr4_copy_library_list (solist);
+      *pnext = so_list_from_svr4_sos (sos);
 
       /* Update PNEXT to point to the next member of the last element.  */
       gdb_assert (*pnext != nullptr);
@@ -1466,7 +1400,7 @@ svr4_collect_probes_sos (svr4_info *info)
 	}
     }
 
-  return sos;
+  return res;
 }
 
 /* Implement the main part of the "current_sos" target_so_ops
@@ -1853,16 +1787,10 @@ solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
   if (info->solib_lists.find (0) != info->solib_lists.end ())
     return 0;
 
-  /* Ensure that the element is actually initialized.  */
-  if (info->solib_lists.find (debug_base) == info->solib_lists.end ())
-    info->solib_lists[debug_base] = nullptr;
-
-  so_list **psolist = &info->solib_lists[debug_base];
-  so_list **pnext = nullptr;
-  so_list *solist = *psolist;
+  std::vector<svr4_so> &solist = info->solib_lists[debug_base];
   CORE_ADDR prev_lm;
 
-  if (solist == nullptr)
+  if (solist.empty ())
     {
       /* svr4_current_sos_direct contains logic to handle a number of
 	 special cases relating to the first elements of the list in
@@ -1872,18 +1800,9 @@ solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
 	return 0;
 
       prev_lm = 0;
-      pnext = psolist;
     }
   else
-    {
-      /* Walk to the end of the list.  */
-      for (; solist->next != nullptr; solist = solist->next)
-	/* Nothing.  */;
-
-      auto *li = gdb::checked_static_cast<lm_info_svr4 *> (solist->lm_info);
-      prev_lm = li->lm_addr;
-      pnext = &solist->next;
-    }
+    prev_lm = solist.back ().lm_info->lm_addr;
 
   /* Read the new objects.  */
   if (info->using_xfer)
@@ -1905,24 +1824,23 @@ solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
 
 	 We expect gdbserver to provide updates for the namespace that
 	 contains LM, which would be this namespace...  */
-      so_list *sos = nullptr;
-      if (library_list.solib_lists.find (debug_base)
-	  != library_list.solib_lists.end ())
-	std::swap (sos, library_list.solib_lists[debug_base]);
-      if (sos == nullptr)
+      std::vector<svr4_so> sos;
+      auto it_debug_base = library_list.solib_lists.find (debug_base);
+      if (it_debug_base != library_list.solib_lists.end ())
+	std::swap (sos, it_debug_base->second);
+      else
 	{
 	  /* ...or for the special zero namespace for earlier versions...  */
-	  if (library_list.solib_lists.find (0)
-	      != library_list.solib_lists.end ())
-	    std::swap (sos, library_list.solib_lists[0]);
+	  auto it_0 = library_list.solib_lists.find (0);
+	  if (it_0 != library_list.solib_lists.end ())
+	    std::swap (sos, it_0->second);
 	}
 
       /* ...but nothing else.  */
-      for (const std::pair<CORE_ADDR, so_list *> tuple
-	     : library_list.solib_lists)
-	gdb_assert (tuple.second == nullptr);
+      for (const auto &tuple : library_list.solib_lists)
+	gdb_assert (tuple.second.empty ());
 
-      *pnext = sos;
+      std::move (sos.begin (), sos.end (), std::back_inserter (solist));
     }
   else
     {
@@ -1930,7 +1848,7 @@ solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
 	 above check and deferral to solist_update_full ensures
 	 that this call to svr4_read_so_list will never see the
 	 first element.  */
-      if (!svr4_read_so_list (info, lm, prev_lm, &pnext, 0))
+      if (!svr4_read_so_list (info, lm, prev_lm, solist, 0))
 	return 0;
     }
 
@@ -1948,7 +1866,7 @@ disable_probes_interface (svr4_info *info)
 	     "Reverting to original interface."));
 
   free_probes_table (info);
-  free_solib_lists (info);
+  info->solib_lists.clear ();
 }
 
 /* Update the solib list as appropriate when using the
@@ -3145,7 +3063,7 @@ svr4_solib_create_inferior_hook (int from_tty)
 
   /* Clear the probes-based interface's state.  */
   free_probes_table (info);
-  free_solib_lists (info);
+  info->solib_lists.clear ();
 
   /* Relocate the main executable if necessary.  */
   svr4_relocate_main_executable ();
@@ -3373,14 +3291,17 @@ find_debug_base_for_solib (so_list *solib)
 
   svr4_info *info = get_svr4_info (current_program_space);
   gdb_assert (info != nullptr);
-  for (const std::pair<CORE_ADDR, so_list *> tuple
-	 : info->solib_lists)
+
+  const lm_info_svr4 *lm_info = (const lm_info_svr4 *) solib->lm_info;
+
+  for (const auto &tuple : info->solib_lists)
     {
       CORE_ADDR debug_base = tuple.first;
-      so_list *solist = tuple.second;
+      const std::vector<svr4_so> &sos = tuple.second;
 
-      for (; solist != nullptr; solist = solist->next)
-	if (svr4_same (*solib, *solist))
+      for (const svr4_so &so : sos)
+	if (svr4_same (solib->so_original_name, so.name.c_str (),
+		       *lm_info, *so.lm_info))
 	  return debug_base;
     }
 
diff --git a/gdb/solib-svr4.h b/gdb/solib-svr4.h
index 1aff3b59da8c..7cb6cabb109f 100644
--- a/gdb/solib-svr4.h
+++ b/gdb/solib-svr4.h
@@ -48,6 +48,8 @@ struct lm_info_svr4 final : public lm_info
   CORE_ADDR l_ld = 0, l_next = 0, l_prev = 0, l_name = 0;
 };
 
+using lm_info_svr4_up = std::unique_ptr<lm_info_svr4>;
+
 /* Critical offsets and sizes which describe struct r_debug and
    struct link_map on SVR4-like targets.  All offsets and sizes are
    in bytes unless otherwise specified.  */
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 13/24] gdb: make solib-rocm not use so_list internally
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (11 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 12/24] gdb: make solib-svr4 not use so_list internally Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-13 18:35   ` Lancelot SIX
  2023-10-17 15:23   ` Pedro Alves
  2023-10-10 20:40 ` [PATCH 14/24] gdb: remove lm_info_vector typedef Simon Marchi
                   ` (12 subsequent siblings)
  25 siblings, 2 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Same rationale as the previous patch, but for solib-rocm.

 - Introduce rocm_so, which is a name a unique_name (see comment in
   rocm_update_solib_list for that) and a unique_ptr to the
   lm_info_svr4.
 - Change the internal lists from so_list lists to vectors of rocm_so.
 - Remove rocm_free_solib_list, as everything is automatic now.
 - Replace rocm_solib_copy_list with so_list_from_rocm_sos.

Change-Id: I71e06e3ea22d6420c9e4e500501c06e9a13398a8
---
 gdb/solib-rocm.c | 92 ++++++++++++++++++++++--------------------------
 1 file changed, 42 insertions(+), 50 deletions(-)

diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
index 403173ecc998..5016c383a807 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -126,14 +126,26 @@ rocm_solib_fd_cache::close (int fd, fileio_error *target_errno)
 
 /* ROCm-specific inferior data.  */
 
+struct rocm_so
+{
+  rocm_so (const char *name, std::string unique_name, lm_info_svr4_up lm_info)
+    : name (name),
+      unique_name (std::move(unique_name)),
+      lm_info (std::move (lm_info))
+  {}
+
+  std::string name, unique_name;
+  lm_info_svr4_up lm_info;
+};
+
 struct solib_info
 {
   explicit solib_info (inferior *inf)
-    : solib_list (nullptr), fd_cache (inf)
+    : fd_cache (inf)
   {};
 
   /* List of code objects loaded into the inferior.  */
-  so_list *solib_list;
+  std::vector<rocm_so> solib_list;
 
   /* Cache of opened FD in the inferior.  */
   rocm_solib_fd_cache fd_cache;
@@ -144,23 +156,6 @@ static const registry<inferior>::key<solib_info> rocm_solib_data;
 
 static target_so_ops rocm_solib_ops;
 
-/* Free the solib linked list.  */
-
-static void
-rocm_free_solib_list (struct solib_info *info)
-{
-  while (info->solib_list != nullptr)
-    {
-      struct so_list *next = info->solib_list->next;
-
-      free_so (*info->solib_list);
-      info->solib_list = next;
-    }
-
-  info->solib_list = nullptr;
-}
-
-
 /* Fetch the solib_info data for INF.  */
 
 static struct solib_info *
@@ -207,27 +202,29 @@ rocm_solib_handle_event ()
   rocm_update_solib_list ();
 }
 
-/* Make a deep copy of the solib linked list.  */
+/* Create so_list objects from rocm_so objects in SOS.  */
 
 static so_list *
-rocm_solib_copy_list (const so_list *src)
+so_list_from_rocm_sos (const std::vector<rocm_so> &sos)
 {
   struct so_list *dst = nullptr;
   struct so_list **link = &dst;
 
-  while (src != nullptr)
+  for (const rocm_so &so : sos)
     {
       so_list *newobj = new so_list;
-      memcpy (newobj, src, sizeof (struct so_list));
+      newobj->lm_info = new lm_info_svr4 (*so.lm_info);
+
+      strncpy (newobj->so_name, so.name.c_str (), sizeof (newobj->so_name));
+      newobj->so_name[sizeof (newobj->so_name) - 1] = '\0';
 
-      auto *src_li = gdb::checked_static_cast<lm_info_svr4 *> (src->lm_info);
-      newobj->lm_info = new lm_info_svr4 (*src_li);
+      strncpy (newobj->so_original_name, so.unique_name.c_str (),
+	       sizeof (newobj->so_original_name));
+      newobj->so_original_name[sizeof (newobj->so_original_name) - 1] = '\0';
 
       newobj->next = nullptr;
       *link = newobj;
       link = &newobj->next;
-
-      src = src->next;
     }
 
   return dst;
@@ -243,21 +240,21 @@ rocm_solib_current_sos ()
   so_list *head = svr4_so_ops.current_sos ();
 
   /* Then, the device-side shared library list.  */
-  so_list *list = get_solib_info (current_inferior ())->solib_list;
+  std::vector<rocm_so> &dev_sos = get_solib_info (current_inferior ())->solib_list;
 
-  if (list == nullptr)
+  if (dev_sos.empty ())
     return head;
 
-  list = rocm_solib_copy_list (list);
+  so_list *dev_so_list = so_list_from_rocm_sos (dev_sos);
 
   if (head == nullptr)
-    return list;
+    return dev_so_list;
 
   /* Append our libraries to the end of the list.  */
   so_list *tail;
   for (tail = head; tail->next; tail = tail->next)
     /* Nothing.  */;
-  tail->next = list;
+  tail->next = dev_so_list;
 
   return head;
 }
@@ -687,7 +684,7 @@ rocm_solib_bfd_open (const char *pathname)
 static void
 rocm_solib_create_inferior_hook (int from_tty)
 {
-  rocm_free_solib_list (get_solib_info (current_inferior ()));
+  get_solib_info (current_inferior ())->solib_list.clear ();
 
   svr4_so_ops.solib_create_inferior_hook (from_tty);
 }
@@ -703,8 +700,8 @@ rocm_update_solib_list ()
 
   solib_info *info = get_solib_info (inf);
 
-  rocm_free_solib_list (info);
-  struct so_list **link = &info->solib_list;
+  info->solib_list.clear ();
+  std::vector<rocm_so> &sos = info->solib_list;
 
   amd_dbgapi_code_object_id_t *code_object_list;
   size_t count;
@@ -736,24 +733,18 @@ rocm_update_solib_list ()
       if (status != AMD_DBGAPI_STATUS_SUCCESS)
 	continue;
 
-      so_list *so = new so_list;
-      lm_info_svr4 *li = new lm_info_svr4;
-      li->l_addr = l_addr;
-      so->lm_info = li;
+      gdb::unique_xmalloc_ptr<char> uri_bytes_holder (uri_bytes);
 
-      strncpy (so->so_name, uri_bytes, sizeof (so->so_name));
-      so->so_name[sizeof (so->so_name) - 1] = '\0';
-      xfree (uri_bytes);
+      lm_info_svr4_up li = gdb::make_unique<lm_info_svr4> ();
+      li->l_addr = l_addr;
 
-      /* Make so_original_name unique so that code objects with the same URI
-	 but different load addresses are seen by gdb core as different shared
+      /* Generate a unique name so that code objects with the same URI but
+	 different load addresses are seen by gdb core as different shared
 	 objects.  */
-      xsnprintf (so->so_original_name, sizeof (so->so_original_name),
-		 "code_object_%ld", code_object_list[i].handle);
+      std::string unique_name
+        = string_printf ("code_object_%ld", code_object_list[i].handle);
 
-      so->next = nullptr;
-      *link = so;
-      link = &so->next;
+      sos.emplace_back (uri_bytes, std::move (unique_name), std::move (li));
     }
 
   xfree (code_object_list);
@@ -778,7 +769,8 @@ rocm_update_solib_list ()
 static void
 rocm_solib_target_inferior_created (inferior *inf)
 {
-  rocm_free_solib_list (get_solib_info (inf));
+  get_solib_info (inf)->solib_list.clear ();
+
   rocm_update_solib_list ();
 
   /* Force GDB to reload the solibs.  */
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 14/24] gdb: remove lm_info_vector typedef
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (12 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 13/24] gdb: make solib-rocm " Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-10 20:40 ` [PATCH 15/24] gdb: make so_list::lm_info a unique_ptr Simon Marchi
                   ` (11 subsequent siblings)
  25 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

I think this typedef hinders readability.  First, it's not well named
(it's not clear it contains lm_info_target objects).  And hiding the
fact that it contains unique pointers is not very useful either.  I was
looking at the code in solib_target_current_sos where the unique
pointers get moved from the vector, and it wasn't obvious at all what
the source of the move was.

Change-Id: I4a5cda7c90554f018b7c466b1535b41d69cbcbe7
---
 gdb/solib-target.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index 05b35103091e..e20a336f9ad3 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -50,7 +50,7 @@ struct lm_info_target final : public lm_info
   section_offsets offsets;
 };
 
-typedef std::vector<std::unique_ptr<lm_info_target>> lm_info_vector;
+using lm_info_target_up = std::unique_ptr<lm_info_target>;
 
 #if !defined(HAVE_LIBEXPAT)
 
@@ -81,7 +81,7 @@ library_list_start_segment (struct gdb_xml_parser *parser,
 			    void *user_data,
 			    std::vector<gdb_xml_value> &attributes)
 {
-  lm_info_vector *list = (lm_info_vector *) user_data;
+  auto *list = (std::vector<lm_info_target_up> *) user_data;
   lm_info_target *last = list->back ().get ();
   ULONGEST *address_p
     = (ULONGEST *) xml_find_attribute (attributes, "address")->value.get ();
@@ -100,7 +100,7 @@ library_list_start_section (struct gdb_xml_parser *parser,
 			    void *user_data,
 			    std::vector<gdb_xml_value> &attributes)
 {
-  lm_info_vector *list = (lm_info_vector *) user_data;
+  auto *list = (std::vector<lm_info_target_up> *) user_data;
   lm_info_target *last = list->back ().get ();
   ULONGEST *address_p
     = (ULONGEST *) xml_find_attribute (attributes, "address")->value.get ();
@@ -121,7 +121,7 @@ library_list_start_library (struct gdb_xml_parser *parser,
 			    void *user_data,
 			    std::vector<gdb_xml_value> &attributes)
 {
-  lm_info_vector *list = (lm_info_vector *) user_data;
+  auto *list = (std::vector<lm_info_target_up> *) user_data;
   lm_info_target *item = new lm_info_target;
   item->name
     = (const char *) xml_find_attribute (attributes, "name")->value.get ();
@@ -134,7 +134,7 @@ library_list_end_library (struct gdb_xml_parser *parser,
 			  const struct gdb_xml_element *element,
 			  void *user_data, const char *body_text)
 {
-  lm_info_vector *list = (lm_info_vector *) user_data;
+  auto *list = (std::vector<lm_info_target_up> *) user_data;
   lm_info_target *lm_info = list->back ().get ();
 
   if (lm_info->segment_bases.empty () && lm_info->section_bases.empty ())
@@ -210,10 +210,10 @@ static const struct gdb_xml_element library_list_elements[] = {
   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
 };
 
-static lm_info_vector
+static std::vector<lm_info_target_up>
 solib_target_parse_libraries (const char *library)
 {
-  lm_info_vector result;
+  std::vector<lm_info_target_up> result;
 
   if (gdb_xml_parse_quick (_("target library list"), "library-list.dtd",
 			   library_list_elements, library, &result) == 0)
@@ -240,14 +240,14 @@ solib_target_current_sos (void)
     return NULL;
 
   /* Parse the list.  */
-  lm_info_vector library_list
+  std::vector<lm_info_target_up> library_list
     = solib_target_parse_libraries (library_document->data ());
 
   if (library_list.empty ())
     return NULL;
 
   /* Build a struct so_list for each entry on the list.  */
-  for (auto &&info : library_list)
+  for (lm_info_target_up &info : library_list)
     {
       so_list *new_solib = new so_list;
       strncpy (new_solib->so_name, info->name.c_str (),
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 15/24] gdb: make so_list::lm_info a unique_ptr
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (13 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 14/24] gdb: remove lm_info_vector typedef Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-10 20:40 ` [PATCH 16/24] gdb: make clear_so a method of struct so_list Simon Marchi
                   ` (10 subsequent siblings)
  25 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Make it a unique_ptr, so it gets automatically deleted when the so_list
is deleted.

Change-Id: Ib62d60ae2a80656239860b80e4359121c93da13d
---
 gdb/nto-tdep.c     |  2 +-
 gdb/solib-aix.c    |  4 ++--
 gdb/solib-darwin.c |  7 ++++---
 gdb/solib-dsbt.c   |  7 ++++---
 gdb/solib-frv.c    | 13 +++++++------
 gdb/solib-rocm.c   |  4 ++--
 gdb/solib-svr4.c   | 25 +++++++++++++++----------
 gdb/solib-target.c |  4 ++--
 gdb/solib.c        |  1 -
 gdb/solist.h       |  4 +++-
 10 files changed, 40 insertions(+), 31 deletions(-)

diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index 67e3c7042754..bcc32fd857ed 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -247,7 +247,7 @@ nto_parse_redirection (char *pargv[], const char **pin, const char **pout,
 static CORE_ADDR
 lm_addr (const so_list &so)
 {
-  auto *li = gdb::checked_static_cast<const lm_info_svr4 *> (so.lm_info);
+  auto *li = gdb::checked_static_cast<const lm_info_svr4 *> (so.lm_info.get ());
 
   return li->l_addr;
 }
diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index 9d232a03ae7b..a9931a60f336 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -316,7 +316,7 @@ solib_aix_relocate_section_addresses (so_list &so, target_section *sec)
   struct bfd_section *bfd_sect = sec->the_bfd_section;
   bfd *abfd = bfd_sect->owner;
   const char *section_name = bfd_section_name (bfd_sect);
-  auto *info = gdb::checked_static_cast<lm_info_aix *> (so.lm_info);
+  auto *info = gdb::checked_static_cast<lm_info_aix *> (so.lm_info.get ());
 
   if (strcmp (section_name, ".text") == 0)
     {
@@ -487,7 +487,7 @@ solib_aix_current_sos (void)
       new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
       memcpy (new_solib->so_name, new_solib->so_original_name,
 	      SO_NAME_MAX_PATH_SIZE);
-      new_solib->lm_info = new lm_info_aix (info);
+      new_solib->lm_info = gdb::make_unique<lm_info_aix> (info);
 
       /* Add it to the list.  */
       if (!start)
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index c42082cfb368..e97c693052bc 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -275,14 +275,15 @@ darwin_current_sos (void)
       /* Create and fill the new so_list element.  */
       so_list *newobj = new so_list;
 
-      lm_info_darwin *li = new lm_info_darwin;
-      newobj->lm_info = li;
+      auto li = gdb::make_unique<lm_info_darwin> ();
 
       strncpy (newobj->so_name, file_path.get (), SO_NAME_MAX_PATH_SIZE - 1);
       newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
       strcpy (newobj->so_original_name, newobj->so_name);
       li->lm_addr = load_addr;
 
+      newobj->lm_info = std::move (li);
+
       if (head == NULL)
 	head = newobj;
       else
@@ -614,7 +615,7 @@ darwin_clear_solib (program_space *pspace)
 static void
 darwin_relocate_section_addresses (so_list &so, target_section *sec)
 {
-  auto *li = gdb::checked_static_cast<lm_info_darwin *> (so.lm_info);
+  auto *li = gdb::checked_static_cast<lm_info_darwin *> (so.lm_info.get ());
 
   sec->addr += li->lm_addr;
   sec->endaddr += li->lm_addr;
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index dca4080dee15..6f4de80782a9 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -595,8 +595,7 @@ dsbt_current_sos (void)
 	    }
 
 	  so_list *sop = new so_list;
-	  lm_info_dsbt *li = new lm_info_dsbt;
-	  sop->lm_info = li;
+	  auto li = gdb::make_unique<lm_info_dsbt> ();
 	  li->map = loadmap;
 	  /* Fetch the name.  */
 	  addr = extract_unsigned_integer (lm_buf.l_name,
@@ -618,6 +617,8 @@ dsbt_current_sos (void)
 	      strcpy (sop->so_original_name, sop->so_name);
 	    }
 
+	  sop->lm_info = std::move (li);
+
 	  *sos_next_ptr = sop;
 	  sos_next_ptr = &sop->next;
 	}
@@ -883,7 +884,7 @@ static void
 dsbt_relocate_section_addresses (so_list &so, target_section *sec)
 {
   int seg;
-  auto *li = gdb::checked_static_cast<lm_info_dsbt *> (so.lm_info);
+  auto *li = gdb::checked_static_cast<lm_info_dsbt *> (so.lm_info.get ());
   int_elf32_dsbt_loadmap *map = li->map;
 
   for (seg = 0; seg < map->nsegs; seg++)
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index 47a81b88efe7..5cbab1493128 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -378,8 +378,7 @@ frv_current_sos (void)
 	    }
 
 	  so_list *sop = new so_list;
-	  lm_info_frv *li = new lm_info_frv;
-	  sop->lm_info = li;
+	  auto li = gdb::make_unique<lm_info_frv> ();
 	  li->map = loadmap;
 	  li->got_value = got_addr;
 	  li->lm_addr = lm_addr;
@@ -402,6 +401,8 @@ frv_current_sos (void)
 	      strcpy (sop->so_original_name, sop->so_name);
 	    }
 
+	  sop->lm_info = std::move (li);
+
 	  *sos_next_ptr = sop;
 	  sos_next_ptr = &sop->next;
 	}
@@ -819,7 +820,7 @@ static void
 frv_relocate_section_addresses (so_list &so, target_section *sec)
 {
   int seg;
-  auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info);
+  auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info.get ());
   int_elf32_fdpic_loadmap *map = li->map;
 
   for (seg = 0; seg < map->nsegs; seg++)
@@ -860,7 +861,7 @@ frv_fdpic_find_global_pointer (CORE_ADDR addr)
   for (struct so_list *so : current_program_space->solibs ())
     {
       int seg;
-      auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info);
+      auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info.get ());
       int_elf32_fdpic_loadmap *map = li->map;
 
       for (seg = 0; seg < map->nsegs; seg++)
@@ -916,7 +917,7 @@ frv_fdpic_find_canonical_descriptor (CORE_ADDR entry_point)
     {
       for (struct so_list *so : current_program_space->solibs ())
 	{
-	  auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info);
+	  auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info.get ());
 
 	  addr = find_canonical_descriptor_in_load_object
 		   (entry_point, got_value, name, so->abfd, li);
@@ -1068,7 +1069,7 @@ frv_fetch_objfile_link_map (struct objfile *objfile)
      of shared libraries.  */
   for (struct so_list *so : current_program_space->solibs ())
     {
-      auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info);
+      auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info.get ());
 
       if (so->objfile == objfile)
 	return li->lm_addr;
diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
index 5016c383a807..3bd5dec6d052 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -181,7 +181,7 @@ rocm_solib_relocate_section_addresses (so_list &so,
       return;
     }
 
-  auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info);
+  auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info.get ());
   sec->addr = sec->addr + li->l_addr;
   sec->endaddr = sec->endaddr + li->l_addr;
 }
@@ -213,7 +213,7 @@ so_list_from_rocm_sos (const std::vector<rocm_so> &sos)
   for (const rocm_so &so : sos)
     {
       so_list *newobj = new so_list;
-      newobj->lm_info = new lm_info_svr4 (*so.lm_info);
+      newobj->lm_info = gdb::make_unique<lm_info_svr4> (*so.lm_info);
 
       strncpy (newobj->so_name, so.name.c_str (), sizeof (newobj->so_name));
       newobj->so_name[sizeof (newobj->so_name) - 1] = '\0';
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 9266929d2895..6a54cd332085 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -188,8 +188,10 @@ svr4_same (const char *gdb_name, const char *inferior_name,
 static int
 svr4_same (const so_list &gdb, const so_list &inferior)
 {
-  auto *lmg = gdb::checked_static_cast<const lm_info_svr4 *> (gdb.lm_info);
-  auto *lmi = gdb::checked_static_cast<const lm_info_svr4 *> (inferior.lm_info);
+  auto *lmg
+    = gdb::checked_static_cast<const lm_info_svr4 *> (gdb.lm_info.get ());
+  auto *lmi
+    = gdb::checked_static_cast<const lm_info_svr4 *> (inferior.lm_info.get ());
 
   return svr4_same (gdb.so_original_name, inferior.so_original_name,
 		    *lmg, *lmi);
@@ -239,7 +241,7 @@ has_lm_dynamic_from_link_map (void)
 static CORE_ADDR
 lm_addr_check (const so_list &so, bfd *abfd)
 {
-  auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info);
+  auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info.get ());
 
   if (!li->l_addr_p)
     {
@@ -979,7 +981,7 @@ svr4_free_objfile_observer (struct objfile *objfile)
 static void
 svr4_clear_so (const so_list &so)
 {
-  auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info);
+  auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info.get ());
 
   if (li != NULL)
     li->l_addr_p = 0;
@@ -1001,7 +1003,7 @@ so_list_from_svr4_sos (const std::vector<svr4_so> &sos)
 	       sizeof (newobj->so_name) - 1);                                                                                                                
       newobj->so_name[sizeof (newobj->so_name) - 1] = 0;                                                                                                                            
       strcpy (newobj->so_original_name, newobj->so_name);    
-      newobj->lm_info = new lm_info_svr4 (*so.lm_info);
+      newobj->lm_info = gdb::make_unique<lm_info_svr4> (*so.lm_info);
 
       newobj->next = NULL;
       *link = newobj;
@@ -1192,13 +1194,14 @@ svr4_default_sos (svr4_info *info)
     return NULL;
 
   so_list *newobj = new so_list;
-  lm_info_svr4 *li = new lm_info_svr4;
-  newobj->lm_info = li;
+  auto li = gdb::make_unique<lm_info_svr4> ();
 
   /* Nothing will ever check the other fields if we set l_addr_p.  */
   li->l_addr = li->l_addr_inferior = info->debug_loader_offset;
   li->l_addr_p = 1;
 
+  newobj->lm_info = std::move (li);
+
   strncpy (newobj->so_name, info->debug_loader_name, SO_NAME_MAX_PATH_SIZE - 1);
   newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
   strcpy (newobj->so_original_name, newobj->so_name);
@@ -1495,7 +1498,7 @@ svr4_current_sos (void)
 		[ 9] .dynamic DYNAMIC ffffffffff700580 000580 0000f0
 	  */
 
-	  auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so->lm_info);
+	  auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so->lm_info.get ());
 
 	  if (address_in_mem_range (li->l_ld, &vsyscall_range))
 	    {
@@ -1531,7 +1534,8 @@ svr4_fetch_objfile_link_map (struct objfile *objfile)
   for (struct so_list *so : current_program_space->solibs ())
     if (so->objfile == objfile)
       {
-	auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so->lm_info);
+	auto *li
+	  = gdb::checked_static_cast<lm_info_svr4 *> (so->lm_info.get ());
 
 	return li->lm_addr;
       }
@@ -3292,7 +3296,8 @@ find_debug_base_for_solib (so_list *solib)
   svr4_info *info = get_svr4_info (current_program_space);
   gdb_assert (info != nullptr);
 
-  const lm_info_svr4 *lm_info = (const lm_info_svr4 *) solib->lm_info;
+  auto *lm_info
+    = gdb::checked_static_cast<const lm_info_svr4 *> (solib->lm_info.get ());
 
   for (const auto &tuple : info->solib_lists)
     {
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index e20a336f9ad3..3743c77e9c76 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -260,7 +260,7 @@ solib_target_current_sos (void)
       /* We no longer need this copy of the name.  */
       info->name.clear ();
 
-      new_solib->lm_info = info.release ();
+      new_solib->lm_info = std::move (info);
 
       /* Add it to the list.  */
       if (!start)
@@ -285,7 +285,7 @@ static void
 solib_target_relocate_section_addresses (so_list &so, target_section *sec)
 {
   CORE_ADDR offset;
-  auto *li = gdb::checked_static_cast<lm_info_target *> (so.lm_info);
+  auto *li = gdb::checked_static_cast<lm_info_target *> (so.lm_info.get ());
 
   /* Build the offset table only once per object file.  We can not do
      it any earlier, since we need to open the file first.  */
diff --git a/gdb/solib.c b/gdb/solib.c
index 4799fabe8213..5ed223b8dfca 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -675,7 +675,6 @@ void
 free_so (so_list &so)
 {
   clear_so (so);
-  delete so.lm_info;
   delete &so;
 }
 
diff --git a/gdb/solist.h b/gdb/solist.h
index 12ea516a10cf..5f9090a07c72 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -34,6 +34,8 @@ struct lm_info
   virtual ~lm_info () = 0;
 };
 
+using lm_info_up = std::unique_ptr<lm_info>;
+
 struct so_list
 {
   /* The following fields of the structure come directly from the
@@ -46,7 +48,7 @@ struct so_list
      will be a copy of struct link_map from the user process, but
      it need not be; it can be any collection of data needed to
      traverse the dynamic linker's data structures.  */
-  struct lm_info *lm_info = nullptr;
+  lm_info_up lm_info;
 
   /* Shared object file name, exactly as it appears in the
      inferior's link map.  This may be a relative path, or something
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 16/24] gdb: make clear_so a method of struct so_list
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (14 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 15/24] gdb: make so_list::lm_info a unique_ptr Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-19 11:08   ` Lancelot SIX
  2023-10-10 20:40 ` [PATCH 17/24] gdb: remove target_section_table typedef Simon Marchi
                   ` (9 subsequent siblings)
  25 siblings, 1 reply; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

... just because it seems to make sense to do so.

Change-Id: Ie283c92d9b90c54e3deee96a43c6a942d8b5910b
---
 gdb/solib.c  | 35 ++++++++++++++---------------------
 gdb/solist.h | 10 ++++++++++
 2 files changed, 24 insertions(+), 21 deletions(-)

diff --git a/gdb/solib.c b/gdb/solib.c
index 5ed223b8dfca..8f5b6e02e71c 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -623,39 +623,32 @@ solib_map_sections (so_list &so)
   return 1;
 }
 
-/* Free symbol-file related contents of SO and reset for possible reloading
-   of SO.  If we have opened a BFD for SO, close it.  If we have placed SO's
-   sections in some target's section table, the caller is responsible for
-   removing them.
+/* See solist.h.  */
 
-   This function doesn't mess with objfiles at all.  If there is an
-   objfile associated with SO that needs to be removed, the caller is
-   responsible for taking care of that.  */
-
-static void
-clear_so (so_list &so)
+void
+so_list::clear ()
 {
   const target_so_ops *ops = gdbarch_so_ops (current_inferior ()->arch ());
 
-  delete so.sections;
-  so.sections = NULL;
+  delete this->sections;
+  this->sections = NULL;
 
-  gdb_bfd_unref (so.abfd);
-  so.abfd = NULL;
+  gdb_bfd_unref (this->abfd);
+  this->abfd = NULL;
 
   /* Our caller closed the objfile, possibly via objfile_purge_solibs.  */
-  so.symbols_loaded = 0;
-  so.objfile = NULL;
+  this->symbols_loaded = 0;
+  this->objfile = NULL;
 
-  so.addr_low = so.addr_high = 0;
+  this->addr_low = this->addr_high = 0;
 
   /* Restore the target-supplied file name.  SO_NAME may be the path
      of the symbol file.  */
-  strcpy (so.so_name, so.so_original_name);
+  strcpy (this->so_name, this->so_original_name);
 
   /* Do the same for target-specific data.  */
   if (ops->clear_so != NULL)
-    ops->clear_so (so);
+    ops->clear_so (*this);
 }
 
 lm_info::~lm_info () = default;
@@ -674,7 +667,7 @@ lm_info::~lm_info () = default;
 void
 free_so (so_list &so)
 {
-  clear_so (so);
+  so.clear ();
   delete &so;
 }
 
@@ -1358,7 +1351,7 @@ reload_shared_libraries_1 (int from_tty)
 	      && !solib_used (so))
 	    so->objfile->unlink ();
 	  current_program_space->remove_target_sections (so);
-	  clear_so (*so);
+	  so->clear ();
 	}
 
       /* If this shared library is now associated with a new symbol
diff --git a/gdb/solist.h b/gdb/solist.h
index 5f9090a07c72..75e8e8ad0182 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -38,6 +38,16 @@ using lm_info_up = std::unique_ptr<lm_info>;
 
 struct so_list
 {
+  /* Free symbol-file related contents of SO and reset for possible reloading
+     of SO.  If we have opened a BFD for SO, close it.  If we have placed SO's
+     sections in some target's section table, the caller is responsible for
+     removing them.
+
+     This function doesn't mess with objfiles at all.  If there is an
+     objfile associated with SO that needs to be removed, the caller is
+     responsible for taking care of that.  */
+  void clear () ;
+
   /* The following fields of the structure come directly from the
      dynamic linker's tables in the inferior, and are initialized by
      current_sos.  */
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 17/24] gdb: remove target_section_table typedef
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (15 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 16/24] gdb: make clear_so a method of struct so_list Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-10 20:40 ` [PATCH 18/24] gdb: make so_list::sections not a pointer Simon Marchi
                   ` (8 subsequent siblings)
  25 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Remove this typedef.  I think that hiding the real type (std::vector)
behind a typedef just hinders readability.

Change-Id: I80949da3392f60a2826c56c268e0ec6f503ad79f
---
 gdb/bfd-target.c       |  6 +++---
 gdb/corelow.c          |  7 +++----
 gdb/exec.c             | 20 ++++++++++----------
 gdb/exec.h             |  6 +++---
 gdb/maint.c            |  2 +-
 gdb/progspace.h        |  6 +++---
 gdb/record-full.c      |  2 +-
 gdb/remote.c           |  3 ++-
 gdb/solib.c            |  2 +-
 gdb/solist.h           |  2 +-
 gdb/symfile.c          |  2 +-
 gdb/symfile.h          |  2 +-
 gdb/target-debug.h     |  4 ++--
 gdb/target-delegates.c | 14 +++++++-------
 gdb/target-section.h   |  4 ----
 gdb/target.c           | 10 +++++-----
 gdb/target.h           |  6 +++---
 17 files changed, 47 insertions(+), 51 deletions(-)

diff --git a/gdb/bfd-target.c b/gdb/bfd-target.c
index 9e1b980957c9..2aa2f9638d3f 100644
--- a/gdb/bfd-target.c
+++ b/gdb/bfd-target.c
@@ -50,7 +50,7 @@ class target_bfd : public target_ops
 		  ULONGEST offset, ULONGEST len,
 		  ULONGEST *xfered_len) override;
 
-  const target_section_table *get_section_table () override;
+  const std::vector<target_section> *get_section_table () override;
 
 private:
   /* The BFD we're wrapping.  */
@@ -59,7 +59,7 @@ class target_bfd : public target_ops
   /* The section table build from the ALLOC sections in BFD.  Note
      that we can't rely on extracting the BFD from a random section in
      the table, since the table can be legitimately empty.  */
-  target_section_table m_table;
+  std::vector<target_section> m_table;
 };
 
 target_xfer_status
@@ -82,7 +82,7 @@ target_bfd::xfer_partial (target_object object,
     }
 }
 
-const target_section_table *
+const std::vector<target_section> *
 target_bfd::get_section_table ()
 {
   return &m_table;
diff --git a/gdb/corelow.c b/gdb/corelow.c
index 4da5e4fa5d46..91b442b4426f 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -142,11 +142,11 @@ class core_target final : public process_stratum_target
      shared library bfds.  The core bfd sections are an implementation
      detail of the core target, just like ptrace is for unix child
      targets.  */
-  target_section_table m_core_section_table;
+  std::vector<target_section> m_core_section_table;
 
   /* File-backed address space mappings: some core files include
      information about memory mapped files.  */
-  target_section_table m_core_file_mappings;
+  std::vector<target_section> m_core_file_mappings;
 
   /* Unavailable mappings.  These correspond to pathnames which either
      weren't found or could not be opened.  Knowing these addresses can
@@ -197,8 +197,7 @@ core_target::core_target ()
   build_file_mappings ();
 }
 
-/* Construct the target_section_table for file-backed mappings if
-   they exist.
+/* Construct the table for file-backed mappings if they exist.
 
    For each unique path in the note, we'll open a BFD with a bfd
    target of "binary".  This is an unstructured bfd target upon which
diff --git a/gdb/exec.c b/gdb/exec.c
index 08c205662ea1..0f9f9d076c68 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -479,8 +479,8 @@ exec_file_attach (const char *filename, int from_tty)
 		 gdb_bfd_errmsg (bfd_get_error (), matching).c_str ());
 	}
 
-	  target_section_table sections
-	  = build_section_table (current_program_space->exec_bfd ());
+      std::vector<target_section> sections
+	= build_section_table (current_program_space->exec_bfd ());
 
       current_program_space->ebfd_mtime
 	= bfd_get_mtime (current_program_space->exec_bfd ());
@@ -568,10 +568,10 @@ file_command (const char *arg, int from_tty)
 
 /* Builds a section table, given args BFD, TABLE.  */
 
-target_section_table
+std::vector<target_section>
 build_section_table (struct bfd *some_bfd)
 {
-  target_section_table table;
+  std::vector<target_section> table;
 
   for (asection *asect : gdb_bfd_sections (some_bfd))
     {
@@ -600,7 +600,7 @@ build_section_table (struct bfd *some_bfd)
 
 void
 program_space::add_target_sections (const void *owner,
-				    const target_section_table &sections)
+				    const std::vector<target_section> &sections)
 {
   if (!sections.empty ())
     {
@@ -745,7 +745,7 @@ exec_read_partial_read_only (gdb_byte *readbuf, ULONGEST offset,
 
 static std::vector<mem_range>
 section_table_available_memory (CORE_ADDR memaddr, ULONGEST len,
-				const target_section_table &sections)
+				const std::vector<target_section> &sections)
 {
   std::vector<mem_range> memory;
 
@@ -779,7 +779,7 @@ enum target_xfer_status
 section_table_read_available_memory (gdb_byte *readbuf, ULONGEST offset,
 				     ULONGEST len, ULONGEST *xfered_len)
 {
-  const target_section_table *table
+  const std::vector<target_section> *table
     = target_get_section_table (current_inferior ()->top_target ());
   std::vector<mem_range> available_memory
     = section_table_available_memory (offset, len, *table);
@@ -819,7 +819,7 @@ enum target_xfer_status
 section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
 				   ULONGEST offset, ULONGEST len,
 				   ULONGEST *xfered_len,
-				   const target_section_table &sections,
+				   const std::vector<target_section> &sections,
 				   gdb::function_view<bool
 				     (const struct target_section *)> match_cb)
 {
@@ -895,7 +895,7 @@ exec_target::xfer_partial (enum target_object object,
 			   const gdb_byte *writebuf,
 			   ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
 {
-  const target_section_table *table = target_get_section_table (this);
+  const std::vector<target_section> *table = target_get_section_table (this);
 
   if (object == TARGET_OBJECT_MEMORY)
     return section_table_xfer_memory_partial (readbuf, writebuf,
@@ -907,7 +907,7 @@ exec_target::xfer_partial (enum target_object object,
 \f
 
 void
-print_section_info (const target_section_table *t, bfd *abfd)
+print_section_info (const std::vector<target_section> *t, bfd *abfd)
 {
   struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
   /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64.  */
diff --git a/gdb/exec.h b/gdb/exec.h
index 200a5c69265a..bde3fc8fcc0e 100644
--- a/gdb/exec.h
+++ b/gdb/exec.h
@@ -32,7 +32,7 @@ struct objfile;
 
 /* Builds a section table, given args BFD.  */
 
-extern target_section_table build_section_table (struct bfd *);
+extern std::vector<target_section> build_section_table (struct bfd *);
 
 /* VFORK_CHILD is a child vforked and its program space is shared with its
    parent.  This pushes the exec target on that inferior's target stack if
@@ -75,7 +75,7 @@ extern enum target_xfer_status
   section_table_xfer_memory_partial (gdb_byte *,
 				     const gdb_byte *,
 				     ULONGEST, ULONGEST, ULONGEST *,
-				     const target_section_table &,
+				     const std::vector<target_section> &,
 				     gdb::function_view<bool
 				       (const struct target_section *)> match_cb
 					 = nullptr);
@@ -95,7 +95,7 @@ extern void exec_set_section_address (const char *, int, CORE_ADDR);
    special cased --- it's filename is omitted; if it is the executable
    file, its entry point is printed.  */
 
-extern void print_section_info (const target_section_table *table,
+extern void print_section_info (const std::vector<target_section> *table,
 				bfd *abfd);
 
 /* Helper function that attempts to open the symbol file at EXEC_FILE_HOST.
diff --git a/gdb/maint.c b/gdb/maint.c
index f91184c20056..e0dc5bc0c7aa 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -470,7 +470,7 @@ maintenance_info_target_sections (const char *arg, int from_tty)
 {
   bfd *abfd = nullptr;
   int digits = 0;
-  const target_section_table *table
+  const std::vector<target_section> *table
     = target_get_section_table (current_inferior ()->top_target ());
   if (table == nullptr)
     return;
diff --git a/gdb/progspace.h b/gdb/progspace.h
index 0930d8583597..20dfc2e92e83 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -291,7 +291,7 @@ struct program_space
   /* Add the sections array defined by SECTIONS to the
      current set of target sections.  */
   void add_target_sections (const void *owner,
-			    const target_section_table &sections);
+			    const std::vector<target_section> &sections);
 
   /* Add the sections of OBJFILE to the current set of target
      sections.  They are given OBJFILE as the "owner".  */
@@ -304,7 +304,7 @@ struct program_space
   }
 
   /* Return a reference to the M_TARGET_SECTIONS table.  */
-  target_section_table &target_sections ()
+  std::vector<target_section> &target_sections ()
   {
     return m_target_sections;
   }
@@ -380,7 +380,7 @@ struct program_space
 private:
   /* The set of target sections matching the sections mapped into
      this program space.  Managed by both exec_ops and solib.c.  */
-  target_section_table m_target_sections;
+  std::vector<target_section> m_target_sections;
 };
 
 /* An address space.  It is used for comparing if
diff --git a/gdb/record-full.c b/gdb/record-full.c
index f072bdd7f882..2ee08e6d54fb 100644
--- a/gdb/record-full.c
+++ b/gdb/record-full.c
@@ -173,7 +173,7 @@ struct record_full_core_buf_entry
 
 /* Record buf with core target.  */
 static detached_regcache *record_full_core_regbuf = NULL;
-static target_section_table record_full_core_sections;
+static std::vector<target_section> record_full_core_sections;
 static struct record_full_core_buf_entry *record_full_core_buf_list = NULL;
 
 /* The following variables are used for managing the linked list that
diff --git a/gdb/remote.c b/gdb/remote.c
index 961061e02a9f..b58dbd4cb66e 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -9446,7 +9446,8 @@ remote_target::remote_xfer_live_readonly_partial (gdb_byte *readbuf,
     {
       ULONGEST memend = memaddr + len;
 
-      const target_section_table *table = target_get_section_table (this);
+      const std::vector<target_section> *table
+	= target_get_section_table (this);
       for (const target_section &p : *table)
 	{
 	  if (memaddr >= p.addr)
diff --git a/gdb/solib.c b/gdb/solib.c
index 8f5b6e02e71c..3d6dfdc4e3c3 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -593,7 +593,7 @@ solib_map_sections (so_list &so)
   strcpy (so.so_name, bfd_get_filename (so.abfd));
 
   if (so.sections == nullptr)
-    so.sections = new target_section_table;
+    so.sections = new std::vector<target_section>;
   *so.sections = build_section_table (so.abfd);
 
   for (target_section &p : *so.sections)
diff --git a/gdb/solist.h b/gdb/solist.h
index 75e8e8ad0182..ca12fd55ba0f 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -84,7 +84,7 @@ struct so_list
      the file cannot be found or after the command "nosharedlibrary".  */
   struct objfile *objfile = nullptr;
 
-  target_section_table *sections = nullptr;
+  std::vector<target_section> *sections = nullptr;
 
   /* Record the range of addresses belonging to this shared library.
      There may not be just one (e.g. if two segments are relocated
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 5eb1638b781c..eebc5ea44b9b 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -216,7 +216,7 @@ find_lowest_section (asection *sect, asection **lowest)
    an existing section table.  */
 
 section_addr_info
-build_section_addr_info_from_section_table (const target_section_table &table)
+build_section_addr_info_from_section_table (const std::vector<target_section> &table)
 {
   section_addr_info sap;
 
diff --git a/gdb/symfile.h b/gdb/symfile.h
index 296fee945c14..bb61a779314e 100644
--- a/gdb/symfile.h
+++ b/gdb/symfile.h
@@ -257,7 +257,7 @@ extern std::string find_separate_debug_file_by_debuglink
    existing section table.  */
 
 extern section_addr_info
-    build_section_addr_info_from_section_table (const target_section_table &table);
+    build_section_addr_info_from_section_table (const std::vector<target_section> &table);
 
 			/*   Variables   */
 
diff --git a/gdb/target-debug.h b/gdb/target-debug.h
index 4082f8271179..3663ec21740e 100644
--- a/gdb/target-debug.h
+++ b/gdb/target-debug.h
@@ -104,8 +104,8 @@
   target_debug_do_print (host_address_to_string (X))
 #define target_debug_print_ui_file_p(X)	\
   target_debug_do_print (host_address_to_string (X))
-#define target_debug_print_const_target_section_table_p(X)	\
-  target_debug_do_print (host_address_to_string (X))
+#define target_debug_print_const_std_vector_target_section_p(X) \
+  target_debug_do_print (host_address_to_string (X->data ()))
 #define target_debug_print_void_p(X) \
   target_debug_do_print (host_address_to_string (X))
 #define target_debug_print_find_memory_region_ftype(X) \
diff --git a/gdb/target-delegates.c b/gdb/target-delegates.c
index d1b71a11c12d..580fc768dd1c 100644
--- a/gdb/target-delegates.c
+++ b/gdb/target-delegates.c
@@ -96,7 +96,7 @@ struct dummy_target : public target_ops
   void rcmd (const char *arg0, struct ui_file *arg1) override;
   const char *pid_to_exec_file (int arg0) override;
   void log_command (const char *arg0) override;
-  const target_section_table *get_section_table () override;
+  const std::vector<target_section> *get_section_table () override;
   thread_control_capabilities get_thread_control_capabilities () override;
   bool attach_no_wait () override;
   bool can_async_p () override;
@@ -271,7 +271,7 @@ struct debug_target : public target_ops
   void rcmd (const char *arg0, struct ui_file *arg1) override;
   const char *pid_to_exec_file (int arg0) override;
   void log_command (const char *arg0) override;
-  const target_section_table *get_section_table () override;
+  const std::vector<target_section> *get_section_table () override;
   thread_control_capabilities get_thread_control_capabilities () override;
   bool attach_no_wait () override;
   bool can_async_p () override;
@@ -2032,27 +2032,27 @@ debug_target::log_command (const char *arg0)
   gdb_puts (")\n", gdb_stdlog);
 }
 
-const target_section_table *
+const std::vector<target_section> *
 target_ops::get_section_table ()
 {
   return this->beneath ()->get_section_table ();
 }
 
-const target_section_table *
+const std::vector<target_section> *
 dummy_target::get_section_table ()
 {
   return default_get_section_table ();
 }
 
-const target_section_table *
+const std::vector<target_section> *
 debug_target::get_section_table ()
 {
   gdb_printf (gdb_stdlog, "-> %s->get_section_table (...)\n", this->beneath ()->shortname ());
-  const target_section_table * result
+  const std::vector<target_section> * result
     = this->beneath ()->get_section_table ();
   gdb_printf (gdb_stdlog, "<- %s->get_section_table (", this->beneath ()->shortname ());
   gdb_puts (") = ", gdb_stdlog);
-  target_debug_print_const_target_section_table_p (result);
+  target_debug_print_const_std_vector_target_section_p (result);
   gdb_puts ("\n", gdb_stdlog);
   return result;
 }
diff --git a/gdb/target-section.h b/gdb/target-section.h
index 5d7c846a6e25..1c902baa7778 100644
--- a/gdb/target-section.h
+++ b/gdb/target-section.h
@@ -51,8 +51,4 @@ struct target_section
   const void *owner;
 };
 
-/* Holds an array of target sections.  */
-
-using target_section_table = std::vector<target_section>;
-
 #endif /* GDB_TARGET_SECTION_H */
diff --git a/gdb/target.c b/gdb/target.c
index 6853c1432318..a2e78780b0bc 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -1381,7 +1381,7 @@ target_xfer_status_to_string (enum target_xfer_status status)
 };
 
 
-const target_section_table *
+const std::vector<target_section> *
 target_get_section_table (struct target_ops *target)
 {
   return target->get_section_table ();
@@ -1392,7 +1392,7 @@ target_get_section_table (struct target_ops *target)
 const struct target_section *
 target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
 {
-  const target_section_table *table = target_get_section_table (target);
+  const std::vector<target_section> *table = target_get_section_table (target);
 
   if (table == NULL)
     return NULL;
@@ -1407,7 +1407,7 @@ target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
 
 /* See target.h.  */
 
-const target_section_table *
+const std::vector<target_section> *
 default_get_section_table ()
 {
   return &current_program_space->target_sections ();
@@ -1541,7 +1541,7 @@ memory_xfer_partial_1 (struct target_ops *ops, enum target_object object,
 
       if (pc_in_unmapped_range (memaddr, section))
 	{
-	  const target_section_table *table = target_get_section_table (ops);
+	  const std::vector<target_section> *table = target_get_section_table (ops);
 	  const char *section_name = section->the_bfd_section->name;
 
 	  memaddr = overlay_mapped_address (memaddr, section);
@@ -1565,7 +1565,7 @@ memory_xfer_partial_1 (struct target_ops *ops, enum target_object object,
       if (secp != NULL
 	  && (bfd_section_flags (secp->the_bfd_section) & SEC_READONLY))
 	{
-	  const target_section_table *table = target_get_section_table (ops);
+	  const std::vector<target_section> *table = target_get_section_table (ops);
 	  return section_table_xfer_memory_partial (readbuf, writebuf,
 						    memaddr, len, xfered_len,
 						    *table);
diff --git a/gdb/target.h b/gdb/target.h
index 446c0a9fdbfa..68b269fb3e61 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -698,7 +698,7 @@ struct target_ops
       TARGET_DEFAULT_RETURN (NULL);
     virtual void log_command (const char *)
       TARGET_DEFAULT_IGNORE ();
-    virtual const target_section_table *get_section_table ()
+    virtual const std::vector<target_section> *get_section_table ()
       TARGET_DEFAULT_RETURN (default_get_section_table ());
 
     /* Provide default values for all "must have" methods.  */
@@ -2401,12 +2401,12 @@ const struct target_section *target_section_by_addr (struct target_ops *target,
 /* Return the target section table this target (or the targets
    beneath) currently manipulate.  */
 
-extern const target_section_table *target_get_section_table
+extern const std::vector<target_section> *target_get_section_table
   (struct target_ops *target);
 
 /* Default implementation of get_section_table for dummy_target.  */
 
-extern const target_section_table *default_get_section_table ();
+extern const std::vector<target_section> *default_get_section_table ();
 
 /* From mem-break.c */
 
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 18/24] gdb: make so_list::sections not a pointer
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (16 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 17/24] gdb: remove target_section_table typedef Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-10 20:40 ` [PATCH 19/24] gdb: make so_list::abfd a gdb_bfd_ref_ptr Simon Marchi
                   ` (7 subsequent siblings)
  25 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Make the field a vector directly, instead of a pointer to a vector.
This was needed when so_list had to be a trivial type, which is not the
case anymore.

Change-Id: I79a8378ce0d0d1e2206ca08a273ebf332cb3ba14
---
 gdb/solib.c  | 18 ++++++------------
 gdb/solist.h |  2 +-
 2 files changed, 7 insertions(+), 13 deletions(-)

diff --git a/gdb/solib.c b/gdb/solib.c
index 3d6dfdc4e3c3..fabb6e1d938b 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -592,11 +592,9 @@ solib_map_sections (so_list &so)
     error (_("Shared library file name is too long."));
   strcpy (so.so_name, bfd_get_filename (so.abfd));
 
-  if (so.sections == nullptr)
-    so.sections = new std::vector<target_section>;
-  *so.sections = build_section_table (so.abfd);
+  so.sections = build_section_table (so.abfd);
 
-  for (target_section &p : *so.sections)
+  for (target_section &p : so.sections)
     {
       /* Relocate the section binding addresses as recorded in the shared
 	 object's file by the base address to which the object was actually
@@ -618,7 +616,7 @@ solib_map_sections (so_list &so)
      section tables.  Do this immediately after mapping the object so
      that later nodes in the list can query this object, as is needed
      in solib-osf.c.  */
-  current_program_space->add_target_sections (&so, *so.sections);
+  current_program_space->add_target_sections (&so, so.sections);
 
   return 1;
 }
@@ -630,8 +628,7 @@ so_list::clear ()
 {
   const target_so_ops *ops = gdbarch_so_ops (current_inferior ()->arch ());
 
-  delete this->sections;
-  this->sections = NULL;
+  this->sections.clear ();
 
   gdb_bfd_unref (this->abfd);
   this->abfd = NULL;
@@ -708,7 +705,7 @@ solib_read_symbols (so_list &so, symfile_add_flags flags)
 	  if (so.objfile == NULL)
 	    {
 	      section_addr_info sap
-		= build_section_addr_info_from_section_table (*so.sections);
+		= build_section_addr_info_from_section_table (so.sections);
 	      gdb_bfd_ref_ptr tmp_bfd
 		(gdb_bfd_ref_ptr::new_reference (so.abfd));
 	      so.objfile = symbol_file_add_from_bfd (tmp_bfd, so.so_name,
@@ -1168,10 +1165,7 @@ bool
 solib_contains_address_p (const so_list &solib,
 			  CORE_ADDR address)
 {
-  if (solib.sections == nullptr)
-    return false;
-
-  for (target_section &p : *solib.sections)
+  for (const target_section &p : solib.sections)
     if (p.addr <= address && address < p.endaddr)
       return true;
 
diff --git a/gdb/solist.h b/gdb/solist.h
index ca12fd55ba0f..51b985d53247 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -84,7 +84,7 @@ struct so_list
      the file cannot be found or after the command "nosharedlibrary".  */
   struct objfile *objfile = nullptr;
 
-  std::vector<target_section> *sections = nullptr;
+  std::vector<target_section> sections;
 
   /* Record the range of addresses belonging to this shared library.
      There may not be just one (e.g. if two segments are relocated
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 19/24] gdb: make so_list::abfd a gdb_bfd_ref_ptr
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (17 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 18/24] gdb: make so_list::sections not a pointer Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-10 20:40 ` [PATCH 20/24] gdb: make so_list::{so_original_name,so_name} std::strings Simon Marchi
                   ` (6 subsequent siblings)
  25 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Change the field from a `bfd *` to a gdb_bfd_ref_ptr to automatically
manage the reference.

Change-Id: I3ace18bea985bc194c5e67bb559eec567e258950
---
 gdb/solib-frv.c    |  2 +-
 gdb/solib-rocm.c   |  2 +-
 gdb/solib-target.c |  6 +++---
 gdb/solib.c        | 15 ++++++---------
 gdb/solist.h       |  2 +-
 5 files changed, 12 insertions(+), 15 deletions(-)

diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index 5cbab1493128..2ed6280b5879 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -920,7 +920,7 @@ frv_fdpic_find_canonical_descriptor (CORE_ADDR entry_point)
 	  auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info.get ());
 
 	  addr = find_canonical_descriptor_in_load_object
-		   (entry_point, got_value, name, so->abfd, li);
+		   (entry_point, got_value, name, so->abfd.get (), li);
 
 	  if (addr != 0)
 	    break;
diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
index 3bd5dec6d052..9a9bfa36fddb 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -175,7 +175,7 @@ static void
 rocm_solib_relocate_section_addresses (so_list &so,
 				       struct target_section *sec)
 {
-  if (!is_amdgpu_arch (gdbarch_from_bfd (so.abfd)))
+  if (!is_amdgpu_arch (gdbarch_from_bfd (so.abfd.get ())))
     {
       svr4_so_ops.relocate_section_addresses (so, sec);
       return;
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index 3743c77e9c76..cccdfa83c9c5 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -291,7 +291,7 @@ solib_target_relocate_section_addresses (so_list &so, target_section *sec)
      it any earlier, since we need to open the file first.  */
   if (li->offsets.empty ())
     {
-      int num_sections = gdb_bfd_count_sections (so.abfd);
+      int num_sections = gdb_bfd_count_sections (so.abfd.get ());
 
       li->offsets.assign (num_sections, 0);
 
@@ -349,7 +349,7 @@ Could not relocate shared library \"%s\": wrong number of ALLOC sections"),
       else if (!li->segment_bases.empty ())
 	{
 	  symfile_segment_data_up data
-	    = get_symfile_segment_data (so.abfd);
+	    = get_symfile_segment_data (so.abfd.get ());
 
 	  if (data == NULL)
 	    warning (_("\
@@ -359,7 +359,7 @@ Could not relocate shared library \"%s\": no segments"), so.so_name);
 	      ULONGEST orig_delta;
 	      int i;
 
-	      if (!symfile_map_offsets_to_segments (so.abfd, data.get (),
+	      if (!symfile_map_offsets_to_segments (so.abfd.get (), data.get (),
 						    li->offsets,
 						    li->segment_bases.size (),
 						    li->segment_bases.data ()))
diff --git a/gdb/solib.c b/gdb/solib.c
index fabb6e1d938b..6b7f0da0498d 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -580,7 +580,7 @@ solib_map_sections (so_list &so)
     return 0;
 
   /* Leave bfd open, core_xfer_memory and "info files" need it.  */
-  so.abfd = abfd.release ();
+  so.abfd = std::move (abfd);
 
   /* Copy the full path name into so_name, allowing symbol_file_add
      to find it later.  This also affects the =library-loaded GDB/MI
@@ -588,11 +588,11 @@ solib_map_sections (so_list &so)
      the library's host-side path.  If we let the target dictate
      that objfile's path, and the target is different from the host,
      GDB/MI will not provide the correct host-side path.  */
-  if (strlen (bfd_get_filename (so.abfd)) >= SO_NAME_MAX_PATH_SIZE)
+  if (strlen (bfd_get_filename (so.abfd.get ())) >= SO_NAME_MAX_PATH_SIZE)
     error (_("Shared library file name is too long."));
-  strcpy (so.so_name, bfd_get_filename (so.abfd));
+  strcpy (so.so_name, bfd_get_filename (so.abfd.get ()));
 
-  so.sections = build_section_table (so.abfd);
+  so.sections = build_section_table (so.abfd.get ());
 
   for (target_section &p : so.sections)
     {
@@ -629,9 +629,7 @@ so_list::clear ()
   const target_so_ops *ops = gdbarch_so_ops (current_inferior ()->arch ());
 
   this->sections.clear ();
-
-  gdb_bfd_unref (this->abfd);
-  this->abfd = NULL;
+  this->abfd = nullptr;
 
   /* Our caller closed the objfile, possibly via objfile_purge_solibs.  */
   this->symbols_loaded = 0;
@@ -706,8 +704,7 @@ solib_read_symbols (so_list &so, symfile_add_flags flags)
 	    {
 	      section_addr_info sap
 		= build_section_addr_info_from_section_table (so.sections);
-	      gdb_bfd_ref_ptr tmp_bfd
-		(gdb_bfd_ref_ptr::new_reference (so.abfd));
+	      gdb_bfd_ref_ptr tmp_bfd = so.abfd;
 	      so.objfile = symbol_file_add_from_bfd (tmp_bfd, so.so_name,
 						      flags, &sap,
 						      OBJF_SHARED, NULL);
diff --git a/gdb/solist.h b/gdb/solist.h
index 51b985d53247..671f8812916c 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -76,7 +76,7 @@ struct so_list
 
      current_sos must initialize these fields to 0.  */
 
-  bfd *abfd = nullptr;
+  gdb_bfd_ref_ptr abfd;
   char symbols_loaded = 0;	/* flag: symbols read in yet?  */
 
   /* objfile with symbols for a loaded library.  Target memory is read from
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 20/24] gdb: make so_list::{so_original_name,so_name} std::strings
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (18 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 19/24] gdb: make so_list::abfd a gdb_bfd_ref_ptr Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-13 22:28   ` [PATCH 20/24] gdb: make so_list::{so_original_name, so_name} std::strings Lancelot SIX
  2023-10-19 11:08   ` [PATCH 20/24] gdb: make so_list::{so_original_name,so_name} std::strings Lancelot SIX
  2023-10-10 20:40 ` [PATCH 21/24] gdb: link so_list using intrusive_list Simon Marchi
                   ` (5 subsequent siblings)
  25 siblings, 2 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Change these two fields, simplifying memory management and copying.

Change-Id: If2559284c515721e71e1ef56ada8b64667eebe55
---
 gdb/break-catch-load.c |  2 +-
 gdb/breakpoint.c       |  2 +-
 gdb/bsd-uthread.c      |  8 +++----
 gdb/mi/mi-cmd-file.c   |  2 +-
 gdb/solib-aix.c        |  8 +++----
 gdb/solib-darwin.c     |  5 ++---
 gdb/solib-dsbt.c       |  5 ++---
 gdb/solib-frv.c        |  6 ++----
 gdb/solib-rocm.c       |  8 ++-----
 gdb/solib-svr4.c       | 25 ++++++++++------------
 gdb/solib-target.c     | 18 ++++++----------
 gdb/solib.c            | 47 +++++++++++++++++++++++-------------------
 gdb/solist.h           |  4 ++--
 13 files changed, 63 insertions(+), 77 deletions(-)

diff --git a/gdb/break-catch-load.c b/gdb/break-catch-load.c
index 94d8b420d327..999de874d5f6 100644
--- a/gdb/break-catch-load.c
+++ b/gdb/break-catch-load.c
@@ -120,7 +120,7 @@ solib_catchpoint::check_status (struct bpstat *bs)
       for (so_list *iter : current_program_space->added_solibs)
 	{
 	  if (!regex
-	      || compiled->exec (iter->so_name, 0, NULL, 0) == 0)
+	      || compiled->exec (iter->so_name.c_str (), 0, NULL, 0) == 0)
 	    return;
 	}
     }
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 75691935553d..3ddd1623cf00 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -8052,7 +8052,7 @@ disable_breakpoints_in_unloaded_shlib (program_space *pspace,
 	      target_terminal::ours_for_output ();
 	      warning (_("Temporarily disabling breakpoints "
 			 "for unloaded shared library \"%s\""),
-		       solib.so_name);
+		       solib.so_name.c_str ());
 	    }
 	  disabled_shlib_breaks = true;
 	}
diff --git a/gdb/bsd-uthread.c b/gdb/bsd-uthread.c
index 6435a5291958..8144f8cc64b7 100644
--- a/gdb/bsd-uthread.c
+++ b/gdb/bsd-uthread.c
@@ -149,7 +149,7 @@ static int bsd_uthread_thread_next_offset = -1;
 static int bsd_uthread_thread_ctx_offset;
 
 /* Name of shared threads library.  */
-static const char *bsd_uthread_solib_name;
+static std::string bsd_uthread_solib_name;
 
 /* Non-zero if the thread stratum implemented by this module is active.  */
 static int bsd_uthread_active;
@@ -245,7 +245,7 @@ bsd_uthread_target::close ()
   bsd_uthread_thread_state_offset = 0;
   bsd_uthread_thread_next_offset = 0;
   bsd_uthread_thread_ctx_offset = 0;
-  bsd_uthread_solib_name = NULL;
+  bsd_uthread_solib_name.clear ();
 }
 
 /* Deactivate the thread stratum implemented by this module.  */
@@ -297,10 +297,10 @@ bsd_uthread_solib_loaded (so_list &so)
 static void
 bsd_uthread_solib_unloaded (program_space *pspace, const so_list &so)
 {
-  if (!bsd_uthread_solib_name)
+  if (bsd_uthread_solib_name.empty ())
     return;
 
-  if (strcmp (so.so_original_name, bsd_uthread_solib_name) == 0)
+  if (so.so_original_name == bsd_uthread_solib_name)
     bsd_uthread_deactivate ();
 }
 
diff --git a/gdb/mi/mi-cmd-file.c b/gdb/mi/mi-cmd-file.c
index 822d3df40005..5f63ce4494cb 100644
--- a/gdb/mi/mi-cmd-file.c
+++ b/gdb/mi/mi-cmd-file.c
@@ -167,7 +167,7 @@ mi_cmd_file_list_shared_libraries (const char *command,
     {
       if (so->so_name[0] == '\0')
 	continue;
-      if (pattern != NULL && !re_exec (so->so_name))
+      if (pattern != NULL && !re_exec (so->so_name.c_str ()))
 	continue;
 
       ui_out_emit_tuple tuple_emitter (uiout, NULL);
diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index a9931a60f336..e5e86c49e712 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -482,11 +482,9 @@ solib_aix_current_sos (void)
 	 so_name = string_printf ("%s(%s)", info.filename.c_str (),
 				  info.member_name.c_str ());
 	}
-      strncpy (new_solib->so_original_name, so_name.c_str (),
-	       SO_NAME_MAX_PATH_SIZE - 1);
-      new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
-      memcpy (new_solib->so_name, new_solib->so_original_name,
-	      SO_NAME_MAX_PATH_SIZE);
+
+      new_solib->so_original_name = so_name;
+      new_solib->so_name = so_name;
       new_solib->lm_info = gdb::make_unique<lm_info_aix> (info);
 
       /* Add it to the list.  */
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index e97c693052bc..2c62e16d386a 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -277,9 +277,8 @@ darwin_current_sos (void)
 
       auto li = gdb::make_unique<lm_info_darwin> ();
 
-      strncpy (newobj->so_name, file_path.get (), SO_NAME_MAX_PATH_SIZE - 1);
-      newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
-      strcpy (newobj->so_original_name, newobj->so_name);
+      newobj->so_name = file_path.get ();
+      newobj->so_original_name = newobj->so_name;
       li->lm_addr = load_addr;
 
       newobj->lm_info = std::move (li);
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index 6f4de80782a9..d3566b9590db 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -612,9 +612,8 @@ dsbt_current_sos (void)
 		gdb_printf (gdb_stdlog, "current_sos: name = %s\n",
 			    name_buf.get ());
 
-	      strncpy (sop->so_name, name_buf.get (), SO_NAME_MAX_PATH_SIZE - 1);
-	      sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
-	      strcpy (sop->so_original_name, sop->so_name);
+	      sop->so_name = name_buf.get ();
+	      sop->so_original_name = sop->so_name;
 	    }
 
 	  sop->lm_info = std::move (li);
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index 2ed6280b5879..d92c35339523 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -395,10 +395,8 @@ frv_current_sos (void)
 	    warning (_("Can't read pathname for link map entry."));
 	  else
 	    {
-	      strncpy (sop->so_name, name_buf.get (),
-		       SO_NAME_MAX_PATH_SIZE - 1);
-	      sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
-	      strcpy (sop->so_original_name, sop->so_name);
+	      sop->so_name = name_buf.get ();
+	      sop->so_original_name = sop->so_name;
 	    }
 
 	  sop->lm_info = std::move (li);
diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
index 9a9bfa36fddb..bdd458995c21 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -215,12 +215,8 @@ so_list_from_rocm_sos (const std::vector<rocm_so> &sos)
       so_list *newobj = new so_list;
       newobj->lm_info = gdb::make_unique<lm_info_svr4> (*so.lm_info);
 
-      strncpy (newobj->so_name, so.name.c_str (), sizeof (newobj->so_name));
-      newobj->so_name[sizeof (newobj->so_name) - 1] = '\0';
-
-      strncpy (newobj->so_original_name, so.unique_name.c_str (),
-	       sizeof (newobj->so_original_name));
-      newobj->so_original_name[sizeof (newobj->so_original_name) - 1] = '\0';
+      newobj->so_name = so.name;
+      newobj->so_original_name = so.unique_name;
 
       newobj->next = nullptr;
       *link = newobj;
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 6a54cd332085..b90b4ca2f680 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -193,8 +193,8 @@ svr4_same (const so_list &gdb, const so_list &inferior)
   auto *lmi
     = gdb::checked_static_cast<const lm_info_svr4 *> (inferior.lm_info.get ());
 
-  return svr4_same (gdb.so_original_name, inferior.so_original_name,
-		    *lmg, *lmi);
+  return svr4_same (gdb.so_original_name.c_str (),
+		    inferior.so_original_name.c_str (), *lmg, *lmi);
 }
 
 static lm_info_svr4_up
@@ -316,7 +316,7 @@ lm_addr_check (const so_list &so, bfd *abfd)
 		gdb_printf (_("Using PIC (Position Independent Code) "
 			      "prelink displacement %s for \"%s\".\n"),
 			    paddress (current_inferior ()->arch (), l_addr),
-			    so.so_name);
+			    so.so_name.c_str ());
 	    }
 	  else
 	    {
@@ -331,7 +331,8 @@ lm_addr_check (const so_list &so, bfd *abfd)
 
 	      warning (_(".dynamic section for \"%s\" "
 			 "is not at the expected address "
-			 "(wrong library or version mismatch?)"), so.so_name);
+			 "(wrong library or version mismatch?)"),
+			 so.so_name.c_str ());
 	    }
 	}
 
@@ -999,10 +1000,8 @@ so_list_from_svr4_sos (const std::vector<svr4_so> &sos)
     {
       so_list *newobj = new so_list;
 
-      strncpy (newobj->so_name, so.name.c_str (),
-	       sizeof (newobj->so_name) - 1);                                                                                                                
-      newobj->so_name[sizeof (newobj->so_name) - 1] = 0;                                                                                                                            
-      strcpy (newobj->so_original_name, newobj->so_name);    
+      newobj->so_name = so.name;
+      newobj->so_original_name = so.name;
       newobj->lm_info = gdb::make_unique<lm_info_svr4> (*so.lm_info);
 
       newobj->next = NULL;
@@ -1201,10 +1200,8 @@ svr4_default_sos (svr4_info *info)
   li->l_addr_p = 1;
 
   newobj->lm_info = std::move (li);
-
-  strncpy (newobj->so_name, info->debug_loader_name, SO_NAME_MAX_PATH_SIZE - 1);
-  newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
-  strcpy (newobj->so_original_name, newobj->so_name);
+  newobj->so_name = info->debug_loader_name;
+  newobj->so_original_name = newobj->so_name;
 
   return newobj;
 }
@@ -2374,7 +2371,7 @@ enable_break (struct svr4_info *info, int from_tty)
 	 address from the shared library table.  */
       for (struct so_list *so : current_program_space->solibs ())
 	{
-	  if (svr4_same_1 (interp_name, so->so_original_name))
+	  if (svr4_same_1 (interp_name, so->so_original_name.c_str ()))
 	    {
 	      load_addr_found = 1;
 	      loader_found_in_list = 1;
@@ -3305,7 +3302,7 @@ find_debug_base_for_solib (so_list *solib)
       const std::vector<svr4_so> &sos = tuple.second;
 
       for (const svr4_so &so : sos)
-	if (svr4_same (solib->so_original_name, so.name.c_str (),
+	if (svr4_same (solib->so_original_name.c_str (), so.name.c_str (),
 		       *lm_info, *so.lm_info))
 	  return debug_base;
     }
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index cccdfa83c9c5..0881d7eda430 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -250,16 +250,10 @@ solib_target_current_sos (void)
   for (lm_info_target_up &info : library_list)
     {
       so_list *new_solib = new so_list;
-      strncpy (new_solib->so_name, info->name.c_str (),
-	       SO_NAME_MAX_PATH_SIZE - 1);
-      new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
-      strncpy (new_solib->so_original_name, info->name.c_str (),
-	       SO_NAME_MAX_PATH_SIZE - 1);
-      new_solib->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
-
-      /* We no longer need this copy of the name.  */
-      info->name.clear ();
 
+      /* We don't need a copy of the name in INFO anymore.  */
+      new_solib->so_name = std::move (info->name);
+      new_solib->so_original_name = new_solib->so_name;
       new_solib->lm_info = std::move (info);
 
       /* Add it to the list.  */
@@ -310,7 +304,7 @@ solib_target_relocate_section_addresses (so_list &so, target_section *sec)
 	  if (num_alloc_sections != li->section_bases.size ())
 	    warning (_("\
 Could not relocate shared library \"%s\": wrong number of ALLOC sections"),
-		     so.so_name);
+		     so.so_name.c_str ());
 	  else
 	    {
 	      int bases_index = 0;
@@ -353,7 +347,7 @@ Could not relocate shared library \"%s\": wrong number of ALLOC sections"),
 
 	  if (data == NULL)
 	    warning (_("\
-Could not relocate shared library \"%s\": no segments"), so.so_name);
+Could not relocate shared library \"%s\": no segments"), so.so_name.c_str ());
 	  else
 	    {
 	      ULONGEST orig_delta;
@@ -364,7 +358,7 @@ Could not relocate shared library \"%s\": no segments"), so.so_name);
 						    li->segment_bases.size (),
 						    li->segment_bases.data ()))
 		warning (_("\
-Could not relocate shared library \"%s\": bad offsets"), so.so_name);
+Could not relocate shared library \"%s\": bad offsets"), so.so_name.c_str ());
 
 	      /* Find the range of addresses to report for this library in
 		 "info sharedlibrary".  Report any consecutive segments
diff --git a/gdb/solib.c b/gdb/solib.c
index 6b7f0da0498d..1815b0910208 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -543,10 +543,11 @@ solib_map_sections (so_list &so)
 {
   const target_so_ops *ops = gdbarch_so_ops (current_inferior ()->arch ());
 
-  gdb::unique_xmalloc_ptr<char> filename (tilde_expand (so.so_name));
+  gdb::unique_xmalloc_ptr<char> filename (tilde_expand (so.so_name.c_str ()));
   gdb_bfd_ref_ptr abfd (ops->bfd_open (filename.get ()));
   gdb::unique_xmalloc_ptr<char> build_id_hexstr
-    = get_cbfd_soname_build_id (current_program_space->cbfd, so.so_name);
+    = get_cbfd_soname_build_id (current_program_space->cbfd,
+				so.so_name.c_str ());
 
   /* If we already know the build-id of this solib from a core file, verify
      it matches ABFD's build-id.  If there is a mismatch or the solib wasn't
@@ -566,7 +567,8 @@ solib_map_sections (so_list &so)
 	{
 	  scoped_fd fd = debuginfod_exec_query ((const unsigned char*)
 						build_id_hexstr.get (),
-						0, so.so_name, &filename);
+						0, so.so_name.c_str (),
+						&filename);
 
 	  if (fd.get () >= 0)
 	    abfd = ops->bfd_open (filename.get ());
@@ -590,8 +592,8 @@ solib_map_sections (so_list &so)
      GDB/MI will not provide the correct host-side path.  */
   if (strlen (bfd_get_filename (so.abfd.get ())) >= SO_NAME_MAX_PATH_SIZE)
     error (_("Shared library file name is too long."));
-  strcpy (so.so_name, bfd_get_filename (so.abfd.get ()));
 
+  so.so_name = bfd_get_filename (so.abfd.get ());
   so.sections = build_section_table (so.abfd.get ());
 
   for (target_section &p : so.sections)
@@ -639,7 +641,7 @@ so_list::clear ()
 
   /* Restore the target-supplied file name.  SO_NAME may be the path
      of the symbol file.  */
-  strcpy (this->so_name, this->so_original_name);
+  this->so_name = this->so_original_name;
 
   /* Do the same for target-specific data.  */
   if (ops->clear_so != NULL)
@@ -693,7 +695,8 @@ solib_read_symbols (so_list &so, symfile_add_flags flags)
 	  so.objfile = nullptr;
 	  for (objfile *objfile : current_program_space->objfiles ())
 	    {
-	      if (filename_cmp (objfile_name (objfile), so.so_name) == 0
+	      if (filename_cmp (objfile_name (objfile),
+				so.so_name.c_str ()) == 0
 		  && objfile->addr_low == so.addr_low)
 		{
 		  so.objfile = objfile;
@@ -705,9 +708,10 @@ solib_read_symbols (so_list &so, symfile_add_flags flags)
 	      section_addr_info sap
 		= build_section_addr_info_from_section_table (so.sections);
 	      gdb_bfd_ref_ptr tmp_bfd = so.abfd;
-	      so.objfile = symbol_file_add_from_bfd (tmp_bfd, so.so_name,
-						      flags, &sap,
-						      OBJF_SHARED, NULL);
+	      so.objfile = symbol_file_add_from_bfd (tmp_bfd,
+						     so.so_name.c_str (),
+						     flags, &sap,
+						     OBJF_SHARED, NULL);
 	      so.objfile->addr_low = so.addr_low;
 	    }
 
@@ -717,7 +721,7 @@ solib_read_symbols (so_list &so, symfile_add_flags flags)
 	{
 	  exception_fprintf (gdb_stderr, e, _("Error while reading shared"
 					      " library symbols for %s:\n"),
-			     so.so_name);
+			     so.so_name.c_str ());
 	}
 
       return true;
@@ -832,7 +836,8 @@ update_solib_list (int from_tty)
 	    }
 	  else
 	    {
-	      if (! filename_cmp (gdb->so_original_name, i->so_original_name))
+	      if (filename_cmp (gdb->so_original_name.c_str (),
+				i->so_original_name.c_str ()) == 0)
 		break;	      
 	    }
 
@@ -901,7 +906,7 @@ update_solib_list (int from_tty)
 		{
 		  not_found++;
 		  if (not_found_filename == NULL)
-		    not_found_filename = i->so_original_name;
+		    not_found_filename = i->so_original_name.c_str ();
 		}
 	    }
 
@@ -961,7 +966,7 @@ libpthread_name_p (const char *name)
 static bool
 libpthread_solib_p (const so_list &so)
 {
-  return libpthread_name_p (so.so_name);
+  return libpthread_name_p (so.so_name.c_str ());
 }
 
 /* Read in symbolic information for any shared objects whose names
@@ -1011,7 +1016,7 @@ solib_add (const char *pattern, int from_tty, int readsyms)
 	add_flags |= SYMFILE_VERBOSE;
 
     for (struct so_list *gdb : current_program_space->solibs ())
-      if (! pattern || re_exec (gdb->so_name))
+      if (! pattern || re_exec (gdb->so_name.c_str ()))
 	{
 	  /* Normally, we would read the symbols from that library
 	     only if READSYMS is set.  However, we're making a small
@@ -1030,7 +1035,7 @@ solib_add (const char *pattern, int from_tty, int readsyms)
 		     libraries we have already loaded.  */
 		  if (pattern && (from_tty || info_verbose))
 		    gdb_printf (_("Symbols already loaded for %s\n"),
-				gdb->so_name);
+				gdb->so_name.c_str ());
 		}
 	      else if (solib_read_symbols (*gdb, add_flags))
 		loaded_any_symbols = true;
@@ -1088,7 +1093,7 @@ info_sharedlibrary_command (const char *pattern, int from_tty)
     {
       if (so->so_name[0])
 	{
-	  if (pattern && ! re_exec (so->so_name))
+	  if (pattern && ! re_exec (so->so_name.c_str ()))
 	    continue;
 	  ++nr_libs;
 	}
@@ -1109,7 +1114,7 @@ info_sharedlibrary_command (const char *pattern, int from_tty)
       {
 	if (! so->so_name[0])
 	  continue;
-	if (pattern && ! re_exec (so->so_name))
+	if (pattern && ! re_exec (so->so_name.c_str ()))
 	  continue;
 
 	ui_out_emit_tuple tuple_emitter (uiout, "lib");
@@ -1187,7 +1192,7 @@ solib_name_from_address (struct program_space *pspace, CORE_ADDR address)
 
   for (so = pspace->so_list; so; so = so->next)
     if (solib_contains_address_p (*so, address))
-      return (so->so_name);
+      return so->so_name.c_str ();
 
   return (0);
 }
@@ -1327,7 +1332,7 @@ reload_shared_libraries_1 (int from_tty)
 	add_flags |= SYMFILE_VERBOSE;
 
       gdb::unique_xmalloc_ptr<char> filename
-	(tilde_expand (so->so_original_name));
+	(tilde_expand (so->so_original_name.c_str ()));
       gdb_bfd_ref_ptr abfd (solib_bfd_open (filename.get ()));
       if (abfd != NULL)
 	found_pathname = bfd_get_filename (abfd.get ());
@@ -1336,7 +1341,7 @@ reload_shared_libraries_1 (int from_tty)
 	 symbol file, close that.  */
       if ((found_pathname == NULL && was_loaded)
 	  || (found_pathname != NULL
-	      && filename_cmp (found_pathname, so->so_name) != 0))
+	      && filename_cmp (found_pathname, so->so_name.c_str ()) != 0))
 	{
 	  if (so->objfile && ! (so->objfile->flags & OBJF_USERLOADED)
 	      && !solib_used (so))
@@ -1349,7 +1354,7 @@ reload_shared_libraries_1 (int from_tty)
 	 file, open it.  */
       if (found_pathname != NULL
 	  && (!was_loaded
-	      || filename_cmp (found_pathname, so->so_name) != 0))
+	      || filename_cmp (found_pathname, so->so_name.c_str ()) != 0))
 	{
 	  bool got_error = false;
 
diff --git a/gdb/solist.h b/gdb/solist.h
index 671f8812916c..9320acb7fd3e 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -65,10 +65,10 @@ struct so_list
      which needs to be looked up in LD_LIBRARY_PATH, etc.  We use it
      to tell which entries in the inferior's dynamic linker's link
      map we've already loaded.  */
-  char so_original_name[SO_NAME_MAX_PATH_SIZE] {};
+  std::string so_original_name;
 
   /* Shared object file name, expanded to something GDB can open.  */
-  char so_name[SO_NAME_MAX_PATH_SIZE] {};
+  std::string so_name;
 
   /* The following fields of the structure are built from
      information gathered from the shared object file itself, and
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 21/24] gdb: link so_list using intrusive_list
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (19 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 20/24] gdb: make so_list::{so_original_name,so_name} std::strings Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-17 19:14   ` Pedro Alves
  2023-10-10 20:40 ` [PATCH 22/24] gdb: don't call so_list::clear in free_so Simon Marchi
                   ` (4 subsequent siblings)
  25 siblings, 1 reply; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Replace the hand-made linked list implementation with intrusive_list,
simplying management of list items.

Change-Id: I7f55fd88325bb197cc655c9be5a2ec966d8cc48d
---
 gdb/mi/mi-cmd-file.c |   8 +-
 gdb/progspace.c      |   4 +-
 gdb/progspace.h      |  12 ++-
 gdb/solib-aix.c      |  23 ++----
 gdb/solib-darwin.c   |  23 ++----
 gdb/solib-dsbt.c     |  12 +--
 gdb/solib-frv.c      |  30 ++++----
 gdb/solib-rocm.c     |  26 +++----
 gdb/solib-svr4.c     |  97 ++++++++++--------------
 gdb/solib-target.c   |  19 ++---
 gdb/solib.c          | 170 ++++++++++++++++++++-----------------------
 gdb/solist.h         |   6 +-
 12 files changed, 179 insertions(+), 251 deletions(-)

diff --git a/gdb/mi/mi-cmd-file.c b/gdb/mi/mi-cmd-file.c
index 5f63ce4494cb..adc324e64fc8 100644
--- a/gdb/mi/mi-cmd-file.c
+++ b/gdb/mi/mi-cmd-file.c
@@ -163,14 +163,14 @@ mi_cmd_file_list_shared_libraries (const char *command,
   /* Print the table header.  */
   ui_out_emit_list list_emitter (uiout, "shared-libraries");
 
-  for (struct so_list *so : current_program_space->solibs ())
+  for (const so_list &so : current_program_space->solibs ())
     {
-      if (so->so_name[0] == '\0')
+      if (so.so_name[0] == '\0')
 	continue;
-      if (pattern != NULL && !re_exec (so->so_name.c_str ()))
+      if (pattern != NULL && !re_exec (so.so_name.c_str ()))
 	continue;
 
       ui_out_emit_tuple tuple_emitter (uiout, NULL);
-      mi_output_solib_attribs (uiout, *so);
+      mi_output_solib_attribs (uiout, so);
     }
 }
diff --git a/gdb/progspace.c b/gdb/progspace.c
index 555fa79ccce8..4b816b4dac8b 100644
--- a/gdb/progspace.c
+++ b/gdb/progspace.c
@@ -132,8 +132,8 @@ void
 program_space::free_all_objfiles ()
 {
   /* Any objfile reference would become stale.  */
-  for (struct so_list *so : current_program_space->solibs ())
-    gdb_assert (so->objfile == NULL);
+  for (const struct so_list &so : current_program_space->solibs ())
+    gdb_assert (so.objfile == NULL);
 
   while (!objfiles_list.empty ())
     objfiles_list.front ()->unlink ();
diff --git a/gdb/progspace.h b/gdb/progspace.h
index 20dfc2e92e83..0a762e6ad5c8 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -28,6 +28,7 @@
 #include "solist.h"
 #include "gdbsupport/next-iterator.h"
 #include "gdbsupport/safe-iterator.h"
+#include "gdbsupport/intrusive_list.h"
 #include <list>
 #include <vector>
 
@@ -253,12 +254,9 @@ struct program_space
      is outside all objfiles in this progspace.  */
   struct objfile *objfile_for_address (CORE_ADDR address);
 
-  /* Return a range adapter for iterating over all the solibs in this
-     program space.  Use it like:
-
-     for (so_list *so : pspace->solibs ()) { ... }  */
-  so_list_range solibs () const
-  { return so_list_range (this->so_list); }
+  /* Return the list of  all the solibs in this program space.  */
+  intrusive_list<struct so_list> &solibs ()
+  { return so_list; }
 
   /* Close and clear exec_bfd.  If we end up with no target sections
      to read memory from, this unpushes the exec_ops target.  */
@@ -361,7 +359,7 @@ struct program_space
 
   /* List of shared objects mapped into this space.  Managed by
      solib.c.  */
-  struct so_list *so_list = NULL;
+  intrusive_list<struct so_list> so_list;
 
   /* Number of calls to solib_add.  */
   unsigned int solib_add_generation = 0;
diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index e5e86c49e712..68798e8735ac 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -445,21 +445,20 @@ solib_aix_solib_create_inferior_hook (int from_tty)
 
 /* Implement the "current_sos" target_so_ops method.  */
 
-static struct so_list *
-solib_aix_current_sos (void)
+static intrusive_list<so_list>
+solib_aix_current_sos ()
 {
-  struct so_list *start = NULL, *last = NULL;
-  int ix;
-
   gdb::optional<std::vector<lm_info_aix>> &library_list
     = solib_aix_get_library_list (current_inferior (), NULL);
   if (!library_list.has_value ())
-    return NULL;
+    return {};
+
+  intrusive_list<so_list> sos;
 
   /* Build a struct so_list for each entry on the list.
      We skip the first entry, since this is the entry corresponding
      to the main executable, not a shared library.  */
-  for (ix = 1; ix < library_list->size (); ix++)
+  for (int ix = 1; ix < library_list->size (); ix++)
     {
       so_list *new_solib = new so_list;
       std::string so_name;
@@ -488,16 +487,10 @@ solib_aix_current_sos (void)
       new_solib->lm_info = gdb::make_unique<lm_info_aix> (info);
 
       /* Add it to the list.  */
-      if (!start)
-	last = start = new_solib;
-      else
-	{
-	  last->next = new_solib;
-	  last = new_solib;
-	}
+      sos.push_back (*new_solib);
     }
 
-  return start;
+  return sos;
 }
 
 /* Implement the "open_symbol_file_object" target_so_ops method.  */
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index 2c62e16d386a..5e3aabbfb831 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -212,32 +212,31 @@ open_symbol_file_object (int from_tty)
 
 /* Build a list of currently loaded shared objects.  See solib-svr4.c.  */
 
-static struct so_list *
-darwin_current_sos (void)
+static intrusive_list<so_list>
+darwin_current_sos ()
 {
   type *ptr_type
     = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
   enum bfd_endian byte_order = type_byte_order (ptr_type);
   int ptr_len = ptr_type->length ();
   unsigned int image_info_size;
-  struct so_list *head = NULL;
-  struct so_list *tail = NULL;
-  int i;
   darwin_info *info = get_darwin_info (current_program_space);
 
   /* Be sure image infos are loaded.  */
   darwin_load_image_infos (info);
 
   if (!darwin_dyld_version_ok (info))
-    return NULL;
+    return {};
 
   image_info_size = ptr_len * 3;
 
+  intrusive_list<so_list> sos;
+
   /* Read infos for each solib.
      The first entry was rumored to be the executable itself, but this is not
      true when a large number of shared libraries are used (table expanded ?).
      We now check all entries, but discard executable images.  */
-  for (i = 0; i < info->all_image.count; i++)
+  for (int i = 0; i < info->all_image.count; i++)
     {
       CORE_ADDR iinfo = info->all_image.info + i * image_info_size;
       gdb_byte buf[image_info_size];
@@ -282,16 +281,10 @@ darwin_current_sos (void)
       li->lm_addr = load_addr;
 
       newobj->lm_info = std::move (li);
-
-      if (head == NULL)
-	head = newobj;
-      else
-	tail->next = newobj;
-
-      tail = newobj;
+      sos.push_back (*newobj);
     }
 
-  return head;
+  return sos;
 }
 
 /* Check LOAD_ADDR points to a Mach-O executable header.  Return LOAD_ADDR
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index d3566b9590db..0c60ad69753d 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -512,14 +512,13 @@ lm_base (void)
    themselves.  The declaration of `struct so_list' says which fields
    we provide values for.  */
 
-static struct so_list *
+static intrusive_list<so_list>
 dsbt_current_sos (void)
 {
   bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
   CORE_ADDR lm_addr;
-  struct so_list *sos_head = NULL;
-  struct so_list **sos_next_ptr = &sos_head;
   dsbt_info *info = get_dsbt_info (current_program_space);
+  intrusive_list<so_list> sos;
 
   /* Make sure that the main executable has been relocated.  This is
      required in order to find the address of the global offset table,
@@ -616,10 +615,7 @@ dsbt_current_sos (void)
 	      sop->so_original_name = sop->so_name;
 	    }
 
-	  sop->lm_info = std::move (li);
-
-	  *sos_next_ptr = sop;
-	  sos_next_ptr = &sop->next;
+	  sos.push_back (*sop);
 	}
       else
 	{
@@ -630,7 +626,7 @@ dsbt_current_sos (void)
 					  sizeof (lm_buf.l_next), byte_order);
     }
 
-  return sos_head;
+  return sos;
 }
 
 /* Return 1 if PC lies in the dynamic symbol resolution code of the
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index d92c35339523..c391462ad9af 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -306,13 +306,12 @@ lm_base (void)
 
 /* Implement the "current_sos" target_so_ops method.  */
 
-static struct so_list *
-frv_current_sos (void)
+static intrusive_list<so_list>
+frv_current_sos ()
 {
   bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
   CORE_ADDR lm_addr, mgot;
-  struct so_list *sos_head = NULL;
-  struct so_list **sos_next_ptr = &sos_head;
+  intrusive_list<so_list> sos;
 
   /* Make sure that the main executable has been relocated.  This is
      required in order to find the address of the global offset table,
@@ -399,10 +398,7 @@ frv_current_sos (void)
 	      sop->so_original_name = sop->so_name;
 	    }
 
-	  sop->lm_info = std::move (li);
-
-	  *sos_next_ptr = sop;
-	  sos_next_ptr = &sop->next;
+	  sos.push_back (*sop);
 	}
       else
 	{
@@ -415,7 +411,7 @@ frv_current_sos (void)
 
   enable_break2 ();
 
-  return sos_head;
+  return sos;
 }
 
 
@@ -856,10 +852,10 @@ main_got (void)
 CORE_ADDR
 frv_fdpic_find_global_pointer (CORE_ADDR addr)
 {
-  for (struct so_list *so : current_program_space->solibs ())
+  for (const so_list &so : current_program_space->solibs ())
     {
       int seg;
-      auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info.get ());
+      auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info.get ());
       int_elf32_fdpic_loadmap *map = li->map;
 
       for (seg = 0; seg < map->nsegs; seg++)
@@ -913,12 +909,12 @@ frv_fdpic_find_canonical_descriptor (CORE_ADDR entry_point)
      in list of shared objects.  */
   if (addr == 0)
     {
-      for (struct so_list *so : current_program_space->solibs ())
+      for (const so_list &so : current_program_space->solibs ())
 	{
-	  auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info.get ());
+	  auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info.get ());
 
 	  addr = find_canonical_descriptor_in_load_object
-		   (entry_point, got_value, name, so->abfd.get (), li);
+		   (entry_point, got_value, name, so.abfd.get(), li);
 
 	  if (addr != 0)
 	    break;
@@ -1065,11 +1061,11 @@ frv_fetch_objfile_link_map (struct objfile *objfile)
 
   /* The other link map addresses may be found by examining the list
      of shared libraries.  */
-  for (struct so_list *so : current_program_space->solibs ())
+  for (const so_list &so : current_program_space->solibs ())
     {
-      auto *li = gdb::checked_static_cast<lm_info_frv *> (so->lm_info.get ());
+      auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info.get ());
 
-      if (so->objfile == objfile)
+      if (so.objfile == objfile)
 	return li->lm_addr;
     }
 
diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
index bdd458995c21..0dc303b5a651 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -204,11 +204,10 @@ rocm_solib_handle_event ()
 
 /* Create so_list objects from rocm_so objects in SOS.  */
 
-static so_list *
+static intrusive_list<so_list>
 so_list_from_rocm_sos (const std::vector<rocm_so> &sos)
 {
-  struct so_list *dst = nullptr;
-  struct so_list **link = &dst;
+  intrusive_list<so_list> dst;
 
   for (const rocm_so &so : sos)
     {
@@ -218,9 +217,7 @@ so_list_from_rocm_sos (const std::vector<rocm_so> &sos)
       newobj->so_name = so.name;
       newobj->so_original_name = so.unique_name;
 
-      newobj->next = nullptr;
-      *link = newobj;
-      link = &newobj->next;
+      dst.push_back (*newobj);
     }
 
   return dst;
@@ -229,30 +226,27 @@ so_list_from_rocm_sos (const std::vector<rocm_so> &sos)
 /* Build a list of `struct so_list' objects describing the shared
    objects currently loaded in the inferior.  */
 
-static struct so_list *
+static intrusive_list<so_list>
 rocm_solib_current_sos ()
 {
   /* First, retrieve the host-side shared library list.  */
-  so_list *head = svr4_so_ops.current_sos ();
+  intrusive_list<so_list> sos = svr4_so_ops.current_sos ();
 
   /* Then, the device-side shared library list.  */
   std::vector<rocm_so> &dev_sos = get_solib_info (current_inferior ())->solib_list;
 
   if (dev_sos.empty ())
-    return head;
+    return sos;
 
-  so_list *dev_so_list = so_list_from_rocm_sos (dev_sos);
+  intrusive_list<so_list> dev_so_list = so_list_from_rocm_sos (dev_sos);
 
-  if (head == nullptr)
+  if (sos.empty ())
     return dev_so_list;
 
   /* Append our libraries to the end of the list.  */
-  so_list *tail;
-  for (tail = head; tail->next; tail = tail->next)
-    /* Nothing.  */;
-  tail->next = dev_so_list;
+  sos.splice (std::move (dev_so_list));
 
-  return head;
+  return sos;
 }
 
 namespace {
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index b90b4ca2f680..6311599ff709 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -954,7 +954,7 @@ open_symbol_file_object (int from_tty)
 
 struct svr4_library_list
 {
-  /* The tail pointer of the current namespace.  This is internal to XML
+  /* The so list for the current namespace.  This is internal to XML
      parsing.  */
   std::vector<svr4_so> *cur_list;
 
@@ -990,11 +990,10 @@ svr4_clear_so (const so_list &so)
 
 /* Create the so_list objects equivalent to the svr4_sos in SOS.  */
 
-static so_list *
+static intrusive_list<so_list>
 so_list_from_svr4_sos (const std::vector<svr4_so> &sos)
 {
-  struct so_list *dst = NULL;
-  struct so_list **link = &dst;
+  intrusive_list<so_list> dst;
 
   for (const svr4_so &so : sos)
     {
@@ -1004,9 +1003,7 @@ so_list_from_svr4_sos (const std::vector<svr4_so> &sos)
       newobj->so_original_name = so.name;
       newobj->lm_info = gdb::make_unique<lm_info_svr4> (*so.lm_info);
 
-      newobj->next = NULL;
-      *link = newobj;
-      link = &newobj->next;
+      dst.push_back (*newobj);
     }
 
   return dst;
@@ -1186,11 +1183,11 @@ svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
 /* If no shared library information is available from the dynamic
    linker, build a fallback list from other sources.  */
 
-static struct so_list *
+static intrusive_list<so_list>
 svr4_default_sos (svr4_info *info)
 {
   if (!info->debug_loader_offset_p)
-    return NULL;
+    return {};
 
   so_list *newobj = new so_list;
   auto li = gdb::make_unique<lm_info_svr4> ();
@@ -1203,7 +1200,10 @@ svr4_default_sos (svr4_info *info)
   newobj->so_name = info->debug_loader_name;
   newobj->so_original_name = newobj->so_name;
 
-  return newobj;
+  intrusive_list<so_list> sos;
+  sos.push_back (*newobj);
+
+  return sos;
 }
 
 /* Read the whole inferior libraries chain starting at address LM.
@@ -1372,32 +1372,15 @@ svr4_current_sos_direct (struct svr4_info *info)
 
 /* Collect sos read and stored by the probes interface.  */
 
-static so_list *
+static intrusive_list<so_list>
 svr4_collect_probes_sos (svr4_info *info)
 {
-  so_list *res = nullptr;
-  so_list **pnext = &res;
+  intrusive_list<so_list> res;
 
   for (const auto &tuple : info->solib_lists)
     {
       const std::vector<svr4_so> &sos = tuple.second;
-
-      /* Allow the linker to report empty namespaces.  */
-      if (sos.empty ())
-	continue;
-
-      *pnext = so_list_from_svr4_sos (sos);
-
-      /* Update PNEXT to point to the next member of the last element.  */
-      gdb_assert (*pnext != nullptr);
-      for (;;)
-	{
-	  so_list *next = *pnext;
-	  if (next == nullptr)
-	    break;
-
-	  pnext = &next->next;
-	}
+      res.splice (so_list_from_svr4_sos (sos));
     }
 
   return res;
@@ -1406,10 +1389,10 @@ svr4_collect_probes_sos (svr4_info *info)
 /* Implement the main part of the "current_sos" target_so_ops
    method.  */
 
-static struct so_list *
+static intrusive_list<so_list>
 svr4_current_sos_1 (svr4_info *info)
 {
-  so_list *sos = nullptr;
+  intrusive_list<so_list> sos;
 
   /* If we're using the probes interface, we can use the cache as it will
      be maintained by probe update/reload actions.  */
@@ -1419,12 +1402,12 @@ svr4_current_sos_1 (svr4_info *info)
   /* If we're not using the probes interface or if we didn't cache
      anything, read the sos to fill the cache, then collect them from the
      cache.  */
-  if (sos == nullptr)
+  if (sos.empty ())
     {
       svr4_current_sos_direct (info);
 
       sos = svr4_collect_probes_sos (info);
-      if (sos == nullptr)
+      if (sos.empty ())
 	sos = svr4_default_sos (info);
     }
 
@@ -1433,11 +1416,11 @@ svr4_current_sos_1 (svr4_info *info)
 
 /* Implement the "current_sos" target_so_ops method.  */
 
-static struct so_list *
-svr4_current_sos (void)
+static intrusive_list<so_list>
+svr4_current_sos ()
 {
   svr4_info *info = get_svr4_info (current_program_space);
-  struct so_list *so_head = svr4_current_sos_1 (info);
+  intrusive_list<so_list> sos = svr4_current_sos_1 (info);
   struct mem_range vsyscall_range;
 
   /* Filter out the vDSO module, if present.  Its symbol file would
@@ -1446,13 +1429,8 @@ svr4_current_sos (void)
   if (gdbarch_vsyscall_range (current_inferior ()->arch (), &vsyscall_range)
       && vsyscall_range.length != 0)
     {
-      struct so_list **sop;
-
-      sop = &so_head;
-      while (*sop != NULL)
+      for (auto so = sos.begin (); so != sos.end (); )
 	{
-	  struct so_list *so = *sop;
-
 	  /* We can't simply match the vDSO by starting address alone,
 	     because lm_info->l_addr_inferior (and also l_addr) do not
 	     necessarily represent the real starting address of the
@@ -1499,16 +1477,17 @@ svr4_current_sos (void)
 
 	  if (address_in_mem_range (li->l_ld, &vsyscall_range))
 	    {
-	      *sop = so->next;
+	      auto next = sos.erase (so);
 	      free_so (*so);
+	      so = next;
 	      break;
 	    }
 
-	  sop = &so->next;
+	  ++so;
 	}
     }
 
-  return so_head;
+  return sos;
 }
 
 /* Get the address of the link_map for a given OBJFILE.  */
@@ -1528,11 +1507,11 @@ svr4_fetch_objfile_link_map (struct objfile *objfile)
 
   /* The other link map addresses may be found by examining the list
      of shared libraries.  */
-  for (struct so_list *so : current_program_space->solibs ())
-    if (so->objfile == objfile)
+  for (const so_list &so : current_program_space->solibs ())
+    if (so.objfile == objfile)
       {
 	auto *li
-	  = gdb::checked_static_cast<lm_info_svr4 *> (so->lm_info.get ());
+	  = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info.get ());
 
 	return li->lm_addr;
       }
@@ -2369,13 +2348,13 @@ enable_break (struct svr4_info *info, int from_tty)
 
       /* On a running target, we can get the dynamic linker's base
 	 address from the shared library table.  */
-      for (struct so_list *so : current_program_space->solibs ())
+      for (const so_list &so : current_program_space->solibs ())
 	{
-	  if (svr4_same_1 (interp_name, so->so_original_name.c_str ()))
+	  if (svr4_same_1 (interp_name, so.so_original_name.c_str ()))
 	    {
 	      load_addr_found = 1;
 	      loader_found_in_list = 1;
-	      load_addr = lm_addr_check (*so, tmp_bfd.get ());
+	      load_addr = lm_addr_check (so, tmp_bfd.get ());
 	      break;
 	    }
 	}
@@ -3259,7 +3238,7 @@ svr4_lp64_fetch_link_map_offsets (void)
 
 /* Return the DSO matching OBJFILE or nullptr if none can be found.  */
 
-static so_list *
+static const so_list *
 find_solib_for_objfile (struct objfile *objfile)
 {
   if (objfile == nullptr)
@@ -3270,9 +3249,9 @@ find_solib_for_objfile (struct objfile *objfile)
   if (objfile->separate_debug_objfile_backlink != nullptr)
     objfile = objfile->separate_debug_objfile_backlink;
 
-  for (so_list *so : current_program_space->solibs ())
-    if (so->objfile == objfile)
-      return so;
+  for (const so_list &so : current_program_space->solibs ())
+    if (so.objfile == objfile)
+      return &so;
 
   return nullptr;
 }
@@ -3285,7 +3264,7 @@ find_solib_for_objfile (struct objfile *objfile)
    right thing for the main executable.  */
 
 static CORE_ADDR
-find_debug_base_for_solib (so_list *solib)
+find_debug_base_for_solib (const so_list *solib)
 {
   if (solib == nullptr)
     return 0;
@@ -3346,7 +3325,7 @@ svr4_iterate_over_objfiles_in_search_order
   /* The linker namespace to iterate identified by the address of its
      r_debug object, defaulting to the initial namespace.  */
   CORE_ADDR initial = elf_locate_base ();
-  so_list *curr_solib = find_solib_for_objfile (current_objfile);
+  const so_list *curr_solib = find_solib_for_objfile (current_objfile);
   CORE_ADDR debug_base = find_debug_base_for_solib (curr_solib);
   if (debug_base == 0)
     debug_base = initial;
@@ -3361,7 +3340,7 @@ svr4_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.  */
-      so_list *solib = find_solib_for_objfile (objfile);
+      const so_list *solib = find_solib_for_objfile (objfile);
       CORE_ADDR solib_base = find_debug_base_for_solib (solib);
       if (solib_base == 0)
 	solib_base = initial;
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index 0881d7eda430..2e1c74f97e70 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -227,25 +227,22 @@ solib_target_parse_libraries (const char *library)
 }
 #endif
 
-static struct so_list *
+static intrusive_list<so_list>
 solib_target_current_sos (void)
 {
-  so_list *start = NULL, *last = NULL;
+  intrusive_list<so_list> sos;
 
   /* Fetch the list of shared libraries.  */
   gdb::optional<gdb::char_vector> library_document
     = target_read_stralloc (current_inferior ()->top_target (),
 			    TARGET_OBJECT_LIBRARIES, NULL);
   if (!library_document)
-    return NULL;
+    return {};
 
   /* Parse the list.  */
   std::vector<lm_info_target_up> library_list
     = solib_target_parse_libraries (library_document->data ());
 
-  if (library_list.empty ())
-    return NULL;
-
   /* Build a struct so_list for each entry on the list.  */
   for (lm_info_target_up &info : library_list)
     {
@@ -257,16 +254,10 @@ solib_target_current_sos (void)
       new_solib->lm_info = std::move (info);
 
       /* Add it to the list.  */
-      if (!start)
-	last = start = new_solib;
-      else
-	{
-	  last->next = new_solib;
-	  last = new_solib;
-	}
+      sos.push_back (*new_solib);
     }
 
-  return start;
+  return sos;
 }
 
 static void
diff --git a/gdb/solib.c b/gdb/solib.c
index 1815b0910208..9c10340ff583 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -734,10 +734,10 @@ solib_read_symbols (so_list &so, symfile_add_flags flags)
    in the list of shared libraries.  Return false otherwise.  */
 
 static bool
-solib_used (const struct so_list *const known)
+solib_used (const so_list &known)
 {
-  for (const struct so_list *pivot : current_program_space->solibs ())
-    if (pivot != known && pivot->objfile == known->objfile)
+  for (const so_list &pivot : current_program_space->solibs ())
+    if (&pivot != &known && pivot.objfile == known.objfile)
       return true;
   return false;
 }
@@ -766,8 +766,6 @@ void
 update_solib_list (int from_tty)
 {
   const target_so_ops *ops = gdbarch_so_ops (current_inferior ()->arch ());
-  struct so_list *inferior = ops->current_sos();
-  struct so_list *gdb, **gdb_link;
 
   /* We can reach here due to changing solib-search-path or the
      sysroot, before having any inferior.  */
@@ -818,42 +816,38 @@ update_solib_list (int from_tty)
      the time we're done walking GDB's list, the inferior's list
      contains only the new shared objects, which we then add.  */
 
-  gdb = current_program_space->so_list;
-  gdb_link = &current_program_space->so_list;
-  while (gdb)
+  intrusive_list<so_list> inferior = ops->current_sos ();
+  intrusive_list<so_list>::iterator gdb_iter
+    = current_program_space->so_list.begin ();
+  while (gdb_iter != current_program_space->so_list.end ())
     {
-      struct so_list *i = inferior;
-      struct so_list **i_link = &inferior;
+      intrusive_list<so_list>::iterator inferior_iter = inferior.begin ();
 
       /* Check to see whether the shared object *gdb also appears in
 	 the inferior's current list.  */
-      while (i)
+      for (; inferior_iter != inferior.end (); ++inferior_iter)
 	{
 	  if (ops->same)
 	    {
-	      if (ops->same (*gdb, *i))
+	      if (ops->same (*gdb_iter, *inferior_iter))
 		break;
 	    }
 	  else
 	    {
-	      if (filename_cmp (gdb->so_original_name.c_str (),
-				i->so_original_name.c_str ()) == 0)
-		break;	      
+	      if (!filename_cmp (gdb_iter->so_original_name.c_str (),
+				 inferior_iter->so_original_name.c_str ()))
+		break;
 	    }
-
-	  i_link = &i->next;
-	  i = *i_link;
 	}
 
       /* If the shared object appears on the inferior's list too, then
 	 it's still loaded, so we don't need to do anything.  Delete
 	 it from the inferior's list, and leave it on GDB's list.  */
-      if (i)
+      if (inferior_iter != inferior.end ())
 	{
-	  *i_link = i->next;
-	  free_so (*i);
-	  gdb_link = &gdb->next;
-	  gdb = *gdb_link;
+	  inferior.erase (inferior_iter);
+	  free_so (*inferior_iter);
+	  ++gdb_iter;
 	}
 
       /* If it's not on the inferior's list, remove it from GDB's tables.  */
@@ -861,52 +855,49 @@ update_solib_list (int from_tty)
 	{
 	  /* Notify any observer that the shared object has been
 	     unloaded before we remove it from GDB's tables.  */
-	  notify_solib_unloaded (current_program_space, *gdb);
+	  notify_solib_unloaded (current_program_space, *gdb_iter);
 
-	  current_program_space->deleted_solibs.push_back (gdb->so_name);
+	  current_program_space->deleted_solibs.push_back (gdb_iter->so_name);
 
-	  *gdb_link = gdb->next;
+	  intrusive_list<so_list>::iterator gdb_iter_next
+	    = current_program_space->so_list.erase (gdb_iter);
 
 	  /* Unless the user loaded it explicitly, free SO's objfile.  */
-	  if (gdb->objfile && ! (gdb->objfile->flags & OBJF_USERLOADED)
-	      && !solib_used (gdb))
-	    gdb->objfile->unlink ();
+	  if (gdb_iter->objfile != nullptr
+	      && !(gdb_iter->objfile->flags & OBJF_USERLOADED)
+	      && !solib_used (*gdb_iter))
+	    gdb_iter->objfile->unlink ();
 
 	  /* Some targets' section tables might be referring to
 	     sections from so.abfd; remove them.  */
-	  current_program_space->remove_target_sections (gdb);
+	  current_program_space->remove_target_sections (&*gdb_iter);
 
-	  free_so (*gdb);
-	  gdb = *gdb_link;
+	  free_so (*gdb_iter);
+	  gdb_iter = gdb_iter_next;
 	}
     }
 
   /* Now the inferior's list contains only shared objects that don't
      appear in GDB's list --- those that are newly loaded.  Add them
      to GDB's shared object list.  */
-  if (inferior)
+  if (!inferior.empty ())
     {
       int not_found = 0;
       const char *not_found_filename = NULL;
 
-      struct so_list *i;
-
-      /* Add the new shared objects to GDB's list.  */
-      *gdb_link = inferior;
-
       /* Fill in the rest of each of the `struct so_list' nodes.  */
-      for (i = inferior; i; i = i->next)
+      for (so_list &new_so : inferior)
 	{
-	  current_program_space->added_solibs.push_back (i);
+	  current_program_space->added_solibs.push_back (&new_so);
 
 	  try
 	    {
 	      /* Fill in the rest of the `struct so_list' node.  */
-	      if (!solib_map_sections (*i))
+	      if (!solib_map_sections (new_so))
 		{
 		  not_found++;
 		  if (not_found_filename == NULL)
-		    not_found_filename = i->so_original_name.c_str ();
+		    not_found_filename = new_so.so_original_name.c_str ();
 		}
 	    }
 
@@ -919,9 +910,12 @@ update_solib_list (int from_tty)
 
 	  /* Notify any observer that the shared object has been
 	     loaded now that we've added it to GDB's tables.  */
-	  notify_solib_loaded (*i);
+	  notify_solib_loaded (new_so);
 	}
 
+      /* Add the new shared objects to GDB's list.  */
+      current_program_space->so_list.splice (std::move (inferior));
+
       /* If a library was not found, issue an appropriate warning
 	 message.  We have to use a single call to warning in case the
 	 front end does something special with warnings, e.g., pop up
@@ -1015,8 +1009,8 @@ solib_add (const char *pattern, int from_tty, int readsyms)
     if (from_tty)
 	add_flags |= SYMFILE_VERBOSE;
 
-    for (struct so_list *gdb : current_program_space->solibs ())
-      if (! pattern || re_exec (gdb->so_name.c_str ()))
+    for (so_list &gdb : current_program_space->solibs ())
+      if (! pattern || re_exec (gdb.so_name.c_str ()))
 	{
 	  /* Normally, we would read the symbols from that library
 	     only if READSYMS is set.  However, we're making a small
@@ -1024,20 +1018,20 @@ solib_add (const char *pattern, int from_tty, int readsyms)
 	     need the library symbols to be loaded in order to provide
 	     thread support (x86-linux for instance).  */
 	  const int add_this_solib =
-	    (readsyms || libpthread_solib_p (*gdb));
+	    (readsyms || libpthread_solib_p (gdb));
 
 	  any_matches = true;
 	  if (add_this_solib)
 	    {
-	      if (gdb->symbols_loaded)
+	      if (gdb.symbols_loaded)
 		{
 		  /* If no pattern was given, be quiet for shared
 		     libraries we have already loaded.  */
 		  if (pattern && (from_tty || info_verbose))
 		    gdb_printf (_("Symbols already loaded for %s\n"),
-				gdb->so_name.c_str ());
+				gdb.so_name.c_str ());
 		}
-	      else if (solib_read_symbols (*gdb, add_flags))
+	      else if (solib_read_symbols (gdb, add_flags))
 		loaded_any_symbols = true;
 	    }
 	}
@@ -1089,11 +1083,11 @@ info_sharedlibrary_command (const char *pattern, int from_tty)
      so we need to make two passes over the libs.  */
 
   nr_libs = 0;
-  for (struct so_list *so : current_program_space->solibs ())
+  for (const so_list &so : current_program_space->solibs ())
     {
-      if (so->so_name[0])
+      if (so.so_name[0])
 	{
-	  if (pattern && ! re_exec (so->so_name.c_str ()))
+	  if (pattern && ! re_exec (so.so_name.c_str ()))
 	    continue;
 	  ++nr_libs;
 	}
@@ -1110,19 +1104,19 @@ info_sharedlibrary_command (const char *pattern, int from_tty)
 
     uiout->table_body ();
 
-    for (struct so_list *so : current_program_space->solibs ())
+    for (const so_list &so : current_program_space->solibs ())
       {
-	if (! so->so_name[0])
+	if (! so.so_name[0])
 	  continue;
-	if (pattern && ! re_exec (so->so_name.c_str ()))
+	if (pattern && ! re_exec (so.so_name.c_str ()))
 	  continue;
 
 	ui_out_emit_tuple tuple_emitter (uiout, "lib");
 
-	if (so->addr_high != 0)
+	if (so.addr_high != 0)
 	  {
-	    uiout->field_core_addr ("from", gdbarch, so->addr_low);
-	    uiout->field_core_addr ("to", gdbarch, so->addr_high);
+	    uiout->field_core_addr ("from", gdbarch, so.addr_low);
+	    uiout->field_core_addr ("to", gdbarch, so.addr_high);
 	  }
 	else
 	  {
@@ -1131,16 +1125,16 @@ info_sharedlibrary_command (const char *pattern, int from_tty)
 	  }
 
 	if (! top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ()
-	    && so->symbols_loaded
-	    && !objfile_has_symbols (so->objfile))
+	    && so.symbols_loaded
+	    && !objfile_has_symbols (so.objfile))
 	  {
 	    so_missing_debug_info = true;
 	    uiout->field_string ("syms-read", "Yes (*)");
 	  }
 	else
-	  uiout->field_string ("syms-read", so->symbols_loaded ? "Yes" : "No");
+	  uiout->field_string ("syms-read", so.symbols_loaded ? "Yes" : "No");
 
-	uiout->field_string ("name", so->so_name, file_name_style.style ());
+	uiout->field_string ("name", so.so_name, file_name_style.style ());
 
 	uiout->text ("\n");
       }
@@ -1188,13 +1182,11 @@ solib_contains_address_p (const so_list &solib,
 const char *
 solib_name_from_address (struct program_space *pspace, CORE_ADDR address)
 {
-  struct so_list *so = NULL;
-
-  for (so = pspace->so_list; so; so = so->next)
-    if (solib_contains_address_p (*so, address))
-      return so->so_name.c_str ();
+  for (const so_list &so : pspace->so_list)
+    if (solib_contains_address_p (so, address))
+      return so.so_name.c_str ();
 
-  return (0);
+  return nullptr;
 }
 
 /* See solib.h.  */
@@ -1219,15 +1211,13 @@ clear_solib (void)
 
   disable_breakpoints_in_shlibs ();
 
-  while (current_program_space->so_list)
+  current_program_space->so_list.clear_and_dispose ([] (so_list &so)
     {
-      struct so_list *so = current_program_space->so_list;
+      notify_solib_unloaded (current_program_space, so);
+      current_program_space->remove_target_sections (&so);
+      free_so (so);
+    });
 
-      current_program_space->so_list = so->next;
-      notify_solib_unloaded (current_program_space, *so);
-      current_program_space->remove_target_sections (so);
-      free_so (*so);
-    }
 
   if (ops->clear_solib != nullptr)
     ops->clear_solib (current_program_space);
@@ -1322,17 +1312,17 @@ reload_shared_libraries_1 (int from_tty)
   if (print_symbol_loading_p (from_tty, 0, 0))
     gdb_printf (_("Loading symbols for shared libraries.\n"));
 
-  for (struct so_list *so : current_program_space->solibs ())
+  for (so_list &so : current_program_space->solibs ())
     {
       const char *found_pathname = NULL;
-      bool was_loaded = so->symbols_loaded != 0;
+      bool was_loaded = so.symbols_loaded != 0;
       symfile_add_flags add_flags = SYMFILE_DEFER_BP_RESET;
 
       if (from_tty)
 	add_flags |= SYMFILE_VERBOSE;
 
       gdb::unique_xmalloc_ptr<char> filename
-	(tilde_expand (so->so_original_name.c_str ()));
+	(tilde_expand (so.so_original_name.c_str ()));
       gdb_bfd_ref_ptr abfd (solib_bfd_open (filename.get ()));
       if (abfd != NULL)
 	found_pathname = bfd_get_filename (abfd.get ());
@@ -1341,26 +1331,26 @@ reload_shared_libraries_1 (int from_tty)
 	 symbol file, close that.  */
       if ((found_pathname == NULL && was_loaded)
 	  || (found_pathname != NULL
-	      && filename_cmp (found_pathname, so->so_name.c_str ()) != 0))
+	      && filename_cmp (found_pathname, so.so_name.c_str ()) != 0))
 	{
-	  if (so->objfile && ! (so->objfile->flags & OBJF_USERLOADED)
+	  if (so.objfile && ! (so.objfile->flags & OBJF_USERLOADED)
 	      && !solib_used (so))
-	    so->objfile->unlink ();
-	  current_program_space->remove_target_sections (so);
-	  so->clear ();
+	    so.objfile->unlink ();
+	  current_program_space->remove_target_sections (&so);
+	  so.clear ();
 	}
 
       /* If this shared library is now associated with a new symbol
 	 file, open it.  */
       if (found_pathname != NULL
 	  && (!was_loaded
-	      || filename_cmp (found_pathname, so->so_name.c_str ()) != 0))
+	      || filename_cmp (found_pathname, so.so_name.c_str ()) != 0))
 	{
 	  bool got_error = false;
 
 	  try
 	    {
-	      solib_map_sections (*so);
+	      solib_map_sections (so);
 	    }
 
 	  catch (const gdb_exception_error &e)
@@ -1372,8 +1362,8 @@ reload_shared_libraries_1 (int from_tty)
 	    }
 
 	    if (!got_error
-		&& (auto_solib_add || was_loaded || libpthread_solib_p (*so)))
-	      solib_read_symbols (*so, add_flags);
+		&& (auto_solib_add || was_loaded || libpthread_solib_p (so)))
+	      solib_read_symbols (so, add_flags);
 	}
     }
 }
@@ -1729,9 +1719,9 @@ remove_user_added_objfile (struct objfile *objfile)
 {
   if (objfile != 0 && objfile->flags & OBJF_USERLOADED)
     {
-      for (struct so_list *so : objfile->pspace->solibs ())
-	if (so->objfile == objfile)
-	  so->objfile = NULL;
+      for (so_list &so : objfile->pspace->solibs ())
+	if (so.objfile == objfile)
+	  so.objfile = nullptr;
     }
 }
 
diff --git a/gdb/solist.h b/gdb/solist.h
index 9320acb7fd3e..31b823a3a482 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -36,7 +36,7 @@ struct lm_info
 
 using lm_info_up = std::unique_ptr<lm_info>;
 
-struct so_list
+struct so_list : intrusive_list_node<so_list>
 {
   /* Free symbol-file related contents of SO and reset for possible reloading
      of SO.  If we have opened a BFD for SO, close it.  If we have placed SO's
@@ -121,7 +121,7 @@ struct target_so_ops
      inferior --- we don't examine any of the shared library files
      themselves.  The declaration of `struct so_list' says which fields
      we provide values for.  */
-  struct so_list *(*current_sos) (void);
+  intrusive_list<so_list> (*current_sos) ();
 
   /* Find, open, and read the symbols for the main executable.  If
      FROM_TTY is non-zero, allow messages to be printed.  */
@@ -171,8 +171,6 @@ struct target_so_ops
   void (*handle_event) (void);
 };
 
-using so_list_range = next_range<so_list>;
-
 /* Free the memory associated with a (so_list *).  */
 void free_so (so_list &so);
 
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 22/24] gdb: don't call so_list::clear in free_so
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (20 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 21/24] gdb: link so_list using intrusive_list Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-10 20:40 ` [PATCH 23/24] gdb: remove free_so function Simon Marchi
                   ` (3 subsequent siblings)
  25 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

I think this `so.clear ()` call is not useful.

 - so_list::clear deletes some things that now get automatically deleted
   when the so_list gets deleted right after in free_so.
 - so_list::clear resets some scalar fields of so_list, which we don't
   really care about since the so_list gets deleted right after.
 - so_list::clear calls target_so_ops::clear_so, of which there is a
   single implementation, svr4_clear_so.  That implementation just
   resets a field in lm_info_svr4, which we don't care about, as it will
   get deleted when the so_list gets deleted right after.

Change-Id: Ie4d72f2a04a4129e55c460bb5c69bc0af0d12b32
---
 gdb/solib.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/gdb/solib.c b/gdb/solib.c
index 9c10340ff583..2e4881798339 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -664,7 +664,6 @@ lm_info::~lm_info () = default;
 void
 free_so (so_list &so)
 {
-  so.clear ();
   delete &so;
 }
 
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 23/24] gdb: remove free_so function
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (21 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 22/24] gdb: don't call so_list::clear in free_so Simon Marchi
@ 2023-10-10 20:40 ` Simon Marchi
  2023-10-10 20:49 ` [PATCH 24/24] gdb: rename struct so_list to so Simon Marchi
                   ` (2 subsequent siblings)
  25 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Remove this function, replace it with deleting the so_list in callers.

Change-Id: Idbd0cb84674ade1d8e17af471550dbd388264f60
---
 gdb/solib-svr4.c |  2 +-
 gdb/solib.c      | 24 +++---------------------
 gdb/solist.h     | 14 +-------------
 3 files changed, 5 insertions(+), 35 deletions(-)

diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 6311599ff709..4628c487ff79 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -1478,7 +1478,7 @@ svr4_current_sos ()
 	  if (address_in_mem_range (li->l_ld, &vsyscall_range))
 	    {
 	      auto next = sos.erase (so);
-	      free_so (*so);
+	      delete &*so;
 	      so = next;
 	      break;
 	    }
diff --git a/gdb/solib.c b/gdb/solib.c
index 2e4881798339..5272dce6fa4a 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -650,24 +650,6 @@ so_list::clear ()
 
 lm_info::~lm_info () = default;
 
-/* Free the storage associated with the `struct so_list' object SO.
-   If we have opened a BFD for SO, close it.
-
-   The caller is responsible for removing SO from whatever list it is
-   a member of.  If we have placed SO's sections in some target's
-   section table, the caller is responsible for removing them.
-
-   This function doesn't mess with objfiles at all.  If there is an
-   objfile associated with SO that needs to be removed, the caller is
-   responsible for taking care of that.  */
-
-void
-free_so (so_list &so)
-{
-  delete &so;
-}
-
-
 /* Read in symbols for shared object SO.  If SYMFILE_VERBOSE is set in FLAGS,
    be chatty about it.  Return true if any symbols were actually loaded.  */
 
@@ -845,7 +827,7 @@ update_solib_list (int from_tty)
       if (inferior_iter != inferior.end ())
 	{
 	  inferior.erase (inferior_iter);
-	  free_so (*inferior_iter);
+	  delete &*inferior_iter;
 	  ++gdb_iter;
 	}
 
@@ -871,7 +853,7 @@ update_solib_list (int from_tty)
 	     sections from so.abfd; remove them.  */
 	  current_program_space->remove_target_sections (&*gdb_iter);
 
-	  free_so (*gdb_iter);
+	  delete &*gdb_iter;
 	  gdb_iter = gdb_iter_next;
 	}
     }
@@ -1214,7 +1196,7 @@ clear_solib (void)
     {
       notify_solib_unloaded (current_program_space, so);
       current_program_space->remove_target_sections (&so);
-      free_so (so);
+      delete &so;
     });
 
 
diff --git a/gdb/solist.h b/gdb/solist.h
index 31b823a3a482..4232156c17a7 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -171,20 +171,8 @@ struct target_so_ops
   void (*handle_event) (void);
 };
 
-/* Free the memory associated with a (so_list *).  */
-void free_so (so_list &so);
-
-/* A deleter that calls free_so.  */
-struct so_deleter
-{
-  void operator() (struct so_list *so) const
-  {
-    free_so (*so);
-  }
-};
-
 /* A unique pointer to a so_list.  */
-typedef std::unique_ptr<so_list, so_deleter> so_list_up;
+using so_list_up = std::unique_ptr<so_list>;
 
 /* Find main executable binary file.  */
 extern gdb::unique_xmalloc_ptr<char> exec_file_find (const char *in_pathname,
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* [PATCH 24/24] gdb: rename struct so_list to so
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (22 preceding siblings ...)
  2023-10-10 20:40 ` [PATCH 23/24] gdb: remove free_so function Simon Marchi
@ 2023-10-10 20:49 ` Simon Marchi
  2023-10-17 19:20 ` [PATCH 00/24] C++ification of struct so_list Pedro Alves
  2023-10-19 11:09 ` [PATCH 0/24] " Lancelot SIX
  25 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-10 20:49 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Now that so_list lists are implemented using intrusive_list, it doesn't
really make sense for the element type to be named "_list".  Rename to
just `struct so`.

Change-Id: I1063061901298bb40fee73bf0cce44cd12154c0e
---
 gdb/break-catch-load.c |  2 +-
 gdb/breakpoint.c       |  5 ++---
 gdb/bsd-uthread.c      |  4 ++--
 gdb/hppa-tdep.c        |  2 +-
 gdb/hppa-tdep.h        |  4 ++--
 gdb/interps.c          |  4 ++--
 gdb/interps.h          | 10 ++++-----
 gdb/mi/mi-cmd-file.c   |  2 +-
 gdb/mi/mi-interp.c     |  6 +++---
 gdb/mi/mi-interp.h     |  6 +++---
 gdb/nto-tdep.c         |  4 ++--
 gdb/nto-tdep.h         |  2 +-
 gdb/observable.h       |  6 +++---
 gdb/progspace.c        |  2 +-
 gdb/progspace.h        |  8 +++----
 gdb/solib-aix.c        | 12 +++++------
 gdb/solib-darwin.c     | 10 ++++-----
 gdb/solib-dsbt.c       | 12 +++++------
 gdb/solib-frv.c        | 14 ++++++-------
 gdb/solib-rocm.c       | 16 +++++++-------
 gdb/solib-svr4.c       | 46 ++++++++++++++++++++---------------------
 gdb/solib-target.c     | 10 ++++-----
 gdb/solib.c            | 47 +++++++++++++++++++++---------------------
 gdb/solib.h            |  6 +++---
 gdb/solist.h           | 16 +++++++-------
 25 files changed, 127 insertions(+), 129 deletions(-)

diff --git a/gdb/break-catch-load.c b/gdb/break-catch-load.c
index 999de874d5f6..e2d5699f8644 100644
--- a/gdb/break-catch-load.c
+++ b/gdb/break-catch-load.c
@@ -117,7 +117,7 @@ solib_catchpoint::check_status (struct bpstat *bs)
 {
   if (is_load)
     {
-      for (so_list *iter : current_program_space->added_solibs)
+      for (so *iter : current_program_space->added_solibs)
 	{
 	  if (!regex
 	      || compiled->exec (iter->so_name.c_str (), 0, NULL, 0) == 0)
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 3ddd1623cf00..3db76b2ca219 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -5046,7 +5046,7 @@ print_solib_event (bool is_catchpoint)
       current_uiout->text (_("  Inferior loaded "));
       ui_out_emit_list list_emitter (current_uiout, "added");
       bool first = true;
-      for (so_list *iter : current_program_space->added_solibs)
+      for (so *iter : current_program_space->added_solibs)
 	{
 	  if (!first)
 	    current_uiout->text ("    ");
@@ -8018,8 +8018,7 @@ disable_breakpoints_in_shlibs (void)
    disabled ones can just stay disabled.  */
 
 static void
-disable_breakpoints_in_unloaded_shlib (program_space *pspace,
-				       const so_list &solib)
+disable_breakpoints_in_unloaded_shlib (program_space *pspace, const so &solib)
 {
   bool disabled_shlib_breaks = false;
 
diff --git a/gdb/bsd-uthread.c b/gdb/bsd-uthread.c
index 8144f8cc64b7..3e8909d4aae2 100644
--- a/gdb/bsd-uthread.c
+++ b/gdb/bsd-uthread.c
@@ -275,7 +275,7 @@ static const char * const bsd_uthread_solib_names[] =
 };
 
 static void
-bsd_uthread_solib_loaded (so_list &so)
+bsd_uthread_solib_loaded (so &so)
 {
   const char * const *names = bsd_uthread_solib_names;
 
@@ -295,7 +295,7 @@ bsd_uthread_solib_loaded (so_list &so)
 }
 
 static void
-bsd_uthread_solib_unloaded (program_space *pspace, const so_list &so)
+bsd_uthread_solib_unloaded (program_space *pspace, const so &so)
 {
   if (bsd_uthread_solib_name.empty ())
     return;
diff --git a/gdb/hppa-tdep.c b/gdb/hppa-tdep.c
index b7c96e8b050d..233d100400ec 100644
--- a/gdb/hppa-tdep.c
+++ b/gdb/hppa-tdep.c
@@ -71,7 +71,7 @@ struct hppa_unwind_info
 struct hppa_objfile_private
   {
     struct hppa_unwind_info *unwind_info = nullptr;	/* a pointer */
-    struct so_list *so_info = nullptr;			/* a pointer  */
+    so *so_info = nullptr;			/* a pointer  */
     CORE_ADDR dp = 0;
 
     int dummy_call_sequence_reg = 0;
diff --git a/gdb/hppa-tdep.h b/gdb/hppa-tdep.h
index 62a5fb8d707e..71d2f4832b15 100644
--- a/gdb/hppa-tdep.h
+++ b/gdb/hppa-tdep.h
@@ -24,7 +24,7 @@
 
 struct trad_frame_saved_reg;
 struct objfile;
-struct so_list;
+struct so;
 
 /* Register numbers of various important registers.  */
 
@@ -114,7 +114,7 @@ struct hppa_gdbarch_tdep : gdbarch_tdep_base
 
   /* These are solib-dependent methods.  They are really HPUX only, but
      we don't have a HPUX-specific tdep vector at the moment.  */
-  CORE_ADDR (*solib_thread_start_addr) (struct so_list *so) = nullptr;
+  CORE_ADDR (*solib_thread_start_addr) (so *so) = nullptr;
   CORE_ADDR (*solib_get_got_by_pc) (CORE_ADDR addr) = nullptr;
   CORE_ADDR (*solib_get_solib_by_pc) (CORE_ADDR addr) = nullptr;
   CORE_ADDR (*solib_get_text_base) (struct objfile *objfile) = nullptr;
diff --git a/gdb/interps.c b/gdb/interps.c
index 544539b0c798..4ca6317918f4 100644
--- a/gdb/interps.c
+++ b/gdb/interps.c
@@ -488,7 +488,7 @@ interps_notify_target_resumed (ptid_t ptid)
 /* See interps.h.  */
 
 void
-interps_notify_solib_loaded (const so_list &so)
+interps_notify_solib_loaded (const so &so)
 {
   interps_notify (&interp::on_solib_loaded, so);
 }
@@ -496,7 +496,7 @@ interps_notify_solib_loaded (const so_list &so)
 /* See interps.h.  */
 
 void
-interps_notify_solib_unloaded (const so_list &so)
+interps_notify_solib_unloaded (const so &so)
 {
   interps_notify (&interp::on_solib_unloaded, so);
 }
diff --git a/gdb/interps.h b/gdb/interps.h
index a90c5aa17b0a..a613885ac403 100644
--- a/gdb/interps.h
+++ b/gdb/interps.h
@@ -31,7 +31,7 @@ struct ui;
 class completion_tracker;
 struct thread_info;
 struct inferior;
-struct so_list;
+struct so;
 struct trace_state_variable;
 
 typedef struct interp *(*interp_factory_func) (const char *name);
@@ -147,10 +147,10 @@ class interp : public intrusive_list_node<interp>
   virtual void on_target_resumed (ptid_t ptid) {}
 
   /* Notify the interpreter that solib SO has been loaded.  */
-  virtual void on_solib_loaded (const so_list &so) {}
+  virtual void on_solib_loaded (const so &so) {}
 
   /* Notify the interpreter that solib SO has been unloaded.  */
-  virtual void on_solib_unloaded (const so_list &so) {}
+  virtual void on_solib_unloaded (const so &so) {}
 
   /* Notify the interpreter that a command it is executing is about to cause
      the inferior to proceed.  */
@@ -324,10 +324,10 @@ extern void interps_notify_record_changed (inferior *inf, int started,
 extern void interps_notify_target_resumed (ptid_t ptid);
 
 /* Notify all interpreters that solib SO has been loaded.  */
-extern void interps_notify_solib_loaded (const so_list &so);
+extern void interps_notify_solib_loaded (const so &so);
 
 /* Notify all interpreters that solib SO has been unloaded.  */
-extern void interps_notify_solib_unloaded (const so_list &so);
+extern void interps_notify_solib_unloaded (const so &so);
 
 /* Notify all interpreters that the selected traceframe changed.
 
diff --git a/gdb/mi/mi-cmd-file.c b/gdb/mi/mi-cmd-file.c
index adc324e64fc8..e5e21fff366e 100644
--- a/gdb/mi/mi-cmd-file.c
+++ b/gdb/mi/mi-cmd-file.c
@@ -163,7 +163,7 @@ mi_cmd_file_list_shared_libraries (const char *command,
   /* Print the table header.  */
   ui_out_emit_list list_emitter (uiout, "shared-libraries");
 
-  for (const so_list &so : current_program_space->solibs ())
+  for (const so &so : current_program_space->solibs ())
     {
       if (so.so_name[0] == '\0')
 	continue;
diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
index 0fae5674201c..f435730e784b 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -724,7 +724,7 @@ mi_interp::on_target_resumed (ptid_t ptid)
 /* See mi-interp.h.  */
 
 void
-mi_output_solib_attribs (ui_out *uiout, const so_list &solib)
+mi_output_solib_attribs (ui_out *uiout, const so &solib)
 {
   gdbarch *gdbarch = current_inferior ()->arch ();
 
@@ -745,7 +745,7 @@ mi_output_solib_attribs (ui_out *uiout, const so_list &solib)
 }
 
 void
-mi_interp::on_solib_loaded (const so_list &solib)
+mi_interp::on_solib_loaded (const so &solib)
 {
   ui_out *uiout = this->interp_ui_out ();
 
@@ -762,7 +762,7 @@ mi_interp::on_solib_loaded (const so_list &solib)
 }
 
 void
-mi_interp::on_solib_unloaded (const so_list &solib)
+mi_interp::on_solib_unloaded (const so &solib)
 {
   ui_out *uiout = this->interp_ui_out ();
 
diff --git a/gdb/mi/mi-interp.h b/gdb/mi/mi-interp.h
index 781a8dc6f466..76ff459203cc 100644
--- a/gdb/mi/mi-interp.h
+++ b/gdb/mi/mi-interp.h
@@ -60,8 +60,8 @@ class mi_interp final : public interp
   void on_record_changed (inferior *inf, int started, const char *method,
 			  const char *format) override;
   void on_target_resumed (ptid_t ptid) override;
-  void on_solib_loaded (const so_list &so) override;
-  void on_solib_unloaded (const so_list &so) override;
+  void on_solib_loaded (const so &so) override;
+  void on_solib_unloaded (const so &so) override;
   void on_about_to_proceed () override;
   void on_traceframe_changed (int tfnum, int tpnum) override;
   void on_tsv_created (const trace_state_variable *tsv) override;
@@ -108,7 +108,7 @@ class mi_interp final : public interp
 
 /* Output the shared object attributes to UIOUT.  */
 
-void mi_output_solib_attribs (ui_out *uiout, const so_list &solib);
+void mi_output_solib_attribs (ui_out *uiout, const so &solib);
 
 /* Returns the INTERP's data cast as mi_interp if INTERP is an MI, and
    returns NULL otherwise.  */
diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index bcc32fd857ed..49b888f753e1 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -245,7 +245,7 @@ nto_parse_redirection (char *pargv[], const char **pin, const char **pout,
 }
 
 static CORE_ADDR
-lm_addr (const so_list &so)
+lm_addr (const so &so)
 {
   auto *li = gdb::checked_static_cast<const lm_info_svr4 *> (so.lm_info.get ());
 
@@ -283,7 +283,7 @@ find_load_phdr (bfd *abfd)
 }
 
 void
-nto_relocate_section_addresses (so_list &so, target_section *sec)
+nto_relocate_section_addresses (so &so, target_section *sec)
 {
   /* Neutrino treats the l_addr base address field in link.h as different than
      the base address in the System V ABI and so the offset needs to be
diff --git a/gdb/nto-tdep.h b/gdb/nto-tdep.h
index 8b644b30c42a..322e690ec26b 100644
--- a/gdb/nto-tdep.h
+++ b/gdb/nto-tdep.h
@@ -166,7 +166,7 @@ void nto_init_solib_absolute_prefix (void);
 char **nto_parse_redirection (char *start_argv[], const char **in,
 			      const char **out, const char **err);
 
-void nto_relocate_section_addresses (so_list &, target_section *);
+void nto_relocate_section_addresses (so &, target_section *);
 
 int nto_map_arch_to_cputype (const char *);
 
diff --git a/gdb/observable.h b/gdb/observable.h
index 5ed6ca547ce0..37a8cee49698 100644
--- a/gdb/observable.h
+++ b/gdb/observable.h
@@ -24,7 +24,7 @@
 #include "target/waitstatus.h"
 
 struct bpstat;
-struct so_list;
+struct so;
 struct objfile;
 struct thread_info;
 struct inferior;
@@ -99,12 +99,12 @@ extern observable<inferior */* parent_inf */, inferior */* child_inf */,
 /* The shared library specified by SOLIB has been loaded.  Note that
    when gdb calls this observer, the library's symbols probably
    haven't been loaded yet.  */
-extern observable<so_list &/* solib */> solib_loaded;
+extern observable<so &/* solib */> solib_loaded;
 
 /* The shared library SOLIB has been unloaded from program space PSPACE.
    Note  when gdb calls this observer, the library's symbols have not
    been unloaded yet, and thus are still available.  */
-extern observable<program_space *, const so_list &/* solib */> solib_unloaded;
+extern observable<program_space *, const so &/* solib */> solib_unloaded;
 
 /* The symbol file specified by OBJFILE has been loaded.  */
 extern observable<struct objfile */* objfile */> new_objfile;
diff --git a/gdb/progspace.c b/gdb/progspace.c
index 4b816b4dac8b..49cccdbd7d5c 100644
--- a/gdb/progspace.c
+++ b/gdb/progspace.c
@@ -132,7 +132,7 @@ void
 program_space::free_all_objfiles ()
 {
   /* Any objfile reference would become stale.  */
-  for (const struct so_list &so : current_program_space->solibs ())
+  for (const so &so : current_program_space->solibs ())
     gdb_assert (so.objfile == NULL);
 
   while (!objfiles_list.empty ())
diff --git a/gdb/progspace.h b/gdb/progspace.h
index 0a762e6ad5c8..c5cf975cc8f7 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -39,7 +39,7 @@ struct inferior;
 struct exec;
 struct address_space;
 struct program_space;
-struct so_list;
+struct so;
 
 typedef std::list<std::unique_ptr<objfile>> objfile_list;
 
@@ -255,7 +255,7 @@ struct program_space
   struct objfile *objfile_for_address (CORE_ADDR address);
 
   /* Return the list of  all the solibs in this program space.  */
-  intrusive_list<struct so_list> &solibs ()
+  intrusive_list<so> &solibs ()
   { return so_list; }
 
   /* Close and clear exec_bfd.  If we end up with no target sections
@@ -359,14 +359,14 @@ struct program_space
 
   /* List of shared objects mapped into this space.  Managed by
      solib.c.  */
-  intrusive_list<struct so_list> so_list;
+  intrusive_list<so> so_list;
 
   /* Number of calls to solib_add.  */
   unsigned int solib_add_generation = 0;
 
   /* When an solib is added, it is also added to this vector.  This
      is so we can properly report solib changes to the user.  */
-  std::vector<struct so_list *> added_solibs;
+  std::vector<so *> added_solibs;
 
   /* When an solib is removed, its name is added to this vector.
      This is so we can properly report solib changes to the user.  */
diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index 68798e8735ac..8760c8a730ea 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -26,7 +26,7 @@
 #include "xcoffread.h"
 #include "observable.h"
 
-/* Our private data in struct so_list.  */
+/* Our private data in struct so.  */
 
 struct lm_info_aix final : public lm_info
 {
@@ -311,7 +311,7 @@ solib_aix_bss_data_overlap (bfd *abfd)
 /* Implement the "relocate_section_addresses" target_so_ops method.  */
 
 static void
-solib_aix_relocate_section_addresses (so_list &so, target_section *sec)
+solib_aix_relocate_section_addresses (so &so, target_section *sec)
 {
   struct bfd_section *bfd_sect = sec->the_bfd_section;
   bfd *abfd = bfd_sect->owner;
@@ -445,7 +445,7 @@ solib_aix_solib_create_inferior_hook (int from_tty)
 
 /* Implement the "current_sos" target_so_ops method.  */
 
-static intrusive_list<so_list>
+static intrusive_list<so>
 solib_aix_current_sos ()
 {
   gdb::optional<std::vector<lm_info_aix>> &library_list
@@ -453,14 +453,14 @@ solib_aix_current_sos ()
   if (!library_list.has_value ())
     return {};
 
-  intrusive_list<so_list> sos;
+  intrusive_list<so> sos;
 
-  /* Build a struct so_list for each entry on the list.
+  /* Build a struct so for each entry on the list.
      We skip the first entry, since this is the entry corresponding
      to the main executable, not a shared library.  */
   for (int ix = 1; ix < library_list->size (); ix++)
     {
-      so_list *new_solib = new so_list;
+      so *new_solib = new so;
       std::string so_name;
 
       lm_info_aix &info = (*library_list)[ix];
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index 5e3aabbfb831..b2e045642417 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -212,7 +212,7 @@ open_symbol_file_object (int from_tty)
 
 /* Build a list of currently loaded shared objects.  See solib-svr4.c.  */
 
-static intrusive_list<so_list>
+static intrusive_list<so>
 darwin_current_sos ()
 {
   type *ptr_type
@@ -230,7 +230,7 @@ darwin_current_sos ()
 
   image_info_size = ptr_len * 3;
 
-  intrusive_list<so_list> sos;
+  intrusive_list<so> sos;
 
   /* Read infos for each solib.
      The first entry was rumored to be the executable itself, but this is not
@@ -271,8 +271,8 @@ darwin_current_sos ()
       if (file_path == nullptr)
 	break;
 
-      /* Create and fill the new so_list element.  */
-      so_list *newobj = new so_list;
+      /* Create and fill the new struct so element.  */
+      so *newobj = new so;
 
       auto li = gdb::make_unique<lm_info_darwin> ();
 
@@ -605,7 +605,7 @@ darwin_clear_solib (program_space *pspace)
    Relocate these VMAs according to solib info.  */
 
 static void
-darwin_relocate_section_addresses (so_list &so, target_section *sec)
+darwin_relocate_section_addresses (so &so, target_section *sec)
 {
   auto *li = gdb::checked_static_cast<lm_info_darwin *> (so.lm_info.get ());
 
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index 0c60ad69753d..26e6081d9a2a 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -503,22 +503,22 @@ lm_base (void)
 }
 
 
-/* Build a list of `struct so_list' objects describing the shared
+/* Build a list of `struct so' objects describing the shared
    objects currently loaded in the inferior.  This list does not
    include an entry for the main executable file.
 
    Note that we only gather information directly available from the
    inferior --- we don't examine any of the shared library files
-   themselves.  The declaration of `struct so_list' says which fields
+   themselves.  The declaration of `struct so' says which fields
    we provide values for.  */
 
-static intrusive_list<so_list>
+static intrusive_list<so>
 dsbt_current_sos (void)
 {
   bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
   CORE_ADDR lm_addr;
   dsbt_info *info = get_dsbt_info (current_program_space);
-  intrusive_list<so_list> sos;
+  intrusive_list<so> sos;
 
   /* Make sure that the main executable has been relocated.  This is
      required in order to find the address of the global offset table,
@@ -593,7 +593,7 @@ dsbt_current_sos (void)
 	      break;
 	    }
 
-	  so_list *sop = new so_list;
+	  so *sop = new so;
 	  auto li = gdb::make_unique<lm_info_dsbt> ();
 	  li->map = loadmap;
 	  /* Fetch the name.  */
@@ -876,7 +876,7 @@ dsbt_clear_solib (program_space *pspace)
 }
 
 static void
-dsbt_relocate_section_addresses (so_list &so, target_section *sec)
+dsbt_relocate_section_addresses (so &so, target_section *sec)
 {
   int seg;
   auto *li = gdb::checked_static_cast<lm_info_dsbt *> (so.lm_info.get ());
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index c391462ad9af..6647599b2dd6 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -306,12 +306,12 @@ lm_base (void)
 
 /* Implement the "current_sos" target_so_ops method.  */
 
-static intrusive_list<so_list>
+static intrusive_list<so>
 frv_current_sos ()
 {
   bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
   CORE_ADDR lm_addr, mgot;
-  intrusive_list<so_list> sos;
+  intrusive_list<so> sos;
 
   /* Make sure that the main executable has been relocated.  This is
      required in order to find the address of the global offset table,
@@ -376,7 +376,7 @@ frv_current_sos ()
 	      break;
 	    }
 
-	  so_list *sop = new so_list;
+	  so *sop = new so;
 	  auto li = gdb::make_unique<lm_info_frv> ();
 	  li->map = loadmap;
 	  li->got_value = got_addr;
@@ -811,7 +811,7 @@ frv_clear_solib (program_space *pspace)
 }
 
 static void
-frv_relocate_section_addresses (so_list &so, target_section *sec)
+frv_relocate_section_addresses (so &so, target_section *sec)
 {
   int seg;
   auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info.get ());
@@ -852,7 +852,7 @@ main_got (void)
 CORE_ADDR
 frv_fdpic_find_global_pointer (CORE_ADDR addr)
 {
-  for (const so_list &so : current_program_space->solibs ())
+  for (const so &so : current_program_space->solibs ())
     {
       int seg;
       auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info.get ());
@@ -909,7 +909,7 @@ frv_fdpic_find_canonical_descriptor (CORE_ADDR entry_point)
      in list of shared objects.  */
   if (addr == 0)
     {
-      for (const so_list &so : current_program_space->solibs ())
+      for (const so &so : current_program_space->solibs ())
 	{
 	  auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info.get ());
 
@@ -1061,7 +1061,7 @@ frv_fetch_objfile_link_map (struct objfile *objfile)
 
   /* The other link map addresses may be found by examining the list
      of shared libraries.  */
-  for (const so_list &so : current_program_space->solibs ())
+  for (const so &so : current_program_space->solibs ())
     {
       auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info.get ());
 
diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
index 0dc303b5a651..6642db9fbd1c 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -172,7 +172,7 @@ get_solib_info (inferior *inf)
 /* Relocate section addresses.  */
 
 static void
-rocm_solib_relocate_section_addresses (so_list &so,
+rocm_solib_relocate_section_addresses (so &so,
 				       struct target_section *sec)
 {
   if (!is_amdgpu_arch (gdbarch_from_bfd (so.abfd.get ())))
@@ -204,14 +204,14 @@ rocm_solib_handle_event ()
 
 /* Create so_list objects from rocm_so objects in SOS.  */
 
-static intrusive_list<so_list>
+static intrusive_list<so>
 so_list_from_rocm_sos (const std::vector<rocm_so> &sos)
 {
-  intrusive_list<so_list> dst;
+  intrusive_list<so> dst;
 
   for (const rocm_so &so : sos)
     {
-      so_list *newobj = new so_list;
+      struct so *newobj = new struct so;
       newobj->lm_info = gdb::make_unique<lm_info_svr4> (*so.lm_info);
 
       newobj->so_name = so.name;
@@ -223,14 +223,14 @@ so_list_from_rocm_sos (const std::vector<rocm_so> &sos)
   return dst;
 }
 
-/* Build a list of `struct so_list' objects describing the shared
+/* Build a list of `struct so' objects describing the shared
    objects currently loaded in the inferior.  */
 
-static intrusive_list<so_list>
+static intrusive_list<so>
 rocm_solib_current_sos ()
 {
   /* First, retrieve the host-side shared library list.  */
-  intrusive_list<so_list> sos = svr4_so_ops.current_sos ();
+  intrusive_list<so> sos = svr4_so_ops.current_sos ();
 
   /* Then, the device-side shared library list.  */
   std::vector<rocm_so> &dev_sos = get_solib_info (current_inferior ())->solib_list;
@@ -238,7 +238,7 @@ rocm_solib_current_sos ()
   if (dev_sos.empty ())
     return sos;
 
-  intrusive_list<so_list> dev_so_list = so_list_from_rocm_sos (dev_sos);
+  intrusive_list<so> dev_so_list = so_list_from_rocm_sos (dev_sos);
 
   if (sos.empty ())
     return dev_so_list;
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 4628c487ff79..aea38d2a6266 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -186,7 +186,7 @@ svr4_same (const char *gdb_name, const char *inferior_name,
 }
 
 static int
-svr4_same (const so_list &gdb, const so_list &inferior)
+svr4_same (const so &gdb, const so &inferior)
 {
   auto *lmg
     = gdb::checked_static_cast<const lm_info_svr4 *> (gdb.lm_info.get ());
@@ -239,7 +239,7 @@ has_lm_dynamic_from_link_map (void)
 }
 
 static CORE_ADDR
-lm_addr_check (const so_list &so, bfd *abfd)
+lm_addr_check (const so &so, bfd *abfd)
 {
   auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info.get ());
 
@@ -980,7 +980,7 @@ svr4_free_objfile_observer (struct objfile *objfile)
 /* Implement target_so_ops.clear_so.  */
 
 static void
-svr4_clear_so (const so_list &so)
+svr4_clear_so (const so &so)
 {
   auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info.get ());
 
@@ -990,14 +990,14 @@ svr4_clear_so (const so_list &so)
 
 /* Create the so_list objects equivalent to the svr4_sos in SOS.  */
 
-static intrusive_list<so_list>
+static intrusive_list<so>
 so_list_from_svr4_sos (const std::vector<svr4_so> &sos)
 {
-  intrusive_list<so_list> dst;
+  intrusive_list<so> dst;
 
   for (const svr4_so &so : sos)
     {
-      so_list *newobj = new so_list;
+      struct so *newobj = new struct so;
 
       newobj->so_name = so.name;
       newobj->so_original_name = so.name;
@@ -1183,13 +1183,13 @@ svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
 /* If no shared library information is available from the dynamic
    linker, build a fallback list from other sources.  */
 
-static intrusive_list<so_list>
+static intrusive_list<so>
 svr4_default_sos (svr4_info *info)
 {
   if (!info->debug_loader_offset_p)
     return {};
 
-  so_list *newobj = new so_list;
+  so *newobj = new so;
   auto li = gdb::make_unique<lm_info_svr4> ();
 
   /* Nothing will ever check the other fields if we set l_addr_p.  */
@@ -1200,7 +1200,7 @@ svr4_default_sos (svr4_info *info)
   newobj->so_name = info->debug_loader_name;
   newobj->so_original_name = newobj->so_name;
 
-  intrusive_list<so_list> sos;
+  intrusive_list<so> sos;
   sos.push_back (*newobj);
 
   return sos;
@@ -1372,10 +1372,10 @@ svr4_current_sos_direct (struct svr4_info *info)
 
 /* Collect sos read and stored by the probes interface.  */
 
-static intrusive_list<so_list>
+static intrusive_list<so>
 svr4_collect_probes_sos (svr4_info *info)
 {
-  intrusive_list<so_list> res;
+  intrusive_list<so> res;
 
   for (const auto &tuple : info->solib_lists)
     {
@@ -1389,10 +1389,10 @@ svr4_collect_probes_sos (svr4_info *info)
 /* Implement the main part of the "current_sos" target_so_ops
    method.  */
 
-static intrusive_list<so_list>
+static intrusive_list<so>
 svr4_current_sos_1 (svr4_info *info)
 {
-  intrusive_list<so_list> sos;
+  intrusive_list<so> sos;
 
   /* If we're using the probes interface, we can use the cache as it will
      be maintained by probe update/reload actions.  */
@@ -1416,11 +1416,11 @@ svr4_current_sos_1 (svr4_info *info)
 
 /* Implement the "current_sos" target_so_ops method.  */
 
-static intrusive_list<so_list>
+static intrusive_list<so>
 svr4_current_sos ()
 {
   svr4_info *info = get_svr4_info (current_program_space);
-  intrusive_list<so_list> sos = svr4_current_sos_1 (info);
+  intrusive_list<so> sos = svr4_current_sos_1 (info);
   struct mem_range vsyscall_range;
 
   /* Filter out the vDSO module, if present.  Its symbol file would
@@ -1507,7 +1507,7 @@ svr4_fetch_objfile_link_map (struct objfile *objfile)
 
   /* The other link map addresses may be found by examining the list
      of shared libraries.  */
-  for (const so_list &so : current_program_space->solibs ())
+  for (const so &so : current_program_space->solibs ())
     if (so.objfile == objfile)
       {
 	auto *li
@@ -2348,7 +2348,7 @@ enable_break (struct svr4_info *info, int from_tty)
 
       /* On a running target, we can get the dynamic linker's base
 	 address from the shared library table.  */
-      for (const so_list &so : current_program_space->solibs ())
+      for (const so &so : current_program_space->solibs ())
 	{
 	  if (svr4_same_1 (interp_name, so.so_original_name.c_str ()))
 	    {
@@ -3097,7 +3097,7 @@ svr4_truncate_ptr (CORE_ADDR addr)
 
 
 static void
-svr4_relocate_section_addresses (so_list &so, target_section *sec)
+svr4_relocate_section_addresses (so &so, target_section *sec)
 {
   bfd *abfd = sec->the_bfd_section->owner;
 
@@ -3238,7 +3238,7 @@ svr4_lp64_fetch_link_map_offsets (void)
 
 /* Return the DSO matching OBJFILE or nullptr if none can be found.  */
 
-static const so_list *
+static const so *
 find_solib_for_objfile (struct objfile *objfile)
 {
   if (objfile == nullptr)
@@ -3249,7 +3249,7 @@ find_solib_for_objfile (struct objfile *objfile)
   if (objfile->separate_debug_objfile_backlink != nullptr)
     objfile = objfile->separate_debug_objfile_backlink;
 
-  for (const so_list &so : current_program_space->solibs ())
+  for (const so &so : current_program_space->solibs ())
     if (so.objfile == objfile)
       return &so;
 
@@ -3264,7 +3264,7 @@ find_solib_for_objfile (struct objfile *objfile)
    right thing for the main executable.  */
 
 static CORE_ADDR
-find_debug_base_for_solib (const so_list *solib)
+find_debug_base_for_solib (const so *solib)
 {
   if (solib == nullptr)
     return 0;
@@ -3325,7 +3325,7 @@ svr4_iterate_over_objfiles_in_search_order
   /* The linker namespace to iterate identified by the address of its
      r_debug object, defaulting to the initial namespace.  */
   CORE_ADDR initial = elf_locate_base ();
-  const so_list *curr_solib = find_solib_for_objfile (current_objfile);
+  const so *curr_solib = find_solib_for_objfile (current_objfile);
   CORE_ADDR debug_base = find_debug_base_for_solib (curr_solib);
   if (debug_base == 0)
     debug_base = initial;
@@ -3340,7 +3340,7 @@ svr4_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 so_list *solib = find_solib_for_objfile (objfile);
+      const so *solib = find_solib_for_objfile (objfile);
       CORE_ADDR solib_base = find_debug_base_for_solib (solib);
       if (solib_base == 0)
 	solib_base = initial;
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index 2e1c74f97e70..73ee9166e382 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -227,10 +227,10 @@ solib_target_parse_libraries (const char *library)
 }
 #endif
 
-static intrusive_list<so_list>
+static intrusive_list<so>
 solib_target_current_sos (void)
 {
-  intrusive_list<so_list> sos;
+  intrusive_list<so> sos;
 
   /* Fetch the list of shared libraries.  */
   gdb::optional<gdb::char_vector> library_document
@@ -243,10 +243,10 @@ solib_target_current_sos (void)
   std::vector<lm_info_target_up> library_list
     = solib_target_parse_libraries (library_document->data ());
 
-  /* Build a struct so_list for each entry on the list.  */
+  /* Build a struct so for each entry on the list.  */
   for (lm_info_target_up &info : library_list)
     {
-      so_list *new_solib = new so_list;
+      so *new_solib = new so;
 
       /* We don't need a copy of the name in INFO anymore.  */
       new_solib->so_name = std::move (info->name);
@@ -267,7 +267,7 @@ solib_target_solib_create_inferior_hook (int from_tty)
 }
 
 static void
-solib_target_relocate_section_addresses (so_list &so, target_section *sec)
+solib_target_relocate_section_addresses (so &so, target_section *sec)
 {
   CORE_ADDR offset;
   auto *li = gdb::checked_static_cast<lm_info_target *> (so.lm_info.get ());
diff --git a/gdb/solib.c b/gdb/solib.c
index 5272dce6fa4a..15648bd029b7 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -539,7 +539,7 @@ get_cbfd_soname_build_id (gdb_bfd_ref_ptr abfd, const char *soname)
    expansion stuff?).  */
 
 static int
-solib_map_sections (so_list &so)
+solib_map_sections (so &so)
 {
   const target_so_ops *ops = gdbarch_so_ops (current_inferior ()->arch ());
 
@@ -626,7 +626,7 @@ solib_map_sections (so_list &so)
 /* See solist.h.  */
 
 void
-so_list::clear ()
+so::clear ()
 {
   const target_so_ops *ops = gdbarch_so_ops (current_inferior ()->arch ());
 
@@ -654,7 +654,7 @@ lm_info::~lm_info () = default;
    be chatty about it.  Return true if any symbols were actually loaded.  */
 
 bool
-solib_read_symbols (so_list &so, symfile_add_flags flags)
+solib_read_symbols (so &so, symfile_add_flags flags)
 {
   if (so.symbols_loaded)
     {
@@ -715,9 +715,9 @@ solib_read_symbols (so_list &so, symfile_add_flags flags)
    in the list of shared libraries.  Return false otherwise.  */
 
 static bool
-solib_used (const so_list &known)
+solib_used (const so &known)
 {
-  for (const so_list &pivot : current_program_space->solibs ())
+  for (const so &pivot : current_program_space->solibs ())
     if (&pivot != &known && pivot.objfile == known.objfile)
       return true;
   return false;
@@ -726,7 +726,7 @@ solib_used (const so_list &known)
 /* Notify interpreters and observers that solib SO has been loaded.  */
 
 static void
-notify_solib_loaded (so_list &so)
+notify_solib_loaded (so &so)
 {
   interps_notify_solib_loaded (so);
   gdb::observers::solib_loaded.notify (so);
@@ -735,7 +735,7 @@ notify_solib_loaded (so_list &so)
 /* Notify interpreters and observers that solib SO has been unloaded.  */
 
 static void
-notify_solib_unloaded (program_space *pspace, const so_list &so)
+notify_solib_unloaded (program_space *pspace, const so &so)
 {
   interps_notify_solib_unloaded (so);
   gdb::observers::solib_unloaded.notify (pspace, so);
@@ -797,12 +797,12 @@ update_solib_list (int from_tty)
      the time we're done walking GDB's list, the inferior's list
      contains only the new shared objects, which we then add.  */
 
-  intrusive_list<so_list> inferior = ops->current_sos ();
-  intrusive_list<so_list>::iterator gdb_iter
+  intrusive_list<so> inferior = ops->current_sos ();
+  intrusive_list<so>::iterator gdb_iter
     = current_program_space->so_list.begin ();
   while (gdb_iter != current_program_space->so_list.end ())
     {
-      intrusive_list<so_list>::iterator inferior_iter = inferior.begin ();
+      intrusive_list<so>::iterator inferior_iter = inferior.begin ();
 
       /* Check to see whether the shared object *gdb also appears in
 	 the inferior's current list.  */
@@ -840,7 +840,7 @@ update_solib_list (int from_tty)
 
 	  current_program_space->deleted_solibs.push_back (gdb_iter->so_name);
 
-	  intrusive_list<so_list>::iterator gdb_iter_next
+	  intrusive_list<so>::iterator gdb_iter_next
 	    = current_program_space->so_list.erase (gdb_iter);
 
 	  /* Unless the user loaded it explicitly, free SO's objfile.  */
@@ -866,14 +866,14 @@ update_solib_list (int from_tty)
       int not_found = 0;
       const char *not_found_filename = NULL;
 
-      /* Fill in the rest of each of the `struct so_list' nodes.  */
-      for (so_list &new_so : inferior)
+      /* Fill in the rest of each of the `so' nodes.  */
+      for (so &new_so : inferior)
 	{
 	  current_program_space->added_solibs.push_back (&new_so);
 
 	  try
 	    {
-	      /* Fill in the rest of the `struct so_list' node.  */
+	      /* Fill in the rest of the `struct so' node.  */
 	      if (!solib_map_sections (new_so))
 		{
 		  not_found++;
@@ -939,7 +939,7 @@ libpthread_name_p (const char *name)
 /* Return non-zero if SO is the libpthread shared library.  */
 
 static bool
-libpthread_solib_p (const so_list &so)
+libpthread_solib_p (const so &so)
 {
   return libpthread_name_p (so.so_name.c_str ());
 }
@@ -990,7 +990,7 @@ solib_add (const char *pattern, int from_tty, int readsyms)
     if (from_tty)
 	add_flags |= SYMFILE_VERBOSE;
 
-    for (so_list &gdb : current_program_space->solibs ())
+    for (so &gdb : current_program_space->solibs ())
       if (! pattern || re_exec (gdb.so_name.c_str ()))
 	{
 	  /* Normally, we would read the symbols from that library
@@ -1064,7 +1064,7 @@ info_sharedlibrary_command (const char *pattern, int from_tty)
      so we need to make two passes over the libs.  */
 
   nr_libs = 0;
-  for (const so_list &so : current_program_space->solibs ())
+  for (const so &so : current_program_space->solibs ())
     {
       if (so.so_name[0])
 	{
@@ -1085,7 +1085,7 @@ info_sharedlibrary_command (const char *pattern, int from_tty)
 
     uiout->table_body ();
 
-    for (const so_list &so : current_program_space->solibs ())
+    for (const so &so : current_program_space->solibs ())
       {
 	if (! so.so_name[0])
 	  continue;
@@ -1139,8 +1139,7 @@ info_sharedlibrary_command (const char *pattern, int from_tty)
 /* See solib.h.  */
 
 bool
-solib_contains_address_p (const so_list &solib,
-			  CORE_ADDR address)
+solib_contains_address_p (const so &solib, CORE_ADDR address)
 {
   for (const target_section &p : solib.sections)
     if (p.addr <= address && address < p.endaddr)
@@ -1163,7 +1162,7 @@ solib_contains_address_p (const so_list &solib,
 const char *
 solib_name_from_address (struct program_space *pspace, CORE_ADDR address)
 {
-  for (const so_list &so : pspace->so_list)
+  for (const so &so : pspace->so_list)
     if (solib_contains_address_p (so, address))
       return so.so_name.c_str ();
 
@@ -1192,7 +1191,7 @@ clear_solib (void)
 
   disable_breakpoints_in_shlibs ();
 
-  current_program_space->so_list.clear_and_dispose ([] (so_list &so)
+  current_program_space->so_list.clear_and_dispose ([] (so &so)
     {
       notify_solib_unloaded (current_program_space, so);
       current_program_space->remove_target_sections (&so);
@@ -1293,7 +1292,7 @@ reload_shared_libraries_1 (int from_tty)
   if (print_symbol_loading_p (from_tty, 0, 0))
     gdb_printf (_("Loading symbols for shared libraries.\n"));
 
-  for (so_list &so : current_program_space->solibs ())
+  for (so &so : current_program_space->solibs ())
     {
       const char *found_pathname = NULL;
       bool was_loaded = so.symbols_loaded != 0;
@@ -1700,7 +1699,7 @@ remove_user_added_objfile (struct objfile *objfile)
 {
   if (objfile != 0 && objfile->flags & OBJF_USERLOADED)
     {
-      for (so_list &so : objfile->pspace->solibs ())
+      for (so &so : objfile->pspace->solibs ())
 	if (so.objfile == objfile)
 	  so.objfile = nullptr;
     }
diff --git a/gdb/solib.h b/gdb/solib.h
index 88ade882396d..a5854f283f8f 100644
--- a/gdb/solib.h
+++ b/gdb/solib.h
@@ -21,7 +21,7 @@
 #define SOLIB_H
 
 /* Forward decl's for prototypes */
-struct so_list;
+struct so;
 struct target_ops;
 struct target_so_ops;
 struct program_space;
@@ -50,7 +50,7 @@ extern void clear_solib (void);
 /* Called to add symbols from a shared library to gdb's symbol table.  */
 
 extern void solib_add (const char *, int, int);
-extern bool solib_read_symbols (so_list &, symfile_add_flags);
+extern bool solib_read_symbols (so &, symfile_add_flags);
 
 /* Function to be called when the inferior starts up, to discover the
    names of shared libraries that are dynamically linked, the base
@@ -65,7 +65,7 @@ extern const char *solib_name_from_address (struct program_space *, CORE_ADDR);
 
 /* Return true if ADDR lies within SOLIB.  */
 
-extern bool solib_contains_address_p (const so_list &, CORE_ADDR);
+extern bool solib_contains_address_p (const so &, CORE_ADDR);
 
 /* Return whether the data starting at VADDR, size SIZE, must be kept
    in a core file for shared libraries loaded before "gcore" is used
diff --git a/gdb/solist.h b/gdb/solist.h
index 4232156c17a7..94fedb79549a 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -36,7 +36,7 @@ struct lm_info
 
 using lm_info_up = std::unique_ptr<lm_info>;
 
-struct so_list : intrusive_list_node<so_list>
+struct so : intrusive_list_node<so>
 {
   /* Free symbol-file related contents of SO and reset for possible reloading
      of SO.  If we have opened a BFD for SO, close it.  If we have placed SO's
@@ -52,7 +52,7 @@ struct so_list : intrusive_list_node<so_list>
      dynamic linker's tables in the inferior, and are initialized by
      current_sos.  */
 
-  struct so_list *next = nullptr;	/* next structure in linked list */
+  so *next = nullptr;	/* next structure in linked list */
 
   /* A pointer to target specific link map information.  Often this
      will be a copy of struct link_map from the user process, but
@@ -99,12 +99,12 @@ struct target_so_ops
 {
   /* Adjust the section binding addresses by the base address at
      which the object was actually mapped.  */
-  void (*relocate_section_addresses) (so_list &so, target_section *);
+  void (*relocate_section_addresses) (so &so, target_section *);
 
   /* Reset private data structures associated with SO.
      This is called when SO is about to be reloaded.
      It is also called when SO is about to be freed.  */
-  void (*clear_so) (const so_list &so);
+  void (*clear_so) (const so &so);
 
   /* Free private data structures associated to PSPACE.  This method
      should not free resources associated to individual so_list entries,
@@ -119,9 +119,9 @@ struct target_so_ops
 
      Note that we only gather information directly available from the
      inferior --- we don't examine any of the shared library files
-     themselves.  The declaration of `struct so_list' says which fields
+     themselves.  The declaration of `struct so' says which fields
      we provide values for.  */
-  intrusive_list<so_list> (*current_sos) ();
+  intrusive_list<so> (*current_sos) ();
 
   /* Find, open, and read the symbols for the main executable.  If
      FROM_TTY is non-zero, allow messages to be printed.  */
@@ -147,7 +147,7 @@ struct target_so_ops
      if they represent the same library.
      Falls back to using strcmp on so_original_name field when set
      to NULL.  */
-  int (*same) (const so_list &gdb, const so_list &inferior);
+  int (*same) (const so &gdb, const so &inferior);
 
   /* Return whether a region of memory must be kept in a core file
      for shared libraries loaded before "gcore" is used to be
@@ -172,7 +172,7 @@ struct target_so_ops
 };
 
 /* A unique pointer to a so_list.  */
-using so_list_up = std::unique_ptr<so_list>;
+using so_up = std::unique_ptr<so>;
 
 /* Find main executable binary file.  */
 extern gdb::unique_xmalloc_ptr<char> exec_file_find (const char *in_pathname,
-- 
2.42.0


^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 03/24] gdb: make interps_notify work with references
  2023-10-10 20:39 ` [PATCH 03/24] gdb: make interps_notify work with references Simon Marchi
@ 2023-10-11  8:48   ` Lancelot SIX
  2023-10-11 14:18     ` Simon Marchi
  0 siblings, 1 reply; 56+ messages in thread
From: Lancelot SIX @ 2023-10-11  8:48 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches, Simon Marchi

Hi Simon,

Just a nit below.

On Tue, Oct 10, 2023 at 04:39:58PM -0400, Simon Marchi wrote:
> From: Simon Marchi <simon.marchi@polymtl.ca>
> 
> A subsequent patch changes the interp::on_solib_loaded and
> interp::on_solib_unloaded methods to take references.  This highlighted
> that interps_notify did not work with reference parameters.
> 
> Fix that by changing interps_notify's `args` arg to be a universal
> reference (&&).  Change the type of the method to be auto-deduced as an
> additional template parameter, otherwise the signature of the callback
> function would never match:
> 
>       CXX    interps.o
>     /home/simark/src/binutils-gdb/gdb/interps.c: In function ‘void interps_notify_signal_received(gdb_signal)’:
>     /home/simark/src/binutils-gdb/gdb/interps.c:378:18: error: no matching function for call to ‘interps_notify(void (interp::*)(gdb_signal), gdb_signal&)’
>       378 |   interps_notify (&interp::on_signal_received, sig);
>           |   ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>     /home/simark/src/binutils-gdb/gdb/interps.c:363:1: note: candidate: ‘template<class ... Args> void interps_notify(void (interp::*)(Args ...), Args&& ...)’
>       363 | interps_notify (void (interp::*method) (Args...), Args&&... args)
>           | ^~~~~~~~~~~~~~
>     /home/simark/src/binutils-gdb/gdb/interps.c:363:1: note:   template argument deduction/substitution failed:
>     /home/simark/src/binutils-gdb/gdb/interps.c:378:18: note:   inconsistent parameter pack deduction with ‘gdb_signal’ and ‘gdb_signal&’
>       378 |   interps_notify (&interp::on_signal_received, sig);
>           |   ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> Change-Id: I0cd9378e24ef039f30f8e14f054f8d7fb539c838
> ---
>  gdb/interps.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/gdb/interps.c b/gdb/interps.c
> index f91b2b62c1ba..62a30583e8c0 100644
> --- a/gdb/interps.c
> +++ b/gdb/interps.c
> @@ -358,15 +358,15 @@ current_interpreter (void)
>  /* Helper interps_notify_* functions.  Call METHOD on the top-level interpreter
>     of all UIs.  */
>  
> -template <typename ...Args>
> +template <typename MethodType, typename ...Args>
>  void
> -interps_notify (void (interp::*method) (Args...), Args... args)
> +interps_notify (MethodType method, Args&&... args)
>  {
>    SWITCH_THRU_ALL_UIS ()
>      {
>        interp *tli = top_level_interpreter ();
>        if (tli != nullptr)
> -	(tli->*method) (args...);
> +	(tli->*method) (std::forward<Args>(args)...);
                                          ^

Space missing before the "(".

Best,
Lancelot.

>      }
>  }
>  
> -- 
> 2.42.0
> 

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 03/24] gdb: make interps_notify work with references
  2023-10-11  8:48   ` Lancelot SIX
@ 2023-10-11 14:18     ` Simon Marchi
  0 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-11 14:18 UTC (permalink / raw)
  To: Lancelot SIX; +Cc: gdb-patches, Simon Marchi

On 10/11/23 04:48, Lancelot SIX wrote:
>> diff --git a/gdb/interps.c b/gdb/interps.c
>> index f91b2b62c1ba..62a30583e8c0 100644
>> --- a/gdb/interps.c
>> +++ b/gdb/interps.c
>> @@ -358,15 +358,15 @@ current_interpreter (void)
>>  /* Helper interps_notify_* functions.  Call METHOD on the top-level interpreter
>>     of all UIs.  */
>>  
>> -template <typename ...Args>
>> +template <typename MethodType, typename ...Args>
>>  void
>> -interps_notify (void (interp::*method) (Args...), Args... args)
>> +interps_notify (MethodType method, Args&&... args)
>>  {
>>    SWITCH_THRU_ALL_UIS ()
>>      {
>>        interp *tli = top_level_interpreter ();
>>        if (tli != nullptr)
>> -	(tli->*method) (args...);
>> +	(tli->*method) (std::forward<Args>(args)...);
>                                           ^
> 
> Space missing before the "(".
> 
> Best,
> Lancelot.

Thanks, fixed locally.

Simon

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 06/24] gdbsupport: make intrusive_list's disposer accept a reference
  2023-10-10 20:40 ` [PATCH 06/24] gdbsupport: make intrusive_list's disposer accept a reference Simon Marchi
@ 2023-10-12 19:05   ` Pedro Alves
  2023-10-14 20:12     ` Simon Marchi
  0 siblings, 1 reply; 56+ messages in thread
From: Pedro Alves @ 2023-10-12 19:05 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 2023-10-10 21:40, Simon Marchi wrote:
> All the API of intrusive_list works with references to item, I'm not
> sure why the disposer used in intrusive_list::clear_and_dispose is
> different and receives a pointer.  Change it to accept a reference
> instead.  This helps simplify a bit a subsequent patch, and I don't see
> any downside to it.

The whole intrusive_list API was copied from Boost intrusive_list (not
the implementation), so that's where that came from.  

I assume that the Disposer interface takes a pointer so that it has the same
interface as a unique_ptr Deleter, for instance.  From a pure disposer/deleter angle,
it seems a bit odd to me to have a "delete" operator wrapper take a reference
instead of a pointer, leading to code like "delete &ref", FWIW.

If this doesn't result in a great tangible improvement, I'd prefer keeping
the interface in sync with Boost's.

Pedro Alves

> 
> Change-Id: I380886cd4ddaf136fe532eb2744f9e69beb41283
> ---
>  gdb/inferior.c                           | 10 +++++-----
>  gdb/unittests/intrusive_list-selftests.c |  4 ++--
>  gdbsupport/intrusive_list.h              |  2 +-
>  3 files changed, 8 insertions(+), 8 deletions(-)
> 
> diff --git a/gdb/inferior.c b/gdb/inferior.c
> index efe57cceae3e..4a55970ccba1 100644
> --- a/gdb/inferior.c
> +++ b/gdb/inferior.c
> @@ -262,13 +262,13 @@ inferior::find_thread (ptid_t ptid)
>  void
>  inferior::clear_thread_list ()
>  {
> -  thread_list.clear_and_dispose ([=] (thread_info *thr)
> +  thread_list.clear_and_dispose ([=] (thread_info &thr)
>      {
>        threads_debug_printf ("deleting thread %s",
> -			    thr->ptid.to_string ().c_str ());
> -      set_thread_exited (thr, {}, true /* silent */);
> -      if (thr->deletable ())
> -	delete thr;
> +			    thr.ptid.to_string ().c_str ());
> +      set_thread_exited (&thr, {}, true /* silent */);
> +      if (thr.deletable ())
> +	delete &thr;
>      });
>    ptid_thread_map.clear ();
>  }
> diff --git a/gdb/unittests/intrusive_list-selftests.c b/gdb/unittests/intrusive_list-selftests.c
> index 1f85b3266830..d990c0878b35 100644
> --- a/gdb/unittests/intrusive_list-selftests.c
> +++ b/gdb/unittests/intrusive_list-selftests.c
> @@ -692,9 +692,9 @@ struct intrusive_list_test
>      list.push_back (b);
>      list.push_back (c);
>  
> -    auto disposer = [&] (const item_type *item)
> +    auto disposer = [&] (const item_type &item)
>        {
> -	disposer_seen.insert (item);
> +	disposer_seen.insert (&item);
>  	disposer_calls++;
>        };
>      list.clear_and_dispose (disposer);
> diff --git a/gdbsupport/intrusive_list.h b/gdbsupport/intrusive_list.h
> index 5e9243867d27..aea1922cabee 100644
> --- a/gdbsupport/intrusive_list.h
> +++ b/gdbsupport/intrusive_list.h
> @@ -513,7 +513,7 @@ class intrusive_list
>    {
>      while (!this->empty ())
>        {
> -	pointer p = &front ();
> +	reference p = front ();
>  	pop_front ();
>  	disposer (p);
>        }


^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 12/24] gdb: make solib-svr4 not use so_list internally
  2023-10-10 20:40 ` [PATCH 12/24] gdb: make solib-svr4 not use so_list internally Simon Marchi
@ 2023-10-13 17:52   ` Lancelot SIX
  2023-10-14 19:59     ` Simon Marchi
  2023-10-19 11:08   ` Lancelot SIX
  1 sibling, 1 reply; 56+ messages in thread
From: Lancelot SIX @ 2023-10-13 17:52 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

Hi Simon,

When applying this patch, I have:

Applying: gdb: make solib-svr4 not use so_list internally
.git/rebase-apply/patch:182: trailing whitespace.
               sizeof (newobj->so_name) - 1);                                                                                                                
.git/rebase-apply/patch:183: trailing whitespace.
      newobj->so_name[sizeof (newobj->so_name) - 1] = 0;                                                                                                                            
.git/rebase-apply/patch:184: trailing whitespace.
      strcpy (newobj->so_original_name, newobj->so_name);    
warning: 3 lines add whitespace errors.

I only skimmed through the patch, I have not payed detailed attention to
details so far.

Best,
Lancelot.

On Tue, Oct 10, 2023 at 04:40:07PM -0400, Simon Marchi wrote:
> A subsequent patch makes use of non-trivial types in struct so_list.
> This trips on the fact that svr4_copy_library_list uses memcpy to copy
> so_list objects:
> 
>       so_list *newobj = new so_list;
>       memcpy (newobj, src, sizeof (struct so_list));
> 
> solib-svr4 maintains lists of so_list objects in its own internal data
> structures.  When requested to return a list of so_list objects (through
> target_so_ops::current_sos), it duplicates the internal so_list lists,
> using memcpy.  When changing so_list to make it non-trivial, we would
> need to replace this use of memcpy somehow.  That would mean making
> so_list copyable, with all the complexity that entails, just to satisfy
> this internal usage of solib-svr4 (and solib-rocm, which does the same).
> 
> Change solib-svr4 to use its own data type for its internal lists.  The
> use of so_list is a bit overkill anyway, as most fields of so_list are
> irrelevant for this internal use.
> 
>  - Introduce svr4_so, which contains just an std::string for the name
>    and a unique_ptr for the lm_info.
>  - Change the internal so_list lists to be std::vector<svr4_so>.  Vector
>    seems like a good choice for this, we don't need to insert/remove
>    elements in the middle of these internal lists.
>  - Remove svr4_free_library_list, free_solib_lists and ~svr4_info, as
>    everything is managed automatically now.
>  - Replace svr4_copy_library_list (which duplicated internal lists in
>    order to return them to the core) with so_list_from_svr4_sos, which
>    creates an so_list list from a vector of svr4_so.
>  - Generalize svr4_same a bit, because find_debug_base_for_solib now
>    needs to compare an so_list and an svr4_so to see if they are the
>    same.
> 
> Change-Id: I6012e48e07aace2a8172b74b389f9547ce777877
> ---
>  gdb/solib-svr4.c | 275 +++++++++++++++++------------------------------
>  gdb/solib-svr4.h |   2 +
>  2 files changed, 100 insertions(+), 177 deletions(-)
> 
> diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
> index 7d247fa455e9..9266929d2895 100644
> --- a/gdb/solib-svr4.c
> +++ b/gdb/solib-svr4.c
> @@ -50,7 +50,6 @@
>  static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
>  static int svr4_have_link_map_offsets (void);
>  static void svr4_relocate_main_executable (void);
> -static void svr4_free_library_list (so_list *solist);
>  static void probes_table_remove_objfile_probes (struct objfile *objfile);
>  static void svr4_iterate_over_objfiles_in_search_order
>    (gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype cb,
> @@ -172,26 +171,35 @@ svr4_same_1 (const char *gdb_so_name, const char *inferior_so_name)
>    return 0;
>  }
>  
> -static int
> -svr4_same (const so_list &gdb, const so_list &inferior)
> +static bool
> +svr4_same (const char *gdb_name, const char *inferior_name,
> +	   const lm_info_svr4 &gdb_lm_info,
> +	   const lm_info_svr4 &inferior_lm_info)
>  {
> -  if (!svr4_same_1 (gdb.so_original_name, inferior.so_original_name))
> +  if (!svr4_same_1 (gdb_name, inferior_name))
>      return false;
>  
>    /* There may be different instances of the same library, in different
>       namespaces.  Each instance, however, must have been loaded at a
>       different address so its relocation offset would be different.  */
> +  return gdb_lm_info.l_addr_inferior == inferior_lm_info.l_addr_inferior;
> +}
> +
> +static int
> +svr4_same (const so_list &gdb, const so_list &inferior)
> +{
>    auto *lmg = gdb::checked_static_cast<const lm_info_svr4 *> (gdb.lm_info);
>    auto *lmi = gdb::checked_static_cast<const lm_info_svr4 *> (inferior.lm_info);
>  
> -  return (lmg->l_addr_inferior == lmi->l_addr_inferior);
> +  return svr4_same (gdb.so_original_name, inferior.so_original_name,
> +		    *lmg, *lmi);
>  }
>  
> -static std::unique_ptr<lm_info_svr4>
> +static lm_info_svr4_up
>  lm_info_read (CORE_ADDR lm_addr)
>  {
>    struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
> -  std::unique_ptr<lm_info_svr4> lm_info;
> +  lm_info_svr4_up lm_info;
>  
>    gdb::byte_vector lm (lmo->link_map_size);
>  
> @@ -203,7 +211,7 @@ lm_info_read (CORE_ADDR lm_addr)
>        type *ptr_type
>  	= builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
>  
> -      lm_info.reset (new lm_info_svr4);
> +      lm_info = gdb::make_unique<lm_info_svr4> ();
>        lm_info->lm_addr = lm_addr;
>  
>        lm_info->l_addr_inferior = extract_typed_address (&lm[lmo->l_addr_offset],
> @@ -333,13 +341,20 @@ lm_addr_check (const so_list &so, bfd *abfd)
>    return li->l_addr;
>  }
>  
> +struct svr4_so
> +{
> +  svr4_so (const char *name, lm_info_svr4_up lm_info)
> +    : name (name), lm_info (std::move (lm_info))
> +  {}
> +
> +  std::string name;
> +  lm_info_svr4_up lm_info;
> +};
> +
>  /* Per pspace SVR4 specific data.  */
>  
>  struct svr4_info
>  {
> -  svr4_info () = default;
> -  ~svr4_info ();
> -
>    /* Base of dynamic linker structures in default namespace.  */
>    CORE_ADDR debug_base = 0;
>  
> @@ -385,7 +400,7 @@ struct svr4_info
>  
>       The special entry zero is reserved for a linear list to support
>       gdbstubs that do not support namespaces.  */
> -  std::map<CORE_ADDR, so_list *> solib_lists;
> +  std::map<CORE_ADDR, std::vector<svr4_so>> solib_lists;
>  };
>  
>  /* Per-program-space data key.  */
> @@ -407,23 +422,6 @@ free_probes_table (struct svr4_info *info)
>    info->probes_table.reset (nullptr);
>  }
>  
> -/* Free the solib lists for all namespaces.  */
> -
> -static void
> -free_solib_lists (svr4_info *info)
> -{
> -  for (const std::pair<CORE_ADDR, so_list *> tuple
> -	 : info->solib_lists)
> -    svr4_free_library_list (tuple.second);
> -
> -  info->solib_lists.clear ();
> -}
> -
> -svr4_info::~svr4_info ()
> -{
> -  free_solib_lists (this);
> -}
> -
>  /* Get the svr4 data for program space PSPACE.  If none is found yet, add it now.
>     This function always returns a valid object.  */
>  
> @@ -955,7 +953,7 @@ struct svr4_library_list
>  {
>    /* The tail pointer of the current namespace.  This is internal to XML
>       parsing.  */
> -  so_list **tailp;
> +  std::vector<svr4_so> *cur_list;
>  
>    /* Inferior address of struct link_map used for the main executable.  It is
>       NULL if not known.  */
> @@ -965,7 +963,7 @@ struct svr4_library_list
>       not include any default sos.
>  
>       See comment on struct svr4_info.solib_lists.  */
> -  std::map<CORE_ADDR, so_list *> solib_lists;
> +  std::map<CORE_ADDR, std::vector<svr4_so>> solib_lists;
>  };
>  
>  /* This module's 'free_objfile' observer.  */
> @@ -987,41 +985,27 @@ svr4_clear_so (const so_list &so)
>      li->l_addr_p = 0;
>  }
>  
> -/* Free so_list built so far.  */
> -
> -static void
> -svr4_free_library_list (so_list *list)
> -{
> -  while (list != NULL)
> -    {
> -      struct so_list *next = list->next;
> +/* Create the so_list objects equivalent to the svr4_sos in SOS.  */
>  
> -      free_so (*list);
> -      list = next;
> -    }
> -}
> -
> -/* Copy library list.  */
> -
> -static struct so_list *
> -svr4_copy_library_list (struct so_list *src)
> +static so_list *
> +so_list_from_svr4_sos (const std::vector<svr4_so> &sos)
>  {
>    struct so_list *dst = NULL;
>    struct so_list **link = &dst;
>  
> -  while (src != NULL)
> +  for (const svr4_so &so : sos)
>      {
>        so_list *newobj = new so_list;
> -      memcpy (newobj, src, sizeof (struct so_list));
>  
> -      auto *src_li = gdb::checked_static_cast<lm_info_svr4 *> (src->lm_info);
> -      newobj->lm_info = new lm_info_svr4 (*src_li);
> +      strncpy (newobj->so_name, so.name.c_str (),
> +	       sizeof (newobj->so_name) - 1);                                                                                                                
> +      newobj->so_name[sizeof (newobj->so_name) - 1] = 0;                                                                                                                            
> +      strcpy (newobj->so_original_name, newobj->so_name);    
> +      newobj->lm_info = new lm_info_svr4 (*so.lm_info);
>  
>        newobj->next = NULL;
>        *link = newobj;
>        link = &newobj->next;
> -
> -      src = src->next;
>      }
>  
>    return dst;
> @@ -1050,48 +1034,25 @@ library_list_start_library (struct gdb_xml_parser *parser,
>    ULONGEST *l_ldp
>      = (ULONGEST *) xml_find_attribute (attributes, "l_ld")->value.get ();
>  
> -  so_list *new_elem = new so_list;
> -  lm_info_svr4 *li = new lm_info_svr4;
> -  new_elem->lm_info = li;
> +  lm_info_svr4_up li = gdb::make_unique<lm_info_svr4> ();
>    li->lm_addr = *lmp;
>    li->l_addr_inferior = *l_addrp;
>    li->l_ld = *l_ldp;
>  
> -  strncpy (new_elem->so_name, name, sizeof (new_elem->so_name) - 1);
> -  new_elem->so_name[sizeof (new_elem->so_name) - 1] = 0;
> -  strcpy (new_elem->so_original_name, new_elem->so_name);
> +  std::vector<svr4_so> *solist;
>  
>    /* Older versions did not supply lmid.  Put the element into the flat
>       list of the special namespace zero in that case.  */
>    gdb_xml_value *at_lmid = xml_find_attribute (attributes, "lmid");
>    if (at_lmid == nullptr)
> -    {
> -      *list->tailp = new_elem;
> -      list->tailp = &new_elem->next;
> -    }
> +    solist = list->cur_list;
>    else
>      {
>        ULONGEST lmid = *(ULONGEST *) at_lmid->value.get ();
> -
> -      /* Ensure that the element is actually initialized.  */
> -      if (list->solib_lists.find (lmid) == list->solib_lists.end ())
> -	list->solib_lists[lmid] = nullptr;
> -
> -      so_list **psolist = &list->solib_lists[lmid];
> -      so_list **pnext = psolist;
> -
> -      /* Walk to the end of the list if we have one.  */
> -      so_list *solist = *psolist;
> -      if (solist != nullptr)
> -	{
> -	  for (; solist->next != nullptr; solist = solist->next)
> -	    /* Nothing.  */;
> -
> -	  pnext = &solist->next;
> -	}
> -
> -      *pnext = new_elem;
> +      solist = &list->solib_lists[lmid];
>      }
> +
> +  solist->emplace_back (name, std::move (li));
>  }
>  
>  /* Handle the start of a <library-list-svr4> element.  */
> @@ -1117,9 +1078,7 @@ svr4_library_list_start_list (struct gdb_xml_parser *parser,
>  
>    /* Older gdbserver do not support namespaces.  We use the special
>       namespace zero for a linear list of libraries.  */
> -  so_list **solist = &list->solib_lists[0];
> -  *solist = nullptr;
> -  list->tailp = solist;
> +  list->cur_list = &list->solib_lists[0];
>  }
>  
>  /* The allowed elements and attributes for an XML library list.
> @@ -1169,13 +1128,9 @@ static int
>  svr4_parse_libraries (const char *document, struct svr4_library_list *list)
>  {
>    auto cleanup = make_scope_exit ([list] ()
> -    {
> -      for (const std::pair<CORE_ADDR, so_list *> tuple
> -	     : list->solib_lists)
> -	svr4_free_library_list (tuple.second);
> -    });
> +    {  list->solib_lists.clear (); });
>  
> -  list->tailp = nullptr;
> +  list->cur_list = nullptr;
>    list->main_lm = 0;
>    list->solib_lists.clear ();
>    if (gdb_xml_parse_quick (_("target library list"), "library-list-svr4.dtd",
> @@ -1253,25 +1208,21 @@ svr4_default_sos (svr4_info *info)
>  
>  /* Read the whole inferior libraries chain starting at address LM.
>     Expect the first entry in the chain's previous entry to be PREV_LM.
> -   Add the entries to the tail referenced by LINK_PTR_PTR.  Ignore the
> -   first entry if IGNORE_FIRST and set global MAIN_LM_ADDR according
> -   to it.  Returns nonzero upon success.  If zero is returned the
> -   entries stored to LINK_PTR_PTR are still valid although they may
> +   Add the entries to SOS.  Ignore the first entry if IGNORE_FIRST and set
> +   global MAIN_LM_ADDR according to it.  Returns nonzero upon success.  If zero
> +   is returned the entries stored to LINK_PTR_PTR are still valid although they may
>     represent only part of the inferior library list.  */
>  
>  static int
>  svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
> -		   struct so_list ***link_ptr_ptr, int ignore_first)
> +		   std::vector<svr4_so> &sos, int ignore_first)
>  {
>    CORE_ADDR first_l_name = 0;
>    CORE_ADDR next_lm;
>  
>    for (; lm != 0; prev_lm = lm, lm = next_lm)
>      {
> -      so_list_up newobj (new so_list);
> -
> -      lm_info_svr4 *li = lm_info_read (lm).release ();
> -      newobj->lm_info = li;
> +      lm_info_svr4_up li = lm_info_read (lm);
>        if (li == NULL)
>  	return 0;
>  
> @@ -1298,9 +1249,9 @@ svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
>  	}
>  
>        /* Extract this shared object's name.  */
> -      gdb::unique_xmalloc_ptr<char> buffer
> +      gdb::unique_xmalloc_ptr<char> name
>  	= target_read_string (li->l_name, SO_NAME_MAX_PATH_SIZE - 1);
> -      if (buffer == nullptr)
> +      if (name == nullptr)
>  	{
>  	  /* If this entry's l_name address matches that of the
>  	     inferior executable, then this is not a normal shared
> @@ -1311,19 +1262,12 @@ svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
>  	  continue;
>  	}
>  
> -      strncpy (newobj->so_name, buffer.get (), SO_NAME_MAX_PATH_SIZE - 1);
> -      newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
> -      strcpy (newobj->so_original_name, newobj->so_name);
> -
>        /* If this entry has no name, or its name matches the name
>  	 for the main executable, don't include it in the list.  */
> -      if (! newobj->so_name[0] || match_main (newobj->so_name))
> +      if (*name == '\0' || match_main (name.get ()))
>  	continue;
>  
> -      newobj->next = 0;
> -      /* Don't free it now.  */
> -      **link_ptr_ptr = newobj.release ();
> -      *link_ptr_ptr = &(**link_ptr_ptr)->next;
> +      sos.emplace_back (name.get (), std::move (li));
>      }
>  
>    return 1;
> @@ -1342,7 +1286,7 @@ svr4_current_sos_direct (struct svr4_info *info)
>    struct svr4_library_list library_list;
>  
>    /* Remove any old libraries.  We're going to read them back in again.  */
> -  free_solib_lists (info);
> +  info->solib_lists.clear ();
>  
>    /* Fall back to manual examination of the target if the packet is not
>       supported or gdbserver failed to find DT_DEBUG.  gdb.server/solib-list.exp
> @@ -1362,10 +1306,10 @@ svr4_current_sos_direct (struct svr4_info *info)
>        /* Remove an empty special zero namespace so we know that when there
>  	 is one, it is actually used, and we have a flat list without
>  	 namespace information.  */
> -      if ((library_list.solib_lists.find (0)
> -	   != library_list.solib_lists.end ())
> -	  && (library_list.solib_lists[0] == nullptr))
> -	library_list.solib_lists.erase (0);
> +      auto it_0 = library_list.solib_lists.find (0);
> +      if (it_0 != library_list.solib_lists.end ()
> +	  && it_0->second.empty ())
> +	library_list.solib_lists.erase (it_0);
>  
>        /* Replace the (empty) solib_lists in INFO with the one generated
>  	 from the target.  We don't want to copy it on assignment and then
> @@ -1391,9 +1335,7 @@ svr4_current_sos_direct (struct svr4_info *info)
>      ignore_first = true;
>  
>    auto cleanup = make_scope_exit ([info] ()
> -    {
> -      free_solib_lists (info);
> -    });
> +    { info->solib_lists.clear (); });
>  
>    /* Collect the sos in each namespace.  */
>    CORE_ADDR debug_base = info->debug_base;
> @@ -1403,12 +1345,8 @@ svr4_current_sos_direct (struct svr4_info *info)
>        /* Walk the inferior's link map list, and build our so_list list.  */
>        lm = solib_svr4_r_map (debug_base);
>        if (lm != 0)
> -	{
> -	  so_list **sos = &info->solib_lists[debug_base];
> -	  *sos = nullptr;
> -
> -	  svr4_read_so_list (info, lm, 0, &sos, ignore_first);
> -	}
> +	svr4_read_so_list (info, lm, 0, info->solib_lists[debug_base],
> +			   ignore_first);
>      }
>  
>    /* On Solaris, the dynamic linker is not in the normal list of
> @@ -1425,11 +1363,8 @@ svr4_current_sos_direct (struct svr4_info *info)
>      {
>        /* Add the dynamic linker's namespace unless we already did.  */
>        if (info->solib_lists.find (debug_base) == info->solib_lists.end ())
> -	{
> -	  so_list **sos = &info->solib_lists[debug_base];
> -	  *sos = nullptr;
> -	  svr4_read_so_list (info, debug_base, 0, &sos, 0);
> -	}
> +	svr4_read_so_list (info, debug_base, 0, info->solib_lists[debug_base],
> +			   0);
>      }
>  
>    cleanup.release ();
> @@ -1440,19 +1375,18 @@ svr4_current_sos_direct (struct svr4_info *info)
>  static so_list *
>  svr4_collect_probes_sos (svr4_info *info)
>  {
> -  so_list *sos = nullptr;
> -  so_list **pnext = &sos;
> +  so_list *res = nullptr;
> +  so_list **pnext = &res;
>  
> -  for (const std::pair<CORE_ADDR, so_list *> tuple
> -	 : info->solib_lists)
> +  for (const auto &tuple : info->solib_lists)
>      {
> -      so_list *solist = tuple.second;
> +      const std::vector<svr4_so> &sos = tuple.second;
>  
>        /* Allow the linker to report empty namespaces.  */
> -      if (solist == nullptr)
> +      if (sos.empty ())
>  	continue;
>  
> -      *pnext = svr4_copy_library_list (solist);
> +      *pnext = so_list_from_svr4_sos (sos);
>  
>        /* Update PNEXT to point to the next member of the last element.  */
>        gdb_assert (*pnext != nullptr);
> @@ -1466,7 +1400,7 @@ svr4_collect_probes_sos (svr4_info *info)
>  	}
>      }
>  
> -  return sos;
> +  return res;
>  }
>  
>  /* Implement the main part of the "current_sos" target_so_ops
> @@ -1853,16 +1787,10 @@ solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
>    if (info->solib_lists.find (0) != info->solib_lists.end ())
>      return 0;
>  
> -  /* Ensure that the element is actually initialized.  */
> -  if (info->solib_lists.find (debug_base) == info->solib_lists.end ())
> -    info->solib_lists[debug_base] = nullptr;
> -
> -  so_list **psolist = &info->solib_lists[debug_base];
> -  so_list **pnext = nullptr;
> -  so_list *solist = *psolist;
> +  std::vector<svr4_so> &solist = info->solib_lists[debug_base];
>    CORE_ADDR prev_lm;
>  
> -  if (solist == nullptr)
> +  if (solist.empty ())
>      {
>        /* svr4_current_sos_direct contains logic to handle a number of
>  	 special cases relating to the first elements of the list in
> @@ -1872,18 +1800,9 @@ solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
>  	return 0;
>  
>        prev_lm = 0;
> -      pnext = psolist;
>      }
>    else
> -    {
> -      /* Walk to the end of the list.  */
> -      for (; solist->next != nullptr; solist = solist->next)
> -	/* Nothing.  */;
> -
> -      auto *li = gdb::checked_static_cast<lm_info_svr4 *> (solist->lm_info);
> -      prev_lm = li->lm_addr;
> -      pnext = &solist->next;
> -    }
> +    prev_lm = solist.back ().lm_info->lm_addr;
>  
>    /* Read the new objects.  */
>    if (info->using_xfer)
> @@ -1905,24 +1824,23 @@ solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
>  
>  	 We expect gdbserver to provide updates for the namespace that
>  	 contains LM, which would be this namespace...  */
> -      so_list *sos = nullptr;
> -      if (library_list.solib_lists.find (debug_base)
> -	  != library_list.solib_lists.end ())
> -	std::swap (sos, library_list.solib_lists[debug_base]);
> -      if (sos == nullptr)
> +      std::vector<svr4_so> sos;
> +      auto it_debug_base = library_list.solib_lists.find (debug_base);
> +      if (it_debug_base != library_list.solib_lists.end ())
> +	std::swap (sos, it_debug_base->second);
> +      else
>  	{
>  	  /* ...or for the special zero namespace for earlier versions...  */
> -	  if (library_list.solib_lists.find (0)
> -	      != library_list.solib_lists.end ())
> -	    std::swap (sos, library_list.solib_lists[0]);
> +	  auto it_0 = library_list.solib_lists.find (0);
> +	  if (it_0 != library_list.solib_lists.end ())
> +	    std::swap (sos, it_0->second);
>  	}
>  
>        /* ...but nothing else.  */
> -      for (const std::pair<CORE_ADDR, so_list *> tuple
> -	     : library_list.solib_lists)
> -	gdb_assert (tuple.second == nullptr);
> +      for (const auto &tuple : library_list.solib_lists)
> +	gdb_assert (tuple.second.empty ());
>  
> -      *pnext = sos;
> +      std::move (sos.begin (), sos.end (), std::back_inserter (solist));
>      }
>    else
>      {
> @@ -1930,7 +1848,7 @@ solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
>  	 above check and deferral to solist_update_full ensures
>  	 that this call to svr4_read_so_list will never see the
>  	 first element.  */
> -      if (!svr4_read_so_list (info, lm, prev_lm, &pnext, 0))
> +      if (!svr4_read_so_list (info, lm, prev_lm, solist, 0))
>  	return 0;
>      }
>  
> @@ -1948,7 +1866,7 @@ disable_probes_interface (svr4_info *info)
>  	     "Reverting to original interface."));
>  
>    free_probes_table (info);
> -  free_solib_lists (info);
> +  info->solib_lists.clear ();
>  }
>  
>  /* Update the solib list as appropriate when using the
> @@ -3145,7 +3063,7 @@ svr4_solib_create_inferior_hook (int from_tty)
>  
>    /* Clear the probes-based interface's state.  */
>    free_probes_table (info);
> -  free_solib_lists (info);
> +  info->solib_lists.clear ();
>  
>    /* Relocate the main executable if necessary.  */
>    svr4_relocate_main_executable ();
> @@ -3373,14 +3291,17 @@ find_debug_base_for_solib (so_list *solib)
>  
>    svr4_info *info = get_svr4_info (current_program_space);
>    gdb_assert (info != nullptr);
> -  for (const std::pair<CORE_ADDR, so_list *> tuple
> -	 : info->solib_lists)
> +
> +  const lm_info_svr4 *lm_info = (const lm_info_svr4 *) solib->lm_info;
> +
> +  for (const auto &tuple : info->solib_lists)
>      {
>        CORE_ADDR debug_base = tuple.first;
> -      so_list *solist = tuple.second;
> +      const std::vector<svr4_so> &sos = tuple.second;
>  
> -      for (; solist != nullptr; solist = solist->next)
> -	if (svr4_same (*solib, *solist))
> +      for (const svr4_so &so : sos)
> +	if (svr4_same (solib->so_original_name, so.name.c_str (),
> +		       *lm_info, *so.lm_info))
>  	  return debug_base;
>      }
>  
> diff --git a/gdb/solib-svr4.h b/gdb/solib-svr4.h
> index 1aff3b59da8c..7cb6cabb109f 100644
> --- a/gdb/solib-svr4.h
> +++ b/gdb/solib-svr4.h
> @@ -48,6 +48,8 @@ struct lm_info_svr4 final : public lm_info
>    CORE_ADDR l_ld = 0, l_next = 0, l_prev = 0, l_name = 0;
>  };
>  
> +using lm_info_svr4_up = std::unique_ptr<lm_info_svr4>;
> +
>  /* Critical offsets and sizes which describe struct r_debug and
>     struct link_map on SVR4-like targets.  All offsets and sizes are
>     in bytes unless otherwise specified.  */
> -- 
> 2.42.0
> 

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 13/24] gdb: make solib-rocm not use so_list internally
  2023-10-10 20:40 ` [PATCH 13/24] gdb: make solib-rocm " Simon Marchi
@ 2023-10-13 18:35   ` Lancelot SIX
  2023-10-14 20:00     ` Simon Marchi
  2023-10-17 15:23   ` Pedro Alves
  1 sibling, 1 reply; 56+ messages in thread
From: Lancelot SIX @ 2023-10-13 18:35 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

Hi Simon,

When applying this patch, I have:

    Applying: gdb: make solib-rocm not use so_list internally
    .git/rebase-apply/patch:171: indent with spaces.
            = string_printf ("code_object_%ld", code_object_list[i].handle);
    warning: 1 line adds whitespace errors.


On Tue, Oct 10, 2023 at 04:40:08PM -0400, Simon Marchi wrote:
> Same rationale as the previous patch, but for solib-rocm.
> 
>  - Introduce rocm_so, which is a name a unique_name (see comment in
>    rocm_update_solib_list for that) and a unique_ptr to the
>    lm_info_svr4.
>  - Change the internal lists from so_list lists to vectors of rocm_so.
>  - Remove rocm_free_solib_list, as everything is automatic now.
>  - Replace rocm_solib_copy_list with so_list_from_rocm_sos.
> 
> Change-Id: I71e06e3ea22d6420c9e4e500501c06e9a13398a8
> ---
>  gdb/solib-rocm.c | 92 ++++++++++++++++++++++--------------------------
>  1 file changed, 42 insertions(+), 50 deletions(-)
> 
> diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
> index 403173ecc998..5016c383a807 100644
> --- a/gdb/solib-rocm.c
> +++ b/gdb/solib-rocm.c
> @@ -243,21 +240,21 @@ rocm_solib_current_sos ()
>    so_list *head = svr4_so_ops.current_sos ();
>  
>    /* Then, the device-side shared library list.  */
> -  so_list *list = get_solib_info (current_inferior ())->solib_list;
> +  std::vector<rocm_so> &dev_sos = get_solib_info (current_inferior ())->solib_list;

There should be a line break (it gets over the line limit), and I think
this dev_sos can be marked const.

Best,
Lancelot.

>  
> -  if (list == nullptr)
> +  if (dev_sos.empty ())
>      return head;
>  
> -  list = rocm_solib_copy_list (list);
> +  so_list *dev_so_list = so_list_from_rocm_sos (dev_sos);
>  
>    if (head == nullptr)
> -    return list;
> +    return dev_so_list;
>  
>    /* Append our libraries to the end of the list.  */
>    so_list *tail;
>    for (tail = head; tail->next; tail = tail->next)
>      /* Nothing.  */;
> -  tail->next = list;
> +  tail->next = dev_so_list;
>  
>    return head;
>  }

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 20/24] gdb: make so_list::{so_original_name, so_name} std::strings
  2023-10-10 20:40 ` [PATCH 20/24] gdb: make so_list::{so_original_name,so_name} std::strings Simon Marchi
@ 2023-10-13 22:28   ` Lancelot SIX
  2023-10-14 20:01     ` Simon Marchi
  2023-10-19 11:08   ` [PATCH 20/24] gdb: make so_list::{so_original_name,so_name} std::strings Lancelot SIX
  1 sibling, 1 reply; 56+ messages in thread
From: Lancelot SIX @ 2023-10-13 22:28 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

Hi Simon,

On Tue, Oct 10, 2023 at 04:40:15PM -0400, Simon Marchi wrote:
> Change these two fields, simplifying memory management and copying.
> 
> Change-Id: If2559284c515721e71e1ef56ada8b64667eebe55
> ---
>  gdb/break-catch-load.c |  2 +-
>  gdb/breakpoint.c       |  2 +-
>  gdb/bsd-uthread.c      |  8 +++----
>  gdb/mi/mi-cmd-file.c   |  2 +-
>  gdb/solib-aix.c        |  8 +++----
>  gdb/solib-darwin.c     |  5 ++---
>  gdb/solib-dsbt.c       |  5 ++---
>  gdb/solib-frv.c        |  6 ++----
>  gdb/solib-rocm.c       |  8 ++-----
>  gdb/solib-svr4.c       | 25 ++++++++++------------
>  gdb/solib-target.c     | 18 ++++++----------
>  gdb/solib.c            | 47 +++++++++++++++++++++++-------------------
>  gdb/solist.h           |  4 ++--
>  13 files changed, 63 insertions(+), 77 deletions(-)
> 
> diff --git a/gdb/mi/mi-cmd-file.c b/gdb/mi/mi-cmd-file.c
> index 822d3df40005..5f63ce4494cb 100644
> --- a/gdb/mi/mi-cmd-file.c
> +++ b/gdb/mi/mi-cmd-file.c
> @@ -167,7 +167,7 @@ mi_cmd_file_list_shared_libraries (const char *command,
>      {
>        if (so->so_name[0] == '\0')

I think this should become "so->so_name.empty ()".

>  	continue;
> -      if (pattern != NULL && !re_exec (so->so_name))
> +      if (pattern != NULL && !re_exec (so->so_name.c_str ()))
>  	continue;
>  
>        ui_out_emit_tuple tuple_emitter (uiout, NULL);
> diff --git a/gdb/solib.c b/gdb/solib.c
> index 6b7f0da0498d..1815b0910208 100644
> --- a/gdb/solib.c
> +++ b/gdb/solib.c
> @@ -1088,7 +1093,7 @@ info_sharedlibrary_command (const char *pattern, int from_tty)
>      {
>        if (so->so_name[0])

Similar here.

>  	{
> -	  if (pattern && ! re_exec (so->so_name))
> +	  if (pattern && ! re_exec (so->so_name.c_str ()))
>  	    continue;
>  	  ++nr_libs;
>  	}
> @@ -1109,7 +1114,7 @@ info_sharedlibrary_command (const char *pattern, int from_tty)
>        {
>  	if (! so->so_name[0])

And here it should be "!so->empty ()".

>  	  continue;
> -	if (pattern && ! re_exec (so->so_name))
> +	if (pattern && ! re_exec (so->so_name.c_str ()))
>  	  continue;
>  
>  	ui_out_emit_tuple tuple_emitter (uiout, "lib");

I might have missed other patterns, I'll need to get back to it with
fresher eyes.

Best,
Lancelot.

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 12/24] gdb: make solib-svr4 not use so_list internally
  2023-10-13 17:52   ` Lancelot SIX
@ 2023-10-14 19:59     ` Simon Marchi
  0 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-14 19:59 UTC (permalink / raw)
  To: Lancelot SIX; +Cc: gdb-patches

On 10/13/23 13:52, Lancelot SIX wrote:
> Hi Simon,
> 
> When applying this patch, I have:
> 
> Applying: gdb: make solib-svr4 not use so_list internally
> .git/rebase-apply/patch:182: trailing whitespace.
>                sizeof (newobj->so_name) - 1);                                                                                                                
> .git/rebase-apply/patch:183: trailing whitespace.
>       newobj->so_name[sizeof (newobj->so_name) - 1] = 0;                                                                                                                            
> .git/rebase-apply/patch:184: trailing whitespace.
>       strcpy (newobj->so_original_name, newobj->so_name);    
> warning: 3 lines add whitespace errors.
> 
> I only skimmed through the patch, I have not payed detailed attention to
> details so far.

Thanks, fixed that locally.

Simon

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 13/24] gdb: make solib-rocm not use so_list internally
  2023-10-13 18:35   ` Lancelot SIX
@ 2023-10-14 20:00     ` Simon Marchi
  0 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-14 20:00 UTC (permalink / raw)
  To: Lancelot SIX; +Cc: gdb-patches

On 10/13/23 14:35, Lancelot SIX wrote:
> Hi Simon,
> 
> When applying this patch, I have:
> 
>     Applying: gdb: make solib-rocm not use so_list internally
>     .git/rebase-apply/patch:171: indent with spaces.
>             = string_printf ("code_object_%ld", code_object_list[i].handle);
>     warning: 1 line adds whitespace errors.

Thanks, fixed that locally.

Simon

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 20/24] gdb: make so_list::{so_original_name, so_name} std::strings
  2023-10-13 22:28   ` [PATCH 20/24] gdb: make so_list::{so_original_name, so_name} std::strings Lancelot SIX
@ 2023-10-14 20:01     ` Simon Marchi
  0 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-14 20:01 UTC (permalink / raw)
  To: Lancelot SIX; +Cc: gdb-patches

On 10/13/23 18:28, Lancelot SIX wrote:
> Hi Simon,
> 
> On Tue, Oct 10, 2023 at 04:40:15PM -0400, Simon Marchi wrote:
>> Change these two fields, simplifying memory management and copying.
>>
>> Change-Id: If2559284c515721e71e1ef56ada8b64667eebe55
>> ---
>>  gdb/break-catch-load.c |  2 +-
>>  gdb/breakpoint.c       |  2 +-
>>  gdb/bsd-uthread.c      |  8 +++----
>>  gdb/mi/mi-cmd-file.c   |  2 +-
>>  gdb/solib-aix.c        |  8 +++----
>>  gdb/solib-darwin.c     |  5 ++---
>>  gdb/solib-dsbt.c       |  5 ++---
>>  gdb/solib-frv.c        |  6 ++----
>>  gdb/solib-rocm.c       |  8 ++-----
>>  gdb/solib-svr4.c       | 25 ++++++++++------------
>>  gdb/solib-target.c     | 18 ++++++----------
>>  gdb/solib.c            | 47 +++++++++++++++++++++++-------------------
>>  gdb/solist.h           |  4 ++--
>>  13 files changed, 63 insertions(+), 77 deletions(-)
>>
>> diff --git a/gdb/mi/mi-cmd-file.c b/gdb/mi/mi-cmd-file.c
>> index 822d3df40005..5f63ce4494cb 100644
>> --- a/gdb/mi/mi-cmd-file.c
>> +++ b/gdb/mi/mi-cmd-file.c
>> @@ -167,7 +167,7 @@ mi_cmd_file_list_shared_libraries (const char *command,
>>      {
>>        if (so->so_name[0] == '\0')
> 
> I think this should become "so->so_name.empty ()".

I believe that it's functionally equivalent, but I agree, I will fix
these up.

Simon

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 06/24] gdbsupport: make intrusive_list's disposer accept a reference
  2023-10-12 19:05   ` Pedro Alves
@ 2023-10-14 20:12     ` Simon Marchi
  0 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-14 20:12 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 10/12/23 15:05, Pedro Alves wrote:
> On 2023-10-10 21:40, Simon Marchi wrote:
>> All the API of intrusive_list works with references to item, I'm not
>> sure why the disposer used in intrusive_list::clear_and_dispose is
>> different and receives a pointer.  Change it to accept a reference
>> instead.  This helps simplify a bit a subsequent patch, and I don't see
>> any downside to it.
> 
> The whole intrusive_list API was copied from Boost intrusive_list (not
> the implementation), so that's where that came from.  
> 
> I assume that the Disposer interface takes a pointer so that it has the same
> interface as a unique_ptr Deleter, for instance.  From a pure disposer/deleter angle,
> it seems a bit odd to me to have a "delete" operator wrapper take a reference
> instead of a pointer, leading to code like "delete &ref", FWIW.
> 
> If this doesn't result in a great tangible improvement, I'd prefer keeping
> the interface in sync with Boost's.

I forgot about that, that's a good reason, I'll drop this patch.

Thanks,

Simon

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 02/24] gdb: add program_space parameter to target_so_ops::clear_solib
  2023-10-10 20:39 ` [PATCH 02/24] gdb: add program_space parameter to target_so_ops::clear_solib Simon Marchi
@ 2023-10-17 14:57   ` Pedro Alves
  2023-10-17 15:19     ` Simon Marchi
  0 siblings, 1 reply; 56+ messages in thread
From: Pedro Alves @ 2023-10-17 14:57 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

Just a tiny nit.

On 2023-10-10 21:39, Simon Marchi wrote:

> diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
> index 98c81bb3bdaa..4796315c373a 100644
> --- a/gdb/solib-darwin.c
> +++ b/gdb/solib-darwin.c
> @@ -78,16 +78,14 @@ static const registry<program_space>::key<darwin_info>
>  /* Get the current darwin data.  If none is found yet, add it now.  This

I guess "current darwin data" -> "darwin data for PSPACE" ?

>     function always returns a valid object.  */
>  
> -static struct darwin_info *
> -get_darwin_info (void)
> +static darwin_info *
> +get_darwin_info (program_space *pspace)
>  {


> --- a/gdb/solib-dsbt.c
> +++ b/gdb/solib-dsbt.c
> @@ -169,16 +169,14 @@ static const registry<program_space>::key<dsbt_info> solib_dsbt_pspace_data;
>  /* Get the current dsbt data.  If none is found yet, add it now.  This

Ditto "current" here.

>     function always returns a valid object.  */
>  
> -static struct dsbt_info *
> -get_dsbt_info (void)
> +static dsbt_info *
> +get_dsbt_info (program_space *pspace)
>  {
> -  struct dsbt_info *info;
> -
> -  info = solib_dsbt_pspace_data.get (current_program_space);
> -  if (info != NULL)
> +  dsbt_info *info = solib_dsbt_pspace_data.get (pspace);
> +  if (info != nullptr)
>      return info;
>  
> -  return solib_dsbt_pspace_data.emplace (current_program_space);
> +  return solib_dsbt_pspace_data.emplace (pspace);
>  }
>  
>  
> @@ -270,9 +268,6 @@ decode_loadmap (const gdb_byte *buf)
>    return int_ldmbuf;
>  }

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 02/24] gdb: add program_space parameter to target_so_ops::clear_solib
  2023-10-17 14:57   ` Pedro Alves
@ 2023-10-17 15:19     ` Simon Marchi
  0 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-17 15:19 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 10/17/23 10:57, Pedro Alves wrote:
> Just a tiny nit.
> 
> On 2023-10-10 21:39, Simon Marchi wrote:
> 
>> diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
>> index 98c81bb3bdaa..4796315c373a 100644
>> --- a/gdb/solib-darwin.c
>> +++ b/gdb/solib-darwin.c
>> @@ -78,16 +78,14 @@ static const registry<program_space>::key<darwin_info>
>>  /* Get the current darwin data.  If none is found yet, add it now.  This
> 
> I guess "current darwin data" -> "darwin data for PSPACE" ?
> 
>>     function always returns a valid object.  */
>>  
>> -static struct darwin_info *
>> -get_darwin_info (void)
>> +static darwin_info *
>> +get_darwin_info (program_space *pspace)
>>  {
> 
> 
>> --- a/gdb/solib-dsbt.c
>> +++ b/gdb/solib-dsbt.c
>> @@ -169,16 +169,14 @@ static const registry<program_space>::key<dsbt_info> solib_dsbt_pspace_data;
>>  /* Get the current dsbt data.  If none is found yet, add it now.  This
> 
> Ditto "current" here.

Thanks, fixed both locally.

Simon

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 13/24] gdb: make solib-rocm not use so_list internally
  2023-10-10 20:40 ` [PATCH 13/24] gdb: make solib-rocm " Simon Marchi
  2023-10-13 18:35   ` Lancelot SIX
@ 2023-10-17 15:23   ` Pedro Alves
  2023-10-17 15:32     ` Simon Marchi
  1 sibling, 1 reply; 56+ messages in thread
From: Pedro Alves @ 2023-10-17 15:23 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 2023-10-10 21:40, Simon Marchi wrote:

> --- a/gdb/solib-rocm.c
> +++ b/gdb/solib-rocm.c
> @@ -126,14 +126,26 @@ rocm_solib_fd_cache::close (int fd, fileio_error *target_errno)
>  
>  /* ROCm-specific inferior data.  */
>  
> +struct rocm_so
> +{
> +  rocm_so (const char *name, std::string unique_name, lm_info_svr4_up lm_info)
> +    : name (name),
> +      unique_name (std::move(unique_name)),

Space after "move".


^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 13/24] gdb: make solib-rocm not use so_list internally
  2023-10-17 15:23   ` Pedro Alves
@ 2023-10-17 15:32     ` Simon Marchi
  0 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-17 15:32 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 10/17/23 11:23, Pedro Alves wrote:
> On 2023-10-10 21:40, Simon Marchi wrote:
> 
>> --- a/gdb/solib-rocm.c
>> +++ b/gdb/solib-rocm.c
>> @@ -126,14 +126,26 @@ rocm_solib_fd_cache::close (int fd, fileio_error *target_errno)
>>  
>>  /* ROCm-specific inferior data.  */
>>  
>> +struct rocm_so
>> +{
>> +  rocm_so (const char *name, std::string unique_name, lm_info_svr4_up lm_info)
>> +    : name (name),
>> +      unique_name (std::move(unique_name)),
> 
> Space after "move".
> 

Thanks, fixed locally.

Simon

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 21/24] gdb: link so_list using intrusive_list
  2023-10-10 20:40 ` [PATCH 21/24] gdb: link so_list using intrusive_list Simon Marchi
@ 2023-10-17 19:14   ` Pedro Alves
  2023-10-17 19:38     ` Simon Marchi
  0 siblings, 1 reply; 56+ messages in thread
From: Pedro Alves @ 2023-10-17 19:14 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 2023-10-10 21:40, Simon Marchi wrote:

> diff --git a/gdb/solist.h b/gdb/solist.h
> index 9320acb7fd3e..31b823a3a482 100644
> --- a/gdb/solist.h
> +++ b/gdb/solist.h
> @@ -36,7 +36,7 @@ struct lm_info
>  
>  using lm_info_up = std::unique_ptr<lm_info>;
>  
> -struct so_list
> +struct so_list : intrusive_list_node<so_list>

I noticed this in patch #24:

 > @@ -52,7 +52,7 @@ struct so_list : intrusive_list_node<so_list>
 >       dynamic linker's tables in the inferior, and are initialized by
 >       current_sos.  */
 >  
 > -  struct so_list *next = nullptr;	/* next structure in linked list */
 > +  so *next = nullptr;	/* next structure in linked list */

Err, what "next" still doing there?  :-)  This patch should have deleted it.

Pedro Alves

>  {
>    /* Free symbol-file related contents of SO and reset for possible reloading
>       of SO.  If we have opened a BFD for SO, close it.  If we have placed SO's
> @@ -121,7 +121,7 @@ struct target_so_ops
>       inferior --- we don't examine any of the shared library files
>       themselves.  The declaration of `struct so_list' says which fields
>       we provide values for.  */
> -  struct so_list *(*current_sos) (void);
> +  intrusive_list<so_list> (*current_sos) ();
>  
>    /* Find, open, and read the symbols for the main executable.  If
>       FROM_TTY is non-zero, allow messages to be printed.  */
> @@ -171,8 +171,6 @@ struct target_so_ops
>    void (*handle_event) (void);
>  };
>  
> -using so_list_range = next_range<so_list>;
> -
>  /* Free the memory associated with a (so_list *).  */
>  void free_so (so_list &so);
>  


^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 00/24] C++ification of struct so_list
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (23 preceding siblings ...)
  2023-10-10 20:49 ` [PATCH 24/24] gdb: rename struct so_list to so Simon Marchi
@ 2023-10-17 19:20 ` Pedro Alves
  2023-10-17 19:53   ` Simon Marchi
  2023-10-19 11:09 ` [PATCH 0/24] " Lancelot SIX
  25 siblings, 1 reply; 56+ messages in thread
From: Pedro Alves @ 2023-10-17 19:20 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 2023-10-10 21:39, Simon Marchi wrote:
> This series modernizes the struct so_list area by C++ifying it a little
> bit and replacing the manual linked list implementation with
> intrusive_list.  It also contains a few other cleanups written along the
> way.

Very nicely split.  Thanks.  I sent a few comments, but nothing too serious.
The only thing on my end that requires some churn is the pointer vs reference
thing in the disposer.  Otherwise, all LGTM.

Oh, I did notice that "struct so" ends up as an ungreppable/unsearchable
name, and wondered whether that is going to make our lives harder in practice.
Maybe we should consider renaming it to "shared_obj" or "shobj" or something easier
to grep.  Or maybe it doesn't matter.  But no real strong opinion, I think I'm OK
with "so" too.


^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 21/24] gdb: link so_list using intrusive_list
  2023-10-17 19:14   ` Pedro Alves
@ 2023-10-17 19:38     ` Simon Marchi
  0 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-17 19:38 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 10/17/23 15:14, Pedro Alves wrote:
> On 2023-10-10 21:40, Simon Marchi wrote:
> 
>> diff --git a/gdb/solist.h b/gdb/solist.h
>> index 9320acb7fd3e..31b823a3a482 100644
>> --- a/gdb/solist.h
>> +++ b/gdb/solist.h
>> @@ -36,7 +36,7 @@ struct lm_info
>>  
>>  using lm_info_up = std::unique_ptr<lm_info>;
>>  
>> -struct so_list
>> +struct so_list : intrusive_list_node<so_list>
> 
> I noticed this in patch #24:
> 
>  > @@ -52,7 +52,7 @@ struct so_list : intrusive_list_node<so_list>
>  >       dynamic linker's tables in the inferior, and are initialized by
>  >       current_sos.  */
>  >  
>  > -  struct so_list *next = nullptr;	/* next structure in linked list */
>  > +  so *next = nullptr;	/* next structure in linked list */
> 
> Err, what "next" still doing there?  :-)  This patch should have deleted it.

Good find, fixed locally.

Simon

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 00/24] C++ification of struct so_list
  2023-10-17 19:20 ` [PATCH 00/24] C++ification of struct so_list Pedro Alves
@ 2023-10-17 19:53   ` Simon Marchi
  2023-10-20 14:40     ` Pedro Alves
  0 siblings, 1 reply; 56+ messages in thread
From: Simon Marchi @ 2023-10-17 19:53 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 10/17/23 15:20, Pedro Alves wrote:
> On 2023-10-10 21:39, Simon Marchi wrote:
>> This series modernizes the struct so_list area by C++ifying it a little
>> bit and replacing the manual linked list implementation with
>> intrusive_list.  It also contains a few other cleanups written along the
>> way.
> 
> Very nicely split.  Thanks.  I sent a few comments, but nothing too serious.
> The only thing on my end that requires some churn is the pointer vs reference
> thing in the disposer.  Otherwise, all LGTM.

I dropped the "use reference in disposer" patch.

> Oh, I did notice that "struct so" ends up as an ungreppable/unsearchable
> name, and wondered whether that is going to make our lives harder in practice.
> Maybe we should consider renaming it to "shared_obj" or "shobj" or something easier
> to grep.  Or maybe it doesn't matter.  But no real strong opinion, I think I'm OK
> with "so" too.

I wondered about that too.  If we are going to change the name, perhaps
we can find a more accurate one.  I'm currently working on trying to
make it possible for program spaces to have more than one solib
implementation providing so_lists / sos.  In the context of ROCm, this
will allow solib-svr4 to provide sos for the host, and solib-rocm to
provide sos for the GPU.  I will also try to see if the JIT mechanism
can then be changed to be one more solib provider, see if that
simplifies some things.  In the case of ROCm, we don't talk about shared
objects, but code objects that are loaded in the memory space.  In the
case of JIT... not sure what we call the generated code, but it's not
really a "shared object".  Perhaps there is a generic name we can find
for those pieces of code that can be loaded and unloaded at runtime.

In the mean time, I'll go with "so", since it just seems weird to have
intrusive_list<so_list>.  If we want to change it again it's not really
complicated, there aren't that many instances.

Simon

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 4/24] gdb: replace some so_list parameters to use references
  2023-10-10 20:39 ` [PATCH 04/24] gdb: replace some so_list parameters to use references Simon Marchi
@ 2023-10-19 11:07   ` Lancelot SIX
  2023-10-19 14:49     ` Simon Marchi
  0 siblings, 1 reply; 56+ messages in thread
From: Lancelot SIX @ 2023-10-19 11:07 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches, Simon Marchi

[-- Attachment #1: Type: text/plain, Size: 3729 bytes --]

Hi Simon,

Some minor remarks below.

On Tue, Oct 10, 2023 at 04:39:59PM -0400, Simon Marchi wrote:
> From: Simon Marchi <simon.marchi@polymtl.ca>
> 
> A subsequent patch changes so_list to be linked using
> intrusive_list.  Iterating an intrusive_list yields some references to
> the list elements.  Convert some functions accepting so_list objects to
> take references, to make things easier and more natural.  Add const
> where possible and convenient.
> 
> Change-Id: Id5ab5339c3eb6432e809ad14782952d6a45806f3
> ---
>  gdb/breakpoint.c     |   5 +-
>  gdb/bsd-uthread.c    |  12 ++---
>  gdb/exec.c           |   4 +-
>  gdb/interps.c        |   4 +-
>  gdb/interps.h        |   8 +--
>  gdb/mi/mi-cmd-file.c |   2 +-
>  gdb/mi/mi-interp.c   |  26 ++++-----
>  gdb/mi/mi-interp.h   |   6 +--
>  gdb/nto-tdep.c       |   6 +--
>  gdb/nto-tdep.h       |   3 +-
>  gdb/observable.h     |   5 +-
>  gdb/progspace.h      |   4 +-
>  gdb/solib-aix.c      |  11 ++--
>  gdb/solib-darwin.c   |  23 ++++----
>  gdb/solib-dsbt.c     |   9 ++--
>  gdb/solib-frv.c      |   9 ++--
>  gdb/solib-rocm.c     |   8 +--
>  gdb/solib-svr4.c     |  35 ++++++------
>  gdb/solib-target.c   |  49 +++++++++--------
>  gdb/solib.c          | 126 +++++++++++++++++++++----------------------
>  gdb/solib.h          |   4 +-
>  gdb/solist.h         |  13 +++--
>  gdb/target-section.h |   2 +-
>  23 files changed, 183 insertions(+), 191 deletions(-)
> 
> diff --git a/gdb/mi/mi-interp.h b/gdb/mi/mi-interp.h
> index f9af61f0a571..781a8dc6f466 100644
> --- a/gdb/mi/mi-interp.h
> +++ b/gdb/mi/mi-interp.h
> @@ -60,8 +60,8 @@ class mi_interp final : public interp
>    void on_record_changed (inferior *inf, int started, const char *method,
>  			  const char *format) override;
>    void on_target_resumed (ptid_t ptid) override;
> -  void on_solib_loaded (so_list *so) override;
> -  void on_solib_unloaded (so_list *so) override;
> +  void on_solib_loaded (const so_list &so) override;
> +  void on_solib_unloaded (const so_list &so) override;

It is orthogonal to this change, but it would make sense for those
methods to be const as well.

Doing this requires interp::interp_ui_out to be const as well (as done
in attached patch).

>    void on_about_to_proceed () override;
>    void on_traceframe_changed (int tfnum, int tpnum) override;
>    void on_tsv_created (const trace_state_variable *tsv) override;
> diff --git a/gdb/observable.h b/gdb/observable.h
> index acb05e9b535c..5ed6ca547ce0 100644
> --- a/gdb/observable.h
> +++ b/gdb/observable.h
> @@ -99,13 +99,12 @@ extern observable<inferior */* parent_inf */, inferior */* child_inf */,
>  /* The shared library specified by SOLIB has been loaded.  Note that
>     when gdb calls this observer, the library's symbols probably
>     haven't been loaded yet.  */
> -extern observable<struct so_list */* solib */> solib_loaded;
> +extern observable<so_list &/* solib */> solib_loaded;

I am wondering, is there a reason to make the solib parameter const for
solib_unloaded but not for solib_loaded?  

Changing it to const seems to still compile just fine.  If down the line
an observer needs to modify the SO, the observer's signature can be
adjusted.

>  
>  /* The shared library SOLIB has been unloaded from program space PSPACE.
>     Note  when gdb calls this observer, the library's symbols have not
>     been unloaded yet, and thus are still available.  */
> -extern observable<struct program_space */* pspace */, struct so_list */* solib */>
> -  solib_unloaded;
> +extern observable<program_space *, const so_list &/* solib */> solib_unloaded;
>  
>  /* The symbol file specified by OBJFILE has been loaded.  */
>  extern observable<struct objfile */* objfile */> new_objfile;

[-- Attachment #2: 0001-gdb-interp-Make-interp-interp_ui_out-const.patch --]
[-- Type: text/x-diff, Size: 3622 bytes --]

From 4e9158ca45ffc4e15c8c57a0df9f99ab49a35c0e Mon Sep 17 00:00:00 2001
From: Lancelot SIX <lancelot.six@amd.com>
Date: Thu, 19 Oct 2023 09:29:39 +0100
Subject: [PATCH] gdb/interp: Make interp::interp_ui_out const

The interp::interp_ui_out can be declared const.  Make this change as it
will be used in a following patch.

Change-Id: I11844d36f44d369400049f916db94aca9f43be51
---
 gdb/cli/cli-interp.c | 4 ++--
 gdb/interps.h        | 2 +-
 gdb/mi/mi-interp.c   | 2 +-
 gdb/mi/mi-interp.h   | 2 +-
 gdb/python/py-dap.c  | 2 +-
 gdb/tui/tui-interp.c | 4 ++--
 6 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/gdb/cli/cli-interp.c b/gdb/cli/cli-interp.c
index 829948b4993..21c78fd98a9 100644
--- a/gdb/cli/cli-interp.c
+++ b/gdb/cli/cli-interp.c
@@ -50,7 +50,7 @@ class cli_interp final : public cli_interp_base
   void resume () override;
   void suspend () override;
   void exec (const char *command_str) override;
-  ui_out *interp_ui_out () override;
+  ui_out *interp_ui_out () const override;
 
 private:
 
@@ -255,7 +255,7 @@ cli_interp_base::supports_command_editing ()
 }
 
 ui_out *
-cli_interp::interp_ui_out ()
+cli_interp::interp_ui_out () const
 {
   return m_cli_uiout.get ();
 }
diff --git a/gdb/interps.h b/gdb/interps.h
index 287df2c8c81..a3cbf32aa12 100644
--- a/gdb/interps.h
+++ b/gdb/interps.h
@@ -65,7 +65,7 @@ class interp : public intrusive_list_node<interp>
      interpreter.  It can be a formatter for stdout, as is the case
      for the console & mi outputs, or it might be a result
      formatter.  */
-  virtual ui_out *interp_ui_out () = 0;
+  virtual ui_out *interp_ui_out () const = 0;
 
   /* Provides a hook for interpreters to do any additional
      setup/cleanup that they might need when logging is enabled or
diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
index 0d3c8235c2c..70075eed16b 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -875,7 +875,7 @@ mi_interp::on_user_selected_context_changed (user_selected_what selection)
 }
 
 ui_out *
-mi_interp::interp_ui_out ()
+mi_interp::interp_ui_out () const
 {
   return this->mi_uiout;
 }
diff --git a/gdb/mi/mi-interp.h b/gdb/mi/mi-interp.h
index f9af61f0a57..ff3c439f1e3 100644
--- a/gdb/mi/mi-interp.h
+++ b/gdb/mi/mi-interp.h
@@ -37,7 +37,7 @@ class mi_interp final : public interp
   void resume () override;
   void suspend () override;
   void exec (const char *command_str) override;
-  ui_out *interp_ui_out () override;
+  ui_out *interp_ui_out () const override;
   void set_logging (ui_file_up logfile, bool logging_redirect,
 		    bool debug_redirect) override;
   void pre_command_loop () override;
diff --git a/gdb/python/py-dap.c b/gdb/python/py-dap.c
index 3444eccb6fb..7a78e54571f 100644
--- a/gdb/python/py-dap.c
+++ b/gdb/python/py-dap.c
@@ -56,7 +56,7 @@ class dap_interp final : public interp
     /* Just ignore it.  */
   }
 
-  ui_out *interp_ui_out () override
+  ui_out *interp_ui_out () const override
   {
     return m_ui_out.get ();
   }
diff --git a/gdb/tui/tui-interp.c b/gdb/tui/tui-interp.c
index 299cc4caea0..a88162c08e1 100644
--- a/gdb/tui/tui-interp.c
+++ b/gdb/tui/tui-interp.c
@@ -50,7 +50,7 @@ class tui_interp final : public cli_interp_base
   void resume () override;
   void suspend () override;
   void exec (const char *command_str) override;
-  ui_out *interp_ui_out () override;
+  ui_out *interp_ui_out () const override;
 };
 
 /* Cleanup the tui before exiting.  */
@@ -141,7 +141,7 @@ tui_interp::suspend ()
 }
 
 ui_out *
-tui_interp::interp_ui_out ()
+tui_interp::interp_ui_out () const
 {
   if (tui_active)
     return tui_out;

-- 
2.34.1


^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 16/24] gdb: make clear_so a method of struct so_list
  2023-10-10 20:40 ` [PATCH 16/24] gdb: make clear_so a method of struct so_list Simon Marchi
@ 2023-10-19 11:08   ` Lancelot SIX
  2023-10-19 14:52     ` Simon Marchi
  0 siblings, 1 reply; 56+ messages in thread
From: Lancelot SIX @ 2023-10-19 11:08 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

Hi Simon,

On Tue, Oct 10, 2023 at 04:40:11PM -0400, Simon Marchi wrote:
> ... just because it seems to make sense to do so.
> 
> Change-Id: Ie283c92d9b90c54e3deee96a43c6a942d8b5910b
> ---
>  gdb/solib.c  | 35 ++++++++++++++---------------------
>  gdb/solist.h | 10 ++++++++++
>  2 files changed, 24 insertions(+), 21 deletions(-)
> 
> diff --git a/gdb/solib.c b/gdb/solib.c
> index 5ed223b8dfca..8f5b6e02e71c 100644
> --- a/gdb/solib.c
> +++ b/gdb/solib.c
> @@ -623,39 +623,32 @@ solib_map_sections (so_list &so)
>    return 1;
>  }
>  
> -/* Free symbol-file related contents of SO and reset for possible reloading
> -   of SO.  If we have opened a BFD for SO, close it.  If we have placed SO's
> -   sections in some target's section table, the caller is responsible for
> -   removing them.
> +/* See solist.h.  */
>  
> -   This function doesn't mess with objfiles at all.  If there is an
> -   objfile associated with SO that needs to be removed, the caller is
> -   responsible for taking care of that.  */
> -
> -static void
> -clear_so (so_list &so)
> +void
> +so_list::clear ()
>  {
>    const target_so_ops *ops = gdbarch_so_ops (current_inferior ()->arch ());
>  
> -  delete so.sections;
> -  so.sections = NULL;
> +  delete this->sections;
> +  this->sections = NULL;

That's just a nit, but while at touching those lines, the NULL could be
changed to nullptr.

Best,
Lancelot.

>  
> -  gdb_bfd_unref (so.abfd);
> -  so.abfd = NULL;
> +  gdb_bfd_unref (this->abfd);
> +  this->abfd = NULL;
>  
>    /* Our caller closed the objfile, possibly via objfile_purge_solibs.  */
> -  so.symbols_loaded = 0;
> -  so.objfile = NULL;
> +  this->symbols_loaded = 0;
> +  this->objfile = NULL;
>  
> -  so.addr_low = so.addr_high = 0;
> +  this->addr_low = this->addr_high = 0;
>  
>    /* Restore the target-supplied file name.  SO_NAME may be the path
>       of the symbol file.  */
> -  strcpy (so.so_name, so.so_original_name);
> +  strcpy (this->so_name, this->so_original_name);
>  
>    /* Do the same for target-specific data.  */
>    if (ops->clear_so != NULL)
> -    ops->clear_so (so);
> +    ops->clear_so (*this);
>  }
>  
>  lm_info::~lm_info () = default;
> @@ -674,7 +667,7 @@ lm_info::~lm_info () = default;
>  void
>  free_so (so_list &so)
>  {
> -  clear_so (so);
> +  so.clear ();
>    delete &so;
>  }
>  
> @@ -1358,7 +1351,7 @@ reload_shared_libraries_1 (int from_tty)
>  	      && !solib_used (so))
>  	    so->objfile->unlink ();
>  	  current_program_space->remove_target_sections (so);
> -	  clear_so (*so);
> +	  so->clear ();
>  	}
>  
>        /* If this shared library is now associated with a new symbol
> diff --git a/gdb/solist.h b/gdb/solist.h
> index 5f9090a07c72..75e8e8ad0182 100644
> --- a/gdb/solist.h
> +++ b/gdb/solist.h
> @@ -38,6 +38,16 @@ using lm_info_up = std::unique_ptr<lm_info>;
>  
>  struct so_list
>  {
> +  /* Free symbol-file related contents of SO and reset for possible reloading
> +     of SO.  If we have opened a BFD for SO, close it.  If we have placed SO's
> +     sections in some target's section table, the caller is responsible for
> +     removing them.
> +
> +     This function doesn't mess with objfiles at all.  If there is an
> +     objfile associated with SO that needs to be removed, the caller is
> +     responsible for taking care of that.  */
> +  void clear () ;
> +
>    /* The following fields of the structure come directly from the
>       dynamic linker's tables in the inferior, and are initialized by
>       current_sos.  */
> 
> -- 
> 2.42.0
> 

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 20/24] gdb: make so_list::{so_original_name,so_name} std::strings
  2023-10-10 20:40 ` [PATCH 20/24] gdb: make so_list::{so_original_name,so_name} std::strings Simon Marchi
  2023-10-13 22:28   ` [PATCH 20/24] gdb: make so_list::{so_original_name, so_name} std::strings Lancelot SIX
@ 2023-10-19 11:08   ` Lancelot SIX
  2023-10-19 14:55     ` Simon Marchi
  1 sibling, 1 reply; 56+ messages in thread
From: Lancelot SIX @ 2023-10-19 11:08 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

Hi Simon,

Some other minor NULL -> nullptr possible conversions below.

On Tue, Oct 10, 2023 at 04:40:15PM -0400, Simon Marchi wrote:
> Change these two fields, simplifying memory management and copying.
> 
> Change-Id: If2559284c515721e71e1ef56ada8b64667eebe55
> ---
>  gdb/break-catch-load.c |  2 +-
>  gdb/breakpoint.c       |  2 +-
>  gdb/bsd-uthread.c      |  8 +++----
>  gdb/mi/mi-cmd-file.c   |  2 +-
>  gdb/solib-aix.c        |  8 +++----
>  gdb/solib-darwin.c     |  5 ++---
>  gdb/solib-dsbt.c       |  5 ++---
>  gdb/solib-frv.c        |  6 ++----
>  gdb/solib-rocm.c       |  8 ++-----
>  gdb/solib-svr4.c       | 25 ++++++++++------------
>  gdb/solib-target.c     | 18 ++++++----------
>  gdb/solib.c            | 47 +++++++++++++++++++++++-------------------
>  gdb/solist.h           |  4 ++--
>  13 files changed, 63 insertions(+), 77 deletions(-)
> 
> diff --git a/gdb/break-catch-load.c b/gdb/break-catch-load.c
> index 94d8b420d327..999de874d5f6 100644
> --- a/gdb/break-catch-load.c
> +++ b/gdb/break-catch-load.c
> @@ -120,7 +120,7 @@ solib_catchpoint::check_status (struct bpstat *bs)
>        for (so_list *iter : current_program_space->added_solibs)
>  	{
>  	  if (!regex
> -	      || compiled->exec (iter->so_name, 0, NULL, 0) == 0)
> +	      || compiled->exec (iter->so_name.c_str (), 0, NULL, 0) == 0)

While at touching this line, s/NULL/nullptr.

>  	    return;
>  	}
>      }
> diff --git a/gdb/mi/mi-cmd-file.c b/gdb/mi/mi-cmd-file.c
> index 822d3df40005..5f63ce4494cb 100644
> --- a/gdb/mi/mi-cmd-file.c
> +++ b/gdb/mi/mi-cmd-file.c
> @@ -167,7 +167,7 @@ mi_cmd_file_list_shared_libraries (const char *command,
>      {
>        if (so->so_name[0] == '\0')
>  	continue;
> -      if (pattern != NULL && !re_exec (so->so_name))
> +      if (pattern != NULL && !re_exec (so->so_name.c_str ()))

Same.

>  	continue;
>  
>        ui_out_emit_tuple tuple_emitter (uiout, NULL);
> diff --git a/gdb/solib.c b/gdb/solib.c
> index 6b7f0da0498d..1815b0910208 100644
> --- a/gdb/solib.c
> +++ b/gdb/solib.c
> @@ -705,9 +708,10 @@ solib_read_symbols (so_list &so, symfile_add_flags flags)
>  	      section_addr_info sap
>  		= build_section_addr_info_from_section_table (so.sections);
>  	      gdb_bfd_ref_ptr tmp_bfd = so.abfd;
> -	      so.objfile = symbol_file_add_from_bfd (tmp_bfd, so.so_name,
> -						      flags, &sap,
> -						      OBJF_SHARED, NULL);
> +	      so.objfile = symbol_file_add_from_bfd (tmp_bfd,
> +						     so.so_name.c_str (),
> +						     flags, &sap,
> +						     OBJF_SHARED, NULL);

Same.

>  	      so.objfile->addr_low = so.addr_low;
>  	    }
>  

Best,
Lancelot.

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 12/24] gdb: make solib-svr4 not use so_list internally
  2023-10-10 20:40 ` [PATCH 12/24] gdb: make solib-svr4 not use so_list internally Simon Marchi
  2023-10-13 17:52   ` Lancelot SIX
@ 2023-10-19 11:08   ` Lancelot SIX
  2023-10-19 14:50     ` Simon Marchi
  1 sibling, 1 reply; 56+ messages in thread
From: Lancelot SIX @ 2023-10-19 11:08 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

Hi Simon,

> @@ -1050,48 +1034,25 @@ library_list_start_library (struct gdb_xml_parser *parser,
>    ULONGEST *l_ldp
>      = (ULONGEST *) xml_find_attribute (attributes, "l_ld")->value.get ();
>  
> -  so_list *new_elem = new so_list;
> -  lm_info_svr4 *li = new lm_info_svr4;
> -  new_elem->lm_info = li;
> +  lm_info_svr4_up li = gdb::make_unique<lm_info_svr4> ();
>    li->lm_addr = *lmp;
>    li->l_addr_inferior = *l_addrp;
>    li->l_ld = *l_ldp;

Just a thought for future changes to continue the c++-ification, adding
a proper ctor (or ctors) to lm_info_svr4 would probably make things
cleaner.

Otherwise, things looks reasonable to me.

Best,
Lancelot.

>  
> -  strncpy (new_elem->so_name, name, sizeof (new_elem->so_name) - 1);
> -  new_elem->so_name[sizeof (new_elem->so_name) - 1] = 0;
> -  strcpy (new_elem->so_original_name, new_elem->so_name);
> +  std::vector<svr4_so> *solist;
>  
>    /* Older versions did not supply lmid.  Put the element into the flat
>       list of the special namespace zero in that case.  */
>    gdb_xml_value *at_lmid = xml_find_attribute (attributes, "lmid");
>    if (at_lmid == nullptr)
> -    {
> -      *list->tailp = new_elem;
> -      list->tailp = &new_elem->next;
> -    }
> +    solist = list->cur_list;
>    else
>      {
>        ULONGEST lmid = *(ULONGEST *) at_lmid->value.get ();
> -
> -      /* Ensure that the element is actually initialized.  */
> -      if (list->solib_lists.find (lmid) == list->solib_lists.end ())
> -	list->solib_lists[lmid] = nullptr;
> -
> -      so_list **psolist = &list->solib_lists[lmid];
> -      so_list **pnext = psolist;
> -
> -      /* Walk to the end of the list if we have one.  */
> -      so_list *solist = *psolist;
> -      if (solist != nullptr)
> -	{
> -	  for (; solist->next != nullptr; solist = solist->next)
> -	    /* Nothing.  */;
> -
> -	  pnext = &solist->next;
> -	}
> -
> -      *pnext = new_elem;
> +      solist = &list->solib_lists[lmid];
>      }
> +
> +  solist->emplace_back (name, std::move (li));
>  }
>  
>  /* Handle the start of a <library-list-svr4> element.  */
> @@ -1117,9 +1078,7 @@ svr4_library_list_start_list (struct gdb_xml_parser *parser,
>  
>    /* Older gdbserver do not support namespaces.  We use the special
>       namespace zero for a linear list of libraries.  */
> -  so_list **solist = &list->solib_lists[0];
> -  *solist = nullptr;
> -  list->tailp = solist;
> +  list->cur_list = &list->solib_lists[0];
>  }
>  
>  /* The allowed elements and attributes for an XML library list.
> @@ -1169,13 +1128,9 @@ static int
>  svr4_parse_libraries (const char *document, struct svr4_library_list *list)
>  {
>    auto cleanup = make_scope_exit ([list] ()
> -    {
> -      for (const std::pair<CORE_ADDR, so_list *> tuple
> -	     : list->solib_lists)
> -	svr4_free_library_list (tuple.second);
> -    });
> +    {  list->solib_lists.clear (); });
>  
> -  list->tailp = nullptr;
> +  list->cur_list = nullptr;
>    list->main_lm = 0;
>    list->solib_lists.clear ();
>    if (gdb_xml_parse_quick (_("target library list"), "library-list-svr4.dtd",
> @@ -1253,25 +1208,21 @@ svr4_default_sos (svr4_info *info)
>  
>  /* Read the whole inferior libraries chain starting at address LM.
>     Expect the first entry in the chain's previous entry to be PREV_LM.
> -   Add the entries to the tail referenced by LINK_PTR_PTR.  Ignore the
> -   first entry if IGNORE_FIRST and set global MAIN_LM_ADDR according
> -   to it.  Returns nonzero upon success.  If zero is returned the
> -   entries stored to LINK_PTR_PTR are still valid although they may
> +   Add the entries to SOS.  Ignore the first entry if IGNORE_FIRST and set
> +   global MAIN_LM_ADDR according to it.  Returns nonzero upon success.  If zero
> +   is returned the entries stored to LINK_PTR_PTR are still valid although they may
>     represent only part of the inferior library list.  */
>  
>  static int
>  svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
> -		   struct so_list ***link_ptr_ptr, int ignore_first)
> +		   std::vector<svr4_so> &sos, int ignore_first)
>  {
>    CORE_ADDR first_l_name = 0;
>    CORE_ADDR next_lm;
>  
>    for (; lm != 0; prev_lm = lm, lm = next_lm)
>      {
> -      so_list_up newobj (new so_list);
> -
> -      lm_info_svr4 *li = lm_info_read (lm).release ();
> -      newobj->lm_info = li;
> +      lm_info_svr4_up li = lm_info_read (lm);
>        if (li == NULL)
>  	return 0;
>  
> @@ -1298,9 +1249,9 @@ svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
>  	}
>  
>        /* Extract this shared object's name.  */
> -      gdb::unique_xmalloc_ptr<char> buffer
> +      gdb::unique_xmalloc_ptr<char> name
>  	= target_read_string (li->l_name, SO_NAME_MAX_PATH_SIZE - 1);
> -      if (buffer == nullptr)
> +      if (name == nullptr)
>  	{
>  	  /* If this entry's l_name address matches that of the
>  	     inferior executable, then this is not a normal shared
> @@ -1311,19 +1262,12 @@ svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
>  	  continue;
>  	}
>  
> -      strncpy (newobj->so_name, buffer.get (), SO_NAME_MAX_PATH_SIZE - 1);
> -      newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
> -      strcpy (newobj->so_original_name, newobj->so_name);
> -
>        /* If this entry has no name, or its name matches the name
>  	 for the main executable, don't include it in the list.  */
> -      if (! newobj->so_name[0] || match_main (newobj->so_name))
> +      if (*name == '\0' || match_main (name.get ()))
>  	continue;
>  
> -      newobj->next = 0;
> -      /* Don't free it now.  */
> -      **link_ptr_ptr = newobj.release ();
> -      *link_ptr_ptr = &(**link_ptr_ptr)->next;
> +      sos.emplace_back (name.get (), std::move (li));
>      }
>  
>    return 1;
> @@ -1342,7 +1286,7 @@ svr4_current_sos_direct (struct svr4_info *info)
>    struct svr4_library_list library_list;
>  
>    /* Remove any old libraries.  We're going to read them back in again.  */
> -  free_solib_lists (info);
> +  info->solib_lists.clear ();
>  
>    /* Fall back to manual examination of the target if the packet is not
>       supported or gdbserver failed to find DT_DEBUG.  gdb.server/solib-list.exp
> @@ -1362,10 +1306,10 @@ svr4_current_sos_direct (struct svr4_info *info)
>        /* Remove an empty special zero namespace so we know that when there
>  	 is one, it is actually used, and we have a flat list without
>  	 namespace information.  */
> -      if ((library_list.solib_lists.find (0)
> -	   != library_list.solib_lists.end ())
> -	  && (library_list.solib_lists[0] == nullptr))
> -	library_list.solib_lists.erase (0);
> +      auto it_0 = library_list.solib_lists.find (0);
> +      if (it_0 != library_list.solib_lists.end ()
> +	  && it_0->second.empty ())
> +	library_list.solib_lists.erase (it_0);
>  
>        /* Replace the (empty) solib_lists in INFO with the one generated
>  	 from the target.  We don't want to copy it on assignment and then
> @@ -1391,9 +1335,7 @@ svr4_current_sos_direct (struct svr4_info *info)
>      ignore_first = true;
>  
>    auto cleanup = make_scope_exit ([info] ()
> -    {
> -      free_solib_lists (info);
> -    });
> +    { info->solib_lists.clear (); });
>  
>    /* Collect the sos in each namespace.  */
>    CORE_ADDR debug_base = info->debug_base;
> @@ -1403,12 +1345,8 @@ svr4_current_sos_direct (struct svr4_info *info)
>        /* Walk the inferior's link map list, and build our so_list list.  */
>        lm = solib_svr4_r_map (debug_base);
>        if (lm != 0)
> -	{
> -	  so_list **sos = &info->solib_lists[debug_base];
> -	  *sos = nullptr;
> -
> -	  svr4_read_so_list (info, lm, 0, &sos, ignore_first);
> -	}
> +	svr4_read_so_list (info, lm, 0, info->solib_lists[debug_base],
> +			   ignore_first);
>      }
>  
>    /* On Solaris, the dynamic linker is not in the normal list of
> @@ -1425,11 +1363,8 @@ svr4_current_sos_direct (struct svr4_info *info)
>      {
>        /* Add the dynamic linker's namespace unless we already did.  */
>        if (info->solib_lists.find (debug_base) == info->solib_lists.end ())
> -	{
> -	  so_list **sos = &info->solib_lists[debug_base];
> -	  *sos = nullptr;
> -	  svr4_read_so_list (info, debug_base, 0, &sos, 0);
> -	}
> +	svr4_read_so_list (info, debug_base, 0, info->solib_lists[debug_base],
> +			   0);
>      }
>  
>    cleanup.release ();
> @@ -1440,19 +1375,18 @@ svr4_current_sos_direct (struct svr4_info *info)
>  static so_list *
>  svr4_collect_probes_sos (svr4_info *info)
>  {
> -  so_list *sos = nullptr;
> -  so_list **pnext = &sos;
> +  so_list *res = nullptr;
> +  so_list **pnext = &res;
>  
> -  for (const std::pair<CORE_ADDR, so_list *> tuple
> -	 : info->solib_lists)
> +  for (const auto &tuple : info->solib_lists)
>      {
> -      so_list *solist = tuple.second;
> +      const std::vector<svr4_so> &sos = tuple.second;
>  
>        /* Allow the linker to report empty namespaces.  */
> -      if (solist == nullptr)
> +      if (sos.empty ())
>  	continue;
>  
> -      *pnext = svr4_copy_library_list (solist);
> +      *pnext = so_list_from_svr4_sos (sos);
>  
>        /* Update PNEXT to point to the next member of the last element.  */
>        gdb_assert (*pnext != nullptr);
> @@ -1466,7 +1400,7 @@ svr4_collect_probes_sos (svr4_info *info)
>  	}
>      }
>  
> -  return sos;
> +  return res;
>  }
>  
>  /* Implement the main part of the "current_sos" target_so_ops
> @@ -1853,16 +1787,10 @@ solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
>    if (info->solib_lists.find (0) != info->solib_lists.end ())
>      return 0;
>  
> -  /* Ensure that the element is actually initialized.  */
> -  if (info->solib_lists.find (debug_base) == info->solib_lists.end ())
> -    info->solib_lists[debug_base] = nullptr;
> -
> -  so_list **psolist = &info->solib_lists[debug_base];
> -  so_list **pnext = nullptr;
> -  so_list *solist = *psolist;
> +  std::vector<svr4_so> &solist = info->solib_lists[debug_base];
>    CORE_ADDR prev_lm;
>  
> -  if (solist == nullptr)
> +  if (solist.empty ())
>      {
>        /* svr4_current_sos_direct contains logic to handle a number of
>  	 special cases relating to the first elements of the list in
> @@ -1872,18 +1800,9 @@ solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
>  	return 0;
>  
>        prev_lm = 0;
> -      pnext = psolist;
>      }
>    else
> -    {
> -      /* Walk to the end of the list.  */
> -      for (; solist->next != nullptr; solist = solist->next)
> -	/* Nothing.  */;
> -
> -      auto *li = gdb::checked_static_cast<lm_info_svr4 *> (solist->lm_info);
> -      prev_lm = li->lm_addr;
> -      pnext = &solist->next;
> -    }
> +    prev_lm = solist.back ().lm_info->lm_addr;
>  
>    /* Read the new objects.  */
>    if (info->using_xfer)
> @@ -1905,24 +1824,23 @@ solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
>  
>  	 We expect gdbserver to provide updates for the namespace that
>  	 contains LM, which would be this namespace...  */
> -      so_list *sos = nullptr;
> -      if (library_list.solib_lists.find (debug_base)
> -	  != library_list.solib_lists.end ())
> -	std::swap (sos, library_list.solib_lists[debug_base]);
> -      if (sos == nullptr)
> +      std::vector<svr4_so> sos;
> +      auto it_debug_base = library_list.solib_lists.find (debug_base);
> +      if (it_debug_base != library_list.solib_lists.end ())
> +	std::swap (sos, it_debug_base->second);
> +      else
>  	{
>  	  /* ...or for the special zero namespace for earlier versions...  */
> -	  if (library_list.solib_lists.find (0)
> -	      != library_list.solib_lists.end ())
> -	    std::swap (sos, library_list.solib_lists[0]);
> +	  auto it_0 = library_list.solib_lists.find (0);
> +	  if (it_0 != library_list.solib_lists.end ())
> +	    std::swap (sos, it_0->second);
>  	}
>  
>        /* ...but nothing else.  */
> -      for (const std::pair<CORE_ADDR, so_list *> tuple
> -	     : library_list.solib_lists)
> -	gdb_assert (tuple.second == nullptr);
> +      for (const auto &tuple : library_list.solib_lists)
> +	gdb_assert (tuple.second.empty ());
>  
> -      *pnext = sos;
> +      std::move (sos.begin (), sos.end (), std::back_inserter (solist));
>      }
>    else
>      {
> @@ -1930,7 +1848,7 @@ solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
>  	 above check and deferral to solist_update_full ensures
>  	 that this call to svr4_read_so_list will never see the
>  	 first element.  */
> -      if (!svr4_read_so_list (info, lm, prev_lm, &pnext, 0))
> +      if (!svr4_read_so_list (info, lm, prev_lm, solist, 0))
>  	return 0;
>      }
>  
> @@ -1948,7 +1866,7 @@ disable_probes_interface (svr4_info *info)
>  	     "Reverting to original interface."));
>  
>    free_probes_table (info);
> -  free_solib_lists (info);
> +  info->solib_lists.clear ();
>  }
>  
>  /* Update the solib list as appropriate when using the
> @@ -3145,7 +3063,7 @@ svr4_solib_create_inferior_hook (int from_tty)
>  
>    /* Clear the probes-based interface's state.  */
>    free_probes_table (info);
> -  free_solib_lists (info);
> +  info->solib_lists.clear ();
>  
>    /* Relocate the main executable if necessary.  */
>    svr4_relocate_main_executable ();
> @@ -3373,14 +3291,17 @@ find_debug_base_for_solib (so_list *solib)
>  
>    svr4_info *info = get_svr4_info (current_program_space);
>    gdb_assert (info != nullptr);
> -  for (const std::pair<CORE_ADDR, so_list *> tuple
> -	 : info->solib_lists)
> +
> +  const lm_info_svr4 *lm_info = (const lm_info_svr4 *) solib->lm_info;
> +
> +  for (const auto &tuple : info->solib_lists)
>      {
>        CORE_ADDR debug_base = tuple.first;
> -      so_list *solist = tuple.second;
> +      const std::vector<svr4_so> &sos = tuple.second;
>  
> -      for (; solist != nullptr; solist = solist->next)
> -	if (svr4_same (*solib, *solist))
> +      for (const svr4_so &so : sos)
> +	if (svr4_same (solib->so_original_name, so.name.c_str (),
> +		       *lm_info, *so.lm_info))
>  	  return debug_base;
>      }
>  
> diff --git a/gdb/solib-svr4.h b/gdb/solib-svr4.h
> index 1aff3b59da8c..7cb6cabb109f 100644
> --- a/gdb/solib-svr4.h
> +++ b/gdb/solib-svr4.h
> @@ -48,6 +48,8 @@ struct lm_info_svr4 final : public lm_info
>    CORE_ADDR l_ld = 0, l_next = 0, l_prev = 0, l_name = 0;
>  };
>  
> +using lm_info_svr4_up = std::unique_ptr<lm_info_svr4>;
> +
>  /* Critical offsets and sizes which describe struct r_debug and
>     struct link_map on SVR4-like targets.  All offsets and sizes are
>     in bytes unless otherwise specified.  */
> 
> -- 
> 2.42.0
> 

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 0/24] C++ification of struct so_list
  2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
                   ` (24 preceding siblings ...)
  2023-10-17 19:20 ` [PATCH 00/24] C++ification of struct so_list Pedro Alves
@ 2023-10-19 11:09 ` Lancelot SIX
  2023-10-19 14:57   ` Simon Marchi
  25 siblings, 1 reply; 56+ messages in thread
From: Lancelot SIX @ 2023-10-19 11:09 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

Hi Simon,

I went through the series again, sent a few extra small remarks, and
FWIW, it looks reasonable to me.

I do agree with Pedro's point that "struct so" becomes a bit harder to
search for, and "shobj" would have less false-positives, but I
nevertheless ok with using "so".  If it turns out to be a hurdle, we
can always change it later.

I have tested the AMDGPU part of the series (gdb.rocm/*.exp), both on
top of master and downstream ROCgdb.

So with the various nits fixed:
Reviewed-By: Lancelot Six <lancelot.six@amd.com>

Thanks for doing this!
Lancelot.

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 4/24] gdb: replace some so_list parameters to use references
  2023-10-19 11:07   ` [PATCH 4/24] " Lancelot SIX
@ 2023-10-19 14:49     ` Simon Marchi
  2023-10-19 15:20       ` Lancelot SIX
  0 siblings, 1 reply; 56+ messages in thread
From: Simon Marchi @ 2023-10-19 14:49 UTC (permalink / raw)
  To: Lancelot SIX, Simon Marchi; +Cc: gdb-patches

On 10/19/23 07:07, Lancelot SIX wrote:
> Hi Simon,
> 
> Some minor remarks below.
> 
> On Tue, Oct 10, 2023 at 04:39:59PM -0400, Simon Marchi wrote:
>> From: Simon Marchi <simon.marchi@polymtl.ca>
>>
>> A subsequent patch changes so_list to be linked using
>> intrusive_list.  Iterating an intrusive_list yields some references to
>> the list elements.  Convert some functions accepting so_list objects to
>> take references, to make things easier and more natural.  Add const
>> where possible and convenient.
>>
>> Change-Id: Id5ab5339c3eb6432e809ad14782952d6a45806f3
>> ---
>>  gdb/breakpoint.c     |   5 +-
>>  gdb/bsd-uthread.c    |  12 ++---
>>  gdb/exec.c           |   4 +-
>>  gdb/interps.c        |   4 +-
>>  gdb/interps.h        |   8 +--
>>  gdb/mi/mi-cmd-file.c |   2 +-
>>  gdb/mi/mi-interp.c   |  26 ++++-----
>>  gdb/mi/mi-interp.h   |   6 +--
>>  gdb/nto-tdep.c       |   6 +--
>>  gdb/nto-tdep.h       |   3 +-
>>  gdb/observable.h     |   5 +-
>>  gdb/progspace.h      |   4 +-
>>  gdb/solib-aix.c      |  11 ++--
>>  gdb/solib-darwin.c   |  23 ++++----
>>  gdb/solib-dsbt.c     |   9 ++--
>>  gdb/solib-frv.c      |   9 ++--
>>  gdb/solib-rocm.c     |   8 +--
>>  gdb/solib-svr4.c     |  35 ++++++------
>>  gdb/solib-target.c   |  49 +++++++++--------
>>  gdb/solib.c          | 126 +++++++++++++++++++++----------------------
>>  gdb/solib.h          |   4 +-
>>  gdb/solist.h         |  13 +++--
>>  gdb/target-section.h |   2 +-
>>  23 files changed, 183 insertions(+), 191 deletions(-)
>>
>> diff --git a/gdb/mi/mi-interp.h b/gdb/mi/mi-interp.h
>> index f9af61f0a571..781a8dc6f466 100644
>> --- a/gdb/mi/mi-interp.h
>> +++ b/gdb/mi/mi-interp.h
>> @@ -60,8 +60,8 @@ class mi_interp final : public interp
>>    void on_record_changed (inferior *inf, int started, const char *method,
>>  			  const char *format) override;
>>    void on_target_resumed (ptid_t ptid) override;
>> -  void on_solib_loaded (so_list *so) override;
>> -  void on_solib_unloaded (so_list *so) override;
>> +  void on_solib_loaded (const so_list &so) override;
>> +  void on_solib_unloaded (const so_list &so) override;
> 
> It is orthogonal to this change, but it would make sense for those
> methods to be const as well.
> 
> Doing this requires interp::interp_ui_out to be const as well (as done
> in attached patch).

Why do you think the interp object should be const (which is the effect
of marking the methods const)?  These methods are notifiers to let the
interp know about certain events that occured.  Whether they should
modify the state of the interpreter or not is up to the particular
implementation of the interpreter.  Perhaps you're right, but I don't
see it.

In your patch, you make interp::interp_ui_out const, but it still
returns a non-const ui_out.  So when outputting something, the
interpreter itself is not modified, but the ui_out behind it is.  I
presume (sometimes) the ui_out needs to be non-const, since for things
like pagination it clearly needs to keep a state.  Design-wise, is it
"correct" to make a const intepreter able to return a non-const ui_out?

>>    void on_about_to_proceed () override;
>>    void on_traceframe_changed (int tfnum, int tpnum) override;
>>    void on_tsv_created (const trace_state_variable *tsv) override;
>> diff --git a/gdb/observable.h b/gdb/observable.h
>> index acb05e9b535c..5ed6ca547ce0 100644
>> --- a/gdb/observable.h
>> +++ b/gdb/observable.h
>> @@ -99,13 +99,12 @@ extern observable<inferior */* parent_inf */, inferior */* child_inf */,
>>  /* The shared library specified by SOLIB has been loaded.  Note that
>>     when gdb calls this observer, the library's symbols probably
>>     haven't been loaded yet.  */
>> -extern observable<struct so_list */* solib */> solib_loaded;
>> +extern observable<so_list &/* solib */> solib_loaded;
> 
> I am wondering, is there a reason to make the solib parameter const for
> solib_unloaded but not for solib_loaded?  
> 
> Changing it to const seems to still compile just fine.  If down the line
> an observer needs to modify the SO, the observer's signature can be
> adjusted.

Just pragmatic reasons.  Do you build with --enable-targets=all?

It's because bsd_uthread_solib_loaded calls solib_read_symbols to read
in the symbols of the library it is looking for, and that requires a
non-const so_list.

Simon

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 12/24] gdb: make solib-svr4 not use so_list internally
  2023-10-19 11:08   ` Lancelot SIX
@ 2023-10-19 14:50     ` Simon Marchi
  0 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-19 14:50 UTC (permalink / raw)
  To: Lancelot SIX, Simon Marchi; +Cc: gdb-patches

On 10/19/23 07:08, Lancelot SIX wrote:
> Hi Simon,
> 
>> @@ -1050,48 +1034,25 @@ library_list_start_library (struct gdb_xml_parser *parser,
>>    ULONGEST *l_ldp
>>      = (ULONGEST *) xml_find_attribute (attributes, "l_ld")->value.get ();
>>  
>> -  so_list *new_elem = new so_list;
>> -  lm_info_svr4 *li = new lm_info_svr4;
>> -  new_elem->lm_info = li;
>> +  lm_info_svr4_up li = gdb::make_unique<lm_info_svr4> ();
>>    li->lm_addr = *lmp;
>>    li->l_addr_inferior = *l_addrp;
>>    li->l_ld = *l_ldp;
> 
> Just a thought for future changes to continue the c++-ification, adding
> a proper ctor (or ctors) to lm_info_svr4 would probably make things
> cleaner.
> 
> Otherwise, things looks reasonable to me.

Ok, I'll look into doing a patch on top instead of inserting one in the
middle, since I don't want to deal with conflicts again.

Simon

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 16/24] gdb: make clear_so a method of struct so_list
  2023-10-19 11:08   ` Lancelot SIX
@ 2023-10-19 14:52     ` Simon Marchi
  0 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-19 14:52 UTC (permalink / raw)
  To: Lancelot SIX, Simon Marchi; +Cc: gdb-patches

On 10/19/23 07:08, Lancelot SIX wrote:
> Hi Simon,
> 
> On Tue, Oct 10, 2023 at 04:40:11PM -0400, Simon Marchi wrote:
>> ... just because it seems to make sense to do so.
>>
>> Change-Id: Ie283c92d9b90c54e3deee96a43c6a942d8b5910b
>> ---
>>  gdb/solib.c  | 35 ++++++++++++++---------------------
>>  gdb/solist.h | 10 ++++++++++
>>  2 files changed, 24 insertions(+), 21 deletions(-)
>>
>> diff --git a/gdb/solib.c b/gdb/solib.c
>> index 5ed223b8dfca..8f5b6e02e71c 100644
>> --- a/gdb/solib.c
>> +++ b/gdb/solib.c
>> @@ -623,39 +623,32 @@ solib_map_sections (so_list &so)
>>    return 1;
>>  }
>>  
>> -/* Free symbol-file related contents of SO and reset for possible reloading
>> -   of SO.  If we have opened a BFD for SO, close it.  If we have placed SO's
>> -   sections in some target's section table, the caller is responsible for
>> -   removing them.
>> +/* See solist.h.  */
>>  
>> -   This function doesn't mess with objfiles at all.  If there is an
>> -   objfile associated with SO that needs to be removed, the caller is
>> -   responsible for taking care of that.  */
>> -
>> -static void
>> -clear_so (so_list &so)
>> +void
>> +so_list::clear ()
>>  {
>>    const target_so_ops *ops = gdbarch_so_ops (current_inferior ()->arch ());
>>  
>> -  delete so.sections;
>> -  so.sections = NULL;
>> +  delete this->sections;
>> +  this->sections = NULL;
> 
> That's just a nit, but while at touching those lines, the NULL could be
> changed to nullptr.

Done.

Simon

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 20/24] gdb: make so_list::{so_original_name,so_name} std::strings
  2023-10-19 11:08   ` [PATCH 20/24] gdb: make so_list::{so_original_name,so_name} std::strings Lancelot SIX
@ 2023-10-19 14:55     ` Simon Marchi
  0 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-19 14:55 UTC (permalink / raw)
  To: Lancelot SIX, Simon Marchi; +Cc: gdb-patches

On 10/19/23 07:08, Lancelot SIX wrote:
> Hi Simon,
> 
> Some other minor NULL -> nullptr possible conversions below.
> 
> On Tue, Oct 10, 2023 at 04:40:15PM -0400, Simon Marchi wrote:
>> Change these two fields, simplifying memory management and copying.
>>
>> Change-Id: If2559284c515721e71e1ef56ada8b64667eebe55
>> ---
>>  gdb/break-catch-load.c |  2 +-
>>  gdb/breakpoint.c       |  2 +-
>>  gdb/bsd-uthread.c      |  8 +++----
>>  gdb/mi/mi-cmd-file.c   |  2 +-
>>  gdb/solib-aix.c        |  8 +++----
>>  gdb/solib-darwin.c     |  5 ++---
>>  gdb/solib-dsbt.c       |  5 ++---
>>  gdb/solib-frv.c        |  6 ++----
>>  gdb/solib-rocm.c       |  8 ++-----
>>  gdb/solib-svr4.c       | 25 ++++++++++------------
>>  gdb/solib-target.c     | 18 ++++++----------
>>  gdb/solib.c            | 47 +++++++++++++++++++++++-------------------
>>  gdb/solist.h           |  4 ++--
>>  13 files changed, 63 insertions(+), 77 deletions(-)
>>
>> diff --git a/gdb/break-catch-load.c b/gdb/break-catch-load.c
>> index 94d8b420d327..999de874d5f6 100644
>> --- a/gdb/break-catch-load.c
>> +++ b/gdb/break-catch-load.c
>> @@ -120,7 +120,7 @@ solib_catchpoint::check_status (struct bpstat *bs)
>>        for (so_list *iter : current_program_space->added_solibs)
>>  	{
>>  	  if (!regex
>> -	      || compiled->exec (iter->so_name, 0, NULL, 0) == 0)
>> +	      || compiled->exec (iter->so_name.c_str (), 0, NULL, 0) == 0)
> 
> While at touching this line, s/NULL/nullptr.
> 
>>  	    return;
>>  	}
>>      }
>> diff --git a/gdb/mi/mi-cmd-file.c b/gdb/mi/mi-cmd-file.c
>> index 822d3df40005..5f63ce4494cb 100644
>> --- a/gdb/mi/mi-cmd-file.c
>> +++ b/gdb/mi/mi-cmd-file.c
>> @@ -167,7 +167,7 @@ mi_cmd_file_list_shared_libraries (const char *command,
>>      {
>>        if (so->so_name[0] == '\0')
>>  	continue;
>> -      if (pattern != NULL && !re_exec (so->so_name))
>> +      if (pattern != NULL && !re_exec (so->so_name.c_str ()))
> 
> Same.
> 
>>  	continue;
>>  
>>        ui_out_emit_tuple tuple_emitter (uiout, NULL);
>> diff --git a/gdb/solib.c b/gdb/solib.c
>> index 6b7f0da0498d..1815b0910208 100644
>> --- a/gdb/solib.c
>> +++ b/gdb/solib.c
>> @@ -705,9 +708,10 @@ solib_read_symbols (so_list &so, symfile_add_flags flags)
>>  	      section_addr_info sap
>>  		= build_section_addr_info_from_section_table (so.sections);
>>  	      gdb_bfd_ref_ptr tmp_bfd = so.abfd;
>> -	      so.objfile = symbol_file_add_from_bfd (tmp_bfd, so.so_name,
>> -						      flags, &sap,
>> -						      OBJF_SHARED, NULL);
>> +	      so.objfile = symbol_file_add_from_bfd (tmp_bfd,
>> +						     so.so_name.c_str (),
>> +						     flags, &sap,
>> +						     OBJF_SHARED, NULL);
> 
> Same.

Done.

Simon

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 0/24] C++ification of struct so_list
  2023-10-19 11:09 ` [PATCH 0/24] " Lancelot SIX
@ 2023-10-19 14:57   ` Simon Marchi
  0 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-19 14:57 UTC (permalink / raw)
  To: Lancelot SIX, Simon Marchi; +Cc: gdb-patches

On 10/19/23 07:09, Lancelot SIX wrote:
> Hi Simon,
> 
> I went through the series again, sent a few extra small remarks, and
> FWIW, it looks reasonable to me.
> 
> I do agree with Pedro's point that "struct so" becomes a bit harder to
> search for, and "shobj" would have less false-positives, but I
> nevertheless ok with using "so".  If it turns out to be a hurdle, we
> can always change it later.

Since you both agree with each other, I'll go with shobj.

> 
> I have tested the AMDGPU part of the series (gdb.rocm/*.exp), both on
> top of master and downstream ROCgdb.
> 
> So with the various nits fixed:
> Reviewed-By: Lancelot Six <lancelot.six@amd.com>

Thanks for reviewing, I will push with all the small nits fixed.

Simon

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 4/24] gdb: replace some so_list parameters to use references
  2023-10-19 14:49     ` Simon Marchi
@ 2023-10-19 15:20       ` Lancelot SIX
  2023-10-19 16:07         ` Simon Marchi
  0 siblings, 1 reply; 56+ messages in thread
From: Lancelot SIX @ 2023-10-19 15:20 UTC (permalink / raw)
  To: Simon Marchi, Simon Marchi; +Cc: gdb-patches


>>> -  void on_solib_loaded (so_list *so) override;
>>> -  void on_solib_unloaded (so_list *so) override;
>>> +  void on_solib_loaded (const so_list &so) override;
>>> +  void on_solib_unloaded (const so_list &so) override;
>>
>> It is orthogonal to this change, but it would make sense for those
>> methods to be const as well.
>>
>> Doing this requires interp::interp_ui_out to be const as well (as done
>> in attached patch).
> 
> Why do you think the interp object should be const (which is the effect
> of marking the methods const)?  These methods are notifiers to let the
> interp know about certain events that occured.  Whether they should
> modify the state of the interpreter or not is up to the particular
> implementation of the interpreter.  Perhaps you're right, but I don't
> see it.
> 

It is just that current interps (MI really) are just forwarding 
notifications.  The event does not change the interp's state.
Does it has to be const? No. But usually I tend to make things const 
unless there is a reason not to.  That's all.

That being said, making only those const would become quite 
inconsistent with the rest of the interface.  So in the end, that was 
not a good idea!

> In your patch, you make interp::interp_ui_out const, but it still
> returns a non-const ui_out.  So when outputting something, the
> interpreter itself is not modified, but the ui_out behind it is.  I
> presume (sometimes) the ui_out needs to be non-const, since for things
> like pagination it clearly needs to keep a state.  Design-wise, is it
> "correct" to make a const intepreter able to return a non-const ui_out?
> 

I don't necessarily see that as an issue, the ui_out might need to 
maintain its own state.

Anyway, since it is not really desirable to change this part of the 
interface, I think this comment can be discarded.

>>>     void on_about_to_proceed () override;
>>>     void on_traceframe_changed (int tfnum, int tpnum) override;
>>>     void on_tsv_created (const trace_state_variable *tsv) override;
>>> diff --git a/gdb/observable.h b/gdb/observable.h
>>> index acb05e9b535c..5ed6ca547ce0 100644
>>> --- a/gdb/observable.h
>>> +++ b/gdb/observable.h
>>> @@ -99,13 +99,12 @@ extern observable<inferior */* parent_inf */, inferior */* child_inf */,
>>>   /* The shared library specified by SOLIB has been loaded.  Note that
>>>      when gdb calls this observer, the library's symbols probably
>>>      haven't been loaded yet.  */
>>> -extern observable<struct so_list */* solib */> solib_loaded;
>>> +extern observable<so_list &/* solib */> solib_loaded;
>>
>> I am wondering, is there a reason to make the solib parameter const for
>> solib_unloaded but not for solib_loaded?
>>
>> Changing it to const seems to still compile just fine.  If down the line
>> an observer needs to modify the SO, the observer's signature can be
>> adjusted.
> 
> Just pragmatic reasons.  Do you build with --enable-targets=all?
> 
> It's because bsd_uthread_solib_loaded calls solib_read_symbols to read
> in the symbols of the library it is looking for, and that requires a
> non-const so_list.
> 

Ah, indeed, my current build tree does not have --enable-targets=all, 
thanks.  Having a user requiring a non-const ref is a good reason.

Thanks,
Lancelot.

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 4/24] gdb: replace some so_list parameters to use references
  2023-10-19 15:20       ` Lancelot SIX
@ 2023-10-19 16:07         ` Simon Marchi
  0 siblings, 0 replies; 56+ messages in thread
From: Simon Marchi @ 2023-10-19 16:07 UTC (permalink / raw)
  To: Lancelot SIX, Simon Marchi; +Cc: gdb-patches

On 10/19/23 11:20, Lancelot SIX wrote:
> 
>>>> -  void on_solib_loaded (so_list *so) override;
>>>> -  void on_solib_unloaded (so_list *so) override;
>>>> +  void on_solib_loaded (const so_list &so) override;
>>>> +  void on_solib_unloaded (const so_list &so) override;
>>>
>>> It is orthogonal to this change, but it would make sense for those
>>> methods to be const as well.
>>>
>>> Doing this requires interp::interp_ui_out to be const as well (as done
>>> in attached patch).
>>
>> Why do you think the interp object should be const (which is the effect
>> of marking the methods const)?  These methods are notifiers to let the
>> interp know about certain events that occured.  Whether they should
>> modify the state of the interpreter or not is up to the particular
>> implementation of the interpreter.  Perhaps you're right, but I don't
>> see it.
>>
> 
> It is just that current interps (MI really) are just forwarding notifications.  The event does not change the interp's state.
> Does it has to be const? No. But usually I tend to make things const unless there is a reason not to.  That's all.

But if I make the method const for MI, I need to make it const for all
interps, including CLI and TUI, right?  So then we need to extend the
reflection to whether it makes sense to make the method const for them
too (hence my question about the non-const ui_out behind a const cli
interp).

Simon

^ permalink raw reply	[flat|nested] 56+ messages in thread

* Re: [PATCH 00/24] C++ification of struct so_list
  2023-10-17 19:53   ` Simon Marchi
@ 2023-10-20 14:40     ` Pedro Alves
  0 siblings, 0 replies; 56+ messages in thread
From: Pedro Alves @ 2023-10-20 14:40 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 2023-10-17 20:53, Simon Marchi wrote:
> On 10/17/23 15:20, Pedro Alves wrote:
>> On 2023-10-10 21:39, Simon Marchi wrote:
>>> This series modernizes the struct so_list area by C++ifying it a little
>>> bit and replacing the manual linked list implementation with
>>> intrusive_list.  It also contains a few other cleanups written along the
>>> way.
>>
>> Very nicely split.  Thanks.  I sent a few comments, but nothing too serious.
>> The only thing on my end that requires some churn is the pointer vs reference
>> thing in the disposer.  Otherwise, all LGTM.
> 
> I dropped the "use reference in disposer" patch.

Right, thanks.  I somehow thought that that would have some cascading effect
in the following patches.  That's what I meant by churn.


^ permalink raw reply	[flat|nested] 56+ messages in thread

end of thread, other threads:[~2023-10-20 14:40 UTC | newest]

Thread overview: 56+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-10 20:39 [PATCH 00/24] C++ification of struct so_list Simon Marchi
2023-10-10 20:39 ` [PATCH 01/24] gdb: remove empty clear_solib functions Simon Marchi
2023-10-10 20:39 ` [PATCH 02/24] gdb: add program_space parameter to target_so_ops::clear_solib Simon Marchi
2023-10-17 14:57   ` Pedro Alves
2023-10-17 15:19     ` Simon Marchi
2023-10-10 20:39 ` [PATCH 03/24] gdb: make interps_notify work with references Simon Marchi
2023-10-11  8:48   ` Lancelot SIX
2023-10-11 14:18     ` Simon Marchi
2023-10-10 20:39 ` [PATCH 04/24] gdb: replace some so_list parameters to use references Simon Marchi
2023-10-19 11:07   ` [PATCH 4/24] " Lancelot SIX
2023-10-19 14:49     ` Simon Marchi
2023-10-19 15:20       ` Lancelot SIX
2023-10-19 16:07         ` Simon Marchi
2023-10-10 20:40 ` [PATCH 05/24] gdbsupport: use "reference" and "pointer" type aliases in intrusive_list Simon Marchi
2023-10-10 20:40 ` [PATCH 06/24] gdbsupport: make intrusive_list's disposer accept a reference Simon Marchi
2023-10-12 19:05   ` Pedro Alves
2023-10-14 20:12     ` Simon Marchi
2023-10-10 20:40 ` [PATCH 07/24] gdb: make get_cbfd_soname_build_id static Simon Marchi
2023-10-10 20:40 ` [PATCH 08/24] gdb: allocate so_list with new, deallocate with delete Simon Marchi
2023-10-10 20:40 ` [PATCH 09/24] gdb: rename lm_info_base to lm_info Simon Marchi
2023-10-10 20:40 ` [PATCH 10/24] gdb: remove target_so_ops::free_so Simon Marchi
2023-10-10 20:40 ` [PATCH 11/24] gdb: use gdb::checked_static_cast when casting lm_info Simon Marchi
2023-10-10 20:40 ` [PATCH 12/24] gdb: make solib-svr4 not use so_list internally Simon Marchi
2023-10-13 17:52   ` Lancelot SIX
2023-10-14 19:59     ` Simon Marchi
2023-10-19 11:08   ` Lancelot SIX
2023-10-19 14:50     ` Simon Marchi
2023-10-10 20:40 ` [PATCH 13/24] gdb: make solib-rocm " Simon Marchi
2023-10-13 18:35   ` Lancelot SIX
2023-10-14 20:00     ` Simon Marchi
2023-10-17 15:23   ` Pedro Alves
2023-10-17 15:32     ` Simon Marchi
2023-10-10 20:40 ` [PATCH 14/24] gdb: remove lm_info_vector typedef Simon Marchi
2023-10-10 20:40 ` [PATCH 15/24] gdb: make so_list::lm_info a unique_ptr Simon Marchi
2023-10-10 20:40 ` [PATCH 16/24] gdb: make clear_so a method of struct so_list Simon Marchi
2023-10-19 11:08   ` Lancelot SIX
2023-10-19 14:52     ` Simon Marchi
2023-10-10 20:40 ` [PATCH 17/24] gdb: remove target_section_table typedef Simon Marchi
2023-10-10 20:40 ` [PATCH 18/24] gdb: make so_list::sections not a pointer Simon Marchi
2023-10-10 20:40 ` [PATCH 19/24] gdb: make so_list::abfd a gdb_bfd_ref_ptr Simon Marchi
2023-10-10 20:40 ` [PATCH 20/24] gdb: make so_list::{so_original_name,so_name} std::strings Simon Marchi
2023-10-13 22:28   ` [PATCH 20/24] gdb: make so_list::{so_original_name, so_name} std::strings Lancelot SIX
2023-10-14 20:01     ` Simon Marchi
2023-10-19 11:08   ` [PATCH 20/24] gdb: make so_list::{so_original_name,so_name} std::strings Lancelot SIX
2023-10-19 14:55     ` Simon Marchi
2023-10-10 20:40 ` [PATCH 21/24] gdb: link so_list using intrusive_list Simon Marchi
2023-10-17 19:14   ` Pedro Alves
2023-10-17 19:38     ` Simon Marchi
2023-10-10 20:40 ` [PATCH 22/24] gdb: don't call so_list::clear in free_so Simon Marchi
2023-10-10 20:40 ` [PATCH 23/24] gdb: remove free_so function Simon Marchi
2023-10-10 20:49 ` [PATCH 24/24] gdb: rename struct so_list to so Simon Marchi
2023-10-17 19:20 ` [PATCH 00/24] C++ification of struct so_list Pedro Alves
2023-10-17 19:53   ` Simon Marchi
2023-10-20 14:40     ` Pedro Alves
2023-10-19 11:09 ` [PATCH 0/24] " Lancelot SIX
2023-10-19 14:57   ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).