module.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. printk(KERN_ERR "module %s: RELOCATION unsupported\n",
  38. me->name);
  39. return -ENOEXEC;
  40. }
  41. int apply_relocate_add(Elf32_Shdr *sechdrs,
  42. const char *strtab,
  43. unsigned int symindex,
  44. unsigned int relsec,
  45. struct module *me)
  46. {
  47. unsigned int i;
  48. Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  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(*rela); i++) {
  52. /* This is where to make the change */
  53. uint32_t *loc = (uint32_t *)(sechdrs[sechdrs[relsec].sh_info].sh_addr
  54. + rela[i].r_offset);
  55. /* This is the symbol it is referring to. Note that all
  56. undefined symbols have been resolved. */
  57. Elf32_Sym *sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  58. + ELF32_R_SYM(rela[i].r_info);
  59. uint32_t v = sym->st_value + rela[i].r_addend;
  60. switch (ELF32_R_TYPE(rela[i].r_info)) {
  61. case R_H8_DIR24R8:
  62. loc = (uint32_t *)((uint32_t)loc - 1);
  63. *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
  64. break;
  65. case R_H8_DIR24A8:
  66. if (ELF32_R_SYM(rela[i].r_info))
  67. *loc += v;
  68. break;
  69. case R_H8_DIR32:
  70. case R_H8_DIR32A16:
  71. *loc += v;
  72. break;
  73. case R_H8_PCREL16:
  74. v -= (unsigned long)loc + 2;
  75. if ((Elf32_Sword)v > 0x7fff ||
  76. (Elf32_Sword)v < -(Elf32_Sword)0x8000)
  77. goto overflow;
  78. else
  79. *(unsigned short *)loc = v;
  80. break;
  81. case R_H8_PCREL8:
  82. v -= (unsigned long)loc + 1;
  83. if ((Elf32_Sword)v > 0x7f ||
  84. (Elf32_Sword)v < -(Elf32_Sword)0x80)
  85. goto overflow;
  86. else
  87. *(unsigned char *)loc = v;
  88. break;
  89. default:
  90. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  91. me->name, ELF32_R_TYPE(rela[i].r_info));
  92. return -ENOEXEC;
  93. }
  94. }
  95. return 0;
  96. overflow:
  97. printk(KERN_ERR "module %s: relocation offset overflow: %08x\n",
  98. me->name, rela[i].r_offset);
  99. return -ENOEXEC;
  100. }
  101. int module_finalize(const Elf_Ehdr *hdr,
  102. const Elf_Shdr *sechdrs,
  103. struct module *me)
  104. {
  105. return module_bug_finalize(hdr, sechdrs, me);
  106. }
  107. void module_arch_cleanup(struct module *mod)
  108. {
  109. module_bug_cleanup(mod);
  110. }