[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:59 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

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

Author: None (eddyz87)

<details>
<summary>Changes</summary>

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).

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


2 Files Affected:

- (added) llvm/test/tools/llvm-objdump/BPF/disassemble-symbolize-operands.s (+24) 
- (modified) llvm/tools/llvm-objdump/llvm-objdump.cpp (+3-1) 


``````````diff
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)

``````````

</details>


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


More information about the llvm-commits mailing list