module.c 8.7 KB

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