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
10*3e35d303SMark Rutland #define pr_fmt(fmt) "Modules: " fmt
11*3e35d303SMark Rutland
12257cb251SWill Deacon #include <linux/bitops.h>
13257cb251SWill Deacon #include <linux/elf.h>
14f1a54ae9SMark Rutland #include <linux/ftrace.h>
15257cb251SWill Deacon #include <linux/gfp.h>
1639d114ddSAndrey Ryabinin #include <linux/kasan.h>
17257cb251SWill Deacon #include <linux/kernel.h>
18257cb251SWill Deacon #include <linux/mm.h>
19257cb251SWill Deacon #include <linux/moduleloader.h>
20e46b7103SMark Rutland #include <linux/random.h>
213b619e22SArd Biesheuvel #include <linux/scs.h>
22257cb251SWill Deacon #include <linux/vmalloc.h>
23e46b7103SMark Rutland
242c2b282dSPaul Walmsley #include <asm/alternative.h>
25c84fced8SJiang Liu #include <asm/insn.h>
263b619e22SArd Biesheuvel #include <asm/scs.h>
27932ded4bSAndre Przywara #include <asm/sections.h>
28c84fced8SJiang Liu
29*3e35d303SMark Rutland static u64 module_direct_base __ro_after_init = 0;
30*3e35d303SMark Rutland static u64 module_plt_base __ro_after_init = 0;
31e46b7103SMark Rutland
32*3e35d303SMark Rutland /*
33*3e35d303SMark Rutland * Choose a random page-aligned base address for a window of 'size' bytes which
34*3e35d303SMark Rutland * entirely contains the interval [start, end - 1].
35*3e35d303SMark Rutland */
random_bounding_box(u64 size,u64 start,u64 end)36*3e35d303SMark Rutland static u64 __init random_bounding_box(u64 size, u64 start, u64 end)
37e46b7103SMark Rutland {
38*3e35d303SMark Rutland u64 max_pgoff, pgoff;
39e46b7103SMark Rutland
40*3e35d303SMark Rutland if ((end - start) >= size)
41e46b7103SMark Rutland return 0;
42e46b7103SMark Rutland
43*3e35d303SMark Rutland max_pgoff = (size - (end - start)) / PAGE_SIZE;
44*3e35d303SMark Rutland pgoff = get_random_u32_inclusive(0, max_pgoff);
45*3e35d303SMark Rutland
46*3e35d303SMark Rutland return start - pgoff * PAGE_SIZE;
47*3e35d303SMark Rutland }
48*3e35d303SMark Rutland
49*3e35d303SMark Rutland /*
50*3e35d303SMark Rutland * Modules may directly reference data and text anywhere within the kernel
51*3e35d303SMark Rutland * image and other modules. References using PREL32 relocations have a +/-2G
52*3e35d303SMark Rutland * range, and so we need to ensure that the entire kernel image and all modules
53*3e35d303SMark Rutland * fall within a 2G window such that these are always within range.
54*3e35d303SMark Rutland *
55*3e35d303SMark Rutland * Modules may directly branch to functions and code within the kernel text,
56*3e35d303SMark Rutland * and to functions and code within other modules. These branches will use
57*3e35d303SMark Rutland * CALL26/JUMP26 relocations with a +/-128M range. Without PLTs, we must ensure
58*3e35d303SMark Rutland * that the entire kernel text and all module text falls within a 128M window
59*3e35d303SMark Rutland * such that these are always within range. With PLTs, we can expand this to a
60*3e35d303SMark Rutland * 2G window.
61*3e35d303SMark Rutland *
62*3e35d303SMark Rutland * We chose the 128M region to surround the entire kernel image (rather than
63*3e35d303SMark Rutland * just the text) as using the same bounds for the 128M and 2G regions ensures
64*3e35d303SMark Rutland * by construction that we never select a 128M region that is not a subset of
65*3e35d303SMark Rutland * the 2G region. For very large and unusual kernel configurations this means
66*3e35d303SMark Rutland * we may fall back to PLTs where they could have been avoided, but this keeps
67*3e35d303SMark Rutland * the logic significantly simpler.
68*3e35d303SMark Rutland */
module_init_limits(void)69*3e35d303SMark Rutland static int __init module_init_limits(void)
70*3e35d303SMark Rutland {
71*3e35d303SMark Rutland u64 kernel_end = (u64)_end;
72*3e35d303SMark Rutland u64 kernel_start = (u64)_text;
73*3e35d303SMark Rutland u64 kernel_size = kernel_end - kernel_start;
74*3e35d303SMark Rutland
75*3e35d303SMark Rutland /*
76*3e35d303SMark Rutland * The default modules region is placed immediately below the kernel
77*3e35d303SMark Rutland * image, and is large enough to use the full 2G relocation range.
78*3e35d303SMark Rutland */
79*3e35d303SMark Rutland BUILD_BUG_ON(KIMAGE_VADDR != MODULES_END);
80*3e35d303SMark Rutland BUILD_BUG_ON(MODULES_VSIZE < SZ_2G);
81*3e35d303SMark Rutland
82*3e35d303SMark Rutland if (!kaslr_enabled()) {
83*3e35d303SMark Rutland if (kernel_size < SZ_128M)
84*3e35d303SMark Rutland module_direct_base = kernel_end - SZ_128M;
85*3e35d303SMark Rutland if (kernel_size < SZ_2G)
86*3e35d303SMark Rutland module_plt_base = kernel_end - SZ_2G;
87*3e35d303SMark Rutland } else {
88*3e35d303SMark Rutland u64 min = kernel_start;
89*3e35d303SMark Rutland u64 max = kernel_end;
90e46b7103SMark Rutland
91e46b7103SMark Rutland if (IS_ENABLED(CONFIG_RANDOMIZE_MODULE_REGION_FULL)) {
92*3e35d303SMark Rutland pr_info("2G module region forced by RANDOMIZE_MODULE_REGION_FULL\n");
93e46b7103SMark Rutland } else {
94*3e35d303SMark Rutland module_direct_base = random_bounding_box(SZ_128M, min, max);
95*3e35d303SMark Rutland if (module_direct_base) {
96*3e35d303SMark Rutland min = module_direct_base;
97*3e35d303SMark Rutland max = module_direct_base + SZ_128M;
98*3e35d303SMark Rutland }
99e46b7103SMark Rutland }
100e46b7103SMark Rutland
101*3e35d303SMark Rutland module_plt_base = random_bounding_box(SZ_2G, min, max);
102*3e35d303SMark Rutland }
103*3e35d303SMark Rutland
104*3e35d303SMark Rutland pr_info("%llu pages in range for non-PLT usage",
105*3e35d303SMark Rutland module_direct_base ? (SZ_128M - kernel_size) / PAGE_SIZE : 0);
106*3e35d303SMark Rutland pr_info("%llu pages in range for PLT usage",
107*3e35d303SMark Rutland module_plt_base ? (SZ_2G - kernel_size) / PAGE_SIZE : 0);
108e46b7103SMark Rutland
109e46b7103SMark Rutland return 0;
110e46b7103SMark Rutland }
111*3e35d303SMark Rutland subsys_initcall(module_init_limits);
112e46b7103SMark Rutland
module_alloc(unsigned long size)113257cb251SWill Deacon void *module_alloc(unsigned long size)
114257cb251SWill Deacon {
115*3e35d303SMark Rutland void *p = NULL;
11639d114ddSAndrey Ryabinin
117ea3752baSMark Rutland /*
118ea3752baSMark Rutland * Where possible, prefer to allocate within direct branch range of the
119*3e35d303SMark Rutland * kernel such that no PLTs are necessary.
120ea3752baSMark Rutland */
121*3e35d303SMark Rutland if (module_direct_base) {
122*3e35d303SMark Rutland p = __vmalloc_node_range(size, MODULE_ALIGN,
123*3e35d303SMark Rutland module_direct_base,
124*3e35d303SMark Rutland module_direct_base + SZ_128M,
125*3e35d303SMark Rutland GFP_KERNEL | __GFP_NOWARN,
126ea3752baSMark Rutland PAGE_KERNEL, 0, NUMA_NO_NODE,
127ea3752baSMark Rutland __builtin_return_address(0));
128*3e35d303SMark Rutland }
129*3e35d303SMark Rutland
130*3e35d303SMark Rutland if (!p && module_plt_base) {
131*3e35d303SMark Rutland p = __vmalloc_node_range(size, MODULE_ALIGN,
132*3e35d303SMark Rutland module_plt_base,
133*3e35d303SMark Rutland module_plt_base + SZ_2G,
134*3e35d303SMark Rutland GFP_KERNEL | __GFP_NOWARN,
135*3e35d303SMark Rutland PAGE_KERNEL, 0, NUMA_NO_NODE,
136*3e35d303SMark Rutland __builtin_return_address(0));
137*3e35d303SMark Rutland }
13839d114ddSAndrey Ryabinin
139ea3752baSMark Rutland if (!p) {
140*3e35d303SMark Rutland pr_warn_ratelimited("%s: unable to allocate memory\n",
141*3e35d303SMark Rutland __func__);
1428339f7d8SMark Rutland }
143fd045f6cSArd Biesheuvel
144ea3752baSMark Rutland if (p && (kasan_alloc_module_shadow(p, size, GFP_KERNEL) < 0)) {
14539d114ddSAndrey Ryabinin vfree(p);
14639d114ddSAndrey Ryabinin return NULL;
14739d114ddSAndrey Ryabinin }
14839d114ddSAndrey Ryabinin
14936c4a73bSAndrey Konovalov /* Memory is intended to be executable, reset the pointer tag. */
15036c4a73bSAndrey Konovalov return kasan_reset_tag(p);
151257cb251SWill Deacon }
152257cb251SWill Deacon
153257cb251SWill Deacon enum aarch64_reloc_op {
154257cb251SWill Deacon RELOC_OP_NONE,
155257cb251SWill Deacon RELOC_OP_ABS,
156257cb251SWill Deacon RELOC_OP_PREL,
157257cb251SWill Deacon RELOC_OP_PAGE,
158257cb251SWill Deacon };
159257cb251SWill Deacon
do_reloc(enum aarch64_reloc_op reloc_op,__le32 * place,u64 val)16002129ae5SLuc Van Oostenryck static u64 do_reloc(enum aarch64_reloc_op reloc_op, __le32 *place, u64 val)
161257cb251SWill Deacon {
162257cb251SWill Deacon switch (reloc_op) {
163257cb251SWill Deacon case RELOC_OP_ABS:
164257cb251SWill Deacon return val;
165257cb251SWill Deacon case RELOC_OP_PREL:
166257cb251SWill Deacon return val - (u64)place;
167257cb251SWill Deacon case RELOC_OP_PAGE:
168257cb251SWill Deacon return (val & ~0xfff) - ((u64)place & ~0xfff);
169257cb251SWill Deacon case RELOC_OP_NONE:
170257cb251SWill Deacon return 0;
171257cb251SWill Deacon }
172257cb251SWill Deacon
173257cb251SWill Deacon pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
174257cb251SWill Deacon return 0;
175257cb251SWill Deacon }
176257cb251SWill Deacon
reloc_data(enum aarch64_reloc_op op,void * place,u64 val,int len)177257cb251SWill Deacon static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
178257cb251SWill Deacon {
179257cb251SWill Deacon s64 sval = do_reloc(op, place, val);
180257cb251SWill Deacon
1811cf24a2cSArd Biesheuvel /*
1821cf24a2cSArd Biesheuvel * The ELF psABI for AArch64 documents the 16-bit and 32-bit place
1833fd00bebSArd Biesheuvel * relative and absolute relocations as having a range of [-2^15, 2^16)
1843fd00bebSArd Biesheuvel * or [-2^31, 2^32), respectively. However, in order to be able to
1853fd00bebSArd Biesheuvel * detect overflows reliably, we have to choose whether we interpret
1863fd00bebSArd Biesheuvel * such quantities as signed or as unsigned, and stick with it.
1871cf24a2cSArd Biesheuvel * The way we organize our address space requires a signed
1881cf24a2cSArd Biesheuvel * interpretation of 32-bit relative references, so let's use that
1891cf24a2cSArd Biesheuvel * for all R_AARCH64_PRELxx relocations. This means our upper
1901cf24a2cSArd Biesheuvel * bound for overflow detection should be Sxx_MAX rather than Uxx_MAX.
1911cf24a2cSArd Biesheuvel */
1921cf24a2cSArd Biesheuvel
193257cb251SWill Deacon switch (len) {
194257cb251SWill Deacon case 16:
195257cb251SWill Deacon *(s16 *)place = sval;
1963fd00bebSArd Biesheuvel switch (op) {
1973fd00bebSArd Biesheuvel case RELOC_OP_ABS:
1983fd00bebSArd Biesheuvel if (sval < 0 || sval > U16_MAX)
1993fd00bebSArd Biesheuvel return -ERANGE;
2003fd00bebSArd Biesheuvel break;
2013fd00bebSArd Biesheuvel case RELOC_OP_PREL:
2021cf24a2cSArd Biesheuvel if (sval < S16_MIN || sval > S16_MAX)
203f9308969SArd Biesheuvel return -ERANGE;
204257cb251SWill Deacon break;
2053fd00bebSArd Biesheuvel default:
2063fd00bebSArd Biesheuvel pr_err("Invalid 16-bit data relocation (%d)\n", op);
2073fd00bebSArd Biesheuvel return 0;
2083fd00bebSArd Biesheuvel }
2093fd00bebSArd Biesheuvel break;
210257cb251SWill Deacon case 32:
211257cb251SWill Deacon *(s32 *)place = sval;
2123fd00bebSArd Biesheuvel switch (op) {
2133fd00bebSArd Biesheuvel case RELOC_OP_ABS:
2143fd00bebSArd Biesheuvel if (sval < 0 || sval > U32_MAX)
2153fd00bebSArd Biesheuvel return -ERANGE;
2163fd00bebSArd Biesheuvel break;
2173fd00bebSArd Biesheuvel case RELOC_OP_PREL:
2181cf24a2cSArd Biesheuvel if (sval < S32_MIN || sval > S32_MAX)
219f9308969SArd Biesheuvel return -ERANGE;
220257cb251SWill Deacon break;
2213fd00bebSArd Biesheuvel default:
2223fd00bebSArd Biesheuvel pr_err("Invalid 32-bit data relocation (%d)\n", op);
2233fd00bebSArd Biesheuvel return 0;
2243fd00bebSArd Biesheuvel }
2253fd00bebSArd Biesheuvel break;
226257cb251SWill Deacon case 64:
227257cb251SWill Deacon *(s64 *)place = sval;
228257cb251SWill Deacon break;
229257cb251SWill Deacon default:
230257cb251SWill Deacon pr_err("Invalid length (%d) for data relocation\n", len);
231257cb251SWill Deacon return 0;
232257cb251SWill Deacon }
233257cb251SWill Deacon return 0;
234257cb251SWill Deacon }
235257cb251SWill Deacon
236b24a5575SArd Biesheuvel enum aarch64_insn_movw_imm_type {
237b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVNZ,
238b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ,
239b24a5575SArd Biesheuvel };
240b24a5575SArd Biesheuvel
reloc_insn_movw(enum aarch64_reloc_op op,__le32 * place,u64 val,int lsb,enum aarch64_insn_movw_imm_type imm_type)24102129ae5SLuc Van Oostenryck static int reloc_insn_movw(enum aarch64_reloc_op op, __le32 *place, u64 val,
242b24a5575SArd Biesheuvel int lsb, enum aarch64_insn_movw_imm_type imm_type)
243257cb251SWill Deacon {
244b24a5575SArd Biesheuvel u64 imm;
245c84fced8SJiang Liu s64 sval;
24602129ae5SLuc Van Oostenryck u32 insn = le32_to_cpu(*place);
247257cb251SWill Deacon
248c84fced8SJiang Liu sval = do_reloc(op, place, val);
249b24a5575SArd Biesheuvel imm = sval >> lsb;
250122e2fa0SWill Deacon
251c84fced8SJiang Liu if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
252257cb251SWill Deacon /*
253257cb251SWill Deacon * For signed MOVW relocations, we have to manipulate the
254257cb251SWill Deacon * instruction encoding depending on whether or not the
255257cb251SWill Deacon * immediate is less than zero.
256257cb251SWill Deacon */
257257cb251SWill Deacon insn &= ~(3 << 29);
258b24a5575SArd Biesheuvel if (sval >= 0) {
259257cb251SWill Deacon /* >=0: Set the instruction to MOVZ (opcode 10b). */
260257cb251SWill Deacon insn |= 2 << 29;
261257cb251SWill Deacon } else {
262257cb251SWill Deacon /*
263257cb251SWill Deacon * <0: Set the instruction to MOVN (opcode 00b).
264257cb251SWill Deacon * Since we've masked the opcode already, we
265257cb251SWill Deacon * don't need to do anything other than
266257cb251SWill Deacon * inverting the new immediate field.
267257cb251SWill Deacon */
268257cb251SWill Deacon imm = ~imm;
269257cb251SWill Deacon }
270257cb251SWill Deacon }
271257cb251SWill Deacon
272257cb251SWill Deacon /* Update the instruction with the new encoding. */
273b24a5575SArd Biesheuvel insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm);
27402129ae5SLuc Van Oostenryck *place = cpu_to_le32(insn);
275257cb251SWill Deacon
276b24a5575SArd Biesheuvel if (imm > U16_MAX)
277257cb251SWill Deacon return -ERANGE;
278257cb251SWill Deacon
279257cb251SWill Deacon return 0;
280257cb251SWill Deacon }
281257cb251SWill Deacon
reloc_insn_imm(enum aarch64_reloc_op op,__le32 * place,u64 val,int lsb,int len,enum aarch64_insn_imm_type imm_type)28202129ae5SLuc Van Oostenryck static int reloc_insn_imm(enum aarch64_reloc_op op, __le32 *place, u64 val,
283c84fced8SJiang Liu int lsb, int len, enum aarch64_insn_imm_type imm_type)
284257cb251SWill Deacon {
285257cb251SWill Deacon u64 imm, imm_mask;
286257cb251SWill Deacon s64 sval;
28702129ae5SLuc Van Oostenryck u32 insn = le32_to_cpu(*place);
288257cb251SWill Deacon
289257cb251SWill Deacon /* Calculate the relocation value. */
290257cb251SWill Deacon sval = do_reloc(op, place, val);
291257cb251SWill Deacon sval >>= lsb;
292257cb251SWill Deacon
293257cb251SWill Deacon /* Extract the value bits and shift them to bit 0. */
294257cb251SWill Deacon imm_mask = (BIT(lsb + len) - 1) >> lsb;
295257cb251SWill Deacon imm = sval & imm_mask;
296257cb251SWill Deacon
297257cb251SWill Deacon /* Update the instruction's immediate field. */
298c84fced8SJiang Liu insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
29902129ae5SLuc Van Oostenryck *place = cpu_to_le32(insn);
300257cb251SWill Deacon
301257cb251SWill Deacon /*
302257cb251SWill Deacon * Extract the upper value bits (including the sign bit) and
303257cb251SWill Deacon * shift them to bit 0.
304257cb251SWill Deacon */
305257cb251SWill Deacon sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
306257cb251SWill Deacon
307257cb251SWill Deacon /*
308257cb251SWill Deacon * Overflow has occurred if the upper bits are not all equal to
309257cb251SWill Deacon * the sign bit of the value.
310257cb251SWill Deacon */
311257cb251SWill Deacon if ((u64)(sval + 1) >= 2)
312257cb251SWill Deacon return -ERANGE;
313257cb251SWill Deacon
314257cb251SWill Deacon return 0;
315257cb251SWill Deacon }
316257cb251SWill Deacon
reloc_insn_adrp(struct module * mod,Elf64_Shdr * sechdrs,__le32 * place,u64 val)317c8ebf64eSJessica Yu static int reloc_insn_adrp(struct module *mod, Elf64_Shdr *sechdrs,
318c8ebf64eSJessica Yu __le32 *place, u64 val)
319a257e025SArd Biesheuvel {
320a257e025SArd Biesheuvel u32 insn;
321a257e025SArd Biesheuvel
322bdb85cd1SArd Biesheuvel if (!is_forbidden_offset_for_adrp(place))
323a257e025SArd Biesheuvel return reloc_insn_imm(RELOC_OP_PAGE, place, val, 12, 21,
324a257e025SArd Biesheuvel AARCH64_INSN_IMM_ADR);
325a257e025SArd Biesheuvel
326a257e025SArd Biesheuvel /* patch ADRP to ADR if it is in range */
327a257e025SArd Biesheuvel if (!reloc_insn_imm(RELOC_OP_PREL, place, val & ~0xfff, 0, 21,
328a257e025SArd Biesheuvel AARCH64_INSN_IMM_ADR)) {
329a257e025SArd Biesheuvel insn = le32_to_cpu(*place);
330a257e025SArd Biesheuvel insn &= ~BIT(31);
331a257e025SArd Biesheuvel } else {
332a257e025SArd Biesheuvel /* out of range for ADR -> emit a veneer */
333c8ebf64eSJessica Yu val = module_emit_veneer_for_adrp(mod, sechdrs, place, val & ~0xfff);
334a257e025SArd Biesheuvel if (!val)
335a257e025SArd Biesheuvel return -ENOEXEC;
336a257e025SArd Biesheuvel insn = aarch64_insn_gen_branch_imm((u64)place, val,
337a257e025SArd Biesheuvel AARCH64_INSN_BRANCH_NOLINK);
338a257e025SArd Biesheuvel }
339a257e025SArd Biesheuvel
340a257e025SArd Biesheuvel *place = cpu_to_le32(insn);
341a257e025SArd Biesheuvel return 0;
342a257e025SArd Biesheuvel }
343a257e025SArd Biesheuvel
apply_relocate_add(Elf64_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me)344257cb251SWill Deacon int apply_relocate_add(Elf64_Shdr *sechdrs,
345257cb251SWill Deacon const char *strtab,
346257cb251SWill Deacon unsigned int symindex,
347257cb251SWill Deacon unsigned int relsec,
348257cb251SWill Deacon struct module *me)
349257cb251SWill Deacon {
350257cb251SWill Deacon unsigned int i;
351257cb251SWill Deacon int ovf;
352257cb251SWill Deacon bool overflow_check;
353257cb251SWill Deacon Elf64_Sym *sym;
354257cb251SWill Deacon void *loc;
355257cb251SWill Deacon u64 val;
356257cb251SWill Deacon Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
357257cb251SWill Deacon
358257cb251SWill Deacon for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
359257cb251SWill Deacon /* loc corresponds to P in the AArch64 ELF document. */
360257cb251SWill Deacon loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
361257cb251SWill Deacon + rel[i].r_offset;
362257cb251SWill Deacon
363257cb251SWill Deacon /* sym is the ELF symbol we're referring to. */
364257cb251SWill Deacon sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
365257cb251SWill Deacon + ELF64_R_SYM(rel[i].r_info);
366257cb251SWill Deacon
367257cb251SWill Deacon /* val corresponds to (S + A) in the AArch64 ELF document. */
368257cb251SWill Deacon val = sym->st_value + rel[i].r_addend;
369257cb251SWill Deacon
370257cb251SWill Deacon /* Check for overflow by default. */
371257cb251SWill Deacon overflow_check = true;
372257cb251SWill Deacon
373257cb251SWill Deacon /* Perform the static relocation. */
374257cb251SWill Deacon switch (ELF64_R_TYPE(rel[i].r_info)) {
375257cb251SWill Deacon /* Null relocations. */
376257cb251SWill Deacon case R_ARM_NONE:
377257cb251SWill Deacon case R_AARCH64_NONE:
378257cb251SWill Deacon ovf = 0;
379257cb251SWill Deacon break;
380257cb251SWill Deacon
381257cb251SWill Deacon /* Data relocations. */
382257cb251SWill Deacon case R_AARCH64_ABS64:
383257cb251SWill Deacon overflow_check = false;
384257cb251SWill Deacon ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
385257cb251SWill Deacon break;
386257cb251SWill Deacon case R_AARCH64_ABS32:
387257cb251SWill Deacon ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
388257cb251SWill Deacon break;
389257cb251SWill Deacon case R_AARCH64_ABS16:
390257cb251SWill Deacon ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
391257cb251SWill Deacon break;
392257cb251SWill Deacon case R_AARCH64_PREL64:
393257cb251SWill Deacon overflow_check = false;
394257cb251SWill Deacon ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
395257cb251SWill Deacon break;
396257cb251SWill Deacon case R_AARCH64_PREL32:
397257cb251SWill Deacon ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
398257cb251SWill Deacon break;
399257cb251SWill Deacon case R_AARCH64_PREL16:
400257cb251SWill Deacon ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
401257cb251SWill Deacon break;
402257cb251SWill Deacon
403257cb251SWill Deacon /* MOVW instruction relocations. */
404257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G0_NC:
405257cb251SWill Deacon overflow_check = false;
406df561f66SGustavo A. R. Silva fallthrough;
407257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G0:
408257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
409b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ);
410257cb251SWill Deacon break;
411257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G1_NC:
412257cb251SWill Deacon overflow_check = false;
413df561f66SGustavo A. R. Silva fallthrough;
414257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G1:
415257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
416b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ);
417257cb251SWill Deacon break;
418257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G2_NC:
419257cb251SWill Deacon overflow_check = false;
420df561f66SGustavo A. R. Silva fallthrough;
421257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G2:
422257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
423b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ);
424257cb251SWill Deacon break;
425257cb251SWill Deacon case R_AARCH64_MOVW_UABS_G3:
426257cb251SWill Deacon /* We're using the top bits so we can't overflow. */
427257cb251SWill Deacon overflow_check = false;
428257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
429b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ);
430257cb251SWill Deacon break;
431257cb251SWill Deacon case R_AARCH64_MOVW_SABS_G0:
432257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
433c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ);
434257cb251SWill Deacon break;
435257cb251SWill Deacon case R_AARCH64_MOVW_SABS_G1:
436257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
437c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ);
438257cb251SWill Deacon break;
439257cb251SWill Deacon case R_AARCH64_MOVW_SABS_G2:
440257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
441c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ);
442257cb251SWill Deacon break;
443257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G0_NC:
444257cb251SWill Deacon overflow_check = false;
445257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
446b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ);
447257cb251SWill Deacon break;
448257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G0:
449257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
450c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ);
451257cb251SWill Deacon break;
452257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G1_NC:
453257cb251SWill Deacon overflow_check = false;
454257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
455b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ);
456257cb251SWill Deacon break;
457257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G1:
458257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
459c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ);
460257cb251SWill Deacon break;
461257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G2_NC:
462257cb251SWill Deacon overflow_check = false;
463257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
464b24a5575SArd Biesheuvel AARCH64_INSN_IMM_MOVKZ);
465257cb251SWill Deacon break;
466257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G2:
467257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
468c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ);
469257cb251SWill Deacon break;
470257cb251SWill Deacon case R_AARCH64_MOVW_PREL_G3:
471257cb251SWill Deacon /* We're using the top bits so we can't overflow. */
472257cb251SWill Deacon overflow_check = false;
473257cb251SWill Deacon ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48,
474c84fced8SJiang Liu AARCH64_INSN_IMM_MOVNZ);
475257cb251SWill Deacon break;
476257cb251SWill Deacon
477257cb251SWill Deacon /* Immediate instruction relocations. */
478257cb251SWill Deacon case R_AARCH64_LD_PREL_LO19:
479257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
480c84fced8SJiang Liu AARCH64_INSN_IMM_19);
481257cb251SWill Deacon break;
482257cb251SWill Deacon case R_AARCH64_ADR_PREL_LO21:
483257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
484c84fced8SJiang Liu AARCH64_INSN_IMM_ADR);
485257cb251SWill Deacon break;
486257cb251SWill Deacon case R_AARCH64_ADR_PREL_PG_HI21_NC:
487257cb251SWill Deacon overflow_check = false;
488df561f66SGustavo A. R. Silva fallthrough;
489257cb251SWill Deacon case R_AARCH64_ADR_PREL_PG_HI21:
490c8ebf64eSJessica Yu ovf = reloc_insn_adrp(me, sechdrs, loc, val);
491a257e025SArd Biesheuvel if (ovf && ovf != -ERANGE)
492a257e025SArd Biesheuvel return ovf;
493257cb251SWill Deacon break;
494257cb251SWill Deacon case R_AARCH64_ADD_ABS_LO12_NC:
495257cb251SWill Deacon case R_AARCH64_LDST8_ABS_LO12_NC:
496257cb251SWill Deacon overflow_check = false;
497257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
498c84fced8SJiang Liu AARCH64_INSN_IMM_12);
499257cb251SWill Deacon break;
500257cb251SWill Deacon case R_AARCH64_LDST16_ABS_LO12_NC:
501257cb251SWill Deacon overflow_check = false;
502257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
503c84fced8SJiang Liu AARCH64_INSN_IMM_12);
504257cb251SWill Deacon break;
505257cb251SWill Deacon case R_AARCH64_LDST32_ABS_LO12_NC:
506257cb251SWill Deacon overflow_check = false;
507257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
508c84fced8SJiang Liu AARCH64_INSN_IMM_12);
509257cb251SWill Deacon break;
510257cb251SWill Deacon case R_AARCH64_LDST64_ABS_LO12_NC:
511257cb251SWill Deacon overflow_check = false;
512257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
513c84fced8SJiang Liu AARCH64_INSN_IMM_12);
514257cb251SWill Deacon break;
515257cb251SWill Deacon case R_AARCH64_LDST128_ABS_LO12_NC:
516257cb251SWill Deacon overflow_check = false;
517257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
518c84fced8SJiang Liu AARCH64_INSN_IMM_12);
519257cb251SWill Deacon break;
520257cb251SWill Deacon case R_AARCH64_TSTBR14:
521257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
522c84fced8SJiang Liu AARCH64_INSN_IMM_14);
523257cb251SWill Deacon break;
524257cb251SWill Deacon case R_AARCH64_CONDBR19:
525257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
526c84fced8SJiang Liu AARCH64_INSN_IMM_19);
527257cb251SWill Deacon break;
528257cb251SWill Deacon case R_AARCH64_JUMP26:
529257cb251SWill Deacon case R_AARCH64_CALL26:
530257cb251SWill Deacon ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
531c84fced8SJiang Liu AARCH64_INSN_IMM_26);
532ea3752baSMark Rutland if (ovf == -ERANGE) {
533c8ebf64eSJessica Yu val = module_emit_plt_entry(me, sechdrs, loc, &rel[i], sym);
5345e8307b9SArd Biesheuvel if (!val)
5355e8307b9SArd Biesheuvel return -ENOEXEC;
536fd045f6cSArd Biesheuvel ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2,
537fd045f6cSArd Biesheuvel 26, AARCH64_INSN_IMM_26);
538fd045f6cSArd Biesheuvel }
539257cb251SWill Deacon break;
540257cb251SWill Deacon
541257cb251SWill Deacon default:
542257cb251SWill Deacon pr_err("module %s: unsupported RELA relocation: %llu\n",
543257cb251SWill Deacon me->name, ELF64_R_TYPE(rel[i].r_info));
544257cb251SWill Deacon return -ENOEXEC;
545257cb251SWill Deacon }
546257cb251SWill Deacon
547257cb251SWill Deacon if (overflow_check && ovf == -ERANGE)
548257cb251SWill Deacon goto overflow;
549257cb251SWill Deacon
550257cb251SWill Deacon }
551257cb251SWill Deacon
552257cb251SWill Deacon return 0;
553257cb251SWill Deacon
554257cb251SWill Deacon overflow:
555257cb251SWill Deacon pr_err("module %s: overflow in relocation type %d val %Lx\n",
556257cb251SWill Deacon me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
557257cb251SWill Deacon return -ENOEXEC;
558257cb251SWill Deacon }
559932ded4bSAndre Przywara
__init_plt(struct plt_entry * plt,unsigned long addr)5603b23e499STorsten Duwe static inline void __init_plt(struct plt_entry *plt, unsigned long addr)
5613b23e499STorsten Duwe {
5623b23e499STorsten Duwe *plt = get_plt_entry(addr, plt);
5633b23e499STorsten Duwe }
5643b23e499STorsten Duwe
module_init_ftrace_plt(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * mod)565f1a54ae9SMark Rutland static int module_init_ftrace_plt(const Elf_Ehdr *hdr,
566f1a54ae9SMark Rutland const Elf_Shdr *sechdrs,
567f1a54ae9SMark Rutland struct module *mod)
568f1a54ae9SMark Rutland {
569ea3752baSMark Rutland #if defined(CONFIG_DYNAMIC_FTRACE)
570f1a54ae9SMark Rutland const Elf_Shdr *s;
5713b23e499STorsten Duwe struct plt_entry *plts;
572f1a54ae9SMark Rutland
573f1a54ae9SMark Rutland s = find_section(hdr, sechdrs, ".text.ftrace_trampoline");
574f1a54ae9SMark Rutland if (!s)
575f1a54ae9SMark Rutland return -ENOEXEC;
576f1a54ae9SMark Rutland
5773b23e499STorsten Duwe plts = (void *)s->sh_addr;
5783b23e499STorsten Duwe
5793b23e499STorsten Duwe __init_plt(&plts[FTRACE_PLT_IDX], FTRACE_ADDR);
5803b23e499STorsten Duwe
5813b23e499STorsten Duwe mod->arch.ftrace_trampolines = plts;
582f1a54ae9SMark Rutland #endif
583f1a54ae9SMark Rutland return 0;
584f1a54ae9SMark Rutland }
585f1a54ae9SMark Rutland
module_finalize(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * me)586bd8b21d3SMark Rutland int module_finalize(const Elf_Ehdr *hdr,
587bd8b21d3SMark Rutland const Elf_Shdr *sechdrs,
588bd8b21d3SMark Rutland struct module *me)
589bd8b21d3SMark Rutland {
590bd8b21d3SMark Rutland const Elf_Shdr *s;
591bd8b21d3SMark Rutland s = find_section(hdr, sechdrs, ".altinstructions");
592bd8b21d3SMark Rutland if (s)
593bd8b21d3SMark Rutland apply_alternatives_module((void *)s->sh_addr, s->sh_size);
594bd8b21d3SMark Rutland
5953b619e22SArd Biesheuvel if (scs_is_dynamic()) {
5963b619e22SArd Biesheuvel s = find_section(hdr, sechdrs, ".init.eh_frame");
5973b619e22SArd Biesheuvel if (s)
5983b619e22SArd Biesheuvel scs_patch((void *)s->sh_addr, s->sh_size);
5993b619e22SArd Biesheuvel }
6003b619e22SArd Biesheuvel
601f1a54ae9SMark Rutland return module_init_ftrace_plt(hdr, sechdrs, me);
602932ded4bSAndre Przywara }
603