PLT should be avoided with shadow stack in 32-bit mode if more than 2
parameters are passed in registers since only 2 parameters can be passed
in registers for external function calls via PLT with shadow stack
enabled.

OK for trunk if there is no regressions?

H.J.
---
gcc/

        PR target/81780
        * config/i386/i386.c (ix86_noplt_attribute_p): New function.
        (ix86_expand_call): Use it.
        (ix86_nopic_noplt_attribute_p): Likewise.

gcc/testsuite/

        PR target/81780
        * gcc.target/i386/pr81780-1.c: New test.
        * gcc.target/i386/pr81780-2.c: Likewise.
        * gcc.target/i386/pr81780-3.c: Likewise.
        * gcc.target/i386/pr81780-4.c: Likewise.
---
 gcc/config/i386/i386.c                    | 38 +++++++++++++++++++++----------
 gcc/testsuite/gcc.target/i386/pr81780-1.c | 14 ++++++++++++
 gcc/testsuite/gcc.target/i386/pr81780-2.c | 14 ++++++++++++
 gcc/testsuite/gcc.target/i386/pr81780-3.c | 14 ++++++++++++
 gcc/testsuite/gcc.target/i386/pr81780-4.c | 14 ++++++++++++
 5 files changed, 82 insertions(+), 12 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/i386/pr81780-1.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr81780-2.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr81780-3.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr81780-4.c

diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
index d2188ad6086..1f9464a2e87 100644
--- a/gcc/config/i386/i386.c
+++ b/gcc/config/i386/i386.c
@@ -101,6 +101,7 @@ static void ix86_print_operand_address_as (FILE *, rtx, 
addr_space_t, bool);
 static bool ix86_save_reg (unsigned int, bool, bool);
 static bool ix86_function_naked (const_tree);
 static bool ix86_notrack_prefixed_insn_p (rtx);
+static bool ix86_noplt_attribute_p (rtx);
 
 #ifndef CHECK_STACK_LIMIT
 #define CHECK_STACK_LIMIT (-1)
@@ -27754,10 +27755,7 @@ ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
          && GET_CODE (addr) == SYMBOL_REF
          && !SYMBOL_REF_LOCAL_P (addr))
        {
-         if (flag_plt
-             && (SYMBOL_REF_DECL (addr) == NULL_TREE
-                 || !lookup_attribute ("noplt",
-                                       DECL_ATTRIBUTES (SYMBOL_REF_DECL 
(addr)))))
+         if (!ix86_noplt_attribute_p (addr))
            {
              if (!TARGET_64BIT
                  || (ix86_cmodel == CM_LARGE_PIC
@@ -27935,6 +27933,29 @@ ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
 }
 
 /* Return true if the function being called was marked with attribute
+   "noplt" or using -fno-plt.  PLT should be avoided with shadow stack
+   if more than 2 parameters are passed in registers since only 2
+   parameters can be passed in registers for external function calls
+   via PLT with shadow stack enabled.  */
+
+static bool
+ix86_noplt_attribute_p (rtx call_op)
+{
+  if (!flag_plt)
+    return true;
+
+  tree decl = SYMBOL_REF_DECL (call_op);
+  if (!decl)
+    return false;
+
+  return (lookup_attribute ("noplt", DECL_ATTRIBUTES (decl))
+         || (!TARGET_64BIT
+             && (flag_cf_protection & CF_RETURN)
+             && TARGET_SHSTK
+             && ix86_function_regparm (TREE_TYPE (decl), decl) > 2));
+}
+
+/* Return true if the function being called was marked with attribute
    "noplt" or using -fno-plt and we are compiling for non-PIC.  We need
    to handle the non-PIC case in the backend because there is no easy
    interface for the front-end to force non-PLT calls to use the GOT.
@@ -27950,14 +27971,7 @@ ix86_nopic_noplt_attribute_p (rtx call_op)
       || SYMBOL_REF_LOCAL_P (call_op))
     return false;
 
-  tree symbol_decl = SYMBOL_REF_DECL (call_op);
-
-  if (!flag_plt
-      || (symbol_decl != NULL_TREE
-          && lookup_attribute ("noplt", DECL_ATTRIBUTES (symbol_decl))))
-    return true;
-
-  return false;
+  return ix86_noplt_attribute_p (call_op);
 }
 
 /* Output the assembly for a call instruction.  */
diff --git a/gcc/testsuite/gcc.target/i386/pr81780-1.c 
b/gcc/testsuite/gcc.target/i386/pr81780-1.c
new file mode 100644
index 00000000000..f5a28020782
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr81780-1.c
@@ -0,0 +1,14 @@
+/* { dg-do compile { target { *-*-linux* && ia32 } } } */
+/* { dg-require-effective-target got32x_reloc } */
+/* { dg-options "-fcf-protection -mcet -O2 -fno-pic -fplt" } */
+
+extern void bar (int) __attribute__ ((regparm (3)));
+
+int
+foo (int i)
+{
+  bar (i);
+  return 0;
+}
+
+/* { dg-final { scan-assembler "call\[ \t\]*.bar@GOT" } } */
diff --git a/gcc/testsuite/gcc.target/i386/pr81780-2.c 
b/gcc/testsuite/gcc.target/i386/pr81780-2.c
new file mode 100644
index 00000000000..59f4c5f4d70
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr81780-2.c
@@ -0,0 +1,14 @@
+/* { dg-do compile { target { *-*-linux* && ia32 } } } */
+/* { dg-require-effective-target got32x_reloc } */
+/* { dg-options "-fcf-protection -mcet -O2 -fno-pic -fplt -mregparm=3" } */
+
+extern void bar (int);
+
+int
+foo (int i)
+{
+  bar (i);
+  return 0;
+}
+
+/* { dg-final { scan-assembler "call\[ \t\]*.bar@GOT" } } */
diff --git a/gcc/testsuite/gcc.target/i386/pr81780-3.c 
b/gcc/testsuite/gcc.target/i386/pr81780-3.c
new file mode 100644
index 00000000000..3bc0c197fbe
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr81780-3.c
@@ -0,0 +1,14 @@
+/* { dg-do compile { target { *-*-linux* && ia32 } } } */
+/* { dg-require-effective-target got32x_reloc } */
+/* { dg-options "-fcf-protection -mcet -O2 -fplt -fpic" } */
+
+extern void bar (int) __attribute__ ((regparm (3)));
+
+int
+foo (int i)
+{
+  bar (i);
+  return 0;
+}
+
+/* { dg-final { scan-assembler "call\[ \t\]*.bar@GOT" } } */
diff --git a/gcc/testsuite/gcc.target/i386/pr81780-4.c 
b/gcc/testsuite/gcc.target/i386/pr81780-4.c
new file mode 100644
index 00000000000..875dff1765c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr81780-4.c
@@ -0,0 +1,14 @@
+/* { dg-do compile { target { *-*-linux* && ia32 } } } */
+/* { dg-require-effective-target got32x_reloc } */
+/* { dg-options "-fcf-protection -mcet -O2 -fpic -fplt -mregparm=3" } */
+
+extern void bar (int);
+
+int
+foo (int i)
+{
+  bar (i);
+  return 0;
+}
+
+/* { dg-final { scan-assembler "call\[ \t\]*.bar@GOT" } } */
-- 
2.13.6

Reply via email to