module.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * Copyright (C) 2017 Zihao Yu
  13. */
  14. #include <linux/elf.h>
  15. #include <linux/err.h>
  16. #include <linux/errno.h>
  17. #include <linux/moduleloader.h>
  18. static int apply_r_riscv_32_rela(struct module *me, u32 *location, Elf_Addr v)
  19. {
  20. if (v != (u32)v) {
  21. pr_err("%s: value %016llx out of range for 32-bit field\n",
  22. me->name, (long long)v);
  23. return -EINVAL;
  24. }
  25. *location = v;
  26. return 0;
  27. }
  28. static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
  29. {
  30. *(u64 *)location = v;
  31. return 0;
  32. }
  33. static int apply_r_riscv_branch_rela(struct module *me, u32 *location,
  34. Elf_Addr v)
  35. {
  36. ptrdiff_t offset = (void *)v - (void *)location;
  37. u32 imm12 = (offset & 0x1000) << (31 - 12);
  38. u32 imm11 = (offset & 0x800) >> (11 - 7);
  39. u32 imm10_5 = (offset & 0x7e0) << (30 - 10);
  40. u32 imm4_1 = (offset & 0x1e) << (11 - 4);
  41. *location = (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1;
  42. return 0;
  43. }
  44. static int apply_r_riscv_jal_rela(struct module *me, u32 *location,
  45. Elf_Addr v)
  46. {
  47. ptrdiff_t offset = (void *)v - (void *)location;
  48. u32 imm20 = (offset & 0x100000) << (31 - 20);
  49. u32 imm19_12 = (offset & 0xff000);
  50. u32 imm11 = (offset & 0x800) << (20 - 11);
  51. u32 imm10_1 = (offset & 0x7fe) << (30 - 10);
  52. *location = (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1;
  53. return 0;
  54. }
  55. static int apply_r_riscv_rcv_branch_rela(struct module *me, u32 *location,
  56. Elf_Addr v)
  57. {
  58. ptrdiff_t offset = (void *)v - (void *)location;
  59. u16 imm8 = (offset & 0x100) << (12 - 8);
  60. u16 imm7_6 = (offset & 0xc0) >> (6 - 5);
  61. u16 imm5 = (offset & 0x20) >> (5 - 2);
  62. u16 imm4_3 = (offset & 0x18) << (12 - 5);
  63. u16 imm2_1 = (offset & 0x6) << (12 - 10);
  64. *(u16 *)location = (*(u16 *)location & 0xe383) |
  65. imm8 | imm7_6 | imm5 | imm4_3 | imm2_1;
  66. return 0;
  67. }
  68. static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location,
  69. Elf_Addr v)
  70. {
  71. ptrdiff_t offset = (void *)v - (void *)location;
  72. u16 imm11 = (offset & 0x800) << (12 - 11);
  73. u16 imm10 = (offset & 0x400) >> (10 - 8);
  74. u16 imm9_8 = (offset & 0x300) << (12 - 11);
  75. u16 imm7 = (offset & 0x80) >> (7 - 6);
  76. u16 imm6 = (offset & 0x40) << (12 - 11);
  77. u16 imm5 = (offset & 0x20) >> (5 - 2);
  78. u16 imm4 = (offset & 0x10) << (12 - 5);
  79. u16 imm3_1 = (offset & 0xe) << (12 - 10);
  80. *(u16 *)location = (*(u16 *)location & 0xe003) |
  81. imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1;
  82. return 0;
  83. }
  84. static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location,
  85. Elf_Addr v)
  86. {
  87. ptrdiff_t offset = (void *)v - (void *)location;
  88. s32 hi20;
  89. if (offset != (s32)offset) {
  90. pr_err(
  91. "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
  92. me->name, (long long)v, location);
  93. return -EINVAL;
  94. }
  95. hi20 = (offset + 0x800) & 0xfffff000;
  96. *location = (*location & 0xfff) | hi20;
  97. return 0;
  98. }
  99. static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *location,
  100. Elf_Addr v)
  101. {
  102. /*
  103. * v is the lo12 value to fill. It is calculated before calling this
  104. * handler.
  105. */
  106. *location = (*location & 0xfffff) | ((v & 0xfff) << 20);
  107. return 0;
  108. }
  109. static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location,
  110. Elf_Addr v)
  111. {
  112. /*
  113. * v is the lo12 value to fill. It is calculated before calling this
  114. * handler.
  115. */
  116. u32 imm11_5 = (v & 0xfe0) << (31 - 11);
  117. u32 imm4_0 = (v & 0x1f) << (11 - 4);
  118. *location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
  119. return 0;
  120. }
  121. static int apply_r_riscv_hi20_rela(struct module *me, u32 *location,
  122. Elf_Addr v)
  123. {
  124. s32 hi20;
  125. if (IS_ENABLED(CMODEL_MEDLOW)) {
  126. pr_err(
  127. "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
  128. me->name, (long long)v, location);
  129. return -EINVAL;
  130. }
  131. hi20 = ((s32)v + 0x800) & 0xfffff000;
  132. *location = (*location & 0xfff) | hi20;
  133. return 0;
  134. }
  135. static int apply_r_riscv_lo12_i_rela(struct module *me, u32 *location,
  136. Elf_Addr v)
  137. {
  138. /* Skip medlow checking because of filtering by HI20 already */
  139. s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
  140. s32 lo12 = ((s32)v - hi20);
  141. *location = (*location & 0xfffff) | ((lo12 & 0xfff) << 20);
  142. return 0;
  143. }
  144. static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location,
  145. Elf_Addr v)
  146. {
  147. /* Skip medlow checking because of filtering by HI20 already */
  148. s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
  149. s32 lo12 = ((s32)v - hi20);
  150. u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11);
  151. u32 imm4_0 = (lo12 & 0x1f) << (11 - 4);
  152. *location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
  153. return 0;
  154. }
  155. static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location,
  156. Elf_Addr v)
  157. {
  158. ptrdiff_t offset = (void *)v - (void *)location;
  159. s32 hi20;
  160. /* Always emit the got entry */
  161. if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
  162. offset = module_emit_got_entry(me, v);
  163. offset = (void *)offset - (void *)location;
  164. } else {
  165. pr_err(
  166. "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n",
  167. me->name, (long long)v, location);
  168. return -EINVAL;
  169. }
  170. hi20 = (offset + 0x800) & 0xfffff000;
  171. *location = (*location & 0xfff) | hi20;
  172. return 0;
  173. }
  174. static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location,
  175. Elf_Addr v)
  176. {
  177. ptrdiff_t offset = (void *)v - (void *)location;
  178. s32 fill_v = offset;
  179. u32 hi20, lo12;
  180. if (offset != fill_v) {
  181. /* Only emit the plt entry if offset over 32-bit range */
  182. if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
  183. offset = module_emit_plt_entry(me, v);
  184. offset = (void *)offset - (void *)location;
  185. } else {
  186. pr_err(
  187. "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
  188. me->name, (long long)v, location);
  189. return -EINVAL;
  190. }
  191. }
  192. hi20 = (offset + 0x800) & 0xfffff000;
  193. lo12 = (offset - hi20) & 0xfff;
  194. *location = (*location & 0xfff) | hi20;
  195. *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
  196. return 0;
  197. }
  198. static int apply_r_riscv_call_rela(struct module *me, u32 *location,
  199. Elf_Addr v)
  200. {
  201. ptrdiff_t offset = (void *)v - (void *)location;
  202. s32 fill_v = offset;
  203. u32 hi20, lo12;
  204. if (offset != fill_v) {
  205. pr_err(
  206. "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
  207. me->name, (long long)v, location);
  208. return -EINVAL;
  209. }
  210. hi20 = (offset + 0x800) & 0xfffff000;
  211. lo12 = (offset - hi20) & 0xfff;
  212. *location = (*location & 0xfff) | hi20;
  213. *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
  214. return 0;
  215. }
  216. static int apply_r_riscv_relax_rela(struct module *me, u32 *location,
  217. Elf_Addr v)
  218. {
  219. return 0;
  220. }
  221. static int apply_r_riscv_align_rela(struct module *me, u32 *location,
  222. Elf_Addr v)
  223. {
  224. pr_err(
  225. "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n",
  226. me->name, location);
  227. return -EINVAL;
  228. }
  229. static int apply_r_riscv_add32_rela(struct module *me, u32 *location,
  230. Elf_Addr v)
  231. {
  232. *(u32 *)location += (u32)v;
  233. return 0;
  234. }
  235. static int apply_r_riscv_sub32_rela(struct module *me, u32 *location,
  236. Elf_Addr v)
  237. {
  238. *(u32 *)location -= (u32)v;
  239. return 0;
  240. }
  241. static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
  242. Elf_Addr v) = {
  243. [R_RISCV_32] = apply_r_riscv_32_rela,
  244. [R_RISCV_64] = apply_r_riscv_64_rela,
  245. [R_RISCV_BRANCH] = apply_r_riscv_branch_rela,
  246. [R_RISCV_JAL] = apply_r_riscv_jal_rela,
  247. [R_RISCV_RVC_BRANCH] = apply_r_riscv_rcv_branch_rela,
  248. [R_RISCV_RVC_JUMP] = apply_r_riscv_rvc_jump_rela,
  249. [R_RISCV_PCREL_HI20] = apply_r_riscv_pcrel_hi20_rela,
  250. [R_RISCV_PCREL_LO12_I] = apply_r_riscv_pcrel_lo12_i_rela,
  251. [R_RISCV_PCREL_LO12_S] = apply_r_riscv_pcrel_lo12_s_rela,
  252. [R_RISCV_HI20] = apply_r_riscv_hi20_rela,
  253. [R_RISCV_LO12_I] = apply_r_riscv_lo12_i_rela,
  254. [R_RISCV_LO12_S] = apply_r_riscv_lo12_s_rela,
  255. [R_RISCV_GOT_HI20] = apply_r_riscv_got_hi20_rela,
  256. [R_RISCV_CALL_PLT] = apply_r_riscv_call_plt_rela,
  257. [R_RISCV_CALL] = apply_r_riscv_call_rela,
  258. [R_RISCV_RELAX] = apply_r_riscv_relax_rela,
  259. [R_RISCV_ALIGN] = apply_r_riscv_align_rela,
  260. [R_RISCV_ADD32] = apply_r_riscv_add32_rela,
  261. [R_RISCV_SUB32] = apply_r_riscv_sub32_rela,
  262. };
  263. int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  264. unsigned int symindex, unsigned int relsec,
  265. struct module *me)
  266. {
  267. Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
  268. int (*handler)(struct module *me, u32 *location, Elf_Addr v);
  269. Elf_Sym *sym;
  270. u32 *location;
  271. unsigned int i, type;
  272. Elf_Addr v;
  273. int res;
  274. pr_debug("Applying relocate section %u to %u\n", relsec,
  275. sechdrs[relsec].sh_info);
  276. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  277. /* This is where to make the change */
  278. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  279. + rel[i].r_offset;
  280. /* This is the symbol it is referring to */
  281. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  282. + ELF_RISCV_R_SYM(rel[i].r_info);
  283. if (IS_ERR_VALUE(sym->st_value)) {
  284. /* Ignore unresolved weak symbol */
  285. if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
  286. continue;
  287. pr_warning("%s: Unknown symbol %s\n",
  288. me->name, strtab + sym->st_name);
  289. return -ENOENT;
  290. }
  291. type = ELF_RISCV_R_TYPE(rel[i].r_info);
  292. if (type < ARRAY_SIZE(reloc_handlers_rela))
  293. handler = reloc_handlers_rela[type];
  294. else
  295. handler = NULL;
  296. if (!handler) {
  297. pr_err("%s: Unknown relocation type %u\n",
  298. me->name, type);
  299. return -EINVAL;
  300. }
  301. v = sym->st_value + rel[i].r_addend;
  302. if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
  303. unsigned int j;
  304. for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
  305. unsigned long hi20_loc =
  306. sechdrs[sechdrs[relsec].sh_info].sh_addr
  307. + rel[j].r_offset;
  308. u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
  309. /* Find the corresponding HI20 relocation entry */
  310. if (hi20_loc == sym->st_value
  311. && (hi20_type == R_RISCV_PCREL_HI20
  312. || hi20_type == R_RISCV_GOT_HI20)) {
  313. s32 hi20, lo12;
  314. Elf_Sym *hi20_sym =
  315. (Elf_Sym *)sechdrs[symindex].sh_addr
  316. + ELF_RISCV_R_SYM(rel[j].r_info);
  317. unsigned long hi20_sym_val =
  318. hi20_sym->st_value
  319. + rel[j].r_addend;
  320. /* Calculate lo12 */
  321. size_t offset = hi20_sym_val - hi20_loc;
  322. if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
  323. && hi20_type == R_RISCV_GOT_HI20) {
  324. offset = module_emit_got_entry(
  325. me, hi20_sym_val);
  326. offset = offset - hi20_loc;
  327. }
  328. hi20 = (offset + 0x800) & 0xfffff000;
  329. lo12 = offset - hi20;
  330. v = lo12;
  331. break;
  332. }
  333. }
  334. if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
  335. pr_err(
  336. "%s: Can not find HI20 relocation information\n",
  337. me->name);
  338. return -EINVAL;
  339. }
  340. }
  341. res = handler(me, location, v);
  342. if (res)
  343. return res;
  344. }
  345. return 0;
  346. }