xref: /openbmc/linux/arch/arm64/kernel/module.c (revision 2c2b282d001e9934adeac93c10eb037b81d532f5)
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>
24257cb251SWill Deacon #include <linux/kernel.h>
25257cb251SWill Deacon #include <linux/mm.h>
26257cb251SWill Deacon #include <linux/moduleloader.h>
27257cb251SWill Deacon #include <linux/vmalloc.h>
28*2c2b282dSPaul Walmsley #include <asm/alternative.h>
29c84fced8SJiang Liu #include <asm/insn.h>
30932ded4bSAndre Przywara #include <asm/sections.h>
31c84fced8SJiang Liu 
32c84fced8SJiang Liu #define	AARCH64_INSN_IMM_MOVNZ		AARCH64_INSN_IMM_MAX
33c84fced8SJiang Liu #define	AARCH64_INSN_IMM_MOVK		AARCH64_INSN_IMM_16
34257cb251SWill Deacon 
35257cb251SWill Deacon void *module_alloc(unsigned long size)
36257cb251SWill Deacon {
37257cb251SWill Deacon 	return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
3840c3baa7SJianguo Wu 				    GFP_KERNEL, PAGE_KERNEL_EXEC, NUMA_NO_NODE,
39257cb251SWill Deacon 				    __builtin_return_address(0));
40257cb251SWill Deacon }
41257cb251SWill Deacon 
42257cb251SWill Deacon enum aarch64_reloc_op {
43257cb251SWill Deacon 	RELOC_OP_NONE,
44257cb251SWill Deacon 	RELOC_OP_ABS,
45257cb251SWill Deacon 	RELOC_OP_PREL,
46257cb251SWill Deacon 	RELOC_OP_PAGE,
47257cb251SWill Deacon };
48257cb251SWill Deacon 
49257cb251SWill Deacon static u64 do_reloc(enum aarch64_reloc_op reloc_op, void *place, u64 val)
50257cb251SWill Deacon {
51257cb251SWill Deacon 	switch (reloc_op) {
52257cb251SWill Deacon 	case RELOC_OP_ABS:
53257cb251SWill Deacon 		return val;
54257cb251SWill Deacon 	case RELOC_OP_PREL:
55257cb251SWill Deacon 		return val - (u64)place;
56257cb251SWill Deacon 	case RELOC_OP_PAGE:
57257cb251SWill Deacon 		return (val & ~0xfff) - ((u64)place & ~0xfff);
58257cb251SWill Deacon 	case RELOC_OP_NONE:
59257cb251SWill Deacon 		return 0;
60257cb251SWill Deacon 	}
61257cb251SWill Deacon 
62257cb251SWill Deacon 	pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
63257cb251SWill Deacon 	return 0;
64257cb251SWill Deacon }
65257cb251SWill Deacon 
66257cb251SWill Deacon static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
67257cb251SWill Deacon {
68257cb251SWill Deacon 	u64 imm_mask = (1 << len) - 1;
69257cb251SWill Deacon 	s64 sval = do_reloc(op, place, val);
70257cb251SWill Deacon 
71257cb251SWill Deacon 	switch (len) {
72257cb251SWill Deacon 	case 16:
73257cb251SWill Deacon 		*(s16 *)place = sval;
74257cb251SWill Deacon 		break;
75257cb251SWill Deacon 	case 32:
76257cb251SWill Deacon 		*(s32 *)place = sval;
77257cb251SWill Deacon 		break;
78257cb251SWill Deacon 	case 64:
79257cb251SWill Deacon 		*(s64 *)place = sval;
80257cb251SWill Deacon 		break;
81257cb251SWill Deacon 	default:
82257cb251SWill Deacon 		pr_err("Invalid length (%d) for data relocation\n", len);
83257cb251SWill Deacon 		return 0;
84257cb251SWill Deacon 	}
85257cb251SWill Deacon 
86257cb251SWill Deacon 	/*
87257cb251SWill Deacon 	 * Extract the upper value bits (including the sign bit) and
88257cb251SWill Deacon 	 * shift them to bit 0.
89257cb251SWill Deacon 	 */
90257cb251SWill Deacon 	sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
91257cb251SWill Deacon 
92257cb251SWill Deacon 	/*
93257cb251SWill Deacon 	 * Overflow has occurred if the value is not representable in
94257cb251SWill Deacon 	 * len bits (i.e the bottom len bits are not sign-extended and
95257cb251SWill Deacon 	 * the top bits are not all zero).
96257cb251SWill Deacon 	 */
97257cb251SWill Deacon 	if ((u64)(sval + 1) > 2)
98257cb251SWill Deacon 		return -ERANGE;
99257cb251SWill Deacon 
100257cb251SWill Deacon 	return 0;
101257cb251SWill Deacon }
102257cb251SWill Deacon 
103c84fced8SJiang Liu static int reloc_insn_movw(enum aarch64_reloc_op op, void *place, u64 val,
104c84fced8SJiang Liu 			   int lsb, enum aarch64_insn_imm_type imm_type)
105257cb251SWill Deacon {
106c84fced8SJiang Liu 	u64 imm, limit = 0;
107c84fced8SJiang Liu 	s64 sval;
108c84fced8SJiang Liu 	u32 insn = le32_to_cpu(*(u32 *)place);
109257cb251SWill Deacon 
110c84fced8SJiang Liu 	sval = do_reloc(op, place, val);
111c84fced8SJiang Liu 	sval >>= lsb;
112c84fced8SJiang Liu 	imm = sval & 0xffff;
113122e2fa0SWill Deacon 
114c84fced8SJiang Liu 	if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
115257cb251SWill Deacon 		/*
116257cb251SWill Deacon 		 * For signed MOVW relocations, we have to manipulate the
117257cb251SWill Deacon 		 * instruction encoding depending on whether or not the
118257cb251SWill Deacon 		 * immediate is less than zero.
119257cb251SWill Deacon 		 */
120257cb251SWill Deacon 		insn &= ~(3 << 29);
121257cb251SWill Deacon 		if ((s64)imm >= 0) {
122257cb251SWill Deacon 			/* >=0: Set the instruction to MOVZ (opcode 10b). */
123257cb251SWill Deacon 			insn |= 2 << 29;
124257cb251SWill Deacon 		} else {
125257cb251SWill Deacon 			/*
126257cb251SWill Deacon 			 * <0: Set the instruction to MOVN (opcode 00b).
127257cb251SWill Deacon 			 *     Since we've masked the opcode already, we
128257cb251SWill Deacon 			 *     don't need to do anything other than
129257cb251SWill Deacon 			 *     inverting the new immediate field.
130257cb251SWill Deacon 			 */
131257cb251SWill Deacon 			imm = ~imm;
132257cb251SWill Deacon 		}
133c84fced8SJiang Liu 		imm_type = AARCH64_INSN_IMM_MOVK;
134257cb251SWill Deacon 	}
135257cb251SWill Deacon 
136257cb251SWill Deacon 	/* Update the instruction with the new encoding. */
137c84fced8SJiang Liu 	insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
138c84fced8SJiang Liu 	*(u32 *)place = cpu_to_le32(insn);
139257cb251SWill Deacon 
140257cb251SWill Deacon 	/* Shift out the immediate field. */
141257cb251SWill Deacon 	sval >>= 16;
142257cb251SWill Deacon 
143257cb251SWill Deacon 	/*
144257cb251SWill Deacon 	 * For unsigned immediates, the overflow check is straightforward.
145257cb251SWill Deacon 	 * For signed immediates, the sign bit is actually the bit past the
146257cb251SWill Deacon 	 * most significant bit of the field.
147c84fced8SJiang Liu 	 * The AARCH64_INSN_IMM_16 immediate type is unsigned.
148257cb251SWill Deacon 	 */
149c84fced8SJiang Liu 	if (imm_type != AARCH64_INSN_IMM_16) {
150257cb251SWill Deacon 		sval++;
151257cb251SWill Deacon 		limit++;
152257cb251SWill Deacon 	}
153257cb251SWill Deacon 
154257cb251SWill Deacon 	/* Check the upper bits depending on the sign of the immediate. */
155257cb251SWill Deacon 	if ((u64)sval > limit)
156257cb251SWill Deacon 		return -ERANGE;
157257cb251SWill Deacon 
158257cb251SWill Deacon 	return 0;
159257cb251SWill Deacon }
160257cb251SWill Deacon 
161257cb251SWill Deacon static int reloc_insn_imm(enum aarch64_reloc_op op, void *place, u64 val,
162c84fced8SJiang Liu 			  int lsb, int len, enum aarch64_insn_imm_type imm_type)
163257cb251SWill Deacon {
164257cb251SWill Deacon 	u64 imm, imm_mask;
165257cb251SWill Deacon 	s64 sval;
166c84fced8SJiang Liu 	u32 insn = le32_to_cpu(*(u32 *)place);
167257cb251SWill Deacon 
168257cb251SWill Deacon 	/* Calculate the relocation value. */
169257cb251SWill Deacon 	sval = do_reloc(op, place, val);
170257cb251SWill Deacon 	sval >>= lsb;
171257cb251SWill Deacon 
172257cb251SWill Deacon 	/* Extract the value bits and shift them to bit 0. */
173257cb251SWill Deacon 	imm_mask = (BIT(lsb + len) - 1) >> lsb;
174257cb251SWill Deacon 	imm = sval & imm_mask;
175257cb251SWill Deacon 
176257cb251SWill Deacon 	/* Update the instruction's immediate field. */
177c84fced8SJiang Liu 	insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
178c84fced8SJiang Liu 	*(u32 *)place = cpu_to_le32(insn);
179257cb251SWill Deacon 
180257cb251SWill Deacon 	/*
181257cb251SWill Deacon 	 * Extract the upper value bits (including the sign bit) and
182257cb251SWill Deacon 	 * shift them to bit 0.
183257cb251SWill Deacon 	 */
184257cb251SWill Deacon 	sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
185257cb251SWill Deacon 
186257cb251SWill Deacon 	/*
187257cb251SWill Deacon 	 * Overflow has occurred if the upper bits are not all equal to
188257cb251SWill Deacon 	 * the sign bit of the value.
189257cb251SWill Deacon 	 */
190257cb251SWill Deacon 	if ((u64)(sval + 1) >= 2)
191257cb251SWill Deacon 		return -ERANGE;
192257cb251SWill Deacon 
193257cb251SWill Deacon 	return 0;
194257cb251SWill Deacon }
195257cb251SWill Deacon 
196257cb251SWill Deacon int apply_relocate_add(Elf64_Shdr *sechdrs,
197257cb251SWill Deacon 		       const char *strtab,
198257cb251SWill Deacon 		       unsigned int symindex,
199257cb251SWill Deacon 		       unsigned int relsec,
200257cb251SWill Deacon 		       struct module *me)
201257cb251SWill Deacon {
202257cb251SWill Deacon 	unsigned int i;
203257cb251SWill Deacon 	int ovf;
204257cb251SWill Deacon 	bool overflow_check;
205257cb251SWill Deacon 	Elf64_Sym *sym;
206257cb251SWill Deacon 	void *loc;
207257cb251SWill Deacon 	u64 val;
208257cb251SWill Deacon 	Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
209257cb251SWill Deacon 
210257cb251SWill Deacon 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
211257cb251SWill Deacon 		/* loc corresponds to P in the AArch64 ELF document. */
212257cb251SWill Deacon 		loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
213257cb251SWill Deacon 			+ rel[i].r_offset;
214257cb251SWill Deacon 
215257cb251SWill Deacon 		/* sym is the ELF symbol we're referring to. */
216257cb251SWill Deacon 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
217257cb251SWill Deacon 			+ ELF64_R_SYM(rel[i].r_info);
218257cb251SWill Deacon 
219257cb251SWill Deacon 		/* val corresponds to (S + A) in the AArch64 ELF document. */
220257cb251SWill Deacon 		val = sym->st_value + rel[i].r_addend;
221257cb251SWill Deacon 
222257cb251SWill Deacon 		/* Check for overflow by default. */
223257cb251SWill Deacon 		overflow_check = true;
224257cb251SWill Deacon 
225257cb251SWill Deacon 		/* Perform the static relocation. */
226257cb251SWill Deacon 		switch (ELF64_R_TYPE(rel[i].r_info)) {
227257cb251SWill Deacon 		/* Null relocations. */
228257cb251SWill Deacon 		case R_ARM_NONE:
229257cb251SWill Deacon 		case R_AARCH64_NONE:
230257cb251SWill Deacon 			ovf = 0;
231257cb251SWill Deacon 			break;
232257cb251SWill Deacon 
233257cb251SWill Deacon 		/* Data relocations. */
234257cb251SWill Deacon 		case R_AARCH64_ABS64:
235257cb251SWill Deacon 			overflow_check = false;
236257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
237257cb251SWill Deacon 			break;
238257cb251SWill Deacon 		case R_AARCH64_ABS32:
239257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
240257cb251SWill Deacon 			break;
241257cb251SWill Deacon 		case R_AARCH64_ABS16:
242257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
243257cb251SWill Deacon 			break;
244257cb251SWill Deacon 		case R_AARCH64_PREL64:
245257cb251SWill Deacon 			overflow_check = false;
246257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
247257cb251SWill Deacon 			break;
248257cb251SWill Deacon 		case R_AARCH64_PREL32:
249257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
250257cb251SWill Deacon 			break;
251257cb251SWill Deacon 		case R_AARCH64_PREL16:
252257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
253257cb251SWill Deacon 			break;
254257cb251SWill Deacon 
255257cb251SWill Deacon 		/* MOVW instruction relocations. */
256257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0_NC:
257257cb251SWill Deacon 			overflow_check = false;
258257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0:
259257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
260c84fced8SJiang Liu 					      AARCH64_INSN_IMM_16);
261257cb251SWill Deacon 			break;
262257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1_NC:
263257cb251SWill Deacon 			overflow_check = false;
264257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1:
265257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
266c84fced8SJiang Liu 					      AARCH64_INSN_IMM_16);
267257cb251SWill Deacon 			break;
268257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2_NC:
269257cb251SWill Deacon 			overflow_check = false;
270257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2:
271257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
272c84fced8SJiang Liu 					      AARCH64_INSN_IMM_16);
273257cb251SWill Deacon 			break;
274257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G3:
275257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
276257cb251SWill Deacon 			overflow_check = false;
277257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
278c84fced8SJiang Liu 					      AARCH64_INSN_IMM_16);
279257cb251SWill Deacon 			break;
280257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G0:
281257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
282c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
283257cb251SWill Deacon 			break;
284257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G1:
285257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
286c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
287257cb251SWill Deacon 			break;
288257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G2:
289257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
290c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
291257cb251SWill Deacon 			break;
292257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0_NC:
293257cb251SWill Deacon 			overflow_check = false;
294257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
295c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVK);
296257cb251SWill Deacon 			break;
297257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0:
298257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
299c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
300257cb251SWill Deacon 			break;
301257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1_NC:
302257cb251SWill Deacon 			overflow_check = false;
303257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
304c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVK);
305257cb251SWill Deacon 			break;
306257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1:
307257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
308c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
309257cb251SWill Deacon 			break;
310257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2_NC:
311257cb251SWill Deacon 			overflow_check = false;
312257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
313c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVK);
314257cb251SWill Deacon 			break;
315257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2:
316257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
317c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
318257cb251SWill Deacon 			break;
319257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G3:
320257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
321257cb251SWill Deacon 			overflow_check = false;
322257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48,
323c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
324257cb251SWill Deacon 			break;
325257cb251SWill Deacon 
326257cb251SWill Deacon 		/* Immediate instruction relocations. */
327257cb251SWill Deacon 		case R_AARCH64_LD_PREL_LO19:
328257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
329c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
330257cb251SWill Deacon 			break;
331257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_LO21:
332257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
333c84fced8SJiang Liu 					     AARCH64_INSN_IMM_ADR);
334257cb251SWill Deacon 			break;
335257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21_NC:
336257cb251SWill Deacon 			overflow_check = false;
337257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21:
338257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PAGE, loc, val, 12, 21,
339c84fced8SJiang Liu 					     AARCH64_INSN_IMM_ADR);
340257cb251SWill Deacon 			break;
341257cb251SWill Deacon 		case R_AARCH64_ADD_ABS_LO12_NC:
342257cb251SWill Deacon 		case R_AARCH64_LDST8_ABS_LO12_NC:
343257cb251SWill Deacon 			overflow_check = false;
344257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
345c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
346257cb251SWill Deacon 			break;
347257cb251SWill Deacon 		case R_AARCH64_LDST16_ABS_LO12_NC:
348257cb251SWill Deacon 			overflow_check = false;
349257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
350c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
351257cb251SWill Deacon 			break;
352257cb251SWill Deacon 		case R_AARCH64_LDST32_ABS_LO12_NC:
353257cb251SWill Deacon 			overflow_check = false;
354257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
355c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
356257cb251SWill Deacon 			break;
357257cb251SWill Deacon 		case R_AARCH64_LDST64_ABS_LO12_NC:
358257cb251SWill Deacon 			overflow_check = false;
359257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
360c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
361257cb251SWill Deacon 			break;
362257cb251SWill Deacon 		case R_AARCH64_LDST128_ABS_LO12_NC:
363257cb251SWill Deacon 			overflow_check = false;
364257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
365c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
366257cb251SWill Deacon 			break;
367257cb251SWill Deacon 		case R_AARCH64_TSTBR14:
368257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
369c84fced8SJiang Liu 					     AARCH64_INSN_IMM_14);
370257cb251SWill Deacon 			break;
371257cb251SWill Deacon 		case R_AARCH64_CONDBR19:
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_JUMP26:
376257cb251SWill Deacon 		case R_AARCH64_CALL26:
377257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
378c84fced8SJiang Liu 					     AARCH64_INSN_IMM_26);
379257cb251SWill Deacon 			break;
380257cb251SWill Deacon 
381257cb251SWill Deacon 		default:
382257cb251SWill Deacon 			pr_err("module %s: unsupported RELA relocation: %llu\n",
383257cb251SWill Deacon 			       me->name, ELF64_R_TYPE(rel[i].r_info));
384257cb251SWill Deacon 			return -ENOEXEC;
385257cb251SWill Deacon 		}
386257cb251SWill Deacon 
387257cb251SWill Deacon 		if (overflow_check && ovf == -ERANGE)
388257cb251SWill Deacon 			goto overflow;
389257cb251SWill Deacon 
390257cb251SWill Deacon 	}
391257cb251SWill Deacon 
392257cb251SWill Deacon 	return 0;
393257cb251SWill Deacon 
394257cb251SWill Deacon overflow:
395257cb251SWill Deacon 	pr_err("module %s: overflow in relocation type %d val %Lx\n",
396257cb251SWill Deacon 	       me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
397257cb251SWill Deacon 	return -ENOEXEC;
398257cb251SWill Deacon }
399932ded4bSAndre Przywara 
400932ded4bSAndre Przywara int module_finalize(const Elf_Ehdr *hdr,
401932ded4bSAndre Przywara 		    const Elf_Shdr *sechdrs,
402932ded4bSAndre Przywara 		    struct module *me)
403932ded4bSAndre Przywara {
404932ded4bSAndre Przywara 	const Elf_Shdr *s, *se;
405932ded4bSAndre Przywara 	const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
406932ded4bSAndre Przywara 
407932ded4bSAndre Przywara 	for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
408932ded4bSAndre Przywara 		if (strcmp(".altinstructions", secstrs + s->sh_name) == 0) {
409932ded4bSAndre Przywara 			apply_alternatives((void *)s->sh_addr, s->sh_size);
410932ded4bSAndre Przywara 			return 0;
411932ded4bSAndre Przywara 		}
412932ded4bSAndre Przywara 	}
413932ded4bSAndre Przywara 
414932ded4bSAndre Przywara 	return 0;
415932ded4bSAndre Przywara }
416