module.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Kernel module help for SH.
  2. SHcompact version by Kaz Kojima and Paul Mundt.
  3. SHmedia bits:
  4. Copyright 2004 SuperH (UK) Ltd
  5. Author: Richard Curnow
  6. Based on the sh version, and on code from the sh64-specific parts of
  7. modutils, originally written by Richard Curnow and Ben Gaster.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/moduleloader.h>
  21. #include <linux/elf.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/bug.h>
  24. #include <linux/fs.h>
  25. #include <linux/string.h>
  26. #include <linux/kernel.h>
  27. #include <asm/unaligned.h>
  28. #include <asm/dwarf.h>
  29. void *module_alloc(unsigned long size)
  30. {
  31. if (size == 0)
  32. return NULL;
  33. return vmalloc_exec(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. }
  40. /* We don't need anything special. */
  41. int module_frob_arch_sections(Elf_Ehdr *hdr,
  42. Elf_Shdr *sechdrs,
  43. char *secstrings,
  44. struct module *mod)
  45. {
  46. return 0;
  47. }
  48. int apply_relocate_add(Elf32_Shdr *sechdrs,
  49. const char *strtab,
  50. unsigned int symindex,
  51. unsigned int relsec,
  52. struct module *me)
  53. {
  54. unsigned int i;
  55. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  56. Elf32_Sym *sym;
  57. Elf32_Addr relocation;
  58. uint32_t *location;
  59. uint32_t value;
  60. pr_debug("Applying relocate section %u to %u\n", relsec,
  61. sechdrs[relsec].sh_info);
  62. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  63. /* This is where to make the change */
  64. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  65. + rel[i].r_offset;
  66. /* This is the symbol it is referring to. Note that all
  67. undefined symbols have been resolved. */
  68. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  69. + ELF32_R_SYM(rel[i].r_info);
  70. relocation = sym->st_value + rel[i].r_addend;
  71. #ifdef CONFIG_SUPERH64
  72. /* For text addresses, bit2 of the st_other field indicates
  73. * whether the symbol is SHmedia (1) or SHcompact (0). If
  74. * SHmedia, the LSB of the symbol needs to be asserted
  75. * for the CPU to be in SHmedia mode when it starts executing
  76. * the branch target. */
  77. relocation |= !!(sym->st_other & 4);
  78. #endif
  79. switch (ELF32_R_TYPE(rel[i].r_info)) {
  80. case R_SH_DIR32:
  81. value = get_unaligned(location);
  82. value += relocation;
  83. put_unaligned(value, location);
  84. break;
  85. case R_SH_REL32:
  86. relocation = (relocation - (Elf32_Addr) location);
  87. value = get_unaligned(location);
  88. value += relocation;
  89. put_unaligned(value, location);
  90. break;
  91. case R_SH_IMM_LOW16:
  92. *location = (*location & ~0x3fffc00) |
  93. ((relocation & 0xffff) << 10);
  94. break;
  95. case R_SH_IMM_MEDLOW16:
  96. *location = (*location & ~0x3fffc00) |
  97. (((relocation >> 16) & 0xffff) << 10);
  98. break;
  99. case R_SH_IMM_LOW16_PCREL:
  100. relocation -= (Elf32_Addr) location;
  101. *location = (*location & ~0x3fffc00) |
  102. ((relocation & 0xffff) << 10);
  103. break;
  104. case R_SH_IMM_MEDLOW16_PCREL:
  105. relocation -= (Elf32_Addr) location;
  106. *location = (*location & ~0x3fffc00) |
  107. (((relocation >> 16) & 0xffff) << 10);
  108. break;
  109. default:
  110. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  111. me->name, ELF32_R_TYPE(rel[i].r_info));
  112. return -ENOEXEC;
  113. }
  114. }
  115. return 0;
  116. }
  117. int apply_relocate(Elf32_Shdr *sechdrs,
  118. const char *strtab,
  119. unsigned int symindex,
  120. unsigned int relsec,
  121. struct module *me)
  122. {
  123. printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
  124. me->name);
  125. return -ENOEXEC;
  126. }
  127. int module_finalize(const Elf_Ehdr *hdr,
  128. const Elf_Shdr *sechdrs,
  129. struct module *me)
  130. {
  131. int ret = 0;
  132. ret |= module_dwarf_finalize(hdr, sechdrs, me);
  133. ret |= module_bug_finalize(hdr, sechdrs, me);
  134. return ret;
  135. }
  136. void module_arch_cleanup(struct module *mod)
  137. {
  138. module_bug_cleanup(mod);
  139. module_dwarf_cleanup(mod);
  140. }