e77659
commit 6f360366f7f76b158a0f4bf20d42f2854ad56264
e77659
Author: Florian Weimer <fweimer@redhat.com>
e77659
Date:   Thu Oct 27 11:36:44 2022 +0200
e77659
e77659
    elf: Introduce to _dl_call_fini
e77659
    
e77659
    This consolidates the destructor invocations from _dl_fini and
e77659
    dlclose.  Remove the micro-optimization that avoids
e77659
    calling _dl_call_fini if they are no destructors (as dlclose is quite
e77659
    expensive anyway).  The debug log message is now printed
e77659
    unconditionally.
e77659
    
e77659
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
e77659
e77659
Conflicts:
e77659
	elf/dl-fini.c
e77659
	  (Missing ELF_INITFINI support downstream.)
e77659
	sysdeps/generic/ldsodefs.h
e77659
	  (Missing dl_init_t declaration downstream.)
e77659
e77659
diff --git a/elf/Makefile b/elf/Makefile
e77659
index 634c3113227d64a6..040d82e243a80c0f 100644
e77659
--- a/elf/Makefile
e77659
+++ b/elf/Makefile
e77659
@@ -50,6 +50,7 @@ routines = \
e77659
 # profiled libraries.
e77659
 dl-routines = \
e77659
   dl-call-libc-early-init \
e77659
+  dl-call_fini \
e77659
   dl-close \
e77659
   dl-debug \
e77659
   dl-deps \
e77659
diff --git a/elf/dl-call_fini.c b/elf/dl-call_fini.c
e77659
new file mode 100644
e77659
index 0000000000000000..9e7ba10fa2a4df77
e77659
--- /dev/null
e77659
+++ b/elf/dl-call_fini.c
e77659
@@ -0,0 +1,50 @@
e77659
+/* Invoke DT_FINI and DT_FINI_ARRAY callbacks.
e77659
+   Copyright (C) 1996-2022 Free Software Foundation, Inc.
e77659
+   This file is part of the GNU C Library.
e77659
+
e77659
+   The GNU C Library is free software; you can redistribute it and/or
e77659
+   modify it under the terms of the GNU Lesser General Public
e77659
+   License as published by the Free Software Foundation; either
e77659
+   version 2.1 of the License, or (at your option) any later version.
e77659
+
e77659
+   The GNU C Library is distributed in the hope that it will be useful,
e77659
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e77659
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e77659
+   Lesser General Public License for more details.
e77659
+
e77659
+   You should have received a copy of the GNU Lesser General Public
e77659
+   License along with the GNU C Library; if not, see
e77659
+   <https://www.gnu.org/licenses/>.  */
e77659
+
e77659
+#include <ldsodefs.h>
e77659
+#include <sysdep.h>
e77659
+
e77659
+void
e77659
+_dl_call_fini (void *closure_map)
e77659
+{
e77659
+  struct link_map *map = closure_map;
e77659
+
e77659
+  /* When debugging print a message first.  */
e77659
+  if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS))
e77659
+    _dl_debug_printf ("\ncalling fini: %s [%lu]\n\n", map->l_name, map->l_ns);
e77659
+
e77659
+  /* Make sure nothing happens if we are called twice.  */
e77659
+  map->l_init_called = 0;
e77659
+
e77659
+  ElfW(Dyn) *fini_array = map->l_info[DT_FINI_ARRAY];
e77659
+  if (fini_array != NULL)
e77659
+    {
e77659
+      ElfW(Addr) *array = (ElfW(Addr) *) (map->l_addr
e77659
+                                          + fini_array->d_un.d_ptr);
e77659
+      size_t sz = (map->l_info[DT_FINI_ARRAYSZ]->d_un.d_val
e77659
+                   / sizeof (ElfW(Addr)));
e77659
+
e77659
+      while (sz-- > 0)
e77659
+        ((fini_t) array[sz]) ();
e77659
+    }
e77659
+
e77659
+  /* Next try the old-style destructor.  */
e77659
+  ElfW(Dyn) *fini = map->l_info[DT_FINI];
e77659
+  if (fini != NULL)
e77659
+    DL_CALL_DT_FINI (map, ((void *) map->l_addr + fini->d_un.d_ptr));
e77659
+}
e77659
diff --git a/elf/dl-close.c b/elf/dl-close.c
e77659
index 22225efb3226c3e1..26ea51dfbadc5b85 100644
e77659
--- a/elf/dl-close.c
e77659
+++ b/elf/dl-close.c
e77659
@@ -35,11 +35,6 @@
e77659
 
