xref: /openbmc/linux/arch/arm64/kernel/module.c (revision 0c2cf6d9487cb90be6ad7fac66044dfa8e8e5243)
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*0c2cf6d9SFlorian Fainelli 	gfp_t gfp_mask = GFP_KERNEL;
3639d114ddSAndrey Ryabinin 	void *p;
3739d114ddSAndrey Ryabinin 
38*0c2cf6d9SFlorian Fainelli 	/* Silence the initial allocation */
39*0c2cf6d9SFlorian Fainelli 	if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
40*0c2cf6d9SFlorian Fainelli 		gfp_mask |= __GFP_NOWARN;
41*0c2cf6d9SFlorian Fainelli 
42f80fb3a3SArd Biesheuvel 	p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base,
43f80fb3a3SArd Biesheuvel 				module_alloc_base + MODULES_VSIZE,
44*0c2cf6d9SFlorian 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 		 */
58fd045f6cSArd Biesheuvel 		p = __vmalloc_node_range(size, MODULE_ALIGN, VMALLOC_START,
59fd045f6cSArd Biesheuvel 				VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_EXEC, 0,
60fd045f6cSArd Biesheuvel 				NUMA_NO_NODE, __builtin_return_address(0));
61fd045f6cSArd Biesheuvel 
6239d114ddSAndrey Ryabinin 	if (p && (kasan_module_alloc(p, size) < 0)) {
6339d114ddSAndrey Ryabinin 		vfree(p);
6439d114ddSAndrey Ryabinin 		return NULL;
6539d114ddSAndrey Ryabinin 	}
6639d114ddSAndrey Ryabinin 
6739d114ddSAndrey Ryabinin 	return p;
68257cb251SWill Deacon }
69257cb251SWill Deacon 
70257cb251SWill Deacon enum aarch64_reloc_op {
71257cb251SWill Deacon 	RELOC_OP_NONE,
72257cb251SWill Deacon 	RELOC_OP_ABS,
73257cb251SWill Deacon 	RELOC_OP_PREL,
74257cb251SWill Deacon 	RELOC_OP_PAGE,
75257cb251SWill Deacon };
76257cb251SWill Deacon 
77257cb251SWill Deacon static u64 do_reloc(enum aarch64_reloc_op reloc_op, void *place, u64 val)
78257cb251SWill Deacon {
79257cb251SWill Deacon 	switch (reloc_op) {
80257cb251SWill Deacon 	case RELOC_OP_ABS:
81257cb251SWill Deacon 		return val;
82257cb251SWill Deacon 	case RELOC_OP_PREL:
83257cb251SWill Deacon 		return val - (u64)place;
84257cb251SWill Deacon 	case RELOC_OP_PAGE:
85257cb251SWill Deacon 		return (val & ~0xfff) - ((u64)place & ~0xfff);
86257cb251SWill Deacon 	case RELOC_OP_NONE:
87257cb251SWill Deacon 		return 0;
88257cb251SWill Deacon 	}
89257cb251SWill Deacon 
90257cb251SWill Deacon 	pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
91257cb251SWill Deacon 	return 0;
92257cb251SWill Deacon }
93257cb251SWill Deacon 
94257cb251SWill Deacon static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
95257cb251SWill Deacon {
96257cb251SWill Deacon 	s64 sval = do_reloc(op, place, val);
97257cb251SWill Deacon 
98257cb251SWill Deacon 	switch (len) {
99257cb251SWill Deacon 	case 16:
100257cb251SWill Deacon 		*(s16 *)place = sval;
101f9308969SArd Biesheuvel 		if (sval < S16_MIN || sval > U16_MAX)
102f9308969SArd Biesheuvel 			return -ERANGE;
103257cb251SWill Deacon 		break;
104257cb251SWill Deacon 	case 32:
105257cb251SWill Deacon 		*(s32 *)place = sval;
106f9308969SArd Biesheuvel 		if (sval < S32_MIN || sval > U32_MAX)
107f9308969SArd Biesheuvel 			return -ERANGE;
108257cb251SWill Deacon 		break;
109257cb251SWill Deacon 	case 64:
110257cb251SWill Deacon 		*(s64 *)place = sval;
111257cb251SWill Deacon 		break;
112257cb251SWill Deacon 	default:
113257cb251SWill Deacon 		pr_err("Invalid length (%d) for data relocation\n", len);
114257cb251SWill Deacon 		return 0;
115257cb251SWill Deacon 	}
116257cb251SWill Deacon 	return 0;
117257cb251SWill Deacon }
118257cb251SWill Deacon 
119b24a5575SArd Biesheuvel enum aarch64_insn_movw_imm_type {
120b24a5575SArd Biesheuvel 	AARCH64_INSN_IMM_MOVNZ,
121b24a5575SArd Biesheuvel 	AARCH64_INSN_IMM_MOVKZ,
122b24a5575SArd Biesheuvel };
123b24a5575SArd Biesheuvel 
124c84fced8SJiang Liu static int reloc_insn_movw(enum aarch64_reloc_op op, void *place, u64 val,
125b24a5575SArd Biesheuvel 			   int lsb, enum aarch64_insn_movw_imm_type imm_type)
126257cb251SWill Deacon {
127b24a5575SArd Biesheuvel 	u64 imm;
128c84fced8SJiang Liu 	s64 sval;
129c84fced8SJiang Liu 	u32 insn = le32_to_cpu(*(u32 *)place);
130257cb251SWill Deacon 
131c84fced8SJiang Liu 	sval = do_reloc(op, place, val);
132b24a5575SArd Biesheuvel 	imm = sval >> lsb;
133122e2fa0SWill Deacon 
134c84fced8SJiang Liu 	if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
135257cb251SWill Deacon 		/*
136257cb251SWill Deacon 		 * For signed MOVW relocations, we have to manipulate the
137257cb251SWill Deacon 		 * instruction encoding depending on whether or not the
138257cb251SWill Deacon 		 * immediate is less than zero.
139257cb251SWill Deacon 		 */
140257cb251SWill Deacon 		insn &= ~(3 << 29);
141b24a5575SArd Biesheuvel 		if (sval >= 0) {
142257cb251SWill Deacon 			/* >=0: Set the instruction to MOVZ (opcode 10b). */
143257cb251SWill Deacon 			insn |= 2 << 29;
144257cb251SWill Deacon 		} else {
145257cb251SWill Deacon 			/*
146257cb251SWill Deacon 			 * <0: Set the instruction to MOVN (opcode 00b).
147257cb251SWill Deacon 			 *     Since we've masked the opcode already, we
148257cb251SWill Deacon 			 *     don't need to do anything other than
149257cb251SWill Deacon 			 *     inverting the new immediate field.
150257cb251SWill Deacon 			 */
151257cb251SWill Deacon 			imm = ~imm;
152257cb251SWill Deacon 		}
153257cb251SWill Deacon 	}
154257cb251SWill Deacon 
155257cb251SWill Deacon 	/* Update the instruction with the new encoding. */
156b24a5575SArd Biesheuvel 	insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm);
157c84fced8SJiang Liu 	*(u32 *)place = cpu_to_le32(insn);
158257cb251SWill Deacon 
159b24a5575SArd Biesheuvel 	if (imm > U16_MAX)
160257cb251SWill Deacon 		return -ERANGE;
161257cb251SWill Deacon 
162257cb251SWill Deacon 	return 0;
163257cb251SWill Deacon }
164257cb251SWill Deacon 
165257cb251SWill Deacon static int reloc_insn_imm(enum aarch64_reloc_op op, void *place, u64 val,
166c84fced8SJiang Liu 			  int lsb, int len, enum aarch64_insn_imm_type imm_type)
167257cb251SWill Deacon {
168257cb251SWill Deacon 	u64 imm, imm_mask;
169257cb251SWill Deacon 	s64 sval;
170c84fced8SJiang Liu 	u32 insn = le32_to_cpu(*(u32 *)place);
171257cb251SWill Deacon 
172257cb251SWill Deacon 	/* Calculate the relocation value. */
173257cb251SWill Deacon 	sval = do_reloc(op, place, val);
174257cb251SWill Deacon 	sval >>= lsb;
175257cb251SWill Deacon 
176257cb251SWill Deacon 	/* Extract the value bits and shift them to bit 0. */
177257cb251SWill Deacon 	imm_mask = (BIT(lsb + len) - 1) >> lsb;
178257cb251SWill Deacon 	imm = sval & imm_mask;
179257cb251SWill Deacon 
180257cb251SWill Deacon 	/* Update the instruction's immediate field. */
181c84fced8SJiang Liu 	insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
182c84fced8SJiang Liu 	*(u32 *)place = cpu_to_le32(insn);
183257cb251SWill Deacon 
184257cb251SWill Deacon 	/*
185257cb251SWill Deacon 	 * Extract the upper value bits (including the sign bit) and
186257cb251SWill Deacon 	 * shift them to bit 0.
187257cb251SWill Deacon 	 */
188257cb251SWill Deacon 	sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
189257cb251SWill Deacon 
190257cb251SWill Deacon 	/*
191257cb251SWill Deacon 	 * Overflow has occurred if the upper bits are not all equal to
192257cb251SWill Deacon 	 * the sign bit of the value.
193257cb251SWill Deacon 	 */
194257cb251SWill Deacon 	if ((u64)(sval + 1) >= 2)
195257cb251SWill Deacon 		return -ERANGE;
196257cb251SWill Deacon 
197257cb251SWill Deacon 	return 0;
198257cb251SWill Deacon }
199257cb251SWill Deacon 
200257cb251SWill Deacon int apply_relocate_add(Elf64_Shdr *sechdrs,
201257cb251SWill Deacon 		       const char *strtab,
202257cb251SWill Deacon 		       unsigned int symindex,
203257cb251SWill Deacon 		       unsigned int relsec,
204257cb251SWill Deacon 		       struct module *me)
205257cb251SWill Deacon {
206257cb251SWill Deacon 	unsigned int i;
207257cb251SWill Deacon 	int ovf;
208257cb251SWill Deacon 	bool overflow_check;
209257cb251SWill Deacon 	Elf64_Sym *sym;
210257cb251SWill Deacon 	void *loc;
211257cb251SWill Deacon 	u64 val;
212257cb251SWill Deacon 	Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
213257cb251SWill Deacon 
214257cb251SWill Deacon 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
215257cb251SWill Deacon 		/* loc corresponds to P in the AArch64 ELF document. */
216257cb251SWill Deacon 		loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
217257cb251SWill Deacon 			+ rel[i].r_offset;
218257cb251SWill Deacon 
219257cb251SWill Deacon 		/* sym is the ELF symbol we're referring to. */
220257cb251SWill Deacon 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
221257cb251SWill Deacon 			+ ELF64_R_SYM(rel[i].r_info);
222257cb251SWill Deacon 
223257cb251SWill Deacon 		/* val corresponds to (S + A) in the AArch64 ELF document. */
224257cb251SWill Deacon 		val = sym->st_value + rel[i].r_addend;
225257cb251SWill Deacon 
226257cb251SWill Deacon 		/* Check for overflow by default. */
227257cb251SWill Deacon 		overflow_check = true;
228257cb251SWill Deacon 
229257cb251SWill Deacon 		/* Perform the static relocation. */
230257cb251SWill Deacon 		switch (ELF64_R_TYPE(rel[i].r_info)) {
231257cb251SWill Deacon 		/* Null relocations. */
232257cb251SWill Deacon 		case R_ARM_NONE:
233257cb251SWill Deacon 		case R_AARCH64_NONE:
234257cb251SWill Deacon 			ovf = 0;
235257cb251SWill Deacon 			break;
236257cb251SWill Deacon 
237257cb251SWill Deacon 		/* Data relocations. */
238257cb251SWill Deacon 		case R_AARCH64_ABS64:
239257cb251SWill Deacon 			overflow_check = false;
240257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
241257cb251SWill Deacon 			break;
242257cb251SWill Deacon 		case R_AARCH64_ABS32:
243257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
244257cb251SWill Deacon 			break;
245257cb251SWill Deacon 		case R_AARCH64_ABS16:
246257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
247257cb251SWill Deacon 			break;
248257cb251SWill Deacon 		case R_AARCH64_PREL64:
249257cb251SWill Deacon 			overflow_check = false;
250257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
251257cb251SWill Deacon 			break;
252257cb251SWill Deacon 		case R_AARCH64_PREL32:
253257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
254257cb251SWill Deacon 			break;
255257cb251SWill Deacon 		case R_AARCH64_PREL16:
256257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
257257cb251SWill Deacon 			break;
258257cb251SWill Deacon 
259257cb251SWill Deacon 		/* MOVW instruction relocations. */
260257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0_NC:
261257cb251SWill Deacon 			overflow_check = false;
262257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0:
263257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
264b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
265257cb251SWill Deacon 			break;
266257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1_NC:
267257cb251SWill Deacon 			overflow_check = false;
268257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1:
269257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
270b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
271257cb251SWill Deacon 			break;
272257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2_NC:
273257cb251SWill Deacon 			overflow_check = false;
274257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2:
275257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
276b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
277257cb251SWill Deacon 			break;
278257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G3:
279257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
280257cb251SWill Deacon 			overflow_check = false;
281257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
282b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
283257cb251SWill Deacon 			break;
284257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G0:
285257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
286c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
287257cb251SWill Deacon 			break;
288257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G1:
289257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
290c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
291257cb251SWill Deacon 			break;
292257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G2:
293257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
294c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
295257cb251SWill Deacon 			break;
296257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0_NC:
297257cb251SWill Deacon 			overflow_check = false;
298257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
299b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
300257cb251SWill Deacon 			break;
301257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0:
302257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
303c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
304257cb251SWill Deacon 			break;
305257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1_NC:
306257cb251SWill Deacon 			overflow_check = false;
307257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
308b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
309257cb251SWill Deacon 			break;
310257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1:
311257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
312c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
313257cb251SWill Deacon 			break;
314257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2_NC:
315257cb251SWill Deacon 			overflow_check = false;
316257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
317b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
318257cb251SWill Deacon 			break;
319257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2:
320257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
321c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
322257cb251SWill Deacon 			break;
323257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G3:
324257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
325257cb251SWill Deacon 			overflow_check = false;
326257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48,
327c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
328257cb251SWill Deacon 			break;
329257cb251SWill Deacon 
330257cb251SWill Deacon 		/* Immediate instruction relocations. */
331257cb251SWill Deacon 		case R_AARCH64_LD_PREL_LO19:
332257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
333c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
334257cb251SWill Deacon 			break;
335257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_LO21:
336257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
337c84fced8SJiang Liu 					     AARCH64_INSN_IMM_ADR);
338257cb251SWill Deacon 			break;
339df057cc7SWill Deacon #ifndef CONFIG_ARM64_ERRATUM_843419
340257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21_NC:
341257cb251SWill Deacon 			overflow_check = false;
342257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21:
343257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PAGE, loc, val, 12, 21,
344c84fced8SJiang Liu 					     AARCH64_INSN_IMM_ADR);
345257cb251SWill Deacon 			break;
346df057cc7SWill Deacon #endif
347257cb251SWill Deacon 		case R_AARCH64_ADD_ABS_LO12_NC:
348257cb251SWill Deacon 		case R_AARCH64_LDST8_ABS_LO12_NC:
349257cb251SWill Deacon 			overflow_check = false;
350257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
351c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
352257cb251SWill Deacon 			break;
353257cb251SWill Deacon 		case R_AARCH64_LDST16_ABS_LO12_NC:
354257cb251SWill Deacon 			overflow_check = false;
355257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
356c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
357257cb251SWill Deacon 			break;
358257cb251SWill Deacon 		case R_AARCH64_LDST32_ABS_LO12_NC:
359257cb251SWill Deacon 			overflow_check = false;
360257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
361c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
362257cb251SWill Deacon 			break;
363257cb251SWill Deacon 		case R_AARCH64_LDST64_ABS_LO12_NC:
364257cb251SWill Deacon 			overflow_check = false;
365257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
366c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
367257cb251SWill Deacon 			break;
368257cb251SWill Deacon 		case R_AARCH64_LDST128_ABS_LO12_NC:
369257cb251SWill Deacon 			overflow_check = false;
370257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
371c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
372257cb251SWill Deacon 			break;
373257cb251SWill Deacon 		case R_AARCH64_TSTBR14:
374257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
375c84fced8SJiang Liu 					     AARCH64_INSN_IMM_14);
376257cb251SWill Deacon 			break;
377257cb251SWill Deacon 		case R_AARCH64_CONDBR19:
378257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
379c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
380257cb251SWill Deacon 			break;
381257cb251SWill Deacon 		case R_AARCH64_JUMP26:
382257cb251SWill Deacon 		case R_AARCH64_CALL26:
383257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
384c84fced8SJiang Liu 					     AARCH64_INSN_IMM_26);
385fd045f6cSArd Biesheuvel 
386fd045f6cSArd Biesheuvel 			if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
387fd045f6cSArd Biesheuvel 			    ovf == -ERANGE) {
38824af6c4eSArd Biesheuvel 				val = module_emit_plt_entry(me, loc, &rel[i], sym);
389fd045f6cSArd Biesheuvel 				ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2,
390fd045f6cSArd Biesheuvel 						     26, AARCH64_INSN_IMM_26);
391fd045f6cSArd Biesheuvel 			}
392257cb251SWill Deacon 			break;
393257cb251SWill Deacon 
394257cb251SWill Deacon 		default:
395257cb251SWill Deacon 			pr_err("module %s: unsupported RELA relocation: %llu\n",
396257cb251SWill Deacon 			       me->name, ELF64_R_TYPE(rel[i].r_info));
397257cb251SWill Deacon 			return -ENOEXEC;
398257cb251SWill Deacon 		}
399257cb251SWill Deacon 
400257cb251SWill Deacon 		if (overflow_check && ovf == -ERANGE)
401257cb251SWill Deacon 			goto overflow;
402257cb251SWill Deacon 
403257cb251SWill Deacon 	}
404257cb251SWill Deacon 
405257cb251SWill Deacon 	return 0;
406257cb251SWill Deacon 
407257cb251SWill Deacon overflow:
408257cb251SWill Deacon 	pr_err("module %s: overflow in relocation type %d val %Lx\n",
409257cb251SWill Deacon 	       me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
410257cb251SWill Deacon 	return -ENOEXEC;
411257cb251SWill Deacon }
412932ded4bSAndre Przywara 
413932ded4bSAndre Przywara int module_finalize(const Elf_Ehdr *hdr,
414932ded4bSAndre Przywara 		    const Elf_Shdr *sechdrs,
415932ded4bSAndre Przywara 		    struct module *me)
416932ded4bSAndre Przywara {
417932ded4bSAndre Przywara 	const Elf_Shdr *s, *se;
418932ded4bSAndre Przywara 	const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
419932ded4bSAndre Przywara 
420932ded4bSAndre Przywara 	for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
421932ded4bSAndre Przywara 		if (strcmp(".altinstructions", secstrs + s->sh_name) == 0) {
422932ded4bSAndre Przywara 			apply_alternatives((void *)s->sh_addr, s->sh_size);
423932ded4bSAndre Przywara 			return 0;
424932ded4bSAndre Przywara 		}
425932ded4bSAndre Przywara 	}
426932ded4bSAndre Przywara 
427932ded4bSAndre Przywara 	return 0;
428932ded4bSAndre Przywara }
429