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