1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
//===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the operating system DynamicLibrary concept.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/DynamicLibrary.h"
#include "llvm-c/Support.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Config/config.h"
#include "llvm/Support/Mutex.h"
#include <vector>
using namespace llvm;
using namespace llvm::sys;
// All methods for HandleSet should be used holding SymbolsMutex.
class DynamicLibrary::HandleSet {
typedef std::vector<void *> HandleList;
HandleList Handles;
void *Process = nullptr;
public:
static void *DLOpen(const char *Filename, std::string *Err);
static void DLClose(void *Handle);
static void *DLSym(void *Handle, const char *Symbol);
HandleSet() = default;
~HandleSet();
HandleList::iterator Find(void *Handle) { return find(Handles, Handle); }
bool Contains(void *Handle) {
return Handle == Process || Find(Handle) != Handles.end();
}
bool AddLibrary(void *Handle, bool IsProcess = false, bool CanClose = true,
bool AllowDuplicates = false) {
#ifdef _WIN32
assert((Handle == this ? IsProcess : !IsProcess) && "Bad Handle.");
#endif
assert((!AllowDuplicates || !CanClose) &&
"CanClose must be false if AllowDuplicates is true.");
if (LLVM_LIKELY(!IsProcess)) {
if (!AllowDuplicates && Find(Handle) != Handles.end()) {
if (CanClose)
DLClose(Handle);
return false;
}
Handles.push_back(Handle);
} else {
#ifndef _WIN32
if (Process) {
if (CanClose)
DLClose(Process);
if (Process == Handle)
return false;
}
#endif
Process = Handle;
}
return true;
}
void CloseLibrary(void *Handle) {
DLClose(Handle);
HandleList::iterator it = Find(Handle);
if (it != Handles.end()) {
Handles.erase(it);
}
}
void *LibLookup(const char *Symbol, DynamicLibrary::SearchOrdering Order) {
if (Order & SO_LoadOrder) {
for (void *Handle : Handles) {
if (void *Ptr = DLSym(Handle, Symbol))
return Ptr;
}
} else {
for (void *Handle : llvm::reverse(Handles)) {
if (void *Ptr = DLSym(Handle, Symbol))
return Ptr;
}
}
return nullptr;
}
void *Lookup(const char *Symbol, DynamicLibrary::SearchOrdering Order) {
assert(!((Order & SO_LoadedFirst) && (Order & SO_LoadedLast)) &&
"Invalid Ordering");
if (!Process || (Order & SO_LoadedFirst)) {
if (void *Ptr = LibLookup(Symbol, Order))
return Ptr;
}
if (Process) {
// Use OS facilities to search the current binary and all loaded libs.
if (void *Ptr = DLSym(Process, Symbol))
return Ptr;
// Search any libs that might have been skipped because of RTLD_LOCAL.
if (Order & SO_LoadedLast) {
if (void *Ptr = LibLookup(Symbol, Order))
return Ptr;
}
}
return nullptr;
}
};
namespace {
struct Globals {
// Collection of symbol name/value pairs to be searched prior to any
// libraries.
llvm::StringMap<void *> ExplicitSymbols;
// Collections of known library handles.
DynamicLibrary::HandleSet OpenedHandles;
DynamicLibrary::HandleSet OpenedTemporaryHandles;
// Lock for ExplicitSymbols, OpenedHandles, and OpenedTemporaryHandles.
llvm::sys::SmartMutex<true> SymbolsMutex;
};
Globals &getGlobals() {
static Globals G;
return G;
}
} // namespace
#ifdef _WIN32
#include "Windows/DynamicLibrary.inc"
#else
#include "Unix/DynamicLibrary.inc"
#endif
char DynamicLibrary::Invalid;
DynamicLibrary::SearchOrdering DynamicLibrary::SearchOrder =
DynamicLibrary::SO_Linker;
namespace llvm {
void *SearchForAddressOfSpecialSymbol(const char *SymbolName) {
return DoSearch(SymbolName); // DynamicLibrary.inc
}
} // namespace llvm
void DynamicLibrary::AddSymbol(StringRef SymbolName, void *SymbolValue) {
auto &G = getGlobals();
SmartScopedLock<true> Lock(G.SymbolsMutex);
G.ExplicitSymbols[SymbolName] = SymbolValue;
}
DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *FileName,
std::string *Err) {
auto &G = getGlobals();
void *Handle = HandleSet::DLOpen(FileName, Err);
if (Handle != &Invalid) {
SmartScopedLock<true> Lock(G.SymbolsMutex);
G.OpenedHandles.AddLibrary(Handle, /*IsProcess*/ FileName == nullptr);
}
return DynamicLibrary(Handle);
}
DynamicLibrary DynamicLibrary::addPermanentLibrary(void *Handle,
std::string *Err) {
auto &G = getGlobals();
SmartScopedLock<true> Lock(G.SymbolsMutex);
// If we've already loaded this library, tell the caller.
if (!G.OpenedHandles.AddLibrary(Handle, /*IsProcess*/ false,
/*CanClose*/ false))
*Err = "Library already loaded";
return DynamicLibrary(Handle);
}
DynamicLibrary DynamicLibrary::getLibrary(const char *FileName,
std::string *Err) {
assert(FileName && "Use getPermanentLibrary() for opening process handle");
void *Handle = HandleSet::DLOpen(FileName, Err);
if (Handle != &Invalid) {
auto &G = getGlobals();
SmartScopedLock<true> Lock(G.SymbolsMutex);
G.OpenedTemporaryHandles.AddLibrary(Handle, /*IsProcess*/ false,
/*CanClose*/ false,
/*AllowDuplicates*/ true);
}
return DynamicLibrary(Handle);
}
void DynamicLibrary::closeLibrary(DynamicLibrary &Lib) {
auto &G = getGlobals();
SmartScopedLock<true> Lock(G.SymbolsMutex);
if (Lib.isValid()) {
G.OpenedTemporaryHandles.CloseLibrary(Lib.Data);
Lib.Data = &Invalid;
}
}
void *DynamicLibrary::getAddressOfSymbol(const char *SymbolName) {
if (!isValid())
return nullptr;
return HandleSet::DLSym(Data, SymbolName);
}
void *DynamicLibrary::SearchForAddressOfSymbol(const char *SymbolName) {
{
auto &G = getGlobals();
SmartScopedLock<true> Lock(G.SymbolsMutex);
// First check symbols added via AddSymbol().
StringMap<void *>::iterator i = G.ExplicitSymbols.find(SymbolName);
if (i != G.ExplicitSymbols.end())
return i->second;
// Now search the libraries.
if (void *Ptr = G.OpenedHandles.Lookup(SymbolName, SearchOrder))
return Ptr;
if (void *Ptr = G.OpenedTemporaryHandles.Lookup(SymbolName, SearchOrder))
return Ptr;
}
return llvm::SearchForAddressOfSpecialSymbol(SymbolName);
}
//===----------------------------------------------------------------------===//
// C API.
//===----------------------------------------------------------------------===//
LLVMBool LLVMLoadLibraryPermanently(const char *Filename) {
return llvm::sys::DynamicLibrary::LoadLibraryPermanently(Filename);
}
void *LLVMSearchForAddressOfSymbol(const char *symbolName) {
return llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(symbolName);
}
void LLVMAddSymbol(const char *symbolName, void *symbolValue) {
return llvm::sys::DynamicLibrary::AddSymbol(symbolName, symbolValue);
}
|