Index: bolt/include/bolt/Core/MCPlusBuilder.h =================================================================== --- bolt/include/bolt/Core/MCPlusBuilder.h +++ bolt/include/bolt/Core/MCPlusBuilder.h @@ -639,9 +639,12 @@ return false; } - /// If non-zero, this is used to fill the executable space with instructions - /// that will trap. Defaults to 0. - virtual unsigned getTrapFillValue() const { return 0; } + /// Used to fill the executable space with instructions + /// that will trap. + virtual StringRef getTrapFillValue() const { + llvm_unreachable("not implemented"); + return StringRef(); + } /// Interface and basic functionality of a MCInstMatcher. The idea is to make /// it easy to match one or more MCInsts against a tree-like pattern and Index: bolt/lib/Core/BinaryEmitter.cpp =================================================================== --- bolt/lib/Core/BinaryEmitter.cpp +++ bolt/lib/Core/BinaryEmitter.cpp @@ -376,7 +376,7 @@ } if (opts::MarkFuncs) - Streamer.emitIntValue(BC.MIB->getTrapFillValue(), 1); + Streamer.emitBytes(BC.MIB->getTrapFillValue()); // Emit CFI end if (Function.hasCFI()) @@ -420,7 +420,7 @@ // case, the call site entries in that LSDA have 0 as offset to the landing // pad, which the runtime interprets as "no handler". To prevent this, // insert some padding. - Streamer.emitIntValue(BC.MIB->getTrapFillValue(), 1); + Streamer.emitBytes(BC.MIB->getTrapFillValue()); } // Track the first emitted instruction with debug info. Index: bolt/lib/Rewrite/RewriteInstance.cpp =================================================================== --- bolt/lib/Rewrite/RewriteInstance.cpp +++ bolt/lib/Rewrite/RewriteInstance.cpp @@ -5272,8 +5272,10 @@ if (!BF.getFileOffset() || !BF.isEmitted()) continue; OS.seek(BF.getFileOffset()); - for (unsigned I = 0; I < BF.getMaxSize(); ++I) - OS.write((unsigned char)BC->MIB->getTrapFillValue()); + StringRef TrapInstr = BC->MIB->getTrapFillValue(); + unsigned NInstr = BF.getMaxSize() / TrapInstr.size(); + for (unsigned I = 0; I < NInstr; ++I) + OS.write(TrapInstr.data(), TrapInstr.size()); } OS.seek(SavedPos); } Index: bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp =================================================================== --- bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp +++ bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp @@ -1132,6 +1132,10 @@ } } + StringRef getTrapFillValue() const override { + return StringRef("\0\0\0\0", 4); + } + bool createReturn(MCInst &Inst) const override { Inst.setOpcode(AArch64::RET); Inst.clear(); Index: bolt/lib/Target/RISCV/RISCVMCPlusBuilder.cpp =================================================================== --- bolt/lib/Target/RISCV/RISCVMCPlusBuilder.cpp +++ bolt/lib/Target/RISCV/RISCVMCPlusBuilder.cpp @@ -171,6 +171,10 @@ return true; } + StringRef getTrapFillValue() const override { + return StringRef("\0\0\0\0", 4); + } + bool analyzeBranch(InstructionIterator Begin, InstructionIterator End, const MCSymbol *&TBB, const MCSymbol *&FBB, MCInst *&CondBranch, Index: bolt/lib/Target/X86/X86MCPlusBuilder.cpp =================================================================== --- bolt/lib/Target/X86/X86MCPlusBuilder.cpp +++ bolt/lib/Target/X86/X86MCPlusBuilder.cpp @@ -397,7 +397,7 @@ } } - unsigned getTrapFillValue() const override { return 0xCC; } + StringRef getTrapFillValue() const override { return StringRef("\314", 1); } struct IndJmpMatcherFrag1 : MCInstMatcher { std::unique_ptr Base; Index: bolt/test/runtime/mark-funcs.c =================================================================== --- /dev/null +++ bolt/test/runtime/mark-funcs.c @@ -0,0 +1,22 @@ +#include + +int dummy() { + printf("Dummy called\n"); + return 0; +} + +int main(int argc, char **argv) { + if (dummy() != 0) + return 1; + printf("Main called\n"); + return 0; +} +// Check that emitting trap value works properly and +// does not break functions +// REQUIRES: system-linux +// RUN: %clangxx -Wl,-q %s -o %t.exe +// RUN: %t.exe | FileCheck %s +// CHECK: Dummy called +// CHECK-NEXT: Main called +// RUN: llvm-bolt %t.exe -o %t.exe.bolt -lite=false --mark-funcs +// RUN: %t.exe.bolt | FileCheck %s