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