Skip to content

Commit b343a44

Browse files
committed
[BOLT][BTI] Disassemble PLT entries when processing BTI binaries
PLT entries are PseudoFunctions, and are not disassembled or emitted. For BTI, we need to check the first MCInst of PLT entries, to see if indirectly calling them is safe or not. This patch disassembles PLTs for binaries using BTI, while not changing the behaviour for binaries without BTI. The PLTs are only disassembled, not emitted.
1 parent e493e90 commit b343a44

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ static cl::opt<bool> TrapOnAVX512(
147147
cl::init(false), cl::ZeroOrMore, cl::Hidden, cl::cat(BoltCategory));
148148

149149
bool shouldPrint(const BinaryFunction &Function) {
150+
// PLT stubs are disassembled for BTI binaries, therefore they should be
151+
// printed.
152+
if (Function.getBinaryContext().usesBTI() && Function.isPLTFunction())
153+
return true;
154+
150155
if (Function.isIgnored())
151156
return false;
152157

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,12 @@ Error RewriteInstance::setProfile(StringRef Filename) {
461461

462462
/// Return true if the function \p BF should be disassembled.
463463
static bool shouldDisassemble(const BinaryFunction &BF) {
464+
465+
const BinaryContext &BC = BF.getBinaryContext();
466+
// Disassemble PLT functions on AArch64 to check BTI landing pads.
467+
if (BC.usesBTI() && BF.isPLTFunction())
468+
return true;
469+
464470
if (BF.isPseudo())
465471
return false;
466472

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This test checks that BOLT disassembles PLT stubs in binaries using BTI,
2+
// while keeping them not disassembled in non-BTI binaries.
3+
4+
// RUN: %clang -fuse-ld=lld --target=aarch64-unknown-linux-gnu %s -o %t.exe \
5+
// RUN: -Wl,-q
6+
// RUN: llvm-bolt %t.exe -o %t.bolt --print-disasm | FileCheck %s
7+
8+
// RUN: %clang -fuse-ld=lld --target=aarch64-unknown-linux-gnu \
9+
// RUN: -mbranch-protection=standard %s -o %t.bti.exe -Wl,-q -Wl,-z,force-bti
10+
// RUN: llvm-bolt %t.bti.exe -o %t.bolt --print-disasm | FileCheck %s \
11+
// RUN: --check-prefix=CHECK-BTI
12+
13+
// For the non-BTI binary, PLTs should not be disassembled.
14+
// CHECK-NOT: Binary Function "{{.*}}@PLT" after disassembly {
15+
16+
// Check that PLTs are disassembled for the BTI binary.
17+
// CHECK-BTI: Binary Function "__libc_start_main@PLT" after disassembly {
18+
// CHECK-BTI: adrp
19+
// CHECK-BTI: ldr
20+
// CHECK-BTI: add
21+
// CHECK-BTI: br
22+
// CHECK-BTI: End of Function "__libc_start_main@PLT"
23+
24+
#include <stdio.h>
25+
#include <stdlib.h>
26+
int main(int argc, char **argv) {
27+
if (argc > 3)
28+
exit(42);
29+
else
30+
printf("Number of args: %d\n", argc);
31+
}

0 commit comments

Comments
 (0)