Skip to content

[lld] check cache in loadDylib before real_path #143595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions lld/MachO/DriverUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
if (fs::real_path(path, realPathBuf))
return StringRef();

SmallString<128> absPathBuf = path;
if (!fs::make_absolute(absPathBuf) && realPathBuf == absPathBuf)
return StringRef();

return uniqueSaver().save(StringRef(realPathBuf));
}

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.
CachedHashStringRef realPath(
realPathIfDifferent(mbref.getBufferIdentifier()));
if (!realPath.val().empty()) {
// Avoid map insertions here so that we do not invalidate the "file"
// reference.
auto it = loadedDylibs.find(realPath);
if (it != loadedDylibs.end()) {
DylibFile *realfile = it->second;
if (explicitlyLinked)
realfile->setExplicitlyLinked();
return realfile;
}
}

DylibFile *newFile;
file_magic magic = identify_magic(mbref.getBuffer());
if (magic == file_magic::tapi_file) {
Expand Down Expand Up @@ -292,6 +315,11 @@ DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
sys::path::filename(newFile->installName) + "' because " +
config->clientName + " is not an allowed client");
}

// If the load path was a symlink, cache the real path too.
if (!realPath.val().empty())
loadedDylibs[realPath] = newFile;

return newFile;
}

Expand Down
74 changes: 74 additions & 0 deletions lld/test/MachO/reexport-with-symlink.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# REQUIRES: aarch64, shell
# RUN: rm -rf %t; split-file %s %t
# RUN: ln -s Versions/A/Developer %t/Developer/Library/Frameworks/Developer.framework/
# RUN: llvm-mc -filetype obj -triple arm64-apple-macos11.0 %t/test.s -o %t/test.o
# RUN: %lld -arch arm64 -platform_version macos 11.0 11.0 -o %t/test -framework Developer -F %t/Developer/Library/Frameworks -L %t/Developer/usr/lib %t/test.o -t | FileCheck %s

# CHECK: {{.*}}/Developer/Library/Frameworks/Developer.framework/Developer
# CHECK: {{.*}}/Developer/usr/lib/libDeveloperSupport.tbd(@rpath/libDeveloperSupport.dylib)
# CHECK-NOT: {{.*}}/Developer/Library/Frameworks/Developer.framework/Versions/A/Developer

#--- Developer/Library/Frameworks/Developer.framework/Versions/A/Developer
{
"tapi_tbd_version": 5,
"main_library": {
"target_info": [
{
"target": "arm64-macos"
}
],
"install_names": [
{
"name": "@rpath/Developer.framework/Developer"
}
],
"exported_symbols": [
{
"text": {
"global": ["_funcPublic"]
}
}
]
}
}
#--- Developer/usr/lib/libDeveloperSupport.tbd
{
"tapi_tbd_version": 5,
"main_library": {
"target_info": [
{
"target": "arm64-macos"
}
],
"install_names": [
{
"name": "@rpath/libDeveloperSupport.dylib"
}
],
"reexported_libraries": [
{
"names": [
"@rpath/Developer.framework/Versions/A/Developer"
]
}
],
"exported_symbols": [
{
"text": {
"global": ["_funcSupport"]
}
}
]
}
}
#--- test.s
.text
.globl _main
.linker_option "-lDeveloperSupport"

_main:
ret

.data
.quad _funcPublic
.quad _funcSupport