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