Skip to content

Commit 05422e1

Browse files
rorthtru
authored andcommitted
[Driver] Fix linking with -lm on Solaris (#65632)
As noticed in D158846, the Solaris driver deviates from other targets in that it links every executable with `-lm`, but doesn't for shared objects. For C code, this is unnecessary, while for C++ `libm` is always needed, even for shared objects. This patch fixes this by following the `Gnu.cpp` precedent. It adjusts the `solaris-ld.c` test accordingly, adding some more tests. Tested on `amd64-pc-solaris2.11`, `sparcv9-sun-solaris2.11`, and `x86_64-pc-linux-gnu`. (cherry picked from commit 1e6b0df)
1 parent 910748f commit 05422e1

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

clang/lib/Driver/ToolChains/Solaris.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
6464
const InputInfoList &Inputs,
6565
const ArgList &Args,
6666
const char *LinkingOutput) const {
67+
const Driver &D = getToolChain().getDriver();
6768
const bool IsPIE = getPIE(Args, getToolChain());
6869
ArgStringList CmdArgs;
6970

@@ -152,8 +153,11 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
152153

153154
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
154155
options::OPT_r)) {
155-
if (getToolChain().ShouldLinkCXXStdlib(Args))
156-
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
156+
if (D.CCCIsCXX()) {
157+
if (getToolChain().ShouldLinkCXXStdlib(Args))
158+
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
159+
CmdArgs.push_back("-lm");
160+
}
157161
if (Args.hasArg(options::OPT_fstack_protector) ||
158162
Args.hasArg(options::OPT_fstack_protector_strong) ||
159163
Args.hasArg(options::OPT_fstack_protector_all)) {
@@ -172,7 +176,6 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
172176
CmdArgs.push_back("-lc");
173177
if (!Args.hasArg(options::OPT_shared)) {
174178
CmdArgs.push_back("-lgcc");
175-
CmdArgs.push_back("-lm");
176179
}
177180
const SanitizerArgs &SA = getToolChain().getSanitizerArgs(Args);
178181
if (NeedsSanitizerDeps) {

clang/test/Driver/solaris-ld.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
// CHECK-LD-SPARC32-SAME: "-lgcc_s"
2121
// CHECK-LD-SPARC32-SAME: "-lc"
2222
// CHECK-LD-SPARC32-SAME: "-lgcc"
23-
// CHECK-LD-SPARC32-SAME: "-lm"
2423
// CHECK-LD-SPARC32-SAME: "[[SYSROOT]]/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2{{/|\\\\}}crtend.o"
2524
// CHECK-LD-SPARC32-SAME: "[[SYSROOT]]/usr/lib{{/|\\\\}}crtn.o"
2625

@@ -43,7 +42,6 @@
4342
// CHECK-LD-SPARC64-SAME: "-lgcc_s"
4443
// CHECK-LD-SPARC64-SAME: "-lc"
4544
// CHECK-LD-SPARC64-SAME: "-lgcc"
46-
// CHECK-LD-SPARC64-SAME: "-lm"
4745
// CHECK-LD-SPARC64-SAME: "[[SYSROOT]]/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/sparcv9{{/|\\\\}}crtend.o"
4846
// CHECK-LD-SPARC64-SAME: "[[SYSROOT]]/usr/lib/sparcv9{{/|\\\\}}crtn.o"
4947

@@ -66,7 +64,6 @@
6664
// CHECK-LD-X32-SAME: "-lgcc_s"
6765
// CHECK-LD-X32-SAME: "-lc"
6866
// CHECK-LD-X32-SAME: "-lgcc"
69-
// CHECK-LD-X32-SAME: "-lm"
7067
// CHECK-LD-X32-SAME: "[[SYSROOT]]/usr/gcc/4.9/lib/gcc/i386-pc-solaris2.11/4.9.4{{/|\\\\}}crtend.o"
7168
// CHECK-LD-X32-SAME: "[[SYSROOT]]/usr/lib{{/|\\\\}}crtn.o"
7269

@@ -90,7 +87,6 @@
9087
// CHECK-LD-X64-SAME: "-lgcc_s"
9188
// CHECK-LD-X64-SAME: "-lc"
9289
// CHECK-LD-X64-SAME: "-lgcc"
93-
// CHECK-LD-X64-SAME: "-lm"
9490
// CHECK-LD-X64-SAME: "[[SYSROOT]]/usr/gcc/4.9/lib/gcc/i386-pc-solaris2.11/4.9.4/amd64{{/|\\\\}}crtend.o"
9591
// CHECK-LD-X64-SAME: "[[SYSROOT]]/usr/lib/amd64{{/|\\\\}}crtn.o"
9692

@@ -104,7 +100,22 @@
104100
// CHECK-SPARC32-SHARED-SAME: "-lgcc_s"
105101
// CHECK-SPARC32-SHARED-SAME: "-lc"
106102
// CHECK-SPARC32-SHARED-NOT: "-lgcc"
107-
// CHECK-SPARC32-SHARED-NOT: "-lm"
103+
104+
// Check that libm is only linked with clang++.
105+
// RUN: %clang -### %s --target=sparc-sun-solaris2.11 \
106+
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
107+
// RUN: | FileCheck --check-prefix=CHECK-NOLIBM %s
108+
// RUN: %clang -### %s -shared --target=sparc-sun-solaris2.11 \
109+
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
110+
// RUN: | FileCheck --check-prefix=CHECK-NOLIBM %s
111+
// RUN: %clangxx -### %s --target=sparc-sun-solaris2.11 \
112+
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
113+
// RUN: | FileCheck --check-prefix=CHECK-LIBM %s
114+
// RUN: %clangxx -### %s -shared --target=sparc-sun-solaris2.11 \
115+
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
116+
// RUN: | FileCheck --check-prefix=CHECK-LIBM %s
117+
// CHECK-LIBM: "-lm"
118+
// CHECK-NOLIBM-NOT: "-lm"
108119

109120
// Check the right ld flags are present with -pie.
110121
// RUN: %clang --target=sparc-sun-solaris2.11 -### %s -pie \

0 commit comments

Comments
 (0)