public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Stas Sergeev <stsp2@yandex.ru>
To: libc-alpha@sourceware.org
Cc: Stas Sergeev <stsp2@yandex.ru>
Subject: [PATCH 14/14] implement RTLD_DI_DEPLIST dlinfo() request
Date: Thu, 18 May 2023 13:28:54 +0500	[thread overview]
Message-ID: <20230518082854.3903342-15-stsp2@yandex.ru> (raw)
In-Reply-To: <20230518082854.3903342-1-stsp2@yandex.ru>

`RTLD_DI_DEPLIST' is a new dlinfo() request that fills in this structure:
typedef struct
{
  void **deps;                  /* Array of handles for the deps.  */
  unsigned int ndeps;           /* Number of entries in the list.  */
} Dl_deplist;

It is needed if the user wants to move also the dependencies of the
loaded solib. In this case he needs to traverse the `deps' array,
make RTLD_DI_MAPINFO dlinfo() request per each handle from an array,
find the object he needs by inspecting the filled-in Dl_mapinfo structure,
make sure this object is not relocated yet, and move it, calling
dlset_object_base() at the end.

The test-suite was run on x86_64/64 and showed no regressions.

Signed-off-by: Stas Sergeev <stsp2@yandex.ru>
---
 dlfcn/Makefile      |  3 +++
 dlfcn/ctorlib1.c    |  9 ++++++---
 dlfcn/dlfcn.h       | 13 ++++++++++++-
 dlfcn/dlinfo.c      | 14 ++++++++++++++
 dlfcn/tst-noreloc.c | 30 +++++++++++++++++++++++++++---
 5 files changed, 62 insertions(+), 7 deletions(-)

diff --git a/dlfcn/Makefile b/dlfcn/Makefile
index da486a9b01..0d6b16c820 100644
--- a/dlfcn/Makefile
+++ b/dlfcn/Makefile
@@ -104,6 +104,9 @@ $(objpfx)glrefmain.out: $(objpfx)glrefmain \
 $(objpfx)failtest.out: $(objpfx)failtestmod.so
 
 $(objpfx)tst-dladdr.out: $(objpfx)glreflib1.so
+
+$(objpfx)ctorlib1.so: $(objpfx)glreflib1.so
+LDFLAGS-ctorlib1 = $(objpfx)glreflib1.so
 $(objpfx)tst-noreloc.out: $(objpfx)ctorlib1.so
 LDFLAGS-tst-noreloc = $(LDFLAGS-rdynamic)
 
diff --git a/dlfcn/ctorlib1.c b/dlfcn/ctorlib1.c
index cf7a9096ea..7d410f6c71 100644
--- a/dlfcn/ctorlib1.c
+++ b/dlfcn/ctorlib1.c
@@ -21,10 +21,12 @@
 #include <stdio.h>
 
 extern int ref1 (void);
+
+extern int cref1 (void);
 int
-ref1 (void)
+cref1 (void)
 {
-  return 42;
+  return 34;
 }
 
 void __attribute__((constructor))
@@ -32,5 +34,6 @@ ctor (void)
 {
   int *ct = (int *) dlsym (RTLD_DEFAULT, "ctor_called");
   assert (ct);
-  (*ct)++;
+  if (ref1 () == 42)
+    (*ct)++;
 }
diff --git a/dlfcn/dlfcn.h b/dlfcn/dlfcn.h
index eee6343833..5b36330aa9 100644
--- a/dlfcn/dlfcn.h
+++ b/dlfcn/dlfcn.h
@@ -179,7 +179,12 @@ enum
        -1 on failure.  */
     RTLD_DI_MAPINFO = 12,
 
-    RTLD_DI_MAX = 12
+    /* Treat ARG as Dl_deplist *, and store the dependency link-maps
+       at that location.  The dlinfo call returns 0 on success or
+       -1 on failure.  */
+    RTLD_DI_DEPLIST = 13,
+
+    RTLD_DI_MAX = 13
   };
 
 
@@ -220,6 +225,12 @@ typedef struct
   int relocated;		/* Indicates whether an object was relocated. */
 } Dl_mapinfo;
 
+typedef struct
+{
+  void **deps;			/* Array of handles for the deps.  */
+  unsigned int ndeps;		/* Number of entries in the list.  */
+} Dl_deplist;
+
 struct dl_find_object
 {
   __extension__ unsigned long long int dlfo_flags;
diff --git a/dlfcn/dlinfo.c b/dlfcn/dlinfo.c
index 8d7db7dbb8..4eb96bc476 100644
--- a/dlfcn/dlinfo.c
+++ b/dlfcn/dlinfo.c
@@ -18,6 +18,7 @@
 
 #include <dlfcn.h>
 #include <link.h>
+#include <assert.h>
 #include <ldsodefs.h>
 #include <libintl.h>
 #include <dl-tls.h>
@@ -103,6 +104,19 @@ dlinfo_doit (void *argsblock)
 	__rtld_lock_unlock_recursive (GL(dl_load_lock));
 	break;
       }
