b1dca6
commit c76147afe917ef7d309ee893f8f017a3c2934aac
b1dca6
Author: Florian Weimer <fweimer@redhat.com>
b1dca6
Date:   Sat Feb 8 15:00:28 2020 +0100
b1dca6
b1dca6
    elf: Extract _dl_sym_post, _dl_sym_find_caller_map from elf/dl-sym.c
b1dca6
    
b1dca6
    The definitions are moved into a new file, elf/dl-sym-post.h, so that
b1dca6
    this code can be used by the dynamic loader as well.
b1dca6
    
b1dca6
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
b1dca6
b1dca6
diff --git a/elf/dl-sym-post.h b/elf/dl-sym-post.h
b1dca6
new file mode 100644
b1dca6
index 0000000000000000..4c4f574633497789
b1dca6
--- /dev/null
b1dca6
+++ b/elf/dl-sym-post.h
b1dca6
@@ -0,0 +1,106 @@
b1dca6
+/* Post-processing of a symbol produced by dlsym, dlvsym.
b1dca6
+   Copyright (C) 1999-2020 Free Software Foundation, Inc.
b1dca6
+   This file is part of the GNU C Library.
b1dca6
+
b1dca6
+   The GNU C Library is free software; you can redistribute it and/or
b1dca6
+   modify it under the terms of the GNU Lesser General Public
b1dca6
+   License as published by the Free Software Foundation; either
b1dca6
+   version 2.1 of the License, or (at your option) any later version.
b1dca6
+
b1dca6
+   The GNU C Library is distributed in the hope that it will be useful,
b1dca6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
b1dca6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b1dca6
+   Lesser General Public License for more details.
b1dca6
+
b1dca6
+   You should have received a copy of the GNU Lesser General Public
b1dca6
+   License along with the GNU C Library; if not, see
b1dca6
+   <https://www.gnu.org/licenses/>.  */
b1dca6
+
b1dca6
+
b1dca6
+/* Return the link map containing the caller address.  */
b1dca6
+static struct link_map *
b1dca6
+_dl_sym_find_caller_link_map (ElfW(Addr) caller)
b1dca6
+{
b1dca6
+  struct link_map *l = _dl_find_dso_for_object (caller);
b1dca6
+  if (l != NULL)
b1dca6
+    return l;
b1dca6
+  else
b1dca6
+    /* If the address is not recognized the call comes from the main
b1dca6
+       program (we hope).  */
b1dca6
+    return GL(dl_ns)[LM_ID_BASE]._ns_loaded;
b1dca6
+}
b1dca6
+
b1dca6
+/* Translates RESULT, *REF, VALUE into a symbol address from the point
b1dca6
+   of view of MATCH.  Performs IFUNC resolution and auditing if
b1dca6
+   necessary.  If MATCH is NULL, CALLER is used to determine it.  */
b1dca6
+static void *
b1dca6
+_dl_sym_post (lookup_t result, const ElfW(Sym) *ref, void *value,
b1dca6
+              ElfW(Addr) caller, struct link_map *match)
b1dca6
+{
b1dca6
+  /* Resolve indirect function address.  */
b1dca6
+  if (__glibc_unlikely (ELFW(ST_TYPE) (ref->st_info) == STT_GNU_IFUNC))
b1dca6
+    {
b1dca6
+      DL_FIXUP_VALUE_TYPE fixup
b1dca6
+        = DL_FIXUP_MAKE_VALUE (result, (ElfW(Addr)) value);
b1dca6
+      fixup = elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (fixup));
b1dca6
+      value = (void *) DL_FIXUP_VALUE_CODE_ADDR (fixup);
b1dca6
+    }
b1dca6
+
b1dca6
+#ifdef SHARED
b1dca6
+  /* Auditing checkpoint: we have a new binding.  Provide the
b1dca6
+     auditing libraries the possibility to change the value and
b1dca6
+     tell us whether further auditing is wanted.  */
b1dca6
+  if (__glibc_unlikely (GLRO(dl_naudit) > 0))
b1dca6
+    {
b1dca6
+      const char *strtab = (const char *) D_PTR (result,
b1dca6
+                                                 l_info[DT_STRTAB]);
b1dca6
+      /* Compute index of the symbol entry in the symbol table of
b1dca6
+         the DSO with the definition.  */
b1dca6
+      unsigned int ndx = (ref - (ElfW(Sym) *) D_PTR (result,
b1dca6
+                                                     l_info[DT_SYMTAB]));
b1dca6
+
b1dca6
+      if (match == NULL)
b1dca6
+        match = _dl_sym_find_caller_link_map (caller);
b1dca6
+
b1dca6
+      if ((match->l_audit_any_plt | result->l_audit_any_plt) != 0)
b1dca6
+        {
b1dca6
+          unsigned int altvalue = 0;
b1dca6
+          struct audit_ifaces *afct = GLRO(dl_audit);
b1dca6
+          /* Synthesize a symbol record where the st_value field is
b1dca6
+             the result.  */
b1dca6
+          ElfW(Sym) sym = *ref;
b1dca6
+          sym.st_value = (ElfW(Addr)) value;
b1dca6
+
b1dca6
+          for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
b1dca6
+            {
b1dca6
+              struct auditstate *match_audit
b1dca6
+                = link_map_audit_state (match, cnt);
b1dca6
+              struct auditstate *result_audit
b1dca6
+                = link_map_audit_state (result, cnt);
b1dca6
+              if (afct->symbind != NULL
b1dca6
+                  && ((match_audit->bindflags & LA_FLG_BINDFROM) != 0
b1dca6
+                      || ((result_audit->bindflags & LA_FLG_BINDTO)
b1dca6
+                          != 0)))
b1dca6
+                {
b1dca6
+                  unsigned int flags = altvalue | LA_SYMB_DLSYM;
b1dca6
+                  uintptr_t new_value
b1dca6
+                    = afct->symbind (&sym, ndx,
b1dca6
+                                     &match_audit->cookie,
b1dca6
+                                     &result_audit->cookie,
b1dca6
+                                     &flags, strtab + ref->st_name);
b1dca6
+                  if (new_value != (uintptr_t) sym.st_value)
b1dca6
+                    {
b1dca6
+                      altvalue = LA_SYMB_ALTVALUE;
b1dca6
+                      sym.st_value = new_value;
b1dca6
+                    }
b1dca6
+                }
b1dca6
+
b1dca6
+              afct = afct->next;
b1dca6
+            }
b1dca6
+
b1dca6
+          value = (void *) sym.st_value;
b1dca6
+        }
b1dca6
+    }
b1dca6
+#endif
b1dca6
+  return value;
b1dca6
+}
b1dca6
diff --git a/elf/dl-sym.c b/elf/dl-sym.c
b1dca6
index b133850a3c6657a4..5698fd7874a0ce48 100644
b1dca6
--- a/elf/dl-sym.c
b1dca6
+++ b/elf/dl-sym.c
b1dca6
@@ -28,6 +28,7 @@
b1dca6
 #include <sysdep-cancel.h>
