2015-09-23 02:19:46 +08:00
|
|
|
//===- Target.cpp ---------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Target.h"
|
2015-09-23 04:54:08 +08:00
|
|
|
#include "Error.h"
|
2015-09-23 02:19:46 +08:00
|
|
|
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2015-09-23 04:54:08 +08:00
|
|
|
#include "llvm/Object/ELF.h"
|
2015-09-23 02:19:46 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
#include "llvm/Support/ELF.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
2015-09-23 04:54:08 +08:00
|
|
|
using namespace llvm::object;
|
2015-09-24 22:16:02 +08:00
|
|
|
using namespace llvm::support::endian;
|
2015-09-23 02:19:46 +08:00
|
|
|
using namespace llvm::ELF;
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace elf2 {
|
|
|
|
|
|
|
|
std::unique_ptr<TargetInfo> Target;
|
|
|
|
|
|
|
|
TargetInfo::~TargetInfo() {}
|
|
|
|
|
2015-09-23 05:35:51 +08:00
|
|
|
X86TargetInfo::X86TargetInfo() {
|
|
|
|
PCRelReloc = R_386_PC32;
|
|
|
|
GotReloc = R_386_GLOB_DAT;
|
|
|
|
}
|
2015-09-23 02:19:46 +08:00
|
|
|
|
|
|
|
void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
|
|
|
|
uint64_t PltEntryAddr) const {
|
|
|
|
ArrayRef<uint8_t> Jmp = {0xff, 0x25}; // jmpl *val
|
|
|
|
memcpy(Buf, Jmp.data(), Jmp.size());
|
|
|
|
Buf += Jmp.size();
|
|
|
|
|
|
|
|
assert(isUInt<32>(GotEntryAddr));
|
2015-09-24 22:16:02 +08:00
|
|
|
write32le(Buf, GotEntryAddr);
|
2015-09-23 02:19:46 +08:00
|
|
|
Buf += 4;
|
|
|
|
|
|
|
|
ArrayRef<uint8_t> Nops = {0x90, 0x90};
|
|
|
|
memcpy(Buf, Nops.data(), Nops.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool X86TargetInfo::relocNeedsGot(uint32_t Type) const {
|
|
|
|
if (relocNeedsPlt(Type))
|
|
|
|
return true;
|
|
|
|
switch (Type) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case R_386_GOT32:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool X86TargetInfo::relocNeedsPlt(uint32_t Type) const {
|
|
|
|
switch (Type) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case R_386_PLT32:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:16:02 +08:00
|
|
|
static void add32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
|
|
|
|
|
2015-09-23 04:54:08 +08:00
|
|
|
void X86TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
|
|
|
|
uint64_t BaseAddr, uint64_t SymVA) const {
|
|
|
|
typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel;
|
|
|
|
auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP);
|
|
|
|
|
|
|
|
uint32_t Offset = Rel.r_offset;
|
|
|
|
uint8_t *Location = Buf + Offset;
|
|
|
|
switch (Type) {
|
|
|
|
case R_386_PC32:
|
2015-09-24 22:16:02 +08:00
|
|
|
add32le(Location, SymVA - (BaseAddr + Offset));
|
2015-09-23 04:54:08 +08:00
|
|
|
break;
|
|
|
|
case R_386_32:
|
2015-09-24 22:16:02 +08:00
|
|
|
add32le(Location, SymVA);
|
2015-09-23 04:54:08 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error(Twine("unrecognized reloc ") + Twine(Type));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 05:35:51 +08:00
|
|
|
X86_64TargetInfo::X86_64TargetInfo() {
|
|
|
|
PCRelReloc = R_X86_64_PC32;
|
|
|
|
GotReloc = R_X86_64_GLOB_DAT;
|
|
|
|
}
|
2015-09-23 02:19:46 +08:00
|
|
|
|
|
|
|
void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
|
|
|
|
uint64_t PltEntryAddr) const {
|
|
|
|
ArrayRef<uint8_t> Jmp = {0xff, 0x25}; // jmpq *val(%rip)
|
|
|
|
memcpy(Buf, Jmp.data(), Jmp.size());
|
|
|
|
Buf += Jmp.size();
|
|
|
|
|
|
|
|
uintptr_t NextPC = PltEntryAddr + 6;
|
2015-09-23 04:06:19 +08:00
|
|
|
intptr_t Delta = GotEntryAddr - NextPC;
|
2015-09-23 02:19:46 +08:00
|
|
|
assert(isInt<32>(Delta));
|
2015-09-24 22:16:02 +08:00
|
|
|
write32le(Buf, Delta);
|
2015-09-23 02:19:46 +08:00
|
|
|
Buf += 4;
|
|
|
|
|
|
|
|
ArrayRef<uint8_t> Nops = {0x90, 0x90};
|
|
|
|
memcpy(Buf, Nops.data(), Nops.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool X86_64TargetInfo::relocNeedsGot(uint32_t Type) const {
|
|
|
|
if (relocNeedsPlt(Type))
|
|
|
|
return true;
|
|
|
|
switch (Type) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case R_X86_64_GOTPCREL:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type) const {
|
|
|
|
switch (Type) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case R_X86_64_PLT32:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2015-09-23 04:54:08 +08:00
|
|
|
|
|
|
|
void X86_64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP,
|
|
|
|
uint32_t Type, uint64_t BaseAddr,
|
|
|
|
uint64_t SymVA) const {
|
|
|
|
typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
|
|
|
|
auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
|
|
|
|
|
|
|
|
uint64_t Offset = Rel.r_offset;
|
|
|
|
uint8_t *Location = Buf + Offset;
|
|
|
|
switch (Type) {
|
|
|
|
case R_X86_64_PC32:
|
2015-09-24 04:08:25 +08:00
|
|
|
case R_X86_64_GOTPCREL:
|
2015-09-24 22:16:02 +08:00
|
|
|
write32le(Location, SymVA + Rel.r_addend - (BaseAddr + Offset));
|
2015-09-23 04:54:08 +08:00
|
|
|
break;
|
|
|
|
case R_X86_64_64:
|
2015-09-24 22:16:02 +08:00
|
|
|
write64le(Location, SymVA + Rel.r_addend);
|
2015-09-23 04:54:08 +08:00
|
|
|
break;
|
|
|
|
case R_X86_64_32: {
|
|
|
|
case R_X86_64_32S:
|
|
|
|
uint64_t VA = SymVA + Rel.r_addend;
|
|
|
|
if (Type == R_X86_64_32 && !isUInt<32>(VA))
|
|
|
|
error("R_X86_64_32 out of range");
|
|
|
|
else if (!isInt<32>(VA))
|
|
|
|
error("R_X86_64_32S out of range");
|
|
|
|
|
2015-09-24 22:16:02 +08:00
|
|
|
write32le(Location, VA);
|
2015-09-23 04:54:08 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
error(Twine("unrecognized reloc ") + Twine(Type));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PPC64TargetInfo::PPC64TargetInfo() {
|
|
|
|
// PCRelReloc = FIXME
|
2015-09-23 05:35:51 +08:00
|
|
|
// GotReloc = FIXME
|
2015-09-23 04:54:08 +08:00
|
|
|
}
|
|
|
|
void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
|
|
|
|
uint64_t PltEntryAddr) const {}
|
|
|
|
bool PPC64TargetInfo::relocNeedsGot(uint32_t Type) const { return false; }
|
|
|
|
bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type) const { return false; }
|
|
|
|
void PPC64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
|
2015-09-23 05:12:55 +08:00
|
|
|
uint64_t BaseAddr, uint64_t SymVA) const {
|
|
|
|
typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela;
|
|
|
|
auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
|
|
|
|
|
|
|
|
uint64_t Offset = Rel.r_offset;
|
|
|
|
uint8_t *Location = Buf + Offset;
|
|
|
|
switch (Type) {
|
|
|
|
case R_PPC64_ADDR64:
|
2015-09-24 22:16:02 +08:00
|
|
|
write64be(Location, SymVA + Rel.r_addend);
|
2015-09-23 05:12:55 +08:00
|
|
|
break;
|
|
|
|
case R_PPC64_TOC:
|
|
|
|
// We don't create a TOC yet.
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error(Twine("unrecognized reloc ") + Twine(Type));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-09-23 05:24:52 +08:00
|
|
|
|
|
|
|
PPCTargetInfo::PPCTargetInfo() {
|
|
|
|
// PCRelReloc = FIXME
|
2015-09-23 05:35:51 +08:00
|
|
|
// GotReloc = FIXME
|
2015-09-23 05:24:52 +08:00
|
|
|
}
|
|
|
|
void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
|
|
|
|
uint64_t PltEntryAddr) const {}
|
|
|
|
bool PPCTargetInfo::relocNeedsGot(uint32_t Type) const { return false; }
|
|
|
|
bool PPCTargetInfo::relocNeedsPlt(uint32_t Type) const { return false; }
|
|
|
|
void PPCTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
|
|
|
|
uint64_t BaseAddr, uint64_t SymVA) const {}
|
|
|
|
|
|
|
|
ARMTargetInfo::ARMTargetInfo() {
|
|
|
|
// PCRelReloc = FIXME
|
2015-09-23 05:35:51 +08:00
|
|
|
// GotReloc = FIXME
|
2015-09-23 05:24:52 +08:00
|
|
|
}
|
|
|
|
void ARMTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
|
|
|
|
uint64_t PltEntryAddr) const {}
|
|
|
|
bool ARMTargetInfo::relocNeedsGot(uint32_t Type) const { return false; }
|
|
|
|
bool ARMTargetInfo::relocNeedsPlt(uint32_t Type) const { return false; }
|
|
|
|
void ARMTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
|
|
|
|
uint64_t BaseAddr, uint64_t SymVA) const {}
|
2015-09-26 08:32:04 +08:00
|
|
|
|
|
|
|
AArch64TargetInfo::AArch64TargetInfo() {
|
|
|
|
// PCRelReloc = FIXME
|
|
|
|
// GotReloc = FIXME
|
|
|
|
}
|
|
|
|
void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
|
|
|
|
uint64_t PltEntryAddr) const {}
|
|
|
|
bool AArch64TargetInfo::relocNeedsGot(uint32_t Type) const { return false; }
|
|
|
|
bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type) const { return false; }
|
2015-09-27 16:45:38 +08:00
|
|
|
|
|
|
|
static void handle_ADR_PREL_LO21(uint8_t *Location, uint64_t S, int64_t A,
|
|
|
|
uint64_t P) {
|
|
|
|
uint64_t X = S + A - P;
|
|
|
|
if (!isInt<21>(X))
|
|
|
|
error("Relocation R_AARCH64_ADR_PREL_LO21 out of range");
|
|
|
|
uint32_t Imm = X & 0x1FFFFF;
|
|
|
|
uint32_t ImmLo = (Imm & 0x3) << 29;
|
|
|
|
uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
|
|
|
|
uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
|
|
|
|
write32le(Location, (read32le(Location) & ~Mask) | ImmLo | ImmHi);
|
|
|
|
}
|
|
|
|
|
2015-09-26 08:32:04 +08:00
|
|
|
void AArch64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP,
|
|
|
|
uint32_t Type, uint64_t BaseAddr,
|
2015-09-27 16:45:38 +08:00
|
|
|
uint64_t SymVA) const {
|
|
|
|
typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
|
|
|
|
auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
|
|
|
|
|
|
|
|
uint8_t *Location = Buf + Rel.r_offset;
|
|
|
|
uint64_t S = SymVA;
|
|
|
|
int64_t A = Rel.r_addend;
|
|
|
|
uint64_t P = BaseAddr + Rel.r_offset;
|
|
|
|
switch (Type) {
|
|
|
|
case R_AARCH64_ADR_PREL_LO21:
|
|
|
|
handle_ADR_PREL_LO21(Location, S, A, P);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error(Twine("unrecognized reloc ") + Twine(Type));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-09-29 13:34:03 +08:00
|
|
|
|
|
|
|
MipsTargetInfo::MipsTargetInfo() {
|
|
|
|
// PCRelReloc = FIXME
|
|
|
|
// GotReloc = FIXME
|
|
|
|
DefaultEntry = "__start";
|
|
|
|
}
|
|
|
|
|
|
|
|
void MipsTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
|
|
|
|
uint64_t PltEntryAddr) const {}
|
|
|
|
|
|
|
|
bool MipsTargetInfo::relocNeedsGot(uint32_t Type) const { return false; }
|
|
|
|
|
|
|
|
bool MipsTargetInfo::relocNeedsPlt(uint32_t Type) const { return false; }
|
|
|
|
|
|
|
|
void MipsTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
|
|
|
|
uint64_t BaseAddr, uint64_t SymVA) const {}
|
2015-09-23 02:19:46 +08:00
|
|
|
}
|
|
|
|
}
|