module.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
  39. GFP_KERNEL, PAGE_KERNEL_EXEC, -1,
  40. __builtin_return_address(0));
  41. }
  42. #else /* CONFIG_MMU */
  43. void *module_alloc(unsigned long size)
  44. {
  45. return size == 0 ? NULL : vmalloc(size);
  46. }
  47. #endif /* !CONFIG_MMU */
  48. void module_free(struct module *module, void *region)
  49. {
  50. vfree(region);
  51. }
  52. int module_frob_arch_sections(Elf_Ehdr *hdr,
  53. Elf_Shdr *sechdrs,
  54. char *secstrings,
  55. struct module *mod)
  56. {
  57. return 0;
  58. }
  59. int
  60. apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  61. unsigned int relindex, struct module *module)
  62. {
  63. Elf32_Shdr *symsec = sechdrs + symindex;
  64. Elf32_Shdr *relsec = sechdrs + relindex;
  65. Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
  66. Elf32_Rel *rel = (void *)relsec->sh_addr;
  67. unsigned int i;
  68. for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
  69. unsigned long loc;
  70. Elf32_Sym *sym;
  71. s32 offset;
  72. #ifdef CONFIG_THUMB2_KERNEL
  73. u32 upper, lower, sign, j1, j2;
  74. #endif
  75. offset = ELF32_R_SYM(rel->r_info);
  76. if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
  77. printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
  78. module->name, relindex, i);
  79. return -ENOEXEC;
  80. }
  81. sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
  82. if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
  83. printk(KERN_ERR "%s: out of bounds relocation, "
  84. "section %d reloc %d offset %d size %d\n",
  85. module->name, relindex, i, rel->r_offset,
  86. dstsec->sh_size);
  87. return -ENOEXEC;
  88. }
  89. loc = dstsec->sh_addr + rel->r_offset;
  90. switch (ELF32_R_TYPE(rel->r_info)) {
  91. case R_ARM_NONE:
  92. /* ignore */
  93. break;
  94. case R_ARM_ABS32:
  95. *(u32 *)loc += sym->st_value;
  96. break;
  97. case R_ARM_PC24:
  98. case R_ARM_CALL:
  99. case R_ARM_JUMP24:
  100. offset = (*(u32 *)loc & 0x00ffffff) << 2;
  101. if (offset & 0x02000000)
  102. offset -= 0x04000000;
  103. offset += sym->st_value - loc;
  104. if (offset & 3 ||
  105. offset <= (s32)0xfe000000 ||
  106. offset >= (s32)0x02000000) {
  107. printk(KERN_ERR
  108. "%s: relocation out of range, section "
  109. "%d reloc %d sym '%s'\n", module->name,
  110. relindex, i, strtab + sym->st_name);
  111. return -ENOEXEC;
  112. }
  113. offset >>= 2;
  114. *(u32 *)loc &= 0xff000000;
  115. *(u32 *)loc |= offset & 0x00ffffff;
  116. break;
  117. case R_ARM_V4BX:
  118. /* Preserve Rm and the condition code. Alter
  119. * other bits to re-code instruction as
  120. * MOV PC,Rm.
  121. */
  122. *(u32 *)loc &= 0xf000000f;
  123. *(u32 *)loc |= 0x01a0f000;
  124. break;
  125. case R_ARM_PREL31:
  126. offset = *(u32 *)loc + sym->st_value - loc;
  127. *(u32 *)loc = offset & 0x7fffffff;
  128. break;
  129. case R_ARM_MOVW_ABS_NC:
  130. case R_ARM_MOVT_ABS:
  131. offset = *(u32 *)loc;
  132. offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
  133. offset = (offset ^ 0x8000) - 0x8000;
  134. offset += sym->st_value;
  135. if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_ABS)
  136. offset >>= 16;
  137. *(u32 *)loc &= 0xfff0f000;
  138. *(u32 *)loc |= ((offset & 0xf000) << 4) |
  139. (offset & 0x0fff);
  140. break;
  141. #ifdef CONFIG_THUMB2_KERNEL
  142. case R_ARM_THM_CALL:
  143. case R_ARM_THM_JUMP24:
  144. upper = *(u16 *)loc;
  145. lower = *(u16 *)(loc + 2);
  146. /*
  147. * 25 bit signed address range (Thumb-2 BL and B.W
  148. * instructions):
  149. * S:I1:I2:imm10:imm11:0
  150. * where:
  151. * S = upper[10] = offset[24]
  152. * I1 = ~(J1 ^ S) = offset[23]
  153. * I2 = ~(J2 ^ S) = offset[22]
  154. * imm10 = upper[9:0] = offset[21:12]
  155. * imm11 = lower[10:0] = offset[11:1]
  156. * J1 = lower[13]
  157. * J2 = lower[11]
  158. */
  159. sign = (upper >> 10) & 1;
  160. j1 = (lower >> 13) & 1;
  161. j2 = (lower >> 11) & 1;
  162. offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
  163. ((~(j2 ^ sign) & 1) << 22) |
  164. ((upper & 0x03ff) << 12) |
  165. ((lower & 0x07ff) << 1);
  166. if (offset & 0x01000000)
  167. offset -= 0x02000000;
  168. offset += sym->st_value - loc;
  169. /* only Thumb addresses allowed (no interworking) */
  170. if (!(offset & 1) ||
  171. offset <= (s32)0xff000000 ||
  172. offset >= (s32)0x01000000) {
  173. printk(KERN_ERR
  174. "%s: relocation out of range, section "
  175. "%d reloc %d sym '%s'\n", module->name,
  176. relindex, i, strtab + sym->st_name);
  177. return -ENOEXEC;
  178. }
  179. sign = (offset >> 24) & 1;
  180. j1 = sign ^ (~(offset >> 23) & 1);
  181. j2 = sign ^ (~(offset >> 22) & 1);
  182. *(u16 *)loc = (u16)((upper & 0xf800) | (sign << 10) |
  183. ((offset >> 12) & 0x03ff));
  184. *(u16 *)(loc + 2) = (u16)((lower & 0xd000) |
  185. (j1 << 13) | (j2 << 11) |
  186. ((offset >> 1) & 0x07ff));
  187. break;
  188. case R_ARM_THM_MOVW_ABS_NC:
  189. case R_ARM_THM_MOVT_ABS:
  190. upper = *(u16 *)loc;
  191. lower = *(u16 *)(loc + 2);
  192. /*
  193. * MOVT/MOVW instructions encoding in Thumb-2:
  194. *
  195. * i = upper[10]
  196. * imm4 = upper[3:0]
  197. * imm3 = lower[14:12]
  198. * imm8 = lower[7:0]
  199. *
  200. * imm16 = imm4:i:imm3:imm8
  201. */
  202. offset = ((upper & 0x000f) << 12) |
  203. ((upper & 0x0400) << 1) |
  204. ((lower & 0x7000) >> 4) | (lower & 0x00ff);
  205. offset = (offset ^ 0x8000) - 0x8000;
  206. offset += sym->st_value;
  207. if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS)
  208. offset >>= 16;
  209. *(u16 *)loc = (u16)((upper & 0xfbf0) |
  210. ((offset & 0xf000) >> 12) |
  211. ((offset & 0x0800) >> 1));
  212. *(u16 *)(loc + 2) = (u16)((lower & 0x8f00) |
  213. ((offset & 0x0700) << 4) |
  214. (offset & 0x00ff));
  215. break;
  216. #endif
  217. default:
  218. printk(KERN_ERR "%s: unknown relocation: %u\n",
  219. module->name, ELF32_R_TYPE(rel->r_info));
  220. return -ENOEXEC;
  221. }
  222. }
  223. return 0;
  224. }
  225. int
  226. apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
  227. unsigned int symindex, unsigned int relsec, struct module *module)
  228. {
  229. printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
  230. module->name);
  231. return -ENOEXEC;
  232. }
  233. struct mod_unwind_map {
  234. const Elf_Shdr *unw_sec;
  235. const Elf_Shdr *txt_sec;
  236. };
  237. int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
  238. struct module *mod)
  239. {
  240. #ifdef CONFIG_ARM_UNWIND
  241. const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  242. const Elf_Shdr *s, *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. return 0;
  280. }
  281. void
  282. module_arch_cleanup(struct module *mod)
  283. {
  284. #ifdef CONFIG_ARM_UNWIND
  285. int i;
  286. for (i = 0; i < ARM_SEC_MAX; i++)
  287. if (mod->arch.unwind[i])
  288. unwind_table_del(mod->arch.unwind[i]);
  289. #endif
  290. }