Skip to content

Commit 855c74d

Browse files
committed
[BOLT] Introduce --disallow-pacret flag
1 parent 054f614 commit 855c74d

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

bolt/include/bolt/Utils/CommandLineOpts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ extern llvm::cl::opt<std::string> OutputFilename;
7878
extern llvm::cl::opt<std::string> PerfData;
7979
extern llvm::cl::opt<bool> PrintCacheMetrics;
8080
extern llvm::cl::opt<bool> PrintSections;
81+
extern llvm::cl::opt<bool> DisallowPacret;
8182

8283
// The format to use with -o in aggregation mode (perf2bolt)
8384
enum ProfileFormatKind { PF_Fdata, PF_YAML };

bolt/lib/Rewrite/BinaryPassManager.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,15 @@ static cl::opt<bool> ShortenInstructions("shorten-instructions",
278278
cl::desc("shorten instructions"),
279279
cl::init(true),
280280
cl::cat(BoltOptCategory));
281+
282+
// This flag is used to "gate" the negate-ra-state CFI handling.
283+
// Sometimes, binaries use pac-ret but not contain negate-ra-state CFIs. That
284+
// should cause no issues for BOLT.
285+
cl::opt<bool> DisallowPacret(
286+
"disallow-pacret",
287+
cl::desc("Disable processing binaries containing negate-ra-state DWARF "
288+
"CFIs (e.g. binaries using pac-ret hardening)"),
289+
cl::cat(BoltOptCategory));
281290
} // namespace opts
282291

283292
namespace llvm {

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,6 +3520,17 @@ void RewriteInstance::disassembleFunctions() {
35203520
}
35213521
}
35223522

3523+
// Check if fillCFIInfoFor removed any OpNegateRAState CFIs from the
3524+
// function.
3525+
if (Function.containedNegateRAState()) {
3526+
if (opts::DisallowPacret) {
3527+
BC->errs() << "BOLT-ERROR: --disallow-pacret flag was used, but "
3528+
<< Function.getPrintName()
3529+
<< " contains .cfi-negate-ra-state.\n";
3530+
exit(1);
3531+
}
3532+
}
3533+
35233534
// Parse LSDA.
35243535
if (Function.getLSDAAddress() != 0 &&
35253536
!BC->getFragmentsToSkip().count(&Function)) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
2+
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
3+
# RUN: not llvm-bolt %t.exe -o %t.exe.bolt --disallow-pacret 2>&1 | FileCheck %s
4+
5+
# CHECK: BOLT-ERROR: --disallow-pacret flag was used, but foo contains .cfi-negate-ra-state.
6+
7+
.text
8+
.globl foo
9+
.p2align 2
10+
.type foo,@function
11+
foo:
12+
.cfi_startproc
13+
hint #25
14+
.cfi_negate_ra_state
15+
mov x1, #0
16+
hint #29
17+
.cfi_negate_ra_state
18+
ret
19+
.cfi_endproc
20+
.size foo, .-foo
21+
22+
.global _start
23+
.type _start, %function
24+
_start:
25+
b foo

0 commit comments

Comments
 (0)