2 * AArch64 loadable module support.
4 * Copyright (C) 2012 ARM Limited
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Author: Will Deacon <will.deacon@arm.com>
21 #include <linux/bitops.h>
22 #include <linux/elf.h>
23 #include <linux/gfp.h>
24 #include <linux/kernel.h>
26 #include <linux/moduleloader.h>
27 #include <linux/vmalloc.h>
29 #include <asm/sections.h>
31 #define AARCH64_INSN_IMM_MOVNZ AARCH64_INSN_IMM_MAX
32 #define AARCH64_INSN_IMM_MOVK AARCH64_INSN_IMM_16
34 void *module_alloc(unsigned long size)
36 return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
37 GFP_KERNEL, PAGE_KERNEL_EXEC, NUMA_NO_NODE,
38 __builtin_return_address(0));
41 enum aarch64_reloc_op {
48 static u64 do_reloc(enum aarch64_reloc_op reloc_op, void *place, u64 val)
54 return val - (u64)place;
56 return (val & ~0xfff) - ((u64)place & ~0xfff);
61 pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
65 static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
67 u64 imm_mask = (1 << len) - 1;
68 s64 sval = do_reloc(op, place, val);
81 pr_err("Invalid length (%d) for data relocation\n", len);
86 * Extract the upper value bits (including the sign bit) and
87 * shift them to bit 0.
89 sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
92 * Overflow has occurred if the value is not representable in
93 * len bits (i.e the bottom len bits are not sign-extended and
94 * the top bits are not all zero).
96 if ((u64)(sval + 1) > 2)
102 static int reloc_insn_movw(enum aarch64_reloc_op op, void *place, u64 val,
103 int lsb, enum aarch64_insn_imm_type imm_type)
107 u32 insn = le32_to_cpu(*(u32 *)place);
109 sval = do_reloc(op, place, val);
113 if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
115 * For signed MOVW relocations, we have to manipulate the
116 * instruction encoding depending on whether or not the
117 * immediate is less than zero.
121 /* >=0: Set the instruction to MOVZ (opcode 10b). */
125 * <0: Set the instruction to MOVN (opcode 00b).
126 * Since we've masked the opcode already, we
127 * don't need to do anything other than
128 * inverting the new immediate field.
132 imm_type = AARCH64_INSN_IMM_MOVK;
135 /* Update the instruction with the new encoding. */
136 insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
137 *(u32 *)place = cpu_to_le32(insn);
139 /* Shift out the immediate field. */
143 * For unsigned immediates, the overflow check is straightforward.
144 * For signed immediates, the sign bit is actually the bit past the
145 * most significant bit of the field.
146 * The AARCH64_INSN_IMM_16 immediate type is unsigned.
148 if (imm_type != AARCH64_INSN_IMM_16) {
153 /* Check the upper bits depending on the sign of the immediate. */
154 if ((u64)sval > limit)
160 static int reloc_insn_imm(enum aarch64_reloc_op op, void *place, u64 val,
161 int lsb, int len, enum aarch64_insn_imm_type imm_type)
165 u32 insn = le32_to_cpu(*(u32 *)place);
167 /* Calculate the relocation value. */
168 sval = do_reloc(op, place, val);
171 /* Extract the value bits and shift them to bit 0. */
172 imm_mask = (BIT(lsb + len) - 1) >> lsb;
173 imm = sval & imm_mask;
175 /* Update the instruction's immediate field. */
176 insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
177 *(u32 *)place = cpu_to_le32(insn);
180 * Extract the upper value bits (including the sign bit) and
181 * shift them to bit 0.
183 sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
186 * Overflow has occurred if the upper bits are not all equal to
187 * the sign bit of the value.
189 if ((u64)(sval + 1) >= 2)
195 int apply_relocate_add(Elf64_Shdr *sechdrs,
197 unsigned int symindex,
207 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
209 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
210 /* loc corresponds to P in the AArch64 ELF document. */
211 loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
214 /* sym is the ELF symbol we're referring to. */
215 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
216 + ELF64_R_SYM(rel[i].r_info);
218 /* val corresponds to (S + A) in the AArch64 ELF document. */
219 val = sym->st_value + rel[i].r_addend;
221 /* Check for overflow by default. */
222 overflow_check = true;
224 /* Perform the static relocation. */
225 switch (ELF64_R_TYPE(rel[i].r_info)) {
226 /* Null relocations. */
232 /* Data relocations. */
233 case R_AARCH64_ABS64:
234 overflow_check = false;
235 ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
237 case R_AARCH64_ABS32:
238 ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
240 case R_AARCH64_ABS16:
241 ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
243 case R_AARCH64_PREL64:
244 overflow_check = false;
245 ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
247 case R_AARCH64_PREL32:
248 ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
250 case R_AARCH64_PREL16:
251 ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
254 /* MOVW instruction relocations. */
255 case R_AARCH64_MOVW_UABS_G0_NC:
256 overflow_check = false;
257 case R_AARCH64_MOVW_UABS_G0:
258 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
259 AARCH64_INSN_IMM_16);
261 case R_AARCH64_MOVW_UABS_G1_NC:
262 overflow_check = false;
263 case R_AARCH64_MOVW_UABS_G1:
264 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
265 AARCH64_INSN_IMM_16);
267 case R_AARCH64_MOVW_UABS_G2_NC:
268 overflow_check = false;
269 case R_AARCH64_MOVW_UABS_G2:
270 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
271 AARCH64_INSN_IMM_16);
273 case R_AARCH64_MOVW_UABS_G3:
274 /* We're using the top bits so we can't overflow. */
275 overflow_check = false;
276 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
277 AARCH64_INSN_IMM_16);
279 case R_AARCH64_MOVW_SABS_G0:
280 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
281 AARCH64_INSN_IMM_MOVNZ);
283 case R_AARCH64_MOVW_SABS_G1:
284 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
285 AARCH64_INSN_IMM_MOVNZ);
287 case R_AARCH64_MOVW_SABS_G2:
288 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
289 AARCH64_INSN_IMM_MOVNZ);
291 case R_AARCH64_MOVW_PREL_G0_NC:
292 overflow_check = false;
293 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
294 AARCH64_INSN_IMM_MOVK);
296 case R_AARCH64_MOVW_PREL_G0:
297 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
298 AARCH64_INSN_IMM_MOVNZ);
300 case R_AARCH64_MOVW_PREL_G1_NC:
301 overflow_check = false;
302 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
303 AARCH64_INSN_IMM_MOVK);
305 case R_AARCH64_MOVW_PREL_G1:
306 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
307 AARCH64_INSN_IMM_MOVNZ);
309 case R_AARCH64_MOVW_PREL_G2_NC:
310 overflow_check = false;
311 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
312 AARCH64_INSN_IMM_MOVK);
314 case R_AARCH64_MOVW_PREL_G2:
315 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
316 AARCH64_INSN_IMM_MOVNZ);
318 case R_AARCH64_MOVW_PREL_G3:
319 /* We're using the top bits so we can't overflow. */
320 overflow_check = false;
321 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48,
322 AARCH64_INSN_IMM_MOVNZ);
325 /* Immediate instruction relocations. */
326 case R_AARCH64_LD_PREL_LO19:
327 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
328 AARCH64_INSN_IMM_19);
330 case R_AARCH64_ADR_PREL_LO21:
331 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
332 AARCH64_INSN_IMM_ADR);
334 case R_AARCH64_ADR_PREL_PG_HI21_NC:
335 overflow_check = false;
336 case R_AARCH64_ADR_PREL_PG_HI21:
337 ovf = reloc_insn_imm(RELOC_OP_PAGE, loc, val, 12, 21,
338 AARCH64_INSN_IMM_ADR);
340 case R_AARCH64_ADD_ABS_LO12_NC:
341 case R_AARCH64_LDST8_ABS_LO12_NC:
342 overflow_check = false;
343 ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
344 AARCH64_INSN_IMM_12);
346 case R_AARCH64_LDST16_ABS_LO12_NC:
347 overflow_check = false;
348 ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
349 AARCH64_INSN_IMM_12);
351 case R_AARCH64_LDST32_ABS_LO12_NC:
352 overflow_check = false;
353 ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
354 AARCH64_INSN_IMM_12);
356 case R_AARCH64_LDST64_ABS_LO12_NC:
357 overflow_check = false;
358 ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
359 AARCH64_INSN_IMM_12);
361 case R_AARCH64_LDST128_ABS_LO12_NC:
362 overflow_check = false;
363 ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
364 AARCH64_INSN_IMM_12);
366 case R_AARCH64_TSTBR14:
367 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
368 AARCH64_INSN_IMM_14);
370 case R_AARCH64_CONDBR19:
371 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
372 AARCH64_INSN_IMM_19);
374 case R_AARCH64_JUMP26:
375 case R_AARCH64_CALL26:
376 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
377 AARCH64_INSN_IMM_26);
381 pr_err("module %s: unsupported RELA relocation: %llu\n",
382 me->name, ELF64_R_TYPE(rel[i].r_info));
386 if (overflow_check && ovf == -ERANGE)
394 pr_err("module %s: overflow in relocation type %d val %Lx\n",
395 me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
399 int module_finalize(const Elf_Ehdr *hdr,
400 const Elf_Shdr *sechdrs,
403 const Elf_Shdr *s, *se;
404 const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
406 for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
407 if (strcmp(".altinstructions", secstrs + s->sh_name) == 0) {
408 apply_alternatives((void *)s->sh_addr, s->sh_size);