elf2flt icon indicating copy to clipboard operation
elf2flt copied to clipboard

elf2flt: add riscv 64-bits support

Open damien-lemoal opened this issue 4 years ago • 7 comments

Add support for riscv 64bits ISA by defining the relocation types R_RISCV_32_PCREL, R_RISCV_ADD32, R_RISCV_SUB32, R_RISCV_32 and R_RISCV_64. riscv64 support also needs the __global_pointer$ symbol to be defined right after the relocation tables in the data section. Furthermore, the .got and .got.plt sections must be reversed. These 2 requirements are handled with runtime modifications of the default linker script using the append_sed() function. (1) For the .got.plt and .got sections order swap, append_sed() is used to rename "(.got.plt)" to "(.got.tmp)" and to rename "(.got)" to "(.got.plt)". A last call finalize the name swap by replacing "(.got.tmp)" with "(.got)" (2) For the global pointer synbol, a definition line starting with "RV64_GP" is added. The "RV64_GP" string is removed if the target CPU type is riscv64. The definition line is dropped for other CPU types.

With these changes, buildroot/busybox builds and run on NOMMU systems with kernel 5.13. Tested on Canaan Kendryte K210 boards.

This patch is based on earlier work by Christoph Hellwig [email protected].

Signed-off-by: Damien Le Moal [email protected]

damien-lemoal avatar Sep 02 '21 08:09 damien-lemoal

Notes:

  • This is an RFC as I am still not entirely sure if this is acceptable. E.g., I cannot get rid of the section ordering problem. ".got" and ".got.plt" need to be reversed. I do not understand why.
  • This still does not include support for RISCV 32-bits as I have not yet done any tests with a 32-bits NOMMU kernel (unsure if it even works).

Comments are more than welcome !

damien-lemoal avatar Sep 02 '21 09:09 damien-lemoal

Thanks for the comment. I will update the patch. I am still doing a little more digging to see if I can remove this section reordering as I really do not see why it is necessary.

damien-lemoal avatar Sep 03 '21 02:09 damien-lemoal

PR updated. I addressed all comments and also fixed a bug with some relocation types handling. With this patch both buildroot and manual busybox NOMMU builds for RISC-V 64-bits work using 5.13+kernels. Reviews are most welcome !

damien-lemoal avatar Oct 26 '21 07:10 damien-lemoal

can you go through and mark conversations as "resolved" ?

and can you cover the question wrt open bug report somewhere for us to link to ?

vapier avatar Oct 26 '21 08:10 vapier

Previous comments are all marked as "resolved" now. I am not sure what you mean with "open bug report" ? This support is not yet upstream anywhere. My primary target is buildroot and the patch series I submitted that uses this elf2flt patch is still pending (I just posted v5 today with this new version of the elf2flt patch). The "bug" with the relocation types I mentioned in my previous comment today is something I found while working on the buildroot patches: sometimes, the buildroot image would not fully boot, with busybox exiting immediately on startup. The problem was that PC relative relocations handled by the loader where not skipped here ("break" statement instead of "continue" in the switch-case within the relocation loop. Fixing that fixed the buildroot issue I was seeing. With that fixed and tested, I updated this elf2flt patch and v5 of the buildroot series (adding support for NO-MMU builds for various boards using the Canaan K210 SoC).

Does this answer your question ?

damien-lemoal avatar Oct 26 '21 08:10 damien-lemoal

PR update: This update adds one patch from Niklas to fix alignment of the data section in the elf2flt.ld.in file. The 32 B alignment is required by Linux kernel binfmt_flat loader. The second patch is also updated to remove reversing of the got sections. The root cause of the problem that this was (badly) fixing was in fact in the kernel binfmt_flat loader. A patch on the kernel side solves the issue and reversing the got and got.plt sections is no longer necessary.

With these patches added to buildroot, NO-MMU riscv64 support is now stable. Tested with QEMU and on Canaan Kendryte K210 boards.

Please consider these patches for addition.

damien-lemoal avatar Apr 22 '22 07:04 damien-lemoal

PR updated to add one more patch to better handle .eh_frame section. Feed back welcome !

damien-lemoal avatar May 11 '22 09:05 damien-lemoal

RISC-V support has been merged into main in later versions of this PR.

gregungerer avatar Sep 06 '23 13:09 gregungerer