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> 18e46b7103SMark Rutland #include <linux/random.h> 193b619e22SArd Biesheuvel #include <linux/scs.h> 20257cb251SWill Deacon #include <linux/vmalloc.h> 21e46b7103SMark Rutland 222c2b282dSPaul Walmsley #include <asm/alternative.h> 23c84fced8SJiang Liu #include <asm/insn.h> 243b619e22SArd Biesheuvel #include <asm/scs.h> 25932ded4bSAndre Przywara #include <asm/sections.h> 26c84fced8SJiang Liu 27e46b7103SMark Rutland static u64 __ro_after_init module_alloc_base = (u64)_etext - MODULES_VSIZE; 28e46b7103SMark Rutland 29e46b7103SMark Rutland #ifdef CONFIG_RANDOMIZE_BASE 30e46b7103SMark Rutland static int __init kaslr_module_init(void) 31e46b7103SMark Rutland { 32e46b7103SMark Rutland u64 module_range; 33e46b7103SMark Rutland u32 seed; 34e46b7103SMark Rutland 35e46b7103SMark Rutland if (!kaslr_enabled()) 36e46b7103SMark Rutland return 0; 37e46b7103SMark Rutland 38e46b7103SMark Rutland seed = get_random_u32(); 39e46b7103SMark Rutland 40e46b7103SMark Rutland if (IS_ENABLED(CONFIG_RANDOMIZE_MODULE_REGION_FULL)) { 41e46b7103SMark Rutland /* 42e46b7103SMark Rutland * Randomize the module region over a 2 GB window covering the 43e46b7103SMark Rutland * kernel. This reduces the risk of modules leaking information 44e46b7103SMark Rutland * about the address of the kernel itself, but results in 45e46b7103SMark Rutland * branches between modules and the core kernel that are 46e46b7103SMark Rutland * resolved via PLTs. (Branches between modules will be 47e46b7103SMark Rutland * resolved normally.) 48e46b7103SMark Rutland */ 49e46b7103SMark Rutland module_range = SZ_2G - (u64)(_end - _stext); 50e46b7103SMark Rutland module_alloc_base = max((u64)_end - SZ_2G, (u64)MODULES_VADDR); 51e46b7103SMark Rutland } else { 52e46b7103SMark Rutland /* 53e46b7103SMark Rutland * Randomize the module region by setting module_alloc_base to 54e46b7103SMark Rutland * a PAGE_SIZE multiple in the range [_etext - MODULES_VSIZE, 55e46b7103SMark Rutland * _stext) . This guarantees that the resulting region still 56e46b7103SMark Rutland * covers [_stext, _etext], and that all relative branches can 57e46b7103SMark Rutland * be resolved without veneers unless this region is exhausted 58e46b7103SMark Rutland * and we fall back to a larger 2GB window in module_alloc() 59e46b7103SMark Rutland * when ARM64_MODULE_PLTS is enabled. 60e46b7103SMark Rutland */ 61e46b7103SMark Rutland module_range = MODULES_VSIZE - (u64)(_etext - _stext); 62e46b7103SMark Rutland } 63e46b7103SMark Rutland 64e46b7103SMark Rutland /* use the lower 21 bits to randomize the base of the module region */ 65e46b7103SMark Rutland module_alloc_base += (module_range * (seed & ((1 << 21) - 1))) >> 21; 66e46b7103SMark Rutland module_alloc_base &= PAGE_MASK; 67e46b7103SMark Rutland 68e46b7103SMark Rutland return 0; 69e46b7103SMark Rutland } 70e46b7103SMark Rutland subsys_initcall(kaslr_module_init) 71e46b7103SMark Rutland #endif 72e46b7103SMark Rutland 73257cb251SWill Deacon void *module_alloc(unsigned long size) 74257cb251SWill Deacon { 756f496a55SArd Biesheuvel u64 module_alloc_end = module_alloc_base + MODULES_VSIZE; 7639d114ddSAndrey Ryabinin void *p; 7739d114ddSAndrey Ryabinin 78*ea3752baSMark Rutland /* 79*ea3752baSMark Rutland * Where possible, prefer to allocate within direct branch range of the 80*ea3752baSMark Rutland * kernel such that no PLTs are necessary. This may fail, so we pass 81*ea3752baSMark Rutland * __GFP_NOWARN to silence the resulting warning. 82*ea3752baSMark Rutland */ 83f80fb3a3SArd Biesheuvel p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base, 84*ea3752baSMark Rutland module_alloc_end, GFP_KERNEL | __GFP_NOWARN, 85*ea3752baSMark Rutland PAGE_KERNEL, 0, NUMA_NO_NODE, 86*ea3752baSMark Rutland __builtin_return_address(0)); 8739d114ddSAndrey Ryabinin 88*ea3752baSMark Rutland if (!p) { 89f2b9ba87SArd Biesheuvel p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base, 90b2eed9b5SArd Biesheuvel module_alloc_base + SZ_2G, GFP_KERNEL, 917dfac3c5SArd Biesheuvel PAGE_KERNEL, 0, NUMA_NO_NODE, 92f2b9ba87SArd Biesheuvel __builtin_return_address(0)); 938339f7d8SMark Rutland } 94fd045f6cSArd Biesheuvel 95*ea3752baSMark Rutland if (p && (kasan_alloc_module_shadow(p, size, GFP_KERNEL) < 0)) { 9639d114ddSAndrey Ryabinin vfree(p); 9739d114ddSAndrey Ryabinin return NULL; 9839d114ddSAndrey Ryabinin } 9939d114ddSAndrey Ryabinin 10036c4a73bSAndrey Konovalov /* Memory is intended to be executable, reset the pointer tag. */ 10136c4a73bSAndrey Konovalov return kasan_reset_tag(p); 102257cb251SWill Deacon } 103257cb251SWill Deacon 104257cb251SWill Deacon enum aarch64_reloc_op { 105257cb251SWill Deacon RELOC_OP_NONE, 106257cb251SWill Deacon RELOC_OP_ABS, 107257cb251SWill Deacon RELOC_OP_PREL, 108257cb251SWill Deacon RELOC_OP_PAGE, 109257cb251SWill Deacon }; 110257cb251SWill Deacon 11102129ae5SLuc Van Oostenryck static u64 do_reloc(enum aarch64_reloc_op reloc_op, __le32 *place, u64 val) 112257cb251SWill Deacon { 113257cb251SWill Deacon switch (reloc_op) { 114257cb251SWill Deacon case RELOC_OP_ABS: 115257cb251SWill Deacon return val; 116257cb251SWill Deacon case RELOC_OP_PREL: 117257cb251SWill Deacon return val - (u64)place; 118257cb251SWill Deacon case RELOC_OP_PAGE: 119257cb251SWill Deacon return (val & ~0xfff) - ((u64)place & ~0xfff); 120257cb251SWill Deacon case RELOC_OP_NONE: 121257cb251SWill Deacon return 0; 122257cb251SWill Deacon } 123257cb251SWill Deacon 124257cb251SWill Deacon pr_err("do_reloc: unknown relocation operation %d\n", reloc_op); 125257cb251SWill Deacon return 0; 126257cb251SWill Deacon } 127257cb251SWill Deacon 128257cb251SWill Deacon static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len) 129257cb251SWill Deacon { 130257cb251SWill Deacon s64 sval = do_reloc(op, place, val); 131257cb251SWill Deacon 1321cf24a2cSArd Biesheuvel /* 1331cf24a2cSArd Biesheuvel * The ELF psABI for AArch64 documents the 16-bit and 32-bit place 1343fd00bebSArd Biesheuvel * relative and absolute relocations as having a range of [-2^15, 2^16) 1353fd00bebSArd Biesheuvel * or [-2^31, 2^32), respectively. However, in order to be able to 1363fd00bebSArd Biesheuvel * detect overflows reliably, we have to choose whether we interpret 1373fd00bebSArd Biesheuvel * such quantities as signed or as unsigned, and stick with it. 1381cf24a2cSArd Biesheuvel * The way we organize our address space requires a signed 1391cf24a2cSArd Biesheuvel * interpretation of 32-bit relative references, so let's use that 1401cf24a2cSArd Biesheuvel * for all R_AARCH64_PRELxx relocations. This means our upper 1411cf24a2cSArd Biesheuvel * bound for overflow detection should be Sxx_MAX rather than Uxx_MAX. 1421cf24a2cSArd Biesheuvel */ 1431cf24a2cSArd Biesheuvel 144257cb251SWill Deacon switch (len) { 145257cb251SWill Deacon case 16: 146257cb251SWill Deacon *(s16 *)place = sval; 1473fd00bebSArd Biesheuvel switch (op) { 1483fd00bebSArd Biesheuvel case RELOC_OP_ABS: 1493fd00bebSArd Biesheuvel if (sval < 0 || sval > U16_MAX) 1503fd00bebSArd Biesheuvel return -ERANGE; 1513fd00bebSArd Biesheuvel break; 1523fd00bebSArd Biesheuvel case RELOC_OP_PREL: 1531cf24a2cSArd Biesheuvel if (sval < S16_MIN || sval > S16_MAX) 154f9308969SArd Biesheuvel return -ERANGE; 155257cb251SWill Deacon break; 1563fd00bebSArd Biesheuvel default: 1573fd00bebSArd Biesheuvel pr_err("Invalid 16-bit data relocation (%d)\n", op); 1583fd00bebSArd Biesheuvel return 0; 1593fd00bebSArd Biesheuvel } 1603fd00bebSArd Biesheuvel break; 161257cb251SWill Deacon case 32: 162257cb251SWill Deacon *(s32 *)place = sval; 1633fd00bebSArd Biesheuvel switch (op) { 1643fd00bebSArd Biesheuvel case RELOC_OP_ABS: 1653fd00bebSArd Biesheuvel if (sval < 0 || sval > U32_MAX) 1663fd00bebSArd Biesheuvel return -ERANGE; 1673fd00bebSArd Biesheuvel break; 1683fd00bebSArd Biesheuvel case RELOC_OP_PREL: 1691cf24a2cSArd Biesheuvel if (sval < S32_MIN || sval > S32_MAX) 170f9308969SArd Biesheuvel return -ERANGE; 171257cb251SWill Deacon break; 1723fd00bebSArd Biesheuvel default: 1733fd00bebSArd Biesheuvel pr_err("Invalid 32-bit data relocation (%d)\n", op); 1743fd00bebSArd Biesheuvel return 0; 1753fd00bebSArd Biesheuvel } 1763fd00bebSArd Biesheuvel break; 177257cb251SWill Deacon case 64: 178257cb251SWill Deacon *(s64 *)place = sval; 179257cb251SWill Deacon break; 180257cb251SWill Deacon default: 181257cb251SWill Deacon pr_err("Invalid length (%d) for data relocation\n", len); 182257cb251SWill Deacon return 0; 183257cb251SWill Deacon } 184257cb251SWill Deacon return 0; 185257cb251SWill Deacon } 186257cb251SWill Deacon 187b24a5575SArd Biesheuvel enum aarch64_insn_movw_imm_type { 188b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVNZ, 189b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ, 190b24a5575SArd Biesheuvel }; 191b24a5575SArd Biesheuvel 19202129ae5SLuc Van Oostenryck static int reloc_insn_movw(enum aarch64_reloc_op op, __le32 *place, u64 val, 193b24a5575SArd Biesheuvel int lsb, enum aarch64_insn_movw_imm_type imm_type) 194257cb251SWill Deacon { 195b24a5575SArd Biesheuvel u64 imm; 196c84fced8SJiang Liu s64 sval; 19702129ae5SLuc Van Oostenryck u32 insn = le32_to_cpu(*place); 198257cb251SWill Deacon 199c84fced8SJiang Liu sval = do_reloc(op, place, val); 200b24a5575SArd Biesheuvel imm = sval >> lsb; 201122e2fa0SWill Deacon 202c84fced8SJiang Liu if (imm_type == AARCH64_INSN_IMM_MOVNZ) { 203257cb251SWill Deacon /* 204257cb251SWill Deacon * For signed MOVW relocations, we have to manipulate the 205257cb251SWill Deacon * instruction encoding depending on whether or not the 206257cb251SWill Deacon * immediate is less than zero. 207257cb251SWill Deacon */ 208257cb251SWill Deacon insn &= ~(3 << 29); 209b24a5575SArd Biesheuvel if (sval >= 0) { 210257cb251SWill Deacon /* >=0: Set the instruction to MOVZ (opcode 10b). */ 211257cb251SWill Deacon insn |= 2 << 29; 212257cb251SWill Deacon } else { 213257cb251SWill Deacon /* 214257cb251SWill Deacon * <0: Set the instruction to MOVN (opcode 00b). 215257cb251SWill Deacon * Since we've masked the opcode already, we 216257cb251SWill Deacon * don't need to do anything other than 217257cb251SWill Deacon * inverting the new immediate field. 218257cb251SWill Deacon */ 219257cb251SWill Deacon imm = ~imm; 220257cb251SWill Deacon } 221257cb251SWill Deacon } 222257cb251SWill Deacon 223257cb251SWill Deacon /* Update the instruction with the new encoding. */ 224b24a5575SArd Biesheuvel insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm); 22502129ae5SLuc Van Oostenryck *place = cpu_to_le32(insn); 226257cb251SWill Deacon 227b24a5575SArd Biesheuvel if (imm > U16_MAX) 228257cb251SWill Deacon return -ERANGE; 229257cb251SWill Deacon 230257cb251SWill Deacon return 0; 231257cb251SWill Deacon } 232257cb251SWill Deacon 23302129ae5SLuc Van Oostenryck static int reloc_insn_imm(enum aarch64_reloc_op op, __le32 *place, u64 val, 234c84fced8SJiang Liu int lsb, int len, enum aarch64_insn_imm_type imm_type) 235257cb251SWill Deacon { 236257cb251SWill Deacon u64 imm, imm_mask; 237257cb251SWill Deacon s64 sval; 23802129ae5SLuc Van Oostenryck u32 insn = le32_to_cpu(*place); 239257cb251SWill Deacon 240257cb251SWill Deacon /* Calculate the relocation value. */ 241257cb251SWill Deacon sval = do_reloc(op, place, val); 242257cb251SWill Deacon sval >>= lsb; 243257cb251SWill Deacon 244257cb251SWill Deacon /* Extract the value bits and shift them to bit 0. */ 245257cb251SWill Deacon imm_mask = (BIT(lsb + len) - 1) >> lsb; 246257cb251SWill Deacon imm = sval & imm_mask; 247257cb251SWill Deacon 248257cb251SWill Deacon /* Update the instruction's immediate field. */ 249c84fced8SJiang Liu insn = aarch64_insn_encode_immediate(imm_type, insn, imm); 25002129ae5SLuc Van Oostenryck *place = cpu_to_le32(insn); 251257cb251SWill Deacon 252257cb251SWill Deacon /* 253257cb251SWill Deacon * Extract the upper value bits (including the sign bit) and 254257cb251SWill Deacon * shift them to bit 0. 255257cb251SWill Deacon */ 256257cb251SWill Deacon sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1); 257257cb251SWill Deacon 258257cb251SWill Deacon /* 259257cb251SWill Deacon * Overflow has occurred if the upper bits are not all equal to 260257cb251SWill Deacon * the sign bit of the value. 261257cb251SWill Deacon */ 262257cb251SWill Deacon if ((u64)(sval + 1) >= 2) 263257cb251SWill Deacon return -ERANGE; 264257cb251SWill Deacon 265257cb251SWill Deacon return 0; 266257cb251SWill Deacon } 267257cb251SWill Deacon 268c8ebf64eSJessica Yu static int reloc_insn_adrp(struct module *mod, Elf64_Shdr *sechdrs, 269c8ebf64eSJessica Yu __le32 *place, u64 val) 270a257e025SArd Biesheuvel { 271a257e025SArd Biesheuvel u32 insn; 272a257e025SArd Biesheuvel 273bdb85cd1SArd Biesheuvel if (!is_forbidden_offset_for_adrp(place)) 274a257e025SArd Biesheuvel return reloc_insn_imm(RELOC_OP_PAGE, place, val, 12, 21, 275a257e025SArd Biesheuvel AARCH64_INSN_IMM_ADR); 276a257e025SArd Biesheuvel 277a257e025SArd Biesheuvel /* patch ADRP to ADR if it is in range */ 278a257e025SArd Biesheuvel if (!reloc_insn_imm(RELOC_OP_PREL, place, val & ~0xfff, 0, 21, 279a257e025SArd Biesheuvel AARCH64_INSN_IMM_ADR)) { 280a257e025SArd Biesheuvel insn = le32_to_cpu(*place); 281a257e025SArd Biesheuvel insn &= ~BIT(31); 282a257e025SArd Biesheuvel } else { 283a257e025SArd Biesheuvel /* out of range for ADR -> emit a veneer */ 284c8ebf64eSJessica Yu val = module_emit_veneer_for_adrp(mod, sechdrs, place, val & ~0xfff); 285a257e025SArd Biesheuvel if (!val) 286a257e025SArd Biesheuvel return -ENOEXEC; 287a257e025SArd Biesheuvel insn = aarch64_insn_gen_branch_imm((u64)place, val, 288a257e025SArd Biesheuvel AARCH64_INSN_BRANCH_NOLINK); 289a257e025SArd Biesheuvel } 290a257e025SArd Biesheuvel 291a257e025SArd Biesheuvel *place = cpu_to_le32(insn); 292a257e025SArd Biesheuvel return 0; 293a257e025SArd Biesheuvel } 294a257e025SArd Biesheuvel 295257cb251SWill Deacon int apply_relocate_add(Elf64_Shdr *sechdrs, 296257cb251SWill Deacon const char *strtab, 297257cb251SWill Deacon unsigned int symindex, 298257cb251SWill Deacon unsigned int relsec, 299257cb251SWill Deacon struct module *me) 300257cb251SWill Deacon { 301257cb251SWill Deacon unsigned int i; 302257cb251SWill Deacon int ovf; 303257cb251SWill Deacon bool overflow_check; 304257cb251SWill Deacon Elf64_Sym *sym; 305257cb251SWill Deacon void *loc; 306257cb251SWill Deacon u64 val; 307257cb251SWill Deacon Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr; 308257cb251SWill Deacon 309257cb251SWill Deacon for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { 310257cb251SWill Deacon /* loc corresponds to P in the AArch64 ELF document. */ 311257cb251SWill Deacon loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr 312257cb251SWill Deacon + rel[i].r_offset; 313257cb251SWill Deacon 314257cb251SWill Deacon /* sym is the ELF symbol we're referring to. */ 315257cb251SWill Deacon sym = (Elf64_Sym *)sechdrs[symindex].sh_addr 316257cb251SWill Deacon + ELF64_R_SYM(rel[i].r_info); 317257cb251SWill Deacon 318257cb251SWill Deacon /* val corresponds to (S + A) in the AArch64 ELF document. */ 319257cb251SWill Deacon val = sym->st_value + rel[i].r_addend; 320257cb251SWill Deacon 321257cb251SWill Deacon /* Check for overflow by default. */ 322257cb251SWill Deacon overflow_check = true; 323257cb251SWill Deacon 324257cb251SWill Deacon /* Perform the static relocation. */ 325257cb251SWill Deacon switch (ELF64_R_TYPE(rel[i].r_info)) { 326257cb251SWill Deacon /* Null relocations. */ 327257cb251SWill Deacon case R_ARM_NONE: 328257cb251SWill Deacon case R_AARCH64_NONE: 329257cb251SWill Deacon ovf = 0; 330257cb251SWill Deacon break; 331257cb251SWill Deacon 332257cb251SWill Deacon /* Data relocations. */ 333257cb251SWill Deacon case R_AARCH64_ABS64: 334257cb251SWill Deacon overflow_check = false; 335257cb251SWill Deacon ovf = reloc_data(RELOC_OP_ABS, loc, val, 64); 336257cb251SWill Deacon break; 337257cb251SWill Deacon case R_AARCH64_ABS32: 338257cb251SWill Deacon ovf = reloc_data(RELOC_OP_ABS, loc, val, 32); 339257cb251SWill Deacon break; 340257cb251SWill Deacon case R_AARCH64_ABS16: 341257cb251SWill Deacon ovf = reloc_data(RELOC_OP_ABS, loc, val, 16); 342257cb251SWill Deacon break; 343257cb251SWill Deacon case R_AARCH64_PREL64: 344257cb251SWill Deacon overflow_check = false; 345257cb251SWill Deacon ovf = reloc_data(RELOC_OP_PREL, loc, val, 64); 346257cb251SWill Deacon break; 347257cb251SWill Deacon case R_AARCH64_PREL32: 348257cb251SWill Deacon ovf = reloc_data(RELOC_OP_PREL, loc, val, 32); 349257cb251SWill Deacon break; 350257cb251SWill Deacon case R_AARCH64_PREL16: 351257cb251SWill Deacon ovf = reloc_data(RELOC_OP_PREL, loc, val, 16); 352257cb251SWill Deacon break; 353257cb251SWill Deacon 354257cb251SWill Deacon /* MOVW instruction relocations. */ 355257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G0_NC: 356257cb251SWill Deacon overflow_check = false; 357df561f66SGustavo A. R. Silva fallthrough; 358257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G0: 359257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0, 360b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ); 361257cb251SWill Deacon break; 362257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G1_NC: 363257cb251SWill Deacon overflow_check = false; 364df561f66SGustavo A. R. Silva fallthrough; 365257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G1: 366257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16, 367b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ); 368257cb251SWill Deacon break; 369257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G2_NC: 370257cb251SWill Deacon overflow_check = false; 371df561f66SGustavo A. R. Silva fallthrough; 372257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G2: 373257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32, 374b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ); 375257cb251SWill Deacon break; 376257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G3: 377257cb251SWill Deacon /* We're using the top bits so we can't overflow. */ 378257cb251SWill Deacon overflow_check = false; 379257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48, 380b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ); 381257cb251SWill Deacon break; 382257cb251SWill Deacon case R_AARCH64_MOVW_SABS_G0: 383257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0, 384c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ); 385257cb251SWill Deacon break; 386257cb251SWill Deacon case R_AARCH64_MOVW_SABS_G1: 387257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16, 388c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ); 389257cb251SWill Deacon break; 390257cb251SWill Deacon case R_AARCH64_MOVW_SABS_G2: 391257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32, 392c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ); 393257cb251SWill Deacon break; 394257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G0_NC: 395257cb251SWill Deacon overflow_check = false; 396257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0, 397b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ); 398257cb251SWill Deacon break; 399257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G0: 400257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0, 401c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ); 402257cb251SWill Deacon break; 403257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G1_NC: 404257cb251SWill Deacon overflow_check = false; 405257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16, 406b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ); 407257cb251SWill Deacon break; 408257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G1: 409257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16, 410c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ); 411257cb251SWill Deacon break; 412257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G2_NC: 413257cb251SWill Deacon overflow_check = false; 414257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32, 415b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ); 416257cb251SWill Deacon break; 417257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G2: 418257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32, 419c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ); 420257cb251SWill Deacon break; 421257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G3: 422257cb251SWill Deacon /* We're using the top bits so we can't overflow. */ 423257cb251SWill Deacon overflow_check = false; 424257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48, 425c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ); 426257cb251SWill Deacon break; 427257cb251SWill Deacon 428257cb251SWill Deacon /* Immediate instruction relocations. */ 429257cb251SWill Deacon case R_AARCH64_LD_PREL_LO19: 430257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19, 431c84fced8SJiang Liu AARCH64_INSN_IMM_19); 432257cb251SWill Deacon break; 433257cb251SWill Deacon case R_AARCH64_ADR_PREL_LO21: 434257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21, 435c84fced8SJiang Liu AARCH64_INSN_IMM_ADR); 436257cb251SWill Deacon break; 437257cb251SWill Deacon case R_AARCH64_ADR_PREL_PG_HI21_NC: 438257cb251SWill Deacon overflow_check = false; 439df561f66SGustavo A. R. Silva fallthrough; 440257cb251SWill Deacon case R_AARCH64_ADR_PREL_PG_HI21: 441c8ebf64eSJessica Yu ovf = reloc_insn_adrp(me, sechdrs, loc, val); 442a257e025SArd Biesheuvel if (ovf && ovf != -ERANGE) 443a257e025SArd Biesheuvel return ovf; 444257cb251SWill Deacon break; 445257cb251SWill Deacon case R_AARCH64_ADD_ABS_LO12_NC: 446257cb251SWill Deacon case R_AARCH64_LDST8_ABS_LO12_NC: 447257cb251SWill Deacon overflow_check = false; 448257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12, 449c84fced8SJiang Liu AARCH64_INSN_IMM_12); 450257cb251SWill Deacon break; 451257cb251SWill Deacon case R_AARCH64_LDST16_ABS_LO12_NC: 452257cb251SWill Deacon overflow_check = false; 453257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11, 454c84fced8SJiang Liu AARCH64_INSN_IMM_12); 455257cb251SWill Deacon break; 456257cb251SWill Deacon case R_AARCH64_LDST32_ABS_LO12_NC: 457257cb251SWill Deacon overflow_check = false; 458257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10, 459c84fced8SJiang Liu AARCH64_INSN_IMM_12); 460257cb251SWill Deacon break; 461257cb251SWill Deacon case R_AARCH64_LDST64_ABS_LO12_NC: 462257cb251SWill Deacon overflow_check = false; 463257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9, 464c84fced8SJiang Liu AARCH64_INSN_IMM_12); 465257cb251SWill Deacon break; 466257cb251SWill Deacon case R_AARCH64_LDST128_ABS_LO12_NC: 467257cb251SWill Deacon overflow_check = false; 468257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8, 469c84fced8SJiang Liu AARCH64_INSN_IMM_12); 470257cb251SWill Deacon break; 471257cb251SWill Deacon case R_AARCH64_TSTBR14: 472257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14, 473c84fced8SJiang Liu AARCH64_INSN_IMM_14); 474257cb251SWill Deacon break; 475257cb251SWill Deacon case R_AARCH64_CONDBR19: 476257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19, 477c84fced8SJiang Liu AARCH64_INSN_IMM_19); 478257cb251SWill Deacon break; 479257cb251SWill Deacon case R_AARCH64_JUMP26: 480257cb251SWill Deacon case R_AARCH64_CALL26: 481257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26, 482c84fced8SJiang Liu AARCH64_INSN_IMM_26); 483*ea3752baSMark Rutland if (ovf == -ERANGE) { 484c8ebf64eSJessica Yu val = module_emit_plt_entry(me, sechdrs, loc, &rel[i], sym); 4855e8307b9SArd Biesheuvel if (!val) 4865e8307b9SArd Biesheuvel return -ENOEXEC; 487fd045f6cSArd Biesheuvel ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 488fd045f6cSArd Biesheuvel 26, AARCH64_INSN_IMM_26); 489fd045f6cSArd Biesheuvel } 490257cb251SWill Deacon break; 491257cb251SWill Deacon 492257cb251SWill Deacon default: 493257cb251SWill Deacon pr_err("module %s: unsupported RELA relocation: %llu\n", 494257cb251SWill Deacon me->name, ELF64_R_TYPE(rel[i].r_info)); 495257cb251SWill Deacon return -ENOEXEC; 496257cb251SWill Deacon } 497257cb251SWill Deacon 498257cb251SWill Deacon if (overflow_check && ovf == -ERANGE) 499257cb251SWill Deacon goto overflow; 500257cb251SWill Deacon 501257cb251SWill Deacon } 502257cb251SWill Deacon 503257cb251SWill Deacon return 0; 504257cb251SWill Deacon 505257cb251SWill Deacon overflow: 506257cb251SWill Deacon pr_err("module %s: overflow in relocation type %d val %Lx\n", 507257cb251SWill Deacon me->name, (int)ELF64_R_TYPE(rel[i].r_info), val); 508257cb251SWill Deacon return -ENOEXEC; 509257cb251SWill Deacon } 510932ded4bSAndre Przywara 5113b23e499STorsten Duwe static inline void __init_plt(struct plt_entry *plt, unsigned long addr) 5123b23e499STorsten Duwe { 5133b23e499STorsten Duwe *plt = get_plt_entry(addr, plt); 5143b23e499STorsten Duwe } 5153b23e499STorsten Duwe 516f1a54ae9SMark Rutland static int module_init_ftrace_plt(const Elf_Ehdr *hdr, 517f1a54ae9SMark Rutland const Elf_Shdr *sechdrs, 518f1a54ae9SMark Rutland struct module *mod) 519f1a54ae9SMark Rutland { 520*ea3752baSMark Rutland #if defined(CONFIG_DYNAMIC_FTRACE) 521f1a54ae9SMark Rutland const Elf_Shdr *s; 5223b23e499STorsten Duwe struct plt_entry *plts; 523f1a54ae9SMark Rutland 524f1a54ae9SMark Rutland s = find_section(hdr, sechdrs, ".text.ftrace_trampoline"); 525f1a54ae9SMark Rutland if (!s) 526f1a54ae9SMark Rutland return -ENOEXEC; 527f1a54ae9SMark Rutland 5283b23e499STorsten Duwe plts = (void *)s->sh_addr; 5293b23e499STorsten Duwe 5303b23e499STorsten Duwe __init_plt(&plts[FTRACE_PLT_IDX], FTRACE_ADDR); 5313b23e499STorsten Duwe 5323b23e499STorsten Duwe mod->arch.ftrace_trampolines = plts; 533f1a54ae9SMark Rutland #endif 534f1a54ae9SMark Rutland return 0; 535f1a54ae9SMark Rutland } 536f1a54ae9SMark Rutland 537bd8b21d3SMark Rutland int module_finalize(const Elf_Ehdr *hdr, 538bd8b21d3SMark Rutland const Elf_Shdr *sechdrs, 539bd8b21d3SMark Rutland struct module *me) 540bd8b21d3SMark Rutland { 541bd8b21d3SMark Rutland const Elf_Shdr *s; 542bd8b21d3SMark Rutland s = find_section(hdr, sechdrs, ".altinstructions"); 543bd8b21d3SMark Rutland if (s) 544bd8b21d3SMark Rutland apply_alternatives_module((void *)s->sh_addr, s->sh_size); 545bd8b21d3SMark Rutland 5463b619e22SArd Biesheuvel if (scs_is_dynamic()) { 5473b619e22SArd Biesheuvel s = find_section(hdr, sechdrs, ".init.eh_frame"); 5483b619e22SArd Biesheuvel if (s) 5493b619e22SArd Biesheuvel scs_patch((void *)s->sh_addr, s->sh_size); 5503b619e22SArd Biesheuvel } 5513b619e22SArd Biesheuvel 552f1a54ae9SMark Rutland return module_init_ftrace_plt(hdr, sechdrs, me); 553932ded4bSAndre Przywara } 554