aba491
commit a74c2e1cbc8673dd7e97aae2f2705392e2ccc3f6
aba491
Author: Florian Weimer <fweimer@redhat.com>
aba491
Date:   Mon Nov 27 11:28:10 2023 +0100
aba491
aba491
    elf: Introduce the _dl_open_relocate_one_object function
aba491
    
aba491
    It is extracted from dl_open_worker_begin.
aba491
    
aba491
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
aba491
aba491
diff --git a/elf/dl-open.c b/elf/dl-open.c
aba491
index 7dfb6b680c108c0b..160451790bb88447 100644
aba491
--- a/elf/dl-open.c
aba491
+++ b/elf/dl-open.c
aba491
@@ -466,6 +466,50 @@ activate_nodelete (struct link_map *new)
aba491
       }
aba491
 }
aba491
 
aba491
+/* Relocate the object L.  *RELOCATION_IN_PROGRESS controls whether
aba491
+   the debugger is notified of the start of relocation processing.  */
aba491
+static void
aba491
+_dl_open_relocate_one_object (struct dl_open_args *args, struct r_debug *r,
aba491
+			      struct link_map *l, int reloc_mode,
aba491
+			      bool *relocation_in_progress)
aba491
+{
aba491
+  if (l->l_real->l_relocated)
aba491
+    return;
aba491
+
aba491
+  if (!*relocation_in_progress)
aba491
+    {
aba491
+      /* Notify the debugger that relocations are about to happen.  */
aba491
+      LIBC_PROBE (reloc_start, 2, args->nsid, r);
aba491
+      *relocation_in_progress = true;
aba491
+    }
aba491
+
aba491
+#ifdef SHARED
aba491
+  if (__glibc_unlikely (GLRO(dl_profile) != NULL))
aba491
+    {
aba491
+      /* If this here is the shared object which we want to profile
aba491
+	 make sure the profile is started.  We can find out whether
aba491
+	 this is necessary or not by observing the `_dl_profile_map'
aba491
+	 variable.  If it was NULL but is not NULL afterwards we must
aba491
+	 start the profiling.  */
aba491
+      struct link_map *old_profile_map = GL(dl_profile_map);
aba491
+
aba491
+      _dl_relocate_object (l, l->l_scope, reloc_mode | RTLD_LAZY, 1);
aba491
+
aba491
+      if (old_profile_map == NULL && GL(dl_profile_map) != NULL)
aba491
+	{
aba491
+	  /* We must prepare the profiling.  */
aba491
+	  _dl_start_profile ();
aba491
+
aba491
+	  /* Prevent unloading the object.  */
aba491
+	  GL(dl_profile_map)->l_nodelete_active = true;
aba491
+	}
aba491
+    }
aba491
+  else
aba491
+#endif
aba491
+    _dl_relocate_object (l, l->l_scope, reloc_mode, 0);
aba491
+}
aba491
+
aba491
+
aba491
 /* struct dl_init_args and call_dl_init are used to call _dl_init with
aba491
    exception handling disabled.  */
aba491
 struct dl_init_args
aba491
@@ -638,7 +682,7 @@ dl_open_worker_begin (void *a)
aba491
     }
aba491
   while (l != NULL);
aba491
 
aba491
-  int relocation_in_progress = 0;
aba491
+  bool relocation_in_progress = false;
aba491
 
aba491
   /* Perform relocation.  This can trigger lazy binding in IFUNC
aba491
      resolvers.  For NODELETE mappings, these dependencies are not
aba491
@@ -649,44 +693,8 @@ dl_open_worker_begin (void *a)
aba491
      are undefined anyway, so this is not a problem.  */
aba491
 
aba491
   for (unsigned int i = last; i-- > first; )
aba491
-    {
aba491
-      l = new->l_initfini[i];
aba491
-
aba491
-      if (l->l_real->l_relocated)
aba491
-	continue;
aba491
-
aba491
-      if (! relocation_in_progress)
aba491
-	{
aba491
-	  /* Notify the debugger that relocations are about to happen.  */
aba491
-	  LIBC_PROBE (reloc_start, 2, args->nsid, r);
aba491
-	  relocation_in_progress = 1;
aba491
-	}
aba491
-
aba491
-#ifdef SHARED
aba491
-      if (__glibc_unlikely (GLRO(dl_profile) != NULL))
aba491
-	{
aba491
-	  /* If this here is the shared object which we want to profile
aba491
-	     make sure the profile is started.  We can find out whether
aba491
-	     this is necessary or not by observing the `_dl_profile_map'
aba491
-	     variable.  If it was NULL but is not NULL afterwards we must
aba491
-	     start the profiling.  */
aba491
-	  struct link_map *old_profile_map = GL(dl_profile_map);
aba491
-
aba491
-	  _dl_relocate_object (l, l->l_scope, reloc_mode | RTLD_LAZY, 1);
aba491
-
aba491
-	  if (old_profile_map == NULL && GL(dl_profile_map) != NULL)
aba491
-	    {
aba491
-	      /* We must prepare the profiling.  */
aba491
-	      _dl_start_profile ();
aba491
-
aba491
-	      /* Prevent unloading the object.  */
aba491
-	      GL(dl_profile_map)->l_nodelete_active = true;
aba491
-	    }
aba491
-	}
aba491
-      else
aba491
-#endif
aba491
-	_dl_relocate_object (l, l->l_scope, reloc_mode, 0);
aba491
-    }
aba491
+    _dl_open_relocate_one_object (args, r, new->l_initfini[i], reloc_mode,
aba491
+				  &relocation_in_progress);
aba491
 
aba491
   /* This only performs the memory allocations.  The actual update of
aba491
      the scopes happens below, after failure is impossible.  */