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