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 09/14] implement RTLD_NORELOCATE dlopen() flag
Date: Thu, 18 May 2023 13:28:49 +0500	[thread overview]
Message-ID: <20230518082854.3903342-10-stsp2@yandex.ru> (raw)
In-Reply-To: <20230518082854.3903342-1-stsp2@yandex.ru>

This flag allows to delay the relocation of the dlopen()ed object.
If this flag is used, then the relocation is called from
_dl_lookup_symbol_x(), which is called by dlsym() among other places.

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

Signed-off-by: Stas Sergeev <stsp2@yandex.ru>
---
 bits/dlfcn.h    |  3 +++
 dlfcn/dlopen.c  |  2 +-
 elf/dl-lookup.c |  6 +++++-
 elf/dl-main.h   |  2 ++
 elf/dl-open.c   | 22 +++++++++++++++++++---
 include/link.h  |  1 +
 6 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/bits/dlfcn.h b/bits/dlfcn.h
index d1e31cf4e0..9cf2d5fb80 100644
--- a/bits/dlfcn.h
+++ b/bits/dlfcn.h
@@ -41,6 +41,9 @@
 #define RTLD_NODELETE	0x01000
 
 #ifdef __USE_GNU
+/* Do not relocte object on dlopen().  */
+#define RTLD_NORELOCATE	0x02000
+
 /* To support profiling of shared objects it is a good idea to call
    the function found using `dlsym' using the following macro since
    these calls do not use the PLT.  But this would mean the dynamic
diff --git a/dlfcn/dlopen.c b/dlfcn/dlopen.c
index 21ed2c964d..866ccf7a4a 100644
--- a/dlfcn/dlopen.c
+++ b/dlfcn/dlopen.c
@@ -50,7 +50,7 @@ dlopen_doit (void *a)
 
   if (args->mode & ~(RTLD_BINDING_MASK | RTLD_NOLOAD | RTLD_DEEPBIND
 		     | RTLD_GLOBAL | RTLD_LOCAL | RTLD_NODELETE
-		     | __RTLD_SPROF))
+		     | __RTLD_SPROF | RTLD_NORELOCATE))
     _dl_signal_error (0, NULL, NULL, _("invalid mode parameter"));
 
   args->new = GLRO(dl_open) (args->file ?: "", args->mode | __RTLD_DLOPEN,
diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c
index 05f36a2507..d60481676d 100644
--- a/elf/dl-lookup.c
+++ b/elf/dl-lookup.c
@@ -31,7 +31,7 @@
 #include <tls.h>
 #include <atomic.h>
 #include <elf_machine_sym_no_match.h>
-
+#include <dl-main.h>
 #include <assert.h>
 
 #define VERSTAG(tag)	(DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
@@ -759,6 +759,10 @@ _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
   struct sym_val current_value = { NULL, NULL };
   struct r_scope_elem **scope = symbol_scope;
 
+  if (undef_map && !undef_map->l_relocated && undef_map->l_reloc_deferred
+      && undef_map->l_type == lt_loaded)
+    _dl_object_reloc (undef_map);
+
   bump_num_relocations ();
 
   /* DL_LOOKUP_RETURN_NEWEST does not make sense for versioned
diff --git a/elf/dl-main.h b/elf/dl-main.h
index 92766d06b4..54d139f2c4 100644
--- a/elf/dl-main.h
+++ b/elf/dl-main.h
@@ -127,4 +127,6 @@ _Noreturn void _dl_help (const char *argv0, struct dl_main_state *state)
 /* Print a diagnostics dump.  */
 _Noreturn void _dl_print_diagnostics (char **environ) attribute_hidden;
 
+extern void _dl_object_reloc (struct link_map *l) attribute_hidden;
+
 #endif /* _DL_MAIN */
diff --git a/elf/dl-open.c b/elf/dl-open.c
index f1f2e8d3a4..5106c9d029 100644
--- a/elf/dl-open.c
+++ b/elf/dl-open.c
@@ -223,6 +223,8 @@ _dl_find_dso_for_object (const ElfW(Addr) addr)
 	      || _dl_addr_inside_object (l, (ElfW(Addr)) addr)))
 	{
 	  assert (ns == l->l_ns);
+	  if (!l->l_relocated)
+	    return NULL;
 	  return l;
 	}
   return NULL;
@@ -681,7 +683,7 @@ dl_reloc_worker_begin (void *a)
   do_reloc_1 (args->map, args->mode, args->nsid, !args->libc_already_loaded);
 }
 
-static void
+void
 _dl_object_reloc (struct link_map *l)
 {
   struct dl_exception ex;
@@ -689,6 +691,8 @@ _dl_object_reloc (struct link_map *l)
   struct dl_open_args *args = l->l_dlopen_args;
   int mode = args->mode;
 
+  l->l_reloc_deferred = 0;
+
   /* Protects global and module specific TLS state.  */
   __rtld_lock_lock_recursive (GL(dl_load_tls_lock));
   err = _dl_catch_exception (&ex, dl_reloc_worker_begin, args);
@@ -760,6 +764,10 @@ dl_open_worker_begin (void *a)
     /* This happens only if we load a DSO for 'sprof'.  */
     return;
 
+  if (__glibc_unlikely ((mode & RTLD_NORELOCATE) && new->l_relocated))
+    _dl_signal_error (EINVAL, new->l_name, NULL,
+		      N_("RTLD_NORELOCATE used with already relocated object"));
+
   /* This object is directly loaded.  */
   ++new->l_direct_opencount;
 
@@ -821,7 +829,12 @@ dl_open_worker_begin (void *a)
           memcpy (new->l_dlopen_args, args, sizeof (*args));
         }
       else
-        new->l_dlopen_args = args;
+        {
+          assert (new->l_relocated);
+          /* If relocated, this flag is filtered above. */
+          assert (!(mode & RTLD_NORELOCATE));
+          new->l_dlopen_args = args;
+        }
     }
   else
     {
@@ -873,7 +886,10 @@ dl_open_worker (void *a)
 
   struct link_map *new = args->map;
 
-  _dl_object_reloc (new);
+  if (__glibc_likely (!(args->mode & RTLD_NORELOCATE)))
+    _dl_object_reloc (new);
+  else
+    new->l_reloc_deferred = 1;
   /* For !lt_loaded we do not malloc(), so needs to null out here. */
   if (new->l_type != lt_loaded)
     new->l_dlopen_args = NULL;
diff --git a/include/link.h b/include/link.h
index fa16dfa337..6c11c33417 100644
--- a/include/link.h
+++ b/include/link.h
@@ -180,6 +180,7 @@ struct link_map
     unsigned int l_dt_relr_ref:1; /* Nonzero if GLIBC_ABI_DT_RELR is
 				     referenced.  */
     unsigned int l_map_completed:1; /* Nonzero if object fully mapped.  */
+    unsigned int l_reloc_deferred:1; /* Nonzero if relocation deferred.  */
     unsigned int l_relocated:1;	/* Nonzero if object's relocations done.  */
     unsigned int l_init_called:1; /* Nonzero if DT_INIT function called.  */
     unsigned int l_global:1;	/* Nonzero if object in _dl_global_scope.  */
-- 
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 ` Stas Sergeev [this message]
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 ` [PATCH 14/14] implement RTLD_DI_DEPLIST dlinfo() request Stas Sergeev

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-10-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).