Skip to content

Commit c58f216

Browse files
committed
Introduce the "retpoline" x86 mitigation technique for variant #2 of the speculative execution vulnerabilities disclosed today, specifically identified by CVE-2017-5715, "Branch Target Injection", and is one of the two halves to Spectre..
Summary: First, we need to explain the core of the vulnerability. Note that this is a very incomplete description, please see the Project Zero blog post for details: https://googleprojectzero.blogspot.com/2018/01/reading-privileged-memory-with-side.html The basis for branch target injection is to direct speculative execution of the processor to some "gadget" of executable code by poisoning the prediction of indirect branches with the address of that gadget. The gadget in turn contains an operation that provides a side channel for reading data. Most commonly, this will look like a load of secret data followed by a branch on the loaded value and then a load of some predictable cache line. The attacker then uses timing of the processors cache to determine which direction the branch took *in the speculative execution*, and in turn what one bit of the loaded value was. Due to the nature of these timing side channels and the branch predictor on Intel processors, this allows an attacker to leak data only accessible to a privileged domain (like the kernel) back into an unprivileged domain. The goal is simple: avoid generating code which contains an indirect branch that could have its prediction poisoned by an attacker. In many cases, the compiler can simply use directed conditional branches and a small search tree. LLVM already has support for lowering switches in this way and the first step of this patch is to disable jump-table lowering of switches and introduce a pass to rewrite explicit indirectbr sequences into a switch over integers. However, there is no fully general alternative to indirect calls. We introduce a new construct we call a "retpoline" to implement indirect calls in a non-speculatable way. It can be thought of loosely as a trampoline for indirect calls which uses the RET instruction on x86. Further, we arrange for a specific call->ret sequence which ensures the processor predicts the return to go to a controlled, known location. The retpoline then "smashes" the return address pushed onto the stack by the call with the desired target of the original indirect call. The result is a predicted return to the next instruction after a call (which can be used to trap speculative execution within an infinite loop) and an actual indirect branch to an arbitrary address. On 64-bit x86 ABIs, this is especially easily done in the compiler by using a guaranteed scratch register to pass the target into this device. For 32-bit ABIs there isn't a guaranteed scratch register and so several different retpoline variants are introduced to use a scratch register if one is available in the calling convention and to otherwise use direct stack push/pop sequences to pass the target address. This "retpoline" mitigation is fully described in the following blog post: https://support.google.com/faqs/answer/7625886 We also support a target feature that disables emission of the retpoline thunk by the compiler to allow for custom thunks if users want them. These are particularly useful in environments like kernels that routinely do hot-patching on boot and want to hot-patch their thunk to different code sequences. They can write this custom thunk and use `-mretpoline-external-thunk` *in addition* to `-mretpoline`. In this case, on x86-64 thu thunk names must be: ``` __llvm_external_retpoline_r11 ``` or on 32-bit: ``` __llvm_external_retpoline_eax __llvm_external_retpoline_ecx __llvm_external_retpoline_edx __llvm_external_retpoline_push ``` And the target of the retpoline is passed in the named register, or in the case of the `push` suffix on the top of the stack via a `pushl` instruction. There is one other important source of indirect branches in x86 ELF binaries: the PLT. These patches also include support for LLD to generate PLT entries that perform a retpoline-style indirection. The only other indirect branches remaining that we are aware of are from precompiled runtimes (such as crt0.o and similar). The ones we have found are not really attackable, and so we have not focused on them here, but eventually these runtimes should also be replicated for retpoline-ed configurations for completeness. For kernels or other freestanding or fully static executables, the compiler switch `-mretpoline` is sufficient to fully mitigate this particular attack. For dynamic executables, you must compile *all* libraries with `-mretpoline` and additionally link the dynamic executable and all shared libraries with LLD and pass `-z retpolineplt` (or use similar functionality from some other linker). We strongly recommend also using `-z now` as non-lazy binding allows the retpoline-mitigated PLT to be substantially smaller. When manually apply similar transformations to `-mretpoline` to the Linux kernel we observed very small performance hits to applications running typical workloads, and relatively minor hits (approximately 2%) even for extremely syscall-heavy applications. This is largely due to the small number of indirect branches that occur in performance sensitive paths of the kernel. When using these patches on statically linked applications, especially C++ applications, you should expect to see a much more dramatic performance hit. For microbenchmarks that are switch, indirect-, or virtual-call heavy we have seen overheads ranging from 10% to 50%. However, real-world workloads exhibit substantially lower performance impact. Notably, techniques such as PGO and ThinLTO dramatically reduce the impact of hot indirect calls (by speculatively promoting them to direct calls) and allow optimized search trees to be used to lower switches. If you need to deploy these techniques in C++ applications, we *strongly* recommend that you ensure all hot call targets are statically linked (avoiding PLT indirection) and use both PGO and ThinLTO. Well tuned servers using all of these techniques saw 5% - 10% overhead from the use of retpoline. We will add detailed documentation covering these components in subsequent patches, but wanted to make the core functionality available as soon as possible. Happy for more code review, but we'd really like to get these patches landed and backported ASAP for obvious reasons. We're planning to backport this to both 6.0 and 5.0 release streams and get a 5.0 release with just this cherry picked ASAP for distros and vendors. This patch is the work of a number of people over the past month: Eric, Reid, Rui, and myself. I'm mailing it out as a single commit due to the time sensitive nature of landing this and the need to backport it. Huge thanks to everyone who helped out here, and everyone at Intel who helped out in discussions about how to craft this. Also, credit goes to Paul Turner (at Google, but not an LLVM contributor) for much of the underlying retpoline design. Reviewers: echristo, rnk, ruiu, craig.topper, DavidKreitzer Subscribers: sanjoy, emaste, mcrosier, mgorny, mehdi_amini, hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D41723 llvm-svn: 323155
1 parent ff2b122 commit c58f216

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1915
-22
lines changed

