module.c 3.7 KB

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