blob: 349df33808c4231d155d2e6018e19cc4cb19cb4d [file] [log] [blame]
Zong Liab1ef682018-03-15 16:50:41 +08001/* SPDX-License-Identifier: GPL-2.0 */
2/* Copyright (C) 2017 Andes Technology Corporation */
3
4#ifndef _ASM_RISCV_MODULE_H
5#define _ASM_RISCV_MODULE_H
6
7#include <asm-generic/module.h>
8
9#define MODULE_ARCH_VERMAGIC "riscv"
10
11u64 module_emit_got_entry(struct module *mod, u64 val);
12u64 module_emit_plt_entry(struct module *mod, u64 val);
13
14#ifdef CONFIG_MODULE_SECTIONS
15struct mod_section {
16 struct elf64_shdr *shdr;
17 int num_entries;
18 int max_entries;
19};
20
21struct mod_arch_specific {
22 struct mod_section got;
23 struct mod_section plt;
Zong Lib8bde0e2018-03-15 16:50:42 +080024 struct mod_section got_plt;
Zong Liab1ef682018-03-15 16:50:41 +080025};
26
27struct got_entry {
28 u64 symbol_addr; /* the real variable address */
29};
30
31static inline struct got_entry emit_got_entry(u64 val)
32{
33 return (struct got_entry) {val};
34}
35
36static inline struct got_entry *get_got_entry(u64 val,
37 const struct mod_section *sec)
38{
39 struct got_entry *got = (struct got_entry *)sec->shdr->sh_addr;
40 int i;
41 for (i = 0; i < sec->num_entries; i++) {
42 if (got[i].symbol_addr == val)
43 return &got[i];
44 }
45 return NULL;
46}
47
48struct plt_entry {
49 /*
50 * Trampoline code to real target address. The return address
51 * should be the original (pc+4) before entring plt entry.
Zong Liab1ef682018-03-15 16:50:41 +080052 */
53 u32 insn_auipc; /* auipc t0, 0x0 */
54 u32 insn_ld; /* ld t1, 0x10(t0) */
55 u32 insn_jr; /* jr t1 */
Zong Liab1ef682018-03-15 16:50:41 +080056};
57
58#define OPC_AUIPC 0x0017
59#define OPC_LD 0x3003
60#define OPC_JALR 0x0067
61#define REG_T0 0x5
62#define REG_T1 0x6
Zong Liab1ef682018-03-15 16:50:41 +080063
Zong Lib8bde0e2018-03-15 16:50:42 +080064static inline struct plt_entry emit_plt_entry(u64 val, u64 plt, u64 got_plt)
Zong Liab1ef682018-03-15 16:50:41 +080065{
66 /*
67 * U-Type encoding:
68 * +------------+----------+----------+
69 * | imm[31:12] | rd[11:7] | opc[6:0] |
70 * +------------+----------+----------+
71 *
72 * I-Type encoding:
73 * +------------+------------+--------+----------+----------+
74 * | imm[31:20] | rs1[19:15] | funct3 | rd[11:7] | opc[6:0] |
75 * +------------+------------+--------+----------+----------+
76 *
77 */
Zong Lib8bde0e2018-03-15 16:50:42 +080078 u64 offset = got_plt - plt;
79 u32 hi20 = (offset + 0x800) & 0xfffff000;
80 u32 lo12 = (offset - hi20);
Zong Liab1ef682018-03-15 16:50:41 +080081 return (struct plt_entry) {
Zong Lib8bde0e2018-03-15 16:50:42 +080082 OPC_AUIPC | (REG_T0 << 7) | hi20,
83 OPC_LD | (lo12 << 20) | (REG_T0 << 15) | (REG_T1 << 7),
84 OPC_JALR | (REG_T1 << 15)
Zong Liab1ef682018-03-15 16:50:41 +080085 };
86}
87
Zong Lib8bde0e2018-03-15 16:50:42 +080088static inline int get_got_plt_idx(u64 val, const struct mod_section *sec)
Zong Liab1ef682018-03-15 16:50:41 +080089{
Zong Lib8bde0e2018-03-15 16:50:42 +080090 struct got_entry *got_plt = (struct got_entry *)sec->shdr->sh_addr;
Zong Liab1ef682018-03-15 16:50:41 +080091 int i;
92 for (i = 0; i < sec->num_entries; i++) {
Zong Lib8bde0e2018-03-15 16:50:42 +080093 if (got_plt[i].symbol_addr == val)
94 return i;
Zong Liab1ef682018-03-15 16:50:41 +080095 }
Zong Lib8bde0e2018-03-15 16:50:42 +080096 return -1;
97}
98
99static inline struct plt_entry *get_plt_entry(u64 val,
100 const struct mod_section *sec_plt,
101 const struct mod_section *sec_got_plt)
102{
103 struct plt_entry *plt = (struct plt_entry *)sec_plt->shdr->sh_addr;
104 int got_plt_idx = get_got_plt_idx(val, sec_got_plt);
105 if (got_plt_idx >= 0)
106 return plt + got_plt_idx;
107 else
108 return NULL;
Zong Liab1ef682018-03-15 16:50:41 +0800109}
110
111#endif /* CONFIG_MODULE_SECTIONS */
112
113#endif /* _ASM_RISCV_MODULE_H */