module.c 9.8 KB

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