14 #ifndef LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H 15 #define LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H 50 std::function<std::shared_ptr<RuntimeDyld::MemoryManager>(
VModuleKey)>;
61 std::unique_ptr<MemoryBuffer>
O)
override;
75 this->ProcessAllSections = ProcessAllSections;
79 mutable std::mutex RTDyldLayerMutex;
83 bool ProcessAllSections;
84 std::map<VModuleKey, RuntimeDyld *> ActiveRTDylds;
85 std::map<VModuleKey, std::shared_ptr<RuntimeDyld::MemoryManager>> MemMgrs;
111 getSymbolMaterializer(std::string
Name) = 0;
117 auto SymEntry = SymbolTable.find(Name);
118 if (SymEntry == SymbolTable.end())
120 if (!SymEntry->second.getFlags().isExported() && ExportedSymbolsOnly)
123 return JITSymbol(getSymbolMaterializer(Name),
124 SymEntry->second.getFlags());
130 bool Finalized =
false;
156 using NotifyFreedFtor = std::function<void(VModuleKey, const object::ObjectFile &Obj)>;
161 template <
typename MemoryManagerPtrT>
166 std::shared_ptr<SymbolResolver>
Resolver,
167 bool ProcessAllSections)
170 MemMgr(std::move(MemMgr)),
171 PFC(llvm::make_unique<PreFinalizeContents>(
172 std::move(Obj), std::move(Resolver),
173 ProcessAllSections)) {
174 buildInitialSymbolTable(PFC->Obj);
177 ~ConcreteLinkedObject()
override {
178 if (this->Parent.NotifyFreed)
179 this->Parent.NotifyFreed(K, *ObjForNotify.getBinary());
181 MemMgr->deregisterEHFrames();
185 assert(PFC &&
"mapSectionAddress called on finalized LinkedObject");
189 PFC->RTDyld = llvm::make_unique<RuntimeDyld>(*MemMgr, ResolverAdapter);
190 PFC->RTDyld->setProcessAllSections(PFC->ProcessAllSections);
194 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> Info =
195 PFC->RTDyld->loadObject(*PFC->Obj.getBinary());
199 auto SymTab = PFC->RTDyld->getSymbolTable();
200 for (
auto &KV : SymTab)
201 SymbolTable[KV.first] = KV.second;
204 if (Parent.NotifyLoaded)
205 Parent.NotifyLoaded(K, *PFC->Obj.getBinary(), *Info);
207 PFC->RTDyld->finalizeWithMemoryManagerLocking();
209 if (PFC->RTDyld->hasError())
210 return make_error<StringError>(PFC->RTDyld->getErrorString(),
213 if (Parent.NotifyFinalized)
214 Parent.NotifyFinalized(K, *PFC->Obj.getBinary(), *Info);
217 if (this->Parent.NotifyFreed)
218 ObjForNotify = std::move(PFC->Obj);
227 if (!this->Finalized)
229 return std::move(Err);
230 return this->
getSymbol(Name,
false).getAddress();
236 assert(PFC &&
"mapSectionAddress called on finalized LinkedObject");
237 assert(PFC->RTDyld &&
"mapSectionAddress called on raw LinkedObject");
238 PFC->RTDyld->mapSectionAddress(LocalAddress, TargetAddr);
242 void buildInitialSymbolTable(
const OwnedObject &Obj) {
260 struct PreFinalizeContents {
262 std::shared_ptr<SymbolResolver> Resolver,
263 bool ProcessAllSections)
264 : Obj(std::move(Obj)),
265 Resolver(std::move(Resolver)),
266 ProcessAllSections(ProcessAllSections) {}
269 std::shared_ptr<SymbolResolver> Resolver;
270 bool ProcessAllSections;
271 std::unique_ptr<RuntimeDyld> RTDyld;
276 MemoryManagerPtrT MemMgr;
278 std::unique_ptr<PreFinalizeContents> PFC;
281 template <
typename MemoryManagerPtrT>
282 std::unique_ptr<ConcreteLinkedObject<MemoryManagerPtrT>>
285 std::shared_ptr<SymbolResolver>
Resolver,
286 bool ProcessAllSections) {
287 using LOS = ConcreteLinkedObject<MemoryManagerPtrT>;
288 return llvm::make_unique<LOS>(Parent, std::move(K), std::move(Obj),
289 std::move(MemMgr), std::move(Resolver),
295 std::shared_ptr<RuntimeDyld::MemoryManager>
MemMgr;
308 : ES(ES), GetResources(
std::move(GetResources)),
309 NotifyLoaded(
std::move(NotifyLoaded)),
310 NotifyFinalized(
std::move(NotifyFinalized)),
311 NotifyFreed(
std::move(NotifyFreed)),
312 ProcessAllSections(
false) {
322 this->ProcessAllSections = ProcessAllSections;
331 return Obj.takeError();
333 assert(!LinkedObjects.count(K) &&
"VModuleKey already in use");
335 auto R = GetResources(K);
337 LinkedObjects[K] = createLinkedObject(
338 *
this, K,
OwnedObject(std::move(*Obj), std::move(ObjBuffer)),
339 std::move(R.MemMgr), std::move(R.Resolver), ProcessAllSections);
353 assert(LinkedObjects.count(K) &&
"VModuleKey not associated with object");
355 LinkedObjects.erase(K);
364 for (
auto &KV : LinkedObjects)
365 if (
auto Sym = KV.second->getSymbol(Name, ExportedSymbolsOnly))
367 else if (
auto Err = Sym.takeError())
368 return std::move(Err);
381 bool ExportedSymbolsOnly) {
382 assert(LinkedObjects.count(K) &&
"VModuleKey not associated with object");
383 return LinkedObjects[K]->getSymbol(Name, ExportedSymbolsOnly);
390 assert(LinkedObjects.count(K) &&
"VModuleKey not associated with object");
391 LinkedObjects[K]->mapSectionAddress(LocalAddress, TargetAddr);
398 assert(LinkedObjects.count(K) &&
"VModuleKey not associated with object");
399 return LinkedObjects[K]->finalize();
405 std::map<VModuleKey, std::unique_ptr<LinkedObject>> LinkedObjects;
410 bool ProcessAllSections =
false;
416 #endif // LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Information about the loaded object.
Represents a symbol in the JIT.
Compute iterated dominance frontiers using a linear time algorithm.
JITSymbol getSymbol(StringRef Name, bool ExportedSymbolsOnly)
Holds an object to be allocated/linked as a unit in the JIT.
Error emitAndFinalize(VModuleKey K)
Immediately emit and finalize the object represented by the given VModuleKey.
Bare bones object linking layer.
void setProcessAllSections(bool ProcessAllSections)
Set the 'ProcessAllSections' flag.
This class is the base class for all object file types.
Error takeError()
Take ownership of the stored error.
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
StringMap< JITEvaluatedSymbol > SymbolTable
std::function< void(VModuleKey, const object::ObjectFile &Obj)> NotifyFreedFtor
Functor for receiving deallocation notifications.
void mapSectionAddress(VModuleKey K, const void *LocalAddress, JITTargetAddress TargetAddr)
Map section addresses for the object associated with the VModuleKey K.
Tagged union holding either a T or a Error.
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
std::function< void(VModuleKey, const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &)> NotifyLoadedFtor
Functor for receiving object-loaded notifications.
uint64_t JITTargetAddress
Represents an address in the target process's address space.
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
RTDyldObjectLinkingLayer2(ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager, NotifyLoadedFunction NotifyLoaded=NotifyLoadedFunction(), NotifyFinalizedFunction NotifyFinalized=NotifyFinalizedFunction())
Construct an ObjectLinkingLayer with the given NotifyLoaded, and NotifyFinalized functors.
void mapSectionAddress(VModuleKey K, const void *LocalAddress, JITTargetAddress TargetAddr) const
Map section addresses for the object associated with the VModuleKey K.
void emit(MaterializationResponsibility R, VModuleKey K, std::unique_ptr< MemoryBuffer > O) override
Emit the object.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
RTDyldObjectLinkingLayer(ExecutionSession &ES, ResourcesGetter GetResources, NotifyLoadedFtor NotifyLoaded=NotifyLoadedFtor(), NotifyFinalizedFtor NotifyFinalized=NotifyFinalizedFtor(), NotifyFreedFtor NotifyFreed=NotifyFreedFtor())
Construct an ObjectLinkingLayer with the given NotifyLoaded, and NotifyFinalized functors.
static JITSymbolFlags fromObjectSymbol(const object::BasicSymbolRef &Symbol)
Construct a JITSymbolFlags value based on the flags of the given libobject symbol.
std::function< std::shared_ptr< RuntimeDyld::MemoryManager >(VModuleKey)> GetMemoryManagerFunction
std::function< Resources(VModuleKey)> ResourcesGetter
void consumeError(Error Err)
Consume a Error without doing anything.
std::function< void(VModuleKey)> NotifyFinalizedFunction
Functor for receiving finalization notifications.
static ErrorSuccess success()
Create a success value.
std::function< void(VModuleKey, const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &)> NotifyFinalizedFtor
Functor for receiving finalization notifications.
An ExecutionSession represents a running JIT program.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
void setProcessAllSections(bool ProcessAllSections)
Set the 'ProcessAllSections' flag.
Represents a symbol that has been evaluated to an address already.
JITSymbol findSymbolIn(VModuleKey K, StringRef Name, bool ExportedSymbolsOnly)
Search for the given named symbol in the context of the loaded object represented by the VModuleKey K...
std::unique_ptr< MemoryBuffer > ObjectPtr
std::function< Expected< JITTargetAddress >()> GetAddressFtor
symbol_iterator_range symbols() const
std::shared_ptr< RuntimeDyld::MemoryManager > MemMgr
JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
Error addObject(VModuleKey K, ObjectPtr ObjBuffer)
Add an object to the JIT.
Error removeObject(VModuleKey K)
Remove the object associated with VModuleKey K.
std::shared_ptr< SymbolResolver > Resolver
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Interface for Layers that accept object files.
std::function< void(VModuleKey, const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &)> NotifyLoadedFunction
Functor for receiving object-loaded notifications.
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...