[llvm] [BOLT] Correctly handle entry point at FDE boundaries (PR #176173)

Elvina Yakubova via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 28 09:45:10 PST 2026


https://github.com/ElvinaYakubova updated https://github.com/llvm/llvm-project/pull/176173

>From dbde81892317de4707730f8be1b1e515ea3c0476 Mon Sep 17 00:00:00 2001
From: Elvina Yakubova <eyakubova at nvidia.com>
Date: Thu, 15 Jan 2026 22:11:53 +0530
Subject: [PATCH 1/2] [BOLT] Correctly handle entry point at FDE boundaries

Fix a bug where getOrCreateLocalLabel() would return getFunctionEndLabel() when a secondary entry point is located exactly at an FDE end.

This occurs when:
An FDE covers code with no symbol
Inlined code follows immediately after the FDE
An external branch targets exactly the FDE end address
---
 bolt/include/bolt/Core/BinaryFunction.h       |  2 +-
 bolt/lib/Core/BinaryFunction.cpp              |  7 +-
 .../AArch64/entry-point-at-function-end.s     | 97 +++++++++++++++++++
 3 files changed, 102 insertions(+), 4 deletions(-)
 create mode 100644 bolt/test/AArch64/entry-point-at-function-end.s

diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h
index 12aef415aa050..62d97c25337ce 100644
--- a/bolt/include/bolt/Core/BinaryFunction.h
+++ b/bolt/include/bolt/Core/BinaryFunction.h
@@ -636,7 +636,7 @@ class BinaryFunction {
   ///
   /// NOTE: the function always returns a local (temp) symbol, even if there's
   ///       a global symbol that corresponds to an entry at this address.
-  MCSymbol *getOrCreateLocalLabel(uint64_t Address);
+  MCSymbol *getOrCreateLocalLabel(uint64_t Address, bool IsEntryPoint = false);
 
   /// Register an data entry at a given \p Offset into the function.
   void markDataAtOffset(uint64_t Offset) {
diff --git a/bolt/lib/Core/BinaryFunction.cpp b/bolt/lib/Core/BinaryFunction.cpp
index dc83b9e8ed76d..7c8b0dda54ff9 100644
--- a/bolt/lib/Core/BinaryFunction.cpp
+++ b/bolt/lib/Core/BinaryFunction.cpp
@@ -1029,7 +1029,8 @@ BinaryFunction::processIndirectBranch(MCInst &Instruction, unsigned Size,
   return BranchType;
 }
 
-MCSymbol *BinaryFunction::getOrCreateLocalLabel(uint64_t Address) {
+MCSymbol *BinaryFunction::getOrCreateLocalLabel(uint64_t Address,
+                                                bool IsEntryPoint) {
   const uint64_t Offset = Address - getAddress();
 
   auto LI = Labels.find(Offset);
@@ -1044,7 +1045,7 @@ MCSymbol *BinaryFunction::getOrCreateLocalLabel(uint64_t Address) {
     }
   }
 
-  if (Offset == getSize())
+  if (Offset == getSize() && !IsEntryPoint)
     return getFunctionEndLabel();
 
   MCSymbol *Label = BC.Ctx->createNamedTempSymbol();
@@ -3800,7 +3801,7 @@ MCSymbol *BinaryFunction::addEntryPointAtOffset(uint64_t Offset) {
   const uint64_t EntryPointAddress = getAddress() + Offset;
   assert(!isInConstantIsland(EntryPointAddress) &&
          "cannot add entry point that points to constant data");
-  MCSymbol *LocalSymbol = getOrCreateLocalLabel(EntryPointAddress);
+  MCSymbol *LocalSymbol = getOrCreateLocalLabel(EntryPointAddress, true);
 
   MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(LocalSymbol);
   if (EntrySymbol)
diff --git a/bolt/test/AArch64/entry-point-at-function-end.s b/bolt/test/AArch64/entry-point-at-function-end.s
new file mode 100644
index 0000000000000..d5a370f4846cc
--- /dev/null
+++ b/bolt/test/AArch64/entry-point-at-function-end.s
@@ -0,0 +1,97 @@
+## Test that BOLT correctly handles entry points at exact FDE boundaries.
+##
+## This test simulates a real-world scenario where:
+## 1. A large function (big_func) contains an FDE with no symbol
+## 2. The FDE covers a sub-function within big_func [fde_start, fde_end)
+## 3. Inlined code follows immediately after the FDE (no symbol, no FDE)
+## 4. .init branches to exactly the FDE end address via section relocation
+##
+## Without the fix, getOrCreateLocalLabel() would return getFunctionEndLabel()
+## when offset == FDE size. The fix ensures a proper entry point label is created.
+##
+
+# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
+# RUN: %clang %cflags %t.o -pie -Wl,-q -Wl,--init=_init -o %t.exe
+# RUN: llvm-bolt %t.exe -o %t.bolt --print-disasm -v=1 2>&1 | FileCheck %s
+
+## The FDE-only function should get an entry point for the inlined code
+# CHECK: BOLT-WARNING: FDE {{.*}} has no corresponding symbol table entry
+# CHECK: Binary Function "{{.*}}__BOLT_FDE_FUNC{{.*}}" after disassembly
+# CHECK: Size        : 0x1c
+# CHECK: MaxSize     : 0x30
+# CHECK: IsMultiEntry: 1
+
+## _init function in .init section
+	.section .init,"ax", at progbits
+	.globl	_init
+	.type	_init, %function
+_init:
+	.cfi_startproc
+	stp	x29, x30, [sp, #-16]!
+	mov	x29, sp
+	## Branch to inlined code that's right after FDE end
+	## Linker converts this to: R_AARCH64_CALL26 .text + offset
+	bl	.Linlined_code
+	ldp	x29, x30, [sp], #16
+	ret
+	.cfi_endproc
+	.size	_init, .-_init
+
+## Main code in .text section
+	.text
+
+## Large function that contains the FDE and inlined code
+	.globl	big_func
+	.type	big_func, %function
+big_func:
+	.cfi_startproc
+	stp	x29, x30, [sp, #-16]!
+	mov	x29, sp
+	mov	w0, #1
+	add	w0, w0, #1
+	ldp	x29, x30, [sp], #16
+	ret
+	.cfi_endproc
+	.size	big_func, .-big_func
+
+## FDE-covered code WITHOUT a symbol - simulates sub-function within big_func
+## This has its own FDE but no symbol in the symbol table
+.Lfde_start:
+	.cfi_startproc
+	stp	x29, x30, [sp, #-16]!
+	mov	x29, sp
+	adrp	x0, .Lsome_data
+	add	x0, x0, :lo12:.Lsome_data
+	ldr	x0, [x0]
+	ldp	x29, x30, [sp], #16
+	ret
+	.cfi_endproc
+## FDE ends here - inlined code follows immediately
+
+## Inlined code at exactly FDE end (offset == FDE size)
+## _init branches here - this is what triggers the bug without the fix
+.Linlined_code:
+	adrp	x0, .Lsome_data
+	ldr	x0, [x0, :lo12:.Lsome_data]
+	cbz	x0, .Linlined_ret
+	mov	w0, #1
+.Linlined_ret:
+	ret
+
+	.globl	_start
+	.type	_start, %function
+_start:
+	.cfi_startproc
+	stp	x29, x30, [sp, #-16]!
+	mov	x29, sp
+	bl	_init
+	bl	big_func
+	ldp	x29, x30, [sp], #16
+	mov	w0, #0
+	ret
+	.cfi_endproc
+	.size	_start, .-_start
+
+	.data
+.Lsome_data:
+	.xword	0

>From 1906ba12258940f4e908f73f8df342f7323442a3 Mon Sep 17 00:00:00 2001
From: Elvina Yakubova <eyakubova at nvidia.com>
Date: Wed, 28 Jan 2026 04:08:26 -0800
Subject: [PATCH 2/2] Simplify the test

---
 .../test/AArch64/entry-point-at-function-end.s | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)

diff --git a/bolt/test/AArch64/entry-point-at-function-end.s b/bolt/test/AArch64/entry-point-at-function-end.s
index d5a370f4846cc..f657e9e522036 100644
--- a/bolt/test/AArch64/entry-point-at-function-end.s
+++ b/bolt/test/AArch64/entry-point-at-function-end.s
@@ -17,8 +17,8 @@
 ## The FDE-only function should get an entry point for the inlined code
 # CHECK: BOLT-WARNING: FDE {{.*}} has no corresponding symbol table entry
 # CHECK: Binary Function "{{.*}}__BOLT_FDE_FUNC{{.*}}" after disassembly
-# CHECK: Size        : 0x1c
-# CHECK: MaxSize     : 0x30
+# CHECK: Size        : 0x14
+# CHECK: MaxSize     : 0x1c
 # CHECK: IsMultiEntry: 1
 
 ## _init function in .init section
@@ -60,9 +60,7 @@ big_func:
 	.cfi_startproc
 	stp	x29, x30, [sp, #-16]!
 	mov	x29, sp
-	adrp	x0, .Lsome_data
-	add	x0, x0, :lo12:.Lsome_data
-	ldr	x0, [x0]
+	mov	w0, #42
 	ldp	x29, x30, [sp], #16
 	ret
 	.cfi_endproc
@@ -71,11 +69,7 @@ big_func:
 ## Inlined code at exactly FDE end (offset == FDE size)
 ## _init branches here - this is what triggers the bug without the fix
 .Linlined_code:
-	adrp	x0, .Lsome_data
-	ldr	x0, [x0, :lo12:.Lsome_data]
-	cbz	x0, .Linlined_ret
-	mov	w0, #1
-.Linlined_ret:
+	nop
 	ret
 
 	.globl	_start
@@ -91,7 +85,3 @@ _start:
 	ret
 	.cfi_endproc
 	.size	_start, .-_start
-
-	.data
-.Lsome_data:
-	.xword	0



More information about the llvm-commits mailing list