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