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