module.c 9.8 KB

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