Skip to content

Commit 3c9c9c1

Browse files
committed
[llvm-objdump] Print target address with evaluateMemoryOperandAddress()
D63847 added `MCInstrAnalysis::evaluateMemoryOperandAddress()`. This patch leverages the feature to print the target addresses for evaluable instructions. ``` -400a: movl 4080(%rip), %eax +400a: movl 4080(%rip), %eax # 5000 <data1> ``` This patch also deletes `MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) || MIA->isConditionalBranch(Inst)` which is used to guard `MCInstrAnalysis::evaluateBranch()` Reviewed By: jhenderson, skan Differential Revision: https://reviews.llvm.org/D78776
1 parent 5c03bee commit 3c9c9c1

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

llvm/lib/Target/ARM/MCTargetDesc/ARMMCTargetDesc.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ class ARMMCInstrAnalysis : public MCInstrAnalysis {
266266
bool evaluateBranch(const MCInst &Inst, uint64_t Addr,
267267
uint64_t Size, uint64_t &Target) const override {
268268
// We only handle PCRel branches for now.
269-
if (Info->get(Inst.getOpcode()).OpInfo[0].OperandType!=MCOI::OPERAND_PCREL)
269+
if (Inst.getNumOperands() == 0 ||
270+
Info->get(Inst.getOpcode()).OpInfo[0].OperandType !=
271+
MCOI::OPERAND_PCREL)
270272
return false;
271273

272274
int64_t Imm = Inst.getOperand(0).getImm();
@@ -285,6 +287,8 @@ class ThumbMCInstrAnalysis : public ARMMCInstrAnalysis {
285287
switch (Inst.getOpcode()) {
286288
default:
287289
OpId = 0;
290+
if (Inst.getNumOperands() == 0)
291+
return false;
288292
break;
289293
case ARM::MVE_WLSTP_8:
290294
case ARM::MVE_WLSTP_16:

llvm/test/MC/X86/tlsdesc-64.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
# SYM: TLS GLOBAL DEFAULT UND a
1111

12-
# CHECK: 0: leaq (%rip), %rax
12+
# CHECK: 0: leaq (%rip), %rax # 7 <{{.*}}>
1313
# CHECK-NEXT: 0000000000000003: R_X86_64_GOTPC32_TLSDESC a-0x4
1414
# CHECK-NEXT: 7: callq *(%rax)
1515
# CHECK-NEXT: 0000000000000007: R_X86_64_TLSDESC_CALL a

llvm/test/tools/llvm-objdump/X86/elf-disassemble-symbol-references.yaml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
# RUN: yaml2obj %s --docnum=1 -o %t
33
# RUN: llvm-objdump %t -d | FileCheck %s --check-prefix=EXEC
44

5-
# EXEC: Disassembly of section .text1:
6-
# EXEC: 4000: e8 00 00 00 00 callq 0x4005 <third>
7-
# EXEC: Disassembly of section .text2:
8-
# EXEC: 4005: e8 12 34 56 78 callq 0x7856741c <fourth+0x78563412>
5+
# EXEC-LABEL: <first>:
6+
# EXEC-NEXT: 4000: e8 00 00 00 00 callq 0x4005 <third>
7+
# EXEC-LABEL: <third>:
8+
# EXEC-NEXT: 4005: e8 12 34 56 78 callq 0x7856741c <data1+0x7856241c>
9+
# EXEC-LABEL: <fourth>:
10+
# EXEC-NEXT: 400a: 8b 05 f0 0f 00 00 movl 4080(%rip), %eax # 5000 <data1>
911

1012
--- !ELF
1113
FileHeader:
@@ -28,6 +30,11 @@ Sections:
2830
Type: SHT_PROGBITS
2931
Flags: [SHF_ALLOC, SHF_EXECINSTR]
3032
Address: 0x400A
33+
Content: '8b05f00f0000' # Case 3: Memory operands
34+
- Name: .data
35+
Type: SHT_PROGBITS
36+
Flags: [SHF_ALLOC, SHF_WRITE]
37+
Address: 0x5000
3138
Symbols:
3239
- Name: first
3340
Section: .text1
@@ -41,6 +48,9 @@ Symbols:
4148
- Name: fourth
4249
Section: .text3
4350
Value: 0x400A
51+
- Name: data1
52+
Section: .data
53+
Value: 0x5000
4454

4555
# RUN: yaml2obj %s --docnum=2 -o %t.o
4656
# RUN: llvm-objdump %t.o -d | FileCheck %s --check-prefix=REL

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,13 +1513,22 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
15131513
Comments.clear();
15141514

15151515
// If disassembly has failed, avoid analysing invalid/incomplete
1516-
// instruction information. Otherwise, try to resolve the target of a
1517-
// call, tail call, etc. to a specific symbol.
1518-
if (Disassembled && MIA &&
1519-
(MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) ||
1520-
MIA->isConditionalBranch(Inst))) {
1516+
// instruction information. Otherwise, try to resolve the target address
1517+
// (jump target or memory operand address) and print it on the right of
1518+
// the instruction.
1519+
if (Disassembled && MIA) {
15211520
uint64_t Target;
1522-
if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) {
1521+
bool PrintTarget =
1522+
MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target);
1523+
if (!PrintTarget)
1524+
if (Optional<uint64_t> MaybeTarget =
1525+
MIA->evaluateMemoryOperandAddress(Inst, SectionAddr + Index,
1526+
Size)) {
1527+
Target = *MaybeTarget;
1528+
PrintTarget = true;
1529+
outs() << " # " << Twine::utohexstr(Target);
1530+
}
1531+
if (PrintTarget) {
15231532
// In a relocatable object, the target's section must reside in
15241533
// the same section as the call instruction or it is accessed
15251534
// through a relocation.

0 commit comments

Comments
 (0)