module.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <linux/gfp.h>
  24. #include <asm/system.h>
  25. #include <asm/page.h>
  26. #include <asm/pgtable.h>
  27. #if 0
  28. #define DEBUGP printk
  29. #else
  30. #define DEBUGP(fmt...)
  31. #endif
  32. void *module_alloc(unsigned long size)
  33. {
  34. struct vm_struct *area;
  35. if (!size)
  36. return NULL;
  37. size = PAGE_ALIGN(size);
  38. if (size > MODULES_LEN)
  39. return NULL;
  40. area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
  41. if (!area)
  42. return NULL;
  43. return __vmalloc_area(area, GFP_KERNEL | __GFP_HIGHMEM,
  44. PAGE_KERNEL_EXEC);
  45. }
  46. /* Free memory returned from module_alloc */
  47. void module_free(struct module *mod, void *module_region)
  48. {
  49. vfree(module_region);
  50. }
  51. /* We don't need anything special. */
  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. #ifdef CONFIG_X86_32
  60. int apply_relocate(Elf32_Shdr *sechdrs,
  61. const char *strtab,
  62. unsigned int symindex,
  63. unsigned int relsec,
  64. struct module *me)
  65. {
  66. unsigned int i;
  67. Elf32_Rel *rel = (void *)sechdrs[relsec].sh_addr;
  68. Elf32_Sym *sym;
  69. uint32_t *location;
  70. DEBUGP("Applying relocate section %u to %u\n", relsec,
  71. sechdrs[relsec].sh_info);
  72. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  73. /* This is where to make the change */
  74. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  75. + rel[i].r_offset;
  76. /* This is the symbol it is referring to. Note that all
  77. undefined symbols have been resolved. */
  78. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  79. + ELF32_R_SYM(rel[i].r_info);
  80. switch (ELF32_R_TYPE(rel[i].r_info)) {
  81. case R_386_32:
  82. /* We add the value into the location given */
  83. *location += sym->st_value;
  84. break;
  85. case R_386_PC32:
  86. /* Add the value, subtract its postition */
  87. *location += sym->st_value - (uint32_t)location;
  88. break;
  89. default:
  90. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  91. me->name, ELF32_R_TYPE(rel[i].r_info));
  92. return -ENOEXEC;
  93. }
  94. }
  95. return 0;
  96. }
  97. int apply_relocate_add(Elf32_Shdr *sechdrs,
  98. const char *strtab,
  99. unsigned int symindex,
  100. unsigned int relsec,
  101. struct module *me)
  102. {
  103. printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
  104. me->name);
  105. return -ENOEXEC;
  106. }
  107. #else /*X86_64*/
  108. int apply_relocate_add(Elf64_Shdr *sechdrs,
  109. const char *strtab,
  110. unsigned int symindex,
  111. unsigned int relsec,
  112. struct module *me)
  113. {
  114. unsigned int i;
  115. Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  116. Elf64_Sym *sym;
  117. void *loc;
  118. u64 val;
  119. DEBUGP("Applying relocate section %u to %u\n", relsec,
  120. sechdrs[relsec].sh_info);
  121. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  122. /* This is where to make the change */
  123. loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  124. + rel[i].r_offset;
  125. /* This is the symbol it is referring to. Note that all
  126. undefined symbols have been resolved. */
  127. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
  128. + ELF64_R_SYM(rel[i].r_info);
  129. DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
  130. (int)ELF64_R_TYPE(rel[i].r_info),
  131. sym->st_value, rel[i].r_addend, (u64)loc);
  132. val = sym->st_value + rel[i].r_addend;
  133. switch (ELF64_R_TYPE(rel[i].r_info)) {
  134. case R_X86_64_NONE:
  135. break;
  136. case R_X86_64_64:
  137. *(u64 *)loc = val;
  138. break;
  139. case R_X86_64_32:
  140. *(u32 *)loc = val;
  141. if (val != *(u32 *)loc)
  142. goto overflow;
  143. break;
  144. case R_X86_64_32S:
  145. *(s32 *)loc = val;
  146. if ((s64)val != *(s32 *)loc)
  147. goto overflow;
  148. break;
  149. case R_X86_64_PC32:
  150. val -= (u64)loc;
  151. *(u32 *)loc = val;
  152. #if 0
  153. if ((s64)val != *(s32 *)loc)
  154. goto overflow;
  155. #endif
  156. break;
  157. default:
  158. printk(KERN_ERR "module %s: Unknown rela relocation: %llu\n",
  159. me->name, ELF64_R_TYPE(rel[i].r_info));
  160. return -ENOEXEC;
  161. }
  162. }
  163. return 0;
  164. overflow:
  165. printk(KERN_ERR "overflow in relocation type %d val %Lx\n",
  166. (int)ELF64_R_TYPE(rel[i].r_info), val);
  167. printk(KERN_ERR "`%s' likely not compiled with -mcmodel=kernel\n",
  168. me->name);
  169. return -ENOEXEC;
  170. }
  171. int apply_relocate(Elf_Shdr *sechdrs,
  172. const char *strtab,
  173. unsigned int symindex,
  174. unsigned int relsec,
  175. struct module *me)
  176. {
  177. printk(KERN_ERR "non add relocation not supported\n");
  178. return -ENOSYS;
  179. }
  180. #endif
  181. int module_finalize(const Elf_Ehdr *hdr,
  182. const Elf_Shdr *sechdrs,
  183. struct module *me)
  184. {
  185. const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL,
  186. *para = NULL;
  187. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  188. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  189. if (!strcmp(".text", secstrings + s->sh_name))
  190. text = s;
  191. if (!strcmp(".altinstructions", secstrings + s->sh_name))
  192. alt = s;
  193. if (!strcmp(".smp_locks", secstrings + s->sh_name))
  194. locks = s;
  195. if (!strcmp(".parainstructions", secstrings + s->sh_name))
  196. para = s;
  197. }
  198. if (alt) {
  199. /* patch .altinstructions */
  200. void *aseg = (void *)alt->sh_addr;
  201. apply_alternatives(aseg, aseg + alt->sh_size);
  202. }
  203. if (locks && text) {
  204. void *lseg = (void *)locks->sh_addr;
  205. void *tseg = (void *)text->sh_addr;
  206. alternatives_smp_module_add(me, me->name,
  207. lseg, lseg + locks->sh_size,
  208. tseg, tseg + text->sh_size);
  209. }
  210. if (para) {
  211. void *pseg = (void *)para->sh_addr;
  212. apply_paravirt(pseg, pseg + para->sh_size);
  213. }
  214. return module_bug_finalize(hdr, sechdrs, me);
  215. }
  216. void module_arch_cleanup(struct module *mod)
  217. {
  218. alternatives_smp_module_del(mod);
  219. module_bug_cleanup(mod);
  220. }