13#ifndef LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_LIBRARYSCANNER_H
14#define LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_LIBRARYSCANNER_H
28#include <shared_mutex>
30#include <unordered_map>
31#include <unordered_set>
44 void clear(
bool isRealPathCache =
false) {
45 std::unique_lock<std::shared_mutex> lock(Mtx);
47 if (isRealPathCache) {
48 RealPathCache.clear();
50 ReadlinkCache.clear();
57 std::unique_lock<std::shared_mutex> lock(Mtx);
58 Seen.insert(CanonPath);
62 std::shared_lock<std::shared_mutex> lock(Mtx);
63 return Seen.contains(CanonPath);
67 std::string s = CanonPath.
str();
69 std::shared_lock<std::shared_mutex> lock(Mtx);
74 std::unique_lock<std::shared_mutex> lock(Mtx);
81 mutable std::shared_mutex Mtx;
84 std::string canonicalPath;
85 std::error_code ErrnoCode;
89 std::unique_lock<std::shared_mutex> lock(Mtx);
93 std::optional<PathInfo> read_realpath(StringRef Path)
const {
94 std::shared_lock<std::shared_mutex> lock(Mtx);
95 auto It = RealPathCache.find(Path);
96 if (It != RealPathCache.end())
103 StringMap<PathInfo> RealPathCache;
106 StringMap<std::string> ReadlinkCache;
107 StringMap<mode_t> LstatCache;
109 void insert_link(StringRef Path,
const std::string &s) {
110 std::unique_lock<std::shared_mutex> lock(Mtx);
111 ReadlinkCache.insert({
Path, s});
114 std::optional<std::string> read_link(StringRef Path)
const {
115 std::shared_lock<std::shared_mutex> lock(Mtx);
116 auto It = ReadlinkCache.find(Path);
117 if (It != ReadlinkCache.end())
123 void insert_lstat(StringRef Path, mode_t m) {
124 std::unique_lock<std::shared_mutex> lock(Mtx);
125 LstatCache.insert({
Path, m});
128 std::optional<mode_t> read_lstat(StringRef Path)
const {
129 std::shared_lock<std::shared_mutex> lock(Mtx);
130 auto It = LstatCache.find(Path);
131 if (It != LstatCache.end())
147 std::shared_ptr<LibraryPathCache> LibPathCache;
151 : LibPathCache(
std::
move(cache)) {}
162 bool baseIsResolved =
false,
163 long symloopLevel = 40);
175 for (
const auto &[ph, value] : Placeholders) {
198 auto real = LibPathResolver.resolve(Path, ec);
236 : Kind(Cfg.type), PlaceholderPrefix(PlaceholderPrefix) {
238 Paths.emplace_back(
path.str());
247 std::vector<std::string> Paths;
249 std::string PlaceholderPrefix;
255 std::vector<SearchPathResolver> Resolvers)
256 : Substitutor(
std::
move(Substitutor)), Validator(Validator),
257 Resolvers(
std::
move(Resolvers)) {}
260 bool VariateLibStem =
false)
const;
263 std::optional<std::string> tryWithExtensions(
StringRef libstem)
const;
267 std::vector<SearchPathResolver> Resolvers;
279 std::vector<SearchPathResolver> Resolvers;
280 for (
const auto &
cfg : SearchPathCfg) {
281 Resolvers.emplace_back(
cfg,
285 impl_ = std::make_unique<DylibResolverImpl>(
286 std::move(Substitutor), Validator, std::move(Resolvers));
290 bool VariateLibStem =
false)
const {
293 return impl_->resolve(libStem, VariateLibStem);
305 std::unique_ptr<DylibResolverImpl> impl_;
328 std::shared_ptr<LibraryPathCache> LibPathCache,
329 std::shared_ptr<PathResolver> LibPathResolver)
330 : LibPathCache(
std::
move(LibPathCache)),
331 LibPathResolver(
std::
move(LibPathResolver)) {
333 "orc",
dbgs() <<
"LibraryScanHelper::LibraryScanHelper: base paths : "
334 << SPaths.size() <<
"\n";);
335 for (
const auto &p : SPaths)
343 std::vector<std::shared_ptr<LibrarySearchPath>>
350 std::vector<std::shared_ptr<LibrarySearchPath>>
getAllUnits()
const;
354 for (
const auto &[
_, SP] : LibSearchPaths)
364 return LibPathCache->hasSeenOrMark(
P);
368 return LibPathResolver->resolve(
P.str(), ec);
372 std::string resolveCanonical(
StringRef P, std::error_code &ec)
const;
375 mutable std::shared_mutex Mtx;
376 std::shared_ptr<LibraryPathCache> LibPathCache;
377 std::shared_ptr<PathResolver> LibPathResolver;
381 std::deque<StringRef> UnscannedUsr;
382 std::deque<StringRef> UnscannedSys;
394 auto ObjOrErr = loadObjectFileWithOwnership(Path);
396 Obj = std::move(*ObjOrErr);
399 Err = ObjOrErr.takeError();
412 return std::move(Err);
413 return *Obj.getBinary();
423 loadObjectFileWithOwnership(
StringRef FilePath);
434 : ScanHelper(
H), LibMgr(LibMgr),
435 ShouldScanCall(
std::
move(ShouldScanCall)) {}
461 std::optional<std::string> shouldScan(
StringRef FilePath);
466 void scanBaseDir(std::shared_ptr<LibrarySearchPath> U);
This file defines the BumpPtrAllocator interface.
Analysis containing CSE Info
std::deque< BasicBlock * > PathType
This file defines the SmallVector class.
StringSet - A set-like wrapper for the StringMap.
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Tile Register Pre configure
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
LLVM_ABI bool starts_with_insensitive(StringRef Prefix) const
Check if this string starts with the given Prefix, ignoring case.
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
This class is the base class for all object file types.
Validates and normalizes dynamic library paths.
std::optional< std::string > normalize(StringRef Path) const
static bool isSharedLibrary(StringRef Path)
DylibPathValidator(PathResolver &PR)
std::optional< std::string > validate(StringRef Path) const
Validate the given path as a shared library.
DylibResolverImpl(DylibSubstitutor Substitutor, DylibPathValidator &Validator, std::vector< SearchPathResolver > Resolvers)
std::optional< std::string > resolve(StringRef Stem, bool VariateLibStem=false) const
static std::string resolvelinkerFlag(StringRef libStem, StringRef loaderPath)
std::optional< std::string > resolve(StringRef libStem, bool VariateLibStem=false) const
DylibResolver(DylibPathValidator &Validator)
void configure(StringRef loaderPath, ArrayRef< SearchPathConfig > SearchPathCfg)
Performs placeholder substitution in dynamic library paths.
std::string substitute(StringRef input) const
void configure(StringRef loaderPath)
Manages library metadata and state for symbol resolution.
bool hasSeen(StringRef CanonPath) const
bool hasSeenOrMark(StringRef CanonPath)
void clear(bool isRealPathCache=false)
LibraryPathCache()=default
friend class PathResolver
void markSeen(const std::string &CanonPath)
Scans and tracks libraries for symbol resolution.
std::vector< std::shared_ptr< LibrarySearchPath > > getNextBatch(PathType Kind, size_t batchSize)
SmallVector< StringRef > getSearchPaths() const
LibraryPathCache & getCache() const
bool isTrackedBasePath(StringRef P) const
std::optional< std::string > resolve(StringRef P, std::error_code &ec) const
bool hasSeenOrMark(StringRef P) const
bool leftToScan(PathType K) const
std::vector< std::shared_ptr< LibrarySearchPath > > getAllUnits() const
PathResolver & getPathResolver() const
void addBasePath(const std::string &P, PathType Kind=PathType::Unknown)
LibraryScanHelper(const std::vector< std::string > &SPaths, std::shared_ptr< LibraryPathCache > LibPathCache, std::shared_ptr< PathResolver > LibPathResolver)
void scanNext(PathType Kind, size_t batchSize=1)
std::function< bool(StringRef)> ShouldScanFn
LibraryScanner(LibraryScanHelper &H, LibraryManager &LibMgr, ShouldScanFn ShouldScanCall=[](StringRef path) { return true;})
ObjectFileLoader & operator=(ObjectFileLoader &&)=default
ObjectFileLoader(ObjectFileLoader &&)=default
ObjectFileLoader & operator=(const ObjectFileLoader &)=delete
ObjectFileLoader(StringRef Path)
Construct an object file loader from the given path.
ObjectFileLoader(const ObjectFileLoader &)=delete
static bool isArchitectureCompatible(const object::ObjectFile &Obj)
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.
std::optional< std::string > readlinkCached(StringRef Path)
PathResolver(std::shared_ptr< LibraryPathCache > cache)
mode_t lstatCached(StringRef Path)
std::optional< std::string > resolve(StringRef Path, std::error_code &ec)
std::optional< std::string > realpathCached(StringRef Path, std::error_code &ec, StringRef base="", bool baseIsResolved=false, long symloopLevel=40)
SearchPathType searchPathType() const
std::optional< std::string > resolve(StringRef libStem, const DylibSubstitutor &Subst, DylibPathValidator &Validator) const
SearchPathResolver(const SearchPathConfig &Cfg, StringRef PlaceholderPrefix="")
LibraryScanner::LibraryDepsInfo LibraryDepsInfo
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
void consumeError(Error Err)
Consume a Error without doing anything.
Implement std::hash so that hash_code can be used in STL containers.
Dependency info for a library.
llvm::BumpPtrAllocator Alloc
void addRunPath(StringRef s)
SmallVector< StringRef, 2 > runPath
SmallVector< StringRef, 4 > deps
void addRPath(StringRef s)
SmallVector< StringRef, 2 > rpath
LibrarySearchPath(std::string Base, PathType K)
std::atomic< ScanState > State
ArrayRef< StringRef > Paths