Skip to content

[llvm-objdump] Support --symbolize-operand on AArch64 #145009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

aengelke
Copy link
Contributor

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.

matthias-springer and others added 2 commits June 20, 2025 10:18
Created using spr 1.3.5-bogner

[skip ci]
Created using spr 1.3.5-bogner
@llvmbot
Copy link
Member

llvmbot commented Jun 20, 2025

@llvm/pr-subscribers-backend-aarch64

@llvm/pr-subscribers-llvm-binary-utilities

Author: Alexis Engelke (aengelke)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/145009.diff

3 Files Affected:

  • (modified) llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.cpp (+10)
  • (added) llvm/test/tools/llvm-objdump/AArch64/elf-disassemble-symbololize-operands.yaml (+42)
  • (modified) llvm/tools/llvm-objdump/llvm-objdump.cpp (+2-1)
diff --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.cpp b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.cpp
index bbe83821eca8e..fa7610db82bfb 100644
--- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.cpp
+++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.cpp
@@ -1784,6 +1784,10 @@ void AArch64InstPrinter::printAlignedLabel(const MCInst *MI, uint64_t Address,
                                            unsigned OpNum,
                                            const MCSubtargetInfo &STI,
                                            raw_ostream &O) {
+  // Do not print the numberic target address when symbolizing.
+  if (SymbolizeOperands)
+    return;
+
   const MCOperand &Op = MI->getOperand(OpNum);
 
   // 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,
                                            unsigned OpNum,
                                            const MCSubtargetInfo &STI,
                                            raw_ostream &O) {
+  // Do not print the numberic target address when symbolizing.
+  // However, do print for ADRP, as this is typically used together with an ADD
+  // or an immediate-offset ldr/str and the label is likely at the wrong point.
+  if (SymbolizeOperands && MI->getOpcode() != AArch64::ADRP)
+    return;
+
   const MCOperand &Op = MI->getOperand(OpNum);
 
   // If the label has already been resolved to an immediate offset (say, when
diff --git a/llvm/test/tools/llvm-objdump/AArch64/elf-disassemble-symbololize-operands.yaml b/llvm/test/tools/llvm-objdump/AArch64/elf-disassemble-symbololize-operands.yaml
new file mode 100644
index 0000000000000..3f3c6f33e620f
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/AArch64/elf-disassemble-symbololize-operands.yaml
@@ -0,0 +1,42 @@
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-objdump %t -d --symbolize-operands --no-show-raw-insn --no-leading-addr | \
+# RUN:   FileCheck %s --match-full-lines
+# RUN: llvm-objdump %t -d --symbolize-operands --no-show-raw-insn --no-leading-addr --adjust-vma=0x2000 | \
+# RUN:   FileCheck %s --match-full-lines
+
+## Expect to find the branch labels and global variable name.
+# CHECK:      <_start>:
+# CHECK-NEXT:   ldr x0, <symbol>
+# CHECK-NEXT: <L0>:
+# CHECK-NEXT:   adrp x1, 0x{{[68]}}000 <symbol+0xff4>
+# CHECK-NEXT:   adr x2, <symbol>
+# CHECK-NEXT:   cmp x1, x2
+# CHECK-NEXT:   b.eq <L1>
+# CHECK-NEXT:   b <L0>
+# CHECK-NEXT: <L1>:
+# CHECK-NEXT:   cbz x2, <L0>
+# CHECK-NEXT:   ret
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_AARCH64
+Sections:
+  - Name:    .text
+    Type:    SHT_PROGBITS
+    Address: 0x4000
+    Flags:   [SHF_ALLOC, SHF_EXECINSTR]
+    Content: '60800058010000d0228000103f0002eb40000054fcffff1762ffffb4c0035fd6'
+  - Name:    .data
+    Type:    SHT_PROGBITS
+    Flags:   [SHF_ALLOC, SHF_WRITE]
+    Address: 0x5000
+Symbols:
+  - Name:    _start
+    Section: .text
+    Value:   0x4000
+  - Name:    symbol
+    Section: .data
+    Value:   0x500c
diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp
index 5ecb33375943f..c5967cd090eec 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.cpp
+++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp
@@ -1495,8 +1495,9 @@ collectLocalBranchTargets(ArrayRef<uint8_t> Bytes, MCInstrAnalysis *MIA,
   // Supported by certain targets.
   const bool isPPC = STI->getTargetTriple().isPPC();
   const bool isX86 = STI->getTargetTriple().isX86();
+  const bool isAArch64 = STI->getTargetTriple().isAArch64();
   const bool isBPF = STI->getTargetTriple().isBPF();
-  if (!isPPC && !isX86 && !isBPF)
+  if (!isPPC && !isX86 && !isAArch64 && !isBPF)
     return;
 
   if (MIA)

Copy link
Member

@MaskRay MaskRay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but I'd check whether @jh7370 has opinions on the test.
(We need an executable for testing. llvm/test cannot use lld, so we have to resort to hexadecimal bytes...)

aengelke added 4 commits June 23, 2025 08:50
Created using spr 1.3.5-bogner
Created using spr 1.3.5-bogner
Created using spr 1.3.5-bogner
Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically looks good. Just some typos and other nits. Also, the documentation for the option says it works only for PPC and X86. Please could you update this and double-check whether the command-line help has the same note.

Created using spr 1.3.5-bogner
@aengelke
Copy link
Contributor Author

Done. The command line help doesn't give any indication on supported architectures.

Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks. Probably want to wait for @MaskRay for final sign-off, but if he's too busy, land it in a couple of days anyway.

# CHECK-NEXT: cbz x2, <L0>
# CHECK-NEXT: ret

## Machine code generated with:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adopting llvm/utils/update_test_body.py to make this easier to re-create https://llvm.org/docs/TestingGuide.html#elaborated-tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obj2yaml produces loads of unnecessary content (program headers, dynamic sections (dynsym/dynstr/hash/dynamic), its output is twice as long as this test currently is. I can do that, but I don't think it's worth the effort.

Created using spr 1.3.5-bogner
@aengelke aengelke changed the base branch from users/aengelke/spr/main.llvm-objdump-support-symbolize-operand-on-aarch64 to main June 25, 2025 15:09
@aengelke aengelke deleted the users/aengelke/spr/llvm-objdump-support-symbolize-operand-on-aarch64 branch June 25, 2025 15:09
@aengelke
Copy link
Contributor Author

sigh sorry for the noise, rebase for merging gone wrong :(

llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Jun 25, 2025
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: llvm/llvm-project#145009
@ldionne
Copy link
Member

ldionne commented Jun 25, 2025

(weird, I only unselected llvm/reviewers-libcxx)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants