module.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #if 0
  22. #define DEBUGP printk
  23. #else
  24. #define DEBUGP(fmt , ...)
  25. #endif
  26. void *module_alloc(unsigned long size)
  27. {
  28. if (size == 0)
  29. return NULL;
  30. return vmalloc(size);
  31. }
  32. /* Free memory returned from module_alloc */
  33. void module_free(struct module *mod, void *module_region)
  34. {
  35. vfree(module_region);
  36. /* FIXME: If module_region == mod->init_region, trim exception
  37. table entries. */
  38. }
  39. /* We don't need anything special. */
  40. int module_frob_arch_sections(Elf_Ehdr *hdr,
  41. Elf_Shdr *sechdrs,
  42. char *secstrings,
  43. struct module *mod)
  44. {
  45. return 0;
  46. }
  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", relsec,
  58. 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_offset
  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. /* We add the value into the location given */
  68. *location += sym->st_value;
  69. }
  70. return 0;
  71. }
  72. int apply_relocate_add(Elf32_Shdr *sechdrs,
  73. const char *strtab,
  74. unsigned int symindex,
  75. unsigned int relsec,
  76. struct module *me)
  77. {
  78. unsigned int i;
  79. Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  80. DEBUGP ("Applying relocate section %u to %u\n", relsec,
  81. sechdrs[relsec].sh_info);
  82. for (i = 0; i < sechdrs[relsec].sh_size / sizeof (*rela); i++) {
  83. /* This is where to make the change */
  84. uint32_t *loc
  85. = ((void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  86. + rela[i].r_offset);
  87. /* This is the symbol it is referring to. Note that all
  88. undefined symbols have been resolved. */
  89. Elf32_Sym *sym
  90. = ((Elf32_Sym *)sechdrs[symindex].sh_addr
  91. + ELF32_R_SYM (rela[i].r_info));
  92. *loc = sym->st_value + rela[i].r_addend;
  93. }
  94. return 0;
  95. }
  96. int module_finalize(const Elf_Ehdr *hdr,
  97. const Elf_Shdr *sechdrs,
  98. struct module *me)
  99. {
  100. return 0;
  101. }
  102. void module_arch_cleanup(struct module *mod)
  103. {
  104. }