[RFC PATCH v4 1/5] RISC-V: Add Zcmt table-jump relocation definitions

Kito Cheng kito.cheng@gmail.com
Fri Nov 20 11:25:20 GMT 2026


Hi Jiawei:

Thanks for keeping this series up to date.  I like the new design: Zcmt
runs after shortening reaches a fixed point, and it is split into three
passes: record, plan and rewrite.

I reviewed and tested it against the bugs we fixed in our internal
version, and none of them show up.  I found only one corner case, and I
would like you to fix it.  It is the same kind of bug as one we hit in
our internal version: the relaxation range checks do not take into
account how the JVT changes the layout (its size and alignment).  In
your version, it just shows up in a different form.

Also, the patches on the mailing list are spread over several versions
now.  Could you send the whole Zcmt series again as v5?


The problem: the JVT section is added to the layout after shortening,
so jumps that cross it can overflow.

riscv_elf_create_jvt_section creates a placeholder of size 0.  Only
zcmt-plan sets its real size and its 64-byte alignment.  By then,
shortening has already made its decisions based on the old layout
(c.j/c.jal: +-2 KiB, jal: +-1 MiB).  The default linker script puts
.riscv.jvt after .text, so everything after .text moves up by the table
size plus up to 63 bytes.  But the bytes that rewrite deletes are not
always between the caller and the callee.

This example links fine without --relax-tbljal, but fails with it:

        .text
        .globl _start
_start:
        .irp idx,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
        .rept 4
        tail far\idx
        .endr
        .endr
        .space 0x200000
        .irp idx,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
        .globl far\idx
far\idx:        ret
        .endr
        .globl near_site
near_site:
        tail fini_fn
        .space 1900
        .section .fini,"ax",@progbits
        .globl fini_fn
fini_fn:
        ret

