abe59f
commit 311c9ee54ea963ff69bd3a2e6981c37e893b4c3e
abe59f
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
abe59f
Date:   Tue Jul 20 14:04:51 2021 -0300
abe59f
abe59f
    elf: Add _dl_audit_objclose
abe59f
    
abe59f
    It consolidates the code required to call la_objclose audit
abe59f
    callback.
abe59f
    
abe59f
    Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
abe59f
    
abe59f
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
abe59f
abe59f
diff --git a/elf/dl-audit.c b/elf/dl-audit.c
abe59f
index 5682427220569d90..cb1c3de93cba447b 100644
abe59f
--- a/elf/dl-audit.c
abe59f
+++ b/elf/dl-audit.c
abe59f
@@ -85,3 +85,24 @@ _dl_audit_objopen (struct link_map *l, Lmid_t nsid)
abe59f
       afct = afct->next;
abe59f
    }
abe59f
 }
abe59f
+
abe59f
+void
abe59f
+_dl_audit_objclose (struct link_map *l)
abe59f
+{
abe59f
+  if (__glibc_likely (GLRO(dl_naudit) == 0)
abe59f
+      || GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
abe59f
+    return;
abe59f
+
abe59f
+  struct audit_ifaces *afct = GLRO(dl_audit);
abe59f
+  for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
abe59f
+    {
abe59f
+      if (afct->objclose != NULL)
abe59f
+	{
abe59f
+	  struct auditstate *state= link_map_audit_state (l, cnt);
abe59f
+	  /* Return value is ignored.  */
abe59f
+	  afct->objclose (&state->cookie);
abe59f
+	}
abe59f
+
abe59f
+      afct = afct->next;
abe59f
+    }
abe59f
+}
abe59f
diff --git a/elf/dl-close.c b/elf/dl-close.c
abe59f
index 1ba594b600c4c87a..74ca9a85dd309780 100644
abe59f
--- a/elf/dl-close.c
abe59f
+++ b/elf/dl-close.c
abe59f
@@ -266,9 +266,6 @@ _dl_close_worker (struct link_map *map, bool force)
abe59f
 		 used + (nsid == LM_ID_BASE), true);
abe59f
 
abe59f
   /* Call all termination functions at once.  */
abe59f
-#ifdef SHARED
abe59f
-  bool do_audit = GLRO(dl_naudit) > 0 && !ns->_ns_loaded->l_auditing;
abe59f
-#endif
abe59f
   bool unload_any = false;
abe59f
   bool scope_mem_left = false;
abe59f
   unsigned int unload_global = 0;
abe59f
@@ -302,22 +299,7 @@ _dl_close_worker (struct link_map *map, bool force)
abe59f
 
abe59f
 #ifdef SHARED
abe59f
 	  /* Auditing checkpoint: we remove an object.  */
abe59f
-	  if (__glibc_unlikely (do_audit))
abe59f
-	    {
abe59f
-	      struct audit_ifaces *afct = GLRO(dl_audit);
abe59f
-	      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
abe59f
-		{
abe59f
-		  if (afct->objclose != NULL)
abe59f
-		    {
abe59f
-		      struct auditstate *state
abe59f
-			= link_map_audit_state (imap, cnt);
abe59f
-		      /* Return value is ignored.  */
abe59f
-		      (void) afct->objclose (&state->cookie);
abe59f
-		    }
abe59f
-
abe59f
-		  afct = afct->next;
abe59f
-		}
abe59f
-	    }
abe59f
+	  _dl_audit_objclose (imap);
abe59f
 #endif
abe59f
 
abe59f
 	  /* This object must not be used anymore.  */
abe59f
diff --git a/elf/dl-fini.c b/elf/dl-fini.c
abe59f
index 915ceb104e1c81d6..e102d93647cb8c47 100644
abe59f
--- a/elf/dl-fini.c
abe59f
+++ b/elf/dl-fini.c
abe59f
@@ -146,21 +146,7 @@ _dl_fini (void)
abe59f
 
abe59f
 #ifdef SHARED
abe59f
 		  /* Auditing checkpoint: another object closed.  */
abe59f
-		  if (!do_audit && __builtin_expect (GLRO(dl_naudit) > 0, 0))
abe59f
-		    {
abe59f
-		      struct audit_ifaces *afct = GLRO(dl_audit);
abe59f
-		      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
abe59f
-			{
abe59f
-			  if (afct->objclose != NULL)
abe59f
-			    {
abe59f
-			      struct auditstate *state
abe59f
-				= link_map_audit_state (l, cnt);
abe59f
-			      /* Return value is ignored.  */
abe59f
-			      (void) afct->objclose (&state->cookie);
abe59f
-			    }
abe59f
-			  afct = afct->next;
abe59f
-			}
abe59f
-		    }
abe59f
+		  _dl_audit_objclose (l);
abe59f
 #endif
abe59f
 		}
abe59f
 
abe59f
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
abe59f
index da83e717e8cd8e0b..3db25c5be1acf871 100644
abe59f
--- a/sysdeps/generic/ldsodefs.h
abe59f
+++ b/sysdeps/generic/ldsodefs.h
abe59f
@@ -1290,6 +1290,10 @@ void _dl_audit_activity_nsid (Lmid_t nsid, int action)
abe59f
    namespace identification NSID.  */
abe59f
 void _dl_audit_objopen (struct link_map *l, Lmid_t nsid)
abe59f
   attribute_hidden;
abe59f
+
abe59f
+/* Call the la_objclose from the audit modules for the link_map L.  */
abe59f
+void _dl_audit_objclose (struct link_map *l)
abe59f
+  attribute_hidden;
abe59f
 #endif /* SHARED */
abe59f
 
abe59f
 __END_DECLS