module.c 4.4 KB

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