module.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Copyright (C) 2017 Zihao Yu
  5. */
  6. #include <linux/elf.h>
  7. #include <linux/err.h>
  8. #include <linux/errno.h>
  9. #include <linux/moduleloader.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/sizes.h>
  12. #include <linux/pgtable.h>
  13. #include <asm/alternative.h>
  14. #include <asm/sections.h>
  15. /*
  16. * The auipc+jalr instruction pair can reach any PC-relative offset
  17. * in the range [-2^31 - 2^11, 2^31 - 2^11)
  18. */
  19. static bool riscv_insn_valid_32bit_offset(ptrdiff_t val)
  20. {
  21. #ifdef CONFIG_32BIT
  22. return true;
  23. #else
  24. return (-(1L << 31) - (1L << 11)) <= val && val < ((1L << 31) - (1L << 11));
  25. #endif
  26. }
  27. static int apply_r_riscv_32_rela(struct module *me, u32 *location, Elf_Addr v)
  28. {
  29. if (v != (u32)v) {
  30. pr_err("%s: value %016llx out of range for 32-bit field\n",
  31. me->name, (long long)v);
  32. return -EINVAL;
  33. }
  34. *location = v;
  35. return 0;
  36. }
  37. static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
  38. {
  39. *(u64 *)location = v;
  40. return 0;
  41. }
  42. static int apply_r_riscv_branch_rela(struct module *me, u32 *location,
  43. Elf_Addr v)
  44. {
  45. ptrdiff_t offset = (void *)v - (void *)location;
  46. u32 imm12 = (offset & 0x1000) << (31 - 12);
  47. u32 imm11 = (offset & 0x800) >> (11 - 7);
  48. u32 imm10_5 = (offset & 0x7e0) << (30 - 10);
  49. u32 imm4_1 = (offset & 0x1e) << (11 - 4);
  50. *location = (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1;
  51. return 0;
  52. }
  53. static int apply_r_riscv_jal_rela(struct module *me, u32 *location,
  54. Elf_Addr v)
  55. {
  56. ptrdiff_t offset = (void *)v - (void *)location;
  57. u32 imm20 = (offset & 0x100000) << (31 - 20);
  58. u32 imm19_12 = (offset & 0xff000);
  59. u32 imm11 = (offset & 0x800) << (20 - 11);
  60. u32 imm10_1 = (offset & 0x7fe) << (30 - 10);
  61. *location = (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1;
  62. return 0;
  63. }
  64. static int apply_r_riscv_rvc_branch_rela(struct module *me, u32 *location,
  65. Elf_Addr v)
  66. {
  67. ptrdiff_t offset = (void *)v - (void *)location;
  68. u16 imm8 = (offset & 0x100) << (12 - 8);
  69. u16 imm7_6 = (offset & 0xc0) >> (6 - 5);
  70. u16 imm5 = (offset & 0x20) >> (5 - 2);
  71. u16 imm4_3 = (offset & 0x18) << (12 - 5);
  72. u16 imm2_1 = (offset & 0x6) << (12 - 10);
  73. *(u16 *)location = (*(u16 *)location & 0xe383) |
  74. imm8 | imm7_6 | imm5 | imm4_3 | imm2_1;
  75. return 0;
  76. }
  77. static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location,
  78. Elf_Addr v)
  79. {
  80. ptrdiff_t offset = (void *)v - (void *)location;
  81. u16 imm11 = (offset & 0x800) << (12 - 11);
  82. u16 imm10 = (offset & 0x400) >> (10 - 8);
  83. u16 imm9_8 = (offset & 0x300) << (12 - 11);
  84. u16 imm7 = (offset & 0x80) >> (7 - 6);
  85. u16 imm6 = (offset & 0x40) << (12 - 11);
  86. u16 imm5 = (offset & 0x20) >> (5 - 2);
  87. u16 imm4 = (offset & 0x10) << (12 - 5);
  88. u16 imm3_1 = (offset & 0xe) << (12 - 10);
  89. *(u16 *)location = (*(u16 *)location & 0xe003) |
  90. imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1;
  91. return 0;
  92. }
  93. static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location,
  94. Elf_Addr v)
  95. {
  96. ptrdiff_t offset = (void *)v - (void *)location;
  97. s32 hi20;
  98. if (!riscv_insn_valid_32bit_offset(offset)) {
  99. pr_err(
  100. "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
  101. me->name, (long long)v, location);
  102. return -EINVAL;
  103. }
  104. hi20 = (offset + 0x800) & 0xfffff000;
  105. *location = (*location & 0xfff) | hi20;
  106. return 0;
  107. }
  108. static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *location,
  109. Elf_Addr v)
  110. {
  111. /*
  112. * v is the lo12 value to fill. It is calculated before calling this
  113. * handler.
  114. */
  115. *location = (*location & 0xfffff) | ((v & 0xfff) << 20);
  116. return 0;
  117. }
  118. static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location,
  119. Elf_Addr v)
  120. {
  121. /*
  122. * v is the lo12 value to fill. It is calculated before calling this
  123. * handler.
  124. */
  125. u32 imm11_5 = (v & 0xfe0) << (31 - 11);
  126. u32 imm4_0 = (v & 0x1f) << (11 - 4);
  127. *location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
  128. return 0;
  129. }
  130. static int apply_r_riscv_hi20_rela(struct module *me, u32 *location,
  131. Elf_Addr v)
  132. {
  133. s32 hi20;
  134. if (IS_ENABLED(CONFIG_CMODEL_MEDLOW)) {
  135. pr_err(
  136. "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
  137. me->name, (long long)v, location);
  138. return -EINVAL;
  139. }
  140. hi20 = ((s32)v + 0x800) & 0xfffff000;
  141. *location = (*location & 0xfff) | hi20;
  142. return 0;
  143. }
  144. static int apply_r_riscv_lo12_i_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. *location = (*location & 0xfffff) | ((lo12 & 0xfff) << 20);
  151. return 0;
  152. }
  153. static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location,
  154. Elf_Addr v)
  155. {
  156. /* Skip medlow checking because of filtering by HI20 already */
  157. s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
  158. s32 lo12 = ((s32)v - hi20);
  159. u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11);
  160. u32 imm4_0 = (lo12 & 0x1f) << (11 - 4);
  161. *location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
  162. return 0;
  163. }
  164. static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location,
  165. Elf_Addr v)
  166. {
  167. ptrdiff_t offset = (void *)v - (void *)location;
  168. s32 hi20;
  169. /* Always emit the got entry */
  170. if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
  171. offset = module_emit_got_entry(me, v);
  172. offset = (void *)offset - (void *)location;
  173. } else {
  174. pr_err(
  175. "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n",
  176. me->name, (long long)v, location);
  177. return -EINVAL;
  178. }
  179. hi20 = (offset + 0x800) & 0xfffff000;
  180. *location = (*location & 0xfff) | hi20;
  181. return 0;
  182. }
  183. static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location,
  184. Elf_Addr v)
  185. {
  186. ptrdiff_t offset = (void *)v - (void *)location;
  187. u32 hi20, lo12;
  188. if (!riscv_insn_valid_32bit_offset(offset)) {
  189. /* Only emit the plt entry if offset over 32-bit range */
  190. if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
  191. offset = module_emit_plt_entry(me, v);
  192. offset = (void *)offset - (void *)location;
  193. } else {
  194. pr_err(
  195. "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
  196. me->name, (long long)v, location);
  197. return -EINVAL;
  198. }
  199. }
  200. hi20 = (offset + 0x800) & 0xfffff000;
  201. lo12 = (offset - hi20) & 0xfff;
  202. *location = (*location & 0xfff) | hi20;
  203. *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
  204. return 0;
  205. }
  206. static int apply_r_riscv_call_rela(struct module *me, u32 *location,
  207. Elf_Addr v)
  208. {
  209. ptrdiff_t offset = (void *)v - (void *)location;
  210. u32 hi20, lo12;
  211. if (!riscv_insn_valid_32bit_offset(offset)) {
  212. pr_err(
  213. "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
  214. me->name, (long long)v, location);
  215. return -EINVAL;
  216. }
  217. hi20 = (offset + 0x800) & 0xfffff000;
  218. lo12 = (offset - hi20) & 0xfff;
  219. *location = (*location & 0xfff) | hi20;
  220. *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
  221. return 0;
  222. }
  223. static int apply_r_riscv_relax_rela(struct module *me, u32 *location,
  224. Elf_Addr v)
  225. {
  226. return 0;
  227. }
  228. static int apply_r_riscv_align_rela(struct module *me, u32 *location,
  229. Elf_Addr v)
  230. {
  231. pr_err(
  232. "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n",
  233. me->name, location);
  234. return -EINVAL;
  235. }
  236. static int apply_r_riscv_add32_rela(struct module *me, u32 *location,
  237. Elf_Addr v)
  238. {
  239. *(u32 *)location += (u32)v;
  240. return 0;
  241. }
  242. static int apply_r_riscv_add64_rela(struct module *me, u32 *location,
  243. Elf_Addr v)
  244. {
  245. *(u64 *)location += (u64)v;
  246. return 0;
  247. }
  248. static int apply_r_riscv_sub32_rela(struct module *me, u32 *location,
  249. Elf_Addr v)
  250. {
  251. *(u32 *)location -= (u32)v;
  252. return 0;
  253. }
  254. static int apply_r_riscv_sub64_rela(struct module *me, u32 *location,
  255. Elf_Addr v)
  256. {
  257. *(u64 *)location -= (u64)v;
  258. return 0;
  259. }
  260. static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
  261. Elf_Addr v) = {
  262. [R_RISCV_32] = apply_r_riscv_32_rela,
  263. [R_RISCV_64] = apply_r_riscv_64_rela,
  264. [R_RISCV_BRANCH] = apply_r_riscv_branch_rela,
  265. [R_RISCV_JAL] = apply_r_riscv_jal_rela,
  266. [R_RISCV_RVC_BRANCH] = apply_r_riscv_rvc_branch_rela,
  267. [R_RISCV_RVC_JUMP] = apply_r_riscv_rvc_jump_rela,
  268. [R_RISCV_PCREL_HI20] = apply_r_riscv_pcrel_hi20_rela,
  269. [R_RISCV_PCREL_LO12_I] = apply_r_riscv_pcrel_lo12_i_rela,
  270. [R_RISCV_PCREL_LO12_S] = apply_r_riscv_pcrel_lo12_s_rela,
  271. [R_RISCV_HI20] = apply_r_riscv_hi20_rela,
  272. [R_RISCV_LO12_I] = apply_r_riscv_lo12_i_rela,
  273. [R_RISCV_LO12_S] = apply_r_riscv_lo12_s_rela,
  274. [R_RISCV_GOT_HI20] = apply_r_riscv_got_hi20_rela,
  275. [R_RISCV_CALL_PLT] = apply_r_riscv_call_plt_rela,
  276. [R_RISCV_CALL] = apply_r_riscv_call_rela,
  277. [R_RISCV_RELAX] = apply_r_riscv_relax_rela,
  278. [R_RISCV_ALIGN] = apply_r_riscv_align_rela,
  279. [R_RISCV_ADD32] = apply_r_riscv_add32_rela,
  280. [R_RISCV_ADD64] = apply_r_riscv_add64_rela,
  281. [R_RISCV_SUB32] = apply_r_riscv_sub32_rela,
  282. [R_RISCV_SUB64] = apply_r_riscv_sub64_rela,
  283. };
  284. int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  285. unsigned int symindex, unsigned int relsec,
  286. struct module *me)
  287. {
  288. Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
  289. int (*handler)(struct module *me, u32 *location, Elf_Addr v);
  290. Elf_Sym *sym;
  291. u32 *location;
  292. unsigned int i, type;
  293. Elf_Addr v;
  294. int res;
  295. pr_debug("Applying relocate section %u to %u\n", relsec,
  296. sechdrs[relsec].sh_info);
  297. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  298. /* This is where to make the change */
  299. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  300. + rel[i].r_offset;
  301. /* This is the symbol it is referring to */
  302. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  303. + ELF_RISCV_R_SYM(rel[i].r_info);
  304. if (IS_ERR_VALUE(sym->st_value)) {
  305. /* Ignore unresolved weak symbol */
  306. if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
  307. continue;
  308. pr_warn("%s: Unknown symbol %s\n",
  309. me->name, strtab + sym->st_name);
  310. return -ENOENT;
  311. }
  312. type = ELF_RISCV_R_TYPE(rel[i].r_info);
  313. if (type < ARRAY_SIZE(reloc_handlers_rela))
  314. handler = reloc_handlers_rela[type];
  315. else
  316. handler = NULL;
  317. if (!handler) {
  318. pr_err("%s: Unknown relocation type %u\n",
  319. me->name, type);
  320. return -EINVAL;
  321. }
  322. v = sym->st_value + rel[i].r_addend;
  323. if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
  324. unsigned int j;
  325. for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
  326. unsigned long hi20_loc =
  327. sechdrs[sechdrs[relsec].sh_info].sh_addr
  328. + rel[j].r_offset;
  329. u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
  330. /* Find the corresponding HI20 relocation entry */
  331. if (hi20_loc == sym->st_value
  332. && (hi20_type == R_RISCV_PCREL_HI20
  333. || hi20_type == R_RISCV_GOT_HI20)) {
  334. s32 hi20, lo12;
  335. Elf_Sym *hi20_sym =
  336. (Elf_Sym *)sechdrs[symindex].sh_addr
  337. + ELF_RISCV_R_SYM(rel[j].r_info);
  338. unsigned long hi20_sym_val =
  339. hi20_sym->st_value
  340. + rel[j].r_addend;
  341. /* Calculate lo12 */
  342. size_t offset = hi20_sym_val - hi20_loc;
  343. if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
  344. && hi20_type == R_RISCV_GOT_HI20) {
  345. offset = module_emit_got_entry(
  346. me, hi20_sym_val);
  347. offset = offset - hi20_loc;
  348. }
  349. hi20 = (offset + 0x800) & 0xfffff000;
  350. lo12 = offset - hi20;
  351. v = lo12;
  352. break;
  353. }
  354. }
  355. if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
  356. pr_err(
  357. "%s: Can not find HI20 relocation information\n",
  358. me->name);
  359. return -EINVAL;
  360. }
  361. }
  362. res = handler(me, location, v);
  363. if (res)
  364. return res;
  365. }
  366. return 0;
  367. }
  368. #if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
  369. void *module_alloc(unsigned long size)
  370. {
  371. return __vmalloc_node_range(size, 1, MODULES_VADDR,
  372. MODULES_END, GFP_KERNEL,
  373. PAGE_KERNEL, 0, NUMA_NO_NODE,
  374. __builtin_return_address(0));
  375. }
  376. #endif
  377. static const Elf_Shdr *find_section(const Elf_Ehdr *hdr,
  378. const Elf_Shdr *sechdrs,
  379. const char *name)
  380. {
  381. const Elf_Shdr *s, *se;
  382. const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  383. for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
  384. if (strcmp(name, secstrs + s->sh_name) == 0)
  385. return s;
  386. }
  387. return NULL;
  388. }
  389. int module_finalize(const Elf_Ehdr *hdr,
  390. const Elf_Shdr *sechdrs,
  391. struct module *me)
  392. {
  393. const Elf_Shdr *s;
  394. s = find_section(hdr, sechdrs, ".alternative");
  395. if (s)
  396. apply_module_alternatives((void *)s->sh_addr, s->sh_size);
  397. return 0;
  398. }