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