[lld] [lld] check cache before real_path in loadDylib (PR #140791)

Daniel Rodríguez Troitiño via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 10 08:09:09 PDT 2025


================
@@ -229,19 +229,31 @@ static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs;
 
 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.
+  DylibFile *&realfile = file;
+  SmallString<128> realPath;
+  std::error_code err = fs::real_path(mbref.getBufferIdentifier(), realPath);
+  if (!err) {
+    CachedHashStringRef resolvedPath(uniqueSaver().save(StringRef(realPath)));
+    realfile = loadedDylibs[resolvedPath];
+    if (realfile) {
+      if (explicitlyLinked)
+        realfile->setExplicitlyLinked();
+
+      file = realfile;
----------------
drodriguez wrote:

Thanks for the review.

At this point, in my head, `file = loadedDylibs[Path]`, but `realfile = loadedDylibs[resolvedPath]`. Originally there was no `DylibFile *&realfile = file;` (it was directly assigned from `loadedDylibs[resolvedPath]`), which might change things, but I am not sure if it does.

The `realfile = file;` below was supposed to also make sure that the entry for `Path` was assigned the result assigned to `resolvedPath` as well, so both refer to the same actual `DylibFile *`.



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


More information about the llvm-commits mailing list