[llvm] [BPF] Support Jump Table (PR #149715)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 23 12:52:18 PDT 2025


eddyz87 wrote:

> > > I think relocations can be made to go away if table content would be implemented as instruction offsets, computed as difference between label and section start. The piece of code I posted [here](https://github.com/llvm/llvm-project/pull/149715#issuecomment-3097615612) generated the following assembly:
> > > ```
> > >         .section        .jumptables,"", at progbits
> > > .L0_0_set_7 = ((LBB0_7-.LBPF.JX.0.0)>>3)-1
> > > .L0_0_set_2 = ((LBB0_2-.LBPF.JX.0.0)>>3)-1
> > > .L0_0_set_8 = ((LBB0_8-.LBPF.JX.0.0)>>3)-1
> > > .L0_0_set_9 = ((LBB0_9-.LBPF.JX.0.0)>>3)-1
> > > .L0_0_set_10 = ((LBB0_10-.LBPF.JX.0.0)>>3)-1
> > > BPF.JT.0.0:
> > >         .long   .L0_0_set_7
> > >         .long   .L0_0_set_2
> > >         .long   .L0_0_set_2
> > >         .long   .L0_0_set_2
> > >         ...
> > > ```
> > >   
> > > And there were no relocations for ".jumptables":
> > > ```
> > > Section Headers:
> > >   [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
> > >   [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
> > >   [ 1] .strtab           STRTAB          0000000000000000 000320 000065 00      0   0  1
> > >   [ 2] .text             PROGBITS        0000000000000000 000040 0000f0 00  AX  0   0  8
> > >   [ 3] .rel.text         REL             0000000000000000 0002f0 000030 10   I  6   2  8
> > >   [ 4] .jumptables       PROGBITS        0000000000000000 000130 000130 00      0   0  1
> > >   [ 5] .llvm_addrsig     LLVM_ADDRSIG    0000000000000000 000320 000000 00   E  6   0  1
> > >   [ 6] .symtab           SYMTAB          0000000000000000 000260 000090 18      1   2  8
> > > ```
> > > 
> > > Do we need these relocations to simplify linking by libbpf?
> > 
> > As we discussed offline, I think the table can be simplified (for libbpf) if we do this '(label - begin_label) >> 3'. Will investigate.
> 
> I investigated and found this is not really possible.

`MCTargetStreamer::switchSection` can be specialized to ensure that `MCSection::getBeginSymbol()` is present, e.g. as here (one commit on top of this branch):
https://github.com/eddyz87/llvm-project/tree/jumptable.offset-in-insn-count

The output looks like:

```
        $ clang -mcpu=v4 --target=bpf -O2 -S jump-table-test.c -o -
                .file   "jump-table-test.c"
                .text
                ...
                .section        .jumptables,"", at progbits
        .L0_0_set_11 = (LBB0_11-.text)>>3
        .L0_0_set_16 = (LBB0_16-.text)>>3
                ...
        BPF.JT.0.0:
                .quad   .L0_0_set_11
                .quad   .L0_0_set_16
                .quad   .L0_0_set_16
                ...
                .size   BPF.JT.0.0, 352

```

And there are no relocations in the `.jumptables` section.
But the question remains, @aspsk , do we need relocations in the `.jumptables` section for libbpf linker?

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


More information about the llvm-commits mailing list