[lld] 8f2c31f - [lld-macho] handle options -search_paths_first, -search_dylibs_first

Greg McGary via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 23 14:57:13 PDT 2020


Author: Greg McGary
Date: 2020-09-23T14:56:33-07:00
New Revision: 8f2c31f22b974da2c0fbc5de0ce4dceffe8ed0b8

URL: https://github.com/llvm/llvm-project/commit/8f2c31f22b974da2c0fbc5de0ce4dceffe8ed0b8
DIFF: https://github.com/llvm/llvm-project/commit/8f2c31f22b974da2c0fbc5de0ce4dceffe8ed0b8.diff

LOG: [lld-macho] handle options -search_paths_first, -search_dylibs_first

Differential Revision: https://reviews.llvm.org/D88054

Added: 
    

Modified: 
    lld/MachO/Config.h
    lld/MachO/Driver.cpp
    lld/MachO/Options.td
    lld/test/MachO/link-search-order.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Config.h b/lld/MachO/Config.h
index 447a0c3a6f21..f6b61d0c8591 100644
--- a/lld/MachO/Config.h
+++ b/lld/MachO/Config.h
@@ -37,6 +37,7 @@ struct Configuration {
   bool forceLoadObjC = false;
   bool staticLink = false;
   bool headerPadMaxInstallNames = false;
+  bool searchDylibsFirst = false;
   uint32_t headerPad;
   llvm::StringRef installName;
   llvm::StringRef outputFile;

diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index c098e7e72fc1..a6e2f5c102e0 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -88,26 +88,29 @@ void MachOOptTable::printHelp(const char *argv0, bool showHidden) const {
   lld::outs() << "\n";
 }
 
-static Optional<std::string> findWithExtension(StringRef base,
-                                               ArrayRef<StringRef> extensions) {
-  for (StringRef ext : extensions) {
-    Twine location = base + ext;
-    if (fs::exists(location))
-      return location.str();
+static Optional<std::string>
+findAlongPathsWithExtensions(StringRef name, ArrayRef<StringRef> 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<std::string> findLibrary(StringRef name) {
-  llvm::SmallString<261> location;
-  for (StringRef dir : config->librarySearchPaths) {
-      location = dir;
-      path::append(location, Twine("lib") + name);
-      if (Optional<std::string> path =
-              findWithExtension(location, {".tbd", ".dylib", ".a"}))
-        return path;
+  if (config->searchDylibsFirst) {
+    if (Optional<std::string> path =
+            findAlongPathsWithExtensions(name, {".tbd", ".dylib"}))
+      return path;
+    return findAlongPathsWithExtensions(name, {".a"});
   }
-  return {};
+  return findAlongPathsWithExtensions(name, {".tbd", ".dylib", ".a"});
 }
 
 static Optional<std::string> findFramework(StringRef name) {
@@ -543,6 +546,10 @@ bool macho::link(llvm::ArrayRef<const char *> argsArr, bool canExitEarly,
 
   getLibrarySearchPaths(args, roots, config->librarySearchPaths);
   getFrameworkSearchPaths(args, roots, config->frameworkSearchPaths);
+  if (const opt::Arg *arg =
+          args.getLastArg(OPT_search_paths_first, OPT_search_dylibs_first))
+    config->searchDylibsFirst =
+        (arg && arg->getOption().getID() == OPT_search_dylibs_first);
   config->forceLoadObjC = args.hasArg(OPT_ObjC);
 
   if (args.hasArg(OPT_v)) {

diff  --git a/lld/MachO/Options.td b/lld/MachO/Options.td
index 602f72372129..ac6424b3f94a 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -104,11 +104,9 @@ def syslibroot : Separate<["-"], "syslibroot">,
      Group<grp_libs>;
 def search_paths_first : Flag<["-"], "search_paths_first">,
      HelpText<"Search for lib<name>.dylib and lib<name>.a at each step in traversing search path (default for Xcode 4 and later)">,
-     Flags<[HelpHidden]>,
      Group<grp_libs>;
 def search_dylibs_first : Flag<["-"], "search_dylibs_first">,
      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)">,
-     Flags<[HelpHidden]>,
      Group<grp_libs>;
 def framework : Separate<["-"], "framework">,
      MetaVarName<"<name>">,

diff  --git a/lld/test/MachO/link-search-order.s b/lld/test/MachO/link-search-order.s
index 9819e171c7dd..3e8803b34498 100644
--- a/lld/test/MachO/link-search-order.s
+++ b/lld/test/MachO/link-search-order.s
@@ -1,22 +1,57 @@
 # REQUIRES: x86
 
-# RUN: mkdir -p %t
+################ Place dynlib in %tD, and archive in %tA
+# RUN: mkdir -p %t %tA %tD
 #
 # 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
 #
 # 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: lld -flavor darwinnew -dylib -install_name @executable_path/libgoodbye.dylib %t/goodbye.o -o %tD/libgoodbye.dylib
+# RUN: llvm-ar --format=darwin crs %tA/libgoodbye.a %t/goodbye.o
 #
 # 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: llvm-objdump --macho --dylibs-used %t/test | FileCheck %s
 
-# CHECK: @executable_path/libhello.dylib
-# CHECK: @executable_path/libgoodbye.dylib
-# CHECK: /usr/lib/libSystem.B.dylib
+################ default, which is the same as -search_paths_first
+# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
+# RUN:     -L%tA -L%tD -L%t -lhello -lgoodbye -lSystem %t/test.o
+# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=ARCHIVE %s
+
+################ Test all permutations of -L%t{A,D} with -search_paths_first
+# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
+# RUN:     -L%tA -L%tD -L%t -lhello -lgoodbye -lSystem %t/test.o -search_paths_first
+# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=ARCHIVE %s
+# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
+# RUN:     -L%tD -L%tA -L%t -lhello -lgoodbye -lSystem %t/test.o -search_paths_first
+# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=DYLIB %s
+# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
+# RUN:     -L%tA       -L%t -lhello -lgoodbye -lSystem %t/test.o -search_paths_first
+# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=ARCHIVE %s
+# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
+# RUN:     -L%tD      -L%t -lhello -lgoodbye -lSystem %t/test.o -search_paths_first
+# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=DYLIB %s
+
+################ Test all permutations of -L%t{A,D} with -search_dylibs_first
+# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
+# RUN:     -L%tA -L%tD -L%t -lhello -lgoodbye -lSystem %t/test.o -search_dylibs_first
+# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=DYLIB %s
+# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
+# RUN:     -L%tD -L%tA -L%t -lhello -lgoodbye -lSystem %t/test.o -search_dylibs_first
+# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=DYLIB %s
+# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
+# RUN:     -L%tA       -L%t -lhello -lgoodbye -lSystem %t/test.o -search_dylibs_first
+# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=ARCHIVE %s
+# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -o %t/test -Z \
+# RUN:     -L%tD       -L%t -lhello -lgoodbye -lSystem %t/test.o -search_dylibs_first
+# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck --check-prefix=DYLIB %s
+
+# DYLIB: @executable_path/libhello.dylib
+# DYLIB: @executable_path/libgoodbye.dylib
+# DYLIB: /usr/lib/libSystem.B.dylib
+
+# ARCHIVE:     @executable_path/libhello.dylib
+# ARCHIVE-NOT: @executable_path/libgoodbye.dylib
+# ARCHIVE:     /usr/lib/libSystem.B.dylib
 
 .section __TEXT,__text
 .global _main


        


More information about the llvm-commits mailing list