module.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. * Copyright (C) 2017 Zihao Yu
  13. */
  14. #include <linux/elf.h>
  15. #include <linux/err.h>
  16. #include <linux/errno.h>
  17. #include <linux/moduleloader.h>
  18. static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
  19. {
  20. *(u64 *)location = v;
  21. return 0;
  22. }
  23. static int apply_r_riscv_branch_rela(struct module *me, u32 *location,
  24. Elf_Addr v)
  25. {
  26. s64 offset = (void *)v - (void *)location;
  27. u32 imm12 = (offset & 0x1000) << (31 - 12);
  28. u32 imm11 = (offset & 0x800) >> (11 - 7);
  29. u32 imm10_5 = (offset & 0x7e0) << (30 - 10);
  30. u32 imm4_1 = (offset & 0x1e) << (11 - 4);
  31. *location = (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1;
  32. return 0;
  33. }
  34. static int apply_r_riscv_jal_rela(struct module *me, u32 *location,
  35. Elf_Addr v)
  36. {
  37. s64 offset = (void *)v - (void *)location;
  38. u32 imm20 = (offset & 0x100000) << (31 - 20);
  39. u32 imm19_12 = (offset & 0xff000);
  40. u32 imm11 = (offset & 0x800) << (20 - 11);
  41. u32 imm10_1 = (offset & 0x7fe) << (30 - 10);
  42. *location = (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1;
  43. return 0;
  44. }
  45. static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location,
  46. Elf_Addr v)
  47. {
  48. s64 offset = (void *)v - (void *)location;
  49. s32 hi20;
  50. if (offset != (s32)offset) {
  51. pr_err(
  52. "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
  53. me->name, v, location);
  54. return -EINVAL;
  55. }
  56. hi20 = (offset + 0x800) & 0xfffff000;
  57. *location = (*location & 0xfff) | hi20;
  58. return 0;
  59. }
  60. static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *location,
  61. Elf_Addr v)
  62. {
  63. /*
  64. * v is the lo12 value to fill. It is calculated before calling this
  65. * handler.
  66. */
  67. *location = (*location & 0xfffff) | ((v & 0xfff) << 20);
  68. return 0;
  69. }
  70. static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location,
  71. Elf_Addr v)
  72. {
  73. /*
  74. * v is the lo12 value to fill. It is calculated before calling this
  75. * handler.
  76. */
  77. u32 imm11_5 = (v & 0xfe0) << (31 - 11);
  78. u32 imm4_0 = (v & 0x1f) << (11 - 4);
  79. *location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
  80. return 0;
  81. }
  82. static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location,
  83. Elf_Addr v)
  84. {
  85. s64 offset = (void *)v - (void *)location;
  86. s32 fill_v = offset;
  87. u32 hi20, lo12;
  88. if (offset != fill_v) {
  89. pr_err(
  90. "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
  91. me->name, v, location);
  92. return -EINVAL;
  93. }
  94. hi20 = (offset + 0x800) & 0xfffff000;
  95. lo12 = (offset - hi20) & 0xfff;
  96. *location = (*location & 0xfff) | hi20;
  97. *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
  98. return 0;
  99. }
  100. static int apply_r_riscv_relax_rela(struct module *me, u32 *location,
  101. Elf_Addr v)
  102. {
  103. return 0;
  104. }
  105. static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
  106. Elf_Addr v) = {
  107. [R_RISCV_64] = apply_r_riscv_64_rela,
  108. [R_RISCV_BRANCH] = apply_r_riscv_branch_rela,
  109. [R_RISCV_JAL] = apply_r_riscv_jal_rela,
  110. [R_RISCV_PCREL_HI20] = apply_r_riscv_pcrel_hi20_rela,
  111. [R_RISCV_PCREL_LO12_I] = apply_r_riscv_pcrel_lo12_i_rela,
  112. [R_RISCV_PCREL_LO12_S] = apply_r_riscv_pcrel_lo12_s_rela,
  113. [R_RISCV_CALL_PLT] = apply_r_riscv_call_plt_rela,
  114. [R_RISCV_RELAX] = apply_r_riscv_relax_rela,
  115. };
  116. int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  117. unsigned int symindex, unsigned int relsec,
  118. struct module *me)
  119. {
  120. Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
  121. int (*handler)(struct module *me, u32 *location, Elf_Addr v);
  122. Elf_Sym *sym;
  123. u32 *location;
  124. unsigned int i, type;
  125. Elf_Addr v;
  126. int res;
  127. pr_debug("Applying relocate section %u to %u\n", relsec,
  128. sechdrs[relsec].sh_info);
  129. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  130. /* This is where to make the change */
  131. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  132. + rel[i].r_offset;
  133. /* This is the symbol it is referring to */
  134. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  135. + ELF_RISCV_R_SYM(rel[i].r_info);
  136. if (IS_ERR_VALUE(sym->st_value)) {
  137. /* Ignore unresolved weak symbol */
  138. if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
  139. continue;
  140. pr_warning("%s: Unknown symbol %s\n",
  141. me->name, strtab + sym->st_name);
  142. return -ENOENT;
  143. }
  144. type = ELF_RISCV_R_TYPE(rel[i].r_info);
  145. if (type < ARRAY_SIZE(reloc_handlers_rela))
  146. handler = reloc_handlers_rela[type];
  147. else
  148. handler = NULL;
  149. if (!handler) {
  150. pr_err("%s: Unknown relocation type %u\n",
  151. me->name, type);
  152. return -EINVAL;
  153. }
  154. v = sym->st_value + rel[i].r_addend;
  155. if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
  156. unsigned int j;
  157. for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
  158. u64 hi20_loc =
  159. sechdrs[sechdrs[relsec].sh_info].sh_addr
  160. + rel[j].r_offset;
  161. /* Find the corresponding HI20 PC-relative relocation entry */
  162. if (hi20_loc == sym->st_value) {
  163. Elf_Sym *hi20_sym =
  164. (Elf_Sym *)sechdrs[symindex].sh_addr
  165. + ELF_RISCV_R_SYM(rel[j].r_info);
  166. u64 hi20_sym_val =
  167. hi20_sym->st_value
  168. + rel[j].r_addend;
  169. /* Calculate lo12 */
  170. s64 offset = hi20_sym_val - hi20_loc;
  171. s32 hi20 = (offset + 0x800) & 0xfffff000;
  172. s32 lo12 = offset - hi20;
  173. v = lo12;
  174. break;
  175. }
  176. }
  177. if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
  178. pr_err(
  179. "%s: Can not find HI20 PC-relative relocation information\n",
  180. me->name);
  181. return -EINVAL;
  182. }
  183. }
  184. res = handler(me, location, v);
  185. if (res)
  186. return res;
  187. }
  188. return 0;
  189. }