#include "Arch/ARM64Common.h"
#include "InputFiles.h"
#include "Symbols.h"
#include "SyntheticSections.h"
#include "Target.h"
#include "lld/Common/ErrorHandler.h"
#include "mach-o/compact_unwind_encoding.h"
#include "llvm/BinaryFormat/MachO.h"
using namespace llvm;
using namespace llvm::MachO;
using namespace lld;
using namespace lld::macho;
namespace {
struct ARM64 : ARM64Common {
ARM64();
void writeStub(uint8_t *buf, const Symbol &, uint64_t) const override;
void writeStubHelperHeader(uint8_t *buf) const override;
void writeStubHelperEntry(uint8_t *buf, const Symbol &,
uint64_t entryAddr) const override;
void writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr,
uint64_t &stubOffset, uint64_t selrefVA,
Symbol *objcMsgSend) const override;
void populateThunk(InputSection *thunk, Symbol *funcSym) override;
void initICFSafeThunkBody(InputSection *thunk,
Symbol *targetSym) const override;
Symbol *getThunkBranchTarget(InputSection *thunk) const override;
uint32_t getICFSafeThunkSize() const override;
};
}
static constexpr std::array<RelocAttrs, 11> relocAttrsArray{{
#define B(x) RelocAttrBits::x
{"UNSIGNED",
B(UNSIGNED) | B(ABSOLUTE) | B(EXTERN) | B(LOCAL) | B(BYTE4) | B(BYTE8)},
{"SUBTRACTOR", B(SUBTRAHEND) | B(EXTERN) | B(BYTE4) | B(BYTE8)},
{"BRANCH26", B(PCREL) | B(EXTERN) | B(BRANCH) | B(BYTE4)},
{"PAGE21", B(PCREL) | B(EXTERN) | B(BYTE4)},
{"PAGEOFF12", B(ABSOLUTE) | B(EXTERN) | B(BYTE4)},
{"GOT_LOAD_PAGE21", B(PCREL) | B(EXTERN) | B(GOT) | B(BYTE4)},
{"GOT_LOAD_PAGEOFF12",
B(ABSOLUTE) | B(EXTERN) | B(GOT) | B(LOAD) | B(BYTE4)},
{"POINTER_TO_GOT", B(PCREL) | B(EXTERN) | B(GOT) | B(POINTER) | B(BYTE4)},
{"TLVP_LOAD_PAGE21", B(PCREL) | B(EXTERN) | B(TLV) | B(BYTE4)},
{"TLVP_LOAD_PAGEOFF12",
B(ABSOLUTE) | B(EXTERN) | B(TLV) | B(LOAD) | B(BYTE4)},
{"ADDEND", B(ADDEND)},
#undef B
}};
static constexpr uint32_t stubCode[] = {
0x90000010,
0xf9400210,
0xd61f0200,
};
void ARM64::writeStub(uint8_t *buf8, const Symbol &sym,
uint64_t pointerVA) const {
::writeStub(buf8, stubCode, sym, pointerVA);
}
static constexpr uint32_t stubHelperHeaderCode[] = {
0x90000011,
0x91000231,
0xa9bf47f0,
0x90000010,
0xf9400210,
0xd61f0200,
};
void ARM64::writeStubHelperHeader(uint8_t *buf8) const {
::writeStubHelperHeader<LP64>(buf8, stubHelperHeaderCode);
}
static constexpr uint32_t stubHelperEntryCode[] = {
0x18000050,
0x14000000,
0x00000000,
};
void ARM64::writeStubHelperEntry(uint8_t *buf8, const Symbol &sym,
uint64_t entryVA) const {
::writeStubHelperEntry(buf8, stubHelperEntryCode, sym, entryVA);
}
static constexpr uint32_t objcStubsFastCode[] = {
0x90000001,
0xf9400021,
0x90000010,
0xf9400210,
0xd61f0200,
0xd4200020,
0xd4200020,
0xd4200020,
};
static constexpr uint32_t objcStubsSmallCode[] = {
0x90000001,
0xf9400021,
0x14000000,
};
void ARM64::writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr,
uint64_t &stubOffset, uint64_t selrefVA,
Symbol *objcMsgSend) const {
uint64_t objcMsgSendAddr;
uint64_t objcStubSize;
uint64_t objcMsgSendIndex;
if (config->objcStubsMode == ObjCStubsMode::fast) {
objcStubSize = target->objcStubsFastSize;
objcMsgSendAddr = in.got->addr;
objcMsgSendIndex = objcMsgSend->gotIndex;
::writeObjCMsgSendFastStub<LP64>(buf, objcStubsFastCode, sym, stubsAddr,
stubOffset, selrefVA, objcMsgSendAddr,
objcMsgSendIndex);
} else {
assert(config->objcStubsMode == ObjCStubsMode::small);
objcStubSize = target->objcStubsSmallSize;
if (auto *d = dyn_cast<Defined>(objcMsgSend)) {
objcMsgSendAddr = d->getVA();
objcMsgSendIndex = 0;
} else {
objcMsgSendAddr = in.stubs->addr;
objcMsgSendIndex = objcMsgSend->stubsIndex;
}
::writeObjCMsgSendSmallStub<LP64>(buf, objcStubsSmallCode, sym, stubsAddr,
stubOffset, selrefVA, objcMsgSendAddr,
objcMsgSendIndex);
}
stubOffset += objcStubSize;
}
static constexpr uint32_t thunkCode[] = {
0x90000010,
0x91000210,
0xd61f0200,
};
void ARM64::populateThunk(InputSection *thunk, Symbol *funcSym) {
thunk->align = 4;
thunk->data = {reinterpret_cast<const uint8_t *>(thunkCode),
sizeof(thunkCode)};
thunk->relocs.emplace_back(ARM64_RELOC_PAGEOFF12,
false, 2,
4, 0,
funcSym);
thunk->relocs.emplace_back(ARM64_RELOC_PAGE21,
true, 2,
0, 0,
funcSym);
}
static constexpr uint32_t icfSafeThunkCode[] = {
0x14000000,
};
void ARM64::initICFSafeThunkBody(InputSection *thunk, Symbol *targetSym) const {
thunk->data = {reinterpret_cast<const uint8_t *>(icfSafeThunkCode),
sizeof(icfSafeThunkCode)};
thunk->relocs.emplace_back(ARM64_RELOC_BRANCH26,
true, 2,
0, 0,
targetSym);
}
Symbol *ARM64::getThunkBranchTarget(InputSection *thunk) const {
assert(thunk->relocs.size() == 1 &&
"expected a single reloc on ARM64 ICF thunk");
auto &reloc = thunk->relocs[0];
assert(isa<Symbol *>(reloc.referent) &&
"ARM64 thunk reloc is expected to point to a Symbol");
return cast<Symbol *>(reloc.referent);
}
uint32_t ARM64::getICFSafeThunkSize() const { return sizeof(icfSafeThunkCode); }
ARM64::ARM64() : ARM64Common(LP64()) {
cpuType = CPU_TYPE_ARM64;
cpuSubtype = CPU_SUBTYPE_ARM64_ALL;
stubSize = sizeof(stubCode);
thunkSize = sizeof(thunkCode);
objcStubsFastSize = sizeof(objcStubsFastCode);
objcStubsFastAlignment = 32;
objcStubsSmallSize = sizeof(objcStubsSmallCode);
objcStubsSmallAlignment = 4;
backwardBranchRange = 128 * 1024 * 1024;
forwardBranchRange = backwardBranchRange - 4;
modeDwarfEncoding = UNWIND_ARM64_MODE_DWARF;
subtractorRelocType = ARM64_RELOC_SUBTRACTOR;
unsignedRelocType = ARM64_RELOC_UNSIGNED;
stubHelperHeaderSize = sizeof(stubHelperHeaderCode);
stubHelperEntrySize = sizeof(stubHelperEntryCode);
relocAttrs = {relocAttrsArray.data(), relocAttrsArray.size()};
}
TargetInfo *macho::createARM64TargetInfo() {
static ARM64 t;
return &t;
}