module.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Kernel module support for Nios II.
  3. *
  4. * Copyright (C) 2004 Microtronix Datacom Ltd.
  5. * Written by Wentao Xu <xuwentao@microtronix.com>
  6. * Copyright (C) 2001, 2003 Rusty Russell
  7. *
  8. * This file is subject to the terms and conditions of the GNU General
  9. * Public License. See the file COPYING in the main directory of this
  10. * archive for more details.
  11. */
  12. #include <linux/moduleloader.h>
  13. #include <linux/elf.h>
  14. #include <linux/mm.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/slab.h>
  17. #include <linux/fs.h>
  18. #include <linux/string.h>
  19. #include <linux/kernel.h>
  20. #include <asm/cacheflush.h>
  21. /*
  22. * Modules should NOT be allocated with kmalloc for (obvious) reasons.
  23. * But we do it for now to avoid relocation issues. CALL26/PCREL26 cannot reach
  24. * from 0x80000000 (vmalloc area) to 0xc00000000 (kernel) (kmalloc returns
  25. * addresses in 0xc0000000)
  26. */
  27. void *module_alloc(unsigned long size)
  28. {
  29. if (size == 0)
  30. return NULL;
  31. return kmalloc(size, GFP_KERNEL);
  32. }
  33. /* Free memory returned from module_alloc */
  34. void module_memfree(void *module_region)
  35. {
  36. kfree(module_region);
  37. }
  38. int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
  39. unsigned int symindex, unsigned int relsec,
  40. struct module *mod)
  41. {
  42. unsigned int i;
  43. Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  44. pr_debug("Applying relocate section %u to %u\n", relsec,
  45. sechdrs[relsec].sh_info);
  46. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
  47. /* This is where to make the change */
  48. uint32_t word;
  49. uint32_t *loc
  50. = ((void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  51. + rela[i].r_offset);
  52. /* This is the symbol it is referring to. Note that all
  53. undefined symbols have been resolved. */
  54. Elf32_Sym *sym
  55. = ((Elf32_Sym *)sechdrs[symindex].sh_addr
  56. + ELF32_R_SYM(rela[i].r_info));
  57. uint32_t v = sym->st_value + rela[i].r_addend;
  58. pr_debug("reltype %d 0x%x name:<%s>\n",
  59. ELF32_R_TYPE(rela[i].r_info),
  60. rela[i].r_offset, strtab + sym->st_name);
  61. switch (ELF32_R_TYPE(rela[i].r_info)) {
  62. case R_NIOS2_NONE:
  63. break;
  64. case R_NIOS2_BFD_RELOC_32:
  65. *loc += v;
  66. break;
  67. case R_NIOS2_PCREL16:
  68. v -= (uint32_t)loc + 4;
  69. if ((int32_t)v > 0x7fff ||
  70. (int32_t)v < -(int32_t)0x8000) {
  71. pr_err("module %s: relocation overflow\n",
  72. mod->name);
  73. return -ENOEXEC;
  74. }
  75. word = *loc;
  76. *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
  77. (word & 0x3f);
  78. break;
  79. case R_NIOS2_CALL26:
  80. if (v & 3) {
  81. pr_err("module %s: dangerous relocation\n",
  82. mod->name);
  83. return -ENOEXEC;
  84. }
  85. if ((v >> 28) != ((uint32_t)loc >> 28)) {
  86. pr_err("module %s: relocation overflow\n",
  87. mod->name);
  88. return -ENOEXEC;
  89. }
  90. *loc = (*loc & 0x3f) | ((v >> 2) << 6);
  91. break;
  92. case R_NIOS2_HI16:
  93. word = *loc;
  94. *loc = ((((word >> 22) << 16) |
  95. ((v >> 16) & 0xffff)) << 6) | (word & 0x3f);
  96. break;
  97. case R_NIOS2_LO16:
  98. word = *loc;
  99. *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
  100. (word & 0x3f);
  101. break;
  102. case R_NIOS2_HIADJ16:
  103. {
  104. Elf32_Addr word2;
  105. word = *loc;
  106. word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
  107. *loc = ((((word >> 22) << 16) | word2) << 6) |
  108. (word & 0x3f);
  109. }
  110. break;
  111. default:
  112. pr_err("module %s: Unknown reloc: %u\n",
  113. mod->name, ELF32_R_TYPE(rela[i].r_info));
  114. return -ENOEXEC;
  115. }
  116. }
  117. return 0;
  118. }
  119. int module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  120. struct module *me)
  121. {
  122. flush_cache_all();
  123. return 0;
  124. }