0001-elf2flt-add-riscv-64-bits-support.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. From ef8fe0487b236c70ee33856ca1a97b1df3043217 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. Signed-off-by: Waldemar Brodkorb <wbx@openadk.org>
  20. ---
  21. elf2flt.c | 26 ++++++++++++++++++++++++++
  22. elf2flt.ld.in | 1 +
  23. ld-elf2flt.c | 8 ++++++++
  24. 3 files changed, 35 insertions(+)
  25. diff --git a/elf2flt.c b/elf2flt.c
  26. index 3c9f4d0..dfdced4 100644
  27. --- a/elf2flt.c
  28. +++ b/elf2flt.c
  29. @@ -81,6 +81,8 @@ const char *elf2flt_progname;
  30. #include <elf/v850.h>
  31. #elif defined(TARGET_xtensa)
  32. #include <elf/xtensa.h>
  33. +#elif defined(TARGET_riscv64)
  34. +#include <elf/riscv.h>
  35. #endif
  36. #if defined(__MINGW32__)
  37. @@ -123,6 +125,8 @@ const char *elf2flt_progname;
  38. #define ARCH "nios2"
  39. #elif defined(TARGET_xtensa)
  40. #define ARCH "xtensa"
  41. +#elif defined(TARGET_riscv64)
  42. +#define ARCH "riscv64"
  43. #else
  44. #error "Don't know how to support your CPU architecture??"
  45. #endif
  46. @@ -818,6 +822,28 @@ output_relocs (
  47. goto good_32bit_resolved_reloc;
  48. default:
  49. goto bad_resolved_reloc;
  50. +#elif defined(TARGET_riscv64)
  51. + case R_RISCV_NONE:
  52. + case R_RISCV_32_PCREL:
  53. + case R_RISCV_ADD8:
  54. + case R_RISCV_ADD16:
  55. + case R_RISCV_ADD32:
  56. + case R_RISCV_ADD64:
  57. + case R_RISCV_SUB6:
  58. + case R_RISCV_SUB8:
  59. + case R_RISCV_SUB16:
  60. + case R_RISCV_SUB32:
  61. + case R_RISCV_SUB64:
  62. + case R_RISCV_SET6:
  63. + case R_RISCV_SET8:
  64. + case R_RISCV_SET16:
  65. + case R_RISCV_SET32:
  66. + continue;
  67. + case R_RISCV_32:
  68. + case R_RISCV_64:
  69. + goto good_32bit_resolved_reloc;
  70. + default:
  71. + goto bad_resolved_reloc;
  72. #else
  73. default:
  74. /* The default is to assume that the
  75. diff --git a/elf2flt.ld.in b/elf2flt.ld.in
  76. index d86c0ba..5c5b2ff 100644
  77. --- a/elf2flt.ld.in
  78. +++ b/elf2flt.ld.in
  79. @@ -106,6 +106,7 @@ W_RODAT: *(.gnu.linkonce.r*)
  80. . = ALIGN(0x20) ;
  81. LONG(-1)
  82. . = ALIGN(0x20) ;
  83. +RISCV_GP: __global_pointer$ = . + 0x800 ;
  84. R_RODAT: *(.rodata)
  85. R_RODAT: *(.rodata1)
  86. R_RODAT: *(.rodata.*)
  87. diff --git a/ld-elf2flt.c b/ld-elf2flt.c
  88. index 7cb02d5..75ee1bb 100644
  89. --- a/ld-elf2flt.c
  90. +++ b/ld-elf2flt.c
  91. @@ -324,6 +324,14 @@ static int do_final_link(void)
  92. append_option(&other_options, concat(got_offset, "=", buf, NULL));
  93. }
  94. + /* riscv adds a global pointer symbol to the linker file with the
  95. + "RISCV_GP:" prefix. Remove the prefix for riscv64 architecture and
  96. + the entire line for other architectures. */
  97. + if (streq(TARGET_CPU, "riscv64"))
  98. + append_sed(&sed, "^RISCV_GP:", "");
  99. + else
  100. + append_sed(&sed, "^RISCV_GP:", NULL);
  101. +
  102. /* Locate the default linker script, if we don't have one provided. */
  103. if (!linker_script)
  104. linker_script = concat(ldscriptpath, "/elf2flt.ld", NULL);
  105. --
  106. 2.39.2