#include "OutputSections.h"
#include "Relocations.h"
#include "Symbols.h"
#include "SyntheticSections.h"
#include "Target.h"
#include "TargetImpl.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/MathExtras.h"
using namespace llvm;
using namespace llvm::object;
using namespace llvm::support::endian;
using namespace llvm::ELF;
using namespace lld;
using namespace lld::elf;
namespace {
class X86_64 : public TargetInfo {
public:
X86_64(Ctx &);
int getTlsGdRelaxSkip(RelType type) const override;
RelExpr getRelExpr(RelType type, const Symbol &s,
const uint8_t *loc) const override;
RelType getDynRel(RelType type) const override;
void writeGotPltHeader(uint8_t *buf) const override;
void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
void writeIgotPlt(uint8_t *buf, const Symbol &s) const override;
void writePltHeader(uint8_t *buf) const override;
void writePlt(uint8_t *buf, const Symbol &sym,
uint64_t pltEntryAddr) const override;
void relocate(uint8_t *loc, const Relocation &rel,
uint64_t val) const override;
int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
void applyJumpInstrMod(uint8_t *loc, JumpModType type,
unsigned size) const override;
RelExpr adjustGotPcExpr(RelType type, int64_t addend,
const uint8_t *loc) const override;
void relocateAlloc(InputSection &sec, uint8_t *buf) const override;
bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
uint8_t stOther) const override;
bool deleteFallThruJmpInsn(InputSection &is, InputFile *file,
InputSection *nextIS) const override;
bool relaxOnce(int pass) const override;
void applyBranchToBranchOpt() const override;
private:
void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
};
}
static const std::vector<std::vector<uint8_t>> nopInstructions = {
{0x90},
{0x66, 0x90},
{0x0f, 0x1f, 0x00},
{0x0f, 0x1f, 0x40, 0x00},
{0x0f, 0x1f, 0x44, 0x00, 0x00},
{0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
{0x0F, 0x1F, 0x80, 0x00, 0x00, 0x00, 0x00},
{0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x66, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}};
X86_64::X86_64(Ctx &ctx) : TargetInfo(ctx) {
copyRel = R_X86_64_COPY;
gotRel = R_X86_64_GLOB_DAT;
pltRel = R_X86_64_JUMP_SLOT;
relativeRel = R_X86_64_RELATIVE;
iRelativeRel = R_X86_64_IRELATIVE;
symbolicRel = R_X86_64_64;
tlsDescRel = R_X86_64_TLSDESC;
tlsGotRel = R_X86_64_TPOFF64;
tlsModuleIndexRel = R_X86_64_DTPMOD64;
tlsOffsetRel = R_X86_64_DTPOFF64;
gotBaseSymInGotPlt = true;
gotEntrySize = 8;
pltHeaderSize = 16;
pltEntrySize = 16;
ipltEntrySize = 16;
trapInstr = {0xcc, 0xcc, 0xcc, 0xcc};
nopInstrs = nopInstructions;
defaultImageBase = 0x200000;
}
int X86_64::getTlsGdRelaxSkip(RelType type) const {
return type == R_X86_64_GOTPC32_TLSDESC ||
type == R_X86_64_CODE_4_GOTPC32_TLSDESC ||
type == R_X86_64_TLSDESC_CALL
? 1
: 2;
}
enum JmpInsnOpcode : uint32_t {
J_JMP_32,
J_JNE_32,
J_JE_32,
J_JG_32,
J_JGE_32,
J_JB_32,
J_JBE_32,
J_JL_32,
J_JLE_32,
J_JA_32,
J_JAE_32,
J_UNKNOWN,
};
static JmpInsnOpcode getJmpInsnType(const uint8_t *first,
const uint8_t *second) {
if (*second == 0xe9)
return J_JMP_32;
if (first == nullptr)
return J_UNKNOWN;
if (*first == 0x0f) {
switch (*second) {
case 0x84:
return J_JE_32;
case 0x85:
return J_JNE_32;
case 0x8f:
return J_JG_32;
case 0x8d:
return J_JGE_32;
case 0x82:
return J_JB_32;
case 0x86:
return J_JBE_32;
case 0x8c:
return J_JL_32;
case 0x8e:
return J_JLE_32;
case 0x87:
return J_JA_32;
case 0x83:
return J_JAE_32;
}
}
return J_UNKNOWN;
}
static unsigned getRelocationWithOffset(const InputSection &is,
uint64_t offset) {
unsigned size = is.relocs().size();
for (unsigned i = size - 1; i + 1 > 0; --i) {
if (is.relocs()[i].offset == offset && is.relocs()[i].expr != R_NONE)
return i;
}
return size;
}
static bool isRelocationForJmpInsn(Relocation &R) {
return R.type == R_X86_64_PLT32 || R.type == R_X86_64_PC32 ||
R.type == R_X86_64_PC8;
}
static bool isFallThruRelocation(InputSection &is, InputFile *file,
InputSection *nextIS, Relocation &r) {
if (!isRelocationForJmpInsn(r))
return false;
uint64_t addrLoc = is.getOutputSection()->addr + is.outSecOff + r.offset;
uint64_t targetOffset = is.getRelocTargetVA(is.getCtx(), r, addrLoc);
uint64_t nextSectionOffset =
nextIS->getOutputSection()->addr + nextIS->outSecOff;
return (addrLoc + 4 + targetOffset) == nextSectionOffset;
}
static JmpInsnOpcode invertJmpOpcode(const JmpInsnOpcode opcode) {
switch (opcode) {
case J_JE_32:
return J_JNE_32;
case J_JNE_32:
return J_JE_32;
case J_JG_32:
return J_JLE_32;
case J_JGE_32:
return J_JL_32;
case J_JB_32:
return J_JAE_32;
case J_JBE_32:
return J_JA_32;
case J_JL_32:
return J_JGE_32;
case J_JLE_32:
return J_JG_32;
case J_JA_32:
return J_JBE_32;
case J_JAE_32:
return J_JB_32;
default:
return J_UNKNOWN;
}
}
bool X86_64::deleteFallThruJmpInsn(InputSection &is, InputFile *file,
InputSection *nextIS) const {
const unsigned sizeOfDirectJmpInsn = 5;
if (nextIS == nullptr)
return false;
if (is.getSize() < sizeOfDirectJmpInsn)
return false;
unsigned rIndex = getRelocationWithOffset(is, is.getSize() - 4);
if (rIndex == is.relocs().size())
return false;
Relocation &r = is.relocs()[rIndex];
const uint8_t *secContents = is.content().data();
if (*(secContents + r.offset - 1) != 0xe9)
return false;
if (isFallThruRelocation(is, file, nextIS, r)) {
r.expr = R_NONE;
r.offset = 0;
is.drop_back(sizeOfDirectJmpInsn);
is.nopFiller = true;
return true;
}
const unsigned sizeOfJmpCCInsn = 6;
if (is.getSize() < sizeOfDirectJmpInsn + sizeOfJmpCCInsn)
return false;
unsigned rbIndex =
getRelocationWithOffset(is, (is.getSize() - sizeOfDirectJmpInsn - 4));
if (rbIndex == is.relocs().size())
return false;
Relocation &rB = is.relocs()[rbIndex];
const uint8_t *jmpInsnB = secContents + rB.offset - 1;
JmpInsnOpcode jmpOpcodeB = getJmpInsnType(jmpInsnB - 1, jmpInsnB);
if (jmpOpcodeB == J_UNKNOWN)
return false;
if (!isFallThruRelocation(is, file, nextIS, rB))
return false;
JmpInsnOpcode jInvert = invertJmpOpcode(jmpOpcodeB);
if (jInvert == J_UNKNOWN)
return false;
is.jumpInstrMod = make<JumpInstrMod>();
*is.jumpInstrMod = {rB.offset - 1, jInvert, 4};
rB = {r.expr, r.type, rB.offset, r.addend, r.sym};
r.expr = R_NONE;
r.offset = 0;
is.drop_back(sizeOfDirectJmpInsn);
is.nopFiller = true;
return true;
}
bool X86_64::relaxOnce(int pass) const {
uint64_t minVA = UINT64_MAX, maxVA = 0;
for (OutputSection *osec : ctx.outputSections) {
if (!(osec->flags & SHF_ALLOC))
continue;
minVA = std::min(minVA, osec->addr);
maxVA = std::max(maxVA, osec->addr + osec->size);
}
if (isUInt<31>(maxVA) || (isUInt<31>(maxVA - minVA) && ctx.arg.isPic))
return false;
SmallVector<InputSection *, 0> storage;
bool changed = false;
for (OutputSection *osec : ctx.outputSections) {
if (!(osec->flags & SHF_EXECINSTR))
continue;
for (InputSection *sec : getInputSections(*osec, storage)) {
for (Relocation &rel : sec->relocs()) {
if (rel.expr != R_RELAX_GOT_PC && rel.expr != R_RELAX_GOT_PC_NOPIC)
continue;
assert(rel.addend == -4);
Relocation rel1 = rel;
rel1.addend = rel.expr == R_RELAX_GOT_PC_NOPIC ? 0 : -4;
uint64_t v = sec->getRelocTargetVA(ctx, rel1,
sec->getOutputSection()->addr +
sec->outSecOff + rel.offset);
if (isInt<32>(v))
continue;
if (rel.sym->auxIdx == 0) {
rel.sym->allocateAux(ctx);
addGotEntry(ctx, *rel.sym);
changed = true;
}
rel.expr = R_GOT_PC;
}
}
}
return changed;
}
RelExpr X86_64::getRelExpr(RelType type, const Symbol &s,
const uint8_t *loc) const {
switch (type) {
case R_X86_64_8:
case R_X86_64_16:
case R_X86_64_32:
case R_X86_64_32S:
case R_X86_64_64:
return R_ABS;
case R_X86_64_DTPOFF32:
case R_X86_64_DTPOFF64:
return R_DTPREL;
case R_X86_64_TPOFF32:
case R_X86_64_TPOFF64:
return R_TPREL;
case R_X86_64_TLSDESC_CALL:
return R_TLSDESC_CALL;
case R_X86_64_TLSLD:
return R_TLSLD_PC;
case R_X86_64_TLSGD:
return R_TLSGD_PC;
case R_X86_64_SIZE32:
case R_X86_64_SIZE64:
return R_SIZE;
case R_X86_64_PLT32:
return R_PLT_PC;
case R_X86_64_PC8:
case R_X86_64_PC16:
case R_X86_64_PC32:
case R_X86_64_PC64:
return R_PC;
case R_X86_64_GOT32:
case R_X86_64_GOT64:
return R_GOTPLT;
case R_X86_64_GOTPC32_TLSDESC:
case R_X86_64_CODE_4_GOTPC32_TLSDESC:
return R_TLSDESC_PC;
case R_X86_64_GOTPCREL:
case R_X86_64_GOTPCRELX:
case R_X86_64_REX_GOTPCRELX:
case R_X86_64_CODE_4_GOTPCRELX:
case R_X86_64_GOTTPOFF:
case R_X86_64_CODE_4_GOTTPOFF:
case R_X86_64_CODE_6_GOTTPOFF:
return R_GOT_PC;
case R_X86_64_GOTOFF64:
return R_GOTPLTREL;
case R_X86_64_PLTOFF64:
return R_PLT_GOTPLT;
case R_X86_64_GOTPC32:
case R_X86_64_GOTPC64:
return R_GOTPLTONLY_PC;
case R_X86_64_NONE:
return R_NONE;
default:
Err(ctx) << getErrorLoc(ctx, loc) << "unknown relocation (" << type.v
<< ") against symbol " << &s;
return R_NONE;
}
}
void X86_64::writeGotPltHeader(uint8_t *buf) const {
write64le(buf, ctx.mainPart->dynamic->getVA());
}
void X86_64::writeGotPlt(uint8_t *buf, const Symbol &s) const {
write64le(buf, s.getPltVA(ctx) + 6);
}
void X86_64::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
if (ctx.arg.writeAddends)
write64le(buf, s.getVA(ctx));
}
void X86_64::writePltHeader(uint8_t *buf) const {
const uint8_t pltData[] = {
0xff, 0x35, 0, 0, 0, 0,
0xff, 0x25, 0, 0, 0, 0,
0x0f, 0x1f, 0x40, 0x00,
};
memcpy(buf, pltData, sizeof(pltData));
uint64_t gotPlt = ctx.in.gotPlt->getVA();
uint64_t plt = ctx.in.ibtPlt ? ctx.in.ibtPlt->getVA() : ctx.in.plt->getVA();
write32le(buf + 2, gotPlt - plt + 2);
write32le(buf + 8, gotPlt - plt + 4);
}
void X86_64::writePlt(uint8_t *buf, const Symbol &sym,
uint64_t pltEntryAddr) const {
const uint8_t inst[] = {
0xff, 0x25, 0, 0, 0, 0,
0x68, 0, 0, 0, 0,
0xe9, 0, 0, 0, 0,
};
memcpy(buf, inst, sizeof(inst));
write32le(buf + 2, sym.getGotPltVA(ctx) - pltEntryAddr - 6);
write32le(buf + 7, sym.getPltIdx(ctx));
write32le(buf + 12, ctx.in.plt->getVA() - pltEntryAddr - 16);
}
RelType X86_64::getDynRel(RelType type) const {
if (type == R_X86_64_64 || type == R_X86_64_PC64 || type == R_X86_64_SIZE32 ||
type == R_X86_64_SIZE64)
return type;
return R_X86_64_NONE;
}
void X86_64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
uint64_t val) const {
if (rel.type == R_X86_64_TLSGD) {
const uint8_t inst[] = {
0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00,
0x00, 0x00,
0x48, 0x8d, 0x80, 0, 0, 0, 0,
};
memcpy(loc - 4, inst, sizeof(inst));
write32le(loc + 8, val + 4);
} else if (rel.type == R_X86_64_GOTPC32_TLSDESC ||
rel.type == R_X86_64_CODE_4_GOTPC32_TLSDESC) {
if ((loc[-3] & 0xfb) != 0x48 || loc[-2] != 0x8d ||
(loc[-1] & 0xc7) != 0x05) {
Err(ctx) << getErrorLoc(ctx, (rel.type == R_X86_64_GOTPC32_TLSDESC)
? loc - 3
: loc - 4)
<< "R_X86_64_GOTPC32_TLSDESC/R_X86_64_CODE_4_GOTPC32_TLSDESC "
"must be used in leaq x@tlsdesc(%rip), %REG";
return;
}
if (rel.type == R_X86_64_GOTPC32_TLSDESC) {
loc[-3] = 0x48 | ((loc[-3] >> 2) & 1);
} else {
loc[-3] = (loc[-3] & ~0x44) | ((loc[-3] & 0x44) >> 2);
}
loc[-2] = 0xc7;
loc[-1] = 0xc0 | ((loc[-1] >> 3) & 7);
write32le(loc, val + 4);
} else {
assert(rel.type == R_X86_64_TLSDESC_CALL);
loc[0] = 0x66;
loc[1] = 0x90;
}
}
void X86_64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
uint64_t val) const {
if (rel.type == R_X86_64_TLSGD) {
const uint8_t inst[] = {
0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00,
0x00, 0x00,
0x48, 0x03, 0x05, 0, 0, 0, 0,
};
memcpy(loc - 4, inst, sizeof(inst));
write32le(loc + 8, val - 8);
} else if (rel.type == R_X86_64_GOTPC32_TLSDESC ||
rel.type == R_X86_64_CODE_4_GOTPC32_TLSDESC) {
if ((loc[-3] & 0xfb) != 0x48 || loc[-2] != 0x8d ||
(loc[-1] & 0xc7) != 0x05) {
Err(ctx) << getErrorLoc(ctx, (rel.type == R_X86_64_GOTPC32_TLSDESC)
? loc - 3
: loc - 4)
<< "R_X86_64_GOTPC32_TLSDESC/R_X86_64_CODE_4_GOTPC32_TLSDESC "
"must be used in leaq x@tlsdesc(%rip), %REG";
return;
}
loc[-2] = 0x8b;
write32le(loc, val);
} else {
assert(rel.type == R_X86_64_TLSDESC_CALL);
loc[0] = 0x66;
loc[1] = 0x90;
}
}
void X86_64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
uint64_t val) const {
uint8_t *inst = loc - 3;
uint8_t reg = loc[-1] >> 3;
uint8_t *regSlot = loc - 1;
if (rel.type == R_X86_64_GOTTPOFF) {
if (memcmp(inst, "\x48\x03\x25", 3) == 0) {
memcpy(inst, "\x48\x81\xc4", 3);
} else if (memcmp(inst, "\x4c\x03\x25", 3) == 0) {
memcpy(inst, "\x49\x81\xc4", 3);
} else if (memcmp(inst, "\x4c\x03", 2) == 0) {
memcpy(inst, "\x4d\x8d", 2);
*regSlot = 0x80 | (reg << 3) | reg;
} else if (memcmp(inst, "\x48\x03", 2) == 0) {
memcpy(inst, "\x48\x8d", 2);
*regSlot = 0x80 | (reg << 3) | reg;
} else if (memcmp(inst, "\x4c\x8b", 2) == 0) {
memcpy(inst, "\x49\xc7", 2);
*regSlot = 0xc0 | reg;
} else if (memcmp(inst, "\x48\x8b", 2) == 0) {
memcpy(inst, "\x48\xc7", 2);
*regSlot = 0xc0 | reg;
} else {
Err(ctx)
<< getErrorLoc(ctx, loc - 3)
<< "R_X86_64_GOTTPOFF must be used in MOVQ or ADDQ instructions only";
}
} else if (rel.type == R_X86_64_CODE_4_GOTTPOFF) {
if (loc[-4] != 0xd5) {
Err(ctx) << getErrorLoc(ctx, loc - 4)
<< "invalid prefix with R_X86_64_CODE_4_GOTTPOFF!";
return;
}
const uint8_t rex = loc[-3];
loc[-3] = (rex & ~0x44) | (rex & 0x44) >> 2;
*regSlot = 0xc0 | reg;
if (loc[-2] == 0x8b) {
loc[-2] = 0xc7;
} else if (loc[-2] == 0x03) {
loc[-2] = 0x81;
} else {
Err(ctx) << getErrorLoc(ctx, loc - 4)
<< "R_X86_64_CODE_4_GOTTPOFF must be used in MOVQ or ADDQ "
"instructions only";
}
} else if (rel.type == R_X86_64_CODE_6_GOTTPOFF) {
if (loc[-6] != 0x62) {
Err(ctx) << getErrorLoc(ctx, loc - 6)
<< "invalid prefix with R_X86_64_CODE_6_GOTTPOFF!";
return;
}
if (((loc[-5] & 0x47) == 0x44) && ((loc[-4] & 0x87) == 0x84) &&
((loc[-3] & 0x14) != 0) && (loc[-2] == 0x1 || loc[-2] == 0x3) &&
((loc[-1] & 0xc7) == 0x5)) {
loc[-2] = 0x81;
const uint8_t evexPayload0 = loc[-5];
if ((evexPayload0 & (1 << 7)) == 0)
loc[-5] = (evexPayload0 | (1 << 7)) & ~(1 << 5);
if ((evexPayload0 & (1 << 4)) == 0)
loc[-5] = evexPayload0 | (1 << 4) | (1 << 3);
*regSlot = 0xc0 | reg;
} else {
Err(ctx) << getErrorLoc(ctx, loc - 6)
<< "R_X86_64_CODE_6_GOTTPOFF must be used in ADDQ instructions "
"with NDD/NF/NDD+NF only";
}
} else {
llvm_unreachable("Unsupported relocation type!");
}
write32le(loc, val + 4);
}
void X86_64::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,
uint64_t val) const {
const uint8_t inst[] = {
0x66, 0x66,
0x66,
0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00,
};
if (loc[4] == 0xe8) {
memcpy(loc - 3, inst, sizeof(inst));
return;
}
if (loc[4] == 0xff && loc[5] == 0x15) {
loc[-3] = 0x66;
memcpy(loc - 2, inst, sizeof(inst));
return;
}
ErrAlways(ctx)
<< getErrorLoc(ctx, loc - 3)
<< "expected R_X86_64_PLT32 or R_X86_64_GOTPCRELX after R_X86_64_TLSLD";
}
void X86_64::applyJumpInstrMod(uint8_t *loc, JumpModType type,
unsigned size) const {
switch (type) {
case J_JMP_32:
if (size == 4)
*loc = 0xe9;
else
*loc = 0xeb;
break;
case J_JE_32:
if (size == 4) {
loc[-1] = 0x0f;
*loc = 0x84;
} else
*loc = 0x74;
break;
case J_JNE_32:
if (size == 4) {
loc[-1] = 0x0f;
*loc = 0x85;
} else
*loc = 0x75;
break;
case J_JG_32:
if (size == 4) {
loc[-1] = 0x0f;
*loc = 0x8f;
} else
*loc = 0x7f;
break;
case J_JGE_32:
if (size == 4) {
loc[-1] = 0x0f;
*loc = 0x8d;
} else
*loc = 0x7d;
break;
case J_JB_32:
if (size == 4) {
loc[-1] = 0x0f;
*loc = 0x82;
} else
*loc = 0x72;
break;
case J_JBE_32:
if (size == 4) {
loc[-1] = 0x0f;
*loc = 0x86;
} else
*loc = 0x76;
break;
case J_JL_32:
if (size == 4) {
loc[-1] = 0x0f;
*loc = 0x8c;
} else
*loc = 0x7c;
break;
case J_JLE_32:
if (size == 4) {
loc[-1] = 0x0f;
*loc = 0x8e;
} else
*loc = 0x7e;
break;
case J_JA_32:
if (size == 4) {
loc[-1] = 0x0f;
*loc = 0x87;
} else
*loc = 0x77;
break;
case J_JAE_32:
if (size == 4) {
loc[-1] = 0x0f;
*loc = 0x83;
} else
*loc = 0x73;
break;
case J_UNKNOWN:
llvm_unreachable("Unknown Jump Relocation");
}
}
int64_t X86_64::getImplicitAddend(const uint8_t *buf, RelType type) const {
switch (type) {
case R_X86_64_8:
case R_X86_64_PC8:
return SignExtend64<8>(*buf);
case R_X86_64_16:
case R_X86_64_PC16:
return SignExtend64<16>(read16le(buf));
case R_X86_64_32:
case R_X86_64_32S:
case R_X86_64_TPOFF32:
case R_X86_64_GOT32:
case R_X86_64_GOTPC32:
case R_X86_64_GOTPC32_TLSDESC:
case R_X86_64_GOTPCREL:
case R_X86_64_GOTPCRELX:
case R_X86_64_REX_GOTPCRELX:
case R_X86_64_CODE_4_GOTPCRELX:
case R_X86_64_PC32:
case R_X86_64_GOTTPOFF:
case R_X86_64_CODE_4_GOTTPOFF:
case R_X86_64_CODE_6_GOTTPOFF:
case R_X86_64_PLT32:
case R_X86_64_TLSGD:
case R_X86_64_TLSLD:
case R_X86_64_DTPOFF32:
case R_X86_64_SIZE32:
return SignExtend64<32>(read32le(buf));
case R_X86_64_64:
case R_X86_64_TPOFF64:
case R_X86_64_DTPOFF64:
case R_X86_64_DTPMOD64:
case R_X86_64_PC64:
case R_X86_64_SIZE64:
case R_X86_64_GLOB_DAT:
case R_X86_64_GOT64:
case R_X86_64_GOTOFF64:
case R_X86_64_GOTPC64:
case R_X86_64_PLTOFF64:
case R_X86_64_IRELATIVE:
case R_X86_64_RELATIVE:
return read64le(buf);
case R_X86_64_TLSDESC:
return read64le(buf + 8);
case R_X86_64_JUMP_SLOT:
case R_X86_64_NONE:
return 0;
default:
InternalErr(ctx, buf) << "cannot read addend for relocation " << type;
return 0;
}
}
static void relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val);
void X86_64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
switch (rel.type) {
case R_X86_64_8:
checkIntUInt(ctx, loc, val, 8, rel);
*loc = val;
break;
case R_X86_64_PC8:
checkInt(ctx, loc, val, 8, rel);
*loc = val;
break;
case R_X86_64_16:
checkIntUInt(ctx, loc, val, 16, rel);
write16le(loc, val);
break;
case R_X86_64_PC16:
checkInt(ctx, loc, val, 16, rel);
write16le(loc, val);
break;
case R_X86_64_32:
checkUInt(ctx, loc, val, 32, rel);
write32le(loc, val);
break;
case R_X86_64_32S:
case R_X86_64_GOT32:
case R_X86_64_GOTPC32:
case R_X86_64_GOTPCREL:
case R_X86_64_PC32:
case R_X86_64_PLT32:
case R_X86_64_DTPOFF32:
case R_X86_64_SIZE32:
checkInt(ctx, loc, val, 32, rel);
write32le(loc, val);
break;
case R_X86_64_64:
case R_X86_64_TPOFF64:
case R_X86_64_DTPOFF64:
case R_X86_64_PC64:
case R_X86_64_SIZE64:
case R_X86_64_GOT64:
case R_X86_64_GOTOFF64:
case R_X86_64_GOTPC64:
case R_X86_64_PLTOFF64:
write64le(loc, val);
break;
case R_X86_64_GOTPCRELX:
case R_X86_64_REX_GOTPCRELX:
case R_X86_64_CODE_4_GOTPCRELX:
if (rel.expr != R_GOT_PC) {
relaxGot(loc, rel, val);
} else {
checkInt(ctx, loc, val, 32, rel);
write32le(loc, val);
}
break;
case R_X86_64_GOTPC32_TLSDESC:
case R_X86_64_CODE_4_GOTPC32_TLSDESC:
case R_X86_64_TLSDESC_CALL:
case R_X86_64_TLSGD:
if (rel.expr == R_RELAX_TLS_GD_TO_LE) {
relaxTlsGdToLe(loc, rel, val);
} else if (rel.expr == R_RELAX_TLS_GD_TO_IE) {
relaxTlsGdToIe(loc, rel, val);
} else {
checkInt(ctx, loc, val, 32, rel);
write32le(loc, val);
}
break;
case R_X86_64_TLSLD:
if (rel.expr == R_RELAX_TLS_LD_TO_LE) {
relaxTlsLdToLe(loc, rel, val);
} else {
checkInt(ctx, loc, val, 32, rel);
write32le(loc, val);
}
break;
case R_X86_64_GOTTPOFF:
case R_X86_64_CODE_4_GOTTPOFF:
case R_X86_64_CODE_6_GOTTPOFF:
if (rel.expr == R_RELAX_TLS_IE_TO_LE) {
relaxTlsIeToLe(loc, rel, val);
} else {
checkInt(ctx, loc, val, 32, rel);
write32le(loc, val);
}
break;
case R_X86_64_TPOFF32:
checkInt(ctx, loc, val, 32, rel);
write32le(loc, val);
break;
case R_X86_64_TLSDESC:
write64le(loc + 8, val);
break;
default:
llvm_unreachable("unknown relocation");
}
}
RelExpr X86_64::adjustGotPcExpr(RelType type, int64_t addend,
const uint8_t *loc) const {
if (!ctx.arg.relax || addend != -4 ||
(type != R_X86_64_GOTPCRELX && type != R_X86_64_REX_GOTPCRELX &&
type != R_X86_64_CODE_4_GOTPCRELX))
return R_GOT_PC;
const uint8_t op = loc[-2];
const uint8_t modRm = loc[-1];
if (op == 0x8b)
return R_RELAX_GOT_PC;
if (op == 0xff && (modRm == 0x15 || modRm == 0x25))
return R_RELAX_GOT_PC;
if (type == R_X86_64_GOTPCRELX)
return R_GOT_PC;
return ctx.arg.isPic ? R_GOT_PC : R_RELAX_GOT_PC_NOPIC;
}
static void relaxGotNoPic(uint8_t *loc, uint64_t val, uint8_t op, uint8_t modRm,
bool isRex2) {
const uint8_t rex = loc[-3];
if (op == 0x85) {
loc[-1] = 0xc0 | (modRm & 0x38) >> 3;
loc[-2] = 0xf7;
if (isRex2)
loc[-3] = (rex & ~0x44) | (rex & 0x44) >> 2;
else
loc[-3] = (rex & ~0x4) | (rex & 0x4) >> 2;
write32le(loc, val);
return;
}
loc[-1] = 0xc0 | (modRm & 0x38) >> 3 | (op & 0x3c);
loc[-2] = 0x81;
if (isRex2)
loc[-3] = (rex & ~0x44) | (rex & 0x44) >> 2;
else
loc[-3] = (rex & ~0x4) | (rex & 0x4) >> 2;
write32le(loc, val);
}
static void relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) {
assert(isInt<32>(val) &&
"GOTPCRELX should not have been relaxed if it overflows");
const uint8_t op = loc[-2];
const uint8_t modRm = loc[-1];
if (op == 0x8b) {
loc[-2] = 0x8d;
write32le(loc, val);
return;
}
if (op != 0xff) {
assert(!rel.sym->file->ctx.arg.isPic);
relaxGotNoPic(loc, val + 4, op, modRm,
rel.type == R_X86_64_CODE_4_GOTPCRELX);
return;
}
if (modRm == 0x15) {
loc[-2] = 0x67;
loc[-1] = 0xe8;
write32le(loc, val);
return;
}
assert(modRm == 0x25);
loc[-2] = 0xe9;
loc[3] = 0x90;
write32le(loc - 1, val + 1);
}
bool X86_64::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
uint8_t stOther) const {
if (!ctx.arg.is64) {
ErrAlways(ctx) << "target doesn't support split stacks";
return false;
}
if (loc + 8 >= end)
return false;
if (memcmp(loc, "\x64\x48\x3b\x24\x25", 5) == 0) {
memcpy(loc, "\xf9\x0f\x1f\x84\x00\x00\x00\x00", 8);
return true;
}
if (memcmp(loc, "\x4c\x8d\x94\x24", 4) == 0 ||
memcmp(loc, "\x4c\x8d\x9c\x24", 4) == 0) {
write32le(loc + 4, read32le(loc + 4) - 0x4000);
return true;
}
return false;
}
void X86_64::relocateAlloc(InputSection &sec, uint8_t *buf) const {
uint64_t secAddr = sec.getOutputSection()->addr + sec.outSecOff;
for (const Relocation &rel : sec.relocs()) {
if (rel.expr == R_NONE)
continue;
uint8_t *loc = buf + rel.offset;
const uint64_t val = sec.getRelocTargetVA(ctx, rel, secAddr + rel.offset);
relocate(loc, rel, val);
}
if (sec.jumpInstrMod) {
applyJumpInstrMod(buf + sec.jumpInstrMod->offset,
sec.jumpInstrMod->original, sec.jumpInstrMod->size);
}
}
static std::optional<uint64_t> getControlTransferAddend(InputSection &is,
Relocation &r) {
if (r.type == R_X86_64_PLT32) {
if (r.sym->isSection())
return r.addend + 4;
return 0;
}
return std::nullopt;
}
static std::pair<Relocation *, uint64_t>
getBranchInfoAtTarget(InputSection &is, uint64_t offset) {
auto content = is.contentMaybeDecompress();
if (content.size() > offset && content[offset] == 0xe9) {
auto *i = llvm::partition_point(
is.relocations, [&](Relocation &r) { return r.offset < offset + 1; });
if (i != is.relocations.end() && i->offset == offset + 1 &&
(i->type == R_X86_64_PC32 || i->type == R_X86_64_PLT32)) {
return {i, i->addend + 4};
}
}
return {nullptr, 0};
}
static void redirectControlTransferRelocations(Relocation &r1,
const Relocation &r2) {
if (r1.sym->isSection())
r1.addend = r2.addend;
else
r1.addend += r2.addend + 4;
r1.expr = r2.expr;
r1.sym = r2.sym;
}
void X86_64::applyBranchToBranchOpt() const {
applyBranchToBranchOptImpl(ctx, getControlTransferAddend,
getBranchInfoAtTarget,
redirectControlTransferRelocations);
}
namespace {
class IntelIBT : public X86_64 {
public:
IntelIBT(Ctx &ctx) : X86_64(ctx) { pltHeaderSize = 0; };
void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
void writePlt(uint8_t *buf, const Symbol &sym,
uint64_t pltEntryAddr) const override;
void writeIBTPlt(uint8_t *buf, size_t numEntries) const override;
static const unsigned IBTPltHeaderSize = 16;
};
}
void IntelIBT::writeGotPlt(uint8_t *buf, const Symbol &s) const {
uint64_t va = ctx.in.ibtPlt->getVA() + IBTPltHeaderSize +
s.getPltIdx(ctx) * pltEntrySize;
write64le(buf, va);
}
void IntelIBT::writePlt(uint8_t *buf, const Symbol &sym,
uint64_t pltEntryAddr) const {
const uint8_t Inst[] = {
0xf3, 0x0f, 0x1e, 0xfa,
0xff, 0x25, 0, 0, 0, 0,
0x66, 0x0f, 0x1f, 0x44, 0, 0,
};
memcpy(buf, Inst, sizeof(Inst));
write32le(buf + 6, sym.getGotPltVA(ctx) - pltEntryAddr - 10);
}
void IntelIBT::writeIBTPlt(uint8_t *buf, size_t numEntries) const {
writePltHeader(buf);
buf += IBTPltHeaderSize;
const uint8_t inst[] = {
0xf3, 0x0f, 0x1e, 0xfa,
0x68, 0, 0, 0, 0,
0xe9, 0, 0, 0, 0,
0x66, 0x90,
};
for (size_t i = 0; i < numEntries; ++i) {
memcpy(buf, inst, sizeof(inst));
write32le(buf + 5, i);
write32le(buf + 10, -pltHeaderSize - sizeof(inst) * i - 30);
buf += sizeof(inst);
}
}
namespace {
class Retpoline : public X86_64 {
public:
Retpoline(Ctx &);
void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
void writePltHeader(uint8_t *buf) const override;
void writePlt(uint8_t *buf, const Symbol &sym,
uint64_t pltEntryAddr) const override;
};
class RetpolineZNow : public X86_64 {
public:
RetpolineZNow(Ctx &);
void writeGotPlt(uint8_t *buf, const Symbol &s) const override {}
void writePltHeader(uint8_t *buf) const override;
void writePlt(uint8_t *buf, const Symbol &sym,
uint64_t pltEntryAddr) const override;
};
}
Retpoline::Retpoline(Ctx &ctx) : X86_64(ctx) {
pltHeaderSize = 48;
pltEntrySize = 32;
ipltEntrySize = 32;
}
void Retpoline::writeGotPlt(uint8_t *buf, const Symbol &s) const {
write64le(buf, s.getPltVA(ctx) + 17);
}
void Retpoline::writePltHeader(uint8_t *buf) const {
const uint8_t insn[] = {
0xff, 0x35, 0, 0, 0, 0,
0x4c, 0x8b, 0x1d, 0, 0, 0, 0,
0xe8, 0x0e, 0x00, 0x00, 0x00,
0xf3, 0x90,
0x0f, 0xae, 0xe8,
0xeb, 0xf9,
0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
0x4c, 0x89, 0x1c, 0x24,
0xc3,
0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
0xcc, 0xcc, 0xcc, 0xcc,
};
memcpy(buf, insn, sizeof(insn));
uint64_t gotPlt = ctx.in.gotPlt->getVA();
uint64_t plt = ctx.in.plt->getVA();
write32le(buf + 2, gotPlt - plt - 6 + 8);
write32le(buf + 9, gotPlt - plt - 13 + 16);
}
void Retpoline::writePlt(uint8_t *buf, const Symbol &sym,
uint64_t pltEntryAddr) const {
const uint8_t insn[] = {
0x4c, 0x8b, 0x1d, 0, 0, 0, 0,
0xe8, 0, 0, 0, 0,
0xe9, 0, 0, 0, 0,
0x68, 0, 0, 0, 0,
0xe9, 0, 0, 0, 0,
0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
};
memcpy(buf, insn, sizeof(insn));
uint64_t off = pltEntryAddr - ctx.in.plt->getVA();
write32le(buf + 3, sym.getGotPltVA(ctx) - pltEntryAddr - 7);
write32le(buf + 8, -off - 12 + 32);
write32le(buf + 13, -off - 17 + 18);
write32le(buf + 18, sym.getPltIdx(ctx));
write32le(buf + 23, -off - 27);
}
RetpolineZNow::RetpolineZNow(Ctx &ctx) : X86_64(ctx) {
pltHeaderSize = 32;
pltEntrySize = 16;
ipltEntrySize = 16;
}
void RetpolineZNow::writePltHeader(uint8_t *buf) const {
const uint8_t insn[] = {
0xe8, 0x0b, 0x00, 0x00, 0x00,
0xf3, 0x90,
0x0f, 0xae, 0xe8,
0xeb, 0xf9,
0xcc, 0xcc, 0xcc, 0xcc,
0x4c, 0x89, 0x1c, 0x24,
0xc3,
0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
0xcc,
};
memcpy(buf, insn, sizeof(insn));
}
void RetpolineZNow::writePlt(uint8_t *buf, const Symbol &sym,
uint64_t pltEntryAddr) const {
const uint8_t insn[] = {
0x4c, 0x8b, 0x1d, 0, 0, 0, 0,
0xe9, 0, 0, 0, 0,
0xcc, 0xcc, 0xcc, 0xcc,
};
memcpy(buf, insn, sizeof(insn));
write32le(buf + 3, sym.getGotPltVA(ctx) - pltEntryAddr - 7);
write32le(buf + 8, ctx.in.plt->getVA() - pltEntryAddr - 12);
}
void elf::setX86_64TargetInfo(Ctx &ctx) {
if (ctx.arg.zRetpolineplt) {
if (ctx.arg.zNow)
ctx.target.reset(new RetpolineZNow(ctx));
else
ctx.target.reset(new Retpoline(ctx));
return;
}
if (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)
ctx.target.reset(new IntelIBT(ctx));
else
ctx.target.reset(new X86_64(ctx));
}