b1dca6
 #include <dl-tls.h>
b1dca6
 #include <dl-irel.h>
b1dca6
+#include <dl-sym-post.h>
b1dca6
 
b1dca6
 
b1dca6
 #ifdef SHARED
b1dca6
@@ -80,19 +81,6 @@ call_dl_lookup (void *ptr)
b1dca6
 					args->flags, NULL);
b1dca6
 }
b1dca6
 
b1dca6
-/* Return the link map containing the caller address.  */
b1dca6
-static inline struct link_map *
b1dca6
-find_caller_link_map (ElfW(Addr) caller)
b1dca6
-{
b1dca6
-  struct link_map *l = _dl_find_dso_for_object (caller);
b1dca6
-  if (l != NULL)
b1dca6
-    return l;
b1dca6
-  else
b1dca6
-    /* If the address is not recognized the call comes from the main
b1dca6
-       program (we hope).  */
b1dca6
-    return GL(dl_ns)[LM_ID_BASE]._ns_loaded;
b1dca6
-}
b1dca6
-
b1dca6
 static void *
b1dca6
 do_sym (void *handle, const char *name, void *who,
b1dca6
 	struct r_found_version *vers, int flags)
b1dca6
@@ -106,7 +94,7 @@ do_sym (void *handle, const char *name, void *who,
b1dca6
 
b1dca6
   if (handle == RTLD_DEFAULT)
b1dca6
     {
b1dca6
-      match = find_caller_link_map (caller);
b1dca6
+      match = _dl_sym_find_caller_link_map (caller);
b1dca6
 
b1dca6
       /* Search the global scope.  We have the simple case where
b1dca6
 	 we look up in the scope of an object which was part of
b1dca6
@@ -140,7 +128,7 @@ do_sym (void *handle, const char *name, void *who,
b1dca6
     }
b1dca6
   else if (handle == RTLD_NEXT)
b1dca6
     {
b1dca6
-      match = find_caller_link_map (caller);
b1dca6
+      match = _dl_sym_find_caller_link_map (caller);
b1dca6
 
b1dca6
       if (__glibc_unlikely (match == GL(dl_ns)[LM_ID_BASE]._ns_loaded))
b1dca6
 	{
b1dca6
@@ -179,73 +167,7 @@ RTLD_NEXT used in code not dynamically loaded"));
b1dca6
 #endif
b1dca6
 	value = DL_SYMBOL_ADDRESS (result, ref);
b1dca6
 
b1dca6
-      /* Resolve indirect function address.  */
b1dca6
-      if (__glibc_unlikely (ELFW(ST_TYPE) (ref->st_info) == STT_GNU_IFUNC))
b1dca6
-	{
b1dca6
-	  DL_FIXUP_VALUE_TYPE fixup
b1dca6
-	    = DL_FIXUP_MAKE_VALUE (result, (ElfW(Addr)) value);
b1dca6
-	  fixup = elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (fixup));
b1dca6
-	  value = (void *) DL_FIXUP_VALUE_CODE_ADDR (fixup);
b1dca6
-	}
b1dca6
-
b1dca6
-#ifdef SHARED
b1dca6
-      /* Auditing checkpoint: we have a new binding.  Provide the
b1dca6
-	 auditing libraries the possibility to change the value and
b1dca6
-	 tell us whether further auditing is wanted.  */
b1dca6
-      if (__glibc_unlikely (GLRO(dl_naudit) > 0))
b1dca6
-	{
b1dca6
-	  const char *strtab = (const char *) D_PTR (result,
b1dca6
-						     l_info[DT_STRTAB]);
b1dca6
-	  /* Compute index of the symbol entry in the symbol table of
b1dca6
-	     the DSO with the definition.  */
b1dca6
-	  unsigned int ndx = (ref - (ElfW(Sym) *) D_PTR (result,
b1dca6
-							 l_info[DT_SYMTAB]));
b1dca6
-
b1dca6
-	  if (match == NULL)
b1dca6
-	    match = find_caller_link_map (caller);
b1dca6
-
b1dca6
-	  if ((match->l_audit_any_plt | result->l_audit_any_plt) != 0)
b1dca6
-	    {
b1dca6
-	      unsigned int altvalue = 0;
b1dca6
-	      struct audit_ifaces *afct = GLRO(dl_audit);
b1dca6
-	      /* Synthesize a symbol record where the st_value field is
b1dca6
-		 the result.  */
b1dca6
-	      ElfW(Sym) sym = *ref;
b1dca6
-	      sym.st_value = (ElfW(Addr)) value;
b1dca6
-
b1dca6
-	      for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
b1dca6
-		{
b1dca6
-		  struct auditstate *match_audit
b1dca6
-		    = link_map_audit_state (match, cnt);
b1dca6
-		  struct auditstate *result_audit
b1dca6
-		    = link_map_audit_state (result, cnt);
b1dca6
-		  if (afct->symbind != NULL
b1dca6
-		      && ((match_audit->bindflags & LA_FLG_BINDFROM) != 0
b1dca6
-			  || ((result_audit->bindflags & LA_FLG_BINDTO)
b1dca6
-			      != 0)))
b1dca6
-		    {
b1dca6
-		      unsigned int flags = altvalue | LA_SYMB_DLSYM;
b1dca6
-		      uintptr_t new_value
b1dca6
-			= afct->symbind (&sym, ndx,
b1dca6
-					 &match_audit->cookie,
b1dca6
-					 &result_audit->cookie,
b1dca6
-					 &flags, strtab + ref->st_name);
b1dca6
-		      if (new_value != (uintptr_t) sym.st_value)
b1dca6
-			{
b1dca6
-			  altvalue = LA_SYMB_ALTVALUE;
b1dca6
-			  sym.st_value = new_value;
b1dca6
-			}
b1dca6
-		    }
b1dca6
-
b1dca6
-		  afct = afct->next;
b1dca6
-		}
b1dca6
-
b1dca6
-	      value = (void *) sym.st_value;
b1dca6
-	    }
b1dca6
-	}
b1dca6
-#endif
b1dca6
-
b1dca6
-      return value;
b1dca6
+      return _dl_sym_post (result, ref, value, caller, match);
b1dca6
     }
b1dca6
 
b1dca6
   return NULL;