Skip to content

Commit 779353e

Browse files
committed
[Driver] Improve legibility of ld -z options on Solaris
Following the lead of the Linux code, this patch passes the `ld -z` options as two separate args on Solaris, improving legibility. For lack of a variadic `std::push_back`, `getAsNeededOption` had to be changed to `addAsNeededOption`, matching other `add*Options` functions, changing callers accordingly. The additional args are also used in a WIP revision of the Solaris GNU ld patch D85309 <https://reviews.llvm.org/D85309>, which will allow runtime selection of the linker to use. Tested on `amd64-pc-solaris2.11` and `x86_64-pc-linux-gnu`. Differential Revision: https://reviews.llvm.org/D158955
1 parent cec855a commit 779353e

File tree

10 files changed

+54
-40
lines changed

10 files changed

+54
-40
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -999,24 +999,30 @@ static bool addSanitizerDynamicList(const ToolChain &TC, const ArgList &Args,
999999
return false;
10001000
}
10011001

1002-
const char *tools::getAsNeededOption(const ToolChain &TC, bool as_needed) {
1002+
void tools::addAsNeededOption(const ToolChain &TC,
1003+
const llvm::opt::ArgList &Args,
1004+
llvm::opt::ArgStringList &CmdArgs,
1005+
bool as_needed) {
10031006
assert(!TC.getTriple().isOSAIX() &&
10041007
"AIX linker does not support any form of --as-needed option yet.");
10051008

10061009
// While the Solaris 11.2 ld added --as-needed/--no-as-needed as aliases
10071010
// for the native forms -z ignore/-z record, they are missing in Illumos,
10081011
// so always use the native form.
1009-
if (TC.getTriple().isOSSolaris())
1010-
return as_needed ? "-zignore" : "-zrecord";
1011-
else
1012-
return as_needed ? "--as-needed" : "--no-as-needed";
1012+
if (TC.getTriple().isOSSolaris()) {
1013+
CmdArgs.push_back("-z");
1014+
CmdArgs.push_back(as_needed ? "ignore" : "record");
1015+
} else {
1016+
CmdArgs.push_back(as_needed ? "--as-needed" : "--no-as-needed");
1017+
}
10131018
}
10141019

10151020
void tools::linkSanitizerRuntimeDeps(const ToolChain &TC,
1021+
const llvm::opt::ArgList &Args,
10161022
ArgStringList &CmdArgs) {
10171023
// Force linking against the system libraries sanitizers depends on
10181024
// (see PR15823 why this is necessary).
1019-
CmdArgs.push_back(getAsNeededOption(TC, false));
1025+
addAsNeededOption(TC, Args, CmdArgs, false);
10201026
// There's no libpthread or librt on RTEMS & Android.
10211027
if (TC.getTriple().getOS() != llvm::Triple::RTEMS &&
10221028
!TC.getTriple().isAndroid() && !TC.getTriple().isOHOSFamily()) {
@@ -1260,8 +1266,10 @@ bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringLis
12601266
return false;
12611267
}
12621268

1263-
void tools::linkXRayRuntimeDeps(const ToolChain &TC, ArgStringList &CmdArgs) {
1264-
CmdArgs.push_back(getAsNeededOption(TC, false));
1269+
void tools::linkXRayRuntimeDeps(const ToolChain &TC,
1270+
const llvm::opt::ArgList &Args,
1271+
ArgStringList &CmdArgs) {
1272+
addAsNeededOption(TC, Args, CmdArgs, false);
12651273
CmdArgs.push_back("-lpthread");
12661274
if (!TC.getTriple().isOSOpenBSD())
12671275
CmdArgs.push_back("-lrt");
@@ -1805,7 +1813,7 @@ static void AddUnwindLibrary(const ToolChain &TC, const Driver &D,
18051813
!TC.getTriple().isAndroid() &&
18061814
!TC.getTriple().isOSCygMing() && !TC.getTriple().isOSAIX();
18071815
if (AsNeeded)
1808-
CmdArgs.push_back(getAsNeededOption(TC, true));
1816+
addAsNeededOption(TC, Args, CmdArgs, true);
18091817

18101818
switch (UNW) {
18111819
case ToolChain::UNW_None:
@@ -1839,7 +1847,7 @@ static void AddUnwindLibrary(const ToolChain &TC, const Driver &D,
18391847
}
18401848

18411849
if (AsNeeded)
1842-
CmdArgs.push_back(getAsNeededOption(TC, false));
1850+
addAsNeededOption(TC, Args, CmdArgs, false);
18431851
}
18441852

18451853
static void AddLibgcc(const ToolChain &TC, const Driver &D,

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args,
4040
llvm::opt::ArgStringList &CmdArgs);
4141

4242
void linkSanitizerRuntimeDeps(const ToolChain &TC,
43+
const llvm::opt::ArgList &Args,
4344
llvm::opt::ArgStringList &CmdArgs);
4445

4546
bool addXRayRuntime(const ToolChain &TC, const llvm::opt::ArgList &Args,
4647
llvm::opt::ArgStringList &CmdArgs);
4748

48-
void linkXRayRuntimeDeps(const ToolChain &TC,
49+
void linkXRayRuntimeDeps(const ToolChain &TC, const llvm::opt::ArgList &Args,
4950
llvm::opt::ArgStringList &CmdArgs);
5051

5152
void AddRunTimeLibs(const ToolChain &TC, const Driver &D,
@@ -142,7 +143,8 @@ void addHIPRuntimeLibArgs(const ToolChain &TC, Compilation &C,
142143
const llvm::opt::ArgList &Args,
143144
llvm::opt::ArgStringList &CmdArgs);
144145

145-
const char *getAsNeededOption(const ToolChain &TC, bool as_needed);
146+
void addAsNeededOption(const ToolChain &TC, const llvm::opt::ArgList &Args,
147+
llvm::opt::ArgStringList &CmdArgs, bool as_needed);
146148

147149
llvm::opt::Arg *getLastCSProfileGenerateArg(const llvm::opt::ArgList &Args);
148150
llvm::opt::Arg *getLastProfileUseArg(const llvm::opt::ArgList &Args);

clang/lib/Driver/ToolChains/FreeBSD.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,9 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
303303
CmdArgs.push_back("-lm");
304304
}
305305
if (NeedsSanitizerDeps)
306-
linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
306+
linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs);
307307
if (NeedsXRayDeps)
308-
linkXRayRuntimeDeps(ToolChain, CmdArgs);
308+
linkXRayRuntimeDeps(ToolChain, Args, CmdArgs);
309309
// FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
310310
// the default system libraries. Just mimic this for now.
311311
if (Profiling)

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,10 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
588588
CmdArgs.push_back("--start-group");
589589

590590
if (NeedsSanitizerDeps)
591-
linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
591+
linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs);
592592

593593
if (NeedsXRayDeps)
594-
linkXRayRuntimeDeps(ToolChain, CmdArgs);
594+
linkXRayRuntimeDeps(ToolChain, Args, CmdArgs);
595595

596596
bool WantPthread = Args.hasArg(options::OPT_pthread) ||
597597
Args.hasArg(options::OPT_pthreads);

clang/lib/Driver/ToolChains/Hexagon.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,12 @@ constructHexagonLinkArgs(Compilation &C, const JobAction &JA,
369369

370370
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
371371
if (NeedsSanitizerDeps) {
372-
linkSanitizerRuntimeDeps(HTC, CmdArgs);
372+
linkSanitizerRuntimeDeps(HTC, Args, CmdArgs);
373373

374374
CmdArgs.push_back("-lunwind");
375375
}
376376
if (NeedsXRayDeps)
377-
linkXRayRuntimeDeps(HTC, CmdArgs);
377+
linkXRayRuntimeDeps(HTC, Args, CmdArgs);
378378

379379
CmdArgs.push_back("-lclang_rt.builtins-hexagon");
380380
CmdArgs.push_back("-lc");

clang/lib/Driver/ToolChains/NetBSD.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
305305
CmdArgs.push_back("-lm");
306306
}
307307
if (NeedsSanitizerDeps)
308-
linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
308+
linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs);
309309
if (NeedsXRayDeps)
310-
linkXRayRuntimeDeps(ToolChain, CmdArgs);
310+
linkXRayRuntimeDeps(ToolChain, Args, CmdArgs);
311311
if (Args.hasArg(options::OPT_pthread))
312312
CmdArgs.push_back("-lpthread");
313313
CmdArgs.push_back("-lc");

clang/lib/Driver/ToolChains/OpenBSD.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
220220
}
221221
if (NeedsSanitizerDeps) {
222222
CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins"));
223-
linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
223+
linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs);
224224
}
225225
if (NeedsXRayDeps) {
226226
CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins"));
227-
linkXRayRuntimeDeps(ToolChain, CmdArgs);
227+
linkXRayRuntimeDeps(ToolChain, Args, CmdArgs);
228228
}
229229
// FIXME: For some reason GCC passes -lgcc before adding
230230
// the default system libraries. Just mimic this for now.

clang/lib/Driver/ToolChains/Solaris.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
164164
// LLVM support for atomics on 32-bit SPARC V8+ is incomplete, so
165165
// forcibly link with libatomic as a workaround.
166166
if (getToolChain().getTriple().getArch() == llvm::Triple::sparc) {
167-
CmdArgs.push_back(getAsNeededOption(getToolChain(), true));
167+
addAsNeededOption(getToolChain(), Args, CmdArgs, true);
168168
CmdArgs.push_back("-latomic");
169-
CmdArgs.push_back(getAsNeededOption(getToolChain(), false));
169+
addAsNeededOption(getToolChain(), Args, CmdArgs, false);
170170
}
171171
CmdArgs.push_back("-lgcc_s");
172172
CmdArgs.push_back("-lc");
@@ -176,20 +176,24 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
176176
}
177177
const SanitizerArgs &SA = getToolChain().getSanitizerArgs(Args);
178178
if (NeedsSanitizerDeps) {
179-
linkSanitizerRuntimeDeps(getToolChain(), CmdArgs);
179+
linkSanitizerRuntimeDeps(getToolChain(), Args, CmdArgs);
180180

181181
// Work around Solaris/amd64 ld bug when calling __tls_get_addr directly.
182182
// However, ld -z relax=transtls is available since Solaris 11.2, but not
183183
// in Illumos.
184184
if (getToolChain().getTriple().getArch() == llvm::Triple::x86_64 &&
185185
(SA.needsAsanRt() || SA.needsStatsRt() ||
186-
(SA.needsUbsanRt() && !SA.requiresMinimalRuntime())))
187-
CmdArgs.push_back("-zrelax=transtls");
186+
(SA.needsUbsanRt() && !SA.requiresMinimalRuntime()))) {
187+
CmdArgs.push_back("-z");
188+
CmdArgs.push_back("relax=transtls");
189+
}
188190
}
189191
// Avoid AsanInitInternal cycle, Issue #64126.
190192
if (getToolChain().getTriple().isX86() && SA.needsSharedRt() &&
191-
SA.needsAsanRt())
192-
CmdArgs.push_back("-znow");
193+
SA.needsAsanRt()) {
194+
CmdArgs.push_back("-z");
195+
CmdArgs.push_back("now");
196+
}
193197
}
194198

