xref: /freebsd/contrib/llvm-project/lld/ELF/Arch/X86_64.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric //===- X86_64.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 
90b57cec5SDimitry Andric #include "InputFiles.h"
105ffd83dbSDimitry Andric #include "OutputSections.h"
110b57cec5SDimitry Andric #include "Symbols.h"
120b57cec5SDimitry Andric #include "SyntheticSections.h"
130b57cec5SDimitry Andric #include "Target.h"
140b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h"
150b57cec5SDimitry Andric #include "llvm/Object/ELF.h"
160b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric using namespace llvm;
190b57cec5SDimitry Andric using namespace llvm::object;
200b57cec5SDimitry Andric using namespace llvm::support::endian;
210b57cec5SDimitry Andric using namespace llvm::ELF;
225ffd83dbSDimitry Andric using namespace lld;
235ffd83dbSDimitry Andric using namespace lld::elf;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric namespace {
260b57cec5SDimitry Andric class X86_64 : public TargetInfo {
270b57cec5SDimitry Andric public:
280b57cec5SDimitry Andric   X86_64();
290b57cec5SDimitry Andric   int getTlsGdRelaxSkip(RelType type) const override;
300b57cec5SDimitry Andric   RelExpr getRelExpr(RelType type, const Symbol &s,
310b57cec5SDimitry Andric                      const uint8_t *loc) const override;
320b57cec5SDimitry Andric   RelType getDynRel(RelType type) const override;
330b57cec5SDimitry Andric   void writeGotPltHeader(uint8_t *buf) const override;
340b57cec5SDimitry Andric   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
35fe6060f1SDimitry Andric   void writeIgotPlt(uint8_t *buf, const Symbol &s) const override;
360b57cec5SDimitry Andric   void writePltHeader(uint8_t *buf) const override;
37480093f4SDimitry Andric   void writePlt(uint8_t *buf, const Symbol &sym,
38480093f4SDimitry Andric                 uint64_t pltEntryAddr) const override;
395ffd83dbSDimitry Andric   void relocate(uint8_t *loc, const Relocation &rel,
405ffd83dbSDimitry Andric                 uint64_t val) const override;
41fe6060f1SDimitry Andric   int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
425ffd83dbSDimitry Andric   void applyJumpInstrMod(uint8_t *loc, JumpModType type,
435ffd83dbSDimitry Andric                          unsigned size) const override;
440b57cec5SDimitry Andric 
45e8d8bef9SDimitry Andric   RelExpr adjustGotPcExpr(RelType type, int64_t addend,
46e8d8bef9SDimitry Andric                           const uint8_t *loc) const override;
475ffd83dbSDimitry Andric   void relaxGot(uint8_t *loc, const Relocation &rel,
485ffd83dbSDimitry Andric                 uint64_t val) const override;
495ffd83dbSDimitry Andric   void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
505ffd83dbSDimitry Andric                       uint64_t val) const override;
515ffd83dbSDimitry Andric   void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
525ffd83dbSDimitry Andric                       uint64_t val) const override;
535ffd83dbSDimitry Andric   void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
545ffd83dbSDimitry Andric                       uint64_t val) const override;
555ffd83dbSDimitry Andric   void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,
565ffd83dbSDimitry Andric                       uint64_t val) const override;
570b57cec5SDimitry Andric   bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
580b57cec5SDimitry Andric                                         uint8_t stOther) const override;
595ffd83dbSDimitry Andric   bool deleteFallThruJmpInsn(InputSection &is, InputFile *file,
605ffd83dbSDimitry Andric                              InputSection *nextIS) const override;
610b57cec5SDimitry Andric };
620b57cec5SDimitry Andric } // namespace
630b57cec5SDimitry Andric 
645ffd83dbSDimitry Andric // This is vector of NOP instructions of sizes from 1 to 8 bytes.  The
655ffd83dbSDimitry Andric // appropriately sized instructions are used to fill the gaps between sections
665ffd83dbSDimitry Andric // which are executed during fall through.
675ffd83dbSDimitry Andric static const std::vector<std::vector<uint8_t>> nopInstructions = {
685ffd83dbSDimitry Andric     {0x90},
695ffd83dbSDimitry Andric     {0x66, 0x90},
705ffd83dbSDimitry Andric     {0x0f, 0x1f, 0x00},
715ffd83dbSDimitry Andric     {0x0f, 0x1f, 0x40, 0x00},
725ffd83dbSDimitry Andric     {0x0f, 0x1f, 0x44, 0x00, 0x00},
735ffd83dbSDimitry Andric     {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
745ffd83dbSDimitry Andric     {0x0F, 0x1F, 0x80, 0x00, 0x00, 0x00, 0x00},
755ffd83dbSDimitry Andric     {0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
765ffd83dbSDimitry Andric     {0x66, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}};
775ffd83dbSDimitry Andric 
780b57cec5SDimitry Andric X86_64::X86_64() {
790b57cec5SDimitry Andric   copyRel = R_X86_64_COPY;
800b57cec5SDimitry Andric   gotRel = R_X86_64_GLOB_DAT;
810b57cec5SDimitry Andric   pltRel = R_X86_64_JUMP_SLOT;
820b57cec5SDimitry Andric   relativeRel = R_X86_64_RELATIVE;
830b57cec5SDimitry Andric   iRelativeRel = R_X86_64_IRELATIVE;
840b57cec5SDimitry Andric   symbolicRel = R_X86_64_64;
850b57cec5SDimitry Andric   tlsDescRel = R_X86_64_TLSDESC;
860b57cec5SDimitry Andric   tlsGotRel = R_X86_64_TPOFF64;
870b57cec5SDimitry Andric   tlsModuleIndexRel = R_X86_64_DTPMOD64;
880b57cec5SDimitry Andric   tlsOffsetRel = R_X86_64_DTPOFF64;
89*349cc55cSDimitry Andric   gotBaseSymInGotPlt = true;
90fe6060f1SDimitry Andric   gotEntrySize = 8;
910b57cec5SDimitry Andric   pltHeaderSize = 16;
92480093f4SDimitry Andric   pltEntrySize = 16;
93480093f4SDimitry Andric   ipltEntrySize = 16;
940b57cec5SDimitry Andric   trapInstr = {0xcc, 0xcc, 0xcc, 0xcc}; // 0xcc = INT3
955ffd83dbSDimitry Andric   nopInstrs = nopInstructions;
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   // Align to the large page size (known as a superpage or huge page).
980b57cec5SDimitry Andric   // FreeBSD automatically promotes large, superpage-aligned allocations.
990b57cec5SDimitry Andric   defaultImageBase = 0x200000;
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric int X86_64::getTlsGdRelaxSkip(RelType type) const { return 2; }
1030b57cec5SDimitry Andric 
1045ffd83dbSDimitry Andric // Opcodes for the different X86_64 jmp instructions.
1055ffd83dbSDimitry Andric enum JmpInsnOpcode : uint32_t {
1065ffd83dbSDimitry Andric   J_JMP_32,
1075ffd83dbSDimitry Andric   J_JNE_32,
1085ffd83dbSDimitry Andric   J_JE_32,
1095ffd83dbSDimitry Andric   J_JG_32,
1105ffd83dbSDimitry Andric   J_JGE_32,
1115ffd83dbSDimitry Andric   J_JB_32,
1125ffd83dbSDimitry Andric   J_JBE_32,
1135ffd83dbSDimitry Andric   J_JL_32,
1145ffd83dbSDimitry Andric   J_JLE_32,
1155ffd83dbSDimitry Andric   J_JA_32,
1165ffd83dbSDimitry Andric   J_JAE_32,
1175ffd83dbSDimitry Andric   J_UNKNOWN,
1185ffd83dbSDimitry Andric };
1195ffd83dbSDimitry Andric 
1205ffd83dbSDimitry Andric // Given the first (optional) and second byte of the insn's opcode, this
1215ffd83dbSDimitry Andric // returns the corresponding enum value.
1225ffd83dbSDimitry Andric static JmpInsnOpcode getJmpInsnType(const uint8_t *first,
1235ffd83dbSDimitry Andric                                     const uint8_t *second) {
1245ffd83dbSDimitry Andric   if (*second == 0xe9)
1255ffd83dbSDimitry Andric     return J_JMP_32;
1265ffd83dbSDimitry Andric 
1275ffd83dbSDimitry Andric   if (first == nullptr)
1285ffd83dbSDimitry Andric     return J_UNKNOWN;
1295ffd83dbSDimitry Andric 
1305ffd83dbSDimitry Andric   if (*first == 0x0f) {
1315ffd83dbSDimitry Andric     switch (*second) {
1325ffd83dbSDimitry Andric     case 0x84:
1335ffd83dbSDimitry Andric       return J_JE_32;
1345ffd83dbSDimitry Andric     case 0x85:
1355ffd83dbSDimitry Andric       return J_JNE_32;
1365ffd83dbSDimitry Andric     case 0x8f:
1375ffd83dbSDimitry Andric       return J_JG_32;
1385ffd83dbSDimitry Andric     case 0x8d:
1395ffd83dbSDimitry Andric       return J_JGE_32;
1405ffd83dbSDimitry Andric     case 0x82:
1415ffd83dbSDimitry Andric       return J_JB_32;
1425ffd83dbSDimitry Andric     case 0x86:
1435ffd83dbSDimitry Andric       return J_JBE_32;
1445ffd83dbSDimitry Andric     case 0x8c:
1455ffd83dbSDimitry Andric       return J_JL_32;
1465ffd83dbSDimitry Andric     case 0x8e:
1475ffd83dbSDimitry Andric       return J_JLE_32;
1485ffd83dbSDimitry Andric     case 0x87:
1495ffd83dbSDimitry Andric       return J_JA_32;
1505ffd83dbSDimitry Andric     case 0x83:
1515ffd83dbSDimitry Andric       return J_JAE_32;
1525ffd83dbSDimitry Andric     }
1535ffd83dbSDimitry Andric   }
1545ffd83dbSDimitry Andric   return J_UNKNOWN;
1555ffd83dbSDimitry Andric }
1565ffd83dbSDimitry Andric 
1575ffd83dbSDimitry Andric // Return the relocation index for input section IS with a specific Offset.
1585ffd83dbSDimitry Andric // Returns the maximum size of the vector if no such relocation is found.
1595ffd83dbSDimitry Andric static unsigned getRelocationWithOffset(const InputSection &is,
1605ffd83dbSDimitry Andric                                         uint64_t offset) {
1615ffd83dbSDimitry Andric   unsigned size = is.relocations.size();
1625ffd83dbSDimitry Andric   for (unsigned i = size - 1; i + 1 > 0; --i) {
1635ffd83dbSDimitry Andric     if (is.relocations[i].offset == offset && is.relocations[i].expr != R_NONE)
1645ffd83dbSDimitry Andric       return i;
1655ffd83dbSDimitry Andric   }
1665ffd83dbSDimitry Andric   return size;
1675ffd83dbSDimitry Andric }
1685ffd83dbSDimitry Andric 
1695ffd83dbSDimitry Andric // Returns true if R corresponds to a relocation used for a jump instruction.
1705ffd83dbSDimitry Andric // TODO: Once special relocations for relaxable jump instructions are available,
1715ffd83dbSDimitry Andric // this should be modified to use those relocations.
1725ffd83dbSDimitry Andric static bool isRelocationForJmpInsn(Relocation &R) {
1735ffd83dbSDimitry Andric   return R.type == R_X86_64_PLT32 || R.type == R_X86_64_PC32 ||
1745ffd83dbSDimitry Andric          R.type == R_X86_64_PC8;
1755ffd83dbSDimitry Andric }
1765ffd83dbSDimitry Andric 
1775ffd83dbSDimitry Andric // Return true if Relocation R points to the first instruction in the
1785ffd83dbSDimitry Andric // next section.
1795ffd83dbSDimitry Andric // TODO: Delete this once psABI reserves a new relocation type for fall thru
1805ffd83dbSDimitry Andric // jumps.
1815ffd83dbSDimitry Andric static bool isFallThruRelocation(InputSection &is, InputFile *file,
1825ffd83dbSDimitry Andric                                  InputSection *nextIS, Relocation &r) {
1835ffd83dbSDimitry Andric   if (!isRelocationForJmpInsn(r))
1845ffd83dbSDimitry Andric     return false;
1855ffd83dbSDimitry Andric 
1865ffd83dbSDimitry Andric   uint64_t addrLoc = is.getOutputSection()->addr + is.outSecOff + r.offset;
1875ffd83dbSDimitry Andric   uint64_t targetOffset = InputSectionBase::getRelocTargetVA(
1885ffd83dbSDimitry Andric       file, r.type, r.addend, addrLoc, *r.sym, r.expr);
1895ffd83dbSDimitry Andric 
1905ffd83dbSDimitry Andric   // If this jmp is a fall thru, the target offset is the beginning of the
1915ffd83dbSDimitry Andric   // next section.
1925ffd83dbSDimitry Andric   uint64_t nextSectionOffset =
1935ffd83dbSDimitry Andric       nextIS->getOutputSection()->addr + nextIS->outSecOff;
1945ffd83dbSDimitry Andric   return (addrLoc + 4 + targetOffset) == nextSectionOffset;
1955ffd83dbSDimitry Andric }
1965ffd83dbSDimitry Andric 
1975ffd83dbSDimitry Andric // Return the jmp instruction opcode that is the inverse of the given
1985ffd83dbSDimitry Andric // opcode.  For example, JE inverted is JNE.
1995ffd83dbSDimitry Andric static JmpInsnOpcode invertJmpOpcode(const JmpInsnOpcode opcode) {
2005ffd83dbSDimitry Andric   switch (opcode) {
2015ffd83dbSDimitry Andric   case J_JE_32:
2025ffd83dbSDimitry Andric     return J_JNE_32;
2035ffd83dbSDimitry Andric   case J_JNE_32:
2045ffd83dbSDimitry Andric     return J_JE_32;
2055ffd83dbSDimitry Andric   case J_JG_32:
2065ffd83dbSDimitry Andric     return J_JLE_32;
2075ffd83dbSDimitry Andric   case J_JGE_32:
2085ffd83dbSDimitry Andric     return J_JL_32;
2095ffd83dbSDimitry Andric   case J_JB_32:
2105ffd83dbSDimitry Andric     return J_JAE_32;
2115ffd83dbSDimitry Andric   case J_JBE_32:
2125ffd83dbSDimitry Andric     return J_JA_32;
2135ffd83dbSDimitry Andric   case J_JL_32:
2145ffd83dbSDimitry Andric     return J_JGE_32;
2155ffd83dbSDimitry Andric   case J_JLE_32:
2165ffd83dbSDimitry Andric     return J_JG_32;
2175ffd83dbSDimitry Andric   case J_JA_32:
2185ffd83dbSDimitry Andric     return J_JBE_32;
2195ffd83dbSDimitry Andric   case J_JAE_32:
2205ffd83dbSDimitry Andric     return J_JB_32;
2215ffd83dbSDimitry Andric   default:
2225ffd83dbSDimitry Andric     return J_UNKNOWN;
2235ffd83dbSDimitry Andric   }
2245ffd83dbSDimitry Andric }
2255ffd83dbSDimitry Andric 
2265ffd83dbSDimitry Andric // Deletes direct jump instruction in input sections that jumps to the
2275ffd83dbSDimitry Andric // following section as it is not required.  If there are two consecutive jump
2285ffd83dbSDimitry Andric // instructions, it checks if they can be flipped and one can be deleted.
2295ffd83dbSDimitry Andric // For example:
2305ffd83dbSDimitry Andric // .section .text
2315ffd83dbSDimitry Andric // a.BB.foo:
2325ffd83dbSDimitry Andric //    ...
2335ffd83dbSDimitry Andric //    10: jne aa.BB.foo
2345ffd83dbSDimitry Andric //    16: jmp bar
2355ffd83dbSDimitry Andric // aa.BB.foo:
2365ffd83dbSDimitry Andric //    ...
2375ffd83dbSDimitry Andric //
2385ffd83dbSDimitry Andric // can be converted to:
2395ffd83dbSDimitry Andric // a.BB.foo:
2405ffd83dbSDimitry Andric //   ...
2415ffd83dbSDimitry Andric //   10: je bar  #jne flipped to je and the jmp is deleted.
2425ffd83dbSDimitry Andric // aa.BB.foo:
2435ffd83dbSDimitry Andric //   ...
2445ffd83dbSDimitry Andric bool X86_64::deleteFallThruJmpInsn(InputSection &is, InputFile *file,
2455ffd83dbSDimitry Andric                                    InputSection *nextIS) const {
2465ffd83dbSDimitry Andric   const unsigned sizeOfDirectJmpInsn = 5;
2475ffd83dbSDimitry Andric 
2485ffd83dbSDimitry Andric   if (nextIS == nullptr)
2495ffd83dbSDimitry Andric     return false;
2505ffd83dbSDimitry Andric 
2515ffd83dbSDimitry Andric   if (is.getSize() < sizeOfDirectJmpInsn)
2525ffd83dbSDimitry Andric     return false;
2535ffd83dbSDimitry Andric 
2545ffd83dbSDimitry Andric   // If this jmp insn can be removed, it is the last insn and the
2555ffd83dbSDimitry Andric   // relocation is 4 bytes before the end.
2565ffd83dbSDimitry Andric   unsigned rIndex = getRelocationWithOffset(is, is.getSize() - 4);
2575ffd83dbSDimitry Andric   if (rIndex == is.relocations.size())
2585ffd83dbSDimitry Andric     return false;
2595ffd83dbSDimitry Andric 
2605ffd83dbSDimitry Andric   Relocation &r = is.relocations[rIndex];
2615ffd83dbSDimitry Andric 
2625ffd83dbSDimitry Andric   // Check if the relocation corresponds to a direct jmp.
2635ffd83dbSDimitry Andric   const uint8_t *secContents = is.data().data();
2645ffd83dbSDimitry Andric   // If it is not a direct jmp instruction, there is nothing to do here.
2655ffd83dbSDimitry Andric   if (*(secContents + r.offset - 1) != 0xe9)
2665ffd83dbSDimitry Andric     return false;
2675ffd83dbSDimitry Andric 
2685ffd83dbSDimitry Andric   if (isFallThruRelocation(is, file, nextIS, r)) {
2695ffd83dbSDimitry Andric     // This is a fall thru and can be deleted.
2705ffd83dbSDimitry Andric     r.expr = R_NONE;
2715ffd83dbSDimitry Andric     r.offset = 0;
2725ffd83dbSDimitry Andric     is.drop_back(sizeOfDirectJmpInsn);
2735ffd83dbSDimitry Andric     is.nopFiller = true;
2745ffd83dbSDimitry Andric     return true;
2755ffd83dbSDimitry Andric   }
2765ffd83dbSDimitry Andric 
2775ffd83dbSDimitry Andric   // Now, check if flip and delete is possible.
2785ffd83dbSDimitry Andric   const unsigned sizeOfJmpCCInsn = 6;
2795ffd83dbSDimitry Andric   // To flip, there must be atleast one JmpCC and one direct jmp.
2805ffd83dbSDimitry Andric   if (is.getSize() < sizeOfDirectJmpInsn + sizeOfJmpCCInsn)
2815ffd83dbSDimitry Andric     return 0;
2825ffd83dbSDimitry Andric 
2835ffd83dbSDimitry Andric   unsigned rbIndex =
2845ffd83dbSDimitry Andric       getRelocationWithOffset(is, (is.getSize() - sizeOfDirectJmpInsn - 4));
2855ffd83dbSDimitry Andric   if (rbIndex == is.relocations.size())
2865ffd83dbSDimitry Andric     return 0;
2875ffd83dbSDimitry Andric 
2885ffd83dbSDimitry Andric   Relocation &rB = is.relocations[rbIndex];
2895ffd83dbSDimitry Andric 
2905ffd83dbSDimitry Andric   const uint8_t *jmpInsnB = secContents + rB.offset - 1;
2915ffd83dbSDimitry Andric   JmpInsnOpcode jmpOpcodeB = getJmpInsnType(jmpInsnB - 1, jmpInsnB);
2925ffd83dbSDimitry Andric   if (jmpOpcodeB == J_UNKNOWN)
2935ffd83dbSDimitry Andric     return false;
2945ffd83dbSDimitry Andric 
2955ffd83dbSDimitry Andric   if (!isFallThruRelocation(is, file, nextIS, rB))
2965ffd83dbSDimitry Andric     return false;
2975ffd83dbSDimitry Andric 
2985ffd83dbSDimitry Andric   // jmpCC jumps to the fall thru block, the branch can be flipped and the
2995ffd83dbSDimitry Andric   // jmp can be deleted.
3005ffd83dbSDimitry Andric   JmpInsnOpcode jInvert = invertJmpOpcode(jmpOpcodeB);
3015ffd83dbSDimitry Andric   if (jInvert == J_UNKNOWN)
3025ffd83dbSDimitry Andric     return false;
3035ffd83dbSDimitry Andric   is.jumpInstrMods.push_back({jInvert, (rB.offset - 1), 4});
3045ffd83dbSDimitry Andric   // Move R's values to rB except the offset.
3055ffd83dbSDimitry Andric   rB = {r.expr, r.type, rB.offset, r.addend, r.sym};
3065ffd83dbSDimitry Andric   // Cancel R
3075ffd83dbSDimitry Andric   r.expr = R_NONE;
3085ffd83dbSDimitry Andric   r.offset = 0;
3095ffd83dbSDimitry Andric   is.drop_back(sizeOfDirectJmpInsn);
3105ffd83dbSDimitry Andric   is.nopFiller = true;
3115ffd83dbSDimitry Andric   return true;
3125ffd83dbSDimitry Andric }
3135ffd83dbSDimitry Andric 
3140b57cec5SDimitry Andric RelExpr X86_64::getRelExpr(RelType type, const Symbol &s,
3150b57cec5SDimitry Andric                            const uint8_t *loc) const {
3160b57cec5SDimitry Andric   if (type == R_X86_64_GOTTPOFF)
3170b57cec5SDimitry Andric     config->hasStaticTlsModel = true;
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric   switch (type) {
3200b57cec5SDimitry Andric   case R_X86_64_8:
3210b57cec5SDimitry Andric   case R_X86_64_16:
3220b57cec5SDimitry Andric   case R_X86_64_32:
3230b57cec5SDimitry Andric   case R_X86_64_32S:
3240b57cec5SDimitry Andric   case R_X86_64_64:
3250b57cec5SDimitry Andric     return R_ABS;
3260b57cec5SDimitry Andric   case R_X86_64_DTPOFF32:
3270b57cec5SDimitry Andric   case R_X86_64_DTPOFF64:
3280b57cec5SDimitry Andric     return R_DTPREL;
3290b57cec5SDimitry Andric   case R_X86_64_TPOFF32:
330e8d8bef9SDimitry Andric     return R_TPREL;
3310b57cec5SDimitry Andric   case R_X86_64_TLSDESC_CALL:
3320b57cec5SDimitry Andric     return R_TLSDESC_CALL;
3330b57cec5SDimitry Andric   case R_X86_64_TLSLD:
3340b57cec5SDimitry Andric     return R_TLSLD_PC;
3350b57cec5SDimitry Andric   case R_X86_64_TLSGD:
3360b57cec5SDimitry Andric     return R_TLSGD_PC;
3370b57cec5SDimitry Andric   case R_X86_64_SIZE32:
3380b57cec5SDimitry Andric   case R_X86_64_SIZE64:
3390b57cec5SDimitry Andric     return R_SIZE;
3400b57cec5SDimitry Andric   case R_X86_64_PLT32:
3410b57cec5SDimitry Andric     return R_PLT_PC;
3420b57cec5SDimitry Andric   case R_X86_64_PC8:
3430b57cec5SDimitry Andric   case R_X86_64_PC16:
3440b57cec5SDimitry Andric   case R_X86_64_PC32:
3450b57cec5SDimitry Andric   case R_X86_64_PC64:
3460b57cec5SDimitry Andric     return R_PC;
3470b57cec5SDimitry Andric   case R_X86_64_GOT32:
3480b57cec5SDimitry Andric   case R_X86_64_GOT64:
3490b57cec5SDimitry Andric     return R_GOTPLT;
3500b57cec5SDimitry Andric   case R_X86_64_GOTPC32_TLSDESC:
3510b57cec5SDimitry Andric     return R_TLSDESC_PC;
3520b57cec5SDimitry Andric   case R_X86_64_GOTPCREL:
3530b57cec5SDimitry Andric   case R_X86_64_GOTPCRELX:
3540b57cec5SDimitry Andric   case R_X86_64_REX_GOTPCRELX:
3550b57cec5SDimitry Andric   case R_X86_64_GOTTPOFF:
3560b57cec5SDimitry Andric     return R_GOT_PC;
3570b57cec5SDimitry Andric   case R_X86_64_GOTOFF64:
3580b57cec5SDimitry Andric     return R_GOTPLTREL;
359*349cc55cSDimitry Andric   case R_X86_64_PLTOFF64:
360*349cc55cSDimitry Andric     return R_PLT_GOTPLT;
3610b57cec5SDimitry Andric   case R_X86_64_GOTPC32:
3620b57cec5SDimitry Andric   case R_X86_64_GOTPC64:
3630b57cec5SDimitry Andric     return R_GOTPLTONLY_PC;
3640b57cec5SDimitry Andric   case R_X86_64_NONE:
3650b57cec5SDimitry Andric     return R_NONE;
3660b57cec5SDimitry Andric   default:
3670b57cec5SDimitry Andric     error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
3680b57cec5SDimitry Andric           ") against symbol " + toString(s));
3690b57cec5SDimitry Andric     return R_NONE;
3700b57cec5SDimitry Andric   }
3710b57cec5SDimitry Andric }
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric void X86_64::writeGotPltHeader(uint8_t *buf) const {
3740b57cec5SDimitry Andric   // The first entry holds the value of _DYNAMIC. It is not clear why that is
3750b57cec5SDimitry Andric   // required, but it is documented in the psabi and the glibc dynamic linker
3760b57cec5SDimitry Andric   // seems to use it (note that this is relevant for linking ld.so, not any
3770b57cec5SDimitry Andric   // other program).
3780b57cec5SDimitry Andric   write64le(buf, mainPart->dynamic->getVA());
3790b57cec5SDimitry Andric }
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric void X86_64::writeGotPlt(uint8_t *buf, const Symbol &s) const {
3820b57cec5SDimitry Andric   // See comments in X86::writeGotPlt.
3830b57cec5SDimitry Andric   write64le(buf, s.getPltVA() + 6);
3840b57cec5SDimitry Andric }
3850b57cec5SDimitry Andric 
386fe6060f1SDimitry Andric void X86_64::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
387fe6060f1SDimitry Andric   // An x86 entry is the address of the ifunc resolver function (for -z rel).
388fe6060f1SDimitry Andric   if (config->writeAddends)
389fe6060f1SDimitry Andric     write64le(buf, s.getVA());
390fe6060f1SDimitry Andric }
391fe6060f1SDimitry Andric 
3920b57cec5SDimitry Andric void X86_64::writePltHeader(uint8_t *buf) const {
3930b57cec5SDimitry Andric   const uint8_t pltData[] = {
3940b57cec5SDimitry Andric       0xff, 0x35, 0, 0, 0, 0, // pushq GOTPLT+8(%rip)
3950b57cec5SDimitry Andric       0xff, 0x25, 0, 0, 0, 0, // jmp *GOTPLT+16(%rip)
3960b57cec5SDimitry Andric       0x0f, 0x1f, 0x40, 0x00, // nop
3970b57cec5SDimitry Andric   };
3980b57cec5SDimitry Andric   memcpy(buf, pltData, sizeof(pltData));
3990b57cec5SDimitry Andric   uint64_t gotPlt = in.gotPlt->getVA();
400480093f4SDimitry Andric   uint64_t plt = in.ibtPlt ? in.ibtPlt->getVA() : in.plt->getVA();
4010b57cec5SDimitry Andric   write32le(buf + 2, gotPlt - plt + 2); // GOTPLT+8
4020b57cec5SDimitry Andric   write32le(buf + 8, gotPlt - plt + 4); // GOTPLT+16
4030b57cec5SDimitry Andric }
4040b57cec5SDimitry Andric 
405480093f4SDimitry Andric void X86_64::writePlt(uint8_t *buf, const Symbol &sym,
406480093f4SDimitry Andric                       uint64_t pltEntryAddr) const {
4070b57cec5SDimitry Andric   const uint8_t inst[] = {
4080b57cec5SDimitry Andric       0xff, 0x25, 0, 0, 0, 0, // jmpq *got(%rip)
4090b57cec5SDimitry Andric       0x68, 0, 0, 0, 0,       // pushq <relocation index>
4100b57cec5SDimitry Andric       0xe9, 0, 0, 0, 0,       // jmpq plt[0]
4110b57cec5SDimitry Andric   };
4120b57cec5SDimitry Andric   memcpy(buf, inst, sizeof(inst));
4130b57cec5SDimitry Andric 
414480093f4SDimitry Andric   write32le(buf + 2, sym.getGotPltVA() - pltEntryAddr - 6);
415480093f4SDimitry Andric   write32le(buf + 7, sym.pltIndex);
416480093f4SDimitry Andric   write32le(buf + 12, in.plt->getVA() - pltEntryAddr - 16);
4170b57cec5SDimitry Andric }
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric RelType X86_64::getDynRel(RelType type) const {
4200b57cec5SDimitry Andric   if (type == R_X86_64_64 || type == R_X86_64_PC64 || type == R_X86_64_SIZE32 ||
4210b57cec5SDimitry Andric       type == R_X86_64_SIZE64)
4220b57cec5SDimitry Andric     return type;
4230b57cec5SDimitry Andric   return R_X86_64_NONE;
4240b57cec5SDimitry Andric }
4250b57cec5SDimitry Andric 
4265ffd83dbSDimitry Andric void X86_64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
4275ffd83dbSDimitry Andric                             uint64_t val) const {
4285ffd83dbSDimitry Andric   if (rel.type == R_X86_64_TLSGD) {
4290b57cec5SDimitry Andric     // Convert
4300b57cec5SDimitry Andric     //   .byte 0x66
4310b57cec5SDimitry Andric     //   leaq x@tlsgd(%rip), %rdi
4320b57cec5SDimitry Andric     //   .word 0x6666
4330b57cec5SDimitry Andric     //   rex64
4340b57cec5SDimitry Andric     //   call __tls_get_addr@plt
4350b57cec5SDimitry Andric     // to the following two instructions.
4360b57cec5SDimitry Andric     const uint8_t inst[] = {
4370b57cec5SDimitry Andric         0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00,
4380b57cec5SDimitry Andric         0x00, 0x00,                            // mov %fs:0x0,%rax
4390b57cec5SDimitry Andric         0x48, 0x8d, 0x80, 0,    0,    0,    0, // lea x@tpoff,%rax
4400b57cec5SDimitry Andric     };
4410b57cec5SDimitry Andric     memcpy(loc - 4, inst, sizeof(inst));
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric     // The original code used a pc relative relocation and so we have to
4440b57cec5SDimitry Andric     // compensate for the -4 in had in the addend.
4450b57cec5SDimitry Andric     write32le(loc + 8, val + 4);
4460b57cec5SDimitry Andric   } else {
4470b57cec5SDimitry Andric     // Convert
4480b57cec5SDimitry Andric     //   lea x@tlsgd(%rip), %rax
4490b57cec5SDimitry Andric     //   call *(%rax)
4500b57cec5SDimitry Andric     // to the following two instructions.
4515ffd83dbSDimitry Andric     assert(rel.type == R_X86_64_GOTPC32_TLSDESC);
4520b57cec5SDimitry Andric     if (memcmp(loc - 3, "\x48\x8d\x05", 3)) {
4530b57cec5SDimitry Andric       error(getErrorLocation(loc - 3) + "R_X86_64_GOTPC32_TLSDESC must be used "
4540b57cec5SDimitry Andric                                         "in callq *x@tlsdesc(%rip), %rax");
4550b57cec5SDimitry Andric       return;
4560b57cec5SDimitry Andric     }
4570b57cec5SDimitry Andric     // movq $x@tpoff(%rip),%rax
4580b57cec5SDimitry Andric     loc[-2] = 0xc7;
4590b57cec5SDimitry Andric     loc[-1] = 0xc0;
4600b57cec5SDimitry Andric     write32le(loc, val + 4);
4610b57cec5SDimitry Andric     // xchg ax,ax
4620b57cec5SDimitry Andric     loc[4] = 0x66;
4630b57cec5SDimitry Andric     loc[5] = 0x90;
4640b57cec5SDimitry Andric   }
4650b57cec5SDimitry Andric }
4660b57cec5SDimitry Andric 
4675ffd83dbSDimitry Andric void X86_64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
4685ffd83dbSDimitry Andric                             uint64_t val) const {
4695ffd83dbSDimitry Andric   if (rel.type == R_X86_64_TLSGD) {
4700b57cec5SDimitry Andric     // Convert
4710b57cec5SDimitry Andric     //   .byte 0x66
4720b57cec5SDimitry Andric     //   leaq x@tlsgd(%rip), %rdi
4730b57cec5SDimitry Andric     //   .word 0x6666
4740b57cec5SDimitry Andric     //   rex64
4750b57cec5SDimitry Andric     //   call __tls_get_addr@plt
4760b57cec5SDimitry Andric     // to the following two instructions.
4770b57cec5SDimitry Andric     const uint8_t inst[] = {
4780b57cec5SDimitry Andric         0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00,
4790b57cec5SDimitry Andric         0x00, 0x00,                            // mov %fs:0x0,%rax
4800b57cec5SDimitry Andric         0x48, 0x03, 0x05, 0,    0,    0,    0, // addq x@gottpoff(%rip),%rax
4810b57cec5SDimitry Andric     };
4820b57cec5SDimitry Andric     memcpy(loc - 4, inst, sizeof(inst));
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric     // Both code sequences are PC relatives, but since we are moving the
4850b57cec5SDimitry Andric     // constant forward by 8 bytes we have to subtract the value by 8.
4860b57cec5SDimitry Andric     write32le(loc + 8, val - 8);
4870b57cec5SDimitry Andric   } else {
4880b57cec5SDimitry Andric     // Convert
4890b57cec5SDimitry Andric     //   lea x@tlsgd(%rip), %rax
4900b57cec5SDimitry Andric     //   call *(%rax)
4910b57cec5SDimitry Andric     // to the following two instructions.
4925ffd83dbSDimitry Andric     assert(rel.type == R_X86_64_GOTPC32_TLSDESC);
4930b57cec5SDimitry Andric     if (memcmp(loc - 3, "\x48\x8d\x05", 3)) {
4940b57cec5SDimitry Andric       error(getErrorLocation(loc - 3) + "R_X86_64_GOTPC32_TLSDESC must be used "
4950b57cec5SDimitry Andric                                         "in callq *x@tlsdesc(%rip), %rax");
4960b57cec5SDimitry Andric       return;
4970b57cec5SDimitry Andric     }
4980b57cec5SDimitry Andric     // movq x@gottpoff(%rip),%rax
4990b57cec5SDimitry Andric     loc[-2] = 0x8b;
5000b57cec5SDimitry Andric     write32le(loc, val);
5010b57cec5SDimitry Andric     // xchg ax,ax
5020b57cec5SDimitry Andric     loc[4] = 0x66;
5030b57cec5SDimitry Andric     loc[5] = 0x90;
5040b57cec5SDimitry Andric   }
5050b57cec5SDimitry Andric }
5060b57cec5SDimitry Andric 
5070b57cec5SDimitry Andric // In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
5080b57cec5SDimitry Andric // R_X86_64_TPOFF32 so that it does not use GOT.
5095ffd83dbSDimitry Andric void X86_64::relaxTlsIeToLe(uint8_t *loc, const Relocation &,
5105ffd83dbSDimitry Andric                             uint64_t val) const {
5110b57cec5SDimitry Andric   uint8_t *inst = loc - 3;
5120b57cec5SDimitry Andric   uint8_t reg = loc[-1] >> 3;
5130b57cec5SDimitry Andric   uint8_t *regSlot = loc - 1;
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric   // Note that ADD with RSP or R12 is converted to ADD instead of LEA
5160b57cec5SDimitry Andric   // because LEA with these registers needs 4 bytes to encode and thus
5170b57cec5SDimitry Andric   // wouldn't fit the space.
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric   if (memcmp(inst, "\x48\x03\x25", 3) == 0) {
5200b57cec5SDimitry Andric     // "addq foo@gottpoff(%rip),%rsp" -> "addq $foo,%rsp"
5210b57cec5SDimitry Andric     memcpy(inst, "\x48\x81\xc4", 3);
5220b57cec5SDimitry Andric   } else if (memcmp(inst, "\x4c\x03\x25", 3) == 0) {
5230b57cec5SDimitry Andric     // "addq foo@gottpoff(%rip),%r12" -> "addq $foo,%r12"
5240b57cec5SDimitry Andric     memcpy(inst, "\x49\x81\xc4", 3);
5250b57cec5SDimitry Andric   } else if (memcmp(inst, "\x4c\x03", 2) == 0) {
5260b57cec5SDimitry Andric     // "addq foo@gottpoff(%rip),%r[8-15]" -> "leaq foo(%r[8-15]),%r[8-15]"
5270b57cec5SDimitry Andric     memcpy(inst, "\x4d\x8d", 2);
5280b57cec5SDimitry Andric     *regSlot = 0x80 | (reg << 3) | reg;
5290b57cec5SDimitry Andric   } else if (memcmp(inst, "\x48\x03", 2) == 0) {
5300b57cec5SDimitry Andric     // "addq foo@gottpoff(%rip),%reg -> "leaq foo(%reg),%reg"
5310b57cec5SDimitry Andric     memcpy(inst, "\x48\x8d", 2);
5320b57cec5SDimitry Andric     *regSlot = 0x80 | (reg << 3) | reg;
5330b57cec5SDimitry Andric   } else if (memcmp(inst, "\x4c\x8b", 2) == 0) {
5340b57cec5SDimitry Andric     // "movq foo@gottpoff(%rip),%r[8-15]" -> "movq $foo,%r[8-15]"
5350b57cec5SDimitry Andric     memcpy(inst, "\x49\xc7", 2);
5360b57cec5SDimitry Andric     *regSlot = 0xc0 | reg;
5370b57cec5SDimitry Andric   } else if (memcmp(inst, "\x48\x8b", 2) == 0) {
5380b57cec5SDimitry Andric     // "movq foo@gottpoff(%rip),%reg" -> "movq $foo,%reg"
5390b57cec5SDimitry Andric     memcpy(inst, "\x48\xc7", 2);
5400b57cec5SDimitry Andric     *regSlot = 0xc0 | reg;
5410b57cec5SDimitry Andric   } else {
5420b57cec5SDimitry Andric     error(getErrorLocation(loc - 3) +
5430b57cec5SDimitry Andric           "R_X86_64_GOTTPOFF must be used in MOVQ or ADDQ instructions only");
5440b57cec5SDimitry Andric   }
5450b57cec5SDimitry Andric 
5460b57cec5SDimitry Andric   // The original code used a PC relative relocation.
5470b57cec5SDimitry Andric   // Need to compensate for the -4 it had in the addend.
5480b57cec5SDimitry Andric   write32le(loc, val + 4);
5490b57cec5SDimitry Andric }
5500b57cec5SDimitry Andric 
5515ffd83dbSDimitry Andric void X86_64::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,
5525ffd83dbSDimitry Andric                             uint64_t val) const {
5535ffd83dbSDimitry Andric   if (rel.type == R_X86_64_DTPOFF64) {
5540b57cec5SDimitry Andric     write64le(loc, val);
5550b57cec5SDimitry Andric     return;
5560b57cec5SDimitry Andric   }
5575ffd83dbSDimitry Andric   if (rel.type == R_X86_64_DTPOFF32) {
5580b57cec5SDimitry Andric     write32le(loc, val);
5590b57cec5SDimitry Andric     return;
5600b57cec5SDimitry Andric   }
5610b57cec5SDimitry Andric 
5620b57cec5SDimitry Andric   const uint8_t inst[] = {
5630b57cec5SDimitry Andric       0x66, 0x66,                                           // .word 0x6666
5640b57cec5SDimitry Andric       0x66,                                                 // .byte 0x66
5650b57cec5SDimitry Andric       0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0,%rax
5660b57cec5SDimitry Andric   };
5670b57cec5SDimitry Andric 
5680b57cec5SDimitry Andric   if (loc[4] == 0xe8) {
5690b57cec5SDimitry Andric     // Convert
5700b57cec5SDimitry Andric     //   leaq bar@tlsld(%rip), %rdi           # 48 8d 3d <Loc>
5710b57cec5SDimitry Andric     //   callq __tls_get_addr@PLT             # e8 <disp32>
5720b57cec5SDimitry Andric     //   leaq bar@dtpoff(%rax), %rcx
5730b57cec5SDimitry Andric     // to
5740b57cec5SDimitry Andric     //   .word 0x6666
5750b57cec5SDimitry Andric     //   .byte 0x66
5760b57cec5SDimitry Andric     //   mov %fs:0,%rax
5770b57cec5SDimitry Andric     //   leaq bar@tpoff(%rax), %rcx
5780b57cec5SDimitry Andric     memcpy(loc - 3, inst, sizeof(inst));
5790b57cec5SDimitry Andric     return;
5800b57cec5SDimitry Andric   }
5810b57cec5SDimitry Andric 
5820b57cec5SDimitry Andric   if (loc[4] == 0xff && loc[5] == 0x15) {
5830b57cec5SDimitry Andric     // Convert
5840b57cec5SDimitry Andric     //   leaq  x@tlsld(%rip),%rdi               # 48 8d 3d <Loc>
5850b57cec5SDimitry Andric     //   call *__tls_get_addr@GOTPCREL(%rip)    # ff 15 <disp32>
5860b57cec5SDimitry Andric     // to
5870b57cec5SDimitry Andric     //   .long  0x66666666
5880b57cec5SDimitry Andric     //   movq   %fs:0,%rax
5890b57cec5SDimitry Andric     // See "Table 11.9: LD -> LE Code Transition (LP64)" in
5900b57cec5SDimitry Andric     // https://raw.githubusercontent.com/wiki/hjl-tools/x86-psABI/x86-64-psABI-1.0.pdf
5910b57cec5SDimitry Andric     loc[-3] = 0x66;
5920b57cec5SDimitry Andric     memcpy(loc - 2, inst, sizeof(inst));
5930b57cec5SDimitry Andric     return;
5940b57cec5SDimitry Andric   }
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric   error(getErrorLocation(loc - 3) +
5970b57cec5SDimitry Andric         "expected R_X86_64_PLT32 or R_X86_64_GOTPCRELX after R_X86_64_TLSLD");
5980b57cec5SDimitry Andric }
5990b57cec5SDimitry Andric 
6005ffd83dbSDimitry Andric // A JumpInstrMod at a specific offset indicates that the jump instruction
6015ffd83dbSDimitry Andric // opcode at that offset must be modified.  This is specifically used to relax
6025ffd83dbSDimitry Andric // jump instructions with basic block sections.  This function looks at the
6035ffd83dbSDimitry Andric // JumpMod and effects the change.
6045ffd83dbSDimitry Andric void X86_64::applyJumpInstrMod(uint8_t *loc, JumpModType type,
6055ffd83dbSDimitry Andric                                unsigned size) const {
6060b57cec5SDimitry Andric   switch (type) {
6075ffd83dbSDimitry Andric   case J_JMP_32:
6085ffd83dbSDimitry Andric     if (size == 4)
6095ffd83dbSDimitry Andric       *loc = 0xe9;
6105ffd83dbSDimitry Andric     else
6115ffd83dbSDimitry Andric       *loc = 0xeb;
6125ffd83dbSDimitry Andric     break;
6135ffd83dbSDimitry Andric   case J_JE_32:
6145ffd83dbSDimitry Andric     if (size == 4) {
6155ffd83dbSDimitry Andric       loc[-1] = 0x0f;
6165ffd83dbSDimitry Andric       *loc = 0x84;
6175ffd83dbSDimitry Andric     } else
6185ffd83dbSDimitry Andric       *loc = 0x74;
6195ffd83dbSDimitry Andric     break;
6205ffd83dbSDimitry Andric   case J_JNE_32:
6215ffd83dbSDimitry Andric     if (size == 4) {
6225ffd83dbSDimitry Andric       loc[-1] = 0x0f;
6235ffd83dbSDimitry Andric       *loc = 0x85;
6245ffd83dbSDimitry Andric     } else
6255ffd83dbSDimitry Andric       *loc = 0x75;
6265ffd83dbSDimitry Andric     break;
6275ffd83dbSDimitry Andric   case J_JG_32:
6285ffd83dbSDimitry Andric     if (size == 4) {
6295ffd83dbSDimitry Andric       loc[-1] = 0x0f;
6305ffd83dbSDimitry Andric       *loc = 0x8f;
6315ffd83dbSDimitry Andric     } else
6325ffd83dbSDimitry Andric       *loc = 0x7f;
6335ffd83dbSDimitry Andric     break;
6345ffd83dbSDimitry Andric   case J_JGE_32:
6355ffd83dbSDimitry Andric     if (size == 4) {
6365ffd83dbSDimitry Andric       loc[-1] = 0x0f;
6375ffd83dbSDimitry Andric       *loc = 0x8d;
6385ffd83dbSDimitry Andric     } else
6395ffd83dbSDimitry Andric       *loc = 0x7d;
6405ffd83dbSDimitry Andric     break;
6415ffd83dbSDimitry Andric   case J_JB_32:
6425ffd83dbSDimitry Andric     if (size == 4) {
6435ffd83dbSDimitry Andric       loc[-1] = 0x0f;
6445ffd83dbSDimitry Andric       *loc = 0x82;
6455ffd83dbSDimitry Andric     } else
6465ffd83dbSDimitry Andric       *loc = 0x72;
6475ffd83dbSDimitry Andric     break;
6485ffd83dbSDimitry Andric   case J_JBE_32:
6495ffd83dbSDimitry Andric     if (size == 4) {
6505ffd83dbSDimitry Andric       loc[-1] = 0x0f;
6515ffd83dbSDimitry Andric       *loc = 0x86;
6525ffd83dbSDimitry Andric     } else
6535ffd83dbSDimitry Andric       *loc = 0x76;
6545ffd83dbSDimitry Andric     break;
6555ffd83dbSDimitry Andric   case J_JL_32:
6565ffd83dbSDimitry Andric     if (size == 4) {
6575ffd83dbSDimitry Andric       loc[-1] = 0x0f;
6585ffd83dbSDimitry Andric       *loc = 0x8c;
6595ffd83dbSDimitry Andric     } else
6605ffd83dbSDimitry Andric       *loc = 0x7c;
6615ffd83dbSDimitry Andric     break;
6625ffd83dbSDimitry Andric   case J_JLE_32:
6635ffd83dbSDimitry Andric     if (size == 4) {
6645ffd83dbSDimitry Andric       loc[-1] = 0x0f;
6655ffd83dbSDimitry Andric       *loc = 0x8e;
6665ffd83dbSDimitry Andric     } else
6675ffd83dbSDimitry Andric       *loc = 0x7e;
6685ffd83dbSDimitry Andric     break;
6695ffd83dbSDimitry Andric   case J_JA_32:
6705ffd83dbSDimitry Andric     if (size == 4) {
6715ffd83dbSDimitry Andric       loc[-1] = 0x0f;
6725ffd83dbSDimitry Andric       *loc = 0x87;
6735ffd83dbSDimitry Andric     } else
6745ffd83dbSDimitry Andric       *loc = 0x77;
6755ffd83dbSDimitry Andric     break;
6765ffd83dbSDimitry Andric   case J_JAE_32:
6775ffd83dbSDimitry Andric     if (size == 4) {
6785ffd83dbSDimitry Andric       loc[-1] = 0x0f;
6795ffd83dbSDimitry Andric       *loc = 0x83;
6805ffd83dbSDimitry Andric     } else
6815ffd83dbSDimitry Andric       *loc = 0x73;
6825ffd83dbSDimitry Andric     break;
6835ffd83dbSDimitry Andric   case J_UNKNOWN:
6845ffd83dbSDimitry Andric     llvm_unreachable("Unknown Jump Relocation");
6855ffd83dbSDimitry Andric   }
6865ffd83dbSDimitry Andric }
6875ffd83dbSDimitry Andric 
688fe6060f1SDimitry Andric int64_t X86_64::getImplicitAddend(const uint8_t *buf, RelType type) const {
689fe6060f1SDimitry Andric   switch (type) {
690fe6060f1SDimitry Andric   case R_X86_64_8:
691fe6060f1SDimitry Andric   case R_X86_64_PC8:
692fe6060f1SDimitry Andric     return SignExtend64<8>(*buf);
693fe6060f1SDimitry Andric   case R_X86_64_16:
694fe6060f1SDimitry Andric   case R_X86_64_PC16:
695fe6060f1SDimitry Andric     return SignExtend64<16>(read16le(buf));
696fe6060f1SDimitry Andric   case R_X86_64_32:
697fe6060f1SDimitry Andric   case R_X86_64_32S:
698fe6060f1SDimitry Andric   case R_X86_64_TPOFF32:
699fe6060f1SDimitry Andric   case R_X86_64_GOT32:
700fe6060f1SDimitry Andric   case R_X86_64_GOTPC32:
701fe6060f1SDimitry Andric   case R_X86_64_GOTPC32_TLSDESC:
702fe6060f1SDimitry Andric   case R_X86_64_GOTPCREL:
703fe6060f1SDimitry Andric   case R_X86_64_GOTPCRELX:
704fe6060f1SDimitry Andric   case R_X86_64_REX_GOTPCRELX:
705fe6060f1SDimitry Andric   case R_X86_64_PC32:
706fe6060f1SDimitry Andric   case R_X86_64_GOTTPOFF:
707fe6060f1SDimitry Andric   case R_X86_64_PLT32:
708fe6060f1SDimitry Andric   case R_X86_64_TLSGD:
709fe6060f1SDimitry Andric   case R_X86_64_TLSLD:
710fe6060f1SDimitry Andric   case R_X86_64_DTPOFF32:
711fe6060f1SDimitry Andric   case R_X86_64_SIZE32:
712fe6060f1SDimitry Andric     return SignExtend64<32>(read32le(buf));
713fe6060f1SDimitry Andric   case R_X86_64_64:
714fe6060f1SDimitry Andric   case R_X86_64_TPOFF64:
715fe6060f1SDimitry Andric   case R_X86_64_DTPOFF64:
716fe6060f1SDimitry Andric   case R_X86_64_DTPMOD64:
717fe6060f1SDimitry Andric   case R_X86_64_PC64:
718fe6060f1SDimitry Andric   case R_X86_64_SIZE64:
719fe6060f1SDimitry Andric   case R_X86_64_GLOB_DAT:
720fe6060f1SDimitry Andric   case R_X86_64_GOT64:
721fe6060f1SDimitry Andric   case R_X86_64_GOTOFF64:
722fe6060f1SDimitry Andric   case R_X86_64_GOTPC64:
723*349cc55cSDimitry Andric   case R_X86_64_PLTOFF64:
724fe6060f1SDimitry Andric   case R_X86_64_IRELATIVE:
725fe6060f1SDimitry Andric   case R_X86_64_RELATIVE:
726fe6060f1SDimitry Andric     return read64le(buf);
727*349cc55cSDimitry Andric   case R_X86_64_TLSDESC:
728*349cc55cSDimitry Andric     return read64le(buf + 8);
729fe6060f1SDimitry Andric   case R_X86_64_JUMP_SLOT:
730fe6060f1SDimitry Andric   case R_X86_64_NONE:
731fe6060f1SDimitry Andric     // These relocations are defined as not having an implicit addend.
732fe6060f1SDimitry Andric     return 0;
733fe6060f1SDimitry Andric   default:
734fe6060f1SDimitry Andric     internalLinkerError(getErrorLocation(buf),
735fe6060f1SDimitry Andric                         "cannot read addend for relocation " + toString(type));
736fe6060f1SDimitry Andric     return 0;
737fe6060f1SDimitry Andric   }
738fe6060f1SDimitry Andric }
739fe6060f1SDimitry Andric 
7405ffd83dbSDimitry Andric void X86_64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
7415ffd83dbSDimitry Andric   switch (rel.type) {
7420b57cec5SDimitry Andric   case R_X86_64_8:
7435ffd83dbSDimitry Andric     checkIntUInt(loc, val, 8, rel);
7440b57cec5SDimitry Andric     *loc = val;
7450b57cec5SDimitry Andric     break;
7460b57cec5SDimitry Andric   case R_X86_64_PC8:
7475ffd83dbSDimitry Andric     checkInt(loc, val, 8, rel);
7480b57cec5SDimitry Andric     *loc = val;
7490b57cec5SDimitry Andric     break;
7500b57cec5SDimitry Andric   case R_X86_64_16:
7515ffd83dbSDimitry Andric     checkIntUInt(loc, val, 16, rel);
7520b57cec5SDimitry Andric     write16le(loc, val);
7530b57cec5SDimitry Andric     break;
7540b57cec5SDimitry Andric   case R_X86_64_PC16:
7555ffd83dbSDimitry Andric     checkInt(loc, val, 16, rel);
7560b57cec5SDimitry Andric     write16le(loc, val);
7570b57cec5SDimitry Andric     break;
7580b57cec5SDimitry Andric   case R_X86_64_32:
7595ffd83dbSDimitry Andric     checkUInt(loc, val, 32, rel);
7600b57cec5SDimitry Andric     write32le(loc, val);
7610b57cec5SDimitry Andric     break;
7620b57cec5SDimitry Andric   case R_X86_64_32S:
7630b57cec5SDimitry Andric   case R_X86_64_TPOFF32:
7640b57cec5SDimitry Andric   case R_X86_64_GOT32:
7650b57cec5SDimitry Andric   case R_X86_64_GOTPC32:
7660b57cec5SDimitry Andric   case R_X86_64_GOTPC32_TLSDESC:
7670b57cec5SDimitry Andric   case R_X86_64_GOTPCREL:
7680b57cec5SDimitry Andric   case R_X86_64_GOTPCRELX:
7690b57cec5SDimitry Andric   case R_X86_64_REX_GOTPCRELX:
7700b57cec5SDimitry Andric   case R_X86_64_PC32:
7710b57cec5SDimitry Andric   case R_X86_64_GOTTPOFF:
7720b57cec5SDimitry Andric   case R_X86_64_PLT32:
7730b57cec5SDimitry Andric   case R_X86_64_TLSGD:
7740b57cec5SDimitry Andric   case R_X86_64_TLSLD:
7750b57cec5SDimitry Andric   case R_X86_64_DTPOFF32:
7760b57cec5SDimitry Andric   case R_X86_64_SIZE32:
7775ffd83dbSDimitry Andric     checkInt(loc, val, 32, rel);
7780b57cec5SDimitry Andric     write32le(loc, val);
7790b57cec5SDimitry Andric     break;
7800b57cec5SDimitry Andric   case R_X86_64_64:
7810b57cec5SDimitry Andric   case R_X86_64_DTPOFF64:
7820b57cec5SDimitry Andric   case R_X86_64_PC64:
7830b57cec5SDimitry Andric   case R_X86_64_SIZE64:
7840b57cec5SDimitry Andric   case R_X86_64_GOT64:
7850b57cec5SDimitry Andric   case R_X86_64_GOTOFF64:
7860b57cec5SDimitry Andric   case R_X86_64_GOTPC64:
787*349cc55cSDimitry Andric   case R_X86_64_PLTOFF64:
7880b57cec5SDimitry Andric     write64le(loc, val);
7890b57cec5SDimitry Andric     break;
790*349cc55cSDimitry Andric   case R_X86_64_TLSDESC:
791*349cc55cSDimitry Andric     // The addend is stored in the second 64-bit word.
792*349cc55cSDimitry Andric     write64le(loc + 8, val);
793*349cc55cSDimitry Andric     break;
7940b57cec5SDimitry Andric   default:
7950b57cec5SDimitry Andric     llvm_unreachable("unknown relocation");
7960b57cec5SDimitry Andric   }
7970b57cec5SDimitry Andric }
7980b57cec5SDimitry Andric 
799e8d8bef9SDimitry Andric RelExpr X86_64::adjustGotPcExpr(RelType type, int64_t addend,
800e8d8bef9SDimitry Andric                                 const uint8_t *loc) const {
801e8d8bef9SDimitry Andric   // Only R_X86_64_[REX_]GOTPCRELX can be relaxed. GNU as may emit GOTPCRELX
802e8d8bef9SDimitry Andric   // with addend != -4. Such an instruction does not load the full GOT entry, so
803e8d8bef9SDimitry Andric   // we cannot relax the relocation. E.g. movl x@GOTPCREL+4(%rip), %rax
804e8d8bef9SDimitry Andric   // (addend=0) loads the high 32 bits of the GOT entry.
805*349cc55cSDimitry Andric   if (!config->relax || addend != -4 ||
806*349cc55cSDimitry Andric       (type != R_X86_64_GOTPCRELX && type != R_X86_64_REX_GOTPCRELX))
807e8d8bef9SDimitry Andric     return R_GOT_PC;
808e8d8bef9SDimitry Andric   const uint8_t op = loc[-2];
809e8d8bef9SDimitry Andric   const uint8_t modRm = loc[-1];
8100b57cec5SDimitry Andric 
8110b57cec5SDimitry Andric   // FIXME: When PIC is disabled and foo is defined locally in the
8120b57cec5SDimitry Andric   // lower 32 bit address space, memory operand in mov can be converted into
8130b57cec5SDimitry Andric   // immediate operand. Otherwise, mov must be changed to lea. We support only
8140b57cec5SDimitry Andric   // latter relaxation at this moment.
8150b57cec5SDimitry Andric   if (op == 0x8b)
8160b57cec5SDimitry Andric     return R_RELAX_GOT_PC;
8170b57cec5SDimitry Andric 
8180b57cec5SDimitry Andric   // Relax call and jmp.
8190b57cec5SDimitry Andric   if (op == 0xff && (modRm == 0x15 || modRm == 0x25))
8200b57cec5SDimitry Andric     return R_RELAX_GOT_PC;
8210b57cec5SDimitry Andric 
822e8d8bef9SDimitry Andric   // We don't support test/binop instructions without a REX prefix.
823e8d8bef9SDimitry Andric   if (type == R_X86_64_GOTPCRELX)
824e8d8bef9SDimitry Andric     return R_GOT_PC;
825e8d8bef9SDimitry Andric 
8260b57cec5SDimitry Andric   // Relaxation of test, adc, add, and, cmp, or, sbb, sub, xor.
8270b57cec5SDimitry Andric   // If PIC then no relaxation is available.
828e8d8bef9SDimitry Andric   return config->isPic ? R_GOT_PC : R_RELAX_GOT_PC_NOPIC;
8290b57cec5SDimitry Andric }
8300b57cec5SDimitry Andric 
8310b57cec5SDimitry Andric // A subset of relaxations can only be applied for no-PIC. This method
8320b57cec5SDimitry Andric // handles such relaxations. Instructions encoding information was taken from:
8330b57cec5SDimitry Andric // "Intel 64 and IA-32 Architectures Software Developer's Manual V2"
8340b57cec5SDimitry Andric // (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/
8350b57cec5SDimitry Andric //    64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
8360b57cec5SDimitry Andric static void relaxGotNoPic(uint8_t *loc, uint64_t val, uint8_t op,
8370b57cec5SDimitry Andric                           uint8_t modRm) {
8380b57cec5SDimitry Andric   const uint8_t rex = loc[-3];
8390b57cec5SDimitry Andric   // Convert "test %reg, foo@GOTPCREL(%rip)" to "test $foo, %reg".
8400b57cec5SDimitry Andric   if (op == 0x85) {
8410b57cec5SDimitry Andric     // See "TEST-Logical Compare" (4-428 Vol. 2B),
8420b57cec5SDimitry Andric     // TEST r/m64, r64 uses "full" ModR / M byte (no opcode extension).
8430b57cec5SDimitry Andric 
8440b57cec5SDimitry Andric     // ModR/M byte has form XX YYY ZZZ, where
8450b57cec5SDimitry Andric     // YYY is MODRM.reg(register 2), ZZZ is MODRM.rm(register 1).
8460b57cec5SDimitry Andric     // XX has different meanings:
8470b57cec5SDimitry Andric     // 00: The operand's memory address is in reg1.
8480b57cec5SDimitry Andric     // 01: The operand's memory address is reg1 + a byte-sized displacement.
8490b57cec5SDimitry Andric     // 10: The operand's memory address is reg1 + a word-sized displacement.
8500b57cec5SDimitry Andric     // 11: The operand is reg1 itself.
8510b57cec5SDimitry Andric     // If an instruction requires only one operand, the unused reg2 field
8520b57cec5SDimitry Andric     // holds extra opcode bits rather than a register code
8530b57cec5SDimitry Andric     // 0xC0 == 11 000 000 binary.
8540b57cec5SDimitry Andric     // 0x38 == 00 111 000 binary.
8550b57cec5SDimitry Andric     // We transfer reg2 to reg1 here as operand.
8560b57cec5SDimitry Andric     // See "2.1.3 ModR/M and SIB Bytes" (Vol. 2A 2-3).
8570b57cec5SDimitry Andric     loc[-1] = 0xc0 | (modRm & 0x38) >> 3; // ModR/M byte.
8580b57cec5SDimitry Andric 
8590b57cec5SDimitry Andric     // Change opcode from TEST r/m64, r64 to TEST r/m64, imm32
8600b57cec5SDimitry Andric     // See "TEST-Logical Compare" (4-428 Vol. 2B).
8610b57cec5SDimitry Andric     loc[-2] = 0xf7;
8620b57cec5SDimitry Andric 
8630b57cec5SDimitry Andric     // Move R bit to the B bit in REX byte.
8640b57cec5SDimitry Andric     // REX byte is encoded as 0100WRXB, where
8650b57cec5SDimitry Andric     // 0100 is 4bit fixed pattern.
8660b57cec5SDimitry Andric     // REX.W When 1, a 64-bit operand size is used. Otherwise, when 0, the
8670b57cec5SDimitry Andric     //   default operand size is used (which is 32-bit for most but not all
8680b57cec5SDimitry Andric     //   instructions).
8690b57cec5SDimitry Andric     // REX.R This 1-bit value is an extension to the MODRM.reg field.
8700b57cec5SDimitry Andric     // REX.X This 1-bit value is an extension to the SIB.index field.
8710b57cec5SDimitry Andric     // REX.B This 1-bit value is an extension to the MODRM.rm field or the
8720b57cec5SDimitry Andric     // SIB.base field.
8730b57cec5SDimitry Andric     // See "2.2.1.2 More on REX Prefix Fields " (2-8 Vol. 2A).
8740b57cec5SDimitry Andric     loc[-3] = (rex & ~0x4) | (rex & 0x4) >> 2;
8750b57cec5SDimitry Andric     write32le(loc, val);
8760b57cec5SDimitry Andric     return;
8770b57cec5SDimitry Andric   }
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric   // If we are here then we need to relax the adc, add, and, cmp, or, sbb, sub
8800b57cec5SDimitry Andric   // or xor operations.
8810b57cec5SDimitry Andric 
8820b57cec5SDimitry Andric   // Convert "binop foo@GOTPCREL(%rip), %reg" to "binop $foo, %reg".
8830b57cec5SDimitry Andric   // Logic is close to one for test instruction above, but we also
8840b57cec5SDimitry Andric   // write opcode extension here, see below for details.
8850b57cec5SDimitry Andric   loc[-1] = 0xc0 | (modRm & 0x38) >> 3 | (op & 0x3c); // ModR/M byte.
8860b57cec5SDimitry Andric 
8870b57cec5SDimitry Andric   // Primary opcode is 0x81, opcode extension is one of:
8880b57cec5SDimitry Andric   // 000b = ADD, 001b is OR, 010b is ADC, 011b is SBB,
8890b57cec5SDimitry Andric   // 100b is AND, 101b is SUB, 110b is XOR, 111b is CMP.
8900b57cec5SDimitry Andric   // This value was wrote to MODRM.reg in a line above.
8910b57cec5SDimitry Andric   // See "3.2 INSTRUCTIONS (A-M)" (Vol. 2A 3-15),
8920b57cec5SDimitry Andric   // "INSTRUCTION SET REFERENCE, N-Z" (Vol. 2B 4-1) for
8930b57cec5SDimitry Andric   // descriptions about each operation.
8940b57cec5SDimitry Andric   loc[-2] = 0x81;
8950b57cec5SDimitry Andric   loc[-3] = (rex & ~0x4) | (rex & 0x4) >> 2;
8960b57cec5SDimitry Andric   write32le(loc, val);
8970b57cec5SDimitry Andric }
8980b57cec5SDimitry Andric 
899e8d8bef9SDimitry Andric void X86_64::relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) const {
900e8d8bef9SDimitry Andric   checkInt(loc, val, 32, rel);
9010b57cec5SDimitry Andric   const uint8_t op = loc[-2];
9020b57cec5SDimitry Andric   const uint8_t modRm = loc[-1];
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric   // Convert "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg".
9050b57cec5SDimitry Andric   if (op == 0x8b) {
9060b57cec5SDimitry Andric     loc[-2] = 0x8d;
9070b57cec5SDimitry Andric     write32le(loc, val);
9080b57cec5SDimitry Andric     return;
9090b57cec5SDimitry Andric   }
9100b57cec5SDimitry Andric 
9110b57cec5SDimitry Andric   if (op != 0xff) {
9120b57cec5SDimitry Andric     // We are relaxing a rip relative to an absolute, so compensate
9130b57cec5SDimitry Andric     // for the old -4 addend.
9140b57cec5SDimitry Andric     assert(!config->isPic);
9150b57cec5SDimitry Andric     relaxGotNoPic(loc, val + 4, op, modRm);
9160b57cec5SDimitry Andric     return;
9170b57cec5SDimitry Andric   }
9180b57cec5SDimitry Andric 
9190b57cec5SDimitry Andric   // Convert call/jmp instructions.
9200b57cec5SDimitry Andric   if (modRm == 0x15) {
9210b57cec5SDimitry Andric     // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call foo".
9220b57cec5SDimitry Andric     // Instead we convert to "addr32 call foo" where addr32 is an instruction
9230b57cec5SDimitry Andric     // prefix. That makes result expression to be a single instruction.
9240b57cec5SDimitry Andric     loc[-2] = 0x67; // addr32 prefix
9250b57cec5SDimitry Andric     loc[-1] = 0xe8; // call
9260b57cec5SDimitry Andric     write32le(loc, val);
9270b57cec5SDimitry Andric     return;
9280b57cec5SDimitry Andric   }
9290b57cec5SDimitry Andric 
9300b57cec5SDimitry Andric   // Convert "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop".
9310b57cec5SDimitry Andric   // jmp doesn't return, so it is fine to use nop here, it is just a stub.
9320b57cec5SDimitry Andric   assert(modRm == 0x25);
9330b57cec5SDimitry Andric   loc[-2] = 0xe9; // jmp
9340b57cec5SDimitry Andric   loc[3] = 0x90;  // nop
9350b57cec5SDimitry Andric   write32le(loc - 1, val + 1);
9360b57cec5SDimitry Andric }
9370b57cec5SDimitry Andric 
9380b57cec5SDimitry Andric // A split-stack prologue starts by checking the amount of stack remaining
9390b57cec5SDimitry Andric // in one of two ways:
9400b57cec5SDimitry Andric // A) Comparing of the stack pointer to a field in the tcb.
9410b57cec5SDimitry Andric // B) Or a load of a stack pointer offset with an lea to r10 or r11.
9420b57cec5SDimitry Andric bool X86_64::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
9430b57cec5SDimitry Andric                                               uint8_t stOther) const {
9440b57cec5SDimitry Andric   if (!config->is64) {
9450b57cec5SDimitry Andric     error("Target doesn't support split stacks.");
9460b57cec5SDimitry Andric     return false;
9470b57cec5SDimitry Andric   }
9480b57cec5SDimitry Andric 
9490b57cec5SDimitry Andric   if (loc + 8 >= end)
9500b57cec5SDimitry Andric     return false;
9510b57cec5SDimitry Andric 
9520b57cec5SDimitry Andric   // Replace "cmp %fs:0x70,%rsp" and subsequent branch
9530b57cec5SDimitry Andric   // with "stc, nopl 0x0(%rax,%rax,1)"
9540b57cec5SDimitry Andric   if (memcmp(loc, "\x64\x48\x3b\x24\x25", 5) == 0) {
9550b57cec5SDimitry Andric     memcpy(loc, "\xf9\x0f\x1f\x84\x00\x00\x00\x00", 8);
9560b57cec5SDimitry Andric     return true;
9570b57cec5SDimitry Andric   }
9580b57cec5SDimitry Andric 
9590b57cec5SDimitry Andric   // Adjust "lea X(%rsp),%rYY" to lea "(X - 0x4000)(%rsp),%rYY" where rYY could
9600b57cec5SDimitry Andric   // be r10 or r11. The lea instruction feeds a subsequent compare which checks
9610b57cec5SDimitry Andric   // if there is X available stack space. Making X larger effectively reserves
9620b57cec5SDimitry Andric   // that much additional space. The stack grows downward so subtract the value.
9630b57cec5SDimitry Andric   if (memcmp(loc, "\x4c\x8d\x94\x24", 4) == 0 ||
9640b57cec5SDimitry Andric       memcmp(loc, "\x4c\x8d\x9c\x24", 4) == 0) {
9650b57cec5SDimitry Andric     // The offset bytes are encoded four bytes after the start of the
9660b57cec5SDimitry Andric     // instruction.
9670b57cec5SDimitry Andric     write32le(loc + 4, read32le(loc + 4) - 0x4000);
9680b57cec5SDimitry Andric     return true;
9690b57cec5SDimitry Andric   }
9700b57cec5SDimitry Andric   return false;
9710b57cec5SDimitry Andric }
9720b57cec5SDimitry Andric 
973480093f4SDimitry Andric // If Intel Indirect Branch Tracking is enabled, we have to emit special PLT
974480093f4SDimitry Andric // entries containing endbr64 instructions. A PLT entry will be split into two
975480093f4SDimitry Andric // parts, one in .plt.sec (writePlt), and the other in .plt (writeIBTPlt).
976480093f4SDimitry Andric namespace {
977480093f4SDimitry Andric class IntelIBT : public X86_64 {
978480093f4SDimitry Andric public:
979480093f4SDimitry Andric   IntelIBT();
980480093f4SDimitry Andric   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
981480093f4SDimitry Andric   void writePlt(uint8_t *buf, const Symbol &sym,
982480093f4SDimitry Andric                 uint64_t pltEntryAddr) const override;
983480093f4SDimitry Andric   void writeIBTPlt(uint8_t *buf, size_t numEntries) const override;
984480093f4SDimitry Andric 
985480093f4SDimitry Andric   static const unsigned IBTPltHeaderSize = 16;
986480093f4SDimitry Andric };
987480093f4SDimitry Andric } // namespace
988480093f4SDimitry Andric 
989480093f4SDimitry Andric IntelIBT::IntelIBT() { pltHeaderSize = 0; }
990480093f4SDimitry Andric 
991480093f4SDimitry Andric void IntelIBT::writeGotPlt(uint8_t *buf, const Symbol &s) const {
992480093f4SDimitry Andric   uint64_t va =
993480093f4SDimitry Andric       in.ibtPlt->getVA() + IBTPltHeaderSize + s.pltIndex * pltEntrySize;
994480093f4SDimitry Andric   write64le(buf, va);
995480093f4SDimitry Andric }
996480093f4SDimitry Andric 
997480093f4SDimitry Andric void IntelIBT::writePlt(uint8_t *buf, const Symbol &sym,
998480093f4SDimitry Andric                         uint64_t pltEntryAddr) const {
999480093f4SDimitry Andric   const uint8_t Inst[] = {
1000480093f4SDimitry Andric       0xf3, 0x0f, 0x1e, 0xfa,       // endbr64
1001480093f4SDimitry Andric       0xff, 0x25, 0,    0,    0, 0, // jmpq *got(%rip)
1002480093f4SDimitry Andric       0x66, 0x0f, 0x1f, 0x44, 0, 0, // nop
1003480093f4SDimitry Andric   };
1004480093f4SDimitry Andric   memcpy(buf, Inst, sizeof(Inst));
1005480093f4SDimitry Andric   write32le(buf + 6, sym.getGotPltVA() - pltEntryAddr - 10);
1006480093f4SDimitry Andric }
1007480093f4SDimitry Andric 
1008480093f4SDimitry Andric void IntelIBT::writeIBTPlt(uint8_t *buf, size_t numEntries) const {
1009480093f4SDimitry Andric   writePltHeader(buf);
1010480093f4SDimitry Andric   buf += IBTPltHeaderSize;
1011480093f4SDimitry Andric 
1012480093f4SDimitry Andric   const uint8_t inst[] = {
1013480093f4SDimitry Andric       0xf3, 0x0f, 0x1e, 0xfa,    // endbr64
1014480093f4SDimitry Andric       0x68, 0,    0,    0,    0, // pushq <relocation index>
1015480093f4SDimitry Andric       0xe9, 0,    0,    0,    0, // jmpq plt[0]
1016480093f4SDimitry Andric       0x66, 0x90,                // nop
1017480093f4SDimitry Andric   };
1018480093f4SDimitry Andric 
1019480093f4SDimitry Andric   for (size_t i = 0; i < numEntries; ++i) {
1020480093f4SDimitry Andric     memcpy(buf, inst, sizeof(inst));
1021480093f4SDimitry Andric     write32le(buf + 5, i);
1022480093f4SDimitry Andric     write32le(buf + 10, -pltHeaderSize - sizeof(inst) * i - 30);
1023480093f4SDimitry Andric     buf += sizeof(inst);
1024480093f4SDimitry Andric   }
1025480093f4SDimitry Andric }
1026480093f4SDimitry Andric 
10270b57cec5SDimitry Andric // These nonstandard PLT entries are to migtigate Spectre v2 security
10280b57cec5SDimitry Andric // vulnerability. In order to mitigate Spectre v2, we want to avoid indirect
10290b57cec5SDimitry Andric // branch instructions such as `jmp *GOTPLT(%rip)`. So, in the following PLT
10300b57cec5SDimitry Andric // entries, we use a CALL followed by MOV and RET to do the same thing as an
10310b57cec5SDimitry Andric // indirect jump. That instruction sequence is so-called "retpoline".
10320b57cec5SDimitry Andric //
10330b57cec5SDimitry Andric // We have two types of retpoline PLTs as a size optimization. If `-z now`
10340b57cec5SDimitry Andric // is specified, all dynamic symbols are resolved at load-time. Thus, when
10350b57cec5SDimitry Andric // that option is given, we can omit code for symbol lazy resolution.
10360b57cec5SDimitry Andric namespace {
10370b57cec5SDimitry Andric class Retpoline : public X86_64 {
10380b57cec5SDimitry Andric public:
10390b57cec5SDimitry Andric   Retpoline();
10400b57cec5SDimitry Andric   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
10410b57cec5SDimitry Andric   void writePltHeader(uint8_t *buf) const override;
1042480093f4SDimitry Andric   void writePlt(uint8_t *buf, const Symbol &sym,
1043480093f4SDimitry Andric                 uint64_t pltEntryAddr) const override;
10440b57cec5SDimitry Andric };
10450b57cec5SDimitry Andric 
10460b57cec5SDimitry Andric class RetpolineZNow : public X86_64 {
10470b57cec5SDimitry Andric public:
10480b57cec5SDimitry Andric   RetpolineZNow();
10490b57cec5SDimitry Andric   void writeGotPlt(uint8_t *buf, const Symbol &s) const override {}
10500b57cec5SDimitry Andric   void writePltHeader(uint8_t *buf) const override;
1051480093f4SDimitry Andric   void writePlt(uint8_t *buf, const Symbol &sym,
1052480093f4SDimitry Andric                 uint64_t pltEntryAddr) const override;
10530b57cec5SDimitry Andric };
10540b57cec5SDimitry Andric } // namespace
10550b57cec5SDimitry Andric 
10560b57cec5SDimitry Andric Retpoline::Retpoline() {
10570b57cec5SDimitry Andric   pltHeaderSize = 48;
10580b57cec5SDimitry Andric   pltEntrySize = 32;
1059480093f4SDimitry Andric   ipltEntrySize = 32;
10600b57cec5SDimitry Andric }
10610b57cec5SDimitry Andric 
10620b57cec5SDimitry Andric void Retpoline::writeGotPlt(uint8_t *buf, const Symbol &s) const {
10630b57cec5SDimitry Andric   write64le(buf, s.getPltVA() + 17);
10640b57cec5SDimitry Andric }
10650b57cec5SDimitry Andric 
10660b57cec5SDimitry Andric void Retpoline::writePltHeader(uint8_t *buf) const {
10670b57cec5SDimitry Andric   const uint8_t insn[] = {
10680b57cec5SDimitry Andric       0xff, 0x35, 0,    0,    0,    0,          // 0:    pushq GOTPLT+8(%rip)
10690b57cec5SDimitry Andric       0x4c, 0x8b, 0x1d, 0,    0,    0,    0,    // 6:    mov GOTPLT+16(%rip), %r11
10700b57cec5SDimitry Andric       0xe8, 0x0e, 0x00, 0x00, 0x00,             // d:    callq next
10710b57cec5SDimitry Andric       0xf3, 0x90,                               // 12: loop: pause
10720b57cec5SDimitry Andric       0x0f, 0xae, 0xe8,                         // 14:   lfence
10730b57cec5SDimitry Andric       0xeb, 0xf9,                               // 17:   jmp loop
10740b57cec5SDimitry Andric       0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 19:   int3; .align 16
10750b57cec5SDimitry Andric       0x4c, 0x89, 0x1c, 0x24,                   // 20: next: mov %r11, (%rsp)
10760b57cec5SDimitry Andric       0xc3,                                     // 24:   ret
10770b57cec5SDimitry Andric       0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 25:   int3; padding
10780b57cec5SDimitry Andric       0xcc, 0xcc, 0xcc, 0xcc,                   // 2c:   int3; padding
10790b57cec5SDimitry Andric   };
10800b57cec5SDimitry Andric   memcpy(buf, insn, sizeof(insn));
10810b57cec5SDimitry Andric 
10820b57cec5SDimitry Andric   uint64_t gotPlt = in.gotPlt->getVA();
10830b57cec5SDimitry Andric   uint64_t plt = in.plt->getVA();
10840b57cec5SDimitry Andric   write32le(buf + 2, gotPlt - plt - 6 + 8);
10850b57cec5SDimitry Andric   write32le(buf + 9, gotPlt - plt - 13 + 16);
10860b57cec5SDimitry Andric }
10870b57cec5SDimitry Andric 
1088480093f4SDimitry Andric void Retpoline::writePlt(uint8_t *buf, const Symbol &sym,
1089480093f4SDimitry Andric                          uint64_t pltEntryAddr) const {
10900b57cec5SDimitry Andric   const uint8_t insn[] = {
10910b57cec5SDimitry Andric       0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // 0:  mov foo@GOTPLT(%rip), %r11
10920b57cec5SDimitry Andric       0xe8, 0,    0,    0,    0,    // 7:  callq plt+0x20
10930b57cec5SDimitry Andric       0xe9, 0,    0,    0,    0,    // c:  jmp plt+0x12
10940b57cec5SDimitry Andric       0x68, 0,    0,    0,    0,    // 11: pushq <relocation index>
10950b57cec5SDimitry Andric       0xe9, 0,    0,    0,    0,    // 16: jmp plt+0
10960b57cec5SDimitry Andric       0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 1b: int3; padding
10970b57cec5SDimitry Andric   };
10980b57cec5SDimitry Andric   memcpy(buf, insn, sizeof(insn));
10990b57cec5SDimitry Andric 
1100480093f4SDimitry Andric   uint64_t off = pltEntryAddr - in.plt->getVA();
11010b57cec5SDimitry Andric 
1102480093f4SDimitry Andric   write32le(buf + 3, sym.getGotPltVA() - pltEntryAddr - 7);
11030b57cec5SDimitry Andric   write32le(buf + 8, -off - 12 + 32);
11040b57cec5SDimitry Andric   write32le(buf + 13, -off - 17 + 18);
1105480093f4SDimitry Andric   write32le(buf + 18, sym.pltIndex);
11060b57cec5SDimitry Andric   write32le(buf + 23, -off - 27);
11070b57cec5SDimitry Andric }
11080b57cec5SDimitry Andric 
11090b57cec5SDimitry Andric RetpolineZNow::RetpolineZNow() {
11100b57cec5SDimitry Andric   pltHeaderSize = 32;
11110b57cec5SDimitry Andric   pltEntrySize = 16;
1112480093f4SDimitry Andric   ipltEntrySize = 16;
11130b57cec5SDimitry Andric }
11140b57cec5SDimitry Andric 
11150b57cec5SDimitry Andric void RetpolineZNow::writePltHeader(uint8_t *buf) const {
11160b57cec5SDimitry Andric   const uint8_t insn[] = {
11170b57cec5SDimitry Andric       0xe8, 0x0b, 0x00, 0x00, 0x00, // 0:    call next
11180b57cec5SDimitry Andric       0xf3, 0x90,                   // 5:  loop: pause
11190b57cec5SDimitry Andric       0x0f, 0xae, 0xe8,             // 7:    lfence
11200b57cec5SDimitry Andric       0xeb, 0xf9,                   // a:    jmp loop
11210b57cec5SDimitry Andric       0xcc, 0xcc, 0xcc, 0xcc,       // c:    int3; .align 16
11220b57cec5SDimitry Andric       0x4c, 0x89, 0x1c, 0x24,       // 10: next: mov %r11, (%rsp)
11230b57cec5SDimitry Andric       0xc3,                         // 14:   ret
11240b57cec5SDimitry Andric       0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 15:   int3; padding
11250b57cec5SDimitry Andric       0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 1a:   int3; padding
11260b57cec5SDimitry Andric       0xcc,                         // 1f:   int3; padding
11270b57cec5SDimitry Andric   };
11280b57cec5SDimitry Andric   memcpy(buf, insn, sizeof(insn));
11290b57cec5SDimitry Andric }
11300b57cec5SDimitry Andric 
1131480093f4SDimitry Andric void RetpolineZNow::writePlt(uint8_t *buf, const Symbol &sym,
1132480093f4SDimitry Andric                              uint64_t pltEntryAddr) const {
11330b57cec5SDimitry Andric   const uint8_t insn[] = {
11340b57cec5SDimitry Andric       0x4c, 0x8b, 0x1d, 0,    0, 0, 0, // mov foo@GOTPLT(%rip), %r11
11350b57cec5SDimitry Andric       0xe9, 0,    0,    0,    0,       // jmp plt+0
11360b57cec5SDimitry Andric       0xcc, 0xcc, 0xcc, 0xcc,          // int3; padding
11370b57cec5SDimitry Andric   };
11380b57cec5SDimitry Andric   memcpy(buf, insn, sizeof(insn));
11390b57cec5SDimitry Andric 
1140480093f4SDimitry Andric   write32le(buf + 3, sym.getGotPltVA() - pltEntryAddr - 7);
1141480093f4SDimitry Andric   write32le(buf + 8, in.plt->getVA() - pltEntryAddr - 12);
11420b57cec5SDimitry Andric }
11430b57cec5SDimitry Andric 
11440b57cec5SDimitry Andric static TargetInfo *getTargetInfo() {
11450b57cec5SDimitry Andric   if (config->zRetpolineplt) {
11460b57cec5SDimitry Andric     if (config->zNow) {
11470b57cec5SDimitry Andric       static RetpolineZNow t;
11480b57cec5SDimitry Andric       return &t;
11490b57cec5SDimitry Andric     }
11500b57cec5SDimitry Andric     static Retpoline t;
11510b57cec5SDimitry Andric     return &t;
11520b57cec5SDimitry Andric   }
11530b57cec5SDimitry Andric 
1154480093f4SDimitry Andric   if (config->andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT) {
1155480093f4SDimitry Andric     static IntelIBT t;
1156480093f4SDimitry Andric     return &t;
1157480093f4SDimitry Andric   }
1158480093f4SDimitry Andric 
11590b57cec5SDimitry Andric   static X86_64 t;
11600b57cec5SDimitry Andric   return &t;
11610b57cec5SDimitry Andric }
11620b57cec5SDimitry Andric 
11635ffd83dbSDimitry Andric TargetInfo *elf::getX86_64TargetInfo() { return getTargetInfo(); }
1164