module.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. void *module_alloc(unsigned long size)
  29. {
  30. if (size == 0)
  31. return NULL;
  32. return vmalloc_exec(size);
  33. }
  34. /* Free memory returned from module_alloc */
  35. void module_free(struct module *mod, void *module_region)
  36. {
  37. vfree(module_region);
  38. /* FIXME: If module_region == mod->init_region, trim exception
  39. table entries. */
  40. }
  41. /* We don't need anything special. */
  42. int module_frob_arch_sections(Elf_Ehdr *hdr,
  43. Elf_Shdr *sechdrs,
  44. char *secstrings,
  45. struct module *mod)
  46. {
  47. return 0;
  48. }
  49. int apply_relocate_add(Elf32_Shdr *sechdrs,
  50. const char *strtab,
  51. unsigned int symindex,
  52. unsigned int relsec,
  53. struct module *me)
  54. {
  55. unsigned int i;
  56. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  57. Elf32_Sym *sym;
  58. Elf32_Addr relocation;
  59. uint32_t *location;
  60. uint32_t value;
  61. pr_debug("Applying relocate section %u to %u\n", relsec,
  62. sechdrs[relsec].sh_info);
  63. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  64. /* This is where to make the change */
  65. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  66. + rel[i].r_offset;
  67. /* This is the symbol it is referring to. Note that all
  68. undefined symbols have been resolved. */
  69. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  70. + ELF32_R_SYM(rel[i].r_info);
  71. relocation = sym->st_value + rel[i].r_addend;
  72. #ifdef CONFIG_SUPERH64
  73. /* For text addresses, bit2 of the st_other field indicates
  74. * whether the symbol is SHmedia (1) or SHcompact (0). If
  75. * SHmedia, the LSB of the symbol needs to be asserted
  76. * for the CPU to be in SHmedia mode when it starts executing
  77. * the branch target. */
  78. relocation |= (sym->st_other & 4);
  79. #endif
  80. switch (ELF32_R_TYPE(rel[i].r_info)) {
  81. case R_SH_DIR32:
  82. value = get_unaligned(location);
  83. value += relocation;
  84. put_unaligned(value, location);
  85. break;
  86. case R_SH_REL32:
  87. relocation = (relocation - (Elf32_Addr) location);
  88. value = get_unaligned(location);
  89. value += relocation;
  90. put_unaligned(value, location);
  91. break;
  92. case R_SH_IMM_LOW16:
  93. *location = (*location & ~0x3fffc00) |
  94. ((relocation & 0xffff) << 10);
  95. break;
  96. case R_SH_IMM_MEDLOW16:
  97. *location = (*location & ~0x3fffc00) |
  98. (((relocation >> 16) & 0xffff) << 10);
  99. break;
  100. case R_SH_IMM_LOW16_PCREL:
  101. relocation -= (Elf32_Addr) location;
  102. *location = (*location & ~0x3fffc00) |
  103. ((relocation & 0xffff) << 10);
  104. break;
  105. case R_SH_IMM_MEDLOW16_PCREL:
  106. relocation -= (Elf32_Addr) location;
  107. *location = (*location & ~0x3fffc00) |
  108. (((relocation >> 16) & 0xffff) << 10);
  109. break;
  110. default:
  111. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  112. me->name, ELF32_R_TYPE(rel[i].r_info));
  113. return -ENOEXEC;
  114. }
  115. }
  116. return 0;
  117. }
  118. int apply_relocate(Elf32_Shdr *sechdrs,
  119. const char *strtab,
  120. unsigned int symindex,
  121. unsigned int relsec,
  122. struct module *me)
  123. {
  124. printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
  125. me->name);
  126. return -ENOEXEC;
  127. }
  128. int module_finalize(const Elf_Ehdr *hdr,
  129. const Elf_Shdr *sechdrs,
  130. struct module *me)
  131. {
  132. return module_bug_finalize(hdr, sechdrs, me);
  133. }
  134. void module_arch_cleanup(struct module *mod)
  135. {
  136. module_bug_cleanup(mod);
  137. }