-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathDynamicLibraryManager.cpp
510 lines (442 loc) · 17.1 KB
/
DynamicLibraryManager.cpp
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
// author: Vassil Vassilev <[email protected]>
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------
#include "DynamicLibraryManager.h"
#include "Compatibility.h"
#include "Paths.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/BinaryFormat/Magic.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#if defined(_WIN32)
#include "llvm/BinaryFormat/COFF.h"
#include "llvm/Support/Endian.h"
#endif
#include <fstream>
#include <system_error>
#include <sys/stat.h>
namespace Cpp {
using namespace Cpp::utils::platform;
using namespace Cpp::utils;
using namespace llvm;
DynamicLibraryManager::DynamicLibraryManager() {
const SmallVector<const char*, 10> kSysLibraryEnv = {
"LD_LIBRARY_PATH",
#if __APPLE__
"DYLD_LIBRARY_PATH",
"DYLD_FALLBACK_LIBRARY_PATH",
/*
"DYLD_VERSIONED_LIBRARY_PATH",
"DYLD_FRAMEWORK_PATH",
"DYLD_FALLBACK_FRAMEWORK_PATH",
"DYLD_VERSIONED_FRAMEWORK_PATH",
*/
#elif defined(_WIN32)
"PATH",
#endif
};
// Behaviour is to not add paths that don't exist...In an interpreted env
// does this make sense? Path could pop into existence at any time.
for (const char* Var : kSysLibraryEnv) {
if (const char* Env = GetEnv(Var)) {
SmallVector<StringRef, 10> CurPaths;
SplitPaths(Env, CurPaths, utils::kPruneNonExistent,
Cpp::utils::platform::kEnvDelim);
for (const auto& Path : CurPaths)
addSearchPath(Path);
}
}
// $CWD is the last user path searched.
addSearchPath(".");
SmallVector<std::string, 64> SysPaths;
Cpp::utils::platform::GetSystemLibraryPaths(SysPaths);
for (const std::string& P : SysPaths)
addSearchPath(P, /*IsUser*/ false);
}
///\returns substitution of pattern in the front of original with replacement
/// Example: substFront("@rpath/abc", "@rpath/", "/tmp") -> "/tmp/abc"
static std::string substFront(StringRef original, StringRef pattern,
StringRef replacement) {
if (!original.starts_with_insensitive(pattern))
return original.str();
SmallString<512> result(replacement);
result.append(original.drop_front(pattern.size()));
return result.str().str();
}
///\returns substitution of all known linker variables in \c original
static std::string substAll(StringRef original,
StringRef libLoader) {
// Handle substitutions (MacOS):
// @rpath - This function does not substitute @rpath, because
// this variable is already handled by lookupLibrary where
// @rpath is replaced with all paths from RPATH one by one.
// @executable_path - Main program path.
// @loader_path - Loader library (or main program) path.
//
// Handle substitutions (Linux):
// https://man7.org/linux/man-pages/man8/ld.so.8.html
// $origin - Loader library (or main program) path.
// $lib - lib lib64
// $platform - x86_64 AT_PLATFORM
std::string result;
#ifdef __APPLE__
SmallString<512> mainExecutablePath(llvm::sys::fs::getMainExecutable(nullptr, nullptr));
llvm::sys::path::remove_filename(mainExecutablePath);
SmallString<512> loaderPath;
if (libLoader.empty()) {
loaderPath = mainExecutablePath;
} else {
loaderPath = libLoader.str();
llvm::sys::path::remove_filename(loaderPath);
}
result = substFront(original, "@executable_path", mainExecutablePath);
result = substFront(result, "@loader_path", loaderPath);
return result;
#else
SmallString<512> loaderPath;
if (libLoader.empty()) {
loaderPath = llvm::sys::fs::getMainExecutable(nullptr, nullptr);
} else {
loaderPath = libLoader.str();
}
llvm::sys::path::remove_filename(loaderPath);
result = substFront(original, "$origin", loaderPath);
//result = substFront(result, "$lib", true?"lib":"lib64");
//result = substFront(result, "$platform", "x86_64");
return result;
#endif
}
std::string
DynamicLibraryManager::lookupLibInPaths(StringRef libStem,
SmallVector<llvm::StringRef,2> RPath /*={}*/,
SmallVector<llvm::StringRef,2> RunPath /*={}*/,
StringRef libLoader /*=""*/) const {
#define DEBUG_TYPE "Dyld::lookupLibInPaths"
LLVM_DEBUG(dbgs() << "Dyld::lookupLibInPaths" << libStem.str()
<< ", ..., libLoader=" << libLoader << "\n");
// Lookup priority is: RPATH, LD_LIBRARY_PATH/m_SearchPaths, RUNPATH
// See: https://en.wikipedia.org/wiki/Rpath
// See: https://amir.rachum.com/blog/2016/09/17/shared-libraries/
LLVM_DEBUG(dbgs() << "Dyld::lookupLibInPaths: \n");
LLVM_DEBUG(dbgs() << ":: RPATH\n");
for (auto Info : RPath) {
LLVM_DEBUG(dbgs() << ":::: " << Info.str() << "\n");
}
LLVM_DEBUG(dbgs() << ":: SearchPaths (LD_LIBRARY_PATH, etc...)\n");
for (auto Info : getSearchPaths()) {
LLVM_DEBUG(dbgs() << ":::: " << Info.Path << ", user=" << (Info.IsUser?"true":"false") << "\n");
}
LLVM_DEBUG(dbgs() << ":: RUNPATH\n");
for (auto Info : RunPath) {
LLVM_DEBUG(dbgs() << ":::: " << Info.str() << "\n");
}
SmallString<512> ThisPath;
// RPATH
for (auto Info : RPath) {
ThisPath = substAll(Info, libLoader);
llvm::sys::path::append(ThisPath, libStem);
// to absolute path?
LLVM_DEBUG(dbgs() << "## Try: " << ThisPath);
if (isSharedLibrary(ThisPath.str())) {
LLVM_DEBUG(dbgs() << " ... Found (in RPATH)!\n");
return ThisPath.str().str();
}
}
// m_SearchPaths
for (const SearchPathInfo& Info : m_SearchPaths) {
ThisPath = Info.Path;
llvm::sys::path::append(ThisPath, libStem);
// to absolute path?
LLVM_DEBUG(dbgs() << "## Try: " << ThisPath);
if (isSharedLibrary(ThisPath.str())) {
LLVM_DEBUG(dbgs() << " ... Found (in SearchPaths)!\n");
return ThisPath.str().str();
}
}
// RUNPATH
for (auto Info : RunPath) {
ThisPath = substAll(Info, libLoader);
llvm::sys::path::append(ThisPath, libStem);
// to absolute path?
LLVM_DEBUG(dbgs() << "## Try: " << ThisPath);
if (isSharedLibrary(ThisPath.str())) {
LLVM_DEBUG(dbgs() << " ... Found (in RUNPATH)!\n");
return ThisPath.str().str();
}
}
LLVM_DEBUG(dbgs() << "## NotFound!!!\n");
return "";
#undef DEBUG_TYPE
}
std::string
DynamicLibraryManager::lookupLibMaybeAddExt(StringRef libStem,
SmallVector<llvm::StringRef,2> RPath /*={}*/,
SmallVector<llvm::StringRef,2> RunPath /*={}*/,
StringRef libLoader /*=""*/) const {
#define DEBUG_TYPE "Dyld::lookupLibMaybeAddExt:"
using namespace llvm::sys;
LLVM_DEBUG(dbgs() << "Dyld::lookupLibMaybeAddExt: " << libStem.str()
<< ", ..., libLoader=" << libLoader << "\n");
std::string foundDyLib = lookupLibInPaths(libStem, RPath, RunPath, libLoader);
if (foundDyLib.empty()) {
// Add DyLib extension:
SmallString<512> filenameWithExt(libStem);
#if defined(LLVM_ON_UNIX)
#ifdef __APPLE__
SmallString<512>::iterator IStemEnd = filenameWithExt.end() - 1;
#endif
static const char* DyLibExt = ".so";
#elif defined(_WIN32)
static const char* DyLibExt = ".dll";
#else
# error "Unsupported platform."
#endif
filenameWithExt += DyLibExt;
foundDyLib = lookupLibInPaths(filenameWithExt, RPath, RunPath, libLoader);
#ifdef __APPLE__
if (foundDyLib.empty()) {
filenameWithExt.erase(IStemEnd + 1, filenameWithExt.end());
filenameWithExt += ".dylib";
foundDyLib = lookupLibInPaths(filenameWithExt, RPath, RunPath, libLoader);
}
#endif
}
if (foundDyLib.empty())
return std::string();
// get canonical path name and check if already loaded
const std::string Path = platform::NormalizePath(foundDyLib);
if (Path.empty()) {
LLVM_DEBUG(dbgs() << "cling::DynamicLibraryManager::lookupLibMaybeAddExt(): "
<< "error getting real (canonical) path of library " << foundDyLib << '\n');
return foundDyLib;
}
return Path;
#undef DEBUG_TYPE
}
std::string DynamicLibraryManager::normalizePath(StringRef path) {
#define DEBUG_TYPE "Dyld::normalizePath:"
// Make the path canonical if the file exists.
const std::string Path = path.str();
struct stat buffer;
if (::stat(Path.c_str(), &buffer) != 0)
return std::string();
const std::string NPath = platform::NormalizePath(Path);
if (NPath.empty())
LLVM_DEBUG(dbgs() << "Could not normalize: '" << Path << "'");
return NPath;
#undef DEBUG_TYPE
}
std::string RPathToStr2(SmallVector<StringRef,2> V) {
std::string result;
for (auto item : V)
result += item.str() + ",";
if (!result.empty())
result.pop_back();
return result;
}
std::string
DynamicLibraryManager::lookupLibrary(StringRef libStem,
SmallVector<llvm::StringRef,2> RPath /*={}*/,
SmallVector<llvm::StringRef,2> RunPath /*={}*/,
StringRef libLoader /*=""*/,
bool variateLibStem /*=true*/) const {
#define DEBUG_TYPE "Dyld::lookupLibrary:"
LLVM_DEBUG(dbgs() << "Dyld::lookupLibrary: " << libStem.str() << ", "
<< RPathToStr2(RPath) << ", " << RPathToStr2(RunPath)
<< ", " << libLoader.str() << "\n");
// If it is an absolute path, don't try iterate over the paths.
if (llvm::sys::path::is_absolute(libStem)) {
if (isSharedLibrary(libStem))
return normalizePath(libStem);
LLVM_DEBUG(dbgs() << "Dyld::lookupLibrary: '" << libStem.str() << "'"
<< "is not a shared library\n");
return std::string();
}
// Subst all known linker variables ($origin, @rpath, etc.)
#ifdef __APPLE__
// On MacOS @rpath is preplaced by all paths in RPATH one by one.
if (libStem.starts_with_insensitive("@rpath")) {
for (auto& P : RPath) {
std::string result = substFront(libStem, "@rpath", P);
if (isSharedLibrary(result))
return normalizePath(result);
}
} else {
#endif
std::string result = substAll(libStem, libLoader);
if (isSharedLibrary(result))
return normalizePath(result);
#ifdef __APPLE__
}
#endif
// Expand libStem with paths, extensions, etc.
std::string foundName;
if (variateLibStem) {
foundName = lookupLibMaybeAddExt(libStem, RPath, RunPath, libLoader);
if (foundName.empty()) {
StringRef libStemName = llvm::sys::path::filename(libStem);
if (!libStemName.starts_with("lib")) {
// try with "lib" prefix:
foundName = lookupLibMaybeAddExt(
libStem.str().insert(libStem.size()-libStemName.size(), "lib"),
RPath,
RunPath,
libLoader
);
}
}
} else {
foundName = lookupLibInPaths(libStem, RPath, RunPath, libLoader);
}
if (!foundName.empty())
return platform::NormalizePath(foundName);
return std::string();
#undef DEBUG_TYPE
}
DynamicLibraryManager::LoadLibResult
DynamicLibraryManager::loadLibrary(StringRef libStem,
bool permanent, bool resolved) {
#define DEBUG_TYPE "Dyld::loadLibrary:"
LLVM_DEBUG(dbgs() << "Dyld::loadLibrary: " << libStem.str() << ", "
<< (permanent ? "permanent" : "not-permanent") << ", "
<< (resolved ? "resolved" : "not-resolved") << "\n");
std::string canonicalLoadedLib;
if (resolved) {
canonicalLoadedLib = libStem.str();
} else {
canonicalLoadedLib = lookupLibrary(libStem);
if (canonicalLoadedLib.empty())
return kLoadLibNotFound;
}
if (m_LoadedLibraries.find(canonicalLoadedLib) != m_LoadedLibraries.end())
return kLoadLibAlreadyLoaded;
// TODO: !permanent case
std::string errMsg;
DyLibHandle dyLibHandle = platform::DLOpen(canonicalLoadedLib, &errMsg);
if (!dyLibHandle) {
// We emit callback to LibraryLoadingFailed when we get error with error message.
//TODO: Implement callbacks
LLVM_DEBUG(dbgs() << "DynamicLibraryManager::loadLibrary(): " << errMsg);
return kLoadLibLoadError;
}
std::pair<DyLibs::iterator, bool> insRes
= m_DyLibs.insert(std::pair<DyLibHandle, std::string>(dyLibHandle,
canonicalLoadedLib));
if (!insRes.second)
return kLoadLibAlreadyLoaded;
m_LoadedLibraries.insert(canonicalLoadedLib);
return kLoadLibSuccess;
#undef DEBUG_TYPE
}
void DynamicLibraryManager::unloadLibrary(StringRef libStem) {
#define DEBUG_TYPE "Dyld::unloadLibrary:"
std::string canonicalLoadedLib = lookupLibrary(libStem);
if (!isLibraryLoaded(canonicalLoadedLib))
return;
DyLibHandle dyLibHandle = nullptr;
for (DyLibs::const_iterator I = m_DyLibs.begin(), E = m_DyLibs.end();
I != E; ++I) {
if (I->second == canonicalLoadedLib) {
dyLibHandle = I->first;
break;
}
}
// TODO: !permanent case
std::string errMsg;
platform::DLClose(dyLibHandle, &errMsg);
if (!errMsg.empty()) {
LLVM_DEBUG(dbgs() << "cling::DynamicLibraryManager::unloadLibrary(): "
<< errMsg << '\n');
}
//TODO: Implement callbacks
m_DyLibs.erase(dyLibHandle);
m_LoadedLibraries.erase(canonicalLoadedLib);
#undef DEBUG_TYPE
}
bool DynamicLibraryManager::isLibraryLoaded(StringRef fullPath) const {
std::string canonPath = normalizePath(fullPath);
if (m_LoadedLibraries.find(canonPath) != m_LoadedLibraries.end())
return true;
return false;
}
void DynamicLibraryManager::dump(llvm::raw_ostream* S /*= nullptr*/) const {
llvm::raw_ostream &OS = S ? *S : llvm::outs();
// FIXME: print in a stable order the contents of m_SearchPaths
for (const auto& Info : getSearchPaths()) {
if (!Info.IsUser)
OS << "[system] ";
OS << Info.Path.c_str() << "\n";
}
}
#if defined(_WIN32)
static bool IsDLL(llvm::StringRef headers) {
using namespace llvm::support::endian;
uint32_t headeroffset = read32le(headers.data() + 0x3c);
auto peheader = headers.substr(headeroffset, 24);
if (peheader.size() != 24) {
return false;
}
// Read Characteristics from the coff header
uint32_t characteristics = read16le(peheader.data() + 22);
return (characteristics & llvm::COFF::IMAGE_FILE_DLL) != 0;
}
#endif
bool DynamicLibraryManager::isSharedLibrary(StringRef libFullPath,
bool* exists /*=0*/) {
using namespace llvm;
auto filetype = sys::fs::get_file_type(libFullPath, /*Follow*/ true);
if (filetype != sys::fs::file_type::regular_file) {
if (exists) {
// get_file_type returns status_error also in case of file_not_found.
*exists = filetype != sys::fs::file_type::status_error;
}
return false;
}
// Do not use the identify_magic overload taking a path: It will open the
// file and then mmap its contents, possibly causing bus errors when another
// process truncates the file while we are trying to read it. Instead just
// read the first 1024 bytes, which should be enough for identify_magic to
// do its work.
// TODO: Fix the code upstream and consider going back to calling the
// convenience function after a future LLVM upgrade.
std::string path = libFullPath.str();
std::ifstream in(path, std::ios::binary);
char header[1024] = {0};
in.read(header, sizeof(header));
if (in.fail()) {
if (exists)
*exists = false;
return false;
}
StringRef headerStr(header, in.gcount());
file_magic Magic = identify_magic(headerStr);
bool result =
#ifdef __APPLE__
(Magic == file_magic::macho_fixed_virtual_memory_shared_lib
|| Magic == file_magic::macho_dynamically_linked_shared_lib
|| Magic == file_magic::macho_dynamically_linked_shared_lib_stub
|| Magic == file_magic::macho_universal_binary)
#elif defined(LLVM_ON_UNIX)
#ifdef __CYGWIN__
(Magic == file_magic::pecoff_executable)
#else
(Magic == file_magic::elf_shared_object)
#endif
#elif defined(_WIN32)
// We should only include dll libraries without including executables,
// object code and others...
(Magic == file_magic::pecoff_executable && IsDLL(headerStr))
#else
# error "Unsupported platform."
#endif
;
return result;
}
} // end namespace Cpp