[clang] f59bec7 - [clang][Driver] Default to /usr/bin/ld on Solaris

Rainer Orth via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 13 13:43:50 PDT 2020


Author: Rainer Orth
Date: 2020-08-13T22:42:58+02:00
New Revision: f59bec7acb8228fc215fca3ee1e524c38083c50b

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

LOG: [clang][Driver] Default to /usr/bin/ld on Solaris

`clang` currently requires the native linker on Solaris:

  - It passes `-C` to `ld` which GNU `ld` doesn't understand.

  - To use `gld`, one needs to pass the correct `-m EMU` option to select
    the right emulation.  Solaris `ld` cannot handle that option.

So far I've worked around this by passing `-DCLANG_DEFAULT_LINKER=/usr/bin/ld`
to `cmake`.  However, if someone forgets this, it depends on the user's
`PATH` whether or not `clang` finds the correct linker, which doesn't make
for a good user experience.

While it would be nice to detect the linker flavor at runtime, this is more
involved.  Instead, this patch defaults to `/usr/bin/ld` on Solaris.  This
doesn't work on its own, however: a link fails with

  clang-12: error: unable to execute command: Executable "x86_64-pc-solaris2.11-/usr/bin/ld" doesn't exist!

I avoid this by leaving absolute paths alone in `ToolChain::GetLinkerPath`.

Tested on `amd64-pc-solaris2.11`, `sparcv9-sun-solaris2.11`, and
`x86_64-pc-linux-gnu`.

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

Added: 
    clang/test/Driver/solaris-ld-sld.c

Modified: 
    clang/lib/Driver/ToolChain.cpp
    clang/lib/Driver/ToolChains/Solaris.h
    llvm/utils/lit/lit/llvm/config.py

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index 2984537c23b4..7be83cade93a 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -568,8 +568,13 @@ std::string ToolChain::GetLinkerPath() const {
   }
   // If we're passed -fuse-ld= with no argument, or with the argument ld,
   // then use whatever the default system linker is.
-  if (UseLinker.empty() || UseLinker == "ld")
-    return GetProgramPath(getDefaultLinker());
+  if (UseLinker.empty() || UseLinker == "ld") {
+    const char *DefaultLinker = getDefaultLinker();
+    if (llvm::sys::path::is_absolute(DefaultLinker))
+      return std::string(DefaultLinker);
+    else
+      return GetProgramPath(DefaultLinker);
+  }
 
   // Extending -fuse-ld= to an absolute or relative path is unexpected. Checking
   // for the linker flavor is brittle. In addition, prepending "ld." or "ld64."

diff  --git a/clang/lib/Driver/ToolChains/Solaris.h b/clang/lib/Driver/ToolChains/Solaris.h
index b79e626ef38d..fbac92c2c0f3 100644
--- a/clang/lib/Driver/ToolChains/Solaris.h
+++ b/clang/lib/Driver/ToolChains/Solaris.h
@@ -65,6 +65,11 @@ class LLVM_LIBRARY_VISIBILITY Solaris : public Generic_ELF {
   SanitizerMask getSupportedSanitizers() const override;
   unsigned GetDefaultDwarfVersion() const override { return 2; }
 
+  const char *getDefaultLinker() const override {
+    // clang currently uses Solaris ld-only options.
+    return "/usr/bin/ld";
+  }
+
 protected:
   Tool *buildAssembler() const override;
   Tool *buildLinker() const override;

diff  --git a/clang/test/Driver/solaris-ld-sld.c b/clang/test/Driver/solaris-ld-sld.c
new file mode 100644
index 000000000000..f65153784b76
--- /dev/null
+++ b/clang/test/Driver/solaris-ld-sld.c
@@ -0,0 +1,7 @@
+// REQUIRES: system-solaris
+
+// Check that clang invokes the native ld.
+
+// RUN: test -f /usr/gnu/bin/ld && env PATH=/usr/gnu/bin %clang -o %t.o %s
+
+int main() { return 0; }

diff  --git a/llvm/utils/lit/lit/llvm/config.py b/llvm/utils/lit/lit/llvm/config.py
index db557a7b1fef..e9fd75e0a5fa 100644
--- a/llvm/utils/lit/lit/llvm/config.py
+++ b/llvm/utils/lit/lit/llvm/config.py
@@ -59,6 +59,8 @@ def __init__(self, lit_config, config):
             features.add('system-netbsd')
         elif platform.system() == 'AIX':
             features.add('system-aix')
+        elif platform.system() == 'SunOS':
+            features.add('system-solaris')
 
         # Native compilation: host arch == default triple arch
         # Both of these values should probably be in every site config (e.g. as


        


More information about the cfe-commits mailing list