Linux Modules
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: linux-modules@vger.kernel.org
Subject: [PATCH] libkmod: elf: Support extended modversions
Date: Fri, 27 Nov 2026 17:50:03 +0100	[thread overview]
Message-ID: <20261127165012.87004-1-tiwai@suse.de> (raw)

Kernels with CONFIG_EXTENDED_MODVERSIONS (which Rust support selects)
put the CRCs of all undefined symbols into the __version_ext_crcs and
__version_ext_names sections.  Symbols whose names don't fit into the
fixed size __versions entries (MODULE_NAME_LEN) appear only there,
hence most Rust of symbols are gone there.

Currently libkmod looked only at __versions, so it reported CRC 0 for
such symbols, and "depmod -e -E Module.symvers" printed false warnings
like:
  depmod: WARNING: .../rnull_mod.ko.zst disagrees about version of symbol _RNvXs0_NtCs8SrOILYbo5E_6kernel5errorNtB5_5ErrorNtNtCs7o82oapQthI_4core3fmt5Debug3fmt

Look up the extended tables when a symbol is not found in __versions.
Also clear SHF_ALLOC on the extended sections when the user forces
modversions, since the kernel prefers them over __versions when they
are present.

Link: https://bugzilla.suse.com/show_bug.cgi?id=1290766
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 libkmod/libkmod-elf.c | 81 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 79 insertions(+), 2 deletions(-)

diff --git a/libkmod/libkmod-elf.c b/libkmod/libkmod-elf.c
index c13e11430d17..a56469c3dd74 100644
--- a/libkmod/libkmod-elf.c
+++ b/libkmod/libkmod-elf.c
@@ -33,6 +33,8 @@ enum kmod_elf_section {
 	KMOD_ELF_SECTION_SYMTAB,
 	KMOD_ELF_SECTION_VERSIONS,
 	KMOD_ELF_SECTION_BUILD_ID,
+	KMOD_ELF_SECTION_VERSION_EXT_CRCS,
+	KMOD_ELF_SECTION_VERSION_EXT_NAMES,
 	KMOD_ELF_SECTION_MAX,
 };
 
@@ -43,6 +45,8 @@ static const char *const section_name_map[] = {
 	[KMOD_ELF_SECTION_SYMTAB] = ".symtab",
 	[KMOD_ELF_SECTION_VERSIONS] = "__versions",
 	[KMOD_ELF_SECTION_BUILD_ID] = ".note.gnu.build-id",
+	[KMOD_ELF_SECTION_VERSION_EXT_CRCS] = "__version_ext_crcs",
+	[KMOD_ELF_SECTION_VERSION_EXT_NAMES] = "__version_ext_names",
 };
 
 struct kmod_elf {
@@ -581,12 +585,13 @@ int kmod_elf_get_modversions(const struct kmod_elf *elf, struct kmod_modversion
 	return count;
 }
 
-static int elf_strip_versions_section(const struct kmod_elf *elf, uint8_t *changed)
+static int elf_strip_section(const struct kmod_elf *elf, const char *section,
+			     uint8_t *changed)
 {
 	uint64_t off, size;
 	const void *buf;
 	/* the off and size values are not used, supply them as dummies */
-	int idx = kmod_elf_get_section(elf, "__versions", &off, &size);
+	int idx = kmod_elf_get_section(elf, section, &off, &size);
 	uint64_t val;
 
 	if (idx < 0)
@@ -608,6 +613,25 @@ static int elf_strip_versions_section(const struct kmod_elf *elf, uint8_t *chang
 	return elf_set_uint(elf, off, size, val, changed);
 }
 
+static int elf_strip_versions_section(const struct kmod_elf *elf, uint8_t *changed)
+{
+	static const char *const sections[] = {
+		"__versions",
+		"__version_ext_crcs",
+		"__version_ext_names",
+	};
+	size_t i;
+	int err;
+
+	for (i = 0; i < ARRAY_SIZE(sections); i++) {
+		err = elf_strip_section(elf, sections[i], changed);
+		if (err < 0)
+			return err;
+	}
+
+	return 0;
+}
+
 static int elf_strip_vermagic(const struct kmod_elf *elf, uint8_t *changed)
 {
 	uint64_t i, sec_off, size;
@@ -955,6 +979,57 @@ static int kmod_elf_crc_find(const struct kmod_elf *elf, uint64_t off,
 	return -1;
 }
 
+/*
+ * Look up the CRC of a symbol in the extended modversions tables
+ * (__version_ext_crcs / __version_ext_names). Kernels built with
+ * CONFIG_EXTENDED_MODVERSIONS record all symbol CRCs there, including
+ * those whose names are too long for __versions, e.g. Rust symbols.
+ */
+static int kmod_elf_ext_crc_find(const struct kmod_elf *elf, const char *name,
+				 uint64_t *crc)
+{
+	uint64_t crc_off, crc_size, name_off, name_size, i, count;
+	const char *names;
+
+	crc_off = elf->sections[KMOD_ELF_SECTION_VERSION_EXT_CRCS].offset;
+	crc_size = elf->sections[KMOD_ELF_SECTION_VERSION_EXT_CRCS].size;
+	name_off = elf->sections[KMOD_ELF_SECTION_VERSION_EXT_NAMES].offset;
+	name_size = elf->sections[KMOD_ELF_SECTION_VERSION_EXT_NAMES].size;
+	if (crc_off == 0 || name_off == 0)
+		return -1;
+
+	if (crc_size % sizeof(uint32_t) != 0) {
+		ELFDBG(elf,
+		       "unexpected __version_ext_crcs of length %" PRIu64
+		       ", not multiple of %zu as expected.\n",
+		       crc_size, sizeof(uint32_t));
+		return -1;
+	}
+
+	count = crc_size / sizeof(uint32_t);
+	names = elf_get_mem(elf, name_off);
+
+	for (i = 0; i < count; i++) {
+		size_t len = strnlen(names, name_size);
+
+		if (len == name_size) {
+			ELFDBG(elf, "more extended version crcs than names\n");
+			return -1;
+		}
+
+		if (streq(name, names)) {
+			*crc = elf_get_uint(elf, crc_off + i * sizeof(uint32_t),
+					    sizeof(uint32_t));
+			return i;
+		}
+
+		names += len + 1;
+		name_size -= len + 1;
+	}
+
+	return -1;
+}
+
 /* from module-init-tools:elfops_core.c */
 #ifndef STT_REGISTER
 #define STT_REGISTER 13 /* Global register reserved to app. */
@@ -1107,6 +1182,8 @@ int kmod_elf_get_dependency_symbols(const struct kmod_elf *elf,
 		idx = kmod_elf_crc_find(elf, ver_off, versionslen, name, &crc);
 		if (idx >= 0 && visited_versions != NULL)
 			visited_versions[idx] = 1;
+		else if (idx < 0 && kmod_elf_ext_crc_find(elf, name, &crc) < 0)
+			crc = 0;
 		symcrcs[i] = crc;
 	}
 
-- 
2.53.0


                 reply	other threads:[~2026-11-27 16:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261127165012.87004-1-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=linux-modules@vger.kernel.org \
    /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