Skip to content

Commit 7e54fc4

Browse files
author
git apple-llvm automerger
committed
Merge commit '98886d20c868' from apple/main into swift/next
2 parents 9d15983 + 98886d2 commit 7e54fc4

File tree

4 files changed

+66
-25
lines changed

4 files changed

+66
-25
lines changed

lld/MachO/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct Configuration {
3737
bool forceLoadObjC = false;
3838
bool staticLink = false;
3939
bool headerPadMaxInstallNames = false;
40+
bool searchDylibsFirst = false;
4041
uint32_t headerPad;
4142
llvm::StringRef installName;
4243
llvm::StringRef outputFile;

lld/MachO/Driver.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,29 @@ void MachOOptTable::printHelp(const char *argv0, bool showHidden) const {
8888
lld::outs() << "\n";
8989
}
9090

91-
static Optional<std::string> findWithExtension(StringRef base,
92-
ArrayRef<StringRef> extensions) {
93-
for (StringRef ext : extensions) {
94-
Twine location = base + ext;
95-
if (fs::exists(location))
96-
return location.str();
91+
static Optional<std::string>
92+
findAlongPathsWithExtensions(StringRef name, ArrayRef<StringRef> extensions) {
93+
llvm::SmallString<261> base;
94+
for (StringRef dir : config->librarySearchPaths) {
95+
base = dir;
96+
path::append(base, Twine("lib") + name);
97+
for (StringRef ext : extensions) {
98+
Twine location = base + ext;
99+
if (fs::exists(location))
100+
return location.str();
101+
}
97102
}
98103
return {};
99104
}
100105

101106
static Optional<std::string> findLibrary(StringRef name) {
102-
llvm::SmallString<261> location;
103-
for (StringRef dir : config->librarySearchPaths) {
104-
location = dir;
105-
path::append(location, Twine("lib") + name);
106-
if (Optional<std::string> path =
107-
findWithExtension(location, {".tbd", ".dylib", ".a"}))
108-
return path;
107+
if (config->searchDylibsFirst) {
108+
if (Optional<std::string> path =
109+
findAlongPathsWithExtensions(name, {".tbd", ".dylib"}))
110+
return path;
111+
return findAlongPathsWithExtensions(name, {".a"});
109112
}
110-
return {};
113+
return findAlongPathsWithExtensions(name, {".tbd", ".dylib", ".a"});
111114
}
112115

113116
static Optional<std::string> findFramework(StringRef name) {
@@ -543,6 +546,10 @@ bool macho::link(llvm::ArrayRef<const char *> argsArr, bool canExitEarly,
543546

544547
getLibrarySearchPaths(args, roots, config->librarySearchPaths);
545548
getFrameworkSearchPaths(args, roots, config->frameworkSearchPaths);
549+
if (const opt::Arg *arg =
550+
args.getLastArg(OPT_search_paths_first, OPT_search_dylibs_first))
551+
config->searchDylibsFirst =
552+
(arg && arg->getOption().getID() == OPT_search_dylibs_first);
546553
config->forceLoadObjC = args.hasArg(OPT_ObjC);
547554

548555
if (args.hasArg(OPT_v)) {

lld/MachO/Options.td

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,9 @@ def syslibroot : Separate<["-"], "syslibroot">,
104104
Group<grp_libs>;
105105
def search_paths_first : Flag<["-"], "search_paths_first">,
106106
HelpText<"Search for lib<name>.dylib and lib<name>.a at each step in traversing search path (default for Xcode 4 and later)">,
107-
Flags<[HelpHidden]>,
108107
Group<grp_libs>;
109108
def search_dylibs_first : Flag<["-"], "search_dylibs_first">,
110109
HelpText<"Search for lib<name>.dylib on first pass, then for lib<name>.a on second pass through search path (default for Xcode 3 and earlier)">,
111-
Flags<[HelpHidden]>,
112110
Group<grp_libs>;
113111
def framework : Separate<["-"], "framework">,
114112
MetaVarName<"<name>">,

lld/test/MachO/link-search-order.s

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,57 @@
11
# REQUIRES: x86
22

3-
# RUN: mkdir -p %t
3+
################ Place dynlib in %tD, and archive in %tA
4+
# RUN: mkdir -p %t %tA %tD
45
#
56
# RUN: llvm-mc -filetype obj -triple x86_64-apple-darwin %p/Inputs/libhello.s -o %t/hello.o
67
# RUN: lld -flavor darwinnew -dylib -install_name @executable_path/libhello.dylib %t/hello.o -o %t/libhello.dylib
78
#
89
# RUN: llvm-mc -filetype obj -triple x86_64-apple-darwin %p/Inputs/libgoodbye.s -o %t/goodbye.o
9-
# RUN: lld -flavor darwinnew -dylib -install_name @executable_path/libgoodbye.dylib %t/goodbye.o -o %t/libgoodbye.dylib
10-
# RUN: llvm-ar --format=darwin crs %t/libgoodbye.a %t/goodbye.o
10+
# RUN: lld -flavor darwinnew -dylib -install_name @executable_path/libgoodbye.dylib %t/goodbye.o -o %tD/libgoodbye.dylib
11+
# RUN: llvm-ar --format=darwin crs %tA/libgoodbye.a %t/goodbye.o
1112
#
1213
# RUN: llvm-mc -filetype obj -triple x86_64-apple-darwin %s -o %t/test.o
13-
# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z -L%t -lhello -lgoodbye -lSystem %t/test.o
14-
#
15-
# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck %s
1614

17-
# CHECK: @executable_path/libhello.dylib
18-
# CHECK: @executable_path/libgoodbye.dylib
19-
# CHECK: /usr/lib/libSystem.B.dylib
15+
################ default, which is the same as -search_paths_first
16+
# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
17+
# RUN: -L%tA -L%tD -L%t -lhello -lgoodbye -lSystem %t/test.o
18+
# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=ARCHIVE %s
19+
20+
################ Test all permutations of -L%t{A,D} with -search_paths_first
21+
# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
22+
# RUN: -L%tA -L%tD -L%t -lhello -lgoodbye -lSystem %t/test.o -search_paths_first
23+
# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=ARCHIVE %s
24+
# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
25+
# RUN: -L%tD -L%tA -L%t -lhello -lgoodbye -lSystem %t/test.o -search_paths_first
26+
# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=DYLIB %s
27+
# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
28+
# RUN: -L%tA -L%t -lhello -lgoodbye -lSystem %t/test.o -search_paths_first
29+
# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=ARCHIVE %s
30+
# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
31+
# RUN: -L%tD -L%t -lhello -lgoodbye -lSystem %t/test.o -search_paths_first
32+
# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=DYLIB %s
33+
34+
################ Test all permutations of -L%t{A,D} with -search_dylibs_first
35+
# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
36+
# RUN: -L%tA -L%tD -L%t -lhello -lgoodbye -lSystem %t/test.o -search_dylibs_first
37+
# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=DYLIB %s
38+
# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
39+
# RUN: -L%tD -L%tA -L%t -lhello -lgoodbye -lSystem %t/test.o -search_dylibs_first
40+
# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=DYLIB %s
41+
# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
42+
# RUN: -L%tA -L%t -lhello -lgoodbye -lSystem %t/test.o -search_dylibs_first
43+
# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=ARCHIVE %s
44+
# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
45+
# RUN: -L%tD -L%t -lhello -lgoodbye -lSystem %t/test.o -search_dylibs_first
46+
# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=DYLIB %s
47+
48+
# DYLIB: @executable_path/libhello.dylib
49+
# DYLIB: @executable_path/libgoodbye.dylib
50+
# DYLIB: /usr/lib/libSystem.B.dylib
51+
52+
# ARCHIVE: @executable_path/libhello.dylib
53+
# ARCHIVE-NOT: @executable_path/libgoodbye.dylib
54+
# ARCHIVE: /usr/lib/libSystem.B.dylib
2055

2156
.section __TEXT,__text
2257
.global _main

0 commit comments

Comments
 (0)