module.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifdef CONFIG_ETRAX_KMALLOCED_MODULES
  27. #define MALLOC_MODULE(size) kmalloc(size, GFP_KERNEL)
  28. #define FREE_MODULE(region) kfree(region)
  29. #else
  30. #define MALLOC_MODULE(size) vmalloc_exec(size)
  31. #define FREE_MODULE(region) vfree(region)
  32. #endif
  33. void *module_alloc(unsigned long size)
  34. {
  35. if (size == 0)
  36. return NULL;
  37. return MALLOC_MODULE(size);
  38. }
  39. /* Free memory returned from module_alloc */
  40. void module_free(struct module *mod, void *module_region)
  41. {
  42. FREE_MODULE(module_region);
  43. /* FIXME: If module_region == mod->init_region, trim exception
  44. table entries. */
  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. int apply_relocate(Elf32_Shdr *sechdrs,
  55. const char *strtab,
  56. unsigned int symindex,
  57. unsigned int relsec,
  58. struct module *me)
  59. {
  60. printk(KERN_ERR "module %s: REL relocation unsupported\n", me->name);
  61. return -ENOEXEC;
  62. }
  63. int apply_relocate_add(Elf32_Shdr *sechdrs,
  64. const char *strtab,
  65. unsigned int symindex,
  66. unsigned int relsec,
  67. struct module *me)
  68. {
  69. unsigned int i;
  70. Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  71. DEBUGP ("Applying add relocate section %u to %u\n", relsec,
  72. sechdrs[relsec].sh_info);
  73. for (i = 0; i < sechdrs[relsec].sh_size / sizeof (*rela); i++) {
  74. /* This is where to make the change */
  75. uint32_t *loc
  76. = ((void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  77. + rela[i].r_offset);
  78. /* This is the symbol it is referring to. Note that all
  79. undefined symbols have been resolved. */
  80. Elf32_Sym *sym
  81. = ((Elf32_Sym *)sechdrs[symindex].sh_addr
  82. + ELF32_R_SYM (rela[i].r_info));
  83. switch (ELF32_R_TYPE(rela[i].r_info)) {
  84. case R_CRIS_32:
  85. *loc = sym->st_value + rela[i].r_addend;
  86. break;
  87. case R_CRIS_32_PCREL:
  88. *loc = sym->st_value - (unsigned)loc + rela[i].r_addend - 4;
  89. break;
  90. default:
  91. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  92. me->name, ELF32_R_TYPE(rela[i].r_info));
  93. return -ENOEXEC;
  94. }
  95. }
  96. return 0;
  97. }
  98. int module_finalize(const Elf_Ehdr *hdr,
  99. const Elf_Shdr *sechdrs,
  100. struct module *me)
  101. {
  102. return 0;
  103. }
  104. void module_arch_cleanup(struct module *mod)
  105. {
  106. }