diff mbox series

[v5,2/3] elf: Introduce _dl_debug_change_state

Message ID 559fd57392e89e3a7354511bb363f1f37de7764e.1688727287.git.fweimer@redhat.com
State New
Headers show
Series Restore support for _r_debug copy relocations, interposition | expand

Commit Message

Florian Weimer July 7, 2023, 10:55 a.m. UTC
It combines updating r_state with the debugger notification.

The change to  _dl_open introduces an additional debugger notification
for dlmopen, but debuggers are expected to ignore it.
---
 elf/dl-close.c             |  6 ++----
 elf/dl-debug.c             |  7 +++++++
 elf/dl-load.c              |  3 +--
 elf/dl-open.c              |  5 ++---
 elf/rtld.c                 |  6 ++----
 sysdeps/generic/ldsodefs.h | 14 ++++++++++++--
 6 files changed, 26 insertions(+), 15 deletions(-)
diff mbox series

Patch

diff --git a/elf/dl-close.c b/elf/dl-close.c
index b887a44888..27fd187385 100644
--- a/elf/dl-close.c
+++ b/elf/dl-close.c
@@ -420,8 +420,7 @@  _dl_close_worker (struct link_map *map, bool force)
 
   /* Notify the debugger we are about to remove some loaded objects.  */
   struct r_debug *r = _dl_debug_update (nsid);
-  r->r_state = RT_DELETE;
-  _dl_debug_state ();
+  _dl_debug_change_state (r, RT_DELETE);
   LIBC_PROBE (unmap_start, 2, nsid, r);
 
   if (unload_global)
@@ -725,8 +724,7 @@  _dl_close_worker (struct link_map *map, bool force)
     while (GL(dl_ns)[GL(dl_nns) - 1]._ns_loaded == NULL);
 
   /* Notify the debugger those objects are finalized and gone.  */
-  r->r_state = RT_CONSISTENT;
-  _dl_debug_state ();
+  _dl_debug_change_state (r, RT_CONSISTENT);
   LIBC_PROBE (unmap_complete, 2, nsid, r);
 
   /* Recheck if we need to retry, release the lock.  */
diff --git a/elf/dl-debug.c b/elf/dl-debug.c
index 88cccfa756..9d05e553d7 100644
--- a/elf/dl-debug.c
+++ b/elf/dl-debug.c
@@ -67,6 +67,13 @@  _dl_debug_update (Lmid_t ns)
   return &r->base;
 }
 
+void
+_dl_debug_change_state (struct r_debug *r, int state)
+{
+  atomic_store_release (&r->r_state, state);
+  _dl_debug_state ();
+}
+
 /* Initialize _r_debug_extended for the namespace NS.  LDBASE is the
    run-time load address of the dynamic linker, to be put in
    _r_debug_extended.r_ldbase.  Return the address of _r_debug.  */
diff --git a/elf/dl-load.c b/elf/dl-load.c
index 9a87fda9c9..4a5fd71f1a 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -1502,8 +1502,7 @@  cannot enable executable stack as shared object requires");
       /* Notify the debugger we have added some objects.  We need to
 	 call _dl_debug_initialize in a static program in case dynamic
 	 linking has not been used before.  */
-      r->r_state = RT_ADD;
-      _dl_debug_state ();
+      _dl_debug_change_state (r, RT_ADD);
       LIBC_PROBE (map_start, 2, nsid, r);
     }
   else
diff --git a/elf/dl-open.c b/elf/dl-open.c
index 2d985e21d8..02235f7e0b 100644
--- a/elf/dl-open.c
+++ b/elf/dl-open.c
@@ -619,8 +619,7 @@  dl_open_worker_begin (void *a)
 
   /* Notify the debugger all new objects are now ready to go.  */
   struct r_debug *r = _dl_debug_update (args->nsid);
-  r->r_state = RT_CONSISTENT;
-  _dl_debug_state ();
+  _dl_debug_change_state (r, RT_CONSISTENT);
   LIBC_PROBE (map_complete, 3, args->nsid, r, new);
 
   _dl_open_check (new);
@@ -853,7 +852,7 @@  no more namespaces available for dlmopen()"));
 	}
 
       GL(dl_ns)[nsid].libc_map = NULL;
-      _dl_debug_update (nsid)->r_state = RT_CONSISTENT;
+      _dl_debug_change_state (_dl_debug_update (nsid), RT_CONSISTENT);
     }
   /* Never allow loading a DSO in a namespace which is empty.  Such
      direct placements is only causing problems.  Also don't allow
diff --git a/elf/rtld.c b/elf/rtld.c
index a91e2a4471..77eea867c9 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -1826,8 +1826,7 @@  dl_main (const ElfW(Phdr) *phdr,
   elf_setup_debug_entry (main_map, r);
 
   /* We start adding objects.  */
-  r->r_state = RT_ADD;
-  _dl_debug_state ();
+  _dl_debug_change_state (r, RT_ADD);
   LIBC_PROBE (init_start, 2, LM_ID_BASE, r);
 
   /* Auditing checkpoint: we are ready to signal that the initial map
@@ -2392,8 +2391,7 @@  dl_main (const ElfW(Phdr) *phdr,
   /* Notify the debugger all new objects are now ready to go.  We must re-get
      the address since by now the variable might be in another object.  */
   r = _dl_debug_update (LM_ID_BASE);
-  r->r_state = RT_CONSISTENT;
-  _dl_debug_state ();
+  _dl_debug_change_state (r, RT_CONSISTENT);
   LIBC_PROBE (init_complete, 2, LM_ID_BASE, r);
 
 #if defined USE_LDCONFIG && !defined MAP_COPY
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
index 55597a268e..99cc78b8e4 100644
--- a/sysdeps/generic/ldsodefs.h
+++ b/sysdeps/generic/ldsodefs.h
@@ -1058,8 +1058,14 @@  extern void _dl_debug_state (void);
 rtld_hidden_proto (_dl_debug_state)
 
 /* Initialize `struct r_debug_extended' for the namespace NS.  LDBASE
-   is the run-time load address of the dynamic linker, to be put in the
-   `r_ldbase' member.  Return the address of the structure.  */
+   is the run-time load address of the dynamic linker, to be put in
+   the `r_ldbase' member.
+
+   This function returns the address of the r_debug structure for the
+   namespace.  This is not merely a convenience or optimization, but
+   it is necessary for the LIBC_PROBE Systemtap/debugger probes to
+   work reliably: direct variable access can create probes that tools
+   cannot consume.  */
 extern struct r_debug *_dl_debug_initialize (ElfW(Addr) ldbase, Lmid_t ns)
      attribute_hidden;
 
@@ -1067,6 +1073,10 @@  extern struct r_debug *_dl_debug_initialize (ElfW(Addr) ldbase, Lmid_t ns)
    of the namespace NS.  */
 extern struct r_debug *_dl_debug_update (Lmid_t ns) attribute_hidden;
 
+/* Updates R->r_state to STATE and notifies the debugger by calling
+   _dl_debug_state.  */
+void _dl_debug_change_state (struct r_debug *r, int state) attribute_hidden;
+
 /* Initialize the basic data structure for the search paths.  SOURCE
    is either "LD_LIBRARY_PATH" or "--library-path".
    GLIBC_HWCAPS_PREPEND adds additional glibc-hwcaps subdirectories to