module.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Kernel module help for SH.
  3. SHcompact version by Kaz Kojima and Paul Mundt.
  4. SHmedia bits:
  5. Copyright 2004 SuperH (UK) Ltd
  6. Author: Richard Curnow
  7. Based on the sh version, and on code from the sh64-specific parts of
  8. modutils, originally written by Richard Curnow and Ben Gaster.
  9. */
  10. #include <linux/moduleloader.h>
  11. #include <linux/elf.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/bug.h>
  14. #include <linux/fs.h>
  15. #include <linux/string.h>
  16. #include <linux/kernel.h>
  17. #include <asm/unaligned.h>
  18. #include <asm/dwarf.h>
  19. int apply_relocate_add(Elf32_Shdr *sechdrs,
  20. const char *strtab,
  21. unsigned int symindex,
  22. unsigned int relsec,
  23. struct module *me)
  24. {
  25. unsigned int i;
  26. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  27. Elf32_Sym *sym;
  28. Elf32_Addr relocation;
  29. uint32_t *location;
  30. uint32_t value;
  31. pr_debug("Applying relocate section %u to %u\n", relsec,
  32. sechdrs[relsec].sh_info);
  33. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  34. /* This is where to make the change */
  35. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  36. + rel[i].r_offset;
  37. /* This is the symbol it is referring to. Note that all
  38. undefined symbols have been resolved. */
  39. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  40. + ELF32_R_SYM(rel[i].r_info);
  41. relocation = sym->st_value + rel[i].r_addend;
  42. switch (ELF32_R_TYPE(rel[i].r_info)) {
  43. case R_SH_NONE:
  44. break;
  45. case R_SH_DIR32:
  46. value = get_unaligned(location);
  47. value += relocation;
  48. put_unaligned(value, location);
  49. break;
  50. case R_SH_REL32:
  51. relocation = (relocation - (Elf32_Addr) location);
  52. value = get_unaligned(location);
  53. value += relocation;
  54. put_unaligned(value, location);
  55. break;
  56. case R_SH_IMM_LOW16:
  57. *location = (*location & ~0x3fffc00) |
  58. ((relocation & 0xffff) << 10);
  59. break;
  60. case R_SH_IMM_MEDLOW16:
  61. *location = (*location & ~0x3fffc00) |
  62. (((relocation >> 16) & 0xffff) << 10);
  63. break;
  64. case R_SH_IMM_LOW16_PCREL:
  65. relocation -= (Elf32_Addr) location;
  66. *location = (*location & ~0x3fffc00) |
  67. ((relocation & 0xffff) << 10);
  68. break;
  69. case R_SH_IMM_MEDLOW16_PCREL:
  70. relocation -= (Elf32_Addr) location;
  71. *location = (*location & ~0x3fffc00) |
  72. (((relocation >> 16) & 0xffff) << 10);
  73. break;
  74. default:
  75. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  76. me->name, ELF32_R_TYPE(rel[i].r_info));
  77. return -ENOEXEC;
  78. }
  79. }
  80. return 0;
  81. }
  82. int module_finalize(const Elf_Ehdr *hdr,
  83. const Elf_Shdr *sechdrs,
  84. struct module *me)
  85. {
  86. int ret = 0;
  87. ret |= module_dwarf_finalize(hdr, sechdrs, me);
  88. return ret;
  89. }
  90. void module_arch_cleanup(struct module *mod)
  91. {
  92. module_dwarf_cleanup(mod);
  93. }