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