Skip to content

Commit c6420bc

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

File tree

8 files changed

+79
-16
lines changed

8 files changed

+79
-16
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
@@ -599,11 +599,18 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
599599
const Driver &D = getToolChain().getDriver();
600600
const llvm::Triple::ArchType Arch = TC.getArch();
601601
const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
602+
const bool IsStaticPIE = getStaticPIE(Args, TC);
602603

603604
if (!D.SysRoot.empty())
604605
CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
605606

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

608615
if (const char *LDMOption = getLDMOption(TC.getTriple(), Args)) {
609616
CmdArgs.push_back("-m");
@@ -633,14 +640,18 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
633640

634641
const char *CRTBegin, *CRTEnd;
635642
if (NeedCRTs) {
636-
if (!Args.hasArg(options::OPT_r))
637-
CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt0.o")));
643+
if (!Args.hasArg(options::OPT_r)) {
644+
const char *crt = "crt0.o";
645+
if (IsStaticPIE)
646+
crt = "rcrt1.o";
647+
CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath(crt)));
648+
}
638649
if (TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D)) {
639650
auto RuntimeLib = TC.GetRuntimeLibType(Args);
640651
switch (RuntimeLib) {
641652
case (ToolChain::RLT_Libgcc): {
642-
CRTBegin = "crtbegin.o";
643-
CRTEnd = "crtend.o";
653+
CRTBegin = IsStaticPIE ? "crtbeginS.o" : "crtbegin.o";
654+
CRTEnd = IsStaticPIE ? "crtendS.o" : "crtend.o";
644655
break;
645656
}
646657
case (ToolChain::RLT_CompilerRT): {

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/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" "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)