blob: 76aa96a9fc08165783566100155776862b780ad9 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009struct module;
David Brazdil0f672f62019-12-10 10:32:29 +000010unsigned long module_emit_got_entry(struct module *mod, unsigned long val);
11unsigned long module_emit_plt_entry(struct module *mod, unsigned long val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012
13#ifdef CONFIG_MODULE_SECTIONS
14struct mod_section {
David Brazdil0f672f62019-12-10 10:32:29 +000015 Elf_Shdr *shdr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016 int num_entries;
17 int max_entries;
18};
19
20struct mod_arch_specific {
21 struct mod_section got;
22 struct mod_section plt;
23 struct mod_section got_plt;
24};
25
26struct got_entry {
David Brazdil0f672f62019-12-10 10:32:29 +000027 unsigned long symbol_addr; /* the real variable address */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000028};
29
David Brazdil0f672f62019-12-10 10:32:29 +000030static inline struct got_entry emit_got_entry(unsigned long val)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031{
32 return (struct got_entry) {val};
33}
34
David Brazdil0f672f62019-12-10 10:32:29 +000035static inline struct got_entry *get_got_entry(unsigned long val,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036 const struct mod_section *sec)
37{
David Brazdil0f672f62019-12-10 10:32:29 +000038 struct got_entry *got = (struct got_entry *)(sec->shdr->sh_addr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039 int i;
40 for (i = 0; i < sec->num_entries; i++) {
41 if (got[i].symbol_addr == val)
42 return &got[i];
43 }
44 return NULL;
45}
46
47struct plt_entry {
48 /*
49 * Trampoline code to real target address. The return address
50 * should be the original (pc+4) before entring plt entry.
51 */
52 u32 insn_auipc; /* auipc t0, 0x0 */
53 u32 insn_ld; /* ld t1, 0x10(t0) */
54 u32 insn_jr; /* jr t1 */
55};
56
57#define OPC_AUIPC 0x0017
58#define OPC_LD 0x3003
59#define OPC_JALR 0x0067
60#define REG_T0 0x5
61#define REG_T1 0x6
62
David Brazdil0f672f62019-12-10 10:32:29 +000063static inline struct plt_entry emit_plt_entry(unsigned long val,
64 unsigned long plt,
65 unsigned long got_plt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066{
67 /*
68 * U-Type encoding:
69 * +------------+----------+----------+
70 * | imm[31:12] | rd[11:7] | opc[6:0] |
71 * +------------+----------+----------+
72 *
73 * I-Type encoding:
74 * +------------+------------+--------+----------+----------+
75 * | imm[31:20] | rs1[19:15] | funct3 | rd[11:7] | opc[6:0] |
76 * +------------+------------+--------+----------+----------+
77 *
78 */
David Brazdil0f672f62019-12-10 10:32:29 +000079 unsigned long offset = got_plt - plt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000080 u32 hi20 = (offset + 0x800) & 0xfffff000;
81 u32 lo12 = (offset - hi20);
82 return (struct plt_entry) {
83 OPC_AUIPC | (REG_T0 << 7) | hi20,
84 OPC_LD | (lo12 << 20) | (REG_T0 << 15) | (REG_T1 << 7),
85 OPC_JALR | (REG_T1 << 15)
86 };
87}
88
David Brazdil0f672f62019-12-10 10:32:29 +000089static inline int get_got_plt_idx(unsigned long val, const struct mod_section *sec)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000090{
91 struct got_entry *got_plt = (struct got_entry *)sec->shdr->sh_addr;
92 int i;
93 for (i = 0; i < sec->num_entries; i++) {
94 if (got_plt[i].symbol_addr == val)
95 return i;
96 }
97 return -1;
98}
99
David Brazdil0f672f62019-12-10 10:32:29 +0000100static inline struct plt_entry *get_plt_entry(unsigned long val,
101 const struct mod_section *sec_plt,
102 const struct mod_section *sec_got_plt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000103{
104 struct plt_entry *plt = (struct plt_entry *)sec_plt->shdr->sh_addr;
105 int got_plt_idx = get_got_plt_idx(val, sec_got_plt);
106 if (got_plt_idx >= 0)
107 return plt + got_plt_idx;
108 else
109 return NULL;
110}
111
112#endif /* CONFIG_MODULE_SECTIONS */
113
114#endif /* _ASM_RISCV_MODULE_H */