1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) 2017 Andes Technology Corporation */
4 #ifndef _ASM_RISCV_MODULE_H
5 #define _ASM_RISCV_MODULE_H
7 #include <asm-generic/module.h>
9 #define MODULE_ARCH_VERMAGIC "riscv"
12 unsigned long module_emit_got_entry(struct module
*mod
, unsigned long val
);
13 unsigned long module_emit_plt_entry(struct module
*mod
, unsigned long val
);
15 #ifdef CONFIG_MODULE_SECTIONS
22 struct mod_arch_specific
{
23 struct mod_section got
;
24 struct mod_section plt
;
25 struct mod_section got_plt
;
29 unsigned long symbol_addr
; /* the real variable address */
32 static inline struct got_entry
emit_got_entry(unsigned long val
)
34 return (struct got_entry
) {val
};
37 static inline struct got_entry
*get_got_entry(unsigned long val
,
38 const struct mod_section
*sec
)
40 struct got_entry
*got
= (struct got_entry
*)(sec
->shdr
->sh_addr
);
42 for (i
= 0; i
< sec
->num_entries
; i
++) {
43 if (got
[i
].symbol_addr
== val
)
51 * Trampoline code to real target address. The return address
52 * should be the original (pc+4) before entring plt entry.
54 u32 insn_auipc
; /* auipc t0, 0x0 */
55 u32 insn_ld
; /* ld t1, 0x10(t0) */
56 u32 insn_jr
; /* jr t1 */
59 #define OPC_AUIPC 0x0017
61 #define OPC_JALR 0x0067
65 static inline struct plt_entry
emit_plt_entry(unsigned long val
,
67 unsigned long got_plt
)
71 * +------------+----------+----------+
72 * | imm[31:12] | rd[11:7] | opc[6:0] |
73 * +------------+----------+----------+
76 * +------------+------------+--------+----------+----------+
77 * | imm[31:20] | rs1[19:15] | funct3 | rd[11:7] | opc[6:0] |
78 * +------------+------------+--------+----------+----------+
81 unsigned long offset
= got_plt
- plt
;
82 u32 hi20
= (offset
+ 0x800) & 0xfffff000;
83 u32 lo12
= (offset
- hi20
);
84 return (struct plt_entry
) {
85 OPC_AUIPC
| (REG_T0
<< 7) | hi20
,
86 OPC_LD
| (lo12
<< 20) | (REG_T0
<< 15) | (REG_T1
<< 7),
87 OPC_JALR
| (REG_T1
<< 15)
91 static inline int get_got_plt_idx(unsigned long val
, const struct mod_section
*sec
)
93 struct got_entry
*got_plt
= (struct got_entry
*)sec
->shdr
->sh_addr
;
95 for (i
= 0; i
< sec
->num_entries
; i
++) {
96 if (got_plt
[i
].symbol_addr
== val
)
102 static inline struct plt_entry
*get_plt_entry(unsigned long val
,
103 const struct mod_section
*sec_plt
,
104 const struct mod_section
*sec_got_plt
)
106 struct plt_entry
*plt
= (struct plt_entry
*)sec_plt
->shdr
->sh_addr
;
107 int got_plt_idx
= get_got_plt_idx(val
, sec_got_plt
);
108 if (got_plt_idx
>= 0)
109 return plt
+ got_plt_idx
;
114 #endif /* CONFIG_MODULE_SECTIONS */
116 #endif /* _ASM_RISCV_MODULE_H */