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