+
+    case RTLD_DI_DEPLIST:
+      {
+	int i = 0;
+	Dl_deplist *list = (Dl_deplist *) args->arg;
+	assert (l == l->l_initfini[0]);
+	list->deps = (void **) &l->l_initfini[1];  /* Skip our own handle. */
+	while (list->deps[i])
+	  i++;
+	list->ndeps = i;
+	args->result = 0;
+	break;
+      }
     }
 }
 
diff --git a/dlfcn/tst-noreloc.c b/dlfcn/tst-noreloc.c
index 96c05a4b06..846d2b9321 100644
--- a/dlfcn/tst-noreloc.c
+++ b/dlfcn/tst-noreloc.c
@@ -29,14 +29,17 @@
 
 int ctor_called;
 
-static void move_object (void *handle)
+static void move_object (void *handle, int is_dep)
 {
   int ret;
   Dl_mapinfo mapinfo;
   void *new_addr;
 
   ret = dlinfo (handle, RTLD_DI_MAPINFO, &mapinfo);
-  TEST_COMPARE (ret, 0);
+  if (!is_dep)
+    TEST_COMPARE (ret, 0);
+  if (is_dep && (ret == -1 || mapinfo.relocated))
+    return;
   TEST_COMPARE (mapinfo.relocated, 0);
   if (mapinfo.map_align != getpagesize ())
     error (EXIT_FAILURE, 0, "unsupported map alignment");
@@ -59,7 +62,10 @@ do_test (void)
   void *handle;
   int (*sym) (void);
   Dl_info info;
+  Dl_info info2;
+  Dl_deplist deplist;
   int ret;
+  int i;
 
   handle = dlopen ("ctorlib1.so", RTLD_NOW | RTLD_NORELOCATE);
   if (handle == NULL)
@@ -97,7 +103,11 @@ do_test (void)
   if (handle == NULL)
     error (EXIT_FAILURE, 0, "cannot load: ctorlib1.so");
 
-  move_object (handle);
+  move_object (handle, 0);
+  ret = dlinfo (handle, RTLD_DI_DEPLIST, &deplist);
+  TEST_COMPARE (ret, 0);
+  for (i = 0; i < deplist.ndeps; i++)
+    move_object (deplist.deps[i], 1);
 
   TEST_COMPARE (ctor_called, 0);
   sym = dlsym (handle, "ref1");
@@ -113,6 +123,20 @@ do_test (void)
   TEST_VERIFY ((uintptr_t) info.dli_fbase < 0x100000000);
 #endif
 
+  sym = dlsym (handle, "cref1");
+  if (sym == NULL)
+    error (EXIT_FAILURE, 0, "dlsym failed");
+  TEST_COMPARE (ctor_called, 1);
+
+  memset (&info2, 0, sizeof (info2));
+  ret = dladdr (sym, &info2);
+  if (ret == 0)
+    error (EXIT_FAILURE, 0, "dladdr failed");
+#if MAP_32BIT != 0
+  TEST_VERIFY ((uintptr_t) info2.dli_fbase < 0x100000000);
+#endif
+  TEST_VERIFY (info2.dli_fbase != info.dli_fbase);
+
   printf ("ret = %d\n", ret);
   printf ("info.dli_fname = %p (\"%s\")\n", info.dli_fname, info.dli_fname);
   printf ("info.dli_fbase = %p\n", info.dli_fbase);
-- 
2.39.2


      parent reply	other threads:[~2023-05-18  8:29 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-18  8:28 [PATCH 00/14] implement RTLD_NORELOCATE api [BZ #30007] Stas Sergeev
2023-05-18  8:28 ` [PATCH 01/14] elf: switch _dl_map_segment() to anonymous mapping Stas Sergeev
2023-05-18  8:28 ` [PATCH 02/14] use initial mmap also for ET_EXEC Stas Sergeev
2023-05-18  8:28 ` [PATCH 03/14] rework maphole Stas Sergeev
2023-05-18  8:28 ` [PATCH 04/14] split do_reloc_1() from dl_open_worker_begin() Stas Sergeev
2023-05-18  8:28 ` [PATCH 05/14] split do_reloc_2() out of do_open_worker() Stas Sergeev
2023-05-18  8:28 ` [PATCH 06/14] move relocation into _dl_object_reloc() func Stas Sergeev
2023-05-18  8:28 ` [PATCH 07/14] split out _dl_finalize_segments() Stas Sergeev
2023-05-18  8:28 ` [PATCH 08/14] finalize elf segments on a relocation step Stas Sergeev
2023-05-18  8:28 ` [PATCH 09/14] implement RTLD_NORELOCATE dlopen() flag Stas Sergeev
2023-05-18  8:28 ` [PATCH 10/14] add test-case for RTLD_NORELOCATE Stas Sergeev
2023-05-18  8:28 ` [PATCH 11/14] implement dlrelocate() function Stas Sergeev
2023-05-18  8:28 ` [PATCH 12/14] implement RTLD_DI_MAPINFO dlinfo() request Stas Sergeev
2023-05-18  8:28 ` [PATCH 13/14] implement dlset_object_base() function Stas Sergeev
2023-05-18  8:28 ` Stas Sergeev [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230518082854.3903342-15-stsp2@yandex.ru \
    --to=stsp2@yandex.ru \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).