diff mbox series

[pushed] i386: Do not define __CET__ for -fcf-protection=check [PR128373]

Message ID CAFULd4aSckRkJvbY2VrLBNC8chSf7oKh_4ztG4Atjzcmq3xLMg@mail.gmail.com
State New
Headers show
Series [pushed] i386: Do not define __CET__ for -fcf-protection=check [PR128373] | expand

Commit Message

Uros Bizjak Nov. 3, 2026, 5:44 p.m. UTC
-fcf-protection=check is ignored at compile time, but it sets
flag_cf_protection to CF_CHECK | CF_SET, so ix86_target_macros defined
__CET__=8 even though neither IBT nor SHSTK is enabled.  Assembly files
including <cet.h> then emitted a GNU_PROPERTY_X86_FEATURE_1_AND note
with a zero value.

Define __CET__ only when CF_BRANCH or CF_RETURN is set, and expose
only those bits in its value.

Assisted-by: Claude Sonnet 5.5

PR target/128373

gcc/ChangeLog:

* config/i386/i386-c.cc (ix86_target_macros):
Define __CET__ only when CF_BRANCH or CF_RETURN is set.

gcc/testsuite/ChangeLog:

* gcc.target/i386/pr128373-1.c: New test.
* gcc.target/i386/pr128373-2.c: Ditto.

Bootstrapped and regression tested on x86_64-pc-linux-gnu {,-m32}.

Uros.

Comments

Richard Biener Nov. 4, 2026, 8:06 a.m. UTC | #1
On Tue, Nov 3, 2026 at 6:45 PM Uros Bizjak <ubizjak@gmail.com> wrote:
>
> -fcf-protection=check is ignored at compile time, but it sets
> flag_cf_protection to CF_CHECK | CF_SET, so ix86_target_macros defined
> __CET__=8 even though neither IBT nor SHSTK is enabled.  Assembly files
> including <cet.h> then emitted a GNU_PROPERTY_X86_FEATURE_1_AND note
> with a zero value.
>
> Define __CET__ only when CF_BRANCH or CF_RETURN is set, and expose
> only those bits in its value.

LGTM.

> Assisted-by: Claude Sonnet 5.5
>
> PR target/128373
>
> gcc/ChangeLog:
>
> * config/i386/i386-c.cc (ix86_target_macros):
> Define __CET__ only when CF_BRANCH or CF_RETURN is set.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.target/i386/pr128373-1.c: New test.
> * gcc.target/i386/pr128373-2.c: Ditto.
>
> Bootstrapped and regression tested on x86_64-pc-linux-gnu {,-m32}.
>
> Uros.
diff mbox series

Patch

diff --git a/gcc/config/i386/i386-c.cc b/gcc/config/i386/i386-c.cc
index 6750332ee9c..20f6f820c1c 100644
--- a/gcc/config/i386/i386-c.cc
+++ b/gcc/config/i386/i386-c.cc
@@ -977,8 +977,9 @@  ix86_target_macros (void)
   cpp_define (parse_in, "__SEG_FS");
   cpp_define (parse_in, "__SEG_GS");
 
-  if (flag_cf_protection != CF_NONE)
-    cpp_define_formatted (parse_in, "__CET__=%d", flag_cf_protection & ~CF_SET);
+  if (flag_cf_protection & (CF_BRANCH | CF_RETURN))
+    cpp_define_formatted (parse_in, "__CET__=%d",
+			  flag_cf_protection & (CF_BRANCH | CF_RETURN));
 }
 
 
diff --git a/gcc/testsuite/gcc.target/i386/pr128373-1.c b/gcc/testsuite/gcc.target/i386/pr128373-1.c
new file mode 100644
index 00000000000..c409a5e5d37
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr128373-1.c
@@ -0,0 +1,7 @@ 
+/* PR target/128373 */
+/* { dg-do preprocess { target *-*-linux* } } */
+/* { dg-options "-fcf-protection=check -march=x86-64" } */
+
+#ifdef __CET__
+# error "__CET__ must not be defined with -fcf-protection=check"
+#endif
diff --git a/gcc/testsuite/gcc.target/i386/pr128373-2.c b/gcc/testsuite/gcc.target/i386/pr128373-2.c
new file mode 100644
index 00000000000..2a6a07aa0b4
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr128373-2.c
@@ -0,0 +1,7 @@ 
+/* PR target/128373 */
+/* { dg-do preprocess { target *-*-linux* } } */
+/* { dg-options "-fcf-protection=full -fcf-protection=check -march=x86-64" } */
+
+#if __CET__ != 3
+# error "__CET__ must be 3 with -fcf-protection=full"
+#endif