1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2257cb251SWill Deacon /* 3257cb251SWill Deacon * AArch64 loadable module support. 4257cb251SWill Deacon * 5257cb251SWill Deacon * Copyright (C) 2012 ARM Limited 6257cb251SWill Deacon * 7257cb251SWill Deacon * Author: Will Deacon <will.deacon@arm.com> 8257cb251SWill Deacon */ 9257cb251SWill Deacon 10257cb251SWill Deacon #include <linux/bitops.h> 11257cb251SWill Deacon #include <linux/elf.h> 12f1a54ae9SMark Rutland #include <linux/ftrace.h> 13257cb251SWill Deacon #include <linux/gfp.h> 1439d114ddSAndrey Ryabinin #include <linux/kasan.h> 15257cb251SWill Deacon #include <linux/kernel.h> 16257cb251SWill Deacon #include <linux/mm.h> 17257cb251SWill Deacon #include <linux/moduleloader.h> 18257cb251SWill Deacon #include <linux/vmalloc.h> 192c2b282dSPaul Walmsley #include <asm/alternative.h> 20c84fced8SJiang Liu #include <asm/insn.h> 21932ded4bSAndre Przywara #include <asm/sections.h> 22c84fced8SJiang Liu 23257cb251SWill Deacon void *module_alloc(unsigned long size) 24257cb251SWill Deacon { 256f496a55SArd Biesheuvel u64 module_alloc_end = module_alloc_base + MODULES_VSIZE; 260c2cf6d9SFlorian Fainelli gfp_t gfp_mask = GFP_KERNEL; 2739d114ddSAndrey Ryabinin void *p; 2839d114ddSAndrey Ryabinin 290c2cf6d9SFlorian Fainelli /* Silence the initial allocation */ 300c2cf6d9SFlorian Fainelli if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS)) 310c2cf6d9SFlorian Fainelli gfp_mask |= __GFP_NOWARN; 320c2cf6d9SFlorian Fainelli 336f496a55SArd Biesheuvel if (IS_ENABLED(CONFIG_KASAN)) 346f496a55SArd Biesheuvel /* don't exceed the static module region - see below */ 356f496a55SArd Biesheuvel module_alloc_end = MODULES_END; 366f496a55SArd Biesheuvel 37f80fb3a3SArd Biesheuvel p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base, 38dfd437a2SLinus Torvalds module_alloc_end, gfp_mask, PAGE_KERNEL, 0, 39cb9e3c29SAndrey Ryabinin NUMA_NO_NODE, __builtin_return_address(0)); 4039d114ddSAndrey Ryabinin 41fd045f6cSArd Biesheuvel if (!p && IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) && 42fd045f6cSArd Biesheuvel !IS_ENABLED(CONFIG_KASAN)) 43fd045f6cSArd Biesheuvel /* 44fd045f6cSArd Biesheuvel * KASAN can only deal with module allocations being served 45fd045f6cSArd Biesheuvel * from the reserved module region, since the remainder of 46fd045f6cSArd Biesheuvel * the vmalloc region is already backed by zero shadow pages, 47fd045f6cSArd Biesheuvel * and punching holes into it is non-trivial. Since the module 48fd045f6cSArd Biesheuvel * region is not randomized when KASAN is enabled, it is even 49fd045f6cSArd Biesheuvel * less likely that the module region gets exhausted, so we 50fd045f6cSArd Biesheuvel * can simply omit this fallback in that case. 51fd045f6cSArd Biesheuvel */ 52f2b9ba87SArd Biesheuvel p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base, 53b2eed9b5SArd Biesheuvel module_alloc_base + SZ_2G, GFP_KERNEL, 547dfac3c5SArd Biesheuvel PAGE_KERNEL, 0, NUMA_NO_NODE, 55f2b9ba87SArd Biesheuvel __builtin_return_address(0)); 56fd045f6cSArd Biesheuvel 5739d114ddSAndrey Ryabinin if (p && (kasan_module_alloc(p, size) < 0)) { 5839d114ddSAndrey Ryabinin vfree(p); 5939d114ddSAndrey Ryabinin return NULL; 6039d114ddSAndrey Ryabinin } 6139d114ddSAndrey Ryabinin 6239d114ddSAndrey Ryabinin return p; 63257cb251SWill Deacon } 64257cb251SWill Deacon 65257cb251SWill Deacon enum aarch64_reloc_op { 66257cb251SWill Deacon RELOC_OP_NONE, 67257cb251SWill Deacon RELOC_OP_ABS, 68257cb251SWill Deacon RELOC_OP_PREL, 69257cb251SWill Deacon RELOC_OP_PAGE, 70257cb251SWill Deacon }; 71257cb251SWill Deacon 7202129ae5SLuc Van Oostenryck static u64 do_reloc(enum aarch64_reloc_op reloc_op, __le32 *place, u64 val) 73257cb251SWill Deacon { 74257cb251SWill Deacon switch (reloc_op) { 75257cb251SWill Deacon case RELOC_OP_ABS: 76257cb251SWill Deacon return val; 77257cb251SWill Deacon case RELOC_OP_PREL: 78257cb251SWill Deacon return val - (u64)place; 79257cb251SWill Deacon case RELOC_OP_PAGE: 80257cb251SWill Deacon return (val & ~0xfff) - ((u64)place & ~0xfff); 81257cb251SWill Deacon case RELOC_OP_NONE: 82257cb251SWill Deacon return 0; 83257cb251SWill Deacon } 84257cb251SWill Deacon 85257cb251SWill Deacon pr_err("do_reloc: unknown relocation operation %d\n", reloc_op); 86257cb251SWill Deacon return 0; 87257cb251SWill Deacon } 88257cb251SWill Deacon 89257cb251SWill Deacon static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len) 90257cb251SWill Deacon { 91257cb251SWill Deacon s64 sval = do_reloc(op, place, val); 92257cb251SWill Deacon 931cf24a2cSArd Biesheuvel /* 941cf24a2cSArd Biesheuvel * The ELF psABI for AArch64 documents the 16-bit and 32-bit place 953fd00bebSArd Biesheuvel * relative and absolute relocations as having a range of [-2^15, 2^16) 963fd00bebSArd Biesheuvel * or [-2^31, 2^32), respectively. However, in order to be able to 973fd00bebSArd Biesheuvel * detect overflows reliably, we have to choose whether we interpret 983fd00bebSArd Biesheuvel * such quantities as signed or as unsigned, and stick with it. 991cf24a2cSArd Biesheuvel * The way we organize our address space requires a signed 1001cf24a2cSArd Biesheuvel * interpretation of 32-bit relative references, so let's use that 1011cf24a2cSArd Biesheuvel * for all R_AARCH64_PRELxx relocations. This means our upper 1021cf24a2cSArd Biesheuvel * bound for overflow detection should be Sxx_MAX rather than Uxx_MAX. 1031cf24a2cSArd Biesheuvel */ 1041cf24a2cSArd Biesheuvel 105257cb251SWill Deacon switch (len) { 106257cb251SWill Deacon case 16: 107257cb251SWill Deacon *(s16 *)place = sval; 1083fd00bebSArd Biesheuvel switch (op) { 1093fd00bebSArd Biesheuvel case RELOC_OP_ABS: 1103fd00bebSArd Biesheuvel if (sval < 0 || sval > U16_MAX) 1113fd00bebSArd Biesheuvel return -ERANGE; 1123fd00bebSArd Biesheuvel break; 1133fd00bebSArd Biesheuvel case RELOC_OP_PREL: 1141cf24a2cSArd Biesheuvel if (sval < S16_MIN || sval > S16_MAX) 115f9308969SArd Biesheuvel return -ERANGE; 116257cb251SWill Deacon break; 1173fd00bebSArd Biesheuvel default: 1183fd00bebSArd Biesheuvel pr_err("Invalid 16-bit data relocation (%d)\n", op); 1193fd00bebSArd Biesheuvel return 0; 1203fd00bebSArd Biesheuvel } 1213fd00bebSArd Biesheuvel break; 122257cb251SWill Deacon case 32: 123257cb251SWill Deacon *(s32 *)place = sval; 1243fd00bebSArd Biesheuvel switch (op) { 1253fd00bebSArd Biesheuvel case RELOC_OP_ABS: 1263fd00bebSArd Biesheuvel if (sval < 0 || sval > U32_MAX) 1273fd00bebSArd Biesheuvel return -ERANGE; 1283fd00bebSArd Biesheuvel break; 1293fd00bebSArd Biesheuvel case RELOC_OP_PREL: 1301cf24a2cSArd Biesheuvel if (sval < S32_MIN || sval > S32_MAX) 131f9308969SArd Biesheuvel return -ERANGE; 132257cb251SWill Deacon break; 1333fd00bebSArd Biesheuvel default: 1343fd00bebSArd Biesheuvel pr_err("Invalid 32-bit data relocation (%d)\n", op); 1353fd00bebSArd Biesheuvel return 0; 1363fd00bebSArd Biesheuvel } 1373fd00bebSArd Biesheuvel break; 138257cb251SWill Deacon case 64: 139257cb251SWill Deacon *(s64 *)place = sval; 140257cb251SWill Deacon break; 141257cb251SWill Deacon default: 142257cb251SWill Deacon pr_err("Invalid length (%d) for data relocation\n", len); 143257cb251SWill Deacon return 0; 144257cb251SWill Deacon } 145257cb251SWill Deacon return 0; 146257cb251SWill Deacon } 147257cb251SWill Deacon 148b24a5575SArd Biesheuvel enum aarch64_insn_movw_imm_type { 149b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVNZ, 150b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ, 151b24a5575SArd Biesheuvel }; 152b24a5575SArd Biesheuvel 15302129ae5SLuc Van Oostenryck static int reloc_insn_movw(enum aarch64_reloc_op op, __le32 *place, u64 val, 154b24a5575SArd Biesheuvel int lsb, enum aarch64_insn_movw_imm_type imm_type) 155257cb251SWill Deacon { 156b24a5575SArd Biesheuvel u64 imm; 157c84fced8SJiang Liu s64 sval; 15802129ae5SLuc Van Oostenryck u32 insn = le32_to_cpu(*place); 159257cb251SWill Deacon 160c84fced8SJiang Liu sval = do_reloc(op, place, val); 161b24a5575SArd Biesheuvel imm = sval >> lsb; 162122e2fa0SWill Deacon 163c84fced8SJiang Liu if (imm_type == AARCH64_INSN_IMM_MOVNZ) { 164257cb251SWill Deacon /* 165257cb251SWill Deacon * For signed MOVW relocations, we have to manipulate the 166257cb251SWill Deacon * instruction encoding depending on whether or not the 167257cb251SWill Deacon * immediate is less than zero. 168257cb251SWill Deacon */ 169257cb251SWill Deacon insn &= ~(3 << 29); 170b24a5575SArd Biesheuvel if (sval >= 0) { 171257cb251SWill Deacon /* >=0: Set the instruction to MOVZ (opcode 10b). */ 172257cb251SWill Deacon insn |= 2 << 29; 173257cb251SWill Deacon } else { 174257cb251SWill Deacon /* 175257cb251SWill Deacon * <0: Set the instruction to MOVN (opcode 00b). 176257cb251SWill Deacon * Since we've masked the opcode already, we 177257cb251SWill Deacon * don't need to do anything other than 178257cb251SWill Deacon * inverting the new immediate field. 179257cb251SWill Deacon */ 180257cb251SWill Deacon imm = ~imm; 181257cb251SWill Deacon } 182257cb251SWill Deacon } 183257cb251SWill Deacon 184257cb251SWill Deacon /* Update the instruction with the new encoding. */ 185b24a5575SArd Biesheuvel insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm); 18602129ae5SLuc Van Oostenryck *place = cpu_to_le32(insn); 187257cb251SWill Deacon 188b24a5575SArd Biesheuvel if (imm > U16_MAX) 189257cb251SWill Deacon return -ERANGE; 190257cb251SWill Deacon 191257cb251SWill Deacon return 0; 192257cb251SWill Deacon } 193257cb251SWill Deacon 19402129ae5SLuc Van Oostenryck static int reloc_insn_imm(enum aarch64_reloc_op op, __le32 *place, u64 val, 195c84fced8SJiang Liu int lsb, int len, enum aarch64_insn_imm_type imm_type) 196257cb251SWill Deacon { 197257cb251SWill Deacon u64 imm, imm_mask; 198257cb251SWill Deacon s64 sval; 19902129ae5SLuc Van Oostenryck u32 insn = le32_to_cpu(*place); 200257cb251SWill Deacon 201257cb251SWill Deacon /* Calculate the relocation value. */ 202257cb251SWill Deacon sval = do_reloc(op, place, val); 203257cb251SWill Deacon sval >>= lsb; 204257cb251SWill Deacon 205257cb251SWill Deacon /* Extract the value bits and shift them to bit 0. */ 206257cb251SWill Deacon imm_mask = (BIT(lsb + len) - 1) >> lsb; 207257cb251SWill Deacon imm = sval & imm_mask; 208257cb251SWill Deacon 209257cb251SWill Deacon /* Update the instruction's immediate field. */ 210c84fced8SJiang Liu insn = aarch64_insn_encode_immediate(imm_type, insn, imm); 21102129ae5SLuc Van Oostenryck *place = cpu_to_le32(insn); 212257cb251SWill Deacon 213257cb251SWill Deacon /* 214257cb251SWill Deacon * Extract the upper value bits (including the sign bit) and 215257cb251SWill Deacon * shift them to bit 0. 216257cb251SWill Deacon */ 217257cb251SWill Deacon sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1); 218257cb251SWill Deacon 219257cb251SWill Deacon /* 220257cb251SWill Deacon * Overflow has occurred if the upper bits are not all equal to 221257cb251SWill Deacon * the sign bit of the value. 222257cb251SWill Deacon */ 223257cb251SWill Deacon if ((u64)(sval + 1) >= 2) 224257cb251SWill Deacon return -ERANGE; 225257cb251SWill Deacon 226257cb251SWill Deacon return 0; 227257cb251SWill Deacon } 228257cb251SWill Deacon 229c8ebf64eSJessica Yu static int reloc_insn_adrp(struct module *mod, Elf64_Shdr *sechdrs, 230c8ebf64eSJessica Yu __le32 *place, u64 val) 231a257e025SArd Biesheuvel { 232a257e025SArd Biesheuvel u32 insn; 233a257e025SArd Biesheuvel 234bdb85cd1SArd Biesheuvel if (!is_forbidden_offset_for_adrp(place)) 235a257e025SArd Biesheuvel return reloc_insn_imm(RELOC_OP_PAGE, place, val, 12, 21, 236a257e025SArd Biesheuvel AARCH64_INSN_IMM_ADR); 237a257e025SArd Biesheuvel 238a257e025SArd Biesheuvel /* patch ADRP to ADR if it is in range */ 239a257e025SArd Biesheuvel if (!reloc_insn_imm(RELOC_OP_PREL, place, val & ~0xfff, 0, 21, 240a257e025SArd Biesheuvel AARCH64_INSN_IMM_ADR)) { 241a257e025SArd Biesheuvel insn = le32_to_cpu(*place); 242a257e025SArd Biesheuvel insn &= ~BIT(31); 243a257e025SArd Biesheuvel } else { 244a257e025SArd Biesheuvel /* out of range for ADR -> emit a veneer */ 245c8ebf64eSJessica Yu val = module_emit_veneer_for_adrp(mod, sechdrs, place, val & ~0xfff); 246a257e025SArd Biesheuvel if (!val) 247a257e025SArd Biesheuvel return -ENOEXEC; 248a257e025SArd Biesheuvel insn = aarch64_insn_gen_branch_imm((u64)place, val, 249a257e025SArd Biesheuvel AARCH64_INSN_BRANCH_NOLINK); 250a257e025SArd Biesheuvel } 251a257e025SArd Biesheuvel 252a257e025SArd Biesheuvel *place = cpu_to_le32(insn); 253a257e025SArd Biesheuvel return 0; 254a257e025SArd Biesheuvel } 255a257e025SArd Biesheuvel 256257cb251SWill Deacon int apply_relocate_add(Elf64_Shdr *sechdrs, 257257cb251SWill Deacon const char *strtab, 258257cb251SWill Deacon unsigned int symindex, 259257cb251SWill Deacon unsigned int relsec, 260257cb251SWill Deacon struct module *me) 261257cb251SWill Deacon { 262257cb251SWill Deacon unsigned int i; 263257cb251SWill Deacon int ovf; 264257cb251SWill Deacon bool overflow_check; 265257cb251SWill Deacon Elf64_Sym *sym; 266257cb251SWill Deacon void *loc; 267257cb251SWill Deacon u64 val; 268257cb251SWill Deacon Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr; 269257cb251SWill Deacon 270257cb251SWill Deacon for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { 271257cb251SWill Deacon /* loc corresponds to P in the AArch64 ELF document. */ 272257cb251SWill Deacon loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr 273257cb251SWill Deacon + rel[i].r_offset; 274257cb251SWill Deacon 275257cb251SWill Deacon /* sym is the ELF symbol we're referring to. */ 276257cb251SWill Deacon sym = (Elf64_Sym *)sechdrs[symindex].sh_addr 277257cb251SWill Deacon + ELF64_R_SYM(rel[i].r_info); 278257cb251SWill Deacon 279257cb251SWill Deacon /* val corresponds to (S + A) in the AArch64 ELF document. */ 280257cb251SWill Deacon val = sym->st_value + rel[i].r_addend; 281257cb251SWill Deacon 282257cb251SWill Deacon /* Check for overflow by default. */ 283257cb251SWill Deacon overflow_check = true; 284257cb251SWill Deacon 285257cb251SWill Deacon /* Perform the static relocation. */ 286257cb251SWill Deacon switch (ELF64_R_TYPE(rel[i].r_info)) { 287257cb251SWill Deacon /* Null relocations. */ 288257cb251SWill Deacon case R_ARM_NONE: 289257cb251SWill Deacon case R_AARCH64_NONE: 290257cb251SWill Deacon ovf = 0; 291257cb251SWill Deacon break; 292257cb251SWill Deacon 293257cb251SWill Deacon /* Data relocations. */ 294257cb251SWill Deacon case R_AARCH64_ABS64: 295257cb251SWill Deacon overflow_check = false; 296257cb251SWill Deacon ovf = reloc_data(RELOC_OP_ABS, loc, val, 64); 297257cb251SWill Deacon break; 298257cb251SWill Deacon case R_AARCH64_ABS32: 299257cb251SWill Deacon ovf = reloc_data(RELOC_OP_ABS, loc, val, 32); 300257cb251SWill Deacon break; 301257cb251SWill Deacon case R_AARCH64_ABS16: 302257cb251SWill Deacon ovf = reloc_data(RELOC_OP_ABS, loc, val, 16); 303257cb251SWill Deacon break; 304257cb251SWill Deacon case R_AARCH64_PREL64: 305257cb251SWill Deacon overflow_check = false; 306257cb251SWill Deacon ovf = reloc_data(RELOC_OP_PREL, loc, val, 64); 307257cb251SWill Deacon break; 308257cb251SWill Deacon case R_AARCH64_PREL32: 309257cb251SWill Deacon ovf = reloc_data(RELOC_OP_PREL, loc, val, 32); 310257cb251SWill Deacon break; 311257cb251SWill Deacon case R_AARCH64_PREL16: 312257cb251SWill Deacon ovf = reloc_data(RELOC_OP_PREL, loc, val, 16); 313257cb251SWill Deacon break; 314257cb251SWill Deacon 315257cb251SWill Deacon /* MOVW instruction relocations. */ 316257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G0_NC: 317257cb251SWill Deacon overflow_check = false; 318*df561f66SGustavo A. R. Silva fallthrough; 319257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G0: 320257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0, 321b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ); 322257cb251SWill Deacon break; 323257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G1_NC: 324257cb251SWill Deacon overflow_check = false; 325*df561f66SGustavo A. R. Silva fallthrough; 326257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G1: 327257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16, 328b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ); 329257cb251SWill Deacon break; 330257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G2_NC: 331257cb251SWill Deacon overflow_check = false; 332*df561f66SGustavo A. R. Silva fallthrough; 333257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G2: 334257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32, 335b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ); 336257cb251SWill Deacon break; 337257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G3: 338257cb251SWill Deacon /* We're using the top bits so we can't overflow. */ 339257cb251SWill Deacon overflow_check = false; 340257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48, 341b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ); 342257cb251SWill Deacon break; 343257cb251SWill Deacon case R_AARCH64_MOVW_SABS_G0: 344257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0, 345c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ); 346257cb251SWill Deacon break; 347257cb251SWill Deacon case R_AARCH64_MOVW_SABS_G1: 348257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16, 349c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ); 350257cb251SWill Deacon break; 351257cb251SWill Deacon case R_AARCH64_MOVW_SABS_G2: 352257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32, 353c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ); 354257cb251SWill Deacon break; 355257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G0_NC: 356257cb251SWill Deacon overflow_check = false; 357257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0, 358b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ); 359257cb251SWill Deacon break; 360257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G0: 361257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0, 362c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ); 363257cb251SWill Deacon break; 364257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G1_NC: 365257cb251SWill Deacon overflow_check = false; 366257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16, 367b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ); 368257cb251SWill Deacon break; 369257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G1: 370257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16, 371c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ); 372257cb251SWill Deacon break; 373257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G2_NC: 374257cb251SWill Deacon overflow_check = false; 375257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32, 376b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ); 377257cb251SWill Deacon break; 378257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G2: 379257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32, 380c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ); 381257cb251SWill Deacon break; 382257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G3: 383257cb251SWill Deacon /* We're using the top bits so we can't overflow. */ 384257cb251SWill Deacon overflow_check = false; 385257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48, 386c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ); 387257cb251SWill Deacon break; 388257cb251SWill Deacon 389257cb251SWill Deacon /* Immediate instruction relocations. */ 390257cb251SWill Deacon case R_AARCH64_LD_PREL_LO19: 391257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19, 392c84fced8SJiang Liu AARCH64_INSN_IMM_19); 393257cb251SWill Deacon break; 394257cb251SWill Deacon case R_AARCH64_ADR_PREL_LO21: 395257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21, 396c84fced8SJiang Liu AARCH64_INSN_IMM_ADR); 397257cb251SWill Deacon break; 398257cb251SWill Deacon case R_AARCH64_ADR_PREL_PG_HI21_NC: 399257cb251SWill Deacon overflow_check = false; 400*df561f66SGustavo A. R. Silva fallthrough; 401257cb251SWill Deacon case R_AARCH64_ADR_PREL_PG_HI21: 402c8ebf64eSJessica Yu ovf = reloc_insn_adrp(me, sechdrs, loc, val); 403a257e025SArd Biesheuvel if (ovf && ovf != -ERANGE) 404a257e025SArd Biesheuvel return ovf; 405257cb251SWill Deacon break; 406257cb251SWill Deacon case R_AARCH64_ADD_ABS_LO12_NC: 407257cb251SWill Deacon case R_AARCH64_LDST8_ABS_LO12_NC: 408257cb251SWill Deacon overflow_check = false; 409257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12, 410c84fced8SJiang Liu AARCH64_INSN_IMM_12); 411257cb251SWill Deacon break; 412257cb251SWill Deacon case R_AARCH64_LDST16_ABS_LO12_NC: 413257cb251SWill Deacon overflow_check = false; 414257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11, 415c84fced8SJiang Liu AARCH64_INSN_IMM_12); 416257cb251SWill Deacon break; 417257cb251SWill Deacon case R_AARCH64_LDST32_ABS_LO12_NC: 418257cb251SWill Deacon overflow_check = false; 419257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10, 420c84fced8SJiang Liu AARCH64_INSN_IMM_12); 421257cb251SWill Deacon break; 422257cb251SWill Deacon case R_AARCH64_LDST64_ABS_LO12_NC: 423257cb251SWill Deacon overflow_check = false; 424257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9, 425c84fced8SJiang Liu AARCH64_INSN_IMM_12); 426257cb251SWill Deacon break; 427257cb251SWill Deacon case R_AARCH64_LDST128_ABS_LO12_NC: 428257cb251SWill Deacon overflow_check = false; 429257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8, 430c84fced8SJiang Liu AARCH64_INSN_IMM_12); 431257cb251SWill Deacon break; 432257cb251SWill Deacon case R_AARCH64_TSTBR14: 433257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14, 434c84fced8SJiang Liu AARCH64_INSN_IMM_14); 435257cb251SWill Deacon break; 436257cb251SWill Deacon case R_AARCH64_CONDBR19: 437257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19, 438c84fced8SJiang Liu AARCH64_INSN_IMM_19); 439257cb251SWill Deacon break; 440257cb251SWill Deacon case R_AARCH64_JUMP26: 441257cb251SWill Deacon case R_AARCH64_CALL26: 442257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26, 443c84fced8SJiang Liu AARCH64_INSN_IMM_26); 444fd045f6cSArd Biesheuvel 445fd045f6cSArd Biesheuvel if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) && 446fd045f6cSArd Biesheuvel ovf == -ERANGE) { 447c8ebf64eSJessica Yu val = module_emit_plt_entry(me, sechdrs, loc, &rel[i], sym); 4485e8307b9SArd Biesheuvel if (!val) 4495e8307b9SArd Biesheuvel return -ENOEXEC; 450fd045f6cSArd Biesheuvel ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 451fd045f6cSArd Biesheuvel 26, AARCH64_INSN_IMM_26); 452fd045f6cSArd Biesheuvel } 453257cb251SWill Deacon break; 454257cb251SWill Deacon 455257cb251SWill Deacon default: 456257cb251SWill Deacon pr_err("module %s: unsupported RELA relocation: %llu\n", 457257cb251SWill Deacon me->name, ELF64_R_TYPE(rel[i].r_info)); 458257cb251SWill Deacon return -ENOEXEC; 459257cb251SWill Deacon } 460257cb251SWill Deacon 461257cb251SWill Deacon if (overflow_check && ovf == -ERANGE) 462257cb251SWill Deacon goto overflow; 463257cb251SWill Deacon 464257cb251SWill Deacon } 465257cb251SWill Deacon 466257cb251SWill Deacon return 0; 467257cb251SWill Deacon 468257cb251SWill Deacon overflow: 469257cb251SWill Deacon pr_err("module %s: overflow in relocation type %d val %Lx\n", 470257cb251SWill Deacon me->name, (int)ELF64_R_TYPE(rel[i].r_info), val); 471257cb251SWill Deacon return -ENOEXEC; 472257cb251SWill Deacon } 473932ded4bSAndre Przywara 474bd8b21d3SMark Rutland static const Elf_Shdr *find_section(const Elf_Ehdr *hdr, 475932ded4bSAndre Przywara const Elf_Shdr *sechdrs, 476bd8b21d3SMark Rutland const char *name) 477932ded4bSAndre Przywara { 478932ded4bSAndre Przywara const Elf_Shdr *s, *se; 479932ded4bSAndre Przywara const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; 480932ded4bSAndre Przywara 481932ded4bSAndre Przywara for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) { 482bd8b21d3SMark Rutland if (strcmp(name, secstrs + s->sh_name) == 0) 483bd8b21d3SMark Rutland return s; 484932ded4bSAndre Przywara } 485932ded4bSAndre Przywara 486bd8b21d3SMark Rutland return NULL; 487bd8b21d3SMark Rutland } 488bd8b21d3SMark Rutland 4893b23e499STorsten Duwe static inline void __init_plt(struct plt_entry *plt, unsigned long addr) 4903b23e499STorsten Duwe { 4913b23e499STorsten Duwe *plt = get_plt_entry(addr, plt); 4923b23e499STorsten Duwe } 4933b23e499STorsten Duwe 494f1a54ae9SMark Rutland static int module_init_ftrace_plt(const Elf_Ehdr *hdr, 495f1a54ae9SMark Rutland const Elf_Shdr *sechdrs, 496f1a54ae9SMark Rutland struct module *mod) 497f1a54ae9SMark Rutland { 498f1a54ae9SMark Rutland #if defined(CONFIG_ARM64_MODULE_PLTS) && defined(CONFIG_DYNAMIC_FTRACE) 499f1a54ae9SMark Rutland const Elf_Shdr *s; 5003b23e499STorsten Duwe struct plt_entry *plts; 501f1a54ae9SMark Rutland 502f1a54ae9SMark Rutland s = find_section(hdr, sechdrs, ".text.ftrace_trampoline"); 503f1a54ae9SMark Rutland if (!s) 504f1a54ae9SMark Rutland return -ENOEXEC; 505f1a54ae9SMark Rutland 5063b23e499STorsten Duwe plts = (void *)s->sh_addr; 5073b23e499STorsten Duwe 5083b23e499STorsten Duwe __init_plt(&plts[FTRACE_PLT_IDX], FTRACE_ADDR); 5093b23e499STorsten Duwe 5103b23e499STorsten Duwe if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS)) 5113b23e499STorsten Duwe __init_plt(&plts[FTRACE_REGS_PLT_IDX], FTRACE_REGS_ADDR); 5123b23e499STorsten Duwe 5133b23e499STorsten Duwe mod->arch.ftrace_trampolines = plts; 514f1a54ae9SMark Rutland #endif 515f1a54ae9SMark Rutland return 0; 516f1a54ae9SMark Rutland } 517f1a54ae9SMark Rutland 518bd8b21d3SMark Rutland int module_finalize(const Elf_Ehdr *hdr, 519bd8b21d3SMark Rutland const Elf_Shdr *sechdrs, 520bd8b21d3SMark Rutland struct module *me) 521bd8b21d3SMark Rutland { 522bd8b21d3SMark Rutland const Elf_Shdr *s; 523bd8b21d3SMark Rutland s = find_section(hdr, sechdrs, ".altinstructions"); 524bd8b21d3SMark Rutland if (s) 525bd8b21d3SMark Rutland apply_alternatives_module((void *)s->sh_addr, s->sh_size); 526bd8b21d3SMark Rutland 527f1a54ae9SMark Rutland return module_init_ftrace_plt(hdr, sechdrs, me); 528932ded4bSAndre Przywara } 529