2023-09-26 08:15:02

by Tiezhu Yang

[permalink] [raw]
Subject: [PATCH v1 0/3] Add support for 32/64_PCREL relocation type

Tiezhu Yang (3):
LoongArch: Define relocation types added in ABI v2.10
LoongArch: Add support for 32_PCREL relocation type
LoongArch: Add support for 64_PCREL relocation type

arch/loongarch/include/asm/elf.h | 9 +++++++++
arch/loongarch/kernel/module.c | 22 +++++++++++++++++++++-
2 files changed, 30 insertions(+), 1 deletion(-)

--
2.1.0


2023-09-26 08:18:08

by Tiezhu Yang

[permalink] [raw]
Subject: [PATCH v1 1/3] LoongArch: Define relocation types added in ABI v2.10

The relocation types from 101 to 109 are used by GNU binutils >= 2.41,
add their definitions to use them in later patches.

Link: https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=include/elf/loongarch.h#l230
Signed-off-by: Tiezhu Yang <[email protected]>
Cc: <[email protected]>
---
arch/loongarch/include/asm/elf.h | 9 +++++++++
arch/loongarch/kernel/module.c | 2 +-
2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/arch/loongarch/include/asm/elf.h b/arch/loongarch/include/asm/elf.h
index 7af0ceb..b9a4ab5 100644
--- a/arch/loongarch/include/asm/elf.h
+++ b/arch/loongarch/include/asm/elf.h
@@ -111,6 +111,15 @@
#define R_LARCH_TLS_GD_HI20 98
#define R_LARCH_32_PCREL 99
#define R_LARCH_RELAX 100
+#define R_LARCH_DELETE 101
+#define R_LARCH_ALIGN 102
+#define R_LARCH_PCREL20_S2 103
+#define R_LARCH_CFA 104
+#define R_LARCH_ADD6 105
+#define R_LARCH_SUB6 106
+#define R_LARCH_ADD_ULEB128 107
+#define R_LARCH_SUB_ULEB128 108
+#define R_LARCH_64_PCREL 109

#ifndef ELF_ARCH

