[clang] [llvm] [Hexagon][Driver] Tell llvm-mc about PIC under -fno-integrated-as (PR #213821)

Brian Cain via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 25 13:51:37 PDT 2026


https://github.com/androm3da updated https://github.com/llvm/llvm-project/pull/213821

>From bb13b8b85eaa18e904c7ec680432977f01e9deec Mon Sep 17 00:00:00 2001
From: Brian Cain <brian.cain at oss.qualcomm.com>
Date: Mon, 3 Aug 2026 21:02:40 -0500
Subject: [PATCH 1/2] [Hexagon][Driver] Tell llvm-mc about PIC under
 -fno-integrated-as

Hexagon picks the .eh_frame FDE pointer encoding from whether the code is
position independent: DW_EH_PE_pcrel if so, DW_EH_PE_absptr otherwise
(MCObjectFileInfo::initELFMCObjectFileInfo).  Unlike most targets, Hexagon
uses llvm-mc rather than GNU as for -fno-integrated-as, and the driver was
not passing the PIC setting along.  llvm-mc defaults to non-PIC, so the same
source assembled the two ways produced different unwind tables: the
integrated assembler emitted R_HEX_32_PCREL, while -fno-integrated-as
emitted R_HEX_32 with an absptr CIE augmentation.

hexagon-unknown-linux-musl defaults to PIC, so it was the default config
that diverged.

Pass -position-independent to llvm-mc when the relocation model is PIC.
Non-PIC keeps DW_EH_PE_absptr, unchanged.
---
 clang/lib/Driver/ToolChains/Hexagon.cpp  |  3 +++
 clang/test/Driver/hexagon-eh-frame-pic.c | 22 ++++++++++++++++++++++
 llvm/test/MC/Hexagon/eh-frame.s          | 21 +++++++++++++++++++++
 3 files changed, 46 insertions(+)
 create mode 100644 clang/test/Driver/hexagon-eh-frame-pic.c
 create mode 100644 llvm/test/MC/Hexagon/eh-frame.s

diff --git a/clang/lib/Driver/ToolChains/Hexagon.cpp b/clang/lib/Driver/ToolChains/Hexagon.cpp
index 933b9f2ea5412d..ec2641301909d1 100644
--- a/clang/lib/Driver/ToolChains/Hexagon.cpp
+++ b/clang/lib/Driver/ToolChains/Hexagon.cpp
@@ -218,6 +218,9 @@ void hexagon::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
       "-mcpu=hexagon" +
       toolchains::HexagonToolChain::GetTargetCPUVersion(Args)));
 
+  if (std::get<0>(ParsePICArgs(HTC, Args)) == llvm::Reloc::PIC_)
+    CmdArgs.push_back("-position-independent");
+
   addSanitizerRuntimes(HTC, Args, CmdArgs, C);
 
   assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
diff --git a/clang/test/Driver/hexagon-eh-frame-pic.c b/clang/test/Driver/hexagon-eh-frame-pic.c
new file mode 100644
index 00000000000000..1b5e8ee584a276
--- /dev/null
+++ b/clang/test/Driver/hexagon-eh-frame-pic.c
@@ -0,0 +1,22 @@
+/// Hexagon selects the .eh_frame FDE pointer encoding from whether the code is
+/// position independent (DW_EH_PE_pcrel vs DW_EH_PE_absptr, see
+/// MCObjectFileInfo::initELFMCObjectFileInfo).  llvm-mc defaults to non-PIC, so
+/// the driver has to tell it, or -fno-integrated-as produces unwind tables that
+/// disagree with the ones the integrated assembler produces for the same input
+/// (R_HEX_32 instead of R_HEX_32_PCREL).
+
+// RUN: %clang --target=hexagon-unknown-linux-musl -fno-integrated-as -fPIC \
+// RUN:   -funwind-tables -c %s -### 2>&1 | FileCheck %s --check-prefix=PIC
+
+/// Non-PIC keeps DW_EH_PE_absptr, so llvm-mc must not be told otherwise.
+// RUN: %clang --target=hexagon-unknown-linux-musl -fno-integrated-as -fno-pic \
+// RUN:   -funwind-tables -c %s -### 2>&1 | FileCheck %s --check-prefix=NOPIC
+
+// PIC: llvm-mc
+// PIC: "-position-independent"
+
+// NOPIC: llvm-mc
+// NOPIC-NOT: "-position-independent"
+
+void f(void);
+void g(void) { f(); }
diff --git a/llvm/test/MC/Hexagon/eh-frame.s b/llvm/test/MC/Hexagon/eh-frame.s
new file mode 100644
index 00000000000000..2807989b427cb7
--- /dev/null
+++ b/llvm/test/MC/Hexagon/eh-frame.s
@@ -0,0 +1,21 @@
+# RUN: llvm-mc -filetype=obj -triple=hexagon %s -o %t.abs.o
+# RUN: llvm-readobj -r %t.abs.o | FileCheck %s --check-prefix=ABS
+# RUN: llvm-mc -filetype=obj -triple=hexagon -position-independent %s -o %t.pic.o
+# RUN: llvm-readobj -r %t.pic.o | FileCheck %s --check-prefix=PIC
+
+// Hexagon selects the FDE pointer encoding from whether the object is PIC.
+func:
+  .cfi_startproc
+  .cfi_endproc
+
+// ABS:      Relocations [
+// ABS-NEXT:   Section ({{.*}}) .rela.eh_frame {
+// ABS-NEXT:     R_HEX_32
+// ABS-NEXT:   }
+// ABS-NEXT: ]
+
+// PIC:      Relocations [
+// PIC-NEXT:   Section ({{.*}}) .rela.eh_frame {
+// PIC-NEXT:     R_HEX_32_PCREL
+// PIC-NEXT:   }
+// PIC-NEXT: ]

>From b96ba8553d460c55ffe1338b7308e39ce12d5579 Mon Sep 17 00:00:00 2001
From: Brian Cain <brian.cain at oss.qualcomm.com>
Date: Fri, 25 Sep 2026 15:46:42 -0500
Subject: [PATCH 2/2] fixup! [Hexagon][Driver] Tell llvm-mc about PIC under
 -fno-integrated-as

---
 llvm/test/MC/Hexagon/eh-frame.s | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/MC/Hexagon/eh-frame.s b/llvm/test/MC/Hexagon/eh-frame.s
index 2807989b427cb7..53d7054bd0b7f3 100644
--- a/llvm/test/MC/Hexagon/eh-frame.s
+++ b/llvm/test/MC/Hexagon/eh-frame.s
@@ -10,7 +10,7 @@ func:
 
 // ABS:      Relocations [
 // ABS-NEXT:   Section ({{.*}}) .rela.eh_frame {
-// ABS-NEXT:     R_HEX_32
+// ABS-NEXT:     R_HEX_32{{[[:space:]]}}
 // ABS-NEXT:   }
 // ABS-NEXT: ]
 



More information about the llvm-commits mailing list