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