Skip to content

Commit abe0fa4

Browse files
committed
[llvm-objdump] Print comments for the disassembled code
LLVM disassembler can generate comments for disassembled instructions. The patch enables printing these comments for 'llvm-objdump -d'. Differential Revision: https://reviews.llvm.org/D104699
1 parent 04242bd commit abe0fa4

File tree

5 files changed

+77
-21
lines changed

5 files changed

+77
-21
lines changed

llvm/test/tools/llvm-objdump/ELF/AArch64/disassemble-align.s

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
## Use '|' to show where the tabs line up.
55
# CHECK:0000000000000000 <$x.0>:
6-
# CHECK-NEXT: 0: 62 10 00 91 |add|x2, x3, #4
6+
# CHECK-NEXT: 0: 62 10 00 91 |add|x2, x3, #4 // =4
7+
# CHECK-NEXT: 4: 1f 20 03 d5 |nop
78
# CHECK-EMPTY:
8-
# CHECK-NEXT:0000000000000004 <$d.1>:
9-
# CHECK-NEXT: 4:|ff ff 00 00|.word|0x0000ffff
9+
# CHECK-NEXT:0000000000000008 <$d.1>:
10+
# CHECK-NEXT: 8:|ff ff 00 00|.word|0x0000ffff
1011

1112
add x2, x3, #4
13+
nop
1214
.word 0xffff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Check that 'llvm-objdump -d' prints comments generated by the disassembler.
2+
3+
# RUN: llvm-mc -filetype=obj -triple=aarch64 -mattr=+sve %s -o %t
4+
# RUN: llvm-objdump -d --mattr=+sve --no-show-raw-insn %t | FileCheck %s
5+
6+
# CHECK: 0000000000000000 <foo>:
7+
# CHECK-NEXT: 0: add x0, x2, #2, lsl #12 // =8192
8+
# CHECK-NEXT: 4: add z31.d, z31.d, #65280 // =0xff00
9+
10+
.text
11+
foo:
12+
add x0, x2, 8192
13+
add z31.d, z31.d, #65280

llvm/test/tools/llvm-objdump/X86/disassemble-align.s

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t
22

33
# Use '|' to show where the tabs line up.
4-
# RUN: llvm-objdump -d --print-imm-hex %t | tr '\t' '|' | FileCheck --strict-whitespace %s
4+
# RUN: llvm-objdump -d --print-imm-hex %t | tr '\t' '|' | \
5+
# RUN: FileCheck --match-full-lines --strict-whitespace %s
56

67
# RUN: llvm-objdump -d --print-imm-hex --no-show-raw-insn %t | tr '\t' '|' | \
7-
# RUN: FileCheck -check-prefix=NORAW -strict-whitespace %s
8+
# RUN: FileCheck -check-prefix=NORAW --match-full-lines -strict-whitespace %s
89

910
# Instructions are expected to be aligned if the instruction in hex is not too long.
1011

11-
# CHECK: 0: c3 |retq
12-
# CHECK-NEXT: 1: 48 8b 05 56 34 12 00 |movq|0x123456(%rip), %rax
13-
# CHECK-NEXT: 8: 48 b8 54 55 55 55 55 55 55 55|movabsq|$0x5555555555555554, %rax
14-
# CHECK-NEXT: 12: 8f ea 00 12 4c 02 40 00 00 00 00 |lwpval|$0x0, 0x40(%rdx,%rax), %r15d
15-
# CHECK-NEXT: 1d: 8f ea 00 12 04 25 f0 1c f0 1c 00 00 00 00 |lwpins|$0x0, 0x1cf01cf0, %r15d
16-
# CHECK-NEXT: 2b: ff ff |<unknown>
12+
# CHECK: 0: c3 |retq
13+
# CHECK-NEXT: 1: 48 8b 05 56 34 12 00 |movq|0x123456(%rip), %rax # 12345e <.text+0x12345e>
14+
# CHECK-NEXT: 8: 48 b8 54 55 55 55 55 55 55 55|movabsq|$0x5555555555555554, %rax # imm = 0x5555555555555554
15+
# CHECK-NEXT: 12: 8f ea 00 12 4c 02 40 00 00 00 00 |lwpval|$0x0, 0x40(%rdx,%rax), %r15d
16+
# CHECK-NEXT: 1d: 8f ea 00 12 04 25 f0 1c f0 1c 00 00 00 00 |lwpins|$0x0, 0x1cf01cf0, %r15d
17+
# CHECK-NEXT: 2b: ff ff |<unknown>
1718

