module.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * arch/score/kernel/module.c
  3. *
  4. * Score Processor version.
  5. *
  6. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  7. * Chen Liqin <liqin.chen@sunplusct.com>
  8. * Lennox Wu <lennox.wu@sunplusct.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, see the file COPYING, or write
  22. * to the Free Software Foundation, Inc.,
  23. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <linux/moduleloader.h>
  26. #include <linux/module.h>
  27. #include <linux/vmalloc.h>
  28. int apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
  29. unsigned int symindex, unsigned int relindex,
  30. struct module *me)
  31. {
  32. Elf32_Shdr *symsec = sechdrs + symindex;
  33. Elf32_Shdr *relsec = sechdrs + relindex;
  34. Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
  35. Elf32_Rel *rel = (void *)relsec->sh_addr;
  36. unsigned int i;
  37. for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
  38. unsigned long loc;
  39. Elf32_Sym *sym;
  40. s32 r_offset;
  41. r_offset = ELF32_R_SYM(rel->r_info);
  42. if ((r_offset < 0) ||
  43. (r_offset > (symsec->sh_size / sizeof(Elf32_Sym)))) {
  44. printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
  45. me->name, relindex, i);
  46. return -ENOEXEC;
  47. }
  48. sym = ((Elf32_Sym *)symsec->sh_addr) + r_offset;
  49. if ((rel->r_offset < 0) ||
  50. (rel->r_offset > dstsec->sh_size - sizeof(u32))) {
  51. printk(KERN_ERR "%s: out of bounds relocation, "
  52. "section %d reloc %d offset %d size %d\n",
  53. me->name, relindex, i, rel->r_offset,
  54. dstsec->sh_size);
  55. return -ENOEXEC;
  56. }
  57. loc = dstsec->sh_addr + rel->r_offset;
  58. switch (ELF32_R_TYPE(rel->r_info)) {
  59. case R_SCORE_NONE:
  60. break;
  61. case R_SCORE_ABS32:
  62. *(unsigned long *)loc += sym->st_value;
  63. break;
  64. case R_SCORE_HI16:
  65. break;
  66. case R_SCORE_LO16: {
  67. unsigned long hi16_offset, offset;
  68. unsigned long uvalue;
  69. unsigned long temp, temp_hi;
  70. temp_hi = *((unsigned long *)loc - 1);
  71. temp = *(unsigned long *)loc;
  72. hi16_offset = (((((temp_hi) >> 16) & 0x3) << 15) |
  73. ((temp_hi) & 0x7fff)) >> 1;
  74. offset = ((temp >> 16 & 0x03) << 15) |
  75. ((temp & 0x7fff) >> 1);
  76. offset = (hi16_offset << 16) | (offset & 0xffff);
  77. uvalue = sym->st_value + offset;
  78. hi16_offset = (uvalue >> 16) << 1;
  79. temp_hi = ((temp_hi) & (~(0x37fff))) |
  80. (hi16_offset & 0x7fff) |
  81. ((hi16_offset << 1) & 0x30000);
  82. *((unsigned long *)loc - 1) = temp_hi;
  83. offset = (uvalue & 0xffff) << 1;
  84. temp = (temp & (~(0x37fff))) | (offset & 0x7fff) |
  85. ((offset << 1) & 0x30000);
  86. *(unsigned long *)loc = temp;
  87. break;
  88. }
  89. case R_SCORE_24: {
  90. unsigned long hi16_offset, offset;
  91. unsigned long uvalue;
  92. unsigned long temp;
  93. temp = *(unsigned long *)loc;
  94. offset = (temp & 0x03FF7FFE);
  95. hi16_offset = (offset & 0xFFFF0000);
  96. offset = (hi16_offset | ((offset & 0xFFFF) << 1)) >> 2;
  97. uvalue = (sym->st_value + offset) >> 1;
  98. uvalue = uvalue & 0x00ffffff;
  99. temp = (temp & 0xfc008001) |
  100. ((uvalue << 2) & 0x3ff0000) |
  101. ((uvalue & 0x3fff) << 1);
  102. *(unsigned long *)loc = temp;
  103. break;
  104. }
  105. default:
  106. printk(KERN_ERR "%s: unknown relocation: %u\n",
  107. me->name, ELF32_R_TYPE(rel->r_info));
  108. return -ENOEXEC;
  109. }
  110. }
  111. return 0;
  112. }
  113. /* Given an address, look for it in the module exception tables. */
  114. const struct exception_table_entry *search_module_dbetables(unsigned long addr)
  115. {
  116. return NULL;
  117. }