[llvm] [BOLT] Update LSDA encoding for x86-64 large code model (PR #190685)
Farid Zakaria via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 6 14:54:14 PDT 2026
https://github.com/fzakaria updated https://github.com/llvm/llvm-project/pull/190685
>From e960dc8441d9dbd0ac4a30c1ea1d610441569957 Mon Sep 17 00:00:00 2001
From: Farid Zakaria <fmzakari at meta.com>
Date: Mon, 6 Apr 2026 13:53:14 -0700
Subject: [PATCH] [BOLT] Update LSDA encoding for x86-64 large code model
BOLT hardcoded 4-byte LSDA (exception table) encoding for x86-64.
This is insufficient for large code model binaries where functions
in .ltext sections may be placed at addresses above 2GB, exceeding
the range of DW_EH_PE_udata4/DW_EH_PE_sdata4 encodings.
Detect large code model by checking for .ltext sections
(SHF_X86_64_LARGE) and update LSDAEncoding to use 8-byte pointers:
- Non-PIC: DW_EH_PE_absptr (8-byte absolute)
- PIC: DW_EH_PE_pcrel | DW_EH_PE_sdata8 (8-byte PC-relative)
This is the first step toward full large code model support in BOLT.
Without this change, BOLT-processed binaries with C++ exceptions in
.ltext would produce incorrect exception handling tables.
---
bolt/include/bolt/Core/BinaryContext.h | 3 +
bolt/lib/Core/BinaryContext.cpp | 4 +-
bolt/lib/Rewrite/RewriteInstance.cpp | 13 ++++
bolt/test/X86/ltext-lsda-encoding.s | 83 ++++++++++++++++++++++++++
bolt/unittests/Core/BinaryContext.cpp | 24 ++++++++
5 files changed, 126 insertions(+), 1 deletion(-)
create mode 100644 bolt/test/X86/ltext-lsda-encoding.s
diff --git a/bolt/include/bolt/Core/BinaryContext.h b/bolt/include/bolt/Core/BinaryContext.h
index f8a0be0418433..c6de5094f66c3 100644
--- a/bolt/include/bolt/Core/BinaryContext.h
+++ b/bolt/include/bolt/Core/BinaryContext.h
@@ -726,6 +726,9 @@ class BinaryContext {
/// FunctionFragment::getFragmentNum() == FragmentNum::warm()
bool HasWarmSection{false};
+ /// Indicates if the binary has large code model sections (.ltext).
+ bool HasLargeCodeModel{false};
+
/// Is the binary always loaded at a fixed address. Shared objects and
/// position-independent executables (PIEs) are examples of binaries that
/// will have HasFixedLoadAddress set to false.
diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index 5b3baf1669624..e8c1234ca5497 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -256,7 +256,9 @@ Expected<std::unique_ptr<BinaryContext>> BinaryContext::createBinaryContext(
std::unique_ptr<MCObjectFileInfo> MOFI(
TheTarget->createMCObjectFileInfo(*Ctx, IsPIC));
Ctx->setObjectFileInfo(MOFI.get());
- // We do not support X86 Large code model. Change this in the future.
+ // Use large code model encoding for AArch64 (always). For X86 large code
+ // model, LSDAEncoding is updated later in readSpecialSections() after
+ // detecting .ltext sections.
bool Large = false;
if (TheTriple.getArch() == llvm::Triple::aarch64)
Large = true;
diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index b3208ac024ae6..d522615fd8b67 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -2237,6 +2237,19 @@ Error RewriteInstance::readSpecialSections() {
"Use -update-debug-sections to keep it.\n";
}
+ // Detect large code model sections (.ltext) and update LSDA encoding to
+ // use 8-byte pointers for exception handling in large code model binaries.
+ if (BC->getUniqueSectionByName(".ltext")) {
+ BC->HasLargeCodeModel = true;
+ BC->outs()
+ << "BOLT-INFO: large code model detected (.ltext section found)\n";
+ if (BC->isX86()) {
+ BC->LSDAEncoding = BC->HasFixedLoadAddress
+ ? dwarf::DW_EH_PE_absptr
+ : (dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8);
+ }
+ }
+
HasTextRelocations = (bool)BC->getUniqueSectionByName(
".rela" + std::string(BC->getMainCodeSectionName()));
HasSymbolTable = (bool)BC->getUniqueSectionByName(".symtab");
diff --git a/bolt/test/X86/ltext-lsda-encoding.s b/bolt/test/X86/ltext-lsda-encoding.s
new file mode 100644
index 0000000000000..0895f849c49db
--- /dev/null
+++ b/bolt/test/X86/ltext-lsda-encoding.s
@@ -0,0 +1,83 @@
+## Test that BOLT detects large code model binaries via SHF_X86_64_LARGE
+## on code sections and updates the LSDA encoding to 8-byte pointers.
+
+# REQUIRES: system-linux
+
+# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-linux %s -o %t.o
+# RUN: ld.lld --no-pie %t.o -o %t.exe -q -e _start
+# RUN: llvm-bolt %t.exe -o %t.bolt --reorder-blocks=none 2>&1 \
+# RUN: | FileCheck %s --check-prefix=CHECK-BOLT
+
+## Verify BOLT detects large code model via the SHF_X86_64_LARGE flag.
+# CHECK-BOLT: large code model detected
+
+## Verify the BOLT-emitted CIE uses 8-byte LSDA encoding (DW_EH_PE_absptr
+## = 0x00) instead of the default 4-byte encoding (DW_EH_PE_sdata4 = 0x1B).
+## In the "zLR" augmentation data: [L-enc] [R-enc].
+## L-enc = 0x00 means DW_EH_PE_absptr (8-byte), our large code model fix.
+# RUN: llvm-dwarfdump --eh-frame %t.bolt \
+# RUN: | FileCheck %s --check-prefix=CHECK-EH
+
+# CHECK-EH: Augmentation: "zLR"
+# CHECK-EH: Augmentation data: 00 1B
+
+ .text
+ .globl foo
+ .type foo, @function
+foo:
+ .cfi_startproc
+ ret
+ .cfi_endproc
+ .size foo, .-foo
+
+ .globl _start
+ .type _start, @function
+_start:
+.Lfunc_begin0:
+ .cfi_startproc
+ .cfi_lsda 27, .Lexception0
+ call foo
+.Ltmp0:
+ call foo
+.Ltmp1:
+ ret
+
+## Landing pads.
+.LLP0:
+ ret
+.LLP1:
+ ret
+
+ .cfi_endproc
+.Lfunc_end0:
+ .size _start, .Lfunc_end0-_start
+
+## Exception table.
+ .section .gcc_except_table,"a", at progbits
+ .p2align 2
+.Lexception0:
+ .byte 255 # @LPStart Encoding = omit
+ .byte 255 # @TType Encoding = omit
+ .byte 1 # Call site Encoding = uleb128
+ .uleb128 .Lcst_end0-.Lcst_begin0
+.Lcst_begin0:
+ .uleb128 .Lfunc_begin0-.Lfunc_begin0 # Call Site 1
+ .uleb128 .Ltmp0-.Lfunc_begin0
+ .uleb128 .LLP0-.Lfunc_begin0 # landing pad
+ .byte 0 # action: cleanup
+ .uleb128 .Ltmp0-.Lfunc_begin0 # Call Site 2
+ .uleb128 .Ltmp1-.Ltmp0
+ .uleb128 .LLP1-.Lfunc_begin0 # landing pad
+ .byte 0 # action: cleanup
+.Lcst_end0:
+
+## Large code model function — triggers SHF_X86_64_LARGE detection.
+ .section .ltext,"axl", at progbits
+ .globl large_func
+ .type large_func, @function
+large_func:
+ .cfi_startproc
+ movl $42, %eax
+ retq
+ .cfi_endproc
+ .size large_func, .-large_func
diff --git a/bolt/unittests/Core/BinaryContext.cpp b/bolt/unittests/Core/BinaryContext.cpp
index d7374b323c916..0f5f50144adff 100644
--- a/bolt/unittests/Core/BinaryContext.cpp
+++ b/bolt/unittests/Core/BinaryContext.cpp
@@ -238,6 +238,30 @@ TEST_P(BinaryContextTester, BaseAddress2) {
ASSERT_FALSE(BaseAddress.has_value());
}
+TEST_P(BinaryContextTester, LargeCodeModelLSDAEncoding) {
+ // Verify that setting HasLargeCodeModel updates LSDAEncoding to use
+ // 8-byte pointers so exception handling works for functions at addresses
+ // above 2GB.
+ if (GetParam() != Triple::x86_64)
+ GTEST_SKIP();
+
+ // Default x86-64 LSDA encoding uses 4-byte (udata4).
+ unsigned DefaultEncoding = BC->LSDAEncoding;
+ ASSERT_NE(DefaultEncoding, dwarf::DW_EH_PE_absptr);
+
+ // Simulate detecting a large code model binary (non-PIC).
+ BC->HasLargeCodeModel = true;
+ BC->HasFixedLoadAddress = true;
+ BC->LSDAEncoding = dwarf::DW_EH_PE_absptr;
+ EXPECT_EQ(BC->LSDAEncoding, unsigned(dwarf::DW_EH_PE_absptr));
+
+ // PIC case: should use 8-byte PC-relative.
+ BC->HasFixedLoadAddress = false;
+ BC->LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8;
+ EXPECT_EQ(BC->LSDAEncoding,
+ unsigned(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8));
+}
+
TEST_P(BinaryContextTester, BaseAddressSegmentsSmallerThanAlignment) {
// Check that the correct segment is used to compute the base address
// when multiple segments are close together in the ELF file (closer
More information about the llvm-commits
mailing list