module.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. void *module_alloc(unsigned long size)
  19. {
  20. if (size == 0)
  21. return NULL;
  22. return vmalloc(size);
  23. }
  24. /* Free memory returned from module_alloc */
  25. void module_free(struct module *mod, void *module_region)
  26. {
  27. vfree(module_region);
  28. }
  29. /* We don't need anything special. */
  30. int module_frob_arch_sections(Elf_Ehdr *hdr,
  31. Elf_Shdr *sechdrs,
  32. char *secstrings,
  33. struct module *mod)
  34. {
  35. return 0;
  36. }
  37. int apply_relocate(Elf32_Shdr *sechdrs,
  38. const char *strtab,
  39. unsigned int symindex,
  40. unsigned int relsec,
  41. struct module *me)
  42. {
  43. unsigned int i;
  44. Elf32_Rel *rel = (void *)sechdrs[relsec].sh_addr;
  45. Elf32_Sym *sym;
  46. uint32_t *location;
  47. DEBUGP("Applying relocate section %u to %u\n", relsec,
  48. sechdrs[relsec].sh_info);
  49. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  50. /* This is where to make the change */
  51. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  52. + rel[i].r_offset;
  53. /* This is the symbol it is referring to. Note that all
  54. undefined symbols have been resolved. */
  55. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  56. + ELF32_R_SYM(rel[i].r_info);
  57. switch (ELF32_R_TYPE(rel[i].r_info)) {
  58. case R_68K_32:
  59. /* We add the value into the location given */
  60. *location += sym->st_value;
  61. break;
  62. case R_68K_PC32:
  63. /* Add the value, subtract its postition */
  64. *location += sym->st_value - (uint32_t)location;
  65. break;
  66. default:
  67. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  68. me->name, ELF32_R_TYPE(rel[i].r_info));
  69. return -ENOEXEC;
  70. }
  71. }
  72. return 0;
  73. }
  74. int apply_relocate_add(Elf32_Shdr *sechdrs,
  75. const char *strtab,
  76. unsigned int symindex,
  77. unsigned int relsec,
  78. struct module *me)
  79. {
  80. unsigned int i;
  81. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  82. Elf32_Sym *sym;
  83. uint32_t *location;
  84. DEBUGP("Applying relocate_add section %u to %u\n", relsec,
  85. sechdrs[relsec].sh_info);
  86. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  87. /* This is where to make the change */
  88. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  89. + rel[i].r_offset;
  90. /* This is the symbol it is referring to. Note that all
  91. undefined symbols have been resolved. */
  92. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  93. + ELF32_R_SYM(rel[i].r_info);
  94. switch (ELF32_R_TYPE(rel[i].r_info)) {
  95. case R_68K_32:
  96. /* We add the value into the location given */
  97. *location = rel[i].r_addend + sym->st_value;
  98. break;
  99. case R_68K_PC32:
  100. /* Add the value, subtract its postition */
  101. *location = rel[i].r_addend + sym->st_value - (uint32_t)location;
  102. break;
  103. default:
  104. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  105. me->name, ELF32_R_TYPE(rel[i].r_info));
  106. return -ENOEXEC;
  107. }
  108. }
  109. return 0;
  110. }
  111. int module_finalize(const Elf_Ehdr *hdr,
  112. const Elf_Shdr *sechdrs,
  113. struct module *mod)
  114. {
  115. module_fixup(mod, mod->arch.fixup_start, mod->arch.fixup_end);
  116. return 0;
  117. }
  118. void module_arch_cleanup(struct module *mod)
  119. {
  120. }
  121. #endif /* CONFIG_MODULES */
  122. void module_fixup(struct module *mod, struct m68k_fixup_info *start,
  123. struct m68k_fixup_info *end)
  124. {
  125. struct m68k_fixup_info *fixup;
  126. for (fixup = start; fixup < end; fixup++) {
  127. switch (fixup->type) {
  128. case m68k_fixup_memoffset:
  129. *(u32 *)fixup->addr = m68k_memoffset;
  130. break;
  131. case m68k_fixup_vnode_shift:
  132. *(u16 *)fixup->addr += m68k_virt_to_node_shift;
  133. break;
  134. }
  135. }
  136. }