[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:00 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());
----------------
zmodem wrote:

How about `CachedHashStringRef realPath = ...` instead? Then you don't have to introduce the extra variable below (and don't have to recompute the hash on line 321).

https://github.com/llvm/llvm-project/pull/143595


More information about the llvm-commits mailing list