Skip to content

[SystemZ][z/OS] Fix "relative immediate relocation section mismatch" - #222437

Merged
redstar merged 1 commit into
mainfrom
users/redstar/fixconstsect
Sep 10, 2026
Merged

[SystemZ][z/OS] Fix "relative immediate relocation section mismatch"#222437
redstar merged 1 commit into
mainfrom
users/redstar/fixconstsect

Conversation

@redstar

@redstar redstar commented Sep 9, 2026

Copy link
Copy Markdown
Member

Constants should go inside the text section, but this is not explicitly coded. In the test case, the EH table is generated into a PR section. Then the constant pool of function fn_with_const_pool() is emitted, which goes into the PR section of the EH table instead of the code section. When the constant is later referenced in the code, the relative offset cannot be calculated because different sections are involved.
The fix is to explicitly return the text section for constants.

@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-backend-systemz

Author: Kai Nacke (redstar)

Changes

Constants should go inside the text section, but this is not explicitly coded. In the test case, the EH table is generated into a PR section. Then the constant pool of function fn_with_const_pool() is emitted, which goes into the PR section of the EH table instead of the code section. When the constant is later referenced in the code, the relative offset cannot be calculated because different sections are involved.
The fix is to explicitly return the text section for constants.


Full diff: https://github.com/llvm/llvm-project/pull/222437.diff

3 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h (+3)
  • (modified) llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (+6)
  • (modified) llvm/test/CodeGen/SystemZ/zos-eh.ll (+5)
diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
index af6eb0269378a..e631d820eb637 100644
--- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
+++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
@@ -333,6 +333,9 @@ class LLVM_ABI TargetLoweringObjectFileGOFF : public TargetLoweringObjectFile {
 
   bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
                                            const Function &F) const override;
+  MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
+                                   const Constant *C, Align &Alignment,
+                                   const Function *F) const override;
   MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
                                     const TargetMachine &TM) const override;
   MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 4643ef995afec..2fb44041c164e 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -2860,6 +2860,12 @@ bool TargetLoweringObjectFileGOFF::shouldPutJumpTableInFunctionSection(
   return true;
 }
 
+MCSection *TargetLoweringObjectFileGOFF::getSectionForConstant(
+    const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
+    const Function *F) const {
+  return TextSection;
+}
+
 MCSection *TargetLoweringObjectFileGOFF::getExplicitSectionGlobal(
     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
   return SelectSectionForGlobal(GO, Kind, TM);
diff --git a/llvm/test/CodeGen/SystemZ/zos-eh.ll b/llvm/test/CodeGen/SystemZ/zos-eh.ll
index 9439289bfbd6b..a424de3068d9e 100644
--- a/llvm/test/CodeGen/SystemZ/zos-eh.ll
+++ b/llvm/test/CodeGen/SystemZ/zos-eh.ll
@@ -13,6 +13,11 @@ bb1:
   ret { ptr, i32 } zeroinitializer
 }
 
+define double @fn_with_const_pool() {
+start:
+  ret double 1.0   ; forces a constant pool entry (L#CPI1_0)
+}
+
 declare i32 @__zos_cxx_personality_v2(...)
 
 ; CHECK:      C_WSA64 CATTR ALIGN(2),FILL(0),NOTEXECUTABLE,RMODE(64),PART(.gcc_excepti

@uweigand uweigand left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks!

Base automatically changed from users/redstar/allowtemplabel to main September 10, 2026 13:46
…atch"

Constants should go inside the text section, but this is not explicitly coded.
In the test case, the EH table is generated into a PR section. Then the
constant pool of function `fn_with_const_pool()` is emitted, which goes into
the PR section of the EH table insted of the code section. When the constant
is later referenced in the code, the relative offset cannot be calculated
because different sections are involved.
The fix is to explicitly return the text section for constants.
@redstar
redstar force-pushed the users/redstar/fixconstsect branch from eee3d12 to 7a92cd5 Compare September 10, 2026 13:48
@redstar
redstar merged commit 746d09a into main Sep 10, 2026
12 checks passed
@redstar
redstar deleted the users/redstar/fixconstsect branch September 10, 2026 14:52
vadimkotov pushed a commit to vadimkotov/llvm-project that referenced this pull request Sep 11, 2026
…lvm#222437)

Constants should go inside the text section, but this is not explicitly
coded. In the test case, the EH table is generated into a PR section.
Then the constant pool of function `fn_with_const_pool()` is emitted,
which goes into the PR section of the EH table instead of the code
section. When the constant is later referenced in the code, the relative
offset cannot be calculated because different sections are involved.
The fix is to explicitly return the text section for constants.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants