10b57cec5SDimitry Andric //===- X86.cpp ------------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
9bdd1243dSDimitry Andric #include "OutputSections.h"
100b57cec5SDimitry Andric #include "Symbols.h"
110b57cec5SDimitry Andric #include "SyntheticSections.h"
120b57cec5SDimitry Andric #include "Target.h"
130b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h"
140b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric using namespace llvm;
170b57cec5SDimitry Andric using namespace llvm::support::endian;
180b57cec5SDimitry Andric using namespace llvm::ELF;
195ffd83dbSDimitry Andric using namespace lld;
205ffd83dbSDimitry Andric using namespace lld::elf;
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric namespace {
230b57cec5SDimitry Andric class X86 : public TargetInfo {
240b57cec5SDimitry Andric public:
250b57cec5SDimitry Andric   X86();
260b57cec5SDimitry Andric   int getTlsGdRelaxSkip(RelType type) const override;
270b57cec5SDimitry Andric   RelExpr getRelExpr(RelType type, const Symbol &s,
280b57cec5SDimitry Andric                      const uint8_t *loc) const override;
290b57cec5SDimitry Andric   int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
300b57cec5SDimitry Andric   void writeGotPltHeader(uint8_t *buf) const override;
310b57cec5SDimitry Andric   RelType getDynRel(RelType type) const override;
320b57cec5SDimitry Andric   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
330b57cec5SDimitry Andric   void writeIgotPlt(uint8_t *buf, const Symbol &s) const override;
340b57cec5SDimitry Andric   void writePltHeader(uint8_t *buf) const override;
35480093f4SDimitry Andric   void writePlt(uint8_t *buf, const Symbol &sym,
36480093f4SDimitry Andric                 uint64_t pltEntryAddr) const override;
375ffd83dbSDimitry Andric   void relocate(uint8_t *loc, const Relocation &rel,
385ffd83dbSDimitry Andric                 uint64_t val) const override;
390b57cec5SDimitry Andric 
40e8d8bef9SDimitry Andric   RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override;
41bdd1243dSDimitry Andric   void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
420b57cec5SDimitry Andric };
430b57cec5SDimitry Andric } // namespace
440b57cec5SDimitry Andric 
X86()450b57cec5SDimitry Andric X86::X86() {
460b57cec5SDimitry Andric   copyRel = R_386_COPY;
470b57cec5SDimitry Andric   gotRel = R_386_GLOB_DAT;
480b57cec5SDimitry Andric   pltRel = R_386_JUMP_SLOT;
490b57cec5SDimitry Andric   iRelativeRel = R_386_IRELATIVE;
500b57cec5SDimitry Andric   relativeRel = R_386_RELATIVE;
510b57cec5SDimitry Andric   symbolicRel = R_386_32;
52349cc55cSDimitry Andric   tlsDescRel = R_386_TLS_DESC;
530b57cec5SDimitry Andric   tlsGotRel = R_386_TLS_TPOFF;
540b57cec5SDimitry Andric   tlsModuleIndexRel = R_386_TLS_DTPMOD32;
550b57cec5SDimitry Andric   tlsOffsetRel = R_386_TLS_DTPOFF32;
56349cc55cSDimitry Andric   gotBaseSymInGotPlt = true;
570b57cec5SDimitry Andric   pltHeaderSize = 16;
58480093f4SDimitry Andric   pltEntrySize = 16;
59480093f4SDimitry Andric   ipltEntrySize = 16;
600b57cec5SDimitry Andric   trapInstr = {0xcc, 0xcc, 0xcc, 0xcc}; // 0xcc = INT3
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric   // Align to the non-PAE large page size (known as a superpage or huge page).
630b57cec5SDimitry Andric   // FreeBSD automatically promotes large, superpage-aligned allocations.
640b57cec5SDimitry Andric   defaultImageBase = 0x400000;
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric 
getTlsGdRelaxSkip(RelType type) const670b57cec5SDimitry Andric int X86::getTlsGdRelaxSkip(RelType type) const {
68349cc55cSDimitry Andric   // TLSDESC relocations are processed separately. See relaxTlsGdToLe below.
69349cc55cSDimitry Andric   return type == R_386_TLS_GOTDESC || type == R_386_TLS_DESC_CALL ? 1 : 2;
700b57cec5SDimitry Andric }
710b57cec5SDimitry Andric 
getRelExpr(RelType type,const Symbol & s,const uint8_t * loc) const720b57cec5SDimitry Andric RelExpr X86::getRelExpr(RelType type, const Symbol &s,
730b57cec5SDimitry Andric                         const uint8_t *loc) const {
740b57cec5SDimitry Andric   switch (type) {
750b57cec5SDimitry Andric   case R_386_8:
760b57cec5SDimitry Andric   case R_386_16:
770b57cec5SDimitry Andric   case R_386_32:
780b57cec5SDimitry Andric     return R_ABS;
790b57cec5SDimitry Andric   case R_386_TLS_LDO_32:
800b57cec5SDimitry Andric     return R_DTPREL;
810b57cec5SDimitry Andric   case R_386_TLS_GD:
820b57cec5SDimitry Andric     return R_TLSGD_GOTPLT;
830b57cec5SDimitry Andric   case R_386_TLS_LDM:
840b57cec5SDimitry Andric     return R_TLSLD_GOTPLT;
850b57cec5SDimitry Andric   case R_386_PLT32:
860b57cec5SDimitry Andric     return R_PLT_PC;
870b57cec5SDimitry Andric   case R_386_PC8:
880b57cec5SDimitry Andric   case R_386_PC16:
890b57cec5SDimitry Andric   case R_386_PC32:
900b57cec5SDimitry Andric     return R_PC;
910b57cec5SDimitry Andric   case R_386_GOTPC:
920b57cec5SDimitry Andric     return R_GOTPLTONLY_PC;
930b57cec5SDimitry Andric   case R_386_TLS_IE:
940b57cec5SDimitry Andric     return R_GOT;
950b57cec5SDimitry Andric   case R_386_GOT32:
960b57cec5SDimitry Andric   case R_386_GOT32X:
970b57cec5SDimitry Andric     // These relocations are arguably mis-designed because their calculations
980b57cec5SDimitry Andric     // depend on the instructions they are applied to. This is bad because we
990b57cec5SDimitry Andric     // usually don't care about whether the target section contains valid
1000b57cec5SDimitry Andric     // machine instructions or not. But this is part of the documented ABI, so
1010b57cec5SDimitry Andric     // we had to implement as the standard requires.
1020b57cec5SDimitry Andric     //
1030b57cec5SDimitry Andric     // x86 does not support PC-relative data access. Therefore, in order to
1040b57cec5SDimitry Andric     // access GOT contents, a GOT address needs to be known at link-time
1050b57cec5SDimitry Andric     // (which means non-PIC) or compilers have to emit code to get a GOT
1060b57cec5SDimitry Andric     // address at runtime (which means code is position-independent but
1070b57cec5SDimitry Andric     // compilers need to emit extra code for each GOT access.) This decision
1080b57cec5SDimitry Andric     // is made at compile-time. In the latter case, compilers emit code to
109480093f4SDimitry Andric     // load a GOT address to a register, which is usually %ebx.
1100b57cec5SDimitry Andric     //
1110b57cec5SDimitry Andric     // So, there are two ways to refer to symbol foo's GOT entry: foo@GOT or
1120b57cec5SDimitry Andric     // foo@GOT(%ebx).
1130b57cec5SDimitry Andric     //
1140b57cec5SDimitry Andric     // foo@GOT is not usable in PIC. If we are creating a PIC output and if we
1150b57cec5SDimitry Andric     // find such relocation, we should report an error. foo@GOT is resolved to
1160b57cec5SDimitry Andric     // an *absolute* address of foo's GOT entry, because both GOT address and
1170b57cec5SDimitry Andric     // foo's offset are known. In other words, it's G + A.
1180b57cec5SDimitry Andric     //
1190b57cec5SDimitry Andric     // foo@GOT(%ebx) needs to be resolved to a *relative* offset from a GOT to
1200b57cec5SDimitry Andric     // foo's GOT entry in the table, because GOT address is not known but foo's
1210b57cec5SDimitry Andric     // offset in the table is known. It's G + A - GOT.
1220b57cec5SDimitry Andric     //
1230b57cec5SDimitry Andric     // It's unfortunate that compilers emit the same relocation for these
1240b57cec5SDimitry Andric     // different use cases. In order to distinguish them, we have to read a
1250b57cec5SDimitry Andric     // machine instruction.
1260b57cec5SDimitry Andric     //
1270b57cec5SDimitry Andric     // The following code implements it. We assume that Loc[0] is the first byte
1280b57cec5SDimitry Andric     // of a displacement or an immediate field of a valid machine
1290b57cec5SDimitry Andric     // instruction. That means a ModRM byte is at Loc[-1]. By taking a look at
1300b57cec5SDimitry Andric     // the byte, we can determine whether the instruction uses the operand as an
1310b57cec5SDimitry Andric     // absolute address (R_GOT) or a register-relative address (R_GOTPLT).
1320b57cec5SDimitry Andric     return (loc[-1] & 0xc7) == 0x5 ? R_GOT : R_GOTPLT;
133349cc55cSDimitry Andric   case R_386_TLS_GOTDESC:
134349cc55cSDimitry Andric     return R_TLSDESC_GOTPLT;
135349cc55cSDimitry Andric   case R_386_TLS_DESC_CALL:
136349cc55cSDimitry Andric     return R_TLSDESC_CALL;
1370b57cec5SDimitry Andric   case R_386_TLS_GOTIE:
1380b57cec5SDimitry Andric     return R_GOTPLT;
1390b57cec5SDimitry Andric   case R_386_GOTOFF:
1400b57cec5SDimitry Andric     return R_GOTPLTREL;
1410b57cec5SDimitry Andric   case R_386_TLS_LE:
142e8d8bef9SDimitry Andric     return R_TPREL;
1430b57cec5SDimitry Andric   case R_386_TLS_LE_32:
144e8d8bef9SDimitry Andric     return R_TPREL_NEG;
1450b57cec5SDimitry Andric   case R_386_NONE:
1460b57cec5SDimitry Andric     return R_NONE;
1470b57cec5SDimitry Andric   default:
1480b57cec5SDimitry Andric     error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
1490b57cec5SDimitry Andric           ") against symbol " + toString(s));
1500b57cec5SDimitry Andric     return R_NONE;
1510b57cec5SDimitry Andric   }
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric 
adjustTlsExpr(RelType type,RelExpr expr) const154e8d8bef9SDimitry Andric RelExpr X86::adjustTlsExpr(RelType type, RelExpr expr) const {
1550b57cec5SDimitry Andric   switch (expr) {
1560b57cec5SDimitry Andric   default:
1570b57cec5SDimitry Andric     return expr;
1580b57cec5SDimitry Andric   case R_RELAX_TLS_GD_TO_IE:
1590b57cec5SDimitry Andric     return R_RELAX_TLS_GD_TO_IE_GOTPLT;
1600b57cec5SDimitry Andric   case R_RELAX_TLS_GD_TO_LE:
161349cc55cSDimitry Andric     return type == R_386_TLS_GD ? R_RELAX_TLS_GD_TO_LE_NEG
162349cc55cSDimitry Andric                                 : R_RELAX_TLS_GD_TO_LE;
1630b57cec5SDimitry Andric   }
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric 
writeGotPltHeader(uint8_t * buf) const1660b57cec5SDimitry Andric void X86::writeGotPltHeader(uint8_t *buf) const {
1670b57cec5SDimitry Andric   write32le(buf, mainPart->dynamic->getVA());
1680b57cec5SDimitry Andric }
1690b57cec5SDimitry Andric 
writeGotPlt(uint8_t * buf,const Symbol & s) const1700b57cec5SDimitry Andric void X86::writeGotPlt(uint8_t *buf, const Symbol &s) const {
1710b57cec5SDimitry Andric   // Entries in .got.plt initially points back to the corresponding
1720b57cec5SDimitry Andric   // PLT entries with a fixed offset to skip the first instruction.
1730b57cec5SDimitry Andric   write32le(buf, s.getPltVA() + 6);
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric 
writeIgotPlt(uint8_t * buf,const Symbol & s) const1760b57cec5SDimitry Andric void X86::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
1770b57cec5SDimitry Andric   // An x86 entry is the address of the ifunc resolver function.
1780b57cec5SDimitry Andric   write32le(buf, s.getVA());
1790b57cec5SDimitry Andric }
1800b57cec5SDimitry Andric 
getDynRel(RelType type) const1810b57cec5SDimitry Andric RelType X86::getDynRel(RelType type) const {
1820b57cec5SDimitry Andric   if (type == R_386_TLS_LE)
1830b57cec5SDimitry Andric     return R_386_TLS_TPOFF;
1840b57cec5SDimitry Andric   if (type == R_386_TLS_LE_32)
1850b57cec5SDimitry Andric     return R_386_TLS_TPOFF32;
1860b57cec5SDimitry Andric   return type;
1870b57cec5SDimitry Andric }
1880b57cec5SDimitry Andric 
writePltHeader(uint8_t * buf) const1890b57cec5SDimitry Andric void X86::writePltHeader(uint8_t *buf) const {
1900b57cec5SDimitry Andric   if (config->isPic) {
1910b57cec5SDimitry Andric     const uint8_t v[] = {
1920b57cec5SDimitry Andric         0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
1930b57cec5SDimitry Andric         0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
1940b57cec5SDimitry Andric         0x90, 0x90, 0x90, 0x90              // nop
1950b57cec5SDimitry Andric     };
1960b57cec5SDimitry Andric     memcpy(buf, v, sizeof(v));
1970b57cec5SDimitry Andric     return;
1980b57cec5SDimitry Andric   }
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric   const uint8_t pltData[] = {
2010b57cec5SDimitry Andric       0xff, 0x35, 0, 0, 0, 0, // pushl (GOTPLT+4)
2020b57cec5SDimitry Andric       0xff, 0x25, 0, 0, 0, 0, // jmp *(GOTPLT+8)
2030b57cec5SDimitry Andric       0x90, 0x90, 0x90, 0x90, // nop
2040b57cec5SDimitry Andric   };
2050b57cec5SDimitry Andric   memcpy(buf, pltData, sizeof(pltData));
2060b57cec5SDimitry Andric   uint32_t gotPlt = in.gotPlt->getVA();
2070b57cec5SDimitry Andric   write32le(buf + 2, gotPlt + 4);
2080b57cec5SDimitry Andric   write32le(buf + 8, gotPlt + 8);
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric 
writePlt(uint8_t * buf,const Symbol & sym,uint64_t pltEntryAddr) const211480093f4SDimitry Andric void X86::writePlt(uint8_t *buf, const Symbol &sym,
212480093f4SDimitry Andric                    uint64_t pltEntryAddr) const {
21304eeddc0SDimitry Andric   unsigned relOff = in.relaPlt->entsize * sym.getPltIdx();
2140b57cec5SDimitry Andric   if (config->isPic) {
2150b57cec5SDimitry Andric     const uint8_t inst[] = {
2160b57cec5SDimitry Andric         0xff, 0xa3, 0, 0, 0, 0, // jmp *foo@GOT(%ebx)
2170b57cec5SDimitry Andric         0x68, 0,    0, 0, 0,    // pushl $reloc_offset
2180b57cec5SDimitry Andric         0xe9, 0,    0, 0, 0,    // jmp .PLT0@PC
2190b57cec5SDimitry Andric     };
2200b57cec5SDimitry Andric     memcpy(buf, inst, sizeof(inst));
221480093f4SDimitry Andric     write32le(buf + 2, sym.getGotPltVA() - in.gotPlt->getVA());
2220b57cec5SDimitry Andric   } else {
2230b57cec5SDimitry Andric     const uint8_t inst[] = {
2240b57cec5SDimitry Andric         0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOT
2250b57cec5SDimitry Andric         0x68, 0,    0, 0, 0,    // pushl $reloc_offset
2260b57cec5SDimitry Andric         0xe9, 0,    0, 0, 0,    // jmp .PLT0@PC
2270b57cec5SDimitry Andric     };
2280b57cec5SDimitry Andric     memcpy(buf, inst, sizeof(inst));
229480093f4SDimitry Andric     write32le(buf + 2, sym.getGotPltVA());
2300b57cec5SDimitry Andric   }
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric   write32le(buf + 7, relOff);
233480093f4SDimitry Andric   write32le(buf + 12, in.plt->getVA() - pltEntryAddr - 16);
2340b57cec5SDimitry Andric }
2350b57cec5SDimitry Andric 
getImplicitAddend(const uint8_t * buf,RelType type) const2360b57cec5SDimitry Andric int64_t X86::getImplicitAddend(const uint8_t *buf, RelType type) const {
2370b57cec5SDimitry Andric   switch (type) {
2380b57cec5SDimitry Andric   case R_386_8:
2390b57cec5SDimitry Andric   case R_386_PC8:
2400b57cec5SDimitry Andric     return SignExtend64<8>(*buf);
2410b57cec5SDimitry Andric   case R_386_16:
2420b57cec5SDimitry Andric   case R_386_PC16:
2430b57cec5SDimitry Andric     return SignExtend64<16>(read16le(buf));
2440b57cec5SDimitry Andric   case R_386_32:
245fe6060f1SDimitry Andric   case R_386_GLOB_DAT:
2460b57cec5SDimitry Andric   case R_386_GOT32:
2470b57cec5SDimitry Andric   case R_386_GOT32X:
2480b57cec5SDimitry Andric   case R_386_GOTOFF:
2490b57cec5SDimitry Andric   case R_386_GOTPC:
250fe6060f1SDimitry Andric   case R_386_IRELATIVE:
2510b57cec5SDimitry Andric   case R_386_PC32:
2520b57cec5SDimitry Andric   case R_386_PLT32:
253fe6060f1SDimitry Andric   case R_386_RELATIVE:
254349cc55cSDimitry Andric   case R_386_TLS_GOTDESC:
255349cc55cSDimitry Andric   case R_386_TLS_DESC_CALL:
256fe6060f1SDimitry Andric   case R_386_TLS_DTPMOD32:
257fe6060f1SDimitry Andric   case R_386_TLS_DTPOFF32:
2580b57cec5SDimitry Andric   case R_386_TLS_LDO_32:
259fe6060f1SDimitry Andric   case R_386_TLS_LDM:
260fe6060f1SDimitry Andric   case R_386_TLS_IE:
261fe6060f1SDimitry Andric   case R_386_TLS_IE_32:
2620b57cec5SDimitry Andric   case R_386_TLS_LE:
263fe6060f1SDimitry Andric   case R_386_TLS_LE_32:
264fe6060f1SDimitry Andric   case R_386_TLS_GD:
265fe6060f1SDimitry Andric   case R_386_TLS_GD_32:
266fe6060f1SDimitry Andric   case R_386_TLS_GOTIE:
267fe6060f1SDimitry Andric   case R_386_TLS_TPOFF:
268fe6060f1SDimitry Andric   case R_386_TLS_TPOFF32:
2690b57cec5SDimitry Andric     return SignExtend64<32>(read32le(buf));
270349cc55cSDimitry Andric   case R_386_TLS_DESC:
271349cc55cSDimitry Andric     return SignExtend64<32>(read32le(buf + 4));
272fe6060f1SDimitry Andric   case R_386_NONE:
273fe6060f1SDimitry Andric   case R_386_JUMP_SLOT:
274fe6060f1SDimitry Andric     // These relocations are defined as not having an implicit addend.
275fe6060f1SDimitry Andric     return 0;
2760b57cec5SDimitry Andric   default:
277fe6060f1SDimitry Andric     internalLinkerError(getErrorLocation(buf),
278fe6060f1SDimitry Andric                         "cannot read addend for relocation " + toString(type));
2790b57cec5SDimitry Andric     return 0;
2800b57cec5SDimitry Andric   }
2810b57cec5SDimitry Andric }
2820b57cec5SDimitry Andric 
relocate(uint8_t * loc,const Relocation & rel,uint64_t val) const2835ffd83dbSDimitry Andric void X86::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
2845ffd83dbSDimitry Andric   switch (rel.type) {
2850b57cec5SDimitry Andric   case R_386_8:
2860b57cec5SDimitry Andric     // R_386_{PC,}{8,16} are not part of the i386 psABI, but they are
2870b57cec5SDimitry Andric     // being used for some 16-bit programs such as boot loaders, so
2880b57cec5SDimitry Andric     // we want to support them.
2895ffd83dbSDimitry Andric     checkIntUInt(loc, val, 8, rel);
2900b57cec5SDimitry Andric     *loc = val;
2910b57cec5SDimitry Andric     break;
2920b57cec5SDimitry Andric   case R_386_PC8:
2935ffd83dbSDimitry Andric     checkInt(loc, val, 8, rel);
2940b57cec5SDimitry Andric     *loc = val;
2950b57cec5SDimitry Andric     break;
2960b57cec5SDimitry Andric   case R_386_16:
2975ffd83dbSDimitry Andric     checkIntUInt(loc, val, 16, rel);
2980b57cec5SDimitry Andric     write16le(loc, val);
2990b57cec5SDimitry Andric     break;
3000b57cec5SDimitry Andric   case R_386_PC16:
3010b57cec5SDimitry Andric     // R_386_PC16 is normally used with 16 bit code. In that situation
3020b57cec5SDimitry Andric     // the PC is 16 bits, just like the addend. This means that it can
3030b57cec5SDimitry Andric     // point from any 16 bit address to any other if the possibility
3040b57cec5SDimitry Andric     // of wrapping is included.
3050b57cec5SDimitry Andric     // The only restriction we have to check then is that the destination
3060b57cec5SDimitry Andric     // address fits in 16 bits. That is impossible to do here. The problem is
3070b57cec5SDimitry Andric     // that we are passed the final value, which already had the
3080b57cec5SDimitry Andric     // current location subtracted from it.
3090b57cec5SDimitry Andric     // We just check that Val fits in 17 bits. This misses some cases, but
3100b57cec5SDimitry Andric     // should have no false positives.
3115ffd83dbSDimitry Andric     checkInt(loc, val, 17, rel);
3120b57cec5SDimitry Andric     write16le(loc, val);
3130b57cec5SDimitry Andric     break;
3140b57cec5SDimitry Andric   case R_386_32:
3150b57cec5SDimitry Andric   case R_386_GOT32:
3160b57cec5SDimitry Andric   case R_386_GOT32X:
3170b57cec5SDimitry Andric   case R_386_GOTOFF:
3180b57cec5SDimitry Andric   case R_386_GOTPC:
3190b57cec5SDimitry Andric   case R_386_PC32:
3200b57cec5SDimitry Andric   case R_386_PLT32:
3210b57cec5SDimitry Andric   case R_386_RELATIVE:
322349cc55cSDimitry Andric   case R_386_TLS_GOTDESC:
323349cc55cSDimitry Andric   case R_386_TLS_DESC_CALL:
3240b57cec5SDimitry Andric   case R_386_TLS_DTPMOD32:
3250b57cec5SDimitry Andric   case R_386_TLS_DTPOFF32:
3260b57cec5SDimitry Andric   case R_386_TLS_GD:
3270b57cec5SDimitry Andric   case R_386_TLS_GOTIE:
3280b57cec5SDimitry Andric   case R_386_TLS_IE:
3290b57cec5SDimitry Andric   case R_386_TLS_LDM:
3300b57cec5SDimitry Andric   case R_386_TLS_LDO_32:
3310b57cec5SDimitry Andric   case R_386_TLS_LE:
3320b57cec5SDimitry Andric   case R_386_TLS_LE_32:
3330b57cec5SDimitry Andric   case R_386_TLS_TPOFF:
3340b57cec5SDimitry Andric   case R_386_TLS_TPOFF32:
3355ffd83dbSDimitry Andric     checkInt(loc, val, 32, rel);
3360b57cec5SDimitry Andric     write32le(loc, val);
3370b57cec5SDimitry Andric     break;
338349cc55cSDimitry Andric   case R_386_TLS_DESC:
339349cc55cSDimitry Andric     // The addend is stored in the second 32-bit word.
340349cc55cSDimitry Andric     write32le(loc + 4, val);
341349cc55cSDimitry Andric     break;
3420b57cec5SDimitry Andric   default:
3430b57cec5SDimitry Andric     llvm_unreachable("unknown relocation");
3440b57cec5SDimitry Andric   }
3450b57cec5SDimitry Andric }
3460b57cec5SDimitry Andric 
relaxTlsGdToLe(uint8_t * loc,const Relocation & rel,uint64_t val)347bdd1243dSDimitry Andric static void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) {
348349cc55cSDimitry Andric   if (rel.type == R_386_TLS_GD) {
349bdd1243dSDimitry Andric     // Convert (loc[-2] == 0x04)
350349cc55cSDimitry Andric     //   leal x@tlsgd(, %ebx, 1), %eax
351bdd1243dSDimitry Andric     //   call ___tls_get_addr@plt
352bdd1243dSDimitry Andric     // or
353bdd1243dSDimitry Andric     //   leal x@tlsgd(%reg), %eax
354bdd1243dSDimitry Andric     //   call *___tls_get_addr@got(%reg)
3550b57cec5SDimitry Andric     // to
3560b57cec5SDimitry Andric     const uint8_t inst[] = {
3570b57cec5SDimitry Andric         0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
358bdd1243dSDimitry Andric         0x81, 0xe8, 0,    0,    0,    0,    // subl x@ntpoff(%ebx), %eax
3590b57cec5SDimitry Andric     };
360bdd1243dSDimitry Andric     uint8_t *w = loc[-2] == 0x04 ? loc - 3 : loc - 2;
361bdd1243dSDimitry Andric     memcpy(w, inst, sizeof(inst));
362bdd1243dSDimitry Andric     write32le(w + 8, val);
363349cc55cSDimitry Andric   } else if (rel.type == R_386_TLS_GOTDESC) {
364349cc55cSDimitry Andric     // Convert leal x@tlsdesc(%ebx), %eax to leal x@ntpoff, %eax.
365349cc55cSDimitry Andric     //
366349cc55cSDimitry Andric     // Note: call *x@tlsdesc(%eax) may not immediately follow this instruction.
367349cc55cSDimitry Andric     if (memcmp(loc - 2, "\x8d\x83", 2)) {
368349cc55cSDimitry Andric       error(getErrorLocation(loc - 2) +
369349cc55cSDimitry Andric             "R_386_TLS_GOTDESC must be used in leal x@tlsdesc(%ebx), %eax");
370349cc55cSDimitry Andric       return;
371349cc55cSDimitry Andric     }
372349cc55cSDimitry Andric     loc[-1] = 0x05;
373349cc55cSDimitry Andric     write32le(loc, val);
374349cc55cSDimitry Andric   } else {
375349cc55cSDimitry Andric     // Convert call *x@tlsdesc(%eax) to xchg ax, ax.
376349cc55cSDimitry Andric     assert(rel.type == R_386_TLS_DESC_CALL);
377349cc55cSDimitry Andric     loc[0] = 0x66;
378349cc55cSDimitry Andric     loc[1] = 0x90;
379349cc55cSDimitry Andric   }
3800b57cec5SDimitry Andric }
3810b57cec5SDimitry Andric 
relaxTlsGdToIe(uint8_t * loc,const Relocation & rel,uint64_t val)382bdd1243dSDimitry Andric static void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) {
383349cc55cSDimitry Andric   if (rel.type == R_386_TLS_GD) {
384bdd1243dSDimitry Andric     // Convert (loc[-2] == 0x04)
385349cc55cSDimitry Andric     //   leal x@tlsgd(, %ebx, 1), %eax
386bdd1243dSDimitry Andric     //   call ___tls_get_addr@plt
387bdd1243dSDimitry Andric     // or
388bdd1243dSDimitry Andric     //   leal x@tlsgd(%reg), %eax
389bdd1243dSDimitry Andric     //   call *___tls_get_addr@got(%reg)
3900b57cec5SDimitry Andric     const uint8_t inst[] = {
3910b57cec5SDimitry Andric         0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
392bdd1243dSDimitry Andric         0x03, 0x83, 0,    0,    0,    0,    // addl x@gottpoff(%ebx), %eax
3930b57cec5SDimitry Andric     };
394bdd1243dSDimitry Andric     uint8_t *w = loc[-2] == 0x04 ? loc - 3 : loc - 2;
395bdd1243dSDimitry Andric     memcpy(w, inst, sizeof(inst));
396bdd1243dSDimitry Andric     write32le(w + 8, val);
397349cc55cSDimitry Andric   } else if (rel.type == R_386_TLS_GOTDESC) {
398349cc55cSDimitry Andric     // Convert leal x@tlsdesc(%ebx), %eax to movl x@gotntpoff(%ebx), %eax.
399349cc55cSDimitry Andric     if (memcmp(loc - 2, "\x8d\x83", 2)) {
400349cc55cSDimitry Andric       error(getErrorLocation(loc - 2) +
401349cc55cSDimitry Andric             "R_386_TLS_GOTDESC must be used in leal x@tlsdesc(%ebx), %eax");
402349cc55cSDimitry Andric       return;
403349cc55cSDimitry Andric     }
404349cc55cSDimitry Andric     loc[-2] = 0x8b;
405349cc55cSDimitry Andric     write32le(loc, val);
406349cc55cSDimitry Andric   } else {
407349cc55cSDimitry Andric     // Convert call *x@tlsdesc(%eax) to xchg ax, ax.
408349cc55cSDimitry Andric     assert(rel.type == R_386_TLS_DESC_CALL);
409349cc55cSDimitry Andric     loc[0] = 0x66;
410349cc55cSDimitry Andric     loc[1] = 0x90;
411349cc55cSDimitry Andric   }
4120b57cec5SDimitry Andric }
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric // In some conditions, relocations can be optimized to avoid using GOT.
4150b57cec5SDimitry Andric // This function does that for Initial Exec to Local Exec case.
relaxTlsIeToLe(uint8_t * loc,const Relocation & rel,uint64_t val)416bdd1243dSDimitry Andric static void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) {
4170b57cec5SDimitry Andric   // Ulrich's document section 6.2 says that @gotntpoff can
4180b57cec5SDimitry Andric   // be used with MOVL or ADDL instructions.
4190b57cec5SDimitry Andric   // @indntpoff is similar to @gotntpoff, but for use in
4200b57cec5SDimitry Andric   // position dependent code.
4210b57cec5SDimitry Andric   uint8_t reg = (loc[-1] >> 3) & 7;
4220b57cec5SDimitry Andric 
4235ffd83dbSDimitry Andric   if (rel.type == R_386_TLS_IE) {
4240b57cec5SDimitry Andric     if (loc[-1] == 0xa1) {
4250b57cec5SDimitry Andric       // "movl foo@indntpoff,%eax" -> "movl $foo,%eax"
4260b57cec5SDimitry Andric       // This case is different from the generic case below because
4270b57cec5SDimitry Andric       // this is a 5 byte instruction while below is 6 bytes.
4280b57cec5SDimitry Andric       loc[-1] = 0xb8;
4290b57cec5SDimitry Andric     } else if (loc[-2] == 0x8b) {
4300b57cec5SDimitry Andric       // "movl foo@indntpoff,%reg" -> "movl $foo,%reg"
4310b57cec5SDimitry Andric       loc[-2] = 0xc7;
4320b57cec5SDimitry Andric       loc[-1] = 0xc0 | reg;
4330b57cec5SDimitry Andric     } else {
4340b57cec5SDimitry Andric       // "addl foo@indntpoff,%reg" -> "addl $foo,%reg"
4350b57cec5SDimitry Andric       loc[-2] = 0x81;
4360b57cec5SDimitry Andric       loc[-1] = 0xc0 | reg;
4370b57cec5SDimitry Andric     }
4380b57cec5SDimitry Andric   } else {
4395ffd83dbSDimitry Andric     assert(rel.type == R_386_TLS_GOTIE);
4400b57cec5SDimitry Andric     if (loc[-2] == 0x8b) {
4410b57cec5SDimitry Andric       // "movl foo@gottpoff(%rip),%reg" -> "movl $foo,%reg"
4420b57cec5SDimitry Andric       loc[-2] = 0xc7;
4430b57cec5SDimitry Andric       loc[-1] = 0xc0 | reg;
4440b57cec5SDimitry Andric     } else {
4450b57cec5SDimitry Andric       // "addl foo@gotntpoff(%rip),%reg" -> "leal foo(%reg),%reg"
4460b57cec5SDimitry Andric       loc[-2] = 0x8d;
4470b57cec5SDimitry Andric       loc[-1] = 0x80 | (reg << 3) | reg;
4480b57cec5SDimitry Andric     }
4490b57cec5SDimitry Andric   }
4500b57cec5SDimitry Andric   write32le(loc, val);
4510b57cec5SDimitry Andric }
4520b57cec5SDimitry Andric 
relaxTlsLdToLe(uint8_t * loc,const Relocation & rel,uint64_t val)453bdd1243dSDimitry Andric static void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) {
4545ffd83dbSDimitry Andric   if (rel.type == R_386_TLS_LDO_32) {
4550b57cec5SDimitry Andric     write32le(loc, val);
4560b57cec5SDimitry Andric     return;
4570b57cec5SDimitry Andric   }
4580b57cec5SDimitry Andric 
459bdd1243dSDimitry Andric   if (loc[4] == 0xe8) {
4600b57cec5SDimitry Andric     // Convert
461bdd1243dSDimitry Andric     //   leal x(%reg),%eax
462bdd1243dSDimitry Andric     //   call ___tls_get_addr@plt
4630b57cec5SDimitry Andric     // to
4640b57cec5SDimitry Andric     const uint8_t inst[] = {
4650b57cec5SDimitry Andric         0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
4660b57cec5SDimitry Andric         0x90,                               // nop
4670b57cec5SDimitry Andric         0x8d, 0x74, 0x26, 0x00,             // leal 0(%esi,1),%esi
4680b57cec5SDimitry Andric     };
4690b57cec5SDimitry Andric     memcpy(loc - 2, inst, sizeof(inst));
470bdd1243dSDimitry Andric     return;
471bdd1243dSDimitry Andric   }
472bdd1243dSDimitry Andric 
473bdd1243dSDimitry Andric   // Convert
474bdd1243dSDimitry Andric   //   leal x(%reg),%eax
475bdd1243dSDimitry Andric   //   call *___tls_get_addr@got(%reg)
476bdd1243dSDimitry Andric   // to
477bdd1243dSDimitry Andric   const uint8_t inst[] = {
478bdd1243dSDimitry Andric       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
479bdd1243dSDimitry Andric       0x8d, 0xb6, 0x00, 0x00, 0x00, 0x00, // leal (%esi),%esi
480bdd1243dSDimitry Andric   };
481bdd1243dSDimitry Andric   memcpy(loc - 2, inst, sizeof(inst));
482bdd1243dSDimitry Andric }
483bdd1243dSDimitry Andric 
relocateAlloc(InputSectionBase & sec,uint8_t * buf) const484bdd1243dSDimitry Andric void X86::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
485bdd1243dSDimitry Andric   uint64_t secAddr = sec.getOutputSection()->addr;
486bdd1243dSDimitry Andric   if (auto *s = dyn_cast<InputSection>(&sec))
487bdd1243dSDimitry Andric     secAddr += s->outSecOff;
488bdd1243dSDimitry Andric   for (const Relocation &rel : sec.relocs()) {
489bdd1243dSDimitry Andric     uint8_t *loc = buf + rel.offset;
490bdd1243dSDimitry Andric     const uint64_t val = SignExtend64(
491bdd1243dSDimitry Andric         sec.getRelocTargetVA(sec.file, rel.type, rel.addend,
492bdd1243dSDimitry Andric                              secAddr + rel.offset, *rel.sym, rel.expr),
493bdd1243dSDimitry Andric         32);
494bdd1243dSDimitry Andric     switch (rel.expr) {
495bdd1243dSDimitry Andric     case R_RELAX_TLS_GD_TO_IE_GOTPLT:
496bdd1243dSDimitry Andric       relaxTlsGdToIe(loc, rel, val);
497bdd1243dSDimitry Andric       continue;
498bdd1243dSDimitry Andric     case R_RELAX_TLS_GD_TO_LE:
499bdd1243dSDimitry Andric     case R_RELAX_TLS_GD_TO_LE_NEG:
500bdd1243dSDimitry Andric       relaxTlsGdToLe(loc, rel, val);
501bdd1243dSDimitry Andric       continue;
502bdd1243dSDimitry Andric     case R_RELAX_TLS_LD_TO_LE:
503bdd1243dSDimitry Andric       relaxTlsLdToLe(loc, rel, val);
504bdd1243dSDimitry Andric       break;
505bdd1243dSDimitry Andric     case R_RELAX_TLS_IE_TO_LE:
506bdd1243dSDimitry Andric       relaxTlsIeToLe(loc, rel, val);
507bdd1243dSDimitry Andric       continue;
508bdd1243dSDimitry Andric     default:
509bdd1243dSDimitry Andric       relocate(loc, rel, val);
510bdd1243dSDimitry Andric       break;
511bdd1243dSDimitry Andric     }
512bdd1243dSDimitry Andric   }
5130b57cec5SDimitry Andric }
5140b57cec5SDimitry Andric 
515480093f4SDimitry Andric // If Intel Indirect Branch Tracking is enabled, we have to emit special PLT
516480093f4SDimitry Andric // entries containing endbr32 instructions. A PLT entry will be split into two
517480093f4SDimitry Andric // parts, one in .plt.sec (writePlt), and the other in .plt (writeIBTPlt).
518480093f4SDimitry Andric namespace {
519480093f4SDimitry Andric class IntelIBT : public X86 {
520480093f4SDimitry Andric public:
521480093f4SDimitry Andric   IntelIBT();
522480093f4SDimitry Andric   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
523480093f4SDimitry Andric   void writePlt(uint8_t *buf, const Symbol &sym,
524480093f4SDimitry Andric                 uint64_t pltEntryAddr) const override;
525480093f4SDimitry Andric   void writeIBTPlt(uint8_t *buf, size_t numEntries) const override;
526480093f4SDimitry Andric 
527480093f4SDimitry Andric   static const unsigned IBTPltHeaderSize = 16;
528480093f4SDimitry Andric };
529480093f4SDimitry Andric } // namespace
530480093f4SDimitry Andric 
IntelIBT()531480093f4SDimitry Andric IntelIBT::IntelIBT() { pltHeaderSize = 0; }
532480093f4SDimitry Andric 
writeGotPlt(uint8_t * buf,const Symbol & s) const533480093f4SDimitry Andric void IntelIBT::writeGotPlt(uint8_t *buf, const Symbol &s) const {
534480093f4SDimitry Andric   uint64_t va =
53504eeddc0SDimitry Andric       in.ibtPlt->getVA() + IBTPltHeaderSize + s.getPltIdx() * pltEntrySize;
536480093f4SDimitry Andric   write32le(buf, va);
537480093f4SDimitry Andric }
538480093f4SDimitry Andric 
writePlt(uint8_t * buf,const Symbol & sym,uint64_t) const539480093f4SDimitry Andric void IntelIBT::writePlt(uint8_t *buf, const Symbol &sym,
540480093f4SDimitry Andric                         uint64_t /*pltEntryAddr*/) const {
541480093f4SDimitry Andric   if (config->isPic) {
542480093f4SDimitry Andric     const uint8_t inst[] = {
543480093f4SDimitry Andric         0xf3, 0x0f, 0x1e, 0xfb,       // endbr32
544480093f4SDimitry Andric         0xff, 0xa3, 0,    0,    0, 0, // jmp *name@GOT(%ebx)
545480093f4SDimitry Andric         0x66, 0x0f, 0x1f, 0x44, 0, 0, // nop
546480093f4SDimitry Andric     };
547480093f4SDimitry Andric     memcpy(buf, inst, sizeof(inst));
548480093f4SDimitry Andric     write32le(buf + 6, sym.getGotPltVA() - in.gotPlt->getVA());
549480093f4SDimitry Andric     return;
550480093f4SDimitry Andric   }
551480093f4SDimitry Andric 
552480093f4SDimitry Andric   const uint8_t inst[] = {
553480093f4SDimitry Andric       0xf3, 0x0f, 0x1e, 0xfb,       // endbr32
554480093f4SDimitry Andric       0xff, 0x25, 0,    0,    0, 0, // jmp *foo@GOT
555480093f4SDimitry Andric       0x66, 0x0f, 0x1f, 0x44, 0, 0, // nop
556480093f4SDimitry Andric   };
557480093f4SDimitry Andric   memcpy(buf, inst, sizeof(inst));
558480093f4SDimitry Andric   write32le(buf + 6, sym.getGotPltVA());
559480093f4SDimitry Andric }
560480093f4SDimitry Andric 
writeIBTPlt(uint8_t * buf,size_t numEntries) const561480093f4SDimitry Andric void IntelIBT::writeIBTPlt(uint8_t *buf, size_t numEntries) const {
562480093f4SDimitry Andric   writePltHeader(buf);
563480093f4SDimitry Andric   buf += IBTPltHeaderSize;
564480093f4SDimitry Andric 
565480093f4SDimitry Andric   const uint8_t inst[] = {
566480093f4SDimitry Andric       0xf3, 0x0f, 0x1e, 0xfb,    // endbr32
567480093f4SDimitry Andric       0x68, 0,    0,    0,    0, // pushl $reloc_offset
568480093f4SDimitry Andric       0xe9, 0,    0,    0,    0, // jmpq .PLT0@PC
569480093f4SDimitry Andric       0x66, 0x90,                // nop
570480093f4SDimitry Andric   };
571480093f4SDimitry Andric 
572480093f4SDimitry Andric   for (size_t i = 0; i < numEntries; ++i) {
573480093f4SDimitry Andric     memcpy(buf, inst, sizeof(inst));
574480093f4SDimitry Andric     write32le(buf + 5, i * sizeof(object::ELF32LE::Rel));
575480093f4SDimitry Andric     write32le(buf + 10, -pltHeaderSize - sizeof(inst) * i - 30);
576480093f4SDimitry Andric     buf += sizeof(inst);
577480093f4SDimitry Andric   }
578480093f4SDimitry Andric }
579480093f4SDimitry Andric 
5800b57cec5SDimitry Andric namespace {
5810b57cec5SDimitry Andric class RetpolinePic : public X86 {
5820b57cec5SDimitry Andric public:
5830b57cec5SDimitry Andric   RetpolinePic();
5840b57cec5SDimitry Andric   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
5850b57cec5SDimitry Andric   void writePltHeader(uint8_t *buf) const override;
586480093f4SDimitry Andric   void writePlt(uint8_t *buf, const Symbol &sym,
587480093f4SDimitry Andric                 uint64_t pltEntryAddr) const override;
5880b57cec5SDimitry Andric };
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric class RetpolineNoPic : public X86 {
5910b57cec5SDimitry Andric public:
5920b57cec5SDimitry Andric   RetpolineNoPic();
5930b57cec5SDimitry Andric   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
5940b57cec5SDimitry Andric   void writePltHeader(uint8_t *buf) const override;
595480093f4SDimitry Andric   void writePlt(uint8_t *buf, const Symbol &sym,
596480093f4SDimitry Andric                 uint64_t pltEntryAddr) const override;
5970b57cec5SDimitry Andric };
5980b57cec5SDimitry Andric } // namespace
5990b57cec5SDimitry Andric 
RetpolinePic()6000b57cec5SDimitry Andric RetpolinePic::RetpolinePic() {
6010b57cec5SDimitry Andric   pltHeaderSize = 48;
6020b57cec5SDimitry Andric   pltEntrySize = 32;
603480093f4SDimitry Andric   ipltEntrySize = 32;
6040b57cec5SDimitry Andric }
6050b57cec5SDimitry Andric 
writeGotPlt(uint8_t * buf,const Symbol & s) const6060b57cec5SDimitry Andric void RetpolinePic::writeGotPlt(uint8_t *buf, const Symbol &s) const {
6070b57cec5SDimitry Andric   write32le(buf, s.getPltVA() + 17);
6080b57cec5SDimitry Andric }
6090b57cec5SDimitry Andric 
writePltHeader(uint8_t * buf) const6100b57cec5SDimitry Andric void RetpolinePic::writePltHeader(uint8_t *buf) const {
6110b57cec5SDimitry Andric   const uint8_t insn[] = {
6120b57cec5SDimitry Andric       0xff, 0xb3, 4,    0,    0,    0,          // 0:    pushl 4(%ebx)
6130b57cec5SDimitry Andric       0x50,                                     // 6:    pushl %eax
6140b57cec5SDimitry Andric       0x8b, 0x83, 8,    0,    0,    0,          // 7:    mov 8(%ebx), %eax
6150b57cec5SDimitry Andric       0xe8, 0x0e, 0x00, 0x00, 0x00,             // d:    call next
6160b57cec5SDimitry Andric       0xf3, 0x90,                               // 12: loop: pause
6170b57cec5SDimitry Andric       0x0f, 0xae, 0xe8,                         // 14:   lfence
6180b57cec5SDimitry Andric       0xeb, 0xf9,                               // 17:   jmp loop
6190b57cec5SDimitry Andric       0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 19:   int3; .align 16
6200b57cec5SDimitry Andric       0x89, 0x0c, 0x24,                         // 20: next: mov %ecx, (%esp)
6210b57cec5SDimitry Andric       0x8b, 0x4c, 0x24, 0x04,                   // 23:   mov 0x4(%esp), %ecx
6220b57cec5SDimitry Andric       0x89, 0x44, 0x24, 0x04,                   // 27:   mov %eax ,0x4(%esp)
6230b57cec5SDimitry Andric       0x89, 0xc8,                               // 2b:   mov %ecx, %eax
6240b57cec5SDimitry Andric       0x59,                                     // 2d:   pop %ecx
6250b57cec5SDimitry Andric       0xc3,                                     // 2e:   ret
6260b57cec5SDimitry Andric       0xcc,                                     // 2f:   int3; padding
6270b57cec5SDimitry Andric   };
6280b57cec5SDimitry Andric   memcpy(buf, insn, sizeof(insn));
6290b57cec5SDimitry Andric }
6300b57cec5SDimitry Andric 
writePlt(uint8_t * buf,const Symbol & sym,uint64_t pltEntryAddr) const631480093f4SDimitry Andric void RetpolinePic::writePlt(uint8_t *buf, const Symbol &sym,
632480093f4SDimitry Andric                             uint64_t pltEntryAddr) const {
63304eeddc0SDimitry Andric   unsigned relOff = in.relaPlt->entsize * sym.getPltIdx();
6340b57cec5SDimitry Andric   const uint8_t insn[] = {
6350b57cec5SDimitry Andric       0x50,                            // pushl %eax
6360b57cec5SDimitry Andric       0x8b, 0x83, 0,    0,    0,    0, // mov foo@GOT(%ebx), %eax
6370b57cec5SDimitry Andric       0xe8, 0,    0,    0,    0,       // call plt+0x20
6380b57cec5SDimitry Andric       0xe9, 0,    0,    0,    0,       // jmp plt+0x12
6390b57cec5SDimitry Andric       0x68, 0,    0,    0,    0,       // pushl $reloc_offset
6400b57cec5SDimitry Andric       0xe9, 0,    0,    0,    0,       // jmp plt+0
6410b57cec5SDimitry Andric       0xcc, 0xcc, 0xcc, 0xcc, 0xcc,    // int3; padding
6420b57cec5SDimitry Andric   };
6430b57cec5SDimitry Andric   memcpy(buf, insn, sizeof(insn));
6440b57cec5SDimitry Andric 
6450b57cec5SDimitry Andric   uint32_t ebx = in.gotPlt->getVA();
646480093f4SDimitry Andric   unsigned off = pltEntryAddr - in.plt->getVA();
647480093f4SDimitry Andric   write32le(buf + 3, sym.getGotPltVA() - ebx);
6480b57cec5SDimitry Andric   write32le(buf + 8, -off - 12 + 32);
6490b57cec5SDimitry Andric   write32le(buf + 13, -off - 17 + 18);
6500b57cec5SDimitry Andric   write32le(buf + 18, relOff);
6510b57cec5SDimitry Andric   write32le(buf + 23, -off - 27);
6520b57cec5SDimitry Andric }
6530b57cec5SDimitry Andric 
RetpolineNoPic()6540b57cec5SDimitry Andric RetpolineNoPic::RetpolineNoPic() {
6550b57cec5SDimitry Andric   pltHeaderSize = 48;
6560b57cec5SDimitry Andric   pltEntrySize = 32;
657480093f4SDimitry Andric   ipltEntrySize = 32;
6580b57cec5SDimitry Andric }
6590b57cec5SDimitry Andric 
writeGotPlt(uint8_t * buf,const Symbol & s) const6600b57cec5SDimitry Andric void RetpolineNoPic::writeGotPlt(uint8_t *buf, const Symbol &s) const {
6610b57cec5SDimitry Andric   write32le(buf, s.getPltVA() + 16);
6620b57cec5SDimitry Andric }
6630b57cec5SDimitry Andric 
writePltHeader(uint8_t * buf) const6640b57cec5SDimitry Andric void RetpolineNoPic::writePltHeader(uint8_t *buf) const {
6650b57cec5SDimitry Andric   const uint8_t insn[] = {
6660b57cec5SDimitry Andric       0xff, 0x35, 0,    0,    0,    0, // 0:    pushl GOTPLT+4
6670b57cec5SDimitry Andric       0x50,                            // 6:    pushl %eax
6680b57cec5SDimitry Andric       0xa1, 0,    0,    0,    0,       // 7:    mov GOTPLT+8, %eax
6690b57cec5SDimitry Andric       0xe8, 0x0f, 0x00, 0x00, 0x00,    // c:    call next
6700b57cec5SDimitry Andric       0xf3, 0x90,                      // 11: loop: pause
6710b57cec5SDimitry Andric       0x0f, 0xae, 0xe8,                // 13:   lfence
6720b57cec5SDimitry Andric       0xeb, 0xf9,                      // 16:   jmp loop
6730b57cec5SDimitry Andric       0xcc, 0xcc, 0xcc, 0xcc, 0xcc,    // 18:   int3
6740b57cec5SDimitry Andric       0xcc, 0xcc, 0xcc,                // 1f:   int3; .align 16
6750b57cec5SDimitry Andric       0x89, 0x0c, 0x24,                // 20: next: mov %ecx, (%esp)
6760b57cec5SDimitry Andric       0x8b, 0x4c, 0x24, 0x04,          // 23:   mov 0x4(%esp), %ecx
6770b57cec5SDimitry Andric       0x89, 0x44, 0x24, 0x04,          // 27:   mov %eax ,0x4(%esp)
6780b57cec5SDimitry Andric       0x89, 0xc8,                      // 2b:   mov %ecx, %eax
6790b57cec5SDimitry Andric       0x59,                            // 2d:   pop %ecx
6800b57cec5SDimitry Andric       0xc3,                            // 2e:   ret
6810b57cec5SDimitry Andric       0xcc,                            // 2f:   int3; padding
6820b57cec5SDimitry Andric   };
6830b57cec5SDimitry Andric   memcpy(buf, insn, sizeof(insn));
6840b57cec5SDimitry Andric 
6850b57cec5SDimitry Andric   uint32_t gotPlt = in.gotPlt->getVA();
6860b57cec5SDimitry Andric   write32le(buf + 2, gotPlt + 4);
6870b57cec5SDimitry Andric   write32le(buf + 8, gotPlt + 8);
6880b57cec5SDimitry Andric }
6890b57cec5SDimitry Andric 
writePlt(uint8_t * buf,const Symbol & sym,uint64_t pltEntryAddr) const690480093f4SDimitry Andric void RetpolineNoPic::writePlt(uint8_t *buf, const Symbol &sym,
691480093f4SDimitry Andric                               uint64_t pltEntryAddr) const {
69204eeddc0SDimitry Andric   unsigned relOff = in.relaPlt->entsize * sym.getPltIdx();
6930b57cec5SDimitry Andric   const uint8_t insn[] = {
6940b57cec5SDimitry Andric       0x50,                         // 0:  pushl %eax
6950b57cec5SDimitry Andric       0xa1, 0,    0,    0,    0,    // 1:  mov foo_in_GOT, %eax
6960b57cec5SDimitry Andric       0xe8, 0,    0,    0,    0,    // 6:  call plt+0x20
6970b57cec5SDimitry Andric       0xe9, 0,    0,    0,    0,    // b:  jmp plt+0x11
6980b57cec5SDimitry Andric       0x68, 0,    0,    0,    0,    // 10: pushl $reloc_offset
6990b57cec5SDimitry Andric       0xe9, 0,    0,    0,    0,    // 15: jmp plt+0
7000b57cec5SDimitry Andric       0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 1a: int3; padding
7010b57cec5SDimitry Andric       0xcc,                         // 1f: int3; padding
7020b57cec5SDimitry Andric   };
7030b57cec5SDimitry Andric   memcpy(buf, insn, sizeof(insn));
7040b57cec5SDimitry Andric 
705480093f4SDimitry Andric   unsigned off = pltEntryAddr - in.plt->getVA();
706480093f4SDimitry Andric   write32le(buf + 2, sym.getGotPltVA());
7070b57cec5SDimitry Andric   write32le(buf + 7, -off - 11 + 32);
7080b57cec5SDimitry Andric   write32le(buf + 12, -off - 16 + 17);
7090b57cec5SDimitry Andric   write32le(buf + 17, relOff);
7100b57cec5SDimitry Andric   write32le(buf + 22, -off - 26);
7110b57cec5SDimitry Andric }
7120b57cec5SDimitry Andric 
getX86TargetInfo()7135ffd83dbSDimitry Andric TargetInfo *elf::getX86TargetInfo() {
7140b57cec5SDimitry Andric   if (config->zRetpolineplt) {
7150b57cec5SDimitry Andric     if (config->isPic) {
7160b57cec5SDimitry Andric       static RetpolinePic t;
7170b57cec5SDimitry Andric       return &t;
7180b57cec5SDimitry Andric     }
7190b57cec5SDimitry Andric     static RetpolineNoPic t;
7200b57cec5SDimitry Andric     return &t;
7210b57cec5SDimitry Andric   }
7220b57cec5SDimitry Andric 
723480093f4SDimitry Andric   if (config->andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT) {
724480093f4SDimitry Andric     static IntelIBT t;
725480093f4SDimitry Andric     return &t;
726480093f4SDimitry Andric   }
727480093f4SDimitry Andric 
7280b57cec5SDimitry Andric   static X86 t;
7290b57cec5SDimitry Andric   return &t;
7300b57cec5SDimitry Andric }
731