xref: /linux-6.15/arch/arm64/kernel/module.c (revision ccf54058)
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 <[email protected]>
8257cb251SWill Deacon  */
9257cb251SWill Deacon 
103e35d303SMark Rutland #define pr_fmt(fmt) "Modules: " fmt
113e35d303SMark Rutland 
12257cb251SWill Deacon #include <linux/bitops.h>
13257cb251SWill Deacon #include <linux/elf.h>
14f1a54ae9SMark Rutland #include <linux/ftrace.h>
1539d114ddSAndrey Ryabinin #include <linux/kasan.h>
16257cb251SWill Deacon #include <linux/kernel.h>
17257cb251SWill Deacon #include <linux/mm.h>
18257cb251SWill Deacon #include <linux/moduleloader.h>
19e46b7103SMark Rutland #include <linux/random.h>
203b619e22SArd Biesheuvel #include <linux/scs.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 
27257cb251SWill Deacon enum aarch64_reloc_op {
28257cb251SWill Deacon 	RELOC_OP_NONE,
29257cb251SWill Deacon 	RELOC_OP_ABS,
30257cb251SWill Deacon 	RELOC_OP_PREL,
31257cb251SWill Deacon 	RELOC_OP_PAGE,
32257cb251SWill Deacon };
33257cb251SWill Deacon 
do_reloc(enum aarch64_reloc_op reloc_op,__le32 * place,u64 val)3402129ae5SLuc Van Oostenryck static u64 do_reloc(enum aarch64_reloc_op reloc_op, __le32 *place, u64 val)
35257cb251SWill Deacon {
36257cb251SWill Deacon 	switch (reloc_op) {
37257cb251SWill Deacon 	case RELOC_OP_ABS:
38257cb251SWill Deacon 		return val;
39257cb251SWill Deacon 	case RELOC_OP_PREL:
40257cb251SWill Deacon 		return val - (u64)place;
41257cb251SWill Deacon 	case RELOC_OP_PAGE:
42257cb251SWill Deacon 		return (val & ~0xfff) - ((u64)place & ~0xfff);
43257cb251SWill Deacon 	case RELOC_OP_NONE:
44257cb251SWill Deacon 		return 0;
45257cb251SWill Deacon 	}
46257cb251SWill Deacon 
47257cb251SWill Deacon 	pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
48257cb251SWill Deacon 	return 0;
49257cb251SWill Deacon }
50257cb251SWill Deacon 
reloc_data(enum aarch64_reloc_op op,void * place,u64 val,int len)51257cb251SWill Deacon static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
52257cb251SWill Deacon {
53257cb251SWill Deacon 	s64 sval = do_reloc(op, place, val);
54257cb251SWill Deacon 
551cf24a2cSArd Biesheuvel 	/*
561cf24a2cSArd Biesheuvel 	 * The ELF psABI for AArch64 documents the 16-bit and 32-bit place
573fd00bebSArd Biesheuvel 	 * relative and absolute relocations as having a range of [-2^15, 2^16)
583fd00bebSArd Biesheuvel 	 * or [-2^31, 2^32), respectively. However, in order to be able to
593fd00bebSArd Biesheuvel 	 * detect overflows reliably, we have to choose whether we interpret
603fd00bebSArd Biesheuvel 	 * such quantities as signed or as unsigned, and stick with it.
611cf24a2cSArd Biesheuvel 	 * The way we organize our address space requires a signed
621cf24a2cSArd Biesheuvel 	 * interpretation of 32-bit relative references, so let's use that
631cf24a2cSArd Biesheuvel 	 * for all R_AARCH64_PRELxx relocations. This means our upper
641cf24a2cSArd Biesheuvel 	 * bound for overflow detection should be Sxx_MAX rather than Uxx_MAX.
651cf24a2cSArd Biesheuvel 	 */
661cf24a2cSArd Biesheuvel 
67257cb251SWill Deacon 	switch (len) {
68257cb251SWill Deacon 	case 16:
69257cb251SWill Deacon 		*(s16 *)place = sval;
703fd00bebSArd Biesheuvel 		switch (op) {
713fd00bebSArd Biesheuvel 		case RELOC_OP_ABS:
723fd00bebSArd Biesheuvel 			if (sval < 0 || sval > U16_MAX)
733fd00bebSArd Biesheuvel 				return -ERANGE;
743fd00bebSArd Biesheuvel 			break;
753fd00bebSArd Biesheuvel 		case RELOC_OP_PREL:
761cf24a2cSArd Biesheuvel 			if (sval < S16_MIN || sval > S16_MAX)
77f9308969SArd Biesheuvel 				return -ERANGE;
78257cb251SWill Deacon 			break;
793fd00bebSArd Biesheuvel 		default:
803fd00bebSArd Biesheuvel 			pr_err("Invalid 16-bit data relocation (%d)\n", op);
813fd00bebSArd Biesheuvel 			return 0;
823fd00bebSArd Biesheuvel 		}
833fd00bebSArd Biesheuvel 		break;
84257cb251SWill Deacon 	case 32:
85257cb251SWill Deacon 		*(s32 *)place = sval;
863fd00bebSArd Biesheuvel 		switch (op) {
873fd00bebSArd Biesheuvel 		case RELOC_OP_ABS:
883fd00bebSArd Biesheuvel 			if (sval < 0 || sval > U32_MAX)
893fd00bebSArd Biesheuvel 				return -ERANGE;
903fd00bebSArd Biesheuvel 			break;
913fd00bebSArd Biesheuvel 		case RELOC_OP_PREL:
921cf24a2cSArd Biesheuvel 			if (sval < S32_MIN || sval > S32_MAX)
93f9308969SArd Biesheuvel 				return -ERANGE;
94257cb251SWill Deacon 			break;
953fd00bebSArd Biesheuvel 		default:
963fd00bebSArd Biesheuvel 			pr_err("Invalid 32-bit data relocation (%d)\n", op);
973fd00bebSArd Biesheuvel 			return 0;
983fd00bebSArd Biesheuvel 		}
993fd00bebSArd Biesheuvel 		break;
100257cb251SWill Deacon 	case 64:
101257cb251SWill Deacon 		*(s64 *)place = sval;
102257cb251SWill Deacon 		break;
103257cb251SWill Deacon 	default:
104257cb251SWill Deacon 		pr_err("Invalid length (%d) for data relocation\n", len);
105257cb251SWill Deacon 		return 0;
106257cb251SWill Deacon 	}
107257cb251SWill Deacon 	return 0;
108257cb251SWill Deacon }
109257cb251SWill Deacon 
110b24a5575SArd Biesheuvel enum aarch64_insn_movw_imm_type {
111b24a5575SArd Biesheuvel 	AARCH64_INSN_IMM_MOVNZ,
112b24a5575SArd Biesheuvel 	AARCH64_INSN_IMM_MOVKZ,
113b24a5575SArd Biesheuvel };
114b24a5575SArd Biesheuvel 
reloc_insn_movw(enum aarch64_reloc_op op,__le32 * place,u64 val,int lsb,enum aarch64_insn_movw_imm_type imm_type)11502129ae5SLuc Van Oostenryck static int reloc_insn_movw(enum aarch64_reloc_op op, __le32 *place, u64 val,
116b24a5575SArd Biesheuvel 			   int lsb, enum aarch64_insn_movw_imm_type imm_type)
117257cb251SWill Deacon {
118b24a5575SArd Biesheuvel 	u64 imm;
119c84fced8SJiang Liu 	s64 sval;
12002129ae5SLuc Van Oostenryck 	u32 insn = le32_to_cpu(*place);
121257cb251SWill Deacon 
122c84fced8SJiang Liu 	sval = do_reloc(op, place, val);
123b24a5575SArd Biesheuvel 	imm = sval >> lsb;
124122e2fa0SWill Deacon 
125c84fced8SJiang Liu 	if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
126257cb251SWill Deacon 		/*
127257cb251SWill Deacon 		 * For signed MOVW relocations, we have to manipulate the
128257cb251SWill Deacon 		 * instruction encoding depending on whether or not the
129257cb251SWill Deacon 		 * immediate is less than zero.
130257cb251SWill Deacon 		 */
131257cb251SWill Deacon 		insn &= ~(3 << 29);
132b24a5575SArd Biesheuvel 		if (sval >= 0) {
133257cb251SWill Deacon 			/* >=0: Set the instruction to MOVZ (opcode 10b). */
134257cb251SWill Deacon 			insn |= 2 << 29;
135257cb251SWill Deacon 		} else {
136257cb251SWill Deacon 			/*
137257cb251SWill Deacon 			 * <0: Set the instruction to MOVN (opcode 00b).
138257cb251SWill Deacon 			 *     Since we've masked the opcode already, we
139257cb251SWill Deacon 			 *     don't need to do anything other than
140257cb251SWill Deacon 			 *     inverting the new immediate field.
141257cb251SWill Deacon 			 */
142257cb251SWill Deacon 			imm = ~imm;
143257cb251SWill Deacon 		}
144257cb251SWill Deacon 	}
145257cb251SWill Deacon 
146257cb251SWill Deacon 	/* Update the instruction with the new encoding. */
147b24a5575SArd Biesheuvel 	insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm);
14802129ae5SLuc Van Oostenryck 	*place = cpu_to_le32(insn);
149257cb251SWill Deacon 
150b24a5575SArd Biesheuvel 	if (imm > U16_MAX)
151257cb251SWill Deacon 		return -ERANGE;
152257cb251SWill Deacon 
153257cb251SWill Deacon 	return 0;
154257cb251SWill Deacon }
155257cb251SWill Deacon 
reloc_insn_imm(enum aarch64_reloc_op op,__le32 * place,u64 val,int lsb,int len,enum aarch64_insn_imm_type imm_type)15602129ae5SLuc Van Oostenryck static int reloc_insn_imm(enum aarch64_reloc_op op, __le32 *place, u64 val,
157c84fced8SJiang Liu 			  int lsb, int len, enum aarch64_insn_imm_type imm_type)
158257cb251SWill Deacon {
159257cb251SWill Deacon 	u64 imm, imm_mask;
160257cb251SWill Deacon 	s64 sval;
16102129ae5SLuc Van Oostenryck 	u32 insn = le32_to_cpu(*place);
162257cb251SWill Deacon 
163257cb251SWill Deacon 	/* Calculate the relocation value. */
164257cb251SWill Deacon 	sval = do_reloc(op, place, val);
165257cb251SWill Deacon 	sval >>= lsb;
166257cb251SWill Deacon 
167257cb251SWill Deacon 	/* Extract the value bits and shift them to bit 0. */
168257cb251SWill Deacon 	imm_mask = (BIT(lsb + len) - 1) >> lsb;
169257cb251SWill Deacon 	imm = sval & imm_mask;
170257cb251SWill Deacon 
171257cb251SWill Deacon 	/* Update the instruction's immediate field. */
172c84fced8SJiang Liu 	insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
17302129ae5SLuc Van Oostenryck 	*place = cpu_to_le32(insn);
174257cb251SWill Deacon 
175257cb251SWill Deacon 	/*
176257cb251SWill Deacon 	 * Extract the upper value bits (including the sign bit) and
177257cb251SWill Deacon 	 * shift them to bit 0.
178257cb251SWill Deacon 	 */
179257cb251SWill Deacon 	sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
180257cb251SWill Deacon 
181257cb251SWill Deacon 	/*
182257cb251SWill Deacon 	 * Overflow has occurred if the upper bits are not all equal to
183257cb251SWill Deacon 	 * the sign bit of the value.
184257cb251SWill Deacon 	 */
185257cb251SWill Deacon 	if ((u64)(sval + 1) >= 2)
186257cb251SWill Deacon 		return -ERANGE;
187257cb251SWill Deacon 
188257cb251SWill Deacon 	return 0;
189257cb251SWill Deacon }
190257cb251SWill Deacon 
reloc_insn_adrp(struct module * mod,Elf64_Shdr * sechdrs,__le32 * place,u64 val)191c8ebf64eSJessica Yu static int reloc_insn_adrp(struct module *mod, Elf64_Shdr *sechdrs,
192c8ebf64eSJessica Yu 			   __le32 *place, u64 val)
193a257e025SArd Biesheuvel {
194a257e025SArd Biesheuvel 	u32 insn;
195a257e025SArd Biesheuvel 
196bdb85cd1SArd Biesheuvel 	if (!is_forbidden_offset_for_adrp(place))
197a257e025SArd Biesheuvel 		return reloc_insn_imm(RELOC_OP_PAGE, place, val, 12, 21,
198a257e025SArd Biesheuvel 				      AARCH64_INSN_IMM_ADR);
199a257e025SArd Biesheuvel 
200a257e025SArd Biesheuvel 	/* patch ADRP to ADR if it is in range */
201a257e025SArd Biesheuvel 	if (!reloc_insn_imm(RELOC_OP_PREL, place, val & ~0xfff, 0, 21,
202a257e025SArd Biesheuvel 			    AARCH64_INSN_IMM_ADR)) {
203a257e025SArd Biesheuvel 		insn = le32_to_cpu(*place);
204a257e025SArd Biesheuvel 		insn &= ~BIT(31);
205a257e025SArd Biesheuvel 	} else {
206a257e025SArd Biesheuvel 		/* out of range for ADR -> emit a veneer */
207c8ebf64eSJessica Yu 		val = module_emit_veneer_for_adrp(mod, sechdrs, place, val & ~0xfff);
208a257e025SArd Biesheuvel 		if (!val)
209a257e025SArd Biesheuvel 			return -ENOEXEC;
210a257e025SArd Biesheuvel 		insn = aarch64_insn_gen_branch_imm((u64)place, val,
211a257e025SArd Biesheuvel 						   AARCH64_INSN_BRANCH_NOLINK);
212a257e025SArd Biesheuvel 	}
213a257e025SArd Biesheuvel 
214a257e025SArd Biesheuvel 	*place = cpu_to_le32(insn);
215a257e025SArd Biesheuvel 	return 0;
216a257e025SArd Biesheuvel }
217a257e025SArd Biesheuvel 
apply_relocate_add(Elf64_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me)218257cb251SWill Deacon int apply_relocate_add(Elf64_Shdr *sechdrs,
219257cb251SWill Deacon 		       const char *strtab,
220257cb251SWill Deacon 		       unsigned int symindex,
221257cb251SWill Deacon 		       unsigned int relsec,
222257cb251SWill Deacon 		       struct module *me)
223257cb251SWill Deacon {
224257cb251SWill Deacon 	unsigned int i;
225257cb251SWill Deacon 	int ovf;
226257cb251SWill Deacon 	bool overflow_check;
227257cb251SWill Deacon 	Elf64_Sym *sym;
228257cb251SWill Deacon 	void *loc;
229257cb251SWill Deacon 	u64 val;
230257cb251SWill Deacon 	Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
231257cb251SWill Deacon 
232257cb251SWill Deacon 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
233257cb251SWill Deacon 		/* loc corresponds to P in the AArch64 ELF document. */
234257cb251SWill Deacon 		loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
235257cb251SWill Deacon 			+ rel[i].r_offset;
236257cb251SWill Deacon 
237257cb251SWill Deacon 		/* sym is the ELF symbol we're referring to. */
238257cb251SWill Deacon 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
239257cb251SWill Deacon 			+ ELF64_R_SYM(rel[i].r_info);
240257cb251SWill Deacon 
241257cb251SWill Deacon 		/* val corresponds to (S + A) in the AArch64 ELF document. */
242257cb251SWill Deacon 		val = sym->st_value + rel[i].r_addend;
243257cb251SWill Deacon 
244257cb251SWill Deacon 		/* Check for overflow by default. */
245257cb251SWill Deacon 		overflow_check = true;
246257cb251SWill Deacon 
247257cb251SWill Deacon 		/* Perform the static relocation. */
248257cb251SWill Deacon 		switch (ELF64_R_TYPE(rel[i].r_info)) {
249257cb251SWill Deacon 		/* Null relocations. */
250257cb251SWill Deacon 		case R_ARM_NONE:
251257cb251SWill Deacon 		case R_AARCH64_NONE:
252257cb251SWill Deacon 			ovf = 0;
253257cb251SWill Deacon 			break;
254257cb251SWill Deacon 
255257cb251SWill Deacon 		/* Data relocations. */
256257cb251SWill Deacon 		case R_AARCH64_ABS64:
257257cb251SWill Deacon 			overflow_check = false;
258257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
259257cb251SWill Deacon 			break;
260257cb251SWill Deacon 		case R_AARCH64_ABS32:
261257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
262257cb251SWill Deacon 			break;
263257cb251SWill Deacon 		case R_AARCH64_ABS16:
264257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
265257cb251SWill Deacon 			break;
266257cb251SWill Deacon 		case R_AARCH64_PREL64:
267257cb251SWill Deacon 			overflow_check = false;
268257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
269257cb251SWill Deacon 			break;
270257cb251SWill Deacon 		case R_AARCH64_PREL32:
271257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
272257cb251SWill Deacon 			break;
273257cb251SWill Deacon 		case R_AARCH64_PREL16:
274257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
275257cb251SWill Deacon 			break;
276257cb251SWill Deacon 
277257cb251SWill Deacon 		/* MOVW instruction relocations. */
278257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0_NC:
279257cb251SWill Deacon 			overflow_check = false;
280df561f66SGustavo A. R. Silva 			fallthrough;
281257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0:
282257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
283b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
284257cb251SWill Deacon 			break;
285257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1_NC:
286257cb251SWill Deacon 			overflow_check = false;
287df561f66SGustavo A. R. Silva 			fallthrough;
288257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1:
289257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
290b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
291257cb251SWill Deacon 			break;
292257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2_NC:
293257cb251SWill Deacon 			overflow_check = false;
294df561f66SGustavo A. R. Silva 			fallthrough;
295257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2:
296257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
297b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
298257cb251SWill Deacon 			break;
299257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G3:
300257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
301257cb251SWill Deacon 			overflow_check = false;
302257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
303b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
304257cb251SWill Deacon 			break;
305257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G0:
306257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
307c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
308257cb251SWill Deacon 			break;
309257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G1:
310257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
311c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
312257cb251SWill Deacon 			break;
313257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G2:
314257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
315c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
316257cb251SWill Deacon 			break;
317257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0_NC:
318257cb251SWill Deacon 			overflow_check = false;
319257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
320b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
321257cb251SWill Deacon 			break;
322257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0:
323257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
324c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
325257cb251SWill Deacon 			break;
326257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1_NC:
327257cb251SWill Deacon 			overflow_check = false;
328257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
329b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
330257cb251SWill Deacon 			break;
331257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1:
332257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
333c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
334257cb251SWill Deacon 			break;
335257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2_NC:
336257cb251SWill Deacon 			overflow_check = false;
337257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
338b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
339257cb251SWill Deacon 			break;
340257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2:
341257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
342c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
343257cb251SWill Deacon 			break;
344257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_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_PREL, loc, val, 48,
348c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
349257cb251SWill Deacon 			break;
350257cb251SWill Deacon 
351257cb251SWill Deacon 		/* Immediate instruction relocations. */
352257cb251SWill Deacon 		case R_AARCH64_LD_PREL_LO19:
353257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
354c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
355257cb251SWill Deacon 			break;
356257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_LO21:
357257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
358c84fced8SJiang Liu 					     AARCH64_INSN_IMM_ADR);
359257cb251SWill Deacon 			break;
360257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21_NC:
361257cb251SWill Deacon 			overflow_check = false;
362df561f66SGustavo A. R. Silva 			fallthrough;
363257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21:
364c8ebf64eSJessica Yu 			ovf = reloc_insn_adrp(me, sechdrs, loc, val);
365a257e025SArd Biesheuvel 			if (ovf && ovf != -ERANGE)
366a257e025SArd Biesheuvel 				return ovf;
367257cb251SWill Deacon 			break;
368257cb251SWill Deacon 		case R_AARCH64_ADD_ABS_LO12_NC:
369257cb251SWill Deacon 		case R_AARCH64_LDST8_ABS_LO12_NC:
370257cb251SWill Deacon 			overflow_check = false;
371257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
372c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
373257cb251SWill Deacon 			break;
374257cb251SWill Deacon 		case R_AARCH64_LDST16_ABS_LO12_NC:
375257cb251SWill Deacon 			overflow_check = false;
376257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
377c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
378257cb251SWill Deacon 			break;
379257cb251SWill Deacon 		case R_AARCH64_LDST32_ABS_LO12_NC:
380257cb251SWill Deacon 			overflow_check = false;
381257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
382c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
383257cb251SWill Deacon 			break;
384257cb251SWill Deacon 		case R_AARCH64_LDST64_ABS_LO12_NC:
385257cb251SWill Deacon 			overflow_check = false;
386257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
387c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
388257cb251SWill Deacon 			break;
389257cb251SWill Deacon 		case R_AARCH64_LDST128_ABS_LO12_NC:
390257cb251SWill Deacon 			overflow_check = false;
391257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
392c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
393257cb251SWill Deacon 			break;
394257cb251SWill Deacon 		case R_AARCH64_TSTBR14:
395257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
396c84fced8SJiang Liu 					     AARCH64_INSN_IMM_14);
397257cb251SWill Deacon 			break;
398257cb251SWill Deacon 		case R_AARCH64_CONDBR19:
399257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
400c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
401257cb251SWill Deacon 			break;
402257cb251SWill Deacon 		case R_AARCH64_JUMP26:
403257cb251SWill Deacon 		case R_AARCH64_CALL26:
404257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
405c84fced8SJiang Liu 					     AARCH64_INSN_IMM_26);
406ea3752baSMark Rutland 			if (ovf == -ERANGE) {
407c8ebf64eSJessica Yu 				val = module_emit_plt_entry(me, sechdrs, loc, &rel[i], sym);
4085e8307b9SArd Biesheuvel 				if (!val)
4095e8307b9SArd Biesheuvel 					return -ENOEXEC;
410fd045f6cSArd Biesheuvel 				ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2,
411fd045f6cSArd Biesheuvel 						     26, AARCH64_INSN_IMM_26);
412fd045f6cSArd Biesheuvel 			}
413257cb251SWill Deacon 			break;
414257cb251SWill Deacon 
415257cb251SWill Deacon 		default:
416257cb251SWill Deacon 			pr_err("module %s: unsupported RELA relocation: %llu\n",
417257cb251SWill Deacon 			       me->name, ELF64_R_TYPE(rel[i].r_info));
418257cb251SWill Deacon 			return -ENOEXEC;
419257cb251SWill Deacon 		}
420257cb251SWill Deacon 
421257cb251SWill Deacon 		if (overflow_check && ovf == -ERANGE)
422257cb251SWill Deacon 			goto overflow;
423257cb251SWill Deacon 
424257cb251SWill Deacon 	}
425257cb251SWill Deacon 
426257cb251SWill Deacon 	return 0;
427257cb251SWill Deacon 
428257cb251SWill Deacon overflow:
429257cb251SWill Deacon 	pr_err("module %s: overflow in relocation type %d val %Lx\n",
430257cb251SWill Deacon 	       me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
431257cb251SWill Deacon 	return -ENOEXEC;
432257cb251SWill Deacon }
433932ded4bSAndre Przywara 
__init_plt(struct plt_entry * plt,unsigned long addr)4343b23e499STorsten Duwe static inline void __init_plt(struct plt_entry *plt, unsigned long addr)
4353b23e499STorsten Duwe {
4363b23e499STorsten Duwe 	*plt = get_plt_entry(addr, plt);
4373b23e499STorsten Duwe }
4383b23e499STorsten Duwe 
module_init_ftrace_plt(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * mod)439f1a54ae9SMark Rutland static int module_init_ftrace_plt(const Elf_Ehdr *hdr,
440f1a54ae9SMark Rutland 				  const Elf_Shdr *sechdrs,
441f1a54ae9SMark Rutland 				  struct module *mod)
442f1a54ae9SMark Rutland {
443ea3752baSMark Rutland #if defined(CONFIG_DYNAMIC_FTRACE)
444f1a54ae9SMark Rutland 	const Elf_Shdr *s;
4453b23e499STorsten Duwe 	struct plt_entry *plts;
446f1a54ae9SMark Rutland 
447f1a54ae9SMark Rutland 	s = find_section(hdr, sechdrs, ".text.ftrace_trampoline");
448f1a54ae9SMark Rutland 	if (!s)
449f1a54ae9SMark Rutland 		return -ENOEXEC;
450f1a54ae9SMark Rutland 
4513b23e499STorsten Duwe 	plts = (void *)s->sh_addr;
4523b23e499STorsten Duwe 
4533b23e499STorsten Duwe 	__init_plt(&plts[FTRACE_PLT_IDX], FTRACE_ADDR);
4543b23e499STorsten Duwe 
4553b23e499STorsten Duwe 	mod->arch.ftrace_trampolines = plts;
456f1a54ae9SMark Rutland #endif
457f1a54ae9SMark Rutland 	return 0;
458f1a54ae9SMark Rutland }
459f1a54ae9SMark Rutland 
module_finalize(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * me)460bd8b21d3SMark Rutland int module_finalize(const Elf_Ehdr *hdr,
461bd8b21d3SMark Rutland 		    const Elf_Shdr *sechdrs,
462bd8b21d3SMark Rutland 		    struct module *me)
463bd8b21d3SMark Rutland {
464bd8b21d3SMark Rutland 	const Elf_Shdr *s;
465*ccf54058SArd Biesheuvel 	int ret;
466*ccf54058SArd Biesheuvel 
467bd8b21d3SMark Rutland 	s = find_section(hdr, sechdrs, ".altinstructions");
468bd8b21d3SMark Rutland 	if (s)
469bd8b21d3SMark Rutland 		apply_alternatives_module((void *)s->sh_addr, s->sh_size);
470bd8b21d3SMark Rutland 
4713b619e22SArd Biesheuvel 	if (scs_is_dynamic()) {
4723b619e22SArd Biesheuvel 		s = find_section(hdr, sechdrs, ".init.eh_frame");
473*ccf54058SArd Biesheuvel 		if (s) {
474*ccf54058SArd Biesheuvel 			ret = __pi_scs_patch((void *)s->sh_addr, s->sh_size);
475*ccf54058SArd Biesheuvel 			if (ret)
476*ccf54058SArd Biesheuvel 				pr_err("module %s: error occurred during dynamic SCS patching (%d)\n",
477*ccf54058SArd Biesheuvel 				       me->name, ret);
478*ccf54058SArd Biesheuvel 		}
4793b619e22SArd Biesheuvel 	}
4803b619e22SArd Biesheuvel 
481f1a54ae9SMark Rutland 	return module_init_ftrace_plt(hdr, sechdrs, me);
482932ded4bSAndre Przywara }
483