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