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 06/14] move relocation into _dl_object_reloc() func
Date: Thu, 18 May 2023 13:28:46 +0500	[thread overview]
Message-ID: <20230518082854.3903342-7-stsp2@yandex.ru> (raw)
In-Reply-To: <20230518082854.3903342-1-stsp2@yandex.ru>

This is a small refactoring to consolidate the relocation code
in a single place.

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

Signed-off-by: Stas Sergeev <stsp2@yandex.ru>
---
 elf/dl-close.c |  2 ++
 elf/dl-open.c  | 55 +++++++++++++++++++++++++++++++++++++++++++++-----
 include/link.h |  2 ++
 3 files changed, 54 insertions(+), 5 deletions(-)

diff --git a/elf/dl-close.c b/elf/dl-close.c
index b887a44888..48ac3663ec 100644
--- a/elf/dl-close.c
+++ b/elf/dl-close.c
@@ -690,6 +690,8 @@ _dl_close_worker (struct link_map *map, bool force)
 	  if (imap == GL(dl_initfirst))
 	    GL(dl_initfirst) = NULL;
 
+	  free (imap->l_dlopen_args);
+
 	  free (imap);
 	}
     }
diff --git a/elf/dl-open.c b/elf/dl-open.c
index 687a393e0d..e553403e8b 100644
--- a/elf/dl-open.c
+++ b/elf/dl-open.c
@@ -666,6 +666,31 @@ do_reloc_2 (struct link_map *new, int mode, struct dl_open_args *args)
     add_to_global_update (new);
 }
 
+static void
+dl_reloc_worker_begin (void *a)
+{
+  struct dl_open_args *args = a;
+  do_reloc_1 (args->map, args->mode, args->nsid, !args->libc_already_loaded);
+}
+
+static void
+_dl_object_reloc (struct link_map *l)
+{
+  struct dl_exception ex;
+  int err;
+  struct dl_open_args *args = l->l_dlopen_args;
+  int mode = args->mode;
+
+  /* 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);
+  __rtld_lock_unlock_recursive (GL(dl_load_tls_lock));
+  if (__glibc_unlikely (ex.errstring != NULL))
+    /* Reraise the error.  */
+    _dl_signal_exception (err, &ex, NULL);
+  do_reloc_2 (l, mode, args);
+}
+
 static void
 dl_open_worker_begin (void *a)
 {
@@ -775,6 +800,27 @@ dl_open_worker_begin (void *a)
   _dl_map_object_deps (new, NULL, 0, 0,
 		       mode & (__RTLD_DLOPEN | RTLD_DEEPBIND | __RTLD_AUDIT));
 
+  args->worker_continue = true;
+  if (!new->l_dlopen_args)
+    {
+      if (new->l_type == lt_loaded)
+        {
+          new->l_dlopen_args = (struct dl_open_args *)
+                               malloc (sizeof (struct dl_open_args));
+          if (new->l_dlopen_args == NULL)
+            _dl_signal_error (ENOMEM, new->l_name, NULL,
+			      N_("cannot allocate memory buffer"));
+          memcpy (new->l_dlopen_args, args, sizeof (*args));
+        }
+      else
+        new->l_dlopen_args = args;
+    }
+  else
+    {
+      assert (new->l_type == lt_loaded);
+      memcpy (new->l_dlopen_args, args, sizeof (*args));
+    }
+
 #ifdef SHARED
   /* Auditing checkpoint: we have added all objects.  */
   _dl_audit_activity_nsid (new->l_ns, LA_ACT_CONSISTENT);
@@ -791,9 +837,6 @@ dl_open_worker_begin (void *a)
   /* Print scope information.  */
   if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES))
     _dl_show_scope (new, 0);
-
-  args->worker_continue = true;
-  do_reloc_1 (new, mode, args->nsid, !args->libc_already_loaded);
 }
 
 static void
@@ -820,10 +863,12 @@ dl_open_worker (void *a)
   if (!args->worker_continue)
     return;
 
-  int mode = args->mode;
   struct link_map *new = args->map;
 
-  do_reloc_2 (new, mode, args);
+  _dl_object_reloc (new);
+  /* For !lt_loaded we do not malloc(), so needs to null out here. */
+  if (new->l_type != lt_loaded)
+    new->l_dlopen_args = NULL;
 
   /* Let the user know about the opencount.  */
   if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
diff --git a/include/link.h b/include/link.h
index 1d74feb2bd..03b9f82715 100644
--- a/include/link.h
+++ b/include/link.h
@@ -347,6 +347,8 @@ struct link_map
     size_t l_relro_size;
 
     unsigned long long int l_serial;
+
+    void *l_dlopen_args;
   };
 
 #include <dl-relocate-ld.h>
-- 
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 ` Stas Sergeev [this message]
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 ` [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-7-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).