1 //===------ ELFNixPlatform.cpp - Utilities for executing MachO in Orc -----===//
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 #include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h"
10 
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/ExecutionEngine/JITLink/ELF_x86_64.h"
13 #include "llvm/ExecutionEngine/JITLink/x86_64.h"
14 #include "llvm/ExecutionEngine/Orc/DebugUtils.h"
15 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
16 #include "llvm/Support/BinaryByteStream.h"
17 #include "llvm/Support/Debug.h"
18 
19 #define DEBUG_TYPE "orc"
20 
21 using namespace llvm;
22 using namespace llvm::orc;
23 using namespace llvm::orc::shared;
24 
25 namespace {
26 
27 class DSOHandleMaterializationUnit : public MaterializationUnit {
28 public:
29   DSOHandleMaterializationUnit(ELFNixPlatform &ENP,
30                                const SymbolStringPtr &DSOHandleSymbol)
31       : MaterializationUnit(createDSOHandleSectionSymbols(ENP, DSOHandleSymbol),
32                             DSOHandleSymbol),
33         ENP(ENP) {}
34 
35   StringRef getName() const override { return "DSOHandleMU"; }
36 
37   void materialize(std::unique_ptr<MaterializationResponsibility> R) override {
38     unsigned PointerSize;
39     support::endianness Endianness;
40     jitlink::Edge::Kind EdgeKind;
41     const auto &TT =
42         ENP.getExecutionSession().getExecutorProcessControl().getTargetTriple();
43 
44     switch (TT.getArch()) {
45     case Triple::x86_64:
46       PointerSize = 8;
47       Endianness = support::endianness::little;
48       EdgeKind = jitlink::x86_64::Pointer64;
49       break;
50     default:
51       llvm_unreachable("Unrecognized architecture");
52     }
53 
54     // void *__dso_handle = &__dso_handle;
55     auto G = std::make_unique<jitlink::LinkGraph>(
56         "<DSOHandleMU>", TT, PointerSize, Endianness,
57         jitlink::getGenericEdgeKindName);
58     auto &DSOHandleSection =
59         G->createSection(".data.__dso_handle", sys::Memory::MF_READ);
60     auto &DSOHandleBlock = G->createContentBlock(
61         DSOHandleSection, getDSOHandleContent(PointerSize), 0, 8, 0);
62     auto &DSOHandleSymbol = G->addDefinedSymbol(
63         DSOHandleBlock, 0, *R->getInitializerSymbol(), DSOHandleBlock.getSize(),
64         jitlink::Linkage::Strong, jitlink::Scope::Default, false, true);
65     DSOHandleBlock.addEdge(EdgeKind, 0, DSOHandleSymbol, 0);
66 
67     ENP.getObjectLinkingLayer().emit(std::move(R), std::move(G));
68   }
69 
70   void discard(const JITDylib &JD, const SymbolStringPtr &Sym) override {}
71 
72 private:
73   static SymbolFlagsMap
74   createDSOHandleSectionSymbols(ELFNixPlatform &ENP,
75                                 const SymbolStringPtr &DSOHandleSymbol) {
76     SymbolFlagsMap SymbolFlags;
77     SymbolFlags[DSOHandleSymbol] = JITSymbolFlags::Exported;
78     return SymbolFlags;
79   }
80 
81   ArrayRef<char> getDSOHandleContent(size_t PointerSize) {
82     static const char Content[8] = {0};
83     assert(PointerSize <= sizeof Content);
84     return {Content, PointerSize};
85   }
86 
87   ELFNixPlatform &ENP;
88 };
89 
90 StringRef EHFrameSectionName = ".eh_frame";
91 StringRef InitArrayFuncSectionName = ".init_array";
92 
93 StringRef ThreadBSSSectionName = ".tbss";
94 StringRef ThreadDataSectionName = ".tdata";
95 
96 StringRef InitSectionNames[] = {InitArrayFuncSectionName};
97 
98 } // end anonymous namespace
99 
100 namespace llvm {
101 namespace orc {
102 
103 Expected<std::unique_ptr<ELFNixPlatform>>
104 ELFNixPlatform::Create(ExecutionSession &ES,
105                        ObjectLinkingLayer &ObjLinkingLayer,
106                        JITDylib &PlatformJD, const char *OrcRuntimePath,
107                        Optional<SymbolAliasMap> RuntimeAliases) {
108 
109   auto &EPC = ES.getExecutorProcessControl();
110 
111   // If the target is not supported then bail out immediately.
112   if (!supportedTarget(EPC.getTargetTriple()))
113     return make_error<StringError>("Unsupported ELFNixPlatform triple: " +
114                                        EPC.getTargetTriple().str(),
115                                    inconvertibleErrorCode());
116 
117   // Create default aliases if the caller didn't supply any.
118   if (!RuntimeAliases)
119     RuntimeAliases = standardPlatformAliases(ES);
120 
121   // Define the aliases.
122   if (auto Err = PlatformJD.define(symbolAliases(std::move(*RuntimeAliases))))
123     return std::move(Err);
124 
125   // Add JIT-dispatch function support symbols.
126   if (auto Err = PlatformJD.define(absoluteSymbols(
127           {{ES.intern("__orc_rt_jit_dispatch"),
128             {EPC.getJITDispatchInfo().JITDispatchFunctionAddress.getValue(),
129              JITSymbolFlags::Exported}},
130            {ES.intern("__orc_rt_jit_dispatch_ctx"),
131             {EPC.getJITDispatchInfo().JITDispatchContextAddress.getValue(),
132              JITSymbolFlags::Exported}}})))
133     return std::move(Err);
134 
135   // Create a generator for the ORC runtime archive.
136   auto OrcRuntimeArchiveGenerator = StaticLibraryDefinitionGenerator::Load(
137       ObjLinkingLayer, OrcRuntimePath, EPC.getTargetTriple());
138   if (!OrcRuntimeArchiveGenerator)
139     return OrcRuntimeArchiveGenerator.takeError();
140 
141   // Create the instance.
142   Error Err = Error::success();
143   auto P = std::unique_ptr<ELFNixPlatform>(
144       new ELFNixPlatform(ES, ObjLinkingLayer, PlatformJD,
145                          std::move(*OrcRuntimeArchiveGenerator), Err));
146   if (Err)
147     return std::move(Err);
148   return std::move(P);
149 }
150 
151 Error ELFNixPlatform::setupJITDylib(JITDylib &JD) {
152   return JD.define(
153       std::make_unique<DSOHandleMaterializationUnit>(*this, DSOHandleSymbol));
154   return Error::success();
155 }
156 
157 Error ELFNixPlatform::notifyAdding(ResourceTracker &RT,
158                                    const MaterializationUnit &MU) {
159   auto &JD = RT.getJITDylib();
160   const auto &InitSym = MU.getInitializerSymbol();
161   if (!InitSym)
162     return Error::success();
163 
164   RegisteredInitSymbols[&JD].add(InitSym,
165                                  SymbolLookupFlags::WeaklyReferencedSymbol);
166   LLVM_DEBUG({
167     dbgs() << "ELFNixPlatform: Registered init symbol " << *InitSym
168            << " for MU " << MU.getName() << "\n";
169   });
170   return Error::success();
171 }
172 
173 Error ELFNixPlatform::notifyRemoving(ResourceTracker &RT) {
174   llvm_unreachable("Not supported yet");
175 }
176 
177 static void addAliases(ExecutionSession &ES, SymbolAliasMap &Aliases,
178                        ArrayRef<std::pair<const char *, const char *>> AL) {
179   for (auto &KV : AL) {
180     auto AliasName = ES.intern(KV.first);
181     assert(!Aliases.count(AliasName) && "Duplicate symbol name in alias map");
182     Aliases[std::move(AliasName)] = {ES.intern(KV.second),
183                                      JITSymbolFlags::Exported};
184   }
185 }
186 
187 SymbolAliasMap ELFNixPlatform::standardPlatformAliases(ExecutionSession &ES) {
188   SymbolAliasMap Aliases;
189   addAliases(ES, Aliases, requiredCXXAliases());
190   addAliases(ES, Aliases, standardRuntimeUtilityAliases());
191   return Aliases;
192 }
193 
194 ArrayRef<std::pair<const char *, const char *>>
195 ELFNixPlatform::requiredCXXAliases() {
196   static const std::pair<const char *, const char *> RequiredCXXAliases[] = {
197       {"__cxa_atexit", "__orc_rt_elfnix_cxa_atexit"}};
198 
199   return ArrayRef<std::pair<const char *, const char *>>(RequiredCXXAliases);
200 }
201 
202 ArrayRef<std::pair<const char *, const char *>>
203 ELFNixPlatform::standardRuntimeUtilityAliases() {
204   static const std::pair<const char *, const char *>
205       StandardRuntimeUtilityAliases[] = {
206           {"__orc_rt_run_program", "__orc_rt_elfnix_run_program"},
207           {"__orc_rt_log_error", "__orc_rt_log_error_to_stderr"}};
208 
209   return ArrayRef<std::pair<const char *, const char *>>(
210       StandardRuntimeUtilityAliases);
211 }
212 
213 bool ELFNixPlatform::isInitializerSection(StringRef SecName) {
214   for (auto &Name : InitSectionNames) {
215     if (Name.equals(SecName))
216       return true;
217   }
218   return false;
219 }
220 
221 bool ELFNixPlatform::supportedTarget(const Triple &TT) {
222   switch (TT.getArch()) {
223   case Triple::x86_64:
224     return true;
225   default:
226     return false;
227   }
228 }
229 
230 ELFNixPlatform::ELFNixPlatform(
231     ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
232     JITDylib &PlatformJD,
233     std::unique_ptr<DefinitionGenerator> OrcRuntimeGenerator, Error &Err)
234     : ES(ES), ObjLinkingLayer(ObjLinkingLayer),
235       DSOHandleSymbol(ES.intern("__dso_handle")) {
236   ErrorAsOutParameter _(&Err);
237 
238   ObjLinkingLayer.addPlugin(std::make_unique<ELFNixPlatformPlugin>(*this));
239 
240   PlatformJD.addGenerator(std::move(OrcRuntimeGenerator));
241 
242   // PlatformJD hasn't been 'set-up' by the platform yet (since we're creating
243   // the platform now), so set it up.
244   if (auto E2 = setupJITDylib(PlatformJD)) {
245     Err = std::move(E2);
246     return;
247   }
248 
249   RegisteredInitSymbols[&PlatformJD].add(
250       DSOHandleSymbol, SymbolLookupFlags::WeaklyReferencedSymbol);
251 
252   // Associate wrapper function tags with JIT-side function implementations.
253   if (auto E2 = associateRuntimeSupportFunctions(PlatformJD)) {
254     Err = std::move(E2);
255     return;
256   }
257 
258   // Lookup addresses of runtime functions callable by the platform,
259   // call the platform bootstrap function to initialize the platform-state
260   // object in the executor.
261   if (auto E2 = bootstrapELFNixRuntime(PlatformJD)) {
262     Err = std::move(E2);
263     return;
264   }
265 }
266 
267 Error ELFNixPlatform::associateRuntimeSupportFunctions(JITDylib &PlatformJD) {
268   ExecutionSession::JITDispatchHandlerAssociationMap WFs;
269 
270   using GetInitializersSPSSig =
271       SPSExpected<SPSELFNixJITDylibInitializerSequence>(SPSString);
272   WFs[ES.intern("__orc_rt_elfnix_get_initializers_tag")] =
273       ES.wrapAsyncWithSPS<GetInitializersSPSSig>(
274           this, &ELFNixPlatform::rt_getInitializers);
275 
276   using GetDeinitializersSPSSig =
277       SPSExpected<SPSELFJITDylibDeinitializerSequence>(SPSExecutorAddress);
278   WFs[ES.intern("__orc_rt_elfnix_get_deinitializers_tag")] =
279       ES.wrapAsyncWithSPS<GetDeinitializersSPSSig>(
280           this, &ELFNixPlatform::rt_getDeinitializers);
281 
282   using LookupSymbolSPSSig =
283       SPSExpected<SPSExecutorAddress>(SPSExecutorAddress, SPSString);
284   WFs[ES.intern("__orc_rt_elfnix_symbol_lookup_tag")] =
285       ES.wrapAsyncWithSPS<LookupSymbolSPSSig>(this,
286                                               &ELFNixPlatform::rt_lookupSymbol);
287 
288   return ES.registerJITDispatchHandlers(PlatformJD, std::move(WFs));
289 }
290 
291 void ELFNixPlatform::getInitializersBuildSequencePhase(
292     SendInitializerSequenceFn SendResult, JITDylib &JD,
293     std::vector<JITDylibSP> DFSLinkOrder) {
294   ELFNixJITDylibInitializerSequence FullInitSeq;
295   {
296     std::lock_guard<std::mutex> Lock(PlatformMutex);
297     for (auto &InitJD : reverse(DFSLinkOrder)) {
298       LLVM_DEBUG({
299         dbgs() << "ELFNixPlatform: Appending inits for \"" << InitJD->getName()
300                << "\" to sequence\n";
301       });
302       auto ISItr = InitSeqs.find(InitJD.get());
303       if (ISItr != InitSeqs.end()) {
304         FullInitSeq.emplace_back(std::move(ISItr->second));
305         InitSeqs.erase(ISItr);
306       }
307     }
308   }
309 
310   SendResult(std::move(FullInitSeq));
311 }
312 
313 void ELFNixPlatform::getInitializersLookupPhase(
314     SendInitializerSequenceFn SendResult, JITDylib &JD) {
315 
316   auto DFSLinkOrder = JD.getDFSLinkOrder();
317   DenseMap<JITDylib *, SymbolLookupSet> NewInitSymbols;
318   ES.runSessionLocked([&]() {
319     for (auto &InitJD : DFSLinkOrder) {
320       auto RISItr = RegisteredInitSymbols.find(InitJD.get());
321       if (RISItr != RegisteredInitSymbols.end()) {
322         NewInitSymbols[InitJD.get()] = std::move(RISItr->second);
323         RegisteredInitSymbols.erase(RISItr);
324       }
325     }
326   });
327 
328   // If there are no further init symbols to look up then move on to the next
329   // phase.
330   if (NewInitSymbols.empty()) {
331     getInitializersBuildSequencePhase(std::move(SendResult), JD,
332                                       std::move(DFSLinkOrder));
333     return;
334   }
335 
336   // Otherwise issue a lookup and re-run this phase when it completes.
337   lookupInitSymbolsAsync(
338       [this, SendResult = std::move(SendResult), &JD](Error Err) mutable {
339         if (Err)
340           SendResult(std::move(Err));
341         else
342           getInitializersLookupPhase(std::move(SendResult), JD);
343       },
344       ES, std::move(NewInitSymbols));
345 }
346 
347 void ELFNixPlatform::rt_getInitializers(SendInitializerSequenceFn SendResult,
348                                         StringRef JDName) {
349   LLVM_DEBUG({
350     dbgs() << "ELFNixPlatform::rt_getInitializers(\"" << JDName << "\")\n";
351   });
352 
353   JITDylib *JD = ES.getJITDylibByName(JDName);
354   if (!JD) {
355     LLVM_DEBUG({
356       dbgs() << "  No such JITDylib \"" << JDName << "\". Sending error.\n";
357     });
358     SendResult(make_error<StringError>("No JITDylib named " + JDName,
359                                        inconvertibleErrorCode()));
360     return;
361   }
362 
363   getInitializersLookupPhase(std::move(SendResult), *JD);
364 }
365 
366 void ELFNixPlatform::rt_getDeinitializers(
367     SendDeinitializerSequenceFn SendResult, ExecutorAddress Handle) {
368   LLVM_DEBUG({
369     dbgs() << "ELFNixPlatform::rt_getDeinitializers(\""
370            << formatv("{0:x}", Handle.getValue()) << "\")\n";
371   });
372 
373   JITDylib *JD = nullptr;
374 
375   {
376     std::lock_guard<std::mutex> Lock(PlatformMutex);
377     auto I = HandleAddrToJITDylib.find(Handle.getValue());
378     if (I != HandleAddrToJITDylib.end())
379       JD = I->second;
380   }
381 
382   if (!JD) {
383     LLVM_DEBUG({
384       dbgs() << "  No JITDylib for handle "
385              << formatv("{0:x}", Handle.getValue()) << "\n";
386     });
387     SendResult(make_error<StringError>("No JITDylib associated with handle " +
388                                            formatv("{0:x}", Handle.getValue()),
389                                        inconvertibleErrorCode()));
390     return;
391   }
392 
393   SendResult(ELFNixJITDylibDeinitializerSequence());
394 }
395 
396 void ELFNixPlatform::rt_lookupSymbol(SendSymbolAddressFn SendResult,
397                                      ExecutorAddress Handle,
398                                      StringRef SymbolName) {
399   LLVM_DEBUG({
400     dbgs() << "ELFNixPlatform::rt_lookupSymbol(\""
401            << formatv("{0:x}", Handle.getValue()) << "\")\n";
402   });
403 
404   JITDylib *JD = nullptr;
405 
406   {
407     std::lock_guard<std::mutex> Lock(PlatformMutex);
408     auto I = HandleAddrToJITDylib.find(Handle.getValue());
409     if (I != HandleAddrToJITDylib.end())
410       JD = I->second;
411   }
412 
413   if (!JD) {
414     LLVM_DEBUG({
415       dbgs() << "  No JITDylib for handle "
416              << formatv("{0:x}", Handle.getValue()) << "\n";
417     });
418     SendResult(make_error<StringError>("No JITDylib associated with handle " +
419                                            formatv("{0:x}", Handle.getValue()),
420                                        inconvertibleErrorCode()));
421     return;
422   }
423 
424   // Use functor class to work around XL build compiler issue on AIX.
425   class RtLookupNotifyComplete {
426   public:
427     RtLookupNotifyComplete(SendSymbolAddressFn &&SendResult)
428         : SendResult(std::move(SendResult)) {}
429     void operator()(Expected<SymbolMap> Result) {
430       if (Result) {
431         assert(Result->size() == 1 && "Unexpected result map count");
432         SendResult(ExecutorAddress(Result->begin()->second.getAddress()));
433       } else {
434         SendResult(Result.takeError());
435       }
436     }
437 
438   private:
439     SendSymbolAddressFn SendResult;
440   };
441 
442   ES.lookup(
443       LookupKind::DLSym, {{JD, JITDylibLookupFlags::MatchExportedSymbolsOnly}},
444       SymbolLookupSet(ES.intern(SymbolName)), SymbolState::Ready,
445       RtLookupNotifyComplete(std::move(SendResult)), NoDependenciesToRegister);
446 }
447 
448 Error ELFNixPlatform::bootstrapELFNixRuntime(JITDylib &PlatformJD) {
449 
450   std::pair<const char *, ExecutorAddress *> Symbols[] = {
451       {"__orc_rt_elfnix_platform_bootstrap", &orc_rt_elfnix_platform_bootstrap},
452       {"__orc_rt_elfnix_platform_shutdown", &orc_rt_elfnix_platform_shutdown},
453       {"__orc_rt_elfnix_register_object_sections",
454        &orc_rt_elfnix_register_object_sections}};
455 
456   SymbolLookupSet RuntimeSymbols;
457   std::vector<std::pair<SymbolStringPtr, ExecutorAddress *>> AddrsToRecord;
458   for (const auto &KV : Symbols) {
459     auto Name = ES.intern(KV.first);
460     RuntimeSymbols.add(Name);
461     AddrsToRecord.push_back({std::move(Name), KV.second});
462   }
463 
464   auto RuntimeSymbolAddrs = ES.lookup(
465       {{&PlatformJD, JITDylibLookupFlags::MatchAllSymbols}}, RuntimeSymbols);
466   if (!RuntimeSymbolAddrs)
467     return RuntimeSymbolAddrs.takeError();
468 
469   for (const auto &KV : AddrsToRecord) {
470     auto &Name = KV.first;
471     assert(RuntimeSymbolAddrs->count(Name) && "Missing runtime symbol?");
472     KV.second->setValue((*RuntimeSymbolAddrs)[Name].getAddress());
473   }
474 
475   if (auto Err = ES.callSPSWrapper<void()>(
476           orc_rt_elfnix_platform_bootstrap.getValue()))
477     return Err;
478 
479   // FIXME: Ordering is fuzzy here. We're probably best off saying
480   // "behavior is undefined if code that uses the runtime is added before
481   // the platform constructor returns", then move all this to the constructor.
482   RuntimeBootstrapped = true;
483   std::vector<ELFPerObjectSectionsToRegister> DeferredPOSRs;
484   {
485     std::lock_guard<std::mutex> Lock(PlatformMutex);
486     DeferredPOSRs = std::move(BootstrapPOSRs);
487   }
488 
489   for (auto &D : DeferredPOSRs)
490     if (auto Err = registerPerObjectSections(D))
491       return Err;
492 
493   return Error::success();
494 }
495 
496 Error ELFNixPlatform::registerInitInfo(
497     JITDylib &JD, ArrayRef<jitlink::Section *> InitSections) {
498 
499   std::unique_lock<std::mutex> Lock(PlatformMutex);
500 
501   ELFNixJITDylibInitializers *InitSeq = nullptr;
502   {
503     auto I = InitSeqs.find(&JD);
504     if (I == InitSeqs.end()) {
505       // If there's no init sequence entry yet then we need to look up the
506       // header symbol to force creation of one.
507       Lock.unlock();
508 
509       auto SearchOrder =
510           JD.withLinkOrderDo([](const JITDylibSearchOrder &SO) { return SO; });
511       if (auto Err = ES.lookup(SearchOrder, DSOHandleSymbol).takeError())
512         return Err;
513 
514       Lock.lock();
515       I = InitSeqs.find(&JD);
516       assert(I != InitSeqs.end() &&
517              "Entry missing after header symbol lookup?");
518     }
519     InitSeq = &I->second;
520   }
521 
522   for (auto *Sec : InitSections) {
523     // FIXME: Avoid copy here.
524     jitlink::SectionRange R(*Sec);
525     InitSeq->InitSections[Sec->getName()].push_back(
526         {ExecutorAddress(R.getStart()), ExecutorAddress(R.getEnd())});
527   }
528 
529   return Error::success();
530 }
531 
532 Error ELFNixPlatform::registerPerObjectSections(
533     const ELFPerObjectSectionsToRegister &POSR) {
534 
535   if (!orc_rt_elfnix_register_object_sections)
536     return make_error<StringError>("Attempting to register per-object "
537                                    "sections, but runtime support has not "
538                                    "been loaded yet",
539                                    inconvertibleErrorCode());
540 
541   Error ErrResult = Error::success();
542   if (auto Err = ES.callSPSWrapper<shared::SPSError(
543                      SPSELFPerObjectSectionsToRegister)>(
544           orc_rt_elfnix_register_object_sections.getValue(), ErrResult, POSR))
545     return Err;
546   return ErrResult;
547 }
548 
549 void ELFNixPlatform::ELFNixPlatformPlugin::modifyPassConfig(
550     MaterializationResponsibility &MR, jitlink::LinkGraph &LG,
551     jitlink::PassConfiguration &Config) {
552 
553   // If the initializer symbol is the __dso_handle symbol then just add
554   // the DSO handle support passes.
555   if (MR.getInitializerSymbol() == MP.DSOHandleSymbol) {
556     addDSOHandleSupportPasses(MR, Config);
557     // The DSOHandle materialization unit doesn't require any other
558     // support, so we can bail out early.
559     return;
560   }
561 
562   // If the object contains initializers then add passes to record them.
563   if (MR.getInitializerSymbol())
564     addInitializerSupportPasses(MR, Config);
565 
566   // Add passes for eh-frame and TLV support.
567   addEHAndTLVSupportPasses(MR, Config);
568 }
569 
570 ObjectLinkingLayer::Plugin::SyntheticSymbolDependenciesMap
571 ELFNixPlatform::ELFNixPlatformPlugin::getSyntheticSymbolDependencies(
572     MaterializationResponsibility &MR) {
573   std::lock_guard<std::mutex> Lock(PluginMutex);
574   auto I = InitSymbolDeps.find(&MR);
575   if (I != InitSymbolDeps.end()) {
576     SyntheticSymbolDependenciesMap Result;
577     Result[MR.getInitializerSymbol()] = std::move(I->second);
578     InitSymbolDeps.erase(&MR);
579     return Result;
580   }
581   return SyntheticSymbolDependenciesMap();
582 }
583 
584 void ELFNixPlatform::ELFNixPlatformPlugin::addInitializerSupportPasses(
585     MaterializationResponsibility &MR, jitlink::PassConfiguration &Config) {
586 
587   /// Preserve init sections.
588   Config.PrePrunePasses.push_back([this, &MR](jitlink::LinkGraph &G) -> Error {
589     if (auto Err = preserveInitSections(G, MR))
590       return Err;
591     return Error::success();
592   });
593 
594   Config.PostFixupPasses.push_back(
595       [this, &JD = MR.getTargetJITDylib()](jitlink::LinkGraph &G) {
596         return registerInitSections(G, JD);
597       });
598 }
599 
600 void ELFNixPlatform::ELFNixPlatformPlugin::addDSOHandleSupportPasses(
601     MaterializationResponsibility &MR, jitlink::PassConfiguration &Config) {
602 
603   Config.PostAllocationPasses.push_back([this, &JD = MR.getTargetJITDylib()](
604                                             jitlink::LinkGraph &G) -> Error {
605     auto I = llvm::find_if(G.defined_symbols(), [this](jitlink::Symbol *Sym) {
606       return Sym->getName() == *MP.DSOHandleSymbol;
607     });
608     assert(I != G.defined_symbols().end() && "Missing DSO handle symbol");
609     {
610       std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
611       JITTargetAddress HandleAddr = (*I)->getAddress();
612       MP.HandleAddrToJITDylib[HandleAddr] = &JD;
613       assert(!MP.InitSeqs.count(&JD) && "InitSeq entry for JD already exists");
614       MP.InitSeqs.insert(
615           std::make_pair(&JD, ELFNixJITDylibInitializers(
616                                   JD.getName(), ExecutorAddress(HandleAddr))));
617     }
618     return Error::success();
619   });
620 }
621 
622 void ELFNixPlatform::ELFNixPlatformPlugin::addEHAndTLVSupportPasses(
623     MaterializationResponsibility &MR, jitlink::PassConfiguration &Config) {
624 
625   // Insert TLV lowering at the start of the PostPrunePasses, since we want
626   // it to run before GOT/PLT lowering.
627   Config.PostPrunePasses.insert(
628       Config.PostPrunePasses.begin(),
629       [this, &JD = MR.getTargetJITDylib()](jitlink::LinkGraph &G) {
630         return fixTLVSectionsAndEdges(G, JD);
631       });
632 
633   // Add a pass to register the final addresses of the eh-frame and TLV sections
634   // with the runtime.
635   Config.PostFixupPasses.push_back([this](jitlink::LinkGraph &G) -> Error {
636     ELFPerObjectSectionsToRegister POSR;
637 
638     if (auto *EHFrameSection = G.findSectionByName(EHFrameSectionName)) {
639       jitlink::SectionRange R(*EHFrameSection);
640       if (!R.empty())
641         POSR.EHFrameSection = {ExecutorAddress(R.getStart()),
642                                ExecutorAddress(R.getEnd())};
643     }
644 
645     // Get a pointer to the thread data section if there is one. It will be used
646     // below.
647     jitlink::Section *ThreadDataSection =
648         G.findSectionByName(ThreadDataSectionName);
649 
650     // Handle thread BSS section if there is one.
651     if (auto *ThreadBSSSection = G.findSectionByName(ThreadBSSSectionName)) {
652       // If there's already a thread data section in this graph then merge the
653       // thread BSS section content into it, otherwise just treat the thread
654       // BSS section as the thread data section.
655       if (ThreadDataSection)
656         G.mergeSections(*ThreadDataSection, *ThreadBSSSection);
657       else
658         ThreadDataSection = ThreadBSSSection;
659     }
660 
661     // Having merged thread BSS (if present) and thread data (if present),
662     // record the resulting section range.
663     if (ThreadDataSection) {
664       jitlink::SectionRange R(*ThreadDataSection);
665       if (!R.empty())
666         POSR.ThreadDataSection = {ExecutorAddress(R.getStart()),
667                                   ExecutorAddress(R.getEnd())};
668     }
669 
670     if (POSR.EHFrameSection.StartAddress ||
671         POSR.ThreadDataSection.StartAddress) {
672 
673       // If we're still bootstrapping the runtime then just record this
674       // frame for now.
675       if (!MP.RuntimeBootstrapped) {
676         std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
677         MP.BootstrapPOSRs.push_back(POSR);
678         return Error::success();
679       }
680 
681       // Otherwise register it immediately.
682       if (auto Err = MP.registerPerObjectSections(POSR))
683         return Err;
684     }
685 
686     return Error::success();
687   });
688 }
689 
690 Error ELFNixPlatform::ELFNixPlatformPlugin::preserveInitSections(
691     jitlink::LinkGraph &G, MaterializationResponsibility &MR) {
692 
693   JITLinkSymbolSet InitSectionSymbols;
694   for (auto &InitSectionName : InitSectionNames) {
695     // Skip non-init sections.
696     auto *InitSection = G.findSectionByName(InitSectionName);
697     if (!InitSection)
698       continue;
699 
700     // Make a pass over live symbols in the section: those blocks are already
701     // preserved.
702     DenseSet<jitlink::Block *> AlreadyLiveBlocks;
703     for (auto &Sym : InitSection->symbols()) {
704       auto &B = Sym->getBlock();
705       if (Sym->isLive() && Sym->getOffset() == 0 &&
706           Sym->getSize() == B.getSize() && !AlreadyLiveBlocks.count(&B)) {
707         InitSectionSymbols.insert(Sym);
708         AlreadyLiveBlocks.insert(&B);
709       }
710     }
711 
712     // Add anonymous symbols to preserve any not-already-preserved blocks.
713     for (auto *B : InitSection->blocks())
714       if (!AlreadyLiveBlocks.count(B))
715         InitSectionSymbols.insert(
716             &G.addAnonymousSymbol(*B, 0, B->getSize(), false, true));
717   }
718 
719   if (!InitSectionSymbols.empty()) {
720     std::lock_guard<std::mutex> Lock(PluginMutex);
721     InitSymbolDeps[&MR] = std::move(InitSectionSymbols);
722   }
723 
724   return Error::success();
725 }
726 
727 Error ELFNixPlatform::ELFNixPlatformPlugin::registerInitSections(
728     jitlink::LinkGraph &G, JITDylib &JD) {
729 
730   SmallVector<jitlink::Section *> InitSections;
731 
732   LLVM_DEBUG({ dbgs() << "ELFNixPlatform::registerInitSections\n"; });
733 
734   for (auto InitSectionName : InitSectionNames) {
735     if (auto *Sec = G.findSectionByName(InitSectionName)) {
736       InitSections.push_back(Sec);
737     }
738   }
739 
740   // Dump the scraped inits.
741   LLVM_DEBUG({
742     dbgs() << "ELFNixPlatform: Scraped " << G.getName() << " init sections:\n";
743     for (auto *Sec : InitSections) {
744       jitlink::SectionRange R(*Sec);
745       dbgs() << "  " << Sec->getName() << ": "
746              << formatv("[ {0:x} -- {1:x} ]", R.getStart(), R.getEnd()) << "\n";
747     }
748   });
749 
750   return MP.registerInitInfo(JD, InitSections);
751 }
752 
753 Error ELFNixPlatform::ELFNixPlatformPlugin::fixTLVSectionsAndEdges(
754     jitlink::LinkGraph &G, JITDylib &JD) {
755 
756   // TODO implement TLV support
757 
758   return Error::success();
759 }
760 
761 } // End namespace orc.
762 } // End namespace llvm.
763