module_64.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/slab.h>
  23. #include <linux/bug.h>
  24. #include <asm/system.h>
  25. #include <asm/page.h>
  26. #include <asm/pgtable.h>
  27. #define DEBUGP(fmt...)
  28. #ifndef CONFIG_UML
  29. void module_free(struct module *mod, void *module_region)
  30. {
  31. vfree(module_region);
  32. /* FIXME: If module_region == mod->init_region, trim exception
  33. table entries. */
  34. }
  35. void *module_alloc(unsigned long size)
  36. {
  37. struct vm_struct *area;
  38. if (!size)
  39. return NULL;
  40. size = PAGE_ALIGN(size);
  41. if (size > MODULES_LEN)
  42. return NULL;
  43. area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
  44. if (!area)
  45. return NULL;
  46. return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC);
  47. }
  48. #endif
  49. /* We don't need anything special. */
  50. int module_frob_arch_sections(Elf_Ehdr *hdr,
  51. Elf_Shdr *sechdrs,
  52. char *secstrings,
  53. struct module *mod)
  54. {
  55. return 0;
  56. }
  57. int apply_relocate_add(Elf64_Shdr *sechdrs,
  58. const char *strtab,
  59. unsigned int symindex,
  60. unsigned int relsec,
  61. struct module *me)
  62. {
  63. unsigned int i;
  64. Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  65. Elf64_Sym *sym;
  66. void *loc;
  67. u64 val;
  68. DEBUGP("Applying relocate section %u to %u\n", relsec,
  69. sechdrs[relsec].sh_info);
  70. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  71. /* This is where to make the change */
  72. loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  73. + rel[i].r_offset;
  74. /* This is the symbol it is referring to. Note that all
  75. undefined symbols have been resolved. */
  76. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
  77. + ELF64_R_SYM(rel[i].r_info);
  78. DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
  79. (int)ELF64_R_TYPE(rel[i].r_info),
  80. sym->st_value, rel[i].r_addend, (u64)loc);
  81. val = sym->st_value + rel[i].r_addend;
  82. switch (ELF64_R_TYPE(rel[i].r_info)) {
  83. case R_X86_64_NONE:
  84. break;
  85. case R_X86_64_64:
  86. *(u64 *)loc = val;
  87. break;
  88. case R_X86_64_32:
  89. *(u32 *)loc = val;
  90. if (val != *(u32 *)loc)
  91. goto overflow;
  92. break;
  93. case R_X86_64_32S:
  94. *(s32 *)loc = val;
  95. if ((s64)val != *(s32 *)loc)
  96. goto overflow;
  97. break;
  98. case R_X86_64_PC32:
  99. val -= (u64)loc;
  100. *(u32 *)loc = val;
  101. #if 0
  102. if ((s64)val != *(s32 *)loc)
  103. goto overflow;
  104. #endif
  105. break;
  106. default:
  107. printk(KERN_ERR "module %s: Unknown rela relocation: %Lu\n",
  108. me->name, ELF64_R_TYPE(rel[i].r_info));
  109. return -ENOEXEC;
  110. }
  111. }
  112. return 0;
  113. overflow:
  114. printk(KERN_ERR "overflow in relocation type %d val %Lx\n",
  115. (int)ELF64_R_TYPE(rel[i].r_info), val);
  116. printk(KERN_ERR "`%s' likely not compiled with -mcmodel=kernel\n",
  117. me->name);
  118. return -ENOEXEC;
  119. }
  120. int apply_relocate(Elf_Shdr *sechdrs,
  121. const char *strtab,
  122. unsigned int symindex,
  123. unsigned int relsec,
  124. struct module *me)
  125. {
  126. printk("non add relocation not supported\n");
  127. return -ENOSYS;
  128. }
  129. int module_finalize(const Elf_Ehdr *hdr,
  130. const Elf_Shdr *sechdrs,
  131. struct module *me)
  132. {
  133. const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL;
  134. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  135. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  136. if (!strcmp(".text", secstrings + s->sh_name))
  137. text = s;
  138. if (!strcmp(".altinstructions", secstrings + s->sh_name))
  139. alt = s;
  140. if (!strcmp(".smp_locks", secstrings + s->sh_name))
  141. locks= s;
  142. }
  143. if (alt) {
  144. /* patch .altinstructions */
  145. void *aseg = (void *)alt->sh_addr;
  146. apply_alternatives(aseg, aseg + alt->sh_size);
  147. }
  148. if (locks && text) {
  149. void *lseg = (void *)locks->sh_addr;
  150. void *tseg = (void *)text->sh_addr;
  151. alternatives_smp_module_add(me, me->name,
  152. lseg, lseg + locks->sh_size,
  153. tseg, tseg + text->sh_size);
  154. }
  155. return module_bug_finalize(hdr, sechdrs, me);
  156. }
  157. void module_arch_cleanup(struct module *mod)
  158. {
  159. alternatives_smp_module_del(mod);
  160. module_bug_cleanup(mod);
  161. }