076f82
commit 198660741b23ec9defb19e22951d4a721de603c8
076f82
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
076f82
Date:   Tue Jul 20 14:04:51 2021 -0300
076f82
076f82
    elf: Add _dl_audit_objclose
076f82
    
076f82
    It consolidates the code required to call la_objclose 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 311c9ee54ea963ff69bd3a2e6981c37e893b4c3e)
076f82
076f82
diff --git a/elf/dl-audit.c b/elf/dl-audit.c
076f82
index 5682427220569d90..cb1c3de93cba447b 100644
076f82
--- a/elf/dl-audit.c
076f82
+++ b/elf/dl-audit.c
076f82
@@ -85,3 +85,24 @@ _dl_audit_objopen (struct link_map *l, Lmid_t nsid)
076f82
       afct = afct->next;
076f82
    }
076f82
 }
076f82
+
076f82
+void
076f82
+_dl_audit_objclose (struct link_map *l)
076f82
+{
076f82
+  if (__glibc_likely (GLRO(dl_naudit) == 0)
076f82
+      || GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
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->objclose != NULL)
076f82
+	{
076f82
+	  struct auditstate *state= link_map_audit_state (l, cnt);
076f82
+	  /* Return value is ignored.  */
076f82
+	  afct->objclose (&state->cookie);
076f82
+	}
076f82
+
076f82
+      afct = afct->next;
076f82
+    }
076f82
+}
076f82
diff --git a/elf/dl-close.c b/elf/dl-close.c
076f82
index 5a8cc9e7cb5186cc..985cd4e2821436af 100644
076f82
--- a/elf/dl-close.c
076f82
+++ b/elf/dl-close.c
076f82
@@ -260,9 +260,6 @@ _dl_close_worker (struct link_map *map, bool force)
076f82
   _dl_sort_maps (maps, nloaded, (nsid == LM_ID_BASE), true);
076f82
 
076f82
   /* Call all termination functions at once.  */
076f82
-#ifdef SHARED
076f82
-  bool do_audit = GLRO(dl_naudit) > 0 && !ns->_ns_loaded->l_auditing;
076f82
-#endif
076f82
   bool unload_any = false;
076f82
   bool scope_mem_left = false;
076f82
   unsigned int unload_global = 0;
076f82
@@ -296,22 +293,7 @@ _dl_close_worker (struct link_map *map, bool force)
076f82
 
076f82
 #ifdef SHARED
076f82
 	  /* Auditing checkpoint: we remove an object.  */
076f82
-	  if (__glibc_unlikely (do_audit))
076f82
-	    {
076f82
-	      struct audit_ifaces *afct = GLRO(dl_audit);
076f82
-	      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
076f82
-		{
076f82
-		  if (afct->objclose != NULL)
076f82
-		    {
076f82
-		      struct auditstate *state
076f82
-			= link_map_audit_state (imap, cnt);
076f82
-		      /* Return value is ignored.  */
076f82
-		      (void) afct->objclose (&state->cookie);
076f82
-		    }
076f82
-
076f82
-		  afct = afct->next;
076f82
-		}
076f82
-	    }
076f82
+	  _dl_audit_objclose (imap);
076f82
 #endif
076f82
 
076f82
 	  /* This object must not be used anymore.  */
076f82
diff --git a/elf/dl-fini.c b/elf/dl-fini.c
076f82
index c683884c355dfd52..b789cfb9f2ac6c85 100644
076f82
--- a/elf/dl-fini.c
076f82
+++ b/elf/dl-fini.c
076f82
@@ -146,21 +146,7 @@ _dl_fini (void)
076f82
 
076f82
 #ifdef SHARED
076f82
 		  /* Auditing checkpoint: another object closed.  */
076f82
-		  if (!do_audit && __builtin_expect (GLRO(dl_naudit) > 0, 0))
076f82
-		    {
076f82
-		      struct audit_ifaces *afct = GLRO(dl_audit);
076f82
-		      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
076f82
-			{
076f82
-			  if (afct->objclose != NULL)
076f82
-			    {
076f82
-			      struct auditstate *state
076f82
-				= link_map_audit_state (l, cnt);
076f82
-			      /* Return value is ignored.  */
076f82
-			      (void) afct->objclose (&state->cookie);
076f82
-			    }
076f82
-			  afct = afct->next;
076f82
-			}
076f82
-		    }
076f82
+		  _dl_audit_objclose (l);
076f82
 #endif
076f82
 		}
076f82
 
076f82
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
076f82
index 1f212a18d7bfc440..982f23c0287955fe 100644
076f82
--- a/sysdeps/generic/ldsodefs.h
076f82
+++ b/sysdeps/generic/ldsodefs.h
076f82
@@ -1394,6 +1394,10 @@ void _dl_audit_activity_nsid (Lmid_t nsid, int action)
076f82
    namespace identification NSID.  */
076f82
 void _dl_audit_objopen (struct link_map *l, Lmid_t nsid)
076f82
   attribute_hidden;
076f82
+
076f82
+/* Call the la_objclose from the audit modules for the link_map L.  */
076f82
+void _dl_audit_objclose (struct link_map *l)
076f82
+  attribute_hidden;
076f82
 #endif /* SHARED */
076f82
 
076f82
 #if PTHREAD_IN_LIBC && defined SHARED