Skip to content

Commit 2cffc95

Browse files
zongboxpalmer-dabbelt
authored andcommitted
RISC-V: Support MODULE_SECTIONS mechanism on RV32
This patch supports dynamic generate got and plt sections mechanism on rv32. It contains the modification as follows: - Always enable MODULE_SECTIONS (both rv64 and rv32) - Change the fixed size type. This patch had been tested by following modules: btrfs 6795991 0 - Live 0xa544b000 test_static_keys 17304 0 - Live 0xa28be000 zstd_compress 1198986 1 btrfs, Live 0xa2a25000 zstd_decompress 608112 1 btrfs, Live 0xa24e7000 lzo 8787 0 - Live 0xa2049000 xor 27461 1 btrfs, Live 0xa2041000 zram 78849 0 - Live 0xa2276000 netdevsim 55909 0 - Live 0xa202d000 tun 211534 0 - Live 0xa21b5000 fuse 566049 0 - Live 0xa25fb000 nfs_layout_flexfiles 192597 0 - Live 0xa229b000 ramoops 74895 0 - Live 0xa2019000 xfs 3973221 0 - Live 0xa507f000 libcrc32c 3053 2 btrfs,xfs, Live 0xa34af000 lzo_compress 17302 2 btrfs,lzo, Live 0xa347d000 lzo_decompress 7178 2 btrfs,lzo, Live 0xa3451000 raid6_pq 142086 1 btrfs, Live 0xa33a4000 reed_solomon 31022 1 ramoops, Live 0xa31eb000 test_bitmap 3734 0 - Live 0xa31af000 test_bpf 1588736 0 - Live 0xa2c11000 test_kmod 41161 0 - Live 0xa29f8000 test_module 1356 0 - Live 0xa299e000 test_printf 6024 0 [permanent], Live 0xa2971000 test_static_key_base 5797 1 test_static_keys, Live 0xa2931000 test_user_copy 4382 0 - Live 0xa28c9000 xxhash 70501 2 zstd_compress,zstd_decompress, Live 0xa2055000 Signed-off-by: Zong Li <zong@andestech.com> Signed-off-by: Palmer Dabbelt <palmer@sifive.com>
1 parent 3b56adc commit 2cffc95

3 files changed

Lines changed: 32 additions & 28 deletions

File tree

‎arch/riscv/Kconfig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ config RISCV
4040
select HAVE_ARCH_TRACEHOOK
4141
select HAVE_PCI
4242
select MODULES_USE_ELF_RELA if MODULES
43+
select MODULE_SECTIONS if MODULES
4344
select THREAD_INFO_IN_TASK
4445
select PCI_DOMAINS_GENERIC if PCI
4546
select PCI_MSI if PCI
@@ -152,7 +153,6 @@ choice
152153
bool "2GiB"
153154
config MAXPHYSMEM_128GB
154155
depends on 64BIT && CMODEL_MEDANY
155-
select MODULE_SECTIONS if MODULES
156156
bool "128GiB"
157157
endchoice
158158

‎arch/riscv/include/asm/module.h‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
#define MODULE_ARCH_VERMAGIC "riscv"
1010

1111
struct module;
12-
u64 module_emit_got_entry(struct module *mod, u64 val);
13-
u64 module_emit_plt_entry(struct module *mod, u64 val);
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);
1414

1515
#ifdef CONFIG_MODULE_SECTIONS
1616
struct mod_section {
17-
struct elf64_shdr *shdr;
17+
Elf_Shdr *shdr;
1818
int num_entries;
1919
int max_entries;
2020
};
@@ -26,18 +26,18 @@ struct mod_arch_specific {
2626
};
2727

2828
struct got_entry {
29-
u64 symbol_addr; /* the real variable address */
29+
unsigned long symbol_addr; /* the real variable address */
3030
};
3131

32-
static inline struct got_entry emit_got_entry(u64 val)
32+
static inline struct got_entry emit_got_entry(unsigned long val)
3333
{
3434
return (struct got_entry) {val};
3535
}
3636

