Skip to content

Commit 5b4a5cf

Browse files
authored
[RISCV][llvm-objdump] Support --symbolize-operands (llvm#166656)
This adds support for `--symbolize-operands`, so that local references are turned back into labels by objdump, which makes it easier to tell what is going on with a linked object. When using `--symbolize-operands`, branch target addresses are not printed, only the referenced symbol is printed, and the address is elided: ``` # Without --symbolize-operands 0: 04a05263 blez a0, 0x44 <.text+0x44> ... 40: fd1ff06f j 0x10 <.text+0x10> 44: 00000613 li a2, 0x0 # With --symbolize-operands 0: 04a05263 blez a0, <L3> ... 40: fd1ff06f j <L0> <L3>: 44: 00000613 li a2, 0x0 ```
1 parent 12cea04 commit 5b4a5cf

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

llvm/docs/CommandGuide/llvm-objdump.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ OPTIONS
284284
any analysis with a special representation (i.e. BlockFrequency,
285285
BranchProbability, etc) are printed as raw hex values.
286286

287-
Only supported for AArch64, BPF, PowerPC, and X86.
287+
Only supported for AArch64, BPF, PowerPC, RISC-V, and X86.
288288

289289
Example:
290290
A non-symbolized branch instruction with a local target and pc-relative memory access like

llvm/docs/ReleaseNotes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ Changes to the PowerPC Backend
120120
Changes to the RISC-V Backend
121121
-----------------------------
122122

123+
* `llvm-objdump` now has support for `--symbolize-operands` with RISC-V.
124+
123125
Changes to the WebAssembly Backend
124126
----------------------------------
125127

llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/MC/MCExpr.h"
1717
#include "llvm/MC/MCInst.h"
1818
#include "llvm/MC/MCInstPrinter.h"
19+
#include "llvm/MC/MCInstrAnalysis.h"
1920
#include "llvm/MC/MCSubtargetInfo.h"
2021
#include "llvm/MC/MCSymbol.h"
2122
#include "llvm/Support/CommandLine.h"
@@ -108,6 +109,10 @@ void RISCVInstPrinter::printBranchOperand(const MCInst *MI, uint64_t Address,
108109
unsigned OpNo,
109110
const MCSubtargetInfo &STI,
110111
raw_ostream &O) {
112+
// Do not print the numeric target address when symbolizing.
113+
if (SymbolizeOperands)
114+
return;
115+
111116
const MCOperand &MO = MI->getOperand(OpNo);
112117
if (!MO.isImm())
113118
return printOperand(MI, OpNo, STI, O);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# RUN: llvm-mc -triple=riscv32 < %s -mattr=-relax -filetype=obj -o - \
2+
# RUN: | llvm-objdump -d --no-leading-addr --no-show-raw-insn --symbolize-operands - \
3+
# RUN: | FileCheck %s
4+
5+
# CHECK-LABEL: <.text>:
6+
.text
7+
.p2align 2
8+
# CHECK: blez a0, <L4>
9+
blez a0, .LBB0_6
10+
li a3, 0
11+
li a2, 0
12+
# CHECK: j <L1>
13+
j .LBB0_3
14+
# CHECK-NEXT: <L0>:
15+
.LBB0_2:
16+
addi a3, a3, 1
17+
# CHECK: beq a3, a0, <L5>
18+
beq a3, a0, .LBB0_7
19+
# CHECK-NEXT: <L1>:
20+
.LBB0_3:
21+
slli a4, a3, 2
22+
add a4, a1, a4
23+
lw a5, 0(a4)
24+
lbu a4, 0(a5)
25+
# CHECK: beqz a4, <L0>
26+
beqz a4, .LBB0_2
27+
addi a5, a5, 1
28+
# CHECK: <L2>
29+
# CHECK-NEXT: jal t5, <L2>
30+
jal t5, foo
31+
# CHECK: <L3>
32+
.LBB0_5:
33+
add a2, a2, a4
34+
lbu a4, 0(a5)
35+
addi a5, a5, 1
36+
# CHECK: bnez a4, <L3>
37+
bnez a4, .LBB0_5
38+
# CHECK-NEXT: j <L0>
39+
j .LBB0_2
40+
# CHECK-NEXT: <L4>:
41+
.LBB0_6:
42+
li a2, 0
43+
# CHECK: <L5>:
44+
.LBB0_7:
45+
mv a0, a2
46+
# CHECK: <L6>:
47+
# CHECK-NEXT: auipc ra, 0x0
48+
# CHECK-NEXT: jalr ra <L6>
49+
call bar
50+
# CHECK-NEXT: <L7>:
51+
# CHECK-NEXT: beq a0, a2, <.text+0x17a>
52+
beq a0, a2, 0x122
53+
# CHECK-NEXT: bge a0, a2, <L7>
54+
bge a0, a2, -0x4
55+
ret

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,8 @@ collectLocalBranchTargets(ArrayRef<uint8_t> Bytes, MCInstrAnalysis *MIA,
16071607
const bool isX86 = STI->getTargetTriple().isX86();
16081608
const bool isAArch64 = STI->getTargetTriple().isAArch64();
16091609
const bool isBPF = STI->getTargetTriple().isBPF();
1610-
if (!isPPC && !isX86 && !isAArch64 && !isBPF)
1610+
const bool isRISCV = STI->getTargetTriple().isRISCV();
1611+
if (!isPPC && !isX86 && !isAArch64 && !isBPF && !isRISCV)
16111612
return;
16121613

16131614
if (MIA)

0 commit comments

Comments
 (0)