xref: /llvm-project/llvm/lib/ExecutionEngine/JITLink/ELF_ppc64.cpp (revision 94239712eb17989bf23f73f790dc5a18f42dd33b)
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_ADDR16:
270       Kind = ppc64::Pointer16;
271       break;
272     case ELF::R_PPC64_ADDR16_DS:
273       Kind = ppc64::Pointer16DS;
274       break;
275     case ELF::R_PPC64_ADDR16_HA:
276       Kind = ppc64::Pointer16HA;
277       break;
278     case ELF::R_PPC64_ADDR16_HI:
279       Kind = ppc64::Pointer16HI;
280       break;
281     case ELF::R_PPC64_ADDR16_HIGH:
282       Kind = ppc64::Pointer16HIGH;
283       break;
284     case ELF::R_PPC64_ADDR16_HIGHA:
285       Kind = ppc64::Pointer16HIGHA;
286       break;
287     case ELF::R_PPC64_ADDR16_HIGHER:
288       Kind = ppc64::Pointer16HIGHER;
289       break;
290     case ELF::R_PPC64_ADDR16_HIGHERA:
291       Kind = ppc64::Pointer16HIGHERA;
292       break;
293     case ELF::R_PPC64_ADDR16_HIGHEST:
294       Kind = ppc64::Pointer16HIGHEST;
295       break;
296     case ELF::R_PPC64_ADDR16_HIGHESTA:
297       Kind = ppc64::Pointer16HIGHESTA;
298       break;
299     case ELF::R_PPC64_ADDR16_LO:
300       Kind = ppc64::Pointer16LO;
301       break;
302     case ELF::R_PPC64_ADDR16_LO_DS:
303       Kind = ppc64::Pointer16LODS;
304       break;
305     case ELF::R_PPC64_ADDR14:
306       Kind = ppc64::Pointer14;
307       break;
308     case ELF::R_PPC64_TOC:
309       Kind = ppc64::TOC;
310       break;
311     case ELF::R_PPC64_TOC16:
312       Kind = ppc64::TOCDelta16;
313       break;
314     case ELF::R_PPC64_TOC16_HA:
315       Kind = ppc64::TOCDelta16HA;
316       break;
317     case ELF::R_PPC64_TOC16_HI:
318       Kind = ppc64::TOCDelta16HI;
319       break;
320     case ELF::R_PPC64_TOC16_DS:
321       Kind = ppc64::TOCDelta16DS;
322       break;
323     case ELF::R_PPC64_TOC16_LO:
324       Kind = ppc64::TOCDelta16LO;
325       break;
326     case ELF::R_PPC64_TOC16_LO_DS:
327       Kind = ppc64::TOCDelta16LODS;
328       break;
329     case ELF::R_PPC64_REL16:
330       Kind = ppc64::Delta16;
331       break;
332     case ELF::R_PPC64_REL16_HA:
333       Kind = ppc64::Delta16HA;
334       break;
335     case ELF::R_PPC64_REL16_HI:
336       Kind = ppc64::Delta16HI;
337       break;
338     case ELF::R_PPC64_REL16_LO:
339       Kind = ppc64::Delta16LO;
340       break;
341     case ELF::R_PPC64_REL32:
342       Kind = ppc64::Delta32;
343       break;
344     case ELF::R_PPC64_REL24_NOTOC:
345       Kind = ppc64::RequestCallNoTOC;
346       break;
347     case ELF::R_PPC64_REL24:
348       Kind = ppc64::RequestCall;
349       assert(Addend == 0 && "Addend is expected to be 0 for a function call");
350       // We assume branching to local entry, will reverse the addend if not.
351       Addend = ELF::decodePPC64LocalEntryOffset((*ObjSymbol)->st_other);
352       break;
353     case ELF::R_PPC64_REL64:
354       Kind = ppc64::Delta64;
355       break;
356     case ELF::R_PPC64_PCREL34:
357       Kind = ppc64::Delta34;
358       break;
359     case ELF::R_PPC64_GOT_TLSGD16_HA:
360       Kind = ppc64::RequestTLSDescInGOTAndTransformToTOCDelta16HA;
361       break;
362     case ELF::R_PPC64_GOT_TLSGD16_LO:
363       Kind = ppc64::RequestTLSDescInGOTAndTransformToTOCDelta16LO;
364       break;
365     }
366 
367     Edge GE(Kind, Offset, *GraphSymbol, Addend);
368     BlockToFix.addEdge(std::move(GE));
369     return Error::success();
370   }
371 
372 public:
373   ELFLinkGraphBuilder_ppc64(StringRef FileName,
374                             const object::ELFFile<ELFT> &Obj, Triple TT,
375                             SubtargetFeatures Features)
376       : ELFLinkGraphBuilder<ELFT>(Obj, std::move(TT), std::move(Features),
377                                   FileName, ppc64::getEdgeKindName) {}
378 };
379 
380 template <support::endianness Endianness>
381 class ELFJITLinker_ppc64 : public JITLinker<ELFJITLinker_ppc64<Endianness>> {
382   using JITLinkerBase = JITLinker<ELFJITLinker_ppc64<Endianness>>;
383   friend JITLinkerBase;
384 
385 public:
386   ELFJITLinker_ppc64(std::unique_ptr<JITLinkContext> Ctx,
387                      std::unique_ptr<LinkGraph> G, PassConfiguration PassConfig)
388       : JITLinkerBase(std::move(Ctx), std::move(G), std::move(PassConfig)) {
389     JITLinkerBase::getPassConfig().PostAllocationPasses.push_back(
390         [this](LinkGraph &G) { return defineTOCBase(G); });
391   }
392 
393 private:
394   Symbol *TOCSymbol = nullptr;
395 
396   Error defineTOCBase(LinkGraph &G) {
397     for (Symbol *Sym : G.defined_symbols()) {
398       if (LLVM_UNLIKELY(Sym->getName() == ELFTOCSymbolName)) {
399         TOCSymbol = Sym;
400         return Error::success();
401       }
402     }
403 
404     assert(TOCSymbol == nullptr &&
405            "TOCSymbol should not be defined at this point");
406 
407     for (Symbol *Sym : G.external_symbols()) {
408       if (Sym->getName() == ELFTOCSymbolName) {
409         TOCSymbol = Sym;
410         break;
411       }
412     }
413 
414     if (Section *TOCSection = G.findSectionByName(
415             ppc64::TOCTableManager<Endianness>::getSectionName())) {
416       assert(!TOCSection->empty() && "TOC section should have reserved an "
417                                      "entry for containing the TOC base");
418 
419       SectionRange SR(*TOCSection);
420       orc::ExecutorAddr TOCBaseAddr(SR.getFirstBlock()->getAddress() +
421                                     ELFTOCBaseOffset);
422       assert(TOCSymbol && TOCSymbol->isExternal() &&
423              ".TOC. should be a external symbol at this point");
424       G.makeAbsolute(*TOCSymbol, TOCBaseAddr);
425       // Create an alias of .TOC. so that rtdyld checker can recognize.
426       G.addAbsoluteSymbol(TOCSymbolAliasIdent, TOCSymbol->getAddress(),
427                           TOCSymbol->getSize(), TOCSymbol->getLinkage(),
428                           TOCSymbol->getScope(), TOCSymbol->isLive());
429       return Error::success();
430     }
431 
432     // If TOC section doesn't exist, which means no TOC relocation is found, we
433     // don't need a TOCSymbol.
434     return Error::success();
435   }
436 
437   Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
438     return ppc64::applyFixup<Endianness>(G, B, E, TOCSymbol);
439   }
440 };
441 
442 template <support::endianness Endianness>
443 Expected<std::unique_ptr<LinkGraph>>
444 createLinkGraphFromELFObject_ppc64(MemoryBufferRef ObjectBuffer) {
445   LLVM_DEBUG({
446     dbgs() << "Building jitlink graph for new input "
447            << ObjectBuffer.getBufferIdentifier() << "...\n";
448   });
449 
450   auto ELFObj = object::ObjectFile::createELFObjectFile(ObjectBuffer);
451   if (!ELFObj)
452     return ELFObj.takeError();
453 
454   auto Features = (*ELFObj)->getFeatures();
455   if (!Features)
456     return Features.takeError();
457 
458   using ELFT = object::ELFType<Endianness, true>;
459   auto &ELFObjFile = cast<object::ELFObjectFile<ELFT>>(**ELFObj);
460   return ELFLinkGraphBuilder_ppc64<Endianness>(
461              (*ELFObj)->getFileName(), ELFObjFile.getELFFile(),
462              (*ELFObj)->makeTriple(), std::move(*Features))
463       .buildGraph();
464 }
465 
466 template <support::endianness Endianness>
467 void link_ELF_ppc64(std::unique_ptr<LinkGraph> G,
468                     std::unique_ptr<JITLinkContext> Ctx) {
469   PassConfiguration Config;
470 
471   if (Ctx->shouldAddDefaultTargetPasses(G->getTargetTriple())) {
472     // Construct a JITLinker and run the link function.
473 
474     // Add eh-frame passes.
475     Config.PrePrunePasses.push_back(DWARFRecordSectionSplitter(".eh_frame"));
476     Config.PrePrunePasses.push_back(EHFrameEdgeFixer(
477         ".eh_frame", G->getPointerSize(), ppc64::Pointer32, ppc64::Pointer64,
478         ppc64::Delta32, ppc64::Delta64, ppc64::NegDelta32));
479     Config.PrePrunePasses.push_back(EHFrameNullTerminator(".eh_frame"));
480 
481     // Add a mark-live pass.
482     if (auto MarkLive = Ctx->getMarkLivePass(G->getTargetTriple()))
483       Config.PrePrunePasses.push_back(std::move(MarkLive));
484     else
485       Config.PrePrunePasses.push_back(markAllSymbolsLive);
486   }
487 
488   Config.PostPrunePasses.push_back(buildTables_ELF_ppc64<Endianness>);
489 
490   if (auto Err = Ctx->modifyPassConfig(*G, Config))
491     return Ctx->notifyFailed(std::move(Err));
492 
493   ELFJITLinker_ppc64<Endianness>::link(std::move(Ctx), std::move(G),
494                                        std::move(Config));
495 }
496 
497 Expected<std::unique_ptr<LinkGraph>>
498 createLinkGraphFromELFObject_ppc64(MemoryBufferRef ObjectBuffer) {
499   return createLinkGraphFromELFObject_ppc64<support::big>(
500       std::move(ObjectBuffer));
501 }
502 
503 Expected<std::unique_ptr<LinkGraph>>
504 createLinkGraphFromELFObject_ppc64le(MemoryBufferRef ObjectBuffer) {
505   return createLinkGraphFromELFObject_ppc64<support::little>(
506       std::move(ObjectBuffer));
507 }
508 
509 /// jit-link the given object buffer, which must be a ELF ppc64 object file.
510 void link_ELF_ppc64(std::unique_ptr<LinkGraph> G,
511                     std::unique_ptr<JITLinkContext> Ctx) {
512   return link_ELF_ppc64<support::big>(std::move(G), std::move(Ctx));
513 }
514 
515 /// jit-link the given object buffer, which must be a ELF ppc64le object file.
516 void link_ELF_ppc64le(std::unique_ptr<LinkGraph> G,
517                       std::unique_ptr<JITLinkContext> Ctx) {
518   return link_ELF_ppc64<support::little>(std::move(G), std::move(Ctx));
519 }
520 
521 } // end namespace llvm::jitlink
522