mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] modpost: fix section mismatch message for RELA
@ 2023-10-31 17:46 Masahiro Yamada
  2023-10-31 18:07 ` Nick Desaulniers
  0 siblings, 1 reply; 2+ messages in thread
From: Masahiro Yamada @ 2023-10-31 17:46 UTC (permalink / raw)
  To: linux-kbuild
  Cc: linux-kernel, Masahiro Yamada, Albert Ou, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, Palmer Dabbelt, Paul Walmsley,
	linux-riscv

The section mismatch check prints a bogus symbol name on some
architectures.

[test code]

  #include <linux/init.h>

  int __initdata foo;
  int get_foo(void) { return foo; }

If you compile it with GCC for riscv or loongarch, modpost will show an
incorrect symbol name:

  WARNING: modpost: vmlinux: section mismatch in reference: get_foo+0x8 (section: .text) -> done (section: .init.data)

To get the correct symbol address, st_value must be added

This issue has never been noticed since commit 93684d3b8062 ("kbuild:
include symbol names in section mismatch warnings") presumably because
st_value becomes zero on most architectures when the referenced symbol
is looked up. It is not true for riscv or loongarch, at least.

With this fix, modpost will show the correct symbol name:

  WARNING: modpost: vmlinux: section mismatch in reference: get_foo+0x8 (section: .text) -> foo (section: .init.data)

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 scripts/mod/modpost.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 973b5e5ae2dd..cb6406f485a9 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -1383,13 +1383,15 @@ static void section_rela(struct module *mod, struct elf_info *elf,
 	const Elf_Rela *rela;
 
 	for (rela = start; rela < stop; rela++) {
+		Elf_Sym *tsym;
 		Elf_Addr taddr, r_offset;
 		unsigned int r_type, r_sym;
 
 		r_offset = TO_NATIVE(rela->r_offset);
 		get_rel_type_and_sym(elf, rela->r_info, &r_type, &r_sym);
 
-		taddr = TO_NATIVE(rela->r_addend);
+		tsym = elf->symtab_start + r_sym;
+		taddr = tsym->st_value + TO_NATIVE(rela->r_addend);
 
 		switch (elf->hdr->e_machine) {
 		case EM_RISCV:
@@ -1404,7 +1406,7 @@ static void section_rela(struct module *mod, struct elf_info *elf,
 			break;
 		}
 
-		check_section_mismatch(mod, elf, elf->symtab_start + r_sym,
+		check_section_mismatch(mod, elf, tsym,
 				       fsecndx, fromsec, r_offset, taddr);
 	}
 }
-- 
2.40.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] modpost: fix section mismatch message for RELA
  2023-10-31 17:46 [PATCH] modpost: fix section mismatch message for RELA Masahiro Yamada
@ 2023-10-31 18:07 ` Nick Desaulniers
  0 siblings, 0 replies; 2+ messages in thread
From: Nick Desaulniers @ 2023-10-31 18:07 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, linux-kernel, Albert Ou, Nathan Chancellor,
	Nicolas Schier, Palmer Dabbelt, Paul Walmsley, linux-riscv,
	Fangrui Song

On Tue, Oct 31, 2023 at 10:46 AM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> The section mismatch check prints a bogus symbol name on some
> architectures.
>
> [test code]
>
>   #include <linux/init.h>
>
>   int __initdata foo;
>   int get_foo(void) { return foo; }
>
> If you compile it with GCC for riscv or loongarch, modpost will show an
> incorrect symbol name:
>
>   WARNING: modpost: vmlinux: section mismatch in reference: get_foo+0x8 (section: .text) -> done (section: .init.data)
>
> To get the correct symbol address, st_value must be added
>
> This issue has never been noticed since commit 93684d3b8062 ("kbuild:
> include symbol names in section mismatch warnings") presumably because
> st_value becomes zero on most architectures when the referenced symbol
> is looked up. It is not true for riscv or loongarch, at least.
>
> With this fix, modpost will show the correct symbol name:
>
>   WARNING: modpost: vmlinux: section mismatch in reference: get_foo+0x8 (section: .text) -> foo (section: .init.data)
>
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Thanks for the patch!
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

> ---
>
>  scripts/mod/modpost.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
> index 973b5e5ae2dd..cb6406f485a9 100644
> --- a/scripts/mod/modpost.c
> +++ b/scripts/mod/modpost.c
> @@ -1383,13 +1383,15 @@ static void section_rela(struct module *mod, struct elf_info *elf,
>         const Elf_Rela *rela;
>
>         for (rela = start; rela < stop; rela++) {
> +               Elf_Sym *tsym;
>                 Elf_Addr taddr, r_offset;
>                 unsigned int r_type, r_sym;
>
>                 r_offset = TO_NATIVE(rela->r_offset);
>                 get_rel_type_and_sym(elf, rela->r_info, &r_type, &r_sym);
>
> -               taddr = TO_NATIVE(rela->r_addend);
> +               tsym = elf->symtab_start + r_sym;
> +               taddr = tsym->st_value + TO_NATIVE(rela->r_addend);
>
>                 switch (elf->hdr->e_machine) {
>                 case EM_RISCV:
> @@ -1404,7 +1406,7 @@ static void section_rela(struct module *mod, struct elf_info *elf,
>                         break;
>                 }
>
> -               check_section_mismatch(mod, elf, elf->symtab_start + r_sym,
> +               check_section_mismatch(mod, elf, tsym,
>                                        fsecndx, fromsec, r_offset, taddr);
>         }
>  }
> --
> 2.40.1
>


-- 
Thanks,
~Nick Desaulniers

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-10-31 18:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-31 17:46 [PATCH] modpost: fix section mismatch message for RELA Masahiro Yamada
2023-10-31 18:07 ` Nick Desaulniers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®