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 DefineExternalGOTSymbolIfPresent =
59 if (
auto *GOTSection =
G.findSectionByName(
62 return {*GOTSection, true};
69 if (
auto Err = DefineExternalGOTSymbolIfPresent(
G))
79 if (
auto *GOTSection =
83 for (
auto *
Sym : GOTSection->symbols())
94 Linkage::Strong, Scope::Local,
true);
98 Linkage::Strong, Scope::Local,
false,
true);
105 for (
auto *
Sym :
G.external_symbols()) {
121template <
typename ELFT>
156 using namespace aarch64;
158 case ELF::R_AARCH64_CALL26:
159 case ELF::R_AARCH64_JUMP26:
161 case ELF::R_AARCH64_LD_PREL_LO19:
163 case ELF::R_AARCH64_ADR_PREL_LO21:
165 case ELF::R_AARCH64_ADR_PREL_PG_HI21:
167 case ELF::R_AARCH64_ADD_ABS_LO12_NC:
169 case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
170 return ELFLdSt8Abs12;
171 case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
172 return ELFLdSt16Abs12;
173 case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
174 return ELFLdSt32Abs12;
175 case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
176 return ELFLdSt64Abs12;
177 case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
178 return ELFLdSt128Abs12;
179 case ELF::R_AARCH64_MOVW_UABS_G0_NC:
181 case ELF::R_AARCH64_MOVW_UABS_G1_NC:
183 case ELF::R_AARCH64_MOVW_UABS_G2_NC:
185 case ELF::R_AARCH64_MOVW_UABS_G3:
187 case ELF::R_AARCH64_TSTBR14:
189 case ELF::R_AARCH64_CONDBR19:
191 case ELF::R_AARCH64_ABS32:
193 case ELF::R_AARCH64_ABS64:
195 case ELF::R_AARCH64_PREL32:
197 case ELF::R_AARCH64_PREL64:
199 case ELF::R_AARCH64_ADR_GOT_PAGE:
200 return ELFAdrGOTPage21;
201 case ELF::R_AARCH64_LD64_GOT_LO12_NC:
202 return ELFLd64GOTLo12;
203 case ELF::R_AARCH64_LD64_GOTPAGE_LO15:
204 return ELFLd64GOTPAGELo15;
205 case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
206 return ELFTLSDescAdrPage21;
207 case ELF::R_AARCH64_TLSDESC_ADD_LO12:
208 return ELFTLSDescAddLo12;
209 case ELF::R_AARCH64_TLSDESC_LD64_LO12:
210 return ELFTLSDescLd64Lo12;
211 case ELF::R_AARCH64_TLSDESC_CALL:
212 return ELFTLSDescCall;
215 return make_error<JITLinkError>(
216 "Unsupported aarch64 relocation:" +
formatv(
"{0:d}: ",
Type) +
224 using Self = ELFLinkGraphBuilder_aarch64<ELFT>;
225 for (
const auto &RelSect : Base::Sections)
226 if (
Error Err = Base::forEachRelaRelocation(RelSect,
this,
227 &Self::addSingleRelocation))
233 Error addSingleRelocation(
const typename ELFT::Rela &Rel,
234 const typename ELFT::Shdr &FixupSect,
239 uint32_t SymbolIndex = Rel.getSymbol(
false);
240 auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
242 return ObjSymbol.takeError();
244 Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
246 return make_error<StringError>(
247 formatv(
"Could not find symbol at given index, did you add it to "
248 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
249 SymbolIndex, (*ObjSymbol)->st_shndx,
250 Base::GraphSymbols.size()),
258 int64_t Addend = Rel.r_addend;
269 switch (*RelocKind) {
277 return make_error<JITLinkError>(
278 "R_AARCH64_LDR_PREL_LO19 target is not an LDR Literal instruction");
286 return make_error<JITLinkError>(
287 "R_AARCH64_ADR_PREL_LO21 target is not an ADR instruction");
300 case ELFLdSt8Abs12: {
304 return make_error<JITLinkError>(
305 "R_AARCH64_LDST8_ABS_LO12_NC target is not a "
306 "LDRB/STRB (imm12) instruction");
311 case ELFLdSt16Abs12: {
315 return make_error<JITLinkError>(
316 "R_AARCH64_LDST16_ABS_LO12_NC target is not a "
317 "LDRH/STRH (imm12) instruction");
322 case ELFLdSt32Abs12: {
326 return make_error<JITLinkError>(
327 "R_AARCH64_LDST32_ABS_LO12_NC target is not a "
328 "LDR/STR (imm12, 32 bit) instruction");
333 case ELFLdSt64Abs12: {
337 return make_error<JITLinkError>(
338 "R_AARCH64_LDST64_ABS_LO12_NC target is not a "
339 "LDR/STR (imm12, 64 bit) instruction");
344 case ELFLdSt128Abs12: {
348 return make_error<JITLinkError>(
349 "R_AARCH64_LDST128_ABS_LO12_NC target is not a "
350 "LDR/STR (imm12, 128 bit) instruction");
359 return make_error<JITLinkError>(
360 "R_AARCH64_MOVW_UABS_G0_NC target is not a "
361 "MOVK/MOVZ (imm16, LSL #0) instruction");
370 return make_error<JITLinkError>(
371 "R_AARCH64_MOVW_UABS_G1_NC target is not a "
372 "MOVK/MOVZ (imm16, LSL #16) instruction");
381 return make_error<JITLinkError>(
382 "R_AARCH64_MOVW_UABS_G2_NC target is not a "
383 "MOVK/MOVZ (imm16, LSL #32) instruction");
392 return make_error<JITLinkError>(
393 "R_AARCH64_MOVW_UABS_G3 target is not a "
394 "MOVK/MOVZ (imm16, LSL #48) instruction");
402 return make_error<JITLinkError>(
"R_AARCH64_TSTBR14 target is not a "
403 "test and branch instruction");
412 return make_error<JITLinkError>(
"R_AARCH64_CONDBR19 target is not a "
413 "conditional branch instruction");
434 case ELFAdrGOTPage21: {
438 case ELFLd64GOTLo12: {
442 case ELFLd64GOTPAGELo15: {
446 case ELFTLSDescAdrPage21: {
450 case ELFTLSDescAddLo12:
451 case ELFTLSDescLd64Lo12: {
455 case ELFTLSDescCall: {
467 BlockToFix.
addEdge(std::move(GE));
473 const char *getELFAArch64RelocationKindName(
Edge::Kind R) {
478 return "ELFAdrPage21";
480 return "ELFAddAbs12";
482 return "ELFLdSt8Abs12";
484 return "ELFLdSt16Abs12";
486 return "ELFLdSt32Abs12";
488 return "ELFLdSt64Abs12";
489 case ELFLdSt128Abs12:
490 return "ELFLdSt128Abs12";
492 return "ELFMovwAbsG0";
494 return "ELFMovwAbsG1";
496 return "ELFMovwAbsG2";
498 return "ELFMovwAbsG3";
507 case ELFAdrGOTPage21:
508 return "ELFAdrGOTPage21";
510 return "ELFLd64GOTLo12";
511 case ELFLd64GOTPAGELo15:
512 return "ELFLd64GOTPAGELo15";
513 case ELFTLSDescAdrPage21:
514 return "ELFTLSDescAdrPage21";
515 case ELFTLSDescAddLo12:
516 return "ELFTLSDescAddLo12";
517 case ELFTLSDescLd64Lo12:
518 return "ELFTLSDescLd64Lo12";
520 return "ELFTLSDescCall";
527 ELFLinkGraphBuilder_aarch64(
StringRef FileName,
535class TLSInfoTableManager_ELF_aarch64
536 :
public TableManager<TLSInfoTableManager_ELF_aarch64> {
540 static const uint8_t TLSInfoEntryContent[16];
547 auto &TLSInfoEntry =
G.createMutableContentBlock(
548 getTLSInfoSection(
G),
G.allocateContent(getTLSInfoEntryContent()),
551 return G.addAnonymousSymbol(TLSInfoEntry, 0, 16,
false,
false);
558 return *TLSInfoTable;
562 return {
reinterpret_cast<const char *
>(TLSInfoEntryContent),
563 sizeof(TLSInfoEntryContent)};
566 Section *TLSInfoTable =
nullptr;
569const uint8_t TLSInfoTableManager_ELF_aarch64::TLSInfoEntryContent[16] = {
570 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
571 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
575class TLSDescTableManager_ELF_aarch64
576 :
public TableManager<TLSDescTableManager_ELF_aarch64> {
578 TLSDescTableManager_ELF_aarch64(
579 TLSInfoTableManager_ELF_aarch64 &TLSInfoTableManager)
580 : TLSInfoTableManager(TLSInfoTableManager) {}
584 static const uint8_t TLSDescEntryContent[16];
600 assert(KindToSet != Edge::Invalid &&
601 "Fell through switch, but no new kind to set");
603 dbgs() <<
" Fixing " <<
G.getEdgeKindName(E.
getKind()) <<
" edge at "
604 <<
B->getFixupAddress(E) <<
" (" <<
B->getAddress() <<
" + "
614 G.createContentBlock(getTLSDescSection(
G), getTLSDescBlockContent(),
618 TLSInfoTableManager.getEntryForTarget(
G,
Target), 0);
619 return G.addAnonymousSymbol(EntryBlock, 0, 8,
false,
false);
630 if (!TLSDescResolver)
631 TLSDescResolver = &
G.addExternalSymbol(
"__tlsdesc_resolver", 8,
false);
632 return *TLSDescResolver;
636 return {
reinterpret_cast<const char *
>(TLSDescEntryContent),
637 sizeof(TLSDescEntryContent)};
641 Symbol *TLSDescResolver =
nullptr;
642 TLSInfoTableManager_ELF_aarch64 &TLSInfoTableManager;
645const uint8_t TLSDescTableManager_ELF_aarch64::TLSDescEntryContent[16] = {
646 0x00, 0x00, 0x00, 0x00,
647 0x00, 0x00, 0x00, 0x00,
648 0x00, 0x00, 0x00, 0x00,
649 0x00, 0x00, 0x00, 0x00
657 TLSInfoTableManager_ELF_aarch64 TLSInfo;
658 TLSDescTableManager_ELF_aarch64 TLSDesc(TLSInfo);
671 dbgs() <<
"Building jitlink graph for new input "
677 return ELFObj.takeError();
679 auto Features = (*ELFObj)->getFeatures();
681 return Features.takeError();
684 "Only AArch64 (little endian) is supported for now");
686 auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj);
687 return ELFLinkGraphBuilder_aarch64<object::ELF64LE>(
688 (*ELFObj)->getFileName(), ELFObjFile.getELFFile(),
689 (*ELFObj)->makeTriple(), std::move(*Features))
694 std::unique_ptr<JITLinkContext> Ctx) {
696 const Triple &TT =
G->getTargetTriple();
697 if (Ctx->shouldAddDefaultTargetPasses(TT)) {
706 if (
auto MarkLive = Ctx->getMarkLivePass(TT))
707 Config.PrePrunePasses.push_back(std::move(MarkLive));
712 Config.PostAllocationPasses.push_back(
717 Config.PostPrunePasses.push_back(buildTables_ELF_aarch64);
720 if (
auto Err = Ctx->modifyPassConfig(*
G,
Config))
721 return Ctx->notifyFailed(std::move(Err));
723 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.
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.
StringRef 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.
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)
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(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.
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.