]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
x86: Insert ENDBR if function will be called indirectly
authorH.J. Lu <hjl.tools@gmail.com>
Fri, 17 Apr 2020 22:23:27 +0000 (15:23 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Fri, 17 Apr 2020 22:23:50 +0000 (15:23 -0700)
Since constant_call_address_operand has

;; Test for a pc-relative call operand
(define_predicate "constant_call_address_operand"
  (match_code "symbol_ref")
{
  if (ix86_cmodel == CM_LARGE || ix86_cmodel == CM_LARGE_PIC
      || flag_force_indirect_call)
    return false;
  if (TARGET_DLLIMPORT_DECL_ATTRIBUTES && SYMBOL_REF_DLLIMPORT_P (op))
    return false;
  return true;
})

even if cgraph_node::get (cfun->decl)->only_called_directly_p () returns
false, the fuction may still be called indirectly.  Copy the logic from
constant_call_address_operand to rest_of_insert_endbranch to insert ENDBR
at function entry if function will be called indirectly.

NB: gcc.target/i386/pr94417-2.c is updated to expect 4 ENDBRs, instead
of 2, since only GCC 10 has the fix for PR target/89355 not to insert
ENDBR after NOTE_INSN_DELETED_LABEL.

gcc/

Backport from master
PR target/94417
* config/i386/i386.c (rest_of_insert_endbranch): Insert ENDBR at
function entry if function will be called indirectly.

gcc/testsuite/

Backport from master
PR target/94417
* gcc.target/i386/pr94417-1.c: New test.
* gcc.target/i386/pr94417-2.c: Likewise.
* gcc.target/i386/pr94417-3.c: Likewise.

(cherry picked from commit c5f379653964a1d2c7037b2de3e947a48370a198)

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

index 29f9f52067d46f0cfe2a228af77ae3079961db15..7df3a0d0990464d095a25cd60860e2f317027a9d 100644 (file)
@@ -1,3 +1,12 @@
+2020-04-17  H.J. Lu  <hongjiu.lu@intel.com>
+
+       Backport from master
+       2020-04-08  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR target/94417
+       * config/i386/i386.c (rest_of_insert_endbranch): Insert ENDBR at
+       function entry if function will be called indirectly.
+
 2020-04-17  Kewen Lin  <linkw@gcc.gnu.org>
 
        Backport from mainline
index 5d12d82db5b1f667576693c156f2218289d9c5ba..3891d44ce8563a19e5d504933410a76dd625f303 100644 (file)
@@ -2535,7 +2535,12 @@ rest_of_insert_endbranch (void)
       && (!flag_manual_endbr
          || lookup_attribute ("cf_check",
                               DECL_ATTRIBUTES (cfun->decl)))
-      && !cgraph_node::get (cfun->decl)->only_called_directly_p ())
+      && (!cgraph_node::get (cfun->decl)->only_called_directly_p ()
+         || ix86_cmodel == CM_LARGE
+         || ix86_cmodel == CM_LARGE_PIC
+         || flag_force_indirect_call
+         || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
+             && DECL_DLLIMPORT_P (cfun->decl))))
     {
       /* Queue ENDBR insertion to x86_function_profiler.  */
       if (crtl->profile && flag_fentry)
index 8742d27ce2f4b63a40176f678c16a24ed352aa4a..3df9dd78d0cfbbf4cb212c7693e46391ffa9f149 100644 (file)
@@ -1,3 +1,13 @@
+2020-04-17  H.J. Lu  <hongjiu.lu@intel.com>
+
+       Backport from master
+       2020-04-08  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR target/94417
+       * gcc.target/i386/pr94417-1.c: New test.
+       * gcc.target/i386/pr94417-2.c: Likewise.
+       * gcc.target/i386/pr94417-3.c: Likewise.
+
 2020-04-17  Kewen Lin  <linkw@gcc.gnu.org>
 
        Backport from mainline
diff --git a/gcc/testsuite/gcc.target/i386/pr94417-1.c b/gcc/testsuite/gcc.target/i386/pr94417-1.c
new file mode 100644 (file)
index 0000000..5bbe057
--- /dev/null
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target lp64 } */
+/* { dg-options "-O2 -fcf-protection -mcmodel=large" } */
+/* { dg-final { scan-assembler-times {\mendbr} 2 } } */
+
+extern void ext (void);
+
+__attribute((noclone, noinline))
+static
+void
+foo (void)
+{
+  ext ();
+}
+
+void
+bar (void)
+{
+  foo ();
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr94417-2.c b/gcc/testsuite/gcc.target/i386/pr94417-2.c
new file mode 100644 (file)
index 0000000..9e9c277
--- /dev/null
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target lp64 } */
+/* { dg-require-effective-target fpic } */
+/* { dg-options "-O2 -fpic -mcmodel=large -fcf-protection" } */
+/* { dg-final { scan-assembler-times {\mendbr} 4 } } */
+
+extern void ext (void);
+
+__attribute((noclone, noinline))
+static
+void
+foo (void)
+{
+  ext ();
+}
+
+void
+bar (void)
+{
+  foo ();
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr94417-3.c b/gcc/testsuite/gcc.target/i386/pr94417-3.c
new file mode 100644 (file)
index 0000000..07c4517
--- /dev/null
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fcf-protection -mforce-indirect-call" } */
+/* { dg-final { scan-assembler-times {\mendbr} 2 } } */
+
+extern void ext (void);
+
+__attribute((noclone, noinline))
+static
+void
+foo (void)
+{
+  ext ();
+}
+
+void
+bar (void)
+{
+  foo ();
+}