module_64.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* Kernel module help for x86-64
  2. Copyright (C) 2001 Rusty Russell.
  3. Copyright (C) 2002,2003 Andi Kleen, SuSE Labs.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <linux/moduleloader.h>
  17. #include <linux/elf.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/fs.h>
  20. #include <linux/string.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/slab.h>
  24. #include <linux/bug.h>
  25. #include <asm/system.h>
  26. #include <asm/page.h>
  27. #include <asm/pgtable.h>
  28. #define DEBUGP(fmt...)
  29. #ifndef CONFIG_UML
  30. void module_free(struct module *mod, void *module_region)
  31. {
  32. vfree(module_region);
  33. /* FIXME: If module_region == mod->init_region, trim exception
  34. table entries. */
  35. }
  36. void *module_alloc(unsigned long size)
  37. {
  38. struct vm_struct *area;
  39. if (!size)
  40. return NULL;
  41. size = PAGE_ALIGN(size);
  42. if (size > MODULES_LEN)
  43. return NULL;
  44. area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
  45. if (!area)
  46. return NULL;
  47. return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC);
  48. }
  49. #endif
  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. int apply_relocate_add(Elf64_Shdr *sechdrs,
  59. const char *strtab,
  60. unsigned int symindex,
  61. unsigned int relsec,
  62. struct module *me)
  63. {
  64. unsigned int i;
  65. Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  66. Elf64_Sym *sym;
  67. void *loc;
  68. u64 val;
  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. loc = (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 = (Elf64_Sym *)sechdrs[symindex].sh_addr
  78. + ELF64_R_SYM(rel[i].r_info);
  79. DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
  80. (int)ELF64_R_TYPE(rel[i].r_info),
  81. sym->st_value, rel[i].r_addend, (u64)loc);
  82. val = sym->st_value + rel[i].r_addend;
  83. switch (ELF64_R_TYPE(rel[i].r_info)) {
  84. case R_X86_64_NONE:
  85. break;
  86. case R_X86_64_64:
  87. *(u64 *)loc = val;
  88. break;
  89. case R_X86_64_32:
  90. *(u32 *)loc = val;
  91. if (val != *(u32 *)loc)
  92. goto overflow;
  93. break;
  94. case R_X86_64_32S:
  95. *(s32 *)loc = val;
  96. if ((s64)val != *(s32 *)loc)
  97. goto overflow;
  98. break;
  99. case R_X86_64_PC32:
  100. val -= (u64)loc;
  101. *(u32 *)loc = val;
  102. #if 0
  103. if ((s64)val != *(s32 *)loc)
  104. goto overflow;
  105. #endif
  106. break;
  107. default:
  108. printk(KERN_ERR "module %s: Unknown rela relocation: %llu\n",
  109. me->name, ELF64_R_TYPE(rel[i].r_info));
  110. return -ENOEXEC;
  111. }
  112. }
  113. return 0;
  114. overflow:
  115. printk(KERN_ERR "overflow in relocation type %d val %Lx\n",
  116. (int)ELF64_R_TYPE(rel[i].r_info), val);
  117. printk(KERN_ERR "`%s' likely not compiled with -mcmodel=kernel\n",
  118. me->name);
  119. return -ENOEXEC;
  120. }
  121. int apply_relocate(Elf_Shdr *sechdrs,
  122. const char *strtab,
  123. unsigned int symindex,
  124. unsigned int relsec,
  125. struct module *me)
  126. {
  127. printk(KERN_ERR "non add relocation not supported\n");
  128. return -ENOSYS;
  129. }
  130. int module_finalize(const Elf_Ehdr *hdr,
  131. const Elf_Shdr *sechdrs,
  132. struct module *me)
  133. {
  134. const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL,
  135. *para = NULL;
  136. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  137. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  138. if (!strcmp(".text", secstrings + s->sh_name))
  139. text = s;
  140. if (!strcmp(".altinstructions", secstrings + s->sh_name))
  141. alt = s;
  142. if (!strcmp(".smp_locks", secstrings + s->sh_name))
  143. locks = s;
  144. if (!strcmp(".parainstructions", secstrings + s->sh_name))
  145. para = s;
  146. }
  147. if (alt) {
  148. /* patch .altinstructions */
  149. void *aseg = (void *)alt->sh_addr;
  150. apply_alternatives(aseg, aseg + alt->sh_size);
  151. }
  152. if (locks && text) {
  153. void *lseg = (void *)locks->sh_addr;
  154. void *tseg = (void *)text->sh_addr;
  155. alternatives_smp_module_add(me, me->name,
  156. lseg, lseg + locks->sh_size,
  157. tseg, tseg + text->sh_size);
  158. }
  159. if (para) {
  160. void *pseg = (void *)para->sh_addr;
  161. apply_paravirt(pseg, pseg + para->sh_size);
  162. }
  163. return module_bug_finalize(hdr, sechdrs, me);
  164. }
  165. void module_arch_cleanup(struct module *mod)
  166. {
  167. alternatives_smp_module_del(mod);
  168. module_bug_cleanup(mod);
  169. }