[Hexagon] Pass -pie to linker when PIE is the toolchain default (#189723)

The Hexagon driver only checked for an explicit -pie flag when
constructing the link command, ignoring the toolchain's PIE default. For
linux-musl targets, isPIEDefault() returns true (via the Linux toolchain
base class), so the compiler generates PIC/PIE code (-pic-level 2
-pic-is-pie) but the linker never received -pie.

This mismatch caused LTO failures: without -pie the linker sets
Reloc::Static for the LTO backend, which generates GP-relative
(small-data) references that lld cannot resolve.

Use hasFlag() to respect the toolchain default, and guard the -pie
emission against -shared and -r (relocatable) modes.
This commit is contained in:
Brian Cain 2026-04-06 13:58:43 -05:00 committed by GitHub
parent de0a81091b
commit ab43cb8520
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 2 deletions

View File

@ -273,7 +273,8 @@ constructHexagonLinkArgs(Compilation &C, const JobAction &JA,
//----------------------------------------------------------------------------
bool IsStatic = Args.hasArg(options::OPT_static);
bool IsShared = Args.hasArg(options::OPT_shared);
bool IsPIE = Args.hasArg(options::OPT_pie);
bool IsPIE = Args.hasFlag(options::OPT_pie, options::OPT_no_pie,
HTC.isPIEDefault(Args));
bool IncStdLib = !Args.hasArg(options::OPT_nostdlib);
bool IncStartFiles = !Args.hasArg(options::OPT_nostartfiles);
bool IncDefLibs = !Args.hasArg(options::OPT_nodefaultlibs);
@ -323,7 +324,7 @@ constructHexagonLinkArgs(Compilation &C, const JobAction &JA,
if (IsStatic)
CmdArgs.push_back("-static");
if (IsPIE && !IsShared)
if (IsPIE && !IsShared && !Args.hasArg(options::OPT_r))
CmdArgs.push_back("-pie");
if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args))

View File

@ -149,3 +149,34 @@
// CHECK013-NOT: "-lgcc_eh"
// CHECK013-NOT: "-lgcc_s"
// CHECK013-NOT: "-lunwind"
// -----------------------------------------------------------------------------
// PIE is the default for linux-musl
// -----------------------------------------------------------------------------
// 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-PIE-DEFAULT %s
// CHECK-PIE-DEFAULT: "-pie"
// 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 -no-pie %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-NO-PIE %s
// CHECK-NO-PIE-NOT: "-pie"
// 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 -shared %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-PIE-SHARED %s
// CHECK-PIE-SHARED-NOT: "-pie"
// 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 -r %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-PIE-RELOCATABLE %s
// CHECK-PIE-RELOCATABLE-NOT: "-pie"