Skip to content

Commit 8e8701a

Browse files
committed
[lld/mac] When loading reexports, look for basename in -F / -L first
Matches ld64 (cf Options::findIndirectDylib()), and fixes PR51218. Differential Revision: https://reviews.llvm.org/D106842
1 parent 21c24ae commit 8e8701a

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

lld/MachO/InputFiles.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,14 +860,37 @@ static DylibFile *loadDylib(StringRef path, DylibFile *umbrella) {
860860
// files.
861861
static DylibFile *findDylib(StringRef path, DylibFile *umbrella,
862862
const InterfaceFile *currentTopLevelTapi) {
863+
// Search order:
864+
// 1. Install name basename in -F / -L directories.
865+
{
866+
StringRef stem = path::stem(path);
867+
SmallString<128> frameworkName;
868+
path::append(frameworkName, stem + ".framework", stem);
869+
bool isFramework = path.endswith(frameworkName);
870+
if (isFramework) {
871+
for (StringRef dir : config->frameworkSearchPaths) {
872+
SmallString<128> candidate = dir;
873+
path::append(candidate, frameworkName);
874+
if (Optional<std::string> dylibPath = resolveDylibPath(candidate))
875+
return loadDylib(*dylibPath, umbrella);
876+
}
877+
} else if (Optional<StringRef> dylibPath = findPathCombination(
878+
stem, config->librarySearchPaths, {".tbd", ".dylib"}))
879+
return loadDylib(*dylibPath, umbrella);
880+
}
881+
882+
// 2. As absolute path.
863883
if (path::is_absolute(path, path::Style::posix))
864884
for (StringRef root : config->systemLibraryRoots)
865885
if (Optional<std::string> dylibPath =
866886
resolveDylibPath((root + path).str()))
867887
return loadDylib(*dylibPath, umbrella);
868888

889+
// 3. As relative path.
890+
869891
// TODO: Handle -dylib_file
870892

893+
// Replace @executable_path, @loader_path, @rpath prefixes in install name.
871894
SmallString<128> newPath;
872895
if (config->outputType == MH_EXECUTE &&
873896
path.consume_front("@executable_path/")) {
@@ -894,6 +917,7 @@ static DylibFile *findDylib(StringRef path, DylibFile *umbrella,
894917
}
895918
}
896919

920+
// FIXME: Should this be further up?
897921
if (currentTopLevelTapi) {
898922
for (InterfaceFile &child :
899923
make_pointee_range(currentTopLevelTapi->documents())) {

lld/test/MachO/sub-library.s

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,36 @@
8686
# RUN: -o %t/libgoodbye.dylib 2>&1 | FileCheck %s --check-prefix=MISSING-FRAMEWORK
8787
# MISSING-FRAMEWORK: error: -sub_umbrella libhello does not match a supplied dylib
8888

89+
90+
## Check that -F (but not -L) can override the search path in install_name for
91+
## frameworks.
92+
# RUN: mkdir -p %t/Hello2.framework
93+
# RUN: %lld -dylib %t/libhello.o \
94+
# RUN: -install_name /path/to/Hello2.framework/Hello2 \
95+
# RUN: -o %t/Hello2.framework/Hello2
96+
# RUN: %lld -dylib -o %t/libgoodbye4.dylib %t/libgoodbye.o \
97+
# RUN: -reexport_library %t/Hello2.framework/Hello2
98+
# RUN: not %lld -lSystem -o %t/hello %t/libgoodbye4.dylib %t/sub-library.o 2>&1 \
99+
# RUN: | FileCheck %s --check-prefix=NOTFOUND
100+
# RUN: not %lld -lSystem -o %t/hello -L%t %t/libgoodbye4.dylib %t/sub-library.o 2>&1 \
101+
# RUN: | FileCheck %s --check-prefix=NOTFOUND
102+
# NOTFOUND: unable to locate re-export with install name /path/to/Hello2.framework/Hello2
103+
# RUN: %lld -lSystem -o %t/hello -F%t %t/libgoodbye4.dylib %t/sub-library.o
104+
105+
## Check that -L (but not -F) can override the search path in install_name for
106+
## libraries.
107+
# RUN: %lld -dylib %t/libhello.o \
108+
# RUN: -install_name /path/to/libhello2.dylib \
109+
# RUN: -o %t/libhello2.dylib
110+
# RUN: %lld -dylib -o %t/libgoodbye5.dylib %t/libgoodbye.o \
111+
# RUN: -reexport_library %t/libhello2.dylib
112+
# RUN: not %lld -lSystem -o %t/hello %t/libgoodbye5.dylib %t/sub-library.o 2>&1 \
113+
# RUN: | FileCheck %s --check-prefix=NOTFOUND2
114+
# RUN: not %lld -lSystem -o %t/hello -F%t %t/libgoodbye5.dylib %t/sub-library.o 2>&1 \
115+
# RUN: | FileCheck %s --check-prefix=NOTFOUND2
116+
# NOTFOUND2: unable to locate re-export with install name /path/to/libhello2.dylib
117+
# RUN: %lld -lSystem -o %t/hello -L%t %t/libgoodbye5.dylib %t/sub-library.o
118+
89119
.text
90120
.globl _main
91121

0 commit comments

Comments
 (0)