[llvm] [BOLT][RISCV] Ignore alignment markers in linked binaries (PR #223994)

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 16 05:28:13 PDT 2026


https://github.com/Thrrreeee created https://github.com/llvm/llvm-project/pull/223994

Skip `R_RISCV_ALIGN` when reading input relocations. It describes padding used for linker relaxation rather than an address to relocate. Since BOLT processes an already-linked binary, retained alignment markers should not enter the ordinary relocation handling path.

>From ad7b81e67ade5db13fad373a177e18db922a99f6 Mon Sep 17 00:00:00 2001
From: shijinrui <shijinrui at bytedance.com>
Date: Wed, 16 Sep 2026 20:25:16 +0800
Subject: [PATCH] [BOLT][RISCV] Ignore alignment markers in linked binaries

---
 bolt/lib/Core/Relocation.cpp  |  1 +
 bolt/test/RISCV/reloc-align.s | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
 create mode 100644 bolt/test/RISCV/reloc-align.s

diff --git a/bolt/lib/Core/Relocation.cpp b/bolt/lib/Core/Relocation.cpp
index 502f448f50c18..5cfa092711ca5 100644
--- a/bolt/lib/Core/Relocation.cpp
+++ b/bolt/lib/Core/Relocation.cpp
@@ -268,6 +268,7 @@ static bool skipRelocationTypeRISCV(uint32_t Type) {
     return false;
   case ELF::R_RISCV_NONE:
   case ELF::R_RISCV_RELAX:
+  case ELF::R_RISCV_ALIGN:
     return true;
   }
 }
diff --git a/bolt/test/RISCV/reloc-align.s b/bolt/test/RISCV/reloc-align.s
new file mode 100644
index 0000000000000..b44a6031294ea
--- /dev/null
+++ b/bolt/test/RISCV/reloc-align.s
@@ -0,0 +1,29 @@
+# RUN: llvm-mc -triple=riscv64 -mattr=+c,+relax -filetype=obj %s -o %t.o
+# RUN: ld.lld --emit-relocs %t.o -o %t.exe
+# RUN: llvm-readelf -r %t.exe | FileCheck %s --check-prefix=INPUT
+# RUN: llvm-bolt %t.exe --reorder-functions=cdsort -o %t.bolt 2>&1 | FileCheck %s
+# RUN: llvm-objdump -d %t.bolt | FileCheck %s --check-prefix=CODE
+
+## Retained R_RISCV_ALIGN relocations describe padding already handled by
+## the linker. Check that BOLT accepts them and preserves valid code.
+# INPUT: R_RISCV_ALIGN
+# CHECK: BOLT-INFO: enabling relocation mode
+# CHECK-NOT: Failed to analyze
+# CODE: <_start>:
+# CODE-NEXT: jal
+# CODE: ret
+
+  .text
+  .globl _start
+  .type _start, at function
+  .p2align 2
+_start:
+  call foo
+  .p2align 2
+  ret
+  .size _start, .-_start
+
+  .type foo, at function
+foo:
+  ret
+  .size foo, .-foo



More information about the llvm-commits mailing list