x86: Error on -fcf-protection with incompatible target
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 14 Jan 2021 13:56:46 +0000 (05:56 -0800)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 14 Jan 2021 15:42:47 +0000 (07:42 -0800)
-fcf-protection with CF_BRANCH inserts ENDBR32 at function entries.
ENDBR32 is NOP only on 64-bit processors and 32-bit TARGET_CMOV
processors.  Issue an error for -fcf-protection with CF_BRANCH when
compiling for 32-bit non-TARGET_CMOV targets.

gcc/

PR target/98667
* config/i386/i386-options.c (ix86_option_override_internal):
Issue an error for -fcf-protection with CF_BRANCH when compiling
for 32-bit non-TARGET_CMOV targets.

gcc/testsuite/

PR target/98667
* gcc.target/i386/pr98667-1.c: New file.
* gcc.target/i386/pr98667-2.c: Likewise.
* gcc.target/i386/pr98667-3.c: Likewise.

gcc/config/i386/i386-options.c
gcc/testsuite/gcc.target/i386/pr98667-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr98667-2.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr98667-3.c [new file with mode: 0644]

index 6819a04238965f0ad63b10323823caa2fb8b147c..a70f6edf7b0bfa6994db372c2507dbacb5526646 100644 (file)
@@ -3014,8 +3014,14 @@ ix86_option_override_internal (bool main_args_p,
     }
 
   if (opts->x_flag_cf_protection != CF_NONE)
-    opts->x_flag_cf_protection
+    {
+      if ((opts->x_flag_cf_protection & CF_BRANCH) == CF_BRANCH
+         && !TARGET_64BIT && !TARGET_CMOV)
+       error ("%<-fcf-protection%> is not compatible with this target");
+
+      opts->x_flag_cf_protection
       = (cf_protection_level) (opts->x_flag_cf_protection | CF_SET);
+    }
 
   if (ix86_tune_features [X86_TUNE_AVOID_256FMA_CHAINS])
     SET_OPTION_IF_UNSET (opts, opts_set, param_avoid_fma_max_bits, 256);
diff --git a/gcc/testsuite/gcc.target/i386/pr98667-1.c b/gcc/testsuite/gcc.target/i386/pr98667-1.c
new file mode 100644 (file)
index 0000000..5bf0c92
--- /dev/null
@@ -0,0 +1,9 @@
+/* { dg-do compile { target ia32 } } */
+/* { dg-options "-O2 -fcf-protection -march=i486" } */
+
+void
+test (void)
+{
+}
+
+/* { dg-error "'-fcf-protection' is not compatible with this target" "" { target *-*-* } 0 } */
diff --git a/gcc/testsuite/gcc.target/i386/pr98667-2.c b/gcc/testsuite/gcc.target/i386/pr98667-2.c
new file mode 100644 (file)
index 0000000..bc3a78c
--- /dev/null
@@ -0,0 +1,9 @@
+/* { dg-do compile { target ia32 } } */
+/* { dg-options "-O2 -fcf-protection=branch -march=i486" } */
+
+void
+test (void)
+{
+}
+
+/* { dg-error "'-fcf-protection' is not compatible with this target" "" { target *-*-* } 0 } */
diff --git a/gcc/testsuite/gcc.target/i386/pr98667-3.c b/gcc/testsuite/gcc.target/i386/pr98667-3.c
new file mode 100644 (file)
index 0000000..a6ea6d0
--- /dev/null
@@ -0,0 +1,7 @@
+/* { dg-do compile { target ia32 } } */
+/* { dg-options "-O2 -fcf-protection=return -march=i486" } */
+
+void
+test (void)
+{
+}