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>
80 using namespace aarch64;
82 case ELF::R_AARCH64_CALL26:
83 case ELF::R_AARCH64_JUMP26:
85 case ELF::R_AARCH64_ADR_PREL_LO21:
87 case ELF::R_AARCH64_ADR_PREL_PG_HI21:
89 case ELF::R_AARCH64_ADD_ABS_LO12_NC:
91 case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
93 case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
94 return ELFLdSt16Abs12;
95 case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
96 return ELFLdSt32Abs12;
97 case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
98 return ELFLdSt64Abs12;
99 case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
100 return ELFLdSt128Abs12;
101 case ELF::R_AARCH64_MOVW_UABS_G0_NC:
103 case ELF::R_AARCH64_MOVW_UABS_G1_NC:
105 case ELF::R_AARCH64_MOVW_UABS_G2_NC:
107 case ELF::R_AARCH64_MOVW_UABS_G3:
109 case ELF::R_AARCH64_TSTBR14:
111 case ELF::R_AARCH64_CONDBR19:
113 case ELF::R_AARCH64_ABS32:
115 case ELF::R_AARCH64_ABS64:
117 case ELF::R_AARCH64_PREL32:
119 case ELF::R_AARCH64_PREL64:
121 case ELF::R_AARCH64_ADR_GOT_PAGE:
122 return ELFAdrGOTPage21;
123 case ELF::R_AARCH64_LD64_GOT_LO12_NC:
124 return ELFLd64GOTLo12;
125 case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
126 return ELFTLSDescAdrPage21;
127 case ELF::R_AARCH64_TLSDESC_ADD_LO12:
128 return ELFTLSDescAddLo12;
129 case ELF::R_AARCH64_TLSDESC_LD64_LO12:
130 return ELFTLSDescLd64Lo12;
131 case ELF::R_AARCH64_TLSDESC_CALL:
132 return ELFTLSDescCall;
135 return make_error<JITLinkError>(
136 "Unsupported aarch64 relocation:" +
formatv(
"{0:d}: ",
Type) +
144 using Self = ELFLinkGraphBuilder_aarch64<ELFT>;
145 for (
const auto &RelSect : Base::Sections)
146 if (
Error Err = Base::forEachRelaRelocation(RelSect,
this,
147 &Self::addSingleRelocation))
153 Error addSingleRelocation(
const typename ELFT::Rela &Rel,
154 const typename ELFT::Shdr &FixupSect,
159 uint32_t SymbolIndex = Rel.getSymbol(
false);
160 auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
162 return ObjSymbol.takeError();
164 Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
166 return make_error<StringError>(
167 formatv(
"Could not find symbol at given index, did you add it to "
168 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
169 SymbolIndex, (*ObjSymbol)->st_shndx,
170 Base::GraphSymbols.size()),
178 int64_t Addend = Rel.r_addend;
189 switch (*RelocKind) {
195 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
197 return make_error<JITLinkError>(
198 "R_AARCH64_ADR_PREL_LO21 target is not an ADR instruction");
211 case ELFLdSt8Abs12: {
212 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
215 return make_error<JITLinkError>(
216 "R_AARCH64_LDST8_ABS_LO12_NC target is not a "
217 "LDRB/STRB (imm12) instruction");
222 case ELFLdSt16Abs12: {
223 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
226 return make_error<JITLinkError>(
227 "R_AARCH64_LDST16_ABS_LO12_NC target is not a "
228 "LDRH/STRH (imm12) instruction");
233 case ELFLdSt32Abs12: {
234 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
237 return make_error<JITLinkError>(
238 "R_AARCH64_LDST32_ABS_LO12_NC target is not a "
239 "LDR/STR (imm12, 32 bit) instruction");
244 case ELFLdSt64Abs12: {
245 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
248 return make_error<JITLinkError>(
249 "R_AARCH64_LDST64_ABS_LO12_NC target is not a "
250 "LDR/STR (imm12, 64 bit) instruction");
255 case ELFLdSt128Abs12: {
256 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
259 return make_error<JITLinkError>(
260 "R_AARCH64_LDST128_ABS_LO12_NC target is not a "
261 "LDR/STR (imm12, 128 bit) instruction");
267 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
270 return make_error<JITLinkError>(
271 "R_AARCH64_MOVW_UABS_G0_NC target is not a "
272 "MOVK/MOVZ (imm16, LSL #0) instruction");
278 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
281 return make_error<JITLinkError>(
282 "R_AARCH64_MOVW_UABS_G1_NC target is not a "
283 "MOVK/MOVZ (imm16, LSL #16) instruction");
289 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
292 return make_error<JITLinkError>(
293 "R_AARCH64_MOVW_UABS_G2_NC target is not a "
294 "MOVK/MOVZ (imm16, LSL #32) instruction");
300 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
303 return make_error<JITLinkError>(
304 "R_AARCH64_MOVW_UABS_G3 target is not a "
305 "MOVK/MOVZ (imm16, LSL #48) instruction");
311 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
313 return make_error<JITLinkError>(
"R_AARCH64_TSTBR14 target is not a "
314 "test and branch instruction");
320 uint32_t Instr = *(
const ulittle32_t *)FixupContent;
323 return make_error<JITLinkError>(
"R_AARCH64_CONDBR19 target is not a "
324 "conditional branch instruction");
345 case ELFAdrGOTPage21: {
349 case ELFLd64GOTLo12: {
353 case ELFTLSDescAdrPage21: {
357 case ELFTLSDescAddLo12:
358 case ELFTLSDescLd64Lo12: {
362 case ELFTLSDescCall: {
374 BlockToFix.
addEdge(std::move(GE));
380 const char *getELFAArch64RelocationKindName(
Edge::Kind R) {
385 return "ELFAdrPage21";
387 return "ELFAddAbs12";
389 return "ELFLdSt8Abs12";
391 return "ELFLdSt16Abs12";
393 return "ELFLdSt32Abs12";
395 return "ELFLdSt64Abs12";
396 case ELFLdSt128Abs12:
397 return "ELFLdSt128Abs12";
399 return "ELFMovwAbsG0";
401 return "ELFMovwAbsG1";
403 return "ELFMovwAbsG2";
405 return "ELFMovwAbsG3";
414 case ELFAdrGOTPage21:
415 return "ELFAdrGOTPage21";
417 return "ELFLd64GOTLo12";
418 case ELFTLSDescAdrPage21:
419 return "ELFTLSDescAdrPage21";
420 case ELFTLSDescAddLo12:
421 return "ELFTLSDescAddLo12";
422 case ELFTLSDescLd64Lo12:
423 return "ELFTLSDescLd64Lo12";
425 return "ELFTLSDescCall";
432 ELFLinkGraphBuilder_aarch64(
StringRef FileName,
440class TLSInfoTableManager_ELF_aarch64
441 :
public TableManager<TLSInfoTableManager_ELF_aarch64> {
445 static const uint8_t TLSInfoEntryContent[16];
452 auto &TLSInfoEntry =
G.createMutableContentBlock(
453 getTLSInfoSection(
G),
G.allocateContent(getTLSInfoEntryContent()),
456 return G.addAnonymousSymbol(TLSInfoEntry, 0, 16,
false,
false);
463 return *TLSInfoTable;
467 return {
reinterpret_cast<const char *
>(TLSInfoEntryContent),
468 sizeof(TLSInfoEntryContent)};
471 Section *TLSInfoTable =
nullptr;
474const uint8_t TLSInfoTableManager_ELF_aarch64::TLSInfoEntryContent[16] = {
475 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
476 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
480class TLSDescTableManager_ELF_aarch64
481 :
public TableManager<TLSDescTableManager_ELF_aarch64> {
483 TLSDescTableManager_ELF_aarch64(
484 TLSInfoTableManager_ELF_aarch64 &TLSInfoTableManager)
485 : TLSInfoTableManager(TLSInfoTableManager) {}
489 static const uint8_t TLSDescEntryContent[16];
505 assert(KindToSet != Edge::Invalid &&
506 "Fell through switch, but no new kind to set");
508 dbgs() <<
" Fixing " <<
G.getEdgeKindName(E.
getKind()) <<
" edge at "
509 <<
B->getFixupAddress(E) <<
" (" <<
B->getAddress() <<
" + "
519 G.createContentBlock(getTLSDescSection(
G), getTLSDescBlockContent(),
523 TLSInfoTableManager.getEntryForTarget(
G,
Target), 0);
524 return G.addAnonymousSymbol(EntryBlock, 0, 8,
false,
false);
535 if (!TLSDescResolver)
536 TLSDescResolver = &
G.addExternalSymbol(
"__tlsdesc_resolver", 8,
false);
537 return *TLSDescResolver;
541 return {
reinterpret_cast<const char *
>(TLSDescEntryContent),
542 sizeof(TLSDescEntryContent)};
546 Symbol *TLSDescResolver =
nullptr;
547 TLSInfoTableManager_ELF_aarch64 &TLSInfoTableManager;
550const uint8_t TLSDescTableManager_ELF_aarch64::TLSDescEntryContent[16] = {
551 0x00, 0x00, 0x00, 0x00,
552 0x00, 0x00, 0x00, 0x00,
553 0x00, 0x00, 0x00, 0x00,
554 0x00, 0x00, 0x00, 0x00
562 TLSInfoTableManager_ELF_aarch64 TLSInfo;
563 TLSDescTableManager_ELF_aarch64 TLSDesc(TLSInfo);
576 dbgs() <<
"Building jitlink graph for new input "
582 return ELFObj.takeError();
584 auto Features = (*ELFObj)->getFeatures();
586 return Features.takeError();
589 "Only AArch64 (little endian) is supported for now");
591 auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj);
592 return ELFLinkGraphBuilder_aarch64<object::ELF64LE>(
593 (*ELFObj)->getFileName(), ELFObjFile.getELFFile(),
594 (*ELFObj)->makeTriple(), std::move(*Features))
599 std::unique_ptr<JITLinkContext> Ctx) {
601 const Triple &TT =
G->getTargetTriple();
602 if (Ctx->shouldAddDefaultTargetPasses(TT)) {
611 if (
auto MarkLive = Ctx->getMarkLivePass(TT))
612 Config.PrePrunePasses.push_back(std::move(MarkLive));
617 Config.PostAllocationPasses.push_back(
622 Config.PostPrunePasses.push_back(buildTables_ELF_aarch64);
625 if (
auto Err = Ctx->modifyPassConfig(*
G,
Config))
626 return Ctx->notifyFailed(std::move(Err));
628 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 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)
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...