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 15 #include <linux/elf.h> 16 #include <linux/err.h> 17 #include <linux/errno.h> 18 #include <linux/moduleloader.h> 19 20 static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v) 21 { 22 *(u64 *)location = v; 23 return 0; 24 } 25 26 static int apply_r_riscv_branch_rela(struct module *me, u32 *location, 27 Elf_Addr v) 28 { 29 s64 offset = (void *)v - (void *)location; 30 u32 imm12 = (offset & 0x1000) << (31 - 12); 31 u32 imm11 = (offset & 0x800) >> (11 - 7); 32 u32 imm10_5 = (offset & 0x7e0) << (30 - 10); 33 u32 imm4_1 = (offset & 0x1e) << (11 - 4); 34 35 *location = (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1; 36 return 0; 37 } 38 39 static int apply_r_riscv_jal_rela(struct module *me, u32 *location, 40 Elf_Addr v) 41 { 42 s64 offset = (void *)v - (void *)location; 43 u32 imm20 = (offset & 0x100000) << (31 - 20); 44 u32 imm19_12 = (offset & 0xff000); 45 u32 imm11 = (offset & 0x800) << (20 - 11); 46 u32 imm10_1 = (offset & 0x7fe) << (30 - 10); 47 48 *location = (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1; 49 return 0; 50 } 51 52 static int apply_r_riscv_rcv_branch_rela(struct module *me, u32 *location, 53 Elf_Addr v) 54 { 55 s64 offset = (void *)v - (void *)location; 56 u16 imm8 = (offset & 0x100) << (12 - 8); 57 u16 imm7_6 = (offset & 0xc0) >> (6 - 5); 58 u16 imm5 = (offset & 0x20) >> (5 - 2); 59 u16 imm4_3 = (offset & 0x18) << (12 - 5); 60 u16 imm2_1 = (offset & 0x6) << (12 - 10); 61 62 *(u16 *)location = (*(u16 *)location & 0xe383) | 63 imm8 | imm7_6 | imm5 | imm4_3 | imm2_1; 64 return 0; 65 } 66 67 static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location, 68 Elf_Addr v) 69 { 70 s64 offset = (void *)v - (void *)location; 71 u16 imm11 = (offset & 0x800) << (12 - 11); 72 u16 imm10 = (offset & 0x400) >> (10 - 8); 73 u16 imm9_8 = (offset & 0x300) << (12 - 11); 74 u16 imm7 = (offset & 0x80) >> (7 - 6); 75 u16 imm6 = (offset & 0x40) << (12 - 11); 76 u16 imm5 = (offset & 0x20) >> (5 - 2); 77 u16 imm4 = (offset & 0x10) << (12 - 5); 78 u16 imm3_1 = (offset & 0xe) << (12 - 10); 79 80 *(u16 *)location = (*(u16 *)location & 0xe003) | 81 imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1; 82 return 0; 83 } 84 85 static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location, 86 Elf_Addr v) 87 { 88 s64 offset = (void *)v - (void *)location; 89 s32 hi20; 90 91 if (offset != (s32)offset) { 92 pr_err( 93 "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", 94 me->name, v, location); 95 return -EINVAL; 96 } 97 98 hi20 = (offset + 0x800) & 0xfffff000; 99 *location = (*location & 0xfff) | hi20; 100 return 0; 101 } 102 103 static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *location, 104 Elf_Addr v) 105 { 106 /* 107 * v is the lo12 value to fill. It is calculated before calling this 108 * handler. 109 */ 110 *location = (*location & 0xfffff) | ((v & 0xfff) << 20); 111 return 0; 112 } 113 114 static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location, 115 Elf_Addr v) 116 { 117 /* 118 * v is the lo12 value to fill. It is calculated before calling this 119 * handler. 120 */ 121 u32 imm11_5 = (v & 0xfe0) << (31 - 11); 122 u32 imm4_0 = (v & 0x1f) << (11 - 4); 123 124 *location = (*location & 0x1fff07f) | imm11_5 | imm4_0; 125 return 0; 126 } 127 128 static int apply_r_riscv_hi20_rela(struct module *me, u32 *location, 129 Elf_Addr v) 130 { 131 s32 hi20; 132 133 if (IS_ENABLED(CMODEL_MEDLOW)) { 134 pr_err( 135 "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", 136 me->name, v, location); 137 return -EINVAL; 138 } 139 140 hi20 = ((s32)v + 0x800) & 0xfffff000; 141 *location = (*location & 0xfff) | hi20; 142 return 0; 143 } 144 145 static int apply_r_riscv_lo12_i_rela(struct module *me, u32 *location, 146 Elf_Addr v) 147 { 148 /* Skip medlow checking because of filtering by HI20 already */ 149 s32 hi20 = ((s32)v + 0x800) & 0xfffff000; 150 s32 lo12 = ((s32)v - hi20); 151 *location = (*location & 0xfffff) | ((lo12 & 0xfff) << 20); 152 return 0; 153 } 154 155 static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location, 156 Elf_Addr v) 157 { 158 /* Skip medlow checking because of filtering by HI20 already */ 159 s32 hi20 = ((s32)v + 0x800) & 0xfffff000; 160 s32 lo12 = ((s32)v - hi20); 161 u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11); 162 u32 imm4_0 = (lo12 & 0x1f) << (11 - 4); 163 *location = (*location & 0x1fff07f) | imm11_5 | imm4_0; 164 return 0; 165 } 166 167 static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location, 168 Elf_Addr v) 169 { 170 s64 offset = (void *)v - (void *)location; 171 s32 hi20; 172 173 /* Always emit the got entry */ 174 if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) { 175 offset = module_emit_got_entry(me, v); 176 offset = (void *)offset - (void *)location; 177 } else { 178 pr_err( 179 "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n", 180 me->name, v, location); 181 return -EINVAL; 182 } 183 184 hi20 = (offset + 0x800) & 0xfffff000; 185 *location = (*location & 0xfff) | hi20; 186 return 0; 187 } 188 189 static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location, 190 Elf_Addr v) 191 { 192 s64 offset = (void *)v - (void *)location; 193 s32 fill_v = offset; 194 u32 hi20, lo12; 195 196 if (offset != fill_v) { 197 /* Only emit the plt entry if offset over 32-bit range */ 198 if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) { 199 offset = module_emit_plt_entry(me, v); 200 offset = (void *)offset - (void *)location; 201 } else { 202 pr_err( 203 "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", 204 me->name, v, location); 205 return -EINVAL; 206 } 207 } 208 209 hi20 = (offset + 0x800) & 0xfffff000; 210 lo12 = (offset - hi20) & 0xfff; 211 *location = (*location & 0xfff) | hi20; 212 *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20); 213 return 0; 214 } 215 216 static int apply_r_riscv_call_rela(struct module *me, u32 *location, 217 Elf_Addr v) 218 { 219 s64 offset = (void *)v - (void *)location; 220 s32 fill_v = offset; 221 u32 hi20, lo12; 222 223 if (offset != fill_v) { 224 pr_err( 225 "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", 226 me->name, v, location); 227 return -EINVAL; 228 } 229 230 hi20 = (offset + 0x800) & 0xfffff000; 231 lo12 = (offset - hi20) & 0xfff; 232 *location = (*location & 0xfff) | hi20; 233 *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20); 234 return 0; 235 } 236 237 static int apply_r_riscv_relax_rela(struct module *me, u32 *location, 238 Elf_Addr v) 239 { 240 return 0; 241 } 242 243 static int apply_r_riscv_align_rela(struct module *me, u32 *location, 244 Elf_Addr v) 245 { 246 pr_err( 247 "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n", 248 me->name, location); 249 return -EINVAL; 250 } 251 252 static int apply_r_riscv_add32_rela(struct module *me, u32 *location, 253 Elf_Addr v) 254 { 255 *(u32 *)location += (*(u32 *)v); 256 return 0; 257 } 258 259 static int (*reloc_handlers_rela[]) (struct module *me, u32 *location, 260 Elf_Addr v) = { 261 [R_RISCV_64] = apply_r_riscv_64_rela, 262 [R_RISCV_BRANCH] = apply_r_riscv_branch_rela, 263 [R_RISCV_JAL] = apply_r_riscv_jal_rela, 264 [R_RISCV_RVC_BRANCH] = apply_r_riscv_rcv_branch_rela, 265 [R_RISCV_RVC_JUMP] = apply_r_riscv_rvc_jump_rela, 266 [R_RISCV_PCREL_HI20] = apply_r_riscv_pcrel_hi20_rela, 267 [R_RISCV_PCREL_LO12_I] = apply_r_riscv_pcrel_lo12_i_rela, 268 [R_RISCV_PCREL_LO12_S] = apply_r_riscv_pcrel_lo12_s_rela, 269 [R_RISCV_HI20] = apply_r_riscv_hi20_rela, 270 [R_RISCV_LO12_I] = apply_r_riscv_lo12_i_rela, 271 [R_RISCV_LO12_S] = apply_r_riscv_lo12_s_rela, 272 [R_RISCV_GOT_HI20] = apply_r_riscv_got_hi20_rela, 273 [R_RISCV_CALL_PLT] = apply_r_riscv_call_plt_rela, 274 [R_RISCV_CALL] = apply_r_riscv_call_rela, 275 [R_RISCV_RELAX] = apply_r_riscv_relax_rela, 276 [R_RISCV_ALIGN] = apply_r_riscv_align_rela, 277 [R_RISCV_ADD32] = apply_r_riscv_add32_rela, 278 }; 279 280 int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, 281 unsigned int symindex, unsigned int relsec, 282 struct module *me) 283 { 284 Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr; 285 int (*handler)(struct module *me, u32 *location, Elf_Addr v); 286 Elf_Sym *sym; 287 u32 *location; 288 unsigned int i, type; 289 Elf_Addr v; 290 int res; 291 292 pr_debug("Applying relocate section %u to %u\n", relsec, 293 sechdrs[relsec].sh_info); 294 295 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { 296 /* This is where to make the change */ 297 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr 298 + rel[i].r_offset; 299 /* This is the symbol it is referring to */ 300 sym = (Elf_Sym *)sechdrs[symindex].sh_addr 301 + ELF_RISCV_R_SYM(rel[i].r_info); 302 if (IS_ERR_VALUE(sym->st_value)) { 303 /* Ignore unresolved weak symbol */ 304 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) 305 continue; 306 pr_warning("%s: Unknown symbol %s\n", 307 me->name, strtab + sym->st_name); 308 return -ENOENT; 309 } 310 311 type = ELF_RISCV_R_TYPE(rel[i].r_info); 312 313 if (type < ARRAY_SIZE(reloc_handlers_rela)) 314 handler = reloc_handlers_rela[type]; 315 else 316 handler = NULL; 317 318 if (!handler) { 319 pr_err("%s: Unknown relocation type %u\n", 320 me->name, type); 321 return -EINVAL; 322 } 323 324 v = sym->st_value + rel[i].r_addend; 325 326 if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) { 327 unsigned int j; 328 329 for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) { 330 u64 hi20_loc = 331 sechdrs[sechdrs[relsec].sh_info].sh_addr 332 + rel[j].r_offset; 333 u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info); 334 335 /* Find the corresponding HI20 relocation entry */ 336 if (hi20_loc == sym->st_value 337 && (hi20_type == R_RISCV_PCREL_HI20 338 || hi20_type == R_RISCV_GOT_HI20)) { 339 s32 hi20, lo12; 340 Elf_Sym *hi20_sym = 341 (Elf_Sym *)sechdrs[symindex].sh_addr 342 + ELF_RISCV_R_SYM(rel[j].r_info); 343 u64 hi20_sym_val = 344 hi20_sym->st_value 345 + rel[j].r_addend; 346 347 /* Calculate lo12 */ 348 u64 offset = hi20_sym_val - hi20_loc; 349 if (IS_ENABLED(CONFIG_MODULE_SECTIONS) 350 && hi20_type == R_RISCV_GOT_HI20) { 351 offset = module_emit_got_entry( 352 me, hi20_sym_val); 353 offset = offset - hi20_loc; 354 } 355 hi20 = (offset + 0x800) & 0xfffff000; 356 lo12 = offset - hi20; 357 v = lo12; 358 359 break; 360 } 361 } 362 if (j == sechdrs[relsec].sh_size / sizeof(*rel)) { 363 pr_err( 364 "%s: Can not find HI20 relocation information\n", 365 me->name); 366 return -EINVAL; 367 } 368 } 369 370 res = handler(me, location, v); 371 if (res) 372 return res; 373 } 374 375 return 0; 376 } 377