[llvm] [BOLT] Check if symbol is in data area of function (PR #160143)
Asher Dobrescu via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 22 09:23:00 PDT 2025
https://github.com/Asher8118 created https://github.com/llvm/llvm-project/pull/160143
There are cases in which `getEntryIDForSymbol` is called, where the given Symbol is in a constant island, and so BOLT can not find its function. This causes BOLT to reach `llvm_unreachable("symbol not found")` and crash. This patch adds a check that avoids this crash and gives a warning to the user.
>From 36823a0c80776a97b6579baeadbee0e762377720 Mon Sep 17 00:00:00 2001
From: Ash Dobrescu <ash.dobrescu at arm.com>
Date: Mon, 22 Sep 2025 16:14:39 +0000
Subject: [PATCH] [BOLT] Check if symbol is in data area of function
There are cases in which `getEntryIDForSymbol` is called,
where the given Symbol is in a constant island, and so BOLT
can not find its function. This causes BOLT to reach
`llvm_unreachable("symbol not found")` and crash. This patch adds
a check that avoid this crash and gives a warning to the user.
---
bolt/lib/Core/BinaryContext.cpp | 10 +++++-
bolt/test/AArch64/check-symbol-area.s | 49 +++++++++++++++++++++++++++
2 files changed, 58 insertions(+), 1 deletion(-)
create mode 100644 bolt/test/AArch64/check-symbol-area.s
diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index 72c72bbaf4a65..5f3b3c0e57152 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -2374,8 +2374,16 @@ BinaryFunction *BinaryContext::getFunctionForSymbol(const MCSymbol *Symbol,
return nullptr;
BinaryFunction *BF = BFI->second;
- if (EntryDesc)
+ if (EntryDesc) {
+ const uint64_t Address = BF->getAddress() + Symbol->getOffset();
+ if (BF->isInConstantIsland(Address)) {
+ this->outs() << "BOLT-WARNING: symbol " << Symbol->getName()
+ << " is in data region of function 0x"
+ << Twine::utohexstr(Address) << ".\n";
+ return nullptr;
+ }
*EntryDesc = BF->getEntryIDForSymbol(Symbol);
+ }
return BF;
}
diff --git a/bolt/test/AArch64/check-symbol-area.s b/bolt/test/AArch64/check-symbol-area.s
new file mode 100644
index 0000000000000..43ce25e16181f
--- /dev/null
+++ b/bolt/test/AArch64/check-symbol-area.s
@@ -0,0 +1,49 @@
+// This test checks that when looking for a function
+// correspnding to a symbol, BOLT is not looking
+// through a data area (constant island).
+
+# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
+# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
+# RUN: llvm-bolt %t.exe -o %t.bolt 2>&1 | FileCheck %s
+
+// Before adding a check for constant islands, BOLT would exit with an error
+// of the form: "symbol not found" and throw an LLVM UNREACHABLE error.
+# CHECK-NOT: symbol not found
+# CHECK-NOT: UNREACHABLE
+
+// Now BOLT throws a warning and does not crash.
+# CHECK: BOLT-WARNING: symbol [[SYM:.*]] is in data region of function 0x{{.*}}.
+
+.text
+.global main
+main:
+ stp x14, x15, [sp, -8]!
+ mov x14, sp
+ adrp x1, .test
+ add x0, x1, :lo12:.test
+ bl first_block
+ ret
+
+.global first_block
+$d:
+first_block:
+ stp x14, x15, [sp, -8]!
+ mov x14, sp
+ bl second_block
+ ret
+second_block:
+ stp x14, x15, [sp, -8]!
+ mov x14, sp
+ bl third_block
+ ret
+$x:
+third_block:
+ stp x14, x15, [sp, -8]!
+ mov x14, sp
+ adrp x1, .data
+ add x0, x1, :lo12:.test
+ ret
+
+.data
+.test:
+ .string "test"
More information about the llvm-commits
mailing list