module.c 3.9 KB

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