module.c 11 KB

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