diff --git a/0004-BOLT-Preserve-original-LSDA-type-encoding.patch b/0004-BOLT-Preserve-original-LSDA-type-encoding.patch new file mode 100644 index 0000000000000000000000000000000000000000..6533c50fd023ddf4a2834fec3dfaf506c7e9e717 --- /dev/null +++ b/0004-BOLT-Preserve-original-LSDA-type-encoding.patch @@ -0,0 +1,185 @@ +From 553c23895217cfe24404a50dede2416c9e1a3a5e Mon Sep 17 00:00:00 2001 +From: "revunov.denis@huawei.com" +Date: Wed, 14 Sep 2022 16:29:48 +0000 +Subject: [PATCH] [BOLT] Preserve original LSDA type encoding + +In non-pie binaries BOLT unconditionally converted type encoding +from indirect to absptr, which broke std exceptions since pointers +to their typeinfo were only assigned at runtime in .data section. +In this patch we preserve original encoding so that indirect +remains indirect and can be resolved at runtime, and absolute remains absolute. + +Reviewed By: rafauler, maksfb + +Differential Revision: https://reviews.llvm.org/D132484 +--- + bolt/include/bolt/Core/BinaryContext.h | 3 +- + bolt/include/bolt/Core/BinaryFunction.h | 5 +++ + bolt/lib/Core/BinaryContext.cpp | 5 --- + bolt/lib/Core/BinaryEmitter.cpp | 11 ++++--- + bolt/lib/Core/Exceptions.cpp | 1 + + bolt/test/runtime/exceptions-no-pie.cpp | 41 +++++++++++++++++++++++++ + 6 files changed, 55 insertions(+), 11 deletions(-) + create mode 100644 bolt/test/runtime/exceptions-no-pie.cpp + +diff --git a/bolt/include/bolt/Core/BinaryContext.h b/bolt/include/bolt/Core/BinaryContext.h +index 41e6e5c981d1..02af0a1c58cf 100644 +--- a/bolt/include/bolt/Core/BinaryContext.h ++++ b/bolt/include/bolt/Core/BinaryContext.h +@@ -388,6 +388,8 @@ public: + } + + unsigned getDWARFEncodingSize(unsigned Encoding) { ++ if (Encoding == dwarf::DW_EH_PE_omit) ++ return 0; + switch (Encoding & 0x0f) { + default: + llvm_unreachable("unknown encoding"); +@@ -671,7 +673,6 @@ public: + + /// DWARF encoding. Available encoding types defined in BinaryFormat/Dwarf.h + /// enum Constants, e.g. DW_EH_PE_omit. +- unsigned TTypeEncoding = dwarf::DW_EH_PE_omit; + unsigned LSDAEncoding = dwarf::DW_EH_PE_omit; + + BinaryContext(std::unique_ptr Ctx, +diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h +index 88b500e30e26..36996ae1d9a4 100644 +--- a/bolt/include/bolt/Core/BinaryFunction.h ++++ b/bolt/include/bolt/Core/BinaryFunction.h +@@ -388,6 +388,9 @@ private: + /// Original LSDA address for the function. + uint64_t LSDAAddress{0}; + ++ /// Original LSDA type encoding ++ unsigned LSDATypeEncoding{dwarf::DW_EH_PE_omit}; ++ + /// Containing compilation unit for the function. + DWARFUnit *DwarfUnit{nullptr}; + +@@ -1438,6 +1441,8 @@ public: + + const LSDATypeTableTy &getLSDATypeTable() const { return LSDATypeTable; } + ++ unsigned getLSDATypeEncoding() const { return LSDATypeEncoding; } ++ + const LSDATypeTableTy &getLSDATypeAddressTable() const { + return LSDATypeAddressTable; + } +diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp +index 1c4c53c4da29..a79d16e629a7 100644 +--- a/bolt/lib/Core/BinaryContext.cpp ++++ b/bolt/lib/Core/BinaryContext.cpp +@@ -186,13 +186,9 @@ BinaryContext::createBinaryContext(const ObjectFile *File, bool IsPIC, + Large = true; + unsigned LSDAEncoding = + Large ? dwarf::DW_EH_PE_absptr : dwarf::DW_EH_PE_udata4; +- unsigned TTypeEncoding = +- Large ? dwarf::DW_EH_PE_absptr : dwarf::DW_EH_PE_udata4; + if (IsPIC) { + LSDAEncoding = dwarf::DW_EH_PE_pcrel | + (Large ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4); +- TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | +- (Large ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4); + } + + std::unique_ptr DisAsm( +@@ -236,7 +232,6 @@ BinaryContext::createBinaryContext(const ObjectFile *File, bool IsPIC, + std::move(InstructionPrinter), std::move(MIA), nullptr, std::move(MRI), + std::move(DisAsm)); + +- BC->TTypeEncoding = TTypeEncoding; + BC->LSDAEncoding = LSDAEncoding; + + BC->MAB = std::unique_ptr( +diff --git a/bolt/lib/Core/BinaryEmitter.cpp b/bolt/lib/Core/BinaryEmitter.cpp +index 5927269500d6..bc5bcadd52d0 100644 +--- a/bolt/lib/Core/BinaryEmitter.cpp ++++ b/bolt/lib/Core/BinaryEmitter.cpp +@@ -897,7 +897,7 @@ void BinaryEmitter::emitLSDA(BinaryFunction &BF, const FunctionFragment &FF) { + + Streamer.switchSection(BC.MOFI->getLSDASection()); + +- const unsigned TTypeEncoding = BC.TTypeEncoding; ++ const unsigned TTypeEncoding = BF.getLSDATypeEncoding(); + const unsigned TTypeEncodingSize = BC.getDWARFEncodingSize(TTypeEncoding); + const uint16_t TTypeAlignment = 4; + +@@ -971,10 +971,11 @@ void BinaryEmitter::emitLSDA(BinaryFunction &BF, const FunctionFragment &FF) { + TTypeBaseOffset; // TType base offset + unsigned SizeAlign = (4 - TotalSize) & 3; + +- // Account for any extra padding that will be added to the call site table +- // length. +- Streamer.emitULEB128IntValue(TTypeBaseOffset, +- /*PadTo=*/TTypeBaseOffsetSize + SizeAlign); ++ if (TTypeEncoding != dwarf::DW_EH_PE_omit) ++ // Account for any extra padding that will be added to the call site table ++ // length. ++ Streamer.emitULEB128IntValue(TTypeBaseOffset, ++ /*PadTo=*/TTypeBaseOffsetSize + SizeAlign); + + // Emit the landing pad call site table. We use signed data4 since we can emit + // a landing pad in a different part of the split function that could appear +diff --git a/bolt/lib/Core/Exceptions.cpp b/bolt/lib/Core/Exceptions.cpp +index 10a2c4bdc610..b7ba53ea75aa 100644 +--- a/bolt/lib/Core/Exceptions.cpp ++++ b/bolt/lib/Core/Exceptions.cpp +@@ -121,6 +121,7 @@ void BinaryFunction::parseLSDA(ArrayRef LSDASectionData, + : *MaybeLPStart - Address; + + const uint8_t TTypeEncoding = Data.getU8(&Offset); ++ LSDATypeEncoding = TTypeEncoding; + size_t TTypeEncodingSize = 0; + uintptr_t TTypeEnd = 0; + if (TTypeEncoding != DW_EH_PE_omit) { +diff --git a/bolt/test/runtime/exceptions-no-pie.cpp b/bolt/test/runtime/exceptions-no-pie.cpp +new file mode 100644 +index 000000000000..0c0df04f086c +--- /dev/null ++++ b/bolt/test/runtime/exceptions-no-pie.cpp +@@ -0,0 +1,41 @@ ++// REQUIRES: system-linux ++// RUN: %clangxx -no-pie -Wl,-q %s -o %t.exe ++// RUN: llvm-bolt %t.exe -o %t.bolt.exe -lite=false ++// RUN: not --crash %t.bolt.exe 2>&1 | FileCheck %s --check-prefix=CHECK-FAIL ++// CHECK-FAIL: Should pass one argument ++// RUN: not %t.bolt.exe -1 | FileCheck %s --check-prefix=CHECK-BAD ++// CHECK-BAD: Bad value ++// RUN: not %t.bolt.exe 0 | FileCheck %s --check-prefix=CHECK-ZERO ++// CHECK-ZERO: Value is zero ++// RUN: %t.bolt.exe 1 | FileCheck %s --check-prefix=CHECK-GOOD ++// CHECK-GOOD: Good value ++#include ++#include ++ ++struct ValIsZero { ++ const char *error = "Value is zero\n"; ++}; ++int dummy(int arg) { ++ if (arg == 0) ++ throw ValIsZero(); ++ if (arg > 0) ++ return 0; ++ else ++ throw std::out_of_range("Bad value"); ++} ++ ++int main(int argc, char **argv) { ++ if (argc != 2) ++ throw std::invalid_argument("Should pass one argument"); ++ try { ++ dummy(std::strtol(argv[1], nullptr, 10)); ++ } catch (std::out_of_range &e) { ++ std::cout << e.what() << "\n"; ++ return 1; ++ } catch (ValIsZero &e) { ++ std::cout << e.error; ++ return 1; ++ } ++ std::cout << "Good value\n"; ++ return 0; ++} +-- +2.33.0 + diff --git a/llvm-bolt.spec b/llvm-bolt.spec index 4107703fbb580af24cb04f0036dcbc8197294b8d..7d6a9bfcbf8b983f6c5c57f9a031d1da10a4a4db 100644 --- a/llvm-bolt.spec +++ b/llvm-bolt.spec @@ -15,7 +15,7 @@ Name: llvm-bolt Version: %{bolt_version} -Release: 2 +Release: 3 Summary: BOLT is a post-link optimizer developed to speed up large applications License: Apache 2.0 URL: https://github.com/llvm/llvm-project/tree/main/bolt @@ -25,6 +25,7 @@ Source0: https://github.com/llvm/llvm-project/releases/download/llvmorg-% Patch1: 0001-AArch64-fix-bug-55005-handle-DW_CFA_GNU_NegateRAState.patch Patch2: 0002-AArch64-Add-AArch64-support-for-hugify.patch Patch3: 0003-AArch64-Add-AArch64-support-for-inline.patch +Patch4: 0004-BOLT-Preserve-original-LSDA-type-encoding.patch BuildRequires: gcc BuildRequires: gcc-c++ @@ -140,6 +141,12 @@ rm -f %{buildroot}/%{_builddir}/%{bolt_srcdir}/lib/lib*.a %doc %{install_docdir} %changelog +* Wed Mar 6 2024 zhenyu zhao 15.0.7-3 +- Type:Fix +- ID:NA +- SUG:NA +- DESC: [Backport] Preserve original LSDA type encoding. + * Thu Sep 7 2023 Xiong Zhou 15.0.7-2 - Type:Update - ID:NA