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