module.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/kernel/module.c
  4. *
  5. * Copyright (C) 2002 Russell King.
  6. * Modified for nommu by Hyok S. Choi
  7. *
  8. * Module allocation method suggested by Andi Kleen.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/moduleloader.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/elf.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/fs.h>
  17. #include <linux/string.h>
  18. #include <linux/gfp.h>
  19. #include <asm/sections.h>
  20. #include <asm/smp_plat.h>
  21. #include <asm/unwind.h>
  22. #include <asm/opcodes.h>
  23. #ifdef CONFIG_XIP_KERNEL
  24. /*
  25. * The XIP kernel text is mapped in the module area for modules and
  26. * some other stuff to work without any indirect relocations.
  27. * MODULES_VADDR is redefined here and not in asm/memory.h to avoid
  28. * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off.
  29. */
  30. #undef MODULES_VADDR
  31. #define MODULES_VADDR (((unsigned long)_exiprom + ~PMD_MASK) & PMD_MASK)
  32. #endif
  33. #ifdef CONFIG_MMU
  34. void *module_alloc(unsigned long size)
  35. {
  36. gfp_t gfp_mask = GFP_KERNEL;
  37. void *p;
  38. /* Silence the initial allocation */
  39. if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS))
  40. gfp_mask |= __GFP_NOWARN;
  41. p = __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
  42. gfp_mask, PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
  43. __builtin_return_address(0));
  44. if (!IS_ENABLED(CONFIG_ARM_MODULE_PLTS) || p)
  45. return p;
  46. return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
  47. GFP_KERNEL, PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
  48. __builtin_return_address(0));
  49. }
  50. #endif
  51. bool module_init_section(const char *name)
  52. {
  53. return strstarts(name, ".init") ||
  54. strstarts(name, ".ARM.extab.init") ||
  55. strstarts(name, ".ARM.exidx.init");
  56. }
  57. bool module_exit_section(const char *name)
  58. {
  59. return strstarts(name, ".exit") ||
  60. strstarts(name, ".ARM.extab.exit") ||
  61. strstarts(name, ".ARM.exidx.exit");
  62. }
  63. #ifdef CONFIG_ARM_HAS_GROUP_RELOCS
  64. /*
  65. * This implements the partitioning algorithm for group relocations as
  66. * documented in the ARM AArch32 ELF psABI (IHI 0044).
  67. *
  68. * A single PC-relative symbol reference is divided in up to 3 add or subtract
  69. * operations, where the final one could be incorporated into a load/store
  70. * instruction with immediate offset. E.g.,
  71. *
  72. * ADD Rd, PC, #... or ADD Rd, PC, #...
  73. * ADD Rd, Rd, #... ADD Rd, Rd, #...
  74. * LDR Rd, [Rd, #...] ADD Rd, Rd, #...
  75. *
  76. * The latter has a guaranteed range of only 16 MiB (3x8 == 24 bits), so it is
  77. * of limited use in the kernel. However, the ADD/ADD/LDR combo has a range of
  78. * -/+ 256 MiB, (2x8 + 12 == 28 bits), which means it has sufficient range for
  79. * any in-kernel symbol reference (unless module PLTs are being used).
  80. *
  81. * The main advantage of this approach over the typical pattern using a literal
  82. * load is that literal loads may miss in the D-cache, and generally lead to
  83. * lower cache efficiency for variables that are referenced often from many
  84. * different places in the code.
  85. */
  86. static u32 get_group_rem(u32 group, u32 *offset)
  87. {
  88. u32 val = *offset;
  89. u32 shift;
  90. do {
  91. shift = val ? (31 - __fls(val)) & ~1 : 32;
  92. *offset = val;
  93. if (!val)
  94. break;
  95. val &= 0xffffff >> shift;
  96. } while (group--);
  97. return shift;
  98. }
  99. #endif
  100. int
  101. apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  102. unsigned int relindex, struct module *module)
  103. {
  104. Elf32_Shdr *symsec = sechdrs + symindex;
  105. Elf32_Shdr *relsec = sechdrs + relindex;
  106. Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
  107. Elf32_Rel *rel = (void *)relsec->sh_addr;
  108. unsigned int i;
  109. for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
  110. unsigned long loc;
  111. Elf32_Sym *sym;
  112. const char *symname;
  113. #ifdef CONFIG_ARM_HAS_GROUP_RELOCS
  114. u32 shift, group = 1;
  115. #endif
  116. s32 offset;
  117. u32 tmp;
  118. #ifdef CONFIG_THUMB2_KERNEL
  119. u32 upper, lower, sign, j1, j2;
  120. #endif
  121. offset = ELF32_R_SYM(rel->r_info);
  122. if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
  123. pr_err("%s: section %u reloc %u: bad relocation sym offset\n",
  124. module->name, relindex, i);
  125. return -ENOEXEC;
  126. }
  127. sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
  128. symname = strtab + sym->st_name;
  129. if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
  130. pr_err("%s: section %u reloc %u sym '%s': out of bounds relocation, offset %d size %u\n",
  131. module->name, relindex, i, symname,
  132. rel->r_offset, dstsec->sh_size);
  133. return -ENOEXEC;
  134. }
  135. loc = dstsec->sh_addr + rel->r_offset;
  136. switch (ELF32_R_TYPE(rel->r_info)) {
  137. case R_ARM_NONE:
  138. /* ignore */
  139. break;
  140. case R_ARM_ABS32:
  141. case R_ARM_TARGET1:
  142. *(u32 *)loc += sym->st_value;
  143. break;
  144. case R_ARM_PC24:
  145. case R_ARM_CALL:
  146. case R_ARM_JUMP24:
  147. if (sym->st_value & 3) {
  148. pr_err("%s: section %u reloc %u sym '%s': unsupported interworking call (ARM -> Thumb)\n",
  149. module->name, relindex, i, symname);
  150. return -ENOEXEC;
  151. }
  152. offset = __mem_to_opcode_arm(*(u32 *)loc);
  153. offset = (offset & 0x00ffffff) << 2;
  154. if (offset & 0x02000000)
  155. offset -= 0x04000000;
  156. offset += sym->st_value - loc;
  157. /*
  158. * Route through a PLT entry if 'offset' exceeds the
  159. * supported range. Note that 'offset + loc + 8'
  160. * contains the absolute jump target, i.e.,
  161. * @sym + addend, corrected for the +8 PC bias.
  162. */
  163. if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) &&
  164. (offset <= (s32)0xfe000000 ||
  165. offset >= (s32)0x02000000))
  166. offset = get_module_plt(module, loc,
  167. offset + loc + 8)
  168. - loc - 8;
  169. if (offset <= (s32)0xfe000000 ||
  170. offset >= (s32)0x02000000) {
  171. pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
  172. module->name, relindex, i, symname,
  173. ELF32_R_TYPE(rel->r_info), loc,
  174. sym->st_value);
  175. return -ENOEXEC;
  176. }
  177. offset >>= 2;
  178. offset &= 0x00ffffff;
  179. *(u32 *)loc &= __opcode_to_mem_arm(0xff000000);
  180. *(u32 *)loc |= __opcode_to_mem_arm(offset);
  181. break;
  182. case R_ARM_V4BX:
  183. /* Preserve Rm and the condition code. Alter
  184. * other bits to re-code instruction as
  185. * MOV PC,Rm.
  186. */
  187. *(u32 *)loc &= __opcode_to_mem_arm(0xf000000f);
  188. *(u32 *)loc |= __opcode_to_mem_arm(0x01a0f000);
  189. break;
  190. case R_ARM_PREL31:
  191. offset = (*(s32 *)loc << 1) >> 1; /* sign extend */
  192. offset += sym->st_value - loc;
  193. if (offset >= 0x40000000 || offset < -0x40000000) {
  194. pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
  195. module->name, relindex, i, symname,
  196. ELF32_R_TYPE(rel->r_info), loc,
  197. sym->st_value);
  198. return -ENOEXEC;
  199. }
  200. *(u32 *)loc &= 0x80000000;
  201. *(u32 *)loc |= offset & 0x7fffffff;
  202. break;
  203. case R_ARM_REL32:
  204. *(u32 *)loc += sym->st_value - loc;
  205. break;
  206. case R_ARM_MOVW_ABS_NC:
  207. case R_ARM_MOVT_ABS:
  208. case R_ARM_MOVW_PREL_NC:
  209. case R_ARM_MOVT_PREL:
  210. offset = tmp = __mem_to_opcode_arm(*(u32 *)loc);
  211. offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
  212. offset = (offset ^ 0x8000) - 0x8000;
  213. offset += sym->st_value;
  214. if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_PREL ||
  215. ELF32_R_TYPE(rel->r_info) == R_ARM_MOVW_PREL_NC)
  216. offset -= loc;
  217. if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_ABS ||
  218. ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_PREL)
  219. offset >>= 16;
  220. tmp &= 0xfff0f000;
  221. tmp |= ((offset & 0xf000) << 4) |
  222. (offset & 0x0fff);
  223. *(u32 *)loc = __opcode_to_mem_arm(tmp);
  224. break;
  225. #ifdef CONFIG_ARM_HAS_GROUP_RELOCS
  226. case R_ARM_ALU_PC_G0_NC:
  227. group = 0;
  228. fallthrough;
  229. case R_ARM_ALU_PC_G1_NC:
  230. tmp = __mem_to_opcode_arm(*(u32 *)loc);
  231. offset = ror32(tmp & 0xff, (tmp & 0xf00) >> 7);
  232. if (tmp & BIT(22))
  233. offset = -offset;
  234. offset += sym->st_value - loc;
  235. if (offset < 0) {
  236. offset = -offset;
  237. tmp = (tmp & ~BIT(23)) | BIT(22); // SUB opcode
  238. } else {
  239. tmp = (tmp & ~BIT(22)) | BIT(23); // ADD opcode
  240. }
  241. shift = get_group_rem(group, &offset);
  242. if (shift < 24) {
  243. offset >>= 24 - shift;
  244. offset |= (shift + 8) << 7;
  245. }
  246. *(u32 *)loc = __opcode_to_mem_arm((tmp & ~0xfff) | offset);
  247. break;
  248. case R_ARM_LDR_PC_G2:
  249. tmp = __mem_to_opcode_arm(*(u32 *)loc);
  250. offset = tmp & 0xfff;
  251. if (~tmp & BIT(23)) // U bit cleared?
  252. offset = -offset;
  253. offset += sym->st_value - loc;
  254. if (offset < 0) {
  255. offset = -offset;
  256. tmp &= ~BIT(23); // clear U bit
  257. } else {
  258. tmp |= BIT(23); // set U bit
  259. }
  260. get_group_rem(2, &offset);
  261. if (offset > 0xfff) {
  262. pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
  263. module->name, relindex, i, symname,
  264. ELF32_R_TYPE(rel->r_info), loc,
  265. sym->st_value);
  266. return -ENOEXEC;
  267. }
  268. *(u32 *)loc = __opcode_to_mem_arm((tmp & ~0xfff) | offset);
  269. break;
  270. #endif
  271. #ifdef CONFIG_THUMB2_KERNEL
  272. case R_ARM_THM_CALL:
  273. case R_ARM_THM_JUMP24:
  274. /*
  275. * For function symbols, only Thumb addresses are
  276. * allowed (no interworking).
  277. *
  278. * For non-function symbols, the destination
  279. * has no specific ARM/Thumb disposition, so
  280. * the branch is resolved under the assumption
  281. * that interworking is not required.
  282. */
  283. if (ELF32_ST_TYPE(sym->st_info) == STT_FUNC &&
  284. !(sym->st_value & 1)) {
  285. pr_err("%s: section %u reloc %u sym '%s': unsupported interworking call (Thumb -> ARM)\n",
  286. module->name, relindex, i, symname);
  287. return -ENOEXEC;
  288. }
  289. upper = __mem_to_opcode_thumb16(*(u16 *)loc);
  290. lower = __mem_to_opcode_thumb16(*(u16 *)(loc + 2));
  291. /*
  292. * 25 bit signed address range (Thumb-2 BL and B.W
  293. * instructions):
  294. * S:I1:I2:imm10:imm11:0
  295. * where:
  296. * S = upper[10] = offset[24]
  297. * I1 = ~(J1 ^ S) = offset[23]
  298. * I2 = ~(J2 ^ S) = offset[22]
  299. * imm10 = upper[9:0] = offset[21:12]
  300. * imm11 = lower[10:0] = offset[11:1]
  301. * J1 = lower[13]
  302. * J2 = lower[11]
  303. */
  304. sign = (upper >> 10) & 1;
  305. j1 = (lower >> 13) & 1;
  306. j2 = (lower >> 11) & 1;
  307. offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
  308. ((~(j2 ^ sign) & 1) << 22) |
  309. ((upper & 0x03ff) << 12) |
  310. ((lower & 0x07ff) << 1);
  311. if (offset & 0x01000000)
  312. offset -= 0x02000000;
  313. offset += sym->st_value - loc;
  314. /*
  315. * Route through a PLT entry if 'offset' exceeds the
  316. * supported range.
  317. */
  318. if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) &&
  319. (offset <= (s32)0xff000000 ||
  320. offset >= (s32)0x01000000))
  321. offset = get_module_plt(module, loc,
  322. offset + loc + 4)
  323. - loc - 4;
  324. if (offset <= (s32)0xff000000 ||
  325. offset >= (s32)0x01000000) {
  326. pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
  327. module->name, relindex, i, symname,
  328. ELF32_R_TYPE(rel->r_info), loc,
  329. sym->st_value);
  330. return -ENOEXEC;
  331. }
  332. sign = (offset >> 24) & 1;
  333. j1 = sign ^ (~(offset >> 23) & 1);
  334. j2 = sign ^ (~(offset >> 22) & 1);
  335. upper = (u16)((upper & 0xf800) | (sign << 10) |
  336. ((offset >> 12) & 0x03ff));
  337. lower = (u16)((lower & 0xd000) |
  338. (j1 << 13) | (j2 << 11) |
  339. ((offset >> 1) & 0x07ff));
  340. *(u16 *)loc = __opcode_to_mem_thumb16(upper);
  341. *(u16 *)(loc + 2) = __opcode_to_mem_thumb16(lower);
  342. break;
  343. case R_ARM_THM_MOVW_ABS_NC:
  344. case R_ARM_THM_MOVT_ABS:
  345. case R_ARM_THM_MOVW_PREL_NC:
  346. case R_ARM_THM_MOVT_PREL:
  347. upper = __mem_to_opcode_thumb16(*(u16 *)loc);
  348. lower = __mem_to_opcode_thumb16(*(u16 *)(loc + 2));
  349. /*
  350. * MOVT/MOVW instructions encoding in Thumb-2:
  351. *
  352. * i = upper[10]
  353. * imm4 = upper[3:0]
  354. * imm3 = lower[14:12]
  355. * imm8 = lower[7:0]
  356. *
  357. * imm16 = imm4:i:imm3:imm8
  358. */
  359. offset = ((upper & 0x000f) << 12) |
  360. ((upper & 0x0400) << 1) |
  361. ((lower & 0x7000) >> 4) | (lower & 0x00ff);
  362. offset = (offset ^ 0x8000) - 0x8000;
  363. offset += sym->st_value;
  364. if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_PREL ||
  365. ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVW_PREL_NC)
  366. offset -= loc;
  367. if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS ||
  368. ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_PREL)
  369. offset >>= 16;
  370. upper = (u16)((upper & 0xfbf0) |
  371. ((offset & 0xf000) >> 12) |
  372. ((offset & 0x0800) >> 1));
  373. lower = (u16)((lower & 0x8f00) |
  374. ((offset & 0x0700) << 4) |
  375. (offset & 0x00ff));
  376. *(u16 *)loc = __opcode_to_mem_thumb16(upper);
  377. *(u16 *)(loc + 2) = __opcode_to_mem_thumb16(lower);
  378. break;
  379. #endif
  380. default:
  381. pr_err("%s: unknown relocation: %u\n",
  382. module->name, ELF32_R_TYPE(rel->r_info));
  383. return -ENOEXEC;
  384. }
  385. }
  386. return 0;
  387. }
  388. struct mod_unwind_map {
  389. const Elf_Shdr *unw_sec;
  390. const Elf_Shdr *txt_sec;
  391. };
  392. static const Elf_Shdr *find_mod_section(const Elf32_Ehdr *hdr,
  393. const Elf_Shdr *sechdrs, const char *name)
  394. {
  395. const Elf_Shdr *s, *se;
  396. const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  397. for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++)
  398. if (strcmp(name, secstrs + s->sh_name) == 0)
  399. return s;
  400. return NULL;
  401. }
  402. extern void fixup_pv_table(const void *, unsigned long);
  403. extern void fixup_smp(const void *, unsigned long);
  404. int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
  405. struct module *mod)
  406. {
  407. const Elf_Shdr *s = NULL;
  408. #ifdef CONFIG_ARM_UNWIND
  409. const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  410. const Elf_Shdr *sechdrs_end = sechdrs + hdr->e_shnum;
  411. struct list_head *unwind_list = &mod->arch.unwind_list;
  412. INIT_LIST_HEAD(unwind_list);
  413. mod->arch.init_table = NULL;
  414. for (s = sechdrs; s < sechdrs_end; s++) {
  415. const char *secname = secstrs + s->sh_name;
  416. const char *txtname;
  417. const Elf_Shdr *txt_sec;
  418. if (!(s->sh_flags & SHF_ALLOC) ||
  419. s->sh_type != ELF_SECTION_UNWIND)
  420. continue;
  421. if (!strcmp(".ARM.exidx", secname))
  422. txtname = ".text";
  423. else
  424. txtname = secname + strlen(".ARM.exidx");
  425. txt_sec = find_mod_section(hdr, sechdrs, txtname);
  426. if (txt_sec) {
  427. struct unwind_table *table =
  428. unwind_table_add(s->sh_addr,
  429. s->sh_size,
  430. txt_sec->sh_addr,
  431. txt_sec->sh_size);
  432. list_add(&table->mod_list, unwind_list);
  433. /* save init table for module_arch_freeing_init */
  434. if (strcmp(".ARM.exidx.init.text", secname) == 0)
  435. mod->arch.init_table = table;
  436. }
  437. }
  438. #endif
  439. #ifdef CONFIG_ARM_PATCH_PHYS_VIRT
  440. s = find_mod_section(hdr, sechdrs, ".pv_table");
  441. if (s)
  442. fixup_pv_table((void *)s->sh_addr, s->sh_size);
  443. #endif
  444. s = find_mod_section(hdr, sechdrs, ".alt.smp.init");
  445. if (s && !is_smp())
  446. #ifdef CONFIG_SMP_ON_UP
  447. fixup_smp((void *)s->sh_addr, s->sh_size);
  448. #else
  449. return -EINVAL;
  450. #endif
  451. return 0;
  452. }
  453. void
  454. module_arch_cleanup(struct module *mod)
  455. {
  456. #ifdef CONFIG_ARM_UNWIND
  457. struct unwind_table *tmp;
  458. struct unwind_table *n;
  459. list_for_each_entry_safe(tmp, n,
  460. &mod->arch.unwind_list, mod_list) {
  461. list_del(&tmp->mod_list);
  462. unwind_table_del(tmp);
  463. }
  464. mod->arch.init_table = NULL;
  465. #endif
  466. }
  467. void __weak module_arch_freeing_init(struct module *mod)
  468. {
  469. #ifdef CONFIG_ARM_UNWIND
  470. struct unwind_table *init = mod->arch.init_table;
  471. if (init) {
  472. mod->arch.init_table = NULL;
  473. list_del(&init->mod_list);
  474. unwind_table_del(init);
  475. }
  476. #endif
  477. }