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