LLVM 17.0.0git
MachOPlatform.cpp
Go to the documentation of this file.
1//===------ MachOPlatform.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
10
18#include "llvm/Support/Debug.h"
19#include <optional>
20
21#define DEBUG_TYPE "orc"
22
23using namespace llvm;
24using namespace llvm::orc;
25using namespace llvm::orc::shared;
26
27namespace llvm {
28namespace orc {
29namespace shared {
30
34
35template <>
37 MachOPlatform::MachOJITDylibDepInfo> {
38public:
39 static size_t size(const MachOPlatform::MachOJITDylibDepInfo &DDI) {
40 return SPSMachOJITDylibDepInfo::AsArgList::size(DDI.Sealed, DDI.DepHeaders);
41 }
42
43 static bool serialize(SPSOutputBuffer &OB,
45 return SPSMachOJITDylibDepInfo::AsArgList::serialize(OB, DDI.Sealed,
46 DDI.DepHeaders);
47 }
48
49 static bool deserialize(SPSInputBuffer &IB,
51 return SPSMachOJITDylibDepInfo::AsArgList::deserialize(IB, DDI.Sealed,
52 DDI.DepHeaders);
53 }
54};
55
56} // namespace shared
57} // namespace orc
58} // namespace llvm
59
60namespace {
61
62std::unique_ptr<jitlink::LinkGraph> createPlatformGraph(MachOPlatform &MOP,
63 std::string Name) {
64 unsigned PointerSize;
66 const auto &TT = MOP.getExecutionSession().getTargetTriple();
67
68 switch (TT.getArch()) {
69 case Triple::aarch64:
70 case Triple::x86_64:
71 PointerSize = 8;
72 Endianness = support::endianness::little;
73 break;
74 default:
75 llvm_unreachable("Unrecognized architecture");
76 }
77
78 return std::make_unique<jitlink::LinkGraph>(std::move(Name), TT, PointerSize,
79 Endianness,
81}
82
83// Generates a MachO header.
84class MachOHeaderMaterializationUnit : public MaterializationUnit {
85public:
86 MachOHeaderMaterializationUnit(MachOPlatform &MOP,
87 const SymbolStringPtr &HeaderStartSymbol)
88 : MaterializationUnit(createHeaderInterface(MOP, HeaderStartSymbol)),
89 MOP(MOP) {}
90
91 StringRef getName() const override { return "MachOHeaderMU"; }
92
93 void materialize(std::unique_ptr<MaterializationResponsibility> R) override {
94 auto G = createPlatformGraph(MOP, "<MachOHeaderMU>");
95 addMachOHeader(*G, MOP, R->getInitializerSymbol());
96 MOP.getObjectLinkingLayer().emit(std::move(R), std::move(G));
97 }
98
99 void discard(const JITDylib &JD, const SymbolStringPtr &Sym) override {}
100
101 static void addMachOHeader(jitlink::LinkGraph &G, MachOPlatform &MOP,
102 const SymbolStringPtr &InitializerSymbol) {
103 auto &HeaderSection = G.createSection("__header", MemProt::Read);
104 auto &HeaderBlock = createHeaderBlock(G, HeaderSection);
105
106 // Init symbol is header-start symbol.
107 G.addDefinedSymbol(HeaderBlock, 0, *InitializerSymbol,
108 HeaderBlock.getSize(), jitlink::Linkage::Strong,
109 jitlink::Scope::Default, false, true);
110 for (auto &HS : AdditionalHeaderSymbols)
111 G.addDefinedSymbol(HeaderBlock, HS.Offset, HS.Name, HeaderBlock.getSize(),
112 jitlink::Linkage::Strong, jitlink::Scope::Default,
113 false, true);
114 }
115
116private:
117 struct HeaderSymbol {
118 const char *Name;
120 };
121
122 static constexpr HeaderSymbol AdditionalHeaderSymbols[] = {
123 {"___mh_executable_header", 0}};
124
125 static jitlink::Block &createHeaderBlock(jitlink::LinkGraph &G,
126 jitlink::Section &HeaderSection) {
129 switch (G.getTargetTriple().getArch()) {
130 case Triple::aarch64:
133 break;
134 case Triple::x86_64:
137 break;
138 default:
139 llvm_unreachable("Unrecognized architecture");
140 }
141 Hdr.filetype = MachO::MH_DYLIB; // Custom file type?
142 Hdr.ncmds = 0;
143 Hdr.sizeofcmds = 0;
144 Hdr.flags = 0;
145 Hdr.reserved = 0;
146
147 if (G.getEndianness() != support::endian::system_endianness())
149
150 auto HeaderContent = G.allocateContent(
151 ArrayRef<char>(reinterpret_cast<const char *>(&Hdr), sizeof(Hdr)));
152
153 return G.createContentBlock(HeaderSection, HeaderContent, ExecutorAddr(), 8,
154 0);
155 }
156
158 createHeaderInterface(MachOPlatform &MOP,
159 const SymbolStringPtr &HeaderStartSymbol) {
160 SymbolFlagsMap HeaderSymbolFlags;
161
162 HeaderSymbolFlags[HeaderStartSymbol] = JITSymbolFlags::Exported;
163 for (auto &HS : AdditionalHeaderSymbols)
164 HeaderSymbolFlags[MOP.getExecutionSession().intern(HS.Name)] =
166
167 return MaterializationUnit::Interface(std::move(HeaderSymbolFlags),
168 HeaderStartSymbol);
169 }
170
171 MachOPlatform &MOP;
172};
173
174constexpr MachOHeaderMaterializationUnit::HeaderSymbol
175 MachOHeaderMaterializationUnit::AdditionalHeaderSymbols[];
176
177// Creates a Bootstrap-Complete LinkGraph to run deferred actions.
178class MachOPlatformCompleteBootstrapMaterializationUnit
179 : public MaterializationUnit {
180public:
181 MachOPlatformCompleteBootstrapMaterializationUnit(
182 MachOPlatform &MOP, StringRef PlatformJDName,
183 SymbolStringPtr CompleteBootstrapSymbol, shared::AllocActions DeferredAAs,
184 ExecutorAddr PlatformBootstrap, ExecutorAddr PlatformShutdown,
185 ExecutorAddr RegisterJITDylib, ExecutorAddr DeregisterJITDylib,
186 ExecutorAddr MachOHeaderAddr)
188 {{{CompleteBootstrapSymbol, JITSymbolFlags::None}}, nullptr}),
189 MOP(MOP), PlatformJDName(PlatformJDName),
190 CompleteBootstrapSymbol(std::move(CompleteBootstrapSymbol)),
191 DeferredAAs(std::move(DeferredAAs)),
192 PlatformBootstrap(PlatformBootstrap),
193 PlatformShutdown(PlatformShutdown), RegisterJITDylib(RegisterJITDylib),
194 DeregisterJITDylib(DeregisterJITDylib),
195 MachOHeaderAddr(MachOHeaderAddr) {}
196
197 StringRef getName() const override {
198 return "MachOPlatformCompleteBootstrap";
199 }
200
201 void materialize(std::unique_ptr<MaterializationResponsibility> R) override {
202 using namespace jitlink;
203 auto G = createPlatformGraph(MOP, "<OrcRTCompleteBootstrap>");
204 auto &PlaceholderSection =
205 G->createSection("__orc_rt_cplt_bs", MemProt::Read);
206 auto &PlaceholderBlock =
207 G->createZeroFillBlock(PlaceholderSection, 1, ExecutorAddr(), 1, 0);
208 G->addDefinedSymbol(PlaceholderBlock, 0, *CompleteBootstrapSymbol, 1,
209 Linkage::Strong, Scope::Hidden, false, true);
210
211 // Reserve space for the stolen actions, plus two extras.
212 G->allocActions().reserve(DeferredAAs.size() + 2);
213
214 // 1. Bootstrap the platform support code.
215 G->allocActions().push_back(
217 cantFail(
218 WrapperFunctionCall::Create<SPSArgList<>>(PlatformShutdown))});
219
220 // 2. Register the platform JITDylib.
221 G->allocActions().push_back(
224 RegisterJITDylib, PlatformJDName, MachOHeaderAddr)),
226 DeregisterJITDylib, MachOHeaderAddr))});
227
228 // 3. Add the deferred actions to the graph.
229 std::move(DeferredAAs.begin(), DeferredAAs.end(),
230 std::back_inserter(G->allocActions()));
231
232 MOP.getObjectLinkingLayer().emit(std::move(R), std::move(G));
233 }
234
235 void discard(const JITDylib &JD, const SymbolStringPtr &Sym) override {}
236
237private:
238 MachOPlatform &MOP;
239 StringRef PlatformJDName;
240 SymbolStringPtr CompleteBootstrapSymbol;
241 shared::AllocActions DeferredAAs;
242 ExecutorAddr PlatformBootstrap;
243 ExecutorAddr PlatformShutdown;
244 ExecutorAddr RegisterJITDylib;
245 ExecutorAddr DeregisterJITDylib;
246 ExecutorAddr MachOHeaderAddr;
247};
248
249} // end anonymous namespace
250
251namespace llvm {
252namespace orc {
253
256 JITDylib &PlatformJD,
257 std::unique_ptr<DefinitionGenerator> OrcRuntime,
258 std::optional<SymbolAliasMap> RuntimeAliases) {
259
260 // If the target is not supported then bail out immediately.
261 if (!supportedTarget(ES.getTargetTriple()))
262 return make_error<StringError>("Unsupported MachOPlatform triple: " +
263 ES.getTargetTriple().str(),
265
266 auto &EPC = ES.getExecutorProcessControl();
267
268 // Create default aliases if the caller didn't supply any.
269 if (!RuntimeAliases)
270 RuntimeAliases = standardPlatformAliases(ES);
271
272 // Define the aliases.
273 if (auto Err = PlatformJD.define(symbolAliases(std::move(*RuntimeAliases))))
274 return std::move(Err);
275
276 // Add JIT-dispatch function support symbols.
277 if (auto Err = PlatformJD.define(
278 absoluteSymbols({{ES.intern("___orc_rt_jit_dispatch"),
279 {EPC.getJITDispatchInfo().JITDispatchFunction,
281 {ES.intern("___orc_rt_jit_dispatch_ctx"),
282 {EPC.getJITDispatchInfo().JITDispatchContext,
284 return std::move(Err);
285
286 // Create the instance.
287 Error Err = Error::success();
288 auto P = std::unique_ptr<MachOPlatform>(new MachOPlatform(
289 ES, ObjLinkingLayer, PlatformJD, std::move(OrcRuntime), Err));
290 if (Err)
291 return std::move(Err);
292 return std::move(P);
293}
294
297 JITDylib &PlatformJD, const char *OrcRuntimePath,
298 std::optional<SymbolAliasMap> RuntimeAliases) {
299
300 // Create a generator for the ORC runtime archive.
301 auto OrcRuntimeArchiveGenerator =
302 StaticLibraryDefinitionGenerator::Load(ObjLinkingLayer, OrcRuntimePath);
303 if (!OrcRuntimeArchiveGenerator)
304 return OrcRuntimeArchiveGenerator.takeError();
305
306 return Create(ES, ObjLinkingLayer, PlatformJD,
307 std::move(*OrcRuntimeArchiveGenerator),
308 std::move(RuntimeAliases));
309}
310
312 if (auto Err = JD.define(std::make_unique<MachOHeaderMaterializationUnit>(
313 *this, MachOHeaderStartSymbol)))
314 return Err;
315
316 return ES.lookup({&JD}, MachOHeaderStartSymbol).takeError();
317}
318
320 std::lock_guard<std::mutex> Lock(PlatformMutex);
321 auto I = JITDylibToHeaderAddr.find(&JD);
322 if (I != JITDylibToHeaderAddr.end()) {
323 assert(HeaderAddrToJITDylib.count(I->second) &&
324 "HeaderAddrToJITDylib missing entry");
325 HeaderAddrToJITDylib.erase(I->second);
326 JITDylibToHeaderAddr.erase(I);
327 }
328 JITDylibToPThreadKey.erase(&JD);
329 return Error::success();
330}
331
333 const MaterializationUnit &MU) {
334 auto &JD = RT.getJITDylib();
335 const auto &InitSym = MU.getInitializerSymbol();
336 if (!InitSym)
337 return Error::success();
338
339 RegisteredInitSymbols[&JD].add(InitSym,
341 LLVM_DEBUG({
342 dbgs() << "MachOPlatform: Registered init symbol " << *InitSym << " for MU "
343 << MU.getName() << "\n";
344 });
345 return Error::success();
346}
347
349 llvm_unreachable("Not supported yet");
350}
351
353 ArrayRef<std::pair<const char *, const char *>> AL) {
354 for (auto &KV : AL) {
355 auto AliasName = ES.intern(KV.first);
356 assert(!Aliases.count(AliasName) && "Duplicate symbol name in alias map");
357 Aliases[std::move(AliasName)] = {ES.intern(KV.second),
359 }
360}
361
363 SymbolAliasMap Aliases;
364 addAliases(ES, Aliases, requiredCXXAliases());
366 return Aliases;
367}
368
371 static const std::pair<const char *, const char *> RequiredCXXAliases[] = {
372 {"___cxa_atexit", "___orc_rt_macho_cxa_atexit"}};
373
374 return ArrayRef<std::pair<const char *, const char *>>(RequiredCXXAliases);
375}
376
379 static const std::pair<const char *, const char *>
380 StandardRuntimeUtilityAliases[] = {
381 {"___orc_rt_run_program", "___orc_rt_macho_run_program"},
382 {"___orc_rt_jit_dlerror", "___orc_rt_macho_jit_dlerror"},
383 {"___orc_rt_jit_dlopen", "___orc_rt_macho_jit_dlopen"},
384 {"___orc_rt_jit_dlclose", "___orc_rt_macho_jit_dlclose"},
385 {"___orc_rt_jit_dlsym", "___orc_rt_macho_jit_dlsym"},
386 {"___orc_rt_log_error", "___orc_rt_log_error_to_stderr"}};
387
389 StandardRuntimeUtilityAliases);
390}
391
392bool MachOPlatform::supportedTarget(const Triple &TT) {
393 switch (TT.getArch()) {
394 case Triple::aarch64:
395 case Triple::x86_64:
396 return true;
397 default:
398 return false;
399 }
400}
401
402MachOPlatform::MachOPlatform(
403 ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
404 JITDylib &PlatformJD,
405 std::unique_ptr<DefinitionGenerator> OrcRuntimeGenerator, Error &Err)
406 : ES(ES), PlatformJD(PlatformJD), ObjLinkingLayer(ObjLinkingLayer) {
408 ObjLinkingLayer.addPlugin(std::make_unique<MachOPlatformPlugin>(*this));
409 PlatformJD.addGenerator(std::move(OrcRuntimeGenerator));
410
411 BootstrapInfo BI;
412 Bootstrap = &BI;
413
414 // Bootstrap process -- here be phase-ordering dragons.
415 //
416 // The MachOPlatform class uses allocation actions to register metadata
417 // sections with the ORC runtime, however the runtime contains metadata
418 // registration functions that have their own metadata that they need to
419 // register (e.g. the frame-info registration functions have frame-info).
420 // We can't use an ordinary lookup to find these registration functions
421 // because their address is needed during the link of the containing graph
422 // itself (to build the allocation actions that will call the registration
423 // functions). Further complicating the situation (a) the graph containing
424 // the registration functions is allowed to depend on other graphs (e.g. the
425 // graph containing the ORC runtime RTTI support) so we need to handle with
426 // an unknown set of dependencies during bootstrap, and (b) these graphs may
427 // be linked concurrently if the user has installed a concurrent dispatcher.
428 //
429 // We satisfy these constraint by implementing a bootstrap phase during which
430 // allocation actions generated by MachOPlatform are appended to a list of
431 // deferred allocation actions, rather than to the graphs themselves. At the
432 // end of the bootstrap process the deferred actions are attached to a final
433 // "complete-bootstrap" graph that causes them to be run.
434 //
435 // The bootstrap steps are as follows:
436 //
437 // 1. Request the graph containing the mach header. This graph is guaranteed
438 // not to have any metadata so the fact that the registration functions
439 // are not available yet is not a problem.
440 //
441 // 2. Look up the registration functions and discard the results. This will
442 // trigger linking of the graph containing these functions, and
443 // consequently any graphs that it depends on. We do not use the lookup
444 // result to find the addresses of the functions requested (as described
445 // above the lookup will return too late for that), instead we capture the
446 // addresses in a post-allocation pass injected by the platform runtime
447 // during bootstrap only.
448 //
449 // 3. During bootstrap the MachOPlatformPlugin keeps a count of the number of
450 // graphs being linked (potentially concurrently), and we block until all
451 // of these graphs have completed linking. This is to avoid a race on the
452 // deferred-actions vector: the lookup for the runtime registration
453 // functions may return while some functions (those that are being
454 // incidentally linked in, but aren't reachable via the runtime functions)
455 // are still being linked, and we need to capture any allocation actions
456 // for this incidental code before we proceed.
457 //
458 // 4. Once all active links are complete we transfer the deferred actions to
459 // a newly added CompleteBootstrap graph and then request a symbol from
460 // the CompleteBootstrap graph to trigger materialization. This will cause
461 // all deferred actions to be run, and once this lookup returns we can
462 // proceed.
463 //
464 // 5. Finally, we associate runtime support methods in MachOPlatform with
465 // the corresponding jit-dispatch tag variables in the ORC runtime to make
466 // the support methods callable. The bootstrap is now complete.
467
468 // Step (1) Add header materialization unit and request.
469 if ((Err = PlatformJD.define(std::make_unique<MachOHeaderMaterializationUnit>(
470 *this, MachOHeaderStartSymbol))))
471 return;
472 if ((Err = ES.lookup(&PlatformJD, MachOHeaderStartSymbol).takeError()))
473 return;
474
475 // Step (2) Request runtime registration functions to trigger
476 // materialization..
477 if ((Err = ES.lookup(makeJITDylibSearchOrder(&PlatformJD),
479 {PlatformBootstrap.Name, PlatformShutdown.Name,
480 RegisterJITDylib.Name, DeregisterJITDylib.Name,
481 RegisterObjectPlatformSections.Name,
482 DeregisterObjectPlatformSections.Name,
483 CreatePThreadKey.Name}))
484 .takeError()))
485 return;
486
487 // Step (3) Wait for any incidental linker work to complete.
488 {
489 std::unique_lock<std::mutex> Lock(BI.Mutex);
490 BI.CV.wait(Lock, [&]() { return BI.ActiveGraphs == 0; });
491 Bootstrap = nullptr;
492 }
493
494 // Step (4) Add complete-bootstrap materialization unit and request.
495 auto BootstrapCompleteSymbol = ES.intern("__orc_rt_macho_complete_bootstrap");
496 if ((Err = PlatformJD.define(
497 std::make_unique<MachOPlatformCompleteBootstrapMaterializationUnit>(
498 *this, PlatformJD.getName(), BootstrapCompleteSymbol,
499 std::move(BI.DeferredAAs), PlatformBootstrap.Addr,
500 PlatformShutdown.Addr, RegisterJITDylib.Addr,
501 DeregisterJITDylib.Addr, BI.MachOHeaderAddr))))
502 return;
503 if ((Err = ES.lookup(makeJITDylibSearchOrder(
505 std::move(BootstrapCompleteSymbol))
506 .takeError()))
507 return;
508
509 // (5) Associate runtime support functions.
510 if ((Err = associateRuntimeSupportFunctions()))
511 return;
512}
513
514Error MachOPlatform::associateRuntimeSupportFunctions() {
516
517 using PushInitializersSPSSig =
519 WFs[ES.intern("___orc_rt_macho_push_initializers_tag")] =
520 ES.wrapAsyncWithSPS<PushInitializersSPSSig>(
521 this, &MachOPlatform::rt_pushInitializers);
522
523 using LookupSymbolSPSSig =
525 WFs[ES.intern("___orc_rt_macho_symbol_lookup_tag")] =
526 ES.wrapAsyncWithSPS<LookupSymbolSPSSig>(this,
527 &MachOPlatform::rt_lookupSymbol);
528
529 return ES.registerJITDispatchHandlers(PlatformJD, std::move(WFs));
530}
531
532void MachOPlatform::pushInitializersLoop(
533 PushInitializersSendResultFn SendResult, JITDylibSP JD) {
536 SmallVector<JITDylib *, 16> Worklist({JD.get()});
537
538 ES.runSessionLocked([&]() {
539 while (!Worklist.empty()) {
540 // FIXME: Check for defunct dylibs.
541
542 auto DepJD = Worklist.back();
543 Worklist.pop_back();
544
545 // If we've already visited this JITDylib on this iteration then continue.
546 if (JDDepMap.count(DepJD))
547 continue;
548
549 // Add dep info.
550 auto &DM = JDDepMap[DepJD];
551 DepJD->withLinkOrderDo([&](const JITDylibSearchOrder &O) {
552 for (auto &KV : O) {
553 if (KV.first == DepJD)
554 continue;
555 DM.push_back(KV.first);
556 Worklist.push_back(KV.first);
557 }
558 });
559
560 // Add any registered init symbols.
561 auto RISItr = RegisteredInitSymbols.find(DepJD);
562 if (RISItr != RegisteredInitSymbols.end()) {
563 NewInitSymbols[DepJD] = std::move(RISItr->second);
564 RegisteredInitSymbols.erase(RISItr);
565 }
566 }
567 });
568
569 // If there are no further init symbols to look up then send the link order
570 // (as a list of header addresses) to the caller.
571 if (NewInitSymbols.empty()) {
572
573 // To make the list intelligible to the runtime we need to convert all
574 // JITDylib pointers to their header addresses. Only include JITDylibs
575 // that appear in the JITDylibToHeaderAddr map (i.e. those that have been
576 // through setupJITDylib) -- bare JITDylibs aren't managed by the platform.
578 HeaderAddrs.reserve(JDDepMap.size());
579 {
580 std::lock_guard<std::mutex> Lock(PlatformMutex);
581 for (auto &KV : JDDepMap) {
582 auto I = JITDylibToHeaderAddr.find(KV.first);
583 if (I != JITDylibToHeaderAddr.end())
584 HeaderAddrs[KV.first] = I->second;
585 }
586 }
587
588 // Build the dep info map to return.
589 MachOJITDylibDepInfoMap DIM;
590 DIM.reserve(JDDepMap.size());
591 for (auto &KV : JDDepMap) {
592 auto HI = HeaderAddrs.find(KV.first);
593 // Skip unmanaged JITDylibs.
594 if (HI == HeaderAddrs.end())
595 continue;
596 auto H = HI->second;
597 MachOJITDylibDepInfo DepInfo;
598 for (auto &Dep : KV.second) {
599 auto HJ = HeaderAddrs.find(Dep);
600 if (HJ != HeaderAddrs.end())
601 DepInfo.DepHeaders.push_back(HJ->second);
602 }
603 DIM.push_back(std::make_pair(H, std::move(DepInfo)));
604 }
605 SendResult(DIM);
606 return;
607 }
608
609 // Otherwise issue a lookup and re-run this phase when it completes.
610 lookupInitSymbolsAsync(
611 [this, SendResult = std::move(SendResult), JD](Error Err) mutable {
612 if (Err)
613 SendResult(std::move(Err));
614 else
615 pushInitializersLoop(std::move(SendResult), JD);
616 },
617 ES, std::move(NewInitSymbols));
618}
619
620void MachOPlatform::rt_pushInitializers(PushInitializersSendResultFn SendResult,
621 ExecutorAddr JDHeaderAddr) {
622 JITDylibSP JD;
623 {
624 std::lock_guard<std::mutex> Lock(PlatformMutex);
625 auto I = HeaderAddrToJITDylib.find(JDHeaderAddr);
626 if (I != HeaderAddrToJITDylib.end())
627 JD = I->second;
628 }
629
630 LLVM_DEBUG({
631 dbgs() << "MachOPlatform::rt_pushInitializers(" << JDHeaderAddr << ") ";
632 if (JD)
633 dbgs() << "pushing initializers for " << JD->getName() << "\n";
634 else
635 dbgs() << "No JITDylib for header address.\n";
636 });
637
638 if (!JD) {
639 SendResult(
640 make_error<StringError>("No JITDylib with header addr " +
641 formatv("{0:x}", JDHeaderAddr.getValue()),
643 return;
644 }
645
646 pushInitializersLoop(std::move(SendResult), JD);
647}
648
649void MachOPlatform::rt_lookupSymbol(SendSymbolAddressFn SendResult,
650 ExecutorAddr Handle, StringRef SymbolName) {
651 LLVM_DEBUG({
652 dbgs() << "MachOPlatform::rt_lookupSymbol(\""
653 << formatv("{0:x}", Handle.getValue()) << "\")\n";
654 });
655
656 JITDylib *JD = nullptr;
657
658 {
659 std::lock_guard<std::mutex> Lock(PlatformMutex);
660 auto I = HeaderAddrToJITDylib.find(Handle);
661 if (I != HeaderAddrToJITDylib.end())
662 JD = I->second;
663 }
664
665 if (!JD) {
666 LLVM_DEBUG({
667 dbgs() << " No JITDylib for handle "
668 << formatv("{0:x}", Handle.getValue()) << "\n";
669 });
670 SendResult(make_error<StringError>("No JITDylib associated with handle " +
671 formatv("{0:x}", Handle.getValue()),
673 return;
674 }
675
676 // Use functor class to work around XL build compiler issue on AIX.
677 class RtLookupNotifyComplete {
678 public:
679 RtLookupNotifyComplete(SendSymbolAddressFn &&SendResult)
680 : SendResult(std::move(SendResult)) {}
681 void operator()(Expected<SymbolMap> Result) {
682 if (Result) {
683 assert(Result->size() == 1 && "Unexpected result map count");
684 SendResult(Result->begin()->second.getAddress());
685 } else {
686 SendResult(Result.takeError());
687 }
688 }
689
690 private:
691 SendSymbolAddressFn SendResult;
692 };
693
694 // FIXME: Proper mangling.
695 auto MangledName = ("_" + SymbolName).str();
696 ES.lookup(
697 LookupKind::DLSym, {{JD, JITDylibLookupFlags::MatchExportedSymbolsOnly}},
698 SymbolLookupSet(ES.intern(MangledName)), SymbolState::Ready,
699 RtLookupNotifyComplete(std::move(SendResult)), NoDependenciesToRegister);
700}
701
702Expected<uint64_t> MachOPlatform::createPThreadKey() {
703 if (!CreatePThreadKey.Addr)
704 return make_error<StringError>(
705 "Attempting to create pthread key in target, but runtime support has "
706 "not been loaded yet",
708
710 if (auto Err = ES.callSPSWrapper<SPSExpected<uint64_t>(void)>(
711 CreatePThreadKey.Addr, Result))
712 return std::move(Err);
713 return Result;
714}
715
716void MachOPlatform::MachOPlatformPlugin::modifyPassConfig(
719
720 using namespace jitlink;
721
722 bool InBootstrapPhase =
723 &MR.getTargetJITDylib() == &MP.PlatformJD && MP.Bootstrap;
724
725 // If we're in the bootstrap phase then increment the active graphs.
726 if (InBootstrapPhase) {
727 Config.PrePrunePasses.push_back(
728 [this](LinkGraph &G) { return bootstrapPipelineStart(G); });
729 Config.PostAllocationPasses.push_back([this](LinkGraph &G) {
730 return bootstrapPipelineRecordRuntimeFunctions(G);
731 });
732 }
733
734 // --- Handle Initializers ---
735 if (auto InitSymbol = MR.getInitializerSymbol()) {
736
737 // If the initializer symbol is the MachOHeader start symbol then just
738 // register it and then bail out -- the header materialization unit
739 // definitely doesn't need any other passes.
740 if (InitSymbol == MP.MachOHeaderStartSymbol && !InBootstrapPhase) {
741 Config.PostAllocationPasses.push_back([this, &MR](LinkGraph &G) {
742 return associateJITDylibHeaderSymbol(G, MR);
743 });
744 return;
745 }
746
747 // If the object contains an init symbol other than the header start symbol
748 // then add passes to preserve, process and register the init
749 // sections/symbols.
750 Config.PrePrunePasses.push_back([this, &MR](LinkGraph &G) {
751 if (auto Err = preserveInitSections(G, MR))
752 return Err;
753 return processObjCImageInfo(G, MR);
754 });
755 }
756
757 // Insert TLV lowering at the start of the PostPrunePasses, since we want
758 // it to run before GOT/PLT lowering.
759 Config.PostPrunePasses.insert(
760 Config.PostPrunePasses.begin(),
761 [this, &JD = MR.getTargetJITDylib()](LinkGraph &G) {
762 return fixTLVSectionsAndEdges(G, JD);
763 });
764
765 // Add a pass to register the final addresses of any special sections in the
766 // object with the runtime.
767 Config.PostAllocationPasses.push_back(
768 [this, &JD = MR.getTargetJITDylib(), InBootstrapPhase](LinkGraph &G) {
769 return registerObjectPlatformSections(G, JD, InBootstrapPhase);
770 });
771
772 // If we're in the bootstrap phase then steal allocation actions and then
773 // decrement the active graphs.
774 if (InBootstrapPhase)
775 Config.PostFixupPasses.push_back(
776 [this](LinkGraph &G) { return bootstrapPipelineEnd(G); });
777}
778
780MachOPlatform::MachOPlatformPlugin::getSyntheticSymbolDependencies(
782 std::lock_guard<std::mutex> Lock(PluginMutex);
783 auto I = InitSymbolDeps.find(&MR);
784 if (I != InitSymbolDeps.end()) {
785 SyntheticSymbolDependenciesMap Result;
786 Result[MR.getInitializerSymbol()] = std::move(I->second);
787 InitSymbolDeps.erase(&MR);
788 return Result;
789 }
790 return SyntheticSymbolDependenciesMap();
791}
792
793Error MachOPlatform::MachOPlatformPlugin::bootstrapPipelineStart(
795 // Increment the active graphs count in BootstrapInfo.
796 std::lock_guard<std::mutex> Lock(MP.Bootstrap.load()->Mutex);
797 ++MP.Bootstrap.load()->ActiveGraphs;
798 return Error::success();
799}
800
801Error MachOPlatform::MachOPlatformPlugin::
802 bootstrapPipelineRecordRuntimeFunctions(jitlink::LinkGraph &G) {
803 // Record bootstrap function names.
804 std::pair<StringRef, ExecutorAddr *> RuntimeSymbols[] = {
805 {*MP.MachOHeaderStartSymbol, &MP.Bootstrap.load()->MachOHeaderAddr},
806 {*MP.PlatformBootstrap.Name, &MP.PlatformBootstrap.Addr},
807 {*MP.PlatformShutdown.Name, &MP.PlatformShutdown.Addr},
808 {*MP.RegisterJITDylib.Name, &MP.RegisterJITDylib.Addr},
809 {*MP.DeregisterJITDylib.Name, &MP.DeregisterJITDylib.Addr},
810 {*MP.RegisterObjectPlatformSections.Name,
811 &MP.RegisterObjectPlatformSections.Addr},
812 {*MP.DeregisterObjectPlatformSections.Name,
813 &MP.DeregisterObjectPlatformSections.Addr},
814 {*MP.CreatePThreadKey.Name, &MP.CreatePThreadKey.Addr}};
815
816 bool RegisterMachOHeader = false;
817
818 for (auto *Sym : G.defined_symbols()) {
819 for (auto &RTSym : RuntimeSymbols) {
820 if (Sym->hasName() && Sym->getName() == RTSym.first) {
821 if (*RTSym.second)
822 return make_error<StringError>(
823 "Duplicate " + RTSym.first +
824 " detected during MachOPlatform bootstrap",
826
827 if (Sym->getName() == *MP.MachOHeaderStartSymbol)
828 RegisterMachOHeader = true;
829
830 *RTSym.second = Sym->getAddress();
831 }
832 }
833 }
834
835 if (RegisterMachOHeader) {
836 // If this graph defines the macho header symbol then create the internal
837 // mapping between it and PlatformJD.
838 std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
839 MP.JITDylibToHeaderAddr[&MP.PlatformJD] =
840 MP.Bootstrap.load()->MachOHeaderAddr;
841 MP.HeaderAddrToJITDylib[MP.Bootstrap.load()->MachOHeaderAddr] =
842 &MP.PlatformJD;
843 }
844
845 return Error::success();
846}
847
848Error MachOPlatform::MachOPlatformPlugin::bootstrapPipelineEnd(
850 std::lock_guard<std::mutex> Lock(MP.Bootstrap.load()->Mutex);
851 assert(MP.Bootstrap && "DeferredAAs reset before bootstrap completed");
852 --MP.Bootstrap.load()->ActiveGraphs;
853 // Notify Bootstrap->CV while holding the mutex because the mutex is
854 // also keeping Bootstrap->CV alive.
855 if (MP.Bootstrap.load()->ActiveGraphs == 0)
856 MP.Bootstrap.load()->CV.notify_all();
857 return Error::success();
858}
859
860Error MachOPlatform::MachOPlatformPlugin::associateJITDylibHeaderSymbol(
862 auto I = llvm::find_if(G.defined_symbols(), [this](jitlink::Symbol *Sym) {
863 return Sym->getName() == *MP.MachOHeaderStartSymbol;
864 });
865 assert(I != G.defined_symbols().end() && "Missing MachO header start symbol");
866
867 auto &JD = MR.getTargetJITDylib();
868 std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
869 auto HeaderAddr = (*I)->getAddress();
870 MP.JITDylibToHeaderAddr[&JD] = HeaderAddr;
871 MP.HeaderAddrToJITDylib[HeaderAddr] = &JD;
872 // We can unconditionally add these actions to the Graph because this pass
873 // isn't used during bootstrap.
874 G.allocActions().push_back(
875 {cantFail(
877 MP.RegisterJITDylib.Addr, JD.getName(), HeaderAddr)),
879 MP.DeregisterJITDylib.Addr, HeaderAddr))});
880 return Error::success();
881}
882
883Error MachOPlatform::MachOPlatformPlugin::preserveInitSections(
885
886 JITLinkSymbolSet InitSectionSymbols;
887 for (auto &InitSectionName : MachOInitSectionNames) {
888 // Skip non-init sections.
889 auto *InitSection = G.findSectionByName(InitSectionName);
890 if (!InitSection)
891 continue;
892
893 // Make a pass over live symbols in the section: those blocks are already
894 // preserved.
895 DenseSet<jitlink::Block *> AlreadyLiveBlocks;
896 for (auto &Sym : InitSection->symbols()) {
897 auto &B = Sym->getBlock();
898 if (Sym->isLive() && Sym->getOffset() == 0 &&
899 Sym->getSize() == B.getSize() && !AlreadyLiveBlocks.count(&B)) {
900 InitSectionSymbols.insert(Sym);
901 AlreadyLiveBlocks.insert(&B);
902 }
903 }
904
905 // Add anonymous symbols to preserve any not-already-preserved blocks.
906 for (auto *B : InitSection->blocks())
907 if (!AlreadyLiveBlocks.count(B))
908 InitSectionSymbols.insert(
909 &G.addAnonymousSymbol(*B, 0, B->getSize(), false, true));
910 }
911
912 if (!InitSectionSymbols.empty()) {
913 std::lock_guard<std::mutex> Lock(PluginMutex);
914 InitSymbolDeps[&MR] = std::move(InitSectionSymbols);
915 }
916
917 return Error::success();
918}
919
920Error MachOPlatform::MachOPlatformPlugin::processObjCImageInfo(
922
923 // If there's an ObjC imagine info then either
924 // (1) It's the first __objc_imageinfo we've seen in this JITDylib. In
925 // this case we name and record it.
926 // OR
927 // (2) We already have a recorded __objc_imageinfo for this JITDylib,
928 // in which case we just verify it.
929 auto *ObjCImageInfo = G.findSectionByName(MachOObjCImageInfoSectionName);
930 if (!ObjCImageInfo)
931 return Error::success();
932
933 auto ObjCImageInfoBlocks = ObjCImageInfo->blocks();
934
935 // Check that the section is not empty if present.
936 if (ObjCImageInfoBlocks.empty())
937 return make_error<StringError>("Empty " + MachOObjCImageInfoSectionName +
938 " section in " + G.getName(),
940
941 // Check that there's only one block in the section.
942 if (std::next(ObjCImageInfoBlocks.begin()) != ObjCImageInfoBlocks.end())
943 return make_error<StringError>("Multiple blocks in " +
945 " section in " + G.getName(),
947
948 // Check that the __objc_imageinfo section is unreferenced.
949 // FIXME: We could optimize this check if Symbols had a ref-count.
950 for (auto &Sec : G.sections()) {
951 if (&Sec != ObjCImageInfo)
952 for (auto *B : Sec.blocks())
953 for (auto &E : B->edges())
954 if (E.getTarget().isDefined() &&
955 &E.getTarget().getBlock().getSection() == ObjCImageInfo)
956 return make_error<StringError>(MachOObjCImageInfoSectionName +
957 " is referenced within file " +
958 G.getName(),
960 }
961
962 auto &ObjCImageInfoBlock = **ObjCImageInfoBlocks.begin();
963 auto *ObjCImageInfoData = ObjCImageInfoBlock.getContent().data();
964 auto Version = support::endian::read32(ObjCImageInfoData, G.getEndianness());
965 auto Flags =
966 support::endian::read32(ObjCImageInfoData + 4, G.getEndianness());
967
968 // Lock the mutex while we verify / update the ObjCImageInfos map.
969 std::lock_guard<std::mutex> Lock(PluginMutex);
970
971 auto ObjCImageInfoItr = ObjCImageInfos.find(&MR.getTargetJITDylib());
972 if (ObjCImageInfoItr != ObjCImageInfos.end()) {
973 // We've already registered an __objc_imageinfo section. Verify the
974 // content of this new section matches, then delete it.
975 if (ObjCImageInfoItr->second.first != Version)
976 return make_error<StringError>(
977 "ObjC version in " + G.getName() +
978 " does not match first registered version",
980 if (ObjCImageInfoItr->second.second != Flags)
981 return make_error<StringError>("ObjC flags in " + G.getName() +
982 " do not match first registered flags",
984
985 // __objc_imageinfo is valid. Delete the block.
986 for (auto *S : ObjCImageInfo->symbols())
987 G.removeDefinedSymbol(*S);
988 G.removeBlock(ObjCImageInfoBlock);
989 } else {
990 // We haven't registered an __objc_imageinfo section yet. Register and
991 // move on. The section should already be marked no-dead-strip.
992 ObjCImageInfos[&MR.getTargetJITDylib()] = std::make_pair(Version, Flags);
993 }
994
995 return Error::success();
996}
997
998Error MachOPlatform::MachOPlatformPlugin::fixTLVSectionsAndEdges(
1000
1001 // Rename external references to __tlv_bootstrap to ___orc_rt_tlv_get_addr.
1002 for (auto *Sym : G.external_symbols())
1003 if (Sym->getName() == "__tlv_bootstrap") {
1004 Sym->setName("___orc_rt_macho_tlv_get_addr");
1005 break;
1006 }
1007
1008 // Store key in __thread_vars struct fields.
1009 if (auto *ThreadDataSec = G.findSectionByName(MachOThreadVarsSectionName)) {
1010 std::optional<uint64_t> Key;
1011 {
1012 std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
1013 auto I = MP.JITDylibToPThreadKey.find(&JD);
1014 if (I != MP.JITDylibToPThreadKey.end())
1015 Key = I->second;
1016 }
1017
1018 if (!Key) {
1019 if (auto KeyOrErr = MP.createPThreadKey())
1020 Key = *KeyOrErr;
1021 else
1022 return KeyOrErr.takeError();
1023 }
1024
1025 uint64_t PlatformKeyBits =
1026 support::endian::byte_swap(*Key, G.getEndianness());
1027
1028 for (auto *B : ThreadDataSec->blocks()) {
1029 if (B->getSize() != 3 * G.getPointerSize())
1030 return make_error<StringError>("__thread_vars block at " +
1031 formatv("{0:x}", B->getAddress()) +
1032 " has unexpected size",
1034
1035 auto NewBlockContent = G.allocateBuffer(B->getSize());
1036 llvm::copy(B->getContent(), NewBlockContent.data());
1037 memcpy(NewBlockContent.data() + G.getPointerSize(), &PlatformKeyBits,
1038 G.getPointerSize());
1039 B->setContent(NewBlockContent);
1040 }
1041 }
1042
1043 // Transform any TLV edges into GOT edges.
1044 for (auto *B : G.blocks())
1045 for (auto &E : B->edges())
1046 if (E.getKind() ==
1048 E.setKind(jitlink::x86_64::
1049 RequestGOTAndTransformToPCRel32GOTLoadREXRelaxable);
1050
1051 return Error::success();
1052}
1053
1054std::optional<MachOPlatform::MachOPlatformPlugin::UnwindSections>
1055MachOPlatform::MachOPlatformPlugin::findUnwindSectionInfo(
1057 using namespace jitlink;
1058
1059 UnwindSections US;
1060
1061 // ScanSection records a section range and adds any executable blocks that
1062 // that section points to to the CodeBlocks vector.
1063 SmallVector<Block *> CodeBlocks;
1064 auto ScanUnwindInfoSection = [&](Section &Sec, ExecutorAddrRange &SecRange) {
1065 if (Sec.blocks().empty())
1066 return;
1067 SecRange = (*Sec.blocks().begin())->getRange();
1068 for (auto *B : Sec.blocks()) {
1069 auto R = B->getRange();
1070 SecRange.Start = std::min(SecRange.Start, R.Start);
1071 SecRange.End = std::max(SecRange.End, R.End);
1072 for (auto &E : B->edges()) {
1073 if (!E.getTarget().isDefined())
1074 continue;
1075 auto &TargetBlock = E.getTarget().getBlock();
1076 auto &TargetSection = TargetBlock.getSection();
1077 if ((TargetSection.getMemProt() & MemProt::Exec) == MemProt::Exec)
1078 CodeBlocks.push_back(&TargetBlock);
1079 }
1080 }
1081 };
1082
1083 if (Section *EHFrameSec = G.findSectionByName(MachOEHFrameSectionName))
1084 ScanUnwindInfoSection(*EHFrameSec, US.DwarfSection);
1085
1086 if (Section *CUInfoSec =
1087 G.findSectionByName(MachOCompactUnwindInfoSectionName))
1088 ScanUnwindInfoSection(*CUInfoSec, US.CompactUnwindSection);
1089
1090 // If we didn't find any pointed-to code-blocks then there's no need to
1091 // register any info.
1092 if (CodeBlocks.empty())
1093 return std::nullopt;
1094
1095 // We have info to register. Sort the code blocks into address order and
1096 // build a list of contiguous address ranges covering them all.
1097 llvm::sort(CodeBlocks, [](const Block *LHS, const Block *RHS) {
1098 return LHS->getAddress() < RHS->getAddress();
1099 });
1100 for (auto *B : CodeBlocks) {
1101 if (US.CodeRanges.empty() || US.CodeRanges.back().End != B->getAddress())
1102 US.CodeRanges.push_back(B->getRange());
1103 else
1104 US.CodeRanges.back().End = B->getRange().End;
1105 }
1106
1107 LLVM_DEBUG({
1108 dbgs() << "MachOPlatform identified unwind info in " << G.getName() << ":\n"
1109 << " DWARF: ";
1110 if (US.DwarfSection.Start)
1111 dbgs() << US.DwarfSection << "\n";
1112 else
1113 dbgs() << "none\n";
1114 dbgs() << " Compact-unwind: ";
1115 if (US.CompactUnwindSection.Start)
1116 dbgs() << US.CompactUnwindSection << "\n";
1117 else
1118 dbgs() << "none\n"
1119 << "for code ranges:\n";
1120 for (auto &CR : US.CodeRanges)
1121 dbgs() << " " << CR << "\n";
1122 if (US.CodeRanges.size() >= G.sections_size())
1123 dbgs() << "WARNING: High number of discontiguous code ranges! "
1124 "Padding may be interfering with coalescing.\n";
1125 });
1126
1127 return US;
1128}
1129
1130Error MachOPlatform::MachOPlatformPlugin::registerObjectPlatformSections(
1131 jitlink::LinkGraph &G, JITDylib &JD, bool InBootstrapPhase) {
1132
1133 // Get a pointer to the thread data section if there is one. It will be used
1134 // below.
1135 jitlink::Section *ThreadDataSection =
1136 G.findSectionByName(MachOThreadDataSectionName);
1137
1138 // Handle thread BSS section if there is one.
1139 if (auto *ThreadBSSSection = G.findSectionByName(MachOThreadBSSSectionName)) {
1140 // If there's already a thread data section in this graph then merge the
1141 // thread BSS section content into it, otherwise just treat the thread
1142 // BSS section as the thread data section.
1143 if (ThreadDataSection)
1144 G.mergeSections(*ThreadDataSection, *ThreadBSSSection);
1145 else
1146 ThreadDataSection = ThreadBSSSection;
1147 }
1148
1150
1151 // Collect data sections to register.
1152 StringRef DataSections[] = {MachODataDataSectionName,
1155 for (auto &SecName : DataSections) {
1156 if (auto *Sec = G.findSectionByName(SecName)) {
1158 if (!R.empty())
1159 MachOPlatformSecs.push_back({SecName, R.getRange()});
1160 }
1161 }
1162
1163 // Having merged thread BSS (if present) and thread data (if present),
1164 // record the resulting section range.
1165 if (ThreadDataSection) {
1166 jitlink::SectionRange R(*ThreadDataSection);
1167 if (!R.empty())
1168 MachOPlatformSecs.push_back({MachOThreadDataSectionName, R.getRange()});
1169 }
1170
1171 // If any platform sections were found then add an allocation action to call
1172 // the registration function.
1173 StringRef PlatformSections[] = {
1178 };
1179
1180 for (auto &SecName : PlatformSections) {
1181 auto *Sec = G.findSectionByName(SecName);
1182 if (!Sec)
1183 continue;
1185 if (R.empty())
1186 continue;
1187
1188 MachOPlatformSecs.push_back({SecName, R.getRange()});
1189 }
1190
1191 std::optional<std::tuple<SmallVector<ExecutorAddrRange>, ExecutorAddrRange,
1193 UnwindInfo;
1194 if (auto UI = findUnwindSectionInfo(G))
1195 UnwindInfo = std::make_tuple(std::move(UI->CodeRanges), UI->DwarfSection,
1196 UI->CompactUnwindSection);
1197
1198 if (!MachOPlatformSecs.empty() || UnwindInfo) {
1199 ExecutorAddr HeaderAddr;
1200 {
1201 std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
1202 auto I = MP.JITDylibToHeaderAddr.find(&JD);
1203 assert(I != MP.JITDylibToHeaderAddr.end() &&
1204 "Missing header for JITDylib");
1205 HeaderAddr = I->second;
1206 }
1207
1208 // Dump the scraped inits.
1209 LLVM_DEBUG({
1210 dbgs() << "MachOPlatform: Scraped " << G.getName() << " init sections:\n";
1211 for (auto &KV : MachOPlatformSecs)
1212 dbgs() << " " << KV.first << ": " << KV.second << "\n";
1213 });
1214
1215 using SPSRegisterObjectPlatformSectionsArgs = SPSArgList<
1220
1221 shared::AllocActions &allocActions = LLVM_LIKELY(!InBootstrapPhase)
1222 ? G.allocActions()
1223 : MP.Bootstrap.load()->DeferredAAs;
1224
1225 allocActions.push_back(
1226 {cantFail(
1227 WrapperFunctionCall::Create<SPSRegisterObjectPlatformSectionsArgs>(
1228 MP.RegisterObjectPlatformSections.Addr, HeaderAddr, UnwindInfo,
1229 MachOPlatformSecs)),
1230 cantFail(
1231 WrapperFunctionCall::Create<SPSRegisterObjectPlatformSectionsArgs>(
1232 MP.DeregisterObjectPlatformSections.Addr, HeaderAddr,
1233 UnwindInfo, MachOPlatformSecs))});
1234 }
1235
1236 return Error::success();
1237}
1238
1239} // End namespace orc.
1240} // End namespace llvm.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:209
#define LLVM_DEBUG(X)
Definition: Debug.h:101
std::string Name
#define _
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
#define H(x, y, z)
Definition: MD5.cpp:57
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
@ Flags
Definition: TextStubV5.cpp:93
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
unsigned size() const
Definition: DenseMap.h:99
bool empty() const
Definition: DenseMap.h:98
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:151
iterator end()
Definition: DenseMap.h:84
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:103
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Helper for Errors used as out-parameters.
Definition: Error.h:1104
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
Tagged union holding either a T or a Error.
Definition: Error.h:470
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
bool empty() const
Definition: SmallVector.h:94
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
const std::string & str() const
Definition: Triple.h:415
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:97
An ExecutionSession represents a running JIT program.
Definition: Core.h:1373
ExecutorProcessControl & getExecutorProcessControl()
Get the ExecutorProcessControl object associated with this ExecutionSession.
Definition: Core.h:1416
const Triple & getTargetTriple() const
Return the triple for the executor.
Definition: Core.h:1419
Error callSPSWrapper(ExecutorAddr WrapperFnAddr, WrapperCallArgTs &&...WrapperCallArgs)
Run a wrapper function using SPS to serialize the arguments and deserialize the results.
Definition: Core.h:1621
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition: Core.h:1427
static JITDispatchHandlerFunction wrapAsyncWithSPS(HandlerT &&H)
Wrap a handler that takes concrete argument types (and a sender for a concrete return type) to produc...
Definition: Core.h:1635
void lookup(LookupKind K, const JITDylibSearchOrder &SearchOrder, SymbolLookupSet Symbols, SymbolState RequiredState, SymbolsResolvedCallback NotifyComplete, RegisterDependenciesFunction RegisterDependencies)
Search the given JITDylibs for the given symbols.
Definition: Core.cpp:2084
Error registerJITDispatchHandlers(JITDylib &JD, JITDispatchHandlerAssociationMap WFs)
For each tag symbol name, associate the corresponding AsyncHandlerWrapperFunction with the address of...
Definition: Core.cpp:2193
decltype(auto) runSessionLocked(Func &&F)
Run the given lambda with the session mutex locked.
Definition: Core.h:1437
Represents an address in the executor process.
uint64_t getValue() const
Represents a JIT'd dynamic library.
Definition: Core.h:964
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:1815
GeneratorT & addGenerator(std::unique_ptr< GeneratorT > DefGenerator)
Adds a definition generator to this JITDylib and returns a referenece to it.
Definition: Core.h:1798
Mediates between MachO initialization and ExecutionSession state.
Definition: MachOPlatform.h:30
ObjectLinkingLayer & getObjectLinkingLayer() const
Definition: MachOPlatform.h:92
Error teardownJITDylib(JITDylib &JD) override
This method will be called outside the session lock each time a JITDylib is removed to allow the Plat...
Error setupJITDylib(JITDylib &JD) override
This method will be called outside the session lock each time a JITDylib is created (unless it is cre...
static ArrayRef< std::pair< const char *, const char * > > standardRuntimeUtilityAliases()
Returns the array of standard runtime utility aliases for MachO.
Error notifyAdding(ResourceTracker &RT, const MaterializationUnit &MU) override
This method will be called under the ExecutionSession lock each time a MaterializationUnit is added t...
static Expected< std::unique_ptr< MachOPlatform > > Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD, std::unique_ptr< DefinitionGenerator > OrcRuntime, std::optional< SymbolAliasMap > RuntimeAliases=std::nullopt)
Try to create a MachOPlatform instance, adding the ORC runtime to the given JITDylib.
static SymbolAliasMap standardPlatformAliases(ExecutionSession &ES)
Returns an AliasMap containing the default aliases for the MachOPlatform.
ExecutionSession & getExecutionSession() const
Definition: MachOPlatform.h:91
Error notifyRemoving(ResourceTracker &RT) override
This method will be called under the ExecutionSession lock when a ResourceTracker is removed.
static ArrayRef< std::pair< const char *, const char * > > requiredCXXAliases()
Returns the array of required CXX aliases.
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:526
const SymbolStringPtr & getInitializerSymbol() const
Returns the initialization pseudo-symbol, if any.
Definition: Core.h:562
JITDylib & getTargetJITDylib() const
Returns the target JITDylib that these symbols are being materialized into.
Definition: Core.h:548
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
Definition: Core.h:675
virtual StringRef getName() const =0
Return the name of this materialization unit.
virtual void materialize(std::unique_ptr< MaterializationResponsibility > R)=0
Implementations of this method should materialize all symbols in the materialzation unit,...
const SymbolStringPtr & getInitializerSymbol() const
Returns the initialization symbol for this MaterializationUnit (if any).
Definition: Core.h:708
An ObjectLayer implementation built on JITLink.
ObjectLinkingLayer & addPlugin(std::unique_ptr< Plugin > P)
Add a pass-config modifier.
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O) override
Emit an object file.
API to remove / transfer ownership of JIT resources.
Definition: Core.h:55
JITDylib & getJITDylib() const
Return the JITDylib targeted by this tracker.
Definition: Core.h:70
static Expected< std::unique_ptr< StaticLibraryDefinitionGenerator > > Load(ObjectLayer &L, const char *FileName, GetObjectFileInterface GetObjFileInterface=GetObjectFileInterface())
Try to create a StaticLibraryDefinitionGenerator from the given path.
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition: Core.h:182
Pointer to a pooled string representing a symbol name.
A utility class for serializing to a blob from a variadic list.
SPS tag type for expecteds, which are either a T or a string representing an error.
Input char buffer with underflow check.
Output char buffer with overflow check.
static bool serialize(SPSOutputBuffer &OB, const MachOPlatform::MachOJITDylibDepInfo &DDI)
static bool deserialize(SPSInputBuffer &IB, MachOPlatform::MachOJITDylibDepInfo &DDI)
Specialize to describe how to serialize/deserialize to/from the given concrete type.
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.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
Key
PAL metadata keys.
const uint64_t Version
Definition: InstrProf.h:1058
@ MH_MAGIC_64
Definition: MachO.h:32
@ CPU_SUBTYPE_ARM64_ALL
Definition: MachO.h:1639
@ MH_DYLIB
Definition: MachO.h:48
void swapStruct(fat_header &mh)
Definition: MachO.h:1139
@ CPU_SUBTYPE_X86_64_ALL
Definition: MachO.h:1609
@ CPU_TYPE_ARM64
Definition: MachO.h:1568
@ CPU_TYPE_X86_64
Definition: MachO.h:1564
constexpr support::endianness Endianness
The endianness of all multi-byte encoded values in MessagePack.
Definition: MsgPack.h:24
SPSTuple< SPSExecutorAddr, SPSExecutorAddr > SPSExecutorAddrRange
std::vector< AllocActionCallPair > AllocActions
A vector of allocation actions to be run for this allocation.
StringRef MachOThreadBSSSectionName
StringRef MachOThreadVarsSectionName
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:165
StringRef MachOCompactUnwindInfoSectionName
std::unique_ptr< ReExportsMaterializationUnit > symbolAliases(SymbolAliasMap Aliases)
Create a ReExportsMaterializationUnit with the given aliases.
Definition: Core.h:819
std::unique_ptr< AbsoluteSymbolsMaterializationUnit > absoluteSymbols(SymbolMap Symbols)
Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
Definition: Core.h:773
StringRef MachOSwift5ProtosSectionName
StringRef MachOEHFrameSectionName
StringRef MachOModInitFuncSectionName
StringRef MachODataDataSectionName
StringRef MachOSwift5ProtoSectionName
static void addAliases(ExecutionSession &ES, SymbolAliasMap &Aliases, ArrayRef< std::pair< const char *, const char * > > AL)
StringRef MachOObjCClassListSectionName
StringRef MachOObjCSelRefsSectionName
StringRef MachOInitSectionNames[6]
StringRef MachOSwift5TypesSectionName
StringRef MachOObjCImageInfoSectionName
RegisterDependenciesFunction NoDependenciesToRegister
This can be used as the value for a RegisterDependenciesFunction if there are no dependants to regist...
Definition: Core.cpp:35
StringRef MachOThreadDataSectionName
StringRef MachODataCommonSectionName
value_type byte_swap(value_type value, endianness endian)
Definition: Endian.h:49
uint32_t read32(const void *P, endianness E)
Definition: Endian.h:363
constexpr endianness system_endianness()
Definition: Endian.h:44
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:79
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1744
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:745
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1921
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:1946
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1846
Definition: BitVector.h:858
Represents an address range in the exceutor process.