module.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. void *module_alloc(unsigned long size)
  29. {
  30. return size ? vmalloc(size) : NULL;
  31. }
  32. /* Free memory returned from module_alloc */
  33. void module_free(struct module *mod, void *module_region)
  34. {
  35. vfree(module_region);
  36. }
  37. int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  38. char *secstrings, struct module *mod)
  39. {
  40. return 0;
  41. }
  42. int apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
  43. unsigned int symindex, unsigned int relindex,
  44. struct module *me)
  45. {
  46. Elf32_Shdr *symsec = sechdrs + symindex;
  47. Elf32_Shdr *relsec = sechdrs + relindex;
  48. Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
  49. Elf32_Rel *rel = (void *)relsec->sh_addr;
  50. unsigned int i;
  51. for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
  52. unsigned long loc;
  53. Elf32_Sym *sym;
  54. s32 r_offset;
  55. r_offset = ELF32_R_SYM(rel->r_info);
  56. if ((r_offset < 0) ||
  57. (r_offset > (symsec->sh_size / sizeof(Elf32_Sym)))) {
  58. printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
  59. me->name, relindex, i);
  60. return -ENOEXEC;
  61. }
  62. sym = ((Elf32_Sym *)symsec->sh_addr) + r_offset;
  63. if ((rel->r_offset < 0) ||
  64. (rel->r_offset > dstsec->sh_size - sizeof(u32))) {
  65. printk(KERN_ERR "%s: out of bounds relocation, "
  66. "section %d reloc %d offset %d size %d\n",
  67. me->name, relindex, i, rel->r_offset,
  68. dstsec->sh_size);
  69. return -ENOEXEC;
  70. }
  71. loc = dstsec->sh_addr + rel->r_offset;
  72. switch (ELF32_R_TYPE(rel->r_info)) {
  73. case R_SCORE_NONE:
  74. break;
  75. case R_SCORE_ABS32:
  76. *(unsigned long *)loc += sym->st_value;
  77. break;
  78. case R_SCORE_HI16:
  79. break;
  80. case R_SCORE_LO16: {
  81. unsigned long hi16_offset, offset;
  82. unsigned long uvalue;
  83. unsigned long temp, temp_hi;
  84. temp_hi = *((unsigned long *)loc - 1);
  85. temp = *(unsigned long *)loc;
  86. hi16_offset = (((((temp_hi) >> 16) & 0x3) << 15) |
  87. ((temp_hi) & 0x7fff)) >> 1;
  88. offset = ((temp >> 16 & 0x03) << 15) |
  89. ((temp & 0x7fff) >> 1);
  90. offset = (hi16_offset << 16) | (offset & 0xffff);
  91. uvalue = sym->st_value + offset;
  92. hi16_offset = (uvalue >> 16) << 1;
  93. temp_hi = ((temp_hi) & (~(0x37fff))) |
  94. (hi16_offset & 0x7fff) |
  95. ((hi16_offset << 1) & 0x30000);
  96. *((unsigned long *)loc - 1) = temp_hi;
  97. offset = (uvalue & 0xffff) << 1;
  98. temp = (temp & (~(0x37fff))) | (offset & 0x7fff) |
  99. ((offset << 1) & 0x30000);
  100. *(unsigned long *)loc = temp;
  101. break;
  102. }
  103. case R_SCORE_24: {
  104. unsigned long hi16_offset, offset;
  105. unsigned long uvalue;
  106. unsigned long temp;
  107. temp = *(unsigned long *)loc;
  108. offset = (temp & 0x03FF7FFE);
  109. hi16_offset = (offset & 0xFFFF0000);
  110. offset = (hi16_offset | ((offset & 0xFFFF) << 1)) >> 2;
  111. uvalue = (sym->st_value + offset) >> 1;
  112. uvalue = uvalue & 0x00ffffff;
  113. temp = (temp & 0xfc008001) |
  114. ((uvalue << 2) & 0x3ff0000) |
  115. ((uvalue & 0x3fff) << 1);
  116. *(unsigned long *)loc = temp;
  117. break;
  118. }
  119. default:
  120. printk(KERN_ERR "%s: unknown relocation: %u\n",
  121. me->name, ELF32_R_TYPE(rel->r_info));
  122. return -ENOEXEC;
  123. }
  124. }
  125. return 0;
  126. }
  127. int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  128. unsigned int symindex, unsigned int relsec,
  129. struct module *me)
  130. {
  131. return 0;
  132. }
  133. /* Given an address, look for it in the module exception tables. */
  134. const struct exception_table_entry *search_module_dbetables(unsigned long addr)
  135. {
  136. return NULL;
  137. }
  138. /* Put in dbe list if necessary. */
  139. int module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  140. struct module *me)
  141. {
  142. return 0;
  143. }
  144. void module_arch_cleanup(struct module *mod) {}