25#define DEBUG_TYPE "jitlink"
32class ELFJITLinker_aarch64 :
public JITLinker<ELFJITLinker_aarch64> {
33 friend class JITLinker<ELFJITLinker_aarch64>;
36 ELFJITLinker_aarch64(std::unique_ptr<JITLinkContext> Ctx,
37 std::unique_ptr<LinkGraph>
G,
39 :
JITLinker(std::move(Ctx), std::move(
G), std::move(PassConfig)) {}
47template <
typename ELFT>
81 using namespace aarch64;
83 case ELF::R_AARCH64_CALL26:
84 case ELF::R_AARCH64_JUMP26:
86 case ELF::R_AARCH64_LD_PREL_LO19:
88 case ELF::R_AARCH64_ADR_PREL_LO21:
90 case ELF::R_AARCH64_ADR_PREL_PG_HI21:
92 case ELF::R_AARCH64_ADD_ABS_LO12_NC:
94 case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
96 case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
97 return ELFLdSt16Abs12;
98 case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
99 return ELFLdSt32Abs12;
100 case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
101 return ELFLdSt64Abs12;
102 case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
103 return ELFLdSt128Abs12;
104 case ELF::R_AARCH64_MOVW_UABS_G0_NC:
106 case ELF::R_AARCH64_MOVW_UABS_G1_NC:
108 case ELF::R_AARCH64_MOVW_UABS_G2_NC:
110 case ELF::R_AARCH64_MOVW_UABS_G3:
112 case ELF::R_AARCH64_TSTBR14:
114 case ELF::R_AARCH64_CONDBR19:
116 case ELF::R_AARCH64_ABS32:
118 case ELF::R_AARCH64_ABS64:
120 case ELF::R_AARCH64_PREL32:
122 case ELF::R_AARCH64_PREL64:
124 case ELF::R_AARCH64_ADR_GOT_PAGE:
125 return ELFAdrGOTPage21;
126 case ELF::R_AARCH64_LD64_GOT_LO12_NC:
127 return ELFLd64GOTLo12;
128 case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
129 return ELFTLSDescAdrPage21;
130 case ELF::R_AARCH64_TLSDESC_ADD_LO12:
131 return ELFTLSDescAddLo12;
132 case ELF::R_AARCH64_TLSDESC_LD64_LO12:
133 return ELFTLSDescLd64Lo12;
134 case ELF::R_AARCH64_TLSDESC_CALL:
135 return ELFTLSDescCall;
138 return make_error<JITLinkError>(
139 "Unsupported aarch64 relocation:" +
formatv(
"{0:d}: ",
Type) +
147 using Self = ELFLinkGraphBuilder_aarch64<ELFT>;
148 for (
const auto &RelSect : Base::Sections)
149 if (
Error Err = Base::forEachRelaRelocation(RelSect,
this,
150 &Self::addSingleRelocation))
156 Error addSingleRelocation(
const typename ELFT::Rela &Rel,
157 const typename ELFT::Shdr &FixupSect,
162 uint32_t SymbolIndex = Rel.getSymbol(
false);
163 auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
165 return ObjSymbol.takeError();
167 Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
169 return make_error<StringError>(
170 formatv(
"Could not find symbol at given index, did you add it to "
171 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
172 SymbolIndex, (*ObjSymbol)->st_shndx,
173 Base::GraphSymbols.size()),
181 int64_t Addend = Rel.r_addend;
192 switch (*RelocKind) {
198 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
200 return make_error<JITLinkError>(
201 "R_AARCH64_LDR_PREL_LO19 target is not an LDR Literal instruction");
207 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
209 return make_error<JITLinkError>(
210 "R_AARCH64_ADR_PREL_LO21 target is not an ADR instruction");
223 case ELFLdSt8Abs12: {
224 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
227 return make_error<JITLinkError>(
228 "R_AARCH64_LDST8_ABS_LO12_NC target is not a "
229 "LDRB/STRB (imm12) instruction");
234 case ELFLdSt16Abs12: {
235 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
238 return make_error<JITLinkError>(
239 "R_AARCH64_LDST16_ABS_LO12_NC target is not a "
240 "LDRH/STRH (imm12) instruction");
245 case ELFLdSt32Abs12: {
246 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
249 return make_error<JITLinkError>(
250 "R_AARCH64_LDST32_ABS_LO12_NC target is not a "
251 "LDR/STR (imm12, 32 bit) instruction");
256 case ELFLdSt64Abs12: {
257 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
260 return make_error<JITLinkError>(
261 "R_AARCH64_LDST64_ABS_LO12_NC target is not a "
262 "LDR/STR (imm12, 64 bit) instruction");
267 case ELFLdSt128Abs12: {
268 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
271 return make_error<JITLinkError>(
272 "R_AARCH64_LDST128_ABS_LO12_NC target is not a "
273 "LDR/STR (imm12, 128 bit) instruction");
279 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
282 return make_error<JITLinkError>(
283 "R_AARCH64_MOVW_UABS_G0_NC target is not a "
284 "MOVK/MOVZ (imm16, LSL #0) instruction");
290 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
293 return make_error<JITLinkError>(
294 "R_AARCH64_MOVW_UABS_G1_NC target is not a "
295 "MOVK/MOVZ (imm16, LSL #16) instruction");
301 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
304 return make_error<JITLinkError>(
305 "R_AARCH64_MOVW_UABS_G2_NC target is not a "
306 "MOVK/MOVZ (imm16, LSL #32) instruction");
312 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
315 return make_error<JITLinkError>(
316 "R_AARCH64_MOVW_UABS_G3 target is not a "
317 "MOVK/MOVZ (imm16, LSL #48) instruction");
323 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
325 return make_error<JITLinkError>(
"R_AARCH64_TSTBR14 target is not a "
326 "test and branch instruction");
332 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
335 return make_error<JITLinkError>(
"R_AARCH64_CONDBR19 target is not a "
336 "conditional branch instruction");
357 case ELFAdrGOTPage21: {
361 case ELFLd64GOTLo12: {
365 case ELFTLSDescAdrPage21: {
369 case ELFTLSDescAddLo12:
370 case ELFTLSDescLd64Lo12: {
374 case ELFTLSDescCall: {
386 BlockToFix.
addEdge(std::move(GE));
392 const char *getELFAArch64RelocationKindName(
Edge::Kind R) {
397 return "ELFAdrPage21";
399 return "ELFAddAbs12";
401 return "ELFLdSt8Abs12";
403 return "ELFLdSt16Abs12";
405 return "ELFLdSt32Abs12";
407 return "ELFLdSt64Abs12";
408 case ELFLdSt128Abs12:
409 return "ELFLdSt128Abs12";
411 return "ELFMovwAbsG0";
413 return "ELFMovwAbsG1";
415 return "ELFMovwAbsG2";
417 return "ELFMovwAbsG3";
426 case ELFAdrGOTPage21:
427 return "ELFAdrGOTPage21";
429 return "ELFLd64GOTLo12";
430 case ELFTLSDescAdrPage21:
431 return "ELFTLSDescAdrPage21";
432 case ELFTLSDescAddLo12:
433 return "ELFTLSDescAddLo12";
434 case ELFTLSDescLd64Lo12:
435 return "ELFTLSDescLd64Lo12";
437 return "ELFTLSDescCall";
444 ELFLinkGraphBuilder_aarch64(
StringRef FileName,
452class TLSInfoTableManager_ELF_aarch64
453 :
public TableManager<TLSInfoTableManager_ELF_aarch64> {
457 static const uint8_t TLSInfoEntryContent[16];
464 auto &TLSInfoEntry =
G.createMutableContentBlock(
465 getTLSInfoSection(
G),
G.allocateContent(getTLSInfoEntryContent()),
468 return G.addAnonymousSymbol(TLSInfoEntry, 0, 16,
false,
false);
475 return *TLSInfoTable;
479 return {
reinterpret_cast<const char *
>(TLSInfoEntryContent),
480 sizeof(TLSInfoEntryContent)};
483 Section *TLSInfoTable =
nullptr;
486const uint8_t TLSInfoTableManager_ELF_aarch64::TLSInfoEntryContent[16] = {
487 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
488 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
492class TLSDescTableManager_ELF_aarch64
493 :
public TableManager<TLSDescTableManager_ELF_aarch64> {
495 TLSDescTableManager_ELF_aarch64(
496 TLSInfoTableManager_ELF_aarch64 &TLSInfoTableManager)
497 : TLSInfoTableManager(TLSInfoTableManager) {}
501 static const uint8_t TLSDescEntryContent[16];
517 assert(KindToSet != Edge::Invalid &&
518 "Fell through switch, but no new kind to set");
520 dbgs() <<
" Fixing " <<
G.getEdgeKindName(E.
getKind()) <<
" edge at "
521 <<
B->getFixupAddress(E) <<
" (" <<
B->getAddress() <<
" + "
531 G.createContentBlock(getTLSDescSection(
G), getTLSDescBlockContent(),
535 TLSInfoTableManager.getEntryForTarget(
G,
Target), 0);
536 return G.addAnonymousSymbol(EntryBlock, 0, 8,
false,
false);
547 if (!TLSDescResolver)
548 TLSDescResolver = &
G.addExternalSymbol(
"__tlsdesc_resolver", 8,
false);
549 return *TLSDescResolver;
553 return {
reinterpret_cast<const char *
>(TLSDescEntryContent),
554 sizeof(TLSDescEntryContent)};
558 Symbol *TLSDescResolver =
nullptr;
559 TLSInfoTableManager_ELF_aarch64 &TLSInfoTableManager;
562const uint8_t TLSDescTableManager_ELF_aarch64::TLSDescEntryContent[16] = {
563 0x00, 0x00, 0x00, 0x00,
564 0x00, 0x00, 0x00, 0x00,
565 0x00, 0x00, 0x00, 0x00,
566 0x00, 0x00, 0x00, 0x00
574 TLSInfoTableManager_ELF_aarch64 TLSInfo;
575 TLSDescTableManager_ELF_aarch64 TLSDesc(TLSInfo);
588 dbgs() <<
"Building jitlink graph for new input "
594 return ELFObj.takeError();
596 auto Features = (*ELFObj)->getFeatures();
598 return Features.takeError();
601 "Only AArch64 (little endian) is supported for now");
603 auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj);
604 return ELFLinkGraphBuilder_aarch64<object::ELF64LE>(
605 (*ELFObj)->getFileName(), ELFObjFile.getELFFile(),
606 (*ELFObj)->makeTriple(), std::move(*Features))
611 std::unique_ptr<JITLinkContext> Ctx) {
613 const Triple &TT =
G->getTargetTriple();
614 if (Ctx->shouldAddDefaultTargetPasses(TT)) {
623 if (
auto MarkLive = Ctx->getMarkLivePass(TT))
624 Config.PrePrunePasses.push_back(std::move(MarkLive));
629 Config.PostAllocationPasses.push_back(
634 Config.PostPrunePasses.push_back(buildTables_ELF_aarch64);
637 if (
auto Err = Ctx->modifyPassConfig(*
G,
Config))
638 return Ctx->notifyFailed(std::move(Err));
640 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, X)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
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)
Represents an object file section.
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.
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.
bool isLDRLiteral(uint32_t Instr)
bool isADR(uint32_t Instr)
bool isCondBranchImm19(uint32_t Instr)
bool isMoveWideImm16(uint32_t Instr)
bool isCompAndBranchImm19(uint32_t Instr)
Error applyFixup(LinkGraph &G, Block &B, const Edge &E)
Apply fixup expression for edge to block content.
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.
@ 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.
Expected< std::unique_ptr< LinkGraph > > createLinkGraphFromELFObject_aarch64(MemoryBufferRef ObjectBuffer)
Create a LinkGraph from an ELF/aarch64 relocatable object.
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.
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)
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(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
An LinkGraph pass configuration, consisting of a list of pre-prune, post-prune, and post-fixup passes...