e77659
 #include <dl-unmap-segments.h>
e77659
 
e77659
-
e77659
-/* Type of the constructor functions.  */
e77659
-typedef void (*fini_t) (void);
e77659
-
e77659
-
e77659
 /* Special l_idx value used to indicate which objects remain loaded.  */
e77659
 #define IDX_STILL_USED -1
e77659
 
e77659
@@ -109,31 +104,6 @@ remove_slotinfo (size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
e77659
   return false;
e77659
 }
e77659
 
e77659
-/* Invoke dstructors for CLOSURE (a struct link_map *).  Called with
e77659
-   exception handling temporarily disabled, to make errors fatal.  */
e77659
-static void
e77659
-call_destructors (void *closure)
e77659
-{
e77659
-  struct link_map *map = closure;
e77659
-
e77659
-  if (map->l_info[DT_FINI_ARRAY] != NULL)
e77659
-    {
e77659
-      ElfW(Addr) *array =
e77659
-	(ElfW(Addr) *) (map->l_addr
e77659
-			+ map->l_info[DT_FINI_ARRAY]->d_un.d_ptr);
e77659
-      unsigned int sz = (map->l_info[DT_FINI_ARRAYSZ]->d_un.d_val
e77659
-			 / sizeof (ElfW(Addr)));
e77659
-
e77659
-      while (sz-- > 0)
e77659
-	((fini_t) array[sz]) ();
e77659
-    }
e77659
-
e77659
-  /* Next try the old-style destructor.  */
e77659
-  if (map->l_info[DT_FINI] != NULL)
e77659
-    DL_CALL_DT_FINI (map, ((void *) map->l_addr
e77659
-			   + map->l_info[DT_FINI]->d_un.d_ptr));
e77659
-}
e77659
-
e77659
 void
e77659
 _dl_close_worker (struct link_map *map, bool force)
e77659
 {
e77659
@@ -279,17 +249,7 @@ _dl_close_worker (struct link_map *map, bool force)
e77659
 	     half-cooked objects.  Temporarily disable exception
e77659
 	     handling, so that errors are fatal.  */
e77659
 	  if (imap->l_init_called)
e77659
-	    {
e77659
-	      /* When debugging print a message first.  */
e77659
-	      if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS,
e77659
-				    0))
e77659
-		_dl_debug_printf ("\ncalling fini: %s [%lu]\n\n",
e77659
-				  imap->l_name, nsid);
e77659
-
e77659
-	      if (imap->l_info[DT_FINI_ARRAY] != NULL
e77659
-		  || imap->l_info[DT_FINI] != NULL)
e77659
-		_dl_catch_exception (NULL, call_destructors, imap);
e77659
-	    }
e77659
+	    _dl_catch_exception (NULL, _dl_call_fini, imap);
e77659
 
e77659
 #ifdef SHARED
e77659
 	  /* Auditing checkpoint: we remove an object.  */
e77659
diff --git a/elf/dl-fini.c b/elf/dl-fini.c
e77659
index e14259a3c8806e0d..2d34658d4c3a470c 100644
e77659
--- a/elf/dl-fini.c
e77659
+++ b/elf/dl-fini.c
e77659
@@ -20,11 +20,6 @@
e77659
 #include <string.h>
e77659
 #include <ldsodefs.h>
e77659
 
