-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathDynamicLibraryManagerSymbol.cpp
1344 lines (1137 loc) · 47.8 KB
/
DynamicLibraryManagerSymbol.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
// author: Vassil Vassilev <[email protected]>
// author: Alexander Penev <[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 "Paths.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/Object/COFF.h"
#include "llvm/Object/ELF.h"
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Object/MachO.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/WithColor.h"
#include <algorithm>
#include <list>
#include <string>
#include <unordered_set>
#include <vector>
#ifdef LLVM_ON_UNIX
#include <dlfcn.h>
#include <unistd.h>
#include <sys/stat.h>
#endif // LLVM_ON_UNIX
#ifdef __APPLE__
#include <mach-o/dyld.h>
#include <sys/stat.h>
#undef LC_LOAD_DYLIB
#undef LC_RPATH
#endif // __APPLE__
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <libloaderapi.h> // For GetModuleFileNameA
#include <memoryapi.h> // For VirtualQuery
#endif
namespace {
using BasePath = std::string;
using namespace llvm;
// This is a GNU implementation of hash used in bloom filter!
static uint32_t GNUHash(StringRef S) {
uint32_t H = 5381;
for (uint8_t C : S)
H = (H << 5) + H + C;
return H;
}
constexpr uint32_t log2u(std::uint32_t n) {
return (n > 1) ? 1 + log2u(n >> 1) : 0;
}
struct BloomFilter {
// https://hur.st/bloomfilter
//
// n = ceil(m / (-k / log(1 - exp(log(p) / k))))
// p = pow(1 - exp(-k / (m / n)), k)
// m = ceil((n * log(p)) / log(1 / pow(2, log(2))));
// k = round((m / n) * log(2));
//
// n = symbolsCount
// p = 0.02
// k = 2 (k1=GNUHash and k2=GNUHash >> bloomShift)
// m = ceil((symbolsCount * log(p)) / log(1 / pow(2, log(2))));
// bloomShift = min(5 for bits=32 or 6 for bits=64, log2(symbolsCount))
// bloomSize = ceil((-1.44 * n * log2f(p)) / bits)
const int m_Bits = 8 * sizeof(uint64_t);
const float m_P = 0.02f;
bool m_IsInitialized = false;
uint32_t m_SymbolsCount = 0;
uint32_t m_BloomSize = 0;
uint32_t m_BloomShift = 0;
std::vector<uint64_t> m_BloomTable;
bool TestHash(uint32_t hash) const {
// This function is superhot. No branches here, breaks inlining and makes
// overall performance around 4x slower.
assert(m_IsInitialized && "Not yet initialized!");
uint32_t hash2 = hash >> m_BloomShift;
uint32_t n = (hash >> log2u(m_Bits)) % m_BloomSize;
uint64_t mask = ((1ULL << (hash % m_Bits)) | (1ULL << (hash2 % m_Bits)));
return (mask & m_BloomTable[n]) == mask;
}
void AddHash(uint32_t hash) {
assert(m_IsInitialized && "Not yet initialized!");
uint32_t hash2 = hash >> m_BloomShift;
uint32_t n = (hash >> log2u(m_Bits)) % m_BloomSize;
uint64_t mask = ((1ULL << (hash % m_Bits)) | (1ULL << (hash2 % m_Bits)));
m_BloomTable[n] |= mask;
}
void ResizeTable(uint32_t newSymbolsCount) {
assert(m_SymbolsCount == 0 && "Not supported yet!");
m_SymbolsCount = newSymbolsCount;
m_BloomSize = ceil((-1.44f * m_SymbolsCount * log2f(m_P)) / m_Bits);
m_BloomShift = std::min(6u, log2u(m_SymbolsCount));
m_BloomTable.resize(m_BloomSize);
}
};
/// An efficient representation of a full path to a library which does not
/// duplicate common path patterns reducing the overall memory footprint.
///
/// For example, `/home/.../lib/libA.so`, m_Path will contain a pointer
/// to `/home/.../lib/`
/// will be stored and .second `libA.so`.
/// This approach reduces the duplicate paths as at one location there may be
/// plenty of libraries.
struct LibraryPath {
const BasePath& m_Path;
std::string m_LibName;
BloomFilter m_Filter;
StringSet<> m_Symbols;
//std::vector<const LibraryPath*> m_LibDeps;
LibraryPath(const BasePath& Path, const std::string& LibName)
: m_Path(Path), m_LibName(LibName) {
}
bool operator==(const LibraryPath &other) const {
return (&m_Path == &other.m_Path || m_Path == other.m_Path) &&
m_LibName == other.m_LibName;
}
const std::string GetFullName() const {
SmallString<512> Vec(m_Path);
llvm::sys::path::append(Vec, StringRef(m_LibName));
return Vec.str().str();
}
void AddBloom(StringRef symbol) {
m_Filter.AddHash(GNUHash(symbol));
}
StringRef AddSymbol(const std::string& symbol) {
auto it = m_Symbols.insert(symbol);
return it.first->getKey();
}
bool hasBloomFilter() const {
return m_Filter.m_IsInitialized;
}
bool isBloomFilterEmpty() const {
assert(m_Filter.m_IsInitialized && "Bloom filter not initialized!");
return m_Filter.m_SymbolsCount == 0;
}
void InitializeBloomFilter(uint32_t newSymbolsCount) {
assert(!m_Filter.m_IsInitialized &&
"Cannot re-initialize non-empty filter!");
m_Filter.m_IsInitialized = true;
m_Filter.ResizeTable(newSymbolsCount);
}
bool MayExistSymbol(uint32_t hash) const {
// The library had no symbols and the bloom filter is empty.
if (isBloomFilterEmpty())
return false;
return m_Filter.TestHash(hash);
}
bool ExistSymbol(StringRef symbol) const {
return m_Symbols.find(symbol) != m_Symbols.end();
}
};
/// A helper class keeping track of loaded libraries. It implements a fast
/// search O(1) while keeping deterministic iterability in a memory efficient
/// way. The underlying set uses a custom hasher for better efficiency given the
/// specific problem where the library names (m_LibName) are relatively short
/// strings and the base paths (m_Path) are repetitive long strings.
class LibraryPaths {
struct LibraryPathHashFn {
size_t operator()(const LibraryPath& item) const {
return std::hash<size_t>()(item.m_Path.length()) ^
std::hash<std::string>()(item.m_LibName);
}
};
std::vector<const LibraryPath*> m_Libs;
std::unordered_set<LibraryPath, LibraryPathHashFn> m_LibsH;
public:
bool HasRegisteredLib(const LibraryPath& Lib) const {
return m_LibsH.count(Lib);
}
const LibraryPath* GetRegisteredLib(const LibraryPath& Lib) const {
auto search = m_LibsH.find(Lib);
if (search != m_LibsH.end())
return &(*search);
return nullptr;
}
const LibraryPath* RegisterLib(const LibraryPath& Lib) {
auto it = m_LibsH.insert(Lib);
assert(it.second && "Already registered!");
m_Libs.push_back(&*it.first);
return &*it.first;
}
void UnregisterLib(const LibraryPath& Lib) {
auto found = m_LibsH.find(Lib);
if (found == m_LibsH.end())
return;
m_Libs.erase(std::find(m_Libs.begin(), m_Libs.end(), &*found));
m_LibsH.erase(found);
}
size_t size() const {
assert(m_Libs.size() == m_LibsH.size());
return m_Libs.size();
}
const std::vector<const LibraryPath*>& GetLibraries() const {
return m_Libs;
}
};
#ifndef _WIN32
// Cached version of system function lstat
static inline mode_t cached_lstat(const char *path) {
static StringMap<mode_t> lstat_cache;
// If already cached - return cached result
auto it = lstat_cache.find(path);
if (it != lstat_cache.end())
return it->second;
// If result not in cache - call system function and cache result
struct stat buf;
mode_t st_mode = (lstat(path, &buf) == -1) ? 0 : buf.st_mode;
lstat_cache.insert(std::pair<StringRef, mode_t>(path, st_mode));
return st_mode;
}
// Cached version of system function readlink
static inline StringRef cached_readlink(const char* pathname) {
static StringMap<std::string> readlink_cache;
// If already cached - return cached result
auto it = readlink_cache.find(pathname);
if (it != readlink_cache.end())
return StringRef(it->second);
// If result not in cache - call system function and cache result
char buf[PATH_MAX];
ssize_t len;
if ((len = readlink(pathname, buf, sizeof(buf))) != -1) {
buf[len] = '\0';
std::string s(buf);
readlink_cache.insert(std::pair<StringRef, std::string>(pathname, s));
return readlink_cache[pathname];
}
return "";
}
#endif
// Cached version of system function realpath
std::string cached_realpath(StringRef path, StringRef base_path = "",
bool is_base_path_real = false,
long symlooplevel = 40) {
if (path.empty()) {
errno = ENOENT;
return "";
}
if (!symlooplevel) {
errno = ELOOP;
return "";
}
// If already cached - return cached result
static StringMap<std::pair<std::string,int>> cache;
bool relative_path = llvm::sys::path::is_relative(path);
if (!relative_path) {
auto it = cache.find(path);
if (it != cache.end()) {
errno = it->second.second;
return it->second.first;
}
}
// If result not in cache - call system function and cache result
StringRef sep(llvm::sys::path::get_separator());
SmallString<256> result(sep);
#ifndef _WIN32
SmallVector<StringRef, 16> p;
// Relative or absolute path
if (relative_path) {
if (is_base_path_real) {
result.assign(base_path);
} else {
if (path[0] == '~' && (path.size() == 1 || llvm::sys::path::is_separator(path[1]))) {
static SmallString<128> home;
if (home.str().empty())
llvm::sys::path::home_directory(home);
StringRef(home).split(p, sep, /*MaxSplit*/ -1, /*KeepEmpty*/ false);
} else if (base_path.empty()) {
static SmallString<256> current_path;
if (current_path.str().empty())
llvm::sys::fs::current_path(current_path);
StringRef(current_path).split(p, sep, /*MaxSplit*/ -1, /*KeepEmpty*/ false);
} else {
base_path.split(p, sep, /*MaxSplit*/ -1, /*KeepEmpty*/ false);
}
}
}
path.split(p, sep, /*MaxSplit*/ -1, /*KeepEmpty*/ false);
// Handle path list items
for (auto item : p) {
if (item == ".")
continue; // skip "." element in "abc/./def"
if (item == "..") {
// collapse "a/b/../c" to "a/c"
size_t s = result.rfind(sep);
if (s != llvm::StringRef::npos)
result.resize(s);
if (result.empty())
result = sep;
continue;
}
size_t old_size = result.size();
llvm::sys::path::append(result, item);
mode_t st_mode = cached_lstat(result.c_str());
if (S_ISLNK(st_mode)) {
StringRef symlink = cached_readlink(result.c_str());
if (llvm::sys::path::is_relative(symlink)) {
result.resize(old_size);
result = cached_realpath(symlink, result, true, symlooplevel - 1);
} else {
result = cached_realpath(symlink, "", true, symlooplevel - 1);
}
} else if (st_mode == 0) {
cache.insert(std::pair<StringRef, std::pair<std::string,int>>(
path,
std::pair<std::string,int>("",ENOENT))
);
errno = ENOENT;
return "";
}
}
#else
llvm::sys::fs::real_path(path, result);
#endif
cache.insert(std::pair<StringRef, std::pair<std::string,int>>(
path,
std::pair<std::string,int>(result.str().str(),errno))
);
return result.str().str();
}
using namespace llvm;
using namespace llvm::object;
template <class ELFT>
static Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT>* Elf) {
auto DynamicEntriesOrError = Elf->dynamicEntries();
if (!DynamicEntriesOrError)
return DynamicEntriesOrError.takeError();
for (const typename ELFT::Dyn& Dyn : *DynamicEntriesOrError) {
if (Dyn.d_tag == ELF::DT_STRTAB) {
auto MappedAddrOrError = Elf->toMappedAddr(Dyn.getPtr());
if (!MappedAddrOrError)
return MappedAddrOrError.takeError();
return StringRef(reinterpret_cast<const char *>(*MappedAddrOrError));
}
}
// If the dynamic segment is not present, we fall back on the sections.
auto SectionsOrError = Elf->sections();
if (!SectionsOrError)
return SectionsOrError.takeError();
for (const typename ELFT::Shdr &Sec : *SectionsOrError) {
if (Sec.sh_type == ELF::SHT_DYNSYM)
return Elf->getStringTableForSymtab(Sec);
}
return createError("dynamic string table not found");
}
static StringRef GetGnuHashSection(llvm::object::ObjectFile *file) {
for (auto S : file->sections()) {
StringRef name = llvm::cantFail(S.getName());
if (name == ".gnu.hash") {
return llvm::cantFail(S.getContents());
}
}
return "";
}
/// Bloom filter is a stochastic data structure which can tell us if a symbol
/// name does not exist in a library with 100% certainty. If it tells us it
/// exists this may not be true:
/// https://blogs.oracle.com/solaris/gnu-hash-elf-sections-v2
///
/// ELF has this optimization in the new linkers by default, It is stored in the
/// gnu.hash section of the object file.
///
///\returns true if the symbol may be in the library.
static bool MayExistInElfObjectFile(llvm::object::ObjectFile *soFile,
uint32_t hash) {
assert(soFile->isELF() && "Not ELF");
// Compute the platform bitness -- either 64 or 32.
const unsigned bits = 8 * soFile->getBytesInAddress();
StringRef contents = GetGnuHashSection(soFile);
if (contents.size() < 16)
// We need to search if the library doesn't have .gnu.hash section!
return true;
const char* hashContent = contents.data();
// See https://flapenguin.me/2017/05/10/elf-lookup-dt-gnu-hash/ for .gnu.hash
// table layout.
uint32_t maskWords = *reinterpret_cast<const uint32_t *>(hashContent + 8);
uint32_t shift2 = *reinterpret_cast<const uint32_t *>(hashContent + 12);
uint32_t hash2 = hash >> shift2;
uint32_t n = (hash / bits) % maskWords;
const char *bloomfilter = hashContent + 16;
const char *hash_pos = bloomfilter + n*(bits/8); // * (Bits / 8)
uint64_t word = *reinterpret_cast<const uint64_t *>(hash_pos);
uint64_t bitmask = ( (1ULL << (hash % bits)) | (1ULL << (hash2 % bits)));
return (bitmask & word) == bitmask;
}
} // anon namespace
// This function isn't referenced outside its translation unit, but it
// can't use the "static" keyword because its address is used for
// GetMainExecutable (since some platforms don't support taking the
// address of main, and some platforms can't implement GetMainExecutable
// without being given the address of a function in the main executable).
std::string GetExecutablePath() {
// This just needs to be some symbol in the binary; C++ doesn't
// allow taking the address of ::main however.
return Cpp::DynamicLibraryManager::getSymbolLocation(&GetExecutablePath);
}
namespace Cpp {
class Dyld {
struct BasePathHashFunction {
size_t operator()(const BasePath& item) const {
return std::hash<std::string>()(item);
}
};
struct BasePathEqFunction {
size_t operator()(const BasePath& l, const BasePath& r) const {
return &l == &r || l == r;
}
};
/// A memory efficient llvm::VectorSet. The class provides O(1) search
/// complexity. It is tuned to compare BasePaths first by checking the
/// address and then the representation which models the base path reuse.
class BasePaths {
public:
std::unordered_set<BasePath, BasePathHashFunction,
BasePathEqFunction> m_Paths;
public:
const BasePath& RegisterBasePath(const std::string& Path,
bool* WasInserted = nullptr) {
auto it = m_Paths.insert(Path);
if (WasInserted)
*WasInserted = it.second;
return *it.first;
}
bool Contains(StringRef Path) {
return m_Paths.count(Path.str());
}
};
bool m_FirstRun = true;
bool m_FirstRunSysLib = true;
bool m_UseBloomFilter = true;
bool m_UseHashTable = true;
const Cpp::DynamicLibraryManager& m_DynamicLibraryManager;
/// The basename of `/home/.../lib/libA.so`,
/// m_BasePaths will contain `/home/.../lib/`
BasePaths m_BasePaths;
LibraryPaths m_Libraries;
LibraryPaths m_SysLibraries;
/// Contains a set of libraries which we gave to the user via ResolveSymbol
/// call and next time we should check if the user loaded them to avoid
/// useless iterations.
LibraryPaths m_QueriedLibraries;
using PermanentlyIgnoreCallbackProto = std::function<bool(StringRef)>;
const PermanentlyIgnoreCallbackProto m_ShouldPermanentlyIgnoreCallback;
const StringRef m_ExecutableFormat;
/// Scan for shared objects which are not yet loaded. They are a our symbol
/// resolution candidate sources.
/// NOTE: We only scan not loaded shared objects.
/// \param[in] searchSystemLibraries - whether to decent to standard system
/// locations for shared objects.
void ScanForLibraries(bool searchSystemLibraries = false);
/// Builds a bloom filter lookup optimization.
void BuildBloomFilter(LibraryPath* Lib, llvm::object::ObjectFile *BinObjFile,
unsigned IgnoreSymbolFlags = 0) const;
/// Looks up symbols from a an object file, representing the library.
///\param[in] Lib - full path to the library.
///\param[in] mangledName - the mangled name to look for.
///\param[in] IgnoreSymbolFlags - The symbols to ignore upon a match.
///\returns true on success.
bool ContainsSymbol(const LibraryPath* Lib, StringRef mangledName,
unsigned IgnoreSymbolFlags = 0) const;
bool ShouldPermanentlyIgnore(StringRef FileName) const;
void dumpDebugInfo() const;
public:
Dyld(const Cpp::DynamicLibraryManager &DLM,
PermanentlyIgnoreCallbackProto shouldIgnore,
StringRef execFormat)
: m_DynamicLibraryManager(DLM),
m_ShouldPermanentlyIgnoreCallback(shouldIgnore),
m_ExecutableFormat(execFormat) { }
~Dyld(){};
std::string searchLibrariesForSymbol(StringRef mangledName,
bool searchSystem);
};
std::string RPathToStr(SmallVector<StringRef,2> V) {
std::string result;
for (auto item : V)
result += item.str() + ",";
if (!result.empty())
result.pop_back();
return result;
}
template <class ELFT>
void HandleDynTab(const ELFFile<ELFT>* Elf, StringRef FileName,
SmallVector<StringRef,2>& RPath,
SmallVector<StringRef,2>& RunPath,
std::vector<StringRef>& Deps,
bool& isPIEExecutable) {
#define DEBUG_TYPE "Dyld:"
const char *Data = "";
if (Expected<StringRef> StrTabOrErr = getDynamicStrTab(Elf))
Data = StrTabOrErr.get().data();
isPIEExecutable = false;
auto DynamicEntriesOrError = Elf->dynamicEntries();
if (!DynamicEntriesOrError) {
LLVM_DEBUG(dbgs() << "Dyld: failed to read dynamic entries in"
<< "'" << FileName.str() << "'\n");
return;
}
for (const typename ELFT::Dyn& Dyn : *DynamicEntriesOrError) {
switch (Dyn.d_tag) {
case ELF::DT_NEEDED:
Deps.push_back(Data + Dyn.d_un.d_val);
break;
case ELF::DT_RPATH:
SplitPaths(Data + Dyn.d_un.d_val, RPath, utils::kAllowNonExistent,
Cpp::utils::platform::kEnvDelim, false);
break;
case ELF::DT_RUNPATH:
SplitPaths(Data + Dyn.d_un.d_val, RunPath, utils::kAllowNonExistent,
Cpp::utils::platform::kEnvDelim, false);
break;
case ELF::DT_FLAGS_1:
// Check if this is not a pie executable.
if (Dyn.d_un.d_val & llvm::ELF::DF_1_PIE)
isPIEExecutable = true;
break;
// (Dyn.d_tag == ELF::DT_NULL) continue;
// (Dyn.d_tag == ELF::DT_AUXILIARY || Dyn.d_tag == ELF::DT_FILTER)
}
}
#undef DEBUG_TYPE
}
void Dyld::ScanForLibraries(bool searchSystemLibraries/* = false*/) {
#define DEBUG_TYPE "Dyld:ScanForLibraries:"
const auto &searchPaths = m_DynamicLibraryManager.getSearchPaths();
LLVM_DEBUG(dbgs() << "Dyld::ScanForLibraries: system=" << (searchSystemLibraries?"true":"false") << "\n");
for (const DynamicLibraryManager::SearchPathInfo &Info : searchPaths)
LLVM_DEBUG(dbgs() << ">>>" << Info.Path << ", " << (Info.IsUser?"user\n":"system\n"));
llvm::SmallSet<const BasePath*, 32> ScannedPaths;
for (const DynamicLibraryManager::SearchPathInfo &Info : searchPaths) {
if (Info.IsUser != searchSystemLibraries) {
// Examples which we should handle.
// File Real
// /lib/1/1.so /lib/1/1.so // file
// /lib/1/2.so->/lib/1/1.so /lib/1/1.so // file local link
// /lib/1/3.so->/lib/3/1.so /lib/3/1.so // file external link
// /lib/2->/lib/1 // path link
// /lib/2/1.so /lib/1/1.so // path link, file
// /lib/2/2.so->/lib/1/1.so /lib/1/1.so // path link, file local link
// /lib/2/3.so->/lib/3/1.so /lib/3/1.so // path link, file external link
//
// /lib/3/1.so
// /lib/3/2.so->/system/lib/s.so
// /lib/3/3.so
// /system/lib/1.so
//
// libL.so NEEDED/RPATH libR.so /lib/some-rpath/libR.so // needed/dependedt library in libL.so RPATH/RUNPATH or other (in)direct dep
//
// Paths = /lib/1 : /lib/2 : /lib/3
// m_BasePaths = ["/lib/1", "/lib/3", "/system/lib"]
// m_*Libraries = [<0,"1.so">, <1,"1.so">, <2,"s.so">, <1,"3.so">]
LLVM_DEBUG(dbgs() << "Dyld::ScanForLibraries Iter:" << Info.Path << " -> ");
std::string RealPath = cached_realpath(Info.Path);
llvm::StringRef DirPath(RealPath);
LLVM_DEBUG(dbgs() << RealPath << "\n");
if (!llvm::sys::fs::is_directory(DirPath) || DirPath.empty())
continue;
// Already searched?
const BasePath &ScannedBPath = m_BasePaths.RegisterBasePath(RealPath);
if (ScannedPaths.count(&ScannedBPath)) {
LLVM_DEBUG(dbgs() << "Dyld::ScanForLibraries Already scanned: " << RealPath << "\n");
continue;
}
// FileName must be always full/absolute/resolved file name.
std::function<void(llvm::StringRef, unsigned)> HandleLib =
[&](llvm::StringRef FileName, unsigned level) {
LLVM_DEBUG(dbgs() << "Dyld::ScanForLibraries HandleLib:" << FileName.str()
<< ", level=" << level << " -> ");
llvm::StringRef FileRealPath = llvm::sys::path::parent_path(FileName);
llvm::StringRef FileRealName = llvm::sys::path::filename(FileName);
const BasePath& BaseP =
m_BasePaths.RegisterBasePath(FileRealPath.str());
LibraryPath LibPath(BaseP, FileRealName.str()); //bp, str
if (m_SysLibraries.GetRegisteredLib(LibPath) ||
m_Libraries.GetRegisteredLib(LibPath)) {
LLVM_DEBUG(dbgs() << "Already handled!!!\n");
return;
}
if (ShouldPermanentlyIgnore(FileName)) {
LLVM_DEBUG(dbgs() << "PermanentlyIgnored!!!\n");
return;
}
if (searchSystemLibraries)
m_SysLibraries.RegisterLib(LibPath);
else
m_Libraries.RegisterLib(LibPath);
// Handle lib dependencies
llvm::SmallVector<llvm::StringRef, 2> RPath;
llvm::SmallVector<llvm::StringRef, 2> RunPath;
std::vector<StringRef> Deps;
auto ObjFileOrErr =
llvm::object::ObjectFile::createObjectFile(FileName);
if (llvm::Error Err = ObjFileOrErr.takeError()) {
std::string Message;
handleAllErrors(std::move(Err), [&](llvm::ErrorInfoBase &EIB) {
Message += EIB.message() + "; ";
});
LLVM_DEBUG(dbgs() << "Dyld::ScanForLibraries: Failed to read object file "
<< FileName.str() << " Errors: " << Message << "\n");
return;
}
llvm::object::ObjectFile *BinObjF = ObjFileOrErr.get().getBinary();
if (BinObjF->isELF()) {
bool isPIEExecutable = false;
if (const auto* ELF = dyn_cast<ELF32LEObjectFile>(BinObjF))
HandleDynTab(&ELF->getELFFile(), FileName, RPath, RunPath, Deps,
isPIEExecutable);
else if (const auto* ELF = dyn_cast<ELF32BEObjectFile>(BinObjF))
HandleDynTab(&ELF->getELFFile(), FileName, RPath, RunPath, Deps,
isPIEExecutable);
else if (const auto* ELF = dyn_cast<ELF64LEObjectFile>(BinObjF))
HandleDynTab(&ELF->getELFFile(), FileName, RPath, RunPath, Deps,
isPIEExecutable);
else if (const auto* ELF = dyn_cast<ELF64BEObjectFile>(BinObjF))
HandleDynTab(&ELF->getELFFile(), FileName, RPath, RunPath, Deps,
isPIEExecutable);
if ((level == 0) && isPIEExecutable) {
if (searchSystemLibraries)
m_SysLibraries.UnregisterLib(LibPath);
else
m_Libraries.UnregisterLib(LibPath);
return;
}
} else if (BinObjF->isMachO()) {
MachOObjectFile *Obj = (MachOObjectFile*)BinObjF;
for (const auto &Command : Obj->load_commands()) {
if (Command.C.cmd == MachO::LC_LOAD_DYLIB) {
//Command.C.cmd == MachO::LC_ID_DYLIB ||
//Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
//Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
//Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
//Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB ||
MachO::dylib_command dylibCmd =
Obj->getDylibIDLoadCommand(Command);
Deps.push_back(StringRef(Command.Ptr + dylibCmd.dylib.name));
}
else if (Command.C.cmd == MachO::LC_RPATH) {
MachO::rpath_command rpathCmd = Obj->getRpathCommand(Command);
SplitPaths(Command.Ptr + rpathCmd.path, RPath,
utils::kAllowNonExistent,
Cpp::utils::platform::kEnvDelim, false);
}
}
} else if (BinObjF->isCOFF()) {
// TODO: COFF support
}
LLVM_DEBUG(dbgs() << "Dyld::ScanForLibraries: Deps Info:\n");
LLVM_DEBUG(dbgs() << "Dyld::ScanForLibraries: RPATH=" << RPathToStr(RPath) << "\n");
LLVM_DEBUG(dbgs() << "Dyld::ScanForLibraries: RUNPATH=" << RPathToStr(RunPath) << "\n");
int x = 0;
for (StringRef dep : Deps)
LLVM_DEBUG(dbgs() << "Dyld::ScanForLibraries: Deps[" << x++ << "]=" << dep.str() << "\n");
// Heuristics for workaround performance problems:
// (H1) If RPATH and RUNPATH == "" -> skip handling Deps
if (RPath.empty() && RunPath.empty()) {
LLVM_DEBUG(dbgs() << "Dyld::ScanForLibraries: Skip all deps by Heuristic1: " << FileName.str() << "\n");
return;
};
// (H2) If RPATH subset of LD_LIBRARY_PATH &&
// RUNPATH subset of LD_LIBRARY_PATH -> skip handling Deps
if (std::all_of(RPath.begin(), RPath.end(), [&](StringRef item){ return std::any_of(searchPaths.begin(), searchPaths.end(), [&](DynamicLibraryManager::SearchPathInfo item1){ return item==item1.Path; }); }) &&
std::all_of(RunPath.begin(), RunPath.end(), [&](StringRef item){ return std::any_of(searchPaths.begin(), searchPaths.end(), [&](DynamicLibraryManager::SearchPathInfo item1){ return item==item1.Path; }); }) ) {
LLVM_DEBUG(dbgs() << "Dyld::ScanForLibraries: Skip all deps by Heuristic2: " << FileName.str() << "\n");
return;
}
// Handle dependencies
for (StringRef dep : Deps) {
std::string dep_full =
m_DynamicLibraryManager.lookupLibrary(dep, RPath, RunPath, FileName, false);
HandleLib(dep_full, level + 1);
}
};
LLVM_DEBUG(dbgs() << "Dyld::ScanForLibraries: Iterator: " << DirPath << "\n");
std::error_code EC;
for (llvm::sys::fs::directory_iterator DirIt(DirPath, EC), DirEnd;
DirIt != DirEnd && !EC; DirIt.increment(EC)) {
LLVM_DEBUG(dbgs() << "Dyld::ScanForLibraries: Iterator >>> "
<< DirIt->path() << ", type="
<< (short)(DirIt->type()) << "\n");
const llvm::sys::fs::file_type ft = DirIt->type();
if (ft == llvm::sys::fs::file_type::regular_file) {
HandleLib(DirIt->path(), 0);
} else if (ft == llvm::sys::fs::file_type::symlink_file) {
std::string DepFileName_str = cached_realpath(DirIt->path());
llvm::StringRef DepFileName = DepFileName_str;
assert(!llvm::sys::fs::is_symlink_file(DepFileName));
if (!llvm::sys::fs::is_directory(DepFileName))
HandleLib(DepFileName, 0);
}
}
// Register the DirPath as fully scanned.
ScannedPaths.insert(&ScannedBPath);
}
}
#undef DEBUG_TYPE
}
void Dyld::BuildBloomFilter(LibraryPath* Lib,
llvm::object::ObjectFile *BinObjFile,
unsigned IgnoreSymbolFlags /*= 0*/) const {
#define DEBUG_TYPE "Dyld::BuildBloomFilter:"
assert(m_UseBloomFilter && "Bloom filter is disabled");
assert(!Lib->hasBloomFilter() && "Already built!");
using namespace llvm;
using namespace llvm::object;
LLVM_DEBUG(dbgs() << "Dyld::BuildBloomFilter: Start building Bloom filter for: "
<< Lib->GetFullName() << "\n");
// If BloomFilter is empty then build it.
// Count Symbols and generate BloomFilter
uint32_t SymbolsCount = 0;
std::list<llvm::StringRef> symbols;
for (const llvm::object::SymbolRef &S : BinObjFile->symbols()) {
uint32_t Flags = llvm::cantFail(S.getFlags());
// Do not insert in the table symbols flagged to ignore.
if (Flags & IgnoreSymbolFlags)
continue;
// Note, we are at last resort and loading library based on a weak
// symbol is allowed. Otherwise, the JIT will issue an unresolved
// symbol error.
//
// There are other weak symbol kinds (marked as 'V') to denote
// typeinfo and vtables. It is unclear whether we should load such
// libraries or from which library we should resolve the symbol.
// We seem to not have a way to differentiate it from the symbol API.
llvm::Expected<llvm::StringRef> SymNameErr = S.getName();
if (!SymNameErr) {
LLVM_DEBUG(dbgs() << "Dyld::BuildBloomFilter: Failed to read symbol "
<< SymNameErr.get() << "\n");
continue;
}
if (SymNameErr.get().empty())
continue;
++SymbolsCount;
symbols.push_back(SymNameErr.get());
}
if (BinObjFile->isELF()) {
// ELF file format has .dynstr section for the dynamic symbol table.
const auto *ElfObj = cast<llvm::object::ELFObjectFileBase>(BinObjFile);
for (const object::SymbolRef &S : ElfObj->getDynamicSymbolIterators()) {
uint32_t Flags = llvm::cantFail(S.getFlags());
// DO NOT insert to table if symbol was undefined
if (Flags & llvm::object::SymbolRef::SF_Undefined)
continue;
// Note, we are at last resort and loading library based on a weak
// symbol is allowed. Otherwise, the JIT will issue an unresolved
// symbol error.
//
// There are other weak symbol kinds (marked as 'V') to denote
// typeinfo and vtables. It is unclear whether we should load such
// libraries or from which library we should resolve the symbol.
// We seem to not have a way to differentiate it from the symbol API.
llvm::Expected<StringRef> SymNameErr = S.getName();
if (!SymNameErr) {
LLVM_DEBUG(dbgs() << "Dyld::BuildBloomFilter: Failed to read symbol "
<< SymNameErr.get() << "\n");
continue;
}
if (SymNameErr.get().empty())
continue;
++SymbolsCount;
symbols.push_back(SymNameErr.get());
}
}
else if (BinObjFile->isCOFF()) { // On Windows, the symbols are present in COFF format.
llvm::object::COFFObjectFile* CoffObj = cast<llvm::object::COFFObjectFile>(BinObjFile);
// In COFF, the symbols are not present in the SymbolTable section
// of the Object file. They are present in the ExportDirectory section.
for (auto I=CoffObj->export_directory_begin(),
E=CoffObj->export_directory_end(); I != E; I = ++I) {
// All the symbols are already flagged as exported.
// We cannot really ignore symbols based on flags as we do on unix.
StringRef Name;
auto Err = I->getSymbolName(Name);
if (Err) {
std::string Message;
handleAllErrors(std::move(Err), [&](llvm::ErrorInfoBase& EIB) {
Message += EIB.message() + "; ";
});
LLVM_DEBUG(dbgs() << "Dyld::BuildBloomFilter: Failed to read symbol "
<< Message << "\n");
continue;
}
if (Name.empty())
continue;
++SymbolsCount;
symbols.push_back(Name);
}
}
Lib->InitializeBloomFilter(SymbolsCount);
if (!SymbolsCount) {
LLVM_DEBUG(dbgs() << "Dyld::BuildBloomFilter: No symbols!\n");
return;
}
LLVM_DEBUG(dbgs() << "Dyld::BuildBloomFilter: Symbols:\n");
for (auto it : symbols)
LLVM_DEBUG(dbgs() << "Dyld::BuildBloomFilter" << "- " << it << "\n");
// Generate BloomFilter
for (const auto &S : symbols) {
if (m_UseHashTable)
Lib->AddBloom(Lib->AddSymbol(S.str()));
else
Lib->AddBloom(S);
}
#undef DEBUG_TYPE
}
bool Dyld::ContainsSymbol(const LibraryPath* Lib,
StringRef mangledName,
unsigned IgnoreSymbolFlags /*= 0*/) const {
#define DEBUG_TYPE "Dyld::ContainsSymbol:"
const std::string library_filename = Lib->GetFullName();
LLVM_DEBUG(dbgs() << "Dyld::ContainsSymbol: Find symbol: lib="
<< library_filename << ", mangled="
<< mangledName.str() << "\n");
auto ObjF = llvm::object::ObjectFile::createObjectFile(library_filename);
if (llvm::Error Err = ObjF.takeError()) {
std::string Message;
handleAllErrors(std::move(Err), [&](llvm::ErrorInfoBase &EIB) {
Message += EIB.message() + "; ";
});
LLVM_DEBUG(dbgs() << "Dyld::ContainsSymbol: Failed to read object file "
<< library_filename << " Errors: " << Message << "\n");
return false;
}
llvm::object::ObjectFile *BinObjFile = ObjF.get().getBinary();
uint32_t hashedMangle = GNUHash(mangledName);
// Check for the gnu.hash section if ELF.
// If the symbol doesn't exist, exit early.
if (BinObjFile->isELF() &&
!MayExistInElfObjectFile(BinObjFile, hashedMangle)) {
LLVM_DEBUG(dbgs() << "Dyld::ContainsSymbol: ELF BloomFilter: Skip symbol <" << mangledName.str() << ">.\n");
return false;
}
if (m_UseBloomFilter) {
// Use our bloom filters and create them if necessary.
if (!Lib->hasBloomFilter())
BuildBloomFilter(const_cast<LibraryPath*>(Lib), BinObjFile,
IgnoreSymbolFlags);
// If the symbol does not exist, exit early. In case it may exist, iterate.
if (!Lib->MayExistSymbol(hashedMangle)) {
LLVM_DEBUG(dbgs() << "Dyld::ContainsSymbol: BloomFilter: Skip symbol <" << mangledName.str() << ">.\n");
return false;
}