module.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Kernel module help for sh64.
  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. Copyright 2004 SuperH (UK) Ltd
  14. Author: Richard Curnow
  15. Based on the sh version, and on code from the sh64-specific parts of
  16. modutils, originally written by Richard Curnow and Ben Gaster.
  17. */
  18. #include <linux/moduleloader.h>
  19. #include <linux/elf.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/fs.h>
  22. #include <linux/string.h>
  23. #include <linux/kernel.h>
  24. #if 0
  25. #define DEBUGP printk
  26. #else
  27. #define DEBUGP(fmt...)
  28. #endif
  29. void *module_alloc(unsigned long size)
  30. {
  31. if (size == 0)
  32. return NULL;
  33. return vmalloc(size);
  34. }
  35. /* Free memory returned from module_alloc */
  36. void module_free(struct module *mod, void *module_region)
  37. {
  38. vfree(module_region);
  39. /* FIXME: If module_region == mod->init_region, trim exception
  40. table entries. */
  41. }
  42. /* We don't need anything special. */
  43. int module_frob_arch_sections(Elf_Ehdr *hdr,
  44. Elf_Shdr *sechdrs,
  45. char *secstrings,
  46. struct module *mod)
  47. {
  48. return 0;
  49. }
  50. int apply_relocate_add(Elf32_Shdr *sechdrs,
  51. const char *strtab,
  52. unsigned int symindex,
  53. unsigned int relsec,
  54. struct module *me)
  55. {
  56. unsigned int i;
  57. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  58. Elf32_Sym *sym;
  59. Elf32_Addr relocation;
  60. uint32_t *location;
  61. int align;
  62. int is_shmedia;
  63. DEBUGP("Applying relocate section %u to %u\n", relsec,
  64. sechdrs[relsec].sh_info);
  65. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  66. /* This is where to make the change */
  67. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  68. + rel[i].r_offset;
  69. /* This is the symbol it is referring to. Note that all
  70. undefined symbols have been resolved. */
  71. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  72. + ELF32_R_SYM(rel[i].r_info);
  73. relocation = sym->st_value + rel[i].r_addend;
  74. align = (int)location & 3;
  75. /* For text addresses, bit2 of the st_other field indicates
  76. * whether the symbol is SHmedia (1) or SHcompact (0). If
  77. * SHmedia, the LSB of the symbol needs to be asserted
  78. * for the CPU to be in SHmedia mode when it starts executing
  79. * the branch target. */
  80. is_shmedia = (sym->st_other & 4) ? 1 : 0;
  81. if (is_shmedia) {
  82. relocation |= 1;
  83. }
  84. switch (ELF32_R_TYPE(rel[i].r_info)) {
  85. case R_SH_DIR32:
  86. DEBUGP("R_SH_DIR32 @%08lx = %08lx\n", (unsigned long) location, (unsigned long) relocation);
  87. *location += relocation;
  88. break;
  89. case R_SH_REL32:
  90. DEBUGP("R_SH_REL32 @%08lx = %08lx\n", (unsigned long) location, (unsigned long) relocation);
  91. relocation -= (Elf32_Addr) location;
  92. *location += relocation;
  93. break;
  94. case R_SH_IMM_LOW16:
  95. DEBUGP("R_SH_IMM_LOW16 @%08lx = %08lx\n", (unsigned long) location, (unsigned long) relocation);
  96. *location = (*location & ~0x3fffc00) |
  97. ((relocation & 0xffff) << 10);
  98. break;
  99. case R_SH_IMM_MEDLOW16:
  100. DEBUGP("R_SH_IMM_MEDLOW16 @%08lx = %08lx\n", (unsigned long) location, (unsigned long) relocation);
  101. *location = (*location & ~0x3fffc00) |
  102. (((relocation >> 16) & 0xffff) << 10);
  103. break;
  104. case R_SH_IMM_LOW16_PCREL:
  105. DEBUGP("R_SH_IMM_LOW16_PCREL @%08lx = %08lx\n", (unsigned long) location, (unsigned long) relocation);
  106. relocation -= (Elf32_Addr) location;
  107. *location = (*location & ~0x3fffc00) |
  108. ((relocation & 0xffff) << 10);
  109. break;
  110. case R_SH_IMM_MEDLOW16_PCREL:
  111. DEBUGP("R_SH_IMM_MEDLOW16_PCREL @%08lx = %08lx\n", (unsigned long) location, (unsigned long) relocation);
  112. relocation -= (Elf32_Addr) location;
  113. *location = (*location & ~0x3fffc00) |
  114. (((relocation >> 16) & 0xffff) << 10);
  115. break;
  116. default:
  117. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  118. me->name, ELF32_R_TYPE(rel[i].r_info));
  119. return -ENOEXEC;
  120. }
  121. }
  122. return 0;
  123. }
  124. int apply_relocate(Elf32_Shdr *sechdrs,
  125. const char *strtab,
  126. unsigned int symindex,
  127. unsigned int relsec,
  128. struct module *me)
  129. {
  130. printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
  131. me->name);
  132. return -ENOEXEC;
  133. }
  134. int module_finalize(const Elf_Ehdr *hdr,
  135. const Elf_Shdr *sechdrs,
  136. struct module *me)
  137. {
  138. return 0;
  139. }
  140. void module_arch_cleanup(struct module *mod)
  141. {
  142. }