195199
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,

clang/test/Driver/solaris-ld-sanitizer.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,49 @@
66
// RUN: %clang --target=sparc-sun-solaris2.11 %s -### 2>&1 \
77
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_sparc_tree \
88
// RUN: | FileCheck --check-prefix=CHECK-LD-SPARC32 %s
9-
// CHECK-LD-SPARC32-NOT: -zrelax=transtls
9+
// CHECK-LD-SPARC32-NOT: "-z" "relax=transtls"
1010

1111
/// Check sparc-sun-solaris2.11, 32bit
1212
// RUN: %clang -fsanitize=undefined --target=sparc-sun-solaris2.11 %s -### 2>&1 \
1313
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_sparc_tree \
1414
// RUN: | FileCheck --check-prefix=CHECK-LD-SPARC32 %s
15-
// CHECK-LD-SPARC32-NOT: -zrelax=transtls
15+
// CHECK-LD-SPARC32-NOT: "-z" "relax=transtls"
1616

1717
/// Check sparc-sun-solaris2.11, 64bit
1818
// RUN: %clang -m64 --target=sparc-sun-solaris2.11 %s -### 2>&1 \
1919
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_sparc_tree \
2020
// RUN: | FileCheck --check-prefix=CHECK-LD-SPARC64 %s
21-
// CHECK-LD-SPARC64-NOT: -zrelax=transtls
21+
// CHECK-LD-SPARC64-NOT: "-z" "relax=transtls"
2222