e77659
-
e77659
-/* Type of the constructor functions.  */
e77659
-typedef void (*fini_t) (void);
e77659
-
e77659
-
e77659
 void
e77659
 _dl_fini (void)
e77659
 {
e77659
@@ -115,38 +110,7 @@ _dl_fini (void)
e77659
 
e77659
 	      if (l->l_init_called)
e77659
 		{
e77659
-		  /* Make sure nothing happens if we are called twice.  */
e77659
-		  l->l_init_called = 0;
e77659
-
e77659
-		  /* Is there a destructor function?  */
e77659
-		  if (l->l_info[DT_FINI_ARRAY] != NULL
e77659
-		      || l->l_info[DT_FINI] != NULL)
e77659
-		    {
e77659
-		      /* When debugging print a message first.  */
e77659
-		      if (__builtin_expect (GLRO(dl_debug_mask)
e77659
-					    & DL_DEBUG_IMPCALLS, 0))
e77659
-			_dl_debug_printf ("\ncalling fini: %s [%lu]\n\n",
e77659
-					  DSO_FILENAME (l->l_name),
e77659
-					  ns);
e77659
-
e77659
-		      /* First see whether an array is given.  */
e77659
-		      if (l->l_info[DT_FINI_ARRAY] != NULL)
e77659
-			{
e77659
-			  ElfW(Addr) *array =
e77659
-			    (ElfW(Addr) *) (l->l_addr
e77659
-					    + l->l_info[DT_FINI_ARRAY]->d_un.d_ptr);
e77659
-			  unsigned int i = (l->l_info[DT_FINI_ARRAYSZ]->d_un.d_val
e77659
-					    / sizeof (ElfW(Addr)));
e77659
-			  while (i-- > 0)
e77659
-			    ((fini_t) array[i]) ();
e77659
-			}
e77659
-
e77659
-		      /* Next try the old-style destructor.  */
e77659
-		      if (l->l_info[DT_FINI] != NULL)
e77659
-			DL_CALL_DT_FINI
e77659
-			  (l, l->l_addr + l->l_info[DT_FINI]->d_un.d_ptr);
e77659
-		    }
e77659
-
e77659
+		  _dl_call_fini (l);
e77659
 #ifdef SHARED
e77659
 		  /* Auditing checkpoint: another object closed.  */
e77659
 		  _dl_audit_objclose (l);
e77659
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
e77659
index 29bbde3e83e37d7e..0bad34d44a5685d9 100644
e77659
--- a/sysdeps/generic/ldsodefs.h
e77659
+++ b/sysdeps/generic/ldsodefs.h
e77659
@@ -93,6 +93,9 @@ typedef struct link_map *lookup_t;
e77659
    : (__glibc_unlikely ((ref)->st_shndx == SHN_ABS) ? 0			\
e77659
       : LOOKUP_VALUE_ADDRESS (map, map_set)) + (ref)->st_value)
e77659
 
e77659
+/* Type of a constructor function, in DT_FINI, DT_FINI_ARRAY.  */
e77659
+typedef void (*fini_t) (void);
e77659
+
e77659
 /* On some architectures a pointer to a function is not just a pointer
e77659
    to the actual code of the function but rather an architecture
e77659
    specific descriptor. */
e77659
@@ -1047,6 +1050,11 @@ extern void _dl_init (struct link_map *main_map, int argc, char **argv,
e77659
    initializer functions have completed.  */
e77659
 extern void _dl_fini (void) attribute_hidden;
e77659
 
e77659
+/* Invoke the DT_FINI_ARRAY and DT_FINI destructors for MAP, which
e77659
+   must be a struct link_map *.  Can be used as an argument to
e77659
+   _dl_catch_exception.  */
e77659
+void _dl_call_fini (void *map) attribute_hidden;
e77659
+
e77659
 /* Sort array MAPS according to dependencies of the contained objects.
e77659
    If FORCE_FIRST, MAPS[0] keeps its place even if the dependencies
e77659
    say otherwise.  */