module.c 10 KB

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