1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2017 Zihao Yu
9 #include <linux/errno.h>
10 #include <linux/hashtable.h>
11 #include <linux/kernel.h>
12 #include <linux/log2.h>
13 #include <linux/moduleloader.h>
14 #include <linux/vmalloc.h>
15 #include <linux/sizes.h>
16 #include <linux/pgtable.h>
17 #include <asm/alternative.h>
18 #include <asm/sections.h>
21 struct list_head head;
22 struct hlist_head *bucket;
25 struct relocation_head {
26 struct hlist_node node;
27 struct list_head *rel_entry;
31 struct relocation_entry {
32 struct list_head head;
37 struct relocation_handlers {
38 int (*reloc_handler)(struct module *me, void *location, Elf_Addr v);
39 int (*accumulate_handler)(struct module *me, void *location,
44 * The auipc+jalr instruction pair can reach any PC-relative offset
45 * in the range [-2^31 - 2^11, 2^31 - 2^11)
47 static bool riscv_insn_valid_32bit_offset(ptrdiff_t val)
52 return (-(1L << 31) - (1L << 11)) <= val && val < ((1L << 31) - (1L << 11));
56 static int riscv_insn_rmw(void *location, u32 keep, u32 set)
58 __le16 *parcel = location;
59 u32 insn = (u32)le16_to_cpu(parcel[0]) | (u32)le16_to_cpu(parcel[1]) << 16;
64 parcel[0] = cpu_to_le16(insn);
65 parcel[1] = cpu_to_le16(insn >> 16);
69 static int riscv_insn_rvc_rmw(void *location, u16 keep, u16 set)
71 __le16 *parcel = location;
72 u16 insn = le16_to_cpu(*parcel);
77 *parcel = cpu_to_le16(insn);
81 static int apply_r_riscv_32_rela(struct module *me, void *location, Elf_Addr v)
84 pr_err("%s: value %016llx out of range for 32-bit field\n",
85 me->name, (long long)v);
92 static int apply_r_riscv_64_rela(struct module *me, void *location, Elf_Addr v)
98 static int apply_r_riscv_branch_rela(struct module *me, void *location,
101 ptrdiff_t offset = (void *)v - location;
102 u32 imm12 = (offset & 0x1000) << (31 - 12);
103 u32 imm11 = (offset & 0x800) >> (11 - 7);
104 u32 imm10_5 = (offset & 0x7e0) << (30 - 10);
105 u32 imm4_1 = (offset & 0x1e) << (11 - 4);
107 return riscv_insn_rmw(location, 0x1fff07f, imm12 | imm11 | imm10_5 | imm4_1);
110 static int apply_r_riscv_jal_rela(struct module *me, void *location,
113 ptrdiff_t offset = (void *)v - location;
114 u32 imm20 = (offset & 0x100000) << (31 - 20);
115 u32 imm19_12 = (offset & 0xff000);
116 u32 imm11 = (offset & 0x800) << (20 - 11);
117 u32 imm10_1 = (offset & 0x7fe) << (30 - 10);
119 return riscv_insn_rmw(location, 0xfff, imm20 | imm19_12 | imm11 | imm10_1);
122 static int apply_r_riscv_rvc_branch_rela(struct module *me, void *location,
125 ptrdiff_t offset = (void *)v - location;
126 u16 imm8 = (offset & 0x100) << (12 - 8);
127 u16 imm7_6 = (offset & 0xc0) >> (6 - 5);
128 u16 imm5 = (offset & 0x20) >> (5 - 2);
129 u16 imm4_3 = (offset & 0x18) << (12 - 5);
130 u16 imm2_1 = (offset & 0x6) << (12 - 10);
132 return riscv_insn_rvc_rmw(location, 0xe383,
133 imm8 | imm7_6 | imm5 | imm4_3 | imm2_1);
136 static int apply_r_riscv_rvc_jump_rela(struct module *me, void *location,
139 ptrdiff_t offset = (void *)v - location;
140 u16 imm11 = (offset & 0x800) << (12 - 11);
141 u16 imm10 = (offset & 0x400) >> (10 - 8);
142 u16 imm9_8 = (offset & 0x300) << (12 - 11);
143 u16 imm7 = (offset & 0x80) >> (7 - 6);
144 u16 imm6 = (offset & 0x40) << (12 - 11);
145 u16 imm5 = (offset & 0x20) >> (5 - 2);
146 u16 imm4 = (offset & 0x10) << (12 - 5);
147 u16 imm3_1 = (offset & 0xe) << (12 - 10);
149 return riscv_insn_rvc_rmw(location, 0xe003,
150 imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1);
153 static int apply_r_riscv_pcrel_hi20_rela(struct module *me, void *location,
156 ptrdiff_t offset = (void *)v - location;
158 if (!riscv_insn_valid_32bit_offset(offset)) {
160 "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
161 me->name, (long long)v, location);
165 return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000);
168 static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, void *location,
172 * v is the lo12 value to fill. It is calculated before calling this
175 return riscv_insn_rmw(location, 0xfffff, (v & 0xfff) << 20);
178 static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, void *location,
182 * v is the lo12 value to fill. It is calculated before calling this
185 u32 imm11_5 = (v & 0xfe0) << (31 - 11);
186 u32 imm4_0 = (v & 0x1f) << (11 - 4);
188 return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0);
191 static int apply_r_riscv_hi20_rela(struct module *me, void *location,
194 if (IS_ENABLED(CONFIG_CMODEL_MEDLOW)) {
196 "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
197 me->name, (long long)v, location);
201 return riscv_insn_rmw(location, 0xfff, ((s32)v + 0x800) & 0xfffff000);
204 static int apply_r_riscv_lo12_i_rela(struct module *me, void *location,
207 /* Skip medlow checking because of filtering by HI20 already */
208 s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
209 s32 lo12 = ((s32)v - hi20);
211 return riscv_insn_rmw(location, 0xfffff, (lo12 & 0xfff) << 20);
214 static int apply_r_riscv_lo12_s_rela(struct module *me, void *location,
217 /* Skip medlow checking because of filtering by HI20 already */
218 s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
219 s32 lo12 = ((s32)v - hi20);
220 u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11);
221 u32 imm4_0 = (lo12 & 0x1f) << (11 - 4);
223 return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0);
226 static int apply_r_riscv_got_hi20_rela(struct module *me, void *location,
229 ptrdiff_t offset = (void *)v - location;
231 /* Always emit the got entry */
232 if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
233 offset = (void *)module_emit_got_entry(me, v) - location;
236 "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n",
237 me->name, (long long)v, location);
241 return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000);
244 static int apply_r_riscv_call_plt_rela(struct module *me, void *location,
247 ptrdiff_t offset = (void *)v - location;
250 if (!riscv_insn_valid_32bit_offset(offset)) {
251 /* Only emit the plt entry if offset over 32-bit range */
252 if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
253 offset = (void *)module_emit_plt_entry(me, v) - location;
256 "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
257 me->name, (long long)v, location);
262 hi20 = (offset + 0x800) & 0xfffff000;
263 lo12 = (offset - hi20) & 0xfff;
264 riscv_insn_rmw(location, 0xfff, hi20);
265 return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20);
268 static int apply_r_riscv_call_rela(struct module *me, void *location,
271 ptrdiff_t offset = (void *)v - location;
274 if (!riscv_insn_valid_32bit_offset(offset)) {
276 "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
277 me->name, (long long)v, location);
281 hi20 = (offset + 0x800) & 0xfffff000;
282 lo12 = (offset - hi20) & 0xfff;
283 riscv_insn_rmw(location, 0xfff, hi20);
284 return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20);
287 static int apply_r_riscv_relax_rela(struct module *me, void *location,
293 static int apply_r_riscv_align_rela(struct module *me, void *location,
297 "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n",
302 static int apply_r_riscv_add8_rela(struct module *me, void *location, Elf_Addr v)
304 *(u8 *)location += (u8)v;
308 static int apply_r_riscv_add16_rela(struct module *me, void *location,
311 *(u16 *)location += (u16)v;
315 static int apply_r_riscv_add32_rela(struct module *me, void *location,
318 *(u32 *)location += (u32)v;
322 static int apply_r_riscv_add64_rela(struct module *me, void *location,
325 *(u64 *)location += (u64)v;
329 static int apply_r_riscv_sub8_rela(struct module *me, void *location, Elf_Addr v)
331 *(u8 *)location -= (u8)v;
335 static int apply_r_riscv_sub16_rela(struct module *me, void *location,
338 *(u16 *)location -= (u16)v;
342 static int apply_r_riscv_sub32_rela(struct module *me, void *location,
345 *(u32 *)location -= (u32)v;
349 static int apply_r_riscv_sub64_rela(struct module *me, void *location,
352 *(u64 *)location -= (u64)v;
356 static int dynamic_linking_not_supported(struct module *me, void *location,
359 pr_err("%s: Dynamic linking not supported in kernel modules PC = %p\n",
364 static int tls_not_supported(struct module *me, void *location, Elf_Addr v)
366 pr_err("%s: Thread local storage not supported in kernel modules PC = %p\n",
371 static int apply_r_riscv_sub6_rela(struct module *me, void *location, Elf_Addr v)
376 *byte = (*byte - (value & 0x3f)) & 0x3f;
380 static int apply_r_riscv_set6_rela(struct module *me, void *location, Elf_Addr v)
385 *byte = (*byte & 0xc0) | (value & 0x3f);
389 static int apply_r_riscv_set8_rela(struct module *me, void *location, Elf_Addr v)
391 *(u8 *)location = (u8)v;
395 static int apply_r_riscv_set16_rela(struct module *me, void *location,
398 *(u16 *)location = (u16)v;
402 static int apply_r_riscv_set32_rela(struct module *me, void *location,
405 *(u32 *)location = (u32)v;
409 static int apply_r_riscv_32_pcrel_rela(struct module *me, void *location,
412 *(u32 *)location = v - (uintptr_t)location;
416 static int apply_r_riscv_plt32_rela(struct module *me, void *location,
419 ptrdiff_t offset = (void *)v - location;
421 if (!riscv_insn_valid_32bit_offset(offset)) {
422 /* Only emit the plt entry if offset over 32-bit range */
423 if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
424 offset = (void *)module_emit_plt_entry(me, v) - location;
426 pr_err("%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
427 me->name, (long long)v, location);
432 *(u32 *)location = (u32)offset;
436 static int apply_r_riscv_set_uleb128(struct module *me, void *location, Elf_Addr v)
438 *(long *)location = v;
442 static int apply_r_riscv_sub_uleb128(struct module *me, void *location, Elf_Addr v)
444 *(long *)location -= v;
448 static int apply_6_bit_accumulation(struct module *me, void *location, long buffer)
454 pr_err("%s: value %ld out of range for 6-bit relocation.\n",
459 *byte = (*byte & 0xc0) | (value & 0x3f);
463 static int apply_8_bit_accumulation(struct module *me, void *location, long buffer)
465 if (buffer > U8_MAX) {
466 pr_err("%s: value %ld out of range for 8-bit relocation.\n",
470 *(u8 *)location = (u8)buffer;
474 static int apply_16_bit_accumulation(struct module *me, void *location, long buffer)
476 if (buffer > U16_MAX) {
477 pr_err("%s: value %ld out of range for 16-bit relocation.\n",
481 *(u16 *)location = (u16)buffer;
485 static int apply_32_bit_accumulation(struct module *me, void *location, long buffer)
487 if (buffer > U32_MAX) {
488 pr_err("%s: value %ld out of range for 32-bit relocation.\n",
492 *(u32 *)location = (u32)buffer;
496 static int apply_64_bit_accumulation(struct module *me, void *location, long buffer)
498 *(u64 *)location = (u64)buffer;
502 static int apply_uleb128_accumulation(struct module *me, void *location, long buffer)
505 * ULEB128 is a variable length encoding. Encode the buffer into
506 * the ULEB128 data format.
510 while (buffer != 0) {
511 u8 value = buffer & 0x7f;
514 value |= (!!buffer) << 7;
522 * Relocations defined in the riscv-elf-psabi-doc.
523 * This handles static linking only.
525 static const struct relocation_handlers reloc_handlers[] = {
526 [R_RISCV_32] = { .reloc_handler = apply_r_riscv_32_rela },
527 [R_RISCV_64] = { .reloc_handler = apply_r_riscv_64_rela },
528 [R_RISCV_RELATIVE] = { .reloc_handler = dynamic_linking_not_supported },
529 [R_RISCV_COPY] = { .reloc_handler = dynamic_linking_not_supported },
530 [R_RISCV_JUMP_SLOT] = { .reloc_handler = dynamic_linking_not_supported },
531 [R_RISCV_TLS_DTPMOD32] = { .reloc_handler = dynamic_linking_not_supported },
532 [R_RISCV_TLS_DTPMOD64] = { .reloc_handler = dynamic_linking_not_supported },
533 [R_RISCV_TLS_DTPREL32] = { .reloc_handler = dynamic_linking_not_supported },
534 [R_RISCV_TLS_DTPREL64] = { .reloc_handler = dynamic_linking_not_supported },
535 [R_RISCV_TLS_TPREL32] = { .reloc_handler = dynamic_linking_not_supported },
536 [R_RISCV_TLS_TPREL64] = { .reloc_handler = dynamic_linking_not_supported },
537 /* 12-15 undefined */
538 [R_RISCV_BRANCH] = { .reloc_handler = apply_r_riscv_branch_rela },
539 [R_RISCV_JAL] = { .reloc_handler = apply_r_riscv_jal_rela },
540 [R_RISCV_CALL] = { .reloc_handler = apply_r_riscv_call_rela },
541 [R_RISCV_CALL_PLT] = { .reloc_handler = apply_r_riscv_call_plt_rela },
542 [R_RISCV_GOT_HI20] = { .reloc_handler = apply_r_riscv_got_hi20_rela },
543 [R_RISCV_TLS_GOT_HI20] = { .reloc_handler = tls_not_supported },
544 [R_RISCV_TLS_GD_HI20] = { .reloc_handler = tls_not_supported },
545 [R_RISCV_PCREL_HI20] = { .reloc_handler = apply_r_riscv_pcrel_hi20_rela },
546 [R_RISCV_PCREL_LO12_I] = { .reloc_handler = apply_r_riscv_pcrel_lo12_i_rela },
547 [R_RISCV_PCREL_LO12_S] = { .reloc_handler = apply_r_riscv_pcrel_lo12_s_rela },
548 [R_RISCV_HI20] = { .reloc_handler = apply_r_riscv_hi20_rela },
549 [R_RISCV_LO12_I] = { .reloc_handler = apply_r_riscv_lo12_i_rela },
550 [R_RISCV_LO12_S] = { .reloc_handler = apply_r_riscv_lo12_s_rela },
551 [R_RISCV_TPREL_HI20] = { .reloc_handler = tls_not_supported },
552 [R_RISCV_TPREL_LO12_I] = { .reloc_handler = tls_not_supported },
553 [R_RISCV_TPREL_LO12_S] = { .reloc_handler = tls_not_supported },
554 [R_RISCV_TPREL_ADD] = { .reloc_handler = tls_not_supported },
555 [R_RISCV_ADD8] = { .reloc_handler = apply_r_riscv_add8_rela,
556 .accumulate_handler = apply_8_bit_accumulation },
557 [R_RISCV_ADD16] = { .reloc_handler = apply_r_riscv_add16_rela,
558 .accumulate_handler = apply_16_bit_accumulation },
559 [R_RISCV_ADD32] = { .reloc_handler = apply_r_riscv_add32_rela,
560 .accumulate_handler = apply_32_bit_accumulation },
561 [R_RISCV_ADD64] = { .reloc_handler = apply_r_riscv_add64_rela,
562 .accumulate_handler = apply_64_bit_accumulation },
563 [R_RISCV_SUB8] = { .reloc_handler = apply_r_riscv_sub8_rela,
564 .accumulate_handler = apply_8_bit_accumulation },
565 [R_RISCV_SUB16] = { .reloc_handler = apply_r_riscv_sub16_rela,
566 .accumulate_handler = apply_16_bit_accumulation },
567 [R_RISCV_SUB32] = { .reloc_handler = apply_r_riscv_sub32_rela,
568 .accumulate_handler = apply_32_bit_accumulation },
569 [R_RISCV_SUB64] = { .reloc_handler = apply_r_riscv_sub64_rela,
570 .accumulate_handler = apply_64_bit_accumulation },
571 /* 41-42 reserved for future standard use */
572 [R_RISCV_ALIGN] = { .reloc_handler = apply_r_riscv_align_rela },
573 [R_RISCV_RVC_BRANCH] = { .reloc_handler = apply_r_riscv_rvc_branch_rela },
574 [R_RISCV_RVC_JUMP] = { .reloc_handler = apply_r_riscv_rvc_jump_rela },
575 /* 46-50 reserved for future standard use */
576 [R_RISCV_RELAX] = { .reloc_handler = apply_r_riscv_relax_rela },
577 [R_RISCV_SUB6] = { .reloc_handler = apply_r_riscv_sub6_rela,
578 .accumulate_handler = apply_6_bit_accumulation },
579 [R_RISCV_SET6] = { .reloc_handler = apply_r_riscv_set6_rela,
580 .accumulate_handler = apply_6_bit_accumulation },
581 [R_RISCV_SET8] = { .reloc_handler = apply_r_riscv_set8_rela,
582 .accumulate_handler = apply_8_bit_accumulation },
583 [R_RISCV_SET16] = { .reloc_handler = apply_r_riscv_set16_rela,
584 .accumulate_handler = apply_16_bit_accumulation },
585 [R_RISCV_SET32] = { .reloc_handler = apply_r_riscv_set32_rela,
586 .accumulate_handler = apply_32_bit_accumulation },
587 [R_RISCV_32_PCREL] = { .reloc_handler = apply_r_riscv_32_pcrel_rela },
588 [R_RISCV_IRELATIVE] = { .reloc_handler = dynamic_linking_not_supported },
589 [R_RISCV_PLT32] = { .reloc_handler = apply_r_riscv_plt32_rela },
590 [R_RISCV_SET_ULEB128] = { .reloc_handler = apply_r_riscv_set_uleb128,
591 .accumulate_handler = apply_uleb128_accumulation },
592 [R_RISCV_SUB_ULEB128] = { .reloc_handler = apply_r_riscv_sub_uleb128,
593 .accumulate_handler = apply_uleb128_accumulation },
594 /* 62-191 reserved for future standard use */
595 /* 192-255 nonstandard ABI extensions */
599 process_accumulated_relocations(struct module *me,
600 struct hlist_head **relocation_hashtable,
601 struct list_head *used_buckets_list)
604 * Only ADD/SUB/SET/ULEB128 should end up here.
606 * Each bucket may have more than one relocation location. All
607 * relocations for a location are stored in a list in a bucket.
609 * Relocations are applied to a temp variable before being stored to the
610 * provided location to check for overflow. This also allows ULEB128 to
611 * properly decide how many entries are needed before storing to
612 * location. The final value is stored into location using the handler
613 * for the last relocation to an address.
615 * Three layers of indexing:
616 * - Each of the buckets in use
617 * - Groups of relocations in each bucket by location address
618 * - Each relocation entry for a location address
620 struct used_bucket *bucket_iter;
621 struct used_bucket *bucket_iter_tmp;
622 struct relocation_head *rel_head_iter;
623 struct hlist_node *rel_head_iter_tmp;
624 struct relocation_entry *rel_entry_iter;
625 struct relocation_entry *rel_entry_iter_tmp;
630 list_for_each_entry_safe(bucket_iter, bucket_iter_tmp,
631 used_buckets_list, head) {
632 hlist_for_each_entry_safe(rel_head_iter, rel_head_iter_tmp,
633 bucket_iter->bucket, node) {
635 location = rel_head_iter->location;
636 list_for_each_entry_safe(rel_entry_iter,
638 rel_head_iter->rel_entry,
640 curr_type = rel_entry_iter->type;
641 reloc_handlers[curr_type].reloc_handler(
642 me, &buffer, rel_entry_iter->value);
643 kfree(rel_entry_iter);
645 reloc_handlers[curr_type].accumulate_handler(
646 me, location, buffer);
647 kfree(rel_head_iter);
652 kfree(*relocation_hashtable);
655 static int add_relocation_to_accumulate(struct module *me, int type,
657 unsigned int hashtable_bits, Elf_Addr v,
658 struct hlist_head *relocation_hashtable,
659 struct list_head *used_buckets_list)
661 struct relocation_entry *entry;
662 struct relocation_head *rel_head;
663 struct hlist_head *current_head;
664 struct used_bucket *bucket;
667 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
672 INIT_LIST_HEAD(&entry->head);
676 hash = hash_min((uintptr_t)location, hashtable_bits);
678 current_head = &relocation_hashtable[hash];
681 * Search for the relocation_head for the relocations that happen at the
685 struct relocation_head *rel_head_iter;
687 hlist_for_each_entry(rel_head_iter, current_head, node) {
688 if (rel_head_iter->location == location) {
690 rel_head = rel_head_iter;
696 * If there has not yet been any relocations at the provided location,
697 * create a relocation_head for that location and populate it with this
701 rel_head = kmalloc(sizeof(*rel_head), GFP_KERNEL);
708 rel_head->rel_entry =
709 kmalloc(sizeof(struct list_head), GFP_KERNEL);
711 if (!rel_head->rel_entry) {
717 INIT_LIST_HEAD(rel_head->rel_entry);
718 rel_head->location = location;
719 INIT_HLIST_NODE(&rel_head->node);
720 if (!current_head->first) {
722 kmalloc(sizeof(struct used_bucket), GFP_KERNEL);
726 kfree(rel_head->rel_entry);
731 INIT_LIST_HEAD(&bucket->head);
732 bucket->bucket = current_head;
733 list_add(&bucket->head, used_buckets_list);
735 hlist_add_head(&rel_head->node, current_head);
738 /* Add relocation to head of discovered rel_head */
739 list_add_tail(&entry->head, rel_head->rel_entry);
745 initialize_relocation_hashtable(unsigned int num_relocations,
746 struct hlist_head **relocation_hashtable)
748 /* Can safely assume that bits is not greater than sizeof(long) */
749 unsigned long hashtable_size = roundup_pow_of_two(num_relocations);
751 * When hashtable_size == 1, hashtable_bits == 0.
752 * This is valid because the hashing algorithm returns 0 in this case.
754 unsigned int hashtable_bits = ilog2(hashtable_size);
757 * Double size of hashtable if num_relocations * 1.25 is greater than
760 int should_double_size = ((num_relocations + (num_relocations >> 2)) > (hashtable_size));
762 hashtable_bits += should_double_size;
764 hashtable_size <<= should_double_size;
766 *relocation_hashtable = kmalloc_array(hashtable_size,
767 sizeof(**relocation_hashtable),
769 if (!*relocation_hashtable)
772 __hash_init(*relocation_hashtable, hashtable_size);
774 return hashtable_bits;
777 int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
778 unsigned int symindex, unsigned int relsec,
781 Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
782 int (*handler)(struct module *me, void *location, Elf_Addr v);
785 unsigned int i, type;
786 unsigned int j_idx = 0;
789 unsigned int num_relocations = sechdrs[relsec].sh_size / sizeof(*rel);
790 struct hlist_head *relocation_hashtable;
791 struct list_head used_buckets_list;
792 unsigned int hashtable_bits;
794 hashtable_bits = initialize_relocation_hashtable(num_relocations,
795 &relocation_hashtable);
797 if (!relocation_hashtable)
800 INIT_LIST_HEAD(&used_buckets_list);
802 pr_debug("Applying relocate section %u to %u\n", relsec,
803 sechdrs[relsec].sh_info);
805 for (i = 0; i < num_relocations; i++) {
806 /* This is where to make the change */
807 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
809 /* This is the symbol it is referring to */
810 sym = (Elf_Sym *)sechdrs[symindex].sh_addr
811 + ELF_RISCV_R_SYM(rel[i].r_info);
812 if (IS_ERR_VALUE(sym->st_value)) {
813 /* Ignore unresolved weak symbol */
814 if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
816 pr_warn("%s: Unknown symbol %s\n",
817 me->name, strtab + sym->st_name);
821 type = ELF_RISCV_R_TYPE(rel[i].r_info);
823 if (type < ARRAY_SIZE(reloc_handlers))
824 handler = reloc_handlers[type].reloc_handler;
829 pr_err("%s: Unknown relocation type %u\n",
834 v = sym->st_value + rel[i].r_addend;
836 if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
837 unsigned int j = j_idx;
841 unsigned long hi20_loc =
842 sechdrs[sechdrs[relsec].sh_info].sh_addr
844 u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
846 /* Find the corresponding HI20 relocation entry */
847 if (hi20_loc == sym->st_value
848 && (hi20_type == R_RISCV_PCREL_HI20
849 || hi20_type == R_RISCV_GOT_HI20)) {
852 (Elf_Sym *)sechdrs[symindex].sh_addr
853 + ELF_RISCV_R_SYM(rel[j].r_info);
854 unsigned long hi20_sym_val =
859 size_t offset = hi20_sym_val - hi20_loc;
860 if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
861 && hi20_type == R_RISCV_GOT_HI20) {
862 offset = module_emit_got_entry(
864 offset = offset - hi20_loc;
866 hi20 = (offset + 0x800) & 0xfffff000;
867 lo12 = offset - hi20;
875 if (j > sechdrs[relsec].sh_size / sizeof(*rel))
878 } while (j_idx != j);
882 "%s: Can not find HI20 relocation information\n",
887 /* Record the previous j-loop end index */
891 if (reloc_handlers[type].accumulate_handler)
892 res = add_relocation_to_accumulate(me, type, location,
894 relocation_hashtable,
897 res = handler(me, location, v);
902 process_accumulated_relocations(me, &relocation_hashtable,
908 #if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
909 void *module_alloc(unsigned long size)
911 return __vmalloc_node_range(size, 1, MODULES_VADDR,
912 MODULES_END, GFP_KERNEL,
913 PAGE_KERNEL, VM_FLUSH_RESET_PERMS,
915 __builtin_return_address(0));
919 int module_finalize(const Elf_Ehdr *hdr,
920 const Elf_Shdr *sechdrs,
925 s = find_section(hdr, sechdrs, ".alternative");
927 apply_module_alternatives((void *)s->sh_addr, s->sh_size);