2323
/// Check sparc-sun-solaris2.11, 64bit
2424
// RUN: %clang -m64 -fsanitize=undefined --target=sparc-sun-solaris2.11 %s -### 2>&1 \
2525
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_sparc_tree \
2626
// RUN: | FileCheck --check-prefix=CHECK-LD-SPARC64 %s
27-
// CHECK-LD-SPARC64-NOT: -zrelax=transtls
27+
// CHECK-LD-SPARC64-NOT: "-z" "relax=transtls"
2828

2929
/// Check i386-pc-solaris2.11, 32bit
3030
// RUN: %clang --target=i386-pc-solaris2.11 %s -### 2>&1 \
3131
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_x86_tree \
3232
// RUN: | FileCheck --check-prefix=CHECK-LD-X32 %s
33-
// CHECK-LD-X32-NOT: -zrelax=transtls
33+
// CHECK-LD-X32-NOT: "-z" "relax=transtls"
3434

3535
/// Check i386-pc-solaris2.11, 32bit
3636
// RUN: %clang -fsanitize=undefined --target=i386-pc-solaris2.11 %s -### 2>&1 \
3737
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_x86_tree \
3838
// RUN: | FileCheck --check-prefix=CHECK-LD-X32 %s
39-
// CHECK-LD-X32-NOT: -zrelax=transtls
39+
// CHECK-LD-X32-NOT: "-z" "relax=transtls"
4040

