module-rela.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  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. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. * Copyright (C) 2001 Rusty Russell.
  17. * Copyright (C) 2003, 2004 Ralf Baechle (ralf@linux-mips.org)
  18. * Copyright (C) 2005 Thiemo Seufer
  19. * Copyright (C) 2015 Imagination Technologies Ltd.
  20. */
  21. #include <linux/elf.h>
  22. #include <linux/err.h>
  23. #include <linux/errno.h>
  24. #include <linux/moduleloader.h>
  25. extern int apply_r_mips_none(struct module *me, u32 *location, Elf_Addr v);
  26. static int apply_r_mips_32_rela(struct module *me, u32 *location, Elf_Addr v)
  27. {
  28. *location = v;
  29. return 0;
  30. }
  31. static int apply_r_mips_26_rela(struct module *me, u32 *location, Elf_Addr v)
  32. {
  33. if (v % 4) {
  34. pr_err("module %s: dangerous R_MIPS_26 RELA relocation\n",
  35. me->name);
  36. return -ENOEXEC;
  37. }
  38. if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  39. pr_err("module %s: relocation overflow\n", me->name);
  40. return -ENOEXEC;
  41. }
  42. *location = (*location & ~0x03ffffff) | ((v >> 2) & 0x03ffffff);
  43. return 0;
  44. }
  45. static int apply_r_mips_hi16_rela(struct module *me, u32 *location, Elf_Addr v)
  46. {
  47. *location = (*location & 0xffff0000) |
  48. ((((long long) v + 0x8000LL) >> 16) & 0xffff);
  49. return 0;
  50. }
  51. static int apply_r_mips_lo16_rela(struct module *me, u32 *location, Elf_Addr v)
  52. {
  53. *location = (*location & 0xffff0000) | (v & 0xffff);
  54. return 0;
  55. }
  56. static int apply_r_mips_pc_rela(struct module *me, u32 *location, Elf_Addr v,
  57. unsigned bits)
  58. {
  59. unsigned long mask = GENMASK(bits - 1, 0);
  60. unsigned long se_bits;
  61. long offset;
  62. if (v % 4) {
  63. pr_err("module %s: dangerous R_MIPS_PC%u RELA relocation\n",
  64. me->name, bits);
  65. return -ENOEXEC;
  66. }
  67. offset = ((long)v - (long)location) >> 2;
  68. /* check the sign bit onwards are identical - ie. we didn't overflow */
  69. se_bits = (offset & BIT(bits - 1)) ? ~0ul : 0;
  70. if ((offset & ~mask) != (se_bits & ~mask)) {
  71. pr_err("module %s: relocation overflow\n", me->name);
  72. return -ENOEXEC;
  73. }
  74. *location = (*location & ~mask) | (offset & mask);
  75. return 0;
  76. }
  77. static int apply_r_mips_pc16_rela(struct module *me, u32 *location, Elf_Addr v)
  78. {
  79. return apply_r_mips_pc_rela(me, location, v, 16);
  80. }
  81. static int apply_r_mips_pc21_rela(struct module *me, u32 *location, Elf_Addr v)
  82. {
  83. return apply_r_mips_pc_rela(me, location, v, 21);
  84. }
  85. static int apply_r_mips_pc26_rela(struct module *me, u32 *location, Elf_Addr v)
  86. {
  87. return apply_r_mips_pc_rela(me, location, v, 26);
  88. }
  89. static int apply_r_mips_64_rela(struct module *me, u32 *location, Elf_Addr v)
  90. {
  91. *(Elf_Addr *)location = v;
  92. return 0;
  93. }
  94. static int apply_r_mips_higher_rela(struct module *me, u32 *location,
  95. Elf_Addr v)
  96. {
  97. *location = (*location & 0xffff0000) |
  98. ((((long long) v + 0x80008000LL) >> 32) & 0xffff);
  99. return 0;
  100. }
  101. static int apply_r_mips_highest_rela(struct module *me, u32 *location,
  102. Elf_Addr v)
  103. {
  104. *location = (*location & 0xffff0000) |
  105. ((((long long) v + 0x800080008000LL) >> 48) & 0xffff);
  106. return 0;
  107. }
  108. static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
  109. Elf_Addr v) = {
  110. [R_MIPS_NONE] = apply_r_mips_none,
  111. [R_MIPS_32] = apply_r_mips_32_rela,
  112. [R_MIPS_26] = apply_r_mips_26_rela,
  113. [R_MIPS_HI16] = apply_r_mips_hi16_rela,
  114. [R_MIPS_LO16] = apply_r_mips_lo16_rela,
  115. [R_MIPS_PC16] = apply_r_mips_pc16_rela,
  116. [R_MIPS_64] = apply_r_mips_64_rela,
  117. [R_MIPS_HIGHER] = apply_r_mips_higher_rela,
  118. [R_MIPS_HIGHEST] = apply_r_mips_highest_rela,
  119. [R_MIPS_PC21_S2] = apply_r_mips_pc21_rela,
  120. [R_MIPS_PC26_S2] = apply_r_mips_pc26_rela,
  121. };
  122. int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  123. unsigned int symindex, unsigned int relsec,
  124. struct module *me)
  125. {
  126. Elf_Mips_Rela *rel = (void *) sechdrs[relsec].sh_addr;
  127. int (*handler)(struct module *me, u32 *location, Elf_Addr v);
  128. Elf_Sym *sym;
  129. u32 *location;
  130. unsigned int i, type;
  131. Elf_Addr v;
  132. int res;
  133. pr_debug("Applying relocate section %u to %u\n", relsec,
  134. sechdrs[relsec].sh_info);
  135. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  136. /* This is where to make the change */
  137. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  138. + rel[i].r_offset;
  139. /* This is the symbol it is referring to */
  140. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  141. + ELF_MIPS_R_SYM(rel[i]);
  142. if (sym->st_value >= -MAX_ERRNO) {
  143. /* Ignore unresolved weak symbol */
  144. if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
  145. continue;
  146. pr_warn("%s: Unknown symbol %s\n",
  147. me->name, strtab + sym->st_name);
  148. return -ENOENT;
  149. }
  150. type = ELF_MIPS_R_TYPE(rel[i]);
  151. if (type < ARRAY_SIZE(reloc_handlers_rela))
  152. handler = reloc_handlers_rela[type];
  153. else
  154. handler = NULL;
  155. if (!handler) {
  156. pr_err("%s: Unknown relocation type %u\n",
  157. me->name, type);
  158. return -EINVAL;
  159. }
  160. v = sym->st_value + rel[i].r_addend;
  161. res = handler(me, location, v);
  162. if (res)
  163. return res;
  164. }
  165. return 0;
  166. }