Diff for: clang/include/clang/Driver/Options.td

+4
Original file line numberDiff line numberDiff line change
@@ -2594,6 +2594,10 @@ def mshstk : Flag<["-"], "mshstk">, Group<m_x86_Features_Group>;
25942594
def mno_shstk : Flag<["-"], "mno-shstk">, Group<m_x86_Features_Group>;
25952595
def mibt : Flag<["-"], "mibt">, Group<m_x86_Features_Group>;
25962596
def mno_ibt : Flag<["-"], "mno-ibt">, Group<m_x86_Features_Group>;
2597+
def mretpoline : Flag<["-"], "mretpoline">, Group<m_x86_Features_Group>;
2598+
def mno_retpoline : Flag<["-"], "mno-retpoline">, Group<m_x86_Features_Group>;
2599+
def mretpoline_external_thunk : Flag<["-"], "mretpoline-external-thunk">, Group<m_x86_Features_Group>;
2600+
def mno_retpoline_external_thunk : Flag<["-"], "mno-retpoline-external-thunk">, Group<m_x86_Features_Group>;
25972601

25982602
// These are legacy user-facing driver-level option spellings. They are always
25992603
// aliases for options that are spelled using the more common Unix / GNU flag

Diff for: clang/lib/Basic/Targets/X86.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,10 @@ bool X86TargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
787787
HasCLZERO = true;
788788
} else if (Feature == "+rdpid") {
789789
HasRDPID = true;
790+
} else if (Feature == "+retpoline") {
791+
HasRetpoline = true;
792+
} else if (Feature == "+retpoline-external-thunk") {
793+
HasRetpolineExternalThunk = true;
790794
}
791795

