Skip to content

Commit 4e5fddc

Browse files
committed
Merging r324449:
------------------------------------------------------------------------ r324449 | chandlerc | 2018-02-06 22:16:24 -0800 (Tue, 06 Feb 2018) | 15 lines [x86/retpoline] Make the external thunk names exactly match the names that happened to end up in GCC. This is really unfortunate, as the names don't have much rhyme or reason to them. Originally in the discussions it seemed fine to rely on aliases to map different names to whatever external thunk code developers wished to use but there are practical problems with that in the kernel it turns out. And since we're discovering this practical problems late and since GCC has already shipped a release with one set of names, we are forced, yet again, to blindly match what is there. Somewhat rushing this patch out for the Linux kernel folks to test and so we can get it patched into our releases. Differential Revision: https://reviews.llvm.org/D42998 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_50@325088 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent b4b2cc0 commit 4e5fddc

File tree

2 files changed

+68
-39
lines changed

2 files changed

+68
-39
lines changed

lib/Target/X86/X86ISelLowering.cpp

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26250,28 +26250,57 @@ static unsigned getOpcodeForRetpoline(unsigned RPOpc) {
2625026250

2625126251
static const char *getRetpolineSymbol(const X86Subtarget &Subtarget,
2625226252
unsigned Reg) {
26253+
if (Subtarget.useRetpolineExternalThunk()) {
26254+
// When using an external thunk for retpolines, we pick names that match the
26255+
// names GCC happens to use as well. This helps simplify the implementation
26256+
// of the thunks for kernels where they have no easy ability to create
26257+
// aliases and are doing non-trivial configuration of the thunk's body. For
26258+
// example, the Linux kernel will do boot-time hot patching of the thunk
26259+
// bodies and cannot easily export aliases of these to loaded modules.
26260+
//
26261+
// Note that at any point in the future, we may need to change the semantics
26262+
// of how we implement retpolines and at that time will likely change the
26263+
// name of the called thunk. Essentially, there is no hard guarantee that
26264+
// LLVM will generate calls to specific thunks, we merely make a best-effort
26265+
// attempt to help out kernels and other systems where duplicating the
26266+
// thunks is costly.
26267+
switch (Reg) {
26268+
case 0:
26269+
assert(!Subtarget.is64Bit() && "R11 should always be available on x64");
26270+
return "__x86_indirect_thunk";
26271+
case X86::EAX:
26272+
assert(!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!");
26273+
return "__x86_indirect_thunk_eax";
26274+
case X86::ECX:
26275+
assert(!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!");
26276+
return "__x86_indirect_thunk_ecx";
26277+
case X86::EDX:
26278+
assert(!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!");
26279+
return "__x86_indirect_thunk_edx";
26280+
case X86::R11:
26281+
assert(Subtarget.is64Bit() && "Should not be using a 64-bit thunk!");
26282+
return "__x86_indirect_thunk_r11";
26283+
}
26284+
llvm_unreachable("unexpected reg for retpoline");
26285+
}
26286+
26287+
// When targeting an internal COMDAT thunk use an LLVM-specific name.
2625326288
switch (Reg) {
2625426289
case 0:
2625526290
assert(!Subtarget.is64Bit() && "R11 should always be available on x64");
26256-
return Subtarget.useRetpolineExternalThunk()
26257-
? "__llvm_external_retpoline_push"
26258-
: "__llvm_retpoline_push";
26291+
return "__llvm_retpoline_push";
2625926292
case X86::EAX:
26260-
return Subtarget.useRetpolineExternalThunk()
26261-
? "__llvm_external_retpoline_eax"
26262-
: "__llvm_retpoline_eax";
26293+
assert(!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!");
26294+
return "__llvm_retpoline_eax";
2626326295
case X86::ECX:
26264-
return Subtarget.useRetpolineExternalThunk()
26265-
? "__llvm_external_retpoline_ecx"
26266-
: "__llvm_retpoline_ecx";
26296+
assert(!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!");
26297+
return "__llvm_retpoline_ecx";
2626726298
case X86::EDX:
26268-
return Subtarget.useRetpolineExternalThunk()
26269-
? "__llvm_external_retpoline_edx"
26270-
: "__llvm_retpoline_edx";
26299+
assert(!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!");
26300+
return "__llvm_retpoline_edx";
2627126301
case X86::R11:
26272-
return Subtarget.useRetpolineExternalThunk()
26273-
? "__llvm_external_retpoline_r11"
26274-
: "__llvm_retpoline_r11";
26302+
assert(Subtarget.is64Bit() && "Should not be using a 64-bit thunk!");
26303+
return "__llvm_retpoline_r11";
2627526304
}
2627626305
llvm_unreachable("unexpected reg for retpoline");
2627726306
}

test/CodeGen/X86/retpoline-external.ll

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ entry:
2323
; X64: callq bar
2424
; X64-DAG: movl %[[x]], %edi
2525
; X64-DAG: movq %[[fp]], %r11
26-
; X64: callq __llvm_external_retpoline_r11
26+
; X64: callq __x86_indirect_thunk_r11
2727
; X64: movl %[[x]], %edi
2828
; X64: callq bar
2929
; X64-DAG: movl %[[x]], %edi
3030
; X64-DAG: movq %[[fp]], %r11
31-
; X64: jmp __llvm_external_retpoline_r11 # TAILCALL
31+
; X64: jmp __x86_indirect_thunk_r11 # TAILCALL
3232

3333
; X64FAST-LABEL: icall_reg:
3434
; X64FAST: callq bar
35-
; X64FAST: callq __llvm_external_retpoline_r11
35+
; X64FAST: callq __x86_indirect_thunk_r11
3636
; X64FAST: callq bar
37-
; X64FAST: jmp __llvm_external_retpoline_r11 # TAILCALL
37+
; X64FAST: jmp __x86_indirect_thunk_r11 # TAILCALL
3838

3939
; X86-LABEL: icall_reg:
4040
; X86-DAG: movl 12(%esp), %[[fp:[^ ]*]]
@@ -43,19 +43,19 @@ entry:
4343
; X86: calll bar
4444
; X86: movl %[[fp]], %eax
4545
; X86: pushl %[[x]]
46-
; X86: calll __llvm_external_retpoline_eax
46+
; X86: calll __x86_indirect_thunk_eax
4747
; X86: pushl %[[x]]
4848
; X86: calll bar
4949
; X86: movl %[[fp]], %eax
5050
; X86: pushl %[[x]]
51-
; X86: calll __llvm_external_retpoline_eax
51+
; X86: calll __x86_indirect_thunk_eax
5252
; X86-NOT: # TAILCALL
5353

5454
; X86FAST-LABEL: icall_reg:
5555
; X86FAST: calll bar
56-
; X86FAST: calll __llvm_external_retpoline_eax
56+
; X86FAST: calll __x86_indirect_thunk_eax
5757
; X86FAST: calll bar
58-
; X86FAST: calll __llvm_external_retpoline_eax
58+
; X86FAST: calll __x86_indirect_thunk_eax
5959

6060

6161
@global_fp = external global void (i32)*
@@ -72,28 +72,28 @@ define void @icall_global_fp(i32 %x, void (i32)** %fpp) #0 {
7272
; X64-LABEL: icall_global_fp:
7373
; X64-DAG: movl %edi, %[[x:[^ ]*]]
7474
; X64-DAG: movq global_fp(%rip), %r11
75-
; X64: callq __llvm_external_retpoline_r11
75+
; X64: callq __x86_indirect_thunk_r11
7676
; X64-DAG: movl %[[x]], %edi
7777
; X64-DAG: movq global_fp(%rip), %r11
78-
; X64: jmp __llvm_external_retpoline_r11 # TAILCALL
78+
; X64: jmp __x86_indirect_thunk_r11 # TAILCALL
7979

8080
; X64FAST-LABEL: icall_global_fp:
8181
; X64FAST: movq global_fp(%rip), %r11
82-
; X64FAST: callq __llvm_external_retpoline_r11
82+
; X64FAST: callq __x86_indirect_thunk_r11
8383
; X64FAST: movq global_fp(%rip), %r11
84-
; X64FAST: jmp __llvm_external_retpoline_r11 # TAILCALL
84+
; X64FAST: jmp __x86_indirect_thunk_r11 # TAILCALL
8585

8686
; X86-LABEL: icall_global_fp:
8787
; X86: movl global_fp, %eax
8888
; X86: pushl 4(%esp)
89-
; X86: calll __llvm_external_retpoline_eax
89+
; X86: calll __x86_indirect_thunk_eax
9090
; X86: addl $4, %esp
9191
; X86: movl global_fp, %eax
92-
; X86: jmp __llvm_external_retpoline_eax # TAILCALL
92+
; X86: jmp __x86_indirect_thunk_eax # TAILCALL
9393

9494
; X86FAST-LABEL: icall_global_fp:
95-
; X86FAST: calll __llvm_external_retpoline_eax
96-
; X86FAST: jmp __llvm_external_retpoline_eax # TAILCALL
95+
; X86FAST: calll __x86_indirect_thunk_eax
96+
; X86FAST: jmp __x86_indirect_thunk_eax # TAILCALL
9797

9898

9999
%struct.Foo = type { void (%struct.Foo*)** }
@@ -114,29 +114,29 @@ define void @vcall(%struct.Foo* %obj) #0 {
114114
; X64: movq (%[[obj]]), %[[vptr:[^ ]*]]
115115
; X64: movq 8(%[[vptr]]), %[[fp:[^ ]*]]
116116
; X64: movq %[[fp]], %r11
117-
; X64: callq __llvm_external_retpoline_r11
117+
; X64: callq __x86_indirect_thunk_r11
118118
; X64-DAG: movq %[[obj]], %rdi
119119
; X64-DAG: movq %[[fp]], %r11
120-
; X64: jmp __llvm_external_retpoline_r11 # TAILCALL
120+
; X64: jmp __x86_indirect_thunk_r11 # TAILCALL
121121

122122
; X64FAST-LABEL: vcall:
123-
; X64FAST: callq __llvm_external_retpoline_r11
124-
; X64FAST: jmp __llvm_external_retpoline_r11 # TAILCALL
123+
; X64FAST: callq __x86_indirect_thunk_r11
124+
; X64FAST: jmp __x86_indirect_thunk_r11 # TAILCALL
125125

126126
; X86-LABEL: vcall:
127127
; X86: movl 8(%esp), %[[obj:[^ ]*]]
128128
; X86: movl (%[[obj]]), %[[vptr:[^ ]*]]
129129
; X86: movl 4(%[[vptr]]), %[[fp:[^ ]*]]
130130
; X86: movl %[[fp]], %eax
131131
; X86: pushl %[[obj]]
132-
; X86: calll __llvm_external_retpoline_eax
132+
; X86: calll __x86_indirect_thunk_eax
133133
; X86: addl $4, %esp
134134
; X86: movl %[[fp]], %eax
135-
; X86: jmp __llvm_external_retpoline_eax # TAILCALL
135+
; X86: jmp __x86_indirect_thunk_eax # TAILCALL
136136

137137
; X86FAST-LABEL: vcall:
138-
; X86FAST: calll __llvm_external_retpoline_eax
139-
; X86FAST: jmp __llvm_external_retpoline_eax # TAILCALL
138+
; X86FAST: calll __x86_indirect_thunk_eax
139+
; X86FAST: jmp __x86_indirect_thunk_eax # TAILCALL
140140

141141

142142
declare void @direct_callee()

0 commit comments

Comments
 (0)