Skip to contentSkip to conversation

modpost: fix section mismatch message for R_ARM_ABS32

PATCH In review Masahiro Yamada 2 participants 3 messages May 15, 2023 – May 21, 2023 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 modpost: fix section mismatch message for R_ARM_ABS32

The section mismatch check does not show proper warning messages for ARM.

Here, very simple test code.

    #include <linux/init.h>

    static int __initdata foo;

    void set_foo(int x)
    {
            foo = x;
    }

    int get_foo(int x)
    {
            return foo;
    }

If I compile it for ARM, modpost does not show the symbol name.

WARNING: modpost: vmlinux.o: section mismatch in reference: set_foo (section: .text) -> (unknown) (section: .init.data)
WARNING: modpost: vmlinux.o: section mismatch in reference: get_foo (section: .text) -> (unknown) (section: .init.data)

If I compile it for other architectures, modpost shows the correct symbol name.

WARNING: modpost: vmlinux.o: section mismatch in reference: set_foo (section: .text) -> foo (section: .init.data)
WARNING: modpost: vmlinux.o: section mismatch in reference: get_foo (section: .text) -> foo (section: .init.data)

For R_ARM_ABS32, addend_arm_rel() sets r->r_addend to a wrong value.

arch/arm/kernel/module.c handles R_ARM_ABS32 as follows:

        case R_ARM_ABS32:
        case R_ARM_TARGET1:
                *(u32 *)loc += sym->st_value;

I just mimicked it in modpost.

Fixes 56a974fa2d59 ("kbuild: make better section mismatch reports on arm")

---

 scripts/mod/modpost.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
