module.c 9.3 KB

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