Skip to content

Commit 585f27c

Browse files
Add necessary linker flags when -static-pie is enabled in BareMetal Toolchain (#147589)
1 parent 19ada02 commit 585f27c

File tree

8 files changed

+87
-24
lines changed

8 files changed

+87
-24
lines changed

clang/include/clang/Driver/CommonArgs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ const char *RelocationModelName(llvm::Reloc::Model Model);
8585
std::tuple<llvm::Reloc::Model, unsigned, bool>
8686
ParsePICArgs(const ToolChain &ToolChain, const llvm::opt::ArgList &Args);
8787

88+
bool getStaticPIE(const llvm::opt::ArgList &Args, const ToolChain &TC);
89+
8890
unsigned ParseFunctionAlignment(const ToolChain &TC,
8991
const llvm::opt::ArgList &Args);
9092

clang/lib/Driver/ToolChains/BareMetal.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,11 +586,18 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
586586
const Driver &D = getToolChain().getDriver();
587587
const llvm::Triple::ArchType Arch = TC.getArch();
588588
const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
589+
const bool IsStaticPIE = getStaticPIE(Args, TC);
589590

590591
if (!D.SysRoot.empty())
591592
CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
592593

593594
CmdArgs.push_back("-Bstatic");
595+
if (IsStaticPIE) {
596+
CmdArgs.push_back("-pie");
597+
CmdArgs.push_back("--no-dynamic-linker");
598+
CmdArgs.push_back("-z");
599+
CmdArgs.push_back("text");
600+
}
594601

595602
if (const char *LDMOption = getLDMOption(TC.getTriple(), Args)) {
596603
CmdArgs.push_back("-m");
@@ -620,14 +627,18 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
620627

621628
const char *CRTBegin, *CRTEnd;
622629
if (NeedCRTs) {
623-
if (!Args.hasArg(options::OPT_r))
624-
CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt0.o")));
630+
if (!Args.hasArg(options::OPT_r)) {
631+
const char *crt = "crt0.o";
632+
if (IsStaticPIE)
633+
crt = "rcrt1.o";
634+
CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath(crt)));
635+
}
625636
if (TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D)) {
626637
auto RuntimeLib = TC.GetRuntimeLibType(Args);
627638
switch (RuntimeLib) {
628639
case (ToolChain::RLT_Libgcc): {
629-
CRTBegin = "crtbegin.o";
630-
CRTEnd = "crtend.o";
640+
CRTBegin = IsStaticPIE ? "crtbeginS.o" : "crtbegin.o";
641+
CRTEnd = IsStaticPIE ? "crtendS.o" : "crtend.o";
631642
break;
632643
}
633644
case (ToolChain::RLT_CompilerRT): {

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,6 +2132,18 @@ tools::ParsePICArgs(const ToolChain &ToolChain, const ArgList &Args) {
21322132
return std::make_tuple(RelocM, 0U, false);
21332133
}
21342134

2135+
bool tools::getStaticPIE(const ArgList &Args, const ToolChain &TC) {
2136+
bool HasStaticPIE = Args.hasArg(options::OPT_static_pie);
2137+
if (HasStaticPIE && Args.hasArg(options::OPT_no_pie)) {
2138+
const Driver &D = TC.getDriver();
2139+
const llvm::opt::OptTable &Opts = D.getOpts();
2140+
StringRef StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
2141+
StringRef NoPIEName = Opts.getOptionName(options::OPT_nopie);
2142+
D.Diag(diag::err_drv_cannot_mix_options) << StaticPIEName << NoPIEName;
2143+
}
2144+
return HasStaticPIE;
2145+
}
2146+
21352147
// `-falign-functions` indicates that the functions should be aligned to the
21362148
// backend's preferred alignment.
21372149
//

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,6 @@ void tools::gcc::Linker::RenderExtraToolArgs(const JobAction &JA,
219219
// The types are (hopefully) good enough.
220220
}
221221

222-
static bool getStaticPIE(const ArgList &Args, const ToolChain &TC) {
223-
bool HasStaticPIE = Args.hasArg(options::OPT_static_pie);
224-
if (HasStaticPIE && Args.hasArg(options::OPT_no_pie)) {
225-
const Driver &D = TC.getDriver();
226-
const llvm::opt::OptTable &Opts = D.getOpts();
227-
StringRef StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
228-
StringRef NoPIEName = Opts.getOptionName(options::OPT_nopie);
229-
D.Diag(diag::err_drv_cannot_mix_options) << StaticPIEName << NoPIEName;
230-
}
231-
return HasStaticPIE;
232-
}
233-
234222
static bool getStatic(const ArgList &Args) {
235223
return Args.hasArg(options::OPT_static) &&
236224
!Args.hasArg(options::OPT_static_pie);

clang/test/Driver/aarch64-toolchain.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,15 @@
157157
// AARCH64-BAREMETAL-UNWINDLIB: "{{.*}}clang_rt.crtbegin.o"
158158
// AARCH64-BAREMETAL-UNWINDLIB: "--start-group" "{{.*}}libclang_rt.builtins{{.*}}.a" "--as-needed" "-lunwind" "--no-as-needed" "-lc" "-lgloss" "--end-group"
159159
// AARCH64-BAREMETAL-UNWINDLIB: "{{.*}}clang_rt.crtend.o"
160+
161+
// RUN: %clang -static-pie -### %s -fuse-ld= \
162+
// RUN: --target=aarch64-none-elf --rtlib=libgcc --unwindlib=platform \
163+
// RUN: --gcc-toolchain=%S/Inputs/basic_aarch64_gcc_tree \
164+
// RUN: --sysroot=%S/Inputs/basic_arm_gcc_tree/aarch64-none-elf 2>&1 \
165+
// RUN: | FileCheck -check-prefix=C-ARM-STATIC-PIE %s
166+
167+
// C-ARM-STATIC-PIE: "-Bstatic" "-pie" "--no-dynamic-linker" "-z" "text" "-m" "aarch64elf" "-EL"
168+
// C-ARM-STATIC-PIE: "{{.*}}rcrt1.o"
169+
// C-ARM-STATIC-PIE: "{{.*}}crtbeginS.o"
170+
// C-ARM-STATIC-PIE: "--start-group" "-lgcc" "-lgcc_eh" "-lc" "-lgloss" "--end-group"
171+
// C-ARM-STATIC-PIE: "{{.*}}crtendS.o"

clang/test/Driver/arm-toolchain.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,15 @@
158158
// ARM-BAREMETAL-UNWINDLIB: "{{.*}}clang_rt.crtbegin.o"
159159
// ARM-BAREMETAL-UNWINDLIB: "--start-group" "{{.*}}libclang_rt.builtins.a" "--as-needed" "-lunwind" "--no-as-needed" "-lc" "-lgloss" "--end-group"
160160
// ARM-BAREMETAL-UNWINDLIB: "{{.*}}clang_rt.crtend.o"
161+
162+
// RUN: %clang -static-pie -### %s -fuse-ld= \
163+
// RUN: --target=armv6m-none-eabi --rtlib=libgcc --unwindlib=platform \
164+
// RUN: --gcc-toolchain=%S/Inputs/basic_arm_gcc_tree \
165+
// RUN: --sysroot=%S/Inputs/basic_arm_gcc_tree/armv6m-none-eabi 2>&1 \
166+
// RUN: | FileCheck -check-prefix=C-ARM-STATIC-PIE %s
167+
168+
// C-ARM-STATIC-PIE: "-Bstatic" "-pie" "--no-dynamic-linker" "-z" "text" "-m" "armelf" "-EL"
169+
// C-ARM-STATIC-PIE: "{{.*}}rcrt1.o"
170+
// C-ARM-STATIC-PIE: "{{.*}}crtbeginS.o"
171+
// C-ARM-STATIC-PIE: "--start-group" "-lgcc" "-lgcc_eh" "-lc" "-lgloss" "--end-group"
172+
// C-ARM-STATIC-PIE: "{{.*}}crtendS.o"

clang/test/Driver/riscv32-toolchain.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,38 +215,51 @@
215215

216216
// RUN: %clang --target=riscv32 %s -emit-llvm -S -o - | FileCheck %s
217217

218-
// Check that "--no-relax" is forwarded to the linker for RISC-V.
218+
/// Check that "--no-relax" is forwarded to the linker for RISC-V.
219219
// RUN: env "PATH=" %clang %s -### 2>&1 -mno-relax \
220220
// RUN: --target=riscv32-unknown-elf --rtlib=platform --unwindlib=platform --sysroot= \
221221
// RUN: -march=rv32imac -mabi=lp32\
222222
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_elf_sdk 2>&1 \
223223
// RUN: | FileCheck --check-prefix=CHECK-RV32-NORELAX %s
224224
// CHECK-RV32-NORELAX: "--no-relax"
225225

226-
// Check that "--no-relax" is not forwarded to the linker for RISC-V.
226+
/// Check that "--no-relax" is not forwarded to the linker for RISC-V.
227227
// RUN:env "PATH=" %clang %s -### 2>&1 \
228228
// RUN: --target=riscv32-unknown-elf --rtlib=platform --unwindlib=platform --sysroot= \
229229
// RUN: -march=rv32imac -mabi=lp32\
230230
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_elf_sdk 2>&1 \
231231
// RUN: | FileCheck --check-prefix=CHECK-RV32-RELAX %s
232232
// CHECK-RV32-RELAX-NOT: "--no-relax"
233233

234-
// Check that "--no-relax" is forwarded to the linker for RISC-V (Gnu.cpp).
234+
/// Check that "--no-relax" is forwarded to the linker for RISC-V (Gnu.cpp).
235235
// RUN: env "PATH=" %clang -### %s -fuse-ld=ld -no-pie -mno-relax \
236236
// RUN: --target=riscv32-unknown-linux-gnu --rtlib=platform --unwindlib=platform -mabi=ilp32 \
237237
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
238238
// RUN: --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
239239
// RUN: | FileCheck -check-prefix=CHECK-RV32-GNU-NORELAX %s
240240
// CHECK-RV32-GNU-NORELAX: "--no-relax"
241241

242-
// Check that "--no-relax" is not forwarded to the linker for RISC-V (Gnu.cpp).
242+
/// Check that "--no-relax" is not forwarded to the linker for RISC-V (Gnu.cpp).
243243
// RUN: env "PATH=" %clang -### %s -fuse-ld=ld -no-pie \
244244
// RUN: --target=riscv32-unknown-linux-gnu --rtlib=platform --unwindlib=platform -mabi=ilp32 \
245245
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
246246
// RUN: --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
247247
// RUN: | FileCheck -check-prefix=CHECK-RV32-GNU-RELAX %s
248248
// CHECK-RV32-GNU-RELAX-NOT: "--no-relax"
249249

250+
/// Check that "-static -pie" is forwarded to linker when "-static-pie" is used
251+
// RUN: %clang -static-pie -### %s -fuse-ld= \
252+
// RUN: --target=riscv32-unknown-elf -rtlib=platform --unwindlib=platform \
253+
// RUN: --gcc-toolchain=%S/Inputs/basic_riscv32_tree \
254+
// RUN: --sysroot=%S/Inputs/basic_riscv32_tree/riscv32-unknown-elf 2>&1 \
255+
// RUN: | FileCheck -check-prefix=C-RV32-STATIC-PIE %s
256+
257+
// C-RV32-STATIC-PIE: "-Bstatic" "-pie" "--no-dynamic-linker" "-z" "text" "-m" "elf32lriscv" "-X"
258+
// C-RV32-STATIC-PIE: "{{.*}}rcrt1.o"
259+
// C-RV32-STATIC-PIE: "{{.*}}crtbeginS.o"
260+
// C-RV32-STATIC-PIE: "--start-group" "-lgcc" "-lc" "-lgloss" "--end-group"
261+
// C-RV32-STATIC-PIE: "{{.*}}crtendS.o"
262+
250263
typedef __builtin_va_list va_list;
251264
typedef __SIZE_TYPE__ size_t;
252265
typedef __PTRDIFF_TYPE__ ptrdiff_t;

clang/test/Driver/riscv64-toolchain.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,38 +171,51 @@
171171

172172
// RUN: %clang --target=riscv64 %s -emit-llvm -S -o - | FileCheck %s
173173

174-
// Check that "--no-relax" is forwarded to the linker for RISC-V.
174+
/// Check that "--no-relax" is forwarded to the linker for RISC-V.
175175
// RUN: env "PATH=" %clang %s -### 2>&1 -mno-relax \
176176
// RUN: --target=riscv64-unknown-elf --rtlib=platform --unwindlib=platform --sysroot= \
177177
// RUN: -march=rv64imac -mabi=lp64\
178178
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_elf_sdk 2>&1 \
179179
// RUN: | FileCheck --check-prefix=CHECK-RV64-NORELAX %s
180180
// CHECK-RV64-NORELAX: "--no-relax"
181181

182-
// Check that "--no-relax" is not forwarded to the linker for RISC-V.
182+
/// Check that "--no-relax" is not forwarded to the linker for RISC-V.
183183
// RUN:env "PATH=" %clang %s -### 2>&1 \
184184
// RUN: --target=riscv64-unknown-elf --rtlib=platform --unwindlib=platform --sysroot= \
185185
// RUN: -march=rv64imac -mabi=lp64\
186186
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_elf_sdk 2>&1 \
187187
// RUN: | FileCheck --check-prefix=CHECK-RV64-RELAX %s
188188
// CHECK-RV64-RELAX-NOT: "--no-relax"
189189

190-
// Check that "--no-relax" is forwarded to the linker for RISC-V (Gnu.cpp).
190+
/// Check that "--no-relax" is forwarded to the linker for RISC-V (Gnu.cpp).
191191
// RUN: env "PATH=" %clang -### %s -fuse-ld=ld -no-pie -mno-relax \
192192
// RUN: --target=riscv64-unknown-linux-gnu --rtlib=platform --unwindlib=platform -mabi=lp64 \
193193
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
194194
// RUN: --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
195195
// RUN: | FileCheck -check-prefix=CHECK-RV64-GNU-NORELAX %s
196196
// CHECK-RV64-GNU-NORELAX: "--no-relax"
197197

198-
// Check that "--no-relax" is not forwarded to the linker for RISC-V (Gnu.cpp).
198+
/// Check that "--no-relax" is not forwarded to the linker for RISC-V (Gnu.cpp).
199199
// RUN: env "PATH=" %clang -### %s -fuse-ld=ld -no-pie \
200200
// RUN: --target=riscv64-unknown-linux-gnu --rtlib=platform --unwindlib=platform -mabi=lp64 \
201201
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
202202
// RUN: --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
203203
// RUN: | FileCheck -check-prefix=CHECK-RV64-GNU-RELAX %s
204204
// CHECK-RV64-GNU-RELAX-NOT: "--no-relax"
205205

206+
/// Check that "-static -pie" is forwarded to linker when "-static-pie" is used
207+
// RUN: %clang -static-pie -### %s -fuse-ld= \
208+
// RUN: --target=riscv64-unknown-elf -rtlib=platform --unwindlib=platform \
209+
// RUN: --gcc-toolchain=%S/Inputs/basic_riscv64_tree \
210+
// RUN: --sysroot=%S/Inputs/basic_riscv64_tree/riscv64-unknown-elf 2>&1 \
211+
// RUN: | FileCheck -check-prefix=C-RV64-STATIC-PIE %s
212+
213+
// C-RV64-STATIC-PIE: "-Bstatic" "-pie" "--no-dynamic-linker" "-z" "text" "-m" "elf64lriscv" "-X"
214+
// C-RV64-STATIC-PIE: "{{.*}}rcrt1.o"
215+
// C-RV64-STATIC-PIE: "{{.*}}crtbeginS.o"
216+
// C-RV64-STATIC-PIE: "--start-group" "-lgcc" "-lc" "-lgloss" "--end-group"
217+
// C-RV64-STATIC-PIE: "{{.*}}crtendS.o"
218+
206219
typedef __builtin_va_list va_list;
207220
typedef __SIZE_TYPE__ size_t;
208221
typedef __PTRDIFF_TYPE__ ptrdiff_t;

0 commit comments

Comments
 (0)