c5d972
commit eff687e8462b0eaf65992a6031b54a4b1cd16796
c5d972
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
c5d972
Date:   Thu Jul 22 17:45:33 2021 -0300
c5d972
c5d972
    elf: Add _dl_audit_pltenter
c5d972
    
c5d972
    It consolidates the code required to call la_pltenter 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 0b6fac8e48877c93..15250c67e8ac1658 100644
c5d972
--- a/elf/dl-audit.c
c5d972
+++ b/elf/dl-audit.c
c5d972
@@ -17,7 +17,9 @@
c5d972
    <https://www.gnu.org/licenses/>.  */
c5d972
 
c5d972
 #include <assert.h>
c5d972
+#include <link.h>
c5d972
 #include <ldsodefs.h>
c5d972
+#include <dl-machine.h>
c5d972
 
c5d972
 void
c5d972
 _dl_audit_activity_map (struct link_map *l, int action)
c5d972
@@ -243,3 +245,78 @@ _dl_audit_symbind (struct link_map *l, struct reloc_result *reloc_result,
c5d972
   reloc_result->flags = flags;
c5d972
   *value = DL_FIXUP_ADDR_VALUE (sym.st_value);
c5d972
 }
c5d972
+
c5d972
+void
c5d972
+_dl_audit_pltenter (struct link_map *l, struct reloc_result *reloc_result,
c5d972
+		    DL_FIXUP_VALUE_TYPE *value, void *regs, long int *framesize)
c5d972
+{
c5d972
+  /* Don't do anything if no auditor wants to intercept this call.  */
c5d972
+  if (GLRO(dl_naudit) == 0
c5d972
+      || (reloc_result->enterexit & LA_SYMB_NOPLTENTER))
c5d972
+    return;
c5d972
+
c5d972
+  /* Sanity check:  DL_FIXUP_VALUE_CODE_ADDR (value) should have been
c5d972
+     initialized earlier in this function or in another thread.  */
c5d972
+  assert (DL_FIXUP_VALUE_CODE_ADDR (*value) != 0);
c5d972
+  ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
c5d972
+					    l_info[DT_SYMTAB])
c5d972
+		       + reloc_result->boundndx);
c5d972
+
c5d972
+  /* Set up the sym parameter.  */
c5d972
+  ElfW(Sym) sym = *defsym;
c5d972
+  sym.st_value = DL_FIXUP_VALUE_ADDR (*value);
c5d972
+
c5d972
+  /* Get the symbol name.  */
c5d972
+  const char *strtab = (const void *) D_PTR (reloc_result->bound,
c5d972
+					     l_info[DT_STRTAB]);
c5d972
+  const char *symname = strtab + sym.st_name;
c5d972
+
c5d972
+  /* Keep track of overwritten addresses.  */
c5d972
+  unsigned int flags = reloc_result->flags;
c5d972
+
c5d972
+  struct audit_ifaces *afct = GLRO(dl_audit);
c5d972
+  for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
c5d972
+    {
c5d972
+      if (afct->ARCH_LA_PLTENTER != NULL
c5d972
+	  && (reloc_result->enterexit
c5d972
+	      & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
c5d972
+	{
c5d972
+	  long int new_framesize = -1;
c5d972
+	  struct auditstate *l_state = link_map_audit_state (l, cnt);
c5d972
+	  struct auditstate *bound_state
c5d972
+	    = link_map_audit_state (reloc_result->bound, cnt);
c5d972
+	  uintptr_t new_value
c5d972
+	    = afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx,
c5d972
+				      &l_state->cookie, &bound_state->cookie,
c5d972
+				      regs, &flags, symname, &new_framesize);
c5d972
+	  if (new_value != (uintptr_t) sym.st_value)
c5d972
+	    {
c5d972
+	      flags |= LA_SYMB_ALTVALUE;
c5d972
+	      sym.st_value = new_value;
c5d972
+	    }
c5d972
+
c5d972
+	  /* Remember the results for every audit library and store a summary
c5d972
+	     in the first two bits.  */
c5d972
+	  reloc_result->enterexit |= ((flags & (LA_SYMB_NOPLTENTER
c5d972
+						| LA_SYMB_NOPLTEXIT))
c5d972
+				      << (2 * (cnt + 1)));
c5d972
+
c5d972
+	  if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
c5d972
+					  << (2 * (cnt + 1))))
c5d972
+	      == 0 && new_framesize != -1 && *framesize != -2)
c5d972
+	    {
c5d972
+	      /* If this is the first call providing information, use it.  */
c5d972
+	      if (*framesize == -1)
c5d972
+		*framesize = new_framesize;
c5d972
+	      /* If two pltenter calls provide conflicting information, use
c5d972
+		 the larger value.  */
c5d972
+	      else if (new_framesize != *framesize)
c5d972
+		*framesize = MAX (new_framesize, *framesize);
c5d972
+	    }
c5d972
+	}
c5d972
+
c5d972
+      afct = afct->next;
c5d972
+    }
c5d972
+
c5d972
+  *value = DL_FIXUP_ADDR_VALUE (sym.st_value);
c5d972
+}
c5d972
diff --git a/elf/dl-runtime.c b/elf/dl-runtime.c
c5d972
index d4840a7c17441126..b46f7d7376e65361 100644
c5d972
--- a/elf/dl-runtime.c
c5d972
+++ b/elf/dl-runtime.c
c5d972
@@ -319,78 +319,7 @@ _dl_profile_fixup (
c5d972
 #ifdef SHARED
c5d972
   /* Auditing checkpoint: report the PLT entering and allow the
c5d972
      auditors to change the value.  */
c5d972
-  if (GLRO(dl_naudit) > 0
c5d972
-      /* Don't do anything if no auditor wants to intercept this call.  */
c5d972
-      && (reloc_result->enterexit & LA_SYMB_NOPLTENTER) == 0)
c5d972
-    {
c5d972
-      /* Sanity check:  DL_FIXUP_VALUE_CODE_ADDR (value) should have been
c5d972
-	 initialized earlier in this function or in another thread.  */
c5d972
-      assert (DL_FIXUP_VALUE_CODE_ADDR (value) != 0);
c5d972
-      ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
c5d972
-						l_info[DT_SYMTAB])
c5d972
-			   + reloc_result->boundndx);
c5d972
-
c5d972
-      /* Set up the sym parameter.  */
c5d972
-      ElfW(Sym) sym = *defsym;
c5d972
-      sym.st_value = DL_FIXUP_VALUE_ADDR (value);
c5d972
-
c5d972
-      /* Get the symbol name.  */
c5d972
-      const char *strtab = (const void *) D_PTR (reloc_result->bound,
c5d972
-						 l_info[DT_STRTAB]);
c5d972
-      const char *symname = strtab + sym.st_name;
c5d972
-
c5d972
-      /* Keep track of overwritten addresses.  */
c5d972
-      unsigned int flags = reloc_result->flags;
c5d972
-
c5d972
-      struct audit_ifaces *afct = GLRO(dl_audit);
c5d972
-      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
c5d972
-	{
c5d972
-	  if (afct->ARCH_LA_PLTENTER != NULL
c5d972
-	      && (reloc_result->enterexit
c5d972
-		  & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
c5d972
-	    {
c5d972
-	      long int new_framesize = -1;
c5d972
-	      struct auditstate *l_state = link_map_audit_state (l, cnt);
c5d972
-	      struct auditstate *bound_state
c5d972
-		= link_map_audit_state (reloc_result->bound, cnt);
c5d972
-	      uintptr_t new_value
c5d972
-		= afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx,
c5d972
-					  &l_state->cookie,
c5d972
-					  &bound_state->cookie,
c5d972
-					  regs, &flags, symname,
c5d972
-					  &new_framesize);
c5d972
-	      if (new_value != (uintptr_t) sym.st_value)
c5d972
-		{
c5d972
-		  flags |= LA_SYMB_ALTVALUE;
c5d972
-		  sym.st_value = new_value;
c5d972
-		}
c5d972
-
c5d972
-	      /* Remember the results for every audit library and
c5d972
-		 store a summary in the first two bits.  */
c5d972
-	      reloc_result->enterexit
c5d972
-		|= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
c5d972
-		    << (2 * (cnt + 1)));
c5d972
-
c5d972
-	      if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
c5d972
-					      << (2 * (cnt + 1))))
c5d972
-		  == 0 && new_framesize != -1 && framesize != -2)
c5d972
-		{
c5d972
-		  /* If this is the first call providing information,
c5d972
-		     use it.  */
c5d972
-		  if (framesize == -1)
c5d972
-		    framesize = new_framesize;
c5d972
-		  /* If two pltenter calls provide conflicting information,
c5d972
-		     use the larger value.  */
c5d972
-		  else if (new_framesize != framesize)
c5d972
-		    framesize = MAX (new_framesize, framesize);
c5d972
-		}
c5d972
-	    }
c5d972
-
c5d972
-	  afct = afct->next;
c5d972
-	}
c5d972
-
c5d972
-      value = DL_FIXUP_ADDR_VALUE (sym.st_value);
c5d972
-    }
c5d972
+  _dl_audit_pltenter (l, reloc_result, &value, regs, &framesize);
c5d972
 #endif
c5d972
 
c5d972
   /* Store the frame size information.  */
c5d972
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
c5d972
index 03676b474c3d37a3..47a9dee5b1c0ca63 100644
c5d972
--- a/sysdeps/generic/ldsodefs.h
c5d972
+++ b/sysdeps/generic/ldsodefs.h
c5d972
@@ -1307,6 +1307,10 @@ void _dl_audit_symbind (struct link_map *l, struct reloc_result *reloc_result,
c5d972
 void _dl_audit_symbind_alt (struct link_map *l, const ElfW(Sym) *ref,
c5d972
 			    void **value, lookup_t result);
c5d972
 rtld_hidden_proto (_dl_audit_symbind_alt)
c5d972
+void _dl_audit_pltenter (struct link_map *l, struct reloc_result *reloc_result,
c5d972
+			 DL_FIXUP_VALUE_TYPE *value, void *regs,
c5d972
+			 long int *framesize)
c5d972
+  attribute_hidden;
c5d972
 #endif /* SHARED */
c5d972
 
c5d972
 __END_DECLS