1e2c0cdfbSPalmer Dabbelt /* 2e2c0cdfbSPalmer Dabbelt * This program is free software; you can redistribute it and/or modify 3e2c0cdfbSPalmer Dabbelt * it under the terms of the GNU General Public License as published by 4e2c0cdfbSPalmer Dabbelt * the Free Software Foundation; either version 2 of the License, or 5e2c0cdfbSPalmer Dabbelt * (at your option) any later version. 6e2c0cdfbSPalmer Dabbelt * 7e2c0cdfbSPalmer Dabbelt * This program is distributed in the hope that it will be useful, 8e2c0cdfbSPalmer Dabbelt * but WITHOUT ANY WARRANTY; without even the implied warranty of 9e2c0cdfbSPalmer Dabbelt * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10e2c0cdfbSPalmer Dabbelt * GNU General Public License for more details. 11e2c0cdfbSPalmer Dabbelt * 12e2c0cdfbSPalmer Dabbelt * Copyright (C) 2017 Zihao Yu 13e2c0cdfbSPalmer Dabbelt */ 14e2c0cdfbSPalmer Dabbelt 15e2c0cdfbSPalmer Dabbelt #include <linux/elf.h> 16e2c0cdfbSPalmer Dabbelt #include <linux/err.h> 17e2c0cdfbSPalmer Dabbelt #include <linux/errno.h> 18e2c0cdfbSPalmer Dabbelt #include <linux/moduleloader.h> 19e2c0cdfbSPalmer Dabbelt 20e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v) 21e2c0cdfbSPalmer Dabbelt { 22e2c0cdfbSPalmer Dabbelt *(u64 *)location = v; 23e2c0cdfbSPalmer Dabbelt return 0; 24e2c0cdfbSPalmer Dabbelt } 25e2c0cdfbSPalmer Dabbelt 26e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_branch_rela(struct module *me, u32 *location, 27e2c0cdfbSPalmer Dabbelt Elf_Addr v) 28e2c0cdfbSPalmer Dabbelt { 29e2c0cdfbSPalmer Dabbelt s64 offset = (void *)v - (void *)location; 30e2c0cdfbSPalmer Dabbelt u32 imm12 = (offset & 0x1000) << (31 - 12); 31e2c0cdfbSPalmer Dabbelt u32 imm11 = (offset & 0x800) >> (11 - 7); 32e2c0cdfbSPalmer Dabbelt u32 imm10_5 = (offset & 0x7e0) << (30 - 10); 33e2c0cdfbSPalmer Dabbelt u32 imm4_1 = (offset & 0x1e) << (11 - 4); 34e2c0cdfbSPalmer Dabbelt 35e2c0cdfbSPalmer Dabbelt *location = (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1; 36e2c0cdfbSPalmer Dabbelt return 0; 37e2c0cdfbSPalmer Dabbelt } 38e2c0cdfbSPalmer Dabbelt 39e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_jal_rela(struct module *me, u32 *location, 40e2c0cdfbSPalmer Dabbelt Elf_Addr v) 41e2c0cdfbSPalmer Dabbelt { 42e2c0cdfbSPalmer Dabbelt s64 offset = (void *)v - (void *)location; 43e2c0cdfbSPalmer Dabbelt u32 imm20 = (offset & 0x100000) << (31 - 20); 44e2c0cdfbSPalmer Dabbelt u32 imm19_12 = (offset & 0xff000); 45e2c0cdfbSPalmer Dabbelt u32 imm11 = (offset & 0x800) << (20 - 11); 46e2c0cdfbSPalmer Dabbelt u32 imm10_1 = (offset & 0x7fe) << (30 - 10); 47e2c0cdfbSPalmer Dabbelt 48e2c0cdfbSPalmer Dabbelt *location = (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1; 49e2c0cdfbSPalmer Dabbelt return 0; 50e2c0cdfbSPalmer Dabbelt } 51e2c0cdfbSPalmer Dabbelt 52e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location, 53e2c0cdfbSPalmer Dabbelt Elf_Addr v) 54e2c0cdfbSPalmer Dabbelt { 55e2c0cdfbSPalmer Dabbelt s64 offset = (void *)v - (void *)location; 56e2c0cdfbSPalmer Dabbelt s32 hi20; 57e2c0cdfbSPalmer Dabbelt 58e2c0cdfbSPalmer Dabbelt if (offset != (s32)offset) { 59e2c0cdfbSPalmer Dabbelt pr_err( 60e2c0cdfbSPalmer Dabbelt "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", 61e2c0cdfbSPalmer Dabbelt me->name, v, location); 62e2c0cdfbSPalmer Dabbelt return -EINVAL; 63e2c0cdfbSPalmer Dabbelt } 64e2c0cdfbSPalmer Dabbelt 65e2c0cdfbSPalmer Dabbelt hi20 = (offset + 0x800) & 0xfffff000; 66e2c0cdfbSPalmer Dabbelt *location = (*location & 0xfff) | hi20; 67e2c0cdfbSPalmer Dabbelt return 0; 68e2c0cdfbSPalmer Dabbelt } 69e2c0cdfbSPalmer Dabbelt 70e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *location, 71e2c0cdfbSPalmer Dabbelt Elf_Addr v) 72e2c0cdfbSPalmer Dabbelt { 73e2c0cdfbSPalmer Dabbelt /* 74e2c0cdfbSPalmer Dabbelt * v is the lo12 value to fill. It is calculated before calling this 75e2c0cdfbSPalmer Dabbelt * handler. 76e2c0cdfbSPalmer Dabbelt */ 77e2c0cdfbSPalmer Dabbelt *location = (*location & 0xfffff) | ((v & 0xfff) << 20); 78e2c0cdfbSPalmer Dabbelt return 0; 79e2c0cdfbSPalmer Dabbelt } 80e2c0cdfbSPalmer Dabbelt 81e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location, 82e2c0cdfbSPalmer Dabbelt Elf_Addr v) 83e2c0cdfbSPalmer Dabbelt { 84e2c0cdfbSPalmer Dabbelt /* 85e2c0cdfbSPalmer Dabbelt * v is the lo12 value to fill. It is calculated before calling this 86e2c0cdfbSPalmer Dabbelt * handler. 87e2c0cdfbSPalmer Dabbelt */ 88e2c0cdfbSPalmer Dabbelt u32 imm11_5 = (v & 0xfe0) << (31 - 11); 89e2c0cdfbSPalmer Dabbelt u32 imm4_0 = (v & 0x1f) << (11 - 4); 90e2c0cdfbSPalmer Dabbelt 91e2c0cdfbSPalmer Dabbelt *location = (*location & 0x1fff07f) | imm11_5 | imm4_0; 92e2c0cdfbSPalmer Dabbelt return 0; 93e2c0cdfbSPalmer Dabbelt } 94e2c0cdfbSPalmer Dabbelt 95*e7456e69SZong Li static int apply_r_riscv_hi20_rela(struct module *me, u32 *location, 96*e7456e69SZong Li Elf_Addr v) 97*e7456e69SZong Li { 98*e7456e69SZong Li s32 hi20; 99*e7456e69SZong Li 100*e7456e69SZong Li if (IS_ENABLED(CMODEL_MEDLOW)) { 101*e7456e69SZong Li pr_err( 102*e7456e69SZong Li "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", 103*e7456e69SZong Li me->name, v, location); 104*e7456e69SZong Li return -EINVAL; 105*e7456e69SZong Li } 106*e7456e69SZong Li 107*e7456e69SZong Li hi20 = ((s32)v + 0x800) & 0xfffff000; 108*e7456e69SZong Li *location = (*location & 0xfff) | hi20; 109*e7456e69SZong Li return 0; 110*e7456e69SZong Li } 111*e7456e69SZong Li 112*e7456e69SZong Li static int apply_r_riscv_lo12_i_rela(struct module *me, u32 *location, 113*e7456e69SZong Li Elf_Addr v) 114*e7456e69SZong Li { 115*e7456e69SZong Li /* Skip medlow checking because of filtering by HI20 already */ 116*e7456e69SZong Li s32 hi20 = ((s32)v + 0x800) & 0xfffff000; 117*e7456e69SZong Li s32 lo12 = ((s32)v - hi20); 118*e7456e69SZong Li *location = (*location & 0xfffff) | ((lo12 & 0xfff) << 20); 119*e7456e69SZong Li return 0; 120*e7456e69SZong Li } 121*e7456e69SZong Li 122*e7456e69SZong Li static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location, 123*e7456e69SZong Li Elf_Addr v) 124*e7456e69SZong Li { 125*e7456e69SZong Li /* Skip medlow checking because of filtering by HI20 already */ 126*e7456e69SZong Li s32 hi20 = ((s32)v + 0x800) & 0xfffff000; 127*e7456e69SZong Li s32 lo12 = ((s32)v - hi20); 128*e7456e69SZong Li u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11); 129*e7456e69SZong Li u32 imm4_0 = (lo12 & 0x1f) << (11 - 4); 130*e7456e69SZong Li *location = (*location & 0x1fff07f) | imm11_5 | imm4_0; 131*e7456e69SZong Li return 0; 132*e7456e69SZong Li } 133*e7456e69SZong Li 134da975dd4SZong Li static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location, 135da975dd4SZong Li Elf_Addr v) 136da975dd4SZong Li { 137da975dd4SZong Li s64 offset = (void *)v - (void *)location; 138da975dd4SZong Li s32 hi20; 139da975dd4SZong Li 140da975dd4SZong Li /* Always emit the got entry */ 141da975dd4SZong Li if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) { 142da975dd4SZong Li offset = module_emit_got_entry(me, v); 143da975dd4SZong Li offset = (void *)offset - (void *)location; 144da975dd4SZong Li } else { 145da975dd4SZong Li pr_err( 146da975dd4SZong Li "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n", 147da975dd4SZong Li me->name, v, location); 148da975dd4SZong Li return -EINVAL; 149da975dd4SZong Li } 150da975dd4SZong Li 151da975dd4SZong Li hi20 = (offset + 0x800) & 0xfffff000; 152da975dd4SZong Li *location = (*location & 0xfff) | hi20; 153da975dd4SZong Li return 0; 154da975dd4SZong Li } 155da975dd4SZong Li 156e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location, 157e2c0cdfbSPalmer Dabbelt Elf_Addr v) 158e2c0cdfbSPalmer Dabbelt { 159e2c0cdfbSPalmer Dabbelt s64 offset = (void *)v - (void *)location; 160e2c0cdfbSPalmer Dabbelt s32 fill_v = offset; 161e2c0cdfbSPalmer Dabbelt u32 hi20, lo12; 162e2c0cdfbSPalmer Dabbelt 163e2c0cdfbSPalmer Dabbelt if (offset != fill_v) { 164da975dd4SZong Li /* Only emit the plt entry if offset over 32-bit range */ 165da975dd4SZong Li if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) { 166da975dd4SZong Li offset = module_emit_plt_entry(me, v); 167da975dd4SZong Li offset = (void *)offset - (void *)location; 168da975dd4SZong Li } else { 169e2c0cdfbSPalmer Dabbelt pr_err( 170e2c0cdfbSPalmer Dabbelt "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", 171e2c0cdfbSPalmer Dabbelt me->name, v, location); 172e2c0cdfbSPalmer Dabbelt return -EINVAL; 173e2c0cdfbSPalmer Dabbelt } 174da975dd4SZong Li } 175e2c0cdfbSPalmer Dabbelt 176e2c0cdfbSPalmer Dabbelt hi20 = (offset + 0x800) & 0xfffff000; 177e2c0cdfbSPalmer Dabbelt lo12 = (offset - hi20) & 0xfff; 178e2c0cdfbSPalmer Dabbelt *location = (*location & 0xfff) | hi20; 179e2c0cdfbSPalmer Dabbelt *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20); 180e2c0cdfbSPalmer Dabbelt return 0; 181e2c0cdfbSPalmer Dabbelt } 182e2c0cdfbSPalmer Dabbelt 183e1910c72SZong Li static int apply_r_riscv_call_rela(struct module *me, u32 *location, 184e1910c72SZong Li Elf_Addr v) 185e1910c72SZong Li { 186e1910c72SZong Li s64 offset = (void *)v - (void *)location; 187e1910c72SZong Li s32 fill_v = offset; 188e1910c72SZong Li u32 hi20, lo12; 189e1910c72SZong Li 190e1910c72SZong Li if (offset != fill_v) { 191e1910c72SZong Li pr_err( 192e1910c72SZong Li "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", 193e1910c72SZong Li me->name, v, location); 194e1910c72SZong Li return -EINVAL; 195e1910c72SZong Li } 196e1910c72SZong Li 197e1910c72SZong Li hi20 = (offset + 0x800) & 0xfffff000; 198e1910c72SZong Li lo12 = (offset - hi20) & 0xfff; 199e1910c72SZong Li *location = (*location & 0xfff) | hi20; 200e1910c72SZong Li *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20); 201e1910c72SZong Li return 0; 202e1910c72SZong Li } 203e1910c72SZong Li 204e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_relax_rela(struct module *me, u32 *location, 205e2c0cdfbSPalmer Dabbelt Elf_Addr v) 206e2c0cdfbSPalmer Dabbelt { 207e2c0cdfbSPalmer Dabbelt return 0; 208e2c0cdfbSPalmer Dabbelt } 209e2c0cdfbSPalmer Dabbelt 210e2c0cdfbSPalmer Dabbelt static int (*reloc_handlers_rela[]) (struct module *me, u32 *location, 211e2c0cdfbSPalmer Dabbelt Elf_Addr v) = { 212e2c0cdfbSPalmer Dabbelt [R_RISCV_64] = apply_r_riscv_64_rela, 213e2c0cdfbSPalmer Dabbelt [R_RISCV_BRANCH] = apply_r_riscv_branch_rela, 214e2c0cdfbSPalmer Dabbelt [R_RISCV_JAL] = apply_r_riscv_jal_rela, 215e2c0cdfbSPalmer Dabbelt [R_RISCV_PCREL_HI20] = apply_r_riscv_pcrel_hi20_rela, 216e2c0cdfbSPalmer Dabbelt [R_RISCV_PCREL_LO12_I] = apply_r_riscv_pcrel_lo12_i_rela, 217e2c0cdfbSPalmer Dabbelt [R_RISCV_PCREL_LO12_S] = apply_r_riscv_pcrel_lo12_s_rela, 218*e7456e69SZong Li [R_RISCV_HI20] = apply_r_riscv_hi20_rela, 219*e7456e69SZong Li [R_RISCV_LO12_I] = apply_r_riscv_lo12_i_rela, 220*e7456e69SZong Li [R_RISCV_LO12_S] = apply_r_riscv_lo12_s_rela, 221da975dd4SZong Li [R_RISCV_GOT_HI20] = apply_r_riscv_got_hi20_rela, 222e2c0cdfbSPalmer Dabbelt [R_RISCV_CALL_PLT] = apply_r_riscv_call_plt_rela, 223e1910c72SZong Li [R_RISCV_CALL] = apply_r_riscv_call_rela, 224e2c0cdfbSPalmer Dabbelt [R_RISCV_RELAX] = apply_r_riscv_relax_rela, 225e2c0cdfbSPalmer Dabbelt }; 226e2c0cdfbSPalmer Dabbelt 227e2c0cdfbSPalmer Dabbelt int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, 228e2c0cdfbSPalmer Dabbelt unsigned int symindex, unsigned int relsec, 229e2c0cdfbSPalmer Dabbelt struct module *me) 230e2c0cdfbSPalmer Dabbelt { 231e2c0cdfbSPalmer Dabbelt Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr; 232e2c0cdfbSPalmer Dabbelt int (*handler)(struct module *me, u32 *location, Elf_Addr v); 233e2c0cdfbSPalmer Dabbelt Elf_Sym *sym; 234e2c0cdfbSPalmer Dabbelt u32 *location; 235e2c0cdfbSPalmer Dabbelt unsigned int i, type; 236e2c0cdfbSPalmer Dabbelt Elf_Addr v; 237e2c0cdfbSPalmer Dabbelt int res; 238e2c0cdfbSPalmer Dabbelt 239e2c0cdfbSPalmer Dabbelt pr_debug("Applying relocate section %u to %u\n", relsec, 240e2c0cdfbSPalmer Dabbelt sechdrs[relsec].sh_info); 241e2c0cdfbSPalmer Dabbelt 242e2c0cdfbSPalmer Dabbelt for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { 243e2c0cdfbSPalmer Dabbelt /* This is where to make the change */ 244e2c0cdfbSPalmer Dabbelt location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr 245e2c0cdfbSPalmer Dabbelt + rel[i].r_offset; 246e2c0cdfbSPalmer Dabbelt /* This is the symbol it is referring to */ 247e2c0cdfbSPalmer Dabbelt sym = (Elf_Sym *)sechdrs[symindex].sh_addr 248e2c0cdfbSPalmer Dabbelt + ELF_RISCV_R_SYM(rel[i].r_info); 249e2c0cdfbSPalmer Dabbelt if (IS_ERR_VALUE(sym->st_value)) { 250e2c0cdfbSPalmer Dabbelt /* Ignore unresolved weak symbol */ 251e2c0cdfbSPalmer Dabbelt if (ELF_ST_BIND(sym->st_info) == STB_WEAK) 252e2c0cdfbSPalmer Dabbelt continue; 253e2c0cdfbSPalmer Dabbelt pr_warning("%s: Unknown symbol %s\n", 254e2c0cdfbSPalmer Dabbelt me->name, strtab + sym->st_name); 255e2c0cdfbSPalmer Dabbelt return -ENOENT; 256e2c0cdfbSPalmer Dabbelt } 257e2c0cdfbSPalmer Dabbelt 258e2c0cdfbSPalmer Dabbelt type = ELF_RISCV_R_TYPE(rel[i].r_info); 259e2c0cdfbSPalmer Dabbelt 260e2c0cdfbSPalmer Dabbelt if (type < ARRAY_SIZE(reloc_handlers_rela)) 261e2c0cdfbSPalmer Dabbelt handler = reloc_handlers_rela[type]; 262e2c0cdfbSPalmer Dabbelt else 263e2c0cdfbSPalmer Dabbelt handler = NULL; 264e2c0cdfbSPalmer Dabbelt 265e2c0cdfbSPalmer Dabbelt if (!handler) { 266e2c0cdfbSPalmer Dabbelt pr_err("%s: Unknown relocation type %u\n", 267e2c0cdfbSPalmer Dabbelt me->name, type); 268e2c0cdfbSPalmer Dabbelt return -EINVAL; 269e2c0cdfbSPalmer Dabbelt } 270e2c0cdfbSPalmer Dabbelt 271e2c0cdfbSPalmer Dabbelt v = sym->st_value + rel[i].r_addend; 272e2c0cdfbSPalmer Dabbelt 273e2c0cdfbSPalmer Dabbelt if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) { 274e2c0cdfbSPalmer Dabbelt unsigned int j; 275e2c0cdfbSPalmer Dabbelt 276e2c0cdfbSPalmer Dabbelt for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) { 277e2c0cdfbSPalmer Dabbelt u64 hi20_loc = 278e2c0cdfbSPalmer Dabbelt sechdrs[sechdrs[relsec].sh_info].sh_addr 279e2c0cdfbSPalmer Dabbelt + rel[j].r_offset; 280da975dd4SZong Li u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info); 281da975dd4SZong Li 282da975dd4SZong Li /* Find the corresponding HI20 relocation entry */ 283da975dd4SZong Li if (hi20_loc == sym->st_value 284da975dd4SZong Li && (hi20_type == R_RISCV_PCREL_HI20 285da975dd4SZong Li || hi20_type == R_RISCV_GOT_HI20)) { 286da975dd4SZong Li s32 hi20, lo12; 287e2c0cdfbSPalmer Dabbelt Elf_Sym *hi20_sym = 288e2c0cdfbSPalmer Dabbelt (Elf_Sym *)sechdrs[symindex].sh_addr 289e2c0cdfbSPalmer Dabbelt + ELF_RISCV_R_SYM(rel[j].r_info); 290e2c0cdfbSPalmer Dabbelt u64 hi20_sym_val = 291e2c0cdfbSPalmer Dabbelt hi20_sym->st_value 292e2c0cdfbSPalmer Dabbelt + rel[j].r_addend; 293da975dd4SZong Li 294e2c0cdfbSPalmer Dabbelt /* Calculate lo12 */ 295da975dd4SZong Li u64 offset = hi20_sym_val - hi20_loc; 296da975dd4SZong Li if (IS_ENABLED(CONFIG_MODULE_SECTIONS) 297da975dd4SZong Li && hi20_type == R_RISCV_GOT_HI20) { 298da975dd4SZong Li offset = module_emit_got_entry( 299da975dd4SZong Li me, hi20_sym_val); 300da975dd4SZong Li offset = offset - hi20_loc; 301da975dd4SZong Li } 302da975dd4SZong Li hi20 = (offset + 0x800) & 0xfffff000; 303da975dd4SZong Li lo12 = offset - hi20; 304e2c0cdfbSPalmer Dabbelt v = lo12; 305da975dd4SZong Li 306e2c0cdfbSPalmer Dabbelt break; 307e2c0cdfbSPalmer Dabbelt } 308e2c0cdfbSPalmer Dabbelt } 309e2c0cdfbSPalmer Dabbelt if (j == sechdrs[relsec].sh_size / sizeof(*rel)) { 310e2c0cdfbSPalmer Dabbelt pr_err( 311da975dd4SZong Li "%s: Can not find HI20 relocation information\n", 312e2c0cdfbSPalmer Dabbelt me->name); 313e2c0cdfbSPalmer Dabbelt return -EINVAL; 314e2c0cdfbSPalmer Dabbelt } 315e2c0cdfbSPalmer Dabbelt } 316e2c0cdfbSPalmer Dabbelt 317e2c0cdfbSPalmer Dabbelt res = handler(me, location, v); 318e2c0cdfbSPalmer Dabbelt if (res) 319e2c0cdfbSPalmer Dabbelt return res; 320e2c0cdfbSPalmer Dabbelt } 321e2c0cdfbSPalmer Dabbelt 322e2c0cdfbSPalmer Dabbelt return 0; 323e2c0cdfbSPalmer Dabbelt } 324