076f82
commit 31473c273be14270f8eef68e35c03fd2305eb2c3
076f82
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
076f82
Date:   Thu Jul 22 17:10:57 2021 -0300
076f82
076f82
    elf: Add _dl_audit_preinit
076f82
    
076f82
    It consolidates the code required to call la_preinit audit
076f82
    callback.
076f82
    
076f82
    Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
076f82
    
076f82
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
076f82
    (cherry picked from commit 0b98a8748759e88b58927882a8714109abe0a2d6)
076f82
076f82
diff --git a/csu/libc-start.c b/csu/libc-start.c
076f82
index 0350b006fdcc22d2..d01e57ea59ceb880 100644
076f82
--- a/csu/libc-start.c
076f82
+++ b/csu/libc-start.c
076f82
@@ -377,32 +377,15 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
076f82
     /* This is a current program.  Use the dynamic segment to find
076f82
        constructors.  */
076f82
     call_init (argc, argv, __environ);
076f82
-#else /* !SHARED */
076f82
-  call_init (argc, argv, __environ);
076f82
-#endif /* SHARED */
076f82
 
076f82
-#ifdef SHARED
076f82
   /* Auditing checkpoint: we have a new object.  */
076f82
-  if (__glibc_unlikely (GLRO(dl_naudit) > 0))
076f82
-    {
076f82
-      struct audit_ifaces *afct = GLRO(dl_audit);
076f82
-      struct link_map *head = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
076f82
-      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
076f82
-	{
076f82
-	  if (afct->preinit != NULL)
076f82
-	    afct->preinit (&link_map_audit_state (head, cnt)->cookie);
076f82
-
076f82
-	  afct = afct->next;
076f82
-	}
076f82
-    }
076f82
-#endif
076f82
+  _dl_audit_preinit (GL(dl_ns)[LM_ID_BASE]._ns_loaded);
076f82
 
076f82
-#ifdef SHARED
076f82
   if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS))
076f82
     GLRO(dl_debug_printf) ("\ntransferring control: %s\n\n", argv[0]);
076f82
-#endif
076f82
+#else /* !SHARED */
076f82
+  call_init (argc, argv, __environ);
076f82
 
076f82
-#ifndef SHARED
076f82
   _dl_debug_initialize (0, LM_ID_BASE);
076f82
 #endif
076f82
 
076f82
diff --git a/elf/Versions b/elf/Versions
076f82
index 164682eaeaa9a1da..bb6697647b397772 100644
076f82
--- a/elf/Versions
076f82
+++ b/elf/Versions
076f82
@@ -58,7 +58,7 @@ ld {
076f82
     _dl_argv; _dl_find_dso_for_object; _dl_get_tls_static_info;
076f82
     _dl_deallocate_tls; _dl_make_stack_executable;
076f82
     _dl_rtld_di_serinfo; _dl_starting_up; _dl_fatal_printf;
076f82
-    _dl_audit_symbind_alt;
076f82
+    _dl_audit_symbind_alt; _dl_audit_preinit;
076f82
     _rtld_global; _rtld_global_ro;
076f82
 
076f82
     # Only here for gdb while a better method is developed.
076f82
diff --git a/elf/dl-audit.c b/elf/dl-audit.c
076f82
index a21530f30bc5524b..0b6fac8e48877c93 100644
076f82
--- a/elf/dl-audit.c
076f82
+++ b/elf/dl-audit.c
076f82
@@ -108,6 +108,21 @@ _dl_audit_objclose (struct link_map *l)
076f82
     }
076f82
 }
076f82
 
076f82
+void
076f82
+_dl_audit_preinit (struct link_map *l)
076f82
+{
076f82
+  if (__glibc_likely (GLRO(dl_naudit) == 0))
076f82
+    return;
076f82
+
076f82
+  struct audit_ifaces *afct = GLRO(dl_audit);
076f82
+  for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
076f82
+    {
076f82
+      if (afct->preinit != NULL)
076f82
+	afct->preinit (&link_map_audit_state (l, cnt)->cookie);
076f82
+      afct = afct->next;
076f82
+    }
076f82
+}
076f82
+
076f82
 void
076f82
 _dl_audit_symbind_alt (struct link_map *l, const ElfW(Sym) *ref, void **value,
076f82
 		       lookup_t result)
076f82
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
076f82
index 61f1dfb3f79a613a..91193a036fc5c6ef 100644
076f82
--- a/sysdeps/generic/ldsodefs.h
076f82
+++ b/sysdeps/generic/ldsodefs.h
076f82
@@ -1399,6 +1399,9 @@ void _dl_audit_objopen (struct link_map *l, Lmid_t nsid)
076f82
 void _dl_audit_objclose (struct link_map *l)
076f82
   attribute_hidden;
076f82
 
076f82
+/* Call the la_preinit from the audit modules for the link_map L.  */
076f82
+void _dl_audit_preinit (struct link_map *l);
076f82
+
076f82
 /* Call the la_symbind{32,64} from the audit modules for the link_map L.  */
076f82
 void _dl_audit_symbind (struct link_map *l, struct reloc_result *reloc_result,
076f82
 			const ElfW(Sym) *defsym, DL_FIXUP_VALUE_TYPE *value,