25#define DEBUG_TYPE "jitlink"
32constexpr StringRef ELFGOTSymbolName =
"_GLOBAL_OFFSET_TABLE_";
34class ELFJITLinker_aarch64 :
public JITLinker<ELFJITLinker_aarch64> {
35 friend class JITLinker<ELFJITLinker_aarch64>;
38 ELFJITLinker_aarch64(std::unique_ptr<JITLinkContext> Ctx,
39 std::unique_ptr<LinkGraph>
G,
41 :
JITLinker(std::move(Ctx), std::move(
G), std::move(PassConfig)) {
44 [
this](
LinkGraph &
G) {
return getOrCreateGOTSymbol(
G); });
48 Symbol *GOTSymbol =
nullptr;
55 auto InteredGOTSymbolName =
56 G.getSymbolStringPool()->intern(ELFGOTSymbolName);
58 auto DefineExternalGOTSymbolIfPresent =
62 if (
auto *GOTSection =
G.findSectionByName(
65 return {*GOTSection, true};
72 if (
auto Err = DefineExternalGOTSymbolIfPresent(
G))
82 if (
auto *GOTSection =
86 for (
auto *
Sym : GOTSection->symbols())
98 Linkage::Strong, Scope::Local,
true);
101 &
G.addDefinedSymbol(*SR.
getFirstBlock(), 0, InteredGOTSymbolName, 0,
102 Linkage::Strong, Scope::Local,
false,
true);
109 for (
auto *
Sym :
G.external_symbols()) {
125template <
typename ELFT>
160 using namespace aarch64;
162 case ELF::R_AARCH64_CALL26:
163 case ELF::R_AARCH64_JUMP26:
165 case ELF::R_AARCH64_LD_PREL_LO19:
167 case ELF::R_AARCH64_ADR_PREL_LO21:
169 case ELF::R_AARCH64_ADR_PREL_PG_HI21:
171 case ELF::R_AARCH64_ADD_ABS_LO12_NC:
173 case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
174 return ELFLdSt8Abs12;
175 case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
176 return ELFLdSt16Abs12;
177 case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
178 return ELFLdSt32Abs12;
179 case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
180 return ELFLdSt64Abs12;
181 case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
182 return ELFLdSt128Abs12;
183 case ELF::R_AARCH64_MOVW_UABS_G0_NC:
185 case ELF::R_AARCH64_MOVW_UABS_G1_NC:
187 case ELF::R_AARCH64_MOVW_UABS_G2_NC:
189 case ELF::R_AARCH64_MOVW_UABS_G3:
191 case ELF::R_AARCH64_TSTBR14:
193 case ELF::R_AARCH64_CONDBR19:
195 case ELF::R_AARCH64_ABS32:
197 case ELF::R_AARCH64_ABS64:
199 case ELF::R_AARCH64_PREL32:
201 case ELF::R_AARCH64_PREL64:
203 case ELF::R_AARCH64_ADR_GOT_PAGE:
204 return ELFAdrGOTPage21;
205 case ELF::R_AARCH64_LD64_GOT_LO12_NC:
206 return ELFLd64GOTLo12;
207 case ELF::R_AARCH64_LD64_GOTPAGE_LO15:
208 return ELFLd64GOTPAGELo15;
209 case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
210 return ELFTLSDescAdrPage21;
211 case ELF::R_AARCH64_TLSDESC_ADD_LO12:
212 return ELFTLSDescAddLo12;
213 case ELF::R_AARCH64_TLSDESC_LD64_LO12:
214 return ELFTLSDescLd64Lo12;
215 case ELF::R_AARCH64_TLSDESC_CALL:
216 return ELFTLSDescCall;
219 return make_error<JITLinkError>(
220 "Unsupported aarch64 relocation:" +
formatv(
"{0:d}: ",
Type) +
228 using Self = ELFLinkGraphBuilder_aarch64<ELFT>;
229 for (
const auto &RelSect : Base::Sections)
230 if (
Error Err = Base::forEachRelaRelocation(RelSect,
this,
231 &Self::addSingleRelocation))
237 Error addSingleRelocation(
const typename ELFT::Rela &Rel,
238 const typename ELFT::Shdr &FixupSect,
243 uint32_t SymbolIndex = Rel.getSymbol(
false);
244 auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
246 return ObjSymbol.takeError();
248 Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
250 return make_error<StringError>(
251 formatv(
"Could not find symbol at given index, did you add it to "
252 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
253 SymbolIndex, (*ObjSymbol)->st_shndx,
254 Base::GraphSymbols.size()),
262 int64_t Addend = Rel.r_addend;
273 switch (*RelocKind) {
281 return make_error<JITLinkError>(
282 "R_AARCH64_LDR_PREL_LO19 target is not an LDR Literal instruction");
290 return make_error<JITLinkError>(
291 "R_AARCH64_ADR_PREL_LO21 target is not an ADR instruction");
304 case ELFLdSt8Abs12: {
308 return make_error<JITLinkError>(
309 "R_AARCH64_LDST8_ABS_LO12_NC target is not a "
310 "LDRB/STRB (imm12) instruction");
315 case ELFLdSt16Abs12: {
319 return make_error<JITLinkError>(
320 "R_AARCH64_LDST16_ABS_LO12_NC target is not a "
321 "LDRH/STRH (imm12) instruction");
326 case ELFLdSt32Abs12: {
330 return make_error<JITLinkError>(
331 "R_AARCH64_LDST32_ABS_LO12_NC target is not a "
332 "LDR/STR (imm12, 32 bit) instruction");
337 case ELFLdSt64Abs12: {
341 return make_error<JITLinkError>(
342 "R_AARCH64_LDST64_ABS_LO12_NC target is not a "
343 "LDR/STR (imm12, 64 bit) instruction");
348 case ELFLdSt128Abs12: {
352 return make_error<JITLinkError>(
353 "R_AARCH64_LDST128_ABS_LO12_NC target is not a "
354 "LDR/STR (imm12, 128 bit) instruction");
363 return make_error<JITLinkError>(
364 "R_AARCH64_MOVW_UABS_G0_NC target is not a "
365 "MOVK/MOVZ (imm16, LSL #0) instruction");
374 return make_error<JITLinkError>(
375 "R_AARCH64_MOVW_UABS_G1_NC target is not a "
376 "MOVK/MOVZ (imm16, LSL #16) instruction");
385 return make_error<JITLinkError>(
386 "R_AARCH64_MOVW_UABS_G2_NC target is not a "
387 "MOVK/MOVZ (imm16, LSL #32) instruction");
396 return make_error<JITLinkError>(
397 "R_AARCH64_MOVW_UABS_G3 target is not a "
398 "MOVK/MOVZ (imm16, LSL #48) instruction");
406 return make_error<JITLinkError>(
"R_AARCH64_TSTBR14 target is not a "
407 "test and branch instruction");
416 return make_error<JITLinkError>(
"R_AARCH64_CONDBR19 target is not a "
417 "conditional branch instruction");
438 case ELFAdrGOTPage21: {
442 case ELFLd64GOTLo12: {
446 case ELFLd64GOTPAGELo15: {
450 case ELFTLSDescAdrPage21: {
454 case ELFTLSDescAddLo12:
455 case ELFTLSDescLd64Lo12: {
459 case ELFTLSDescCall: {
471 BlockToFix.
addEdge(std::move(GE));
477 const char *getELFAArch64RelocationKindName(
Edge::Kind R) {
482 return "ELFAdrPage21";
484 return "ELFAddAbs12";
486 return "ELFLdSt8Abs12";
488 return "ELFLdSt16Abs12";
490 return "ELFLdSt32Abs12";
492 return "ELFLdSt64Abs12";
493 case ELFLdSt128Abs12:
494 return "ELFLdSt128Abs12";
496 return "ELFMovwAbsG0";
498 return "ELFMovwAbsG1";
500 return "ELFMovwAbsG2";
502 return "ELFMovwAbsG3";
511 case ELFAdrGOTPage21:
512 return "ELFAdrGOTPage21";
514 return "ELFLd64GOTLo12";
515 case ELFLd64GOTPAGELo15:
516 return "ELFLd64GOTPAGELo15";
517 case ELFTLSDescAdrPage21:
518 return "ELFTLSDescAdrPage21";
519 case ELFTLSDescAddLo12:
520 return "ELFTLSDescAddLo12";
521 case ELFTLSDescLd64Lo12:
522 return "ELFTLSDescLd64Lo12";
524 return "ELFTLSDescCall";
531 ELFLinkGraphBuilder_aarch64(
StringRef FileName,
533 std::shared_ptr<orc::SymbolStringPool> SSP,
542class TLSInfoTableManager_ELF_aarch64
543 :
public TableManager<TLSInfoTableManager_ELF_aarch64> {
547 static const uint8_t TLSInfoEntryContent[16];
554 auto &TLSInfoEntry =
G.createMutableContentBlock(
555 getTLSInfoSection(
G),
G.allocateContent(getTLSInfoEntryContent()),
558 return G.addAnonymousSymbol(TLSInfoEntry, 0, 16,
false,
false);
565 return *TLSInfoTable;
569 return {
reinterpret_cast<const char *
>(TLSInfoEntryContent),
570 sizeof(TLSInfoEntryContent)};
573 Section *TLSInfoTable =
nullptr;
576const uint8_t TLSInfoTableManager_ELF_aarch64::TLSInfoEntryContent[16] = {
577 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
578 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
582class TLSDescTableManager_ELF_aarch64
583 :
public TableManager<TLSDescTableManager_ELF_aarch64> {
585 TLSDescTableManager_ELF_aarch64(
586 TLSInfoTableManager_ELF_aarch64 &TLSInfoTableManager)
587 : TLSInfoTableManager(TLSInfoTableManager) {}
591 static const uint8_t TLSDescEntryContent[16];
607 assert(KindToSet != Edge::Invalid &&
608 "Fell through switch, but no new kind to set");
610 dbgs() <<
" Fixing " <<
G.getEdgeKindName(E.
getKind()) <<
" edge at "
611 <<
B->getFixupAddress(E) <<
" (" <<
B->getAddress() <<
" + "
621 G.createContentBlock(getTLSDescSection(
G), getTLSDescBlockContent(),
625 TLSInfoTableManager.getEntryForTarget(
G,
Target), 0);
626 return G.addAnonymousSymbol(EntryBlock, 0, 8,
false,
false);
637 if (!TLSDescResolver)
638 TLSDescResolver = &
G.addExternalSymbol(
"__tlsdesc_resolver", 8,
false);
639 return *TLSDescResolver;
643 return {
reinterpret_cast<const char *
>(TLSDescEntryContent),
644 sizeof(TLSDescEntryContent)};
648 Symbol *TLSDescResolver =
nullptr;
649 TLSInfoTableManager_ELF_aarch64 &TLSInfoTableManager;
652const uint8_t TLSDescTableManager_ELF_aarch64::TLSDescEntryContent[16] = {
653 0x00, 0x00, 0x00, 0x00,
654 0x00, 0x00, 0x00, 0x00,
655 0x00, 0x00, 0x00, 0x00,
656 0x00, 0x00, 0x00, 0x00
664 TLSInfoTableManager_ELF_aarch64 TLSInfo;
665 TLSDescTableManager_ELF_aarch64 TLSDesc(TLSInfo);
676 MemoryBufferRef ObjectBuffer, std::shared_ptr<orc::SymbolStringPool> SSP) {
678 dbgs() <<
"Building jitlink graph for new input "
684 return ELFObj.takeError();
686 auto Features = (*ELFObj)->getFeatures();
688 return Features.takeError();
691 "Only AArch64 (little endian) is supported for now");
693 auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj);
694 return ELFLinkGraphBuilder_aarch64<object::ELF64LE>(
695 (*ELFObj)->getFileName(), ELFObjFile.getELFFile(), std::move(SSP),
696 (*ELFObj)->makeTriple(), std::move(*Features))
701 std::unique_ptr<JITLinkContext> Ctx) {
703 const Triple &TT =
G->getTargetTriple();
704 if (Ctx->shouldAddDefaultTargetPasses(TT)) {
713 if (
auto MarkLive = Ctx->getMarkLivePass(TT))
714 Config.PrePrunePasses.push_back(std::move(MarkLive));
719 Config.PostAllocationPasses.push_back(
724 Config.PostPrunePasses.push_back(buildTables_ELF_aarch64);
727 if (
auto Err = Ctx->modifyPassConfig(*
G,
Config))
728 return Ctx->notifyFailed(std::move(Err));
730 ELFJITLinker_aarch64::link(std::move(Ctx), std::move(
G), std::move(
Config));
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
DenseMap< Block *, BlockRelaxAux > Blocks
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
StringRef getBufferIdentifier() const
StringRef - Represent a constant reference to a string, i.e.
Manages the enabling and disabling of subtarget specific features.
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
The instances of the Type class are immutable: once they are created, they are never changed.
orc::ExecutorAddr getAddress() const
An Addressable with content and edges.
ArrayRef< char > getContent() const
Get the content for this block. Block must not be a zero-fill block.
void addEdge(Edge::Kind K, Edge::OffsetT Offset, Symbol &Target, Edge::AddendT Addend)
Add an edge to this block.
A LinkGraph pass that splits blocks in a section that follows the DWARF Record format into sub-blocks...
A LinkGraph pass that adds missing FDE-to-CIE, FDE-to-PC and FDE-to-LSDA edges.
LinkGraph building code that's specific to the given ELFT, but common across all architectures.
virtual Error addRelocations()=0
Call to derived class to handle relocations.
Represents fixups and constraints in the LinkGraph.
Symbol & getTarget() const
OffsetT getOffset() const
void setTarget(Symbol &Target)
PassConfiguration & getPassConfig()
bool shouldAddDefaultTargetPasses(const Triple &TT)
Represents a section address range via a pair of Block pointers to the first and last Blocks in the s...
Block * getFirstBlock() const
Represents an object file section.
const orc::SymbolStringPtr & getName() const
Returns the name of this symbol (empty if the symbol is anonymous).
orc::ExecutorAddr getAddress() const
Returns the address of this symbol.
A CRTP base for tables that are built on demand, e.g.
Symbol & getEntryForTarget(LinkGraph &G, Symbol &Target)
Return the constructed entry.
Global Offset Table Builder.
static StringRef getSectionName()
Procedure Linkage Table Builder.
static Expected< std::unique_ptr< ObjectFile > > createELFObjectFile(MemoryBufferRef Object, bool InitContent=true)
Represents an address in the executor process.
static constexpr const StringLiteral & getSectionName(DebugSectionKind SectionKind)
Return the name of the section.
const char * getEdgeKindName(Edge::Kind K)
Get a human-readable name for the given AArch32 edge kind.
bool isLDRLiteral(uint32_t Instr)
bool isADR(uint32_t Instr)
bool isCondBranchImm19(uint32_t Instr)
Error applyFixup(LinkGraph &G, Block &B, const Edge &E, const Symbol *GOTSymbol)
Apply fixup expression for edge to block content.
bool isMoveWideImm16(uint32_t Instr)
bool isCompAndBranchImm19(uint32_t Instr)
bool isLoadStoreImm12(uint32_t Instr)
const char * getEdgeKindName(Edge::Kind K)
Returns a string name for the given aarch64 edge.
@ LDRLiteral19
The signed 21-bit delta from the fixup to the target.
@ RequestGOTAndTransformToPageOffset15
A GOT entry getter/constructor, transformed to Pageoffset15 pointing at the GOT entry for the origina...
@ CondBranch19PCRel
A 19-bit PC-relative conditional branch.
@ RequestTLSDescEntryAndTransformToPageOffset12
A TLSDesc entry getter/constructor, transformed to PageOffset12.
@ Page21
The signed 21-bit delta from the fixup page to the page containing the target.
@ Branch26PCRel
A 26-bit PC-relative branch.
@ Pointer64
A plain 64-bit pointer value relocation.
@ Pointer32
A plain 32-bit pointer value relocation.
@ MoveWide16
A 16-bit slice of the target address (which slice depends on the instruction at the fixup location).
@ TestAndBranch14PCRel
A 14-bit PC-relative test and branch.
@ RequestGOTAndTransformToPage21
A GOT entry getter/constructor, transformed to Page21 pointing at the GOT entry for the original targ...
@ ADRLiteral21
The signed 21-bit delta from the fixup to the target.
@ RequestGOTAndTransformToPageOffset12
A GOT entry getter/constructor, transformed to Pageoffset12 pointing at the GOT entry for the origina...
@ NegDelta32
A 32-bit negative delta.
@ PageOffset12
The 12-bit (potentially shifted) offset of the target within its page.
@ RequestTLSDescEntryAndTransformToPage21
A TLSDesc entry getter/constructor, transformed to Page21.
bool isTestAndBranchImm14(uint32_t Instr)
unsigned getPageOffset12Shift(uint32_t Instr)
unsigned getMoveWide16Shift(uint32_t Instr)
const char * getGenericEdgeKindName(Edge::Kind K)
Returns the string name of the given generic edge kind, or "unknown" otherwise.
void visitExistingEdges(LinkGraph &G, VisitorTs &&...Vs)
For each edge in the given graph, apply a list of visitors to the edge, stopping when the first visit...
Error markAllSymbolsLive(LinkGraph &G)
Marks all symbols in a graph live.
void link_ELF_aarch64(std::unique_ptr< LinkGraph > G, std::unique_ptr< JITLinkContext > Ctx)
jit-link the given object buffer, which must be a ELF aarch64 relocatable object file.
Expected< std::unique_ptr< LinkGraph > > createLinkGraphFromELFObject_aarch64(MemoryBufferRef ObjectBuffer, std::shared_ptr< orc::SymbolStringPool > SSP)
Create a LinkGraph from an ELF/aarch64 relocatable object.
void printEdge(raw_ostream &OS, const Block &B, const Edge &E, StringRef EdgeKindName)
SectionRangeSymbolDesc identifyELFSectionStartAndEndSymbols(LinkGraph &G, Symbol &Sym)
ELF section start/end symbol detection.
void visitEdge(LinkGraph &G, Block *B, Edge &E)
Base case for edge-visitors where the visitor-list is empty.
DefineExternalSectionStartAndEndSymbols< SymbolIdentifierFunction > createDefineExternalSectionStartAndEndSymbolsPass(SymbolIdentifierFunction &&F)
Returns a JITLink pass (as a function class) that uses the given symbol identification function to id...
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
NodeAddr< InstrNode * > Instr
detail::packed_endian_specific_integral< uint32_t, llvm::endianness::little, unaligned > ulittle32_t
This is an optimization pass for GlobalISel generic memory operations.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.
An LinkGraph pass configuration, consisting of a list of pre-prune, post-prune, and post-fixup passes...
LinkGraphPassList PostAllocationPasses
Post-allocation passes.