module.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* Kernel module help for x86.
  2. Copyright (C) 2001 Rusty Russell.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include <linux/moduleloader.h>
  16. #include <linux/elf.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/fs.h>
  19. #include <linux/string.h>
  20. #include <linux/kernel.h>
  21. #include <linux/bug.h>
  22. #include <linux/mm.h>
  23. #include <asm/system.h>
  24. #include <asm/page.h>
  25. #include <asm/pgtable.h>
  26. #if 0
  27. #define DEBUGP printk
  28. #else
  29. #define DEBUGP(fmt...)
  30. #endif
  31. void *module_alloc(unsigned long size)
  32. {
  33. struct vm_struct *area;
  34. if (!size)
  35. return NULL;
  36. size = PAGE_ALIGN(size);
  37. if (size > MODULES_LEN)
  38. return NULL;
  39. area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
  40. if (!area)
  41. return NULL;
  42. return __vmalloc_area(area, GFP_KERNEL | __GFP_HIGHMEM,
  43. PAGE_KERNEL_EXEC);
  44. }
  45. /* Free memory returned from module_alloc */
  46. void module_free(struct module *mod, void *module_region)
  47. {
  48. vfree(module_region);
  49. /* FIXME: If module_region == mod->init_region, trim exception
  50. table entries. */
  51. }
  52. /* We don't need anything special. */
  53. int module_frob_arch_sections(Elf_Ehdr *hdr,
  54. Elf_Shdr *sechdrs,
  55. char *secstrings,
  56. struct module *mod)
  57. {
  58. return 0;
  59. }
  60. #ifdef CONFIG_X86_32
  61. int apply_relocate(Elf32_Shdr *sechdrs,
  62. const char *strtab,
  63. unsigned int symindex,
  64. unsigned int relsec,
  65. struct module *me)
  66. {
  67. unsigned int i;
  68. Elf32_Rel *rel = (void *)sechdrs[relsec].sh_addr;
  69. Elf32_Sym *sym;
  70. uint32_t *location;
  71. DEBUGP("Applying relocate section %u to %u\n", relsec,
  72. sechdrs[relsec].sh_info);
  73. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  74. /* This is where to make the change */
  75. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  76. + rel[i].r_offset;
  77. /* This is the symbol it is referring to. Note that all
  78. undefined symbols have been resolved. */
  79. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  80. + ELF32_R_SYM(rel[i].r_info);
  81. switch (ELF32_R_TYPE(rel[i].r_info)) {
  82. case R_386_32:
  83. /* We add the value into the location given */
  84. *location += sym->st_value;
  85. break;
  86. case R_386_PC32:
  87. /* Add the value, subtract its postition */
  88. *location += sym->st_value - (uint32_t)location;
  89. break;
  90. default:
  91. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  92. me->name, ELF32_R_TYPE(rel[i].r_info));
  93. return -ENOEXEC;
  94. }
  95. }
  96. return 0;
  97. }
  98. int apply_relocate_add(Elf32_Shdr *sechdrs,
  99. const char *strtab,
  100. unsigned int symindex,
  101. unsigned int relsec,
  102. struct module *me)
  103. {
  104. printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
  105. me->name);
  106. return -ENOEXEC;
  107. }
  108. #else /*X86_64*/
  109. int apply_relocate_add(Elf64_Shdr *sechdrs,
  110. const char *strtab,
  111. unsigned int symindex,
  112. unsigned int relsec,
  113. struct module *me)
  114. {
  115. unsigned int i;
  116. Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  117. Elf64_Sym *sym;
  118. void *loc;
  119. u64 val;
  120. DEBUGP("Applying relocate section %u to %u\n", relsec,
  121. sechdrs[relsec].sh_info);
  122. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  123. /* This is where to make the change */
  124. loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  125. + rel[i].r_offset;
  126. /* This is the symbol it is referring to. Note that all
  127. undefined symbols have been resolved. */
  128. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
  129. + ELF64_R_SYM(rel[i].r_info);
  130. DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
  131. (int)ELF64_R_TYPE(rel[i].r_info),
  132. sym->st_value, rel[i].r_addend, (u64)loc);
  133. val = sym->st_value + rel[i].r_addend;
  134. switch (ELF64_R_TYPE(rel[i].r_info)) {
  135. case R_X86_64_NONE:
  136. break;
  137. case R_X86_64_64:
  138. *(u64 *)loc = val;
  139. break;
  140. case R_X86_64_32:
  141. *(u32 *)loc = val;
  142. if (val != *(u32 *)loc)
  143. goto overflow;
  144. break;
  145. case R_X86_64_32S:
  146. *(s32 *)loc = val;
  147. if ((s64)val != *(s32 *)loc)
  148. goto overflow;
  149. break;
  150. case R_X86_64_PC32:
  151. val -= (u64)loc;
  152. *(u32 *)loc = val;
  153. #if 0
  154. if ((s64)val != *(s32 *)loc)
  155. goto overflow;
  156. #endif
  157. break;
  158. default:
  159. printk(KERN_ERR "module %s: Unknown rela relocation: %llu\n",
  160. me->name, ELF64_R_TYPE(rel[i].r_info));
  161. return -ENOEXEC;
  162. }
  163. }
  164. return 0;
  165. overflow:
  166. printk(KERN_ERR "overflow in relocation type %d val %Lx\n",
  167. (int)ELF64_R_TYPE(rel[i].r_info), val);
  168. printk(KERN_ERR "`%s' likely not compiled with -mcmodel=kernel\n",
  169. me->name);
  170. return -ENOEXEC;
  171. }
  172. int apply_relocate(Elf_Shdr *sechdrs,
  173. const char *strtab,
  174. unsigned int symindex,
  175. unsigned int relsec,
  176. struct module *me)
  177. {
  178. printk(KERN_ERR "non add relocation not supported\n");
  179. return -ENOSYS;
  180. }
  181. #endif
  182. int module_finalize(const Elf_Ehdr *hdr,
  183. const Elf_Shdr *sechdrs,
  184. struct module *me)
  185. {
  186. const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL,
  187. *para = NULL;
  188. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  189. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  190. if (!strcmp(".text", secstrings + s->sh_name))
  191. text = s;
  192. if (!strcmp(".altinstructions", secstrings + s->sh_name))
  193. alt = s;
  194. if (!strcmp(".smp_locks", secstrings + s->sh_name))
  195. locks = s;
  196. if (!strcmp(".parainstructions", secstrings + s->sh_name))
  197. para = s;
  198. }
  199. if (alt) {
  200. /* patch .altinstructions */
  201. void *aseg = (void *)alt->sh_addr;
  202. apply_alternatives(aseg, aseg + alt->sh_size);
  203. }
  204. if (locks && text) {
  205. void *lseg = (void *)locks->sh_addr;
  206. void *tseg = (void *)text->sh_addr;
  207. alternatives_smp_module_add(me, me->name,
  208. lseg, lseg + locks->sh_size,
  209. tseg, tseg + text->sh_size);
  210. }
  211. if (para) {
  212. void *pseg = (void *)para->sh_addr;
  213. apply_paravirt(pseg, pseg + para->sh_size);
  214. }
  215. return module_bug_finalize(hdr, sechdrs, me);
  216. }
  217. void module_arch_cleanup(struct module *mod)
  218. {
  219. alternatives_smp_module_del(mod);
  220. module_bug_cleanup(mod);
  221. }