diff --git a/arch/loongarch/kernel/module.c b/arch/loongarch/kernel/module.c
index b8b8608..c3f9d2f 100644
--- a/arch/loongarch/kernel/module.c
+++ b/arch/loongarch/kernel/module.c
@@ -382,7 +382,7 @@ typedef int (*reloc_rela_handler)(struct module *mod, u32 *location, Elf_Addr v,

/* The handlers for known reloc types */
static reloc_rela_handler reloc_rela_handlers[] = {
- [R_LARCH_NONE ... R_LARCH_RELAX] = apply_r_larch_error,
+ [R_LARCH_NONE ... R_LARCH_64_PCREL] = apply_r_larch_error,

[R_LARCH_NONE] = apply_r_larch_none,
[R_LARCH_32] = apply_r_larch_32,
--
2.1.0

2023-09-26 08:22:49

by Tiezhu Yang

[permalink] [raw]
Subject: [PATCH v1 2/3] LoongArch: Add support for 32_PCREL relocation type

When build and update kernel with the latest upstream binutils and
loongson3_defconfig, it can not start the system, the serial console
shows "You are in emergency mode", there are more info like this:

kmod: zsmalloc: Unsupport relocation type 99, please add its support.
kmod: fuse: Unsupport relocation type 99, please add its support.
kmod: ipmi_msghandler: Unsupport relocation type 99, please add its support.
kmod: ipmi_msghandler: Unsupport relocation type 99, please add its support.
kmod: pstore: Unsupport relocation type 99, please add its support.
kmod: drm_display_helper: Unsupport relocation type 99, please add its support.
kmod: drm_display_helper: Unsupport relocation type 99, please add its support.
kmod: drm_display_helper: Unsupport relocation type 99, please add its support.
kmod: fuse: Unsupport relocation type 99, please add its support.
kmod: fat: Unsupport relocation type 99, please add its support.

This is because the latest upstream binutils replaces a pair of ADD32
and SUB32 with 32_PCREL, so add support for 32_PCREL relocation type.

Link: https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=ecb802d02eeb
Co-developed-by: Youling Tang <[email protected]>
Signed-off-by: Youling Tang <[email protected]>
Signed-off-by: Tiezhu Yang <[email protected]>
Cc: <[email protected]>
---
arch/loongarch/kernel/module.c | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/arch/loongarch/kernel/module.c b/arch/loongarch/kernel/module.c
index c3f9d2f..9e10c44 100644
--- a/arch/loongarch/kernel/module.c
+++ b/arch/loongarch/kernel/module.c
@@ -367,6 +367,15 @@ static int apply_r_larch_got_pc(struct module *mod,
return apply_r_larch_pcala(mod, location, got, rela_stack, rela_stack_top, type);
}

+static int apply_r_larch_32_pcrel(struct module *mod, u32 *location, Elf_Addr v,
+ s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
+{
+ ptrdiff_t offset = (void *)v - (void *)location;
+
+ *(u32 *)location = offset;
+ return 0;
+}
+
/*
* reloc_handlers_rela() - Apply a particular relocation to a module
* @mod: the module to apply the reloc to
@@ -396,6 +405,7 @@ static reloc_rela_handler reloc_rela_handlers[] = {
[R_LARCH_SOP_POP_32_S_10_5 ... R_LARCH_SOP_POP_32_U] = apply_r_larch_sop_imm_field,
[R_LARCH_ADD32 ... R_LARCH_SUB64] = apply_r_larch_add_sub,
[R_LARCH_PCALA_HI20...R_LARCH_PCALA64_HI12] = apply_r_larch_pcala,
+ [R_LARCH_32_PCREL] = apply_r_larch_32_pcrel,
};

int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
--
2.1.0

2023-09-26 08:23:05

by Tiezhu Yang

[permalink] [raw]
Subject: [PATCH v1 3/3] LoongArch: Add support for 64_PCREL relocation type

When build and update kernel with the latest upstream binutils and
loongson3_defconfig, the serial console works well with the previous
32_PCREL patch, but it can not enter the graphical interface due to
it failed to start lightdm service, there are more info like this:

kmod: zsmalloc: Unknown relocation type 109
kmod: fuse: Unknown relocation type 109
kmod: fuse: Unknown relocation type 109
kmod: radeon: Unknown relocation type 109
kmod: nf_tables: Unknown relocation type 109
kmod: nf_tables: Unknown relocation type 109

This is because the latest upstream binutils replaces a pair of ADD64
and SUB64 with 64_PCREL, so add support for 64_PCREL relocation type.

Link: https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=ecb802d02eeb
Signed-off-by: Tiezhu Yang <[email protected]>
Cc: <[email protected]>
---
arch/loongarch/kernel/module.c | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/arch/loongarch/kernel/module.c b/arch/loongarch/kernel/module.c
index 9e10c44..b13b285 100644
--- a/arch/loongarch/kernel/module.c
+++ b/arch/loongarch/kernel/module.c
@@ -376,6 +376,15 @@ static int apply_r_larch_32_pcrel(struct module *mod, u32 *location, Elf_Addr v,
return 0;
}

+static int apply_r_larch_64_pcrel(struct module *mod, u32 *location, Elf_Addr v,
+ s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
+{
+ ptrdiff_t offset = (void *)v - (void *)location;
+
+ *(u64 *)location = offset;
+ return 0;
+}
+
/*
* reloc_handlers_rela() - Apply a particular relocation to a module
* @mod: the module to apply the reloc to
@@ -406,6 +415,7 @@ static reloc_rela_handler reloc_rela_handlers[] = {
[R_LARCH_ADD32 ... R_LARCH_SUB64] = apply_r_larch_add_sub,
[R_LARCH_PCALA_HI20...R_LARCH_PCALA64_HI12] = apply_r_larch_pcala,
[R_LARCH_32_PCREL] = apply_r_larch_32_pcrel,
+ [R_LARCH_64_PCREL] = apply_r_larch_64_pcrel,
};

int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
--
2.1.0

2023-09-26 09:43:29

by Huacai Chen

[permalink] [raw]
Subject: Re: [PATCH v1 0/3] Add support for 32/64_PCREL relocation type

Queued for loongarch-fixes, thanks.

Huacai

On Tue, Sep 26, 2023 at 4:11 PM Tiezhu Yang <[email protected]> wrote:
>
> Tiezhu Yang (3):
> LoongArch: Define relocation types added in ABI v2.10
> LoongArch: Add support for 32_PCREL relocation type
> LoongArch: Add support for 64_PCREL relocation type
>
> arch/loongarch/include/asm/elf.h | 9 +++++++++
> arch/loongarch/kernel/module.c | 22 +++++++++++++++++++++-
> 2 files changed, 30 insertions(+), 1 deletion(-)
>
> --
> 2.1.0
>