Skip to content

Commit 62945bb

Browse files
committed
[Driver] Add PIE support on Solaris
`clang` currently lacks PIE support on Solaris. This patch fixes this, also linking with `crtbeginS.o` and `crtendS.o` for `-pie` and `-shared`. Tested on `amd64-pc-solaris2.11`, `sparcv9-sun-solaris2.11`, and `x86_64-pc-linux-gnu`. Differential Revision: https://reviews.llvm.org/D158206
1 parent 2de024e commit 62945bb

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

clang/lib/Driver/ToolChains/Solaris.cpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,24 @@ void solaris::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
4747
Exec, CmdArgs, Inputs, Output));
4848
}
4949

50+
static bool getPIE(const ArgList &Args, const ToolChain &TC) {
51+
if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_static) ||
52+
Args.hasArg(options::OPT_r))
53+
return false;
54+
55+
Arg *A = Args.getLastArg(options::OPT_pie, options::OPT_no_pie,
56+
options::OPT_nopie);
57+
if (!A)
58+
return TC.isPIEDefault(Args);
59+
return A->getOption().matches(options::OPT_pie);
60+
}
61+
5062
void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
5163
const InputInfo &Output,
5264
const InputInfoList &Inputs,
5365
const ArgList &Args,
5466
const char *LinkingOutput) const {
67+
const bool IsPIE = getPIE(Args, getToolChain());
5568
ArgStringList CmdArgs;
5669

5770
// Demangle C++ names in errors
@@ -62,6 +75,11 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
6275
CmdArgs.push_back("_start");
6376
}
6477

78+
if (IsPIE) {
79+
CmdArgs.push_back("-z");
80+
CmdArgs.push_back("type=pie");
81+
}
82+
6583
if (Args.hasArg(options::OPT_static)) {
6684
CmdArgs.push_back("-Bstatic");
6785
CmdArgs.push_back("-dn");
@@ -113,8 +131,13 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
113131
values_xpg = "values-xpg4.o";
114132
CmdArgs.push_back(
115133
Args.MakeArgString(getToolChain().GetFilePath(values_xpg)));
116-
CmdArgs.push_back(
117-
Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
134+
135+
const char *crtbegin = nullptr;
136+
if (Args.hasArg(options::OPT_shared) || IsPIE)
137+
crtbegin = "crtbeginS.o";
138+
else
139+
crtbegin = "crtbegin.o";
140+
CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(crtbegin)));
118141
// Add crtfastmath.o if available and fast math is enabled.
119142
getToolChain().addFastMathRuntimeIfAvailable(Args, CmdArgs);
120143
}
@@ -171,8 +194,12 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
171194

172195
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
173196
options::OPT_r)) {
174-
CmdArgs.push_back(
175-
Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
197+
if (Args.hasArg(options::OPT_shared) || IsPIE)
198+
CmdArgs.push_back(
199+
Args.MakeArgString(getToolChain().GetFilePath("crtendS.o")));
200+
else
201+
CmdArgs.push_back(
202+
Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
176203
CmdArgs.push_back(
177204
Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
178205
}

clang/test/Driver/Inputs/solaris_sparc_tree/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/crtbeginS.o

Whitespace-only changes.

clang/test/Driver/Inputs/solaris_sparc_tree/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/sparcv9/crtbeginS.o

Whitespace-only changes.

clang/test/Driver/solaris-ld.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,33 @@
106106
// CHECK-SPARC32-SHARED-NOT: "-lgcc"
107107
// CHECK-SPARC32-SHARED-NOT: "-lm"
108108

109+
// Check the right ld flags are present with -pie.
110+
// RUN: %clang --target=sparc-sun-solaris2.11 -### %s -pie \
111+
// RUN: --gcc-toolchain="" \
112+
// RUN: --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
113+
// RUN: | FileCheck --check-prefix=CHECK-PIE %s
114+
// RUN: %clang --target=sparc-sun-solaris2.11 -### %s -nopie \
115+
// RUN: --gcc-toolchain="" \
116+
// RUN: --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
117+
// RUN: | FileCheck --check-prefix=CHECK-NOPIE %s
118+
119+
// Check that -shared/-r/-static disable PIE.
120+
// RUN: %clang --target=sparc-sun-solaris2.11 -### %s -shared -pie \
121+
// RUN: --gcc-toolchain="" \
122+
// RUN: --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
123+
// RUN: | FileCheck --check-prefix=CHECK-NOPIE %s
124+
// RUN: %clang --target=sparc-sun-solaris2.11 -### %s -r -pie \
125+
// RUN: --gcc-toolchain="" \
126+
// RUN: --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
127+
// RUN: | FileCheck --check-prefix=CHECK-NOPIE %s
128+
// RUN: %clang --target=sparc-sun-solaris2.11 -### %s -static -pie \
129+
// RUN: --gcc-toolchain="" \
130+
// RUN: --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
131+
// RUN: | FileCheck --check-prefix=CHECK-NOPIE %s
132+
133+
// CHECK-PIE: "-z" "type=pie"
134+
// CHECK-NOPIE-NOT: "-z" "type=pie"
135+
109136
// -r suppresses default -l and crt*.o, values-*.o like -nostdlib.
110137
// RUN: %clang -### %s --target=sparc-sun-solaris2.11 -r 2>&1 \
111138
// RUN: | FileCheck %s --check-prefix=CHECK-RELOCATABLE
@@ -115,6 +142,28 @@
115142
// CHECK-RELOCATABLE-NOT: /crt{{[^.]+}}.o
116143
// CHECK-RELOCATABLE-NOT: /values-{{[^.]+}}.o
117144

145+
// Check that crt{begin,end}S.o is linked with -shared/-pie.
146+
// RUN: %clang --target=sparc-sun-solaris2.11 -### %s \
147+
// RUN: --gcc-toolchain="" \
148+
// RUN: --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
149+
// RUN: | FileCheck --check-prefix=CHECK-NOCRTS %s
150+
// RUN: %clang --target=sparc-sun-solaris2.11 -### %s -shared \
151+
// RUN: --gcc-toolchain="" \
152+
// RUN: --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
153+
// RUN: | FileCheck --check-prefix=CHECK-CRTS %s
154+
// RUN: %clang --target=sparc-sun-solaris2.11 -### %s -nopie \
155+
// RUN: --gcc-toolchain="" \
156+
// RUN: --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
157+
// RUN: | FileCheck --check-prefix=CHECK-NOCRTS %s
158+
// RUN: %clang --target=sparc-sun-solaris2.11 -### %s -pie \
159+
// RUN: --gcc-toolchain="" \
160+
// RUN: --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
161+
// RUN: | FileCheck --check-prefix=CHECK-CRTS %s
162+
// CHECK-CRTS: crtbeginS.o
163+
// CHECK-CRTS: crtendS.o
164+
// CHECK-NOCRTS-NOT: crtbeginS.o
165+
// CHECK-NOCRTS-NOT: crtendS.o
166+
118167
// Check that crtfastmath.o is linked with -ffast-math.
119168

120169
// Check sparc-sun-solaris2.11, 32bit

0 commit comments

Comments
 (0)