13#ifndef LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_LIBRARYSCANNER_H
14#define LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_LIBRARYSCANNER_H
28#include <shared_mutex>
42 void clear(
bool isRealPathCache =
false) {
43 std::unique_lock<std::shared_mutex> lock(Mtx);
45 if (isRealPathCache) {
46 RealPathCache.clear();
48 ReadlinkCache.clear();
55 std::unique_lock<std::shared_mutex> lock(Mtx);
56 Seen.insert(CanonPath);
60 std::shared_lock<std::shared_mutex> lock(Mtx);
61 return Seen.contains(CanonPath);
65 std::string s = CanonPath.
str();
67 std::shared_lock<std::shared_mutex> lock(Mtx);
72 std::unique_lock<std::shared_mutex> lock(Mtx);
79 mutable std::shared_mutex Mtx;
82 std::string canonicalPath;
83 std::error_code ErrnoCode;
87 std::unique_lock<std::shared_mutex> lock(Mtx);
91 std::optional<PathInfo> read_realpath(StringRef Path)
const {
92 std::shared_lock<std::shared_mutex> lock(Mtx);
93 auto It = RealPathCache.find(Path);
94 if (It != RealPathCache.end())
101 StringMap<PathInfo> RealPathCache;
104 StringMap<std::string> ReadlinkCache;
105 StringMap<mode_t> LstatCache;
107 void insert_link(StringRef Path,
const std::string &s) {
108 std::unique_lock<std::shared_mutex> lock(Mtx);
109 ReadlinkCache.insert({
Path, s});
112 std::optional<std::string> read_link(StringRef Path)
const {
113 std::shared_lock<std::shared_mutex> lock(Mtx);
114 auto It = ReadlinkCache.find(Path);
115 if (It != ReadlinkCache.end())
121 void insert_lstat(StringRef Path, mode_t m) {
122 std::unique_lock<std::shared_mutex> lock(Mtx);
123 LstatCache.insert({
Path, m});
126 std::optional<mode_t> read_lstat(StringRef Path)
const {
127 std::shared_lock<std::shared_mutex> lock(Mtx);
128 auto It = LstatCache.find(Path);
129 if (It != LstatCache.end())
145 std::shared_ptr<LibraryPathCache> LibPathCache;
149 : LibPathCache(
std::
move(cache)) {}
160 bool baseIsResolved =
false,
161 long symloopLevel = 40);
173 for (
const auto &[ph, value] : Placeholders) {
196 auto real = LibPathResolver.resolve(Path, ec);
234 : Kind(Cfg.type), PlaceholderPrefix(PlaceholderPrefix) {
236 Paths.emplace_back(
path.str());
245 std::vector<std::string> Paths;
247 std::string PlaceholderPrefix;
253 std::vector<SearchPathResolver> Resolvers)
254 : Substitutor(
std::
move(Substitutor)), Validator(Validator),
255 Resolvers(
std::
move(Resolvers)) {}
258 bool VariateLibStem =
false)
const;
261 std::optional<std::string> tryWithExtensions(
StringRef libstem)
const;
265 std::vector<SearchPathResolver> Resolvers;
277 std::vector<SearchPathResolver> Resolvers;
278 for (
const auto &
cfg : SearchPathCfg) {
279 Resolvers.emplace_back(
cfg,
283 impl_ = std::make_unique<DylibResolverImpl>(
284 std::move(Substitutor), Validator, std::move(Resolvers));
288 bool VariateLibStem =
false)
const {
291 return impl_->resolve(libStem, VariateLibStem);
303 std::unique_ptr<DylibResolverImpl> impl_;
326 std::shared_ptr<LibraryPathCache> LibPathCache,
327 std::shared_ptr<PathResolver> LibPathResolver)
328 : LibPathCache(
std::
move(LibPathCache)),
329 LibPathResolver(
std::
move(LibPathResolver)) {
331 "orc",
dbgs() <<
"LibraryScanHelper::LibraryScanHelper: base paths : "
332 << SPaths.size() <<
"\n";);
333 for (
const auto &p : SPaths)
341 std::vector<std::shared_ptr<LibrarySearchPath>>
348 std::vector<std::shared_ptr<LibrarySearchPath>>
getAllUnits()
const;
352 for (
const auto &[
_, SP] : LibSearchPaths)
362 return LibPathCache->hasSeenOrMark(
P);
366 return LibPathResolver->resolve(
P.str(), ec);
370 std::string resolveCanonical(
StringRef P, std::error_code &ec)
const;
373 mutable std::shared_mutex Mtx;
374 std::shared_ptr<LibraryPathCache> LibPathCache;
375 std::shared_ptr<PathResolver> LibPathResolver;
379 std::deque<StringRef> UnscannedUsr;
380 std::deque<StringRef> UnscannedSys;
392 auto ObjOrErr = loadObjectFileWithOwnership(Path);
394 Obj = std::move(*ObjOrErr);
397 Err = ObjOrErr.takeError();
410 return std::move(Err);
411 return *Obj.getBinary();
421 loadObjectFileWithOwnership(
StringRef FilePath);
432 : ScanHelper(
H), LibMgr(LibMgr),
433 ShouldScanCall(
std::
move(ShouldScanCall)) {}
459 std::optional<std::string> shouldScan(
StringRef FilePath);
464 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.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
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