module.c 9.8 KB

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