37-
static inline struct got_entry *get_got_entry(u64 val,
37+
static inline struct got_entry *get_got_entry(unsigned long val,
3838
const struct mod_section *sec)
3939
{
40-
struct got_entry *got = (struct got_entry *)sec->shdr->sh_addr;
40+
struct got_entry *got = (struct got_entry *)(sec->shdr->sh_addr);
4141
int i;
4242
for (i = 0; i < sec->num_entries; i++) {
4343
if (got[i].symbol_addr == val)
@@ -62,7 +62,9 @@ struct plt_entry {
6262
#define REG_T0 0x5
6363
#define REG_T1 0x6
6464

65-
static inline struct plt_entry emit_plt_entry(u64 val, u64 plt, u64 got_plt)
65+
static inline struct plt_entry emit_plt_entry(unsigned long val,
66+
unsigned long plt,
67+
unsigned long got_plt)
6668
{
6769
/*
6870
* U-Type encoding:
@@ -76,7 +78,7 @@ static inline struct plt_entry emit_plt_entry(u64 val, u64 plt, u64 got_plt)
7678
* +------------+------------+--------+----------+----------+
7779
*
7880
*/
79-
u64 offset = got_plt - plt;
81+
unsigned long offset = got_plt - plt;
8082
u32 hi20 = (offset + 0x800) & 0xfffff000;
8183
u32 lo12 = (offset - hi20);
8284
return (struct plt_entry) {
@@ -86,7 +88,7 @@ static inline struct plt_entry emit_plt_entry(u64 val, u64 plt, u64 got_plt)
8688
};
8789
}
8890

89-
static inline int get_got_plt_idx(u64 val, const struct mod_section *sec)
91+
static inline int get_got_plt_idx(unsigned long val, const struct mod_section *sec)
9092
{
9193
struct got_entry *got_plt = (struct got_entry *)sec->shdr->sh_addr;
9294
int i;
@@ -97,9 +99,9 @@ static inline int get_got_plt_idx(u64 val, const struct mod_section *sec)
9799
return -1;
98100
}
99101

100-
static inline struct plt_entry *get_plt_entry(u64 val,
101-
const struct mod_section *sec_plt,
102-
const struct mod_section *sec_got_plt)
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)
103105
{
104106
struct plt_entry *plt = (struct plt_entry *)sec_plt->shdr->sh_addr;
105107
int got_plt_idx = get_got_plt_idx(val, sec_got_plt);

‎arch/riscv/kernel/module-sections.c‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
#include <linux/kernel.h>
1010
#include <linux/module.h>
1111

12-
u64 module_emit_got_entry(struct module *mod, u64 val)
12+
unsigned long module_emit_got_entry(struct module *mod, unsigned long val)
1313
{
1414
struct mod_section *got_sec = &mod->arch.got;
1515
int i = got_sec->num_entries;
1616
struct got_entry *got = get_got_entry(val, got_sec);
1717

1818
if (got)
19-
return (u64)got;
19+
return (unsigned long)got;
2020

2121
/* There is no duplicate entry, create a new one */
2222
got = (struct got_entry *)got_sec->shdr->sh_addr;
@@ -25,10 +25,10 @@ u64 module_emit_got_entry(struct module *mod, u64 val)
2525
got_sec->num_entries++;
2626
BUG_ON(got_sec->num_entries > got_sec->max_entries);
2727

28-
return (u64)&got[i];
28+
return (unsigned long)&got[i];
2929
}
3030

31-
u64 module_emit_plt_entry(struct module *mod, u64 val)
31+
unsigned long module_emit_plt_entry(struct module *mod, unsigned long val)
3232
{
3333
struct mod_section *got_plt_sec = &mod->arch.got_plt;
3434
struct got_entry *got_plt;
@@ -37,27 +37,29 @@ u64 module_emit_plt_entry(struct module *mod, u64 val)
3737
int i = plt_sec->num_entries;
3838

3939
if (plt)
40-
return (u64)plt;
40+
return (unsigned long)plt;
4141

4242
/* There is no duplicate entry, create a new one */
4343
got_plt = (struct got_entry *)got_plt_sec->shdr->sh_addr;
4444
got_plt[i] = emit_got_entry(val);
4545
plt = (struct plt_entry *)plt_sec->shdr->sh_addr;
46-
plt[i] = emit_plt_entry(val, (u64)&plt[i], (u64)&got_plt[i]);
46+
plt[i] = emit_plt_entry(val,
47+
(unsigned long)&plt[i],
48+
(unsigned long)&got_plt[i]);
4749

4850
plt_sec->num_entries++;
4951
got_plt_sec->num_entries++;
5052
BUG_ON(plt_sec->num_entries > plt_sec->max_entries);
5153

52-
return (u64)&plt[i];
54+
return (unsigned long)&plt[i];
5355
}
5456

55-
static int is_rela_equal(const Elf64_Rela *x, const Elf64_Rela *y)
57+
static int is_rela_equal(const Elf_Rela *x, const Elf_Rela *y)
5658
{
5759
return x->r_info == y->r_info && x->r_addend == y->r_addend;
5860
}
5961

60-
static bool duplicate_rela(const Elf64_Rela *rela, int idx)
62+
static bool duplicate_rela(const Elf_Rela *rela, int idx)
6163
{
6264
int i;
6365
for (i = 0; i < idx; i++) {
@@ -67,13 +69,13 @@ static bool duplicate_rela(const Elf64_Rela *rela, int idx)
6769
return false;
6870
}
6971

70-
static void count_max_entries(Elf64_Rela *relas, int num,
72+
static void count_max_entries(Elf_Rela *relas, int num,
7173
unsigned int *plts, unsigned int *gots)
7274
{
7375
unsigned int type, i;
7476

7577
for (i = 0; i < num; i++) {
76-
type = ELF64_R_TYPE(relas[i].r_info);
78+
type = ELF_RISCV_R_TYPE(relas[i].r_info);
7779
if (type == R_RISCV_CALL_PLT) {
7880
if (!duplicate_rela(relas, i))
7981
(*plts)++;
@@ -118,9 +120,9 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
118120

119121
/* Calculate the maxinum number of entries */
120122
for (i = 0; i < ehdr->e_shnum; i++) {
121-
Elf64_Rela *relas = (void *)ehdr + sechdrs[i].sh_offset;
122-
int num_rela = sechdrs[i].sh_size / sizeof(Elf64_Rela);
123-
Elf64_Shdr *dst_sec = sechdrs + sechdrs[i].sh_info;
123+
Elf_Rela *relas = (void *)ehdr + sechdrs[i].sh_offset;
124+
int num_rela = sechdrs[i].sh_size / sizeof(Elf_Rela);
125+
Elf_Shdr *dst_sec = sechdrs + sechdrs[i].sh_info;
124126

125127
if (sechdrs[i].sh_type != SHT_RELA)
126128
continue;

0 commit comments

Comments
 (0)