Skip to content

Commit d13720d

Browse files
rorthAndrés Villegas
authored and
Andrés Villegas
committed
[Driver] Fix linking with -lm on Solaris (llvm#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`.
1 parent f2ec1ca commit d13720d

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
@@ -87,6 +87,7 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
8787
const InputInfoList &Inputs,
8888
const ArgList &Args,
8989
const char *LinkingOutput) const {
90+
const Driver &D = getToolChain().getDriver();
9091
const bool IsPIE = getPIE(Args, getToolChain());
9192
ArgStringList CmdArgs;
9293
bool LinkerIsGnuLd = isLinkerGnuLd(getToolChain(), Args);
@@ -217,8 +218,11 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
217218

218219
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
219220
options::OPT_r)) {
220-
if (getToolChain().ShouldLinkCXXStdlib(Args))
221-
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
221+
if (D.CCCIsCXX()) {
222+
if (getToolChain().ShouldLinkCXXStdlib(Args))
223+
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
224+
CmdArgs.push_back("-lm");
225+
}
222226
if (Args.hasArg(options::OPT_fstack_protector) ||
223227
Args.hasArg(options::OPT_fstack_protector_strong) ||
224228
Args.hasArg(options::OPT_fstack_protector_all)) {
@@ -239,7 +243,6 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
239243
CmdArgs.push_back("-lc");
240244
if (!Args.hasArg(options::OPT_shared)) {
241245
CmdArgs.push_back("-lgcc");
242-
CmdArgs.push_back("-lm");
243246
}
244247
const SanitizerArgs &SA = getToolChain().getSanitizerArgs(Args);
245248
if (NeedsSanitizerDeps) {

clang/test/Driver/solaris-ld.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
// CHECK-GLD: "--as-needed" "-lgcc_s" "--no-as-needed"
2727
// CHECK-LD-SPARC32-SAME: "-lc"
2828
// CHECK-LD-SPARC32-SAME: "-lgcc"
29-
// CHECK-LD-SPARC32-SAME: "-lm"
3029
// CHECK-LD-SPARC32-SAME: "[[SYSROOT]]/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2{{/|\\\\}}crtend.o"
3130
// CHECK-LD-SPARC32-SAME: "[[SYSROOT]]/usr/lib{{/|\\\\}}crtn.o"
3231

@@ -49,7 +48,6 @@
4948
// CHECK-LD-SPARC64-SAME: "-lgcc_s"
5049
// CHECK-LD-SPARC64-SAME: "-lc"
5150
// CHECK-LD-SPARC64-SAME: "-lgcc"
52-
// CHECK-LD-SPARC64-SAME: "-lm"
5351
// CHECK-LD-SPARC64-SAME: "[[SYSROOT]]/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/sparcv9{{/|\\\\}}crtend.o"
5452
// CHECK-LD-SPARC64-SAME: "[[SYSROOT]]/usr/lib/sparcv9{{/|\\\\}}crtn.o"
5553

@@ -72,7 +70,6 @@
7270
// CHECK-LD-X32-SAME: "-lgcc_s"
7371
// CHECK-LD-X32-SAME: "-lc"
7472
// CHECK-LD-X32-SAME: "-lgcc"
75-
// CHECK-LD-X32-SAME: "-lm"
7673
// CHECK-LD-X32-SAME: "[[SYSROOT]]/usr/gcc/4.9/lib/gcc/i386-pc-solaris2.11/4.9.4{{/|\\\\}}crtend.o"
7774
// CHECK-LD-X32-SAME: "[[SYSROOT]]/usr/lib{{/|\\\\}}crtn.o"
7875

@@ -96,7 +93,6 @@
9693
// CHECK-LD-X64-SAME: "-lgcc_s"
9794
// CHECK-LD-X64-SAME: "-lc"
9895
// CHECK-LD-X64-SAME: "-lgcc"
99-
// CHECK-LD-X64-SAME: "-lm"
10096
// CHECK-LD-X64-SAME: "[[SYSROOT]]/usr/gcc/4.9/lib/gcc/i386-pc-solaris2.11/4.9.4/amd64{{/|\\\\}}crtend.o"
10197
// CHECK-LD-X64-SAME: "[[SYSROOT]]/usr/lib/amd64{{/|\\\\}}crtn.o"
10298

@@ -110,7 +106,22 @@
110106
// CHECK-SPARC32-SHARED-SAME: "-lgcc_s"
111107
// CHECK-SPARC32-SHARED-SAME: "-lc"
112108
// CHECK-SPARC32-SHARED-NOT: "-lgcc"
113-
// CHECK-SPARC32-SHARED-NOT: "-lm"
109+
110+
// Check that libm is only linked with clang++.
111+
// RUN: %clang -### %s --target=sparc-sun-solaris2.11 \
112+
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
113+
// RUN: | FileCheck --check-prefix=CHECK-NOLIBM %s
114+
// RUN: %clang -### %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-NOLIBM %s
117+
// RUN: %clangxx -### %s --target=sparc-sun-solaris2.11 \
118+
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
119+
// RUN: | FileCheck --check-prefix=CHECK-LIBM %s
120+
// RUN: %clangxx -### %s -shared --target=sparc-sun-solaris2.11 \
121+
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
122+
// RUN: | FileCheck --check-prefix=CHECK-LIBM %s
123+
// CHECK-LIBM: "-lm"
124+
// CHECK-NOLIBM-NOT: "-lm"
114125

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

0 commit comments

Comments
 (0)