1dnl
2dnl GCC_CET_FLAGS
3dnl    (SHELL-CODE_HANDLER)
4dnl
5AC_DEFUN([GCC_CET_FLAGS],[dnl
6GCC_ENABLE(cet, no, ,[enable Intel CET in target libraries],
7	   permit yes|no|auto)
8AC_MSG_CHECKING([for CET support])
9
10case "$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
23asm ("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    ;;
43esac
44if test x$enable_cet = xyes; then
45  $1="-fcf-protection -mshstk"
46  AC_MSG_RESULT([yes])
47else
48  AC_MSG_RESULT([no])
49fi
50])
51
52dnl
53dnl GCC_CET_HOST_FLAGS
54dnl    (SHELL-CODE_HANDLER)
55dnl
56AC_DEFUN([GCC_CET_HOST_FLAGS],[dnl
57GCC_ENABLE(cet, auto, ,[enable Intel CET in host libraries],
58	   permit yes|no|auto)
59AC_MSG_CHECKING([for CET support])
60
61case "$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
77asm ("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    ;;
99esac
100
101save_CFLAGS="$CFLAGS"
102CFLAGS="$CFLAGS -fcf-protection=none"
103save_LDFLAGS="$LDFLAGS"
104LDFLAGS="$LDFLAGS -Wl,-z,ibt,-z,shstk"
105if test x$may_have_cet = xyes; then
106  # Check whether -fcf-protection=none -Wl,-z,ibt,-z,shstk work.
107  AC_TRY_LINK(
108    [],[return 0;],
109    [may_have_cet=yes],
110    [may_have_cet=no])
111fi
112
113if test x$may_have_cet = xyes; then
114  if test x$cross_compiling = xno; then
115    AC_TRY_RUN([
116static void
117foo (void)
118{
119}
120
121static void
122__attribute__ ((noinline, noclone))
123xxx (void (*f) (void))
124{
125  f ();
126}
127
128static void
129__attribute__ ((noinline, noclone))
130bar (void)
131{
132  xxx (foo);
133}
134
135int
136main ()
137{
138  bar ();
139  return 0;
140}
141    ],
142    [have_cet=no],
143    [have_cet=yes])
144    if test x$enable_cet = xno -a x$have_cet = xyes; then
145      AC_MSG_ERROR([Intel CET must be enabled on Intel CET enabled host])
146    fi
147  fi
148else
149  # Enable CET in cross compiler if possible so that it will run on both
150  # CET and non-CET hosts.
151  have_cet=yes
152fi
153if test x$enable_cet = xyes; then
154  $1="-fcf-protection"
155  AC_MSG_RESULT([yes])
156else
157  AC_MSG_RESULT([no])
158fi
159CFLAGS="$save_CFLAGS"
160LDFLAGS="$save_LDFLAGS"
161])
162