module.c 6.2 KB

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