LLVM 20.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
35class SPSMachOExecutorSymbolFlags;
36
37template <>
39 MachOPlatform::MachOJITDylibDepInfo> {
40public:
41 static size_t size(const MachOPlatform::MachOJITDylibDepInfo &DDI) {
42 return SPSMachOJITDylibDepInfo::AsArgList::size(DDI.Sealed, DDI.DepHeaders);
43 }
44
45 static bool serialize(SPSOutputBuffer &OB,
47 return SPSMachOJITDylibDepInfo::AsArgList::serialize(OB, DDI.Sealed,
48 DDI.DepHeaders);
49 }
50
51 static bool deserialize(SPSInputBuffer &IB,
53 return SPSMachOJITDylibDepInfo::AsArgList::deserialize(IB, DDI.Sealed,
54 DDI.DepHeaders);
55 }
56};
57
58template <>
59class SPSSerializationTraits<SPSMachOExecutorSymbolFlags,
60 MachOPlatform::MachOExecutorSymbolFlags> {
61private:
62 using UT = std::underlying_type_t<MachOPlatform::MachOExecutorSymbolFlags>;
63
64public:
66 return sizeof(UT);
67 }
68
69 static bool serialize(SPSOutputBuffer &OB,
71 return SPSArgList<UT>::serialize(OB, static_cast<UT>(SF));
72 }
73
74 static bool deserialize(SPSInputBuffer &IB,
76 UT Tmp;
77 if (!SPSArgList<UT>::deserialize(IB, Tmp))
78 return false;
79 SF = static_cast<MachOPlatform::MachOExecutorSymbolFlags>(Tmp);
80 return true;
81 }
82};
83
84} // namespace shared
85} // namespace orc
86} // namespace llvm
87
88namespace {
89
90using SPSRegisterSymbolsArgs =
93 SPSMachOExecutorSymbolFlags>>>;
94
95std::unique_ptr<jitlink::LinkGraph> createPlatformGraph(MachOPlatform &MOP,
96 std::string Name) {
97 auto &ES = MOP.getExecutionSession();
98 return std::make_unique<jitlink::LinkGraph>(
99 std::move(Name), ES.getSymbolStringPool(), ES.getTargetTriple(),
101}
102
103// Creates a Bootstrap-Complete LinkGraph to run deferred actions.
104class MachOPlatformCompleteBootstrapMaterializationUnit
105 : public MaterializationUnit {
106public:
107 using SymbolTableVector =
110
111 MachOPlatformCompleteBootstrapMaterializationUnit(
112 MachOPlatform &MOP, StringRef PlatformJDName,
113 SymbolStringPtr CompleteBootstrapSymbol, SymbolTableVector SymTab,
114 shared::AllocActions DeferredAAs, ExecutorAddr MachOHeaderAddr,
115 ExecutorAddr PlatformBootstrap, ExecutorAddr PlatformShutdown,
116 ExecutorAddr RegisterJITDylib, ExecutorAddr DeregisterJITDylib,
117 ExecutorAddr RegisterObjectSymbolTable,
118 ExecutorAddr DeregisterObjectSymbolTable)
120 {{{CompleteBootstrapSymbol, JITSymbolFlags::None}}, nullptr}),
121 MOP(MOP), PlatformJDName(PlatformJDName),
122 CompleteBootstrapSymbol(std::move(CompleteBootstrapSymbol)),
123 SymTab(std::move(SymTab)), DeferredAAs(std::move(DeferredAAs)),
124 MachOHeaderAddr(MachOHeaderAddr), PlatformBootstrap(PlatformBootstrap),
125 PlatformShutdown(PlatformShutdown), RegisterJITDylib(RegisterJITDylib),
126 DeregisterJITDylib(DeregisterJITDylib),
127 RegisterObjectSymbolTable(RegisterObjectSymbolTable),
128 DeregisterObjectSymbolTable(DeregisterObjectSymbolTable) {}
129
130 StringRef getName() const override {
131 return "MachOPlatformCompleteBootstrap";
132 }
133
134 void materialize(std::unique_ptr<MaterializationResponsibility> R) override {
135 using namespace jitlink;
136 auto G = createPlatformGraph(MOP, "<OrcRTCompleteBootstrap>");
137 auto &PlaceholderSection =
138 G->createSection("__orc_rt_cplt_bs", MemProt::Read);
139 auto &PlaceholderBlock =
140 G->createZeroFillBlock(PlaceholderSection, 1, ExecutorAddr(), 1, 0);
141 G->addDefinedSymbol(PlaceholderBlock, 0, *CompleteBootstrapSymbol, 1,
142 Linkage::Strong, Scope::Hidden, false, true);
143
144 // Reserve space for the stolen actions, plus two extras.
145 G->allocActions().reserve(DeferredAAs.size() + 3);
146
147 // 1. Bootstrap the platform support code.
148 G->allocActions().push_back(
150 cantFail(
151 WrapperFunctionCall::Create<SPSArgList<>>(PlatformShutdown))});
152
153 // 2. Register the platform JITDylib.
154 G->allocActions().push_back(
157 RegisterJITDylib, PlatformJDName, MachOHeaderAddr)),
159 DeregisterJITDylib, MachOHeaderAddr))});
160
161 // 3. Register deferred symbols.
162 G->allocActions().push_back(
163 {cantFail(WrapperFunctionCall::Create<SPSRegisterSymbolsArgs>(
164 RegisterObjectSymbolTable, MachOHeaderAddr, SymTab)),
165 cantFail(WrapperFunctionCall::Create<SPSRegisterSymbolsArgs>(
166 DeregisterObjectSymbolTable, MachOHeaderAddr, SymTab))});
167
168 // 4. Add the deferred actions to the graph.
169 std::move(DeferredAAs.begin(), DeferredAAs.end(),
170 std::back_inserter(G->allocActions()));
171
172 MOP.getObjectLinkingLayer().emit(std::move(R), std::move(G));
173 }
174
175 void discard(const JITDylib &JD, const SymbolStringPtr &Sym) override {}
176
177private:
178 MachOPlatform &MOP;
179 StringRef PlatformJDName;
180 SymbolStringPtr CompleteBootstrapSymbol;
181 SymbolTableVector SymTab;
182 shared::AllocActions DeferredAAs;
183 ExecutorAddr MachOHeaderAddr;
184 ExecutorAddr PlatformBootstrap;
185 ExecutorAddr PlatformShutdown;
186 ExecutorAddr RegisterJITDylib;
187 ExecutorAddr DeregisterJITDylib;
188 ExecutorAddr RegisterObjectSymbolTable;
189 ExecutorAddr DeregisterObjectSymbolTable;
190};
191
192static StringRef ObjCRuntimeObjectSectionsData[] = {
199
200static StringRef ObjCRuntimeObjectSectionsText[] = {
206
207static StringRef ObjCRuntimeObjectSectionName =
208 "__llvm_jitlink_ObjCRuntimeRegistrationObject";
209
210static StringRef ObjCImageInfoSymbolName =
211 "__llvm_jitlink_macho_objc_imageinfo";
212
213struct ObjCImageInfoFlags {
214 uint16_t SwiftABIVersion;
215 uint16_t SwiftVersion;
216 bool HasCategoryClassProperties;
217 bool HasSignedObjCClassROs;
218
219 static constexpr uint32_t SIGNED_CLASS_RO = (1 << 4);
220 static constexpr uint32_t HAS_CATEGORY_CLASS_PROPERTIES = (1 << 6);
221
222 explicit ObjCImageInfoFlags(uint32_t RawFlags) {
223 HasSignedObjCClassROs = RawFlags & SIGNED_CLASS_RO;
224 HasCategoryClassProperties = RawFlags & HAS_CATEGORY_CLASS_PROPERTIES;
225 SwiftABIVersion = (RawFlags >> 8) & 0xFF;
226 SwiftVersion = (RawFlags >> 16) & 0xFFFF;
227 }
228
229 uint32_t rawFlags() const {
230 uint32_t Result = 0;
231 if (HasCategoryClassProperties)
232 Result |= HAS_CATEGORY_CLASS_PROPERTIES;
233 if (HasSignedObjCClassROs)
234 Result |= SIGNED_CLASS_RO;
235 Result |= (SwiftABIVersion << 8);
236 Result |= (SwiftVersion << 16);
237 return Result;
238 }
239};
240} // end anonymous namespace
241
242namespace llvm {
243namespace orc {
244
245std::optional<MachOPlatform::HeaderOptions::BuildVersionOpts>
247 uint32_t MinOS,
248 uint32_t SDK) {
249
251 switch (TT.getOS()) {
252 case Triple::IOS:
253 Platform = TT.isSimulatorEnvironment() ? MachO::PLATFORM_IOSSIMULATOR
254 : MachO::PLATFORM_IOS;
255 break;
256 case Triple::MacOSX:
257 Platform = MachO::PLATFORM_MACOS;
258 break;
259 case Triple::TvOS:
260 Platform = TT.isSimulatorEnvironment() ? MachO::PLATFORM_TVOSSIMULATOR
261 : MachO::PLATFORM_TVOS;
262 break;
263 case Triple::WatchOS:
264 Platform = TT.isSimulatorEnvironment() ? MachO::PLATFORM_WATCHOSSIMULATOR
265 : MachO::PLATFORM_WATCHOS;
266 break;
267 case Triple::XROS:
268 Platform = TT.isSimulatorEnvironment() ? MachO::PLATFORM_XROS_SIMULATOR
269 : MachO::PLATFORM_XROS;
270 break;
271 default:
272 return std::nullopt;
273 }
274
276}
277
280 std::unique_ptr<DefinitionGenerator> OrcRuntime,
281 HeaderOptions PlatformJDOpts,
282 MachOHeaderMUBuilder BuildMachOHeaderMU,
283 std::optional<SymbolAliasMap> RuntimeAliases) {
284
285 auto &ES = ObjLinkingLayer.getExecutionSession();
286
287 // If the target is not supported then bail out immediately.
288 if (!supportedTarget(ES.getTargetTriple()))
289 return make_error<StringError>("Unsupported MachOPlatform triple: " +
290 ES.getTargetTriple().str(),
292
293 auto &EPC = ES.getExecutorProcessControl();
294
295 // Create default aliases if the caller didn't supply any.
296 if (!RuntimeAliases)
297 RuntimeAliases = standardPlatformAliases(ES);
298
299 // Define the aliases.
300 if (auto Err = PlatformJD.define(symbolAliases(std::move(*RuntimeAliases))))
301 return std::move(Err);
302
303 // Add JIT-dispatch function support symbols.
304 if (auto Err = PlatformJD.define(
305 absoluteSymbols({{ES.intern("___orc_rt_jit_dispatch"),
306 {EPC.getJITDispatchInfo().JITDispatchFunction,
308 {ES.intern("___orc_rt_jit_dispatch_ctx"),
309 {EPC.getJITDispatchInfo().JITDispatchContext,
311 return std::move(Err);
312
313 // Create the instance.
314 Error Err = Error::success();
315 auto P = std::unique_ptr<MachOPlatform>(new MachOPlatform(
316 ObjLinkingLayer, PlatformJD, std::move(OrcRuntime),
317 std::move(PlatformJDOpts), std::move(BuildMachOHeaderMU), Err));
318 if (Err)
319 return std::move(Err);
320 return std::move(P);
321}
322
325 const char *OrcRuntimePath, HeaderOptions PlatformJDOpts,
326 MachOHeaderMUBuilder BuildMachOHeaderMU,
327 std::optional<SymbolAliasMap> RuntimeAliases) {
328
329 // Create a generator for the ORC runtime archive.
330 auto OrcRuntimeArchiveGenerator =
331 StaticLibraryDefinitionGenerator::Load(ObjLinkingLayer, OrcRuntimePath);
332 if (!OrcRuntimeArchiveGenerator)
333 return OrcRuntimeArchiveGenerator.takeError();
334
335 return Create(ObjLinkingLayer, PlatformJD,
336 std::move(*OrcRuntimeArchiveGenerator),
337 std::move(PlatformJDOpts), std::move(BuildMachOHeaderMU),
338 std::move(RuntimeAliases));
339}
340
342 return setupJITDylib(JD, /*Opts=*/{});
343}
344
346 if (auto Err = JD.define(BuildMachOHeaderMU(*this, std::move(Opts))))
347 return Err;
348
349 return ES.lookup({&JD}, MachOHeaderStartSymbol).takeError();
350}
351
353 std::lock_guard<std::mutex> Lock(PlatformMutex);
354 auto I = JITDylibToHeaderAddr.find(&JD);
355 if (I != JITDylibToHeaderAddr.end()) {
356 assert(HeaderAddrToJITDylib.count(I->second) &&
357 "HeaderAddrToJITDylib missing entry");
358 HeaderAddrToJITDylib.erase(I->second);
359 JITDylibToHeaderAddr.erase(I);
360 }
361 JITDylibToPThreadKey.erase(&JD);
362 return Error::success();
363}
364
366 const MaterializationUnit &MU) {
367 auto &JD = RT.getJITDylib();
368 const auto &InitSym = MU.getInitializerSymbol();
369 if (!InitSym)
370 return Error::success();
371
372 RegisteredInitSymbols[&JD].add(InitSym,
374 LLVM_DEBUG({
375 dbgs() << "MachOPlatform: Registered init symbol " << *InitSym << " for MU "
376 << MU.getName() << "\n";
377 });
378 return Error::success();
379}
380
382 llvm_unreachable("Not supported yet");
383}
384
386 ArrayRef<std::pair<const char *, const char *>> AL) {
387 for (auto &KV : AL) {
388 auto AliasName = ES.intern(KV.first);
389 assert(!Aliases.count(AliasName) && "Duplicate symbol name in alias map");
390 Aliases[std::move(AliasName)] = {ES.intern(KV.second),
392 }
393}
394
396 SymbolAliasMap Aliases;
397 addAliases(ES, Aliases, requiredCXXAliases());
400 return Aliases;
401}
402
405 static const std::pair<const char *, const char *> RequiredCXXAliases[] = {
406 {"___cxa_atexit", "___orc_rt_macho_cxa_atexit"}};
407
408 return ArrayRef<std::pair<const char *, const char *>>(RequiredCXXAliases);
409}
410
413 static const std::pair<const char *, const char *>
414 StandardRuntimeUtilityAliases[] = {
415 {"___orc_rt_run_program", "___orc_rt_macho_run_program"},
416 {"___orc_rt_jit_dlerror", "___orc_rt_macho_jit_dlerror"},
417 {"___orc_rt_jit_dlopen", "___orc_rt_macho_jit_dlopen"},
418 {"___orc_rt_jit_dlupdate", "___orc_rt_macho_jit_dlupdate"},
419 {"___orc_rt_jit_dlclose", "___orc_rt_macho_jit_dlclose"},
420 {"___orc_rt_jit_dlsym", "___orc_rt_macho_jit_dlsym"},
421 {"___orc_rt_log_error", "___orc_rt_log_error_to_stderr"}};
422
424 StandardRuntimeUtilityAliases);
425}
426
429 static const std::pair<const char *, const char *>
430 StandardLazyCompilationAliases[] = {
431 {"__orc_rt_reenter", "__orc_rt_sysv_reenter"},
432 {"__orc_rt_resolve_tag", "___orc_rt_resolve_tag"}};
433
435 StandardLazyCompilationAliases);
436}
437
438bool MachOPlatform::supportedTarget(const Triple &TT) {
439 switch (TT.getArch()) {
440 case Triple::aarch64:
441 case Triple::x86_64:
442 return true;
443 default:
444 return false;
445 }
446}
447
448jitlink::Edge::Kind MachOPlatform::getPointerEdgeKind(jitlink::LinkGraph &G) {
449 switch (G.getTargetTriple().getArch()) {
450 case Triple::aarch64:
452 case Triple::x86_64:
454 default:
455 llvm_unreachable("Unsupported architecture");
456 }
457}
458
460MachOPlatform::flagsForSymbol(jitlink::Symbol &Sym) {
462 if (Sym.getLinkage() == jitlink::Linkage::Weak)
464
465 if (Sym.isCallable())
467
468 return Flags;
469}
470
471MachOPlatform::MachOPlatform(
472 ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD,
473 std::unique_ptr<DefinitionGenerator> OrcRuntimeGenerator,
474 HeaderOptions PlatformJDOpts, MachOHeaderMUBuilder BuildMachOHeaderMU,
475 Error &Err)
476 : ES(ObjLinkingLayer.getExecutionSession()), PlatformJD(PlatformJD),
477 ObjLinkingLayer(ObjLinkingLayer),
478 BuildMachOHeaderMU(std::move(BuildMachOHeaderMU)) {
480 ObjLinkingLayer.addPlugin(std::make_unique<MachOPlatformPlugin>(*this));
481 PlatformJD.addGenerator(std::move(OrcRuntimeGenerator));
482
483 BootstrapInfo BI;
484 Bootstrap = &BI;
485
486 // Bootstrap process -- here be phase-ordering dragons.
487 //
488 // The MachOPlatform class uses allocation actions to register metadata
489 // sections with the ORC runtime, however the runtime contains metadata
490 // registration functions that have their own metadata that they need to
491 // register (e.g. the frame-info registration functions have frame-info).
492 // We can't use an ordinary lookup to find these registration functions
493 // because their address is needed during the link of the containing graph
494 // itself (to build the allocation actions that will call the registration
495 // functions). Further complicating the situation (a) the graph containing
496 // the registration functions is allowed to depend on other graphs (e.g. the
497 // graph containing the ORC runtime RTTI support) so we need to handle an
498 // unknown set of dependencies during bootstrap, and (b) these graphs may
499 // be linked concurrently if the user has installed a concurrent dispatcher.
500 //
501 // We satisfy these constraints by implementing a bootstrap phase during which
502 // allocation actions generated by MachOPlatform are appended to a list of
503 // deferred allocation actions, rather than to the graphs themselves. At the
504 // end of the bootstrap process the deferred actions are attached to a final
505 // "complete-bootstrap" graph that causes them to be run.
506 //
507 // The bootstrap steps are as follows:
508 //
509 // 1. Request the graph containing the mach header. This graph is guaranteed
510 // not to have any metadata so the fact that the registration functions
511 // are not available yet is not a problem.
512 //
513 // 2. Look up the registration functions and discard the results. This will
514 // trigger linking of the graph containing these functions, and
515 // consequently any graphs that it depends on. We do not use the lookup
516 // result to find the addresses of the functions requested (as described
517 // above the lookup will return too late for that), instead we capture the
518 // addresses in a post-allocation pass injected by the platform runtime
519 // during bootstrap only.
520 //
521 // 3. During bootstrap the MachOPlatformPlugin keeps a count of the number of
522 // graphs being linked (potentially concurrently), and we block until all
523 // of these graphs have completed linking. This is to avoid a race on the
524 // deferred-actions vector: the lookup for the runtime registration
525 // functions may return while some functions (those that are being
526 // incidentally linked in, but aren't reachable via the runtime functions)
527 // are still being linked, and we need to capture any allocation actions
528 // for this incidental code before we proceed.
529 //
530 // 4. Once all active links are complete we transfer the deferred actions to
531 // a newly added CompleteBootstrap graph and then request a symbol from
532 // the CompleteBootstrap graph to trigger materialization. This will cause
533 // all deferred actions to be run, and once this lookup returns we can
534 // proceed.
535 //
536 // 5. Finally, we associate runtime support methods in MachOPlatform with
537 // the corresponding jit-dispatch tag variables in the ORC runtime to make
538 // the support methods callable. The bootstrap is now complete.
539
540 // Step (1) Add header materialization unit and request.
541 if ((Err = PlatformJD.define(
542 this->BuildMachOHeaderMU(*this, std::move(PlatformJDOpts)))))
543 return;
544 if ((Err = ES.lookup(&PlatformJD, MachOHeaderStartSymbol).takeError()))
545 return;
546
547 // Step (2) Request runtime registration functions to trigger
548 // materialization..
549 if ((Err = ES.lookup(makeJITDylibSearchOrder(&PlatformJD),
551 {PlatformBootstrap.Name, PlatformShutdown.Name,
552 RegisterJITDylib.Name, DeregisterJITDylib.Name,
553 RegisterObjectSymbolTable.Name,
554 DeregisterObjectSymbolTable.Name,
555 RegisterObjectPlatformSections.Name,
556 DeregisterObjectPlatformSections.Name,
557 CreatePThreadKey.Name}))
558 .takeError()))
559 return;
560
561 // Step (3) Wait for any incidental linker work to complete.
562 {
563 std::unique_lock<std::mutex> Lock(BI.Mutex);
564 BI.CV.wait(Lock, [&]() { return BI.ActiveGraphs == 0; });
565 Bootstrap = nullptr;
566 }
567
568 // Step (4) Add complete-bootstrap materialization unit and request.
569 auto BootstrapCompleteSymbol = ES.intern("__orc_rt_macho_complete_bootstrap");
570 if ((Err = PlatformJD.define(
571 std::make_unique<MachOPlatformCompleteBootstrapMaterializationUnit>(
572 *this, PlatformJD.getName(), BootstrapCompleteSymbol,
573 std::move(BI.SymTab), std::move(BI.DeferredAAs),
574 BI.MachOHeaderAddr, PlatformBootstrap.Addr,
575 PlatformShutdown.Addr, RegisterJITDylib.Addr,
576 DeregisterJITDylib.Addr, RegisterObjectSymbolTable.Addr,
577 DeregisterObjectSymbolTable.Addr))))
578 return;
579 if ((Err = ES.lookup(makeJITDylibSearchOrder(
581 std::move(BootstrapCompleteSymbol))
582 .takeError()))
583 return;
584
585 // (5) Associate runtime support functions.
586 if ((Err = associateRuntimeSupportFunctions()))
587 return;
588}
589
590Error MachOPlatform::associateRuntimeSupportFunctions() {
592
593 using PushInitializersSPSSig =
595 WFs[ES.intern("___orc_rt_macho_push_initializers_tag")] =
596 ES.wrapAsyncWithSPS<PushInitializersSPSSig>(
597 this, &MachOPlatform::rt_pushInitializers);
598
599 using PushSymbolsSPSSig =
601 WFs[ES.intern("___orc_rt_macho_push_symbols_tag")] =
602 ES.wrapAsyncWithSPS<PushSymbolsSPSSig>(this,
603 &MachOPlatform::rt_pushSymbols);
604
605 return ES.registerJITDispatchHandlers(PlatformJD, std::move(WFs));
606}
607
608void MachOPlatform::pushInitializersLoop(
609 PushInitializersSendResultFn SendResult, JITDylibSP JD) {
612 SmallVector<JITDylib *, 16> Worklist({JD.get()});
613
614 ES.runSessionLocked([&]() {
615 while (!Worklist.empty()) {
616 // FIXME: Check for defunct dylibs.
617
618 auto DepJD = Worklist.back();
619 Worklist.pop_back();
620
621 // If we've already visited this JITDylib on this iteration then continue.
622 if (JDDepMap.count(DepJD))
623 continue;
624
625 // Add dep info.
626 auto &DM = JDDepMap[DepJD];
627 DepJD->withLinkOrderDo([&](const JITDylibSearchOrder &O) {
628 for (auto &KV : O) {
629 if (KV.first == DepJD)
630 continue;
631 DM.push_back(KV.first);
632 Worklist.push_back(KV.first);
633 }
634 });
635
636 // Add any registered init symbols.
637 auto RISItr = RegisteredInitSymbols.find(DepJD);
638 if (RISItr != RegisteredInitSymbols.end()) {
639 NewInitSymbols[DepJD] = std::move(RISItr->second);
640 RegisteredInitSymbols.erase(RISItr);
641 }
642 }
643 });
644
645 // If there are no further init symbols to look up then send the link order
646 // (as a list of header addresses) to the caller.
647 if (NewInitSymbols.empty()) {
648
649 // To make the list intelligible to the runtime we need to convert all
650 // JITDylib pointers to their header addresses. Only include JITDylibs
651 // that appear in the JITDylibToHeaderAddr map (i.e. those that have been
652 // through setupJITDylib) -- bare JITDylibs aren't managed by the platform.
654 HeaderAddrs.reserve(JDDepMap.size());
655 {
656 std::lock_guard<std::mutex> Lock(PlatformMutex);
657 for (auto &KV : JDDepMap) {
658 auto I = JITDylibToHeaderAddr.find(KV.first);
659 if (I != JITDylibToHeaderAddr.end())
660 HeaderAddrs[KV.first] = I->second;
661 }
662 }
663
664 // Build the dep info map to return.
665 MachOJITDylibDepInfoMap DIM;
666 DIM.reserve(JDDepMap.size());
667 for (auto &KV : JDDepMap) {
668 auto HI = HeaderAddrs.find(KV.first);
669 // Skip unmanaged JITDylibs.
670 if (HI == HeaderAddrs.end())
671 continue;
672 auto H = HI->second;
673 MachOJITDylibDepInfo DepInfo;
674 for (auto &Dep : KV.second) {
675 auto HJ = HeaderAddrs.find(Dep);
676 if (HJ != HeaderAddrs.end())
677 DepInfo.DepHeaders.push_back(HJ->second);
678 }
679 DIM.push_back(std::make_pair(H, std::move(DepInfo)));
680 }
681 SendResult(DIM);
682 return;
683 }
684
685 // Otherwise issue a lookup and re-run this phase when it completes.
686 lookupInitSymbolsAsync(
687 [this, SendResult = std::move(SendResult), JD](Error Err) mutable {
688 if (Err)
689 SendResult(std::move(Err));
690 else
691 pushInitializersLoop(std::move(SendResult), JD);
692 },
693 ES, std::move(NewInitSymbols));
694}
695
696void MachOPlatform::rt_pushInitializers(PushInitializersSendResultFn SendResult,
697 ExecutorAddr JDHeaderAddr) {
698 JITDylibSP JD;
699 {
700 std::lock_guard<std::mutex> Lock(PlatformMutex);
701 auto I = HeaderAddrToJITDylib.find(JDHeaderAddr);
702 if (I != HeaderAddrToJITDylib.end())
703 JD = I->second;
704 }
705
706 LLVM_DEBUG({
707 dbgs() << "MachOPlatform::rt_pushInitializers(" << JDHeaderAddr << ") ";
708 if (JD)
709 dbgs() << "pushing initializers for " << JD->getName() << "\n";
710 else
711 dbgs() << "No JITDylib for header address.\n";
712 });
713
714 if (!JD) {
715 SendResult(make_error<StringError>("No JITDylib with header addr " +
716 formatv("{0:x}", JDHeaderAddr),
718 return;
719 }
720
721 pushInitializersLoop(std::move(SendResult), JD);
722}
723
724void MachOPlatform::rt_pushSymbols(
725 PushSymbolsInSendResultFn SendResult, ExecutorAddr Handle,
726 const std::vector<std::pair<StringRef, bool>> &SymbolNames) {
727
728 JITDylib *JD = nullptr;
729
730 {
731 std::lock_guard<std::mutex> Lock(PlatformMutex);
732 auto I = HeaderAddrToJITDylib.find(Handle);
733 if (I != HeaderAddrToJITDylib.end())
734 JD = I->second;
735 }
736 LLVM_DEBUG({
737 dbgs() << "MachOPlatform::rt_pushSymbols(";
738 if (JD)
739 dbgs() << "\"" << JD->getName() << "\", [ ";
740 else
741 dbgs() << "<invalid handle " << Handle << ">, [ ";
742 for (auto &Name : SymbolNames)
743 dbgs() << "\"" << Name.first << "\" ";
744 dbgs() << "])\n";
745 });
746
747 if (!JD) {
748 SendResult(make_error<StringError>("No JITDylib associated with handle " +
749 formatv("{0:x}", Handle),
751 return;
752 }
753
755 for (auto &[Name, Required] : SymbolNames)
756 LS.add(ES.intern(Name), Required
757 ? SymbolLookupFlags::RequiredSymbol
758 : SymbolLookupFlags::WeaklyReferencedSymbol);
759
760 ES.lookup(
761 LookupKind::DLSym, {{JD, JITDylibLookupFlags::MatchExportedSymbolsOnly}},
762 std::move(LS), SymbolState::Ready,
763 [SendResult = std::move(SendResult)](Expected<SymbolMap> Result) mutable {
764 SendResult(Result.takeError());
765 },
767}
768
769Expected<uint64_t> MachOPlatform::createPThreadKey() {
770 if (!CreatePThreadKey.Addr)
771 return make_error<StringError>(
772 "Attempting to create pthread key in target, but runtime support has "
773 "not been loaded yet",
775
777 if (auto Err = ES.callSPSWrapper<SPSExpected<uint64_t>(void)>(
778 CreatePThreadKey.Addr, Result))
779 return std::move(Err);
780 return Result;
781}
782
783void MachOPlatform::MachOPlatformPlugin::modifyPassConfig(
786
787 using namespace jitlink;
788
789 bool InBootstrapPhase = false;
790
791 if (LLVM_UNLIKELY(&MR.getTargetJITDylib() == &MP.PlatformJD)) {
792 std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
793 if (MP.Bootstrap) {
794 InBootstrapPhase = true;
795 ++MP.Bootstrap->ActiveGraphs;
796 }
797 }
798
799 // If we're in the bootstrap phase then increment the active graphs.
800 if (LLVM_UNLIKELY(InBootstrapPhase))
801 Config.PostAllocationPasses.push_back([this](LinkGraph &G) {
802 return bootstrapPipelineRecordRuntimeFunctions(G);
803 });
804
805 // --- Handle Initializers ---
806 if (auto InitSymbol = MR.getInitializerSymbol()) {
807
808 // If the initializer symbol is the MachOHeader start symbol then just
809 // register it and then bail out -- the header materialization unit
810 // definitely doesn't need any other passes.
811 if (InitSymbol == MP.MachOHeaderStartSymbol && !InBootstrapPhase) {
812 Config.PostAllocationPasses.push_back([this, &MR](LinkGraph &G) {
813 return associateJITDylibHeaderSymbol(G, MR);
814 });
815 return;
816 }
817
818 // If the object contains an init symbol other than the header start symbol
819 // then add passes to preserve, process and register the init
820 // sections/symbols.
821 Config.PrePrunePasses.push_back([this, &MR](LinkGraph &G) {
822 if (auto Err = preserveImportantSections(G, MR))
823 return Err;
824 return processObjCImageInfo(G, MR);
825 });
826 Config.PostPrunePasses.push_back(
827 [this](LinkGraph &G) { return createObjCRuntimeObject(G); });
828 Config.PostAllocationPasses.push_back(
829 [this, &MR](LinkGraph &G) { return populateObjCRuntimeObject(G, MR); });
830 }
831
832 // Insert TLV lowering at the start of the PostPrunePasses, since we want
833 // it to run before GOT/PLT lowering.
834 Config.PostPrunePasses.insert(
835 Config.PostPrunePasses.begin(),
836 [this, &JD = MR.getTargetJITDylib()](LinkGraph &G) {
837 return fixTLVSectionsAndEdges(G, JD);
838 });
839
840 // Add symbol table prepare and register passes: These will add strings for
841 // all symbols to the c-strings section, and build a symbol table registration
842 // call.
843 auto JITSymTabInfo = std::make_shared<JITSymTabVector>();
844 Config.PostPrunePasses.push_back([this, JITSymTabInfo](LinkGraph &G) {
845 return prepareSymbolTableRegistration(G, *JITSymTabInfo);
846 });
847 Config.PostFixupPasses.push_back([this, &MR, JITSymTabInfo,
848 InBootstrapPhase](LinkGraph &G) {
849 return addSymbolTableRegistration(G, MR, *JITSymTabInfo, InBootstrapPhase);
850 });
851
852 // Add a pass to register the final addresses of any special sections in the
853 // object with the runtime.
854 Config.PostAllocationPasses.push_back(
855 [this, &JD = MR.getTargetJITDylib(), InBootstrapPhase](LinkGraph &G) {
856 return registerObjectPlatformSections(G, JD, InBootstrapPhase);
857 });
858
859 // If we're in the bootstrap phase then steal allocation actions and then
860 // decrement the active graphs.
861 if (InBootstrapPhase)
862 Config.PostFixupPasses.push_back(
863 [this](LinkGraph &G) { return bootstrapPipelineEnd(G); });
864}
865
866Error MachOPlatform::MachOPlatformPlugin::
867 bootstrapPipelineRecordRuntimeFunctions(jitlink::LinkGraph &G) {
868 // Record bootstrap function names.
869 std::pair<StringRef, ExecutorAddr *> RuntimeSymbols[] = {
870 {*MP.MachOHeaderStartSymbol, &MP.Bootstrap->MachOHeaderAddr},
871 {*MP.PlatformBootstrap.Name, &MP.PlatformBootstrap.Addr},
872 {*MP.PlatformShutdown.Name, &MP.PlatformShutdown.Addr},
873 {*MP.RegisterJITDylib.Name, &MP.RegisterJITDylib.Addr},
874 {*MP.DeregisterJITDylib.Name, &MP.DeregisterJITDylib.Addr},
875 {*MP.RegisterObjectSymbolTable.Name, &MP.RegisterObjectSymbolTable.Addr},
876 {*MP.DeregisterObjectSymbolTable.Name,
877 &MP.DeregisterObjectSymbolTable.Addr},
878 {*MP.RegisterObjectPlatformSections.Name,
879 &MP.RegisterObjectPlatformSections.Addr},
880 {*MP.DeregisterObjectPlatformSections.Name,
881 &MP.DeregisterObjectPlatformSections.Addr},
882 {*MP.CreatePThreadKey.Name, &MP.CreatePThreadKey.Addr},
883 {*MP.RegisterObjCRuntimeObject.Name, &MP.RegisterObjCRuntimeObject.Addr},
884 {*MP.DeregisterObjCRuntimeObject.Name,
885 &MP.DeregisterObjCRuntimeObject.Addr}};
886
887 bool RegisterMachOHeader = false;
888
889 for (auto *Sym : G.defined_symbols()) {
890 for (auto &RTSym : RuntimeSymbols) {
891 if (Sym->hasName() && *Sym->getName() == RTSym.first) {
892 if (*RTSym.second)
893 return make_error<StringError>(
894 "Duplicate " + RTSym.first +
895 " detected during MachOPlatform bootstrap",
897
898 if (Sym->getName() == MP.MachOHeaderStartSymbol)
899 RegisterMachOHeader = true;
900
901 *RTSym.second = Sym->getAddress();
902 }
903 }
904 }
905
906 if (RegisterMachOHeader) {
907 // If this graph defines the macho header symbol then create the internal
908 // mapping between it and PlatformJD.
909 std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
910 MP.JITDylibToHeaderAddr[&MP.PlatformJD] = MP.Bootstrap->MachOHeaderAddr;
911 MP.HeaderAddrToJITDylib[MP.Bootstrap->MachOHeaderAddr] = &MP.PlatformJD;
912 }
913
914 return Error::success();
915}
916
917Error MachOPlatform::MachOPlatformPlugin::bootstrapPipelineEnd(
919 std::lock_guard<std::mutex> Lock(MP.Bootstrap->Mutex);
920
921 --MP.Bootstrap->ActiveGraphs;
922 // Notify Bootstrap->CV while holding the mutex because the mutex is
923 // also keeping Bootstrap->CV alive.
924 if (MP.Bootstrap->ActiveGraphs == 0)
925 MP.Bootstrap->CV.notify_all();
926 return Error::success();
927}
928
929Error MachOPlatform::MachOPlatformPlugin::associateJITDylibHeaderSymbol(
931 auto I = llvm::find_if(G.defined_symbols(), [this](jitlink::Symbol *Sym) {
932 return Sym->getName() == MP.MachOHeaderStartSymbol;
933 });
934 assert(I != G.defined_symbols().end() && "Missing MachO header start symbol");
935
936 auto &JD = MR.getTargetJITDylib();
937 std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
938 auto HeaderAddr = (*I)->getAddress();
939 MP.JITDylibToHeaderAddr[&JD] = HeaderAddr;
940 MP.HeaderAddrToJITDylib[HeaderAddr] = &JD;
941 // We can unconditionally add these actions to the Graph because this pass
942 // isn't used during bootstrap.
943 G.allocActions().push_back(
944 {cantFail(
946 MP.RegisterJITDylib.Addr, JD.getName(), HeaderAddr)),
948 MP.DeregisterJITDylib.Addr, HeaderAddr))});
949 return Error::success();
950}
951
952Error MachOPlatform::MachOPlatformPlugin::preserveImportantSections(
954 // __objc_imageinfo is "important": we want to preserve it and record its
955 // address in the first graph that it appears in, then verify and discard it
956 // in all subsequent graphs. In this pass we preserve unconditionally -- we'll
957 // manually throw it away in the processObjCImageInfo pass.
958 if (auto *ObjCImageInfoSec =
959 G.findSectionByName(MachOObjCImageInfoSectionName)) {
960 if (ObjCImageInfoSec->blocks_size() != 1)
961 return make_error<StringError>(
962 "In " + G.getName() +
963 "__DATA,__objc_imageinfo contains multiple blocks",
965 G.addAnonymousSymbol(**ObjCImageInfoSec->blocks().begin(), 0, 0, false,
966 true);
967
968 for (auto *B : ObjCImageInfoSec->blocks())
969 if (!B->edges_empty())
970 return make_error<StringError>("In " + G.getName() + ", " +
972 " contains references to symbols",
974 }
975
976 // Init sections are important: We need to preserve them and so that their
977 // addresses can be captured and reported to the ORC runtime in
978 // registerObjectPlatformSections.
979 if (const auto &InitSymName = MR.getInitializerSymbol()) {
980
981 jitlink::Symbol *InitSym = nullptr;
982 for (auto &InitSectionName : MachOInitSectionNames) {
983 // Skip ObjCImageInfo -- this shouldn't have any dependencies, and we may
984 // remove it later.
985 if (InitSectionName == MachOObjCImageInfoSectionName)
986 continue;
987
988 // Skip non-init sections.
989 auto *InitSection = G.findSectionByName(InitSectionName);
990 if (!InitSection || InitSection->empty())
991 continue;
992
993 // Create the init symbol if it has not been created already and attach it
994 // to the first block.
995 if (!InitSym) {
996 auto &B = **InitSection->blocks().begin();
997 InitSym = &G.addDefinedSymbol(
998 B, 0, *InitSymName, B.getSize(), jitlink::Linkage::Strong,
1000 }
1001
1002 // Add keep-alive edges to anonymous symbols in all other init blocks.
1003 for (auto *B : InitSection->blocks()) {
1004 if (B == &InitSym->getBlock())
1005 continue;
1006
1007 auto &S = G.addAnonymousSymbol(*B, 0, B->getSize(), false, true);
1008 InitSym->getBlock().addEdge(jitlink::Edge::KeepAlive, 0, S, 0);
1009 }
1010 }
1011 }
1012
1013 return Error::success();
1014}
1015
1016Error MachOPlatform::MachOPlatformPlugin::processObjCImageInfo(
1018
1019 // If there's an ObjC imagine info then either
1020 // (1) It's the first __objc_imageinfo we've seen in this JITDylib. In
1021 // this case we name and record it.
1022 // OR
1023 // (2) We already have a recorded __objc_imageinfo for this JITDylib,
1024 // in which case we just verify it.
1025 auto *ObjCImageInfo = G.findSectionByName(MachOObjCImageInfoSectionName);
1026 if (!ObjCImageInfo)
1027 return Error::success();
1028
1029 auto ObjCImageInfoBlocks = ObjCImageInfo->blocks();
1030
1031 // Check that the section is not empty if present.
1032 if (ObjCImageInfoBlocks.empty())
1033 return make_error<StringError>("Empty " + MachOObjCImageInfoSectionName +
1034 " section in " + G.getName(),
1036
1037 // Check that there's only one block in the section.
1038 if (std::next(ObjCImageInfoBlocks.begin()) != ObjCImageInfoBlocks.end())
1039 return make_error<StringError>("Multiple blocks in " +
1041 " section in " + G.getName(),
1043
1044 // Check that the __objc_imageinfo section is unreferenced.
1045 // FIXME: We could optimize this check if Symbols had a ref-count.
1046 for (auto &Sec : G.sections()) {
1047 if (&Sec != ObjCImageInfo)
1048 for (auto *B : Sec.blocks())
1049 for (auto &E : B->edges())
1050 if (E.getTarget().isDefined() &&
1051 &E.getTarget().getBlock().getSection() == ObjCImageInfo)
1052 return make_error<StringError>(MachOObjCImageInfoSectionName +
1053 " is referenced within file " +
1054 G.getName(),
1056 }
1057
1058 auto &ObjCImageInfoBlock = **ObjCImageInfoBlocks.begin();
1059 auto *ObjCImageInfoData = ObjCImageInfoBlock.getContent().data();
1060 auto Version = support::endian::read32(ObjCImageInfoData, G.getEndianness());
1061 auto Flags =
1062 support::endian::read32(ObjCImageInfoData + 4, G.getEndianness());
1063
1064 // Lock the mutex while we verify / update the ObjCImageInfos map.
1065 std::lock_guard<std::mutex> Lock(PluginMutex);
1066
1067 auto ObjCImageInfoItr = ObjCImageInfos.find(&MR.getTargetJITDylib());
1068 if (ObjCImageInfoItr != ObjCImageInfos.end()) {
1069 // We've already registered an __objc_imageinfo section. Verify the
1070 // content of this new section matches, then delete it.
1071 if (ObjCImageInfoItr->second.Version != Version)
1072 return make_error<StringError>(
1073 "ObjC version in " + G.getName() +
1074 " does not match first registered version",
1076 if (ObjCImageInfoItr->second.Flags != Flags)
1077 if (Error E = mergeImageInfoFlags(G, MR, ObjCImageInfoItr->second, Flags))
1078 return E;
1079
1080 // __objc_imageinfo is valid. Delete the block.
1081 for (auto *S : ObjCImageInfo->symbols())
1082 G.removeDefinedSymbol(*S);
1083 G.removeBlock(ObjCImageInfoBlock);
1084 } else {
1085 LLVM_DEBUG({
1086 dbgs() << "MachOPlatform: Registered __objc_imageinfo for "
1087 << MR.getTargetJITDylib().getName() << " in " << G.getName()
1088 << "; flags = " << formatv("{0:x4}", Flags) << "\n";
1089 });
1090 // We haven't registered an __objc_imageinfo section yet. Register and
1091 // move on. The section should already be marked no-dead-strip.
1092 G.addDefinedSymbol(ObjCImageInfoBlock, 0, ObjCImageInfoSymbolName,
1093 ObjCImageInfoBlock.getSize(), jitlink::Linkage::Strong,
1094 jitlink::Scope::Hidden, false, true);
1095 if (auto Err = MR.defineMaterializing(
1096 {{MR.getExecutionSession().intern(ObjCImageInfoSymbolName),
1097 JITSymbolFlags()}}))
1098 return Err;
1099 ObjCImageInfos[&MR.getTargetJITDylib()] = {Version, Flags, false};
1100 }
1101
1102 return Error::success();
1103}
1104
1105Error MachOPlatform::MachOPlatformPlugin::mergeImageInfoFlags(
1107 ObjCImageInfo &Info, uint32_t NewFlags) {
1108 if (Info.Flags == NewFlags)
1109 return Error::success();
1110
1111 ObjCImageInfoFlags Old(Info.Flags);
1112 ObjCImageInfoFlags New(NewFlags);
1113
1114 // Check for incompatible flags.
1115 if (Old.SwiftABIVersion && New.SwiftABIVersion &&
1116 Old.SwiftABIVersion != New.SwiftABIVersion)
1117 return make_error<StringError>("Swift ABI version in " + G.getName() +
1118 " does not match first registered flags",
1120
1121 // HasCategoryClassProperties and HasSignedObjCClassROs can be disabled before
1122 // they are registered, if necessary, but once they are in use must be
1123 // supported by subsequent objects.
1124 if (Info.Finalized && Old.HasCategoryClassProperties &&
1125 !New.HasCategoryClassProperties)
1126 return make_error<StringError>("ObjC category class property support in " +
1127 G.getName() +
1128 " does not match first registered flags",
1130 if (Info.Finalized && Old.HasSignedObjCClassROs && !New.HasSignedObjCClassROs)
1131 return make_error<StringError>("ObjC class_ro_t pointer signing in " +
1132 G.getName() +
1133 " does not match first registered flags",
1135
1136 // If we cannot change the flags, ignore any remaining differences. Adding
1137 // Swift or changing its version are unlikely to cause problems in practice.
1138 if (Info.Finalized)
1139 return Error::success();
1140
1141 // Use the minimum Swift version.
1142 if (Old.SwiftVersion && New.SwiftVersion)
1143 New.SwiftVersion = std::min(Old.SwiftVersion, New.SwiftVersion);
1144 else if (Old.SwiftVersion)
1145 New.SwiftVersion = Old.SwiftVersion;
1146 // Add a Swift ABI version if it was pure objc before.
1147 if (!New.SwiftABIVersion)
1148 New.SwiftABIVersion = Old.SwiftABIVersion;
1149 // Disable class properties if any object does not support it.
1150 if (Old.HasCategoryClassProperties != New.HasCategoryClassProperties)
1151 New.HasCategoryClassProperties = false;
1152 // Disable signed class ro data if any object does not support it.
1153 if (Old.HasSignedObjCClassROs != New.HasSignedObjCClassROs)
1154 New.HasSignedObjCClassROs = false;
1155
1156 LLVM_DEBUG({
1157 dbgs() << "MachOPlatform: Merging __objc_imageinfo flags for "
1158 << MR.getTargetJITDylib().getName() << " (was "
1159 << formatv("{0:x4}", Old.rawFlags()) << ")"
1160 << " with " << G.getName() << " (" << formatv("{0:x4}", NewFlags)
1161 << ")"
1162 << " -> " << formatv("{0:x4}", New.rawFlags()) << "\n";
1163 });
1164
1165 Info.Flags = New.rawFlags();
1166 return Error::success();
1167}
1168
1169Error MachOPlatform::MachOPlatformPlugin::fixTLVSectionsAndEdges(
1171 auto TLVBootStrapSymbolName = G.intern("__tlv_bootstrap");
1172 // Rename external references to __tlv_bootstrap to ___orc_rt_tlv_get_addr.
1173 for (auto *Sym : G.external_symbols())
1174 if (Sym->getName() == TLVBootStrapSymbolName) {
1175 auto TLSGetADDR =
1176 MP.getExecutionSession().intern("___orc_rt_macho_tlv_get_addr");
1177 Sym->setName(std::move(TLSGetADDR));
1178 break;
1179 }
1180
1181 // Store key in __thread_vars struct fields.
1182 if (auto *ThreadDataSec = G.findSectionByName(MachOThreadVarsSectionName)) {
1183 std::optional<uint64_t> Key;
1184 {
1185 std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
1186 auto I = MP.JITDylibToPThreadKey.find(&JD);
1187 if (I != MP.JITDylibToPThreadKey.end())
1188 Key = I->second;
1189 }
1190
1191 if (!Key) {
1192 if (auto KeyOrErr = MP.createPThreadKey())
1193 Key = *KeyOrErr;
1194 else
1195 return KeyOrErr.takeError();
1196 }
1197
1198 uint64_t PlatformKeyBits =
1199 support::endian::byte_swap(*Key, G.getEndianness());
1200
1201 for (auto *B : ThreadDataSec->blocks()) {
1202 if (B->getSize() != 3 * G.getPointerSize())
1203 return make_error<StringError>("__thread_vars block at " +
1204 formatv("{0:x}", B->getAddress()) +
1205 " has unexpected size",
1207
1208 auto NewBlockContent = G.allocateBuffer(B->getSize());
1209 llvm::copy(B->getContent(), NewBlockContent.data());
1210 memcpy(NewBlockContent.data() + G.getPointerSize(), &PlatformKeyBits,
1211 G.getPointerSize());
1212 B->setContent(NewBlockContent);
1213 }
1214 }
1215
1216 // Transform any TLV edges into GOT edges.
1217 for (auto *B : G.blocks())
1218 for (auto &E : B->edges())
1219 if (E.getKind() ==
1221 E.setKind(jitlink::x86_64::
1222 RequestGOTAndTransformToPCRel32GOTLoadREXRelaxable);
1223
1224 return Error::success();
1225}
1226
1227std::optional<MachOPlatform::MachOPlatformPlugin::UnwindSections>
1228MachOPlatform::MachOPlatformPlugin::findUnwindSectionInfo(
1230 using namespace jitlink;
1231
1232 UnwindSections US;
1233
1234 // ScanSection records a section range and adds any executable blocks that
1235 // that section points to to the CodeBlocks vector.
1236 SmallVector<Block *> CodeBlocks;
1237 auto ScanUnwindInfoSection = [&](Section &Sec, ExecutorAddrRange &SecRange) {
1238 if (Sec.blocks().empty())
1239 return;
1240 SecRange = (*Sec.blocks().begin())->getRange();
1241 for (auto *B : Sec.blocks()) {
1242 auto R = B->getRange();
1243 SecRange.Start = std::min(SecRange.Start, R.Start);
1244 SecRange.End = std::max(SecRange.End, R.End);
1245 for (auto &E : B->edges()) {
1246 if (!E.getTarget().isDefined())
1247 continue;
1248 auto &TargetBlock = E.getTarget().getBlock();
1249 auto &TargetSection = TargetBlock.getSection();
1250 if ((TargetSection.getMemProt() & MemProt::Exec) == MemProt::Exec)
1251 CodeBlocks.push_back(&TargetBlock);
1252 }
1253 }
1254 };
1255
1256 if (Section *EHFrameSec = G.findSectionByName(MachOEHFrameSectionName))
1257 ScanUnwindInfoSection(*EHFrameSec, US.DwarfSection);
1258
1259 if (Section *CUInfoSec =
1260 G.findSectionByName(MachOCompactUnwindInfoSectionName))
1261 ScanUnwindInfoSection(*CUInfoSec, US.CompactUnwindSection);
1262
1263 // If we didn't find any pointed-to code-blocks then there's no need to
1264 // register any info.
1265 if (CodeBlocks.empty())
1266 return std::nullopt;
1267
1268 // We have info to register. Sort the code blocks into address order and
1269 // build a list of contiguous address ranges covering them all.
1270 llvm::sort(CodeBlocks, [](const Block *LHS, const Block *RHS) {
1271 return LHS->getAddress() < RHS->getAddress();
1272 });
1273 for (auto *B : CodeBlocks) {
1274 if (US.CodeRanges.empty() || US.CodeRanges.back().End != B->getAddress())
1275 US.CodeRanges.push_back(B->getRange());
1276 else
1277 US.CodeRanges.back().End = B->getRange().End;
1278 }
1279
1280 LLVM_DEBUG({
1281 dbgs() << "MachOPlatform identified unwind info in " << G.getName() << ":\n"
1282 << " DWARF: ";
1283 if (US.DwarfSection.Start)
1284 dbgs() << US.DwarfSection << "\n";
1285 else
1286 dbgs() << "none\n";
1287 dbgs() << " Compact-unwind: ";
1288 if (US.CompactUnwindSection.Start)
1289 dbgs() << US.CompactUnwindSection << "\n";
1290 else
1291 dbgs() << "none\n"
1292 << "for code ranges:\n";
1293 for (auto &CR : US.CodeRanges)
1294 dbgs() << " " << CR << "\n";
1295 if (US.CodeRanges.size() >= G.sections_size())
1296 dbgs() << "WARNING: High number of discontiguous code ranges! "
1297 "Padding may be interfering with coalescing.\n";
1298 });
1299
1300 return US;
1301}
1302
1303Error MachOPlatform::MachOPlatformPlugin::registerObjectPlatformSections(
1304 jitlink::LinkGraph &G, JITDylib &JD, bool InBootstrapPhase) {
1305
1306 // Get a pointer to the thread data section if there is one. It will be used
1307 // below.
1308 jitlink::Section *ThreadDataSection =
1309 G.findSectionByName(MachOThreadDataSectionName);
1310
1311 // Handle thread BSS section if there is one.
1312 if (auto *ThreadBSSSection = G.findSectionByName(MachOThreadBSSSectionName)) {
1313 // If there's already a thread data section in this graph then merge the
1314 // thread BSS section content into it, otherwise just treat the thread
1315 // BSS section as the thread data section.
1316 if (ThreadDataSection)
1317 G.mergeSections(*ThreadDataSection, *ThreadBSSSection);
1318 else
1319 ThreadDataSection = ThreadBSSSection;
1320 }
1321
1323
1324 // Collect data sections to register.
1325 StringRef DataSections[] = {MachODataDataSectionName,
1328 for (auto &SecName : DataSections) {
1329 if (auto *Sec = G.findSectionByName(SecName)) {
1331 if (!R.empty())
1332 MachOPlatformSecs.push_back({SecName, R.getRange()});
1333 }
1334 }
1335
1336 // Having merged thread BSS (if present) and thread data (if present),
1337 // record the resulting section range.
1338 if (ThreadDataSection) {
1339 jitlink::SectionRange R(*ThreadDataSection);
1340 if (!R.empty())
1341 MachOPlatformSecs.push_back({MachOThreadDataSectionName, R.getRange()});
1342 }
1343
1344 // If any platform sections were found then add an allocation action to call
1345 // the registration function.
1346 StringRef PlatformSections[] = {MachOModInitFuncSectionName,
1347 ObjCRuntimeObjectSectionName};
1348
1349 for (auto &SecName : PlatformSections) {
1350 auto *Sec = G.findSectionByName(SecName);
1351 if (!Sec)
1352 continue;
1354 if (R.empty())
1355 continue;
1356
1357 MachOPlatformSecs.push_back({SecName, R.getRange()});
1358 }
1359
1360 std::optional<std::tuple<SmallVector<ExecutorAddrRange>, ExecutorAddrRange,
1362 UnwindInfo;
1363 if (auto UI = findUnwindSectionInfo(G))
1364 UnwindInfo = std::make_tuple(std::move(UI->CodeRanges), UI->DwarfSection,
1365 UI->CompactUnwindSection);
1366
1367 if (!MachOPlatformSecs.empty() || UnwindInfo) {
1368 // Dump the scraped inits.
1369 LLVM_DEBUG({
1370 dbgs() << "MachOPlatform: Scraped " << G.getName() << " init sections:\n";
1371 for (auto &KV : MachOPlatformSecs)
1372 dbgs() << " " << KV.first << ": " << KV.second << "\n";
1373 });
1374
1375 using SPSRegisterObjectPlatformSectionsArgs = SPSArgList<
1380
1381 ExecutorAddr HeaderAddr;
1382 {
1383 std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
1384 auto I = MP.JITDylibToHeaderAddr.find(&JD);
1385 assert(I != MP.JITDylibToHeaderAddr.end() &&
1386 "No header registered for JD");
1387 assert(I->second && "Null header registered for JD");
1388 HeaderAddr = I->second;
1389 }
1390
1392 cantFail(
1393 WrapperFunctionCall::Create<SPSRegisterObjectPlatformSectionsArgs>(
1394 MP.RegisterObjectPlatformSections.Addr, HeaderAddr, UnwindInfo,
1395 MachOPlatformSecs)),
1396 cantFail(
1397 WrapperFunctionCall::Create<SPSRegisterObjectPlatformSectionsArgs>(
1398 MP.DeregisterObjectPlatformSections.Addr, HeaderAddr,
1399 UnwindInfo, MachOPlatformSecs))};
1400
1401 if (LLVM_LIKELY(!InBootstrapPhase))
1402 G.allocActions().push_back(std::move(AllocActions));
1403 else {
1404 std::lock_guard<std::mutex> Lock(MP.Bootstrap->Mutex);
1405 MP.Bootstrap->DeferredAAs.push_back(std::move(AllocActions));
1406 }
1407 }
1408
1409 return Error::success();
1410}
1411
1412Error MachOPlatform::MachOPlatformPlugin::createObjCRuntimeObject(
1414
1415 bool NeedTextSegment = false;
1416 size_t NumRuntimeSections = 0;
1417
1418 for (auto ObjCRuntimeSectionName : ObjCRuntimeObjectSectionsData)
1419 if (G.findSectionByName(ObjCRuntimeSectionName))
1420 ++NumRuntimeSections;
1421
1422 for (auto ObjCRuntimeSectionName : ObjCRuntimeObjectSectionsText) {
1423 if (G.findSectionByName(ObjCRuntimeSectionName)) {
1424 ++NumRuntimeSections;
1425 NeedTextSegment = true;
1426 }
1427 }
1428
1429 // Early out for no runtime sections.
1430 if (NumRuntimeSections == 0)
1431 return Error::success();
1432
1433 // If there were any runtime sections then we need to add an __objc_imageinfo
1434 // section.
1435 ++NumRuntimeSections;
1436
1437 size_t MachOSize = sizeof(MachO::mach_header_64) +
1438 (NeedTextSegment + 1) * sizeof(MachO::segment_command_64) +
1439 NumRuntimeSections * sizeof(MachO::section_64);
1440
1441 auto &Sec = G.createSection(ObjCRuntimeObjectSectionName,
1442 MemProt::Read | MemProt::Write);
1443 G.createMutableContentBlock(Sec, MachOSize, ExecutorAddr(), 16, 0, true);
1444
1445 return Error::success();
1446}
1447
1448Error MachOPlatform::MachOPlatformPlugin::populateObjCRuntimeObject(
1450
1451 auto *ObjCRuntimeObjectSec =
1452 G.findSectionByName(ObjCRuntimeObjectSectionName);
1453
1454 if (!ObjCRuntimeObjectSec)
1455 return Error::success();
1456
1457 switch (G.getTargetTriple().getArch()) {
1458 case Triple::aarch64:
1459 case Triple::x86_64:
1460 // Supported.
1461 break;
1462 default:
1463 return make_error<StringError>("Unrecognized MachO arch in triple " +
1464 G.getTargetTriple().str(),
1466 }
1467
1468 auto &SecBlock = **ObjCRuntimeObjectSec->blocks().begin();
1469
1470 struct SecDesc {
1472 unique_function<void(size_t RecordOffset)> AddFixups;
1473 };
1474
1475 std::vector<SecDesc> TextSections, DataSections;
1476 auto AddSection = [&](SecDesc &SD, jitlink::Section &GraphSec) {
1477 jitlink::SectionRange SR(GraphSec);
1478 StringRef FQName = GraphSec.getName();
1479 memset(&SD.Sec, 0, sizeof(MachO::section_64));
1480 memcpy(SD.Sec.sectname, FQName.drop_front(7).data(), FQName.size() - 7);
1481 memcpy(SD.Sec.segname, FQName.data(), 6);
1482 SD.Sec.addr = SR.getStart() - SecBlock.getAddress();
1483 SD.Sec.size = SR.getSize();
1484 SD.Sec.flags = MachO::S_REGULAR;
1485 };
1486
1487 // Add the __objc_imageinfo section.
1488 {
1489 DataSections.push_back({});
1490 auto &SD = DataSections.back();
1491 memset(&SD.Sec, 0, sizeof(SD.Sec));
1492 memcpy(SD.Sec.sectname, "__objc_imageinfo", 16);
1493 strcpy(SD.Sec.segname, "__DATA");
1494 SD.Sec.size = 8;
1495 SD.AddFixups = [&](size_t RecordOffset) {
1496 auto PointerEdge = getPointerEdgeKind(G);
1497
1498 // Look for an existing __objc_imageinfo symbol.
1499 jitlink::Symbol *ObjCImageInfoSym = nullptr;
1500 for (auto *Sym : G.external_symbols())
1501 if (Sym->hasName() && *Sym->getName() == ObjCImageInfoSymbolName) {
1502 ObjCImageInfoSym = Sym;
1503 break;
1504 }
1505 if (!ObjCImageInfoSym)
1506 for (auto *Sym : G.absolute_symbols())
1507 if (Sym->hasName() && *Sym->getName() == ObjCImageInfoSymbolName) {
1508 ObjCImageInfoSym = Sym;
1509 break;
1510 }
1511 if (!ObjCImageInfoSym)
1512 for (auto *Sym : G.defined_symbols())
1513 if (Sym->hasName() && *Sym->getName() == ObjCImageInfoSymbolName) {
1514 ObjCImageInfoSym = Sym;
1515 std::optional<uint32_t> Flags;
1516 {
1517 std::lock_guard<std::mutex> Lock(PluginMutex);
1518 auto It = ObjCImageInfos.find(&MR.getTargetJITDylib());
1519 if (It != ObjCImageInfos.end()) {
1520 It->second.Finalized = true;
1521 Flags = It->second.Flags;
1522 }
1523 }
1524
1525 if (Flags) {
1526 // We own the definition of __objc_image_info; write the final
1527 // merged flags value.
1528 auto Content = Sym->getBlock().getMutableContent(G);
1529 assert(Content.size() == 8 &&
1530 "__objc_image_info size should have been verified already");
1531 support::endian::write32(&Content[4], *Flags, G.getEndianness());
1532 }
1533 break;
1534 }
1535 if (!ObjCImageInfoSym)
1536 ObjCImageInfoSym =
1537 &G.addExternalSymbol(ObjCImageInfoSymbolName, 8, false);
1538
1539 SecBlock.addEdge(PointerEdge,
1540 RecordOffset + ((char *)&SD.Sec.addr - (char *)&SD.Sec),
1541 *ObjCImageInfoSym, -SecBlock.getAddress().getValue());
1542 };
1543 }
1544
1545 for (auto ObjCRuntimeSectionName : ObjCRuntimeObjectSectionsData) {
1546 if (auto *GraphSec = G.findSectionByName(ObjCRuntimeSectionName)) {
1547 DataSections.push_back({});
1548 AddSection(DataSections.back(), *GraphSec);
1549 }
1550 }
1551
1552 for (auto ObjCRuntimeSectionName : ObjCRuntimeObjectSectionsText) {
1553 if (auto *GraphSec = G.findSectionByName(ObjCRuntimeSectionName)) {
1554 TextSections.push_back({});
1555 AddSection(TextSections.back(), *GraphSec);
1556 }
1557 }
1558
1559 assert(ObjCRuntimeObjectSec->blocks_size() == 1 &&
1560 "Unexpected number of blocks in runtime sections object");
1561
1562 // Build the header struct up-front. This also gives us a chance to check
1563 // that the triple is supported, which we'll assume below.
1566 switch (G.getTargetTriple().getArch()) {
1567 case Triple::aarch64:
1570 break;
1571 case Triple::x86_64:
1574 break;
1575 default:
1576 llvm_unreachable("Unsupported architecture");
1577 }
1578
1580 Hdr.ncmds = 1 + !TextSections.empty();
1581 Hdr.sizeofcmds =
1582 Hdr.ncmds * sizeof(MachO::segment_command_64) +
1583 (TextSections.size() + DataSections.size()) * sizeof(MachO::section_64);
1584 Hdr.flags = 0;
1585 Hdr.reserved = 0;
1586
1587 auto SecContent = SecBlock.getAlreadyMutableContent();
1588 char *P = SecContent.data();
1589 auto WriteMachOStruct = [&](auto S) {
1590 if (G.getEndianness() != llvm::endianness::native)
1592 memcpy(P, &S, sizeof(S));
1593 P += sizeof(S);
1594 };
1595
1596 auto WriteSegment = [&](StringRef Name, std::vector<SecDesc> &Secs) {
1598 memset(&SegLC, 0, sizeof(SegLC));
1599 memcpy(SegLC.segname, Name.data(), Name.size());
1600 SegLC.cmd = MachO::LC_SEGMENT_64;
1601 SegLC.cmdsize = sizeof(MachO::segment_command_64) +
1602 Secs.size() * sizeof(MachO::section_64);
1603 SegLC.nsects = Secs.size();
1604 WriteMachOStruct(SegLC);
1605 for (auto &SD : Secs) {
1606 if (SD.AddFixups)
1607 SD.AddFixups(P - SecContent.data());
1608 WriteMachOStruct(SD.Sec);
1609 }
1610 };
1611
1612 WriteMachOStruct(Hdr);
1613 if (!TextSections.empty())
1614 WriteSegment("__TEXT", TextSections);
1615 if (!DataSections.empty())
1616 WriteSegment("__DATA", DataSections);
1617
1618 assert(P == SecContent.end() && "Underflow writing ObjC runtime object");
1619 return Error::success();
1620}
1621
1622Error MachOPlatform::MachOPlatformPlugin::prepareSymbolTableRegistration(
1623 jitlink::LinkGraph &G, JITSymTabVector &JITSymTabInfo) {
1624
1625 auto *CStringSec = G.findSectionByName(MachOCStringSectionName);
1626 if (!CStringSec)
1627 CStringSec = &G.createSection(MachOCStringSectionName,
1628 MemProt::Read | MemProt::Exec);
1629
1630 // Make a map of existing strings so that we can re-use them:
1632 for (auto *Sym : CStringSec->symbols()) {
1633
1634 // The LinkGraph builder should have created single strings blocks, and all
1635 // plugins should have maintained this invariant.
1636 auto Content = Sym->getBlock().getContent();
1637 ExistingStrings.insert(
1638 std::make_pair(StringRef(Content.data(), Content.size()), Sym));
1639 }
1640
1641 // Add all symbol names to the string section, and record the symbols for
1642 // those names.
1643 {
1644 SmallVector<jitlink::Symbol *> SymsToProcess;
1645 for (auto *Sym : G.defined_symbols())
1646 SymsToProcess.push_back(Sym);
1647 for (auto *Sym : G.absolute_symbols())
1648 SymsToProcess.push_back(Sym);
1649
1650 for (auto *Sym : SymsToProcess) {
1651 if (!Sym->hasName())
1652 continue;
1653
1654 auto I = ExistingStrings.find(*Sym->getName());
1655 if (I == ExistingStrings.end()) {
1656 auto &NameBlock = G.createMutableContentBlock(
1657 *CStringSec, G.allocateCString(*Sym->getName()),
1658 orc::ExecutorAddr(), 1, 0);
1659 auto &SymbolNameSym = G.addAnonymousSymbol(
1660 NameBlock, 0, NameBlock.getSize(), false, true);
1661 JITSymTabInfo.push_back({Sym, &SymbolNameSym});
1662 } else
1663 JITSymTabInfo.push_back({Sym, I->second});
1664 }
1665 }
1666
1667 return Error::success();
1668}
1669
1670Error MachOPlatform::MachOPlatformPlugin::addSymbolTableRegistration(
1672 JITSymTabVector &JITSymTabInfo, bool InBootstrapPhase) {
1673
1674 ExecutorAddr HeaderAddr;
1675 {
1676 std::lock_guard<std::mutex> Lock(MP.PlatformMutex);
1677 auto I = MP.JITDylibToHeaderAddr.find(&MR.getTargetJITDylib());
1678 assert(I != MP.JITDylibToHeaderAddr.end() && "No header registered for JD");
1679 assert(I->second && "Null header registered for JD");
1680 HeaderAddr = I->second;
1681 }
1682
1683 if (LLVM_UNLIKELY(InBootstrapPhase)) {
1684 // If we're in the bootstrap phase then just record these symbols in the
1685 // bootstrap object and then bail out -- registration will be attached to
1686 // the bootstrap graph.
1687 std::lock_guard<std::mutex> Lock(MP.Bootstrap->Mutex);
1688 auto &SymTab = MP.Bootstrap->SymTab;
1689 for (auto &[OriginalSymbol, NameSym] : JITSymTabInfo)
1690 SymTab.push_back({NameSym->getAddress(), OriginalSymbol->getAddress(),
1691 flagsForSymbol(*OriginalSymbol)});
1692 return Error::success();
1693 }
1694
1695 SymbolTableVector SymTab;
1696 for (auto &[OriginalSymbol, NameSym] : JITSymTabInfo)
1697 SymTab.push_back({NameSym->getAddress(), OriginalSymbol->getAddress(),
1698 flagsForSymbol(*OriginalSymbol)});
1699
1700 G.allocActions().push_back(
1701 {cantFail(WrapperFunctionCall::Create<SPSRegisterSymbolsArgs>(
1702 MP.RegisterObjectSymbolTable.Addr, HeaderAddr, SymTab)),
1703 cantFail(WrapperFunctionCall::Create<SPSRegisterSymbolsArgs>(
1704 MP.DeregisterObjectSymbolTable.Addr, HeaderAddr, SymTab))});
1705
1706 return Error::success();
1707}
1708
1709template <typename MachOTraits>
1711 const MachOPlatform::HeaderOptions &Opts,
1713 jitlink::Section &HeaderSection) {
1714 auto HdrInfo =
1716 MachOBuilder<MachOTraits> B(HdrInfo.PageSize);
1717
1718 B.Header.filetype = MachO::MH_DYLIB;
1719 B.Header.cputype = HdrInfo.CPUType;
1720 B.Header.cpusubtype = HdrInfo.CPUSubType;
1721
1722 if (Opts.IDDylib)
1723 B.template addLoadCommand<MachO::LC_ID_DYLIB>(
1724 Opts.IDDylib->Name, Opts.IDDylib->Timestamp,
1725 Opts.IDDylib->CurrentVersion, Opts.IDDylib->CompatibilityVersion);
1726 else
1727 B.template addLoadCommand<MachO::LC_ID_DYLIB>(JD.getName(), 0, 0, 0);
1728
1729 for (auto &BV : Opts.BuildVersions)
1730 B.template addLoadCommand<MachO::LC_BUILD_VERSION>(
1731 BV.Platform, BV.MinOS, BV.SDK, static_cast<uint32_t>(0));
1732 for (auto &D : Opts.LoadDylibs)
1733 B.template addLoadCommand<MachO::LC_LOAD_DYLIB>(
1734 D.Name, D.Timestamp, D.CurrentVersion, D.CompatibilityVersion);
1735 for (auto &P : Opts.RPaths)
1736 B.template addLoadCommand<MachO::LC_RPATH>(P);
1737
1738 auto HeaderContent = G.allocateBuffer(B.layout());
1739 B.write(HeaderContent);
1740
1741 return G.createContentBlock(HeaderSection, HeaderContent, ExecutorAddr(), 8,
1742 0);
1743}
1744
1746 SymbolStringPtr HeaderStartSymbol,
1749 createHeaderInterface(MOP, std::move(HeaderStartSymbol))),
1750 MOP(MOP), Opts(std::move(Opts)) {}
1751
1753 std::unique_ptr<MaterializationResponsibility> R) {
1754 auto G = createPlatformGraph(MOP, "<MachOHeaderMU>");
1755 addMachOHeader(R->getTargetJITDylib(), *G, R->getInitializerSymbol());
1756 MOP.getObjectLinkingLayer().emit(std::move(R), std::move(G));
1757}
1758
1760 const SymbolStringPtr &Sym) {}
1761
1762void SimpleMachOHeaderMU::addMachOHeader(
1764 const SymbolStringPtr &InitializerSymbol) {
1765 auto &HeaderSection = G.createSection("__header", MemProt::Read);
1766 auto &HeaderBlock = createHeaderBlock(JD, G, HeaderSection);
1767
1768 // Init symbol is header-start symbol.
1769 G.addDefinedSymbol(HeaderBlock, 0, *InitializerSymbol, HeaderBlock.getSize(),
1771 true);
1772 for (auto &HS : AdditionalHeaderSymbols)
1773 G.addDefinedSymbol(HeaderBlock, HS.Offset, HS.Name, HeaderBlock.getSize(),
1775 true);
1776}
1777
1780 jitlink::Section &HeaderSection) {
1782 case Triple::aarch64:
1783 case Triple::x86_64:
1784 return ::createHeaderBlock<MachO64LE>(MOP, Opts, JD, G, HeaderSection);
1785 default:
1786 llvm_unreachable("Unsupported architecture");
1787 }
1788}
1789
1790MaterializationUnit::Interface SimpleMachOHeaderMU::createHeaderInterface(
1791 MachOPlatform &MOP, const SymbolStringPtr &HeaderStartSymbol) {
1792 SymbolFlagsMap HeaderSymbolFlags;
1793
1794 HeaderSymbolFlags[HeaderStartSymbol] = JITSymbolFlags::Exported;
1795 for (auto &HS : AdditionalHeaderSymbols)
1796 HeaderSymbolFlags[MOP.getExecutionSession().intern(HS.Name)] =
1798
1799 return MaterializationUnit::Interface(std::move(HeaderSymbolFlags),
1800 HeaderStartSymbol);
1801}
1802
1804 switch (TT.getArch()) {
1805 case Triple::aarch64:
1806 return {/* PageSize = */ 16 * 1024,
1807 /* CPUType = */ MachO::CPU_TYPE_ARM64,
1808 /* CPUSubType = */ MachO::CPU_SUBTYPE_ARM64_ALL};
1809 case Triple::x86_64:
1810 return {/* PageSize = */ 4 * 1024,
1811 /* CPUType = */ MachO::CPU_TYPE_X86_64,
1812 /* CPUSubType = */ MachO::CPU_SUBTYPE_X86_64_ALL};
1813 default:
1814 llvm_unreachable("Unrecognized architecture");
1815 }
1816}
1817
1818} // End namespace orc.
1819} // End namespace llvm.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:320
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:319
#define LLVM_DEBUG(...)
Definition: Debug.h:106
T Content
std::string Name
RelaxConfig Config
Definition: ELF_riscv.cpp:506
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define _
static std::optional< ConstantRange > getRange(Value *V, const InstrInfoQuery &IIQ)
Helper method to get range from metadata or attribute.
#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())
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:156
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:152
iterator end()
Definition: DenseMap.h:84
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:211
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:103
Helper for Errors used as out-parameters.
Definition: Error.h:1130
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
bool empty() const
Definition: SmallVector.h:81
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:609
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:144
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:383
An ExecutionSession represents a running JIT program.
Definition: Core.h:1340
const Triple & getTargetTriple() const
Return the triple for the executor.
Definition: Core.h:1383
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition: Core.h:1394
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:1608
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:1798
Error registerJITDispatchHandlers(JITDylib &JD, JITDispatchHandlerAssociationMap WFs)
For each tag symbol name, associate the corresponding AsyncHandlerWrapperFunction with the address of...
Definition: Core.cpp:1893
decltype(auto) runSessionLocked(Func &&F)
Run the given lambda with the session mutex locked.
Definition: Core.h:1404
Represents an address in the executor process.
Represents a JIT'd dynamic library.
Definition: Core.h:897
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:1823
GeneratorT & addGenerator(std::unique_ptr< GeneratorT > DefGenerator)
Adds a definition generator to this JITDylib and returns a referenece to it.
Definition: Core.h:1806
ExecutionSession & getExecutionSession()
LinkGraphLinkingLayer & addPlugin(std::shared_ptr< Plugin > P)
Add a plugin.
Mediates between MachO initialization and ExecutionSession state.
Definition: MachOPlatform.h:30
ObjectLinkingLayer & getObjectLinkingLayer() const
Error teardownJITDylib(JITDylib &JD) override
This method will be called outside the session lock each time a JITDylib is removed to allow the Plat...
static ArrayRef< std::pair< const char *, const char * > > standardLazyCompilationAliases()
Returns a list of aliases required to enable lazy compilation via the ORC runtime.
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.
unique_function< std::unique_ptr< MaterializationUnit >(MachOPlatform &MOP, HeaderOptions Opts)> MachOHeaderMUBuilder
Used by setupJITDylib to create MachO header MaterializationUnits for JITDylibs.
Definition: MachOPlatform.h:91
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 SymbolAliasMap standardPlatformAliases(ExecutionSession &ES)
Returns an AliasMap containing the default aliases for the MachOPlatform.
ExecutionSession & getExecutionSession() const
Error notifyRemoving(ResourceTracker &RT) override
This method will be called under the ExecutionSession lock when a ResourceTracker is removed.
static Expected< std::unique_ptr< MachOPlatform > > Create(ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD, std::unique_ptr< DefinitionGenerator > OrcRuntime, HeaderOptions PlatformJDOpts={}, MachOHeaderMUBuilder BuildMachOHeaderMU=buildSimpleMachOHeaderMU, std::optional< SymbolAliasMap > RuntimeAliases=std::nullopt)
Try to create a MachOPlatform instance, adding the ORC runtime to the given JITDylib.
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:571
Error defineMaterializing(SymbolFlagsMap SymbolFlags)
Attempt to claim responsibility for new definitions.
Definition: Core.h:1953
const SymbolStringPtr & getInitializerSymbol() const
Returns the initialization pseudo-symbol, if any.
Definition: Core.h:610
JITDylib & getTargetJITDylib() const
Returns the target JITDylib that these symbols are being materialized into.
Definition: Core.h:596
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.
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).
An ObjectLayer implementation built on JITLink.
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O) override
Emit an object file.
Platforms set up standard symbols and mediate interactions between dynamic initializers (e....
Definition: Core.h:1268
API to remove / transfer ownership of JIT resources.
Definition: Core.h:77
JITDylib & getJITDylib() const
Return the JITDylib targeted by this tracker.
Definition: Core.h:92
MachOPlatform::HeaderOptions Opts
void materialize(std::unique_ptr< MaterializationResponsibility > R) override
Implementations of this method should materialize all symbols in the materialzation unit,...
virtual jitlink::Block & createHeaderBlock(JITDylib &JD, jitlink::LinkGraph &G, jitlink::Section &HeaderSection)
SimpleMachOHeaderMU(MachOPlatform &MOP, SymbolStringPtr HeaderStartSymbol, MachOPlatform::HeaderOptions Opts)
void discard(const JITDylib &JD, const SymbolStringPtr &Sym) override
Implementations of this method should discard the given symbol from the source (e....
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.
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition: Core.h:194
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 deserialize(SPSInputBuffer &IB, MachOPlatform::MachOExecutorSymbolFlags &SF)
static bool serialize(SPSOutputBuffer &OB, const MachOPlatform::MachOExecutorSymbolFlags &SF)
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.
Key
PAL metadata keys.
@ MH_MAGIC_64
Definition: MachO.h:32
@ MH_DYLIB
Definition: MachO.h:48
@ S_REGULAR
S_REGULAR - Regular section.
Definition: MachO.h:127
void swapStruct(fat_header &mh)
Definition: MachO.h:1140
@ CPU_SUBTYPE_ARM64_ALL
Definition: MachO.h:1641
@ CPU_SUBTYPE_X86_64_ALL
Definition: MachO.h:1611
@ CPU_TYPE_ARM64
Definition: MachO.h:1570
@ CPU_TYPE_X86_64
Definition: MachO.h:1566
SPSTuple< SPSExecutorAddr, SPSExecutorAddr > SPSExecutorAddrRange
std::vector< AllocActionCallPair > AllocActions
A vector of allocation actions to be run for this allocation.
StringRef MachOSwift5EntrySectionName
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:177
StringRef MachOCompactUnwindInfoSectionName
std::unique_ptr< ReExportsMaterializationUnit > symbolAliases(SymbolAliasMap Aliases)
Create a ReExportsMaterializationUnit with the given aliases.
Definition: Core.h:745
std::unique_ptr< AbsoluteSymbolsMaterializationUnit > absoluteSymbols(SymbolMap Symbols)
Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
StringRef MachOObjCProtoListSectionName
StringRef MachOSwift5ProtosSectionName
StringRef MachOEHFrameSectionName
StringRef MachOModInitFuncSectionName
StringRef MachOObjCConstSectionName
StringRef MachODataDataSectionName
StringRef MachOSwift5ProtoSectionName
static void addAliases(ExecutionSession &ES, SymbolAliasMap &Aliases, ArrayRef< std::pair< const char *, const char * > > AL)
StringRef MachOObjCCatListSectionName
StringRef MachOObjCClassRefsSectionName
StringRef MachOObjCDataSectionName
StringRef MachOObjCClassNameSectionName
StringRef MachOObjCMethNameSectionName
StringRef MachOInitSectionNames[22]
StringRef MachOObjCClassListSectionName
StringRef MachOObjCSelRefsSectionName
StringRef MachOSwift5FieldMetadataSectionName
StringRef MachOCStringSectionName
StringRef MachOObjCMethTypeSectionName
StringRef MachOSwift5TypesSectionName
StringRef MachOObjCNLCatListSectionName
jitlink::Block & createHeaderBlock(MachOPlatform &MOP, const MachOPlatform::HeaderOptions &Opts, JITDylib &JD, jitlink::LinkGraph &G, jitlink::Section &HeaderSection)
StringRef MachOObjCNLClassListSectionName
StringRef MachOObjCImageInfoSectionName
MachOHeaderInfo getMachOHeaderInfoFromTriple(const Triple &TT)
RegisterDependenciesFunction NoDependenciesToRegister
This can be used as the value for a RegisterDependenciesFunction if there are no dependants to regist...
Definition: Core.cpp:38
StringRef MachOThreadDataSectionName
StringRef MachODataCommonSectionName
StringRef MachOObjCProtoRefsSectionName
StringRef MachOSwift5TypeRefSectionName
StringRef MachOObjCCatList2SectionName
value_type byte_swap(value_type value, endianness endian)
Definition: Endian.h:44
uint32_t read32(const void *P, endianness E)
Definition: Endian.h:405
void write32(void *P, uint32_t V, endianness E)
Definition: Endian.h:448
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1664
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:756
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1841
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:1873
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:1766
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
Represents an address range in the exceutor process.
static std::optional< BuildVersionOpts > fromTriple(const Triple &TT, uint32_t MinOS, uint32_t SDK)
Configuration for the mach-o header of a JITDylib.
Definition: MachOPlatform.h:52
std::optional< Dylib > IDDylib
Override for LC_IC_DYLIB.
Definition: MachOPlatform.h:74
std::vector< std::string > RPaths
List of LC_RPATHs.
Definition: MachOPlatform.h:79
std::vector< BuildVersionOpts > BuildVersions
List of LC_BUILD_VERSIONs.
Definition: MachOPlatform.h:81
std::vector< Dylib > LoadDylibs
List of LC_LOAD_DYLIBs.
Definition: MachOPlatform.h:77
A pair of WrapperFunctionCalls, one to be run at finalization time, one to be run at deallocation tim...