[llvm] 7272a6c - [BPF] Avoid relocation for jumptable entries (#166301)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 4 11:32:16 PST 2025
Author: yonghong-song
Date: 2025-11-04T11:32:12-08:00
New Revision: 7272a6c8882d99fe1fc73d2c69ddf976948f0e50
URL: https://github.com/llvm/llvm-project/commit/7272a6c8882d99fe1fc73d2c69ddf976948f0e50
DIFF: https://github.com/llvm/llvm-project/commit/7272a6c8882d99fe1fc73d2c69ddf976948f0e50.diff
LOG: [BPF] Avoid relocation for jumptable entries (#166301)
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.......
Added:
Modified:
llvm/lib/Target/BPF/BPFAsmPrinter.cpp
llvm/test/CodeGen/BPF/jump_table_blockaddr.ll
llvm/test/CodeGen/BPF/jump_table_global_var.ll
llvm/test/CodeGen/BPF/jump_table_switch_stmt.ll
Removed:
################################################################################
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