From 0c3cda3f7cd03e5770ea1f17933481690eae4551 Mon Sep 17 00:00:00 2001 From: Maksim Panchenko Date: Tue, 19 Nov 2024 19:15:18 -0800 Subject: [PATCH 1/2] [BOLT] Use ULEB128 encoding for PIE/DSO exception tables Use ULEB128 encoding for call sites in PIE/DSO binaries. The encoding reduces the size of the tables compared to sdata4 and is the default format used by Clang. Note that for fixed-address executables we still use absolute addressing to cover cases where landing pads can reside in different function fragments. --- bolt/lib/Core/BinaryEmitter.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/bolt/lib/Core/BinaryEmitter.cpp b/bolt/lib/Core/BinaryEmitter.cpp index 408663180935c..7c4ff960a439d 100644 --- a/bolt/lib/Core/BinaryEmitter.cpp +++ b/bolt/lib/Core/BinaryEmitter.cpp @@ -959,9 +959,9 @@ void BinaryEmitter::emitLSDA(BinaryFunction &BF, const FunctionFragment &FF) { if (NeedsLPAdjustment) LPOffsetExpr = MCBinaryExpr::createAdd( LPOffsetExpr, MCConstantExpr::create(1, *BC.Ctx), *BC.Ctx); - Streamer.emitValue(LPOffsetExpr, 4); + Streamer.emitULEB128Value(LPOffsetExpr); } else { - Streamer.emitIntValue(0, 4); + Streamer.emitULEB128IntValue(0); } }; } @@ -979,7 +979,10 @@ void BinaryEmitter::emitLSDA(BinaryFunction &BF, const FunctionFragment &FF) { // 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 // earlier in the address space than LPStart. - Streamer.emitIntValue(dwarf::DW_EH_PE_sdata4, 1); + if (BC.HasFixedLoadAddress) + Streamer.emitIntValue(dwarf::DW_EH_PE_sdata4, 1); + else + Streamer.emitIntValue(dwarf::DW_EH_PE_uleb128, 1); MCSymbol *CSTStartLabel = BC.Ctx->createTempSymbol("CSTStart"); MCSymbol *CSTEndLabel = BC.Ctx->createTempSymbol("CSTEnd"); @@ -996,8 +999,13 @@ void BinaryEmitter::emitLSDA(BinaryFunction &BF, const FunctionFragment &FF) { // Start of the range is emitted relative to the start of current // function split part. - Streamer.emitAbsoluteSymbolDiff(BeginLabel, StartSymbol, 4); - Streamer.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4); + if (BC.HasFixedLoadAddress) { + Streamer.emitAbsoluteSymbolDiff(BeginLabel, StartSymbol, 4); + Streamer.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4); + } else { + Streamer.emitAbsoluteSymbolDiffAsULEB128(BeginLabel, StartSymbol); + Streamer.emitAbsoluteSymbolDiffAsULEB128(EndLabel, BeginLabel); + } emitLandingPad(CallSite.LP); Streamer.emitULEB128IntValue(CallSite.Action); } From 04fb4524e9f8a6c48993e57c820bfd8600249706 Mon Sep 17 00:00:00 2001 From: Maksim Panchenko Date: Wed, 20 Nov 2024 00:13:46 -0800 Subject: [PATCH 2/2] fixup! [BOLT] Use ULEB128 encoding for PIE/DSO exception tables --- bolt/lib/Core/BinaryEmitter.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bolt/lib/Core/BinaryEmitter.cpp b/bolt/lib/Core/BinaryEmitter.cpp index 7c4ff960a439d..4b5d8154728cc 100644 --- a/bolt/lib/Core/BinaryEmitter.cpp +++ b/bolt/lib/Core/BinaryEmitter.cpp @@ -976,9 +976,8 @@ void BinaryEmitter::emitLSDA(BinaryFunction &BF, const FunctionFragment &FF) { Streamer.emitLabel(TTBaseRefLabel); } - // 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 - // earlier in the address space than LPStart. + // Emit encoding of entries in the call site table. The format is used for the + // call site start, length, and corresponding landing pad. if (BC.HasFixedLoadAddress) Streamer.emitIntValue(dwarf::DW_EH_PE_sdata4, 1); else