Skip to content

Commit 475a8a4

Browse files
authored
[lld] check cache before real_path in loadDylib (llvm#140791)
1 parent 28eb66b commit 475a8a4

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

lld/MachO/DriverUtils.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,19 +227,31 @@ static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs;
227227

228228
DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
229229
bool isBundleLoader, bool explicitlyLinked) {
230-
// Frameworks can be found from different symlink paths, so resolve
231-
// symlinks before looking up in the dylib cache.
232-
SmallString<128> realPath;
233-
std::error_code err = fs::real_path(mbref.getBufferIdentifier(), realPath);
234-
CachedHashStringRef path(!err ? uniqueSaver().save(StringRef(realPath))
235-
: mbref.getBufferIdentifier());
230+
CachedHashStringRef path(mbref.getBufferIdentifier());
236231
DylibFile *&file = loadedDylibs[path];
237232
if (file) {
238233
if (explicitlyLinked)
239234
file->setExplicitlyLinked();
240235
return file;
241236
}
242237

238+
// Frameworks can be found from different symlink paths, so resolve
239+
// symlinks and look up in the dylib cache.
240+
DylibFile *&realfile = file;
241+
SmallString<128> realPath;
242+
std::error_code err = fs::real_path(mbref.getBufferIdentifier(), realPath);
243+
if (!err) {
244+
CachedHashStringRef resolvedPath(uniqueSaver().save(StringRef(realPath)));
245+
realfile = loadedDylibs[resolvedPath];
246+
if (realfile) {
247+
if (explicitlyLinked)
248+
realfile->setExplicitlyLinked();
249+
250+
file = realfile;
251+
return realfile;
252+
}
253+
}
254+
243255
DylibFile *newFile;
244256
file_magic magic = identify_magic(mbref.getBuffer());
245257
if (magic == file_magic::tapi_file) {
@@ -251,6 +263,7 @@ DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
251263
}
252264
file =
253265
make<DylibFile>(**result, umbrella, isBundleLoader, explicitlyLinked);
266+
realfile = file;
254267

255268
// parseReexports() can recursively call loadDylib(). That's fine since
256269
// we wrote the DylibFile we just loaded to the loadDylib cache via the
@@ -266,6 +279,7 @@ DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
266279
magic == file_magic::macho_executable ||
267280
magic == file_magic::macho_bundle);
268281
file = make<DylibFile>(mbref, umbrella, isBundleLoader, explicitlyLinked);
282+
realfile = file;
269283

270284
// parseLoadCommands() can also recursively call loadDylib(). See comment
271285
// in previous block for why this means we must copy `file` here.

0 commit comments

Comments
 (0)