[llvm] [BOLT] Add support for CREL code relocations (PR #196383)

Amina Chabane via llvm-commits llvm-commits at lists.llvm.org
Fri May 8 08:50:01 PDT 2026


https://github.com/Amichaxx updated https://github.com/llvm/llvm-project/pull/196383

>From 238d914fe93f90f7eede45b9db3e35025ae679bb Mon Sep 17 00:00:00 2001
From: Amichaxx <amina.chabane at arm.com>
Date: Thu, 7 May 2026 17:48:15 +0000
Subject: [PATCH 1/3] [BOLT] Add support for CREL code relocations

BOLT only checked for .rela sections when deciding whether
relocation mode could be enabled. This caused binaries with SHT_CREL
code relocations (such as .crel.text) to be rejected as missing
relocations.

No support for SHT_REL has been added in this patch.

Co-Author: @0xfk0
---
 bolt/lib/Rewrite/RewriteInstance.cpp |  9 ++++-
 bolt/test/crel-relocs.test           | 56 ++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 2 deletions(-)
 create mode 100644 bolt/test/crel-relocs.test

diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index 43d4421e06928..d7c100990b44d 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -2249,8 +2249,9 @@ Error RewriteInstance::readSpecialSections() {
     BC->updateLSDAEncoding();
   }
 
-  HasTextRelocations = (bool)BC->getUniqueSectionByName(
-      ".rela" + std::string(BC->getMainCodeSectionName()));
+  HasTextRelocations =
+      (bool)BC->getUniqueSectionByName(std::string(".rela") + std::string(BC->getMainCodeSectionName())) ||
+      (bool)BC->getUniqueSectionByName(std::string(".crel") + std::string(BC->getMainCodeSectionName()));
   HasSymbolTable = (bool)BC->getUniqueSectionByName(".symtab");
   EHFrameSection = BC->getUniqueSectionByName(".eh_frame");
 
@@ -2474,6 +2475,10 @@ int64_t getRelocationAddend(const ELFObjectFile<ELFT> *Obj,
     Addend = RelA->r_addend;
     break;
   }
+  case ELF::SHT_CREL: {
+    Addend = Obj->getCrel(Rel).r_addend;
+    break;
+  }
   }
 
   return Addend;
diff --git a/bolt/test/crel-relocs.test b/bolt/test/crel-relocs.test
new file mode 100644
index 0000000000000..8ac0b21d0b56c
--- /dev/null
+++ b/bolt/test/crel-relocs.test
@@ -0,0 +1,56 @@
+## Check that BOLT accepts relocation mode when code relocations are stored
+## in SHT_CREL.
+
+# RUN: yaml2obj %s -o %t.exe
+# RUN: llvm-readelf -S %t.exe | FileCheck %s --check-prefix=SECTIONS
+# RUN: llvm-bolt %t.exe -o %t.bolt -relocs 2>&1 | FileCheck %s
+
+# SECTIONS: .crel.text
+
+# CHECK: BOLT-INFO: enabling relocation mode
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_AARCH64
+  Entry:   0x2670000
+ProgramHeaders:
+  - Type:     PT_LOAD
+    Flags:    [ PF_X, PF_R ]
+    FirstSec: .text
+    LastSec:  .text
+    VAddr:    0x2670000
+    Align:    0x10000
+Sections:
+  - Name:         .text
+    Type:         SHT_PROGBITS
+    Flags:        [ SHF_ALLOC, SHF_EXECINSTR ]
+    Address:      0x2670000
+    AddressAlign: 0x4
+    Content:      00000094c0035fd6
+  - Name:         .crel.text
+    Type:         SHT_CREL
+    Flags:        [ SHF_INFO_LINK ]
+    Link:         .symtab
+    Info:         .text
+    AddressAlign: 0x8
+    Relocations:
+      - Offset:   0x2670000
+        Symbol:   target
+        Type:     R_AARCH64_CALL26
+Symbols:
+  - Name:    _start
+    Type:    STT_FUNC
+    Section: .text
+    Binding: STB_GLOBAL
+    Value:   0x2670000
+    Size:    0x8
+  - Name:    target
+    Type:    STT_FUNC
+    Section: .text
+    Binding: STB_GLOBAL
+    Value:   0x2670004
+    Size:    0x4
+...

>From cf58159e40306b80d2d4489a885e2a48d7c13e9a Mon Sep 17 00:00:00 2001
From: Amichaxx <amina.chabane at arm.com>
Date: Thu, 7 May 2026 18:26:15 +0000
Subject: [PATCH 2/3] clang format

---
 bolt/lib/Rewrite/RewriteInstance.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index d7c100990b44d..d1dd0cf4bc5de 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -2250,8 +2250,10 @@ Error RewriteInstance::readSpecialSections() {
   }
 
   HasTextRelocations =
-      (bool)BC->getUniqueSectionByName(std::string(".rela") + std::string(BC->getMainCodeSectionName())) ||
-      (bool)BC->getUniqueSectionByName(std::string(".crel") + std::string(BC->getMainCodeSectionName()));
+      (bool)BC->getUniqueSectionByName(
+          std::string(".rela") + std::string(BC->getMainCodeSectionName())) ||
+      (bool)BC->getUniqueSectionByName(
+          std::string(".crel") + std::string(BC->getMainCodeSectionName()));
   HasSymbolTable = (bool)BC->getUniqueSectionByName(".symtab");
   EHFrameSection = BC->getUniqueSectionByName(".eh_frame");
 

>From 0c3dbc1e29e347815c3eeff50d7eb98175a857fe Mon Sep 17 00:00:00 2001
From: Amichaxx <amina.chabane at arm.com>
Date: Fri, 8 May 2026 15:26:56 +0000
Subject: [PATCH 3/3] Strip stale crel sections

---
 bolt/lib/Rewrite/RewriteInstance.cpp | 3 ++-
 bolt/test/crel-relocs.test           | 4 ++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index d1dd0cf4bc5de..1e40ba13381c5 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -4886,7 +4886,8 @@ template <typename ELFShdrTy>
 bool RewriteInstance::shouldStrip(const ELFShdrTy &Section,
                                   StringRef SectionName) {
   // Strip non-allocatable relocation sections.
-  if (!(Section.sh_flags & ELF::SHF_ALLOC) && Section.sh_type == ELF::SHT_RELA)
+  if (!(Section.sh_flags & ELF::SHF_ALLOC) &&
+      (Section.sh_type == ELF::SHT_RELA || Section.sh_type == ELF::SHT_CREL))
     return true;
 
   // Strip debug sections if not updating them.
diff --git a/bolt/test/crel-relocs.test b/bolt/test/crel-relocs.test
index 8ac0b21d0b56c..3f421af886fac 100644
--- a/bolt/test/crel-relocs.test
+++ b/bolt/test/crel-relocs.test
@@ -4,11 +4,15 @@
 # RUN: yaml2obj %s -o %t.exe
 # RUN: llvm-readelf -S %t.exe | FileCheck %s --check-prefix=SECTIONS
 # RUN: llvm-bolt %t.exe -o %t.bolt -relocs 2>&1 | FileCheck %s
+# RUN: llvm-readelf -S %t.bolt | FileCheck %s --check-prefix=OUTPUT
 
 # SECTIONS: .crel.text
 
 # CHECK: BOLT-INFO: enabling relocation mode
 
+# OUTPUT: .text
+# OUTPUT-NOT: .crel.text
+
 --- !ELF
 FileHeader:
   Class:   ELFCLASS64



More information about the llvm-commits mailing list