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