module.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Kernel module help for M32R.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  13. */
  14. #include <linux/moduleloader.h>
  15. #include <linux/elf.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/fs.h>
  18. #include <linux/string.h>
  19. #include <linux/kernel.h>
  20. #if 0
  21. #define DEBUGP printk
  22. #else
  23. #define DEBUGP(fmt...)
  24. #endif
  25. #define COPY_UNALIGNED_WORD(sw, tw, align) \
  26. { \
  27. void *__s = &(sw), *__t = &(tw); \
  28. unsigned short *__s2 = __s, *__t2 =__t; \
  29. unsigned char *__s1 = __s, *__t1 =__t; \
  30. switch ((align)) \
  31. { \
  32. case 0: \
  33. *(unsigned long *) __t = *(unsigned long *) __s; \
  34. break; \
  35. case 2: \
  36. *__t2++ = *__s2++; \
  37. *__t2 = *__s2; \
  38. break; \
  39. default: \
  40. *__t1++ = *__s1++; \
  41. *__t1++ = *__s1++; \
  42. *__t1++ = *__s1++; \
  43. *__t1 = *__s1; \
  44. break; \
  45. } \
  46. }
  47. #define COPY_UNALIGNED_HWORD(sw, tw, align) \
  48. { \
  49. void *__s = &(sw), *__t = &(tw); \
  50. unsigned short *__s2 = __s, *__t2 =__t; \
  51. unsigned char *__s1 = __s, *__t1 =__t; \
  52. switch ((align)) \
  53. { \
  54. case 0: \
  55. *__t2 = *__s2; \
  56. break; \
  57. default: \
  58. *__t1++ = *__s1++; \
  59. *__t1 = *__s1; \
  60. break; \
  61. } \
  62. }
  63. int apply_relocate_add(Elf32_Shdr *sechdrs,
  64. const char *strtab,
  65. unsigned int symindex,
  66. unsigned int relsec,
  67. struct module *me)
  68. {
  69. unsigned int i;
  70. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  71. Elf32_Sym *sym;
  72. Elf32_Addr relocation;
  73. uint32_t *location;
  74. uint32_t value;
  75. unsigned short *hlocation;
  76. unsigned short hvalue;
  77. int svalue;
  78. int align;
  79. DEBUGP("Applying relocate section %u to %u\n", relsec,
  80. sechdrs[relsec].sh_info);
  81. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  82. /* This is where to make the change */
  83. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  84. + rel[i].r_offset;
  85. /* This is the symbol it is referring to. Note that all
  86. undefined symbols have been resolved. */
  87. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  88. + ELF32_R_SYM(rel[i].r_info);
  89. relocation = sym->st_value + rel[i].r_addend;
  90. align = (int)location & 3;
  91. switch (ELF32_R_TYPE(rel[i].r_info)) {
  92. case R_M32R_32_RELA:
  93. COPY_UNALIGNED_WORD (*location, value, align);
  94. value += relocation;
  95. COPY_UNALIGNED_WORD (value, *location, align);
  96. break;
  97. case R_M32R_HI16_ULO_RELA:
  98. COPY_UNALIGNED_WORD (*location, value, align);
  99. relocation = (relocation >>16) & 0xffff;
  100. /* RELA must has 0 at relocation field. */
  101. value += relocation;
  102. COPY_UNALIGNED_WORD (value, *location, align);
  103. break;
  104. case R_M32R_HI16_SLO_RELA:
  105. COPY_UNALIGNED_WORD (*location, value, align);
  106. if (relocation & 0x8000) relocation += 0x10000;
  107. relocation = (relocation >>16) & 0xffff;
  108. /* RELA must has 0 at relocation field. */
  109. value += relocation;
  110. COPY_UNALIGNED_WORD (value, *location, align);
  111. break;
  112. case R_M32R_16_RELA:
  113. hlocation = (unsigned short *)location;
  114. relocation = relocation & 0xffff;
  115. /* RELA must has 0 at relocation field. */
  116. hvalue = relocation;
  117. COPY_UNALIGNED_WORD (hvalue, *hlocation, align);
  118. break;
  119. case R_M32R_SDA16_RELA:
  120. case R_M32R_LO16_RELA:
  121. COPY_UNALIGNED_WORD (*location, value, align);
  122. relocation = relocation & 0xffff;
  123. /* RELA must has 0 at relocation field. */
  124. value += relocation;
  125. COPY_UNALIGNED_WORD (value, *location, align);
  126. break;
  127. case R_M32R_24_RELA:
  128. COPY_UNALIGNED_WORD (*location, value, align);
  129. relocation = relocation & 0xffffff;
  130. /* RELA must has 0 at relocation field. */
  131. value += relocation;
  132. COPY_UNALIGNED_WORD (value, *location, align);
  133. break;
  134. case R_M32R_18_PCREL_RELA:
  135. relocation = (relocation - (Elf32_Addr) location);
  136. if (relocation < -0x20000 || 0x1fffc < relocation)
  137. {
  138. printk(KERN_ERR "module %s: relocation overflow: %u\n",
  139. me->name, relocation);
  140. return -ENOEXEC;
  141. }
  142. COPY_UNALIGNED_WORD (*location, value, align);
  143. if (value & 0xffff)
  144. {
  145. /* RELA must has 0 at relocation field. */
  146. printk(KERN_ERR "module %s: illegal relocation field: %u\n",
  147. me->name, value);
  148. return -ENOEXEC;
  149. }
  150. relocation = (relocation >> 2) & 0xffff;
  151. value += relocation;
  152. COPY_UNALIGNED_WORD (value, *location, align);
  153. break;
  154. case R_M32R_10_PCREL_RELA:
  155. hlocation = (unsigned short *)location;
  156. relocation = (relocation - (Elf32_Addr) location);
  157. COPY_UNALIGNED_HWORD (*hlocation, hvalue, align);
  158. svalue = (int)hvalue;
  159. svalue = (signed char)svalue << 2;
  160. relocation += svalue;
  161. relocation = (relocation >> 2) & 0xff;
  162. hvalue = hvalue & 0xff00;
  163. hvalue += relocation;
  164. COPY_UNALIGNED_HWORD (hvalue, *hlocation, align);
  165. break;
  166. case R_M32R_26_PCREL_RELA:
  167. relocation = (relocation - (Elf32_Addr) location);
  168. if (relocation < -0x2000000 || 0x1fffffc < relocation)
  169. {
  170. printk(KERN_ERR "module %s: relocation overflow: %u\n",
  171. me->name, relocation);
  172. return -ENOEXEC;
  173. }
  174. COPY_UNALIGNED_WORD (*location, value, align);
  175. if (value & 0xffffff)
  176. {
  177. /* RELA must has 0 at relocation field. */
  178. printk(KERN_ERR "module %s: illegal relocation field: %u\n",
  179. me->name, value);
  180. return -ENOEXEC;
  181. }
  182. relocation = (relocation >> 2) & 0xffffff;
  183. value += relocation;
  184. COPY_UNALIGNED_WORD (value, *location, align);
  185. break;
  186. default:
  187. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  188. me->name, ELF32_R_TYPE(rel[i].r_info));
  189. return -ENOEXEC;
  190. }
  191. }
  192. return 0;
  193. }