23#define DEBUG_TYPE "jitlink"
30class ELFJITLinker_aarch64 :
public JITLinker<ELFJITLinker_aarch64> {
31 friend class JITLinker<ELFJITLinker_aarch64>;
34 ELFJITLinker_aarch64(std::unique_ptr<JITLinkContext> Ctx,
35 std::unique_ptr<LinkGraph>
G,
37 :
JITLinker(std::move(Ctx), std::move(
G), std::move(PassConfig)) {}
45template <
typename ELFT>
78 using namespace aarch64;
80 case ELF::R_AARCH64_CALL26:
81 case ELF::R_AARCH64_JUMP26:
83 case ELF::R_AARCH64_ADR_PREL_LO21:
85 case ELF::R_AARCH64_ADR_PREL_PG_HI21:
87 case ELF::R_AARCH64_ADD_ABS_LO12_NC:
89 case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
91 case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
92 return ELFLdSt16Abs12;
93 case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
94 return ELFLdSt32Abs12;
95 case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
96 return ELFLdSt64Abs12;
97 case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
98 return ELFLdSt128Abs12;
99 case ELF::R_AARCH64_MOVW_UABS_G0_NC:
101 case ELF::R_AARCH64_MOVW_UABS_G1_NC:
103 case ELF::R_AARCH64_MOVW_UABS_G2_NC:
105 case ELF::R_AARCH64_MOVW_UABS_G3:
107 case ELF::R_AARCH64_TSTBR14:
109 case ELF::R_AARCH64_CONDBR19:
111 case ELF::R_AARCH64_ABS32:
113 case ELF::R_AARCH64_ABS64:
115 case ELF::R_AARCH64_PREL32:
117 case ELF::R_AARCH64_PREL64:
119 case ELF::R_AARCH64_ADR_GOT_PAGE:
120 return ELFAdrGOTPage21;
121 case ELF::R_AARCH64_LD64_GOT_LO12_NC:
122 return ELFLd64GOTLo12;
123 case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
124 return ELFTLSDescAdrPage21;
125 case ELF::R_AARCH64_TLSDESC_ADD_LO12:
126 return ELFTLSDescAddLo12;
127 case ELF::R_AARCH64_TLSDESC_LD64_LO12:
128 return ELFTLSDescLd64Lo12;
129 case ELF::R_AARCH64_TLSDESC_CALL:
130 return ELFTLSDescCall;
133 return make_error<JITLinkError>(
134 "Unsupported aarch64 relocation:" +
formatv(
"{0:d}: ",
Type) +
142 using Self = ELFLinkGraphBuilder_aarch64<ELFT>;
143 for (
const auto &RelSect : Base::Sections)
144 if (
Error Err = Base::forEachRelaRelocation(RelSect,
this,
145 &Self::addSingleRelocation))
151 Error addSingleRelocation(
const typename ELFT::Rela &Rel,
152 const typename ELFT::Shdr &FixupSect,
157 uint32_t SymbolIndex = Rel.getSymbol(
false);
158 auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
160 return ObjSymbol.takeError();
162 Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
164 return make_error<StringError>(
165 formatv(
"Could not find symbol at given index, did you add it to "
166 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
167 SymbolIndex, (*ObjSymbol)->st_shndx,
168 Base::GraphSymbols.size()),
176 int64_t Addend = Rel.r_addend;
187 switch (*RelocKind) {
193 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
195 return make_error<JITLinkError>(
196 "R_AARCH64_ADR_PREL_LO21 target is not an ADR instruction");
209 case ELFLdSt8Abs12: {
210 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
213 return make_error<JITLinkError>(
214 "R_AARCH64_LDST8_ABS_LO12_NC target is not a "
215 "LDRB/STRB (imm12) instruction");
220 case ELFLdSt16Abs12: {
221 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
224 return make_error<JITLinkError>(
225 "R_AARCH64_LDST16_ABS_LO12_NC target is not a "
226 "LDRH/STRH (imm12) instruction");
231 case ELFLdSt32Abs12: {
232 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
235 return make_error<JITLinkError>(
236 "R_AARCH64_LDST32_ABS_LO12_NC target is not a "
237 "LDR/STR (imm12, 32 bit) instruction");
242 case ELFLdSt64Abs12: {
243 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
246 return make_error<JITLinkError>(
247 "R_AARCH64_LDST64_ABS_LO12_NC target is not a "
248 "LDR/STR (imm12, 64 bit) instruction");
253 case ELFLdSt128Abs12: {
254 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
257 return make_error<JITLinkError>(
258 "R_AARCH64_LDST128_ABS_LO12_NC target is not a "
259 "LDR/STR (imm12, 128 bit) instruction");
265 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
268 return make_error<JITLinkError>(
269 "R_AARCH64_MOVW_UABS_G0_NC target is not a "
270 "MOVK/MOVZ (imm16, LSL #0) instruction");
276 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
279 return make_error<JITLinkError>(
280 "R_AARCH64_MOVW_UABS_G1_NC target is not a "
281 "MOVK/MOVZ (imm16, LSL #16) instruction");
287 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
290 return make_error<JITLinkError>(
291 "R_AARCH64_MOVW_UABS_G2_NC target is not a "
292 "MOVK/MOVZ (imm16, LSL #32) instruction");
298 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
301 return make_error<JITLinkError>(
302 "R_AARCH64_MOVW_UABS_G3 target is not a "
303 "MOVK/MOVZ (imm16, LSL #48) instruction");
309 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
311 return make_error<JITLinkError>(
"R_AARCH64_TSTBR14 target is not a "
312 "test and branch instruction");
318 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
321 return make_error<JITLinkError>(
"R_AARCH64_CONDBR19 target is not a "
322 "conditional branch instruction");
343 case ELFAdrGOTPage21: {
347 case ELFLd64GOTLo12: {
351 case ELFTLSDescAdrPage21: {
355 case ELFTLSDescAddLo12:
356 case ELFTLSDescLd64Lo12: {
360 case ELFTLSDescCall: {
372 BlockToFix.
addEdge(std::move(GE));
378 const char *getELFAArch64RelocationKindName(
Edge::Kind R) {
383 return "ELFAdrPage21";
385 return "ELFAddAbs12";
387 return "ELFLdSt8Abs12";
389 return "ELFLdSt16Abs12";
391 return "ELFLdSt32Abs12";
393 return "ELFLdSt64Abs12";
394 case ELFLdSt128Abs12:
395 return "ELFLdSt128Abs12";
397 return "ELFMovwAbsG0";
399 return "ELFMovwAbsG1";
401 return "ELFMovwAbsG2";
403 return "ELFMovwAbsG3";
412 case ELFAdrGOTPage21:
413 return "ELFAdrGOTPage21";
415 return "ELFLd64GOTLo12";
416 case ELFTLSDescAdrPage21:
417 return "ELFTLSDescAdrPage21";
418 case ELFTLSDescAddLo12:
419 return "ELFTLSDescAddLo12";
420 case ELFTLSDescLd64Lo12:
421 return "ELFTLSDescLd64Lo12";
423 return "ELFTLSDescCall";
430 ELFLinkGraphBuilder_aarch64(
StringRef FileName,
438class TLSInfoTableManager_ELF_aarch64
439 :
public TableManager<TLSInfoTableManager_ELF_aarch64> {
441 static StringRef getSectionName() {
return "$__TLSINFO"; }
443 static const uint8_t TLSInfoEntryContent[16];
450 auto &TLSInfoEntry =
G.createMutableContentBlock(
451 getTLSInfoSection(
G),
G.allocateContent(getTLSInfoEntryContent()),
454 return G.addAnonymousSymbol(TLSInfoEntry, 0, 16,
false,
false);
460 TLSInfoTable = &
G.createSection(getSectionName(), orc::MemProt::Read);
461 return *TLSInfoTable;
465 return {
reinterpret_cast<const char *
>(TLSInfoEntryContent),
466 sizeof(TLSInfoEntryContent)};
469 Section *TLSInfoTable =
nullptr;
472const uint8_t TLSInfoTableManager_ELF_aarch64::TLSInfoEntryContent[16] = {
473 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
474 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
478class TLSDescTableManager_ELF_aarch64
479 :
public TableManager<TLSDescTableManager_ELF_aarch64> {
481 TLSDescTableManager_ELF_aarch64(
482 TLSInfoTableManager_ELF_aarch64 &TLSInfoTableManager)
483 : TLSInfoTableManager(TLSInfoTableManager) {}
485 static StringRef getSectionName() {
return "$__TLSDESC"; }
487 static const uint8_t TLSDescEntryContent[16];
491 switch (
E.getKind()) {
503 assert(KindToSet != Edge::Invalid &&
504 "Fell through switch, but no new kind to set");
506 dbgs() <<
" Fixing " <<
G.getEdgeKindName(
E.getKind()) <<
" edge at "
507 <<
B->getFixupAddress(
E) <<
" (" <<
B->getAddress() <<
" + "
508 <<
formatv(
"{0:x}",
E.getOffset()) <<
")\n";
510 E.setKind(KindToSet);
517 G.createContentBlock(getTLSDescSection(
G), getTLSDescBlockContent(),
521 TLSInfoTableManager.getEntryForTarget(
G,
Target), 0);
522 return G.addAnonymousSymbol(EntryBlock, 0, 8,
false,
false);
528 GOTSection = &
G.createSection(getSectionName(), orc::MemProt::Read);
533 if (!TLSDescResolver)
534 TLSDescResolver = &
G.addExternalSymbol(
"__tlsdesc_resolver", 8,
false);
535 return *TLSDescResolver;
539 return {
reinterpret_cast<const char *
>(TLSDescEntryContent),
540 sizeof(TLSDescEntryContent)};
544 Symbol *TLSDescResolver =
nullptr;
545 TLSInfoTableManager_ELF_aarch64 &TLSInfoTableManager;
548const uint8_t TLSDescTableManager_ELF_aarch64::TLSDescEntryContent[16] = {
549 0x00, 0x00, 0x00, 0x00,
550 0x00, 0x00, 0x00, 0x00,
551 0x00, 0x00, 0x00, 0x00,
552 0x00, 0x00, 0x00, 0x00
560 TLSInfoTableManager_ELF_aarch64 TLSInfo;
561 TLSDescTableManager_ELF_aarch64 TLSDesc(TLSInfo);
574 dbgs() <<
"Building jitlink graph for new input "
580 return ELFObj.takeError();
582 auto Features = (*ELFObj)->getFeatures();
584 return Features.takeError();
587 "Only AArch64 (little endian) is supported for now");
589 auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj);
590 return ELFLinkGraphBuilder_aarch64<object::ELF64LE>(
591 (*ELFObj)->getFileName(), ELFObjFile.getELFFile(),
592 (*ELFObj)->makeTriple(), std::move(*Features))
597 std::unique_ptr<JITLinkContext> Ctx) {
599 const Triple &TT =
G->getTargetTriple();
600 if (Ctx->shouldAddDefaultTargetPasses(TT)) {
609 if (
auto MarkLive = Ctx->getMarkLivePass(TT))
610 Config.PrePrunePasses.push_back(std::move(MarkLive));
615 Config.PostPrunePasses.push_back(buildTables_ELF_aarch64);
618 if (
auto Err = Ctx->modifyPassConfig(*
G,
Config))
619 return Ctx->notifyFailed(std::move(Err));
621 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.
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.
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.
@ 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)
void visitEdge(LinkGraph &G, Block *B, Edge &E)
Base case for edge-visitors where the visitor-list is empty.
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
This is an optimization pass for GlobalISel generic memory operations.
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
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...