4141
/// Check i386-pc-solaris2.11, 64bit
4242
// RUN: %clang -m64 --target=i386-pc-solaris2.11 %s -### 2>&1 \
4343
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_x86_tree \
4444
// RUN: | FileCheck --check-prefix=CHECK-LD-X64 %s
45-
// CHECK-LD-X64-NOT: -zrelax=transtls
45+
// CHECK-LD-X64-NOT: "-z" "relax=transtls"
4646

4747
/// Check i386-pc-solaris2.11, 64bit
4848
// RUN: %clang -m64 -fsanitize=undefined --target=i386-pc-solaris2.11 %s -### 2>&1 \
4949
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_x86_tree \
5050
// RUN: | FileCheck --check-prefix=CHECK-LD-X64-UBSAN %s
51-
// CHECK-LD-X64-UBSAN: -zrelax=transtls
51+
// CHECK-LD-X64-UBSAN: "-z" "relax=transtls"
5252

5353
/// General tests that the ld -z now workaround is only applied on
5454
/// Solaris/i386 with shared libclang_rt.asan.. Note that we use sysroot to
@@ -58,10 +58,10 @@
5858
// RUN: %clang -fsanitize=address -shared-libasan --target=i386-pc-solaris2.11 %s -### 2>&1 \
5959
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_x86_tree \
6060
// RUN: | FileCheck --check-prefix=CHECK-LD-X32-ASAN-SHARED %s
61-
// CHECK-LD-X32-ASAN-SHARED: -znow
61+
// CHECK-LD-X32-ASAN-SHARED: "-z" "now"
6262

6363
/// Check i386-pc-solaris2.11, 32bit, static libclang_rt.asan
6464
// RUN: %clang -fsanitize=address --target=i386-pc-solaris2.11 %s -### 2>&1 \
6565
// RUN: --gcc-toolchain="" --sysroot=%S/Inputs/solaris_x86_tree \
6666
// RUN: | FileCheck --check-prefix=CHECK-LD-X32-ASAN %s
67-
// CHECK-LD-X32-ASAN-NOT: -znow
67+
// CHECK-LD-X32-ASAN-NOT: "-z" "now"

clang/test/Driver/solaris-ld.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// CHECK-LD-SPARC32-SAME: "-L[[SYSROOT]]/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2"
1717
// CHECK-LD-SPARC32-SAME: "-L[[SYSROOT]]/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/../../.."
1818
// CHECK-LD-SPARC32-SAME: "-L[[SYSROOT]]/usr/lib"
19-
// CHECK-LD-SPARC32-SAME: "-zignore" "-latomic" "-zrecord"
19+
// CHECK-LD-SPARC32-SAME: "-z" "ignore" "-latomic" "-z" "record"
2020
// CHECK-LD-SPARC32-SAME: "-lgcc_s"
2121
// CHECK-LD-SPARC32-SAME: "-lc"
2222
// CHECK-LD-SPARC32-SAME: "-lgcc"

0 commit comments

Comments
 (0)