LLVM 24.0.0git
LibraryResolver.cpp
Go to the documentation of this file.
1//===- LibraryResolver.cpp - Library Resolution of Unresolved Symbols ---===//
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//
9// Library resolution impl for unresolved symbols
10//
11//===----------------------------------------------------------------------===//
12
15
16#include "llvm/Object/COFF.h"
19#include "llvm/Support/DJB.h"
20#include "llvm/Support/Error.h"
21
22#define DEBUG_TYPE "orc-resolver"
23
24namespace llvm::orc {
25
27 : LibMgr(LibraryManager()),
28 LibPathCache(std::make_shared<LibraryPathCache>()),
29 LibPathResolver(std::make_shared<PathResolver>(LibPathCache)),
30 ScanHelper(S.BasePaths, LibPathCache, LibPathResolver),
31 FB(S.FilterBuilder),
32 ShouldScanCall(S.ShouldScanCall ? S.ShouldScanCall
33 : [](StringRef) -> bool { return true; }),
34 scanBatchSize(S.ScanBatchSize) {
35
36 if (!ScanHelper.hasSearchPath()) {
37 LLVM_DEBUG(dbgs() << "Warning: No base paths provided for scanning.\n");
38 }
39}
40
41std::unique_ptr<LibraryResolutionDriver>
43 auto LR = std::make_unique<LibraryResolver>(S);
44 return std::unique_ptr<LibraryResolutionDriver>(
45 new LibraryResolutionDriver(std::move(LR)));
46}
47
48void LibraryResolutionDriver::addScanPath(const std::string &Path, PathType K) {
49 LR->ScanHelper.addBasePath(Path, K);
50}
51
53 LR->LibMgr.markLoaded(Path);
54}
55
57 LR->LibMgr.markUnloaded(Path);
58}
59
62 const SearchConfig &Config) {
63 LR->searchSymbolsInLibraries(Symbols, std::move(OnCompletion), Config);
64}
65
67 uint32_t IgnoreFlags) {
68 Expected<uint32_t> FlagsOrErr = Sym.getFlags();
69 if (!FlagsOrErr) {
70 consumeError(FlagsOrErr.takeError());
71 return true;
72 }
73
74 uint32_t Flags = *FlagsOrErr;
75
77 if ((IgnoreFlags & Filter::IgnoreUndefined) &&
79 return true;
80 if ((IgnoreFlags & Filter::IgnoreNonExported) &&
82 return true;
83 if ((IgnoreFlags & Filter::IgnoreNonGlobal) &&
85 return true;
86 if ((IgnoreFlags & Filter::IgnoreHidden) &&
88 return true;
89 if ((IgnoreFlags & Filter::IgnoreIndirect) &&
91 return true;
92 if ((IgnoreFlags & Filter::IgnoreWeak) &&
94 return true;
95
96 return false;
97}
98
100 OnEachSymbolFn OnEach,
101 const SymbolEnumeratorOptions &Opts) {
102 if (!Obj)
103 return false;
104
105 auto processSymbolRange =
107 for (const auto &Sym : Range) {
108 if (shouldIgnoreSymbol(Sym, Opts.FilterFlags))
109 continue;
110
111 auto NameOrErr = Sym.getName();
112 if (!NameOrErr) {
113 consumeError(NameOrErr.takeError());
114 continue;
115 }
116
117 StringRef Name = *NameOrErr;
118 if (Name.empty())
119 continue;
120
121 EnumerateResult Res = OnEach(Name);
122 if (Res != EnumerateResult::Continue)
123 return Res;
124 }
125 return EnumerateResult::Continue;
126 };
127
128 EnumerateResult Res = processSymbolRange(Obj->symbols());
129 if (Res != EnumerateResult::Continue)
130 return Res == EnumerateResult::Stop;
131
132 if (Obj->isELF()) {
133 const auto *ElfObj = cast<object::ELFObjectFileBase>(Obj);
134 Res = processSymbolRange(ElfObj->getDynamicSymbolIterators());
135 if (Res != EnumerateResult::Continue)
136 return Res == EnumerateResult::Stop;
137 } else if (Obj->isCOFF()) {
138 const auto *CoffObj = cast<object::COFFObjectFile>(Obj);
139 for (auto I = CoffObj->export_directory_begin(),
140 E = CoffObj->export_directory_end();
141 I != E; ++I) {
142 StringRef Name;
143 if (I->getSymbolName(Name))
144 continue;
145 if (Name.empty())
146 continue;
147
148 EnumerateResult Res = OnEach(Name);
149 if (Res != EnumerateResult::Continue)
150 return Res == EnumerateResult::Stop;
151 }
152 } else if (Obj->isMachO()) {
153 }
154
155 return true;
156}
157
159 const SymbolEnumeratorOptions &Opts) {
160 ObjectFileLoader ObjLoader(Path);
161
162 auto ObjOrErr = ObjLoader.getObjectFile();
163 if (!ObjOrErr) {
164 std::string ErrMsg;
165 handleAllErrors(ObjOrErr.takeError(),
166 [&](const ErrorInfoBase &EIB) { ErrMsg = EIB.message(); });
167 LLVM_DEBUG(dbgs() << "Failed loading object file: " << Path
168 << "\nError: " << ErrMsg << "\n");
169 return false;
170 }
171
172 return SymbolEnumerator::enumerateSymbols(&ObjOrErr.get(), OnEach, Opts);
173}
174
176 for (auto S : file->sections()) {
177 StringRef name = llvm::cantFail(S.getName());
178 if (name == ".gnu.hash") {
179 return llvm::cantFail(S.getContents());
180 }
181 }
182 return "";
183}
184
185/// Bloom filter is a stochastic data structure which can tell us if a symbol
186/// name does not exist in a library with 100% certainty. If it tells us it
187/// exists this may not be true:
188/// https://blogs.oracle.com/solaris/gnu-hash-elf-sections-v2
189///
190/// ELF has this optimization in the new linkers by default, It is stored in the
191/// gnu.hash section of the object file.
192///
193///\returns true if the symbol may be in the library.
195 StringRef Sym) {
196 assert(soFile->isELF() && "Not ELF");
197
198 uint32_t hash = djbHash(Sym);
199 // Compute the platform bitness -- either 64 or 32.
200 const unsigned bits = 8 * soFile->getBytesInAddress();
201
202 StringRef contents = GetGnuHashSection(soFile);
203 if (contents.size() < 16)
204 // We need to search if the library doesn't have .gnu.hash section!
205 return true;
206 const char *hashContent = contents.data();
207
208 // See https://flapenguin.me/2017/05/10/elf-lookup-dt-gnu-hash/ for .gnu.hash
209 // table layout.
210 uint32_t maskWords = *reinterpret_cast<const uint32_t *>(hashContent + 8);
211 uint32_t shift2 = *reinterpret_cast<const uint32_t *>(hashContent + 12);
212 uint32_t hash2 = hash >> shift2;
213 uint32_t n = (hash / bits) % maskWords;
214
215 const char *bloomfilter = hashContent + 16;
216 const char *hash_pos = bloomfilter + n * (bits / 8); // * (Bits / 8)
217 uint64_t word = *reinterpret_cast<const uint64_t *>(hash_pos);
218 uint64_t bitmask = ((1ULL << (hash % bits)) | (1ULL << (hash2 % bits)));
219 return (bitmask & word) == bitmask;
220}
221
222void LibraryResolver::resolveSymbolsInLibrary(
223 LibraryInfo *Lib, SymbolQuery &Query, const SymbolEnumeratorOptions &Opts) {
224 LLVM_DEBUG(dbgs() << "Checking unresolved symbols "
225 << " in library : " << Lib->getFileName() << "\n";);
226
227 if (!Query.hasUnresolved()) {
228 LLVM_DEBUG(dbgs() << "Skipping library: " << Lib->getFullPath()
229 << " — unresolved symbols exist.\n";);
230 return;
231 }
232
233 bool HadAnySym = false;
234
235 // Build candidate vector
236 SmallVector<StringRef, 24> CandidateVec;
237
238 Query.getUnresolvedSymbols(CandidateVec, [&](StringRef S) {
239 return !Lib->hasFilter() || Lib->mayContain(S);
240 });
241
242 LLVM_DEBUG(dbgs() << "Total candidate symbols : " << CandidateVec.size()
243 << "\n";);
244 if (CandidateVec.empty()) {
245 LLVM_DEBUG(dbgs() << "No symbol Exist "
246 " in library: "
247 << Lib->getFullPath() << "\n";);
248 return;
249 }
250
251 bool BuildingFilter = !Lib->hasFilter();
252
253 ObjectFileLoader ObjLoader(Lib->getFullPath());
254 auto ObjOrErr = ObjLoader.getObjectFile();
255 if (!ObjOrErr) {
256 std::string ErrMsg;
257 handleAllErrors(ObjOrErr.takeError(),
258 [&](const ErrorInfoBase &EIB) { ErrMsg = EIB.message(); });
259 LLVM_DEBUG(dbgs() << "Failed loading object file: " << Lib->getFullPath()
260 << "\nError: " << ErrMsg << "\n");
261 return;
262 }
263
264 object::ObjectFile *Obj = &ObjOrErr.get();
265 if (BuildingFilter && Obj->isELF()) {
266
267 erase_if(CandidateVec,
268 [&](StringRef C) { return !MayExistInElfObjectFile(Obj, C); });
269 if (CandidateVec.empty())
270 return;
271 }
272
273 SmallVector<StringRef, 256> SymbolVec;
274
275 LLVM_DEBUG(dbgs() << "Enumerating symbols in library: " << Lib->getFullPath()
276 << "\n";);
277
278 SymbolEnumerator::enumerateSymbols(
279 Obj,
280 [&](StringRef S) {
281 // Collect symbols if we're building a filter
282 if (BuildingFilter)
283 SymbolVec.push_back(S);
284
285 // auto It = std::lower_bound(CandidateVec.begin(),
286 // CandidateVec.end(), S);
287 auto It = std::find(CandidateVec.begin(), CandidateVec.end(), S);
288 if (It != CandidateVec.end() && *It == S) {
289 // Resolve and remove from CandidateVec
290 LLVM_DEBUG(dbgs() << "Symbol '" << S << "' resolved in library: "
291 << Lib->getFullPath() << "\n";);
292 Query.resolve(S, Lib->getFullPath());
293 HadAnySym = true;
294 *It = CandidateVec.back();
295 CandidateVec.pop_back();
296
297 // Stop — if nothing remains, stop enumeration
298 if (!BuildingFilter && CandidateVec.empty()) {
299 return EnumerateResult::Stop;
300 }
301 // Also stop if SymbolQuery has no more unresolved symbols
302 if (!BuildingFilter && !Query.hasUnresolved())
303 return EnumerateResult::Stop;
304 }
305
306 return EnumerateResult::Continue;
307 },
308 Opts);
309
310 if (BuildingFilter) {
311 LLVM_DEBUG(dbgs() << "Building filter for library: " << Lib->getFullPath()
312 << "\n";);
313 if (SymbolVec.empty()) {
314 LLVM_DEBUG(dbgs() << " Skip : No symbols found in : "
315 << Lib->getFullPath() << "\n";);
316 return;
317 }
318
319 Lib->ensureFilterBuilt(FB, SymbolVec);
320 LLVM_DEBUG({
321 dbgs() << "DiscoveredSymbols : " << SymbolVec.size() << "\n";
322 for (const auto &S : SymbolVec)
323 dbgs() << "DiscoveredSymbols : " << S << "\n";
324 });
325 }
326
327 if (HadAnySym && Lib->getState() != LibState::Loaded)
328 Lib->setState(LibState::Queried);
329}
330
332 OnSearchComplete OnComplete,
333 const SearchConfig &Config) {
334 SymbolQuery Q(SymbolList);
335
336 using LibraryType = PathType;
337 auto tryResolveFrom = [&](LibState S, LibraryType K) {
338 LLVM_DEBUG(dbgs() << "Trying resolve from state=" << static_cast<int>(S)
339 << " type=" << static_cast<int>(K) << "\n";);
340
341 LibraryCursor Cur = LibMgr.getCursor(K, S);
342 while (!Q.allResolved()) {
343 const LibraryInfo *Lib = Cur.nextValidLib();
344 // Cursor not valid?
345 if (!Lib) {
346 if (!scanForNewLibraries(K, Cur))
347 break; // nothing new was added
348 continue; // Try to resolve next library
349 }
350
351 // can use Async here?
352 resolveSymbolsInLibrary(const_cast<LibraryInfo *>(Lib), Q,
353 Config.Options);
354 if (Q.allResolved())
355 break;
356 }
357 };
358
359 for (const auto &[St, Ty] : Config.Policy.Plan) {
360 tryResolveFrom(St, Ty);
361 if (Q.allResolved())
362 break;
363 }
364
365 // done:
366 LLVM_DEBUG({
367 dbgs() << "Search complete.\n";
368 for (const auto &r : Q.getAllResults())
369 dbgs() << "Resolved Symbol:" << r->Name << " -> " << r->ResolvedLibPath
370 << "\n";
371 });
372
373 OnComplete(Q);
374}
375
376bool LibraryResolver::scanForNewLibraries(PathType K, LibraryCursor &Cur) {
377 while (ScanHelper.leftToScan(K)) {
378 scanLibrariesIfNeeded(K, scanBatchSize);
379
380 // Check if scanning added new libraries
381 if (Cur.hasMoreValidLib())
382 return true;
383 }
384
385 // No new libraries were added
386 return false;
387}
388
389bool LibraryResolver::scanLibrariesIfNeeded(PathType PK, size_t BatchSize) {
390 LLVM_DEBUG(dbgs() << "LibraryResolver::scanLibrariesIfNeeded: Scanning for "
391 << (PK == PathType::User ? "User" : "System")
392 << " libraries\n";);
393 if (!ScanHelper.leftToScan(PK))
394 return false;
395
396 LibraryScanner Scanner(ScanHelper, LibMgr, ShouldScanCall);
397 Scanner.scanNext(PK, BatchSize);
398 return true;
399}
400} // end namespace llvm::orc
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::deque< BasicBlock * > PathType
#define I(x, y, z)
Definition MD5.cpp:57
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
dot regions Print regions of function to dot file(with no function bodies)"
static const char * name
#define LLVM_DEBUG(...)
Definition Debug.h:119
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Base class for error info classes.
Definition Error.h:44
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
Expected< uint32_t > getFlags() const
Get symbol flags (bitwise OR of SymbolRef::Flags)
bool isELF() const
Definition Binary.h:125
This class is the base class for all object file types.
Definition ObjectFile.h:231
virtual uint8_t getBytesInAddress() const =0
The number of bytes used to represent an address in this object file format.
iterator_range< symbol_iterator > symbol_iterator_range
Definition ObjectFile.h:322
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition ObjectFile.h:170
Manages library metadata and state for symbol resolution.
LLVM_ABI void resolveSymbols(ArrayRef< StringRef > Symbols, LibraryResolver::OnSearchComplete OnCompletion, const SearchConfig &Config=SearchConfig())
LLVM_ABI void addScanPath(const std::string &Path, PathType Kind)
LLVM_ABI void markLibraryUnLoaded(StringRef Path)
LLVM_ABI void markLibraryLoaded(StringRef Path)
static LLVM_ABI std::unique_ptr< LibraryResolutionDriver > create(const LibraryResolver::Setup &S)
std::function< EnumerateResult(StringRef Sym)> OnEachSymbolFn
static LLVM_ABI bool enumerateSymbols(object::ObjectFile *Obj, OnEachSymbolFn OnEach, const SymbolEnumeratorOptions &Opts)
Tracks a set of symbols and the libraries where they are resolved.
std::vector< const Entry * > getAllResults() const
unique_function< void(SymbolQuery &)> OnSearchComplete
LLVM_ABI void searchSymbolsInLibraries(ArrayRef< StringRef > SymList, OnSearchComplete OnComplete, const SearchConfig &Config=SearchConfig())
Loads an object file and provides access to it.
Expected< object::ObjectFile & > getObjectFile()
Get the loaded object file, or return an error if loading failed.
Resolves file system paths with optional caching of results.
static bool shouldIgnoreSymbol(const object::SymbolRef &Sym, uint32_t IgnoreFlags)
static StringRef GetGnuHashSection(llvm::object::ObjectFile *file)
static bool MayExistInElfObjectFile(llvm::object::ObjectFile *soFile, StringRef Sym)
Bloom filter is a stochastic data structure which can tell us if a symbol name does not exist in a li...
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition Error.h:1013
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
uint32_t djbHash(StringRef Buffer, uint32_t H=5381)
The Bernstein hash function used by the DWARF accelerator tables.
Definition DJB.h:22
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2192
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
SymbolEnumeratorOptions Options
std::vector< SearchPlanEntry > Plan