module.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/slab.h>
  16. #include <linux/fs.h>
  17. #include <linux/string.h>
  18. #include <linux/kernel.h>
  19. #include <asm/cacheflush.h>
  20. int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
  21. unsigned int symindex, unsigned int relsec,
  22. struct module *mod)
  23. {
  24. unsigned int i;
  25. Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  26. pr_debug("Applying relocate section %u to %u\n", relsec,
  27. sechdrs[relsec].sh_info);
  28. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
  29. /* This is where to make the change */
  30. uint32_t word;
  31. uint32_t *loc
  32. = ((void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  33. + rela[i].r_offset);
  34. /* This is the symbol it is referring to. Note that all
  35. undefined symbols have been resolved. */
  36. Elf32_Sym *sym
  37. = ((Elf32_Sym *)sechdrs[symindex].sh_addr
  38. + ELF32_R_SYM(rela[i].r_info));
  39. uint32_t v = sym->st_value + rela[i].r_addend;
  40. pr_debug("reltype %d 0x%x name:<%s>\n",
  41. ELF32_R_TYPE(rela[i].r_info),
  42. rela[i].r_offset, strtab + sym->st_name);
  43. switch (ELF32_R_TYPE(rela[i].r_info)) {
  44. case R_NIOS2_NONE:
  45. break;
  46. case R_NIOS2_BFD_RELOC_32:
  47. *loc += v;
  48. break;
  49. case R_NIOS2_PCREL16:
  50. v -= (uint32_t)loc + 4;
  51. if ((int32_t)v > 0x7fff ||
  52. (int32_t)v < -(int32_t)0x8000) {
  53. pr_err("module %s: relocation overflow\n",
  54. mod->name);
  55. return -ENOEXEC;
  56. }
  57. word = *loc;
  58. *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
  59. (word & 0x3f);
  60. break;
  61. case R_NIOS2_CALL26:
  62. if (v & 3) {
  63. pr_err("module %s: dangerous relocation\n",
  64. mod->name);
  65. return -ENOEXEC;
  66. }
  67. if ((v >> 28) != ((uint32_t)loc >> 28)) {
  68. pr_err("module %s: relocation overflow\n",
  69. mod->name);
  70. return -ENOEXEC;
  71. }
  72. *loc = (*loc & 0x3f) | ((v >> 2) << 6);
  73. break;
  74. case R_NIOS2_HI16:
  75. word = *loc;
  76. *loc = ((((word >> 22) << 16) |
  77. ((v >> 16) & 0xffff)) << 6) | (word & 0x3f);
  78. break;
  79. case R_NIOS2_LO16:
  80. word = *loc;
  81. *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
  82. (word & 0x3f);
  83. break;
  84. case R_NIOS2_HIADJ16:
  85. {
  86. Elf32_Addr word2;
  87. word = *loc;
  88. word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
  89. *loc = ((((word >> 22) << 16) | word2) << 6) |
  90. (word & 0x3f);
  91. }
  92. break;
  93. default:
  94. pr_err("module %s: Unknown reloc: %u\n",
  95. mod->name, ELF32_R_TYPE(rela[i].r_info));
  96. return -ENOEXEC;
  97. }
  98. }
  99. return 0;
  100. }
  101. int module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  102. struct module *me)
  103. {
  104. flush_cache_all();
  105. return 0;
  106. }