'Retpoline' mitigation technique for Spectre (branch target injection) [CVE-2017-5715]: https://security.googleblog.com/2018/01/more-details-about-mitigations-for-cpu_4.html https://support.google.com/faqs/answer/7625886 https://spectreattack.com/ https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-5715 Patch copied from the 'retpoline-20180107' branch of upstream source repository (please add new / update existing patches when new 'retpoline-xxxxxxxx' branch appears): http://git.infradead.org/users/dwmw2/gcc-retpoline.git From 29d5a3f23c18c96944dd3230a41380a6edcd25fd Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Tue, 5 Dec 2017 13:29:06 -0800 Subject: [PATCH 11/17] Add -mfunction-return= and function_return attribute Add -mfunction-return= and function_return attribute tests -mfunction-return=thunk Convert function return instruction to PC-relative call thunk. -mfunction-return=thunk-inline Convert function return instruction to PC-relative call thunk with thunk inlined. -mfunction-return=thunk-extern Convert function return instruction to PC-relative call to external thunk. Add function_return attribute to function declaration __attribute__ ((function_return("thunk"))) __attribute__ ((function_return("thunk-inline"))) __attribute__ ((function_return("thunk-extern"))) __attribute__ ((function_return("keep"))) --- gcc/config/i386/i386-protos.h | 1 + gcc/config/i386/i386.c | 146 +++++++++++++++++++++++++-- gcc/config/i386/i386.h | 3 + gcc/config/i386/i386.md | 9 +- gcc/config/i386/i386.opt | 6 +- gcc/testsuite/gcc.target/i386/ret-thunk-1.c | 12 +++ gcc/testsuite/gcc.target/i386/ret-thunk-10.c | 22 ++++ gcc/testsuite/gcc.target/i386/ret-thunk-11.c | 22 ++++ gcc/testsuite/gcc.target/i386/ret-thunk-12.c | 21 ++++ gcc/testsuite/gcc.target/i386/ret-thunk-13.c | 21 ++++ gcc/testsuite/gcc.target/i386/ret-thunk-14.c | 21 ++++ gcc/testsuite/gcc.target/i386/ret-thunk-15.c | 21 ++++ gcc/testsuite/gcc.target/i386/ret-thunk-16.c | 18 ++++ gcc/testsuite/gcc.target/i386/ret-thunk-2.c | 12 +++ gcc/testsuite/gcc.target/i386/ret-thunk-3.c | 12 +++ gcc/testsuite/gcc.target/i386/ret-thunk-4.c | 12 +++ gcc/testsuite/gcc.target/i386/ret-thunk-5.c | 14 +++ gcc/testsuite/gcc.target/i386/ret-thunk-6.c | 13 +++ gcc/testsuite/gcc.target/i386/ret-thunk-7.c | 13 +++ gcc/testsuite/gcc.target/i386/ret-thunk-8.c | 14 +++ gcc/testsuite/gcc.target/i386/ret-thunk-9.c | 23 +++++ 21 files changed, 421 insertions(+), 15 deletions(-) create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-1.c create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-10.c create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-11.c create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-12.c create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-13.c create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-14.c create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-15.c create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-16.c create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-2.c create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-3.c create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-4.c create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-5.c create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-6.c create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-7.c create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-8.c create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-9.c diff --git a/gcc/config/i386/i386-protos.h b/gcc/config/i386/i386-protos.h index b746429f420..213663811de 100644 --- a/gcc/config/i386/i386-protos.h +++ b/gcc/config/i386/i386-protos.h @@ -316,6 +316,7 @@ extern enum attr_cpu ix86_schedule; extern const char * ix86_output_call_insn (rtx_insn *insn, rtx call_op); extern const char * ix86_output_indirect_jmp (rtx call_op); +extern const char * ix86_output_function_return (bool long_p); extern bool ix86_operands_ok_for_move_multiple (rtx *operands, bool load, enum machine_mode mode); diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c index be1ff4752a9..7ae3523095c 100644 --- a/gcc/config/i386/i386.c +++ b/gcc/config/i386/i386.c @@ -7166,6 +7166,31 @@ ix86_set_indirect_branch_type (tree fndecl) else cfun->machine->indirect_branch_type = ix86_indirect_branch; } + + if (cfun->machine->function_return_type == indirect_branch_unset) + { + tree attr = lookup_attribute ("function_return", + DECL_ATTRIBUTES (fndecl)); + if (attr != NULL) + { + tree args = TREE_VALUE (attr); + if (args == NULL) + gcc_unreachable (); + tree cst = TREE_VALUE (args); + if (strcmp (TREE_STRING_POINTER (cst), "keep") == 0) + cfun->machine->function_return_type = indirect_branch_keep; + else if (strcmp (TREE_STRING_POINTER (cst), "thunk") == 0) + cfun->machine->function_return_type = indirect_branch_thunk; + else if (strcmp (TREE_STRING_POINTER (cst), "thunk-inline") == 0) + cfun->machine->function_return_type = indirect_branch_thunk_inline; + else if (strcmp (TREE_STRING_POINTER (cst), "thunk-extern") == 0) + cfun->machine->function_return_type = indirect_branch_thunk_extern; + else + gcc_unreachable (); + } + else + cfun->machine->function_return_type = ix86_function_return; + } } /* Establish appropriate back-end context for processing the function @@ -11958,8 +11983,12 @@ static int indirect_thunks_bnd_used; /* Fills in the label name that should be used for the indirect thunk. */ static void -indirect_thunk_name (char name[32], int regno, bool need_bnd_p) +indirect_thunk_name (char name[32], int regno, bool need_bnd_p, + bool ret_p) { + if (regno >= 0 && ret_p) + gcc_unreachable (); + if (USE_HIDDEN_LINKONCE) { const char *bnd = need_bnd_p ? "_bnd" : ""; @@ -11974,7 +12003,10 @@ indirect_thunk_name (char name[32], int regno, bool need_bnd_p) bnd, reg_prefix, reg_names[regno]); } else - sprintf (name, "__x86.indirect_thunk%s", bnd); + { + const char *ret = ret_p ? "return" : "indirect"; + sprintf (name, "__x86.%s_thunk%s", ret, bnd); + } } else { @@ -11987,10 +12019,20 @@ indirect_thunk_name (char name[32], int regno, bool need_bnd_p) } else { - if (need_bnd_p) - ASM_GENERATE_INTERNAL_LABEL (name, "LITB", 0); + if (ret_p) + { + if (need_bnd_p) + ASM_GENERATE_INTERNAL_LABEL (name, "LRTB", 0); + else + ASM_GENERATE_INTERNAL_LABEL (name, "LRT", 0); + } else - ASM_GENERATE_INTERNAL_LABEL (name, "LIT", 0); + { + if (need_bnd_p) + ASM_GENERATE_INTERNAL_LABEL (name, "LITB", 0); + else + ASM_GENERATE_INTERNAL_LABEL (name, "LIT", 0); + } } } } @@ -12071,7 +12113,7 @@ output_indirect_thunk_function (bool need_bnd_p, int regno) tree decl; /* Create __x86.indirect_thunk/__x86.indirect_thunk_bnd. */ - indirect_thunk_name (name, regno, need_bnd_p); + indirect_thunk_name (name, regno, need_bnd_p, false); decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name), build_function_type_list (void_type_node, NULL_TREE)); @@ -12114,6 +12156,35 @@ output_indirect_thunk_function (bool need_bnd_p, int regno) ASM_OUTPUT_LABEL (asm_out_file, name); } + if (regno < 0) + { + /* Create alias for __x86.return_thunk/__x86.return_thunk_bnd. */ + char alias[32]; + + indirect_thunk_name (alias, regno, need_bnd_p, true); + ASM_OUTPUT_DEF (asm_out_file, alias, name); +#if TARGET_MACHO + if (TARGET_MACHO) + { + fputs ("\t.weak_definition\t", asm_out_file); + assemble_name (asm_out_file, alias); + fputs ("\n\t.private_extern\t", asm_out_file); + assemble_name (asm_out_file, alias); + putc ('\n', asm_out_file); + } +#else + if (USE_HIDDEN_LINKONCE) + { + fputs ("\t.globl\t", asm_out_file); + assemble_name (asm_out_file, alias); + putc ('\n', asm_out_file); + fputs ("\t.hidden\t", asm_out_file); + assemble_name (asm_out_file, alias); + putc ('\n', asm_out_file); + } +#endif + } + DECL_INITIAL (decl) = make_node (BLOCK); current_function_decl = decl; allocate_struct_function (decl, false); @@ -28736,7 +28807,7 @@ ix86_output_indirect_branch (rtx call_op, const char *xasm, indirect_thunk_needed = true; } } - indirect_thunk_name (thunk_name_buf, regno, need_bnd_p); + indirect_thunk_name (thunk_name_buf, regno, need_bnd_p, false); thunk_name = thunk_name_buf; } else @@ -28860,6 +28931,43 @@ ix86_output_indirect_jmp (rtx call_op) return "%!jmp\t%A0"; } +const char * +ix86_output_function_return (bool long_p) +{ + if (cfun->machine->function_return_type != indirect_branch_keep) + { + char thunk_name[32]; + bool need_bnd_p = ix86_bnd_prefixed_insn_p (current_output_insn); + + if (cfun->machine->function_return_type + != indirect_branch_thunk_inline) + { + bool need_thunk = (cfun->machine->function_return_type + == indirect_branch_thunk); + indirect_thunk_name (thunk_name, -1, need_bnd_p, true); + if (need_bnd_p) + { + indirect_thunk_bnd_needed |= need_thunk; + fprintf (asm_out_file, "\tbnd jmp\t%s\n", thunk_name); + } + else + { + indirect_thunk_needed |= need_thunk; + fprintf (asm_out_file, "\tjmp\t%s\n", thunk_name); + } + } + else + output_indirect_thunk (need_bnd_p, -1); + + return ""; + } + + if (!long_p || ix86_bnd_prefixed_insn_p (current_output_insn)) + return "%!ret"; + + return "rep%; ret"; +} + /* Output the assembly for a call instruction. */ const char * @@ -41916,6 +42024,28 @@ ix86_handle_fndecl_attribute (tree *node, tree name, tree args, int, } } + if (is_attribute_p ("function_return", name)) + { + tree cst = TREE_VALUE (args); + if (TREE_CODE (cst) != STRING_CST) + { + warning (OPT_Wattributes, + "%qE attribute requires a string constant argument", + name); + *no_add_attrs = true; + } + else if (strcmp (TREE_STRING_POINTER (cst), "keep") != 0 + && strcmp (TREE_STRING_POINTER (cst), "thunk") != 0 + && strcmp (TREE_STRING_POINTER (cst), "thunk-inline") != 0 + && strcmp (TREE_STRING_POINTER (cst), "thunk-extern") != 0) + { + warning (OPT_Wattributes, + "argument to %qE attribute is not " + "(keep|thunk|thunk-inline|thunk-extern)", name); + *no_add_attrs = true; + } + } + return NULL_TREE; } @@ -46212,6 +46342,8 @@ static const struct attribute_spec ix86_attribute_table[] = ix86_handle_no_caller_saved_registers_attribute, false }, { "indirect_branch", 1, 1, true, false, false, ix86_handle_fndecl_attribute, false }, + { "function_return", 1, 1, true, false, false, + ix86_handle_fndecl_attribute, false }, /* End element. */ { NULL, 0, 0, false, false, false, NULL, false } diff --git a/gcc/config/i386/i386.h b/gcc/config/i386/i386.h index a9c199a107c..f248f3ba2f5 100644 --- a/gcc/config/i386/i386.h +++ b/gcc/config/i386/i386.h @@ -2607,6 +2607,9 @@ struct GTY(()) machine_function { /* How to generate indirec branch. */ ENUM_BITFIELD(indirect_branch) indirect_branch_type : 3; + /* How to generate function return. */ + ENUM_BITFIELD(indirect_branch) function_return_type : 3; + /* If true, the current function is a function specified with the "interrupt" or "no_caller_saved_registers" attribute. */ BOOL_BITFIELD no_caller_saved_registers : 1; diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index 01b7b2039e6..00a9afef225 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -12288,7 +12288,7 @@ (define_insn "simple_return_internal" [(simple_return)] "reload_completed" - "%!ret" + "* return ix86_output_function_return (false);" [(set_attr "length" "1") (set_attr "atom_unit" "jeu") (set_attr "length_immediate" "0") @@ -12310,12 +12310,7 @@ [(simple_return) (unspec [(const_int 0)] UNSPEC_REP)] "reload_completed" -{ - if (ix86_bnd_prefixed_insn_p (insn)) - return "%!ret"; - - return "rep%; ret"; -} + "* return ix86_output_function_return (true);" [(set_attr "length" "2") (set_attr "atom_unit" "jeu") (set_attr "length_immediate" "0") diff --git a/gcc/config/i386/i386.opt b/gcc/config/i386/i386.opt index bc81e6bea86..fc2c81c3fb5 100644 --- a/gcc/config/i386/i386.opt +++ b/gcc/config/i386/i386.opt @@ -932,9 +932,13 @@ mindirect-branch= Target Report RejectNegative Joined Enum(indirect_branch) Var(ix86_indirect_branch) Init(indirect_branch_keep) Update indirect call and jump. +mfunction-return= +Target Report RejectNegative Joined Enum(indirect_branch) Var(ix86_function_return) Init(indirect_branch_keep) +Update function return. + Enum Name(indirect_branch) Type(enum indirect_branch) -Known indirect branch choices (for use with the -mindirect-branch= option): +Known indirect branch choices (for use with the -mindirect-branch=/-mfunction-return= options): EnumValue Enum(indirect_branch) String(keep) Value(indirect_branch_keep) diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-1.c b/gcc/testsuite/gcc.target/i386/ret-thunk-1.c new file mode 100644 index 00000000000..406956f48e5 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-1.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=thunk" } */ + +void +foo (void) +{ +} + +/* { dg-final { scan-assembler "jmp\[ \t\]*__x86.return_thunk" } } */ +/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler {\tlfence} } } */ diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-10.c b/gcc/testsuite/gcc.target/i386/ret-thunk-10.c new file mode 100644 index 00000000000..aecea4224f9 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-10.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=thunk-inline -mindirect-branch=thunk -fno-pic" } */ + +extern void (*bar) (void); + +int +foo (void) +{ + bar (); + return 0; +} + +/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler-not "jmp\[ \t\]*__x86.return_thunk" } } */ +/* { dg-final { scan-assembler-times {\tlfence} 2 } } */ +/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?bar" { target { ! x32 } } } } */ +/* { dg-final { scan-assembler "jmp\[ \t\]*__x86.indirect_thunk" { target { ! x32 } } } } */ +/* { dg-final { scan-assembler "__x86.indirect_thunk:" { target { ! x32 } } } } */ +/* { dg-final { scan-assembler "call\[ \t\]*__x86.indirect_thunk\.(r|e)ax" { target { x32 } } } } */ +/* { dg-final { scan-assembler "__x86.indirect_thunk\.(r|e)ax:" { target { x32 } } } } */ +/* { dg-final { scan-assembler-not "pushq\[ \t\]%rax" { target x32 } } } */ diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-11.c b/gcc/testsuite/gcc.target/i386/ret-thunk-11.c new file mode 100644 index 00000000000..3bacfb54dfd --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-11.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=thunk-extern -mindirect-branch=thunk -fno-pic" } */ + +extern void (*bar) (void); + +int +foo (void) +{ + bar (); + return 0; +} + +/* { dg-final { scan-assembler "jmp\[ \t\]*__x86.return_thunk" } } */ +/* { dg-final { scan-assembler-times {\tlfence} 1 } } */ +/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?bar" { target { ! x32 } } } } */ +/* { dg-final { scan-assembler "jmp\[ \t\]*__x86.indirect_thunk" { target { ! x32 } } } } */ +/* { dg-final { scan-assembler "__x86.indirect_thunk:" { target { ! x32 } } } } */ +/* { dg-final { scan-assembler "call\[ \t\]*__x86.indirect_thunk\.(r|e)ax" { target { x32 } } } } */ +/* { dg-final { scan-assembler "__x86.indirect_thunk\.(r|e)ax:" { target { x32 } } } } */ +/* { dg-final { scan-assembler-not "pushq\[ \t\]%rax" { target x32 } } } */ diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-12.c b/gcc/testsuite/gcc.target/i386/ret-thunk-12.c new file mode 100644 index 00000000000..851115ac507 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-12.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=keep -mindirect-branch=thunk -fno-pic" } */ + +extern void (*bar) (void); + +int +foo (void) +{ + bar (); + return 0; +} + +/* { dg-final { scan-assembler-not "jmp\[ \t\]*__x86.return_thunk" } } */ +/* { dg-final { scan-assembler-times {\tlfence} 1 } } */ +/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler "jmp\[ \t\]*__x86.indirect_thunk" { target { ! x32 } } } } */ +/* { dg-final { scan-assembler "__x86.indirect_thunk:" { target { ! x32 } } } } */ +/* { dg-final { scan-assembler "call\[ \t\]*__x86.indirect_thunk\.(r|e)ax" { target { x32 } } } } */ +/* { dg-final { scan-assembler "__x86.indirect_thunk\.(r|e)ax:" { target { x32 } } } } */ +/* { dg-final { scan-assembler-not "pushq\[ \t\]%rax" { target x32 } } } */ diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-13.c b/gcc/testsuite/gcc.target/i386/ret-thunk-13.c new file mode 100644 index 00000000000..7acb6fa5eae --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-13.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=keep -mindirect-branch=thunk-inline -fno-pic" } */ + +extern void (*bar) (void); +extern int foo (void) __attribute__ ((function_return("thunk"))); + +int +foo (void) +{ + bar (); + return 0; +} + +/* { dg-final { scan-assembler "jmp\[ \t\]*__x86.return_thunk" } } */ +/* { dg-final { scan-assembler-times {\tlfence} 2 } } */ +/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?bar" { target { ! x32 } } } } */ +/* { dg-final { scan-assembler-times "jmp\[ \t\]*\.LIND" 3 } } */ +/* { dg-final { scan-assembler-times "call\[ \t\]*\.LIND" 3 } } */ +/* { dg-final { scan-assembler-not "jmp\[ \t\]*__x86.indirect_thunk" } } */ +/* { dg-final { scan-assembler-not "call\[ \t\]*__x86.indirect_thunk\.(r|e)ax" { target { x32 } } } } */ +/* { dg-final { scan-assembler-not "pushq\[ \t\]%rax" { target x32 } } } */ diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-14.c b/gcc/testsuite/gcc.target/i386/ret-thunk-14.c new file mode 100644 index 00000000000..bf340fac7c6 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-14.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=keep -mindirect-branch=thunk-extern -fno-pic" } */ + +extern void (*bar) (void); + +__attribute__ ((function_return("thunk-inline"))) +int +foo (void) +{ + bar (); + return 0; +} + +/* { dg-final { scan-assembler-times {\tlfence} 1 } } */ +/* { dg-final { scan-assembler-not "jmp\[ \t\]*__x86.return_thunk" } } */ +/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?bar" { target { ! x32 } } } } */ +/* { dg-final { scan-assembler "jmp\[ \t\]*__x86.indirect_thunk" { target { ! x32 } } } } */ +/* { dg-final { scan-assembler "call\[ \t\]*__x86.indirect_thunk\.(r|e)ax" { target { x32 } } } } */ +/* { dg-final { scan-assembler-not "pushq\[ \t\]%rax" { target x32 } } } */ diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-15.c b/gcc/testsuite/gcc.target/i386/ret-thunk-15.c new file mode 100644 index 00000000000..735f8648c96 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-15.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=keep -mindirect-branch=keep -fno-pic" } */ + +extern void (*bar) (void); + +__attribute__ ((function_return("thunk-extern"), indirect_branch("thunk"))) +int +foo (void) +{ + bar (); + return 0; +} + +/* { dg-final { scan-assembler "jmp\[ \t\]*__x86.return_thunk" } } */ +/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler-times {\tlfence} 1 } } */ +/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?bar" { target { ! x32 } } } } */ +/* { dg-final { scan-assembler "jmp\[ \t\]*__x86.indirect_thunk" { target { ! x32 } } } } */ +/* { dg-final { scan-assembler "call\[ \t\]*__x86.indirect_thunk\.(r|e)ax" { target x32 } } } */ +/* { dg-final { scan-assembler-not "pushq\[ \t\]%rax" { target x32 } } } */ diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-16.c b/gcc/testsuite/gcc.target/i386/ret-thunk-16.c new file mode 100644 index 00000000000..cf3920563e0 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-16.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=thunk-inline -mindirect-branch=thunk-extern -fno-pic" } */ + +extern void (*bar) (void); + +__attribute__ ((function_return("keep"), indirect_branch("keep"))) +int +foo (void) +{ + bar (); + return 0; +} + +/* { dg-final { scan-assembler-not "__x86.indirect_thunk" } } */ +/* { dg-final { scan-assembler-not "__x86.return_thunk" } } */ +/* { dg-final { scan-assembler-not {\tlfence} } } */ +/* { dg-final { scan-assembler-not "jmp\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler-not "call\[ \t\]*\.LIND" } } */ diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-2.c b/gcc/testsuite/gcc.target/i386/ret-thunk-2.c new file mode 100644 index 00000000000..190947cc2ca --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-2.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=thunk-inline" } */ + +void +foo (void) +{ +} + +/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler {\tlfence} } } */ +/* { dg-final { scan-assembler-not "jmp\[ \t\]*__x86.return_thunk" } } */ diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-3.c b/gcc/testsuite/gcc.target/i386/ret-thunk-3.c new file mode 100644 index 00000000000..d71de3ac520 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-3.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=thunk-extern" } */ + +void +foo (void) +{ +} + +/* { dg-final { scan-assembler "jmp\[ \t\]*__x86.return_thunk" } } */ +/* { dg-final { scan-assembler-not {\tlfence} } } */ +/* { dg-final { scan-assembler-not "jmp\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler-not "call\[ \t\]*\.LIND" } } */ diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-4.c b/gcc/testsuite/gcc.target/i386/ret-thunk-4.c new file mode 100644 index 00000000000..68c22122f0d --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-4.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=keep" } */ + +void +foo (void) +{ +} + +/* { dg-final { scan-assembler-not "jmp\[ \t\]*__x86.return_thunk" } } */ +/* { dg-final { scan-assembler-not {\tlfence} } } */ +/* { dg-final { scan-assembler-not "jmp\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler-not "call\[ \t\]*\.LIND" } } */ diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-5.c b/gcc/testsuite/gcc.target/i386/ret-thunk-5.c new file mode 100644 index 00000000000..28c576e2267 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-5.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=keep" } */ + +extern void foo (void) __attribute__ ((function_return("thunk"))); + +void +foo (void) +{ +} + +/* { dg-final { scan-assembler "jmp\[ \t\]*__x86.return_thunk" } } */ +/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler {\tlfence} } } */ diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-6.c b/gcc/testsuite/gcc.target/i386/ret-thunk-6.c new file mode 100644 index 00000000000..10ad40b9c26 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-6.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=keep" } */ + +__attribute__ ((function_return("thunk-inline"))) +void +foo (void) +{ +} + +/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler {\tlfence} } } */ +/* { dg-final { scan-assembler-not "jmp\[ \t\]*__x86.return_thunk" } } */ diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-7.c b/gcc/testsuite/gcc.target/i386/ret-thunk-7.c new file mode 100644 index 00000000000..7ac0beaa73e --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-7.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=keep" } */ + +__attribute__ ((function_return("thunk-extern"))) +void +foo (void) +{ +} + +/* { dg-final { scan-assembler "jmp\[ \t\]*__x86.return_thunk" } } */ +/* { dg-final { scan-assembler-not {\tlfence} } } */ +/* { dg-final { scan-assembler-not "jmp\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler-not "call\[ \t\]*\.LIND" } } */ diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-8.c b/gcc/testsuite/gcc.target/i386/ret-thunk-8.c new file mode 100644 index 00000000000..777ab7c8088 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-8.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=thunk-inline" } */ + +extern void foo (void) __attribute__ ((function_return("keep"))); + +void +foo (void) +{ +} + +/* { dg-final { scan-assembler-not "jmp\[ \t\]*__x86.return_thunk" } } */ +/* { dg-final { scan-assembler-not {\tlfence} } } */ +/* { dg-final { scan-assembler-not "jmp\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler-not "call\[ \t\]*\.LIND" } } */ diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-9.c b/gcc/testsuite/gcc.target/i386/ret-thunk-9.c new file mode 100644 index 00000000000..569e5f47973 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/ret-thunk-9.c @@ -0,0 +1,23 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mfunction-return=thunk -mindirect-branch=thunk -fno-pic" } */ + +extern void (*bar) (void); + +int +foo (void) +{ + bar (); + return 0; +} + +/* { dg-final { scan-assembler "jmp\[ \t\]*__x86.return_thunk" } } */ +/* { dg-final { scan-assembler-not "__x86.return_thunk:" } } */ +/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */ +/* { dg-final { scan-assembler "__x86.indirect_thunk:" } } */ +/* { dg-final { scan-assembler-times {\tlfence} 1 { target { ! x32 } } } } */ +/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?bar" { target { ! x32 } } } } */ +/* { dg-final { scan-assembler "jmp\[ \t\]*__x86.indirect_thunk" { target { ! x32 } } } } */ +/* { dg-final { scan-assembler-times {\tlfence} 2 { target { x32 } } } } */ +/* { dg-final { scan-assembler "call\[ \t\]*__x86.indirect_thunk\.(r|e)ax" { target { x32 } } } } */ +/* { dg-final { scan-assembler-not "pushq\[ \t\]%rax" { target x32 } } } */ -- 2.15.1