[lld] r217566 - If lld is renamed (or symlinked) to "ld" automatically pick the right flavor.

Nick Kledzik kledzik at apple.com
Wed Sep 10 17:52:05 PDT 2014


Author: kledzik
Date: Wed Sep 10 19:52:05 2014
New Revision: 217566

URL: http://llvm.org/viewvc/llvm-project?rev=217566&view=rev
Log:
If lld is renamed (or symlinked) to "ld" automatically pick the right flavor.

The existing system linkers on Darwin and Linux are called "ld".  We'd like to
eventually drop in lld as "ld" and have it just work.  But lld is a universal
linker that requires the first option to be -flavor to know which command line
mode to emulate (gnu or darwin).

This change tests if argv[0] is "ld" and if so, if the tool was built on MacOSX
then assume the darwin flavor otherwise the gnu flavor.  There are two test
cases which copy lld to "ld" and then run it. One for darwin and one for linux.

Added:
    lld/trunk/test/elf/linker-as-ld.test
    lld/trunk/test/mach-o/linker-as-ld.yaml
Modified:
    lld/trunk/lib/Driver/UniversalDriver.cpp
    lld/trunk/test/lit.cfg

Modified: lld/trunk/lib/Driver/UniversalDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/UniversalDriver.cpp?rev=217566&r1=217565&r2=217566&view=diff
==============================================================================
--- lld/trunk/lib/Driver/UniversalDriver.cpp (original)
+++ lld/trunk/lib/Driver/UniversalDriver.cpp Wed Sep 10 19:52:05 2014
@@ -89,13 +89,7 @@ static Flavor strToFlavor(StringRef str)
       .Case("lld-link", Flavor::win_link)
       .Case("darwin", Flavor::darwin_ld)
       .Case("core", Flavor::core)
-#if __APPLE__
-      // On a Darwin systems, if linker binary is named "ld", use Darwin driver.
-      .Case("ld", Flavor::darwin_ld)
-#else
-      // On other *nix systems, if linker binary is named "ld", use gnu driver.
       .Case("ld", Flavor::gnu_ld)
-#endif
       .Default(Flavor::invalid);
 }
 
@@ -160,6 +154,17 @@ bool UniversalDriver::link(int argc, con
 
   Flavor flavor;
 
+#if LLVM_ON_UNIX
+  if (llvm::sys::path::filename(argv[0]).equals("ld")) {
+#if __APPLE__
+    // On a Darwin systems, if linker binary is named "ld", use Darwin driver.
+    flavor = Flavor::darwin_ld;
+#else
+    // On a ELF based systems, if linker binary is named "ld", use gnu driver.
+    flavor = Flavor::gnu_ld;
+#endif
+  } else
+#endif
   if (parsedArgs->getLastArg(OPT_core)) {
     flavor = Flavor::core;
     argv++;

Added: lld/trunk/test/elf/linker-as-ld.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/linker-as-ld.test?rev=217566&view=auto
==============================================================================
--- lld/trunk/test/elf/linker-as-ld.test (added)
+++ lld/trunk/test/elf/linker-as-ld.test Wed Sep 10 19:52:05 2014
@@ -0,0 +1,16 @@
+REQUIRES: system-linker-elf
+
+RUN: mkdir -p %t.dir && cp `which lld` %t.dir/ld
+RUN: %t.dir/ld -target x86_64-linux -o %t %p/Inputs/relocs.x86-64 \
+RUN:  -e _start -static
+RUN: llvm-readobj -t %t | FileCheck %s
+
+# Test linker run as "ld" on elf based system works like gnu linker.
+
+
+CHECK:        Symbol {
+CHECK:          Name: i
+CHECK-NEXT:     Value:
+CHECK-NEXT:     Size:
+CHECK-NEXT:     Binding:
+CHECK-NEXT:     Type: Object

Modified: lld/trunk/test/lit.cfg
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/lit.cfg?rev=217566&r1=217565&r2=217566&view=diff
==============================================================================
--- lld/trunk/test/lit.cfg (original)
+++ lld/trunk/test/lit.cfg Wed Sep 10 19:52:05 2014
@@ -124,6 +124,14 @@ if lit_config.useValgrind:
 if platform.system() not in ['Windows'] or lit_config.getBashPath() != '':
     config.available_features.add('shell')
 
+# Running on Darwin OS
+if platform.system() in ['Darwin']:
+    config.available_features.add('system-linker-mach-o')
+
+# Running on ELF based *nix
+if platform.system() in ['Linux']:
+    config.available_features.add('system-linker-elf')
+
 # llvm-config knows whether it is compiled with asserts (and)
 # whether we are operating in release/debug mode.
 import subprocess

Added: lld/trunk/test/mach-o/linker-as-ld.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/linker-as-ld.yaml?rev=217566&view=auto
==============================================================================
--- lld/trunk/test/mach-o/linker-as-ld.yaml (added)
+++ lld/trunk/test/mach-o/linker-as-ld.yaml Wed Sep 10 19:52:05 2014
@@ -0,0 +1,39 @@
+# REQUIRES: system-linker-mach-o
+#
+# RUN: mkdir -p %t.dir && cp `which lld` %t.dir/ld \
+# RUN:  && %t.dir/ld -arch x86_64 -macosx_version_min 10.8 %s -o %t \
+# RUN:  && llvm-nm %t | FileCheck %s
+#
+# Test linker run as "ld" on darwin works as darwin linker.
+#
+
+--- !mach-o
+arch:            x86_64
+file-type:       MH_OBJECT
+flags:           [ MH_SUBSECTIONS_VIA_SYMBOLS ]
+has-UUID:        false
+OS:              unknown
+sections:
+  - segment:         __TEXT
+    section:         __text
+    type:            S_REGULAR
+    attributes:      [ S_ATTR_PURE_INSTRUCTIONS, S_ATTR_SOME_INSTRUCTIONS ]
+    address:         0x0000000000000000
+    content:         [ 0xC3 ]
+global-symbols:
+  - name:            _main
+    type:            N_SECT
+    scope:           [ N_EXT ]
+    sect:            1
+    value:           0x0000000000000000
+
+--- !mach-o
+arch:            x86_64
+file-type:       MH_DYLIB
+install-name:    /usr/lib/libSystem.B.dylib
+exports:
+  - name:            dyld_stub_binder
+
+...
+
+# CHECK:	          T _main





More information about the llvm-commits mailing list