[lld] [lld] check cache in loadDylib before real_path (PR #143595)
Hans Wennborg via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 12 01:39:01 PDT 2025
================
@@ -225,21 +225,44 @@ std::optional<StringRef> macho::resolveDylibPath(StringRef dylibPath) {
// especially if it's a commonly re-exported core library.
static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs;
+static StringRef realPathIfDifferent(StringRef path) {
+ SmallString<128> realPathBuf;
+ std::error_code err = fs::real_path(path, realPathBuf);
+ if (err)
+ return StringRef();
+
+ StringRef realPath(realPathBuf);
+ SmallString<128> absPathBuf = path;
+ err = fs::make_absolute(absPathBuf);
+ if (!err && realPath == StringRef(absPathBuf))
+ return StringRef();
+
+ return uniqueSaver().save(realPath);
+}
+
DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
bool isBundleLoader, bool explicitlyLinked) {
- // Frameworks can be found from different symlink paths, so resolve
- // symlinks before looking up in the dylib cache.
- SmallString<128> realPath;
- std::error_code err = fs::real_path(mbref.getBufferIdentifier(), realPath);
- CachedHashStringRef path(!err ? uniqueSaver().save(StringRef(realPath))
- : mbref.getBufferIdentifier());
+ CachedHashStringRef path(mbref.getBufferIdentifier());
DylibFile *&file = loadedDylibs[path];
if (file) {
if (explicitlyLinked)
file->setExplicitlyLinked();
return file;
}
+ // Frameworks can be found from different symlink paths, so resolve
+ // symlinks and look up in the dylib cache.
+ StringRef realPath = realPathIfDifferent(mbref.getBufferIdentifier());
+ if (!realPath.empty()) {
+ CachedHashStringRef cachedRealPath(realPath);
+ if (loadedDylibs.contains(cachedRealPath)) {
+ DylibFile *realfile = loadedDylibs.at(cachedRealPath);
----------------
zmodem wrote:
It's probably worth adding a comment here, explaining that you're taking care to not invalidate the `file` reference above.
The way to combine .contains() and .at() to avoid multiple lookups would be to use .find():
```
auto it = loadedDylibs.find(realPath);
if (it != loadedDylibs.end()) {
DylibFile *realFile = *it;
if (expliticlyLinked)
...
```
https://github.com/llvm/llvm-project/pull/143595
More information about the llvm-commits
mailing list