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*da975dd4SZong Li static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location, 96*da975dd4SZong Li Elf_Addr v) 97*da975dd4SZong Li { 98*da975dd4SZong Li s64 offset = (void *)v - (void *)location; 99*da975dd4SZong Li s32 hi20; 100*da975dd4SZong Li 101*da975dd4SZong Li /* Always emit the got entry */ 102*da975dd4SZong Li if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) { 103*da975dd4SZong Li offset = module_emit_got_entry(me, v); 104*da975dd4SZong Li offset = (void *)offset - (void *)location; 105*da975dd4SZong Li } else { 106*da975dd4SZong Li pr_err( 107*da975dd4SZong Li "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n", 108*da975dd4SZong Li me->name, v, location); 109*da975dd4SZong Li return -EINVAL; 110*da975dd4SZong Li } 111*da975dd4SZong Li 112*da975dd4SZong Li hi20 = (offset + 0x800) & 0xfffff000; 113*da975dd4SZong Li *location = (*location & 0xfff) | hi20; 114*da975dd4SZong Li return 0; 115*da975dd4SZong Li } 116*da975dd4SZong Li 117e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location, 118e2c0cdfbSPalmer Dabbelt Elf_Addr v) 119e2c0cdfbSPalmer Dabbelt { 120e2c0cdfbSPalmer Dabbelt s64 offset = (void *)v - (void *)location; 121e2c0cdfbSPalmer Dabbelt s32 fill_v = offset; 122e2c0cdfbSPalmer Dabbelt u32 hi20, lo12; 123e2c0cdfbSPalmer Dabbelt 124e2c0cdfbSPalmer Dabbelt if (offset != fill_v) { 125*da975dd4SZong Li /* Only emit the plt entry if offset over 32-bit range */ 126*da975dd4SZong Li if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) { 127*da975dd4SZong Li offset = module_emit_plt_entry(me, v); 128*da975dd4SZong Li offset = (void *)offset - (void *)location; 129*da975dd4SZong Li } else { 130e2c0cdfbSPalmer Dabbelt pr_err( 131e2c0cdfbSPalmer Dabbelt "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", 132e2c0cdfbSPalmer Dabbelt me->name, v, location); 133e2c0cdfbSPalmer Dabbelt return -EINVAL; 134e2c0cdfbSPalmer Dabbelt } 135*da975dd4SZong Li } 136e2c0cdfbSPalmer Dabbelt 137e2c0cdfbSPalmer Dabbelt hi20 = (offset + 0x800) & 0xfffff000; 138e2c0cdfbSPalmer Dabbelt lo12 = (offset - hi20) & 0xfff; 139e2c0cdfbSPalmer Dabbelt *location = (*location & 0xfff) | hi20; 140e2c0cdfbSPalmer Dabbelt *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20); 141e2c0cdfbSPalmer Dabbelt return 0; 142e2c0cdfbSPalmer Dabbelt } 143e2c0cdfbSPalmer Dabbelt 144e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_relax_rela(struct module *me, u32 *location, 145e2c0cdfbSPalmer Dabbelt Elf_Addr v) 146e2c0cdfbSPalmer Dabbelt { 147e2c0cdfbSPalmer Dabbelt return 0; 148e2c0cdfbSPalmer Dabbelt } 149e2c0cdfbSPalmer Dabbelt 150e2c0cdfbSPalmer Dabbelt static int (*reloc_handlers_rela[]) (struct module *me, u32 *location, 151e2c0cdfbSPalmer Dabbelt Elf_Addr v) = { 152e2c0cdfbSPalmer Dabbelt [R_RISCV_64] = apply_r_riscv_64_rela, 153e2c0cdfbSPalmer Dabbelt [R_RISCV_BRANCH] = apply_r_riscv_branch_rela, 154e2c0cdfbSPalmer Dabbelt [R_RISCV_JAL] = apply_r_riscv_jal_rela, 155e2c0cdfbSPalmer Dabbelt [R_RISCV_PCREL_HI20] = apply_r_riscv_pcrel_hi20_rela, 156e2c0cdfbSPalmer Dabbelt [R_RISCV_PCREL_LO12_I] = apply_r_riscv_pcrel_lo12_i_rela, 157e2c0cdfbSPalmer Dabbelt [R_RISCV_PCREL_LO12_S] = apply_r_riscv_pcrel_lo12_s_rela, 158*da975dd4SZong Li [R_RISCV_GOT_HI20] = apply_r_riscv_got_hi20_rela, 159e2c0cdfbSPalmer Dabbelt [R_RISCV_CALL_PLT] = apply_r_riscv_call_plt_rela, 160e2c0cdfbSPalmer Dabbelt [R_RISCV_RELAX] = apply_r_riscv_relax_rela, 161e2c0cdfbSPalmer Dabbelt }; 162e2c0cdfbSPalmer Dabbelt 163e2c0cdfbSPalmer Dabbelt int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, 164e2c0cdfbSPalmer Dabbelt unsigned int symindex, unsigned int relsec, 165e2c0cdfbSPalmer Dabbelt struct module *me) 166e2c0cdfbSPalmer Dabbelt { 167e2c0cdfbSPalmer Dabbelt Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr; 168e2c0cdfbSPalmer Dabbelt int (*handler)(struct module *me, u32 *location, Elf_Addr v); 169e2c0cdfbSPalmer Dabbelt Elf_Sym *sym; 170e2c0cdfbSPalmer Dabbelt u32 *location; 171e2c0cdfbSPalmer Dabbelt unsigned int i, type; 172e2c0cdfbSPalmer Dabbelt Elf_Addr v; 173e2c0cdfbSPalmer Dabbelt int res; 174e2c0cdfbSPalmer Dabbelt 175e2c0cdfbSPalmer Dabbelt pr_debug("Applying relocate section %u to %u\n", relsec, 176e2c0cdfbSPalmer Dabbelt sechdrs[relsec].sh_info); 177e2c0cdfbSPalmer Dabbelt 178e2c0cdfbSPalmer Dabbelt for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { 179e2c0cdfbSPalmer Dabbelt /* This is where to make the change */ 180e2c0cdfbSPalmer Dabbelt location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr 181e2c0cdfbSPalmer Dabbelt + rel[i].r_offset; 182e2c0cdfbSPalmer Dabbelt /* This is the symbol it is referring to */ 183e2c0cdfbSPalmer Dabbelt sym = (Elf_Sym *)sechdrs[symindex].sh_addr 184e2c0cdfbSPalmer Dabbelt + ELF_RISCV_R_SYM(rel[i].r_info); 185e2c0cdfbSPalmer Dabbelt if (IS_ERR_VALUE(sym->st_value)) { 186e2c0cdfbSPalmer Dabbelt /* Ignore unresolved weak symbol */ 187e2c0cdfbSPalmer Dabbelt if (ELF_ST_BIND(sym->st_info) == STB_WEAK) 188e2c0cdfbSPalmer Dabbelt continue; 189e2c0cdfbSPalmer Dabbelt pr_warning("%s: Unknown symbol %s\n", 190e2c0cdfbSPalmer Dabbelt me->name, strtab + sym->st_name); 191e2c0cdfbSPalmer Dabbelt return -ENOENT; 192e2c0cdfbSPalmer Dabbelt } 193e2c0cdfbSPalmer Dabbelt 194e2c0cdfbSPalmer Dabbelt type = ELF_RISCV_R_TYPE(rel[i].r_info); 195e2c0cdfbSPalmer Dabbelt 196e2c0cdfbSPalmer Dabbelt if (type < ARRAY_SIZE(reloc_handlers_rela)) 197e2c0cdfbSPalmer Dabbelt handler = reloc_handlers_rela[type]; 198e2c0cdfbSPalmer Dabbelt else 199e2c0cdfbSPalmer Dabbelt handler = NULL; 200e2c0cdfbSPalmer Dabbelt 201e2c0cdfbSPalmer Dabbelt if (!handler) { 202e2c0cdfbSPalmer Dabbelt pr_err("%s: Unknown relocation type %u\n", 203e2c0cdfbSPalmer Dabbelt me->name, type); 204e2c0cdfbSPalmer Dabbelt return -EINVAL; 205e2c0cdfbSPalmer Dabbelt } 206e2c0cdfbSPalmer Dabbelt 207e2c0cdfbSPalmer Dabbelt v = sym->st_value + rel[i].r_addend; 208e2c0cdfbSPalmer Dabbelt 209e2c0cdfbSPalmer Dabbelt if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) { 210e2c0cdfbSPalmer Dabbelt unsigned int j; 211e2c0cdfbSPalmer Dabbelt 212e2c0cdfbSPalmer Dabbelt for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) { 213e2c0cdfbSPalmer Dabbelt u64 hi20_loc = 214e2c0cdfbSPalmer Dabbelt sechdrs[sechdrs[relsec].sh_info].sh_addr 215e2c0cdfbSPalmer Dabbelt + rel[j].r_offset; 216*da975dd4SZong Li u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info); 217*da975dd4SZong Li 218*da975dd4SZong Li /* Find the corresponding HI20 relocation entry */ 219*da975dd4SZong Li if (hi20_loc == sym->st_value 220*da975dd4SZong Li && (hi20_type == R_RISCV_PCREL_HI20 221*da975dd4SZong Li || hi20_type == R_RISCV_GOT_HI20)) { 222*da975dd4SZong Li s32 hi20, lo12; 223e2c0cdfbSPalmer Dabbelt Elf_Sym *hi20_sym = 224e2c0cdfbSPalmer Dabbelt (Elf_Sym *)sechdrs[symindex].sh_addr 225e2c0cdfbSPalmer Dabbelt + ELF_RISCV_R_SYM(rel[j].r_info); 226e2c0cdfbSPalmer Dabbelt u64 hi20_sym_val = 227e2c0cdfbSPalmer Dabbelt hi20_sym->st_value 228e2c0cdfbSPalmer Dabbelt + rel[j].r_addend; 229*da975dd4SZong Li 230e2c0cdfbSPalmer Dabbelt /* Calculate lo12 */ 231*da975dd4SZong Li u64 offset = hi20_sym_val - hi20_loc; 232*da975dd4SZong Li if (IS_ENABLED(CONFIG_MODULE_SECTIONS) 233*da975dd4SZong Li && hi20_type == R_RISCV_GOT_HI20) { 234*da975dd4SZong Li offset = module_emit_got_entry( 235*da975dd4SZong Li me, hi20_sym_val); 236*da975dd4SZong Li offset = offset - hi20_loc; 237*da975dd4SZong Li } 238*da975dd4SZong Li hi20 = (offset + 0x800) & 0xfffff000; 239*da975dd4SZong Li lo12 = offset - hi20; 240e2c0cdfbSPalmer Dabbelt v = lo12; 241*da975dd4SZong Li 242e2c0cdfbSPalmer Dabbelt break; 243e2c0cdfbSPalmer Dabbelt } 244e2c0cdfbSPalmer Dabbelt } 245e2c0cdfbSPalmer Dabbelt if (j == sechdrs[relsec].sh_size / sizeof(*rel)) { 246e2c0cdfbSPalmer Dabbelt pr_err( 247*da975dd4SZong Li "%s: Can not find HI20 relocation information\n", 248e2c0cdfbSPalmer Dabbelt me->name); 249e2c0cdfbSPalmer Dabbelt return -EINVAL; 250e2c0cdfbSPalmer Dabbelt } 251e2c0cdfbSPalmer Dabbelt } 252e2c0cdfbSPalmer Dabbelt 253e2c0cdfbSPalmer Dabbelt res = handler(me, location, v); 254e2c0cdfbSPalmer Dabbelt if (res) 255e2c0cdfbSPalmer Dabbelt return res; 256e2c0cdfbSPalmer Dabbelt } 257e2c0cdfbSPalmer Dabbelt 258e2c0cdfbSPalmer Dabbelt return 0; 259e2c0cdfbSPalmer Dabbelt } 260