`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(-)
@@ -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)
@@ -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)++;
}
@@ -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;
@@ -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;
+ }
}
}
@@ -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);