Skip to content

Commit 0395868

Browse files
yonghong-songYonghong Song
and
Yonghong Song
authored
[BPF] Make llvm-objdump disasm default cpu v4 (#102166)
Currently, with the following example, $ cat t.c void foo(int a, _Atomic int *b) { *b &= a; } $ clang --target=bpf -O2 -c -mcpu=v3 t.c $ llvm-objdump -d t.o t.o: file format elf64-bpf Disassembly of section .text: 0000000000000000 <foo>: 0: c3 12 00 00 51 00 00 00 <unknown> 1: 95 00 00 00 00 00 00 00 exit Basically, the default cpu for llvm-objdump is v1 and it won't be able to decode insn properly. If we add --mcpu=v3 to llvm-objdump command line, we will have $ llvm-objdump -d --mcpu=v3 t.o t.o: file format elf64-bpf Disassembly of section .text: 0000000000000000 <foo>: 0: c3 12 00 00 51 00 00 00 w1 = atomic_fetch_and((u32 *)(r2 + 0x0), w1) 1: 95 00 00 00 00 00 00 00 exit The atomic_fetch_and insn can be decoded properly. Using latest cpu version --mcpu=v4 can also decode properly like the above --mcpu=v3. To avoid the above '<unknown>' decoding with common 'llvm-objdump -d t.o', this patch marked the default cpu for llvm-objdump with the current highest cpu number v4 in ELFObjectFileBase::tryGetCPUName(). The cpu number in ELFObjectFileBase::tryGetCPUName() will be adjusted in the future if cpu number is increased e.g. v5 etc. Such an approach also aligns with gcc-bpf as discussed in [1]. Six bpf unit tests are affected with this change. I changed test output for three unit tests and added --mcpu=v1 for the other three unit tests, to demonstrate the default (cpu v4) behavior and explicit --mcpu=v1 behavior. [1] https://lore.kernel.org/bpf/[email protected]/T/#m0f7e63c390bc8f5a5523e7f2f0537becd4205200 Co-authored-by: Yonghong Song <[email protected]>
1 parent 128ef9e commit 0395868

File tree

7 files changed

+16
-14
lines changed

7 files changed

+16
-14
lines changed

llvm/lib/Object/ELFObjectFile.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ std::optional<StringRef> ELFObjectFileBase::tryGetCPUName() const {
441441
case ELF::EM_PPC:
442442
case ELF::EM_PPC64:
443443
return StringRef("future");
444+
case ELF::EM_BPF:
445+
return StringRef("v4");
444446
default:
445447
return std::nullopt;
446448
}

llvm/test/CodeGen/BPF/objdump_atomics.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
; CHECK-LABEL: test_load_add_32
44
; CHECK: c3 21
5-
; CHECK: r2 = atomic_fetch_add((u32 *)(r1 + 0), r2)
5+
; CHECK: w2 = atomic_fetch_add((u32 *)(r1 + 0), w2)
66
define void @test_load_add_32(ptr %p, i32 zeroext %v) {
77
entry:
88
atomicrmw add ptr %p, i32 %v seq_cst

llvm/test/CodeGen/BPF/objdump_cond_op.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: llc -mtriple=bpfel -filetype=obj -o - %s | llvm-objdump --no-print-imm-hex -d - | FileCheck %s
1+
; RUN: llc -mtriple=bpfel -filetype=obj -o - %s | llvm-objdump --no-print-imm-hex --mcpu=v1 -d - | FileCheck %s
22

33
; Source Code:
44
; int gbl;

llvm/test/CodeGen/BPF/objdump_imm_hex.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ define i32 @test(i64, i64) local_unnamed_addr #0 {
5353
%14 = phi i32 [ %12, %10 ], [ %7, %4 ]
5454
%15 = phi i32 [ 2, %10 ], [ 1, %4 ]
5555
store i32 %14, ptr @gbl, align 4
56-
; CHECK-DEC: 63 12 00 00 00 00 00 00 *(u32 *)(r2 + 0) = r1
57-
; CHECK-HEX: 63 12 00 00 00 00 00 00 *(u32 *)(r2 + 0x0) = r1
56+
; CHECK-DEC: 63 12 00 00 00 00 00 00 *(u32 *)(r2 + 0) = w1
57+
; CHECK-HEX: 63 12 00 00 00 00 00 00 *(u32 *)(r2 + 0x0) = w1
5858
br label %16
5959

6060
; <label>:16: ; preds = %13, %8

llvm/test/CodeGen/BPF/objdump_static_var.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
; RUN: llc -mtriple=bpfel -filetype=obj -o - %s | llvm-objdump --no-print-imm-hex -d - | FileCheck --check-prefix=CHECK %s
2-
; RUN: llc -mtriple=bpfeb -filetype=obj -o - %s | llvm-objdump --no-print-imm-hex -d - | FileCheck --check-prefix=CHECK %s
1+
; RUN: llc -mtriple=bpfel -filetype=obj -o - %s | llvm-objdump --no-print-imm-hex --mcpu=v1 -d - | FileCheck --check-prefix=CHECK %s
2+
; RUN: llc -mtriple=bpfeb -filetype=obj -o - %s | llvm-objdump --no-print-imm-hex --mcpu=v1 -d - | FileCheck --check-prefix=CHECK %s
33

44
; src:
55
; static volatile long a = 2;

llvm/test/MC/BPF/insn-unit.s

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
r6 = *(u16 *)(r1 + 8) // BPF_LDX | BPF_H
3535
r7 = *(u32 *)(r2 + 16) // BPF_LDX | BPF_W
3636
r8 = *(u64 *)(r3 - 30) // BPF_LDX | BPF_DW
37-
// CHECK-64: 71 05 00 00 00 00 00 00 r5 = *(u8 *)(r0 + 0)
38-
// CHECK-64: 69 16 08 00 00 00 00 00 r6 = *(u16 *)(r1 + 8)
39-
// CHECK-64: 61 27 10 00 00 00 00 00 r7 = *(u32 *)(r2 + 16)
37+
// CHECK-64: 71 05 00 00 00 00 00 00 w5 = *(u8 *)(r0 + 0)
38+
// CHECK-64: 69 16 08 00 00 00 00 00 w6 = *(u16 *)(r1 + 8)
39+
// CHECK-64: 61 27 10 00 00 00 00 00 w7 = *(u32 *)(r2 + 16)
4040
// CHECK-32: 71 05 00 00 00 00 00 00 w5 = *(u8 *)(r0 + 0)
4141
// CHECK-32: 69 16 08 00 00 00 00 00 w6 = *(u16 *)(r1 + 8)
4242
// CHECK-32: 61 27 10 00 00 00 00 00 w7 = *(u32 *)(r2 + 16)
@@ -47,17 +47,17 @@
4747
*(u16 *)(r1 + 8) = r8 // BPF_STX | BPF_H
4848
*(u32 *)(r2 + 16) = r9 // BPF_STX | BPF_W
4949
*(u64 *)(r3 - 30) = r10 // BPF_STX | BPF_DW
50-
// CHECK-64: 73 70 00 00 00 00 00 00 *(u8 *)(r0 + 0) = r7
51-
// CHECK-64: 6b 81 08 00 00 00 00 00 *(u16 *)(r1 + 8) = r8
52-
// CHECK-64: 63 92 10 00 00 00 00 00 *(u32 *)(r2 + 16) = r9
50+
// CHECK-64: 73 70 00 00 00 00 00 00 *(u8 *)(r0 + 0) = w7
51+
// CHECK-64: 6b 81 08 00 00 00 00 00 *(u16 *)(r1 + 8) = w8
52+
// CHECK-64: 63 92 10 00 00 00 00 00 *(u32 *)(r2 + 16) = w9
5353
// CHECK-32: 73 70 00 00 00 00 00 00 *(u8 *)(r0 + 0) = w7
5454
// CHECK-32: 6b 81 08 00 00 00 00 00 *(u16 *)(r1 + 8) = w8
5555
// CHECK-32: 63 92 10 00 00 00 00 00 *(u32 *)(r2 + 16) = w9
5656
// CHECK: 7b a3 e2 ff 00 00 00 00 *(u64 *)(r3 - 30) = r10
5757

5858
lock *(u32 *)(r2 + 16) += r9 // BPF_STX | BPF_W | BPF_XADD
5959
lock *(u64 *)(r3 - 30) += r10 // BPF_STX | BPF_DW | BPF_XADD
60-
// CHECK-64: c3 92 10 00 00 00 00 00 lock *(u32 *)(r2 + 16) += r9
60+
// CHECK-64: c3 92 10 00 00 00 00 00 lock *(u32 *)(r2 + 16) += w9
6161
// CHECK-32: c3 92 10 00 00 00 00 00 lock *(u32 *)(r2 + 16) += w9
6262
// CHECK: db a3 e2 ff 00 00 00 00 lock *(u64 *)(r3 - 30) += r10
6363

llvm/test/MC/BPF/load-store-32.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# RUN: llvm-mc -triple bpfel -filetype=obj -o %t %s
22
# RUN: llvm-objdump --no-print-imm-hex --mattr=+alu32 -d -r %t | FileCheck --check-prefix=CHECK-32 %s
3-
# RUN: llvm-objdump --no-print-imm-hex -d -r %t | FileCheck %s
3+
# RUN: llvm-objdump --no-print-imm-hex --mcpu=v1 -d -r %t | FileCheck %s
44

55
// ======== BPF_LDX Class ========
66
w5 = *(u8 *)(r0 + 0) // BPF_LDX | BPF_B

0 commit comments

Comments
 (0)