[clang] c45b4e4 - [Hexagon] Link static PIE executables against rcrt1.o (#210125)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jul 19 08:48:53 PDT 2026
Author: Brian Cain
Date: 2026-07-19T10:48:48-05:00
New Revision: c45b4e4d00bed488d6ece5608560561732ae5b9e
URL: https://github.com/llvm/llvm-project/commit/c45b4e4d00bed488d6ece5608560561732ae5b9e
DIFF: https://github.com/llvm/llvm-project/commit/c45b4e4d00bed488d6ece5608560561732ae5b9e.diff
LOG: [Hexagon] Link static PIE executables against rcrt1.o (#210125)
We always selected crt1.o as the CRT start file, regardless of link
mode.
Select rcrt1.o - the self-relocating static-PIE start file when -static
and PIE are active
Added:
Modified:
clang/lib/Driver/ToolChains/Hexagon.cpp
clang/test/Driver/hexagon-toolchain-linux.c
Removed:
################################################################################
diff --git a/clang/lib/Driver/ToolChains/Hexagon.cpp b/clang/lib/Driver/ToolChains/Hexagon.cpp
index f5b0df3bf0b8d..2ddc15ddbd818 100644
--- a/clang/lib/Driver/ToolChains/Hexagon.cpp
+++ b/clang/lib/Driver/ToolChains/Hexagon.cpp
@@ -365,11 +365,14 @@ constructHexagonLinkArgs(Compilation &C, const JobAction &JA,
};
if (!Args.hasArg(options::OPT_shared, options::OPT_nostartfiles,
- options::OPT_nostdlib, options::OPT_r))
- CmdArgs.push_back(Args.MakeArgString(StartFile("crt1.o")));
- else if (Args.hasArg(options::OPT_shared) &&
- !Args.hasArg(options::OPT_nostartfiles, options::OPT_nostdlib,
- options::OPT_r))
+ options::OPT_nostdlib, options::OPT_r)) {
+ if (IsStatic && IsPIE)
+ CmdArgs.push_back(Args.MakeArgString(StartFile("rcrt1.o")));
+ else
+ CmdArgs.push_back(Args.MakeArgString(StartFile("crt1.o")));
+ } else if (Args.hasArg(options::OPT_shared) &&
+ !Args.hasArg(options::OPT_nostartfiles, options::OPT_nostdlib,
+ options::OPT_r))
CmdArgs.push_back(Args.MakeArgString(StartFile("crti.o")));
if (!MLSuffix.empty()) {
diff --git a/clang/test/Driver/hexagon-toolchain-linux.c b/clang/test/Driver/hexagon-toolchain-linux.c
index ed2acd9c61c2e..1365c6adc22eb 100644
--- a/clang/test/Driver/hexagon-toolchain-linux.c
+++ b/clang/test/Driver/hexagon-toolchain-linux.c
@@ -200,6 +200,49 @@
// CHECK-RELOC-NORMAL: "-dynamic-linker={{/|\\\\}}lib{{/|\\\\}}ld-musl-hexagon.so.1"
// CHECK-RELOC-NORMAL: "{{.*}}crt1.o"
+// -----------------------------------------------------------------------------
+// Startup object: -static alone gets PIE by default (see PIE-DEFAULT above),
+// so it must link the self-relocating rcrt1.o, not the plain crt1.o. Plain
+// crt1.o never processes the R_HEX_RELATIVE relocations a static-PIE image
+// needs, and the resulting binary crashes during libc startup.
+// -----------------------------------------------------------------------------
+// RUN: %clang -### --target=hexagon-unknown-linux-musl \
+// RUN: -ccc-install-dir %S/Inputs/hexagon_tree/Tools/bin \
+// RUN: -mcpu=hexagonv60 \
+// RUN: --sysroot=%S/Inputs/basic_linux_libcxx_tree -static %s 2>&1 \
+// RUN: | FileCheck -check-prefix=CHECK-STATIC-PIE %s
+// CHECK-STATIC-PIE: "-static" "-pie"
+// CHECK-STATIC-PIE: "{{.*}}basic_linux_libcxx_tree{{/|\\\\}}usr{{/|\\\\}}lib{{/|\\\\}}rcrt1.o"
+// CHECK-STATIC-PIE-NOT: "{{.*}}basic_linux_libcxx_tree{{/|\\\\}}usr{{/|\\\\}}lib{{/|\\\\}}crt1.o"
+
+// -----------------------------------------------------------------------------
+// Startup object: -static -no-pie must still link the plain, non-relocating
+// crt1.o (a static non-PIE image has no relocations to apply).
+// -----------------------------------------------------------------------------
+// RUN: %clang -### --target=hexagon-unknown-linux-musl \
+// RUN: -ccc-install-dir %S/Inputs/hexagon_tree/Tools/bin \
+// RUN: -mcpu=hexagonv60 \
+// RUN: --sysroot=%S/Inputs/basic_linux_libcxx_tree -static -no-pie %s 2>&1 \
+// RUN: | FileCheck -check-prefix=CHECK-STATIC-NO-PIE %s
+// CHECK-STATIC-NO-PIE: "-static"
+// CHECK-STATIC-NO-PIE-NOT: "-pie"
+// CHECK-STATIC-NO-PIE: "{{.*}}basic_linux_libcxx_tree{{/|\\\\}}usr{{/|\\\\}}lib{{/|\\\\}}crt1.o"
+// CHECK-STATIC-NO-PIE-NOT: "{{.*}}basic_linux_libcxx_tree{{/|\\\\}}usr{{/|\\\\}}lib{{/|\\\\}}rcrt1.o"
+
+// -----------------------------------------------------------------------------
+// Startup object: a non-static (dynamic) link keeps using crt1.o even though
+// PIE is on by default -- relocation is the dynamic linker's job there, not
+// the CRT's.
+// -----------------------------------------------------------------------------
+// RUN: %clang -### --target=hexagon-unknown-linux-musl \
+// RUN: -ccc-install-dir %S/Inputs/hexagon_tree/Tools/bin \
+// RUN: -mcpu=hexagonv60 \
+// RUN: --sysroot=%S/Inputs/basic_linux_libcxx_tree %s 2>&1 \
+// RUN: | FileCheck -check-prefix=CHECK-DYNAMIC-PIE %s
+// CHECK-DYNAMIC-PIE: "-pie"
+// CHECK-DYNAMIC-PIE: "{{.*}}basic_linux_libcxx_tree{{/|\\\\}}usr{{/|\\\\}}lib{{/|\\\\}}crt1.o"
+// CHECK-DYNAMIC-PIE-NOT: "{{.*}}basic_linux_libcxx_tree{{/|\\\\}}usr{{/|\\\\}}lib{{/|\\\\}}rcrt1.o"
+
// -----------------------------------------------------------------------------
// Sanitizer library paths: -fsanitize=memory
// -----------------------------------------------------------------------------
More information about the cfe-commits
mailing list