792796
X86SSEEnum Level = llvm::StringSwitch<X86SSEEnum>(Feature)
@@ -1333,6 +1337,8 @@ bool X86TargetInfo::hasFeature(StringRef Feature) const {
13331337
.Case("rdpid", HasRDPID)
13341338
.Case("rdrnd", HasRDRND)
13351339
.Case("rdseed", HasRDSEED)
1340+
.Case("retpoline", HasRetpoline)
1341+
.Case("retpoline-external-thunk", HasRetpolineExternalThunk)
13361342
.Case("rtm", HasRTM)
13371343
.Case("sgx", HasSGX)
13381344
.Case("sha", HasSHA)

Diff for: clang/lib/Basic/Targets/X86.h

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ class LLVM_LIBRARY_VISIBILITY X86TargetInfo : public TargetInfo {
9797
bool HasMOVBE = false;
9898
bool HasPREFETCHWT1 = false;
9999
bool HasRDPID = false;
100+
bool HasRetpoline = false;
101+
bool HasRetpolineExternalThunk = false;
100102

101103
/// \brief Enumeration of all of the X86 CPUs supported by Clang.
102104
///

Diff for: clang/test/Driver/x86-target-features.c

+10
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,13 @@
129129
// RUN: %clang -target i386-unknown-linux-gnu -march=i386 -mno-rdpid %s -### -o %t.o 2>&1 | FileCheck -check-prefix=NO-RDPID %s
130130
// RDPID: "-target-feature" "+rdpid"
131131
// NO-RDPID: "-target-feature" "-rdpid"
132+
133+
// RUN: %clang -target i386-linux-gnu -mretpoline %s -### -o %t.o 2>&1 | FileCheck -check-prefix=RETPOLINE %s
134+
// RUN: %clang -target i386-linux-gnu -mno-retpoline %s -### -o %t.o 2>&1 | FileCheck -check-prefix=NO-RETPOLINE %s
135+
// RETPOLINE: "-target-feature" "+retpoline"
136+
// NO-RETPOLINE: "-target-feature" "-retpoline"
137+
138+
// RUN: %clang -target i386-linux-gnu -mretpoline -mretpoline-external-thunk %s -### -o %t.o 2>&1 | FileCheck -check-prefix=RETPOLINE-EXTERNAL-THUNK %s
139+
// RUN: %clang -target i386-linux-gnu -mretpoline -mno-retpoline-external-thunk %s -### -o %t.o 2>&1 | FileCheck -check-prefix=NO-RETPOLINE-EXTERNAL-THUNK %s
140+
// RETPOLINE-EXTERNAL-THUNK: "-target-feature" "+retpoline-external-thunk"
141+
// NO-RETPOLINE-EXTERNAL-THUNK: "-target-feature" "-retpoline-external-thunk"

Diff for: lld/ELF/Arch/X86.cpp

+141-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ using namespace lld;
2121
using namespace lld::elf;
2222

2323
namespace {
24-
class X86 final : public TargetInfo {
24+
class X86 : public TargetInfo {
2525
public:
2626
X86();
2727
RelExpr getRelExpr(RelType Type, const Symbol &S,
@@ -399,7 +399,145 @@ void X86::relaxTlsLdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const {
399399
memcpy(Loc - 2, Inst, sizeof(Inst));
400400
}
401401

402+
namespace {
403+
class RetpolinePic : public X86 {
404+
public:
405+
RetpolinePic();
406+
void writeGotPlt(uint8_t *Buf, const Symbol &S) const override;
407+
void writePltHeader(uint8_t *Buf) const override;
408+
void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
409+
int32_t Index, unsigned RelOff) const override;
410+
};
411+
412+
class RetpolineNoPic : public X86 {
413+
public:
414+
RetpolineNoPic();
415+
void writeGotPlt(uint8_t *Buf, const Symbol &S) const override;
416+
void writePltHeader(uint8_t *Buf) const override;
417+
void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
418+
int32_t Index, unsigned RelOff) const override;
419+
};
420+
} // namespace
421+
422+
RetpolinePic::RetpolinePic() {
423+
PltHeaderSize = 48;
424+
PltEntrySize = 32;
425+
}
426+
427+
void RetpolinePic::writeGotPlt(uint8_t *Buf, const Symbol &S) const {
428+
write32le(Buf, S.getPltVA() + 17);
429+
}
430+
431+
void RetpolinePic::writePltHeader(uint8_t *Buf) const {
432+
const uint8_t Insn[] = {
433+
0xff, 0xb3, 0, 0, 0, 0, // 0: pushl GOTPLT+4(%ebx)
434+
0x50, // 6: pushl %eax
435+
0x8b, 0x83, 0, 0, 0, 0, // 7: mov GOTPLT+8(%ebx), %eax
436+
0xe8, 0x0e, 0x00, 0x00, 0x00, // d: call next
437+
0xf3, 0x90, // 12: loop: pause
438+
0x0f, 0xae, 0xe8, // 14: lfence
439+
0xeb, 0xf9, // 17: jmp loop
440+
0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 19: int3; .align 16
441+
0x89, 0x0c, 0x24, // 20: next: mov %ecx, (%esp)
442+
0x8b, 0x4c, 0x24, 0x04, // 23: mov 0x4(%esp), %ecx
443+
0x89, 0x44, 0x24, 0x04, // 27: mov %eax ,0x4(%esp)
444+
0x89, 0xc8, // 2b: mov %ecx, %eax
445+
0x59, // 2d: pop %ecx
446+
0xc3, // 2e: ret
447+
};
448+
memcpy(Buf, Insn, sizeof(Insn));
449+
450+
uint32_t Ebx = InX::Got->getVA() + InX::Got->getSize();
451+
uint32_t GotPlt = InX::GotPlt->getVA() - Ebx;
452+
write32le(Buf + 2, GotPlt + 4);
453+
write32le(Buf + 9, GotPlt + 8);
454+
}
455+
456+
void RetpolinePic::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
457+
uint64_t PltEntryAddr, int32_t Index,
458+
unsigned RelOff) const {
459+
const uint8_t Insn[] = {
460+
0x50, // pushl %eax
461+
0x8b, 0x83, 0, 0, 0, 0, // mov foo@GOT(%ebx), %eax
462+
0xe8, 0, 0, 0, 0, // call plt+0x20
463+
0xe9, 0, 0, 0, 0, // jmp plt+0x12
464+
0x68, 0, 0, 0, 0, // pushl $reloc_offset
465+
0xe9, 0, 0, 0, 0, // jmp plt+0
466+
};
467+
memcpy(Buf, Insn, sizeof(Insn));
468+
469+
uint32_t Ebx = InX::Got->getVA() + InX::Got->getSize();
470+
write32le(Buf + 3, GotPltEntryAddr - Ebx);
471+
write32le(Buf + 8, -Index * PltEntrySize - PltHeaderSize - 12 + 32);
472+
write32le(Buf + 13, -Index * PltEntrySize - PltHeaderSize - 17 + 18);
473+
write32le(Buf + 18, RelOff);
474+
write32le(Buf + 23, -Index * PltEntrySize - PltHeaderSize - 27);
475+
}
476+
477+
RetpolineNoPic::RetpolineNoPic() {
478+
PltHeaderSize = 64;
479+
PltEntrySize = 32;
480+
}
481+
482+
void RetpolineNoPic::writeGotPlt(uint8_t *Buf, const Symbol &S) const {
483+
write32le(Buf, S.getPltVA() + 16);
484+
}
485+
486+
void RetpolineNoPic::writePltHeader(uint8_t *Buf) const {
487+
const uint8_t PltData[] = {
488+
0xff, 0x35, 0, 0, 0, 0, // 0: pushl GOTPLT+4
489+
0x50, // 6: pushl %eax
490+
0xa1, 0, 0, 0, 0, // 7: mov GOTPLT+8, %eax
491+
0xe8, 0x0f, 0x00, 0x00, 0x00, // c: call next
492+
0xf3, 0x90, // 11: loop: pause
493+
0x0f, 0xae, 0xe8, // 13: lfence
494+
0xeb, 0xf9, // 16: jmp loop
495+
0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 18: int3
496+
0xcc, 0xcc, 0xcc, // 1f: int3; .align 16
497+
0x89, 0x0c, 0x24, // 20: next: mov %ecx, (%esp)
498+
0x8b, 0x4c, 0x24, 0x04, // 23: mov 0x4(%esp), %ecx
499+
0x89, 0x44, 0x24, 0x04, // 27: mov %eax ,0x4(%esp)
500+
0x89, 0xc8, // 2b: mov %ecx, %eax
501+
0x59, // 2d: pop %ecx
502+
0xc3, // 2e: ret
503+
};
504+
memcpy(Buf, PltData, sizeof(PltData));
505+
506+
uint32_t GotPlt = InX::GotPlt->getVA();
507+
write32le(Buf + 2, GotPlt + 4);
508+
write32le(Buf + 8, GotPlt + 8);
509+
}
510+
511+
void RetpolineNoPic::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
512+
uint64_t PltEntryAddr, int32_t Index,
513+
unsigned RelOff) const {
514+
const uint8_t Insn[] = {
515+
0x50, // 0: pushl %eax
516+
0xa1, 0, 0, 0, 0, // 1: mov foo_in_GOT, %eax
517+
0xe8, 0, 0, 0, 0, // 6: call plt+0x20
518+
0xe9, 0, 0, 0, 0, // b: jmp plt+0x11
519+
0x68, 0, 0, 0, 0, // 10: pushl $reloc_offset
520+
0xe9, 0, 0, 0, 0, // 15: jmp plt+0
521+
};
522+
memcpy(Buf, Insn, sizeof(Insn));
523+
524+
write32le(Buf + 2, GotPltEntryAddr);
525+
write32le(Buf + 7, -Index * PltEntrySize - PltHeaderSize - 11 + 32);
526+
write32le(Buf + 12, -Index * PltEntrySize - PltHeaderSize - 16 + 17);
527+
write32le(Buf + 17, RelOff);
528+
write32le(Buf + 22, -Index * PltEntrySize - PltHeaderSize - 26);
529+
}
530+
402531
TargetInfo *elf::getX86TargetInfo() {
403-
static X86 Target;
404-
return &Target;
532+
if (Config->ZRetpolineplt) {
533+
if (Config->Pic) {
534+
static RetpolinePic T;
535+
return &T;
536+
}
537+
static RetpolineNoPic T;
538+
return &T;
539+
}
540+
541+
static X86 T;
542+
return &T;
405543
}

Diff for: lld/ELF/Arch/X86_64.cpp

+120-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using namespace lld;
2323
using namespace lld::elf;
2424

2525
namespace {
26-
template <class ELFT> class X86_64 final : public TargetInfo {
26+
template <class ELFT> class X86_64 : public TargetInfo {
2727
public:
2828
X86_64();
2929
RelExpr getRelExpr(RelType Type, const Symbol &S,
@@ -460,12 +460,125 @@ void X86_64<ELFT>::relaxGot(uint8_t *Loc, uint64_t Val) const {
460460
write32le(Loc - 1, Val + 1);
461461
}
462462

463-
TargetInfo *elf::getX32TargetInfo() {
464-
static X86_64<ELF32LE> Target;
465-
return &Target;
463+
namespace {
464+
template <class ELFT> class Retpoline : public X86_64<ELFT> {
465+
public:
466+
Retpoline();
467+
void writeGotPlt(uint8_t *Buf, const Symbol &S) const override;
468+
void writePltHeader(uint8_t *Buf) const override;
469+
void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
470+
int32_t Index, unsigned RelOff) const override;
471+
};
472+
473+
template <class ELFT> class RetpolineZNow : public X86_64<ELFT> {
474+
public:
475+
RetpolineZNow();
476+
void writeGotPlt(uint8_t *Buf, const Symbol &S) const override {}
477+
void writePltHeader(uint8_t *Buf) const override;
478+
void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
479+
int32_t Index, unsigned RelOff) const override;
480+
};
481+
} // namespace
482+
483+
template <class ELFT> Retpoline<ELFT>::Retpoline() {
484+
TargetInfo::PltHeaderSize = 48;
485+
TargetInfo::PltEntrySize = 32;
486+
}
487+
488+
template <class ELFT>
489+
void Retpoline<ELFT>::writeGotPlt(uint8_t *Buf, const Symbol &S) const {
490+
write32le(Buf, S.getPltVA() + 17);
491+
}
492+
493+
template <class ELFT> void Retpoline<ELFT>::writePltHeader(uint8_t *Buf) const {
494+
const uint8_t Insn[] = {
495+
0xff, 0x35, 0, 0, 0, 0, // 0: pushq GOTPLT+8(%rip)
496+
0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // 6: mov GOTPLT+16(%rip), %r11
497+
0xe8, 0x0e, 0x00, 0x00, 0x00, // d: callq next
498+
0xf3, 0x90, // 12: loop: pause
499+
0x0f, 0xae, 0xe8, // 14: lfence
500+
0xeb, 0xf9, // 17: jmp loop
501+
0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 19: int3; .align 16
502+
0x4c, 0x89, 0x1c, 0x24, // 20: next: mov %r11, (%rsp)
503+
0xc3, // 24: ret
504+
};
505+
memcpy(Buf, Insn, sizeof(Insn));
506+
507+
uint64_t GotPlt = InX::GotPlt->getVA();
508+
uint64_t Plt = InX::Plt->getVA();
509+
write32le(Buf + 2, GotPlt - Plt - 6 + 8);
510+
write32le(Buf + 9, GotPlt - Plt - 13 + 16);
511+
}
512+
513+
template <class ELFT>
514+
void Retpoline<ELFT>::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
515+
uint64_t PltEntryAddr, int32_t Index,
516+
unsigned RelOff) const {
517+
const uint8_t Insn[] = {
518+
0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // 0: mov foo@GOTPLT(%rip), %r11
519+
0xe8, 0, 0, 0, 0, // 7: callq plt+0x20
520+
0xe9, 0, 0, 0, 0, // c: jmp plt+0x12
521+
0x68, 0, 0, 0, 0, // 11: pushq <relocation index>
522+
0xe9, 0, 0, 0, 0, // 16: jmp plt+0
523+
};
524+
memcpy(Buf, Insn, sizeof(Insn));
525+
526+
uint64_t Off = TargetInfo::PltHeaderSize + TargetInfo::PltEntrySize * Index;
527+
528+
write32le(Buf + 3, GotPltEntryAddr - PltEntryAddr - 7);
529+
write32le(Buf + 8, -Off - 12 + 32);
530+
write32le(Buf + 13, -Off - 17 + 18);
531+
write32le(Buf + 18, Index);
532+
write32le(Buf + 23, -Off - 27);
533+
}
534+
535+
template <class ELFT> RetpolineZNow<ELFT>::RetpolineZNow() {
536+
TargetInfo::PltHeaderSize = 32;
537+
TargetInfo::PltEntrySize = 16;
538+
}
539+
540+
template <class ELFT>
541+
void RetpolineZNow<ELFT>::writePltHeader(uint8_t *Buf) const {
542+
const uint8_t Insn[] = {
543+
0xe8, 0x0b, 0x00, 0x00, 0x00, // 0: call next
544+
0xf3, 0x90, // 5: loop: pause
545+
0x0f, 0xae, 0xe8, // 7: lfence
546+
0xeb, 0xf9, // a: jmp loop
547+
0xcc, 0xcc, 0xcc, 0xcc, // c: int3; .align 16
548+
0x4c, 0x89, 0x1c, 0x24, // 10: next: mov %r11, (%rsp)
549+
0xc3, // 14: ret
550+
};
551+
memcpy(Buf, Insn, sizeof(Insn));
466552
}
467553

468-
TargetInfo *elf::getX86_64TargetInfo() {
469-
static X86_64<ELF64LE> Target;
470-
return &Target;
554+
template <class ELFT>
555+
void RetpolineZNow<ELFT>::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
556+
uint64_t PltEntryAddr, int32_t Index,
557+
unsigned RelOff) const {
558+
const uint8_t Insn[] = {
559+
0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // mov foo@GOTPLT(%rip), %r11
560+
0xe9, 0, 0, 0, 0, // jmp plt+0
561+
};
562+
memcpy(Buf, Insn, sizeof(Insn));
563+
564+
write32le(Buf + 3, GotPltEntryAddr - PltEntryAddr - 7);
565+
write32le(Buf + 8,
566+
-Index * TargetInfo::PltEntrySize - TargetInfo::PltHeaderSize - 12);
471567
}
568+
569+
template <class ELFT> TargetInfo *getTargetInfo() {
570+
if (Config->ZRetpolineplt) {
571+
if (Config->ZNow) {
572+
static RetpolineZNow<ELFT> T;
573+
return &T;
574+
}
575+
static Retpoline<ELFT> T;
576+
return &T;
577+
}
578+
579+
static X86_64<ELFT> T;
580+
return &T;
581+
}
582+
583+
TargetInfo *elf::getX32TargetInfo() { return getTargetInfo<ELF32LE>(); }
584+
TargetInfo *elf::getX86_64TargetInfo() { return getTargetInfo<ELF64LE>(); }

Diff for: lld/ELF/Config.h

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ struct Configuration {
160160
bool ZRelro;
161161
bool ZRodynamic;
162162
bool ZText;
163+
bool ZRetpolineplt;
163164
bool ExitEarly;
164165
bool ZWxneeded;
165166
DiscardPolicy Discard;

Diff for: lld/ELF/Driver.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ void LinkerDriver::readConfigs(opt::InputArgList &Args) {
678678
Config->ZNow = hasZOption(Args, "now");
679679
Config->ZOrigin = hasZOption(Args, "origin");
680680
Config->ZRelro = !hasZOption(Args, "norelro");
681+
Config->ZRetpolineplt = hasZOption(Args, "retpolineplt");
681682
Config->ZRodynamic = hasZOption(Args, "rodynamic");
682683
Config->ZStackSize = args::getZOptionValue(Args, OPT_z, "stack-size", 0);
683684
Config->ZText = !hasZOption(Args, "notext");

0 commit comments

Comments
 (0)