xref: /openbmc/linux/arch/arm64/kernel/module.c (revision 36c4a73bf8d24721222d9ee4080ac955973fd388)
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 
330fea6e9aSAndrey Konovalov 	if (IS_ENABLED(CONFIG_KASAN_GENERIC) ||
340fea6e9aSAndrey Konovalov 	    IS_ENABLED(CONFIG_KASAN_SW_TAGS))
356f496a55SArd Biesheuvel 		/* don't exceed the static module region - see below */
366f496a55SArd Biesheuvel 		module_alloc_end = MODULES_END;
376f496a55SArd Biesheuvel 
38f80fb3a3SArd Biesheuvel 	p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base,
3960115fa5SKefeng Wang 				module_alloc_end, gfp_mask, PAGE_KERNEL, VM_DEFER_KMEMLEAK,
40cb9e3c29SAndrey Ryabinin 				NUMA_NO_NODE, __builtin_return_address(0));
4139d114ddSAndrey Ryabinin 
42fd045f6cSArd Biesheuvel 	if (!p && IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
4331d02e7aSLecopzer Chen 	    (IS_ENABLED(CONFIG_KASAN_VMALLOC) ||
4431d02e7aSLecopzer Chen 	     (!IS_ENABLED(CONFIG_KASAN_GENERIC) &&
4531d02e7aSLecopzer Chen 	      !IS_ENABLED(CONFIG_KASAN_SW_TAGS))))
46fd045f6cSArd Biesheuvel 		/*
4731d02e7aSLecopzer Chen 		 * KASAN without KASAN_VMALLOC can only deal with module
4831d02e7aSLecopzer Chen 		 * allocations being served from the reserved module region,
4931d02e7aSLecopzer Chen 		 * since the remainder of the vmalloc region is already
5031d02e7aSLecopzer Chen 		 * backed by zero shadow pages, and punching holes into it
5131d02e7aSLecopzer Chen 		 * is non-trivial. Since the module region is not randomized
5231d02e7aSLecopzer Chen 		 * when KASAN is enabled without KASAN_VMALLOC, it is even
53fd045f6cSArd Biesheuvel 		 * less likely that the module region gets exhausted, so we
54fd045f6cSArd Biesheuvel 		 * can simply omit this fallback in that case.
55fd045f6cSArd Biesheuvel 		 */
56f2b9ba87SArd Biesheuvel 		p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base,
57b2eed9b5SArd Biesheuvel 				module_alloc_base + SZ_2G, GFP_KERNEL,
587dfac3c5SArd Biesheuvel 				PAGE_KERNEL, 0, NUMA_NO_NODE,
59f2b9ba87SArd Biesheuvel 				__builtin_return_address(0));
60fd045f6cSArd Biesheuvel 
6163840de2SAndrey Konovalov 	if (p && (kasan_alloc_module_shadow(p, size, gfp_mask) < 0)) {
6239d114ddSAndrey Ryabinin 		vfree(p);
6339d114ddSAndrey Ryabinin 		return NULL;
6439d114ddSAndrey Ryabinin 	}
6539d114ddSAndrey Ryabinin 
66*36c4a73bSAndrey Konovalov 	/* Memory is intended to be executable, reset the pointer tag. */
67*36c4a73bSAndrey Konovalov 	return kasan_reset_tag(p);
68257cb251SWill Deacon }
69257cb251SWill Deacon 
70257cb251SWill Deacon enum aarch64_reloc_op {
71257cb251SWill Deacon 	RELOC_OP_NONE,
72257cb251SWill Deacon 	RELOC_OP_ABS,
73257cb251SWill Deacon 	RELOC_OP_PREL,
74257cb251SWill Deacon 	RELOC_OP_PAGE,
75257cb251SWill Deacon };
76257cb251SWill Deacon 
7702129ae5SLuc Van Oostenryck static u64 do_reloc(enum aarch64_reloc_op reloc_op, __le32 *place, u64 val)
78257cb251SWill Deacon {
79257cb251SWill Deacon 	switch (reloc_op) {
80257cb251SWill Deacon 	case RELOC_OP_ABS:
81257cb251SWill Deacon 		return val;
82257cb251SWill Deacon 	case RELOC_OP_PREL:
83257cb251SWill Deacon 		return val - (u64)place;
84257cb251SWill Deacon 	case RELOC_OP_PAGE:
85257cb251SWill Deacon 		return (val & ~0xfff) - ((u64)place & ~0xfff);
86257cb251SWill Deacon 	case RELOC_OP_NONE:
87257cb251SWill Deacon 		return 0;
88257cb251SWill Deacon 	}
89257cb251SWill Deacon 
90257cb251SWill Deacon 	pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
91257cb251SWill Deacon 	return 0;
92257cb251SWill Deacon }
93257cb251SWill Deacon 
94257cb251SWill Deacon static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
95257cb251SWill Deacon {
96257cb251SWill Deacon 	s64 sval = do_reloc(op, place, val);
97257cb251SWill Deacon 
981cf24a2cSArd Biesheuvel 	/*
991cf24a2cSArd Biesheuvel 	 * The ELF psABI for AArch64 documents the 16-bit and 32-bit place
1003fd00bebSArd Biesheuvel 	 * relative and absolute relocations as having a range of [-2^15, 2^16)
1013fd00bebSArd Biesheuvel 	 * or [-2^31, 2^32), respectively. However, in order to be able to
1023fd00bebSArd Biesheuvel 	 * detect overflows reliably, we have to choose whether we interpret
1033fd00bebSArd Biesheuvel 	 * such quantities as signed or as unsigned, and stick with it.
1041cf24a2cSArd Biesheuvel 	 * The way we organize our address space requires a signed
1051cf24a2cSArd Biesheuvel 	 * interpretation of 32-bit relative references, so let's use that
1061cf24a2cSArd Biesheuvel 	 * for all R_AARCH64_PRELxx relocations. This means our upper
1071cf24a2cSArd Biesheuvel 	 * bound for overflow detection should be Sxx_MAX rather than Uxx_MAX.
1081cf24a2cSArd Biesheuvel 	 */
1091cf24a2cSArd Biesheuvel 
110257cb251SWill Deacon 	switch (len) {
111257cb251SWill Deacon 	case 16:
112257cb251SWill Deacon 		*(s16 *)place = sval;
1133fd00bebSArd Biesheuvel 		switch (op) {
1143fd00bebSArd Biesheuvel 		case RELOC_OP_ABS:
1153fd00bebSArd Biesheuvel 			if (sval < 0 || sval > U16_MAX)
1163fd00bebSArd Biesheuvel 				return -ERANGE;
1173fd00bebSArd Biesheuvel 			break;
1183fd00bebSArd Biesheuvel 		case RELOC_OP_PREL:
1191cf24a2cSArd Biesheuvel 			if (sval < S16_MIN || sval > S16_MAX)
120f9308969SArd Biesheuvel 				return -ERANGE;
121257cb251SWill Deacon 			break;
1223fd00bebSArd Biesheuvel 		default:
1233fd00bebSArd Biesheuvel 			pr_err("Invalid 16-bit data relocation (%d)\n", op);
1243fd00bebSArd Biesheuvel 			return 0;
1253fd00bebSArd Biesheuvel 		}
1263fd00bebSArd Biesheuvel 		break;
127257cb251SWill Deacon 	case 32:
128257cb251SWill Deacon 		*(s32 *)place = sval;
1293fd00bebSArd Biesheuvel 		switch (op) {
1303fd00bebSArd Biesheuvel 		case RELOC_OP_ABS:
1313fd00bebSArd Biesheuvel 			if (sval < 0 || sval > U32_MAX)
1323fd00bebSArd Biesheuvel 				return -ERANGE;
1333fd00bebSArd Biesheuvel 			break;
1343fd00bebSArd Biesheuvel 		case RELOC_OP_PREL:
1351cf24a2cSArd Biesheuvel 			if (sval < S32_MIN || sval > S32_MAX)
136f9308969SArd Biesheuvel 				return -ERANGE;
137257cb251SWill Deacon 			break;
1383fd00bebSArd Biesheuvel 		default:
1393fd00bebSArd Biesheuvel 			pr_err("Invalid 32-bit data relocation (%d)\n", op);
1403fd00bebSArd Biesheuvel 			return 0;
1413fd00bebSArd Biesheuvel 		}
1423fd00bebSArd Biesheuvel 		break;
143257cb251SWill Deacon 	case 64:
144257cb251SWill Deacon 		*(s64 *)place = sval;
145257cb251SWill Deacon 		break;
146257cb251SWill Deacon 	default:
147257cb251SWill Deacon 		pr_err("Invalid length (%d) for data relocation\n", len);
148257cb251SWill Deacon 		return 0;
149257cb251SWill Deacon 	}
150257cb251SWill Deacon 	return 0;
151257cb251SWill Deacon }
152257cb251SWill Deacon 
153b24a5575SArd Biesheuvel enum aarch64_insn_movw_imm_type {
154b24a5575SArd Biesheuvel 	AARCH64_INSN_IMM_MOVNZ,
155b24a5575SArd Biesheuvel 	AARCH64_INSN_IMM_MOVKZ,
156b24a5575SArd Biesheuvel };
157b24a5575SArd Biesheuvel 
15802129ae5SLuc Van Oostenryck static int reloc_insn_movw(enum aarch64_reloc_op op, __le32 *place, u64 val,
159b24a5575SArd Biesheuvel 			   int lsb, enum aarch64_insn_movw_imm_type imm_type)
160257cb251SWill Deacon {
161b24a5575SArd Biesheuvel 	u64 imm;
162c84fced8SJiang Liu 	s64 sval;
16302129ae5SLuc Van Oostenryck 	u32 insn = le32_to_cpu(*place);
164257cb251SWill Deacon 
165c84fced8SJiang Liu 	sval = do_reloc(op, place, val);
166b24a5575SArd Biesheuvel 	imm = sval >> lsb;
167122e2fa0SWill Deacon 
168c84fced8SJiang Liu 	if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
169257cb251SWill Deacon 		/*
170257cb251SWill Deacon 		 * For signed MOVW relocations, we have to manipulate the
171257cb251SWill Deacon 		 * instruction encoding depending on whether or not the
172257cb251SWill Deacon 		 * immediate is less than zero.
173257cb251SWill Deacon 		 */
174257cb251SWill Deacon 		insn &= ~(3 << 29);
175b24a5575SArd Biesheuvel 		if (sval >= 0) {
176257cb251SWill Deacon 			/* >=0: Set the instruction to MOVZ (opcode 10b). */
177257cb251SWill Deacon 			insn |= 2 << 29;
178257cb251SWill Deacon 		} else {
179257cb251SWill Deacon 			/*
180257cb251SWill Deacon 			 * <0: Set the instruction to MOVN (opcode 00b).
181257cb251SWill Deacon 			 *     Since we've masked the opcode already, we
182257cb251SWill Deacon 			 *     don't need to do anything other than
183257cb251SWill Deacon 			 *     inverting the new immediate field.
184257cb251SWill Deacon 			 */
185257cb251SWill Deacon 			imm = ~imm;
186257cb251SWill Deacon 		}
187257cb251SWill Deacon 	}
188257cb251SWill Deacon 
189257cb251SWill Deacon 	/* Update the instruction with the new encoding. */
190b24a5575SArd Biesheuvel 	insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm);
19102129ae5SLuc Van Oostenryck 	*place = cpu_to_le32(insn);
192257cb251SWill Deacon 
193b24a5575SArd Biesheuvel 	if (imm > U16_MAX)
194257cb251SWill Deacon 		return -ERANGE;
195257cb251SWill Deacon 
196257cb251SWill Deacon 	return 0;
197257cb251SWill Deacon }
198257cb251SWill Deacon 
19902129ae5SLuc Van Oostenryck static int reloc_insn_imm(enum aarch64_reloc_op op, __le32 *place, u64 val,
200c84fced8SJiang Liu 			  int lsb, int len, enum aarch64_insn_imm_type imm_type)
201257cb251SWill Deacon {
202257cb251SWill Deacon 	u64 imm, imm_mask;
203257cb251SWill Deacon 	s64 sval;
20402129ae5SLuc Van Oostenryck 	u32 insn = le32_to_cpu(*place);
205257cb251SWill Deacon 
206257cb251SWill Deacon 	/* Calculate the relocation value. */
207257cb251SWill Deacon 	sval = do_reloc(op, place, val);
208257cb251SWill Deacon 	sval >>= lsb;
209257cb251SWill Deacon 
210257cb251SWill Deacon 	/* Extract the value bits and shift them to bit 0. */
211257cb251SWill Deacon 	imm_mask = (BIT(lsb + len) - 1) >> lsb;
212257cb251SWill Deacon 	imm = sval & imm_mask;
213257cb251SWill Deacon 
214257cb251SWill Deacon 	/* Update the instruction's immediate field. */
215c84fced8SJiang Liu 	insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
21602129ae5SLuc Van Oostenryck 	*place = cpu_to_le32(insn);
217257cb251SWill Deacon 
218257cb251SWill Deacon 	/*
219257cb251SWill Deacon 	 * Extract the upper value bits (including the sign bit) and
220257cb251SWill Deacon 	 * shift them to bit 0.
221257cb251SWill Deacon 	 */
222257cb251SWill Deacon 	sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
223257cb251SWill Deacon 
224257cb251SWill Deacon 	/*
225257cb251SWill Deacon 	 * Overflow has occurred if the upper bits are not all equal to
226257cb251SWill Deacon 	 * the sign bit of the value.
227257cb251SWill Deacon 	 */
228257cb251SWill Deacon 	if ((u64)(sval + 1) >= 2)
229257cb251SWill Deacon 		return -ERANGE;
230257cb251SWill Deacon 
231257cb251SWill Deacon 	return 0;
232257cb251SWill Deacon }
233257cb251SWill Deacon 
234c8ebf64eSJessica Yu static int reloc_insn_adrp(struct module *mod, Elf64_Shdr *sechdrs,
235c8ebf64eSJessica Yu 			   __le32 *place, u64 val)
236a257e025SArd Biesheuvel {
237a257e025SArd Biesheuvel 	u32 insn;
238a257e025SArd Biesheuvel 
239bdb85cd1SArd Biesheuvel 	if (!is_forbidden_offset_for_adrp(place))
240a257e025SArd Biesheuvel 		return reloc_insn_imm(RELOC_OP_PAGE, place, val, 12, 21,
241a257e025SArd Biesheuvel 				      AARCH64_INSN_IMM_ADR);
242a257e025SArd Biesheuvel 
243a257e025SArd Biesheuvel 	/* patch ADRP to ADR if it is in range */
244a257e025SArd Biesheuvel 	if (!reloc_insn_imm(RELOC_OP_PREL, place, val & ~0xfff, 0, 21,
245a257e025SArd Biesheuvel 			    AARCH64_INSN_IMM_ADR)) {
246a257e025SArd Biesheuvel 		insn = le32_to_cpu(*place);
247a257e025SArd Biesheuvel 		insn &= ~BIT(31);
248a257e025SArd Biesheuvel 	} else {
249a257e025SArd Biesheuvel 		/* out of range for ADR -> emit a veneer */
250c8ebf64eSJessica Yu 		val = module_emit_veneer_for_adrp(mod, sechdrs, place, val & ~0xfff);
251a257e025SArd Biesheuvel 		if (!val)
252a257e025SArd Biesheuvel 			return -ENOEXEC;
253a257e025SArd Biesheuvel 		insn = aarch64_insn_gen_branch_imm((u64)place, val,
254a257e025SArd Biesheuvel 						   AARCH64_INSN_BRANCH_NOLINK);
255a257e025SArd Biesheuvel 	}
256a257e025SArd Biesheuvel 
257a257e025SArd Biesheuvel 	*place = cpu_to_le32(insn);
258a257e025SArd Biesheuvel 	return 0;
259a257e025SArd Biesheuvel }
260a257e025SArd Biesheuvel 
261257cb251SWill Deacon int apply_relocate_add(Elf64_Shdr *sechdrs,
262257cb251SWill Deacon 		       const char *strtab,
263257cb251SWill Deacon 		       unsigned int symindex,
264257cb251SWill Deacon 		       unsigned int relsec,
265257cb251SWill Deacon 		       struct module *me)
266257cb251SWill Deacon {
267257cb251SWill Deacon 	unsigned int i;
268257cb251SWill Deacon 	int ovf;
269257cb251SWill Deacon 	bool overflow_check;
270257cb251SWill Deacon 	Elf64_Sym *sym;
271257cb251SWill Deacon 	void *loc;
272257cb251SWill Deacon 	u64 val;
273257cb251SWill Deacon 	Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
274257cb251SWill Deacon 
275257cb251SWill Deacon 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
276257cb251SWill Deacon 		/* loc corresponds to P in the AArch64 ELF document. */
277257cb251SWill Deacon 		loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
278257cb251SWill Deacon 			+ rel[i].r_offset;
279257cb251SWill Deacon 
280257cb251SWill Deacon 		/* sym is the ELF symbol we're referring to. */
281257cb251SWill Deacon 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
282257cb251SWill Deacon 			+ ELF64_R_SYM(rel[i].r_info);
283257cb251SWill Deacon 
284257cb251SWill Deacon 		/* val corresponds to (S + A) in the AArch64 ELF document. */
285257cb251SWill Deacon 		val = sym->st_value + rel[i].r_addend;
286257cb251SWill Deacon 
287257cb251SWill Deacon 		/* Check for overflow by default. */
288257cb251SWill Deacon 		overflow_check = true;
289257cb251SWill Deacon 
290257cb251SWill Deacon 		/* Perform the static relocation. */
291257cb251SWill Deacon 		switch (ELF64_R_TYPE(rel[i].r_info)) {
292257cb251SWill Deacon 		/* Null relocations. */
293257cb251SWill Deacon 		case R_ARM_NONE:
294257cb251SWill Deacon 		case R_AARCH64_NONE:
295257cb251SWill Deacon 			ovf = 0;
296257cb251SWill Deacon 			break;
297257cb251SWill Deacon 
298257cb251SWill Deacon 		/* Data relocations. */
299257cb251SWill Deacon 		case R_AARCH64_ABS64:
300257cb251SWill Deacon 			overflow_check = false;
301257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
302257cb251SWill Deacon 			break;
303257cb251SWill Deacon 		case R_AARCH64_ABS32:
304257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
305257cb251SWill Deacon 			break;
306257cb251SWill Deacon 		case R_AARCH64_ABS16:
307257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
308257cb251SWill Deacon 			break;
309257cb251SWill Deacon 		case R_AARCH64_PREL64:
310257cb251SWill Deacon 			overflow_check = false;
311257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
312257cb251SWill Deacon 			break;
313257cb251SWill Deacon 		case R_AARCH64_PREL32:
314257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
315257cb251SWill Deacon 			break;
316257cb251SWill Deacon 		case R_AARCH64_PREL16:
317257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
318257cb251SWill Deacon 			break;
319257cb251SWill Deacon 
320257cb251SWill Deacon 		/* MOVW instruction relocations. */
321257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0_NC:
322257cb251SWill Deacon 			overflow_check = false;
323df561f66SGustavo A. R. Silva 			fallthrough;
324257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0:
325257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
326b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
327257cb251SWill Deacon 			break;
328257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1_NC:
329257cb251SWill Deacon 			overflow_check = false;
330df561f66SGustavo A. R. Silva 			fallthrough;
331257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1:
332257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
333b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
334257cb251SWill Deacon 			break;
335257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2_NC:
336257cb251SWill Deacon 			overflow_check = false;
337df561f66SGustavo A. R. Silva 			fallthrough;
338257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2:
339257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
340b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
341257cb251SWill Deacon 			break;
342257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G3:
343257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
344257cb251SWill Deacon 			overflow_check = false;
345257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
346b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
347257cb251SWill Deacon 			break;
348257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G0:
349257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
350c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
351257cb251SWill Deacon 			break;
352257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G1:
353257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
354c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
355257cb251SWill Deacon 			break;
356257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G2:
357257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
358c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
359257cb251SWill Deacon 			break;
360257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0_NC:
361257cb251SWill Deacon 			overflow_check = false;
362257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
363b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
364257cb251SWill Deacon 			break;
365257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0:
366257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
367c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
368257cb251SWill Deacon 			break;
369257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1_NC:
370257cb251SWill Deacon 			overflow_check = false;
371257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
372b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
373257cb251SWill Deacon 			break;
374257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1:
375257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
376c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
377257cb251SWill Deacon 			break;
378257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2_NC:
379257cb251SWill Deacon 			overflow_check = false;
380257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
381b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
382257cb251SWill Deacon 			break;
383257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2:
384257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
385c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
386257cb251SWill Deacon 			break;
387257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G3:
388257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
389257cb251SWill Deacon 			overflow_check = false;
390257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48,
391c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
392257cb251SWill Deacon 			break;
393257cb251SWill Deacon 
394257cb251SWill Deacon 		/* Immediate instruction relocations. */
395257cb251SWill Deacon 		case R_AARCH64_LD_PREL_LO19:
396257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
397c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
398257cb251SWill Deacon 			break;
399257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_LO21:
400257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
401c84fced8SJiang Liu 					     AARCH64_INSN_IMM_ADR);
402257cb251SWill Deacon 			break;
403257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21_NC:
404257cb251SWill Deacon 			overflow_check = false;
405df561f66SGustavo A. R. Silva 			fallthrough;
406257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21:
407c8ebf64eSJessica Yu 			ovf = reloc_insn_adrp(me, sechdrs, loc, val);
408a257e025SArd Biesheuvel 			if (ovf && ovf != -ERANGE)
409a257e025SArd Biesheuvel 				return ovf;
410257cb251SWill Deacon 			break;
411257cb251SWill Deacon 		case R_AARCH64_ADD_ABS_LO12_NC:
412257cb251SWill Deacon 		case R_AARCH64_LDST8_ABS_LO12_NC:
413257cb251SWill Deacon 			overflow_check = false;
414257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
415c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
416257cb251SWill Deacon 			break;
417257cb251SWill Deacon 		case R_AARCH64_LDST16_ABS_LO12_NC:
418257cb251SWill Deacon 			overflow_check = false;
419257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
420c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
421257cb251SWill Deacon 			break;
422257cb251SWill Deacon 		case R_AARCH64_LDST32_ABS_LO12_NC:
423257cb251SWill Deacon 			overflow_check = false;
424257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
425c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
426257cb251SWill Deacon 			break;
427257cb251SWill Deacon 		case R_AARCH64_LDST64_ABS_LO12_NC:
428257cb251SWill Deacon 			overflow_check = false;
429257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
430c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
431257cb251SWill Deacon 			break;
432257cb251SWill Deacon 		case R_AARCH64_LDST128_ABS_LO12_NC:
433257cb251SWill Deacon 			overflow_check = false;
434257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
435c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
436257cb251SWill Deacon 			break;
437257cb251SWill Deacon 		case R_AARCH64_TSTBR14:
438257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
439c84fced8SJiang Liu 					     AARCH64_INSN_IMM_14);
440257cb251SWill Deacon 			break;
441257cb251SWill Deacon 		case R_AARCH64_CONDBR19:
442257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
443c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
444257cb251SWill Deacon 			break;
445257cb251SWill Deacon 		case R_AARCH64_JUMP26:
446257cb251SWill Deacon 		case R_AARCH64_CALL26:
447257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
448c84fced8SJiang Liu 					     AARCH64_INSN_IMM_26);
449fd045f6cSArd Biesheuvel 
450fd045f6cSArd Biesheuvel 			if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
451fd045f6cSArd Biesheuvel 			    ovf == -ERANGE) {
452c8ebf64eSJessica Yu 				val = module_emit_plt_entry(me, sechdrs, loc, &rel[i], sym);
4535e8307b9SArd Biesheuvel 				if (!val)
4545e8307b9SArd Biesheuvel 					return -ENOEXEC;
455fd045f6cSArd Biesheuvel 				ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2,
456fd045f6cSArd Biesheuvel 						     26, AARCH64_INSN_IMM_26);
457fd045f6cSArd Biesheuvel 			}
458257cb251SWill Deacon 			break;
459257cb251SWill Deacon 
460257cb251SWill Deacon 		default:
461257cb251SWill Deacon 			pr_err("module %s: unsupported RELA relocation: %llu\n",
462257cb251SWill Deacon 			       me->name, ELF64_R_TYPE(rel[i].r_info));
463257cb251SWill Deacon 			return -ENOEXEC;
464257cb251SWill Deacon 		}
465257cb251SWill Deacon 
466257cb251SWill Deacon 		if (overflow_check && ovf == -ERANGE)
467257cb251SWill Deacon 			goto overflow;
468257cb251SWill Deacon 
469257cb251SWill Deacon 	}
470257cb251SWill Deacon 
471257cb251SWill Deacon 	return 0;
472257cb251SWill Deacon 
473257cb251SWill Deacon overflow:
474257cb251SWill Deacon 	pr_err("module %s: overflow in relocation type %d val %Lx\n",
475257cb251SWill Deacon 	       me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
476257cb251SWill Deacon 	return -ENOEXEC;
477257cb251SWill Deacon }
478932ded4bSAndre Przywara 
479bd8b21d3SMark Rutland static const Elf_Shdr *find_section(const Elf_Ehdr *hdr,
480932ded4bSAndre Przywara 				    const Elf_Shdr *sechdrs,
481bd8b21d3SMark Rutland 				    const char *name)
482932ded4bSAndre Przywara {
483932ded4bSAndre Przywara 	const Elf_Shdr *s, *se;
484932ded4bSAndre Przywara 	const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
485932ded4bSAndre Przywara 
486932ded4bSAndre Przywara 	for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
487bd8b21d3SMark Rutland 		if (strcmp(name, secstrs + s->sh_name) == 0)
488bd8b21d3SMark Rutland 			return s;
489932ded4bSAndre Przywara 	}
490932ded4bSAndre Przywara 
491bd8b21d3SMark Rutland 	return NULL;
492bd8b21d3SMark Rutland }
493bd8b21d3SMark Rutland 
4943b23e499STorsten Duwe static inline void __init_plt(struct plt_entry *plt, unsigned long addr)
4953b23e499STorsten Duwe {
4963b23e499STorsten Duwe 	*plt = get_plt_entry(addr, plt);
4973b23e499STorsten Duwe }
4983b23e499STorsten Duwe 
499f1a54ae9SMark Rutland static int module_init_ftrace_plt(const Elf_Ehdr *hdr,
500f1a54ae9SMark Rutland 				  const Elf_Shdr *sechdrs,
501f1a54ae9SMark Rutland 				  struct module *mod)
502f1a54ae9SMark Rutland {
503f1a54ae9SMark Rutland #if defined(CONFIG_ARM64_MODULE_PLTS) && defined(CONFIG_DYNAMIC_FTRACE)
504f1a54ae9SMark Rutland 	const Elf_Shdr *s;
5053b23e499STorsten Duwe 	struct plt_entry *plts;
506f1a54ae9SMark Rutland 
507f1a54ae9SMark Rutland 	s = find_section(hdr, sechdrs, ".text.ftrace_trampoline");
508f1a54ae9SMark Rutland 	if (!s)
509f1a54ae9SMark Rutland 		return -ENOEXEC;
510f1a54ae9SMark Rutland 
5113b23e499STorsten Duwe 	plts = (void *)s->sh_addr;
5123b23e499STorsten Duwe 
5133b23e499STorsten Duwe 	__init_plt(&plts[FTRACE_PLT_IDX], FTRACE_ADDR);
5143b23e499STorsten Duwe 
5153b23e499STorsten Duwe 	if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
5163b23e499STorsten Duwe 		__init_plt(&plts[FTRACE_REGS_PLT_IDX], FTRACE_REGS_ADDR);
5173b23e499STorsten Duwe 
5183b23e499STorsten Duwe 	mod->arch.ftrace_trampolines = plts;
519f1a54ae9SMark Rutland #endif
520f1a54ae9SMark Rutland 	return 0;
521f1a54ae9SMark Rutland }
522f1a54ae9SMark Rutland 
523bd8b21d3SMark Rutland int module_finalize(const Elf_Ehdr *hdr,
524bd8b21d3SMark Rutland 		    const Elf_Shdr *sechdrs,
525bd8b21d3SMark Rutland 		    struct module *me)
526bd8b21d3SMark Rutland {
527bd8b21d3SMark Rutland 	const Elf_Shdr *s;
528bd8b21d3SMark Rutland 	s = find_section(hdr, sechdrs, ".altinstructions");
529bd8b21d3SMark Rutland 	if (s)
530bd8b21d3SMark Rutland 		apply_alternatives_module((void *)s->sh_addr, s->sh_size);
531bd8b21d3SMark Rutland 
532f1a54ae9SMark Rutland 	return module_init_ftrace_plt(hdr, sechdrs, me);
533932ded4bSAndre Przywara }
534