module.c 8.8 KB

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