cet.m4 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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, auto, ,[enable Intel CET in target libraries],
  7. permit yes|no|auto)
  8. AC_MSG_CHECKING([for CET support])
  9. # NB: Avoid nested save_CFLAGS and save_LDFLAGS.
  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 compiler and assembler support CET insn.
  16. cet_save_CFLAGS="$CFLAGS"
  17. CFLAGS="$CFLAGS -fcf-protection"
  18. AC_COMPILE_IFELSE(
  19. [AC_LANG_PROGRAM(
  20. [],
  21. [
  22. #if !defined(__SSE2__)
  23. #error target does not support multi-byte NOPs
  24. #else
  25. asm ("setssbsy");
  26. #endif
  27. ])],
  28. [enable_cet=yes],
  29. [enable_cet=no])
  30. CFLAGS="$cet_save_CFLAGS"
  31. ;;
  32. yes)
  33. # Check if assembler supports CET.
  34. AC_COMPILE_IFELSE(
  35. [AC_LANG_PROGRAM(
  36. [],
  37. [asm ("setssbsy");])],
  38. [],
  39. [AC_MSG_ERROR([assembler with CET support is required for --enable-cet])])
  40. ;;
  41. esac
  42. ;;
  43. *)
  44. enable_cet=no
  45. ;;
  46. esac
  47. if test x$enable_cet = xyes; then
  48. $1="-fcf-protection -mshstk"
  49. AC_MSG_RESULT([yes])
  50. else
  51. AC_MSG_RESULT([no])
  52. fi
  53. ])
  54. dnl
  55. dnl GCC_CET_HOST_FLAGS
  56. dnl (SHELL-CODE_HANDLER)
  57. dnl
  58. AC_DEFUN([GCC_CET_HOST_FLAGS],[dnl
  59. GCC_ENABLE(cet, auto, ,[enable Intel CET in host libraries],
  60. permit yes|no|auto)
  61. AC_MSG_CHECKING([for CET support])
  62. case "$host" in
  63. i[[34567]]86-*-linux* | x86_64-*-linux*)
  64. may_have_cet=yes
  65. cet_save_CFLAGS="$CFLAGS"
  66. CFLAGS="$CFLAGS -fcf-protection"
  67. case "$enable_cet" in
  68. auto)
  69. # Check if target supports multi-byte NOPs
  70. # and if compiler and assembler support CET.
  71. AC_COMPILE_IFELSE(
  72. [AC_LANG_PROGRAM(
  73. [],
  74. [
  75. #if !defined(__SSE2__)
  76. #error target does not support multi-byte NOPs
  77. #else
  78. asm ("setssbsy");
  79. #endif
  80. ])],
  81. [enable_cet=yes],
  82. [enable_cet=no])
  83. ;;
  84. yes)
  85. # Check if compiler and assembler support CET.
  86. AC_COMPILE_IFELSE(
  87. [AC_LANG_PROGRAM(
  88. [],
  89. [asm ("setssbsy");])],
  90. [support_cet=yes],
  91. [support_cet=no])
  92. if test $support_cet = "no"; then
  93. if test x$enable_bootstrap != xno \
  94. && test -z "${with_build_subdir}" \
  95. && (test ! -f ../stage_current \
  96. || test `cat ../stage_current` != "stage1"); then
  97. # Require CET support only for the final GCC build.
  98. AC_MSG_ERROR([compiler and assembler with CET support are required for --enable-cet])
  99. else
  100. # Don't enable CET without CET support for non-bootstrap
  101. # build, in stage1 nor for build support.
  102. enable_cet=no
  103. fi
  104. fi
  105. ;;
  106. esac
  107. CFLAGS="$cet_save_CFLAGS"
  108. ;;
  109. *)
  110. may_have_cet=no
  111. enable_cet=no
  112. ;;
  113. esac
  114. cet_save_CFLAGS="$CFLAGS"
  115. CFLAGS="$CFLAGS -fcf-protection=none"
  116. cet_save_LDFLAGS="$LDFLAGS"
  117. LDFLAGS="$LDFLAGS -Wl,-z,ibt,-z,shstk"
  118. if test x$may_have_cet = xyes; then
  119. # Check whether -fcf-protection=none -Wl,-z,ibt,-z,shstk work.
  120. AC_TRY_LINK(
  121. [],[return 0;],
  122. [may_have_cet=yes],
  123. [may_have_cet=no])
  124. fi
  125. if test x$may_have_cet = xyes; then
  126. if test x$cross_compiling = xno; then
  127. AC_TRY_RUN([
  128. int
  129. main ()
  130. {
  131. asm ("endbr32");
  132. return 0;
  133. }
  134. ],
  135. [have_multi_byte_nop=yes],
  136. [have_multi_byte_nop=no])
  137. have_cet=no
  138. if test x$have_multi_byte_nop = xyes; then
  139. AC_TRY_RUN([
  140. static void
  141. foo (void)
  142. {
  143. }
  144. static void
  145. __attribute__ ((noinline, noclone))
  146. xxx (void (*f) (void))
  147. {
  148. f ();
  149. }
  150. static void
  151. __attribute__ ((noinline, noclone))
  152. bar (void)
  153. {
  154. xxx (foo);
  155. }
  156. int
  157. main ()
  158. {
  159. bar ();
  160. return 0;
  161. }
  162. ],
  163. [have_cet=no],
  164. [have_cet=yes])
  165. fi
  166. if test x$enable_cet = xno -a x$have_cet = xyes; then
  167. AC_MSG_ERROR([Intel CET must be enabled on Intel CET enabled host])
  168. fi
  169. fi
  170. else
  171. # Enable CET in cross compiler if possible so that it will run on both
  172. # CET and non-CET hosts.
  173. have_cet=yes
  174. fi
  175. if test x$enable_cet = xyes; then
  176. $1="-fcf-protection"
  177. AC_MSG_RESULT([yes])
  178. else
  179. AC_MSG_RESULT([no])
  180. fi
  181. CFLAGS="$cet_save_CFLAGS"
  182. LDFLAGS="$cet_save_LDFLAGS"
  183. ])