module.c 4.3 KB

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