flag_cf_protection is a bitmask, so a nonzero value does not imply
that shadow stack is enabled: it may hold only CF_BRANCH, or the
CF_SET and CF_CHECK bookkeeping bits.  Test the CF_RETURN bit when
deciding to use an indirect jump for EH return, and when passing
set_cfa to pro_epilogue_adjust_stack.

Assisted-by: Claude Sonnet 5.5

PR target/128409

gcc/ChangeLog:

* config/i386/i386.cc (ix86_expand_epilogue): Check
flag_cf_protection & CF_RETURN instead of flag_cf_protection.

gcc/testsuite/ChangeLog:

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

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

Uros.
diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc
index 92706169866..5a08490abc7 100644
--- a/gcc/config/i386/i386.cc
+++ b/gcc/config/i386/i386.cc
@@ -10575,7 +10575,7 @@ ix86_expand_epilogue (int style)
 
              pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
                                         const0_rtx, style,
-                                        flag_cf_protection);
+                                        flag_cf_protection & CF_RETURN);
            }
          else
            {
@@ -10767,7 +10767,7 @@ ix86_expand_epilogue (int style)
       /* In case of return from EH a simple return cannot be used
         as a return address will be compared with a shadow stack
         return address.  Use indirect jump instead.  */
-      if (style == 2 && flag_cf_protection)
+      if (style == 2 && (flag_cf_protection & CF_RETURN))
        {
          /* Register used in indirect jump must be in word_mode.  But
             Pmode may not be the same as word_mode for x32.  */
diff --git a/gcc/testsuite/gcc.target/i386/pr128409-1.c 
b/gcc/testsuite/gcc.target/i386/pr128409-1.c
new file mode 100644
index 00000000000..41d0b32a979
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr128409-1.c
@@ -0,0 +1,12 @@
+/* PR target/128409 */
+/* { dg-do compile { target *-*-linux } } */
+/* { dg-options "-O2 -fcf-protection=branch -march=x86-64" } */
+
+void
+foo (long offset, void *handler)
+{
+  __builtin_eh_return (offset, handler);
+}
+
+/* { dg-final { scan-assembler-not "\tjmp\[ \t\]+\\*%\[er\]cx" } } */
+/* { dg-final { scan-assembler "\tret" } } */
diff --git a/gcc/testsuite/gcc.target/i386/pr128409-2.c 
b/gcc/testsuite/gcc.target/i386/pr128409-2.c
new file mode 100644
index 00000000000..ec234f4bcab
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr128409-2.c
@@ -0,0 +1,12 @@
+/* PR target/128409 */
+/* { dg-do compile { target *-*-linux* } } */
+/* { dg-options "-O2 -fcf-protection=return -march=x86-64" } */
+
+void
+foo (long offset, void *handler)
+{
+  __builtin_eh_return (offset, handler);
+}
+
+/* { dg-final { scan-assembler-not "\tret" } } */
+/* { dg-final { scan-assembler "\tjmp\[ \t\]+\\*%\[er\]cx" } } */

Reply via email to