1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2017 Zihao Yu
9 #include <linux/errno.h>
10 #include <linux/moduleloader.h>
11 #include <linux/vmalloc.h>
12 #include <linux/sizes.h>
13 #include <asm/pgtable.h>
14 #include <asm/sections.h>
16 static int apply_r_riscv_32_rela(struct module *me, u32 *location, Elf_Addr v)
19 pr_err("%s: value %016llx out of range for 32-bit field\n",
20 me->name, (long long)v);
27 static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
33 static int apply_r_riscv_branch_rela(struct module *me, u32 *location,
36 ptrdiff_t offset = (void *)v - (void *)location;
37 u32 imm12 = (offset & 0x1000) << (31 - 12);
38 u32 imm11 = (offset & 0x800) >> (11 - 7);
39 u32 imm10_5 = (offset & 0x7e0) << (30 - 10);
40 u32 imm4_1 = (offset & 0x1e) << (11 - 4);
42 *location = (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1;
46 static int apply_r_riscv_jal_rela(struct module *me, u32 *location,
49 ptrdiff_t offset = (void *)v - (void *)location;
50 u32 imm20 = (offset & 0x100000) << (31 - 20);
51 u32 imm19_12 = (offset & 0xff000);
52 u32 imm11 = (offset & 0x800) << (20 - 11);
53 u32 imm10_1 = (offset & 0x7fe) << (30 - 10);
55 *location = (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1;
59 static int apply_r_riscv_rcv_branch_rela(struct module *me, u32 *location,
62 ptrdiff_t offset = (void *)v - (void *)location;
63 u16 imm8 = (offset & 0x100) << (12 - 8);
64 u16 imm7_6 = (offset & 0xc0) >> (6 - 5);
65 u16 imm5 = (offset & 0x20) >> (5 - 2);
66 u16 imm4_3 = (offset & 0x18) << (12 - 5);
67 u16 imm2_1 = (offset & 0x6) << (12 - 10);
69 *(u16 *)location = (*(u16 *)location & 0xe383) |
70 imm8 | imm7_6 | imm5 | imm4_3 | imm2_1;
74 static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location,
77 ptrdiff_t offset = (void *)v - (void *)location;
78 u16 imm11 = (offset & 0x800) << (12 - 11);
79 u16 imm10 = (offset & 0x400) >> (10 - 8);
80 u16 imm9_8 = (offset & 0x300) << (12 - 11);
81 u16 imm7 = (offset & 0x80) >> (7 - 6);
82 u16 imm6 = (offset & 0x40) << (12 - 11);
83 u16 imm5 = (offset & 0x20) >> (5 - 2);
84 u16 imm4 = (offset & 0x10) << (12 - 5);
85 u16 imm3_1 = (offset & 0xe) << (12 - 10);
87 *(u16 *)location = (*(u16 *)location & 0xe003) |
88 imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1;
92 static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location,
95 ptrdiff_t offset = (void *)v - (void *)location;
98 if (offset != (s32)offset) {
100 "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
101 me->name, (long long)v, location);
105 hi20 = (offset + 0x800) & 0xfffff000;
106 *location = (*location & 0xfff) | hi20;
110 static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *location,
114 * v is the lo12 value to fill. It is calculated before calling this
117 *location = (*location & 0xfffff) | ((v & 0xfff) << 20);
121 static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location,
125 * v is the lo12 value to fill. It is calculated before calling this
128 u32 imm11_5 = (v & 0xfe0) << (31 - 11);
129 u32 imm4_0 = (v & 0x1f) << (11 - 4);
131 *location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
135 static int apply_r_riscv_hi20_rela(struct module *me, u32 *location,
140 if (IS_ENABLED(CONFIG_CMODEL_MEDLOW)) {
142 "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
143 me->name, (long long)v, location);
147 hi20 = ((s32)v + 0x800) & 0xfffff000;
148 *location = (*location & 0xfff) | hi20;
152 static int apply_r_riscv_lo12_i_rela(struct module *me, u32 *location,
155 /* Skip medlow checking because of filtering by HI20 already */
156 s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
157 s32 lo12 = ((s32)v - hi20);
158 *location = (*location & 0xfffff) | ((lo12 & 0xfff) << 20);
162 static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location,
165 /* Skip medlow checking because of filtering by HI20 already */
166 s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
167 s32 lo12 = ((s32)v - hi20);
168 u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11);
169 u32 imm4_0 = (lo12 & 0x1f) << (11 - 4);
170 *location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
174 static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location,
177 ptrdiff_t offset = (void *)v - (void *)location;
180 /* Always emit the got entry */
181 if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
182 offset = module_emit_got_entry(me, v);
183 offset = (void *)offset - (void *)location;
186 "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n",
187 me->name, (long long)v, location);
191 hi20 = (offset + 0x800) & 0xfffff000;
192 *location = (*location & 0xfff) | hi20;
196 static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location,
199 ptrdiff_t offset = (void *)v - (void *)location;
203 if (offset != fill_v) {
204 /* Only emit the plt entry if offset over 32-bit range */
205 if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
206 offset = module_emit_plt_entry(me, v);
207 offset = (void *)offset - (void *)location;
210 "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
211 me->name, (long long)v, location);
216 hi20 = (offset + 0x800) & 0xfffff000;
217 lo12 = (offset - hi20) & 0xfff;
218 *location = (*location & 0xfff) | hi20;
219 *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
223 static int apply_r_riscv_call_rela(struct module *me, u32 *location,
226 ptrdiff_t offset = (void *)v - (void *)location;
230 if (offset != fill_v) {
232 "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
233 me->name, (long long)v, location);
237 hi20 = (offset + 0x800) & 0xfffff000;
238 lo12 = (offset - hi20) & 0xfff;
239 *location = (*location & 0xfff) | hi20;
240 *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
244 static int apply_r_riscv_relax_rela(struct module *me, u32 *location,
250 static int apply_r_riscv_align_rela(struct module *me, u32 *location,
254 "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n",
259 static int apply_r_riscv_add32_rela(struct module *me, u32 *location,
262 *(u32 *)location += (u32)v;
266 static int apply_r_riscv_sub32_rela(struct module *me, u32 *location,
269 *(u32 *)location -= (u32)v;
273 static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
275 [R_RISCV_32] = apply_r_riscv_32_rela,
276 [R_RISCV_64] = apply_r_riscv_64_rela,
277 [R_RISCV_BRANCH] = apply_r_riscv_branch_rela,
278 [R_RISCV_JAL] = apply_r_riscv_jal_rela,
279 [R_RISCV_RVC_BRANCH] = apply_r_riscv_rcv_branch_rela,
280 [R_RISCV_RVC_JUMP] = apply_r_riscv_rvc_jump_rela,
281 [R_RISCV_PCREL_HI20] = apply_r_riscv_pcrel_hi20_rela,
282 [R_RISCV_PCREL_LO12_I] = apply_r_riscv_pcrel_lo12_i_rela,
283 [R_RISCV_PCREL_LO12_S] = apply_r_riscv_pcrel_lo12_s_rela,
284 [R_RISCV_HI20] = apply_r_riscv_hi20_rela,
285 [R_RISCV_LO12_I] = apply_r_riscv_lo12_i_rela,
286 [R_RISCV_LO12_S] = apply_r_riscv_lo12_s_rela,
287 [R_RISCV_GOT_HI20] = apply_r_riscv_got_hi20_rela,
288 [R_RISCV_CALL_PLT] = apply_r_riscv_call_plt_rela,
289 [R_RISCV_CALL] = apply_r_riscv_call_rela,
290 [R_RISCV_RELAX] = apply_r_riscv_relax_rela,
291 [R_RISCV_ALIGN] = apply_r_riscv_align_rela,
292 [R_RISCV_ADD32] = apply_r_riscv_add32_rela,
293 [R_RISCV_SUB32] = apply_r_riscv_sub32_rela,
296 int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
297 unsigned int symindex, unsigned int relsec,
300 Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
301 int (*handler)(struct module *me, u32 *location, Elf_Addr v);
304 unsigned int i, type;
308 pr_debug("Applying relocate section %u to %u\n", relsec,
309 sechdrs[relsec].sh_info);
311 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
312 /* This is where to make the change */
313 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
315 /* This is the symbol it is referring to */
316 sym = (Elf_Sym *)sechdrs[symindex].sh_addr
317 + ELF_RISCV_R_SYM(rel[i].r_info);
318 if (IS_ERR_VALUE(sym->st_value)) {
319 /* Ignore unresolved weak symbol */
320 if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
322 pr_warn("%s: Unknown symbol %s\n",
323 me->name, strtab + sym->st_name);
327 type = ELF_RISCV_R_TYPE(rel[i].r_info);
329 if (type < ARRAY_SIZE(reloc_handlers_rela))
330 handler = reloc_handlers_rela[type];
335 pr_err("%s: Unknown relocation type %u\n",
340 v = sym->st_value + rel[i].r_addend;
342 if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
345 for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
346 unsigned long hi20_loc =
347 sechdrs[sechdrs[relsec].sh_info].sh_addr
349 u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
351 /* Find the corresponding HI20 relocation entry */
352 if (hi20_loc == sym->st_value
353 && (hi20_type == R_RISCV_PCREL_HI20
354 || hi20_type == R_RISCV_GOT_HI20)) {
357 (Elf_Sym *)sechdrs[symindex].sh_addr
358 + ELF_RISCV_R_SYM(rel[j].r_info);
359 unsigned long hi20_sym_val =
364 size_t offset = hi20_sym_val - hi20_loc;
365 if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
366 && hi20_type == R_RISCV_GOT_HI20) {
367 offset = module_emit_got_entry(
369 offset = offset - hi20_loc;
371 hi20 = (offset + 0x800) & 0xfffff000;
372 lo12 = offset - hi20;
378 if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
380 "%s: Can not find HI20 relocation information\n",
386 res = handler(me, location, v);
394 #if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
395 #define VMALLOC_MODULE_START \
396 max(PFN_ALIGN((unsigned long)&_end - SZ_2G), VMALLOC_START)
397 void *module_alloc(unsigned long size)
399 return __vmalloc_node_range(size, 1, VMALLOC_MODULE_START,
400 VMALLOC_END, GFP_KERNEL,
401 PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
402 __builtin_return_address(0));