Skip to content

Commit a1f3797

Browse files
committed
Propogate linker flags when -static-pie is enabled in BareMetal
toolchain Change-Id: I580875585e9eac2e9568e84650265f71d028f3ff
1 parent 64c3ba8 commit a1f3797

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed

clang/lib/Driver/ToolChains/BareMetal.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,12 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
604604
CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
605605

606606
CmdArgs.push_back("-Bstatic");
607+
if (IsStaticPIE) {
608+
CmdArgs.push_back("-pie");
609+
CmdArgs.push_back("--no-dynamic-linker");
610+
CmdArgs.push_back("-z");
611+
CmdArgs.push_back("text");
612+
}
607613

608614
if (const char *LDMOption = getLDMOption(TC.getTriple(), Args)) {
609615
CmdArgs.push_back("-m");

clang/lib/Driver/ToolChains/CommonArgs.cpp

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

2092+
bool tools::getStaticPIE(const ArgList &Args, const ToolChain &TC) {
2093+
bool HasStaticPIE = Args.hasArg(options::OPT_static_pie);
2094+
if (HasStaticPIE && Args.hasArg(options::OPT_no_pie)) {
2095+
const Driver &D = TC.getDriver();
2096+
const llvm::opt::OptTable &Opts = D.getOpts();
2097+
StringRef StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
2098+
StringRef NoPIEName = Opts.getOptionName(options::OPT_nopie);
2099+
D.Diag(diag::err_drv_cannot_mix_options) << StaticPIEName << NoPIEName;
2100+
}
2101+
return HasStaticPIE;
2102+
}
2103+
20922104
// `-falign-functions` indicates that the functions should be aligned to the
20932105
// backend's preferred alignment.
20942106
//

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" "aarch64linux" "-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_linux_eabi" "-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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,19 @@
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,19 @@
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)