#ifndef LLVM_EXECUTIONENGINE_ORC_ELFNIXPLATFORM_H
#define LLVM_EXECUTIONENGINE_ORC_ELFNIXPLATFORM_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 {
struct ELFPerObjectSectionsToRegister {
ExecutorAddrRange EHFrameSection;
ExecutorAddrRange ThreadDataSection;
};
struct ELFNixJITDylibInitializers {
using SectionList = std::vector<ExecutorAddrRange>;
ELFNixJITDylibInitializers(std::string Name, ExecutorAddr DSOHandleAddress)
: Name(std::move(Name)), DSOHandleAddress(std::move(DSOHandleAddress)) {}
std::string Name;
ExecutorAddr DSOHandleAddress;
StringMap<SectionList> InitSections;
};
class ELFNixJITDylibDeinitializers {};
using ELFNixJITDylibInitializerSequence =
std::vector<ELFNixJITDylibInitializers>;
using ELFNixJITDylibDeinitializerSequence =
std::vector<ELFNixJITDylibDeinitializers>;
class ELFNixPlatform : public Platform {
public:
static Expected<std::unique_ptr<ELFNixPlatform>>
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 Expected<SymbolAliasMap> standardPlatformAliases(ExecutionSession &ES,
JITDylib &PlatformJD);
static ArrayRef<std::pair<const char *, const char *>> requiredCXXAliases();
static ArrayRef<std::pair<const char *, const char *>>
standardRuntimeUtilityAliases();
static bool isInitializerSection(StringRef SecName);
private:
class ELFNixPlatformPlugin : public ObjectLinkingLayer::Plugin {
public:
ELFNixPlatformPlugin(ELFNixPlatform &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>;
void addInitializerSupportPasses(MaterializationResponsibility &MR,
jitlink::PassConfiguration &Config);
void addDSOHandleSupportPasses(MaterializationResponsibility &MR,
jitlink::PassConfiguration &Config);
void addEHAndTLVSupportPasses(MaterializationResponsibility &MR,
jitlink::PassConfiguration &Config);
Error preserveInitSections(jitlink::LinkGraph &G,
MaterializationResponsibility &MR);
Error registerInitSections(jitlink::LinkGraph &G, JITDylib &JD);
Error fixTLVSectionsAndEdges(jitlink::LinkGraph &G, JITDylib &JD);
std::mutex PluginMutex;
ELFNixPlatform &MP;
InitSymbolDepMap InitSymbolDeps;
};
using SendInitializerSequenceFn =
unique_function<void(Expected<ELFNixJITDylibInitializerSequence>)>;
using SendDeinitializerSequenceFn =
unique_function<void(Expected<ELFNixJITDylibDeinitializerSequence>)>;
using SendSymbolAddressFn = unique_function<void(Expected<ExecutorAddr>)>;
static bool supportedTarget(const Triple &TT);
ELFNixPlatform(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
JITDylib &PlatformJD,
std::unique_ptr<DefinitionGenerator> OrcRuntimeGenerator,
Error &Err);
Error associateRuntimeSupportFunctions(JITDylib &PlatformJD);
void getInitializersBuildSequencePhase(SendInitializerSequenceFn SendResult,
JITDylib &JD,
std::vector<JITDylibSP> DFSLinkOrder);
void getInitializersLookupPhase(SendInitializerSequenceFn SendResult,
JITDylib &JD);
void rt_getInitializers(SendInitializerSequenceFn SendResult,
StringRef JDName);
void rt_getDeinitializers(SendDeinitializerSequenceFn SendResult,
ExecutorAddr Handle);
void rt_lookupSymbol(SendSymbolAddressFn SendResult, ExecutorAddr Handle,
StringRef SymbolName);
Error bootstrapELFNixRuntime(JITDylib &PlatformJD);
Error registerInitInfo(JITDylib &JD,
ArrayRef<jitlink::Section *> InitSections);
Error registerPerObjectSections(const ELFPerObjectSectionsToRegister &POSR);
Expected<uint64_t> createPThreadKey();
ExecutionSession &ES;
ObjectLinkingLayer &ObjLinkingLayer;
SymbolStringPtr DSOHandleSymbol;
std::atomic<bool> RuntimeBootstrapped{false};
ExecutorAddr orc_rt_elfnix_platform_bootstrap;
ExecutorAddr orc_rt_elfnix_platform_shutdown;
ExecutorAddr orc_rt_elfnix_register_object_sections;
ExecutorAddr orc_rt_elfnix_create_pthread_key;
DenseMap<JITDylib *, SymbolLookupSet> RegisteredInitSymbols;
std::mutex PlatformMutex;
DenseMap<JITDylib *, ELFNixJITDylibInitializers> InitSeqs;
std::vector<ELFPerObjectSectionsToRegister> BootstrapPOSRs;
DenseMap<ExecutorAddr, JITDylib *> HandleAddrToJITDylib;
DenseMap<JITDylib *, uint64_t> JITDylibToPThreadKey;
};
namespace shared {
using SPSELFPerObjectSectionsToRegister =
SPSTuple<SPSExecutorAddrRange, SPSExecutorAddrRange>;
template <>
class SPSSerializationTraits<SPSELFPerObjectSectionsToRegister,
ELFPerObjectSectionsToRegister> {
public:
static size_t size(const ELFPerObjectSectionsToRegister &MOPOSR) {
return SPSELFPerObjectSectionsToRegister::AsArgList::size(
MOPOSR.EHFrameSection, MOPOSR.ThreadDataSection);
}
static bool serialize(SPSOutputBuffer &OB,
const ELFPerObjectSectionsToRegister &MOPOSR) {
return SPSELFPerObjectSectionsToRegister::AsArgList::serialize(
OB, MOPOSR.EHFrameSection, MOPOSR.ThreadDataSection);
}
static bool deserialize(SPSInputBuffer &IB,
ELFPerObjectSectionsToRegister &MOPOSR) {
return SPSELFPerObjectSectionsToRegister::AsArgList::deserialize(
IB, MOPOSR.EHFrameSection, MOPOSR.ThreadDataSection);
}
};
using SPSNamedExecutorAddrRangeSequenceMap =
SPSSequence<SPSTuple<SPSString, SPSExecutorAddrRangeSequence>>;
using SPSELFNixJITDylibInitializers =
SPSTuple<SPSString, SPSExecutorAddr, SPSNamedExecutorAddrRangeSequenceMap>;
using SPSELFNixJITDylibInitializerSequence =
SPSSequence<SPSELFNixJITDylibInitializers>;
template <>
class SPSSerializationTraits<SPSELFNixJITDylibInitializers,
ELFNixJITDylibInitializers> {
public:
static size_t size(const ELFNixJITDylibInitializers &MOJDIs) {
return SPSELFNixJITDylibInitializers::AsArgList::size(
MOJDIs.Name, MOJDIs.DSOHandleAddress, MOJDIs.InitSections);
}
static bool serialize(SPSOutputBuffer &OB,
const ELFNixJITDylibInitializers &MOJDIs) {
return SPSELFNixJITDylibInitializers::AsArgList::serialize(
OB, MOJDIs.Name, MOJDIs.DSOHandleAddress, MOJDIs.InitSections);
}
static bool deserialize(SPSInputBuffer &IB,
ELFNixJITDylibInitializers &MOJDIs) {
return SPSELFNixJITDylibInitializers::AsArgList::deserialize(
IB, MOJDIs.Name, MOJDIs.DSOHandleAddress, MOJDIs.InitSections);
}
};
using SPSELFJITDylibDeinitializers = SPSEmpty;
using SPSELFJITDylibDeinitializerSequence =
SPSSequence<SPSELFJITDylibDeinitializers>;
template <>
class SPSSerializationTraits<SPSELFJITDylibDeinitializers,
ELFNixJITDylibDeinitializers> {
public:
static size_t size(const ELFNixJITDylibDeinitializers &MOJDDs) { return 0; }
static bool serialize(SPSOutputBuffer &OB,
const ELFNixJITDylibDeinitializers &MOJDDs) {
return true;
}
static bool deserialize(SPSInputBuffer &IB,
ELFNixJITDylibDeinitializers &MOJDDs) {
MOJDDs = ELFNixJITDylibDeinitializers();
return true;
}
};
} } }
#endif