module.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * linux/arch/arm/kernel/module.c
  3. *
  4. * Copyright (C) 2002 Russell King.
  5. * Modified for nommu by Hyok S. Choi
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Module allocation method suggested by Andi Kleen.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleloader.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/elf.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/fs.h>
  20. #include <linux/string.h>
  21. #include <linux/gfp.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/sections.h>
  24. #include <asm/smp_plat.h>
  25. #include <asm/unwind.h>
  26. #include <asm/opcodes.h>
  27. #ifdef CONFIG_XIP_KERNEL
  28. /*
  29. * The XIP kernel text is mapped in the module area for modules and
  30. * some other stuff to work without any indirect relocations.
  31. * MODULES_VADDR is redefined here and not in asm/memory.h to avoid
  32. * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off.
  33. */
  34. #undef MODULES_VADDR
  35. #define MODULES_VADDR (((unsigned long)_exiprom + ~PMD_MASK) & PMD_MASK)
  36. #endif
  37. #ifdef CONFIG_MMU
  38. void *module_alloc(unsigned long size)
  39. {
  40. gfp_t gfp_mask = GFP_KERNEL;
  41. void *p;
  42. /* Silence the initial allocation */
  43. if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS))
  44. gfp_mask |= __GFP_NOWARN;
  45. p = __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
  46. gfp_mask, PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
  47. __builtin_return_address(0));
  48. if (!IS_ENABLED(CONFIG_ARM_MODULE_PLTS) || p)
  49. return p;
  50. return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
  51. GFP_KERNEL, PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
  52. __builtin_return_address(0));
  53. }
  54. #endif
  55. int
  56. apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  57. unsigned int relindex, struct module *module)
  58. {
  59. Elf32_Shdr *symsec = sechdrs + symindex;
  60. Elf32_Shdr *relsec = sechdrs + relindex;
  61. Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
  62. Elf32_Rel *rel = (void *)relsec->sh_addr;
  63. unsigned int i;
  64. for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
  65. unsigned long loc;
  66. Elf32_Sym *sym;
  67. const char *symname;
  68. s32 offset;
  69. u32 tmp;
  70. #ifdef CONFIG_THUMB2_KERNEL
  71. u32 upper, lower, sign, j1, j2;
  72. #endif
  73. offset = ELF32_R_SYM(rel->r_info);
  74. if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
  75. pr_err("%s: section %u reloc %u: bad relocation sym offset\n",
  76. module->name, relindex, i);
  77. return -ENOEXEC;
  78. }
  79. sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
  80. symname = strtab + sym->st_name;
  81. if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
  82. pr_err("%s: section %u reloc %u sym '%s': out of bounds relocation, offset %d size %u\n",
  83. module->name, relindex, i, symname,
  84. rel->r_offset, dstsec->sh_size);
  85. return -ENOEXEC;
  86. }
  87. loc = dstsec->sh_addr + rel->r_offset;
  88. switch (ELF32_R_TYPE(rel->r_info)) {
  89. case R_ARM_NONE:
  90. /* ignore */
  91. break;
  92. case R_ARM_ABS32:
  93. case R_ARM_TARGET1:
  94. *(u32 *)loc += sym->st_value;
  95. break;
  96. case R_ARM_PC24:
  97. case R_ARM_CALL:
  98. case R_ARM_JUMP24:
  99. if (sym->st_value & 3) {
  100. pr_err("%s: section %u reloc %u sym '%s': unsupported interworking call (ARM -> Thumb)\n",
  101. module->name, relindex, i, symname);
  102. return -ENOEXEC;
  103. }
  104. offset = __mem_to_opcode_arm(*(u32 *)loc);
  105. offset = (offset & 0x00ffffff) << 2;
  106. if (offset & 0x02000000)
  107. offset -= 0x04000000;
  108. offset += sym->st_value - loc;
  109. /*
  110. * Route through a PLT entry if 'offset' exceeds the
  111. * supported range. Note that 'offset + loc + 8'
  112. * contains the absolute jump target, i.e.,
  113. * @sym + addend, corrected for the +8 PC bias.
  114. */
  115. if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) &&
  116. (offset <= (s32)0xfe000000 ||
  117. offset >= (s32)0x02000000))
  118. offset = get_module_plt(module, loc,
  119. offset + loc + 8)
  120. - loc - 8;
  121. if (offset <= (s32)0xfe000000 ||
  122. offset >= (s32)0x02000000) {
  123. pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
  124. module->name, relindex, i, symname,
  125. ELF32_R_TYPE(rel->r_info), loc,
  126. sym->st_value);
  127. return -ENOEXEC;
  128. }
  129. offset >>= 2;
  130. offset &= 0x00ffffff;
  131. *(u32 *)loc &= __opcode_to_mem_arm(0xff000000);
  132. *(u32 *)loc |= __opcode_to_mem_arm(offset);
  133. break;
  134. case R_ARM_V4BX:
  135. /* Preserve Rm and the condition code. Alter
  136. * other bits to re-code instruction as
  137. * MOV PC,Rm.
  138. */
  139. *(u32 *)loc &= __opcode_to_mem_arm(0xf000000f);
  140. *(u32 *)loc |= __opcode_to_mem_arm(0x01a0f000);
  141. break;
  142. case R_ARM_PREL31:
  143. offset = (*(s32 *)loc << 1) >> 1; /* sign extend */
  144. offset += sym->st_value - loc;
  145. if (offset >= 0x40000000 || offset < -0x40000000) {
  146. pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
  147. module->name, relindex, i, symname,
  148. ELF32_R_TYPE(rel->r_info), loc,
  149. sym->st_value);
  150. return -ENOEXEC;
  151. }
  152. *(u32 *)loc &= 0x80000000;
  153. *(u32 *)loc |= offset & 0x7fffffff;
  154. break;
  155. case R_ARM_MOVW_ABS_NC:
  156. case R_ARM_MOVT_ABS:
  157. offset = tmp = __mem_to_opcode_arm(*(u32 *)loc);
  158. offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
  159. offset = (offset ^ 0x8000) - 0x8000;
  160. offset += sym->st_value;
  161. if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_ABS)
  162. offset >>= 16;
  163. tmp &= 0xfff0f000;
  164. tmp |= ((offset & 0xf000) << 4) |
  165. (offset & 0x0fff);
  166. *(u32 *)loc = __opcode_to_mem_arm(tmp);
  167. break;
  168. #ifdef CONFIG_THUMB2_KERNEL
  169. case R_ARM_THM_CALL:
  170. case R_ARM_THM_JUMP24:
  171. /*
  172. * For function symbols, only Thumb addresses are
  173. * allowed (no interworking).
  174. *
  175. * For non-function symbols, the destination
  176. * has no specific ARM/Thumb disposition, so
  177. * the branch is resolved under the assumption
  178. * that interworking is not required.
  179. */
  180. if (ELF32_ST_TYPE(sym->st_info) == STT_FUNC &&
  181. !(sym->st_value & 1)) {
  182. pr_err("%s: section %u reloc %u sym '%s': unsupported interworking call (Thumb -> ARM)\n",
  183. module->name, relindex, i, symname);
  184. return -ENOEXEC;
  185. }
  186. upper = __mem_to_opcode_thumb16(*(u16 *)loc);
  187. lower = __mem_to_opcode_thumb16(*(u16 *)(loc + 2));
  188. /*
  189. * 25 bit signed address range (Thumb-2 BL and B.W
  190. * instructions):
  191. * S:I1:I2:imm10:imm11:0
  192. * where:
  193. * S = upper[10] = offset[24]
  194. * I1 = ~(J1 ^ S) = offset[23]
  195. * I2 = ~(J2 ^ S) = offset[22]
  196. * imm10 = upper[9:0] = offset[21:12]
  197. * imm11 = lower[10:0] = offset[11:1]
  198. * J1 = lower[13]
  199. * J2 = lower[11]
  200. */
  201. sign = (upper >> 10) & 1;
  202. j1 = (lower >> 13) & 1;
  203. j2 = (lower >> 11) & 1;
  204. offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
  205. ((~(j2 ^ sign) & 1) << 22) |
  206. ((upper & 0x03ff) << 12) |
  207. ((lower & 0x07ff) << 1);
  208. if (offset & 0x01000000)
  209. offset -= 0x02000000;
  210. offset += sym->st_value - loc;
  211. /*
  212. * Route through a PLT entry if 'offset' exceeds the
  213. * supported range.
  214. */
  215. if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) &&
  216. (offset <= (s32)0xff000000 ||
  217. offset >= (s32)0x01000000))
  218. offset = get_module_plt(module, loc,
  219. offset + loc + 4)
  220. - loc - 4;
  221. if (offset <= (s32)0xff000000 ||
  222. offset >= (s32)0x01000000) {
  223. pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
  224. module->name, relindex, i, symname,
  225. ELF32_R_TYPE(rel->r_info), loc,
  226. sym->st_value);
  227. return -ENOEXEC;
  228. }
  229. sign = (offset >> 24) & 1;
  230. j1 = sign ^ (~(offset >> 23) & 1);
  231. j2 = sign ^ (~(offset >> 22) & 1);
  232. upper = (u16)((upper & 0xf800) | (sign << 10) |
  233. ((offset >> 12) & 0x03ff));
  234. lower = (u16)((lower & 0xd000) |
  235. (j1 << 13) | (j2 << 11) |
  236. ((offset >> 1) & 0x07ff));
  237. *(u16 *)loc = __opcode_to_mem_thumb16(upper);
  238. *(u16 *)(loc + 2) = __opcode_to_mem_thumb16(lower);
  239. break;
  240. case R_ARM_THM_MOVW_ABS_NC:
  241. case R_ARM_THM_MOVT_ABS:
  242. upper = __mem_to_opcode_thumb16(*(u16 *)loc);
  243. lower = __mem_to_opcode_thumb16(*(u16 *)(loc + 2));
  244. /*
  245. * MOVT/MOVW instructions encoding in Thumb-2:
  246. *
  247. * i = upper[10]
  248. * imm4 = upper[3:0]
  249. * imm3 = lower[14:12]
  250. * imm8 = lower[7:0]
  251. *
  252. * imm16 = imm4:i:imm3:imm8
  253. */
  254. offset = ((upper & 0x000f) << 12) |
  255. ((upper & 0x0400) << 1) |
  256. ((lower & 0x7000) >> 4) | (lower & 0x00ff);
  257. offset = (offset ^ 0x8000) - 0x8000;
  258. offset += sym->st_value;
  259. if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS)
  260. offset >>= 16;
  261. upper = (u16)((upper & 0xfbf0) |
  262. ((offset & 0xf000) >> 12) |
  263. ((offset & 0x0800) >> 1));
  264. lower = (u16)((lower & 0x8f00) |
  265. ((offset & 0x0700) << 4) |
  266. (offset & 0x00ff));
  267. *(u16 *)loc = __opcode_to_mem_thumb16(upper);
  268. *(u16 *)(loc + 2) = __opcode_to_mem_thumb16(lower);
  269. break;
  270. #endif
  271. default:
  272. pr_err("%s: unknown relocation: %u\n",
  273. module->name, ELF32_R_TYPE(rel->r_info));
  274. return -ENOEXEC;
  275. }
  276. }
  277. return 0;
  278. }
  279. struct mod_unwind_map {
  280. const Elf_Shdr *unw_sec;
  281. const Elf_Shdr *txt_sec;
  282. };
  283. static const Elf_Shdr *find_mod_section(const Elf32_Ehdr *hdr,
  284. const Elf_Shdr *sechdrs, const char *name)
  285. {
  286. const Elf_Shdr *s, *se;
  287. const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  288. for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++)
  289. if (strcmp(name, secstrs + s->sh_name) == 0)
  290. return s;
  291. return NULL;
  292. }
  293. extern void fixup_pv_table(const void *, unsigned long);
  294. extern void fixup_smp(const void *, unsigned long);
  295. int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
  296. struct module *mod)
  297. {
  298. const Elf_Shdr *s = NULL;
  299. #ifdef CONFIG_ARM_UNWIND
  300. const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  301. const Elf_Shdr *sechdrs_end = sechdrs + hdr->e_shnum;
  302. struct mod_unwind_map maps[ARM_SEC_MAX];
  303. int i;
  304. memset(maps, 0, sizeof(maps));
  305. for (s = sechdrs; s < sechdrs_end; s++) {
  306. const char *secname = secstrs + s->sh_name;
  307. if (!(s->sh_flags & SHF_ALLOC))
  308. continue;
  309. if (strcmp(".ARM.exidx.init.text", secname) == 0)
  310. maps[ARM_SEC_INIT].unw_sec = s;
  311. else if (strcmp(".ARM.exidx", secname) == 0)
  312. maps[ARM_SEC_CORE].unw_sec = s;
  313. else if (strcmp(".ARM.exidx.exit.text", secname) == 0)
  314. maps[ARM_SEC_EXIT].unw_sec = s;
  315. else if (strcmp(".ARM.exidx.text.unlikely", secname) == 0)
  316. maps[ARM_SEC_UNLIKELY].unw_sec = s;
  317. else if (strcmp(".ARM.exidx.text.hot", secname) == 0)
  318. maps[ARM_SEC_HOT].unw_sec = s;
  319. else if (strcmp(".init.text", secname) == 0)
  320. maps[ARM_SEC_INIT].txt_sec = s;
  321. else if (strcmp(".text", secname) == 0)
  322. maps[ARM_SEC_CORE].txt_sec = s;
  323. else if (strcmp(".exit.text", secname) == 0)
  324. maps[ARM_SEC_EXIT].txt_sec = s;
  325. else if (strcmp(".text.unlikely", secname) == 0)
  326. maps[ARM_SEC_UNLIKELY].txt_sec = s;
  327. else if (strcmp(".text.hot", secname) == 0)
  328. maps[ARM_SEC_HOT].txt_sec = s;
  329. }
  330. for (i = 0; i < ARM_SEC_MAX; i++)
  331. if (maps[i].unw_sec && maps[i].txt_sec)
  332. mod->arch.unwind[i] =
  333. unwind_table_add(maps[i].unw_sec->sh_addr,
  334. maps[i].unw_sec->sh_size,
  335. maps[i].txt_sec->sh_addr,
  336. maps[i].txt_sec->sh_size);
  337. #endif
  338. #ifdef CONFIG_ARM_PATCH_PHYS_VIRT
  339. s = find_mod_section(hdr, sechdrs, ".pv_table");
  340. if (s)
  341. fixup_pv_table((void *)s->sh_addr, s->sh_size);
  342. #endif
  343. s = find_mod_section(hdr, sechdrs, ".alt.smp.init");
  344. if (s && !is_smp())
  345. #ifdef CONFIG_SMP_ON_UP
  346. fixup_smp((void *)s->sh_addr, s->sh_size);
  347. #else
  348. return -EINVAL;
  349. #endif
  350. return 0;
  351. }
  352. void
  353. module_arch_cleanup(struct module *mod)
  354. {
  355. #ifdef CONFIG_ARM_UNWIND
  356. int i;
  357. for (i = 0; i < ARM_SEC_MAX; i++)
  358. if (mod->arch.unwind[i])
  359. unwind_table_del(mod->arch.unwind[i]);
  360. #endif
  361. }