Skip to content

Commit bb1f85d

Browse files
xhackerustcpalmer-dabbelt
authored andcommitted
riscv: switch to relative exception tables
Similar as other architectures such as arm64, x86 and so on, use offsets relative to the exception table entry values rather than absolute addresses for both the exception locationand the fixup. However, RISCV label difference will actually produce two relocations, a pair of R_RISCV_ADD32 and R_RISCV_SUB32. Take below simple code for example: $ cat test.S .section .text 1: nop .section __ex_table,"a" .balign 4 .long (1b - .) .previous $ riscv64-linux-gnu-gcc -c test.S $ riscv64-linux-gnu-readelf -r test.o Relocation section '.rela__ex_table' at offset 0x100 contains 2 entries: Offset Info Type Sym. Value Sym. Name + Addend 000000000000 000600000023 R_RISCV_ADD32 0000000000000000 .L1^B1 + 0 000000000000 000500000027 R_RISCV_SUB32 0000000000000000 .L0 + 0 The modpost will complain the R_RISCV_SUB32 relocation, so we need to patch modpost.c to skip this relocation for .rela__ex_table section. After this patch, the __ex_table section size of defconfig vmlinux is reduced from 7072 Bytes to 3536 Bytes. Signed-off-by: Jisheng Zhang <jszhang@kernel.org> Reviewed-by: Kefeng Wang <wangkefeng.wang@huawei.com> Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent f8f2ad0 commit bb1f85d

7 files changed

Lines changed: 46 additions & 7 deletions

File tree

‎arch/riscv/include/asm/Kbuild‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# SPDX-License-Identifier: GPL-2.0
22
generic-y += early_ioremap.h
3-
generic-y += extable.h
43
generic-y += flat.h
54
generic-y += kvm_para.h
65
generic-y += user.h

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_RISCV_EXTABLE_H
3+
#define _ASM_RISCV_EXTABLE_H
4+
5+
/*
6+
* The exception table consists of pairs of relative offsets: the first
7+
* is the relative offset to an instruction that is allowed to fault,
8+
* and the second is the relative offset at which the program should
9+
* continue. No registers are modified, so it is entirely up to the
10+
* continuation code to figure out what to do.
11+
*
12+
* All the routines below use bits of fixup code that are out of line
13+
* with the main instruction path. This means when everything is well,
14+
* we don't even have to jump over them. Further, they do not intrude
15+
* on our cache or tlb entries.
16+
*/
17+
18+
struct exception_table_entry {
19+
int insn, fixup;
20+
};
21+
22+
#define ARCH_HAS_RELATIVE_EXTABLE
23+
24+
int fixup_exception(struct pt_regs *regs);
25+
#endif

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
#define _ASM_EXTABLE(from, to) \
1414
" .pushsection __ex_table, \"a\"\n" \
15-
" .balign " RISCV_SZPTR " \n" \
16-
" " RISCV_PTR "(" #from "), (" #to ")\n" \
15+
" .balign 4\n" \
16+
" .long (" #from " - .), (" #to " - .)\n" \
1717
" .popsection\n"
1818

1919
/*

‎arch/riscv/lib/uaccess.S‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
100:
88
\op \reg, \addr
99
.section __ex_table,"a"
10-
.balign RISCV_SZPTR
11-
RISCV_PTR 100b, \lbl
10+
.balign 4
11+
.long (100b - .), (\lbl - .)
1212
.previous
1313
.endm
1414

‎arch/riscv/mm/extable.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ int fixup_exception(struct pt_regs *regs)
2828
return rv_bpf_fixup_exception(fixup, regs);
2929
#endif
3030

31-
regs->epc = fixup->fixup;
31+
regs->epc = (unsigned long)&fixup->fixup + fixup->fixup;
3232
return 1;
3333
}

‎scripts/mod/modpost.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,14 @@ static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
18301830
return 0;
18311831
}
18321832

1833+
#ifndef EM_RISCV
1834+
#define EM_RISCV 243
1835+
#endif
1836+
1837+
#ifndef R_RISCV_SUB32
1838+
#define R_RISCV_SUB32 39
1839+
#endif
1840+
18331841
static void section_rela(const char *modname, struct elf_info *elf,
18341842
Elf_Shdr *sechdr)
18351843
{
@@ -1866,6 +1874,13 @@ static void section_rela(const char *modname, struct elf_info *elf,
18661874
r_sym = ELF_R_SYM(r.r_info);
18671875
#endif
18681876
r.r_addend = TO_NATIVE(rela->r_addend);
1877+
switch (elf->hdr->e_machine) {
1878+
case EM_RISCV:
1879+
if (!strcmp("__ex_table", fromsec) &&
1880+
ELF_R_TYPE(r.r_info) == R_RISCV_SUB32)
1881+
continue;
1882+
break;
1883+
}
18691884
sym = elf->symtab_start + r_sym;
18701885
/* Skip special sections */
18711886
if (is_shndx_special(sym->st_shndx))

‎scripts/sorttable.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,14 @@ static int do_file(char const *const fname, void *addr)
346346
case EM_PARISC:
347347
case EM_PPC:
348348
case EM_PPC64:
349+
case EM_RISCV:
349350
custom_sort = sort_relative_table;
350351
break;
351352
case EM_ARCOMPACT:
352353
case EM_ARCV2:
353354
case EM_ARM:
354355
case EM_MICROBLAZE:
355356
case EM_MIPS:
356-
case EM_RISCV:
357357
case EM_XTENSA:
358358
break;
359359
default:

0 commit comments

Comments
 (0)