module.c 11 KB

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