module.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <linux/gfp.h>
  24. #include <linux/jump_label.h>
  25. #include <asm/page.h>
  26. #include <asm/pgtable.h>
  27. #if 0
  28. #define DEBUGP printk
  29. #else
  30. #define DEBUGP(fmt...)
  31. #endif
  32. void *module_alloc(unsigned long size)
  33. {
  34. if (PAGE_ALIGN(size) > MODULES_LEN)
  35. return NULL;
  36. return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
  37. GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
  38. -1, __builtin_return_address(0));
  39. }
  40. #ifdef CONFIG_X86_32
  41. int apply_relocate(Elf32_Shdr *sechdrs,
  42. const char *strtab,
  43. unsigned int symindex,
  44. unsigned int relsec,
  45. struct module *me)
  46. {
  47. unsigned int i;
  48. Elf32_Rel *rel = (void *)sechdrs[relsec].sh_addr;
  49. Elf32_Sym *sym;
  50. uint32_t *location;
  51. DEBUGP("Applying relocate section %u to %u\n", relsec,
  52. sechdrs[relsec].sh_info);
  53. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  54. /* This is where to make the change */
  55. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  56. + rel[i].r_offset;
  57. /* This is the symbol it is referring to. Note that all
  58. undefined symbols have been resolved. */
  59. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  60. + ELF32_R_SYM(rel[i].r_info);
  61. switch (ELF32_R_TYPE(rel[i].r_info)) {
  62. case R_386_32:
  63. /* We add the value into the location given */
  64. *location += sym->st_value;
  65. break;
  66. case R_386_PC32:
  67. /* Add the value, subtract its postition */
  68. *location += sym->st_value - (uint32_t)location;
  69. break;
  70. default:
  71. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  72. me->name, ELF32_R_TYPE(rel[i].r_info));
  73. return -ENOEXEC;
  74. }
  75. }
  76. return 0;
  77. }
  78. #else /*X86_64*/
  79. int apply_relocate_add(Elf64_Shdr *sechdrs,
  80. const char *strtab,
  81. unsigned int symindex,
  82. unsigned int relsec,
  83. struct module *me)
  84. {
  85. unsigned int i;
  86. Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  87. Elf64_Sym *sym;
  88. void *loc;
  89. u64 val;
  90. DEBUGP("Applying relocate section %u to %u\n", relsec,
  91. sechdrs[relsec].sh_info);
  92. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  93. /* This is where to make the change */
  94. loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  95. + rel[i].r_offset;
  96. /* This is the symbol it is referring to. Note that all
  97. undefined symbols have been resolved. */
  98. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
  99. + ELF64_R_SYM(rel[i].r_info);
  100. DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
  101. (int)ELF64_R_TYPE(rel[i].r_info),
  102. sym->st_value, rel[i].r_addend, (u64)loc);
  103. val = sym->st_value + rel[i].r_addend;
  104. switch (ELF64_R_TYPE(rel[i].r_info)) {
  105. case R_X86_64_NONE:
  106. break;
  107. case R_X86_64_64:
  108. *(u64 *)loc = val;
  109. break;
  110. case R_X86_64_32:
  111. *(u32 *)loc = val;
  112. if (val != *(u32 *)loc)
  113. goto overflow;
  114. break;
  115. case R_X86_64_32S:
  116. *(s32 *)loc = val;
  117. if ((s64)val != *(s32 *)loc)
  118. goto overflow;
  119. break;
  120. case R_X86_64_PC32:
  121. val -= (u64)loc;
  122. *(u32 *)loc = val;
  123. #if 0
  124. if ((s64)val != *(s32 *)loc)
  125. goto overflow;
  126. #endif
  127. break;
  128. default:
  129. printk(KERN_ERR "module %s: Unknown rela relocation: %llu\n",
  130. me->name, ELF64_R_TYPE(rel[i].r_info));
  131. return -ENOEXEC;
  132. }
  133. }
  134. return 0;
  135. overflow:
  136. printk(KERN_ERR "overflow in relocation type %d val %Lx\n",
  137. (int)ELF64_R_TYPE(rel[i].r_info), val);
  138. printk(KERN_ERR "`%s' likely not compiled with -mcmodel=kernel\n",
  139. me->name);
  140. return -ENOEXEC;
  141. }
  142. #endif
  143. int module_finalize(const Elf_Ehdr *hdr,
  144. const Elf_Shdr *sechdrs,
  145. struct module *me)
  146. {
  147. const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL,
  148. *para = NULL;
  149. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  150. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  151. if (!strcmp(".text", secstrings + s->sh_name))
  152. text = s;
  153. if (!strcmp(".altinstructions", secstrings + s->sh_name))
  154. alt = s;
  155. if (!strcmp(".smp_locks", secstrings + s->sh_name))
  156. locks = s;
  157. if (!strcmp(".parainstructions", secstrings + s->sh_name))
  158. para = s;
  159. }
  160. if (alt) {
  161. /* patch .altinstructions */
  162. void *aseg = (void *)alt->sh_addr;
  163. apply_alternatives(aseg, aseg + alt->sh_size);
  164. }
  165. if (locks && text) {
  166. void *lseg = (void *)locks->sh_addr;
  167. void *tseg = (void *)text->sh_addr;
  168. alternatives_smp_module_add(me, me->name,
  169. lseg, lseg + locks->sh_size,
  170. tseg, tseg + text->sh_size);
  171. }
  172. if (para) {
  173. void *pseg = (void *)para->sh_addr;
  174. apply_paravirt(pseg, pseg + para->sh_size);
  175. }
  176. /* make jump label nops */
  177. jump_label_apply_nops(me);
  178. return 0;
  179. }
  180. void module_arch_cleanup(struct module *mod)
  181. {
  182. alternatives_smp_module_del(mod);
  183. }