module_32.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Kernel module help for i386.
  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. #if 0
  23. #define DEBUGP printk
  24. #else
  25. #define DEBUGP(fmt...)
  26. #endif
  27. void *module_alloc(unsigned long size)
  28. {
  29. if (size == 0)
  30. return NULL;
  31. return vmalloc_exec(size);
  32. }
  33. /* Free memory returned from module_alloc */
  34. void module_free(struct module *mod, void *module_region)
  35. {
  36. vfree(module_region);
  37. /* FIXME: If module_region == mod->init_region, trim exception
  38. table entries. */
  39. }
  40. /* We don't need anything special. */
  41. int module_frob_arch_sections(Elf_Ehdr *hdr,
  42. Elf_Shdr *sechdrs,
  43. char *secstrings,
  44. struct module *mod)
  45. {
  46. return 0;
  47. }
  48. int apply_relocate(Elf32_Shdr *sechdrs,
  49. const char *strtab,
  50. unsigned int symindex,
  51. unsigned int relsec,
  52. struct module *me)
  53. {
  54. unsigned int i;
  55. Elf32_Rel *rel = (void *)sechdrs[relsec].sh_addr;
  56. Elf32_Sym *sym;
  57. uint32_t *location;
  58. DEBUGP("Applying relocate section %u to %u\n", relsec,
  59. sechdrs[relsec].sh_info);
  60. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  61. /* This is where to make the change */
  62. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  63. + rel[i].r_offset;
  64. /* This is the symbol it is referring to. Note that all
  65. undefined symbols have been resolved. */
  66. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  67. + ELF32_R_SYM(rel[i].r_info);
  68. switch (ELF32_R_TYPE(rel[i].r_info)) {
  69. case R_386_32:
  70. /* We add the value into the location given */
  71. *location += sym->st_value;
  72. break;
  73. case R_386_PC32:
  74. /* Add the value, subtract its postition */
  75. *location += sym->st_value - (uint32_t)location;
  76. break;
  77. default:
  78. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  79. me->name, ELF32_R_TYPE(rel[i].r_info));
  80. return -ENOEXEC;
  81. }
  82. }
  83. return 0;
  84. }
  85. int apply_relocate_add(Elf32_Shdr *sechdrs,
  86. const char *strtab,
  87. unsigned int symindex,
  88. unsigned int relsec,
  89. struct module *me)
  90. {
  91. printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
  92. me->name);
  93. return -ENOEXEC;
  94. }
  95. int module_finalize(const Elf_Ehdr *hdr,
  96. const Elf_Shdr *sechdrs,
  97. struct module *me)
  98. {
  99. const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL,
  100. *para = NULL;
  101. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  102. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  103. if (!strcmp(".text", secstrings + s->sh_name))
  104. text = s;
  105. if (!strcmp(".altinstructions", secstrings + s->sh_name))
  106. alt = s;
  107. if (!strcmp(".smp_locks", secstrings + s->sh_name))
  108. locks = s;
  109. if (!strcmp(".parainstructions", secstrings + s->sh_name))
  110. para = s;
  111. }
  112. if (alt) {
  113. /* patch .altinstructions */
  114. void *aseg = (void *)alt->sh_addr;
  115. apply_alternatives(aseg, aseg + alt->sh_size);
  116. }
  117. if (locks && text) {
  118. void *lseg = (void *)locks->sh_addr;
  119. void *tseg = (void *)text->sh_addr;
  120. alternatives_smp_module_add(me, me->name,
  121. lseg, lseg + locks->sh_size,
  122. tseg, tseg + text->sh_size);
  123. }
  124. if (para) {
  125. void *pseg = (void *)para->sh_addr;
  126. apply_paravirt(pseg, pseg + para->sh_size);
  127. }
  128. return module_bug_finalize(hdr, sechdrs, me);
  129. }
  130. void module_arch_cleanup(struct module *mod)
  131. {
  132. alternatives_smp_module_del(mod);
  133. module_bug_cleanup(mod);
  134. }