xref: /openbmc/linux/arch/arm64/kernel/module.c (revision c84fced8d990dd86c523233d38b4685a52a4fc3f)
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*c84fced8SJiang Liu #include <asm/insn.h>
29*c84fced8SJiang Liu 
30*c84fced8SJiang Liu #define	AARCH64_INSN_IMM_MOVNZ		AARCH64_INSN_IMM_MAX
31*c84fced8SJiang Liu #define	AARCH64_INSN_IMM_MOVK		AARCH64_INSN_IMM_16
32257cb251SWill Deacon 
33257cb251SWill Deacon void *module_alloc(unsigned long size)
34257cb251SWill Deacon {
35257cb251SWill Deacon 	return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
3640c3baa7SJianguo Wu 				    GFP_KERNEL, PAGE_KERNEL_EXEC, NUMA_NO_NODE,
37257cb251SWill Deacon 				    __builtin_return_address(0));
38257cb251SWill Deacon }
39257cb251SWill Deacon 
40257cb251SWill Deacon enum aarch64_reloc_op {
41257cb251SWill Deacon 	RELOC_OP_NONE,
42257cb251SWill Deacon 	RELOC_OP_ABS,
43257cb251SWill Deacon 	RELOC_OP_PREL,
44257cb251SWill Deacon 	RELOC_OP_PAGE,
45257cb251SWill Deacon };
46257cb251SWill Deacon 
47257cb251SWill Deacon static u64 do_reloc(enum aarch64_reloc_op reloc_op, void *place, u64 val)
48257cb251SWill Deacon {
49257cb251SWill Deacon 	switch (reloc_op) {
50257cb251SWill Deacon 	case RELOC_OP_ABS:
51257cb251SWill Deacon 		return val;
52257cb251SWill Deacon 	case RELOC_OP_PREL:
53257cb251SWill Deacon 		return val - (u64)place;
54257cb251SWill Deacon 	case RELOC_OP_PAGE:
55257cb251SWill Deacon 		return (val & ~0xfff) - ((u64)place & ~0xfff);
56257cb251SWill Deacon 	case RELOC_OP_NONE:
57257cb251SWill Deacon 		return 0;
58257cb251SWill Deacon 	}
59257cb251SWill Deacon 
60257cb251SWill Deacon 	pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
61257cb251SWill Deacon 	return 0;
62257cb251SWill Deacon }
63257cb251SWill Deacon 
64257cb251SWill Deacon static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
65257cb251SWill Deacon {
66257cb251SWill Deacon 	u64 imm_mask = (1 << len) - 1;
67257cb251SWill Deacon 	s64 sval = do_reloc(op, place, val);
68257cb251SWill Deacon 
69257cb251SWill Deacon 	switch (len) {
70257cb251SWill Deacon 	case 16:
71257cb251SWill Deacon 		*(s16 *)place = sval;
72257cb251SWill Deacon 		break;
73257cb251SWill Deacon 	case 32:
74257cb251SWill Deacon 		*(s32 *)place = sval;
75257cb251SWill Deacon 		break;
76257cb251SWill Deacon 	case 64:
77257cb251SWill Deacon 		*(s64 *)place = sval;
78257cb251SWill Deacon 		break;
79257cb251SWill Deacon 	default:
80257cb251SWill Deacon 		pr_err("Invalid length (%d) for data relocation\n", len);
81257cb251SWill Deacon 		return 0;
82257cb251SWill Deacon 	}
83257cb251SWill Deacon 
84257cb251SWill Deacon 	/*
85257cb251SWill Deacon 	 * Extract the upper value bits (including the sign bit) and
86257cb251SWill Deacon 	 * shift them to bit 0.
87257cb251SWill Deacon 	 */
88257cb251SWill Deacon 	sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
89257cb251SWill Deacon 
90257cb251SWill Deacon 	/*
91257cb251SWill Deacon 	 * Overflow has occurred if the value is not representable in
92257cb251SWill Deacon 	 * len bits (i.e the bottom len bits are not sign-extended and
93257cb251SWill Deacon 	 * the top bits are not all zero).
94257cb251SWill Deacon 	 */
95257cb251SWill Deacon 	if ((u64)(sval + 1) > 2)
96257cb251SWill Deacon 		return -ERANGE;
97257cb251SWill Deacon 
98257cb251SWill Deacon 	return 0;
99257cb251SWill Deacon }
100257cb251SWill Deacon 
101*c84fced8SJiang Liu static int reloc_insn_movw(enum aarch64_reloc_op op, void *place, u64 val,
102*c84fced8SJiang Liu 			   int lsb, enum aarch64_insn_imm_type imm_type)
103257cb251SWill Deacon {
104*c84fced8SJiang Liu 	u64 imm, limit = 0;
105*c84fced8SJiang Liu 	s64 sval;
106*c84fced8SJiang Liu 	u32 insn = le32_to_cpu(*(u32 *)place);
107257cb251SWill Deacon 
108*c84fced8SJiang Liu 	sval = do_reloc(op, place, val);
109*c84fced8SJiang Liu 	sval >>= lsb;
110*c84fced8SJiang Liu 	imm = sval & 0xffff;
111122e2fa0SWill Deacon 
112*c84fced8SJiang Liu 	if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
113257cb251SWill Deacon 		/*
114257cb251SWill Deacon 		 * For signed MOVW relocations, we have to manipulate the
115257cb251SWill Deacon 		 * instruction encoding depending on whether or not the
116257cb251SWill Deacon 		 * immediate is less than zero.
117257cb251SWill Deacon 		 */
118257cb251SWill Deacon 		insn &= ~(3 << 29);
119257cb251SWill Deacon 		if ((s64)imm >= 0) {
120257cb251SWill Deacon 			/* >=0: Set the instruction to MOVZ (opcode 10b). */
121257cb251SWill Deacon 			insn |= 2 << 29;
122257cb251SWill Deacon 		} else {
123257cb251SWill Deacon 			/*
124257cb251SWill Deacon 			 * <0: Set the instruction to MOVN (opcode 00b).
125257cb251SWill Deacon 			 *     Since we've masked the opcode already, we
126257cb251SWill Deacon 			 *     don't need to do anything other than
127257cb251SWill Deacon 			 *     inverting the new immediate field.
128257cb251SWill Deacon 			 */
129257cb251SWill Deacon 			imm = ~imm;
130257cb251SWill Deacon 		}
131*c84fced8SJiang Liu 		imm_type = AARCH64_INSN_IMM_MOVK;
132257cb251SWill Deacon 	}
133257cb251SWill Deacon 
134257cb251SWill Deacon 	/* Update the instruction with the new encoding. */
135*c84fced8SJiang Liu 	insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
136*c84fced8SJiang Liu 	*(u32 *)place = cpu_to_le32(insn);
137257cb251SWill Deacon 
138257cb251SWill Deacon 	/* Shift out the immediate field. */
139257cb251SWill Deacon 	sval >>= 16;
140257cb251SWill Deacon 
141257cb251SWill Deacon 	/*
142257cb251SWill Deacon 	 * For unsigned immediates, the overflow check is straightforward.
143257cb251SWill Deacon 	 * For signed immediates, the sign bit is actually the bit past the
144257cb251SWill Deacon 	 * most significant bit of the field.
145*c84fced8SJiang Liu 	 * The AARCH64_INSN_IMM_16 immediate type is unsigned.
146257cb251SWill Deacon 	 */
147*c84fced8SJiang Liu 	if (imm_type != AARCH64_INSN_IMM_16) {
148257cb251SWill Deacon 		sval++;
149257cb251SWill Deacon 		limit++;
150257cb251SWill Deacon 	}
151257cb251SWill Deacon 
152257cb251SWill Deacon 	/* Check the upper bits depending on the sign of the immediate. */
153257cb251SWill Deacon 	if ((u64)sval > limit)
154257cb251SWill Deacon 		return -ERANGE;
155257cb251SWill Deacon 
156257cb251SWill Deacon 	return 0;
157257cb251SWill Deacon }
158257cb251SWill Deacon 
159257cb251SWill Deacon static int reloc_insn_imm(enum aarch64_reloc_op op, void *place, u64 val,
160*c84fced8SJiang Liu 			  int lsb, int len, enum aarch64_insn_imm_type imm_type)
161257cb251SWill Deacon {
162257cb251SWill Deacon 	u64 imm, imm_mask;
163257cb251SWill Deacon 	s64 sval;
164*c84fced8SJiang Liu 	u32 insn = le32_to_cpu(*(u32 *)place);
165257cb251SWill Deacon 
166257cb251SWill Deacon 	/* Calculate the relocation value. */
167257cb251SWill Deacon 	sval = do_reloc(op, place, val);
168257cb251SWill Deacon 	sval >>= lsb;
169257cb251SWill Deacon 
170257cb251SWill Deacon 	/* Extract the value bits and shift them to bit 0. */
171257cb251SWill Deacon 	imm_mask = (BIT(lsb + len) - 1) >> lsb;
172257cb251SWill Deacon 	imm = sval & imm_mask;
173257cb251SWill Deacon 
174257cb251SWill Deacon 	/* Update the instruction's immediate field. */
175*c84fced8SJiang Liu 	insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
176*c84fced8SJiang Liu 	*(u32 *)place = cpu_to_le32(insn);
177257cb251SWill Deacon 
178257cb251SWill Deacon 	/*
179257cb251SWill Deacon 	 * Extract the upper value bits (including the sign bit) and
180257cb251SWill Deacon 	 * shift them to bit 0.
181257cb251SWill Deacon 	 */
182257cb251SWill Deacon 	sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
183257cb251SWill Deacon 
184257cb251SWill Deacon 	/*
185257cb251SWill Deacon 	 * Overflow has occurred if the upper bits are not all equal to
186257cb251SWill Deacon 	 * the sign bit of the value.
187257cb251SWill Deacon 	 */
188257cb251SWill Deacon 	if ((u64)(sval + 1) >= 2)
189257cb251SWill Deacon 		return -ERANGE;
190257cb251SWill Deacon 
191257cb251SWill Deacon 	return 0;
192257cb251SWill Deacon }
193257cb251SWill Deacon 
194257cb251SWill Deacon int apply_relocate_add(Elf64_Shdr *sechdrs,
195257cb251SWill Deacon 		       const char *strtab,
196257cb251SWill Deacon 		       unsigned int symindex,
197257cb251SWill Deacon 		       unsigned int relsec,
198257cb251SWill Deacon 		       struct module *me)
199257cb251SWill Deacon {
200257cb251SWill Deacon 	unsigned int i;
201257cb251SWill Deacon 	int ovf;
202257cb251SWill Deacon 	bool overflow_check;
203257cb251SWill Deacon 	Elf64_Sym *sym;
204257cb251SWill Deacon 	void *loc;
205257cb251SWill Deacon 	u64 val;
206257cb251SWill Deacon 	Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
207257cb251SWill Deacon 
208257cb251SWill Deacon 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
209257cb251SWill Deacon 		/* loc corresponds to P in the AArch64 ELF document. */
210257cb251SWill Deacon 		loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
211257cb251SWill Deacon 			+ rel[i].r_offset;
212257cb251SWill Deacon 
213257cb251SWill Deacon 		/* sym is the ELF symbol we're referring to. */
214257cb251SWill Deacon 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
215257cb251SWill Deacon 			+ ELF64_R_SYM(rel[i].r_info);
216257cb251SWill Deacon 
217257cb251SWill Deacon 		/* val corresponds to (S + A) in the AArch64 ELF document. */
218257cb251SWill Deacon 		val = sym->st_value + rel[i].r_addend;
219257cb251SWill Deacon 
220257cb251SWill Deacon 		/* Check for overflow by default. */
221257cb251SWill Deacon 		overflow_check = true;
222257cb251SWill Deacon 
223257cb251SWill Deacon 		/* Perform the static relocation. */
224257cb251SWill Deacon 		switch (ELF64_R_TYPE(rel[i].r_info)) {
225257cb251SWill Deacon 		/* Null relocations. */
226257cb251SWill Deacon 		case R_ARM_NONE:
227257cb251SWill Deacon 		case R_AARCH64_NONE:
228257cb251SWill Deacon 			ovf = 0;
229257cb251SWill Deacon 			break;
230257cb251SWill Deacon 
231257cb251SWill Deacon 		/* Data relocations. */
232257cb251SWill Deacon 		case R_AARCH64_ABS64:
233257cb251SWill Deacon 			overflow_check = false;
234257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
235257cb251SWill Deacon 			break;
236257cb251SWill Deacon 		case R_AARCH64_ABS32:
237257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
238257cb251SWill Deacon 			break;
239257cb251SWill Deacon 		case R_AARCH64_ABS16:
240257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
241257cb251SWill Deacon 			break;
242257cb251SWill Deacon 		case R_AARCH64_PREL64:
243257cb251SWill Deacon 			overflow_check = false;
244257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
245257cb251SWill Deacon 			break;
246257cb251SWill Deacon 		case R_AARCH64_PREL32:
247257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
248257cb251SWill Deacon 			break;
249257cb251SWill Deacon 		case R_AARCH64_PREL16:
250257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
251257cb251SWill Deacon 			break;
252257cb251SWill Deacon 
253257cb251SWill Deacon 		/* MOVW instruction relocations. */
254257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0_NC:
255257cb251SWill Deacon 			overflow_check = false;
256257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0:
257257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
258*c84fced8SJiang Liu 					      AARCH64_INSN_IMM_16);
259257cb251SWill Deacon 			break;
260257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1_NC:
261257cb251SWill Deacon 			overflow_check = false;
262257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1:
263257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
264*c84fced8SJiang Liu 					      AARCH64_INSN_IMM_16);
265257cb251SWill Deacon 			break;
266257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2_NC:
267257cb251SWill Deacon 			overflow_check = false;
268257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2:
269257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
270*c84fced8SJiang Liu 					      AARCH64_INSN_IMM_16);
271257cb251SWill Deacon 			break;
272257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G3:
273257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
274257cb251SWill Deacon 			overflow_check = false;
275257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
276*c84fced8SJiang Liu 					      AARCH64_INSN_IMM_16);
277257cb251SWill Deacon 			break;
278257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G0:
279257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
280*c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
281257cb251SWill Deacon 			break;
282257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G1:
283257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
284*c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
285257cb251SWill Deacon 			break;
286257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G2:
287257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
288*c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
289257cb251SWill Deacon 			break;
290257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0_NC:
291257cb251SWill Deacon 			overflow_check = false;
292257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
293*c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVK);
294257cb251SWill Deacon 			break;
295257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0:
296257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
297*c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
298257cb251SWill Deacon 			break;
299257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1_NC:
300257cb251SWill Deacon 			overflow_check = false;
301257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
302*c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVK);
303257cb251SWill Deacon 			break;
304257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1:
305257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
306*c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
307257cb251SWill Deacon 			break;
308257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2_NC:
309257cb251SWill Deacon 			overflow_check = false;
310257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
311*c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVK);
312257cb251SWill Deacon 			break;
313257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2:
314257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
315*c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
316257cb251SWill Deacon 			break;
317257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G3:
318257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
319257cb251SWill Deacon 			overflow_check = false;
320257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48,
321*c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
322257cb251SWill Deacon 			break;
323257cb251SWill Deacon 
324257cb251SWill Deacon 		/* Immediate instruction relocations. */
325257cb251SWill Deacon 		case R_AARCH64_LD_PREL_LO19:
326257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
327*c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
328257cb251SWill Deacon 			break;
329257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_LO21:
330257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
331*c84fced8SJiang Liu 					     AARCH64_INSN_IMM_ADR);
332257cb251SWill Deacon 			break;
333257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21_NC:
334257cb251SWill Deacon 			overflow_check = false;
335257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21:
336257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PAGE, loc, val, 12, 21,
337*c84fced8SJiang Liu 					     AARCH64_INSN_IMM_ADR);
338257cb251SWill Deacon 			break;
339257cb251SWill Deacon 		case R_AARCH64_ADD_ABS_LO12_NC:
340257cb251SWill Deacon 		case R_AARCH64_LDST8_ABS_LO12_NC:
341257cb251SWill Deacon 			overflow_check = false;
342257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
343*c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
344257cb251SWill Deacon 			break;
345257cb251SWill Deacon 		case R_AARCH64_LDST16_ABS_LO12_NC:
346257cb251SWill Deacon 			overflow_check = false;
347257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
348*c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
349257cb251SWill Deacon 			break;
350257cb251SWill Deacon 		case R_AARCH64_LDST32_ABS_LO12_NC:
351257cb251SWill Deacon 			overflow_check = false;
352257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
353*c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
354257cb251SWill Deacon 			break;
355257cb251SWill Deacon 		case R_AARCH64_LDST64_ABS_LO12_NC:
356257cb251SWill Deacon 			overflow_check = false;
357257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
358*c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
359257cb251SWill Deacon 			break;
360257cb251SWill Deacon 		case R_AARCH64_LDST128_ABS_LO12_NC:
361257cb251SWill Deacon 			overflow_check = false;
362257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
363*c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
364257cb251SWill Deacon 			break;
365257cb251SWill Deacon 		case R_AARCH64_TSTBR14:
366257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
367*c84fced8SJiang Liu 					     AARCH64_INSN_IMM_14);
368257cb251SWill Deacon 			break;
369257cb251SWill Deacon 		case R_AARCH64_CONDBR19:
370257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
371*c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
372257cb251SWill Deacon 			break;
373257cb251SWill Deacon 		case R_AARCH64_JUMP26:
374257cb251SWill Deacon 		case R_AARCH64_CALL26:
375257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
376*c84fced8SJiang Liu 					     AARCH64_INSN_IMM_26);
377257cb251SWill Deacon 			break;
378257cb251SWill Deacon 
379257cb251SWill Deacon 		default:
380257cb251SWill Deacon 			pr_err("module %s: unsupported RELA relocation: %llu\n",
381257cb251SWill Deacon 			       me->name, ELF64_R_TYPE(rel[i].r_info));
382257cb251SWill Deacon 			return -ENOEXEC;
383257cb251SWill Deacon 		}
384257cb251SWill Deacon 
385257cb251SWill Deacon 		if (overflow_check && ovf == -ERANGE)
386257cb251SWill Deacon 			goto overflow;
387257cb251SWill Deacon 
388257cb251SWill Deacon 	}
389257cb251SWill Deacon 
390257cb251SWill Deacon 	return 0;
391257cb251SWill Deacon 
392257cb251SWill Deacon overflow:
393257cb251SWill Deacon 	pr_err("module %s: overflow in relocation type %d val %Lx\n",
394257cb251SWill Deacon 	       me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
395257cb251SWill Deacon 	return -ENOEXEC;
396257cb251SWill Deacon }
397