public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Masahiro Yamada <masahiroy@kernel.org>
To: linux-kbuild@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Greg Ungerer <gerg@kernel.org>,
	Jack Brennen <jbrennen@google.com>,
	Masahiro Yamada <masahiroy@kernel.org>,
	Nathan Chancellor <nathan@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Nicolas Schier <nicolas@fjasle.eu>
Subject: [PATCH 7/7] modpost: look up the correct symbol in check_export_symbol()
Date: Thu,  2 Nov 2023 00:04:04 +0900	[thread overview]
Message-ID: <20231101150404.754108-8-masahiroy@kernel.org> (raw)
In-Reply-To: <20231101150404.754108-1-masahiroy@kernel.org>

Greg Ungerer reported modpost produced false-positive
"local symbol '...' was exported" errors when m68k-uclinux-gcc is used.

I had assumed ELF_R_SYM(Elf_Rela::r_info) pointed to the exported symbol
itself if it is in the global scope. This assumption worked for many
toolchains, but as it turned out, it was not true for m68k-uclinux-gcc,
at least.

If the 'sym' argument passed to check_export_symbol() is not the
exported symbol, look up the correct one in the symbol table. It incurs
a search cost, but since we know its section index and address, we can
exploit the binary search.

Reported-by: Greg Ungerer <gerg@kernel.org>
Closes: https://lore.kernel.org/all/1fac9d12-2ec2-4ccb-bb81-34f3fc34789e@westnet.com.au/
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 scripts/mod/modpost.c | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 896ecfa8483f..ee67bc6d71ee 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -1021,6 +1021,18 @@ static Elf_Sym *find_tosym(struct elf_info *elf, Elf_Addr addr, Elf_Sym *sym)
 				      true, 20);
 }
 
+static Elf_Sym *find_tosym_with_name(struct elf_info *elf, Elf_Addr addr,
+				     Elf_Sym *sym, const char *name)
+{
+	/* If the supplied symbol has the expected name, return it. */
+	if (!strcmp(sym_name(elf, sym), name))
+		return sym;
+
+	/* Look up a symbol with the given name. */
+	return symsearch_find_with_name(elf, addr, get_secindex(elf, sym),
+					true, 20, name);
+}
+
 static bool is_executable_section(struct elf_info *elf, unsigned int secndx)
 {
 	if (secndx >= elf->num_sections)
@@ -1079,7 +1091,7 @@ static void default_mismatch_handler(const char *modname, struct elf_info *elf,
 
 static void check_export_symbol(struct module *mod, struct elf_info *elf,
 				Elf_Addr faddr, const char *secname,
-				Elf_Sym *sym)
+				Elf_Sym *sym, Elf_Addr taddr)
 {
 	static const char *prefix = "__export_symbol_";
 	const char *label_name, *name, *data;
@@ -1096,6 +1108,14 @@ static void check_export_symbol(struct module *mod, struct elf_info *elf,
 		return;
 	}
 
+	name = label_name + strlen(prefix);
+	sym = find_tosym_with_name(elf, taddr, sym, name);
+	if (!sym) {
+		error("%s: could not find the the export symbol '%s'\n",
+		      mod->name, name);
+		return;
+	}
+
 	if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
 	    ELF_ST_BIND(sym->st_info) != STB_WEAK) {
 		error("%s: local symbol '%s' was exported\n", mod->name,
@@ -1103,13 +1123,6 @@ static void check_export_symbol(struct module *mod, struct elf_info *elf,
 		return;
 	}
 
-	name = sym_name(elf, sym);
-	if (strcmp(label_name + strlen(prefix), name)) {
-		error("%s: .export_symbol section references '%s', but it does not seem to be an export symbol\n",
-		      mod->name, name);
-		return;
-	}
-
 	data = sym_get_data(elf, label);	/* license */
 	if (!strcmp(data, "GPL")) {
 		is_gpl = true;
@@ -1156,7 +1169,7 @@ static void check_section_mismatch(struct module *mod, struct elf_info *elf,
 	const struct sectioncheck *mismatch;
 
 	if (module_enabled && elf->export_symbol_secndx == fsecndx) {
-		check_export_symbol(mod, elf, faddr, tosec, sym);
+		check_export_symbol(mod, elf, faddr, tosec, sym, taddr);
 		return;
 	}
 
-- 
2.40.1


  parent reply	other threads:[~2023-11-01 15:05 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-01 15:03 [PATCH 0/7] modpost: fix modpost errors for m68k-uclinux-gcc Masahiro Yamada
2023-11-01 15:03 ` [PATCH 1/7] modpost: move sym_name() to modpost.h Masahiro Yamada
2023-11-01 15:03 ` [PATCH 2/7] modpost: add const qualifier to syminfo table Masahiro Yamada
2023-11-01 15:04 ` [PATCH 3/7] modpost: add table_size local variable to symsearch_find_nearest() Masahiro Yamada
2023-11-01 15:04 ` [PATCH 4/7] modpost: introduce a filtering feature to symsearch Masahiro Yamada
2023-11-01 15:04 ` [PATCH 5/7] modpost: prefer global symbols in symsearch_find_nearest() Masahiro Yamada
2023-11-01 15:04 ` [PATCH 6/7] modpost: add symsearch_find_with_name() helper function Masahiro Yamada
2023-11-01 15:04 ` Masahiro Yamada [this message]
2023-11-02 15:00 ` [PATCH 0/7] modpost: fix modpost errors for m68k-uclinux-gcc Greg Ungerer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231101150404.754108-8-masahiroy@kernel.org \
    --to=masahiroy@kernel.org \
    --cc=gerg@kernel.org \
    --cc=jbrennen@google.com \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=nicolas@fjasle.eu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox