[llvm] [BPF] Avoid relocation for jumptable entries (PR #166301)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 3 20:58:16 PST 2025
https://github.com/yonghong-song updated https://github.com/llvm/llvm-project/pull/166301
>From c63d9d9e558e5a2d17803772f060aa3ba2031ce0 Mon Sep 17 00:00:00 2001
From: Yonghong Song <yonghong.song at linux.dev>
Date: Mon, 3 Nov 2025 20:25:22 -0800
Subject: [PATCH] [BPF] Avoid relocation for jumptable entries
Currently, the jump table entry contains labels only.
For example, the following is one example:
BPF.JT.0.0:
.quad LBB0_1
.quad LBB0_2
.size BPF.JT.0.0, 16
Since the jump table entry contains a label, the relocation
is necessary so linker can resolve the label value. The relocation
looks like below:
Relocation section '.rel.jumptables' at offset 0x160 contains 2 entries:
Offset Info Type Symbol's Value Symbol's Name
0000000000000000 0000000200000002 R_BPF_64_ABS64 0000000000000000 .text
0000000000000008 0000000200000002 R_BPF_64_ABS64 0000000000000000 .text
You can see that the symbol value is 0 which makes .rel.jumptables
not very useful.
Instead of having the label itself in the jump table entry,
use the difference of label and the section begin symbol.
This can avoid the relocation and the eventual jumptable
entries in object file remains the same as before.
Hex dump of section '.jumptables':
0x00000000 68000000 00000000 78000000 00000000 h.......x.......
---
llvm/lib/Target/BPF/BPFAsmPrinter.cpp | 10 +++-
llvm/test/CodeGen/BPF/jump_table_blockaddr.ll | 4 +-
.../test/CodeGen/BPF/jump_table_global_var.ll | 4 +-
.../CodeGen/BPF/jump_table_switch_stmt.ll | 60 +++++++++----------
4 files changed, 42 insertions(+), 36 deletions(-)
diff --git a/llvm/lib/Target/BPF/BPFAsmPrinter.cpp b/llvm/lib/Target/BPF/BPFAsmPrinter.cpp
index b2a82040ee823..378a72ab27dd5 100644
--- a/llvm/lib/Target/BPF/BPFAsmPrinter.cpp
+++ b/llvm/lib/Target/BPF/BPFAsmPrinter.cpp
@@ -219,6 +219,10 @@ void BPFAsmPrinter::emitJumpTableInfo() {
const TargetLoweringObjectFile &TLOF = getObjFileLowering();
const Function &F = MF->getFunction();
+
+ MCSection *Sec = OutStreamer->getCurrentSectionOnly();
+ MCSymbol *SecStart = Sec->getBeginSymbol();
+
MCSection *JTS = TLOF.getSectionForJumpTable(F, TM);
assert(MJTI->getEntryKind() == MachineJumpTableInfo::EK_BlockAddress);
unsigned EntrySize = MJTI->getEntrySize(getDataLayout());
@@ -231,8 +235,10 @@ void BPFAsmPrinter::emitJumpTableInfo() {
MCSymbol *JTStart = getJTPublicSymbol(JTI);
OutStreamer->emitLabel(JTStart);
for (const MachineBasicBlock *MBB : JTBBs) {
- const MCExpr *LHS = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
- OutStreamer->emitValue(LHS, EntrySize);
+ const MCExpr *Diff = MCBinaryExpr::createSub(
+ MCSymbolRefExpr::create(MBB->getSymbol(), OutContext),
+ MCSymbolRefExpr::create(SecStart, OutContext), OutContext);
+ OutStreamer->emitValue(Diff, EntrySize);
}
const MCExpr *JTSize =
MCConstantExpr::create(JTBBs.size() * EntrySize, OutContext);
diff --git a/llvm/test/CodeGen/BPF/jump_table_blockaddr.ll b/llvm/test/CodeGen/BPF/jump_table_blockaddr.ll
index d5a1d63b644a8..b7d518639d70e 100644
--- a/llvm/test/CodeGen/BPF/jump_table_blockaddr.ll
+++ b/llvm/test/CodeGen/BPF/jump_table_blockaddr.ll
@@ -84,8 +84,8 @@ llc -march=bpf -mcpu=v4 < test.ll \
; CHECK: .cfi_endproc
; CHECK: .section .jumptables,"", at progbits
; CHECK: BPF.JT.0.0:
-; CHECK: .quad LBB0_3
+; CHECK: .quad LBB0_3-.text
; CHECK: .size BPF.JT.0.0, 8
; CHECK: BPF.JT.0.1:
-; CHECK: .quad LBB0_4
+; CHECK: .quad LBB0_4-.text
; CHECK: .size BPF.JT.0.1, 8
diff --git a/llvm/test/CodeGen/BPF/jump_table_global_var.ll b/llvm/test/CodeGen/BPF/jump_table_global_var.ll
index bbca46850843b..71c682f5530ed 100644
--- a/llvm/test/CodeGen/BPF/jump_table_global_var.ll
+++ b/llvm/test/CodeGen/BPF/jump_table_global_var.ll
@@ -78,6 +78,6 @@ llc -march=bpf -mcpu=v4 < test.ll \
; CHECK: .cfi_endproc
; CHECK: .section .jumptables,"", at progbits
; CHECK: BPF.JT.0.0:
-; CHECK: .quad LBB0_1
-; CHECK: .quad LBB0_2
+; CHECK: .quad LBB0_1-.text
+; CHECK: .quad LBB0_2-.text
; CHECK: .size BPF.JT.0.0, 16
diff --git a/llvm/test/CodeGen/BPF/jump_table_switch_stmt.ll b/llvm/test/CodeGen/BPF/jump_table_switch_stmt.ll
index 682b025d665d6..eb1e5bff11013 100644
--- a/llvm/test/CodeGen/BPF/jump_table_switch_stmt.ll
+++ b/llvm/test/CodeGen/BPF/jump_table_switch_stmt.ll
@@ -93,34 +93,34 @@ llc -march=bpf -mcpu=v4 -bpf-min-jump-table-entries=3 < test.ll \
; CHECK: .cfi_endproc
; CHECK: .section .jumptables,"", at progbits
; CHECK: BPF.JT.0.0:
-; CHECK: .quad LBB0_4
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_2
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_5
-; CHECK: .quad LBB0_3
+; CHECK: .quad LBB0_4-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_2-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_5-.text
+; CHECK: .quad LBB0_3-.text
; CHECK: .size BPF.JT.0.0, 240
More information about the llvm-commits
mailing list