LLVM: lib/ExecutionEngine/JITLink/ELF_ppc64.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
18
22
23#define DEBUG_TYPE "jitlink"
24
25namespace {
26
27using namespace llvm;
29
30constexpr StringRef ELFTOCSymbolName = ".TOC.";
31constexpr StringRef TOCSymbolAliasIdent = "__TOC__";
32constexpr uint64_t ELFTOCBaseOffset = 0x8000;
33constexpr StringRef ELFTLSInfoSectionName = "$__TLSINFO";
34
35template <llvm::endianness Endianness>
36class TLSInfoTableManager_ELF_ppc64
37 : public TableManager<TLSInfoTableManager_ELF_ppc64> {
38public:
39 static const uint8_t TLSInfoEntryContent[16];
40
42
44 Edge::Kind K = E.getKind();
45 switch (K) {
48 E.setTarget(this->getEntryForTarget(G, E.getTarget()));
49 return true;
52 E.setTarget(this->getEntryForTarget(G, E.getTarget()));
53 return true;
56 E.setTarget(this->getEntryForTarget(G, E.getTarget()));
57 return true;
58 default:
59 return false;
60 }
61 }
62
64
65
66 auto &TLSInfoEntry = G.createMutableContentBlock(
67 getTLSInfoSection(G), G.allocateContent(getTLSInfoEntryContent()),
70 return G.addAnonymousSymbol(TLSInfoEntry, 0, 16, false, false);
71 }
72
73private:
75 if (!TLSInfoTable)
76 TLSInfoTable =
78 return *TLSInfoTable;
79 }
80
82 return {reinterpret_cast<const char *>(TLSInfoEntryContent),
83 sizeof(TLSInfoEntryContent)};
84 }
85
86 Section *TLSInfoTable = nullptr;
87};
88
89template <>
90const uint8_t TLSInfoTableManager_ELF_ppc64<
92 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
94};
95
96template <>
97const uint8_t TLSInfoTableManager_ELF_ppc64<
99 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
101};
102
103template <llvm::endianness Endianness>
106 Symbol *TOCSymbol = nullptr;
107
108 for (Symbol *Sym : G.defined_symbols())
109 if (LLVM_UNLIKELY(Sym->hasName() && *Sym->getName() == ELFTOCSymbolName)) {
110 TOCSymbol = Sym;
111 break;
112 }
113
115 for (Symbol *Sym : G.external_symbols())
116 if (Sym->hasName() && *Sym->getName() == ELFTOCSymbolName) {
117 TOCSymbol = Sym;
118 break;
119 }
120 }
121
122 if (!TOCSymbol)
123 TOCSymbol = &G.addExternalSymbol(ELFTOCSymbolName, 0, false);
124
125 return TOC.getEntryForTarget(G, *TOCSymbol);
126}
127
128
129template <llvm::endianness Endianness>
130inline void
131registerExistingGOTEntries(LinkGraph &G,
133 auto isGOTEntry = [](const Edge &E) {
134 return E.getKind() == ppc64::Pointer64 && E.getTarget().isExternal();
135 };
136 if (Section *dotTOCSection = G.findSectionByName(".toc")) {
137 for (Block *B : dotTOCSection->blocks())
138 for (Edge &E : B->edges())
139 if (isGOTEntry(E))
140 TOC.registerPreExistingEntry(E.getTarget(),
141 G.addAnonymousSymbol(*B, E.getOffset(),
142 G.getPointerSize(),
143 false, false));
144 }
145}
146
147template <llvm::endianness Endianness>
151
152
153
154
155
156 createELFGOTHeader(G, TOC);
157
158
159 registerExistingGOTEntries(G, TOC);
160
162 TLSInfoTableManager_ELF_ppc64 TLSInfo;
164
165
166
167
168
169
170 if (Section *TOCSection = G.findSectionByName(TOC.getSectionName())) {
171
172
173 if (Section *gotSection = G.findSectionByName(".got"))
174 G.mergeSections(*TOCSection, *gotSection);
175 if (Section *tocSection = G.findSectionByName(".toc"))
176 G.mergeSections(*TOCSection, *tocSection);
177 if (Section *sdataSection = G.findSectionByName(".sdata"))
178 G.mergeSections(*TOCSection, *sdataSection);
179 if (Section *sbssSection = G.findSectionByName(".sbss"))
180 G.mergeSections(*TOCSection, *sbssSection);
181
182
183 if (Section *tocbssSection = G.findSectionByName(".tocbss"))
184 G.mergeSections(*TOCSection, *tocbssSection);
185 if (Section *pltSection = G.findSectionByName(".plt"))
186 G.mergeSections(*TOCSection, *pltSection);
187 }
188
190}
191
192}
193
195
196template <llvm::endianness Endianness>
199private:
202
203 using Base::G;
204
207
210
213 G->getTargetTriple().getArchName() +
214 " ELF object files",
216
218 &Self::addSingleRelocation))
219 return Err;
220 }
221
223 }
224
225 Error addSingleRelocation(const typename ELFT::Rela &Rel,
226 const typename ELFT::Shdr &FixupSection,
227 Block &BlockToFix) {
229 auto ELFReloc = Rel.getType(false);
230
231
232 if (LLVM_UNLIKELY(ELFReloc == ELF::R_PPC64_NONE))
234
235
236 if (ELFReloc == ELF::R_PPC64_TLSGD)
238 if (ELFReloc == ELF::R_PPC64_TLSLD)
241
242 if (ELFReloc == ELF::R_PPC64_PCREL_OPT)
243
245
246 if (ELFReloc == ELF::R_PPC64_TPREL34)
249
251 if (!ObjSymbol)
252 return ObjSymbol.takeError();
253
254 uint32_t SymbolIndex = Rel.getSymbol(false);
256 if (!GraphSymbol)
258 formatv("Could not find symbol at given index, did you add it to "
259 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
260 SymbolIndex, (*ObjSymbol)->st_shndx,
263
264 int64_t Addend = Rel.r_addend;
267 Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
268 Edge::Kind Kind = Edge::Invalid;
269
270 switch (ELFReloc) {
271 default:
273 "In " + G->getName() + ": Unsupported ppc64 relocation type " +
275 case ELF::R_PPC64_ADDR64:
277 break;
278 case ELF::R_PPC64_ADDR32:
280 break;
281 case ELF::R_PPC64_ADDR16:
283 break;
284 case ELF::R_PPC64_ADDR16_DS:
286 break;
287 case ELF::R_PPC64_ADDR16_HA:
289 break;
290 case ELF::R_PPC64_ADDR16_HI:
292 break;
293 case ELF::R_PPC64_ADDR16_HIGH:
295 break;
296 case ELF::R_PPC64_ADDR16_HIGHA:
298 break;
299 case ELF::R_PPC64_ADDR16_HIGHER:
301 break;
302 case ELF::R_PPC64_ADDR16_HIGHERA:
304 break;
305 case ELF::R_PPC64_ADDR16_HIGHEST:
307 break;
308 case ELF::R_PPC64_ADDR16_HIGHESTA:
310 break;
311 case ELF::R_PPC64_ADDR16_LO:
313 break;
314 case ELF::R_PPC64_ADDR16_LO_DS:
316 break;
317 case ELF::R_PPC64_ADDR14:
319 break;
320 case ELF::R_PPC64_TOC:
322 break;
323 case ELF::R_PPC64_TOC16:
325 break;
326 case ELF::R_PPC64_TOC16_HA:
328 break;
329 case ELF::R_PPC64_TOC16_HI:
331 break;
332 case ELF::R_PPC64_TOC16_DS:
334 break;
335 case ELF::R_PPC64_TOC16_LO:
337 break;
338 case ELF::R_PPC64_TOC16_LO_DS:
340 break;
341 case ELF::R_PPC64_REL16:
343 break;
344 case ELF::R_PPC64_REL16_HA:
346 break;
347 case ELF::R_PPC64_REL16_HI:
349 break;
350 case ELF::R_PPC64_REL16_LO:
352 break;
353 case ELF::R_PPC64_REL32:
355 break;
356 case ELF::R_PPC64_REL24_NOTOC:
358 break;
359 case ELF::R_PPC64_REL24:
361
362
363
364
365
366
368 break;
369 case ELF::R_PPC64_REL64:
371 break;
372 case ELF::R_PPC64_PCREL34:
374 break;
375 case ELF::R_PPC64_GOT_PCREL34:
377 break;
378 case ELF::R_PPC64_GOT_TLSGD16_HA:
380 break;
381 case ELF::R_PPC64_GOT_TLSGD16_LO:
383 break;
384 case ELF::R_PPC64_GOT_TLSGD_PCREL34:
386 break;
387 }
388
389 Edge GE(Kind, Offset, *GraphSymbol, Addend);
390 BlockToFix.addEdge(std::move(GE));
392 }
393
394public:
397 std::shared_ptrorc::SymbolStringPool SSP,
400 std::move(Features), FileName,
401 ppc64::getEdgeKindName) {}
402};
403
404template <llvm::endianness Endianness>
407 friend JITLinkerBase;
408
409public:
414 [this](LinkGraph &G) { return defineTOCBase(G); });
415 }
416
417private:
418 Symbol *TOCSymbol = nullptr;
419
421 for (Symbol *Sym : G.defined_symbols()) {
423 *Sym->getName() == ELFTOCSymbolName)) {
424 TOCSymbol = Sym;
426 }
427 }
428
429 assert(TOCSymbol == nullptr &&
430 "TOCSymbol should not be defined at this point");
431
432 for (Symbol *Sym : G.external_symbols()) {
433 if (Sym->hasName() && *Sym->getName() == ELFTOCSymbolName) {
434 TOCSymbol = Sym;
435 break;
436 }
437 }
438
439 if (Section *TOCSection = G.findSectionByName(
441 assert(!TOCSection->empty() && "TOC section should have reserved an "
442 "entry for containing the TOC base");
443
444 SectionRange SR(*TOCSection);
445 orc::ExecutorAddr TOCBaseAddr(SR.getFirstBlock()->getAddress() +
446 ELFTOCBaseOffset);
447 assert(TOCSymbol && TOCSymbol->isExternal() &&
448 ".TOC. should be a external symbol at this point");
449 G.makeAbsolute(*TOCSymbol, TOCBaseAddr);
450
451 G.addAbsoluteSymbol(TOCSymbolAliasIdent, TOCSymbol->getAddress(),
452 TOCSymbol->getSize(), TOCSymbol->getLinkage(),
453 TOCSymbol->getScope(), TOCSymbol->isLive());
455 }
456
457
458
460 }
461
462 Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
464 }
465};
466
467template <llvm::endianness Endianness>
468Expected<std::unique_ptr>
470 std::shared_ptrorc::SymbolStringPool SSP) {
472 dbgs() << "Building jitlink graph for new input "
474 });
475
477 if (!ELFObj)
478 return ELFObj.takeError();
479
480 auto Features = (*ELFObj)->getFeatures();
481 if (!Features)
482 return Features.takeError();
483
487 (*ELFObj)->getFileName(), ELFObjFile.getELFFile(), std::move(SSP),
488 (*ELFObj)->makeTriple(), std::move(*Features))
490}
491
492template <llvm::endianness Endianness>
494 std::unique_ptr Ctx) {
496
497 if (Ctx->shouldAddDefaultTargetPasses(G->getTargetTriple())) {
498
499
500
506
507
508 if (auto MarkLive = Ctx->getMarkLivePass(G->getTargetTriple()))
509 Config.PrePrunePasses.push_back(std::move(MarkLive));
510 else
512 }
513
514 Config.PostPrunePasses.push_back(buildTables_ELF_ppc64);
515
516 if (auto Err = Ctx->modifyPassConfig(*G, Config))
517 return Ctx->notifyFailed(std::move(Err));
518
520 std::move(Config));
521}
522
525 std::shared_ptrorc::SymbolStringPool SSP) {
527 std::move(ObjectBuffer), std::move(SSP));
528}
529
531 MemoryBufferRef ObjectBuffer, std::shared_ptrorc::SymbolStringPool SSP) {
533 std::move(ObjectBuffer), std::move(SSP));
534}
535
536
538 std::unique_ptr Ctx) {
540}
541
542
544 std::unique_ptr Ctx) {
546}
547
548}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_UNLIKELY(EXPR)
#define LLVM_LIKELY(EXPR)
std::pair< BasicBlock *, BasicBlock * > Edge
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.
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.
orc::ExecutorAddr getAddress() const
An Addressable with content and edges.
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.
ELFJITLinker_ppc64(std::unique_ptr< JITLinkContext > Ctx, std::unique_ptr< LinkGraph > G, PassConfiguration PassConfig)
Definition ELF_ppc64.cpp:410
std::unique_ptr< LinkGraph > G
Definition ELF_ppc64.cpp:198
ELFLinkGraphBuilder_ppc64(StringRef FileName, const object::ELFFile< ELFT > &Obj, std::shared_ptr< orc::SymbolStringPool > SSP, Triple TT, SubtargetFeatures Features)
Definition ELF_ppc64.cpp:395
const ELFFile::Elf_Shdr * SymTabSec
virtual Error addRelocations()=0
ELFLinkGraphBuilder(const object::ELFFile< object::ELFType< Endianness, true > > &Obj, std::shared_ptr< orc::SymbolStringPool > SSP, Triple TT, SubtargetFeatures Features, StringRef FileName, LinkGraph::GetEdgeKindNameFunction GetEdgeKindName)
Expected< std::unique_ptr< LinkGraph > > buildGraph()
Attempt to construct and return the LinkGraph.
DenseMap< ELFSymbolIndex, Symbol * > GraphSymbols
ELFFile::Elf_Shdr_Range Sections
Error forEachRelaRelocation(const typename ELFT::Shdr &RelSect, RelocHandlerMethod &&Func)
Traverse all matching ELFT::Rela relocation records in the given section.
Symbol * getGraphSymbol(ELFSymbolIndex SymIndex)
Represents fixups and constraints in the LinkGraph.
PassConfiguration & getPassConfig()
static void link(ArgTs &&... Args)
Represents an object file section.
A CRTP base for tables that are built on demand, e.g.
static StringRef getSectionName()
static Expected< std::unique_ptr< ObjectFile > > createELFObjectFile(MemoryBufferRef Object, bool InitContent=true)
Represents an address in the executor process.
static int64_t decodePPC64LocalEntryOffset(unsigned Other)
static constexpr const StringLiteral & getSectionName(DebugSectionKind SectionKind)
Return the name of the section.
Error applyFixup(LinkGraph &G, Block &B, const Edge &E, const Symbol *TOCSymbol)
Apply fixup expression for edge to block content.
@ RequestTLSDescInGOTAndTransformToTOCDelta16HA
@ RequestGOTAndTransformToDelta34
@ RequestTLSDescInGOTAndTransformToTOCDelta16LO
@ RequestTLSDescInGOTAndTransformToDelta34
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...
void link_ELF_ppc64le(std::unique_ptr< LinkGraph > G, std::unique_ptr< JITLinkContext > Ctx)
jit-link the given object buffer, which must be a ELF ppc64le object file.
Definition ELF_ppc64.cpp:543
LLVM_ABI Error markAllSymbolsLive(LinkGraph &G)
Marks all symbols in a graph live.
void link_ELF_ppc64(std::unique_ptr< LinkGraph > G, std::unique_ptr< JITLinkContext > Ctx)
jit-link the given object buffer, which must be a ELF ppc64le object file.
Definition ELF_ppc64.cpp:537
Expected< std::unique_ptr< LinkGraph > > createLinkGraphFromELFObject_ppc64le(MemoryBufferRef ObjectBuffer, std::shared_ptr< orc::SymbolStringPool > SSP)
Create a LinkGraph from an ELF/ppc64le relocatable object.
Definition ELF_ppc64.cpp:530
void visitEdge(LinkGraph &G, Block *B, Edge &E)
Base case for edge-visitors where the visitor-list is empty.
Expected< std::unique_ptr< LinkGraph > > createLinkGraphFromELFObject_ppc64(MemoryBufferRef ObjectBuffer, std::shared_ptr< orc::SymbolStringPool > SSP)
Create a LinkGraph from an ELF/ppc64 relocatable object.
Definition ELF_ppc64.cpp:524
LLVM_ABI StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI 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)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
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.
LinkGraphPassList PostPrunePasses
Post-prune passes.
LinkGraphPassList PrePrunePasses
Pre-prune passes.
Elf_Rel_Impl< ELFType< E, Is64 >, true > Rela
Elf_Shdr_Impl< ELFType< E, Is64 > > Shdr