module.c 3.4 KB

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