xref: /llvm-project/llvm/lib/ExecutionEngine/JITLink/ELF_ppc64.cpp (revision 9c38a178d3a61b0d9d84c564e8b6dba0f90dad4e)
1 //===------- ELF_ppc64.cpp -JIT linker implementation for ELF/ppc64 -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // ELF/ppc64 jit-link implementation.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ExecutionEngine/JITLink/ELF_ppc64.h"
14 #include "llvm/ExecutionEngine/JITLink/DWARFRecordSectionSplitter.h"
15 #include "llvm/ExecutionEngine/JITLink/TableManager.h"
16 #include "llvm/ExecutionEngine/JITLink/ppc64.h"
17 #include "llvm/Object/ELFObjectFile.h"
18 #include "llvm/Support/Endian.h"
19 
20 #include "EHFrameSupportImpl.h"
21 #include "ELFLinkGraphBuilder.h"
22 #include "JITLinkGeneric.h"
23 
24 #define DEBUG_TYPE "jitlink"
25 
26 namespace {
27 
28 using namespace llvm;
29 using namespace llvm::jitlink;
30 
31 constexpr StringRef ELFTOCSymbolName = ".TOC.";
32 constexpr StringRef TOCSymbolAliasIdent = "__TOC__";
33 constexpr uint64_t ELFTOCBaseOffset = 0x8000;
34 constexpr StringRef ELFTLSInfoSectionName = "$__TLSINFO";
35 
36 template <support::endianness Endianness>
37 class TLSInfoTableManager_ELF_ppc64
38     : public TableManager<TLSInfoTableManager_ELF_ppc64<Endianness>> {
39 public:
40   static const uint8_t TLSInfoEntryContent[16];
41 
42   static StringRef getSectionName() { return ELFTLSInfoSectionName; }
43 
44   bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
45     Edge::Kind K = E.getKind();
46     if (K == ppc64::RequestTLSDescInGOTAndTransformToTOCDelta16HA) {
47       E.setKind(ppc64::TOCDelta16HA);
48       E.setTarget(this->getEntryForTarget(G, E.getTarget()));
49       return true;
50     }
51     if (K == ppc64::RequestTLSDescInGOTAndTransformToTOCDelta16LO) {
52       E.setKind(ppc64::TOCDelta16LO);
53       E.setTarget(this->getEntryForTarget(G, E.getTarget()));
54       return true;
55     }
56     return false;
57   }
58 
59   Symbol &createEntry(LinkGraph &G, Symbol &Target) {
60     // The TLS Info entry's key value will be written by
61     // `fixTLVSectionsAndEdges`, so create mutable content.
62     auto &TLSInfoEntry = G.createMutableContentBlock(
63         getTLSInfoSection(G), G.allocateContent(getTLSInfoEntryContent()),
64         orc::ExecutorAddr(), 8, 0);
65     TLSInfoEntry.addEdge(ppc64::Pointer64, 8, Target, 0);
66     return G.addAnonymousSymbol(TLSInfoEntry, 0, 16, false, false);
67   }
68 
69 private:
70   Section &getTLSInfoSection(LinkGraph &G) {
71     if (!TLSInfoTable)
72       TLSInfoTable =
73           &G.createSection(ELFTLSInfoSectionName, orc::MemProt::Read);
74     return *TLSInfoTable;
75   }
76 
77   ArrayRef<char> getTLSInfoEntryContent() const {
78     return {reinterpret_cast<const char *>(TLSInfoEntryContent),
79             sizeof(TLSInfoEntryContent)};
80   }
81 
82   Section *TLSInfoTable = nullptr;
83 };
84 
85 template <>
86 const uint8_t TLSInfoTableManager_ELF_ppc64<
87     support::endianness::little>::TLSInfoEntryContent[16] = {
88     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*pthread key */
89     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  /*data address*/
90 };
91 
92 template <>
93 const uint8_t TLSInfoTableManager_ELF_ppc64<
94     support::endianness::big>::TLSInfoEntryContent[16] = {
95     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*pthread key */
96     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  /*data address*/
97 };
98 
99 template <support::endianness Endianness>
100 Symbol &createELFGOTHeader(LinkGraph &G,
101                            ppc64::TOCTableManager<Endianness> &TOC) {
102   Symbol *TOCSymbol = nullptr;
103 
104   for (Symbol *Sym : G.defined_symbols())
105     if (LLVM_UNLIKELY(Sym->getName() == ELFTOCSymbolName)) {
106       TOCSymbol = Sym;
107       break;
108     }
109 
110   if (LLVM_LIKELY(TOCSymbol == nullptr)) {
111     for (Symbol *Sym : G.external_symbols())
112       if (Sym->getName() == ELFTOCSymbolName) {
113         TOCSymbol = Sym;
114         break;
115       }
116   }
117 
118   if (!TOCSymbol)
119     TOCSymbol = &G.addExternalSymbol(ELFTOCSymbolName, 0, false);
120 
121   return TOC.getEntryForTarget(G, *TOCSymbol);
122 }
123 
124 // Register preexisting GOT entries with TOC table manager.
125 template <support::endianness Endianness>
126 inline void
127 registerExistingGOTEntries(LinkGraph &G,
128                            ppc64::TOCTableManager<Endianness> &TOC) {
129   auto isGOTEntry = [](const Edge &E) {
130     return E.getKind() == ppc64::Pointer64 && E.getTarget().isExternal();
131   };
132   if (Section *dotTOCSection = G.findSectionByName(".toc")) {
133     for (Block *B : dotTOCSection->blocks())
134       for (Edge &E : B->edges())
135         if (isGOTEntry(E))
136           TOC.registerPreExistingEntry(E.getTarget(),
137                                        G.addAnonymousSymbol(*B, E.getOffset(),
138                                                             G.getPointerSize(),
139                                                             false, false));
140   }
141 }
142 
143 template <support::endianness Endianness>
144 Error buildTables_ELF_ppc64(LinkGraph &G) {
145   LLVM_DEBUG(dbgs() << "Visiting edges in graph:\n");
146   ppc64::TOCTableManager<Endianness> TOC;
147   // Before visiting edges, we create a header containing the address of TOC
148   // base as ELFABIv2 suggests:
149   //  > The GOT consists of an 8-byte header that contains the TOC base (the
150   //  first TOC base when multiple TOCs are present), followed by an array of
151   //  8-byte addresses.
152   createELFGOTHeader(G, TOC);
153 
154   // There might be compiler-generated GOT entries in ELF relocatable file.
155   registerExistingGOTEntries(G, TOC);
156 
157   ppc64::PLTTableManager<Endianness> PLT(TOC);
158   TLSInfoTableManager_ELF_ppc64<Endianness> TLSInfo;
159   visitExistingEdges(G, TOC, PLT, TLSInfo);
160 
161   // After visiting edges in LinkGraph, we have GOT entries built in the
162   // synthesized section.
163   // Merge sections included in TOC into synthesized TOC section,
164   // thus TOC is compact and reducing chances of relocation
165   // overflow.
166   if (Section *TOCSection = G.findSectionByName(TOC.getSectionName())) {
167     // .got and .plt are not normally present in a relocatable object file
168     // because they are linker generated.
169     if (Section *gotSection = G.findSectionByName(".got"))
170       G.mergeSections(*TOCSection, *gotSection);
171     if (Section *tocSection = G.findSectionByName(".toc"))
172       G.mergeSections(*TOCSection, *tocSection);
173     if (Section *sdataSection = G.findSectionByName(".sdata"))
174       G.mergeSections(*TOCSection, *sdataSection);
175     if (Section *sbssSection = G.findSectionByName(".sbss"))
176       G.mergeSections(*TOCSection, *sbssSection);
177     // .tocbss no longer appears in ELFABIv2. Leave it here to be compatible
178     // with rtdyld.
179     if (Section *tocbssSection = G.findSectionByName(".tocbss"))
180       G.mergeSections(*TOCSection, *tocbssSection);
181     if (Section *pltSection = G.findSectionByName(".plt"))
182       G.mergeSections(*TOCSection, *pltSection);
183   }
184 
185   return Error::success();
186 }
187 
188 } // namespace
189 
190 namespace llvm::jitlink {
191 
192 template <support::endianness Endianness>
193 class ELFLinkGraphBuilder_ppc64
194     : public ELFLinkGraphBuilder<object::ELFType<Endianness, true>> {
195 private:
196   using ELFT = object::ELFType<Endianness, true>;
197   using Base = ELFLinkGraphBuilder<ELFT>;
198 
199   using Base::G; // Use LinkGraph pointer from base class.
200 
201   Error addRelocations() override {
202     LLVM_DEBUG(dbgs() << "Processing relocations:\n");
203 
204     using Self = ELFLinkGraphBuilder_ppc64<Endianness>;
205     for (const auto &RelSect : Base::Sections) {
206       // Validate the section to read relocation entries from.
207       if (RelSect.sh_type == ELF::SHT_REL)
208         return make_error<StringError>("No SHT_REL in valid " +
209                                            G->getTargetTriple().getArchName() +
210                                            " ELF object files",
211                                        inconvertibleErrorCode());
212 
213       if (Error Err = Base::forEachRelaRelocation(RelSect, this,
214                                                   &Self::addSingleRelocation))
215         return Err;
216     }
217 
218     return Error::success();
219   }
220 
221   Error addSingleRelocation(const typename ELFT::Rela &Rel,
222                             const typename ELFT::Shdr &FixupSection,
223                             Block &BlockToFix) {
224     using Base = ELFLinkGraphBuilder<ELFT>;
225     auto ELFReloc = Rel.getType(false);
226 
227     // R_PPC64_NONE is a no-op.
228     if (LLVM_UNLIKELY(ELFReloc == ELF::R_PPC64_NONE))
229       return Error::success();
230 
231     // TLS model markers. We only support global-dynamic model now.
232     if (ELFReloc == ELF::R_PPC64_TLSGD)
233       return Error::success();
234     if (ELFReloc == ELF::R_PPC64_TLSLD)
235       return make_error<StringError>("Local-dynamic TLS model is not supported",
236                                      inconvertibleErrorCode());
237 
238     auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
239     if (!ObjSymbol)
240       return ObjSymbol.takeError();
241 
242     uint32_t SymbolIndex = Rel.getSymbol(false);
243     Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
244     if (!GraphSymbol)
245       return make_error<StringError>(
246           formatv("Could not find symbol at given index, did you add it to "
247                   "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
248                   SymbolIndex, (*ObjSymbol)->st_shndx,
249                   Base::GraphSymbols.size()),
250           inconvertibleErrorCode());
251 
252     int64_t Addend = Rel.r_addend;
253     orc::ExecutorAddr FixupAddress =
254         orc::ExecutorAddr(FixupSection.sh_addr) + Rel.r_offset;
255     Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
256     Edge::Kind Kind = Edge::Invalid;
257 
258     switch (ELFReloc) {
259     default:
260       return make_error<JITLinkError>(
261           "In " + G->getName() + ": Unsupported ppc64 relocation type " +
262           object::getELFRelocationTypeName(ELF::EM_PPC64, ELFReloc));
263     case ELF::R_PPC64_ADDR64:
264       Kind = ppc64::Pointer64;
265       break;
266     case ELF::R_PPC64_ADDR32:
267       Kind = ppc64::Pointer32;
268       break;
269     case ELF::R_PPC64_TOC16_HA:
270       Kind = ppc64::TOCDelta16HA;
271       break;
272     case ELF::R_PPC64_TOC16_DS:
273       Kind = ppc64::TOCDelta16DS;
274       break;
275     case ELF::R_PPC64_TOC16_LO:
276       Kind = ppc64::TOCDelta16LO;
277       break;
278     case ELF::R_PPC64_TOC16_LO_DS:
279       Kind = ppc64::TOCDelta16LODS;
280       break;
281     case ELF::R_PPC64_REL16:
282       Kind = ppc64::Delta16;
283       break;
284     case ELF::R_PPC64_REL16_HA:
285       Kind = ppc64::Delta16HA;
286       break;
287     case ELF::R_PPC64_REL16_LO:
288       Kind = ppc64::Delta16LO;
289       break;
290     case ELF::R_PPC64_REL32:
291       Kind = ppc64::Delta32;
292       break;
293     case ELF::R_PPC64_REL24_NOTOC:
294       Kind = ppc64::RequestCallNoTOC;
295       break;
296     case ELF::R_PPC64_REL24:
297       Kind = ppc64::RequestCall;
298       assert(Addend == 0 && "Addend is expected to be 0 for a function call");
299       // We assume branching to local entry, will reverse the addend if not.
300       Addend = ELF::decodePPC64LocalEntryOffset((*ObjSymbol)->st_other);
301       break;
302     case ELF::R_PPC64_REL64:
303       Kind = ppc64::Delta64;
304       break;
305     case ELF::R_PPC64_PCREL34:
306       Kind = ppc64::Delta34;
307       break;
308     case ELF::R_PPC64_GOT_TLSGD16_HA:
309       Kind = ppc64::RequestTLSDescInGOTAndTransformToTOCDelta16HA;
310       break;
311     case ELF::R_PPC64_GOT_TLSGD16_LO:
312       Kind = ppc64::RequestTLSDescInGOTAndTransformToTOCDelta16LO;
313       break;
314     }
315 
316     Edge GE(Kind, Offset, *GraphSymbol, Addend);
317     BlockToFix.addEdge(std::move(GE));
318     return Error::success();
319   }
320 
321 public:
322   ELFLinkGraphBuilder_ppc64(StringRef FileName,
323                             const object::ELFFile<ELFT> &Obj, Triple TT,
324                             SubtargetFeatures Features)
325       : ELFLinkGraphBuilder<ELFT>(Obj, std::move(TT), std::move(Features),
326                                   FileName, ppc64::getEdgeKindName) {}
327 };
328 
329 template <support::endianness Endianness>
330 class ELFJITLinker_ppc64 : public JITLinker<ELFJITLinker_ppc64<Endianness>> {
331   using JITLinkerBase = JITLinker<ELFJITLinker_ppc64<Endianness>>;
332   friend JITLinkerBase;
333 
334 public:
335   ELFJITLinker_ppc64(std::unique_ptr<JITLinkContext> Ctx,
336                      std::unique_ptr<LinkGraph> G, PassConfiguration PassConfig)
337       : JITLinkerBase(std::move(Ctx), std::move(G), std::move(PassConfig)) {
338     JITLinkerBase::getPassConfig().PostAllocationPasses.push_back(
339         [this](LinkGraph &G) { return defineTOCBase(G); });
340   }
341 
342 private:
343   Symbol *TOCSymbol = nullptr;
344 
345   Error defineTOCBase(LinkGraph &G) {
346     for (Symbol *Sym : G.defined_symbols()) {
347       if (LLVM_UNLIKELY(Sym->getName() == ELFTOCSymbolName)) {
348         TOCSymbol = Sym;
349         return Error::success();
350       }
351     }
352 
353     assert(TOCSymbol == nullptr &&
354            "TOCSymbol should not be defined at this point");
355 
356     for (Symbol *Sym : G.external_symbols()) {
357       if (Sym->getName() == ELFTOCSymbolName) {
358         TOCSymbol = Sym;
359         break;
360       }
361     }
362 
363     if (Section *TOCSection = G.findSectionByName(
364             ppc64::TOCTableManager<Endianness>::getSectionName())) {
365       assert(!TOCSection->empty() && "TOC section should have reserved an "
366                                      "entry for containing the TOC base");
367 
368       SectionRange SR(*TOCSection);
369       orc::ExecutorAddr TOCBaseAddr(SR.getFirstBlock()->getAddress() +
370                                     ELFTOCBaseOffset);
371       assert(TOCSymbol && TOCSymbol->isExternal() &&
372              ".TOC. should be a external symbol at this point");
373       G.makeAbsolute(*TOCSymbol, TOCBaseAddr);
374       // Create an alias of .TOC. so that rtdyld checker can recognize.
375       G.addAbsoluteSymbol(TOCSymbolAliasIdent, TOCSymbol->getAddress(),
376                           TOCSymbol->getSize(), TOCSymbol->getLinkage(),
377                           TOCSymbol->getScope(), TOCSymbol->isLive());
378       return Error::success();
379     }
380 
381     // If TOC section doesn't exist, which means no TOC relocation is found, we
382     // don't need a TOCSymbol.
383     return Error::success();
384   }
385 
386   Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
387     return ppc64::applyFixup<Endianness>(G, B, E, TOCSymbol);
388   }
389 };
390 
391 template <support::endianness Endianness>
392 Expected<std::unique_ptr<LinkGraph>>
393 createLinkGraphFromELFObject_ppc64(MemoryBufferRef ObjectBuffer) {
394   LLVM_DEBUG({
395     dbgs() << "Building jitlink graph for new input "
396            << ObjectBuffer.getBufferIdentifier() << "...\n";
397   });
398 
399   auto ELFObj = object::ObjectFile::createELFObjectFile(ObjectBuffer);
400   if (!ELFObj)
401     return ELFObj.takeError();
402 
403   auto Features = (*ELFObj)->getFeatures();
404   if (!Features)
405     return Features.takeError();
406 
407   using ELFT = object::ELFType<Endianness, true>;
408   auto &ELFObjFile = cast<object::ELFObjectFile<ELFT>>(**ELFObj);
409   return ELFLinkGraphBuilder_ppc64<Endianness>(
410              (*ELFObj)->getFileName(), ELFObjFile.getELFFile(),
411              (*ELFObj)->makeTriple(), std::move(*Features))
412       .buildGraph();
413 }
414 
415 template <support::endianness Endianness>
416 void link_ELF_ppc64(std::unique_ptr<LinkGraph> G,
417                     std::unique_ptr<JITLinkContext> Ctx) {
418   PassConfiguration Config;
419 
420   if (Ctx->shouldAddDefaultTargetPasses(G->getTargetTriple())) {
421     // Construct a JITLinker and run the link function.
422 
423     // Add eh-frame passses.
424     Config.PrePrunePasses.push_back(DWARFRecordSectionSplitter(".eh_frame"));
425     Config.PrePrunePasses.push_back(EHFrameEdgeFixer(
426         ".eh_frame", G->getPointerSize(), ppc64::Pointer32, ppc64::Pointer64,
427         ppc64::Delta32, ppc64::Delta64, ppc64::NegDelta32));
428     Config.PrePrunePasses.push_back(EHFrameNullTerminator(".eh_frame"));
429 
430     // Add a mark-live pass.
431     if (auto MarkLive = Ctx->getMarkLivePass(G->getTargetTriple()))
432       Config.PrePrunePasses.push_back(std::move(MarkLive));
433     else
434       Config.PrePrunePasses.push_back(markAllSymbolsLive);
435   }
436 
437   Config.PostPrunePasses.push_back(buildTables_ELF_ppc64<Endianness>);
438 
439   if (auto Err = Ctx->modifyPassConfig(*G, Config))
440     return Ctx->notifyFailed(std::move(Err));
441 
442   ELFJITLinker_ppc64<Endianness>::link(std::move(Ctx), std::move(G),
443                                        std::move(Config));
444 }
445 
446 Expected<std::unique_ptr<LinkGraph>>
447 createLinkGraphFromELFObject_ppc64(MemoryBufferRef ObjectBuffer) {
448   return createLinkGraphFromELFObject_ppc64<support::big>(
449       std::move(ObjectBuffer));
450 }
451 
452 Expected<std::unique_ptr<LinkGraph>>
453 createLinkGraphFromELFObject_ppc64le(MemoryBufferRef ObjectBuffer) {
454   return createLinkGraphFromELFObject_ppc64<support::little>(
455       std::move(ObjectBuffer));
456 }
457 
458 /// jit-link the given object buffer, which must be a ELF ppc64 object file.
459 void link_ELF_ppc64(std::unique_ptr<LinkGraph> G,
460                     std::unique_ptr<JITLinkContext> Ctx) {
461   return link_ELF_ppc64<support::big>(std::move(G), std::move(Ctx));
462 }
463 
464 /// jit-link the given object buffer, which must be a ELF ppc64le object file.
465 void link_ELF_ppc64le(std::unique_ptr<LinkGraph> G,
466                       std::unique_ptr<JITLinkContext> Ctx) {
467   return link_ELF_ppc64<support::little>(std::move(G), std::move(Ctx));
468 }
469 
470 } // end namespace llvm::jitlink
471