module_32.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. int apply_relocate(Elf32_Shdr *sechdrs,
  34. const char *strtab,
  35. unsigned int symindex,
  36. unsigned int relsec,
  37. struct module *me)
  38. {
  39. unsigned int i;
  40. Elf32_Rel *rel = (void *)sechdrs[relsec].sh_addr;
  41. Elf32_Sym *sym;
  42. uint32_t *location;
  43. DEBUGP("Applying relocate section %u to %u\n", relsec,
  44. sechdrs[relsec].sh_info);
  45. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  46. /* This is where to make the change */
  47. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  48. + rel[i].r_offset;
  49. /* This is the symbol it is referring to. Note that all
  50. undefined symbols have been resolved. */
  51. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  52. + ELF32_R_SYM(rel[i].r_info);
  53. switch (ELF32_R_TYPE(rel[i].r_info)) {
  54. case R_386_32:
  55. /* We add the value into the location given */
  56. *location += sym->st_value;
  57. break;
  58. case R_386_PC32:
  59. /* Add the value, subtract its postition */
  60. *location += sym->st_value - (uint32_t)location;
  61. break;
  62. default:
  63. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  64. me->name, ELF32_R_TYPE(rel[i].r_info));
  65. return -ENOEXEC;
  66. }
  67. }
  68. return 0;
  69. }
  70. int apply_relocate_add(Elf32_Shdr *sechdrs,
  71. const char *strtab,
  72. unsigned int symindex,
  73. unsigned int relsec,
  74. struct module *me)
  75. {
  76. printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
  77. me->name);
  78. return -ENOEXEC;
  79. }