[llvm] [llvm-objdump][BPF] infer local label names in BPF disassembly (PR #100550)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 25 03:15:26 PDT 2024


https://github.com/eddyz87 created https://github.com/llvm/llvm-project/pull/100550

Enable local labels computation for BPF disassembly when `--symbolize-address` option is specified.
This relies on `MCInstrAnalysis::evaluateBranch()` method, which is already defined in `BPFMCInstrAnalysis::evaluateBranch`.

After this change the assembly code below:

```
        if r1 > 42 goto +1
        r1 -= 10
        ...
```

Would be printed as:

```
        if r1 > 42 goto +1 <L0>
        r1 -= 10
      <L0>:
        ...
```

(when `--symbolize-address` option is set).

>From 40f77112791e23e4989870e046a6ab6bffe82c25 Mon Sep 17 00:00:00 2001
From: Eduard Zingerman <eddyz87 at gmail.com>
Date: Thu, 25 Jul 2024 03:04:12 -0700
Subject: [PATCH] [llvm-objdump][BPF] infer local label names in BPF
 disassembly

Enable local labels computation for BPF disassembly when
--symbolize-address option is specified.
This relies on MCInstrAnalysis::evaluateBranch() method,
which is already defined in BPFMCInstrAnalysis::evaluateBranch.

After this change the assembly code below:

        if r1 > 42 goto +1
        r1 -= 10
        ...

Would be printed as:

        if r1 > 42 goto +1 <L0>
        r1 -= 10
      <L0>:
        ...

(when --symbolize-address option is set).
---
 .../BPF/disassemble-symbolize-operands.s      | 24 +++++++++++++++++++
 llvm/tools/llvm-objdump/llvm-objdump.cpp      |  4 +++-
 2 files changed, 27 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/tools/llvm-objdump/BPF/disassemble-symbolize-operands.s

diff --git a/llvm/test/tools/llvm-objdump/BPF/disassemble-symbolize-operands.s b/llvm/test/tools/llvm-objdump/BPF/disassemble-symbolize-operands.s
new file mode 100644
index 0000000000000..fd4c3d5b2d26e
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/BPF/disassemble-symbolize-operands.s
@@ -0,0 +1,24 @@
+# REQUIRES: bpf-registered-target
+
+## Verify generation of 'Lxx' labels for local jump targets,
+## when --symbolize-operands option is specified.
+
+# RUN: llvm-mc -triple=bpfel %s -filetype=obj -o - | \
+# RUN:   llvm-objdump -d --symbolize-operands --no-show-raw-insn --no-leading-addr - | \
+# RUN:   FileCheck %s
+        .text
+main:
+        if r1 > 42 goto +2
+        r1 -= 10
+        goto -3
+        r0 = 0
+        exit
+
+# CHECK:      <main>:
+# CHECK-NEXT: <L1>:
+# CHECK-NEXT: 	if r1 > 0x2a goto +0x2 <L0>
+# CHECK-NEXT: 	r1 -= 0xa
+# CHECK-NEXT: 	goto -0x3 <main>
+# CHECK-NEXT: <L0>:
+# CHECK-NEXT: 	r0 = 0x0
+# CHECK-NEXT: 	exit
diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp
index d1240025625ca..bd8a6b0c91c63 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.cpp
+++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp
@@ -1486,7 +1486,9 @@ collectLocalBranchTargets(ArrayRef<uint8_t> Bytes, MCInstrAnalysis *MIA,
                           std::unordered_map<uint64_t, std::string> &Labels) {
   // So far only supports PowerPC and X86.
   const bool isPPC = STI->getTargetTriple().isPPC();
-  if (!isPPC && !STI->getTargetTriple().isX86())
+  const bool isX86 = STI->getTargetTriple().isX86();
+  const bool isBPF = STI->getTargetTriple().isBPF();
+  if (!isPPC && !isX86 && !isBPF)
     return;
 
   if (MIA)



More information about the llvm-commits mailing list