[llvm] [BOLT] Fix RELR addend updates for non-zero image bases (PR #213000)

Adam Bzowski via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 30 04:42:38 PDT 2026


https://github.com/adam-bzowski-arm created https://github.com/llvm/llvm-project/pull/213000

This patch fixes an AArch64 BOLT failure exposed by LLVM commit `fbba327208d7f6994f9a28a51dca3e913e3b444a`, which enabled packed relative relocations (RELR) globally. BOLT incorrectly used a virtual relocation address as a file offset when updating RELR addends, corrupting instrumented executables when their image base was non-zero. The observed CMake compiler-check failure was only a consequence of the corrupted BOLT-instrumented Clang executable crashing; the defect itself is in BOLT, not CMake.

The regression test links an AArch64 PIE with a non-zero image base and verifies that BOLT updates the RELR addend at the correct file offset.

>From 67778ab2c6955d14c566bd7631739de61d5b90a3 Mon Sep 17 00:00:00 2001
From: Adam Bzowski <Adam.Bzowski at arm.com>
Date: Wed, 29 Jul 2026 14:09:28 +0000
Subject: [PATCH] [BOLT] Fix RELR addend updates for non-zero image bases

Change-Id: Ie801c01cc8f41328d42ed13b5197212466671813
---
 bolt/lib/Rewrite/RewriteInstance.cpp           |  2 +-
 bolt/test/AArch64/constant_island_pie_update.s | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index ecb1d976ee161..965572e3608b1 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -5828,7 +5828,7 @@ void RewriteInstance::patchELFAllocatableRelrSection(
       RelOffset = RelOffset == 0 ? SectionAddress + Rel.Offset : RelOffset;
       assert((RelOffset & 1) == 0 && "Wrong relocation offset");
       RelOffsets.emplace(RelOffset);
-      FixAddend(Section, Rel, RelOffset);
+      FixAddend(Section, Rel, getFileOffsetForAddress(RelOffset));
     }
   }
 
diff --git a/bolt/test/AArch64/constant_island_pie_update.s b/bolt/test/AArch64/constant_island_pie_update.s
index 889f6b6acd9f2..8d83a79782cd2 100644
--- a/bolt/test/AArch64/constant_island_pie_update.s
+++ b/bolt/test/AArch64/constant_island_pie_update.s
@@ -21,8 +21,23 @@
 # RUN: llvm-readelf -rsW %t.relr.bolt | FileCheck --check-prefix=RELRELFCHECK %s
 # RUN: llvm-readelf -SW %t.relr.bolt | FileCheck --check-prefix=RELRSZCHECK %s
 
+// Check that RELR addends are patched at file offsets rather than virtual
+// addresses when the two differ.
+# RUN: %clang %cflags -fPIC -pie %t.o -o %t.relr.bias.exe -nostdlib \
+# RUN:   -Wl,-q -Wl,-z,notext -Wl,--pack-dyn-relocs=relr \
+# RUN:   -Wl,--image-base=0x20000
+# RUN: llvm-objcopy --remove-section .rela.mytext %t.relr.bias.exe
+# RUN: llvm-readelf -lW %t.relr.bias.exe | FileCheck --check-prefix=BIAS %s
+# RUN: llvm-bolt %t.relr.bias.exe -o %t.relr.bias.bolt --use-old-text=0 \
+# RUN:   --lite=0
+# RUN: llvm-objdump -j .text -d -z %t.relr.bias.bolt | \
+# RUN:   FileCheck %s --check-prefix=ADDENDCHECK
+
 // Check that the CI value was updated
 # CHECK: [[#%x,ADDR:]] <exitLocal>:
+
+# BIAS: LOAD {{.*}} 0x0000000000020000
+
 # CHECK: {{.*}} <$d>:
 # CHECK-NEXT: {{.*}} .word 0x{{[0]+}}[[#ADDR]]
 # CHECK-NEXT: {{.*}} .word 0x00000000



More information about the llvm-commits mailing list