module_64.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_alloc(unsigned long size)
  31. {
  32. struct vm_struct *area;
  33. if (!size)
  34. return NULL;
  35. size = PAGE_ALIGN(size);
  36. if (size > MODULES_LEN)
  37. return NULL;
  38. area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
  39. if (!area)
  40. return NULL;
  41. return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC);
  42. }
  43. #endif
  44. int apply_relocate_add(Elf64_Shdr *sechdrs,
  45. const char *strtab,
  46. unsigned int symindex,
  47. unsigned int relsec,
  48. struct module *me)
  49. {
  50. unsigned int i;
  51. Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  52. Elf64_Sym *sym;
  53. void *loc;
  54. u64 val;
  55. DEBUGP("Applying relocate section %u to %u\n", relsec,
  56. sechdrs[relsec].sh_info);
  57. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  58. /* This is where to make the change */
  59. loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  60. + rel[i].r_offset;
  61. /* This is the symbol it is referring to. Note that all
  62. undefined symbols have been resolved. */
  63. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
  64. + ELF64_R_SYM(rel[i].r_info);
  65. DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
  66. (int)ELF64_R_TYPE(rel[i].r_info),
  67. sym->st_value, rel[i].r_addend, (u64)loc);
  68. val = sym->st_value + rel[i].r_addend;
  69. switch (ELF64_R_TYPE(rel[i].r_info)) {
  70. case R_X86_64_NONE:
  71. break;
  72. case R_X86_64_64:
  73. *(u64 *)loc = val;
  74. break;
  75. case R_X86_64_32:
  76. *(u32 *)loc = val;
  77. if (val != *(u32 *)loc)
  78. goto overflow;
  79. break;
  80. case R_X86_64_32S:
  81. *(s32 *)loc = val;
  82. if ((s64)val != *(s32 *)loc)
  83. goto overflow;
  84. break;
  85. case R_X86_64_PC32:
  86. val -= (u64)loc;
  87. *(u32 *)loc = val;
  88. #if 0
  89. if ((s64)val != *(s32 *)loc)
  90. goto overflow;
  91. #endif
  92. break;
  93. default:
  94. printk(KERN_ERR "module %s: Unknown rela relocation: %llu\n",
  95. me->name, ELF64_R_TYPE(rel[i].r_info));
  96. return -ENOEXEC;
  97. }
  98. }
  99. return 0;
  100. overflow:
  101. printk(KERN_ERR "overflow in relocation type %d val %Lx\n",
  102. (int)ELF64_R_TYPE(rel[i].r_info), val);
  103. printk(KERN_ERR "`%s' likely not compiled with -mcmodel=kernel\n",
  104. me->name);
  105. return -ENOEXEC;
  106. }
  107. int apply_relocate(Elf_Shdr *sechdrs,
  108. const char *strtab,
  109. unsigned int symindex,
  110. unsigned int relsec,
  111. struct module *me)
  112. {
  113. printk(KERN_ERR "non add relocation not supported\n");
  114. return -ENOSYS;
  115. }