xref: /openbmc/linux/arch/arm64/kernel/module.c (revision 932ded4b0b9bf111fbf9d176ec12152a0d29b0fd)
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>
28c84fced8SJiang Liu #include <asm/insn.h>
29*932ded4bSAndre Przywara #include <asm/sections.h>
30c84fced8SJiang Liu 
31c84fced8SJiang Liu #define	AARCH64_INSN_IMM_MOVNZ		AARCH64_INSN_IMM_MAX
32c84fced8SJiang Liu #define	AARCH64_INSN_IMM_MOVK		AARCH64_INSN_IMM_16
33257cb251SWill Deacon 
34257cb251SWill Deacon void *module_alloc(unsigned long size)
35257cb251SWill Deacon {
36257cb251SWill Deacon 	return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
3740c3baa7SJianguo Wu 				    GFP_KERNEL, PAGE_KERNEL_EXEC, NUMA_NO_NODE,
38257cb251SWill Deacon 				    __builtin_return_address(0));
39257cb251SWill Deacon }
40257cb251SWill Deacon 
41257cb251SWill Deacon enum aarch64_reloc_op {
42257cb251SWill Deacon 	RELOC_OP_NONE,
43257cb251SWill Deacon 	RELOC_OP_ABS,
44257cb251SWill Deacon 	RELOC_OP_PREL,
45257cb251SWill Deacon 	RELOC_OP_PAGE,
46257cb251SWill Deacon };
47257cb251SWill Deacon 
48257cb251SWill Deacon static u64 do_reloc(enum aarch64_reloc_op reloc_op, void *place, u64 val)
49257cb251SWill Deacon {
50257cb251SWill Deacon 	switch (reloc_op) {
51257cb251SWill Deacon 	case RELOC_OP_ABS:
52257cb251SWill Deacon 		return val;
53257cb251SWill Deacon 	case RELOC_OP_PREL:
54257cb251SWill Deacon 		return val - (u64)place;
55257cb251SWill Deacon 	case RELOC_OP_PAGE:
56257cb251SWill Deacon 		return (val & ~0xfff) - ((u64)place & ~0xfff);
57257cb251SWill Deacon 	case RELOC_OP_NONE:
58257cb251SWill Deacon 		return 0;
59257cb251SWill Deacon 	}
60257cb251SWill Deacon 
61257cb251SWill Deacon 	pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
62257cb251SWill Deacon 	return 0;
63257cb251SWill Deacon }
64257cb251SWill Deacon 
65257cb251SWill Deacon static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
66257cb251SWill Deacon {
67257cb251SWill Deacon 	u64 imm_mask = (1 << len) - 1;
68257cb251SWill Deacon 	s64 sval = do_reloc(op, place, val);
69257cb251SWill Deacon 
70257cb251SWill Deacon 	switch (len) {
71257cb251SWill Deacon 	case 16:
72257cb251SWill Deacon 		*(s16 *)place = sval;
73257cb251SWill Deacon 		break;
74257cb251SWill Deacon 	case 32:
75257cb251SWill Deacon 		*(s32 *)place = sval;
76257cb251SWill Deacon 		break;
77257cb251SWill Deacon 	case 64:
78257cb251SWill Deacon 		*(s64 *)place = sval;
79257cb251SWill Deacon 		break;
80257cb251SWill Deacon 	default:
81257cb251SWill Deacon 		pr_err("Invalid length (%d) for data relocation\n", len);
82257cb251SWill Deacon 		return 0;
83257cb251SWill Deacon 	}
84257cb251SWill Deacon 
85257cb251SWill Deacon 	/*
86257cb251SWill Deacon 	 * Extract the upper value bits (including the sign bit) and
87257cb251SWill Deacon 	 * shift them to bit 0.
88257cb251SWill Deacon 	 */
89257cb251SWill Deacon 	sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
90257cb251SWill Deacon 
91257cb251SWill Deacon 	/*
92257cb251SWill Deacon 	 * Overflow has occurred if the value is not representable in
93257cb251SWill Deacon 	 * len bits (i.e the bottom len bits are not sign-extended and
94257cb251SWill Deacon 	 * the top bits are not all zero).
95257cb251SWill Deacon 	 */
96257cb251SWill Deacon 	if ((u64)(sval + 1) > 2)
97257cb251SWill Deacon 		return -ERANGE;
98257cb251SWill Deacon 
99257cb251SWill Deacon 	return 0;
100257cb251SWill Deacon }
101257cb251SWill Deacon 
102c84fced8SJiang Liu static int reloc_insn_movw(enum aarch64_reloc_op op, void *place, u64 val,
103c84fced8SJiang Liu 			   int lsb, enum aarch64_insn_imm_type imm_type)
104257cb251SWill Deacon {
105c84fced8SJiang Liu 	u64 imm, limit = 0;
106c84fced8SJiang Liu 	s64 sval;
107c84fced8SJiang Liu 	u32 insn = le32_to_cpu(*(u32 *)place);
108257cb251SWill Deacon 
109c84fced8SJiang Liu 	sval = do_reloc(op, place, val);
110c84fced8SJiang Liu 	sval >>= lsb;
111c84fced8SJiang Liu 	imm = sval & 0xffff;
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);
120257cb251SWill Deacon 		if ((s64)imm >= 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 		}
132c84fced8SJiang Liu 		imm_type = AARCH64_INSN_IMM_MOVK;
133257cb251SWill Deacon 	}
134257cb251SWill Deacon 
135257cb251SWill Deacon 	/* Update the instruction with the new encoding. */
136c84fced8SJiang Liu 	insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
137c84fced8SJiang Liu 	*(u32 *)place = cpu_to_le32(insn);
138257cb251SWill Deacon 
139257cb251SWill Deacon 	/* Shift out the immediate field. */
140257cb251SWill Deacon 	sval >>= 16;
141257cb251SWill Deacon 
142257cb251SWill Deacon 	/*
143257cb251SWill Deacon 	 * For unsigned immediates, the overflow check is straightforward.
144257cb251SWill Deacon 	 * For signed immediates, the sign bit is actually the bit past the
145257cb251SWill Deacon 	 * most significant bit of the field.
146c84fced8SJiang Liu 	 * The AARCH64_INSN_IMM_16 immediate type is unsigned.
147257cb251SWill Deacon 	 */
148c84fced8SJiang Liu 	if (imm_type != AARCH64_INSN_IMM_16) {
149257cb251SWill Deacon 		sval++;
150257cb251SWill Deacon 		limit++;
151257cb251SWill Deacon 	}
152257cb251SWill Deacon 
153257cb251SWill Deacon 	/* Check the upper bits depending on the sign of the immediate. */
154257cb251SWill Deacon 	if ((u64)sval > limit)
155257cb251SWill Deacon 		return -ERANGE;
156257cb251SWill Deacon 
157257cb251SWill Deacon 	return 0;
158257cb251SWill Deacon }
159257cb251SWill Deacon 
160257cb251SWill Deacon static int reloc_insn_imm(enum aarch64_reloc_op op, void *place, u64 val,
161c84fced8SJiang Liu 			  int lsb, int len, enum aarch64_insn_imm_type imm_type)
162257cb251SWill Deacon {
163257cb251SWill Deacon 	u64 imm, imm_mask;
164257cb251SWill Deacon 	s64 sval;
165c84fced8SJiang Liu 	u32 insn = le32_to_cpu(*(u32 *)place);
166257cb251SWill Deacon 
167257cb251SWill Deacon 	/* Calculate the relocation value. */
168257cb251SWill Deacon 	sval = do_reloc(op, place, val);
169257cb251SWill Deacon 	sval >>= lsb;
170257cb251SWill Deacon 
171257cb251SWill Deacon 	/* Extract the value bits and shift them to bit 0. */
172257cb251SWill Deacon 	imm_mask = (BIT(lsb + len) - 1) >> lsb;
173257cb251SWill Deacon 	imm = sval & imm_mask;
174257cb251SWill Deacon 
175257cb251SWill Deacon 	/* Update the instruction's immediate field. */
176c84fced8SJiang Liu 	insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
177c84fced8SJiang Liu 	*(u32 *)place = cpu_to_le32(insn);
178257cb251SWill Deacon 
179257cb251SWill Deacon 	/*
180257cb251SWill Deacon 	 * Extract the upper value bits (including the sign bit) and
181257cb251SWill Deacon 	 * shift them to bit 0.
182257cb251SWill Deacon 	 */
183257cb251SWill Deacon 	sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
184257cb251SWill Deacon 
185257cb251SWill Deacon 	/*
186257cb251SWill Deacon 	 * Overflow has occurred if the upper bits are not all equal to
187257cb251SWill Deacon 	 * the sign bit of the value.
188257cb251SWill Deacon 	 */
189257cb251SWill Deacon 	if ((u64)(sval + 1) >= 2)
190257cb251SWill Deacon 		return -ERANGE;
191257cb251SWill Deacon 
192257cb251SWill Deacon 	return 0;
193257cb251SWill Deacon }
194257cb251SWill Deacon 
195257cb251SWill Deacon int apply_relocate_add(Elf64_Shdr *sechdrs,
196257cb251SWill Deacon 		       const char *strtab,
197257cb251SWill Deacon 		       unsigned int symindex,
198257cb251SWill Deacon 		       unsigned int relsec,
199257cb251SWill Deacon 		       struct module *me)
200257cb251SWill Deacon {
201257cb251SWill Deacon 	unsigned int i;
202257cb251SWill Deacon 	int ovf;
203257cb251SWill Deacon 	bool overflow_check;
204257cb251SWill Deacon 	Elf64_Sym *sym;
205257cb251SWill Deacon 	void *loc;
206257cb251SWill Deacon 	u64 val;
207257cb251SWill Deacon 	Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
208257cb251SWill Deacon 
209257cb251SWill Deacon 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
210257cb251SWill Deacon 		/* loc corresponds to P in the AArch64 ELF document. */
211257cb251SWill Deacon 		loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
212257cb251SWill Deacon 			+ rel[i].r_offset;
213257cb251SWill Deacon 
214257cb251SWill Deacon 		/* sym is the ELF symbol we're referring to. */
215257cb251SWill Deacon 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
216257cb251SWill Deacon 			+ ELF64_R_SYM(rel[i].r_info);
217257cb251SWill Deacon 
218257cb251SWill Deacon 		/* val corresponds to (S + A) in the AArch64 ELF document. */
219257cb251SWill Deacon 		val = sym->st_value + rel[i].r_addend;
220257cb251SWill Deacon 
221257cb251SWill Deacon 		/* Check for overflow by default. */
222257cb251SWill Deacon 		overflow_check = true;
223257cb251SWill Deacon 
224257cb251SWill Deacon 		/* Perform the static relocation. */
225257cb251SWill Deacon 		switch (ELF64_R_TYPE(rel[i].r_info)) {
226257cb251SWill Deacon 		/* Null relocations. */
227257cb251SWill Deacon 		case R_ARM_NONE:
228257cb251SWill Deacon 		case R_AARCH64_NONE:
229257cb251SWill Deacon 			ovf = 0;
230257cb251SWill Deacon 			break;
231257cb251SWill Deacon 
232257cb251SWill Deacon 		/* Data relocations. */
233257cb251SWill Deacon 		case R_AARCH64_ABS64:
234257cb251SWill Deacon 			overflow_check = false;
235257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
236257cb251SWill Deacon 			break;
237257cb251SWill Deacon 		case R_AARCH64_ABS32:
238257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
239257cb251SWill Deacon 			break;
240257cb251SWill Deacon 		case R_AARCH64_ABS16:
241257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
242257cb251SWill Deacon 			break;
243257cb251SWill Deacon 		case R_AARCH64_PREL64:
244257cb251SWill Deacon 			overflow_check = false;
245257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
246257cb251SWill Deacon 			break;
247257cb251SWill Deacon 		case R_AARCH64_PREL32:
248257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
249257cb251SWill Deacon 			break;
250257cb251SWill Deacon 		case R_AARCH64_PREL16:
251257cb251SWill Deacon 			ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
252257cb251SWill Deacon 			break;
253257cb251SWill Deacon 
254257cb251SWill Deacon 		/* MOVW instruction relocations. */
255257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0_NC:
256257cb251SWill Deacon 			overflow_check = false;
257257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G0:
258257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
259c84fced8SJiang Liu 					      AARCH64_INSN_IMM_16);
260257cb251SWill Deacon 			break;
261257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1_NC:
262257cb251SWill Deacon 			overflow_check = false;
263257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G1:
264257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
265c84fced8SJiang Liu 					      AARCH64_INSN_IMM_16);
266257cb251SWill Deacon 			break;
267257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2_NC:
268257cb251SWill Deacon 			overflow_check = false;
269257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G2:
270257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
271c84fced8SJiang Liu 					      AARCH64_INSN_IMM_16);
272257cb251SWill Deacon 			break;
273257cb251SWill Deacon 		case R_AARCH64_MOVW_UABS_G3:
274257cb251SWill Deacon 			/* We're using the top bits so we can't overflow. */
275257cb251SWill Deacon 			overflow_check = false;
276257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
277c84fced8SJiang Liu 					      AARCH64_INSN_IMM_16);
278257cb251SWill Deacon 			break;
279257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G0:
280257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
281c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
282257cb251SWill Deacon 			break;
283257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G1:
284257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
285c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
286257cb251SWill Deacon 			break;
287257cb251SWill Deacon 		case R_AARCH64_MOVW_SABS_G2:
288257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
289c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
290257cb251SWill Deacon 			break;
291257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0_NC:
292257cb251SWill Deacon 			overflow_check = false;
293257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
294c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVK);
295257cb251SWill Deacon 			break;
296257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G0:
297257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
298c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
299257cb251SWill Deacon 			break;
300257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1_NC:
301257cb251SWill Deacon 			overflow_check = false;
302257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
303c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVK);
304257cb251SWill Deacon 			break;
305257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G1:
306257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
307c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
308257cb251SWill Deacon 			break;
309257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2_NC:
310257cb251SWill Deacon 			overflow_check = false;
311257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
312c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVK);
313257cb251SWill Deacon 			break;
314257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_G2:
315257cb251SWill Deacon 			ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
316c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
317257cb251SWill Deacon 			break;
318257cb251SWill Deacon 		case R_AARCH64_MOVW_PREL_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_PREL, loc, val, 48,
322c84fced8SJiang Liu 					      AARCH64_INSN_IMM_MOVNZ);
323257cb251SWill Deacon 			break;
324257cb251SWill Deacon 
325257cb251SWill Deacon 		/* Immediate instruction relocations. */
326257cb251SWill Deacon 		case R_AARCH64_LD_PREL_LO19:
327257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
328c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
329257cb251SWill Deacon 			break;
330257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_LO21:
331257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
332c84fced8SJiang Liu 					     AARCH64_INSN_IMM_ADR);
333257cb251SWill Deacon 			break;
334257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21_NC:
335257cb251SWill Deacon 			overflow_check = false;
336257cb251SWill Deacon 		case R_AARCH64_ADR_PREL_PG_HI21:
337257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PAGE, loc, val, 12, 21,
338c84fced8SJiang Liu 					     AARCH64_INSN_IMM_ADR);
339257cb251SWill Deacon 			break;
340257cb251SWill Deacon 		case R_AARCH64_ADD_ABS_LO12_NC:
341257cb251SWill Deacon 		case R_AARCH64_LDST8_ABS_LO12_NC:
342257cb251SWill Deacon 			overflow_check = false;
343257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
344c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
345257cb251SWill Deacon 			break;
346257cb251SWill Deacon 		case R_AARCH64_LDST16_ABS_LO12_NC:
347257cb251SWill Deacon 			overflow_check = false;
348257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
349c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
350257cb251SWill Deacon 			break;
351257cb251SWill Deacon 		case R_AARCH64_LDST32_ABS_LO12_NC:
352257cb251SWill Deacon 			overflow_check = false;
353257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
354c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
355257cb251SWill Deacon 			break;
356257cb251SWill Deacon 		case R_AARCH64_LDST64_ABS_LO12_NC:
357257cb251SWill Deacon 			overflow_check = false;
358257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
359c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
360257cb251SWill Deacon 			break;
361257cb251SWill Deacon 		case R_AARCH64_LDST128_ABS_LO12_NC:
362257cb251SWill Deacon 			overflow_check = false;
363257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
364c84fced8SJiang Liu 					     AARCH64_INSN_IMM_12);
365257cb251SWill Deacon 			break;
366257cb251SWill Deacon 		case R_AARCH64_TSTBR14:
367257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
368c84fced8SJiang Liu 					     AARCH64_INSN_IMM_14);
369257cb251SWill Deacon 			break;
370257cb251SWill Deacon 		case R_AARCH64_CONDBR19:
371257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
372c84fced8SJiang Liu 					     AARCH64_INSN_IMM_19);
373257cb251SWill Deacon 			break;
374257cb251SWill Deacon 		case R_AARCH64_JUMP26:
375257cb251SWill Deacon 		case R_AARCH64_CALL26:
376257cb251SWill Deacon 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
377c84fced8SJiang Liu 					     AARCH64_INSN_IMM_26);
378257cb251SWill Deacon 			break;
379257cb251SWill Deacon 
380257cb251SWill Deacon 		default:
381257cb251SWill Deacon 			pr_err("module %s: unsupported RELA relocation: %llu\n",
382257cb251SWill Deacon 			       me->name, ELF64_R_TYPE(rel[i].r_info));
383257cb251SWill Deacon 			return -ENOEXEC;
384257cb251SWill Deacon 		}
385257cb251SWill Deacon 
386257cb251SWill Deacon 		if (overflow_check && ovf == -ERANGE)
387257cb251SWill Deacon 			goto overflow;
388257cb251SWill Deacon 
389257cb251SWill Deacon 	}
390257cb251SWill Deacon 
391257cb251SWill Deacon 	return 0;
392257cb251SWill Deacon 
393257cb251SWill Deacon overflow:
394257cb251SWill Deacon 	pr_err("module %s: overflow in relocation type %d val %Lx\n",
395257cb251SWill Deacon 	       me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
396257cb251SWill Deacon 	return -ENOEXEC;
397257cb251SWill Deacon }
398*932ded4bSAndre Przywara 
399*932ded4bSAndre Przywara int module_finalize(const Elf_Ehdr *hdr,
400*932ded4bSAndre Przywara 		    const Elf_Shdr *sechdrs,
401*932ded4bSAndre Przywara 		    struct module *me)
402*932ded4bSAndre Przywara {
403*932ded4bSAndre Przywara 	const Elf_Shdr *s, *se;
404*932ded4bSAndre Przywara 	const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
405*932ded4bSAndre Przywara 
406*932ded4bSAndre Przywara 	for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
407*932ded4bSAndre Przywara 		if (strcmp(".altinstructions", secstrs + s->sh_name) == 0) {
408*932ded4bSAndre Przywara 			apply_alternatives((void *)s->sh_addr, s->sh_size);
409*932ded4bSAndre Przywara 			return 0;
410*932ded4bSAndre Przywara 		}
411*932ded4bSAndre Przywara 	}
412*932ded4bSAndre Przywara 
413*932ded4bSAndre Przywara 	return 0;
414*932ded4bSAndre Przywara }
415