xref: /openbmc/linux/arch/arm64/kernel/module.c (revision 3b619e22c4601b444ed2d6a5458271f72625ac89)
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>
18*3b619e22SArd Biesheuvel #include <linux/scs.h>
19257cb251SWill Deacon #include <linux/vmalloc.h>
202c2b282dSPaul Walmsley #include <asm/alternative.h>
21c84fced8SJiang Liu #include <asm/insn.h>
22*3b619e22SArd Biesheuvel #include <asm/scs.h>
23932ded4bSAndre Przywara #include <asm/sections.h>
24c84fced8SJiang Liu 
25257cb251SWill Deacon void *module_alloc(unsigned long size)
26257cb251SWill Deacon {
276f496a55SArd Biesheuvel 	u64 module_alloc_end = module_alloc_base + MODULES_VSIZE;
280c2cf6d9SFlorian Fainelli 	gfp_t gfp_mask = GFP_KERNEL;
2939d114ddSAndrey Ryabinin 	void *p;
3039d114ddSAndrey Ryabinin 
310c2cf6d9SFlorian Fainelli 	/* Silence the initial allocation */
320c2cf6d9SFlorian Fainelli 	if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
330c2cf6d9SFlorian Fainelli 		gfp_mask |= __GFP_NOWARN;
340c2cf6d9SFlorian Fainelli 
350fea6e9aSAndrey Konovalov 	if (IS_ENABLED(CONFIG_KASAN_GENERIC) ||
360fea6e9aSAndrey Konovalov 	    IS_ENABLED(CONFIG_KASAN_SW_TAGS))
376f496a55SArd Biesheuvel 		/* don't exceed the static module region - see below */
386f496a55SArd Biesheuvel 		module_alloc_end = MODULES_END;
396f496a55SArd Biesheuvel 
40f80fb3a3SArd Biesheuvel 	p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base,
4160115fa5SKefeng Wang 				module_alloc_end, gfp_mask, PAGE_KERNEL, VM_DEFER_KMEMLEAK,
42cb9e3c29SAndrey Ryabinin 				NUMA_NO_NODE, __builtin_return_address(0));
4339d114ddSAndrey Ryabinin 
44fd045f6cSArd Biesheuvel 	if (!p && IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
4531d02e7aSLecopzer Chen 	    (IS_ENABLED(CONFIG_KASAN_VMALLOC) ||
4631d02e7aSLecopzer Chen 	     (!IS_ENABLED(CONFIG_KASAN_GENERIC) &&
4731d02e7aSLecopzer Chen 	      !IS_ENABLED(CONFIG_KASAN_SW_TAGS))))
48fd045f6cSArd Biesheuvel 		/*
4931d02e7aSLecopzer Chen 		 * KASAN without KASAN_VMALLOC can only deal with module
5031d02e7aSLecopzer Chen 		 * allocations being served from the reserved module region,
5131d02e7aSLecopzer Chen 		 * since the remainder of the vmalloc region is already
5231d02e7aSLecopzer Chen 		 * backed by zero shadow pages, and punching holes into it
5331d02e7aSLecopzer Chen 		 * is non-trivial. Since the module region is not randomized
5431d02e7aSLecopzer Chen 		 * when KASAN is enabled without KASAN_VMALLOC, it is even
55fd045f6cSArd Biesheuvel 		 * less likely that the module region gets exhausted, so we
56fd045f6cSArd Biesheuvel 		 * can simply omit this fallback in that case.
57fd045f6cSArd Biesheuvel 		 */
58f2b9ba87SArd Biesheuvel 		p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base,
59b2eed9b5SArd Biesheuvel 				module_alloc_base + SZ_2G, GFP_KERNEL,
607dfac3c5SArd Biesheuvel 				PAGE_KERNEL, 0, NUMA_NO_NODE,
61f2b9ba87SArd Biesheuvel 				__builtin_return_address(0));
62fd045f6cSArd Biesheuvel 
6363840de2SAndrey Konovalov 	if (p && (kasan_alloc_module_shadow(p, size, gfp_mask) < 0)) {
6439d114ddSAndrey Ryabinin 		vfree(p);
6539d114ddSAndrey Ryabinin 		return NULL;
6639d114ddSAndrey Ryabinin 	}
6739d114ddSAndrey Ryabinin 
6836c4a73bSAndrey Konovalov 	/* Memory is intended to be executable, reset the pointer tag. */
6936c4a73bSAndrey Konovalov 	return kasan_reset_tag(p);
70257cb251SWill Deacon }
71257cb251SWill Deacon 
72257cb251SWill Deacon enum aarch64_reloc_op {
73257cb251SWill Deacon 	RELOC_OP_NONE,
74257cb251SWill Deacon 	RELOC_OP_ABS,
75257cb251SWill Deacon 	RELOC_OP_PREL,
76257cb251SWill Deacon 	RELOC_OP_PAGE,
77257cb251SWill Deacon };
78257cb251SWill Deacon 
7902129ae5SLuc Van Oostenryck static u64 do_reloc(enum aarch64_reloc_op reloc_op, __le32 *place, u64 val)
80257cb251SWill Deacon {
81257cb251SWill Deacon 	switch (reloc_op) {
82257cb251SWill Deacon 	case RELOC_OP_ABS:
83257cb251SWill Deacon 		return val;
84257cb251SWill Deacon 	case RELOC_OP_PREL:
85257cb251SWill Deacon 		return val - (u64)place;
86257cb251SWill Deacon 	case RELOC_OP_PAGE:
87257cb251SWill Deacon 		return (val & ~0xfff) - ((u64)place & ~0xfff);
88257cb251SWill Deacon 	case RELOC_OP_NONE:
89257cb251SWill Deacon 		return 0;
90257cb251SWill Deacon 	}
91257cb251SWill Deacon 
92257cb251SWill Deacon 	pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
93257cb251SWill Deacon 	return 0;
94257cb251SWill Deacon }
95257cb251SWill Deacon 
96257cb251SWill Deacon static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
97257cb251SWill Deacon {
98257cb251SWill Deacon 	s64 sval = do_reloc(op, place, val);
99257cb251SWill Deacon 
1001cf24a2cSArd Biesheuvel 	/*
1011cf24a2cSArd Biesheuvel 	 * The ELF psABI for AArch64 documents the 16-bit and 32-bit place
1023fd00bebSArd Biesheuvel 	 * relative and absolute relocations as having a range of [-2^15, 2^16)
1033fd00bebSArd Biesheuvel 	 * or [-2^31, 2^32), respectively. However, in order to be able to
1043fd00bebSArd Biesheuvel 	 * detect overflows reliably, we have to choose whether we interpret
1053fd00bebSArd Biesheuvel 	 * such quantities as signed or as unsigned, and stick with it.
1061cf24a2cSArd Biesheuvel 	 * The way we organize our address space requires a signed
1071cf24a2cSArd Biesheuvel 	 * interpretation of 32-bit relative references, so let's use that
1081cf24a2cSArd Biesheuvel 	 * for all R_AARCH64_PRELxx relocations. This means our upper
1091cf24a2cSArd Biesheuvel 	 * bound for overflow detection should be Sxx_MAX rather than Uxx_MAX.
1101cf24a2cSArd Biesheuvel 	 */
1111cf24a2cSArd Biesheuvel 
112257cb251SWill Deacon 	switch (len) {
113257cb251SWill Deacon 	case 16:
114257cb251SWill Deacon 		*(s16 *)place = sval;
1153fd00bebSArd Biesheuvel 		switch (op) {
1163fd00bebSArd Biesheuvel 		case RELOC_OP_ABS:
1173fd00bebSArd Biesheuvel 			if (sval < 0 || sval > U16_MAX)
1183fd00bebSArd Biesheuvel 				return -ERANGE;
1193fd00bebSArd Biesheuvel 			break;
1203fd00bebSArd Biesheuvel 		case RELOC_OP_PREL:
1211cf24a2cSArd Biesheuvel 			if (sval < S16_MIN || sval > S16_MAX)
122f9308969SArd Biesheuvel 				return -ERANGE;
123257cb251SWill Deacon 			break;
1243fd00bebSArd Biesheuvel 		default:
1253fd00bebSArd Biesheuvel 			pr_err("Invalid 16-bit data relocation (%d)\n", op);
1263fd00bebSArd Biesheuvel 			return 0;
1273fd00bebSArd Biesheuvel 		}
1283fd00bebSArd Biesheuvel 		break;
129257cb251SWill Deacon 	case 32:
130257cb251SWill Deacon 		*(s32 *)place = sval;
1313fd00bebSArd Biesheuvel 		switch (op) {
1323fd00bebSArd Biesheuvel 		case RELOC_OP_ABS:
1333fd00bebSArd Biesheuvel 			if (sval < 0 || sval > U32_MAX)
1343fd00bebSArd Biesheuvel 				return -ERANGE;
1353fd00bebSArd Biesheuvel 			break;
1363fd00bebSArd Biesheuvel 		case RELOC_OP_PREL:
1371cf24a2cSArd Biesheuvel 			if (sval < S32_MIN || sval > S32_MAX)
138f9308969SArd Biesheuvel 				return -ERANGE;
139257cb251SWill Deacon 			break;
1403fd00bebSArd Biesheuvel 		default:
1413fd00bebSArd Biesheuvel 			pr_err("Invalid 32-bit data relocation (%d)\n", op);
1423fd00bebSArd Biesheuvel 			return 0;
1433fd00bebSArd Biesheuvel 		}
1443fd00bebSArd Biesheuvel 		break;
145257cb251SWill Deacon 	case 64:
146257cb251SWill Deacon 		*(s64 *)place = sval;
147257cb251SWill Deacon 		break;
148257cb251SWill Deacon 	default:
149257cb251SWill Deacon 		pr_err("Invalid length (%d) for data relocation\n", len);
150257cb251SWill Deacon 		return 0;
151257cb251SWill Deacon 	}
152257cb251SWill Deacon 	return 0;
153257cb251SWill Deacon }
154257cb251SWill Deacon 
155b24a5575SArd Biesheuvel enum aarch64_insn_movw_imm_type {
156b24a5575SArd Biesheuvel 	AARCH64_INSN_IMM_MOVNZ,
157b24a5575SArd Biesheuvel 	AARCH64_INSN_IMM_MOVKZ,
158b24a5575SArd Biesheuvel };
159b24a5575SArd Biesheuvel 
16002129ae5SLuc Van Oostenryck static int reloc_insn_movw(enum aarch64_reloc_op op, __le32 *place, u64 val,
161b24a5575SArd Biesheuvel 			   int lsb, enum aarch64_insn_movw_imm_type imm_type)
162257cb251SWill Deacon {
163b24a5575SArd Biesheuvel 	u64 imm;
164c84fced8SJiang Liu 	s64 sval;
16502129ae5SLuc Van Oostenryck 	u32 insn = le32_to_cpu(*place);
166257cb251SWill Deacon 
167c84fced8SJiang Liu 	sval = do_reloc(op, place, val);
168b24a5575SArd Biesheuvel 	imm = sval >> lsb;
169122e2fa0SWill Deacon 
170c84fced8SJiang Liu 	if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
171257cb251SWill Deacon 		/*
172257cb251SWill Deacon 		 * For signed MOVW relocations, we have to manipulate the
173257cb251SWill Deacon 		 * instruction encoding depending on whether or not the
174257cb251SWill Deacon 		 * immediate is less than zero.
175257cb251SWill Deacon 		 */
176257cb251SWill Deacon 		insn &= ~(3 << 29);
177b24a5575SArd Biesheuvel 		if (sval >= 0) {
178257cb251SWill Deacon 			/* >=0: Set the instruction to MOVZ (opcode 10b). */
179257cb251SWill Deacon 			insn |= 2 << 29;
180257cb251SWill Deacon 		} else {
181257cb251SWill Deacon 			/*
182257cb251SWill Deacon 			 * <0: Set the instruction to MOVN (opcode 00b).
183257cb251SWill Deacon 			 *     Since we've masked the opcode already, we
184257cb251SWill Deacon 			 *     don't need to do anything other than
185257cb251SWill Deacon 			 *     inverting the new immediate field.
186257cb251SWill Deacon 			 */
187257cb251SWill Deacon 			imm = ~imm;
188257cb251SWill Deacon 		}
189257cb251SWill Deacon 	}
190257cb251SWill Deacon 
191257cb251SWill Deacon 	/* Update the instruction with the new encoding. */
192b24a5575SArd Biesheuvel 	insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm);
19302129ae5SLuc Van Oostenryck 	*place = cpu_to_le32(insn);
194257cb251SWill Deacon 
195b24a5575SArd Biesheuvel 	if (imm > U16_MAX)
196257cb251SWill Deacon 		return -ERANGE;
197257cb251SWill Deacon 
198257cb251SWill Deacon 	return 0;
199257cb251SWill Deacon }
200257cb251SWill Deacon 
20102129ae5SLuc Van Oostenryck static int reloc_insn_imm(enum aarch64_reloc_op op, __le32 *place, u64 val,
202c84fced8SJiang Liu 			  int lsb, int len, enum aarch64_insn_imm_type imm_type)
203257cb251SWill Deacon {
204257cb251SWill Deacon 	u64 imm, imm_mask;
205257cb251SWill Deacon 	s64 sval;
20602129ae5SLuc Van Oostenryck 	u32 insn = le32_to_cpu(*place);
207257cb251SWill Deacon 
208257cb251SWill Deacon 	/* Calculate the relocation value. */
209257cb251SWill Deacon 	sval = do_reloc(op, place, val);
210257cb251SWill Deacon 	sval >>= lsb;
211257cb251SWill Deacon 
212257cb251SWill Deacon 	/* Extract the value bits and shift them to bit 0. */
213257cb251SWill Deacon 	imm_mask = (BIT(lsb + len) - 1) >> lsb;
214257cb251SWill Deacon 	imm = sval & imm_mask;
215257cb251SWill Deacon 
216257cb251SWill Deacon 	/* Update the instruction's immediate field. */
217c84fced8SJiang Liu 	insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
21802129ae5SLuc Van Oostenryck 	*place = cpu_to_le32(insn);
219257cb251SWill Deacon 
220257cb251SWill Deacon 	/*
221257cb251SWill Deacon 	 * Extract the upper value bits (including the sign bit) and
222257cb251SWill Deacon 	 * shift them to bit 0.
223257cb251SWill Deacon 	 */
224257cb251SWill Deacon 	sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
225257cb251SWill Deacon 
226257cb251SWill Deacon 	/*
227257cb251SWill Deacon 	 * Overflow has occurred if the upper bits are not all equal to
228257cb251SWill Deacon 	 * the sign bit of the value.
229257cb251SWill Deacon 	 */
230257cb251SWill Deacon 	if ((u64)(sval + 1) >= 2)
231257cb251SWill Deacon 		return -ERANGE;
232257cb251SWill Deacon 
233257cb251SWill Deacon 	return 0;
234257cb251SWill Deacon }
235257cb251SWill Deacon 
236c8ebf64eSJessica Yu static int reloc_insn_adrp(struct module *mod, Elf64_Shdr *sechdrs,
237c8ebf64eSJessica Yu 			   __le32 *place, u64 val)
238a257e025SArd Biesheuvel {
239a257e025SArd Biesheuvel 	u32 insn;
240a257e025SArd Biesheuvel 
241bdb85cd1SArd Biesheuvel 	if (!is_forbidden_offset_for_adrp(place))
242a257e025SArd Biesheuvel 		return reloc_insn_imm(RELOC_OP_PAGE, place, val, 12, 21,
243a257e025SArd Biesheuvel 				      AARCH64_INSN_IMM_ADR);
244a257e025SArd Biesheuvel 
245a257e025SArd Biesheuvel 	/* patch ADRP to ADR if it is in range */
246a257e025SArd Biesheuvel 	if (!reloc_insn_imm(RELOC_OP_PREL, place, val & ~0xfff, 0, 21,
247a257e025SArd Biesheuvel 			    AARCH64_INSN_IMM_ADR)) {
248a257e025SArd Biesheuvel 		insn = le32_to_cpu(*place);
249a257e025SArd Biesheuvel 		insn &= ~BIT(31);
250a257e025SArd Biesheuvel 	} else {
251a257e025SArd Biesheuvel 		/* out of range for ADR -> emit a veneer */
252c8ebf64eSJessica Yu 		val = module_emit_veneer_for_adrp(mod, sechdrs, place, val & ~0xfff);
253a257e025SArd Biesheuvel 		if (!val)
254a257e025SArd Biesheuvel 			return -ENOEXEC;
255a257e025SArd Biesheuvel 		insn = aarch64_insn_gen_branch_imm((u64)place, val,
256a257e025SArd Biesheuvel 						   AARCH64_INSN_BRANCH_NOLINK);
257a257e025SArd Biesheuvel 	}
258a257e025SArd Biesheuvel 
259a257e025SArd Biesheuvel 	*place = cpu_to_le32(insn);
260a257e025SArd Biesheuvel 	return 0;
261a257e025SArd Biesheuvel }
262a257e025SArd Biesheuvel 
263257cb251SWill Deacon int apply_relocate_add(Elf64_Shdr *sechdrs,
264257cb251SWill Deacon 		       const char *strtab,
265257cb251SWill Deacon 		       unsigned int symindex,
266257cb251SWill Deacon 		       unsigned int relsec,
267257cb251SWill Deacon 		       struct module *me)
268257cb251SWill Deacon {
269257cb251SWill Deacon 	unsigned int i;
270257cb251SWill Deacon 	int ovf;
271257cb251SWill Deacon 	bool overflow_check;
272257cb251SWill Deacon 	Elf64_Sym *sym;
273257cb251SWill Deacon 	void *loc;
274257cb251SWill Deacon 	u64 val;
275257cb251SWill Deacon 	Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
276257cb251SWill Deacon 
277257cb251SWill Deacon 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
278257cb251SWill Deacon 		/* loc corresponds to P in the AArch64 ELF document. */
279257cb251SWill Deacon 		loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
280257cb251SWill Deacon 			+ rel[i].r_offset;
281257cb251SWill Deacon 
282257cb251SWill Deacon 		/* sym is the ELF symbol we're referring to. */
283257cb251SWill Deacon 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
284257cb251SWill Deacon 			+ ELF64_R_SYM(rel[i].r_info);
285257cb251SWill Deacon 
286257cb251SWill Deacon 		/* val corresponds to (S + A) in the AArch64 ELF document. */
287257cb251SWill Deacon 		val = sym->st_value + rel[i].r_addend;
288257cb251SWill Deacon 
289257cb251SWill Deacon 		/* Check for overflow by default. */
290257cb251SWill Deacon 		overflow_check = true;
291257cb251SWill Deacon 
292257cb251SWill Deacon 		/* Perform the static relocation. */
293257cb251SWill Deacon 		switch (ELF64_R_TYPE(rel[i].r_info)) {
294257cb251SWill Deacon 		/* Null relocations. */
295257cb251SWill Deacon 		case R_ARM_NONE:
296257cb251SWill Deacon 		case R_AARCH64_NONE:
297257cb251SWill Deacon 			ovf = 0;
298257cb251SWill Deacon 			break;
299257cb251SWill Deacon 
300257cb251SWill Deacon 		/* Data relocations. */
301257cb251SWill Deacon 		case R_AARCH64_ABS64:
302257cb251SWill Deacon 			overflow_check = false;
303257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
304257cb251SWill Deacon 			break;
305257cb251SWill Deacon 		case R_AARCH64_ABS32:
306257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
307257cb251SWill Deacon 			break;
308257cb251SWill Deacon 		case R_AARCH64_ABS16:
309257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
310257cb251SWill Deacon 			break;
311257cb251SWill Deacon 		case R_AARCH64_PREL64:
312257cb251SWill Deacon 			overflow_check = false;
313257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
314257cb251SWill Deacon 			break;
315257cb251SWill Deacon 		case R_AARCH64_PREL32:
316257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
317257cb251SWill Deacon 			break;
318257cb251SWill Deacon 		case R_AARCH64_PREL16:
319257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
320257cb251SWill Deacon 			break;
321257cb251SWill Deacon 
322257cb251SWill Deacon 		/* MOVW instruction relocations. */
323257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0_NC:
324257cb251SWill Deacon 			overflow_check = false;
325df561f66SGustavo A. R. Silva 			fallthrough;
326257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0:
327257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
328b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
329257cb251SWill Deacon 			break;
330257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1_NC:
331257cb251SWill Deacon 			overflow_check = false;
332df561f66SGustavo A. R. Silva 			fallthrough;
333257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1:
334257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
335b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
336257cb251SWill Deacon 			break;
337257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2_NC:
338257cb251SWill Deacon 			overflow_check = false;
339df561f66SGustavo A. R. Silva 			fallthrough;
340257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2:
341257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
342b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
343257cb251SWill Deacon 			break;
344257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G3:
345257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
346257cb251SWill Deacon 			overflow_check = false;
347257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
348b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
349257cb251SWill Deacon 			break;
350257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G0:
351257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
352c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
353257cb251SWill Deacon 			break;
354257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G1:
355257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
356c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
357257cb251SWill Deacon 			break;
358257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G2:
359257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
360c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
361257cb251SWill Deacon 			break;
362257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0_NC:
363257cb251SWill Deacon 			overflow_check = false;
364257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
365b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
366257cb251SWill Deacon 			break;
367257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0:
368257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
369c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
370257cb251SWill Deacon 			break;
371257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1_NC:
372257cb251SWill Deacon 			overflow_check = false;
373257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
374b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
375257cb251SWill Deacon 			break;
376257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1:
377257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
378c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
379257cb251SWill Deacon 			break;
380257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2_NC:
381257cb251SWill Deacon 			overflow_check = false;
382257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
383b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
384257cb251SWill Deacon 			break;
385257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2:
386257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
387c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
388257cb251SWill Deacon 			break;
389257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G3:
390257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
391257cb251SWill Deacon 			overflow_check = false;
392257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48,
393c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
394257cb251SWill Deacon 			break;
395257cb251SWill Deacon 
396257cb251SWill Deacon 		/* Immediate instruction relocations. */
397257cb251SWill Deacon 		case R_AARCH64_LD_PREL_LO19:
398257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
399c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
400257cb251SWill Deacon 			break;
401257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_LO21:
402257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
403c84fced8SJiang Liu 					     AARCH64_INSN_IMM_ADR);
404257cb251SWill Deacon 			break;
405257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21_NC:
406257cb251SWill Deacon 			overflow_check = false;
407df561f66SGustavo A. R. Silva 			fallthrough;
408257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21:
409c8ebf64eSJessica Yu 			ovf = reloc_insn_adrp(me, sechdrs, loc, val);
410a257e025SArd Biesheuvel 			if (ovf && ovf != -ERANGE)
411a257e025SArd Biesheuvel 				return ovf;
412257cb251SWill Deacon 			break;
413257cb251SWill Deacon 		case R_AARCH64_ADD_ABS_LO12_NC:
414257cb251SWill Deacon 		case R_AARCH64_LDST8_ABS_LO12_NC:
415257cb251SWill Deacon 			overflow_check = false;
416257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
417c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
418257cb251SWill Deacon 			break;
419257cb251SWill Deacon 		case R_AARCH64_LDST16_ABS_LO12_NC:
420257cb251SWill Deacon 			overflow_check = false;
421257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
422c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
423257cb251SWill Deacon 			break;
424257cb251SWill Deacon 		case R_AARCH64_LDST32_ABS_LO12_NC:
425257cb251SWill Deacon 			overflow_check = false;
426257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
427c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
428257cb251SWill Deacon 			break;
429257cb251SWill Deacon 		case R_AARCH64_LDST64_ABS_LO12_NC:
430257cb251SWill Deacon 			overflow_check = false;
431257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
432c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
433257cb251SWill Deacon 			break;
434257cb251SWill Deacon 		case R_AARCH64_LDST128_ABS_LO12_NC:
435257cb251SWill Deacon 			overflow_check = false;
436257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
437c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
438257cb251SWill Deacon 			break;
439257cb251SWill Deacon 		case R_AARCH64_TSTBR14:
440257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
441c84fced8SJiang Liu 					     AARCH64_INSN_IMM_14);
442257cb251SWill Deacon 			break;
443257cb251SWill Deacon 		case R_AARCH64_CONDBR19:
444257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
445c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
446257cb251SWill Deacon 			break;
447257cb251SWill Deacon 		case R_AARCH64_JUMP26:
448257cb251SWill Deacon 		case R_AARCH64_CALL26:
449257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
450c84fced8SJiang Liu 					     AARCH64_INSN_IMM_26);
451fd045f6cSArd Biesheuvel 
452fd045f6cSArd Biesheuvel 			if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
453fd045f6cSArd Biesheuvel 			    ovf == -ERANGE) {
454c8ebf64eSJessica Yu 				val = module_emit_plt_entry(me, sechdrs, loc, &rel[i], sym);
4555e8307b9SArd Biesheuvel 				if (!val)
4565e8307b9SArd Biesheuvel 					return -ENOEXEC;
457fd045f6cSArd Biesheuvel 				ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2,
458fd045f6cSArd Biesheuvel 						     26, AARCH64_INSN_IMM_26);
459fd045f6cSArd Biesheuvel 			}
460257cb251SWill Deacon 			break;
461257cb251SWill Deacon 
462257cb251SWill Deacon 		default:
463257cb251SWill Deacon 			pr_err("module %s: unsupported RELA relocation: %llu\n",
464257cb251SWill Deacon 			       me->name, ELF64_R_TYPE(rel[i].r_info));
465257cb251SWill Deacon 			return -ENOEXEC;
466257cb251SWill Deacon 		}
467257cb251SWill Deacon 
468257cb251SWill Deacon 		if (overflow_check && ovf == -ERANGE)
469257cb251SWill Deacon 			goto overflow;
470257cb251SWill Deacon 
471257cb251SWill Deacon 	}
472257cb251SWill Deacon 
473257cb251SWill Deacon 	return 0;
474257cb251SWill Deacon 
475257cb251SWill Deacon overflow:
476257cb251SWill Deacon 	pr_err("module %s: overflow in relocation type %d val %Lx\n",
477257cb251SWill Deacon 	       me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
478257cb251SWill Deacon 	return -ENOEXEC;
479257cb251SWill Deacon }
480932ded4bSAndre Przywara 
4813b23e499STorsten Duwe static inline void __init_plt(struct plt_entry *plt, unsigned long addr)
4823b23e499STorsten Duwe {
4833b23e499STorsten Duwe 	*plt = get_plt_entry(addr, plt);
4843b23e499STorsten Duwe }
4853b23e499STorsten Duwe 
486f1a54ae9SMark Rutland static int module_init_ftrace_plt(const Elf_Ehdr *hdr,
487f1a54ae9SMark Rutland 				  const Elf_Shdr *sechdrs,
488f1a54ae9SMark Rutland 				  struct module *mod)
489f1a54ae9SMark Rutland {
490f1a54ae9SMark Rutland #if defined(CONFIG_ARM64_MODULE_PLTS) && defined(CONFIG_DYNAMIC_FTRACE)
491f1a54ae9SMark Rutland 	const Elf_Shdr *s;
4923b23e499STorsten Duwe 	struct plt_entry *plts;
493f1a54ae9SMark Rutland 
494f1a54ae9SMark Rutland 	s = find_section(hdr, sechdrs, ".text.ftrace_trampoline");
495f1a54ae9SMark Rutland 	if (!s)
496f1a54ae9SMark Rutland 		return -ENOEXEC;
497f1a54ae9SMark Rutland 
4983b23e499STorsten Duwe 	plts = (void *)s->sh_addr;
4993b23e499STorsten Duwe 
5003b23e499STorsten Duwe 	__init_plt(&plts[FTRACE_PLT_IDX], FTRACE_ADDR);
5013b23e499STorsten Duwe 
5023b23e499STorsten Duwe 	if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
5033b23e499STorsten Duwe 		__init_plt(&plts[FTRACE_REGS_PLT_IDX], FTRACE_REGS_ADDR);
5043b23e499STorsten Duwe 
5053b23e499STorsten Duwe 	mod->arch.ftrace_trampolines = plts;
506f1a54ae9SMark Rutland #endif
507f1a54ae9SMark Rutland 	return 0;
508f1a54ae9SMark Rutland }
509f1a54ae9SMark Rutland 
510bd8b21d3SMark Rutland int module_finalize(const Elf_Ehdr *hdr,
511bd8b21d3SMark Rutland 		    const Elf_Shdr *sechdrs,
512bd8b21d3SMark Rutland 		    struct module *me)
513bd8b21d3SMark Rutland {
514bd8b21d3SMark Rutland 	const Elf_Shdr *s;
515bd8b21d3SMark Rutland 	s = find_section(hdr, sechdrs, ".altinstructions");
516bd8b21d3SMark Rutland 	if (s)
517bd8b21d3SMark Rutland 		apply_alternatives_module((void *)s->sh_addr, s->sh_size);
518bd8b21d3SMark Rutland 
519*3b619e22SArd Biesheuvel 	if (scs_is_dynamic()) {
520*3b619e22SArd Biesheuvel 		s = find_section(hdr, sechdrs, ".init.eh_frame");
521*3b619e22SArd Biesheuvel 		if (s)
522*3b619e22SArd Biesheuvel 			scs_patch((void *)s->sh_addr, s->sh_size);
523*3b619e22SArd Biesheuvel 	}
524*3b619e22SArd Biesheuvel 
525f1a54ae9SMark Rutland 	return module_init_ftrace_plt(hdr, sechdrs, me);
526932ded4bSAndre Przywara }
527