module.c 5.6 KB

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