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       {"__orc_rt_elfnix_create_pthread_key",
456        &orc_rt_elfnix_create_pthread_key}};
457 
458   SymbolLookupSet RuntimeSymbols;
459   std::vector<std::pair<SymbolStringPtr, ExecutorAddress *>> AddrsToRecord;
460   for (const auto &KV : Symbols) {
461     auto Name = ES.intern(KV.first);
462     RuntimeSymbols.add(Name);
463     AddrsToRecord.push_back({std::move(Name), KV.second});
464   }
465 
466   auto RuntimeSymbolAddrs = ES.lookup(
467       {{&PlatformJD, JITDylibLookupFlags::MatchAllSymbols}}, RuntimeSymbols);
468   if (!RuntimeSymbolAddrs)
469     return RuntimeSymbolAddrs.takeError();
470 
471   for (const auto &KV : AddrsToRecord) {
472     auto &Name = KV.first;
473     assert(RuntimeSymbolAddrs->count(Name) && "Missing runtime symbol?");
474     KV.second->setValue((*RuntimeSymbolAddrs)[Name].getAddress());
475   }
476 
477   if (auto Err = ES.callSPSWrapper<void()>(
478           orc_rt_elfnix_platform_bootstrap.getValue()))
479     return Err;
480 
481   // FIXME: Ordering is fuzzy here. We're probably best off saying
482   // "behavior is undefined if code that uses the runtime is added before
483   // the platform constructor returns", then move all this to the constructor.
484   RuntimeBootstrapped = true;
485   std::vector<ELFPerObjectSectionsToRegister> DeferredPOSRs;
486   {
487     std::lock_guard<std::mutex> Lock(PlatformMutex);
488     DeferredPOSRs = std::move(BootstrapPOSRs);
489   }
490 
491   for (auto &D : DeferredPOSRs)
492     if (auto Err = registerPerObjectSections(D))
493       return Err;
494 
495   return Error::success();
496 }
497 
498 Error ELFNixPlatform::registerInitInfo(
499     JITDylib &JD, ArrayRef<jitlink::Section *> InitSections) {
500 
501   std::unique_lock<std::mutex> Lock(PlatformMutex);
502 
503   ELFNixJITDylibInitializers *InitSeq = nullptr;
504   {
505     auto I = InitSeqs.find(&JD);
506     if (I == InitSeqs.end()) {
507       // If there's no init sequence entry yet then we need to look up the
508       // header symbol to force creation of one.
509       Lock.unlock();
510 
511       auto SearchOrder =
512           JD.withLinkOrderDo([](const JITDylibSearchOrder &SO) { return SO; });
513       if (auto Err = ES.lookup(SearchOrder, DSOHandleSymbol).takeError())
514         return Err;
515 
516       Lock.lock();
517       I = InitSeqs.find(&JD);
518       assert(I != InitSeqs.end() &&
519              "Entry missing after header symbol lookup?");
520     }
521     InitSeq = &I->second;
522   }
523 
524   for (auto *Sec : InitSections) {
525     // FIXME: Avoid copy here.
526     jitlink::SectionRange R(*Sec);
527     InitSeq->InitSections[Sec->getName()].push_back(
528         {ExecutorAddress(R.getStart()), ExecutorAddress(R.getEnd())});
529   }
530 
531   return Error::success();
532 }
533 
534 Error ELFNixPlatform::registerPerObjectSections(
535     const ELFPerObjectSectionsToRegister &POSR) {
536 
537   if (!orc_rt_elfnix_register_object_sections)
538     return make_error<StringError>("Attempting to register per-object "
539                                    "sections, but runtime support has not "
540                                    "been loaded yet",
541                                    inconvertibleErrorCode());
542 
543   Error ErrResult = Error::success();
544   if (auto Err = ES.callSPSWrapper<shared::SPSError(
545                      SPSELFPerObjectSectionsToRegister)>(
546           orc_rt_elfnix_register_object_sections.getValue(), ErrResult, POSR))
547     return Err;
548   return ErrResult;
549 }
550 
551 Expected<uint64_t> ELFNixPlatform::createPThreadKey() {
552   if (!orc_rt_elfnix_create_pthread_key)
553     return make_error<StringError>(
554         "Attempting to create pthread key in target, but runtime support has "
555         "not been loaded yet",
556         inconvertibleErrorCode());
557 
558   Expected<uint64_t> Result(0);
559   if (auto Err = ES.callSPSWrapper<SPSExpected<uint64_t>(void)>(
560           orc_rt_elfnix_create_pthread_key.getValue(), Result))
561     return std::move(Err);
562   return Result;
563 }
564 
565 void ELFNixPlatform::ELFNixPlatformPlugin::modifyPassConfig(
566     MaterializationResponsibility &MR, jitlink::LinkGraph &LG,
567     jitlink::PassConfiguration &Config) {
568 
569   // If the initializer symbol is the __dso_handle symbol then just add
570   // the DSO handle support passes.
571   if (MR.getInitializerSymbol() == MP.DSOHandleSymbol) {
572     addDSOHandleSupportPasses(MR, Config);
573     // The DSOHandle materialization unit doesn't require any other
574     // support, so we can bail out early.
575     return;
576   }
577 
578   // If the object contains initializers then add passes to record them.
579   if (MR.getInitializerSymbol())
580     addInitializerSupportPasses(MR, Config);
581 
582   // Add passes for eh-frame and TLV support.
583   addEHAndTLVSupportPasses(MR, Config);
584 }
585 
586 ObjectLinkingLayer::Plugin::SyntheticSymbolDependenciesMap
587 ELFNixPlatform::ELFNixPlatformPlugin::getSyntheticSymbolDependencies(
588     MaterializationResponsibility &MR) {
589   std::lock_guard<std::mutex> Lock(PluginMutex);
590   auto I = InitSymbolDeps.find(&MR);
591   if (I != InitSymbolDeps.end()) {
592     SyntheticSymbolDependenciesMap Result;
593     Result[MR.getInitializerSymbol()] = std::move(I->second);
594     InitSymbolDeps.erase(&MR);
595     return Result;
596   }
597   return SyntheticSymbolDependenciesMap();
598 }
599 
600 void ELFNixPlatform::ELFNixPlatformPlugin::addInitializerSupportPasses(
601     MaterializationResponsibility &MR, jitlink::PassConfiguration &Config) {
602 
603   /// Preserve init sections.
604   Config.PrePrunePasses.push_back([this, &MR](jitlink::LinkGraph &G) -> Error {
605     if (auto Err = preserveInitSections(G, MR))
606       return Err;
607     return Error::success();
608   });
609 
610   Config.PostFixupPasses.push_back(
611       [this, &JD = MR.getTargetJITDylib()](jitlink::LinkGraph &G) {
612         return registerInitSections(G, JD);
613       });
614 }
615 
616 void ELFNixPlatform::ELFNixPlatformPlugin::addDSOHandleSupportPasses(
617     MaterializationResponsibility &MR, jitlink::PassConfiguration &Config) {
618 
619   Config.PostAllocationPasses.push_back([this, &JD = MR.getTargetJITDylib()](
620                                             jitlink::LinkGraph &G) -> Error {
621     auto I = llvm::find_if(G.defined_symbols(), [this](jitlink::Symbol *Sym) {
622       return Sym->getName() == *MP.DSOHandleSymbol;
623     });
624     assert(I != G.defined_symbols().end() && "Missing DSO handle symbol");
625     {
626       std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
627       JITTargetAddress HandleAddr = (*I)->getAddress();
628       MP.HandleAddrToJITDylib[HandleAddr] = &JD;
629       assert(!MP.InitSeqs.count(&JD) && "InitSeq entry for JD already exists");
630       MP.InitSeqs.insert(
631           std::make_pair(&JD, ELFNixJITDylibInitializers(
632                                   JD.getName(), ExecutorAddress(HandleAddr))));
633     }
634     return Error::success();
635   });
636 }
637 
638 void ELFNixPlatform::ELFNixPlatformPlugin::addEHAndTLVSupportPasses(
639     MaterializationResponsibility &MR, jitlink::PassConfiguration &Config) {
640 
641   // Insert TLV lowering at the start of the PostPrunePasses, since we want
642   // it to run before GOT/PLT lowering.
643 
644   // TODO: Check that before the fixTLVSectionsAndEdges pass, the GOT/PLT build
645   // pass has done. Because the TLS descriptor need to be allocate in GOT.
646   Config.PostPrunePasses.push_back(
647       [this, &JD = MR.getTargetJITDylib()](jitlink::LinkGraph &G) {
648         return fixTLVSectionsAndEdges(G, JD);
649       });
650 
651   // Add a pass to register the final addresses of the eh-frame and TLV sections
652   // with the runtime.
653   Config.PostFixupPasses.push_back([this](jitlink::LinkGraph &G) -> Error {
654     ELFPerObjectSectionsToRegister POSR;
655 
656     if (auto *EHFrameSection = G.findSectionByName(EHFrameSectionName)) {
657       jitlink::SectionRange R(*EHFrameSection);
658       if (!R.empty())
659         POSR.EHFrameSection = {ExecutorAddress(R.getStart()),
660                                ExecutorAddress(R.getEnd())};
661     }
662 
663     // Get a pointer to the thread data section if there is one. It will be used
664     // below.
665     jitlink::Section *ThreadDataSection =
666         G.findSectionByName(ThreadDataSectionName);
667 
668     // Handle thread BSS section if there is one.
669     if (auto *ThreadBSSSection = G.findSectionByName(ThreadBSSSectionName)) {
670       // If there's already a thread data section in this graph then merge the
671       // thread BSS section content into it, otherwise just treat the thread
672       // BSS section as the thread data section.
673       if (ThreadDataSection)
674         G.mergeSections(*ThreadDataSection, *ThreadBSSSection);
675       else
676         ThreadDataSection = ThreadBSSSection;
677     }
678 
679     // Having merged thread BSS (if present) and thread data (if present),
680     // record the resulting section range.
681     if (ThreadDataSection) {
682       jitlink::SectionRange R(*ThreadDataSection);
683       if (!R.empty())
684         POSR.ThreadDataSection = {ExecutorAddress(R.getStart()),
685                                   ExecutorAddress(R.getEnd())};
686     }
687 
688     if (POSR.EHFrameSection.StartAddress ||
689         POSR.ThreadDataSection.StartAddress) {
690 
691       // If we're still bootstrapping the runtime then just record this
692       // frame for now.
693       if (!MP.RuntimeBootstrapped) {
694         std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
695         MP.BootstrapPOSRs.push_back(POSR);
696         return Error::success();
697       }
698 
699       // Otherwise register it immediately.
700       if (auto Err = MP.registerPerObjectSections(POSR))
701         return Err;
702     }
703 
704     return Error::success();
705   });
706 }
707 
708 Error ELFNixPlatform::ELFNixPlatformPlugin::preserveInitSections(
709     jitlink::LinkGraph &G, MaterializationResponsibility &MR) {
710 
711   JITLinkSymbolSet InitSectionSymbols;
712   for (auto &InitSectionName : InitSectionNames) {
713     // Skip non-init sections.
714     auto *InitSection = G.findSectionByName(InitSectionName);
715     if (!InitSection)
716       continue;
717 
718     // Make a pass over live symbols in the section: those blocks are already
719     // preserved.
720     DenseSet<jitlink::Block *> AlreadyLiveBlocks;
721     for (auto &Sym : InitSection->symbols()) {
722       auto &B = Sym->getBlock();
723       if (Sym->isLive() && Sym->getOffset() == 0 &&
724           Sym->getSize() == B.getSize() && !AlreadyLiveBlocks.count(&B)) {
725         InitSectionSymbols.insert(Sym);
726         AlreadyLiveBlocks.insert(&B);
727       }
728     }
729 
730     // Add anonymous symbols to preserve any not-already-preserved blocks.
731     for (auto *B : InitSection->blocks())
732       if (!AlreadyLiveBlocks.count(B))
733         InitSectionSymbols.insert(
734             &G.addAnonymousSymbol(*B, 0, B->getSize(), false, true));
735   }
736 
737   if (!InitSectionSymbols.empty()) {
738     std::lock_guard<std::mutex> Lock(PluginMutex);
739     InitSymbolDeps[&MR] = std::move(InitSectionSymbols);
740   }
741 
742   return Error::success();
743 }
744 
745 Error ELFNixPlatform::ELFNixPlatformPlugin::registerInitSections(
746     jitlink::LinkGraph &G, JITDylib &JD) {
747 
748   SmallVector<jitlink::Section *> InitSections;
749 
750   LLVM_DEBUG({ dbgs() << "ELFNixPlatform::registerInitSections\n"; });
751 
752   for (auto InitSectionName : InitSectionNames) {
753     if (auto *Sec = G.findSectionByName(InitSectionName)) {
754       InitSections.push_back(Sec);
755     }
756   }
757 
758   // Dump the scraped inits.
759   LLVM_DEBUG({
760     dbgs() << "ELFNixPlatform: Scraped " << G.getName() << " init sections:\n";
761     for (auto *Sec : InitSections) {
762       jitlink::SectionRange R(*Sec);
763       dbgs() << "  " << Sec->getName() << ": "
764              << formatv("[ {0:x} -- {1:x} ]", R.getStart(), R.getEnd()) << "\n";
765     }
766   });
767 
768   return MP.registerInitInfo(JD, InitSections);
769 }
770 
771 Error ELFNixPlatform::ELFNixPlatformPlugin::fixTLVSectionsAndEdges(
772     jitlink::LinkGraph &G, JITDylib &JD) {
773 
774   // TODO implement TLV support
775   for (auto *Sym : G.external_symbols())
776     if (Sym->getName() == "__tls_get_addr") {
777       Sym->setName("___orc_rt_elfnix_tls_get_addr");
778     }
779 
780   auto *TLSInfoEntrySection = G.findSectionByName("$__TLSINFO");
781 
782   if (TLSInfoEntrySection) {
783     Optional<uint64_t> Key;
784     {
785       std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
786       auto I = MP.JITDylibToPThreadKey.find(&JD);
787       if (I != MP.JITDylibToPThreadKey.end())
788         Key = I->second;
789     }
790     if (!Key) {
791       if (auto KeyOrErr = MP.createPThreadKey())
792         Key = *KeyOrErr;
793       else
794         return KeyOrErr.takeError();
795     }
796 
797     uint64_t PlatformKeyBits =
798         support::endian::byte_swap(*Key, G.getEndianness());
799 
800     for (auto *B : TLSInfoEntrySection->blocks()) {
801       // FIXME: The TLS descriptor byte length may different with different
802       // ISA
803       assert(B->getSize() == (G.getPointerSize() * 2) &&
804              "TLS descriptor must be 2 words length");
805       auto TLSInfoEntryContent = B->getMutableContent(G);
806       memcpy(TLSInfoEntryContent.data(), &PlatformKeyBits, G.getPointerSize());
807     }
808   }
809 
810   return Error::success();
811 }
812 
813 } // End namespace orc.
814 } // End namespace llvm.
815