From c9022a29b4d3c0c7b82c1ae795405c238a14bde1 Mon Sep 17 00:00:00 2001 From: Maksim Panchenko Date: Thu, 29 May 2025 19:24:34 -0700 Subject: [PATCH] [BOLT][AArch64] Detect veneers with missing data markers (#142069) The linker may omit data markers for long absolute veneers causing BOLT to treat data as code. Detect such veneers and introduce data markers artificially before BOLT's disassembler kicks in. --- bolt/lib/Rewrite/RewriteInstance.cpp | 13 +++++++++++++ bolt/test/AArch64/veneer-lld-abs.s | 13 ++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp index ad062ea3622d..69c39cf5e5ba 100644 --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/bolt/lib/Rewrite/RewriteInstance.cpp @@ -1341,6 +1341,19 @@ void RewriteInstance::discoverFileObjects() { } } } + + // The linker may omit data markers for absolute long veneers. Introduce + // those markers artificially to assist the disassembler. + for (BinaryFunction &BF : + llvm::make_second_range(BC->getBinaryFunctions())) { + if (BF.getOneName().starts_with("__AArch64AbsLongThunk_") && + BF.getSize() == 16 && !BF.getSizeOfDataInCodeAt(8)) { + BC->errs() << "BOLT-WARNING: missing data marker detected in veneer " + << BF << '\n'; + BF.markDataAtOffset(8); + BC->AddressToConstantIslandMap[BF.getAddress() + 8] = &BF; + } + } } if (!BC->IsLinuxKernel) { diff --git a/bolt/test/AArch64/veneer-lld-abs.s b/bolt/test/AArch64/veneer-lld-abs.s index 7e6fe2d12706..b22301db66c5 100644 --- a/bolt/test/AArch64/veneer-lld-abs.s +++ b/bolt/test/AArch64/veneer-lld-abs.s @@ -6,10 +6,21 @@ # RUN: -fuse-ld=lld -Wl,-q # RUN: llvm-objdump -d %t.exe | FileCheck --check-prefix=CHECK-INPUT %s # RUN: llvm-objcopy --remove-section .rela.mytext %t.exe -# RUN: llvm-bolt %t.exe -o %t.bolt --elim-link-veneers=true --lite=0 +# RUN: llvm-bolt %t.exe -o %t.bolt # RUN: llvm-objdump -d -j .text %t.bolt | \ # RUN: FileCheck --check-prefix=CHECK-OUTPUT %s +## Occasionally, we see the linker not generating $d symbols for long veneers +## causing BOLT to fail veneer elimination. +# RUN: llvm-objcopy --remove-symbol-prefix=\$d %t.exe %t.no-marker.exe +# RUN: llvm-bolt %t.no-marker.exe -o %t.no-marker.bolt \ +# RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-BOLT +# RUN: llvm-objdump -d -j .text %t.no-marker.bolt | \ +# RUN: FileCheck --check-prefix=CHECK-OUTPUT %s + +# CHECK-BOLT-NOT: BOLT-WARNING: unable to disassemble instruction +# CHECK-BOLT: BOLT-WARNING: missing data marker detected in veneer __AArch64AbsLongThunk_far_function + .text .balign 4 .global far_function