scripts/mod/modpost.c +4 -3
index d4531d09984d..c93780d93caf 100644
@@ -1460,12 +1460,13 @@ static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
 {
 	unsigned int r_typ = ELF_R_TYPE(r->r_info);
+	unsigned int *location = reloc_location(elf, sechdr, r);
+	Elf_Sym *sym;
 
 	switch (r_typ) {
 	case R_ARM_ABS32:
-		/* From ARM ABI: (S + A) | T */
-		r->r_addend = (int)(long)
-			      (elf->symtab_start + ELF_R_SYM(r->r_info));
+		sym = elf->symtab_start + ELF_R_SYM(r->r_info);
+		r->r_addend = TO_NATIVE(*location) + sym->st_value;
 		break;
 	case R_ARM_PC24:
 	case R_ARM_CALL:
Signature · 1 line
2.39.2
#1 lore raw
Quoted 51 lines · Masahiro Yamada

The section mismatch check does not show proper warning messages for ARM.

Here, very simple test code.

    #include <linux/init.h>

    static int __initdata foo;

    void set_foo(int x)
    {
            foo = x;
    }

    int get_foo(int x)
    {
            return foo;
    }

If I compile it for ARM, modpost does not show the symbol name.

WARNING: modpost: vmlinux.o: section mismatch in reference: set_foo (section: .text) -> (unknown) (section: .init.data)
WARNING: modpost: vmlinux.o: section mismatch in reference: get_foo (section: .text) -> (unknown) (section: .init.data)

If I compile it for other architectures, modpost shows the correct symbol name.

WARNING: modpost: vmlinux.o: section mismatch in reference: set_foo (section: .text) -> foo (section: .init.data)
WARNING: modpost: vmlinux.o: section mismatch in reference: get_foo (section: .text) -> foo (section: .init.data)

For R_ARM_ABS32, addend_arm_rel() sets r->r_addend to a wrong value.

arch/arm/kernel/module.c handles R_ARM_ABS32 as follows:

        case R_ARM_ABS32:
        case R_ARM_TARGET1:
                *(u32 *)loc += sym->st_value;

I just mimicked it in modpost.

Fixes 56a974fa2d59 ("kbuild: make better section mismatch reports on arm")

---

 scripts/mod/modpost.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
scripts/mod/modpost.c +0 -0
index d4531d09984d..c93780d93caf 100644

scripts/mod/modpost.c static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) · line 1463

Quoted 4 lines
scripts/mod/modpost.c +1 -0
@@ -1460,12 +1460,13 @@ static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
 {
        unsigned int r_typ = ELF_R_TYPE(r->r_info);
+       unsigned int *location = reloc_location(elf, sechdr, r);

If location is only used in one case of the switch, consider computing location only in that case.

Quoted 15 lines
scripts/mod/modpost.c +3 -4
+       Elf_Sym *sym;
 
        switch (r_typ) {
        case R_ARM_ABS32:
-               /* From ARM ABI: (S + A) | T */
-               r->r_addend = (int)(long)
-                             (elf->symtab_start + ELF_R_SYM(r->r_info));
+               sym = elf->symtab_start + ELF_R_SYM(r->r_info);
+               r->r_addend = TO_NATIVE(*location) + sym->st_value;
                break;
        case R_ARM_PC24:
        case R_ARM_CALL:
--

2.39.2

Signature · 2 lines
Thanks,
~Nick Desaulniers
#2 lore raw
Quoted 48 lines · Nick Desaulniers
Quoted 46 lines · Masahiro Yamada

The section mismatch check does not show proper warning messages for ARM.

Here, very simple test code.

    #include <linux/init.h>

    static int __initdata foo;

    void set_foo(int x)
    {
            foo = x;
    }

    int get_foo(int x)
    {
            return foo;
    }

If I compile it for ARM, modpost does not show the symbol name.

WARNING: modpost: vmlinux.o: section mismatch in reference: set_foo (section: .text) -> (unknown) (section: .init.data)
WARNING: modpost: vmlinux.o: section mismatch in reference: get_foo (section: .text) -> (unknown) (section: .init.data)

If I compile it for other architectures, modpost shows the correct symbol name.

WARNING: modpost: vmlinux.o: section mismatch in reference: set_foo (section: .text) -> foo (section: .init.data)
WARNING: modpost: vmlinux.o: section mismatch in reference: get_foo (section: .text) -> foo (section: .init.data)

For R_ARM_ABS32, addend_arm_rel() sets r->r_addend to a wrong value.

arch/arm/kernel/module.c handles R_ARM_ABS32 as follows:

        case R_ARM_ABS32:
        case R_ARM_TARGET1:
                *(u32 *)loc += sym->st_value;

I just mimicked it in modpost.

Fixes 56a974fa2d59 ("kbuild: make better section mismatch reports on arm")

---

 scripts/mod/modpost.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
Quoted 12 lines

scripts/mod/modpost.c static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) · line 1463

Quoted 9 lines
scripts/mod/modpost.c +1 -0
index d4531d09984d..c93780d93caf 100644
@@ -1460,12 +1460,13 @@ static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
 {
        unsigned int r_typ = ELF_R_TYPE(r->r_info);
+       unsigned int *location = reloc_location(elf, sechdr, r);

If location is only used in one case of the switch, consider computing location only in that case.

I really suspect the other case labels are also wrong.

For example, see R_ARM_PC24 in arch/arm/kernel/module.c

The offset is encoded in the instruction.
If you can compute the addend without reading the instruction, I do not know how.

Anyway, I will fix another breakage.
It will need 'location' as well.

Quoted 21 lines
Quoted 14 lines
scripts/mod/modpost.c +3 -4
+       Elf_Sym *sym;
 
        switch (r_typ) {
        case R_ARM_ABS32:
-               /* From ARM ABI: (S + A) | T */
-               r->r_addend = (int)(long)
-                             (elf->symtab_start + ELF_R_SYM(r->r_info));
+               sym = elf->symtab_start + ELF_R_SYM(r->r_info);
+               r->r_addend = TO_NATIVE(*location) + sym->st_value;
                break;
        case R_ARM_PC24:
        case R_ARM_CALL:
--
Quoted 2 lines

2.39.2

--
Thanks,
~Nick Desaulniers

Signature · 7 lines
Best Regards
Masahiro Yamada

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel