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