0003-elf2flt-add-riscv-64-bits-support.patch 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. From 3f1f323feb5cf25d8c80861991d0360784f4d2e6 Mon Sep 17 00:00:00 2001
  2. From: Damien Le Moal <damien.lemoal@wdc.com>
  3. Date: Wed, 9 Sep 2020 17:31:33 +0900
  4. Subject: [PATCH] elf2flt: add riscv 64-bits support
  5. Add support for riscv 64bits ISA by defining the relocation types
  6. R_RISCV_32_PCREL, R_RISCV_ADD32, R_RISCV_SUB32, R_RISCV_32 and
  7. R_RISCV_64. riscv64 support also needs the __global_pointer$ symbol to
  8. be defined right after the relocation tables in the data section. To
  9. define this symbol, the "RISCV_GP" line prefix is added. The "RISCV_GP"
  10. string is removed if the target CPU type is riscv64 and the definition
  11. line is dropped for other CPU types.
  12. With these changes, buildroot and busybox build and run on riscv NOMMU
  13. systems with Linux kernel including patch 6045ab5fea4c
  14. ("binfmt_flat: do not stop relocating GOT entries prematurely on riscv")
  15. fixing the binfmt_flat loader. Tested on QEMU and Canaan Kendryte K210
  16. boards.
  17. This patch is based on earlier work by Christoph Hellwig <hch@lst.de>.
  18. Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
  19. ---
  20. elf2flt.c | 16 ++++++++++++++++
  21. elf2flt.ld.in | 1 +
  22. ld-elf2flt.c | 8 ++++++++
  23. 3 files changed, 25 insertions(+)
  24. diff --git a/elf2flt.c b/elf2flt.c
  25. index da25e93..a03ea3a 100644
  26. --- a/elf2flt.c
  27. +++ b/elf2flt.c
  28. @@ -81,6 +81,8 @@ const char *elf2flt_progname;
  29. #include <elf/v850.h>
  30. #elif defined(TARGET_xtensa)
  31. #include <elf/xtensa.h>
  32. +#elif defined(TARGET_riscv64)
  33. +#include <elf/riscv.h>
  34. #endif
  35. #if defined(__MINGW32__)
  36. @@ -123,6 +125,8 @@ const char *elf2flt_progname;
  37. #define ARCH "nios2"
  38. #elif defined(TARGET_xtensa)
  39. #define ARCH "xtensa"
  40. +#elif defined(TARGET_riscv64)
  41. +#define ARCH "riscv64"
  42. #else
  43. #error "Don't know how to support your CPU architecture??"
  44. #endif
  45. @@ -812,6 +816,18 @@ output_relocs (
  46. goto good_32bit_resolved_reloc;
  47. default:
  48. goto bad_resolved_reloc;
  49. +#elif defined(TARGET_riscv64)
  50. + case R_RISCV_32_PCREL:
  51. + case R_RISCV_ADD32:
  52. + case R_RISCV_ADD64:
  53. + case R_RISCV_SUB32:
  54. + case R_RISCV_SUB64:
  55. + continue;
  56. + case R_RISCV_32:
  57. + case R_RISCV_64:
  58. + goto good_32bit_resolved_reloc;
  59. + default:
  60. + goto bad_resolved_reloc;
  61. #else
  62. default:
  63. /* The default is to assume that the
  64. diff --git a/elf2flt.ld.in b/elf2flt.ld.in
  65. index e5aea14..950849e 100644
  66. --- a/elf2flt.ld.in
  67. +++ b/elf2flt.ld.in
  68. @@ -106,6 +106,7 @@ W_RODAT: *(.gnu.linkonce.r*)
  69. . = ALIGN(0x20) ;
  70. LONG(-1)
  71. . = ALIGN(0x20) ;
  72. +RISCV_GP: __global_pointer$ = . + 0x800 ;
  73. R_RODAT: *(.rodata)
  74. R_RODAT: *(.rodata1)
  75. R_RODAT: *(.rodata.*)
  76. diff --git a/ld-elf2flt.c b/ld-elf2flt.c
  77. index 7cb02d5..75ee1bb 100644
  78. --- a/ld-elf2flt.c
  79. +++ b/ld-elf2flt.c
  80. @@ -324,6 +324,14 @@ static int do_final_link(void)
  81. append_option(&other_options, concat(got_offset, "=", buf, NULL));
  82. }
  83. + /* riscv adds a global pointer symbol to the linker file with the
  84. + "RISCV_GP:" prefix. Remove the prefix for riscv64 architecture and
  85. + the entire line for other architectures. */
  86. + if (streq(TARGET_CPU, "riscv64"))
  87. + append_sed(&sed, "^RISCV_GP:", "");
  88. + else
  89. + append_sed(&sed, "^RISCV_GP:", NULL);
  90. +
  91. /* Locate the default linker script, if we don't have one provided. */
  92. if (!linker_script)
  93. linker_script = concat(ldscriptpath, "/elf2flt.ld", NULL);
  94. --
  95. 2.36.1