dl.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* dl.c - arch-dependent part of loadable module support */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2018 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/elf.h>
  21. #include <grub/misc.h>
  22. #include <grub/err.h>
  23. #include <grub/mm.h>
  24. #include <grub/i18n.h>
  25. /*
  26. * Instructions and instruction encoding are documented in the RISC-V
  27. * specification. This file is based on version 2.2:
  28. *
  29. * https://github.com/riscv/riscv-isa-manual/blob/master/release/riscv-spec-v2.2.pdf
  30. */
  31. #define LDR 0x58000050
  32. #define BR 0xd61f0200
  33. /*
  34. * Check if EHDR is a valid ELF header.
  35. */
  36. grub_err_t
  37. grub_arch_dl_check_header (void *ehdr)
  38. {
  39. Elf_Ehdr *e = ehdr;
  40. /* Check the magic numbers. */
  41. if (e->e_ident[EI_DATA] != ELFDATA2LSB || e->e_machine != EM_RISCV)
  42. return grub_error (GRUB_ERR_BAD_OS,
  43. N_("invalid arch-dependent ELF magic"));
  44. return GRUB_ERR_NONE;
  45. }
  46. #pragma GCC diagnostic ignored "-Wcast-align"
  47. /* Relocate symbols. */
  48. grub_err_t
  49. grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
  50. Elf_Shdr *s, grub_dl_segment_t seg)
  51. {
  52. Elf_Rel *rel, *max;
  53. for (rel = (Elf_Rel *) ((char *) ehdr + s->sh_offset),
  54. max = (Elf_Rel *) ((char *) rel + s->sh_size);
  55. rel < max;
  56. rel = (Elf_Rel *) ((char *) rel + s->sh_entsize))
  57. {
  58. Elf_Sym *sym;
  59. void *place;
  60. grub_size_t sym_addr;
  61. if (rel->r_offset >= seg->size)
  62. return grub_error (GRUB_ERR_BAD_MODULE,
  63. "reloc offset is out of the segment");
  64. sym = (Elf_Sym *) ((char *) mod->symtab
  65. + mod->symsize * ELF_R_SYM (rel->r_info));
  66. sym_addr = sym->st_value;
  67. if (s->sh_type == SHT_RELA)
  68. sym_addr += ((Elf_Rela *) rel)->r_addend;
  69. place = (void *) ((grub_addr_t) seg->addr + rel->r_offset);
  70. switch (ELF_R_TYPE (rel->r_info))
  71. {
  72. case R_RISCV_32:
  73. {
  74. grub_uint32_t *abs_place = place;
  75. grub_dprintf ("dl", " reloc_abs32 %p => 0x%016llx\n",
  76. place, (unsigned long long) sym_addr);
  77. *abs_place = (grub_uint32_t) sym_addr;
  78. }
  79. break;
  80. case R_RISCV_64:
  81. {
  82. grub_size_t *abs_place = place;
  83. grub_dprintf ("dl", " reloc_abs64 %p => 0x%016llx\n",
  84. place, (unsigned long long) sym_addr);
  85. *abs_place = (grub_size_t) sym_addr;
  86. }
  87. break;
  88. case R_RISCV_ADD8:
  89. {
  90. grub_uint8_t *abs_place = place;
  91. *abs_place += (grub_uint8_t) sym_addr;
  92. }
  93. break;
  94. case R_RISCV_ADD16:
  95. {
  96. grub_uint16_t *abs_place = place;
  97. *abs_place += (grub_uint16_t) sym_addr;
  98. }
  99. break;
  100. case R_RISCV_ADD32:
  101. {
  102. grub_uint32_t *abs_place = place;
  103. *abs_place += (grub_uint32_t) sym_addr;
  104. }
  105. break;
  106. case R_RISCV_ADD64:
  107. {
  108. grub_size_t *abs_place = place;
  109. *abs_place += (grub_size_t) sym_addr;
  110. }
  111. break;
  112. case R_RISCV_SUB8:
  113. {
  114. grub_uint8_t *abs_place = place;
  115. *abs_place -= (grub_uint8_t) sym_addr;
  116. }
  117. break;
  118. case R_RISCV_SUB16:
  119. {
  120. grub_uint16_t *abs_place = place;
  121. *abs_place -= (grub_uint16_t) sym_addr;
  122. }
  123. break;
  124. case R_RISCV_SUB32:
  125. {
  126. grub_uint32_t *abs_place = place;
  127. *abs_place -= (grub_uint32_t) sym_addr;
  128. }
  129. break;
  130. case R_RISCV_SUB64:
  131. {
  132. grub_size_t *abs_place = place;
  133. *abs_place -= (grub_size_t) sym_addr;
  134. }
  135. break;
  136. case R_RISCV_BRANCH:
  137. {
  138. grub_uint32_t *abs_place = place;
  139. grub_ssize_t off = sym_addr - (grub_addr_t) place;
  140. grub_uint32_t imm12 = (off & 0x1000) << (31 - 12);
  141. grub_uint32_t imm11 = (off & 0x800) >> (11 - 7);
  142. grub_uint32_t imm10_5 = (off & 0x7e0) << (30 - 10);
  143. grub_uint32_t imm4_1 = (off & 0x1e) << (11 - 4);
  144. *abs_place = (*abs_place & 0x1fff07f)
  145. | imm12 | imm11 | imm10_5 | imm4_1;
  146. }
  147. break;
  148. case R_RISCV_JAL:
  149. {
  150. grub_uint32_t *abs_place = place;
  151. grub_ssize_t off = sym_addr - (grub_addr_t) place;
  152. grub_uint32_t imm20 = (off & 0x100000) << (31 - 20);
  153. grub_uint32_t imm19_12 = (off & 0xff000);
  154. grub_uint32_t imm11 = (off & 0x800) << (20 - 11);
  155. grub_uint32_t imm10_1 = (off & 0x7fe) << (30 - 10);
  156. *abs_place = (*abs_place & 0xfff)
  157. | imm20 | imm19_12 | imm11 | imm10_1;
  158. }
  159. break;
  160. case R_RISCV_CALL:
  161. case R_RISCV_CALL_PLT:
  162. {
  163. grub_uint32_t *abs_place = place;
  164. grub_ssize_t off = sym_addr - (grub_addr_t) place;
  165. grub_uint32_t hi20, lo12;
  166. if (off != (grub_int32_t) off)
  167. return grub_error (GRUB_ERR_BAD_MODULE, "relocation overflow");
  168. hi20 = (off + 0x800) & 0xfffff000;
  169. lo12 = (off - hi20) & 0xfff;
  170. abs_place[0] = (abs_place[0] & 0xfff) | hi20;
  171. abs_place[1] = (abs_place[1] & 0xfffff) | (lo12 << 20);
  172. }
  173. break;
  174. case R_RISCV_RVC_BRANCH:
  175. {
  176. grub_uint16_t *abs_place = place;
  177. grub_ssize_t off = sym_addr - (grub_addr_t) place;
  178. grub_uint16_t imm8 = (off & 0x100) << (12 - 8);
  179. grub_uint16_t imm7_6 = (off & 0xc0) >> (6 - 5);
  180. grub_uint16_t imm5 = (off & 0x20) >> (5 - 2);
  181. grub_uint16_t imm4_3 = (off & 0x18) << (12 - 5);
  182. grub_uint16_t imm2_1 = (off & 0x6) << (12 - 10);
  183. *abs_place = (*abs_place & 0xe383)
  184. | imm8 | imm7_6 | imm5 | imm4_3 | imm2_1;
  185. }
  186. break;
  187. case R_RISCV_RVC_JUMP:
  188. {
  189. grub_uint16_t *abs_place = place;
  190. grub_ssize_t off = sym_addr - (grub_addr_t) place;
  191. grub_uint16_t imm11 = (off & 0x800) << (12 - 11);
  192. grub_uint16_t imm10 = (off & 0x400) >> (10 - 8);
  193. grub_uint16_t imm9_8 = (off & 0x300) << (12 - 11);
  194. grub_uint16_t imm7 = (off & 0x80) >> (7 - 6);
  195. grub_uint16_t imm6 = (off & 0x40) << (12 - 11);
  196. grub_uint16_t imm5 = (off & 0x20) >> (5 - 2);
  197. grub_uint16_t imm4 = (off & 0x10) << (12 - 5);
  198. grub_uint16_t imm3_1 = (off & 0xe) << (12 - 10);
  199. *abs_place = ((*abs_place & 0xe003)
  200. | imm11 | imm10 | imm9_8 | imm7 | imm6
  201. | imm5 | imm4 | imm3_1);
  202. }
  203. break;
  204. case R_RISCV_PCREL_HI20:
  205. {
  206. grub_uint32_t *abs_place = place;
  207. grub_ssize_t off = sym_addr - (grub_addr_t) place;
  208. grub_int32_t hi20;
  209. if (off != (grub_int32_t)off)
  210. return grub_error (GRUB_ERR_BAD_MODULE, "relocation overflow");
  211. hi20 = (off + 0x800) & 0xfffff000;
  212. *abs_place = (*abs_place & 0xfff) | hi20;
  213. }
  214. break;
  215. case R_RISCV_PCREL_LO12_I:
  216. case R_RISCV_PCREL_LO12_S:
  217. {
  218. grub_uint32_t *t32 = place;
  219. Elf_Rela *rel2;
  220. /* Search backwards for matching HI20 reloc. */
  221. for (rel2 = (Elf_Rela *) ((char *) rel - s->sh_entsize);
  222. (unsigned long)rel2 >= ((unsigned long)ehdr + s->sh_offset);
  223. rel2 = (Elf_Rela *) ((char *) rel2 - s->sh_entsize))
  224. {
  225. Elf_Addr rel2_info;
  226. Elf_Addr rel2_offset;
  227. Elf_Addr rel2_sym_addr;
  228. Elf_Addr rel2_loc;
  229. grub_ssize_t rel2_off;
  230. grub_ssize_t off;
  231. Elf_Sym *sym2;
  232. rel2_offset = rel2->r_offset;
  233. rel2_info = rel2->r_info;
  234. rel2_loc = (grub_addr_t) seg->addr + rel2_offset;
  235. if (ELF_R_TYPE (rel2_info) == R_RISCV_PCREL_HI20
  236. && rel2_loc == sym_addr)
  237. {
  238. sym2 = (Elf_Sym *) ((char *) mod->symtab
  239. + mod->symsize * ELF_R_SYM (rel2->r_info));
  240. rel2_sym_addr = sym2->st_value;
  241. if (s->sh_type == SHT_RELA)
  242. rel2_sym_addr += ((Elf_Rela *) rel2)->r_addend;
  243. rel2_off = rel2_sym_addr - rel2_loc;
  244. off = rel2_off - ((rel2_off + 0x800) & 0xfffff000);
  245. if (ELF_R_TYPE (rel->r_info) == R_RISCV_PCREL_LO12_I)
  246. *t32 = (*t32 & 0xfffff) | (off & 0xfff) << 20;
  247. else
  248. {
  249. grub_uint32_t imm11_5 = (off & 0xfe0) << (31 - 11);
  250. grub_uint32_t imm4_0 = (off & 0x1f) << (11 - 4);
  251. *t32 = (*t32 & 0x1fff07f) | imm11_5 | imm4_0;
  252. }
  253. break;
  254. }
  255. }
  256. if ((unsigned long)rel2 < ((unsigned long)ehdr + s->sh_offset))
  257. return grub_error (GRUB_ERR_BAD_MODULE, "cannot find matching HI20 relocation");
  258. }
  259. break;
  260. case R_RISCV_HI20:
  261. {
  262. grub_uint32_t *abs_place = place;
  263. *abs_place = (*abs_place & 0xfff) |
  264. (((grub_int32_t) sym_addr + 0x800) & 0xfffff000);
  265. }
  266. break;
  267. case R_RISCV_LO12_I:
  268. {
  269. grub_uint32_t *abs_place = place;
  270. grub_int32_t lo12 = (grub_int32_t) sym_addr -
  271. (((grub_int32_t) sym_addr + 0x800) & 0xfffff000);
  272. *abs_place = (*abs_place & 0xfffff) | ((lo12 & 0xfff) << 20);
  273. }
  274. break;
  275. case R_RISCV_LO12_S:
  276. {
  277. grub_uint32_t *abs_place = place;
  278. grub_int32_t lo12 = (grub_int32_t) sym_addr -
  279. (((grub_int32_t) sym_addr + 0x800) & 0xfffff000);
  280. grub_uint32_t imm11_5 = (lo12 & 0xfe0) << (31 - 11);
  281. grub_uint32_t imm4_0 = (lo12 & 0x1f) << (11 - 4);
  282. *abs_place = (*abs_place & 0x1fff07f) | imm11_5 | imm4_0;
  283. }
  284. break;
  285. case R_RISCV_RELAX:
  286. break;
  287. default:
  288. {
  289. char rel_info[17]; /* log16(2^64) = 16, plus NUL. */
  290. grub_snprintf (rel_info, sizeof (rel_info) - 1, "%" PRIxGRUB_UINT64_T,
  291. (grub_uint64_t) ELF_R_TYPE (rel->r_info));
  292. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  293. N_("relocation 0x%s is not implemented yet"), rel_info);
  294. }
  295. }
  296. }
  297. return GRUB_ERR_NONE;
  298. }