[PATCH v3 1/2] RISC-V: Add framework for vendor-specific relocations

Kito Cheng kito.cheng@gmail.com
Fri Nov 20 08:46:06 GMT 2026


Hi Ethan:

Thanks for the patch.  The framework looks good to me overall, but I have
a few suggestions.  I would like to keep the vendor logic apart from the
common code as much as possible.

With the current design, perform_relocation and riscv_elf_relocate_section
both dispatch on vendor_id with a switch, and each vendor must list all of
its relocations in these two functions (for example, the NDS cases in
patch 2).  I would prefer to move this out, so that each vendor keeps its
own functions.

For example:

```
struct riscv_vendor_reloc_ops
{
  bool (*check_reloc) (bfd *abfd, struct bfd_link_info *info,
                       const reloc_howto_type *howto,
                       struct elf_link_hash_entry *h);

  bfd_reloc_status_type (*relocate) (struct bfd_link_info *info,
                                     const reloc_howto_type *howto,
                                     const char *name, bfd_vma *relocation,
                                     const char **msg);

  bfd_reloc_status_type (*encode) (const reloc_howto_type *howto,
                                   bfd_vma *value);
};
```

Each vendor defines its own riscv_<name>_reloc_ops, and the ops table is
built from RISCV_VENDORS.  The common code calls the hooks like this:

- riscv_elf_check_relocs calls check_reloc, so a vendor can check its
  relocations before the link, for example to reject a relocation that is
  not supported with -shared.
- perform_relocation calls encode.
- riscv_elf_relocate_section calls relocate.

Then the Andes logic can live in riscv_nds_check_reloc, riscv_nds_encode
and riscv_nds_relocate, and adding a new vendor does not need any change
to these three common functions.  This also removes the need for the
finish_relocation label.

I think this will make the code easier to extend and maintain :)


More information about the Binutils mailing list