Skip to content

Commit 4111841

Browse files
authored
[BOLT] Correctly print preferred disassembly for annotated instructions (#120564)
This patch makes sure that `BinaryContext::printInstruction` prints the preferred disassembly. Preferred disassembly only gets printed when there are no annotations on the MCInst. Therefore, this patch temporarily removes the annotations before printing it. A few examples of before and after on AArch64 instructions are as follows: ``` BEFORE AFTER (preferred disassembly) ret x30 ret orr x30, xzr, x0 mov x30, x0 hint #29 autiasp hint #12 autia1716 ``` Clearly, the preferred disassembly is easier for developers to read, and is the disassembly that tools should be printing. This patch is motivated as part of future work on the llvm-bolt-binary-analysis tool, making sure that the reports it prints do use preferred disassembly. This patch was cherry-picked from https://github.com/kbeyls/llvm-project/tree/bolt-gadget-scanner-prototype. In this current patch, this only affects existing RISCV test cases. This patch also does improve test cases in future patches that will introduce a binary analysis for llvm-bolt-binary-analysis that checks for correct application of pac-ret (pointer authentication on return addresses).
1 parent 4096dd6 commit 4111841

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

bolt/lib/Core/BinaryContext.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -1961,7 +1961,15 @@ void BinaryContext::printInstruction(raw_ostream &OS, const MCInst &Instruction,
19611961
OS << "\tjit\t" << MIB->getTargetSymbol(Instruction)->getName()
19621962
<< " # ID: " << DynamicID;
19631963
} else {
1964-
InstPrinter->printInst(&Instruction, 0, "", *STI, OS);
1964+
// If there are annotations on the instruction, the MCInstPrinter will fail
1965+
// to print the preferred alias as it only does so when the number of
1966+
// operands is as expected. See
1967+
// https://github.com/llvm/llvm-project/blob/782f1a0d895646c364a53f9dcdd6d4ec1f3e5ea0/llvm/lib/MC/MCInstPrinter.cpp#L142
1968+
// Therefore, create a temporary copy of the Inst from which the annotations
1969+
// are removed, and print that Inst.
1970+
MCInst InstNoAnnot = Instruction;
1971+
MIB->stripAnnotations(InstNoAnnot);
1972+
InstPrinter->printInst(&InstNoAnnot, 0, "", *STI, OS);
19651973
}
19661974
if (MIB->isCall(Instruction)) {
19671975
if (MIB->isTailCall(Instruction))

bolt/test/RISCV/call-annotations.s

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ f:
1616

1717
// CHECK-LABEL: Binary Function "_start" after building cfg {
1818
// CHECK: auipc ra, f
19-
// CHECK-NEXT: jalr ra, -0x4(ra) # Offset: 4
20-
// CHECK-NEXT: jal ra, f # Offset: 8
21-
// CHECK-NEXT: jal zero, f # TAILCALL # Offset: 12
19+
// CHECK-NEXT: jalr -0x4(ra) # Offset: 4
20+
// CHECK-NEXT: jal f # Offset: 8
21+
// CHECK-NEXT: j f # TAILCALL # Offset: 12
2222

2323
// CHECK-LABEL: Binary Function "long_tail" after building cfg {
2424
// CHECK: auipc t1, f
25-
// CHECK-NEXT: jalr zero, -0x18(t1) # TAILCALL # Offset: 8
25+
// CHECK-NEXT: jr -0x18(t1) # TAILCALL # Offset: 8
2626

2727
// CHECK-LABEL: Binary Function "compressed_tail" after building cfg {
2828
// CHECK: jr a0 # TAILCALL # Offset: 0

bolt/test/RISCV/relax.s

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
// RUN: llvm-objdump -d %t.bolt | FileCheck --check-prefix=OBJDUMP %s
66

77
// CHECK: Binary Function "_start" after building cfg {
8-
// CHECK: jal ra, near_f
8+
// CHECK: jal near_f
99
// CHECK-NEXT: auipc ra, far_f
10-
// CHECK-NEXT: jalr ra, 0xc(ra)
10+
// CHECK-NEXT: jalr 0xc(ra)
1111
// CHECK-NEXT: j near_f
1212

1313
// CHECK: Binary Function "_start" after fix-riscv-calls {

bolt/test/RISCV/reloc-branch.s

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
.p2align 1
88
// CHECK: Binary Function "_start" after building cfg {
99
_start:
10-
// CHECK: beq zero, zero, .Ltmp0
10+
// CHECK: beqz zero, .Ltmp0
1111
beq zero, zero, 1f
1212
nop
1313
// CHECK: .Ltmp0

bolt/test/RISCV/reloc-jal.s

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ f:
1414
.globl _start
1515
.p2align 1
1616
_start:
17-
// CHECK: jal ra, f
17+
// CHECK: jal f
1818
jal ra, f
1919
ret
2020
.size _start, .-_start

0 commit comments

Comments
 (0)