LLVM: lib/Support/DynamicLibrary.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
17#include "llvm/Config/config.h"
19#include
20
21using namespace llvm;
23
24
26 using HandleList = std::vector<void *>;
27 HandleList Handles;
28 void *Process = &Invalid;
29
30public:
31 static void *DLOpen(const char *Filename, std::string *Err);
32 static void DLClose(void *Handle);
33 static void *DLSym(void *Handle, const char *Symbol);
34
37
38 HandleList::iterator Find(void *Handle) { return find(Handles, Handle); }
39
41 return Handle == Process || Find(Handle) != Handles.end();
42 }
43
44 bool AddLibrary(void *Handle, bool IsProcess = false, bool CanClose = true,
45 bool AllowDuplicates = false) {
46#ifdef _WIN32
47 assert((Handle == this ? IsProcess : !IsProcess) && "Bad Handle.");
48#endif
49 assert((!AllowDuplicates || !CanClose) &&
50 "CanClose must be false if AllowDuplicates is true.");
51
53 if (!AllowDuplicates && Find(Handle) != Handles.end()) {
54 if (CanClose)
56 return false;
57 }
58 Handles.push_back(Handle);
59 } else {
60#ifndef _WIN32
61 if (Process != &Invalid) {
62 if (CanClose)
64 if (Process == Handle)
65 return false;
66 }
67#endif
68 Process = Handle;
69 }
70 return true;
71 }
72
75 HandleList::iterator it = Find(Handle);
76 if (it != Handles.end()) {
77 Handles.erase(it);
78 }
79 }
80
83 for (void *Handle : Handles) {
84 if (void *Ptr = DLSym(Handle, Symbol))
85 return Ptr;
86 }
87 } else {
89 if (void *Ptr = DLSym(Handle, Symbol))
90 return Ptr;
91 }
92 }
93 return nullptr;
94 }
95
98 "Invalid Ordering");
99
100 if (Process == &Invalid || (Order & SO_LoadedFirst)) {
101 if (void *Ptr = LibLookup(Symbol, Order))
102 return Ptr;
103 }
104 if (Process != &Invalid) {
105
106 if (void *Ptr = DLSym(Process, Symbol))
107 return Ptr;
108
109
111 if (void *Ptr = LibLookup(Symbol, Order))
112 return Ptr;
113 }
114 }
115 return nullptr;
116 }
117};
118
119namespace {
120
121struct Globals {
122
123
125
128
130};
131
132Globals &getGlobals() {
133 static Globals G;
134 return G;
135}
136
137}
138
139#ifdef _WIN32
140
142
143#else
144
146
147#endif
148
149char DynamicLibrary::Invalid;
152
153namespace llvm {
155 return DoSearch(SymbolName);
156}
157}
158
160 auto &G = getGlobals();
162 G.ExplicitSymbols[SymbolName] = SymbolValue;
163}
164
166 std::string *Err) {
167 auto &G = getGlobals();
169 if (Handle != &Invalid) {
171 G.OpenedHandles.AddLibrary(Handle, FileName == nullptr);
172 }
173
175}
176
178 std::string *Err) {
179 auto &G = getGlobals();
181
182 if (.OpenedHandles.AddLibrary(Handle, false,
183 false))
184 *Err = "Library already loaded";
185
187}
188
190 std::string *Err) {
191 assert(FileName && "Use getPermanentLibrary() for opening process handle");
193 if (Handle != &Invalid) {
194 auto &G = getGlobals();
196 G.OpenedTemporaryHandles.AddLibrary(Handle, false,
197 false,
198 true);
199 }
201}
202
204 auto &G = getGlobals();
206 if (Lib.isValid()) {
207 G.OpenedTemporaryHandles.CloseLibrary(Lib.Data);
208 Lib.Data = &Invalid;
209 }
210}
211
217
219 {
220 auto &G = getGlobals();
222
223
225
226 if (i != G.ExplicitSymbols.end())
227 return i->second;
228
229
230 if (void *Ptr = G.OpenedHandles.Lookup(SymbolName, SearchOrder))
231 return Ptr;
232 if (void *Ptr = G.OpenedTemporaryHandles.Lookup(SymbolName, SearchOrder))
233 return Ptr;
234 }
235
237}
238
239
240
241
242
246
250
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
#define LLVM_LIKELY(EXPR)
Definition DynamicLibrary.cpp:25
bool AddLibrary(void *Handle, bool IsProcess=false, bool CanClose=true, bool AllowDuplicates=false)
Definition DynamicLibrary.cpp:44
void * Lookup(const char *Symbol, DynamicLibrary::SearchOrdering Order)
Definition DynamicLibrary.cpp:96
bool Contains(void *Handle)
Definition DynamicLibrary.cpp:40
HandleList::iterator Find(void *Handle)
Definition DynamicLibrary.cpp:38
static void * DLOpen(const char *Filename, std::string *Err)
void * LibLookup(const char *Symbol, DynamicLibrary::SearchOrdering Order)
Definition DynamicLibrary.cpp:81
static void * DLSym(void *Handle, const char *Symbol)
static void DLClose(void *Handle)
void CloseLibrary(void *Handle)
Definition DynamicLibrary.cpp:73
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
StringMapIterBase< ValueTy, false > iterator
StringRef - Represent a constant reference to a string, i.e.
static LLVM_ABI DynamicLibrary getLibrary(const char *FileName, std::string *Err=nullptr)
This function loads the dynamic library at the given path, using the library load operation from the ...
Definition DynamicLibrary.cpp:189
static LLVM_ABI DynamicLibrary addPermanentLibrary(void *handle, std::string *errMsg=nullptr)
Registers an externally loaded library.
Definition DynamicLibrary.cpp:177
static LLVM_ABI void AddSymbol(StringRef symbolName, void *symbolValue)
This functions permanently adds the symbol symbolName with the value symbolValue.
Definition DynamicLibrary.cpp:159
static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=nullptr)
This function permanently loads the dynamic library at the given path.
DynamicLibrary(void *data=&Invalid)
@ SO_LoadedLast
SO_LoadedLast - Search as SO_Linker would, then loaded libraries.
@ SO_LoadOrder
SO_LoadOrder - Or this in to search libraries in the ordered loaded.
@ SO_Linker
SO_Linker - Search as a call to dlsym(dlopen(NULL)) would when DynamicLibrary::getPermanentLibrary(NU...
@ SO_LoadedFirst
SO_LoadedFirst - Search all loaded libraries, then as SO_Linker would.
static LLVM_ABI DynamicLibrary getPermanentLibrary(const char *filename, std::string *errMsg=nullptr)
This function permanently loads the dynamic library at the given path using the library load operatio...
Definition DynamicLibrary.cpp:165
static LLVM_ABI SearchOrdering SearchOrder
LLVM_ABI void * getAddressOfSymbol(const char *symbolName)
Searches through the library for the symbol symbolName.
Definition DynamicLibrary.cpp:212
bool isValid() const
Returns true if the object refers to a valid library.
static LLVM_ABI void * SearchForAddressOfSymbol(const char *symbolName)
This function will search through all previously loaded dynamic libraries for the symbol symbolName.
Definition DynamicLibrary.cpp:218
static LLVM_ABI void closeLibrary(DynamicLibrary &Lib)
This function closes the dynamic library at the given path, using the library close operation of the ...
Definition DynamicLibrary.cpp:203
SmartMutex - A mutex with a compile time constant parameter that indicates whether this mutex should ...
LLVM_C_ABI LLVMBool LLVMLoadLibraryPermanently(const char *Filename)
This function permanently loads the dynamic library at the given path.
Definition DynamicLibrary.cpp:243
LLVM_C_ABI void * LLVMSearchForAddressOfSymbol(const char *symbolName)
This function will search through all previously loaded dynamic libraries for the symbol symbolName.
Definition DynamicLibrary.cpp:247
LLVM_C_ABI void LLVMAddSymbol(const char *symbolName, void *symbolValue)
This functions permanently adds the symbol symbolName with the value symbolValue.
Definition DynamicLibrary.cpp:251
std::lock_guard< SmartMutex< mt_only > > SmartScopedLock
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
auto reverse(ContainerTy &&C)
void * SearchForAddressOfSpecialSymbol(const char *SymbolName)
Definition DynamicLibrary.cpp:154