module.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Kernel module help for SH.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  13. */
  14. #include <linux/moduleloader.h>
  15. #include <linux/elf.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/fs.h>
  18. #include <linux/string.h>
  19. #include <linux/kernel.h>
  20. #if 0
  21. #define DEBUGP printk
  22. #else
  23. #define DEBUGP(fmt...)
  24. #endif
  25. void *module_alloc(unsigned long size)
  26. {
  27. if (size == 0)
  28. return NULL;
  29. return vmalloc(size);
  30. }
  31. /* Free memory returned from module_alloc */
  32. void module_free(struct module *mod, void *module_region)
  33. {
  34. vfree(module_region);
  35. /* FIXME: If module_region == mod->init_region, trim exception
  36. table entries. */
  37. }
  38. /* We don't need anything special. */
  39. int module_frob_arch_sections(Elf_Ehdr *hdr,
  40. Elf_Shdr *sechdrs,
  41. char *secstrings,
  42. struct module *mod)
  43. {
  44. return 0;
  45. }
  46. #define COPY_UNALIGNED_WORD(sw, tw, align) \
  47. { \
  48. void *__s = &(sw), *__t = &(tw); \
  49. unsigned short *__s2 = __s, *__t2 = __t; \
  50. unsigned char *__s1 = __s, *__t1 = __t; \
  51. switch ((align)) \
  52. { \
  53. case 0: \
  54. *(unsigned long *) __t = *(unsigned long *) __s; \
  55. break; \
  56. case 2: \
  57. *__t2++ = *__s2++; \
  58. *__t2 = *__s2; \
  59. break; \
  60. default: \
  61. *__t1++ = *__s1++; \
  62. *__t1++ = *__s1++; \
  63. *__t1++ = *__s1++; \
  64. *__t1 = *__s1; \
  65. break; \
  66. } \
  67. }
  68. int apply_relocate_add(Elf32_Shdr *sechdrs,
  69. const char *strtab,
  70. unsigned int symindex,
  71. unsigned int relsec,
  72. struct module *me)
  73. {
  74. unsigned int i;
  75. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  76. Elf32_Sym *sym;
  77. Elf32_Addr relocation;
  78. uint32_t *location;
  79. uint32_t value;
  80. int align;
  81. DEBUGP("Applying relocate section %u to %u\n", relsec,
  82. sechdrs[relsec].sh_info);
  83. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  84. /* This is where to make the change */
  85. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  86. + rel[i].r_offset;
  87. /* This is the symbol it is referring to. Note that all
  88. undefined symbols have been resolved. */
  89. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  90. + ELF32_R_SYM(rel[i].r_info);
  91. relocation = sym->st_value + rel[i].r_addend;
  92. align = (int)location & 3;
  93. switch (ELF32_R_TYPE(rel[i].r_info)) {
  94. case R_SH_DIR32:
  95. COPY_UNALIGNED_WORD (*location, value, align);
  96. value += relocation;
  97. COPY_UNALIGNED_WORD (value, *location, align);
  98. break;
  99. case R_SH_REL32:
  100. relocation = (relocation - (Elf32_Addr) location);
  101. COPY_UNALIGNED_WORD (*location, value, align);
  102. value += relocation;
  103. COPY_UNALIGNED_WORD (value, *location, align);
  104. break;
  105. default:
  106. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  107. me->name, ELF32_R_TYPE(rel[i].r_info));
  108. return -ENOEXEC;
  109. }
  110. }
  111. return 0;
  112. }
  113. int apply_relocate(Elf32_Shdr *sechdrs,
  114. const char *strtab,
  115. unsigned int symindex,
  116. unsigned int relsec,
  117. struct module *me)
  118. {
  119. printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
  120. me->name);
  121. return -ENOEXEC;
  122. }
  123. int module_finalize(const Elf_Ehdr *hdr,
  124. const Elf_Shdr *sechdrs,
  125. struct module *me)
  126. {
  127. return 0;
  128. }
  129. void module_arch_cleanup(struct module *mod)
  130. {
  131. }