module.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_NONE:
  81. break;
  82. case R_SH_DIR32:
  83. value = get_unaligned(location);
  84. value += relocation;
  85. put_unaligned(value, location);
  86. break;
  87. case R_SH_REL32:
  88. relocation = (relocation - (Elf32_Addr) location);
  89. value = get_unaligned(location);
  90. value += relocation;
  91. put_unaligned(value, location);
  92. break;
  93. case R_SH_IMM_LOW16:
  94. *location = (*location & ~0x3fffc00) |
  95. ((relocation & 0xffff) << 10);
  96. break;
  97. case R_SH_IMM_MEDLOW16:
  98. *location = (*location & ~0x3fffc00) |
  99. (((relocation >> 16) & 0xffff) << 10);
  100. break;
  101. case R_SH_IMM_LOW16_PCREL:
  102. relocation -= (Elf32_Addr) location;
  103. *location = (*location & ~0x3fffc00) |
  104. ((relocation & 0xffff) << 10);
  105. break;
  106. case R_SH_IMM_MEDLOW16_PCREL:
  107. relocation -= (Elf32_Addr) location;
  108. *location = (*location & ~0x3fffc00) |
  109. (((relocation >> 16) & 0xffff) << 10);
  110. break;
  111. default:
  112. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  113. me->name, ELF32_R_TYPE(rel[i].r_info));
  114. return -ENOEXEC;
  115. }
  116. }
  117. return 0;
  118. }
  119. int apply_relocate(Elf32_Shdr *sechdrs,
  120. const char *strtab,
  121. unsigned int symindex,
  122. unsigned int relsec,
  123. struct module *me)
  124. {
  125. printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
  126. me->name);
  127. return -ENOEXEC;
  128. }
  129. int module_finalize(const Elf_Ehdr *hdr,
  130. const Elf_Shdr *sechdrs,
  131. struct module *me)
  132. {
  133. int ret = 0;
  134. ret |= module_dwarf_finalize(hdr, sechdrs, me);
  135. return ret;
  136. }
  137. void module_arch_cleanup(struct module *mod)
  138. {
  139. module_dwarf_cleanup(mod);
  140. }