module.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <linux/moduleloader.h>
  2. #include <linux/elf.h>
  3. #include <linux/vmalloc.h>
  4. #include <linux/fs.h>
  5. #include <linux/string.h>
  6. #include <linux/kernel.h>
  7. #if 0
  8. #define DEBUGP printk
  9. #else
  10. #define DEBUGP(fmt...)
  11. #endif
  12. void *module_alloc(unsigned long size)
  13. {
  14. if (size == 0)
  15. return NULL;
  16. return vmalloc(size);
  17. }
  18. /* Free memory returned from module_alloc */
  19. void module_free(struct module *mod, void *module_region)
  20. {
  21. vfree(module_region);
  22. }
  23. /* We don't need anything special. */
  24. int module_frob_arch_sections(Elf_Ehdr *hdr,
  25. Elf_Shdr *sechdrs,
  26. char *secstrings,
  27. struct module *mod)
  28. {
  29. return 0;
  30. }
  31. int apply_relocate(Elf32_Shdr *sechdrs,
  32. const char *strtab,
  33. unsigned int symindex,
  34. unsigned int relsec,
  35. struct module *me)
  36. {
  37. unsigned int i;
  38. Elf32_Rel *rel = (void *)sechdrs[relsec].sh_addr;
  39. Elf32_Sym *sym;
  40. uint32_t *location;
  41. DEBUGP("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. switch (ELF32_R_TYPE(rel[i].r_info)) {
  52. case R_68K_32:
  53. /* We add the value into the location given */
  54. *location += sym->st_value;
  55. break;
  56. case R_68K_PC32:
  57. /* Add the value, subtract its postition */
  58. *location += sym->st_value - (uint32_t)location;
  59. break;
  60. default:
  61. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  62. me->name, ELF32_R_TYPE(rel[i].r_info));
  63. return -ENOEXEC;
  64. }
  65. }
  66. return 0;
  67. }
  68. int apply_relocate_add(Elf32_Shdr *sechdrs,
  69. const char *strtab,
  70. unsigned int symindex,
  71. unsigned int relsec,
  72. struct module *me)
  73. {
  74. unsigned int i;
  75. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  76. Elf32_Sym *sym;
  77. uint32_t *location;
  78. DEBUGP("Applying relocate_add section %u to %u\n", relsec,
  79. sechdrs[relsec].sh_info);
  80. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  81. /* This is where to make the change */
  82. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  83. + rel[i].r_offset;
  84. /* This is the symbol it is referring to. Note that all
  85. undefined symbols have been resolved. */
  86. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  87. + ELF32_R_SYM(rel[i].r_info);
  88. switch (ELF32_R_TYPE(rel[i].r_info)) {
  89. case R_68K_32:
  90. /* We add the value into the location given */
  91. *location = rel[i].r_addend + sym->st_value;
  92. break;
  93. case R_68K_PC32:
  94. /* Add the value, subtract its postition */
  95. *location = rel[i].r_addend + sym->st_value - (uint32_t)location;
  96. break;
  97. default:
  98. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  99. me->name, ELF32_R_TYPE(rel[i].r_info));
  100. return -ENOEXEC;
  101. }
  102. }
  103. return 0;
  104. }
  105. int module_finalize(const Elf_Ehdr *hdr,
  106. const Elf_Shdr *sechdrs,
  107. struct module *me)
  108. {
  109. return 0;
  110. }
  111. void module_arch_cleanup(struct module *mod)
  112. {
  113. }