Skip to content

[lld-macho]Fix bug in finding "chained" re-exported libs. #135241

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 3 commits into from
Apr 28, 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
24 changes: 21 additions & 3 deletions lld/MachO/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,17 @@ static DylibFile *findDylib(StringRef path, DylibFile *umbrella,
if (std::optional<StringRef> dylibPath = resolveDylibPath(newPath.str()))
return loadDylib(*dylibPath, umbrella);
}
// If not found in umbrella, try the rpaths specified via -rpath too.
for (StringRef rpath : config->runtimePaths) {
newPath.clear();
if (rpath.consume_front("@loader_path/")) {
fs::real_path(umbrella->getName(), newPath);
path::remove_filename(newPath);
}
path::append(newPath, rpath, path.drop_front(strlen("@rpath/")));
if (std::optional<StringRef> dylibPath = resolveDylibPath(newPath.str()))
return loadDylib(*dylibPath, umbrella);
}
}

// FIXME: Should this be further up?
Expand Down Expand Up @@ -1678,9 +1689,16 @@ static bool isImplicitlyLinked(StringRef path) {
void DylibFile::loadReexport(StringRef path, DylibFile *umbrella,
const InterfaceFile *currentTopLevelTapi) {
DylibFile *reexport = findDylib(path, umbrella, currentTopLevelTapi);
if (!reexport)
error(toString(this) + ": unable to locate re-export with install name " +
path);
if (!reexport) {
// If not found in umbrella, retry since some rpaths might have been
// defined in "this" dylib (which contains the LC_REEXPORT_DYLIB cmd) and
// not in the umbrella.
DylibFile *reexport2 = findDylib(path, this, currentTopLevelTapi);
if (!reexport2) {
error(toString(this) + ": unable to locate re-export with install name " +
path);
}
}
}

DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella,
Expand Down
35 changes: 35 additions & 0 deletions lld/test/MachO/reexport-with-rpath.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# REQUIRES: x86
# RUN: rm -rf %t; split-file %s %t
# RUN: mkdir -p %t/cc/two/three
# RUN: mkdir -p %t/bb/two/three
# RUN: mkdir -p %t/aa/two/three

# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/c.s -o %t/c.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/b.s -o %t/b.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/a.s -o %t/a.o

# RUN: %lld -dylib -install_name @rpath/two/three/libCee.dylib %t/c.o -o %t/cc/two/three/libCee.dylib
# RUN: %lld -dylib -install_name @rpath/two/three/libBee.dylib -L%t/cc/two/three -sub_library libCee -lCee %t/b.o -o %t/bb/two/three/libBee.dylib -rpath %t/cc
# RUN: %lld -dylib -install_name @rpath/two/three/libAee.dylib -L%t/bb/two/three -sub_library libBee -lBee %t/a.o -o %t/aa/two/three/libAee.dylib

#--- c.s
.text
.global _c_func
_c_func:
mov $0, %rax
ret

#--- b.s
.text
.global _b_func

_b_func:
mov $0, %rax
ret

#--- a.s
.text
.global _a_func
_a_func:
mov $0, %rax
ret