Skip to content

Add necessary linker flags when -static-pie is enabled in BareMetal Toolchain #147589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/Driver/CommonArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ const char *RelocationModelName(llvm::Reloc::Model Model);
std::tuple<llvm::Reloc::Model, unsigned, bool>
ParsePICArgs(const ToolChain &ToolChain, const llvm::opt::ArgList &Args);

bool getStaticPIE(const llvm::opt::ArgList &Args, const ToolChain &TC);

unsigned ParseFunctionAlignment(const ToolChain &TC,
const llvm::opt::ArgList &Args);

Expand Down
19 changes: 15 additions & 4 deletions clang/lib/Driver/ToolChains/BareMetal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,18 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
const Driver &D = getToolChain().getDriver();
const llvm::Triple::ArchType Arch = TC.getArch();
const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
const bool IsStaticPIE = getStaticPIE(Args, TC);

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

CmdArgs.push_back("-Bstatic");
if (IsStaticPIE) {
CmdArgs.push_back("-pie");
CmdArgs.push_back("--no-dynamic-linker");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already the default for LLD, is this needed for other linkers like GNU ld?

Copy link
Contributor Author

@quic-garvgupt quic-garvgupt Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is needed for both LLD and GNU LD. I dont see static -pie flags being passed by default for LLD if -static-pie is passed to clang driver. Moreover, without this change we get a unused argument warning as well.
Pls see below for verbose logs.

clang --target=aarch64-none-elf -fuse-ld=lld -static-pie empty.c -###

clang: warning: argument unused during compilation: '-static-pie' [-Wunused-command-line-argument]
...............................
ld.lld" "-Bstatic" "-m" "aarch64linux" "-EL" "crt0.o" "-L<path-to-bin>/../lib/clang-runtimes/aarch64-none-elf/lib" "-L<path>lib/clang/21/lib/aarch64-unknown-none-elf" "-L<path-to-bin>/../lib/clang-runtimes/aarch64-none-elf/lib" "/tmp/empty-09660d.o" "--start-group" "<path>/lib/clang/21/lib/aarch64-unknown-none-elf/libclang_rt.builtins.a" "-lc" "--end-group" "-o" "a.out"

Pls let me know if I missed something in your comment or interpreted it differently.

CmdArgs.push_back("-z");
CmdArgs.push_back("text");
}

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

