module.c 4.2 KB

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