#ifndef LLVM_EXECUTIONENGINE_ORC_MACHOPLATFORM_H
#define LLVM_EXECUTIONENGINE_ORC_MACHOPLATFORM_H
#include "llvm/ADT/StringRef.h"
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
#include <future>
#include <thread>
#include <vector>
namespace llvm {
namespace orc {
class MachOPlatform : public Platform {
public:
struct MachOJITDylibDepInfo {
bool Sealed = false;
std::vector<ExecutorAddr> DepHeaders;
};
using MachOJITDylibDepInfoMap =
std::vector<std::pair<ExecutorAddr, MachOJITDylibDepInfo>>;
static Expected<std::unique_ptr<MachOPlatform>>
Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
JITDylib &PlatformJD, const char *OrcRuntimePath,
Optional<SymbolAliasMap> RuntimeAliases = None);
ExecutionSession &getExecutionSession() const { return ES; }
ObjectLinkingLayer &getObjectLinkingLayer() const { return ObjLinkingLayer; }
Error setupJITDylib(JITDylib &JD) override;
Error teardownJITDylib(JITDylib &JD) override;
Error notifyAdding(ResourceTracker &RT,
const MaterializationUnit &MU) override;
Error notifyRemoving(ResourceTracker &RT) override;
static SymbolAliasMap standardPlatformAliases(ExecutionSession &ES);
static ArrayRef<std::pair<const char *, const char *>> requiredCXXAliases();
static ArrayRef<std::pair<const char *, const char *>>
standardRuntimeUtilityAliases();
static bool isInitializerSection(StringRef SegName, StringRef SectName);
private:
class MachOPlatformPlugin : public ObjectLinkingLayer::Plugin {
public:
MachOPlatformPlugin(MachOPlatform &MP) : MP(MP) {}
void modifyPassConfig(MaterializationResponsibility &MR,
jitlink::LinkGraph &G,
jitlink::PassConfiguration &Config) override;
SyntheticSymbolDependenciesMap
getSyntheticSymbolDependencies(MaterializationResponsibility &MR) override;
Error notifyFailed(MaterializationResponsibility &MR) override {
return Error::success();
}
Error notifyRemovingResources(ResourceKey K) override {
return Error::success();
}
void notifyTransferringResources(ResourceKey DstKey,
ResourceKey SrcKey) override {}
private:
using InitSymbolDepMap =
DenseMap<MaterializationResponsibility *, JITLinkSymbolSet>;
Error associateJITDylibHeaderSymbol(jitlink::LinkGraph &G,
MaterializationResponsibility &MR);
Error preserveInitSections(jitlink::LinkGraph &G,
MaterializationResponsibility &MR);
Error processObjCImageInfo(jitlink::LinkGraph &G,
MaterializationResponsibility &MR);
Error fixTLVSectionsAndEdges(jitlink::LinkGraph &G, JITDylib &JD);
Error registerObjectPlatformSections(jitlink::LinkGraph &G, JITDylib &JD);
Error registerEHSectionsPhase1(jitlink::LinkGraph &G);
std::mutex PluginMutex;
MachOPlatform &MP;
DenseMap<JITDylib *, std::pair<uint32_t, uint32_t>> ObjCImageInfos;
DenseMap<JITDylib *, ExecutorAddr> HeaderAddrs;
InitSymbolDepMap InitSymbolDeps;
};
using GetJITDylibHeaderSendResultFn =
unique_function<void(Expected<ExecutorAddr>)>;
using GetJITDylibNameSendResultFn =
unique_function<void(Expected<StringRef>)>;
using PushInitializersSendResultFn =
unique_function<void(Expected<MachOJITDylibDepInfoMap>)>;
using SendSymbolAddressFn = unique_function<void(Expected<ExecutorAddr>)>;
static bool supportedTarget(const Triple &TT);
MachOPlatform(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
JITDylib &PlatformJD,
std::unique_ptr<DefinitionGenerator> OrcRuntimeGenerator,
Error &Err);
Error associateRuntimeSupportFunctions(JITDylib &PlatformJD);
void pushInitializersLoop(PushInitializersSendResultFn SendResult,
JITDylibSP JD);
void rt_pushInitializers(PushInitializersSendResultFn SendResult,
ExecutorAddr JDHeaderAddr);
void rt_lookupSymbol(SendSymbolAddressFn SendResult, ExecutorAddr Handle,
StringRef SymbolName);
Error bootstrapMachORuntime(JITDylib &PlatformJD);
Expected<uint64_t> createPThreadKey();
enum PlatformState { BootstrapPhase1, BootstrapPhase2, Initialized };
ExecutionSession &ES;
ObjectLinkingLayer &ObjLinkingLayer;
SymbolStringPtr MachOHeaderStartSymbol;
std::atomic<PlatformState> State{BootstrapPhase1};
ExecutorAddr orc_rt_macho_platform_bootstrap;
ExecutorAddr orc_rt_macho_platform_shutdown;
ExecutorAddr orc_rt_macho_register_ehframe_section;
ExecutorAddr orc_rt_macho_deregister_ehframe_section;
ExecutorAddr orc_rt_macho_register_jitdylib;
ExecutorAddr orc_rt_macho_deregister_jitdylib;
ExecutorAddr orc_rt_macho_register_object_platform_sections;
ExecutorAddr orc_rt_macho_deregister_object_platform_sections;
ExecutorAddr orc_rt_macho_create_pthread_key;
DenseMap<JITDylib *, SymbolLookupSet> RegisteredInitSymbols;
std::mutex PlatformMutex;
DenseMap<JITDylib *, ExecutorAddr> JITDylibToHeaderAddr;
DenseMap<ExecutorAddr, JITDylib *> HeaderAddrToJITDylib;
DenseMap<JITDylib *, uint64_t> JITDylibToPThreadKey;
};
namespace shared {
using SPSNamedExecutorAddrRangeSequence =
SPSSequence<SPSTuple<SPSString, SPSExecutorAddrRange>>;
} } }
#endif