module.c 6.3 KB

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