Enable Intel CET in liblto_plugin.so on Intel CET enabled host
[official-gcc.git] / config / cet.m4
blob8b9e01fd492beac573394a5a6773cdc573600f39
1 dnl
2 dnl GCC_CET_FLAGS
3 dnl    (SHELL-CODE_HANDLER)
4 dnl
5 AC_DEFUN([GCC_CET_FLAGS],[dnl
6 GCC_ENABLE(cet, no, ,[enable Intel CET in target libraries],
7            permit yes|no|auto)
8 AC_MSG_CHECKING([for CET support])
10 case "$host" in
11   i[[34567]]86-*-linux* | x86_64-*-linux*)
12     case "$enable_cet" in
13       auto)
14         # Check if target supports multi-byte NOPs
15         # and if assembler supports CET insn.
16         AC_COMPILE_IFELSE(
17          [AC_LANG_PROGRAM(
18           [],
19           [
20 #if !defined(__SSE2__)
21 #error target does not support multi-byte NOPs
22 #else
23 asm ("setssbsy");
24 #endif
25           ])],
26          [enable_cet=yes],
27          [enable_cet=no])
28         ;;
29       yes)
30         # Check if assembler supports CET.
31         AC_COMPILE_IFELSE(
32          [AC_LANG_PROGRAM(
33           [],
34           [asm ("setssbsy");])],
35          [],
36          [AC_MSG_ERROR([assembler with CET support is required for --enable-cet])])
37         ;;
38     esac
39     ;;
40   *)
41     enable_cet=no
42     ;;
43 esac
44 if test x$enable_cet = xyes; then
45   $1="-fcf-protection -mshstk"
46   AC_MSG_RESULT([yes])
47 else
48   AC_MSG_RESULT([no])
52 dnl
53 dnl GCC_CET_HOST_FLAGS
54 dnl    (SHELL-CODE_HANDLER)
55 dnl
56 AC_DEFUN([GCC_CET_HOST_FLAGS],[dnl
57 GCC_ENABLE(cet, auto, ,[enable Intel CET in host libraries],
58            permit yes|no|auto)
59 AC_MSG_CHECKING([for CET support])
61 case "$host" in
62   i[[34567]]86-*-linux* | x86_64-*-linux*)
63     may_have_cet=yes
64     save_CFLAGS="$CFLAGS"
65     CFLAGS="$CFLAGS -fcf-protection"
66     case "$enable_cet" in
67       auto)
68         # Check if target supports multi-byte NOPs
69         # and if assembler supports CET insn.
70         AC_COMPILE_IFELSE(
71          [AC_LANG_PROGRAM(
72           [],
73           [
74 #if !defined(__SSE2__)
75 #error target does not support multi-byte NOPs
76 #else
77 asm ("setssbsy");
78 #endif
79           ])],
80          [enable_cet=yes],
81          [enable_cet=no])
82         ;;
83       yes)
84         # Check if assembler supports CET.
85         AC_COMPILE_IFELSE(
86          [AC_LANG_PROGRAM(
87           [],
88           [asm ("setssbsy");])],
89          [],
90          [AC_MSG_ERROR([assembler with CET support is required for --enable-cet])])
91         ;;
92     esac
93     CFLAGS="$save_CFLAGS"
94     ;;
95   *)
96     may_have_cet=no
97     enable_cet=no
98     ;;
99 esac
101 if test x$may_have_cet = xyes; then
102   save_LDFLAGS="$LDFLAGS"
103   LDFLAGS="$LDFLAGS -Wl,-z,ibt,-z,shstk"
104   AC_TRY_RUN([
105 static void
106 foo (void)
110 static void
111 __attribute__ ((noinline, noclone))
112 xxx (void (*f) (void))
114   f ();
117 static void
118 __attribute__ ((noinline, noclone))
119 bar (void)
121   xxx (foo);
125 main ()
127   bar ();
128   return 0;
130   ],
131   [have_cet=no],
132   [have_cet=yes])
133   LDFLAGS="$save_LDFLAGS"
134   if test x$enable_cet = xno -a x$have_cet = xyes; then
135     AC_MSG_ERROR([Intel CET must be enabled on Intel CET enabled host])
136   fi
138 if test x$enable_cet = xyes; then
139   $1="-fcf-protection"
140   AC_MSG_RESULT([yes])
141 else
142   AC_MSG_RESULT([no])