1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2e2c0cdfbSPalmer Dabbelt /* 3e2c0cdfbSPalmer Dabbelt * 4e2c0cdfbSPalmer Dabbelt * Copyright (C) 2017 Zihao Yu 5e2c0cdfbSPalmer Dabbelt */ 6e2c0cdfbSPalmer Dabbelt 7e2c0cdfbSPalmer Dabbelt #include <linux/elf.h> 8e2c0cdfbSPalmer Dabbelt #include <linux/err.h> 9e2c0cdfbSPalmer Dabbelt #include <linux/errno.h> 10e2c0cdfbSPalmer Dabbelt #include <linux/moduleloader.h> 110cff8bffSVincent Chen #include <linux/vmalloc.h> 120cff8bffSVincent Chen #include <linux/sizes.h> 13ca5999fdSMike Rapoport #include <linux/pgtable.h> 140cff8bffSVincent Chen #include <asm/sections.h> 15e2c0cdfbSPalmer Dabbelt 160966d385SEmil Renner Berthing /* 170966d385SEmil Renner Berthing * The auipc+jalr instruction pair can reach any PC-relative offset 180966d385SEmil Renner Berthing * in the range [-2^31 - 2^11, 2^31 - 2^11) 190966d385SEmil Renner Berthing */ 200966d385SEmil Renner Berthing static bool riscv_insn_valid_32bit_offset(ptrdiff_t val) 210966d385SEmil Renner Berthing { 220966d385SEmil Renner Berthing #ifdef CONFIG_32BIT 230966d385SEmil Renner Berthing return true; 240966d385SEmil Renner Berthing #else 250966d385SEmil Renner Berthing return (-(1L << 31) - (1L << 11)) <= val && val < ((1L << 31) - (1L << 11)); 260966d385SEmil Renner Berthing #endif 270966d385SEmil Renner Berthing } 280966d385SEmil Renner Berthing 2977aa85deSAndreas Schwab static int apply_r_riscv_32_rela(struct module *me, u32 *location, Elf_Addr v) 3077aa85deSAndreas Schwab { 3177aa85deSAndreas Schwab if (v != (u32)v) { 3277aa85deSAndreas Schwab pr_err("%s: value %016llx out of range for 32-bit field\n", 33ef3a6140SOlof Johansson me->name, (long long)v); 3477aa85deSAndreas Schwab return -EINVAL; 3577aa85deSAndreas Schwab } 3677aa85deSAndreas Schwab *location = v; 3777aa85deSAndreas Schwab return 0; 3877aa85deSAndreas Schwab } 3977aa85deSAndreas Schwab 40e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v) 41e2c0cdfbSPalmer Dabbelt { 42e2c0cdfbSPalmer Dabbelt *(u64 *)location = v; 43e2c0cdfbSPalmer Dabbelt return 0; 44e2c0cdfbSPalmer Dabbelt } 45e2c0cdfbSPalmer Dabbelt 46e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_branch_rela(struct module *me, u32 *location, 47e2c0cdfbSPalmer Dabbelt Elf_Addr v) 48e2c0cdfbSPalmer Dabbelt { 497df85002SZong Li ptrdiff_t offset = (void *)v - (void *)location; 50e2c0cdfbSPalmer Dabbelt u32 imm12 = (offset & 0x1000) << (31 - 12); 51e2c0cdfbSPalmer Dabbelt u32 imm11 = (offset & 0x800) >> (11 - 7); 52e2c0cdfbSPalmer Dabbelt u32 imm10_5 = (offset & 0x7e0) << (30 - 10); 53e2c0cdfbSPalmer Dabbelt u32 imm4_1 = (offset & 0x1e) << (11 - 4); 54e2c0cdfbSPalmer Dabbelt 55e2c0cdfbSPalmer Dabbelt *location = (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1; 56e2c0cdfbSPalmer Dabbelt return 0; 57e2c0cdfbSPalmer Dabbelt } 58e2c0cdfbSPalmer Dabbelt 59e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_jal_rela(struct module *me, u32 *location, 60e2c0cdfbSPalmer Dabbelt Elf_Addr v) 61e2c0cdfbSPalmer Dabbelt { 627df85002SZong Li ptrdiff_t offset = (void *)v - (void *)location; 63e2c0cdfbSPalmer Dabbelt u32 imm20 = (offset & 0x100000) << (31 - 20); 64e2c0cdfbSPalmer Dabbelt u32 imm19_12 = (offset & 0xff000); 65e2c0cdfbSPalmer Dabbelt u32 imm11 = (offset & 0x800) << (20 - 11); 66e2c0cdfbSPalmer Dabbelt u32 imm10_1 = (offset & 0x7fe) << (30 - 10); 67e2c0cdfbSPalmer Dabbelt 68e2c0cdfbSPalmer Dabbelt *location = (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1; 69e2c0cdfbSPalmer Dabbelt return 0; 70e2c0cdfbSPalmer Dabbelt } 71e2c0cdfbSPalmer Dabbelt 72*61748760SWu Caize static int apply_r_riscv_rvc_branch_rela(struct module *me, u32 *location, 7356ea45aeSZong Li Elf_Addr v) 7456ea45aeSZong Li { 757df85002SZong Li ptrdiff_t offset = (void *)v - (void *)location; 7656ea45aeSZong Li u16 imm8 = (offset & 0x100) << (12 - 8); 7756ea45aeSZong Li u16 imm7_6 = (offset & 0xc0) >> (6 - 5); 7856ea45aeSZong Li u16 imm5 = (offset & 0x20) >> (5 - 2); 7956ea45aeSZong Li u16 imm4_3 = (offset & 0x18) << (12 - 5); 8056ea45aeSZong Li u16 imm2_1 = (offset & 0x6) << (12 - 10); 8156ea45aeSZong Li 8256ea45aeSZong Li *(u16 *)location = (*(u16 *)location & 0xe383) | 8356ea45aeSZong Li imm8 | imm7_6 | imm5 | imm4_3 | imm2_1; 8456ea45aeSZong Li return 0; 8556ea45aeSZong Li } 8656ea45aeSZong Li 8756ea45aeSZong Li static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location, 8856ea45aeSZong Li Elf_Addr v) 8956ea45aeSZong Li { 907df85002SZong Li ptrdiff_t offset = (void *)v - (void *)location; 9156ea45aeSZong Li u16 imm11 = (offset & 0x800) << (12 - 11); 9256ea45aeSZong Li u16 imm10 = (offset & 0x400) >> (10 - 8); 9356ea45aeSZong Li u16 imm9_8 = (offset & 0x300) << (12 - 11); 9456ea45aeSZong Li u16 imm7 = (offset & 0x80) >> (7 - 6); 9556ea45aeSZong Li u16 imm6 = (offset & 0x40) << (12 - 11); 9656ea45aeSZong Li u16 imm5 = (offset & 0x20) >> (5 - 2); 9756ea45aeSZong Li u16 imm4 = (offset & 0x10) << (12 - 5); 9856ea45aeSZong Li u16 imm3_1 = (offset & 0xe) << (12 - 10); 9956ea45aeSZong Li 10056ea45aeSZong Li *(u16 *)location = (*(u16 *)location & 0xe003) | 10156ea45aeSZong Li imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1; 10256ea45aeSZong Li return 0; 10356ea45aeSZong Li } 10456ea45aeSZong Li 105e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location, 106e2c0cdfbSPalmer Dabbelt Elf_Addr v) 107e2c0cdfbSPalmer Dabbelt { 1087df85002SZong Li ptrdiff_t offset = (void *)v - (void *)location; 109e2c0cdfbSPalmer Dabbelt s32 hi20; 110e2c0cdfbSPalmer Dabbelt 1110966d385SEmil Renner Berthing if (!riscv_insn_valid_32bit_offset(offset)) { 112e2c0cdfbSPalmer Dabbelt pr_err( 113e2c0cdfbSPalmer Dabbelt "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", 114ef3a6140SOlof Johansson me->name, (long long)v, location); 115e2c0cdfbSPalmer Dabbelt return -EINVAL; 116e2c0cdfbSPalmer Dabbelt } 117e2c0cdfbSPalmer Dabbelt 118e2c0cdfbSPalmer Dabbelt hi20 = (offset + 0x800) & 0xfffff000; 119e2c0cdfbSPalmer Dabbelt *location = (*location & 0xfff) | hi20; 120e2c0cdfbSPalmer Dabbelt return 0; 121e2c0cdfbSPalmer Dabbelt } 122e2c0cdfbSPalmer Dabbelt 123e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *location, 124e2c0cdfbSPalmer Dabbelt Elf_Addr v) 125e2c0cdfbSPalmer Dabbelt { 126e2c0cdfbSPalmer Dabbelt /* 127e2c0cdfbSPalmer Dabbelt * v is the lo12 value to fill. It is calculated before calling this 128e2c0cdfbSPalmer Dabbelt * handler. 129e2c0cdfbSPalmer Dabbelt */ 130e2c0cdfbSPalmer Dabbelt *location = (*location & 0xfffff) | ((v & 0xfff) << 20); 131e2c0cdfbSPalmer Dabbelt return 0; 132e2c0cdfbSPalmer Dabbelt } 133e2c0cdfbSPalmer Dabbelt 134e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location, 135e2c0cdfbSPalmer Dabbelt Elf_Addr v) 136e2c0cdfbSPalmer Dabbelt { 137e2c0cdfbSPalmer Dabbelt /* 138e2c0cdfbSPalmer Dabbelt * v is the lo12 value to fill. It is calculated before calling this 139e2c0cdfbSPalmer Dabbelt * handler. 140e2c0cdfbSPalmer Dabbelt */ 141e2c0cdfbSPalmer Dabbelt u32 imm11_5 = (v & 0xfe0) << (31 - 11); 142e2c0cdfbSPalmer Dabbelt u32 imm4_0 = (v & 0x1f) << (11 - 4); 143e2c0cdfbSPalmer Dabbelt 144e2c0cdfbSPalmer Dabbelt *location = (*location & 0x1fff07f) | imm11_5 | imm4_0; 145e2c0cdfbSPalmer Dabbelt return 0; 146e2c0cdfbSPalmer Dabbelt } 147e2c0cdfbSPalmer Dabbelt 148e7456e69SZong Li static int apply_r_riscv_hi20_rela(struct module *me, u32 *location, 149e7456e69SZong Li Elf_Addr v) 150e7456e69SZong Li { 151e7456e69SZong Li s32 hi20; 152e7456e69SZong Li 153da4ed378SJoe Perches if (IS_ENABLED(CONFIG_CMODEL_MEDLOW)) { 154e7456e69SZong Li pr_err( 155e7456e69SZong Li "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", 156ef3a6140SOlof Johansson me->name, (long long)v, location); 157e7456e69SZong Li return -EINVAL; 158e7456e69SZong Li } 159e7456e69SZong Li 160e7456e69SZong Li hi20 = ((s32)v + 0x800) & 0xfffff000; 161e7456e69SZong Li *location = (*location & 0xfff) | hi20; 162e7456e69SZong Li return 0; 163e7456e69SZong Li } 164e7456e69SZong Li 165e7456e69SZong Li static int apply_r_riscv_lo12_i_rela(struct module *me, u32 *location, 166e7456e69SZong Li Elf_Addr v) 167e7456e69SZong Li { 168e7456e69SZong Li /* Skip medlow checking because of filtering by HI20 already */ 169e7456e69SZong Li s32 hi20 = ((s32)v + 0x800) & 0xfffff000; 170e7456e69SZong Li s32 lo12 = ((s32)v - hi20); 171e7456e69SZong Li *location = (*location & 0xfffff) | ((lo12 & 0xfff) << 20); 172e7456e69SZong Li return 0; 173e7456e69SZong Li } 174e7456e69SZong Li 175e7456e69SZong Li static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location, 176e7456e69SZong Li Elf_Addr v) 177e7456e69SZong Li { 178e7456e69SZong Li /* Skip medlow checking because of filtering by HI20 already */ 179e7456e69SZong Li s32 hi20 = ((s32)v + 0x800) & 0xfffff000; 180e7456e69SZong Li s32 lo12 = ((s32)v - hi20); 181e7456e69SZong Li u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11); 182e7456e69SZong Li u32 imm4_0 = (lo12 & 0x1f) << (11 - 4); 183e7456e69SZong Li *location = (*location & 0x1fff07f) | imm11_5 | imm4_0; 184e7456e69SZong Li return 0; 185e7456e69SZong Li } 186e7456e69SZong Li 187da975dd4SZong Li static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location, 188da975dd4SZong Li Elf_Addr v) 189da975dd4SZong Li { 1907df85002SZong Li ptrdiff_t offset = (void *)v - (void *)location; 191da975dd4SZong Li s32 hi20; 192da975dd4SZong Li 193da975dd4SZong Li /* Always emit the got entry */ 194da975dd4SZong Li if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) { 195da975dd4SZong Li offset = module_emit_got_entry(me, v); 196da975dd4SZong Li offset = (void *)offset - (void *)location; 197da975dd4SZong Li } else { 198da975dd4SZong Li pr_err( 199da975dd4SZong Li "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n", 200ef3a6140SOlof Johansson me->name, (long long)v, location); 201da975dd4SZong Li return -EINVAL; 202da975dd4SZong Li } 203da975dd4SZong Li 204da975dd4SZong Li hi20 = (offset + 0x800) & 0xfffff000; 205da975dd4SZong Li *location = (*location & 0xfff) | hi20; 206da975dd4SZong Li return 0; 207da975dd4SZong Li } 208da975dd4SZong Li 209e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location, 210e2c0cdfbSPalmer Dabbelt Elf_Addr v) 211e2c0cdfbSPalmer Dabbelt { 2127df85002SZong Li ptrdiff_t offset = (void *)v - (void *)location; 213e2c0cdfbSPalmer Dabbelt u32 hi20, lo12; 214e2c0cdfbSPalmer Dabbelt 2150966d385SEmil Renner Berthing if (!riscv_insn_valid_32bit_offset(offset)) { 216da975dd4SZong Li /* Only emit the plt entry if offset over 32-bit range */ 217da975dd4SZong Li if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) { 218da975dd4SZong Li offset = module_emit_plt_entry(me, v); 219da975dd4SZong Li offset = (void *)offset - (void *)location; 220da975dd4SZong Li } else { 221e2c0cdfbSPalmer Dabbelt pr_err( 222e2c0cdfbSPalmer Dabbelt "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", 223ef3a6140SOlof Johansson me->name, (long long)v, location); 224e2c0cdfbSPalmer Dabbelt return -EINVAL; 225e2c0cdfbSPalmer Dabbelt } 226da975dd4SZong Li } 227e2c0cdfbSPalmer Dabbelt 228e2c0cdfbSPalmer Dabbelt hi20 = (offset + 0x800) & 0xfffff000; 229e2c0cdfbSPalmer Dabbelt lo12 = (offset - hi20) & 0xfff; 230e2c0cdfbSPalmer Dabbelt *location = (*location & 0xfff) | hi20; 231e2c0cdfbSPalmer Dabbelt *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20); 232e2c0cdfbSPalmer Dabbelt return 0; 233e2c0cdfbSPalmer Dabbelt } 234e2c0cdfbSPalmer Dabbelt 235e1910c72SZong Li static int apply_r_riscv_call_rela(struct module *me, u32 *location, 236e1910c72SZong Li Elf_Addr v) 237e1910c72SZong Li { 2387df85002SZong Li ptrdiff_t offset = (void *)v - (void *)location; 239e1910c72SZong Li u32 hi20, lo12; 240e1910c72SZong Li 2410966d385SEmil Renner Berthing if (!riscv_insn_valid_32bit_offset(offset)) { 242e1910c72SZong Li pr_err( 243e1910c72SZong Li "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", 244ef3a6140SOlof Johansson me->name, (long long)v, location); 245e1910c72SZong Li return -EINVAL; 246e1910c72SZong Li } 247e1910c72SZong Li 248e1910c72SZong Li hi20 = (offset + 0x800) & 0xfffff000; 249e1910c72SZong Li lo12 = (offset - hi20) & 0xfff; 250e1910c72SZong Li *location = (*location & 0xfff) | hi20; 251e1910c72SZong Li *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20); 252e1910c72SZong Li return 0; 253e1910c72SZong Li } 254e1910c72SZong Li 255e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_relax_rela(struct module *me, u32 *location, 256e2c0cdfbSPalmer Dabbelt Elf_Addr v) 257e2c0cdfbSPalmer Dabbelt { 258e2c0cdfbSPalmer Dabbelt return 0; 259e2c0cdfbSPalmer Dabbelt } 260e2c0cdfbSPalmer Dabbelt 26129e405cdSZong Li static int apply_r_riscv_align_rela(struct module *me, u32 *location, 26229e405cdSZong Li Elf_Addr v) 26329e405cdSZong Li { 26429e405cdSZong Li pr_err( 26529e405cdSZong Li "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n", 26629e405cdSZong Li me->name, location); 26729e405cdSZong Li return -EINVAL; 26829e405cdSZong Li } 26929e405cdSZong Li 2708e691b16SZong Li static int apply_r_riscv_add32_rela(struct module *me, u32 *location, 2718e691b16SZong Li Elf_Addr v) 2728e691b16SZong Li { 273781c8fe2SAndreas Schwab *(u32 *)location += (u32)v; 2748e691b16SZong Li return 0; 2758e691b16SZong Li } 2768e691b16SZong Li 27711a54f42SEmil Renner Berthing static int apply_r_riscv_add64_rela(struct module *me, u32 *location, 27811a54f42SEmil Renner Berthing Elf_Addr v) 27911a54f42SEmil Renner Berthing { 28011a54f42SEmil Renner Berthing *(u64 *)location += (u64)v; 28111a54f42SEmil Renner Berthing return 0; 28211a54f42SEmil Renner Berthing } 28311a54f42SEmil Renner Berthing 2844aad074cSZong Li static int apply_r_riscv_sub32_rela(struct module *me, u32 *location, 2854aad074cSZong Li Elf_Addr v) 2864aad074cSZong Li { 287781c8fe2SAndreas Schwab *(u32 *)location -= (u32)v; 2884aad074cSZong Li return 0; 2894aad074cSZong Li } 2904aad074cSZong Li 29111a54f42SEmil Renner Berthing static int apply_r_riscv_sub64_rela(struct module *me, u32 *location, 29211a54f42SEmil Renner Berthing Elf_Addr v) 29311a54f42SEmil Renner Berthing { 29411a54f42SEmil Renner Berthing *(u64 *)location -= (u64)v; 29511a54f42SEmil Renner Berthing return 0; 29611a54f42SEmil Renner Berthing } 29711a54f42SEmil Renner Berthing 298e2c0cdfbSPalmer Dabbelt static int (*reloc_handlers_rela[]) (struct module *me, u32 *location, 299e2c0cdfbSPalmer Dabbelt Elf_Addr v) = { 30077aa85deSAndreas Schwab [R_RISCV_32] = apply_r_riscv_32_rela, 301e2c0cdfbSPalmer Dabbelt [R_RISCV_64] = apply_r_riscv_64_rela, 302e2c0cdfbSPalmer Dabbelt [R_RISCV_BRANCH] = apply_r_riscv_branch_rela, 303e2c0cdfbSPalmer Dabbelt [R_RISCV_JAL] = apply_r_riscv_jal_rela, 304*61748760SWu Caize [R_RISCV_RVC_BRANCH] = apply_r_riscv_rvc_branch_rela, 30556ea45aeSZong Li [R_RISCV_RVC_JUMP] = apply_r_riscv_rvc_jump_rela, 306e2c0cdfbSPalmer Dabbelt [R_RISCV_PCREL_HI20] = apply_r_riscv_pcrel_hi20_rela, 307e2c0cdfbSPalmer Dabbelt [R_RISCV_PCREL_LO12_I] = apply_r_riscv_pcrel_lo12_i_rela, 308e2c0cdfbSPalmer Dabbelt [R_RISCV_PCREL_LO12_S] = apply_r_riscv_pcrel_lo12_s_rela, 309e7456e69SZong Li [R_RISCV_HI20] = apply_r_riscv_hi20_rela, 310e7456e69SZong Li [R_RISCV_LO12_I] = apply_r_riscv_lo12_i_rela, 311e7456e69SZong Li [R_RISCV_LO12_S] = apply_r_riscv_lo12_s_rela, 312da975dd4SZong Li [R_RISCV_GOT_HI20] = apply_r_riscv_got_hi20_rela, 313e2c0cdfbSPalmer Dabbelt [R_RISCV_CALL_PLT] = apply_r_riscv_call_plt_rela, 314e1910c72SZong Li [R_RISCV_CALL] = apply_r_riscv_call_rela, 315e2c0cdfbSPalmer Dabbelt [R_RISCV_RELAX] = apply_r_riscv_relax_rela, 31629e405cdSZong Li [R_RISCV_ALIGN] = apply_r_riscv_align_rela, 3178e691b16SZong Li [R_RISCV_ADD32] = apply_r_riscv_add32_rela, 31811a54f42SEmil Renner Berthing [R_RISCV_ADD64] = apply_r_riscv_add64_rela, 3194aad074cSZong Li [R_RISCV_SUB32] = apply_r_riscv_sub32_rela, 32011a54f42SEmil Renner Berthing [R_RISCV_SUB64] = apply_r_riscv_sub64_rela, 321e2c0cdfbSPalmer Dabbelt }; 322e2c0cdfbSPalmer Dabbelt 323e2c0cdfbSPalmer Dabbelt int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, 324e2c0cdfbSPalmer Dabbelt unsigned int symindex, unsigned int relsec, 325e2c0cdfbSPalmer Dabbelt struct module *me) 326e2c0cdfbSPalmer Dabbelt { 327e2c0cdfbSPalmer Dabbelt Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr; 328e2c0cdfbSPalmer Dabbelt int (*handler)(struct module *me, u32 *location, Elf_Addr v); 329e2c0cdfbSPalmer Dabbelt Elf_Sym *sym; 330e2c0cdfbSPalmer Dabbelt u32 *location; 331e2c0cdfbSPalmer Dabbelt unsigned int i, type; 332e2c0cdfbSPalmer Dabbelt Elf_Addr v; 333e2c0cdfbSPalmer Dabbelt int res; 334e2c0cdfbSPalmer Dabbelt 335e2c0cdfbSPalmer Dabbelt pr_debug("Applying relocate section %u to %u\n", relsec, 336e2c0cdfbSPalmer Dabbelt sechdrs[relsec].sh_info); 337e2c0cdfbSPalmer Dabbelt 338e2c0cdfbSPalmer Dabbelt for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { 339e2c0cdfbSPalmer Dabbelt /* This is where to make the change */ 340e2c0cdfbSPalmer Dabbelt location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr 341e2c0cdfbSPalmer Dabbelt + rel[i].r_offset; 342e2c0cdfbSPalmer Dabbelt /* This is the symbol it is referring to */ 343e2c0cdfbSPalmer Dabbelt sym = (Elf_Sym *)sechdrs[symindex].sh_addr 344e2c0cdfbSPalmer Dabbelt + ELF_RISCV_R_SYM(rel[i].r_info); 345e2c0cdfbSPalmer Dabbelt if (IS_ERR_VALUE(sym->st_value)) { 346e2c0cdfbSPalmer Dabbelt /* Ignore unresolved weak symbol */ 347e2c0cdfbSPalmer Dabbelt if (ELF_ST_BIND(sym->st_info) == STB_WEAK) 348e2c0cdfbSPalmer Dabbelt continue; 34904ce8d3fSKefeng Wang pr_warn("%s: Unknown symbol %s\n", 350e2c0cdfbSPalmer Dabbelt me->name, strtab + sym->st_name); 351e2c0cdfbSPalmer Dabbelt return -ENOENT; 352e2c0cdfbSPalmer Dabbelt } 353e2c0cdfbSPalmer Dabbelt 354e2c0cdfbSPalmer Dabbelt type = ELF_RISCV_R_TYPE(rel[i].r_info); 355e2c0cdfbSPalmer Dabbelt 356e2c0cdfbSPalmer Dabbelt if (type < ARRAY_SIZE(reloc_handlers_rela)) 357e2c0cdfbSPalmer Dabbelt handler = reloc_handlers_rela[type]; 358e2c0cdfbSPalmer Dabbelt else 359e2c0cdfbSPalmer Dabbelt handler = NULL; 360e2c0cdfbSPalmer Dabbelt 361e2c0cdfbSPalmer Dabbelt if (!handler) { 362e2c0cdfbSPalmer Dabbelt pr_err("%s: Unknown relocation type %u\n", 363e2c0cdfbSPalmer Dabbelt me->name, type); 364e2c0cdfbSPalmer Dabbelt return -EINVAL; 365e2c0cdfbSPalmer Dabbelt } 366e2c0cdfbSPalmer Dabbelt 367e2c0cdfbSPalmer Dabbelt v = sym->st_value + rel[i].r_addend; 368e2c0cdfbSPalmer Dabbelt 369e2c0cdfbSPalmer Dabbelt if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) { 370e2c0cdfbSPalmer Dabbelt unsigned int j; 371e2c0cdfbSPalmer Dabbelt 372e2c0cdfbSPalmer Dabbelt for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) { 3737df85002SZong Li unsigned long hi20_loc = 374e2c0cdfbSPalmer Dabbelt sechdrs[sechdrs[relsec].sh_info].sh_addr 375e2c0cdfbSPalmer Dabbelt + rel[j].r_offset; 376da975dd4SZong Li u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info); 377da975dd4SZong Li 378da975dd4SZong Li /* Find the corresponding HI20 relocation entry */ 379da975dd4SZong Li if (hi20_loc == sym->st_value 380da975dd4SZong Li && (hi20_type == R_RISCV_PCREL_HI20 381da975dd4SZong Li || hi20_type == R_RISCV_GOT_HI20)) { 382da975dd4SZong Li s32 hi20, lo12; 383e2c0cdfbSPalmer Dabbelt Elf_Sym *hi20_sym = 384e2c0cdfbSPalmer Dabbelt (Elf_Sym *)sechdrs[symindex].sh_addr 385e2c0cdfbSPalmer Dabbelt + ELF_RISCV_R_SYM(rel[j].r_info); 3867df85002SZong Li unsigned long hi20_sym_val = 387e2c0cdfbSPalmer Dabbelt hi20_sym->st_value 388e2c0cdfbSPalmer Dabbelt + rel[j].r_addend; 389da975dd4SZong Li 390e2c0cdfbSPalmer Dabbelt /* Calculate lo12 */ 3917df85002SZong Li size_t offset = hi20_sym_val - hi20_loc; 392da975dd4SZong Li if (IS_ENABLED(CONFIG_MODULE_SECTIONS) 393da975dd4SZong Li && hi20_type == R_RISCV_GOT_HI20) { 394da975dd4SZong Li offset = module_emit_got_entry( 395da975dd4SZong Li me, hi20_sym_val); 396da975dd4SZong Li offset = offset - hi20_loc; 397da975dd4SZong Li } 398da975dd4SZong Li hi20 = (offset + 0x800) & 0xfffff000; 399da975dd4SZong Li lo12 = offset - hi20; 400e2c0cdfbSPalmer Dabbelt v = lo12; 401da975dd4SZong Li 402e2c0cdfbSPalmer Dabbelt break; 403e2c0cdfbSPalmer Dabbelt } 404e2c0cdfbSPalmer Dabbelt } 405e2c0cdfbSPalmer Dabbelt if (j == sechdrs[relsec].sh_size / sizeof(*rel)) { 406e2c0cdfbSPalmer Dabbelt pr_err( 407da975dd4SZong Li "%s: Can not find HI20 relocation information\n", 408e2c0cdfbSPalmer Dabbelt me->name); 409e2c0cdfbSPalmer Dabbelt return -EINVAL; 410e2c0cdfbSPalmer Dabbelt } 411e2c0cdfbSPalmer Dabbelt } 412e2c0cdfbSPalmer Dabbelt 413e2c0cdfbSPalmer Dabbelt res = handler(me, location, v); 414e2c0cdfbSPalmer Dabbelt if (res) 415e2c0cdfbSPalmer Dabbelt return res; 416e2c0cdfbSPalmer Dabbelt } 417e2c0cdfbSPalmer Dabbelt 418e2c0cdfbSPalmer Dabbelt return 0; 419e2c0cdfbSPalmer Dabbelt } 4200cff8bffSVincent Chen 4210cff8bffSVincent Chen #if defined(CONFIG_MMU) && defined(CONFIG_64BIT) 4220cff8bffSVincent Chen void *module_alloc(unsigned long size) 4230cff8bffSVincent Chen { 4242bfc6cd8SAlexandre Ghiti return __vmalloc_node_range(size, 1, MODULES_VADDR, 4252bfc6cd8SAlexandre Ghiti MODULES_END, GFP_KERNEL, 4265387054bSJisheng Zhang PAGE_KERNEL, 0, NUMA_NO_NODE, 4270cff8bffSVincent Chen __builtin_return_address(0)); 4280cff8bffSVincent Chen } 4290cff8bffSVincent Chen #endif 430