18-
# NORAW: 0: |retq
19-
# NORAW-NEXT: 1: |movq|0x123456(%rip), %rax
20-
# NORAW-NEXT: 8: |movabsq|$0x5555555555555554, %rax
21-
# NORAW-NEXT: 12: |lwpval|$0x0, 0x40(%rdx,%rax), %r15d
22-
# NORAW-NEXT: 1d: |lwpins|$0x0, 0x1cf01cf0, %r15d
23-
# NORAW-NEXT: 2b: |<unknown>
19+
# NORAW: 0: |retq
20+
# NORAW-NEXT: 1: |movq|0x123456(%rip), %rax # 12345e <.text+0x12345e>
21+
# NORAW-NEXT: 8: |movabsq|$0x5555555555555554, %rax # imm = 0x5555555555555554
22+
# NORAW-NEXT: 12: |lwpval|$0x0, 0x40(%rdx,%rax), %r15d
23+
# NORAW-NEXT: 1d: |lwpins|$0x0, 0x1cf01cf0, %r15d
24+
# NORAW-NEXT: 2b: |<unknown>
2425

2526
.text
2627
retq
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Check that 'llvm-objdump -d' prints comments generated by the disassembler.
2+
3+
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t
4+
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
5+
6+
# CHECK: 0000000000000000 <foo>:
7+
# CHECK-NEXT: 0: nop
8+
# CHECK-NEXT: 1: cmpl $305419896, %eax # imm = 0x12345678
9+
10+
.text
11+
foo:
12+
nop
13+
cmpl $0x12345678, %eax

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

+31-4
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,29 @@ static StringRef getSegmentName(const MachOObjectFile *MachO,
10391039
return "";
10401040
}
10411041

1042+
static void emitPostInstructionInfo(formatted_raw_ostream &FOS,
1043+
const MCAsmInfo &MAI,
1044+
const MCSubtargetInfo &STI,
1045+
StringRef Comments,
1046+
LiveVariablePrinter &LVP) {
1047+
do {
1048+
if (!Comments.empty()) {
1049+
// Emit a line of comments.
1050+
StringRef Comment;
1051+
std::tie(Comment, Comments) = Comments.split('\n');
1052+
// MAI.getCommentColumn() assumes that instructions are printed at the
1053+
// position of 8, while getInstStartColumn() returns the actual position.
1054+
unsigned CommentColumn =
1055+
MAI.getCommentColumn() - 8 + getInstStartColumn(STI);
1056+
FOS.PadToColumn(CommentColumn);
1057+
FOS << MAI.getCommentString() << ' ' << Comment;
1058+
}
1059+
LVP.printAfterInst(FOS);
1060+
FOS << '\n';
1061+
} while (!Comments.empty());
1062+
FOS.flush();
1063+
}
1064+
10421065
static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
10431066
MCContext &Ctx, MCDisassembler *PrimaryDisAsm,
10441067
MCDisassembler *SecondaryDisAsm,
@@ -1396,12 +1419,14 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
13961419
LVP.update({Index, Section.getIndex()},
13971420
{Index + Size, Section.getIndex()}, Index + Size != End);
13981421

1422+
IP->setCommentStream(CommentStream);
1423+
13991424
PIP.printInst(
14001425
*IP, Disassembled ? &Inst : nullptr, Bytes.slice(Index, Size),
14011426
{SectionAddr + Index + VMAAdjustment, Section.getIndex()}, FOS,
14021427
"", *STI, &SP, Obj->getFileName(), &Rels, LVP);
1403-
FOS << CommentStream.str();
1404-
Comments.clear();
1428+
1429+
IP->setCommentStream(llvm::nulls());
14051430

14061431
// If disassembly has failed, avoid analysing invalid/incomplete
14071432
// instruction information. Otherwise, try to resolve the target
@@ -1498,8 +1523,10 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
14981523
}
14991524
}
15001525

1501-
LVP.printAfterInst(FOS);
1502-
FOS << "\n";
1526+
assert(Ctx.getAsmInfo());
1527+
emitPostInstructionInfo(FOS, *Ctx.getAsmInfo(), *STI,
1528+
CommentStream.str(), LVP);
1529+
Comments.clear();
15031530

15041531
// Hexagon does this in pretty printer
15051532
if (Obj->getArch() != Triple::hexagon) {

0 commit comments

Comments
 (0)