077c9d
commit 7b1f9406761331cf35fe521fbdb592beecf68a2c
077c9d
Author: H.J. Lu <hjl.tools@gmail.com>
077c9d
Date:   Fri Sep 28 13:31:19 2018 -0700
077c9d
077c9d
    i386: Use _dl_runtime_[resolve|profile]_shstk for SHSTK [BZ #23716]
077c9d
    
077c9d
    When elf_machine_runtime_setup is called to set up resolver, it should
077c9d
    use _dl_runtime_resolve_shstk or _dl_runtime_profile_shstk if SHSTK is
077c9d
    enabled by kernel.
077c9d
    
077c9d
    Tested on i686 with and without --enable-cet as well as on CET emulator
077c9d
    with --enable-cet.
077c9d
    
077c9d
            [BZ #23716]
077c9d
            * sysdeps/i386/dl-cet.c: Removed.
077c9d
            * sysdeps/i386/dl-machine.h (_dl_runtime_resolve_shstk): New
077c9d
            prototype.
077c9d
            (_dl_runtime_profile_shstk): Likewise.
077c9d
            (elf_machine_runtime_setup): Use _dl_runtime_profile_shstk or
077c9d
            _dl_runtime_resolve_shstk if SHSTK is enabled by kernel.
077c9d
    
077c9d
    Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
077c9d
077c9d
diff --git a/sysdeps/i386/dl-cet.c b/sysdeps/i386/dl-cet.c
077c9d
deleted file mode 100644
077c9d
index 5d9a4e8d5179b572..0000000000000000
077c9d
--- a/sysdeps/i386/dl-cet.c
077c9d
+++ /dev/null
077c9d
@@ -1,67 +0,0 @@
077c9d
-/* Linux/i386 CET initializers function.
077c9d
-   Copyright (C) 2018 Free Software Foundation, Inc.
077c9d
-
077c9d
-   The GNU C Library is free software; you can redistribute it and/or
077c9d
-   modify it under the terms of the GNU Lesser General Public
077c9d
-   License as published by the Free Software Foundation; either
077c9d
-   version 2.1 of the License, or (at your option) any later version.
077c9d
-
077c9d
-   The GNU C Library is distributed in the hope that it will be useful,
077c9d
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
077c9d
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
077c9d
-   Lesser General Public License for more details.
077c9d
-
077c9d
-   You should have received a copy of the GNU Lesser General Public
077c9d
-   License along with the GNU C Library; if not, see
077c9d
-   <http://www.gnu.org/licenses/>.  */
077c9d
-
077c9d
-
077c9d
-#define LINKAGE static inline
077c9d
-#define _dl_cet_check cet_check
077c9d
-#include <sysdeps/x86/dl-cet.c>
077c9d
-#undef _dl_cet_check
077c9d
-
077c9d
-#ifdef SHARED
077c9d
-void
077c9d
-_dl_cet_check (struct link_map *main_map, const char *program)
077c9d
-{
077c9d
-  cet_check (main_map, program);
077c9d
-
077c9d
-  if ((GL(dl_x86_feature_1)[0] & GNU_PROPERTY_X86_FEATURE_1_SHSTK))
077c9d
-    {
077c9d
-      /* Replace _dl_runtime_resolve and _dl_runtime_profile with
077c9d
-         _dl_runtime_resolve_shstk and _dl_runtime_profile_shstk,
077c9d
-	 respectively if SHSTK is enabled.  */
077c9d
-      extern void _dl_runtime_resolve (Elf32_Word) attribute_hidden;
077c9d
-      extern void _dl_runtime_resolve_shstk (Elf32_Word) attribute_hidden;
077c9d
-      extern void _dl_runtime_profile (Elf32_Word) attribute_hidden;
077c9d
-      extern void _dl_runtime_profile_shstk (Elf32_Word) attribute_hidden;
077c9d
-      unsigned int i;
077c9d
-      struct link_map *l;
077c9d
-      Elf32_Addr *got;
077c9d
-
077c9d
-      if (main_map->l_info[DT_JMPREL])
077c9d
-	{
077c9d
-	  got = (Elf32_Addr *) D_PTR (main_map, l_info[DT_PLTGOT]);
077c9d
-	  if (got[2] == (Elf32_Addr) &_dl_runtime_resolve)
077c9d
-	    got[2] = (Elf32_Addr) &_dl_runtime_resolve_shstk;
077c9d
-	  else if (got[2] == (Elf32_Addr) &_dl_runtime_profile)
077c9d
-	    got[2] = (Elf32_Addr) &_dl_runtime_profile_shstk;
077c9d
-	}
077c9d
-
077c9d
-      i = main_map->l_searchlist.r_nlist;
077c9d
-      while (i-- > 0)
077c9d
-	{
077c9d
-	  l = main_map->l_initfini[i];
077c9d
-	  if (l->l_info[DT_JMPREL])
077c9d
-	    {
077c9d
-	      got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
077c9d
-	      if (got[2] == (Elf32_Addr) &_dl_runtime_resolve)
077c9d
-		got[2] = (Elf32_Addr) &_dl_runtime_resolve_shstk;
077c9d
-	      else if (got[2] == (Elf32_Addr) &_dl_runtime_profile)
077c9d
-		got[2] = (Elf32_Addr) &_dl_runtime_profile_shstk;
077c9d
-	    }
077c9d
-	}
077c9d
-    }
077c9d
-}
077c9d
-#endif
077c9d
diff --git a/sysdeps/i386/dl-machine.h b/sysdeps/i386/dl-machine.h
077c9d
index 1afdcbd9ea2626e4..f6cfb90e21015250 100644
077c9d
--- a/sysdeps/i386/dl-machine.h
077c9d
+++ b/sysdeps/i386/dl-machine.h
077c9d
@@ -67,6 +67,11 @@ elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
077c9d
   Elf32_Addr *got;
077c9d
   extern void _dl_runtime_resolve (Elf32_Word) attribute_hidden;
077c9d
   extern void _dl_runtime_profile (Elf32_Word) attribute_hidden;
077c9d
+  extern void _dl_runtime_resolve_shstk (Elf32_Word) attribute_hidden;
077c9d
+  extern void _dl_runtime_profile_shstk (Elf32_Word) attribute_hidden;
077c9d
+  /* Check if SHSTK is enabled by kernel.  */
077c9d
+  bool shstk_enabled
077c9d
+    = (GL(dl_x86_feature_1)[0] & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0;
077c9d
 
077c9d
   if (l->l_info[DT_JMPREL] && lazy)
077c9d
     {
077c9d
@@ -93,7 +98,9 @@ elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
077c9d
 	 end in this function.  */
077c9d
       if (__glibc_unlikely (profile))
077c9d
 	{
077c9d
-	  got[2] = (Elf32_Addr) &_dl_runtime_profile;
077c9d
+	  got[2] = (shstk_enabled
077c9d
+		    ? (Elf32_Addr) &_dl_runtime_profile_shstk
077c9d
+		    : (Elf32_Addr) &_dl_runtime_profile);
077c9d
 
077c9d
 	  if (GLRO(dl_profile) != NULL
077c9d
 	      && _dl_name_match_p (GLRO(dl_profile), l))
077c9d
@@ -104,7 +111,9 @@ elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
077c9d
       else
077c9d
 	/* This function will get called to fix up the GOT entry indicated by
077c9d
 	   the offset on the stack, and then jump to the resolved address.  */
077c9d
-	got[2] = (Elf32_Addr) &_dl_runtime_resolve;
077c9d
+	got[2] = (shstk_enabled
077c9d
+		  ? (Elf32_Addr) &_dl_runtime_resolve_shstk
077c9d
+		  : (Elf32_Addr) &_dl_runtime_resolve);
077c9d
     }
077c9d
 
077c9d
   return lazy;