LLVM 22.0.0git
EPCDynamicLibrarySearchGenerator.cpp
Go to the documentation of this file.
1//===---------------- EPCDynamicLibrarySearchGenerator.cpp ----------------===//
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
13#include "llvm/Support/Error.h"
14
15#define DEBUG_TYPE "orc"
16
17namespace llvm {
18namespace orc {
19
20Expected<std::unique_ptr<EPCDynamicLibrarySearchGenerator>>
22 ExecutionSession &ES, const char *LibraryPath, SymbolPredicate Allow,
23 AddAbsoluteSymbolsFn AddAbsoluteSymbols) {
24 auto Handle =
26 if (!Handle)
27 return Handle.takeError();
28
29 return std::make_unique<EPCDynamicLibrarySearchGenerator>(
30 ES, *Handle, std::move(Allow), std::move(AddAbsoluteSymbols));
31}
32
35 JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) {
36
37 if (Symbols.empty())
38 return Error::success();
39
41 dbgs() << "EPCDynamicLibrarySearchGenerator trying to generate "
42 << Symbols << "\n";
43 });
44
45 // If there's no handle then resolve all requested symbols to null.
46 if (!H) {
47 assert(Allow && "No handle or filter?");
48 SymbolMap Nulls;
49 for (auto &[Name, LookupFlags] : Symbols) {
50 if (Allow(Name))
51 Nulls[Name] = {};
52 }
53 return addAbsolutes(JD, std::move(Nulls));
54 }
55
56 // Otherwise proceed with lookup in the remote.
57 SymbolLookupSet LookupSymbols;
58
59 for (auto &KV : Symbols) {
60 // Skip symbols that don't match the filter.
61 if (Allow && !Allow(KV.first))
62 continue;
63 LookupSymbols.add(KV.first, SymbolLookupFlags::WeaklyReferencedSymbol);
64 }
65
66 DylibManager::LookupRequest Request(*H, LookupSymbols);
67 // Copy-capture LookupSymbols, since LookupRequest keeps a reference.
68 EPC.getDylibMgr().lookupSymbolsAsync(Request, [this, &JD, LS = std::move(LS),
69 LookupSymbols](
70 auto Result) mutable {
71 if (!Result) {
73 dbgs() << "EPCDynamicLibrarySearchGenerator lookup failed due to error";
74 });
75 return LS.continueLookup(Result.takeError());
76 }
77
78 assert(Result->size() == 1 && "Results for more than one library returned");
79 assert(Result->front().size() == LookupSymbols.size() &&
80 "Result has incorrect number of elements");
81
82 SymbolMap NewSymbols;
83 auto ResultI = Result->front().begin();
84 for (auto &KV : LookupSymbols) {
85 if (ResultI->getAddress())
86 NewSymbols[KV.first] = *ResultI;
87 ++ResultI;
88 }
89
91 dbgs() << "EPCDynamicLibrarySearchGenerator lookup returned "
92 << NewSymbols << "\n";
93 });
94
95 // If there were no resolved symbols bail out.
96 if (NewSymbols.empty())
97 return LS.continueLookup(Error::success());
98
99 // Define resolved symbols.
100 Error Err = addAbsolutes(JD, std::move(NewSymbols));
101
102 LS.continueLookup(std::move(Err));
103 });
104
105 return Error::success();
106}
107
108Error EPCDynamicLibrarySearchGenerator::addAbsolutes(JITDylib &JD,
109 SymbolMap Symbols) {
110 return AddAbsoluteSymbols ? AddAbsoluteSymbols(JD, std::move(Symbols))
111 : JD.define(absoluteSymbols(std::move(Symbols)));
112}
113
114} // end namespace orc
115} // end namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_DEBUG(...)
Definition Debug.h:114
bool empty() const
Definition DenseMap.h:107
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
friend class ExecutionSession
Definition Core.h:860
virtual Expected< tpctypes::DylibHandle > loadDylib(const char *DylibPath)=0
Load the dynamic library at the given path and return a handle to it.
static Expected< std::unique_ptr< EPCDynamicLibrarySearchGenerator > > Load(ExecutionSession &ES, const char *LibraryPath, SymbolPredicate Allow=SymbolPredicate(), AddAbsoluteSymbolsFn AddAbsoluteSymbols=nullptr)
Permanently loads the library at the given path and, on success, returns an EPCDynamicLibrarySearchGe...
unique_function< bool(const SymbolStringPtr &)> SymbolPredicate
unique_function< Error(JITDylib &, SymbolMap)> AddAbsoluteSymbolsFn
Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD, JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) override
DefinitionGenerators should override this method to insert new definitions into the parent JITDylib.
ExecutorProcessControl & getExecutorProcessControl()
Get the ExecutorProcessControl object associated with this ExecutionSession.
Definition Core.h:1395
DylibManager & getDylibMgr() const
Return the DylibManager for the target process.
Represents a JIT'd dynamic library.
Definition Core.h:902
Wraps state for a lookup-in-progress.
Definition Core.h:834
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition Core.h:195
UnderlyingVector::size_type size() const
Definition Core.h:276
SymbolLookupSet & add(SymbolStringPtr Name, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Add an element to the set.
Definition Core.h:261
std::unique_ptr< AbsoluteSymbolsMaterializationUnit > absoluteSymbols(SymbolMap Symbols)
Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
JITDylibLookupFlags
Lookup flags that apply to each dylib in the search order for a lookup.
Definition Core.h:147
DenseMap< SymbolStringPtr, ExecutorSymbolDef > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
LookupKind
Describes the kind of lookup being performed.
Definition Core.h:169
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
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:1847
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:851
A pair of a dylib and a set of symbols to be looked up.