const char *CRTBegin, *CRTEnd;
if (NeedCRTs) {
if (!Args.hasArg(options::OPT_r))
CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt0.o")));
if (!Args.hasArg(options::OPT_r)) {
const char *crt = "crt0.o";
if (IsStaticPIE)
crt = "rcrt1.o";
CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath(crt)));
}
if (TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D)) {
auto RuntimeLib = TC.GetRuntimeLibType(Args);
switch (RuntimeLib) {
case (ToolChain::RLT_Libgcc): {
CRTBegin = "crtbegin.o";
CRTEnd = "crtend.o";
CRTBegin = IsStaticPIE ? "crtbeginS.o" : "crtbegin.o";
CRTEnd = IsStaticPIE ? "crtendS.o" : "crtend.o";
break;
}
case (ToolChain::RLT_CompilerRT): {
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,18 @@ tools::ParsePICArgs(const ToolChain &ToolChain, const ArgList &Args) {
return std::make_tuple(RelocM, 0U, false);
}

bool tools::getStaticPIE(const ArgList &Args, const ToolChain &TC) {
bool HasStaticPIE = Args.hasArg(options::OPT_static_pie);
if (HasStaticPIE && Args.hasArg(options::OPT_no_pie)) {
const Driver &D = TC.getDriver();
const llvm::opt::OptTable &Opts = D.getOpts();
StringRef StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
StringRef NoPIEName = Opts.getOptionName(options::OPT_nopie);
D.Diag(diag::err_drv_cannot_mix_options) << StaticPIEName << NoPIEName;
}
return HasStaticPIE;
}

// `-falign-functions` indicates that the functions should be aligned to the
// backend's preferred alignment.
//
Expand Down
12 changes: 0 additions & 12 deletions clang/lib/Driver/ToolChains/Gnu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,6 @@ void tools::gcc::Linker::RenderExtraToolArgs(const JobAction &JA,
// The types are (hopefully) good enough.
}

static bool getStaticPIE(const ArgList &Args, const ToolChain &TC) {
bool HasStaticPIE = Args.hasArg(options::OPT_static_pie);
if (HasStaticPIE && Args.hasArg(options::OPT_no_pie)) {
const Driver &D = TC.getDriver();
const llvm::opt::OptTable &Opts = D.getOpts();
StringRef StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
StringRef NoPIEName = Opts.getOptionName(options::OPT_nopie);
D.Diag(diag::err_drv_cannot_mix_options) << StaticPIEName << NoPIEName;
}
return HasStaticPIE;
}

static bool getStatic(const ArgList &Args) {
return Args.hasArg(options::OPT_static) &&
!Args.hasArg(options::OPT_static_pie);
Expand Down
12 changes: 12 additions & 0 deletions clang/test/Driver/aarch64-toolchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,15 @@
// AARCH64-BAREMETAL-UNWINDLIB: "{{.*}}clang_rt.crtbegin.o"
// AARCH64-BAREMETAL-UNWINDLIB: "--start-group" "{{.*}}libclang_rt.builtins{{.*}}.a" "--as-needed" "-lunwind" "--no-as-needed" "-lc" "-lgloss" "--end-group"
// AARCH64-BAREMETAL-UNWINDLIB: "{{.*}}clang_rt.crtend.o"

// RUN: %clang -static-pie -### %s -fuse-ld= \
// RUN: --target=aarch64-none-elf --rtlib=libgcc --unwindlib=platform \
// RUN: --gcc-toolchain=%S/Inputs/basic_aarch64_gcc_tree \
// RUN: --sysroot=%S/Inputs/basic_arm_gcc_tree/aarch64-none-elf 2>&1 \
// RUN: | FileCheck -check-prefix=C-ARM-STATIC-PIE %s

// C-ARM-STATIC-PIE: "-Bstatic" "-pie" "--no-dynamic-linker" "-z" "text" "-m" "aarch64linux" "-EL"
// C-ARM-STATIC-PIE: "{{.*}}rcrt1.o"
// C-ARM-STATIC-PIE: "{{.*}}crtbeginS.o"
// C-ARM-STATIC-PIE: "--start-group" "-lgcc" "-lgcc_eh" "-lc" "-lgloss" "--end-group"
// C-ARM-STATIC-PIE: "{{.*}}crtendS.o"
12 changes: 12 additions & 0 deletions clang/test/Driver/arm-toolchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,15 @@
// ARM-BAREMETAL-UNWINDLIB: "{{.*}}clang_rt.crtbegin.o"
// ARM-BAREMETAL-UNWINDLIB: "--start-group" "{{.*}}libclang_rt.builtins.a" "--as-needed" "-lunwind" "--no-as-needed" "-lc" "-lgloss" "--end-group"
// ARM-BAREMETAL-UNWINDLIB: "{{.*}}clang_rt.crtend.o"

// RUN: %clang -static-pie -### %s -fuse-ld= \
// RUN: --target=armv6m-none-eabi --rtlib=libgcc --unwindlib=platform \
// RUN: --gcc-toolchain=%S/Inputs/basic_arm_gcc_tree \
// RUN: --sysroot=%S/Inputs/basic_arm_gcc_tree/armv6m-none-eabi 2>&1 \
// RUN: | FileCheck -check-prefix=C-ARM-STATIC-PIE %s

// C-ARM-STATIC-PIE: "-Bstatic" "-pie" "--no-dynamic-linker" "-z" "text" "-m" "armelf_linux_eabi" "-EL"
// C-ARM-STATIC-PIE: "{{.*}}rcrt1.o"
// C-ARM-STATIC-PIE: "{{.*}}crtbeginS.o"
// C-ARM-STATIC-PIE: "--start-group" "-lgcc" "-lgcc_eh" "-lc" "-lgloss" "--end-group"
// C-ARM-STATIC-PIE: "{{.*}}crtendS.o"
13 changes: 13 additions & 0 deletions clang/test/Driver/riscv32-toolchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,19 @@
// RUN: | FileCheck -check-prefix=CHECK-RV32-GNU-RELAX %s
// CHECK-RV32-GNU-RELAX-NOT: "--no-relax"

// Check that "-static -pie" is forwarded to linker when "-static-pie" is used
// RUN: %clang -static-pie -### %s -fuse-ld= \
// RUN: --target=riscv32-unknown-elf -rtlib=platform --unwindlib=platform \
// RUN: --gcc-toolchain=%S/Inputs/basic_riscv32_tree \
// RUN: --sysroot=%S/Inputs/basic_riscv32_tree/riscv32-unknown-elf 2>&1 \
// RUN: | FileCheck -check-prefix=C-RV32-STATIC-PIE %s

// C-RV32-STATIC-PIE: "-Bstatic" "-pie" "--no-dynamic-linker" "-z" "text" "-m" "elf32lriscv" "-X"
// C-RV32-STATIC-PIE: "{{.*}}rcrt1.o"
// C-RV32-STATIC-PIE: "{{.*}}crtbeginS.o"
// C-RV32-STATIC-PIE: "--start-group" "-lgcc" "-lc" "-lgloss" "--end-group"
// C-RV32-STATIC-PIE: "{{.*}}crtendS.o"

typedef __builtin_va_list va_list;
typedef __SIZE_TYPE__ size_t;
typedef __PTRDIFF_TYPE__ ptrdiff_t;
Expand Down
13 changes: 13 additions & 0 deletions clang/test/Driver/riscv64-toolchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,19 @@
// RUN: | FileCheck -check-prefix=CHECK-RV64-GNU-RELAX %s
// CHECK-RV64-GNU-RELAX-NOT: "--no-relax"

// Check that "-static -pie" is forwarded to linker when "-static-pie" is used
// RUN: %clang -static-pie -### %s -fuse-ld= \
// RUN: --target=riscv64-unknown-elf -rtlib=platform --unwindlib=platform \
// RUN: --gcc-toolchain=%S/Inputs/basic_riscv64_tree \
// RUN: --sysroot=%S/Inputs/basic_riscv64_tree/riscv64-unknown-elf 2>&1 \
// RUN: | FileCheck -check-prefix=C-RV64-STATIC-PIE %s

// C-RV64-STATIC-PIE: "-Bstatic" "-pie" "--no-dynamic-linker" "-z" "text" "-m" "elf64lriscv" "-X"
// C-RV64-STATIC-PIE: "{{.*}}rcrt1.o"
// C-RV64-STATIC-PIE: "{{.*}}crtbeginS.o"
// C-RV64-STATIC-PIE: "--start-group" "-lgcc" "-lc" "-lgloss" "--end-group"
// C-RV64-STATIC-PIE: "{{.*}}crtendS.o"

typedef __builtin_va_list va_list;
typedef __SIZE_TYPE__ size_t;
typedef __PTRDIFF_TYPE__ ptrdiff_t;
Expand Down