module.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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/slab.h>
  20. #include <linux/fs.h>
  21. #include <linux/string.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. for (s = sechdrs; s < sechdrs_end; s++) {
  65. if (strcmp(".ARM.exidx.init.text", secstrings + s->sh_name) == 0)
  66. mod->arch.unw_sec_init = s;
  67. else if (strcmp(".ARM.exidx.devinit.text", secstrings + s->sh_name) == 0)
  68. mod->arch.unw_sec_devinit = s;
  69. else if (strcmp(".ARM.exidx", secstrings + s->sh_name) == 0)
  70. mod->arch.unw_sec_core = s;
  71. else if (strcmp(".init.text", secstrings + s->sh_name) == 0)
  72. mod->arch.sec_init_text = s;
  73. else if (strcmp(".devinit.text", secstrings + s->sh_name) == 0)
  74. mod->arch.sec_devinit_text = s;
  75. else if (strcmp(".text", secstrings + s->sh_name) == 0)
  76. mod->arch.sec_core_text = s;
  77. }
  78. #endif
  79. return 0;
  80. }
  81. int
  82. apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  83. unsigned int relindex, struct module *module)
  84. {
  85. Elf32_Shdr *symsec = sechdrs + symindex;
  86. Elf32_Shdr *relsec = sechdrs + relindex;
  87. Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
  88. Elf32_Rel *rel = (void *)relsec->sh_addr;
  89. unsigned int i;
  90. for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
  91. unsigned long loc;
  92. Elf32_Sym *sym;
  93. s32 offset;
  94. u32 upper, lower, sign, j1, j2;
  95. offset = ELF32_R_SYM(rel->r_info);
  96. if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
  97. printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
  98. module->name, relindex, i);
  99. return -ENOEXEC;
  100. }
  101. sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
  102. if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
  103. printk(KERN_ERR "%s: out of bounds relocation, "
  104. "section %d reloc %d offset %d size %d\n",
  105. module->name, relindex, i, rel->r_offset,
  106. dstsec->sh_size);
  107. return -ENOEXEC;
  108. }
  109. loc = dstsec->sh_addr + rel->r_offset;
  110. switch (ELF32_R_TYPE(rel->r_info)) {
  111. case R_ARM_NONE:
  112. /* ignore */
  113. break;
  114. case R_ARM_ABS32:
  115. *(u32 *)loc += sym->st_value;
  116. break;
  117. case R_ARM_PC24:
  118. case R_ARM_CALL:
  119. case R_ARM_JUMP24:
  120. offset = (*(u32 *)loc & 0x00ffffff) << 2;
  121. if (offset & 0x02000000)
  122. offset -= 0x04000000;
  123. offset += sym->st_value - loc;
  124. if (offset & 3 ||
  125. offset <= (s32)0xfe000000 ||
  126. offset >= (s32)0x02000000) {
  127. printk(KERN_ERR
  128. "%s: relocation out of range, section "
  129. "%d reloc %d sym '%s'\n", module->name,
  130. relindex, i, strtab + sym->st_name);
  131. return -ENOEXEC;
  132. }
  133. offset >>= 2;
  134. *(u32 *)loc &= 0xff000000;
  135. *(u32 *)loc |= offset & 0x00ffffff;
  136. break;
  137. case R_ARM_V4BX:
  138. /* Preserve Rm and the condition code. Alter
  139. * other bits to re-code instruction as
  140. * MOV PC,Rm.
  141. */
  142. *(u32 *)loc &= 0xf000000f;
  143. *(u32 *)loc |= 0x01a0f000;
  144. break;
  145. case R_ARM_PREL31:
  146. offset = *(u32 *)loc + sym->st_value - loc;
  147. *(u32 *)loc = offset & 0x7fffffff;
  148. break;
  149. case R_ARM_MOVW_ABS_NC:
  150. case R_ARM_MOVT_ABS:
  151. offset = *(u32 *)loc;
  152. offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
  153. offset = (offset ^ 0x8000) - 0x8000;
  154. offset += sym->st_value;
  155. if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_ABS)
  156. offset >>= 16;
  157. *(u32 *)loc &= 0xfff0f000;
  158. *(u32 *)loc |= ((offset & 0xf000) << 4) |
  159. (offset & 0x0fff);
  160. break;
  161. case R_ARM_THM_CALL:
  162. case R_ARM_THM_JUMP24:
  163. upper = *(u16 *)loc;
  164. lower = *(u16 *)(loc + 2);
  165. /*
  166. * 25 bit signed address range (Thumb-2 BL and B.W
  167. * instructions):
  168. * S:I1:I2:imm10:imm11:0
  169. * where:
  170. * S = upper[10] = offset[24]
  171. * I1 = ~(J1 ^ S) = offset[23]
  172. * I2 = ~(J2 ^ S) = offset[22]
  173. * imm10 = upper[9:0] = offset[21:12]
  174. * imm11 = lower[10:0] = offset[11:1]
  175. * J1 = lower[13]
  176. * J2 = lower[11]
  177. */
  178. sign = (upper >> 10) & 1;
  179. j1 = (lower >> 13) & 1;
  180. j2 = (lower >> 11) & 1;
  181. offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
  182. ((~(j2 ^ sign) & 1) << 22) |
  183. ((upper & 0x03ff) << 12) |
  184. ((lower & 0x07ff) << 1);
  185. if (offset & 0x01000000)
  186. offset -= 0x02000000;
  187. offset += sym->st_value - loc;
  188. /* only Thumb addresses allowed (no interworking) */
  189. if (!(offset & 1) ||
  190. offset <= (s32)0xff000000 ||
  191. offset >= (s32)0x01000000) {
  192. printk(KERN_ERR
  193. "%s: relocation out of range, section "
  194. "%d reloc %d sym '%s'\n", module->name,
  195. relindex, i, strtab + sym->st_name);
  196. return -ENOEXEC;
  197. }
  198. sign = (offset >> 24) & 1;
  199. j1 = sign ^ (~(offset >> 23) & 1);
  200. j2 = sign ^ (~(offset >> 22) & 1);
  201. *(u16 *)loc = (u16)((upper & 0xf800) | (sign << 10) |
  202. ((offset >> 12) & 0x03ff));
  203. *(u16 *)(loc + 2) = (u16)((lower & 0xd000) |
  204. (j1 << 13) | (j2 << 11) |
  205. ((offset >> 1) & 0x07ff));
  206. upper = *(u16 *)loc;
  207. lower = *(u16 *)(loc + 2);
  208. break;
  209. default:
  210. printk(KERN_ERR "%s: unknown relocation: %u\n",
  211. module->name, ELF32_R_TYPE(rel->r_info));
  212. return -ENOEXEC;
  213. }
  214. }
  215. return 0;
  216. }
  217. int
  218. apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
  219. unsigned int symindex, unsigned int relsec, struct module *module)
  220. {
  221. printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
  222. module->name);
  223. return -ENOEXEC;
  224. }
  225. #ifdef CONFIG_ARM_UNWIND
  226. static void register_unwind_tables(struct module *mod)
  227. {
  228. if (mod->arch.unw_sec_init && mod->arch.sec_init_text)
  229. mod->arch.unwind_init =
  230. unwind_table_add(mod->arch.unw_sec_init->sh_addr,
  231. mod->arch.unw_sec_init->sh_size,
  232. mod->arch.sec_init_text->sh_addr,
  233. mod->arch.sec_init_text->sh_size);
  234. if (mod->arch.unw_sec_devinit && mod->arch.sec_devinit_text)
  235. mod->arch.unwind_devinit =
  236. unwind_table_add(mod->arch.unw_sec_devinit->sh_addr,
  237. mod->arch.unw_sec_devinit->sh_size,
  238. mod->arch.sec_devinit_text->sh_addr,
  239. mod->arch.sec_devinit_text->sh_size);
  240. if (mod->arch.unw_sec_core && mod->arch.sec_core_text)
  241. mod->arch.unwind_core =
  242. unwind_table_add(mod->arch.unw_sec_core->sh_addr,
  243. mod->arch.unw_sec_core->sh_size,
  244. mod->arch.sec_core_text->sh_addr,
  245. mod->arch.sec_core_text->sh_size);
  246. }
  247. static void unregister_unwind_tables(struct module *mod)
  248. {
  249. unwind_table_del(mod->arch.unwind_init);
  250. unwind_table_del(mod->arch.unwind_devinit);
  251. unwind_table_del(mod->arch.unwind_core);
  252. }
  253. #else
  254. static inline void register_unwind_tables(struct module *mod) { }
  255. static inline void unregister_unwind_tables(struct module *mod) { }
  256. #endif
  257. int
  258. module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
  259. struct module *module)
  260. {
  261. register_unwind_tables(module);
  262. return 0;
  263. }
  264. void
  265. module_arch_cleanup(struct module *mod)
  266. {
  267. unregister_unwind_tables(mod);
  268. }