[llvm] [llvm-objdump][BPF] --symbolize-operands: infer local labels for BPF (PR #100550)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 28 01:21:59 PDT 2024


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

>From 4ba4a00778caea13fbf02b0555e9be2abbe708a6 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 1/2] [llvm-objdump][BPF] infer local label names in BPF
 disassembly

Enable local labels computation for BPF disassembly when
--symbolize-operands 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-operands 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)

>From f44ec2fbd186b5c99baab8668aa92580f9704763 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 2/2] fixup: stylistic changes requested by @MaskRay and
 @jh7370

- Use temporary file for llvm-mc results in the test;
- Moved 'when' in the test comment one line up to correctly fill 80
  chars per line;
- Corrected a comment in the collectLocalBranchTargets
---
 .../llvm-objdump/BPF/disassemble-symbolize-operands.s     | 8 ++++----
 llvm/tools/llvm-objdump/llvm-objdump.cpp                  | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/llvm/test/tools/llvm-objdump/BPF/disassemble-symbolize-operands.s b/llvm/test/tools/llvm-objdump/BPF/disassemble-symbolize-operands.s
index fd4c3d5b2d26e..a52ed56d680e3 100644
--- a/llvm/test/tools/llvm-objdump/BPF/disassemble-symbolize-operands.s
+++ b/llvm/test/tools/llvm-objdump/BPF/disassemble-symbolize-operands.s
@@ -1,10 +1,10 @@
 # REQUIRES: bpf-registered-target
 
-## Verify generation of 'Lxx' labels for local jump targets,
-## when --symbolize-operands option is specified.
+## 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: llvm-mc -triple=bpfel %s -filetype=obj -o %t
+# RUN: llvm-objdump -d --symbolize-operands --no-show-raw-insn --no-leading-addr %t | \
 # RUN:   FileCheck %s
         .text
 main:
diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp
index bd8a6b0c91c63..768a976cd9202 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.cpp
+++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp
@@ -1484,7 +1484,7 @@ collectLocalBranchTargets(ArrayRef<uint8_t> Bytes, MCInstrAnalysis *MIA,
                           const MCSubtargetInfo *STI, uint64_t SectionAddr,
                           uint64_t Start, uint64_t End,
                           std::unordered_map<uint64_t, std::string> &Labels) {
-  // So far only supports PowerPC and X86.
+  // Supported by certain targets.
   const bool isPPC = STI->getTargetTriple().isPPC();
   const bool isX86 = STI->getTargetTriple().isX86();
   const bool isBPF = STI->getTargetTriple().isBPF();



More information about the llvm-commits mailing list