diff mbox series

[7/7] modpost: look up the correct symbol in check_export_symbol()

Message ID 20231101150404.754108-8-masahiroy@kernel.org (mailing list archive)
State New, archived
Headers show
Series modpost: fix modpost errors for m68k-uclinux-gcc | expand

Commit Message

Masahiro Yamada Nov. 1, 2023, 3:04 p.m. UTC
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 mbox series

Patch

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;
 	}