xref: /openbmc/linux/arch/arm64/kernel/module.c (revision 6f496a555d93db7a11d4860b9220d904822f586a)
1257cb251SWill Deacon /*
2257cb251SWill Deacon  * AArch64 loadable module support.
3257cb251SWill Deacon  *
4257cb251SWill Deacon  * Copyright (C) 2012 ARM Limited
5257cb251SWill Deacon  *
6257cb251SWill Deacon  * This program is free software; you can redistribute it and/or modify
7257cb251SWill Deacon  * it under the terms of the GNU General Public License version 2 as
8257cb251SWill Deacon  * published by the Free Software Foundation.
9257cb251SWill Deacon  *
10257cb251SWill Deacon  * This program is distributed in the hope that it will be useful,
11257cb251SWill Deacon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12257cb251SWill Deacon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13257cb251SWill Deacon  * GNU General Public License for more details.
14257cb251SWill Deacon  *
15257cb251SWill Deacon  * You should have received a copy of the GNU General Public License
16257cb251SWill Deacon  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17257cb251SWill Deacon  *
18257cb251SWill Deacon  * Author: Will Deacon <will.deacon@arm.com>
19257cb251SWill Deacon  */
20257cb251SWill Deacon 
21257cb251SWill Deacon #include <linux/bitops.h>
22257cb251SWill Deacon #include <linux/elf.h>
23257cb251SWill Deacon #include <linux/gfp.h>
2439d114ddSAndrey Ryabinin #include <linux/kasan.h>
25257cb251SWill Deacon #include <linux/kernel.h>
26257cb251SWill Deacon #include <linux/mm.h>
27257cb251SWill Deacon #include <linux/moduleloader.h>
28257cb251SWill Deacon #include <linux/vmalloc.h>
292c2b282dSPaul Walmsley #include <asm/alternative.h>
30c84fced8SJiang Liu #include <asm/insn.h>
31932ded4bSAndre Przywara #include <asm/sections.h>
32c84fced8SJiang Liu 
33257cb251SWill Deacon void *module_alloc(unsigned long size)
34257cb251SWill Deacon {
35*6f496a55SArd Biesheuvel 	u64 module_alloc_end = module_alloc_base + MODULES_VSIZE;
360c2cf6d9SFlorian Fainelli 	gfp_t gfp_mask = GFP_KERNEL;
3739d114ddSAndrey Ryabinin 	void *p;
3839d114ddSAndrey Ryabinin 
390c2cf6d9SFlorian Fainelli 	/* Silence the initial allocation */
400c2cf6d9SFlorian Fainelli 	if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
410c2cf6d9SFlorian Fainelli 		gfp_mask |= __GFP_NOWARN;
420c2cf6d9SFlorian Fainelli 
43*6f496a55SArd Biesheuvel 	if (IS_ENABLED(CONFIG_KASAN))
44*6f496a55SArd Biesheuvel 		/* don't exceed the static module region - see below */
45*6f496a55SArd Biesheuvel 		module_alloc_end = MODULES_END;
46*6f496a55SArd Biesheuvel 
47f80fb3a3SArd Biesheuvel 	p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base,
48*6f496a55SArd Biesheuvel 				module_alloc_end, gfp_mask, PAGE_KERNEL_EXEC, 0,
49cb9e3c29SAndrey Ryabinin 				NUMA_NO_NODE, __builtin_return_address(0));
5039d114ddSAndrey Ryabinin 
51fd045f6cSArd Biesheuvel 	if (!p && IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
52fd045f6cSArd Biesheuvel 	    !IS_ENABLED(CONFIG_KASAN))
53fd045f6cSArd Biesheuvel 		/*
54fd045f6cSArd Biesheuvel 		 * KASAN can only deal with module allocations being served
55fd045f6cSArd Biesheuvel 		 * from the reserved module region, since the remainder of
56fd045f6cSArd Biesheuvel 		 * the vmalloc region is already backed by zero shadow pages,
57fd045f6cSArd Biesheuvel 		 * and punching holes into it is non-trivial. Since the module
58fd045f6cSArd Biesheuvel 		 * region is not randomized when KASAN is enabled, it is even
59fd045f6cSArd Biesheuvel 		 * less likely that the module region gets exhausted, so we
60fd045f6cSArd Biesheuvel 		 * can simply omit this fallback in that case.
61fd045f6cSArd Biesheuvel 		 */
62f2b9ba87SArd Biesheuvel 		p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base,
63b2eed9b5SArd Biesheuvel 				module_alloc_base + SZ_2G, GFP_KERNEL,
64f2b9ba87SArd Biesheuvel 				PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
65f2b9ba87SArd Biesheuvel 				__builtin_return_address(0));
66fd045f6cSArd Biesheuvel 
6739d114ddSAndrey Ryabinin 	if (p && (kasan_module_alloc(p, size) < 0)) {
6839d114ddSAndrey Ryabinin 		vfree(p);
6939d114ddSAndrey Ryabinin 		return NULL;
7039d114ddSAndrey Ryabinin 	}
7139d114ddSAndrey Ryabinin 
7239d114ddSAndrey Ryabinin 	return p;
73257cb251SWill Deacon }
74257cb251SWill Deacon 
75257cb251SWill Deacon enum aarch64_reloc_op {
76257cb251SWill Deacon 	RELOC_OP_NONE,
77257cb251SWill Deacon 	RELOC_OP_ABS,
78257cb251SWill Deacon 	RELOC_OP_PREL,
79257cb251SWill Deacon 	RELOC_OP_PAGE,
80257cb251SWill Deacon };
81257cb251SWill Deacon 
8202129ae5SLuc Van Oostenryck static u64 do_reloc(enum aarch64_reloc_op reloc_op, __le32 *place, u64 val)
83257cb251SWill Deacon {
84257cb251SWill Deacon 	switch (reloc_op) {
85257cb251SWill Deacon 	case RELOC_OP_ABS:
86257cb251SWill Deacon 		return val;
87257cb251SWill Deacon 	case RELOC_OP_PREL:
88257cb251SWill Deacon 		return val - (u64)place;
89257cb251SWill Deacon 	case RELOC_OP_PAGE:
90257cb251SWill Deacon 		return (val & ~0xfff) - ((u64)place & ~0xfff);
91257cb251SWill Deacon 	case RELOC_OP_NONE:
92257cb251SWill Deacon 		return 0;
93257cb251SWill Deacon 	}
94257cb251SWill Deacon 
95257cb251SWill Deacon 	pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
96257cb251SWill Deacon 	return 0;
97257cb251SWill Deacon }
98257cb251SWill Deacon 
99257cb251SWill Deacon static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
100257cb251SWill Deacon {
101257cb251SWill Deacon 	s64 sval = do_reloc(op, place, val);
102257cb251SWill Deacon 
1031cf24a2cSArd Biesheuvel 	/*
1041cf24a2cSArd Biesheuvel 	 * The ELF psABI for AArch64 documents the 16-bit and 32-bit place
1053fd00bebSArd Biesheuvel 	 * relative and absolute relocations as having a range of [-2^15, 2^16)
1063fd00bebSArd Biesheuvel 	 * or [-2^31, 2^32), respectively. However, in order to be able to
1073fd00bebSArd Biesheuvel 	 * detect overflows reliably, we have to choose whether we interpret
1083fd00bebSArd Biesheuvel 	 * such quantities as signed or as unsigned, and stick with it.
1091cf24a2cSArd Biesheuvel 	 * The way we organize our address space requires a signed
1101cf24a2cSArd Biesheuvel 	 * interpretation of 32-bit relative references, so let's use that
1111cf24a2cSArd Biesheuvel 	 * for all R_AARCH64_PRELxx relocations. This means our upper
1121cf24a2cSArd Biesheuvel 	 * bound for overflow detection should be Sxx_MAX rather than Uxx_MAX.
1131cf24a2cSArd Biesheuvel 	 */
1141cf24a2cSArd Biesheuvel 
115257cb251SWill Deacon 	switch (len) {
116257cb251SWill Deacon 	case 16:
117257cb251SWill Deacon 		*(s16 *)place = sval;
1183fd00bebSArd Biesheuvel 		switch (op) {
1193fd00bebSArd Biesheuvel 		case RELOC_OP_ABS:
1203fd00bebSArd Biesheuvel 			if (sval < 0 || sval > U16_MAX)
1213fd00bebSArd Biesheuvel 				return -ERANGE;
1223fd00bebSArd Biesheuvel 			break;
1233fd00bebSArd Biesheuvel 		case RELOC_OP_PREL:
1241cf24a2cSArd Biesheuvel 			if (sval < S16_MIN || sval > S16_MAX)
125f9308969SArd Biesheuvel 				return -ERANGE;
126257cb251SWill Deacon 			break;
1273fd00bebSArd Biesheuvel 		default:
1283fd00bebSArd Biesheuvel 			pr_err("Invalid 16-bit data relocation (%d)\n", op);
1293fd00bebSArd Biesheuvel 			return 0;
1303fd00bebSArd Biesheuvel 		}
1313fd00bebSArd Biesheuvel 		break;
132257cb251SWill Deacon 	case 32:
133257cb251SWill Deacon 		*(s32 *)place = sval;
1343fd00bebSArd Biesheuvel 		switch (op) {
1353fd00bebSArd Biesheuvel 		case RELOC_OP_ABS:
1363fd00bebSArd Biesheuvel 			if (sval < 0 || sval > U32_MAX)
1373fd00bebSArd Biesheuvel 				return -ERANGE;
1383fd00bebSArd Biesheuvel 			break;
1393fd00bebSArd Biesheuvel 		case RELOC_OP_PREL:
1401cf24a2cSArd Biesheuvel 			if (sval < S32_MIN || sval > S32_MAX)
141f9308969SArd Biesheuvel 				return -ERANGE;
142257cb251SWill Deacon 			break;
1433fd00bebSArd Biesheuvel 		default:
1443fd00bebSArd Biesheuvel 			pr_err("Invalid 32-bit data relocation (%d)\n", op);
1453fd00bebSArd Biesheuvel 			return 0;
1463fd00bebSArd Biesheuvel 		}
1473fd00bebSArd Biesheuvel 		break;
148257cb251SWill Deacon 	case 64:
149257cb251SWill Deacon 		*(s64 *)place = sval;
150257cb251SWill Deacon 		break;
151257cb251SWill Deacon 	default:
152257cb251SWill Deacon 		pr_err("Invalid length (%d) for data relocation\n", len);
153257cb251SWill Deacon 		return 0;
154257cb251SWill Deacon 	}
155257cb251SWill Deacon 	return 0;
156257cb251SWill Deacon }
157257cb251SWill Deacon 
158b24a5575SArd Biesheuvel enum aarch64_insn_movw_imm_type {
159b24a5575SArd Biesheuvel 	AARCH64_INSN_IMM_MOVNZ,
160b24a5575SArd Biesheuvel 	AARCH64_INSN_IMM_MOVKZ,
161b24a5575SArd Biesheuvel };
162b24a5575SArd Biesheuvel 
16302129ae5SLuc Van Oostenryck static int reloc_insn_movw(enum aarch64_reloc_op op, __le32 *place, u64 val,
164b24a5575SArd Biesheuvel 			   int lsb, enum aarch64_insn_movw_imm_type imm_type)
165257cb251SWill Deacon {
166b24a5575SArd Biesheuvel 	u64 imm;
167c84fced8SJiang Liu 	s64 sval;
16802129ae5SLuc Van Oostenryck 	u32 insn = le32_to_cpu(*place);
169257cb251SWill Deacon 
170c84fced8SJiang Liu 	sval = do_reloc(op, place, val);
171b24a5575SArd Biesheuvel 	imm = sval >> lsb;
172122e2fa0SWill Deacon 
173c84fced8SJiang Liu 	if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
174257cb251SWill Deacon 		/*
175257cb251SWill Deacon 		 * For signed MOVW relocations, we have to manipulate the
176257cb251SWill Deacon 		 * instruction encoding depending on whether or not the
177257cb251SWill Deacon 		 * immediate is less than zero.
178257cb251SWill Deacon 		 */
179257cb251SWill Deacon 		insn &= ~(3 << 29);
180b24a5575SArd Biesheuvel 		if (sval >= 0) {
181257cb251SWill Deacon 			/* >=0: Set the instruction to MOVZ (opcode 10b). */
182257cb251SWill Deacon 			insn |= 2 << 29;
183257cb251SWill Deacon 		} else {
184257cb251SWill Deacon 			/*
185257cb251SWill Deacon 			 * <0: Set the instruction to MOVN (opcode 00b).
186257cb251SWill Deacon 			 *     Since we've masked the opcode already, we
187257cb251SWill Deacon 			 *     don't need to do anything other than
188257cb251SWill Deacon 			 *     inverting the new immediate field.
189257cb251SWill Deacon 			 */
190257cb251SWill Deacon 			imm = ~imm;
191257cb251SWill Deacon 		}
192257cb251SWill Deacon 	}
193257cb251SWill Deacon 
194257cb251SWill Deacon 	/* Update the instruction with the new encoding. */
195b24a5575SArd Biesheuvel 	insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm);
19602129ae5SLuc Van Oostenryck 	*place = cpu_to_le32(insn);
197257cb251SWill Deacon 
198b24a5575SArd Biesheuvel 	if (imm > U16_MAX)
199257cb251SWill Deacon 		return -ERANGE;
200257cb251SWill Deacon 
201257cb251SWill Deacon 	return 0;
202257cb251SWill Deacon }
203257cb251SWill Deacon 
20402129ae5SLuc Van Oostenryck static int reloc_insn_imm(enum aarch64_reloc_op op, __le32 *place, u64 val,
205c84fced8SJiang Liu 			  int lsb, int len, enum aarch64_insn_imm_type imm_type)
206257cb251SWill Deacon {
207257cb251SWill Deacon 	u64 imm, imm_mask;
208257cb251SWill Deacon 	s64 sval;
20902129ae5SLuc Van Oostenryck 	u32 insn = le32_to_cpu(*place);
210257cb251SWill Deacon 
211257cb251SWill Deacon 	/* Calculate the relocation value. */
212257cb251SWill Deacon 	sval = do_reloc(op, place, val);
213257cb251SWill Deacon 	sval >>= lsb;
214257cb251SWill Deacon 
215257cb251SWill Deacon 	/* Extract the value bits and shift them to bit 0. */
216257cb251SWill Deacon 	imm_mask = (BIT(lsb + len) - 1) >> lsb;
217257cb251SWill Deacon 	imm = sval & imm_mask;
218257cb251SWill Deacon 
219257cb251SWill Deacon 	/* Update the instruction's immediate field. */
220c84fced8SJiang Liu 	insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
22102129ae5SLuc Van Oostenryck 	*place = cpu_to_le32(insn);
222257cb251SWill Deacon 
223257cb251SWill Deacon 	/*
224257cb251SWill Deacon 	 * Extract the upper value bits (including the sign bit) and
225257cb251SWill Deacon 	 * shift them to bit 0.
226257cb251SWill Deacon 	 */
227257cb251SWill Deacon 	sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
228257cb251SWill Deacon 
229257cb251SWill Deacon 	/*
230257cb251SWill Deacon 	 * Overflow has occurred if the upper bits are not all equal to
231257cb251SWill Deacon 	 * the sign bit of the value.
232257cb251SWill Deacon 	 */
233257cb251SWill Deacon 	if ((u64)(sval + 1) >= 2)
234257cb251SWill Deacon 		return -ERANGE;
235257cb251SWill Deacon 
236257cb251SWill Deacon 	return 0;
237257cb251SWill Deacon }
238257cb251SWill Deacon 
239c8ebf64eSJessica Yu static int reloc_insn_adrp(struct module *mod, Elf64_Shdr *sechdrs,
240c8ebf64eSJessica Yu 			   __le32 *place, u64 val)
241a257e025SArd Biesheuvel {
242a257e025SArd Biesheuvel 	u32 insn;
243a257e025SArd Biesheuvel 
244bdb85cd1SArd Biesheuvel 	if (!is_forbidden_offset_for_adrp(place))
245a257e025SArd Biesheuvel 		return reloc_insn_imm(RELOC_OP_PAGE, place, val, 12, 21,
246a257e025SArd Biesheuvel 				      AARCH64_INSN_IMM_ADR);
247a257e025SArd Biesheuvel 
248a257e025SArd Biesheuvel 	/* patch ADRP to ADR if it is in range */
249a257e025SArd Biesheuvel 	if (!reloc_insn_imm(RELOC_OP_PREL, place, val & ~0xfff, 0, 21,
250a257e025SArd Biesheuvel 			    AARCH64_INSN_IMM_ADR)) {
251a257e025SArd Biesheuvel 		insn = le32_to_cpu(*place);
252a257e025SArd Biesheuvel 		insn &= ~BIT(31);
253a257e025SArd Biesheuvel 	} else {
254a257e025SArd Biesheuvel 		/* out of range for ADR -> emit a veneer */
255c8ebf64eSJessica Yu 		val = module_emit_veneer_for_adrp(mod, sechdrs, place, val & ~0xfff);
256a257e025SArd Biesheuvel 		if (!val)
257a257e025SArd Biesheuvel 			return -ENOEXEC;
258a257e025SArd Biesheuvel 		insn = aarch64_insn_gen_branch_imm((u64)place, val,
259a257e025SArd Biesheuvel 						   AARCH64_INSN_BRANCH_NOLINK);
260a257e025SArd Biesheuvel 	}
261a257e025SArd Biesheuvel 
262a257e025SArd Biesheuvel 	*place = cpu_to_le32(insn);
263a257e025SArd Biesheuvel 	return 0;
264a257e025SArd Biesheuvel }
265a257e025SArd Biesheuvel 
266257cb251SWill Deacon int apply_relocate_add(Elf64_Shdr *sechdrs,
267257cb251SWill Deacon 		       const char *strtab,
268257cb251SWill Deacon 		       unsigned int symindex,
269257cb251SWill Deacon 		       unsigned int relsec,
270257cb251SWill Deacon 		       struct module *me)
271257cb251SWill Deacon {
272257cb251SWill Deacon 	unsigned int i;
273257cb251SWill Deacon 	int ovf;
274257cb251SWill Deacon 	bool overflow_check;
275257cb251SWill Deacon 	Elf64_Sym *sym;
276257cb251SWill Deacon 	void *loc;
277257cb251SWill Deacon 	u64 val;
278257cb251SWill Deacon 	Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
279257cb251SWill Deacon 
280257cb251SWill Deacon 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
281257cb251SWill Deacon 		/* loc corresponds to P in the AArch64 ELF document. */
282257cb251SWill Deacon 		loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
283257cb251SWill Deacon 			+ rel[i].r_offset;
284257cb251SWill Deacon 
285257cb251SWill Deacon 		/* sym is the ELF symbol we're referring to. */
286257cb251SWill Deacon 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
287257cb251SWill Deacon 			+ ELF64_R_SYM(rel[i].r_info);
288257cb251SWill Deacon 
289257cb251SWill Deacon 		/* val corresponds to (S + A) in the AArch64 ELF document. */
290257cb251SWill Deacon 		val = sym->st_value + rel[i].r_addend;
291257cb251SWill Deacon 
292257cb251SWill Deacon 		/* Check for overflow by default. */
293257cb251SWill Deacon 		overflow_check = true;
294257cb251SWill Deacon 
295257cb251SWill Deacon 		/* Perform the static relocation. */
296257cb251SWill Deacon 		switch (ELF64_R_TYPE(rel[i].r_info)) {
297257cb251SWill Deacon 		/* Null relocations. */
298257cb251SWill Deacon 		case R_ARM_NONE:
299257cb251SWill Deacon 		case R_AARCH64_NONE:
300257cb251SWill Deacon 			ovf = 0;
301257cb251SWill Deacon 			break;
302257cb251SWill Deacon 
303257cb251SWill Deacon 		/* Data relocations. */
304257cb251SWill Deacon 		case R_AARCH64_ABS64:
305257cb251SWill Deacon 			overflow_check = false;
306257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
307257cb251SWill Deacon 			break;
308257cb251SWill Deacon 		case R_AARCH64_ABS32:
309257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
310257cb251SWill Deacon 			break;
311257cb251SWill Deacon 		case R_AARCH64_ABS16:
312257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
313257cb251SWill Deacon 			break;
314257cb251SWill Deacon 		case R_AARCH64_PREL64:
315257cb251SWill Deacon 			overflow_check = false;
316257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
317257cb251SWill Deacon 			break;
318257cb251SWill Deacon 		case R_AARCH64_PREL32:
319257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
320257cb251SWill Deacon 			break;
321257cb251SWill Deacon 		case R_AARCH64_PREL16:
322257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
323257cb251SWill Deacon 			break;
324257cb251SWill Deacon 
325257cb251SWill Deacon 		/* MOVW instruction relocations. */
326257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0_NC:
327257cb251SWill Deacon 			overflow_check = false;
328257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0:
329257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
330b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
331257cb251SWill Deacon 			break;
332257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1_NC:
333257cb251SWill Deacon 			overflow_check = false;
334257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1:
335257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
336b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
337257cb251SWill Deacon 			break;
338257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2_NC:
339257cb251SWill Deacon 			overflow_check = false;
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;
407257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21:
408c8ebf64eSJessica Yu 			ovf = reloc_insn_adrp(me, sechdrs, loc, val);
409a257e025SArd Biesheuvel 			if (ovf && ovf != -ERANGE)
410a257e025SArd Biesheuvel 				return ovf;
411257cb251SWill Deacon 			break;
412257cb251SWill Deacon 		case R_AARCH64_ADD_ABS_LO12_NC:
413257cb251SWill Deacon 		case R_AARCH64_LDST8_ABS_LO12_NC:
414257cb251SWill Deacon 			overflow_check = false;
415257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
416c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
417257cb251SWill Deacon 			break;
418257cb251SWill Deacon 		case R_AARCH64_LDST16_ABS_LO12_NC:
419257cb251SWill Deacon 			overflow_check = false;
420257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
421c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
422257cb251SWill Deacon 			break;
423257cb251SWill Deacon 		case R_AARCH64_LDST32_ABS_LO12_NC:
424257cb251SWill Deacon 			overflow_check = false;
425257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
426c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
427257cb251SWill Deacon 			break;
428257cb251SWill Deacon 		case R_AARCH64_LDST64_ABS_LO12_NC:
429257cb251SWill Deacon 			overflow_check = false;
430257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
431c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
432257cb251SWill Deacon 			break;
433257cb251SWill Deacon 		case R_AARCH64_LDST128_ABS_LO12_NC:
434257cb251SWill Deacon 			overflow_check = false;
435257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
436c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
437257cb251SWill Deacon 			break;
438257cb251SWill Deacon 		case R_AARCH64_TSTBR14:
439257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
440c84fced8SJiang Liu 					     AARCH64_INSN_IMM_14);
441257cb251SWill Deacon 			break;
442257cb251SWill Deacon 		case R_AARCH64_CONDBR19:
443257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
444c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
445257cb251SWill Deacon 			break;
446257cb251SWill Deacon 		case R_AARCH64_JUMP26:
447257cb251SWill Deacon 		case R_AARCH64_CALL26:
448257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
449c84fced8SJiang Liu 					     AARCH64_INSN_IMM_26);
450fd045f6cSArd Biesheuvel 
451fd045f6cSArd Biesheuvel 			if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
452fd045f6cSArd Biesheuvel 			    ovf == -ERANGE) {
453c8ebf64eSJessica Yu 				val = module_emit_plt_entry(me, sechdrs, loc, &rel[i], sym);
4545e8307b9SArd Biesheuvel 				if (!val)
4555e8307b9SArd Biesheuvel 					return -ENOEXEC;
456fd045f6cSArd Biesheuvel 				ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2,
457fd045f6cSArd Biesheuvel 						     26, AARCH64_INSN_IMM_26);
458fd045f6cSArd Biesheuvel 			}
459257cb251SWill Deacon 			break;
460257cb251SWill Deacon 
461257cb251SWill Deacon 		default:
462257cb251SWill Deacon 			pr_err("module %s: unsupported RELA relocation: %llu\n",
463257cb251SWill Deacon 			       me->name, ELF64_R_TYPE(rel[i].r_info));
464257cb251SWill Deacon 			return -ENOEXEC;
465257cb251SWill Deacon 		}
466257cb251SWill Deacon 
467257cb251SWill Deacon 		if (overflow_check && ovf == -ERANGE)
468257cb251SWill Deacon 			goto overflow;
469257cb251SWill Deacon 
470257cb251SWill Deacon 	}
471257cb251SWill Deacon 
472257cb251SWill Deacon 	return 0;
473257cb251SWill Deacon 
474257cb251SWill Deacon overflow:
475257cb251SWill Deacon 	pr_err("module %s: overflow in relocation type %d val %Lx\n",
476257cb251SWill Deacon 	       me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
477257cb251SWill Deacon 	return -ENOEXEC;
478257cb251SWill Deacon }
479932ded4bSAndre Przywara 
480932ded4bSAndre Przywara int module_finalize(const Elf_Ehdr *hdr,
481932ded4bSAndre Przywara 		    const Elf_Shdr *sechdrs,
482932ded4bSAndre Przywara 		    struct module *me)
483932ded4bSAndre Przywara {
484932ded4bSAndre Przywara 	const Elf_Shdr *s, *se;
485932ded4bSAndre Przywara 	const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
486932ded4bSAndre Przywara 
487932ded4bSAndre Przywara 	for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
48842938868SWill Deacon 		if (strcmp(".altinstructions", secstrs + s->sh_name) == 0)
48942938868SWill Deacon 			apply_alternatives_module((void *)s->sh_addr, s->sh_size);
490e71a4e1bSArd Biesheuvel #ifdef CONFIG_ARM64_MODULE_PLTS
491e71a4e1bSArd Biesheuvel 		if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE) &&
492e71a4e1bSArd Biesheuvel 		    !strcmp(".text.ftrace_trampoline", secstrs + s->sh_name))
493e71a4e1bSArd Biesheuvel 			me->arch.ftrace_trampoline = (void *)s->sh_addr;
494e71a4e1bSArd Biesheuvel #endif
495932ded4bSAndre Przywara 	}
496932ded4bSAndre Przywara 
497932ded4bSAndre Przywara 	return 0;
498932ded4bSAndre Przywara }
499