module.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. offset = ELF32_R_SYM(rel->r_info);
  95. if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
  96. printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
  97. module->name, relindex, i);
  98. return -ENOEXEC;
  99. }
  100. sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
  101. if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
  102. printk(KERN_ERR "%s: out of bounds relocation, "
  103. "section %d reloc %d offset %d size %d\n",
  104. module->name, relindex, i, rel->r_offset,
  105. dstsec->sh_size);
  106. return -ENOEXEC;
  107. }
  108. loc = dstsec->sh_addr + rel->r_offset;
  109. switch (ELF32_R_TYPE(rel->r_info)) {
  110. case R_ARM_NONE:
  111. /* ignore */
  112. break;
  113. case R_ARM_ABS32:
  114. *(u32 *)loc += sym->st_value;
  115. break;
  116. case R_ARM_PC24:
  117. case R_ARM_CALL:
  118. case R_ARM_JUMP24:
  119. offset = (*(u32 *)loc & 0x00ffffff) << 2;
  120. if (offset & 0x02000000)
  121. offset -= 0x04000000;
  122. offset += sym->st_value - loc;
  123. if (offset & 3 ||
  124. offset <= (s32)0xfe000000 ||
  125. offset >= (s32)0x02000000) {
  126. printk(KERN_ERR
  127. "%s: relocation out of range, section "
  128. "%d reloc %d sym '%s'\n", module->name,
  129. relindex, i, strtab + sym->st_name);
  130. return -ENOEXEC;
  131. }
  132. offset >>= 2;
  133. *(u32 *)loc &= 0xff000000;
  134. *(u32 *)loc |= offset & 0x00ffffff;
  135. break;
  136. case R_ARM_V4BX:
  137. /* Preserve Rm and the condition code. Alter
  138. * other bits to re-code instruction as
  139. * MOV PC,Rm.
  140. */
  141. *(u32 *)loc &= 0xf000000f;
  142. *(u32 *)loc |= 0x01a0f000;
  143. break;
  144. case R_ARM_PREL31:
  145. offset = *(u32 *)loc + sym->st_value - loc;
  146. *(u32 *)loc = offset & 0x7fffffff;
  147. break;
  148. case R_ARM_MOVW_ABS_NC:
  149. case R_ARM_MOVT_ABS:
  150. offset = *(u32 *)loc;
  151. offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
  152. offset = (offset ^ 0x8000) - 0x8000;
  153. offset += sym->st_value;
  154. if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_ABS)
  155. offset >>= 16;
  156. *(u32 *)loc &= 0xfff0f000;
  157. *(u32 *)loc |= ((offset & 0xf000) << 4) |
  158. (offset & 0x0fff);
  159. break;
  160. default:
  161. printk(KERN_ERR "%s: unknown relocation: %u\n",
  162. module->name, ELF32_R_TYPE(rel->r_info));
  163. return -ENOEXEC;
  164. }
  165. }
  166. return 0;
  167. }
  168. int
  169. apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
  170. unsigned int symindex, unsigned int relsec, struct module *module)
  171. {
  172. printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
  173. module->name);
  174. return -ENOEXEC;
  175. }
  176. #ifdef CONFIG_ARM_UNWIND
  177. static void register_unwind_tables(struct module *mod)
  178. {
  179. if (mod->arch.unw_sec_init && mod->arch.sec_init_text)
  180. mod->arch.unwind_init =
  181. unwind_table_add(mod->arch.unw_sec_init->sh_addr,
  182. mod->arch.unw_sec_init->sh_size,
  183. mod->arch.sec_init_text->sh_addr,
  184. mod->arch.sec_init_text->sh_size);
  185. if (mod->arch.unw_sec_devinit && mod->arch.sec_devinit_text)
  186. mod->arch.unwind_devinit =
  187. unwind_table_add(mod->arch.unw_sec_devinit->sh_addr,
  188. mod->arch.unw_sec_devinit->sh_size,
  189. mod->arch.sec_devinit_text->sh_addr,
  190. mod->arch.sec_devinit_text->sh_size);
  191. if (mod->arch.unw_sec_core && mod->arch.sec_core_text)
  192. mod->arch.unwind_core =
  193. unwind_table_add(mod->arch.unw_sec_core->sh_addr,
  194. mod->arch.unw_sec_core->sh_size,
  195. mod->arch.sec_core_text->sh_addr,
  196. mod->arch.sec_core_text->sh_size);
  197. }
  198. static void unregister_unwind_tables(struct module *mod)
  199. {
  200. unwind_table_del(mod->arch.unwind_init);
  201. unwind_table_del(mod->arch.unwind_devinit);
  202. unwind_table_del(mod->arch.unwind_core);
  203. }
  204. #else
  205. static inline void register_unwind_tables(struct module *mod) { }
  206. static inline void unregister_unwind_tables(struct module *mod) { }
  207. #endif
  208. int
  209. module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
  210. struct module *module)
  211. {
  212. register_unwind_tables(module);
  213. return 0;
  214. }
  215. void
  216. module_arch_cleanup(struct module *mod)
  217. {
  218. unregister_unwind_tables(mod);
  219. }