module.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file COPYING in the main directory of this archive
  4. * for more details.
  5. */
  6. #include <linux/moduleloader.h>
  7. #include <linux/elf.h>
  8. #include <linux/vmalloc.h>
  9. #include <linux/fs.h>
  10. #include <linux/string.h>
  11. #include <linux/kernel.h>
  12. #if 0
  13. #define DEBUGP printk
  14. #else
  15. #define DEBUGP(fmt...)
  16. #endif
  17. #ifdef CONFIG_MODULES
  18. int apply_relocate(Elf32_Shdr *sechdrs,
  19. const char *strtab,
  20. unsigned int symindex,
  21. unsigned int relsec,
  22. struct module *me)
  23. {
  24. unsigned int i;
  25. Elf32_Rel *rel = (void *)sechdrs[relsec].sh_addr;
  26. Elf32_Sym *sym;
  27. uint32_t *location;
  28. DEBUGP("Applying relocate section %u to %u\n", relsec,
  29. sechdrs[relsec].sh_info);
  30. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  31. /* This is where to make the change */
  32. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  33. + rel[i].r_offset;
  34. /* This is the symbol it is referring to. Note that all
  35. undefined symbols have been resolved. */
  36. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  37. + ELF32_R_SYM(rel[i].r_info);
  38. switch (ELF32_R_TYPE(rel[i].r_info)) {
  39. case R_68K_32:
  40. /* We add the value into the location given */
  41. *location += sym->st_value;
  42. break;
  43. case R_68K_PC32:
  44. /* Add the value, subtract its position */
  45. *location += sym->st_value - (uint32_t)location;
  46. break;
  47. default:
  48. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  49. me->name, ELF32_R_TYPE(rel[i].r_info));
  50. return -ENOEXEC;
  51. }
  52. }
  53. return 0;
  54. }
  55. int apply_relocate_add(Elf32_Shdr *sechdrs,
  56. const char *strtab,
  57. unsigned int symindex,
  58. unsigned int relsec,
  59. struct module *me)
  60. {
  61. unsigned int i;
  62. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  63. Elf32_Sym *sym;
  64. uint32_t *location;
  65. DEBUGP("Applying relocate_add section %u to %u\n", relsec,
  66. sechdrs[relsec].sh_info);
  67. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  68. /* This is where to make the change */
  69. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  70. + rel[i].r_offset;
  71. /* This is the symbol it is referring to. Note that all
  72. undefined symbols have been resolved. */
  73. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  74. + ELF32_R_SYM(rel[i].r_info);
  75. switch (ELF32_R_TYPE(rel[i].r_info)) {
  76. case R_68K_32:
  77. /* We add the value into the location given */
  78. *location = rel[i].r_addend + sym->st_value;
  79. break;
  80. case R_68K_PC32:
  81. /* Add the value, subtract its position */
  82. *location = rel[i].r_addend + sym->st_value - (uint32_t)location;
  83. break;
  84. default:
  85. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  86. me->name, ELF32_R_TYPE(rel[i].r_info));
  87. return -ENOEXEC;
  88. }
  89. }
  90. return 0;
  91. }
  92. int module_finalize(const Elf_Ehdr *hdr,
  93. const Elf_Shdr *sechdrs,
  94. struct module *mod)
  95. {
  96. module_fixup(mod, mod->arch.fixup_start, mod->arch.fixup_end);
  97. return 0;
  98. }
  99. #endif /* CONFIG_MODULES */
  100. void module_fixup(struct module *mod, struct m68k_fixup_info *start,
  101. struct m68k_fixup_info *end)
  102. {
  103. #ifdef CONFIG_MMU
  104. struct m68k_fixup_info *fixup;
  105. for (fixup = start; fixup < end; fixup++) {
  106. switch (fixup->type) {
  107. case m68k_fixup_memoffset:
  108. *(u32 *)fixup->addr = m68k_memoffset;
  109. break;
  110. case m68k_fixup_vnode_shift:
  111. *(u16 *)fixup->addr += m68k_virt_to_node_shift;
  112. break;
  113. }
  114. }
  115. #endif
  116. }