xref: /openbmc/linux/arch/arm64/kernel/module.c (revision f930896967fa3f9ab16a6f87267b92798308d48f)
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 {
3539d114ddSAndrey Ryabinin 	void *p;
3639d114ddSAndrey Ryabinin 
3739d114ddSAndrey Ryabinin 	p = __vmalloc_node_range(size, MODULE_ALIGN, MODULES_VADDR, MODULES_END,
38cb9e3c29SAndrey Ryabinin 				GFP_KERNEL, PAGE_KERNEL_EXEC, 0,
39cb9e3c29SAndrey Ryabinin 				NUMA_NO_NODE, __builtin_return_address(0));
4039d114ddSAndrey Ryabinin 
4139d114ddSAndrey Ryabinin 	if (p && (kasan_module_alloc(p, size) < 0)) {
4239d114ddSAndrey Ryabinin 		vfree(p);
4339d114ddSAndrey Ryabinin 		return NULL;
4439d114ddSAndrey Ryabinin 	}
4539d114ddSAndrey Ryabinin 
4639d114ddSAndrey Ryabinin 	return p;
47257cb251SWill Deacon }
48257cb251SWill Deacon 
49257cb251SWill Deacon enum aarch64_reloc_op {
50257cb251SWill Deacon 	RELOC_OP_NONE,
51257cb251SWill Deacon 	RELOC_OP_ABS,
52257cb251SWill Deacon 	RELOC_OP_PREL,
53257cb251SWill Deacon 	RELOC_OP_PAGE,
54257cb251SWill Deacon };
55257cb251SWill Deacon 
56257cb251SWill Deacon static u64 do_reloc(enum aarch64_reloc_op reloc_op, void *place, u64 val)
57257cb251SWill Deacon {
58257cb251SWill Deacon 	switch (reloc_op) {
59257cb251SWill Deacon 	case RELOC_OP_ABS:
60257cb251SWill Deacon 		return val;
61257cb251SWill Deacon 	case RELOC_OP_PREL:
62257cb251SWill Deacon 		return val - (u64)place;
63257cb251SWill Deacon 	case RELOC_OP_PAGE:
64257cb251SWill Deacon 		return (val & ~0xfff) - ((u64)place & ~0xfff);
65257cb251SWill Deacon 	case RELOC_OP_NONE:
66257cb251SWill Deacon 		return 0;
67257cb251SWill Deacon 	}
68257cb251SWill Deacon 
69257cb251SWill Deacon 	pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
70257cb251SWill Deacon 	return 0;
71257cb251SWill Deacon }
72257cb251SWill Deacon 
73257cb251SWill Deacon static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
74257cb251SWill Deacon {
75257cb251SWill Deacon 	s64 sval = do_reloc(op, place, val);
76257cb251SWill Deacon 
77257cb251SWill Deacon 	switch (len) {
78257cb251SWill Deacon 	case 16:
79257cb251SWill Deacon 		*(s16 *)place = sval;
80*f9308969SArd Biesheuvel 		if (sval < S16_MIN || sval > U16_MAX)
81*f9308969SArd Biesheuvel 			return -ERANGE;
82257cb251SWill Deacon 		break;
83257cb251SWill Deacon 	case 32:
84257cb251SWill Deacon 		*(s32 *)place = sval;
85*f9308969SArd Biesheuvel 		if (sval < S32_MIN || sval > U32_MAX)
86*f9308969SArd Biesheuvel 			return -ERANGE;
87257cb251SWill Deacon 		break;
88257cb251SWill Deacon 	case 64:
89257cb251SWill Deacon 		*(s64 *)place = sval;
90257cb251SWill Deacon 		break;
91257cb251SWill Deacon 	default:
92257cb251SWill Deacon 		pr_err("Invalid length (%d) for data relocation\n", len);
93257cb251SWill Deacon 		return 0;
94257cb251SWill Deacon 	}
95257cb251SWill Deacon 	return 0;
96257cb251SWill Deacon }
97257cb251SWill Deacon 
98b24a5575SArd Biesheuvel enum aarch64_insn_movw_imm_type {
99b24a5575SArd Biesheuvel 	AARCH64_INSN_IMM_MOVNZ,
100b24a5575SArd Biesheuvel 	AARCH64_INSN_IMM_MOVKZ,
101b24a5575SArd Biesheuvel };
102b24a5575SArd Biesheuvel 
103c84fced8SJiang Liu static int reloc_insn_movw(enum aarch64_reloc_op op, void *place, u64 val,
104b24a5575SArd Biesheuvel 			   int lsb, enum aarch64_insn_movw_imm_type imm_type)
105257cb251SWill Deacon {
106b24a5575SArd Biesheuvel 	u64 imm;
107c84fced8SJiang Liu 	s64 sval;
108c84fced8SJiang Liu 	u32 insn = le32_to_cpu(*(u32 *)place);
109257cb251SWill Deacon 
110c84fced8SJiang Liu 	sval = do_reloc(op, place, val);
111b24a5575SArd Biesheuvel 	imm = sval >> lsb;
112122e2fa0SWill Deacon 
113c84fced8SJiang Liu 	if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
114257cb251SWill Deacon 		/*
115257cb251SWill Deacon 		 * For signed MOVW relocations, we have to manipulate the
116257cb251SWill Deacon 		 * instruction encoding depending on whether or not the
117257cb251SWill Deacon 		 * immediate is less than zero.
118257cb251SWill Deacon 		 */
119257cb251SWill Deacon 		insn &= ~(3 << 29);
120b24a5575SArd Biesheuvel 		if (sval >= 0) {
121257cb251SWill Deacon 			/* >=0: Set the instruction to MOVZ (opcode 10b). */
122257cb251SWill Deacon 			insn |= 2 << 29;
123257cb251SWill Deacon 		} else {
124257cb251SWill Deacon 			/*
125257cb251SWill Deacon 			 * <0: Set the instruction to MOVN (opcode 00b).
126257cb251SWill Deacon 			 *     Since we've masked the opcode already, we
127257cb251SWill Deacon 			 *     don't need to do anything other than
128257cb251SWill Deacon 			 *     inverting the new immediate field.
129257cb251SWill Deacon 			 */
130257cb251SWill Deacon 			imm = ~imm;
131257cb251SWill Deacon 		}
132257cb251SWill Deacon 	}
133257cb251SWill Deacon 
134257cb251SWill Deacon 	/* Update the instruction with the new encoding. */
135b24a5575SArd Biesheuvel 	insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm);
136c84fced8SJiang Liu 	*(u32 *)place = cpu_to_le32(insn);
137257cb251SWill Deacon 
138b24a5575SArd Biesheuvel 	if (imm > U16_MAX)
139257cb251SWill Deacon 		return -ERANGE;
140257cb251SWill Deacon 
141257cb251SWill Deacon 	return 0;
142257cb251SWill Deacon }
143257cb251SWill Deacon 
144257cb251SWill Deacon static int reloc_insn_imm(enum aarch64_reloc_op op, void *place, u64 val,
145c84fced8SJiang Liu 			  int lsb, int len, enum aarch64_insn_imm_type imm_type)
146257cb251SWill Deacon {
147257cb251SWill Deacon 	u64 imm, imm_mask;
148257cb251SWill Deacon 	s64 sval;
149c84fced8SJiang Liu 	u32 insn = le32_to_cpu(*(u32 *)place);
150257cb251SWill Deacon 
151257cb251SWill Deacon 	/* Calculate the relocation value. */
152257cb251SWill Deacon 	sval = do_reloc(op, place, val);
153257cb251SWill Deacon 	sval >>= lsb;
154257cb251SWill Deacon 
155257cb251SWill Deacon 	/* Extract the value bits and shift them to bit 0. */
156257cb251SWill Deacon 	imm_mask = (BIT(lsb + len) - 1) >> lsb;
157257cb251SWill Deacon 	imm = sval & imm_mask;
158257cb251SWill Deacon 
159257cb251SWill Deacon 	/* Update the instruction's immediate field. */
160c84fced8SJiang Liu 	insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
161c84fced8SJiang Liu 	*(u32 *)place = cpu_to_le32(insn);
162257cb251SWill Deacon 
163257cb251SWill Deacon 	/*
164257cb251SWill Deacon 	 * Extract the upper value bits (including the sign bit) and
165257cb251SWill Deacon 	 * shift them to bit 0.
166257cb251SWill Deacon 	 */
167257cb251SWill Deacon 	sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
168257cb251SWill Deacon 
169257cb251SWill Deacon 	/*
170257cb251SWill Deacon 	 * Overflow has occurred if the upper bits are not all equal to
171257cb251SWill Deacon 	 * the sign bit of the value.
172257cb251SWill Deacon 	 */
173257cb251SWill Deacon 	if ((u64)(sval + 1) >= 2)
174257cb251SWill Deacon 		return -ERANGE;
175257cb251SWill Deacon 
176257cb251SWill Deacon 	return 0;
177257cb251SWill Deacon }
178257cb251SWill Deacon 
179257cb251SWill Deacon int apply_relocate_add(Elf64_Shdr *sechdrs,
180257cb251SWill Deacon 		       const char *strtab,
181257cb251SWill Deacon 		       unsigned int symindex,
182257cb251SWill Deacon 		       unsigned int relsec,
183257cb251SWill Deacon 		       struct module *me)
184257cb251SWill Deacon {
185257cb251SWill Deacon 	unsigned int i;
186257cb251SWill Deacon 	int ovf;
187257cb251SWill Deacon 	bool overflow_check;
188257cb251SWill Deacon 	Elf64_Sym *sym;
189257cb251SWill Deacon 	void *loc;
190257cb251SWill Deacon 	u64 val;
191257cb251SWill Deacon 	Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
192257cb251SWill Deacon 
193257cb251SWill Deacon 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
194257cb251SWill Deacon 		/* loc corresponds to P in the AArch64 ELF document. */
195257cb251SWill Deacon 		loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
196257cb251SWill Deacon 			+ rel[i].r_offset;
197257cb251SWill Deacon 
198257cb251SWill Deacon 		/* sym is the ELF symbol we're referring to. */
199257cb251SWill Deacon 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
200257cb251SWill Deacon 			+ ELF64_R_SYM(rel[i].r_info);
201257cb251SWill Deacon 
202257cb251SWill Deacon 		/* val corresponds to (S + A) in the AArch64 ELF document. */
203257cb251SWill Deacon 		val = sym->st_value + rel[i].r_addend;
204257cb251SWill Deacon 
205257cb251SWill Deacon 		/* Check for overflow by default. */
206257cb251SWill Deacon 		overflow_check = true;
207257cb251SWill Deacon 
208257cb251SWill Deacon 		/* Perform the static relocation. */
209257cb251SWill Deacon 		switch (ELF64_R_TYPE(rel[i].r_info)) {
210257cb251SWill Deacon 		/* Null relocations. */
211257cb251SWill Deacon 		case R_ARM_NONE:
212257cb251SWill Deacon 		case R_AARCH64_NONE:
213257cb251SWill Deacon 			ovf = 0;
214257cb251SWill Deacon 			break;
215257cb251SWill Deacon 
216257cb251SWill Deacon 		/* Data relocations. */
217257cb251SWill Deacon 		case R_AARCH64_ABS64:
218257cb251SWill Deacon 			overflow_check = false;
219257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
220257cb251SWill Deacon 			break;
221257cb251SWill Deacon 		case R_AARCH64_ABS32:
222257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
223257cb251SWill Deacon 			break;
224257cb251SWill Deacon 		case R_AARCH64_ABS16:
225257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
226257cb251SWill Deacon 			break;
227257cb251SWill Deacon 		case R_AARCH64_PREL64:
228257cb251SWill Deacon 			overflow_check = false;
229257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
230257cb251SWill Deacon 			break;
231257cb251SWill Deacon 		case R_AARCH64_PREL32:
232257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
233257cb251SWill Deacon 			break;
234257cb251SWill Deacon 		case R_AARCH64_PREL16:
235257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
236257cb251SWill Deacon 			break;
237257cb251SWill Deacon 
238257cb251SWill Deacon 		/* MOVW instruction relocations. */
239257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0_NC:
240257cb251SWill Deacon 			overflow_check = false;
241257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0:
242257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
243b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
244257cb251SWill Deacon 			break;
245257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1_NC:
246257cb251SWill Deacon 			overflow_check = false;
247257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1:
248257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
249b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
250257cb251SWill Deacon 			break;
251257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2_NC:
252257cb251SWill Deacon 			overflow_check = false;
253257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2:
254257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
255b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
256257cb251SWill Deacon 			break;
257257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G3:
258257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
259257cb251SWill Deacon 			overflow_check = false;
260257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
261b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
262257cb251SWill Deacon 			break;
263257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G0:
264257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
265c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
266257cb251SWill Deacon 			break;
267257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G1:
268257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
269c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
270257cb251SWill Deacon 			break;
271257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G2:
272257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
273c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
274257cb251SWill Deacon 			break;
275257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0_NC:
276257cb251SWill Deacon 			overflow_check = false;
277257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
278b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
279257cb251SWill Deacon 			break;
280257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0:
281257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
282c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
283257cb251SWill Deacon 			break;
284257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1_NC:
285257cb251SWill Deacon 			overflow_check = false;
286257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
287b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
288257cb251SWill Deacon 			break;
289257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1:
290257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
291c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
292257cb251SWill Deacon 			break;
293257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2_NC:
294257cb251SWill Deacon 			overflow_check = false;
295257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
296b24a5575SArd Biesheuvel 					      AARCH64_INSN_IMM_MOVKZ);
297257cb251SWill Deacon 			break;
298257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2:
299257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
300c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
301257cb251SWill Deacon 			break;
302257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G3:
303257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
304257cb251SWill Deacon 			overflow_check = false;
305257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48,
306c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
307257cb251SWill Deacon 			break;
308257cb251SWill Deacon 
309257cb251SWill Deacon 		/* Immediate instruction relocations. */
310257cb251SWill Deacon 		case R_AARCH64_LD_PREL_LO19:
311257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
312c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
313257cb251SWill Deacon 			break;
314257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_LO21:
315257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
316c84fced8SJiang Liu 					     AARCH64_INSN_IMM_ADR);
317257cb251SWill Deacon 			break;
318df057cc7SWill Deacon #ifndef CONFIG_ARM64_ERRATUM_843419
319257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21_NC:
320257cb251SWill Deacon 			overflow_check = false;
321257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21:
322257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PAGE, loc, val, 12, 21,
323c84fced8SJiang Liu 					     AARCH64_INSN_IMM_ADR);
324257cb251SWill Deacon 			break;
325df057cc7SWill Deacon #endif
326257cb251SWill Deacon 		case R_AARCH64_ADD_ABS_LO12_NC:
327257cb251SWill Deacon 		case R_AARCH64_LDST8_ABS_LO12_NC:
328257cb251SWill Deacon 			overflow_check = false;
329257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
330c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
331257cb251SWill Deacon 			break;
332257cb251SWill Deacon 		case R_AARCH64_LDST16_ABS_LO12_NC:
333257cb251SWill Deacon 			overflow_check = false;
334257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
335c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
336257cb251SWill Deacon 			break;
337257cb251SWill Deacon 		case R_AARCH64_LDST32_ABS_LO12_NC:
338257cb251SWill Deacon 			overflow_check = false;
339257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
340c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
341257cb251SWill Deacon 			break;
342257cb251SWill Deacon 		case R_AARCH64_LDST64_ABS_LO12_NC:
343257cb251SWill Deacon 			overflow_check = false;
344257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
345c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
346257cb251SWill Deacon 			break;
347257cb251SWill Deacon 		case R_AARCH64_LDST128_ABS_LO12_NC:
348257cb251SWill Deacon 			overflow_check = false;
349257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
350c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
351257cb251SWill Deacon 			break;
352257cb251SWill Deacon 		case R_AARCH64_TSTBR14:
353257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
354c84fced8SJiang Liu 					     AARCH64_INSN_IMM_14);
355257cb251SWill Deacon 			break;
356257cb251SWill Deacon 		case R_AARCH64_CONDBR19:
357257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
358c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
359257cb251SWill Deacon 			break;
360257cb251SWill Deacon 		case R_AARCH64_JUMP26:
361257cb251SWill Deacon 		case R_AARCH64_CALL26:
362257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
363c84fced8SJiang Liu 					     AARCH64_INSN_IMM_26);
364257cb251SWill Deacon 			break;
365257cb251SWill Deacon 
366257cb251SWill Deacon 		default:
367257cb251SWill Deacon 			pr_err("module %s: unsupported RELA relocation: %llu\n",
368257cb251SWill Deacon 			       me->name, ELF64_R_TYPE(rel[i].r_info));
369257cb251SWill Deacon 			return -ENOEXEC;
370257cb251SWill Deacon 		}
371257cb251SWill Deacon 
372257cb251SWill Deacon 		if (overflow_check && ovf == -ERANGE)
373257cb251SWill Deacon 			goto overflow;
374257cb251SWill Deacon 
375257cb251SWill Deacon 	}
376257cb251SWill Deacon 
377257cb251SWill Deacon 	return 0;
378257cb251SWill Deacon 
379257cb251SWill Deacon overflow:
380257cb251SWill Deacon 	pr_err("module %s: overflow in relocation type %d val %Lx\n",
381257cb251SWill Deacon 	       me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
382257cb251SWill Deacon 	return -ENOEXEC;
383257cb251SWill Deacon }
384932ded4bSAndre Przywara 
385932ded4bSAndre Przywara int module_finalize(const Elf_Ehdr *hdr,
386932ded4bSAndre Przywara 		    const Elf_Shdr *sechdrs,
387932ded4bSAndre Przywara 		    struct module *me)
388932ded4bSAndre Przywara {
389932ded4bSAndre Przywara 	const Elf_Shdr *s, *se;
390932ded4bSAndre Przywara 	const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
391932ded4bSAndre Przywara 
392932ded4bSAndre Przywara 	for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
393932ded4bSAndre Przywara 		if (strcmp(".altinstructions", secstrs + s->sh_name) == 0) {
394932ded4bSAndre Przywara 			apply_alternatives((void *)s->sh_addr, s->sh_size);
395932ded4bSAndre Przywara 			return 0;
396932ded4bSAndre Przywara 		}
397932ded4bSAndre Przywara 	}
398932ded4bSAndre Przywara 
399932ded4bSAndre Przywara 	return 0;
400932ded4bSAndre Przywara }
401