$ as -march=rv64ic_zcmt -o cross.o cross.s
$ ld -o a.out cross.o                 # OK, near_site is c.j
$ ld --relax-tbljal -o a.out cross.o
(.text+0x200140): relocation truncated to fit: R_RISCV_RVC_JUMP
against symbol `fini_fn' defined in .fini section

One way to fix it: count the largest possible JVT (size plus alignment)
in the layout before shortening, and after plan only let it shrink,
never grow.  This matches what the other relaxations already assume:
when a section shrinks, a distance can grow by at most alignment - 1
bytes because of the alignment of a later section, and shortening
already keeps a reserve for that based on the max alignment.  Another
way is to let shortening keep an extra reserve for targets after the
JVT.  A better idea from you is welcome too.  Please also add this
example as a testcase.

One more thing: when you move the relax_zcmt field to 3/5, as we
discussed, please also add back the !htab->params->relax_zcmt check in
riscv_use_jvt.  v4 2/5 removed this check.  If 3/5 does not add it back,
Zcmt relaxation is always on whenever the arch has zcmt, and
--relax-tbljal does nothing.  I also suggest putting 3/5 before 2/5
(3/5 only adds the option and the field, and 2/5 starts to use them).
Then no commit in the middle turns Zcmt on by default, and bisect works.

Some small issues:

- In _bfd_riscv_jvt_record, sec is marked ATTRIBUTE_UNUSED, but it is
  used.
- The comment above riscv_jvt_key_t says "This v2 implementation ...".
  The code does not need a version number.
- In the 3/5 commit message, I do not understand "Retain the upstream
  BFD pass registration entry point."  Could you explain it, or remove
  it?

Thanks!

Kito Cheng <kito.cheng@gmail.com> 於 2026年10月1日週四 下午5:23寫道:

>
> I pushed this and
> https://patchwork.sourceware.org/project/binutils/patch/20260924155402.420585-5-jiawei@iscas.ac.cn/
> ahead to make some progress here :)
>
> Jiawei <jiawei@iscas.ac.cn> 於 2026年10月1日週四 下午5:04寫道:
> >
> > Add the linker-internal table-jump marker and instruction encoding
> > definitions. The marker has no public ELF relocation or howto entry.
> >
> > bfd/ChangeLog:
> >
> >         * elfxx-riscv.c: Add TABLE_JUMP howto.
> >         * elfxx-riscv.h (struct riscv_elf_params): Add relax_zcmt field.
> >
> > include/ChangeLog:
> >
> >         * elf/riscv.h (R_RISCV_TABLE_JUMP): New reloc number.
> >         (RISCV_TABLE_JUMP_BASE_SYMBOL): New symbol name.
> >         (TABLE_JUMP_SEC_NAME): New section name.
> >         * opcode/riscv.h (ZCMT_TOTAL_ENTRIES): New Zcmt constants.
> >         (ZCMT_JT_BEGIN): Ditto.
> >         (ZCMT_JT_END): Ditto.
> >         (ZCMT_JT_NUM_ENTRIES): Ditto.
> >         (ZCMT_JALT_BEGIN): Ditto.
> >         (ZCMT_JALT_END): Ditto.
> >         (ZCMT_JALT_NUM_ENTRIES): Ditto.
> > ---
> >  include/elf/riscv.h    | 5 +++++
> >  include/opcode/riscv.h | 9 +++++++++
> >  2 files changed, 14 insertions(+)
> >
> > diff --git a/include/elf/riscv.h b/include/elf/riscv.h
> > index f6ca3d4acd8..02a0184b7ee 100644
> > --- a/include/elf/riscv.h
> > +++ b/include/elf/riscv.h
> > @@ -105,6 +105,7 @@ END_RELOC_NUMBERS (R_RISCV_max)
> >  #define R_RISCV_GPREL_S           (R_RISCV_max + 4)
> >  #define R_RISCV_TPREL_I           (R_RISCV_max + 5)
> >  #define R_RISCV_TPREL_S           (R_RISCV_max + 6)
> > +#define R_RISCV_TABLE_JUMP        (R_RISCV_max + 7)
> >
> >  /* Processor specific flags for the ELF header e_flags field.  */
> >
> > @@ -132,6 +133,10 @@ END_RELOC_NUMBERS (R_RISCV_max)
> >  /* The name of the global pointer symbol.  */
> >  #define RISCV_GP_SYMBOL "__global_pointer$"
> >
> > +#define RISCV_TABLE_JUMP_BASE_SYMBOL "__jvt_base$"
> > +
> > +#define TABLE_JUMP_SEC_NAME ".riscv.jvt"
> > +
> >  /* Processor specific dynamic array tags.  */
> >  #define DT_RISCV_VARIANT_CC (DT_LOPROC + 1)
> >
> > diff --git a/include/opcode/riscv.h b/include/opcode/riscv.h
> > index 95b1f44567a..a4a89ddda8d 100644
> > --- a/include/opcode/riscv.h
> > +++ b/include/opcode/riscv.h
> > @@ -746,6 +746,15 @@ struct riscv_opcode
> >     disassembler, and requires special treatment by the assembler.  */
> >  #define INSN_MACRO             0xffffffff
> >
> > +/* Define Zcmt jump table constants.  */
> > +#define ZCMT_TOTAL_ENTRIES 256
> > +#define ZCMT_JT_BEGIN 0
> > +#define ZCMT_JT_END 31
> > +#define ZCMT_JT_NUM_ENTRIES (ZCMT_JT_END - ZCMT_JT_BEGIN + 1)
> > +#define ZCMT_JALT_BEGIN (ZCMT_JT_END + 1)
> > +#define ZCMT_JALT_END (ZCMT_TOTAL_ENTRIES - 1)
> > +#define ZCMT_JALT_NUM_ENTRIES (ZCMT_JALT_END - ZCMT_JALT_BEGIN + 1)
> > +
> >  /* This is a list of macro expanded instructions.  */
> >  enum
> >  {
> > --
> > 2.43.0
> >


More information about the Binutils mailing list