Skip to content

Commit 36819ea

Browse files
authored
[llvm-objdump] Support --symbolize-operand on AArch64
Similar to the existing implementations for X86 and PPC, support symbolizing branch targets for AArch64. Do not omit the address for ADRP as the target is typically not at an intended location. Pull Request: #145009
1 parent 10edc3d commit 36819ea

File tree

5 files changed

+159
-2
lines changed

5 files changed

+159
-2
lines changed

llvm/docs/CommandGuide/llvm-objdump.rst

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

281-
Only works with PowerPC objects or X86 linked images.
281+
Only supported for AArch64, BPF, PowerPC, and X86.
282282

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

llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,6 +1784,10 @@ void AArch64InstPrinter::printAlignedLabel(const MCInst *MI, uint64_t Address,
17841784
unsigned OpNum,
17851785
const MCSubtargetInfo &STI,
17861786
raw_ostream &O) {
1787+
// Do not print the numeric target address when symbolizing.
1788+
if (SymbolizeOperands)
1789+
return;
1790+
17871791
const MCOperand &Op = MI->getOperand(OpNum);
17881792

17891793
// If the label has already been resolved to an immediate offset (say, when
@@ -1813,6 +1817,12 @@ void AArch64InstPrinter::printAdrAdrpLabel(const MCInst *MI, uint64_t Address,
18131817
unsigned OpNum,
18141818
const MCSubtargetInfo &STI,
18151819
raw_ostream &O) {
1820+
// Do not print the numeric target address when symbolizing.
1821+
// However, do print for ADRP, as this is typically used together with an ADD
1822+
// or an immediate-offset ldr/str and the label is likely at the wrong point.
1823+
if (SymbolizeOperands && MI->getOpcode() != AArch64::ADRP)
1824+
return;
1825+
18161826
const MCOperand &Op = MI->getOperand(OpNum);
18171827

18181828
// If the label has already been resolved to an immediate offset (say, when
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# RUN: yaml2obj %s -o %t
2+
# RUN: llvm-objdump %t -d --symbolize-operands --no-show-raw-insn --no-leading-addr | \
3+
# RUN: FileCheck %s --match-full-lines -DABS_ADRP_VAL=0x6000
4+
# RUN: llvm-objdump %t -d --symbolize-operands --no-show-raw-insn --no-leading-addr --adjust-vma=0x2000 | \
5+
# RUN: FileCheck %s --match-full-lines -DABS_ADRP_VAL=0x8000
6+
7+
## Expect to find the branch labels and global variable name.
8+
# CHECK: <_start>:
9+
# CHECK-NEXT: ldr x0, <symbol>
10+
# CHECK-NEXT: <L0>:
11+
# CHECK-NEXT: adrp x1, [[ABS_ADRP_VAL]] <symbol+0xff4>
12+
# CHECK-NEXT: adr x2, <symbol>
13+
# CHECK-NEXT: cmp x1, x2
14+
# CHECK-NEXT: b.eq <L1>
15+
# CHECK-NEXT: b <L0>
16+
# CHECK-NEXT: <L1>:
17+
# CHECK-NEXT: cbz x2, <L0>
18+
# CHECK-NEXT: ret
19+
20+
## Machine code generated with:
21+
# llvm-mc --arch=aarch64 --filetype=obj -o tmp.o <<EOF
22+
# .text
23+
# .p2align 14
24+
# .globl .start
25+
# _start:
26+
# ldr x0, symbol
27+
# 1:
28+
# adrp x1, symbol + 0x1000
29+
# adr x2, symbol
30+
# cmp x1, x2
31+
# b.eq 2f
32+
# b 1b
33+
# 2:
34+
# cbz x2, 1b
35+
# ret
36+
#
37+
# .data
38+
# .p2align 12
39+
# .skip 12
40+
# symbol:
41+
# EOF
42+
# ld.lld -shared --nmagic -o tmp.so tmp.o
43+
# llvm-objdump -s tmp.so --section=.text
44+
45+
--- !ELF
46+
FileHeader:
47+
Class: ELFCLASS64
48+
Data: ELFDATA2LSB
49+
Type: ET_EXEC
50+
Machine: EM_AARCH64
51+
Sections:
52+
- Name: .text
53+
Type: SHT_PROGBITS
54+
Address: 0x4000
55+
Flags: [SHF_ALLOC, SHF_EXECINSTR]
56+
Content: '60800058010000d0228000103f0002eb40000054fcffff1762ffffb4c0035fd6'
57+
- Name: .data
58+
Type: SHT_PROGBITS
59+
Flags: [SHF_ALLOC, SHF_WRITE]
60+
Address: 0x5000
61+
Symbols:
62+
- Name: _start
63+
Section: .text
64+
Value: 0x4000
65+
- Name: symbol
66+
Section: .data
67+
Value: 0x500c
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# RUN: llvm-mc --triple=aarch64-elf --filetype=obj < %s | \
2+
# RUN: llvm-objdump -d -r --symbolize-operands --no-show-raw-insn --no-leading-addr - | \
3+
# RUN: FileCheck %s --match-full-lines
4+
5+
# CHECK: <fn1>:
6+
# CHECK-NEXT: b <L0>
7+
# CHECK-NEXT: tbz x0, #0x2c, <L2>
8+
# CHECK-NEXT: <L0>:
9+
# CHECK-NEXT: b.eq <L1>
10+
# CHECK-NEXT: <L1>:
11+
# CHECK-NEXT: cbz x1, <L0>
12+
# CHECK-NEXT: <L2>:
13+
# CHECK-NEXT: nop
14+
# CHECK-NEXT: <L3>:
15+
# CHECK-NEXT: bl <L3>
16+
# CHECK-NEXT: R_AARCH64_CALL26 fn2
17+
# CHECK-NEXT: bl <fn2>
18+
# CHECK-NEXT: adr x0, <L2>
19+
# CHECK-NEXT: <L4>:
20+
# CHECK-NEXT: adr x1, <L4>
21+
# CHECK-NEXT: R_AARCH64_ADR_PREL_LO21 fn2
22+
# CHECK-NEXT: adr x2, <fn2>
23+
# CHECK-NEXT: ldr w0, <L2>
24+
# CHECK-NEXT: <L5>:
25+
# CHECK-NEXT: ldr w0, <L5>
26+
# CHECK-NEXT: R_AARCH64_LD_PREL_LO19 fn2
27+
# CHECK-NEXT: ret
28+
# CHECK-NEXT: nop
29+
# CHECK-NEXT: nop
30+
# CHECK-NEXT: nop
31+
# CHECK-EMPTY:
32+
# CHECK-NEXT: <fn2>:
33+
# CHECK-NEXT: bl <L0>
34+
# CHECK-NEXT: adrp x3, 0x0 <fn1>
35+
# CHECK-NEXT: R_AARCH64_ADR_PREL_PG_HI21 fn2
36+
# CHECK-NEXT: add x3, x3, #0x0
37+
# CHECK-NEXT: R_AARCH64_ADD_ABS_LO12_NC fn2
38+
# CHECK-NEXT: adrp x3, 0x0 <fn1>
39+
# CHECK-NEXT: R_AARCH64_ADR_PREL_PG_HI21 fn2
40+
# CHECK-NEXT: ldr x0, [x3]
41+
# CHECK-NEXT: R_AARCH64_LDST64_ABS_LO12_NC fn2
42+
# CHECK-NEXT: ret
43+
# CHECK-NEXT: nop
44+
# CHECK-NEXT: nop
45+
# CHECK-NEXT: <L0>:
46+
# CHECK-NEXT: ret
47+
48+
.p2align 4
49+
.global fn1
50+
fn1:
51+
b 0f
52+
tbz x0, 44, 2f
53+
0: b.eq 1f
54+
1: cbz x1, 0b
55+
2: nop
56+
bl fn2
57+
bl .Lfn2
58+
adr x0, 2b
59+
adr x1, fn2
60+
adr x2, .Lfn2
61+
ldr w0, 2b
62+
ldr w0, fn2
63+
ret
64+
65+
.p2align 4
66+
.global fn2
67+
fn2:
68+
.Lfn2: ## Local label for non-interposable call.
69+
bl .Lfn3
70+
## In future, we might identify the pairs and symbolize the operands properly.
71+
adrp x3, fn2
72+
add x3, x3, :lo12:fn2
73+
adrp x3, fn2
74+
ldr x0, [x3, :lo12:fn2]
75+
ret
76+
77+
.p2align 4
78+
.Lfn3: ## Private function
79+
ret

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1495,8 +1495,9 @@ collectLocalBranchTargets(ArrayRef<uint8_t> Bytes, MCInstrAnalysis *MIA,
14951495
// Supported by certain targets.
14961496
const bool isPPC = STI->getTargetTriple().isPPC();
14971497
const bool isX86 = STI->getTargetTriple().isX86();
1498+
const bool isAArch64 = STI->getTargetTriple().isAArch64();
14981499
const bool isBPF = STI->getTargetTriple().isBPF();
1499-
if (!isPPC && !isX86 && !isBPF)
1500+
if (!isPPC && !isX86 && !isAArch64 && !isBPF)
15001501
return;
15011502

15021503
if (MIA)

0 commit comments

Comments
 (0)