diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -88,26 +88,30 @@ lld::outs() << "\n"; } -static Optional findWithExtension(StringRef base, - ArrayRef extensions) { - for (StringRef ext : extensions) { - Twine location = base + ext; - if (fs::exists(location)) - return location.str(); +static Optional +findAlongPathsWithExtension(StringRef name, ArrayRef extensions) { + llvm::SmallString<261> base; + for (StringRef dir : config->librarySearchPaths) { + base = dir; + path::append(base, Twine("lib") + name); + for (StringRef ext : extensions) { + Twine location = base + ext; + if (fs::exists(location)) + return location.str(); + } } return {}; } -static Optional findLibrary(StringRef name) { - llvm::SmallString<261> location; - for (StringRef dir : config->librarySearchPaths) { - location = dir; - path::append(location, Twine("lib") + name); - if (Optional path = - findWithExtension(location, {".tbd", ".dylib", ".a"})) - return path; +static Optional findLibrary(opt::InputArgList &args, + StringRef name) { + const opt::Arg *arg = + args.getLastArg(OPT_search_paths_first, OPT_search_dylibs_first); + if (arg && arg->getOption().getID() == OPT_search_dylibs_first) { + return findAlongPathsWithExtension(name, {".tbd", ".dylib"}); + return findAlongPathsWithExtension(name, {".a"}); } - return {}; + return findAlongPathsWithExtension(name, {".tbd", ".dylib", ".a"}); } static Optional findFramework(StringRef name) { @@ -572,7 +576,7 @@ break; case OPT_l: { StringRef name = arg->getValue(); - if (Optional path = findLibrary(name)) { + if (Optional path = findLibrary(args, name)) { addFile(*path); break; } @@ -607,6 +611,8 @@ case OPT_syslibroot: case OPT_sectcreate: case OPT_dynamic: + case OPT_search_paths_first: + case OPT_search_dylibs_first: // handled elsewhere break; default: diff --git a/lld/MachO/Options.td b/lld/MachO/Options.td --- a/lld/MachO/Options.td +++ b/lld/MachO/Options.td @@ -105,11 +105,9 @@ Group; def search_paths_first : Flag<["-"], "search_paths_first">, HelpText<"Search for lib.dylib and lib.a at each step in traversing search path (default for Xcode 4 and later)">, - Flags<[HelpHidden]>, Group; def search_dylibs_first : Flag<["-"], "search_dylibs_first">, HelpText<"Search for lib.dylib on first pass, then for lib.a on second pass through search path (default for Xcode 3 and earlier)">, - Flags<[HelpHidden]>, Group; def framework : Separate<["-"], "framework">, MetaVarName<"">, diff --git a/lld/test/MachO/link-search-order.s b/lld/test/MachO/link-search-order.s --- a/lld/test/MachO/link-search-order.s +++ b/lld/test/MachO/link-search-order.s @@ -1,6 +1,6 @@ # REQUIRES: x86 -# RUN: mkdir -p %t +# RUN: mkdir -p %t %tA # # RUN: llvm-mc -filetype obj -triple x86_64-apple-darwin %p/Inputs/libhello.s -o %t/hello.o # RUN: lld -flavor darwinnew -dylib -install_name @executable_path/libhello.dylib %t/hello.o -o %t/libhello.dylib @@ -8,15 +8,22 @@ # RUN: llvm-mc -filetype obj -triple x86_64-apple-darwin %p/Inputs/libgoodbye.s -o %t/goodbye.o # RUN: lld -flavor darwinnew -dylib -install_name @executable_path/libgoodbye.dylib %t/goodbye.o -o %t/libgoodbye.dylib # RUN: llvm-ar --format=darwin crs %t/libgoodbye.a %t/goodbye.o +# RUN: cp %t/libgoodbye.a %tA # # RUN: llvm-mc -filetype obj -triple x86_64-apple-darwin %s -o %t/test.o -# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z -L%t -lhello -lgoodbye -lSystem %t/test.o +# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %tA/test -Z -L%tA -L%t -lhello -lgoodbye -lSystem %t/test.o +# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z -L%tA -L%t -lhello -lgoodbye -lSystem %t/test.o -search_dylibs_first # -# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck %s +# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=DYLIBS-FIRST %s +# RUN: llvm-objdump --macho --dylibs-used %tA/test | FileCheck --check-prefix=PATHS-FIRST %s -# CHECK: @executable_path/libhello.dylib -# CHECK: @executable_path/libgoodbye.dylib -# CHECK: /usr/lib/libSystem.B.dylib +# DYLIBS-FIRST: @executable_path/libhello.dylib +# DYLIBS-FIRST: @executable_path/libgoodbye.dylib +# DYLIBS-FIRST: /usr/lib/libSystem.B.dylib + +# PATHS-FIRST: @executable_path/libhello.dylib +# PATHS-FIRST-NOT: @executable_path/libgoodbye.dylib +# PATHS-FIRST: /usr/lib/libSystem.B.dylib .section __TEXT,__text .global _main