21 void lookup(
const LookupSet &Symbols, OnResolvedFunction OnResolved)
override {
22 auto &ES = MR.getTargetJITDylib().getExecutionSession();
26 for (
auto &S : Symbols)
27 InternedSymbols.
add(ES.intern(S));
31 auto OnResolvedWithUnwrap =
34 if (!InternedResult) {
35 OnResolved(InternedResult.takeError());
40 for (
auto &KV : *InternedResult)
47 MR.addDependenciesForAll(Deps);
51 MR.getTargetJITDylib().withLinkOrderDo(
55 RegisterDependencies);
61 for (
auto &KV : MR.getSymbols()) {
62 if (Symbols.count(*KV.first))
80 :
ObjectLayer(ES), GetMemoryManager(GetMemoryManager) {
85 assert(MemMgrs.
empty() &&
"Layer destroyed with resources still attached");
89 std::unique_ptr<MaterializationResponsibility> R,
90 std::unique_ptr<MemoryBuffer>
O) {
91 assert(
O &&
"Object must not be null");
99 R->failMaterialization();
105 auto InternalSymbols = std::make_shared<std::set<StringRef>>();
107 for (
auto &Sym : (*Obj)->symbols()) {
110 if (
auto SymType = Sym.getType()) {
114 ES.reportError(SymType.takeError());
115 R->failMaterialization();
120 if (!SymFlagsOrErr) {
122 ES.reportError(SymFlagsOrErr.
takeError());
123 R->failMaterialization();
129 if (
auto SymName = Sym.getName())
130 InternalSymbols->insert(*SymName);
132 ES.reportError(SymName.takeError());
133 R->failMaterialization();
140 auto MemMgr = GetMemoryManager();
141 auto &MemMgrRef = *MemMgr;
145 std::shared_ptr<MaterializationResponsibility> SharedR(
std::move(R));
147 JITDylibSearchOrderResolver
Resolver(*SharedR);
151 MemMgrRef,
Resolver, ProcessAllSections,
152 [
this, SharedR, &MemMgrRef, InternalSymbols](
155 std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) {
156 return onObjLoad(*SharedR, Obj, MemMgrRef, LoadedObjInfo,
157 ResolvedSymbols, *InternalSymbols);
159 [
this, SharedR, MemMgr =
std::move(MemMgr)](
161 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
169 std::lock_guard<std::mutex>
Lock(RTDyldLayerMutex);
171 "Listener has already been registered");
172 EventListeners.push_back(&L);
176 std::lock_guard<std::mutex>
Lock(RTDyldLayerMutex);
178 assert(
I != EventListeners.end() &&
"Listener not registered");
179 EventListeners.erase(
I);
182 Error RTDyldObjectLinkingLayer::onObjLoad(
186 std::map<StringRef, JITEvaluatedSymbol> Resolved,
187 std::set<StringRef> &InternalSymbols) {
193 if (
auto *COFFObj = dyn_cast<object::COFFObjectFile>(&Obj)) {
199 for (
auto &Sym : COFFObj->symbols()) {
204 auto Name = Sym.getName();
206 return Name.takeError();
212 R.getSymbols().count(ES.intern(*
Name)))
214 auto Sec = Sym.getSection();
216 return Sec.takeError();
217 if (*Sec == COFFObj->section_end())
219 auto &COFFSec = *COFFObj->getCOFFSection(**Sec);
229 if (InternalSymbols.count(KV.first))
233 auto Flags = KV.second.getFlags();
237 if (OverrideObjectFlags || AutoClaimObjectSymbols) {
238 auto I = R.getSymbols().find(InternedName);
240 if (OverrideObjectFlags &&
I != R.getSymbols().end())
242 else if (AutoClaimObjectSymbols &&
I == R.getSymbols().end())
243 ExtraSymbolsToClaim[InternedName] = Flags;
249 if (!ExtraSymbolsToClaim.
empty()) {
250 if (
auto Err =
R.defineMaterializing(ExtraSymbolsToClaim))
255 for (
auto &KV : ExtraSymbolsToClaim)
256 if (KV.second.isWeak() && !
R.getSymbols().count(KV.first))
257 Symbols.erase(KV.first);
260 if (
auto Err =
R.notifyResolved(Symbols)) {
261 R.failMaterialization();
266 NotifyLoaded(R, Obj, LoadedObjInfo);
271 void RTDyldObjectLinkingLayer::onObjEmit(
274 std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,
275 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
Error Err) {
278 R.failMaterialization();
282 if (
auto Err =
R.notifyEmitted()) {
284 R.failMaterialization();
288 std::unique_ptr<object::ObjectFile> Obj;
289 std::unique_ptr<MemoryBuffer> ObjBuffer;
290 std::tie(Obj, ObjBuffer) =
O.takeBinary();
294 std::lock_guard<std::mutex>
Lock(RTDyldLayerMutex);
295 for (
auto *L : EventListeners)
303 if (
auto Err =
R.withResourceKeyDo(
304 [&](
ResourceKey K) { MemMgrs[K].push_back(std::move(MemMgr)); })) {
306 R.failMaterialization();
312 std::vector<MemoryManagerUP> MemMgrsToRemove;
315 auto I = MemMgrs.
find(K);
316 if (
I != MemMgrs.
end()) {
323 std::lock_guard<std::mutex>
Lock(RTDyldLayerMutex);
324 for (
auto &MemMgr : MemMgrsToRemove) {
325 for (
auto *L : EventListeners)
327 MemMgr->deregisterEHFrames();
334 void RTDyldObjectLinkingLayer::handleTransferResources(
ResourceKey DstKey,
336 auto I = MemMgrs.
find(SrcKey);
337 if (
I != MemMgrs.
end()) {
338 auto &SrcMemMgrs =
I->second;
339 auto &DstMemMgrs = MemMgrs[DstKey];
340 DstMemMgrs.
reserve(DstMemMgrs.size() + SrcMemMgrs.size());
341 for (
auto &MemMgr : SrcMemMgrs)
346 MemMgrs.
erase(SrcKey);
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Queried, materialization begun.
Information about the loaded object.
This class represents lattice values for constants.
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup.
This class is the base class for all object file types.
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O) override
Emit the object.
Error takeError()
Take ownership of the stored error.
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
RTDyldObjectLinkingLayer(ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
Construct an ObjectLinkingLayer with the given NotifyLoaded, and NotifyEmitted functors.
Tagged union holding either a T or a Error.
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
ExecutionSession & getExecutionSession()
Returns the execution session for this layer.
SymbolLookupSet & add(SymbolStringPtr Name, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Add an element to the set.
iterator find(const_arg_type_t< KeyT > Val)
std::vector< JITTargetAddress > LookupResult
bool erase(const KeyT &Val)
* if(!EatIfPresent(lltok::kw_thread_local)) return false
parseOptionalThreadLocal := /*empty
JITTargetAddress pointerToJITTargetAddress(T *Ptr)
Convert a pointer to a JITTargetAddress.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Symbol resolution interface.
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
void registerJITEventListener(JITEventListener &L)
Register a JITEventListener.
static ErrorSuccess success()
Create a success value.
std::function< std::unique_ptr< RuntimeDyld::MemoryManager >()> GetMemoryManagerFunction
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
A set of symbols to look up, each associated with a SymbolLookupFlags value.
An ExecutionSession represents a running JIT program.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Represents a symbol that has been evaluated to an address already.
void reportError(Error Err)
Report a error for this execution session.
void registerResourceManager(ResourceManager &RM)
Register the given ResourceManager with this ExecutionSession.
LLVM_NODISCARD bool empty() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void unregisterJITEventListener(JITEventListener &L)
Unregister a JITEventListener.
Lightweight error class with error context and mandatory checking.
void jitLinkForORC(object::OwningBinary< object::ObjectFile > O, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver, bool ProcessAllSections, unique_function< Error(const object::ObjectFile &Obj, RuntimeDyld::LoadedObjectInfo &, std::map< StringRef, JITEvaluatedSymbol >)> OnLoaded, unique_function< void(object::OwningBinary< object::ObjectFile >, std::unique_ptr< RuntimeDyld::LoadedObjectInfo >, Error)> OnEmitted)
Interface for Layers that accept object files.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
decltype(auto) runSessionLocked(Func &&F)
Run the given lambda with the session mutex locked.
~RTDyldObjectLinkingLayer()
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.