LLVM 24.0.0git
ELFNixPlatform.cpp
Go to the documentation of this file.
1//===----- ELFNixPlatform.cpp - Utilities for executing ELFNix 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
10
20#include "llvm/Support/Debug.h"
21#include <optional>
22
23#define DEBUG_TYPE "orc"
24
25using namespace llvm;
26using namespace llvm::orc;
27using namespace llvm::orc::shared;
28
29namespace {
30
31template <typename SPSSerializer, typename... ArgTs>
33getArgDataBufferType(const ArgTs &...Args) {
35 ArgData.resize(SPSSerializer::size(Args...));
36 SPSOutputBuffer OB(ArgData.empty() ? nullptr : ArgData.data(),
37 ArgData.size());
38 if (SPSSerializer::serialize(OB, Args...))
39 return ArgData;
40 return {};
41}
42
43std::unique_ptr<jitlink::LinkGraph> createPlatformGraph(ELFNixPlatform &MOP,
44 std::string Name) {
45 auto &ES = MOP.getExecutionSession();
46 return std::make_unique<jitlink::LinkGraph>(
47 std::move(Name), ES.getSymbolStringPool(), ES.getTargetTriple(),
49}
50
51// Creates a Bootstrap-Complete LinkGraph to run deferred actions.
52class ELFNixPlatformCompleteBootstrapMaterializationUnit
53 : public MaterializationUnit {
54public:
55 ELFNixPlatformCompleteBootstrapMaterializationUnit(
56 ELFNixPlatform &MOP, StringRef PlatformJDName,
57 SymbolStringPtr CompleteBootstrapSymbol, DeferredRuntimeFnMap DeferredAAs,
58 ExecutorAddr ELFNixHeaderAddr, ExecutorAddr PlatformBootstrap,
59 ExecutorAddr PlatformShutdown, ExecutorAddr RegisterJITDylib,
60 ExecutorAddr DeregisterJITDylib)
62 {{{CompleteBootstrapSymbol, JITSymbolFlags::None}}, nullptr}),
63 MOP(MOP), PlatformJDName(PlatformJDName),
64 CompleteBootstrapSymbol(std::move(CompleteBootstrapSymbol)),
65 DeferredAAsMap(std::move(DeferredAAs)),
66 ELFNixHeaderAddr(ELFNixHeaderAddr),
67 PlatformBootstrap(PlatformBootstrap),
68 PlatformShutdown(PlatformShutdown), RegisterJITDylib(RegisterJITDylib),
69 DeregisterJITDylib(DeregisterJITDylib) {}
70
71 StringRef getName() const override {
72 return "ELFNixPlatformCompleteBootstrap";
73 }
74
75 void materialize(std::unique_ptr<MaterializationResponsibility> R) override {
76 using namespace jitlink;
77 auto G = createPlatformGraph(MOP, "<OrcRTCompleteBootstrap>");
78 auto &PlaceholderSection =
79 G->createSection("__orc_rt_cplt_bs", MemProt::Read);
80 auto &PlaceholderBlock =
81 G->createZeroFillBlock(PlaceholderSection, 1, ExecutorAddr(), 1, 0);
82 G->addDefinedSymbol(PlaceholderBlock, 0, *CompleteBootstrapSymbol, 1,
83 Linkage::Strong, Scope::Hidden, false, true);
84
85 // 1. Bootstrap the platform support code.
86 G->allocActions().push_back(
88 PlatformBootstrap, ELFNixHeaderAddr)),
90 WrapperFunctionCall::Create<SPSArgList<>>(PlatformShutdown))});
91
92 // 2. Register the platform JITDylib.
93 G->allocActions().push_back(
96 RegisterJITDylib, PlatformJDName, ELFNixHeaderAddr)),
98 DeregisterJITDylib, ELFNixHeaderAddr))});
99
100 // 4. Add the deferred actions to the graph.
101 for (auto &[Fn, CallDatas] : DeferredAAsMap) {
102 for (auto &CallData : CallDatas) {
103 G->allocActions().push_back(
104 {WrapperFunctionCall(Fn.first->Addr, std::move(CallData.first)),
105 WrapperFunctionCall(Fn.second->Addr, std::move(CallData.second))});
106 }
107 }
108
109 MOP.getObjectLinkingLayer().emit(std::move(R), std::move(G));
110 }
111
112 void discard(const JITDylib &JD, const SymbolStringPtr &Sym) override {}
113
114private:
115 ELFNixPlatform &MOP;
116 StringRef PlatformJDName;
117 SymbolStringPtr CompleteBootstrapSymbol;
118 DeferredRuntimeFnMap DeferredAAsMap;
119 ExecutorAddr ELFNixHeaderAddr;
120 ExecutorAddr PlatformBootstrap;
121 ExecutorAddr PlatformShutdown;
122 ExecutorAddr RegisterJITDylib;
123 ExecutorAddr DeregisterJITDylib;
124};
125
126class DSOHandleMaterializationUnit : public MaterializationUnit {
127public:
128 DSOHandleMaterializationUnit(ELFNixPlatform &ENP,
129 const SymbolStringPtr &DSOHandleSymbol)
131 createDSOHandleSectionInterface(ENP, DSOHandleSymbol)),
132 ENP(ENP) {}
133
134 StringRef getName() const override { return "DSOHandleMU"; }
135
136 void materialize(std::unique_ptr<MaterializationResponsibility> R) override {
137
138 auto &ES = ENP.getExecutionSession();
139
140 jitlink::Edge::Kind EdgeKind;
141
142 switch (ES.getTargetTriple().getArch()) {
143 case Triple::x86_64:
145 break;
146 case Triple::aarch64:
148 break;
149 case Triple::ppc64:
150 EdgeKind = jitlink::ppc64::Pointer64;
151 break;
152 case Triple::ppc64le:
153 EdgeKind = jitlink::ppc64::Pointer64;
154 break;
157 break;
158 case Triple::systemz:
160 break;
161 default:
162 llvm_unreachable("Unrecognized architecture");
163 }
164
165 // void *__dso_handle = &__dso_handle;
166 auto G = std::make_unique<jitlink::LinkGraph>(
167 "<DSOHandleMU>", ES.getSymbolStringPool(), ES.getTargetTriple(),
169 auto &DSOHandleSection =
170 G->createSection(".data.__dso_handle", MemProt::Read);
171 auto &DSOHandleBlock = G->createContentBlock(
172 DSOHandleSection, getDSOHandleContent(G->getPointerSize()),
173 orc::ExecutorAddr(), 8, 0);
174 auto &DSOHandleSymbol = G->addDefinedSymbol(
175 DSOHandleBlock, 0, *R->getInitializerSymbol(), DSOHandleBlock.getSize(),
177 DSOHandleBlock.addEdge(EdgeKind, 0, DSOHandleSymbol, 0);
178
179 ENP.getObjectLinkingLayer().emit(std::move(R), std::move(G));
180 }
181
182 void discard(const JITDylib &JD, const SymbolStringPtr &Sym) override {}
183
184private:
186 createDSOHandleSectionInterface(ELFNixPlatform &ENP,
187 const SymbolStringPtr &DSOHandleSymbol) {
188 SymbolFlagsMap SymbolFlags;
189 SymbolFlags[DSOHandleSymbol] = JITSymbolFlags::Exported;
190 return MaterializationUnit::Interface(std::move(SymbolFlags),
191 DSOHandleSymbol);
192 }
193
194 ArrayRef<char> getDSOHandleContent(size_t PointerSize) {
195 static const char Content[8] = {0};
196 assert(PointerSize <= sizeof Content);
197 return {Content, PointerSize};
198 }
199
200 ELFNixPlatform &ENP;
201};
202
203} // end anonymous namespace
204
205namespace llvm {
206namespace orc {
207
208Expected<std::unique_ptr<ELFNixPlatform>>
210 JITDylib &PlatformJD,
211 std::unique_ptr<DefinitionGenerator> OrcRuntime,
212 std::optional<SymbolAliasMap> RuntimeAliases) {
213
214 auto &ES = ObjLinkingLayer.getExecutionSession();
215
216 // If the target is not supported then bail out immediately.
217 if (!supportedTarget(ES.getTargetTriple()))
218 return make_error<StringError>("Unsupported ELFNixPlatform triple: " +
219 ES.getTargetTriple().str(),
221
222 // Create default aliases if the caller didn't supply any.
223 if (!RuntimeAliases) {
224 auto StandardRuntimeAliases = standardPlatformAliases(ES, PlatformJD);
225 if (!StandardRuntimeAliases)
226 return StandardRuntimeAliases.takeError();
227 RuntimeAliases = std::move(*StandardRuntimeAliases);
228 }
229
230 // Define the aliases.
231 if (auto Err = PlatformJD.define(symbolAliases(std::move(*RuntimeAliases))))
232 return std::move(Err);
233
234 {
235 // Add JIT dispatch reexports from bootstrap JITDylib.
236 auto Exports = buildSimpleReexportsAliasMap(
237 ES.getBootstrapJITDylib(),
238 {{ES.intern(rt::DispatchName), ES.intern(rt::DispatchCtxName)}});
239 if (!Exports)
240 return Exports.takeError();
241 if (auto Err =
242 PlatformJD.define(reexports(ES.getBootstrapJITDylib(), *Exports)))
243 return Err;
244 }
245
246 // Create the instance.
247 Error Err = Error::success();
248 auto P = std::unique_ptr<ELFNixPlatform>(new ELFNixPlatform(
249 ObjLinkingLayer, PlatformJD, std::move(OrcRuntime), Err));
250 if (Err)
251 return std::move(Err);
252 return std::move(P);
253}
254
257 JITDylib &PlatformJD, const char *OrcRuntimePath,
258 std::optional<SymbolAliasMap> RuntimeAliases) {
259
260 // Create a generator for the ORC runtime archive.
261 auto OrcRuntimeArchiveGenerator =
262 StaticLibraryDefinitionGenerator::Load(ObjLinkingLayer, OrcRuntimePath);
263 if (!OrcRuntimeArchiveGenerator)
264 return OrcRuntimeArchiveGenerator.takeError();
265
266 return Create(ObjLinkingLayer, PlatformJD,
267 std::move(*OrcRuntimeArchiveGenerator),
268 std::move(RuntimeAliases));
269}
270
272 if (auto Err = JD.define(std::make_unique<DSOHandleMaterializationUnit>(
273 *this, DSOHandleSymbol)))
274 return Err;
275
276 return ES.lookup({&JD}, DSOHandleSymbol).takeError();
277}
278
280 std::lock_guard<std::mutex> Lock(PlatformMutex);
281 auto I = JITDylibToHandleAddr.find(&JD);
282 if (I != JITDylibToHandleAddr.end()) {
283 assert(HandleAddrToJITDylib.count(I->second) &&
284 "HandleAddrToJITDylib missing entry");
285 HandleAddrToJITDylib.erase(I->second);
286 JITDylibToHandleAddr.erase(I);
287 }
288 return Error::success();
289}
290
292 const MaterializationUnit &MU) {
293
294 auto &JD = RT.getJITDylib();
295 const auto &InitSym = MU.getInitializerSymbol();
296 if (!InitSym)
297 return Error::success();
298
299 RegisteredInitSymbols[&JD].add(InitSym,
301 LLVM_DEBUG({
302 dbgs() << "ELFNixPlatform: Registered init symbol " << *InitSym
303 << " for MU " << MU.getName() << "\n";
304 });
305 return Error::success();
306}
307
311
313 ArrayRef<std::pair<const char *, const char *>> AL) {
314 for (auto &KV : AL) {
315 auto AliasName = ES.intern(KV.first);
316 assert(!Aliases.count(AliasName) && "Duplicate symbol name in alias map");
317 Aliases[std::move(AliasName)] = {ES.intern(KV.second),
319 }
320}
321
324 JITDylib &PlatformJD) {
325 SymbolAliasMap Aliases;
326 addAliases(ES, Aliases, requiredCXXAliases());
329 return Aliases;
330}
331
334 static const std::pair<const char *, const char *> RequiredCXXAliases[] = {
335 {"__cxa_atexit", "__orc_rt_elfnix_cxa_atexit"},
336 {"atexit", "__orc_rt_elfnix_atexit"}};
337
338 return ArrayRef<std::pair<const char *, const char *>>(RequiredCXXAliases);
339}
340
343 static const std::pair<const char *, const char *>
344 StandardRuntimeUtilityAliases[] = {
345 {"__orc_rt_run_program", "__orc_rt_elfnix_run_program"},
346 {"__orc_rt_jit_dlerror", "__orc_rt_elfnix_jit_dlerror"},
347 {"__orc_rt_jit_dlopen", "__orc_rt_elfnix_jit_dlopen"},
348 {"__orc_rt_jit_dlupdate", "__orc_rt_elfnix_jit_dlupdate"},
349 {"__orc_rt_jit_dlclose", "__orc_rt_elfnix_jit_dlclose"},
350 {"__orc_rt_jit_dlsym", "__orc_rt_elfnix_jit_dlsym"},
351 {"__orc_rt_log_error", "__orc_rt_log_error_to_stderr"}};
352
354 StandardRuntimeUtilityAliases);
355}
356
359 static const std::pair<const char *, const char *>
360 StandardLazyCompilationAliases[] = {
361 {"__orc_rt_reenter", "__orc_rt_sysv_reenter"}};
362
364 StandardLazyCompilationAliases);
365}
366
367bool ELFNixPlatform::supportedTarget(const Triple &TT) {
368 switch (TT.getArch()) {
369 case Triple::x86_64:
370 case Triple::aarch64:
371 // FIXME: jitlink for ppc64 hasn't been well tested, leave it unsupported
372 // right now.
373 case Triple::ppc64le:
375 case Triple::systemz:
376 return true;
377 default:
378 return false;
379 }
380}
381
382ELFNixPlatform::ELFNixPlatform(
383 ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD,
384 std::unique_ptr<DefinitionGenerator> OrcRuntimeGenerator, Error &Err)
385 : ES(ObjLinkingLayer.getExecutionSession()), PlatformJD(PlatformJD),
386 ObjLinkingLayer(ObjLinkingLayer),
387 DSOHandleSymbol(ES.intern("__dso_handle")) {
389 ObjLinkingLayer.addPlugin(std::make_unique<ELFNixPlatformPlugin>(*this));
390
391 PlatformJD.addGenerator(std::move(OrcRuntimeGenerator));
392
393 BootstrapInfo BI;
394 Bootstrap = &BI;
395
396 // PlatformJD hasn't been 'set-up' by the platform yet (since we're creating
397 // the platform now), so set it up.
398 if (auto E2 = setupJITDylib(PlatformJD)) {
399 Err = std::move(E2);
400 return;
401 }
402
403 // Step (2) Request runtime registration functions to trigger
404 // materialization..
405 if ((Err = ES.lookup(
406 makeJITDylibSearchOrder(&PlatformJD),
407 SymbolLookupSet(
408 {PlatformBootstrap.Name, PlatformShutdown.Name,
409 RegisterJITDylib.Name, DeregisterJITDylib.Name,
410 RegisterInitSections.Name, DeregisterInitSections.Name,
411 RegisterFiniSections.Name, DeregisterFiniSections.Name,
412 RegisterObjectSections.Name,
413 DeregisterObjectSections.Name, CreatePThreadKey.Name}))
414 .takeError()))
415 return;
416
417 // Step (3) Wait for any incidental linker work to complete.
418 {
419 std::unique_lock<std::mutex> Lock(BI.Mutex);
420 BI.CV.wait(Lock, [&]() { return BI.ActiveGraphs == 0; });
421 Bootstrap = nullptr;
422 }
423
424 // Step (4) Add complete-bootstrap materialization unit and request.
425 auto BootstrapCompleteSymbol =
426 ES.intern("__orc_rt_elfnix_complete_bootstrap");
427 if ((Err = PlatformJD.define(
428 std::make_unique<ELFNixPlatformCompleteBootstrapMaterializationUnit>(
429 *this, PlatformJD.getName(), BootstrapCompleteSymbol,
430 std::move(BI.DeferredRTFnMap), BI.ELFNixHeaderAddr,
431 PlatformBootstrap.Addr, PlatformShutdown.Addr,
432 RegisterJITDylib.Addr, DeregisterJITDylib.Addr))))
433 return;
434 if ((Err = ES.lookup(makeJITDylibSearchOrder(
436 std::move(BootstrapCompleteSymbol))
437 .takeError()))
438 return;
439
440 // Associate wrapper function tags with JIT-side function implementations.
441 if (auto E2 = associateRuntimeSupportFunctions(PlatformJD)) {
442 Err = std::move(E2);
443 return;
444 }
445}
446
447Error ELFNixPlatform::associateRuntimeSupportFunctions(JITDylib &PlatformJD) {
449
450 using RecordInitializersSPSSig =
451 SPSExpected<SPSELFNixJITDylibDepInfoMap>(SPSExecutorAddr);
452 WFs[ES.intern("__orc_rt_elfnix_push_initializers_tag")] =
453 ES.wrapAsyncWithSPS<RecordInitializersSPSSig>(
454 this, &ELFNixPlatform::rt_recordInitializers);
455
456 using LookupSymbolSPSSig =
457 SPSExpected<SPSExecutorAddr>(SPSExecutorAddr, SPSString);
458 WFs[ES.intern("__orc_rt_elfnix_symbol_lookup_tag")] =
459 ES.wrapAsyncWithSPS<LookupSymbolSPSSig>(this,
460 &ELFNixPlatform::rt_lookupSymbol);
461
462 return ES.registerJITDispatchHandlers(PlatformJD, std::move(WFs));
463}
464
465void ELFNixPlatform::pushInitializersLoop(
466 PushInitializersSendResultFn SendResult, JITDylibSP JD) {
467 DenseMap<JITDylib *, SymbolLookupSet> NewInitSymbols;
468 DenseMap<JITDylib *, SmallVector<JITDylib *>> JDDepMap;
469 SmallVector<JITDylib *, 16> Worklist({JD.get()});
470
471 ES.runSessionLocked([&]() {
472 while (!Worklist.empty()) {
473 // FIXME: Check for defunct dylibs.
474
475 auto DepJD = Worklist.back();
476 Worklist.pop_back();
477
478 // If we've already visited this JITDylib on this iteration then continue.
479 auto [It, Inserted] = JDDepMap.try_emplace(DepJD);
480 if (!Inserted)
481 continue;
482
483 // Add dep info.
484 auto &DM = It->second;
485 DepJD->withLinkOrderDo([&](const JITDylibSearchOrder &O) {
486 for (auto &KV : O) {
487 if (KV.first == DepJD)
488 continue;
489 DM.push_back(KV.first);
490 Worklist.push_back(KV.first);
491 }
492 });
493
494 // Add any registered init symbols.
495 auto RISItr = RegisteredInitSymbols.find(DepJD);
496 if (RISItr != RegisteredInitSymbols.end()) {
497 NewInitSymbols[DepJD] = std::move(RISItr->second);
498 RegisteredInitSymbols.erase(RISItr);
499 }
500 }
501 });
502
503 // If there are no further init symbols to look up then send the link order
504 // (as a list of header addresses) to the caller.
505 if (NewInitSymbols.empty()) {
506
507 // To make the list intelligible to the runtime we need to convert all
508 // JITDylib pointers to their header addresses. Only include JITDylibs
509 // that appear in the JITDylibToHandleAddr map (i.e. those that have been
510 // through setupJITDylib) -- bare JITDylibs aren't managed by the platform.
511 DenseMap<JITDylib *, ExecutorAddr> HeaderAddrs;
512 HeaderAddrs.reserve(JDDepMap.size());
513 {
514 std::lock_guard<std::mutex> Lock(PlatformMutex);
515 for (auto &KV : JDDepMap) {
516 auto I = JITDylibToHandleAddr.find(KV.first);
517 if (I != JITDylibToHandleAddr.end())
518 HeaderAddrs[KV.first] = I->second;
519 }
520 }
521
522 // Build the dep info map to return.
524 DIM.reserve(JDDepMap.size());
525 for (auto &KV : JDDepMap) {
526 auto HI = HeaderAddrs.find(KV.first);
527 // Skip unmanaged JITDylibs.
528 if (HI == HeaderAddrs.end())
529 continue;
530 auto H = HI->second;
531 ELFNixJITDylibDepInfo DepInfo;
532 for (auto &Dep : KV.second) {
533 auto HJ = HeaderAddrs.find(Dep);
534 if (HJ != HeaderAddrs.end())
535 DepInfo.push_back(HJ->second);
536 }
537 DIM.push_back(std::make_pair(H, std::move(DepInfo)));
538 }
539 SendResult(DIM);
540 return;
541 }
542
543 // Otherwise issue a lookup and re-run this phase when it completes.
545 [this, SendResult = std::move(SendResult), JD](Error Err) mutable {
546 if (Err)
547 SendResult(std::move(Err));
548 else
549 pushInitializersLoop(std::move(SendResult), JD);
550 },
551 ES, std::move(NewInitSymbols));
552}
553
554void ELFNixPlatform::rt_recordInitializers(
555 PushInitializersSendResultFn SendResult, ExecutorAddr JDHeaderAddr) {
556 JITDylibSP JD;
557 {
558 std::lock_guard<std::mutex> Lock(PlatformMutex);
559 auto I = HandleAddrToJITDylib.find(JDHeaderAddr);
560 if (I != HandleAddrToJITDylib.end())
561 JD = I->second;
562 }
563
564 LLVM_DEBUG({
565 dbgs() << "ELFNixPlatform::rt_recordInitializers(" << JDHeaderAddr << ") ";
566 if (JD)
567 dbgs() << "pushing initializers for " << JD->getName() << "\n";
568 else
569 dbgs() << "No JITDylib for header address.\n";
570 });
571
572 if (!JD) {
573 SendResult(make_error<StringError>("No JITDylib with header addr " +
574 formatv("{0:x}", JDHeaderAddr),
576 return;
577 }
578
579 pushInitializersLoop(std::move(SendResult), JD);
580}
581
582void ELFNixPlatform::rt_lookupSymbol(SendSymbolAddressFn SendResult,
583 ExecutorAddr Handle,
584 StringRef SymbolName) {
585 LLVM_DEBUG({
586 dbgs() << "ELFNixPlatform::rt_lookupSymbol(\"" << Handle << "\")\n";
587 });
588
589 JITDylib *JD = nullptr;
590
591 {
592 std::lock_guard<std::mutex> Lock(PlatformMutex);
593 auto I = HandleAddrToJITDylib.find(Handle);
594 if (I != HandleAddrToJITDylib.end())
595 JD = I->second;
596 }
597
598 if (!JD) {
599 LLVM_DEBUG(dbgs() << " No JITDylib for handle " << Handle << "\n");
600 SendResult(make_error<StringError>("No JITDylib associated with handle " +
601 formatv("{0:x}", Handle),
603 return;
604 }
605
606 // Use functor class to work around XL build compiler issue on AIX.
607 class RtLookupNotifyComplete {
608 public:
609 RtLookupNotifyComplete(SendSymbolAddressFn &&SendResult)
610 : SendResult(std::move(SendResult)) {}
611 void operator()(Expected<SymbolMap> Result) {
612 if (Result) {
613 assert(Result->size() == 1 && "Unexpected result map count");
614 SendResult(Result->begin()->second.getAddress());
615 } else {
616 SendResult(Result.takeError());
617 }
618 }
619
620 private:
621 SendSymbolAddressFn SendResult;
622 };
623
624 ES.lookup(
626 SymbolLookupSet(ES.intern(SymbolName)), SymbolState::Ready,
627 RtLookupNotifyComplete(std::move(SendResult)), NoDependenciesToRegister);
628}
629
630Error ELFNixPlatform::ELFNixPlatformPlugin::bootstrapPipelineStart(
631 jitlink::LinkGraph &G) {
632 // Increment the active graphs count in BootstrapInfo.
633 std::lock_guard<std::mutex> Lock(MP.Bootstrap.load()->Mutex);
634 ++MP.Bootstrap.load()->ActiveGraphs;
635 return Error::success();
636}
637
638Error ELFNixPlatform::ELFNixPlatformPlugin::
639 bootstrapPipelineRecordRuntimeFunctions(jitlink::LinkGraph &G) {
640 // Record bootstrap function names.
641 std::pair<StringRef, ExecutorAddr *> RuntimeSymbols[] = {
642 {*MP.DSOHandleSymbol, &MP.Bootstrap.load()->ELFNixHeaderAddr},
643 {*MP.PlatformBootstrap.Name, &MP.PlatformBootstrap.Addr},
644 {*MP.PlatformShutdown.Name, &MP.PlatformShutdown.Addr},
645 {*MP.RegisterJITDylib.Name, &MP.RegisterJITDylib.Addr},
646 {*MP.DeregisterJITDylib.Name, &MP.DeregisterJITDylib.Addr},
647 {*MP.RegisterObjectSections.Name, &MP.RegisterObjectSections.Addr},
648 {*MP.DeregisterObjectSections.Name, &MP.DeregisterObjectSections.Addr},
649 {*MP.RegisterInitSections.Name, &MP.RegisterInitSections.Addr},
650 {*MP.DeregisterInitSections.Name, &MP.DeregisterInitSections.Addr},
651 {*MP.RegisterFiniSections.Name, &MP.RegisterFiniSections.Addr},
652 {*MP.DeregisterFiniSections.Name, &MP.DeregisterFiniSections.Addr},
653 {*MP.CreatePThreadKey.Name, &MP.CreatePThreadKey.Addr}};
654
655 bool RegisterELFNixHeader = false;
656
657 for (auto *Sym : G.defined_symbols()) {
658 for (auto &RTSym : RuntimeSymbols) {
659 if (Sym->hasName() && *Sym->getName() == RTSym.first) {
660 if (*RTSym.second)
662 "Duplicate " + RTSym.first +
663 " detected during ELFNixPlatform bootstrap",
665
666 if (*Sym->getName() == *MP.DSOHandleSymbol)
667 RegisterELFNixHeader = true;
668
669 *RTSym.second = Sym->getAddress();
670 }
671 }
672 }
673
674 if (RegisterELFNixHeader) {
675 // If this graph defines the elfnix header symbol then create the internal
676 // mapping between it and PlatformJD.
677 std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
678 MP.JITDylibToHandleAddr[&MP.PlatformJD] =
679 MP.Bootstrap.load()->ELFNixHeaderAddr;
680 MP.HandleAddrToJITDylib[MP.Bootstrap.load()->ELFNixHeaderAddr] =
681 &MP.PlatformJD;
682 }
683
684 return Error::success();
685}
686
687Error ELFNixPlatform::ELFNixPlatformPlugin::bootstrapPipelineEnd(
688 jitlink::LinkGraph &G) {
689 std::lock_guard<std::mutex> Lock(MP.Bootstrap.load()->Mutex);
690 assert(MP.Bootstrap && "DeferredAAs reset before bootstrap completed");
691 --MP.Bootstrap.load()->ActiveGraphs;
692 // Notify Bootstrap->CV while holding the mutex because the mutex is
693 // also keeping Bootstrap->CV alive.
694 if (MP.Bootstrap.load()->ActiveGraphs == 0)
695 MP.Bootstrap.load()->CV.notify_all();
696 return Error::success();
697}
698
699Error ELFNixPlatform::registerPerObjectSections(
700 jitlink::LinkGraph &G, const ELFPerObjectSectionsToRegister &POSR,
701 bool IsBootstrapping) {
702 using SPSRegisterPerObjSectionsArgs =
703 SPSArgList<SPSELFPerObjectSectionsToRegister>;
704
705 if (LLVM_UNLIKELY(IsBootstrapping)) {
706 Bootstrap.load()->addArgumentsToRTFnMap(
707 &RegisterObjectSections, &DeregisterObjectSections,
708 getArgDataBufferType<SPSRegisterPerObjSectionsArgs>(POSR),
709 getArgDataBufferType<SPSRegisterPerObjSectionsArgs>(POSR));
710 return Error::success();
711 }
712
713 G.allocActions().push_back(
715 RegisterObjectSections.Addr, POSR)),
717 DeregisterObjectSections.Addr, POSR))});
718
719 return Error::success();
720}
721
722Expected<uint64_t> ELFNixPlatform::createPThreadKey() {
723 if (!CreatePThreadKey.Addr)
725 "Attempting to create pthread key in target, but runtime support has "
726 "not been loaded yet",
728
729 Expected<uint64_t> Result(0);
730 if (auto Err = ES.callSPSWrapper<SPSExpected<uint64_t>(void)>(
731 CreatePThreadKey.Addr, Result))
732 return std::move(Err);
733 return Result;
734}
735
736void ELFNixPlatform::ELFNixPlatformPlugin::modifyPassConfig(
737 MaterializationResponsibility &MR, jitlink::LinkGraph &LG,
738 jitlink::PassConfiguration &Config) {
739 using namespace jitlink;
740
741 bool InBootstrapPhase =
742 &MR.getTargetJITDylib() == &MP.PlatformJD && MP.Bootstrap;
743
744 // If we're in the bootstrap phase then increment the active graphs.
745 if (InBootstrapPhase) {
746 Config.PrePrunePasses.push_back(
747 [this](LinkGraph &G) { return bootstrapPipelineStart(G); });
748 Config.PostAllocationPasses.push_back([this](LinkGraph &G) {
749 return bootstrapPipelineRecordRuntimeFunctions(G);
750 });
751 }
752
753 // If the initializer symbol is the __dso_handle symbol then just add
754 // the DSO handle support passes.
755 if (auto InitSymbol = MR.getInitializerSymbol()) {
756 if (InitSymbol == MP.DSOHandleSymbol && !InBootstrapPhase) {
757 addDSOHandleSupportPasses(MR, Config);
758 // The DSOHandle materialization unit doesn't require any other
759 // support, so we can bail out early.
760 return;
761 }
762
763 /// Preserve init sections.
764 Config.PrePrunePasses.push_back(
765 [this, &MR](jitlink::LinkGraph &G) -> Error {
766 if (auto Err = preserveInitSections(G, MR))
767 return Err;
768 return Error::success();
769 });
770 }
771
772 // Add passes for eh-frame and TLV support.
773 addEHAndTLVSupportPasses(MR, Config, InBootstrapPhase);
774
775 // If the object contains initializers then add passes to record them.
776 Config.PostFixupPasses.push_back([this, &JD = MR.getTargetJITDylib(),
777 InBootstrapPhase](jitlink::LinkGraph &G) {
778 return registerInitSections(G, JD, InBootstrapPhase);
779 });
780
781 // If the object contains finalizers then add passes to record them.
782 Config.PostFixupPasses.push_back([this, &JD = MR.getTargetJITDylib(),
783 InBootstrapPhase](jitlink::LinkGraph &G) {
784 return registerFiniSections(G, JD, InBootstrapPhase);
785 });
786
787 // If we're in the bootstrap phase then steal allocation actions and then
788 // decrement the active graphs.
789 if (InBootstrapPhase)
790 Config.PostFixupPasses.push_back(
791 [this](LinkGraph &G) { return bootstrapPipelineEnd(G); });
792}
793
794void ELFNixPlatform::ELFNixPlatformPlugin::addDSOHandleSupportPasses(
795 MaterializationResponsibility &MR, jitlink::PassConfiguration &Config) {
796
797 Config.PostAllocationPasses.push_back([this, &JD = MR.getTargetJITDylib()](
798 jitlink::LinkGraph &G) -> Error {
799 auto I = llvm::find_if(G.defined_symbols(), [this](jitlink::Symbol *Sym) {
800 return Sym->getName() == MP.DSOHandleSymbol;
801 });
802 assert(I != G.defined_symbols().end() && "Missing DSO handle symbol");
803 {
804 std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
805 auto HandleAddr = (*I)->getAddress();
806 MP.HandleAddrToJITDylib[HandleAddr] = &JD;
807 MP.JITDylibToHandleAddr[&JD] = HandleAddr;
808
809 G.allocActions().push_back(
811 SPSArgList<SPSString, SPSExecutorAddr>>(
812 MP.RegisterJITDylib.Addr, JD.getName(), HandleAddr)),
813 cantFail(WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddr>>(
814 MP.DeregisterJITDylib.Addr, HandleAddr))});
815 }
816 return Error::success();
817 });
818}
819
820void ELFNixPlatform::ELFNixPlatformPlugin::addEHAndTLVSupportPasses(
821 MaterializationResponsibility &MR, jitlink::PassConfiguration &Config,
822 bool IsBootstrapping) {
823
824 // Insert TLV lowering at the start of the PostPrunePasses, since we want
825 // it to run before GOT/PLT lowering.
826
827 // TODO: Check that before the fixTLVSectionsAndEdges pass, the GOT/PLT build
828 // pass has done. Because the TLS descriptor need to be allocate in GOT.
829 Config.PostPrunePasses.push_back(
830 [this, &JD = MR.getTargetJITDylib()](jitlink::LinkGraph &G) {
831 return fixTLVSectionsAndEdges(G, JD);
832 });
833
834 // Add a pass to register the final addresses of the eh-frame and TLV sections
835 // with the runtime.
836 Config.PostFixupPasses.push_back([this, IsBootstrapping](
837 jitlink::LinkGraph &G) -> Error {
838 ELFPerObjectSectionsToRegister POSR;
839
840 if (auto *EHFrameSection = G.findSectionByName(ELFEHFrameSectionName)) {
841 jitlink::SectionRange R(*EHFrameSection);
842 if (!R.empty())
843 POSR.EHFrameSection = R.getRange();
844 }
845
846 // Get a pointer to the thread data section if there is one. It will be used
847 // below.
848 jitlink::Section *ThreadDataSection =
849 G.findSectionByName(ELFThreadDataSectionName);
850
851 // Handle thread BSS section if there is one.
852 if (auto *ThreadBSSSection = G.findSectionByName(ELFThreadBSSSectionName)) {
853 // If there's already a thread data section in this graph then merge the
854 // thread BSS section content into it, otherwise just treat the thread
855 // BSS section as the thread data section.
856 if (ThreadDataSection)
857 G.mergeSections(*ThreadDataSection, *ThreadBSSSection);
858 else
859 ThreadDataSection = ThreadBSSSection;
860 }
861
862 // Having merged thread BSS (if present) and thread data (if present),
863 // record the resulting section range.
864 if (ThreadDataSection) {
865 jitlink::SectionRange R(*ThreadDataSection);
866 if (!R.empty())
867 POSR.ThreadDataSection = R.getRange();
868 }
869
870 if (POSR.EHFrameSection.Start || POSR.ThreadDataSection.Start) {
871 if (auto Err = MP.registerPerObjectSections(G, POSR, IsBootstrapping))
872 return Err;
873 }
874
875 return Error::success();
876 });
877}
878
879Error ELFNixPlatform::ELFNixPlatformPlugin::preserveInitSections(
880 jitlink::LinkGraph &G, MaterializationResponsibility &MR) {
881
882 if (const auto &InitSymName = MR.getInitializerSymbol()) {
883
884 jitlink::Symbol *InitSym = nullptr;
885
886 for (auto &InitSection : G.sections()) {
887 // Skip non-init sections.
888 if (!isELFInitializerSection(InitSection.getName()) ||
889 InitSection.empty())
890 continue;
891
892 // Create the init symbol if it has not been created already and attach it
893 // to the first block.
894 if (!InitSym) {
895 auto &B = **InitSection.blocks().begin();
896 InitSym = &G.addDefinedSymbol(
897 B, 0, *InitSymName, B.getSize(), jitlink::Linkage::Strong,
899 }
900
901 // Add keep-alive edges to anonymous symbols in all other init blocks.
902 for (auto *B : InitSection.blocks()) {
903 if (B == &InitSym->getBlock())
904 continue;
905
906 auto &S = G.addAnonymousSymbol(*B, 0, B->getSize(), false, true);
907 InitSym->getBlock().addEdge(jitlink::Edge::KeepAlive, 0, S, 0);
908 }
909 }
910
911 // Also preserve fini sections (.fini_array, .fini, .dtors)
912 for (auto &FiniSection : G.sections()) {
913 // Skip non-fini sections.
914 if (!isELFFinalizerSection(FiniSection.getName()) || FiniSection.empty())
915 continue;
916
917 // Create the init symbol if it has not been created already and attach it
918 // to the first fini block.
919 if (!InitSym) {
920 auto &B = **FiniSection.blocks().begin();
921 InitSym = &G.addDefinedSymbol(
922 B, 0, *InitSymName, B.getSize(), jitlink::Linkage::Strong,
924 }
925
926 // Add keep-alive edges to anonymous symbols in all fini blocks.
927 for (auto *B : FiniSection.blocks()) {
928 if (B == &InitSym->getBlock())
929 continue;
930
931 auto &S = G.addAnonymousSymbol(*B, 0, B->getSize(), false, true);
932 InitSym->getBlock().addEdge(jitlink::Edge::KeepAlive, 0, S, 0);
933 }
934 }
935 }
936
937 return Error::success();
938}
939
940Error ELFNixPlatform::ELFNixPlatformPlugin::registerInitSections(
941 jitlink::LinkGraph &G, JITDylib &JD, bool IsBootstrapping) {
942 SmallVector<ExecutorAddrRange> ELFNixPlatformSecs;
943 LLVM_DEBUG(dbgs() << "ELFNixPlatform::registerInitSections\n");
944
945 SmallVector<jitlink::Section *> OrderedInitSections;
946 for (auto &Sec : G.sections())
947 if (isELFInitializerSection(Sec.getName()))
948 OrderedInitSections.push_back(&Sec);
949
950 // Helper to get section type and priority for sorting.
951 // Returns: {type_order, priority, has_priority}
952 // type_order: 0 = .init_array, 1 = .init, 2 = .ctors
953 auto getInitSectionInfo =
954 [](const jitlink::Section *Sec) -> std::tuple<int, uint64_t, bool> {
955 StringRef Name = Sec->getName();
956 if (Name.starts_with(".init_array")) {
957 StringRef PrioStr = Name;
958 uint64_t Prio = 0;
959 bool HasPrio = PrioStr.consume_front(".init_array.") &&
960 !PrioStr.getAsInteger(10, Prio);
961 return {0, Prio, HasPrio};
962 }
963 if (Name.starts_with(".init"))
964 return {1, 0, false};
965 if (Name.starts_with(".ctors")) {
966 StringRef PrioStr = Name;
967 uint64_t Prio = 0;
968 bool HasPrio =
969 PrioStr.consume_front(".ctors.") && !PrioStr.getAsInteger(10, Prio);
970 return {2, Prio, HasPrio};
971 }
972 return {3, 0, false};
973 };
974
975 // Sort init sections:
976 // 1. .init_array sections first (ascending priority - lower runs first)
977 // 2. .init sections next
978 // 3. .ctors sections last (descending priority - higher runs first, legacy
979 // behavior)
980 llvm::sort(OrderedInitSections,
981 [&](const jitlink::Section *LHS, const jitlink::Section *RHS) {
982 auto [LType, LPrio, LHasPrio] = getInitSectionInfo(LHS);
983 auto [RType, RPrio, RHasPrio] = getInitSectionInfo(RHS);
984
985 if (LType != RType)
986 return LType < RType;
987
988 // Same type - sort by priority
989 if (LType == 0) {
990 // .init_array: ascending priority (lower priority number runs
991 // first)
992 if (LHasPrio && RHasPrio)
993 return LPrio < RPrio;
994 if (LHasPrio)
995 return true;
996 if (RHasPrio)
997 return false;
998 } else if (LType == 2) {
999 // .ctors: descending priority (higher priority number runs
1000 // first)
1001 if (LHasPrio && RHasPrio)
1002 return LPrio > RPrio;
1003 if (LHasPrio)
1004 return true;
1005 if (RHasPrio)
1006 return false;
1007 }
1008 return LHS->getName() < RHS->getName();
1009 });
1010
1011 for (auto &Sec : OrderedInitSections)
1012 ELFNixPlatformSecs.push_back(jitlink::SectionRange(*Sec).getRange());
1013
1014 // Dump the scraped inits.
1015 LLVM_DEBUG({
1016 dbgs() << "ELFNixPlatform: Scraped " << G.getName() << " init sections:\n";
1017 for (auto &Sec : G.sections()) {
1018 jitlink::SectionRange R(Sec);
1019 dbgs() << " " << Sec.getName() << ": " << R.getRange() << "\n";
1020 }
1021 });
1022
1023 ExecutorAddr HeaderAddr;
1024 {
1025 std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
1026 auto I = MP.JITDylibToHandleAddr.find(&JD);
1027 assert(I != MP.JITDylibToHandleAddr.end() && "No header registered for JD");
1028 assert(I->second && "Null header registered for JD");
1029 HeaderAddr = I->second;
1030 }
1031
1032 using SPSRegisterInitSectionsArgs =
1033 SPSArgList<SPSExecutorAddr, SPSSequence<SPSExecutorAddrRange>>;
1034
1035 if (LLVM_UNLIKELY(IsBootstrapping)) {
1036 MP.Bootstrap.load()->addArgumentsToRTFnMap(
1037 &MP.RegisterInitSections, &MP.DeregisterInitSections,
1038 getArgDataBufferType<SPSRegisterInitSectionsArgs>(HeaderAddr,
1039 ELFNixPlatformSecs),
1040 getArgDataBufferType<SPSRegisterInitSectionsArgs>(HeaderAddr,
1041 ELFNixPlatformSecs));
1042 return Error::success();
1043 }
1044
1045 G.allocActions().push_back(
1047 MP.RegisterInitSections.Addr, HeaderAddr, ELFNixPlatformSecs)),
1049 MP.DeregisterInitSections.Addr, HeaderAddr, ELFNixPlatformSecs))});
1050
1051 return Error::success();
1052}
1053
1054Error ELFNixPlatform::ELFNixPlatformPlugin::registerFiniSections(
1055 jitlink::LinkGraph &G, JITDylib &JD, bool IsBootstrapping) {
1056 SmallVector<ExecutorAddrRange> ELFNixFiniSecs;
1057 LLVM_DEBUG(dbgs() << "ELFNixPlatform::registerFiniSections\n");
1058
1059 SmallVector<jitlink::Section *> OrderedFiniSections;
1060 for (auto &Sec : G.sections())
1061 if (isELFFinalizerSection(Sec.getName()))
1062 OrderedFiniSections.push_back(&Sec);
1063
1064 // Helper to get section type and priority for sorting.
1065 // Returns: {type_order, priority, has_priority}
1066 // type_order: 0 = .dtors, 1 = .fini, 2 = .fini_array
1067 auto getFiniSectionInfo =
1068 [](const jitlink::Section *Sec) -> std::tuple<int, uint64_t, bool> {
1069 StringRef Name = Sec->getName();
1070 if (Name.starts_with(".dtors")) {
1071 StringRef PrioStr = Name;
1072 uint64_t Prio = 0;
1073 bool HasPrio =
1074 PrioStr.consume_front(".dtors.") && !PrioStr.getAsInteger(10, Prio);
1075 return {0, Prio, HasPrio};
1076 }
1077 if (Name.starts_with(".fini"))
1078 if (!Name.starts_with(".fini_array"))
1079 return {1, 0, false};
1080 if (Name.starts_with(".fini_array")) {
1081 StringRef PrioStr = Name;
1082 uint64_t Prio = 0;
1083 bool HasPrio = PrioStr.consume_front(".fini_array.") &&
1084 !PrioStr.getAsInteger(10, Prio);
1085 return {2, Prio, HasPrio};
1086 }
1087 return {3, 0, false};
1088 };
1089
1090 // Sort fini sections:
1091 // 1. .dtors sections first (ascending priority, as they appear)
1092 // 2. .fini sections next
1093 // 3. .fini_array sections last (descending priority - higher runs first)
1094 llvm::sort(OrderedFiniSections,
1095 [&](const jitlink::Section *LHS, const jitlink::Section *RHS) {
1096 auto [LType, LPrio, LHasPrio] = getFiniSectionInfo(LHS);
1097 auto [RType, RPrio, RHasPrio] = getFiniSectionInfo(RHS);
1098
1099 if (LType != RType)
1100 return LType < RType;
1101
1102 // Same type - sort by priority
1103 if (LType == 0) {
1104 // .dtors: no-priority first, then ascending priority (lower
1105 // runs first)
1106 if (LHasPrio && RHasPrio)
1107 return LPrio < RPrio;
1108 if (LHasPrio)
1109 return false;
1110 if (RHasPrio)
1111 return true;
1112 } else if (LType == 2) {
1113 // .fini_array: no-priority first, then descending priority
1114 // (higher runs first)
1115 if (LHasPrio && RHasPrio)
1116 return LPrio > RPrio;
1117 if (LHasPrio)
1118 return false;
1119 if (RHasPrio)
1120 return true;
1121 }
1122 return LHS->getName() < RHS->getName();
1123 });
1124
1125 for (auto *Sec : OrderedFiniSections)
1126 ELFNixFiniSecs.push_back(jitlink::SectionRange(*Sec).getRange());
1127
1128 if (ELFNixFiniSecs.empty())
1129 return Error::success();
1130
1131 // Dump the scraped finis.
1132 LLVM_DEBUG({
1133 dbgs() << "ELFNixPlatform: Scraped " << G.getName() << " fini sections:\n";
1134 for (auto *Sec : OrderedFiniSections) {
1135 jitlink::SectionRange R(*Sec);
1136 dbgs() << " " << Sec->getName() << ": " << R.getRange() << "\n";
1137 }
1138 });
1139
1140 ExecutorAddr HeaderAddr;
1141 {
1142 std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
1143 auto I = MP.JITDylibToHandleAddr.find(&JD);
1144 assert(I != MP.JITDylibToHandleAddr.end() && "No header registered for JD");
1145 assert(I->second && "Null header registered for JD");
1146 HeaderAddr = I->second;
1147 }
1148
1149 using SPSRegisterFiniSectionsArgs =
1150 SPSArgList<SPSExecutorAddr, SPSSequence<SPSExecutorAddrRange>>;
1151
1152 if (LLVM_UNLIKELY(IsBootstrapping)) {
1153 MP.Bootstrap.load()->addArgumentsToRTFnMap(
1154 &MP.RegisterFiniSections, &MP.DeregisterFiniSections,
1155 getArgDataBufferType<SPSRegisterFiniSectionsArgs>(HeaderAddr,
1156 ELFNixFiniSecs),
1157 getArgDataBufferType<SPSRegisterFiniSectionsArgs>(HeaderAddr,
1158 ELFNixFiniSecs));
1159 return Error::success();
1160 }
1161
1162 G.allocActions().push_back(
1164 MP.RegisterFiniSections.Addr, HeaderAddr, ELFNixFiniSecs)),
1166 MP.DeregisterFiniSections.Addr, HeaderAddr, ELFNixFiniSecs))});
1167
1168 return Error::success();
1169}
1170
1171Error ELFNixPlatform::ELFNixPlatformPlugin::fixTLVSectionsAndEdges(
1172 jitlink::LinkGraph &G, JITDylib &JD) {
1173 auto TLSGetAddrSymbolName = G.intern("__tls_get_addr");
1174 auto TLSDescResolveSymbolName = G.intern("__tlsdesc_resolver");
1175 auto TLSGetOffsetSymbolName = G.intern("__tls_get_offset");
1176 for (auto *Sym : G.external_symbols()) {
1177 if (Sym->getName() == TLSGetAddrSymbolName) {
1178 auto TLSGetAddr =
1179 MP.getExecutionSession().intern("___orc_rt_elfnix_tls_get_addr");
1180 Sym->setName(std::move(TLSGetAddr));
1181 } else if (Sym->getName() == TLSDescResolveSymbolName) {
1182 auto TLSGetAddr =
1183 MP.getExecutionSession().intern("___orc_rt_elfnix_tlsdesc_resolver");
1184 Sym->setName(std::move(TLSGetAddr));
1185 } else if (Sym->getName() == TLSGetOffsetSymbolName) {
1186 auto TLSGetAddr =
1187 MP.getExecutionSession().intern("___orc_rt_elfnix_tls_get_offset");
1188 Sym->setName(std::move(TLSGetAddr));
1189 }
1190 }
1191
1192 auto *TLSInfoEntrySection = G.findSectionByName("$__TLSINFO");
1193
1194 if (TLSInfoEntrySection) {
1195 std::optional<uint64_t> Key;
1196 {
1197 std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
1198 auto I = MP.JITDylibToPThreadKey.find(&JD);
1199 if (I != MP.JITDylibToPThreadKey.end())
1200 Key = I->second;
1201 }
1202 if (!Key) {
1203 if (auto KeyOrErr = MP.createPThreadKey())
1204 Key = *KeyOrErr;
1205 else
1206 return KeyOrErr.takeError();
1207 }
1208
1209 uint64_t PlatformKeyBits =
1210 support::endian::byte_swap(*Key, G.getEndianness());
1211
1212 for (auto *B : TLSInfoEntrySection->blocks()) {
1213 // FIXME: The TLS descriptor byte length may different with different
1214 // ISA
1215 assert(B->getSize() == (G.getPointerSize() * 2) &&
1216 "TLS descriptor must be 2 words length");
1217 auto TLSInfoEntryContent = B->getMutableContent(G);
1218 memcpy(TLSInfoEntryContent.data(), &PlatformKeyBits, G.getPointerSize());
1219 }
1220 }
1221
1222 return Error::success();
1223}
1224
1225} // End namespace orc.
1226} // End namespace llvm.
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:344
static RegisterPass< DebugifyModulePass > DM("debugify", "Attach debug info to everything")
#define _
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
#define H(x, y, z)
Definition MD5.cpp:56
#define P(N)
static StringRef getName(Value *V)
#define LLVM_DEBUG(...)
Definition Debug.h:119
Value * RHS
Value * LHS
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:223
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:299
unsigned size() const
Definition DenseMap.h:172
bool empty() const
Definition DenseMap.h:171
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition DenseMap.h:219
iterator end()
Definition DenseMap.h:141
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:176
Helper for Errors used as out-parameters.
Definition Error.h:1160
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
void resize(size_type N)
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition StringRef.h:490
bool consume_front(char Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition StringRef.h:661
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
@ loongarch64
Definition Triple.h:66
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
Mediates between ELFNix initialization and ExecutionSession state.
ObjectLinkingLayer & getObjectLinkingLayer() const
static Expected< SymbolAliasMap > standardPlatformAliases(ExecutionSession &ES, JITDylib &PlatformJD)
Returns an AliasMap containing the default aliases for the ELFNixPlatform.
Error notifyAdding(ResourceTracker &RT, const MaterializationUnit &MU) override
This method will be called under the ExecutionSession lock each time a MaterializationUnit is added t...
Error notifyRemoving(ResourceTracker &RT) override
This method will be called under the ExecutionSession lock when a ResourceTracker is removed.
Error setupJITDylib(JITDylib &JD) override
This method will be called outside the session lock each time a JITDylib is created (unless it is cre...
ExecutionSession & getExecutionSession() const
static ArrayRef< std::pair< const char *, const char * > > standardRuntimeUtilityAliases()
Returns the array of standard runtime utility aliases for ELF.
static Expected< std::unique_ptr< ELFNixPlatform > > Create(ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD, std::unique_ptr< DefinitionGenerator > OrcRuntime, std::optional< SymbolAliasMap > RuntimeAliases=std::nullopt)
Try to create a ELFNixPlatform instance, adding the ORC runtime to the given JITDylib.
static ArrayRef< std::pair< const char *, const char * > > standardLazyCompilationAliases()
Returns a list of aliases required to enable lazy compilation via the ORC runtime.
static ArrayRef< std::pair< const char *, const char * > > requiredCXXAliases()
Returns the array of required CXX aliases.
Error teardownJITDylib(JITDylib &JD) override
This method will be called outside the session lock each time a JITDylib is removed to allow the Plat...
An ExecutionSession represents a running JIT program.
Definition Core.h:1111
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition Core.h:1165
DenseMap< SymbolStringPtr, JITDispatchHandlerFunction > JITDispatchHandlerAssociationMap
A map associating tag names with asynchronous wrapper function implementations in the JIT.
Definition Core.h:1134
Represents an address in the executor process.
Represents a JIT'd dynamic library.
Definition Core.h:675
Error define(std::unique_ptr< MaterializationUnitType > &&MU, ResourceTrackerSP RT=nullptr)
Define all symbols provided by the materialization unit to be part of this JITDylib.
Definition Core.h:1649
GeneratorT & addGenerator(std::unique_ptr< GeneratorT > DefGenerator)
Adds a definition generator to this JITDylib and returns a referenece to it.
Definition Core.h:1632
LinkGraphLinkingLayer & addPlugin(std::shared_ptr< Plugin > P)
Add a plugin.
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition Core.h:349
const SymbolStringPtr & getInitializerSymbol() const
Returns the initialization pseudo-symbol, if any.
Definition Core.h:388
JITDylib & getTargetJITDylib() const
Returns the target JITDylib that these symbols are being materialized into.
Definition Core.h:374
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
virtual StringRef getName() const =0
Return the name of this materialization unit.
const SymbolStringPtr & getInitializerSymbol() const
Returns the initialization symbol for this MaterializationUnit (if any).
An ObjectLayer implementation built on JITLink.
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O) override
Emit an object file.
static void lookupInitSymbolsAsync(unique_function< void(Error)> OnComplete, ExecutionSession &ES, const DenseMap< JITDylib *, SymbolLookupSet > &InitSyms)
Performs an async lookup for the given symbols in each of the given JITDylibs, calling the given hand...
Definition Core.cpp:1489
API to remove / transfer ownership of JIT resources.
Definition Core.h:63
JITDylib & getJITDylib() const
Return the JITDylib targeted by this tracker.
Definition Core.h:78
static Expected< std::unique_ptr< StaticLibraryDefinitionGenerator > > Load(ObjectLayer &L, const char *FileName, VisitMembersFunction VisitMembers=VisitMembersFunction(), GetObjectFileInterface GetObjFileInterface=GetObjectFileInterface())
Try to create a StaticLibraryDefinitionGenerator from the given path.
Pointer to a pooled string representing a symbol name.
A utility class for serializing to a blob from a variadic list.
Output char buffer with overflow check.
Represents a serialized wrapper function call.
static Expected< WrapperFunctionCall > Create(ExecutorAddr FnAddr, const ArgTs &...Args)
Create a WrapperFunctionCall using the given SPS serializer to serialize the arguments.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
SPSSequence< char > SPSString
SPS tag type for strings, which are equivalent to sequences of chars.
LLVM_ABI StringRef ELFThreadBSSSectionName
JITDylibSearchOrder makeJITDylibSearchOrder(ArrayRef< JITDylib * > JDs, JITDylibLookupFlags Flags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Convenience function for creating a search order from an ArrayRef of JITDylib*, all with the same fla...
Definition Core.h:153
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup.
Definition Core.h:148
IntrusiveRefCntPtr< JITDylib > JITDylibSP
Definition Core.h:58
std::vector< std::pair< ExecutorAddr, ELFNixJITDylibDepInfo > > ELFNixJITDylibDepInfoMap
std::unique_ptr< ReExportsMaterializationUnit > symbolAliases(SymbolAliasMap Aliases)
Create a ReExportsMaterializationUnit with the given aliases.
Definition Core.h:523
std::vector< ExecutorAddr > ELFNixJITDylibDepInfo
LLVM_ABI StringRef ELFEHFrameSectionName
std::unique_ptr< ReExportsMaterializationUnit > reexports(JITDylib &SourceJD, SymbolAliasMap Aliases, JITDylibLookupFlags SourceJDLookupFlags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Create a materialization unit for re-exporting symbols from another JITDylib with alternative names/f...
Definition Core.h:532
static void addAliases(ExecutionSession &ES, SymbolAliasMap &Aliases, ArrayRef< std::pair< const char *, const char * > > AL)
DenseMap< std::pair< RuntimeFunction *, RuntimeFunction * >, SmallVector< std::pair< shared::WrapperFunctionCall::ArgDataBufferType, shared::WrapperFunctionCall::ArgDataBufferType > > > DeferredRuntimeFnMap
LLVM_ABI bool isELFFinalizerSection(StringRef SecName)
LLVM_ABI StringRef ELFThreadDataSectionName
LLVM_ABI RegisterDependenciesFunction NoDependenciesToRegister
This can be used as the value for a RegisterDependenciesFunction if there are no dependants to regist...
Definition Core.cpp:40
@ Ready
Emitted to memory, but waiting on transitive dependencies.
Definition Core.h:551
DenseMap< SymbolStringPtr, SymbolAliasMapEntry > SymbolAliasMap
A map of Symbols to (Symbol, Flags) pairs.
Definition Core.h:173
LLVM_ABI Expected< SymbolAliasMap > buildSimpleReexportsAliasMap(JITDylib &SourceJD, const SymbolNameSet &Symbols)
Build a SymbolAliasMap for the common case where you want to re-export symbols from another JITDylib ...
Definition Core.cpp:482
LLVM_ABI bool isELFInitializerSection(StringRef SecName)
DenseMap< SymbolStringPtr, JITSymbolFlags > SymbolFlagsMap
A map from symbol names (as SymbolStringPtrs) to JITSymbolFlags.
value_type byte_swap(value_type value, endianness endian)
Definition Endian.h:44
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:94
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917