1 // SPDX-License-Identifier: GPL-2.0+
3 * Author: Hanlu Li <lihanlu@loongson.cn>
4 * Huacai Chen <chenhuacai@loongson.cn>
6 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
9 #define pr_fmt(fmt) "kmod: " fmt
11 #include <linux/moduleloader.h>
12 #include <linux/elf.h>
14 #include <linux/numa.h>
15 #include <linux/vmalloc.h>
16 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kernel.h>
21 static int rela_stack_push(s64 stack_value, s64 *rela_stack, size_t *rela_stack_top)
23 if (*rela_stack_top >= RELA_STACK_DEPTH)
26 rela_stack[(*rela_stack_top)++] = stack_value;
27 pr_debug("%s stack_value = 0x%llx\n", __func__, stack_value);
32 static int rela_stack_pop(s64 *stack_value, s64 *rela_stack, size_t *rela_stack_top)
34 if (*rela_stack_top == 0)
37 *stack_value = rela_stack[--(*rela_stack_top)];
38 pr_debug("%s stack_value = 0x%llx\n", __func__, *stack_value);
43 static int apply_r_larch_none(struct module *mod, u32 *location, Elf_Addr v,
44 s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
49 static int apply_r_larch_error(struct module *me, u32 *location, Elf_Addr v,
50 s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
52 pr_err("%s: Unsupport relocation type %u, please add its support.\n", me->name, type);
56 static int apply_r_larch_32(struct module *mod, u32 *location, Elf_Addr v,
57 s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
63 static int apply_r_larch_64(struct module *mod, u32 *location, Elf_Addr v,
64 s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
66 *(Elf_Addr *)location = v;
70 static int apply_r_larch_sop_push_pcrel(struct module *mod, u32 *location, Elf_Addr v,
71 s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
73 return rela_stack_push(v - (u64)location, rela_stack, rela_stack_top);
76 static int apply_r_larch_sop_push_absolute(struct module *mod, u32 *location, Elf_Addr v,
77 s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
79 return rela_stack_push(v, rela_stack, rela_stack_top);
82 static int apply_r_larch_sop_push_dup(struct module *mod, u32 *location, Elf_Addr v,
83 s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
88 err = rela_stack_pop(&opr1, rela_stack, rela_stack_top);
91 err = rela_stack_push(opr1, rela_stack, rela_stack_top);
94 err = rela_stack_push(opr1, rela_stack, rela_stack_top);
101 static int apply_r_larch_sop_push_plt_pcrel(struct module *mod, u32 *location, Elf_Addr v,
102 s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
104 ptrdiff_t offset = (void *)v - (void *)location;
106 if (offset >= SZ_128M)
107 v = module_emit_plt_entry(mod, v);
109 if (offset < -SZ_128M)
110 v = module_emit_plt_entry(mod, v);
112 return apply_r_larch_sop_push_pcrel(mod, location, v, rela_stack, rela_stack_top, type);
115 static int apply_r_larch_sop(struct module *mod, u32 *location, Elf_Addr v,
116 s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
119 s64 opr1, opr2, opr3;
121 if (type == R_LARCH_SOP_IF_ELSE) {
122 err = rela_stack_pop(&opr3, rela_stack, rela_stack_top);
127 err = rela_stack_pop(&opr2, rela_stack, rela_stack_top);
130 err = rela_stack_pop(&opr1, rela_stack, rela_stack_top);
135 case R_LARCH_SOP_AND:
136 err = rela_stack_push(opr1 & opr2, rela_stack, rela_stack_top);
138 case R_LARCH_SOP_ADD:
139 err = rela_stack_push(opr1 + opr2, rela_stack, rela_stack_top);
141 case R_LARCH_SOP_SUB:
142 err = rela_stack_push(opr1 - opr2, rela_stack, rela_stack_top);
145 err = rela_stack_push(opr1 << opr2, rela_stack, rela_stack_top);
148 err = rela_stack_push(opr1 >> opr2, rela_stack, rela_stack_top);
150 case R_LARCH_SOP_IF_ELSE:
151 err = rela_stack_push(opr1 ? opr2 : opr3, rela_stack, rela_stack_top);
154 pr_err("%s: Unsupport relocation type %u\n", mod->name, type);
161 static int apply_r_larch_sop_imm_field(struct module *mod, u32 *location, Elf_Addr v,
162 s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
166 union loongarch_instruction *insn = (union loongarch_instruction *)location;
168 err = rela_stack_pop(&opr1, rela_stack, rela_stack_top);
173 case R_LARCH_SOP_POP_32_U_10_12:
174 if (!unsigned_imm_check(opr1, 12))
177 /* (*(uint32_t *) PC) [21 ... 10] = opr [11 ... 0] */
178 insn->reg2i12_format.immediate = opr1 & 0xfff;
180 case R_LARCH_SOP_POP_32_S_10_12:
181 if (!signed_imm_check(opr1, 12))
184 insn->reg2i12_format.immediate = opr1 & 0xfff;
186 case R_LARCH_SOP_POP_32_S_10_16:
187 if (!signed_imm_check(opr1, 16))
190 insn->reg2i16_format.immediate = opr1 & 0xffff;
192 case R_LARCH_SOP_POP_32_S_10_16_S2:
196 if (!signed_imm_check(opr1, 18))
199 insn->reg2i16_format.immediate = (opr1 >> 2) & 0xffff;
201 case R_LARCH_SOP_POP_32_S_5_20:
202 if (!signed_imm_check(opr1, 20))
205 insn->reg1i20_format.immediate = (opr1) & 0xfffff;
207 case R_LARCH_SOP_POP_32_S_0_5_10_16_S2:
211 if (!signed_imm_check(opr1, 23))
215 insn->reg1i21_format.immediate_l = opr1 & 0xffff;
216 insn->reg1i21_format.immediate_h = (opr1 >> 16) & 0x1f;
218 case R_LARCH_SOP_POP_32_S_0_10_10_16_S2:
222 if (!signed_imm_check(opr1, 28))
226 insn->reg0i26_format.immediate_l = opr1 & 0xffff;
227 insn->reg0i26_format.immediate_h = (opr1 >> 16) & 0x3ff;
229 case R_LARCH_SOP_POP_32_U:
230 if (!unsigned_imm_check(opr1, 32))
233 /* (*(uint32_t *) PC) = opr */
234 *location = (u32)opr1;
237 pr_err("%s: Unsupport relocation type %u\n", mod->name, type);
242 pr_err("module %s: opr1 = 0x%llx overflow! dangerous %s (%u) relocation\n",
243 mod->name, opr1, __func__, type);
247 pr_err("module %s: opr1 = 0x%llx unaligned! dangerous %s (%u) relocation\n",
248 mod->name, opr1, __func__, type);
252 static int apply_r_larch_add_sub(struct module *mod, u32 *location, Elf_Addr v,
253 s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
257 *(s32 *)location += v;
260 *(s64 *)location += v;
263 *(s32 *)location -= v;
266 *(s64 *)location -= v;
269 pr_err("%s: Unsupport relocation type %u\n", mod->name, type);
274 static int apply_r_larch_b26(struct module *mod, u32 *location, Elf_Addr v,
275 s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
277 ptrdiff_t offset = (void *)v - (void *)location;
278 union loongarch_instruction *insn = (union loongarch_instruction *)location;
280 if (offset >= SZ_128M)
281 v = module_emit_plt_entry(mod, v);
283 if (offset < -SZ_128M)
284 v = module_emit_plt_entry(mod, v);
286 offset = (void *)v - (void *)location;
289 pr_err("module %s: jump offset = 0x%llx unaligned! dangerous R_LARCH_B26 (%u) relocation\n",
290 mod->name, (long long)offset, type);
294 if (!signed_imm_check(offset, 28)) {
295 pr_err("module %s: jump offset = 0x%llx overflow! dangerous R_LARCH_B26 (%u) relocation\n",
296 mod->name, (long long)offset, type);
301 insn->reg0i26_format.immediate_l = offset & 0xffff;
302 insn->reg0i26_format.immediate_h = (offset >> 16) & 0x3ff;
307 static int apply_r_larch_pcala(struct module *mod, u32 *location, Elf_Addr v,
308 s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
310 union loongarch_instruction *insn = (union loongarch_instruction *)location;
311 /* Use s32 for a sign-extension deliberately. */
312 s32 offset_hi20 = (void *)((v + 0x800) & ~0xfff) -
313 (void *)((Elf_Addr)location & ~0xfff);
314 Elf_Addr anchor = (((Elf_Addr)location) & ~0xfff) + offset_hi20;
315 ptrdiff_t offset_rem = (void *)v - (void *)anchor;
318 case R_LARCH_PCALA_LO12:
319 insn->reg2i12_format.immediate = v & 0xfff;
321 case R_LARCH_PCALA_HI20:
322 v = offset_hi20 >> 12;
323 insn->reg1i20_format.immediate = v & 0xfffff;
325 case R_LARCH_PCALA64_LO20:
326 v = offset_rem >> 32;
327 insn->reg1i20_format.immediate = v & 0xfffff;
329 case R_LARCH_PCALA64_HI12:
330 v = offset_rem >> 52;
331 insn->reg2i12_format.immediate = v & 0xfff;
334 pr_err("%s: Unsupport relocation type %u\n", mod->name, type);
341 static int apply_r_larch_got_pc(struct module *mod, u32 *location, Elf_Addr v,
342 s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
344 Elf_Addr got = module_emit_got_entry(mod, v);
350 case R_LARCH_GOT_PC_LO12:
351 type = R_LARCH_PCALA_LO12;
353 case R_LARCH_GOT_PC_HI20:
354 type = R_LARCH_PCALA_HI20;
357 pr_err("%s: Unsupport relocation type %u\n", mod->name, type);
361 return apply_r_larch_pcala(mod, location, got, rela_stack, rela_stack_top, type);
365 * reloc_handlers_rela() - Apply a particular relocation to a module
366 * @mod: the module to apply the reloc to
367 * @location: the address at which the reloc is to be applied
368 * @v: the value of the reloc, with addend for RELA-style
369 * @rela_stack: the stack used for store relocation info, LOCAL to THIS module
370 * @rela_stac_top: where the stack operation(pop/push) applies to
372 * Return: 0 upon success, else -ERRNO
374 typedef int (*reloc_rela_handler)(struct module *mod, u32 *location, Elf_Addr v,
375 s64 *rela_stack, size_t *rela_stack_top, unsigned int type);
377 /* The handlers for known reloc types */
378 static reloc_rela_handler reloc_rela_handlers[] = {
379 [R_LARCH_NONE ... R_LARCH_64_PCREL] = apply_r_larch_error,
381 [R_LARCH_NONE] = apply_r_larch_none,
382 [R_LARCH_32] = apply_r_larch_32,
383 [R_LARCH_64] = apply_r_larch_64,
384 [R_LARCH_MARK_LA] = apply_r_larch_none,
385 [R_LARCH_MARK_PCREL] = apply_r_larch_none,
386 [R_LARCH_SOP_PUSH_PCREL] = apply_r_larch_sop_push_pcrel,
387 [R_LARCH_SOP_PUSH_ABSOLUTE] = apply_r_larch_sop_push_absolute,
388 [R_LARCH_SOP_PUSH_DUP] = apply_r_larch_sop_push_dup,
389 [R_LARCH_SOP_PUSH_PLT_PCREL] = apply_r_larch_sop_push_plt_pcrel,
390 [R_LARCH_SOP_SUB ... R_LARCH_SOP_IF_ELSE] = apply_r_larch_sop,
391 [R_LARCH_SOP_POP_32_S_10_5 ... R_LARCH_SOP_POP_32_U] = apply_r_larch_sop_imm_field,
392 [R_LARCH_ADD32 ... R_LARCH_SUB64] = apply_r_larch_add_sub,
393 [R_LARCH_B26] = apply_r_larch_b26,
394 [R_LARCH_PCALA_HI20...R_LARCH_PCALA64_HI12] = apply_r_larch_pcala,
395 [R_LARCH_GOT_PC_HI20...R_LARCH_GOT_PC_LO12] = apply_r_larch_got_pc,
398 int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
399 unsigned int symindex, unsigned int relsec,
404 s64 rela_stack[RELA_STACK_DEPTH];
405 size_t rela_stack_top = 0;
406 reloc_rela_handler handler;
410 Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
412 pr_debug("%s: Applying relocate section %u to %u\n", __func__, relsec,
413 sechdrs[relsec].sh_info);
416 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
417 /* This is where to make the change */
418 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr + rel[i].r_offset;
419 /* This is the symbol it is referring to */
420 sym = (Elf_Sym *)sechdrs[symindex].sh_addr + ELF_R_SYM(rel[i].r_info);
421 if (IS_ERR_VALUE(sym->st_value)) {
422 /* Ignore unresolved weak symbol */
423 if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
425 pr_warn("%s: Unknown symbol %s\n", mod->name, strtab + sym->st_name);
429 type = ELF_R_TYPE(rel[i].r_info);
431 if (type < ARRAY_SIZE(reloc_rela_handlers))
432 handler = reloc_rela_handlers[type];
437 pr_err("%s: Unknown relocation type %u\n", mod->name, type);
441 pr_debug("type %d st_value %llx r_addend %llx loc %llx\n",
442 (int)ELF_R_TYPE(rel[i].r_info),
443 sym->st_value, rel[i].r_addend, (u64)location);
445 v = sym->st_value + rel[i].r_addend;
446 err = handler(mod, location, v, rela_stack, &rela_stack_top, type);
454 void *module_alloc(unsigned long size)
456 return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
457 GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE, __builtin_return_address(0));