xref: /openbmc/linux/arch/arm64/kernel/module.c (revision 1cf24a2cc3fd40942b0f9e6199aaec579e89a832)
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 {
350c2cf6d9SFlorian Fainelli 	gfp_t gfp_mask = GFP_KERNEL;
3639d114ddSAndrey Ryabinin 	void *p;
3739d114ddSAndrey Ryabinin 
380c2cf6d9SFlorian Fainelli 	/* Silence the initial allocation */
390c2cf6d9SFlorian Fainelli 	if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
400c2cf6d9SFlorian Fainelli 		gfp_mask |= __GFP_NOWARN;
410c2cf6d9SFlorian Fainelli 
42f80fb3a3SArd Biesheuvel 	p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base,
43f80fb3a3SArd Biesheuvel 				module_alloc_base + MODULES_VSIZE,
440c2cf6d9SFlorian Fainelli 				gfp_mask, PAGE_KERNEL_EXEC, 0,
45cb9e3c29SAndrey Ryabinin 				NUMA_NO_NODE, __builtin_return_address(0));
4639d114ddSAndrey Ryabinin 
47fd045f6cSArd Biesheuvel 	if (!p && IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
48fd045f6cSArd Biesheuvel 	    !IS_ENABLED(CONFIG_KASAN))
49fd045f6cSArd Biesheuvel 		/*
50fd045f6cSArd Biesheuvel 		 * KASAN can only deal with module allocations being served
51fd045f6cSArd Biesheuvel 		 * from the reserved module region, since the remainder of
52fd045f6cSArd Biesheuvel 		 * the vmalloc region is already backed by zero shadow pages,
53fd045f6cSArd Biesheuvel 		 * and punching holes into it is non-trivial. Since the module
54fd045f6cSArd Biesheuvel 		 * region is not randomized when KASAN is enabled, 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,
60f2b9ba87SArd Biesheuvel 				PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
61f2b9ba87SArd Biesheuvel 				__builtin_return_address(0));
62fd045f6cSArd Biesheuvel 
6339d114ddSAndrey Ryabinin 	if (p && (kasan_module_alloc(p, size) < 0)) {
6439d114ddSAndrey Ryabinin 		vfree(p);
6539d114ddSAndrey Ryabinin 		return NULL;
6639d114ddSAndrey Ryabinin 	}
6739d114ddSAndrey Ryabinin 
6839d114ddSAndrey Ryabinin 	return p;
69257cb251SWill Deacon }
70257cb251SWill Deacon 
71257cb251SWill Deacon enum aarch64_reloc_op {
72257cb251SWill Deacon 	RELOC_OP_NONE,
73257cb251SWill Deacon 	RELOC_OP_ABS,
74257cb251SWill Deacon 	RELOC_OP_PREL,
75257cb251SWill Deacon 	RELOC_OP_PAGE,
76257cb251SWill Deacon };
77257cb251SWill Deacon 
7802129ae5SLuc Van Oostenryck static u64 do_reloc(enum aarch64_reloc_op reloc_op, __le32 *place, u64 val)
79257cb251SWill Deacon {
80257cb251SWill Deacon 	switch (reloc_op) {
81257cb251SWill Deacon 	case RELOC_OP_ABS:
82257cb251SWill Deacon 		return val;
83257cb251SWill Deacon 	case RELOC_OP_PREL:
84257cb251SWill Deacon 		return val - (u64)place;
85257cb251SWill Deacon 	case RELOC_OP_PAGE:
86257cb251SWill Deacon 		return (val & ~0xfff) - ((u64)place & ~0xfff);
87257cb251SWill Deacon 	case RELOC_OP_NONE:
88257cb251SWill Deacon 		return 0;
89257cb251SWill Deacon 	}
90257cb251SWill Deacon 
91257cb251SWill Deacon 	pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
92257cb251SWill Deacon 	return 0;
93257cb251SWill Deacon }
94257cb251SWill Deacon 
95257cb251SWill Deacon static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
96257cb251SWill Deacon {
97257cb251SWill Deacon 	s64 sval = do_reloc(op, place, val);
98257cb251SWill Deacon 
99*1cf24a2cSArd Biesheuvel 	/*
100*1cf24a2cSArd Biesheuvel 	 * The ELF psABI for AArch64 documents the 16-bit and 32-bit place
101*1cf24a2cSArd Biesheuvel 	 * relative relocations as having a range of [-2^15, 2^16) or
102*1cf24a2cSArd Biesheuvel 	 * [-2^31, 2^32), respectively. However, in order to be able to detect
103*1cf24a2cSArd Biesheuvel 	 * overflows reliably, we have to choose whether we interpret such
104*1cf24a2cSArd Biesheuvel 	 * quantities as signed or as unsigned, and stick with it.
105*1cf24a2cSArd Biesheuvel 	 * The way we organize our address space requires a signed
106*1cf24a2cSArd Biesheuvel 	 * interpretation of 32-bit relative references, so let's use that
107*1cf24a2cSArd Biesheuvel 	 * for all R_AARCH64_PRELxx relocations. This means our upper
108*1cf24a2cSArd Biesheuvel 	 * bound for overflow detection should be Sxx_MAX rather than Uxx_MAX.
109*1cf24a2cSArd Biesheuvel 	 */
110*1cf24a2cSArd Biesheuvel 
111257cb251SWill Deacon 	switch (len) {
112257cb251SWill Deacon 	case 16:
113257cb251SWill Deacon 		*(s16 *)place = sval;
114*1cf24a2cSArd Biesheuvel 		if (sval < S16_MIN || sval > S16_MAX)
115f9308969SArd Biesheuvel 			return -ERANGE;
116257cb251SWill Deacon 		break;
117257cb251SWill Deacon 	case 32:
118257cb251SWill Deacon 		*(s32 *)place = sval;
119*1cf24a2cSArd Biesheuvel 		if (sval < S32_MIN || sval > S32_MAX)
120f9308969SArd Biesheuvel 			return -ERANGE;
121257cb251SWill Deacon 		break;
122257cb251SWill Deacon 	case 64:
123257cb251SWill Deacon 		*(s64 *)place = sval;
124257cb251SWill Deacon 		break;
125257cb251SWill Deacon 	default:
126257cb251SWill Deacon 		pr_err("Invalid length (%d) for data relocation\n", len);
127257cb251SWill Deacon 		return 0;
128257cb251SWill Deacon 	}
129257cb251SWill Deacon 	return 0;
130257cb251SWill Deacon }
131257cb251SWill Deacon 
132b24a5575SArd Biesheuvel enum aarch64_insn_movw_imm_type {
133b24a5575SArd Biesheuvel 	AARCH64_INSN_IMM_MOVNZ,
134b24a5575SArd Biesheuvel 	AARCH64_INSN_IMM_MOVKZ,
135b24a5575SArd Biesheuvel };
136b24a5575SArd Biesheuvel 
13702129ae5SLuc Van Oostenryck static int reloc_insn_movw(enum aarch64_reloc_op op, __le32 *place, u64 val,
138b24a5575SArd Biesheuvel 			   int lsb, enum aarch64_insn_movw_imm_type imm_type)
139257cb251SWill Deacon {
140b24a5575SArd Biesheuvel 	u64 imm;
141c84fced8SJiang Liu 	s64 sval;
14202129ae5SLuc Van Oostenryck 	u32 insn = le32_to_cpu(*place);
143257cb251SWill Deacon 
144c84fced8SJiang Liu 	sval = do_reloc(op, place, val);
145b24a5575SArd Biesheuvel 	imm = sval >> lsb;
146122e2fa0SWill Deacon 
147c84fced8SJiang Liu 	if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
148257cb251SWill Deacon 		/*
149257cb251SWill Deacon 		 * For signed MOVW relocations, we have to manipulate the
150257cb251SWill Deacon 		 * instruction encoding depending on whether or not the
151257cb251SWill Deacon 		 * immediate is less than zero.
152257cb251SWill Deacon 		 */
153257cb251SWill Deacon 		insn &= ~(3 << 29);
154b24a5575SArd Biesheuvel 		if (sval >= 0) {
155257cb251SWill Deacon 			/* >=0: Set the instruction to MOVZ (opcode 10b). */
156257cb251SWill Deacon 			insn |= 2 << 29;
157257cb251SWill Deacon 		} else {
158257cb251SWill Deacon 			/*
159257cb251SWill Deacon 			 * <0: Set the instruction to MOVN (opcode 00b).
160257cb251SWill Deacon 			 *     Since we've masked the opcode already, we
161257cb251SWill Deacon 			 *     don't need to do anything other than
162257cb251SWill Deacon 			 *     inverting the new immediate field.
163257cb251SWill Deacon 			 */
164257cb251SWill Deacon 			imm = ~imm;
165257cb251SWill Deacon 		}
166257cb251SWill Deacon 	}
167257cb251SWill Deacon 
168257cb251SWill Deacon 	/* Update the instruction with the new encoding. */
169b24a5575SArd Biesheuvel 	insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm);
17002129ae5SLuc Van Oostenryck 	*place = cpu_to_le32(insn);
171257cb251SWill Deacon 
172b24a5575SArd Biesheuvel 	if (imm > U16_MAX)
173257cb251SWill Deacon 		return -ERANGE;
174257cb251SWill Deacon 
175257cb251SWill Deacon 	return 0;
176257cb251SWill Deacon }
177257cb251SWill Deacon 
17802129ae5SLuc Van Oostenryck static int reloc_insn_imm(enum aarch64_reloc_op op, __le32 *place, u64 val,
179c84fced8SJiang Liu 			  int lsb, int len, enum aarch64_insn_imm_type imm_type)
180257cb251SWill Deacon {
181257cb251SWill Deacon 	u64 imm, imm_mask;
182257cb251SWill Deacon 	s64 sval;
18302129ae5SLuc Van Oostenryck 	u32 insn = le32_to_cpu(*place);
184257cb251SWill Deacon 
185257cb251SWill Deacon 	/* Calculate the relocation value. */
186257cb251SWill Deacon 	sval = do_reloc(op, place, val);
187257cb251SWill Deacon 	sval >>= lsb;
188257cb251SWill Deacon 
189257cb251SWill Deacon 	/* Extract the value bits and shift them to bit 0. */
190257cb251SWill Deacon 	imm_mask = (BIT(lsb + len) - 1) >> lsb;
191257cb251SWill Deacon 	imm = sval & imm_mask;
192257cb251SWill Deacon 
193257cb251SWill Deacon 	/* Update the instruction's immediate field. */
194c84fced8SJiang Liu 	insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
19502129ae5SLuc Van Oostenryck 	*place = cpu_to_le32(insn);
196257cb251SWill Deacon 
197257cb251SWill Deacon 	/*
198257cb251SWill Deacon 	 * Extract the upper value bits (including the sign bit) and
199257cb251SWill Deacon 	 * shift them to bit 0.
200257cb251SWill Deacon 	 */
201257cb251SWill Deacon 	sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
202257cb251SWill Deacon 
203257cb251SWill Deacon 	/*
204257cb251SWill Deacon 	 * Overflow has occurred if the upper bits are not all equal to
205257cb251SWill Deacon 	 * the sign bit of the value.
206257cb251SWill Deacon 	 */
207257cb251SWill Deacon 	if ((u64)(sval + 1) >= 2)
208257cb251SWill Deacon 		return -ERANGE;
209257cb251SWill Deacon 
210257cb251SWill Deacon 	return 0;
211257cb251SWill Deacon }
212257cb251SWill Deacon 
213c8ebf64eSJessica Yu static int reloc_insn_adrp(struct module *mod, Elf64_Shdr *sechdrs,
214c8ebf64eSJessica Yu 			   __le32 *place, u64 val)
215a257e025SArd Biesheuvel {
216a257e025SArd Biesheuvel 	u32 insn;
217a257e025SArd Biesheuvel 
218bdb85cd1SArd Biesheuvel 	if (!is_forbidden_offset_for_adrp(place))
219a257e025SArd Biesheuvel 		return reloc_insn_imm(RELOC_OP_PAGE, place, val, 12, 21,
220a257e025SArd Biesheuvel 				      AARCH64_INSN_IMM_ADR);
221a257e025SArd Biesheuvel 
222a257e025SArd Biesheuvel 	/* patch ADRP to ADR if it is in range */
223a257e025SArd Biesheuvel 	if (!reloc_insn_imm(RELOC_OP_PREL, place, val & ~0xfff, 0, 21,
224a257e025SArd Biesheuvel 			    AARCH64_INSN_IMM_ADR)) {
225a257e025SArd Biesheuvel 		insn = le32_to_cpu(*place);
226a257e025SArd Biesheuvel 		insn &= ~BIT(31);
227a257e025SArd Biesheuvel 	} else {
228a257e025SArd Biesheuvel 		/* out of range for ADR -> emit a veneer */
229c8ebf64eSJessica Yu 		val = module_emit_veneer_for_adrp(mod, sechdrs, place, val & ~0xfff);
230a257e025SArd Biesheuvel 		if (!val)
231a257e025SArd Biesheuvel 			return -ENOEXEC;
232a257e025SArd Biesheuvel 		insn = aarch64_insn_gen_branch_imm((u64)place, val,
233a257e025SArd Biesheuvel 						   AARCH64_INSN_BRANCH_NOLINK);
234a257e025SArd Biesheuvel 	}
235a257e025SArd Biesheuvel 
236a257e025SArd Biesheuvel 	*place = cpu_to_le32(insn);
237a257e025SArd Biesheuvel 	return 0;
238a257e025SArd Biesheuvel }
239a257e025SArd Biesheuvel 
240257cb251SWill Deacon int apply_relocate_add(Elf64_Shdr *sechdrs,
241257cb251SWill Deacon 		       const char *strtab,
242257cb251SWill Deacon 		       unsigned int symindex,
243257cb251SWill Deacon 		       unsigned int relsec,
244257cb251SWill Deacon 		       struct module *me)
245257cb251SWill Deacon {
246257cb251SWill Deacon 	unsigned int i;
247257cb251SWill Deacon 	int ovf;
248257cb251SWill Deacon 	bool overflow_check;
249257cb251SWill Deacon 	Elf64_Sym *sym;
250257cb251SWill Deacon 	void *loc;
251257cb251SWill Deacon 	u64 val;
252257cb251SWill Deacon 	Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
253257cb251SWill Deacon 
254257cb251SWill Deacon 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
255257cb251SWill Deacon 		/* loc corresponds to P in the AArch64 ELF document. */
256257cb251SWill Deacon 		loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
257257cb251SWill Deacon 			+ rel[i].r_offset;
258257cb251SWill Deacon 
259257cb251SWill Deacon 		/* sym is the ELF symbol we're referring to. */
260257cb251SWill Deacon 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
261257cb251SWill Deacon 			+ ELF64_R_SYM(rel[i].r_info);
262257cb251SWill Deacon 
263257cb251SWill Deacon 		/* val corresponds to (S + A) in the AArch64 ELF document. */
264257cb251SWill Deacon 		val = sym->st_value + rel[i].r_addend;
265257cb251SWill Deacon 
266257cb251SWill Deacon 		/* Check for overflow by default. */
267257cb251SWill Deacon 		overflow_check = true;
268257cb251SWill Deacon 
269257cb251SWill Deacon 		/* Perform the static relocation. */
270257cb251SWill Deacon 		switch (ELF64_R_TYPE(rel[i].r_info)) {
271257cb251SWill Deacon 		/* Null relocations. */
272257cb251SWill Deacon 		case R_ARM_NONE:
273257cb251SWill Deacon 		case R_AARCH64_NONE:
274257cb251SWill Deacon 			ovf = 0;
275257cb251SWill Deacon 			break;
276257cb251SWill Deacon 
277257cb251SWill Deacon 		/* Data relocations. */
278257cb251SWill Deacon 		case R_AARCH64_ABS64:
279257cb251SWill Deacon 			overflow_check = false;
280257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
281257cb251SWill Deacon 			break;
282257cb251SWill Deacon 		case R_AARCH64_ABS32:
283257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
284257cb251SWill Deacon 			break;
285257cb251SWill Deacon 		case R_AARCH64_ABS16:
286257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
287257cb251SWill Deacon 			break;
288257cb251SWill Deacon 		case R_AARCH64_PREL64:
289257cb251SWill Deacon 			overflow_check = false;
290257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
291257cb251SWill Deacon 			break;
292257cb251SWill Deacon 		case R_AARCH64_PREL32:
293257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
294257cb251SWill Deacon 			break;
295257cb251SWill Deacon 		case R_AARCH64_PREL16:
296257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
297257cb251SWill Deacon 			break;
298257cb251SWill Deacon 
299257cb251SWill Deacon 		/* MOVW instruction relocations. */
300257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0_NC:
301257cb251SWill Deacon 			overflow_check = false;
302257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0:
303257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
304b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
305257cb251SWill Deacon 			break;
306257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1_NC:
307257cb251SWill Deacon 			overflow_check = false;
308257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1:
309257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
310b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
311257cb251SWill Deacon 			break;
312257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2_NC:
313257cb251SWill Deacon 			overflow_check = false;
314257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2:
315257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
316b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
317257cb251SWill Deacon 			break;
318257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G3:
319257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
320257cb251SWill Deacon 			overflow_check = false;
321257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
322b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
323257cb251SWill Deacon 			break;
324257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G0:
325257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
326c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
327257cb251SWill Deacon 			break;
328257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G1:
329257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
330c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
331257cb251SWill Deacon 			break;
332257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G2:
333257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
334c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
335257cb251SWill Deacon 			break;
336257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0_NC:
337257cb251SWill Deacon 			overflow_check = false;
338257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
339b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
340257cb251SWill Deacon 			break;
341257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0:
342257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
343c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
344257cb251SWill Deacon 			break;
345257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1_NC:
346257cb251SWill Deacon 			overflow_check = false;
347257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
348b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
349257cb251SWill Deacon 			break;
350257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1:
351257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
352c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
353257cb251SWill Deacon 			break;
354257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2_NC:
355257cb251SWill Deacon 			overflow_check = false;
356257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
357b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
358257cb251SWill Deacon 			break;
359257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2:
360257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
361c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
362257cb251SWill Deacon 			break;
363257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G3:
364257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
365257cb251SWill Deacon 			overflow_check = false;
366257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48,
367c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
368257cb251SWill Deacon 			break;
369257cb251SWill Deacon 
370257cb251SWill Deacon 		/* Immediate instruction relocations. */
371257cb251SWill Deacon 		case R_AARCH64_LD_PREL_LO19:
372257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
373c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
374257cb251SWill Deacon 			break;
375257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_LO21:
376257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
377c84fced8SJiang Liu 					     AARCH64_INSN_IMM_ADR);
378257cb251SWill Deacon 			break;
379257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21_NC:
380257cb251SWill Deacon 			overflow_check = false;
381257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21:
382c8ebf64eSJessica Yu 			ovf = reloc_insn_adrp(me, sechdrs, loc, val);
383a257e025SArd Biesheuvel 			if (ovf && ovf != -ERANGE)
384a257e025SArd Biesheuvel 				return ovf;
385257cb251SWill Deacon 			break;
386257cb251SWill Deacon 		case R_AARCH64_ADD_ABS_LO12_NC:
387257cb251SWill Deacon 		case R_AARCH64_LDST8_ABS_LO12_NC:
388257cb251SWill Deacon 			overflow_check = false;
389257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
390c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
391257cb251SWill Deacon 			break;
392257cb251SWill Deacon 		case R_AARCH64_LDST16_ABS_LO12_NC:
393257cb251SWill Deacon 			overflow_check = false;
394257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
395c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
396257cb251SWill Deacon 			break;
397257cb251SWill Deacon 		case R_AARCH64_LDST32_ABS_LO12_NC:
398257cb251SWill Deacon 			overflow_check = false;
399257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
400c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
401257cb251SWill Deacon 			break;
402257cb251SWill Deacon 		case R_AARCH64_LDST64_ABS_LO12_NC:
403257cb251SWill Deacon 			overflow_check = false;
404257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
405c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
406257cb251SWill Deacon 			break;
407257cb251SWill Deacon 		case R_AARCH64_LDST128_ABS_LO12_NC:
408257cb251SWill Deacon 			overflow_check = false;
409257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
410c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
411257cb251SWill Deacon 			break;
412257cb251SWill Deacon 		case R_AARCH64_TSTBR14:
413257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
414c84fced8SJiang Liu 					     AARCH64_INSN_IMM_14);
415257cb251SWill Deacon 			break;
416257cb251SWill Deacon 		case R_AARCH64_CONDBR19:
417257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
418c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
419257cb251SWill Deacon 			break;
420257cb251SWill Deacon 		case R_AARCH64_JUMP26:
421257cb251SWill Deacon 		case R_AARCH64_CALL26:
422257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
423c84fced8SJiang Liu 					     AARCH64_INSN_IMM_26);
424fd045f6cSArd Biesheuvel 
425fd045f6cSArd Biesheuvel 			if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
426fd045f6cSArd Biesheuvel 			    ovf == -ERANGE) {
427c8ebf64eSJessica Yu 				val = module_emit_plt_entry(me, sechdrs, loc, &rel[i], sym);
4285e8307b9SArd Biesheuvel 				if (!val)
4295e8307b9SArd Biesheuvel 					return -ENOEXEC;
430fd045f6cSArd Biesheuvel 				ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2,
431fd045f6cSArd Biesheuvel 						     26, AARCH64_INSN_IMM_26);
432fd045f6cSArd Biesheuvel 			}
433257cb251SWill Deacon 			break;
434257cb251SWill Deacon 
435257cb251SWill Deacon 		default:
436257cb251SWill Deacon 			pr_err("module %s: unsupported RELA relocation: %llu\n",
437257cb251SWill Deacon 			       me->name, ELF64_R_TYPE(rel[i].r_info));
438257cb251SWill Deacon 			return -ENOEXEC;
439257cb251SWill Deacon 		}
440257cb251SWill Deacon 
441257cb251SWill Deacon 		if (overflow_check && ovf == -ERANGE)
442257cb251SWill Deacon 			goto overflow;
443257cb251SWill Deacon 
444257cb251SWill Deacon 	}
445257cb251SWill Deacon 
446257cb251SWill Deacon 	return 0;
447257cb251SWill Deacon 
448257cb251SWill Deacon overflow:
449257cb251SWill Deacon 	pr_err("module %s: overflow in relocation type %d val %Lx\n",
450257cb251SWill Deacon 	       me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
451257cb251SWill Deacon 	return -ENOEXEC;
452257cb251SWill Deacon }
453932ded4bSAndre Przywara 
454932ded4bSAndre Przywara int module_finalize(const Elf_Ehdr *hdr,
455932ded4bSAndre Przywara 		    const Elf_Shdr *sechdrs,
456932ded4bSAndre Przywara 		    struct module *me)
457932ded4bSAndre Przywara {
458932ded4bSAndre Przywara 	const Elf_Shdr *s, *se;
459932ded4bSAndre Przywara 	const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
460932ded4bSAndre Przywara 
461932ded4bSAndre Przywara 	for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
46242938868SWill Deacon 		if (strcmp(".altinstructions", secstrs + s->sh_name) == 0)
46342938868SWill Deacon 			apply_alternatives_module((void *)s->sh_addr, s->sh_size);
464e71a4e1bSArd Biesheuvel #ifdef CONFIG_ARM64_MODULE_PLTS
465e71a4e1bSArd Biesheuvel 		if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE) &&
466e71a4e1bSArd Biesheuvel 		    !strcmp(".text.ftrace_trampoline", secstrs + s->sh_name))
467e71a4e1bSArd Biesheuvel 			me->arch.ftrace_trampoline = (void *)s->sh_addr;
468e71a4e1bSArd Biesheuvel #endif
469932ded4bSAndre Przywara 	}
470932ded4bSAndre Przywara 
471932ded4bSAndre Przywara 	return 0;
472932ded4bSAndre Przywara }
473