Blame SOURCES/kmod-0004-libkmod-elf-resolve-CRC-if-module-is-built-with-MODU.patch

532e2b
From 1e48901166efd65c574de9f6a2b7139721b72623 Mon Sep 17 00:00:00 2001
532e2b
From: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
532e2b
Date: Wed, 19 Jul 2017 17:56:49 +0300
532e2b
Subject: [PATCH] libkmod-elf: resolve CRC if module is built with
532e2b
 MODULE_REL_CRCS
532e2b
532e2b
Normally exported symbol's crc is stored as absolute (SHN_ABS)
532e2b
value of special named symbol __crc_<symbol name>.
532e2b
532e2b
When the kernel and modules are built with the config option
532e2b
CONFIG_MODULE_REL_CRCS, all the CRCs are put in a special section
532e2b
and the __crc_<symbol name> symbols values are offsets in the
532e2b
section. See patch description of the commit:
532e2b
532e2b
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=56067812d5b0e737ac2063e94a50f76b810d6ca3
532e2b
532e2b
Add kmod support of this configuration.
532e2b
532e2b
Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
532e2b
---
532e2b
 libkmod/libkmod-elf.c | 30 +++++++++++++++++++++++++++++-
532e2b
 1 file changed, 29 insertions(+), 1 deletion(-)
532e2b
532e2b
diff --git a/libkmod/libkmod-elf.c b/libkmod/libkmod-elf.c
532e2b
index 90da89aebbaf..ef4a8a3142a1 100644
532e2b
--- a/libkmod/libkmod-elf.c
532e2b
+++ b/libkmod/libkmod-elf.c
532e2b
@@ -747,6 +747,31 @@ static inline uint8_t kmod_symbol_bind_from_elf(uint8_t elf_value)
532e2b
 	}
532e2b
 }
532e2b
 
532e2b
+static uint64_t kmod_elf_resolve_crc(const struct kmod_elf *elf, uint64_t crc, uint16_t shndx)
532e2b
+{
532e2b
+	int err;
532e2b
+	uint64_t off, size;
532e2b
+	uint32_t nameoff;
532e2b
+
532e2b
+	if (shndx == SHN_ABS || shndx == SHN_UNDEF)
532e2b
+		return crc;
532e2b
+
532e2b
+	err = elf_get_section_info(elf, shndx, &off, &size, &nameoff);
532e2b
+	if (err < 0) {
532e2b
+		ELFDBG("Cound not find section index %"PRIu16" for crc", shndx);
532e2b
+		return (uint64_t)-1;
532e2b
+	}
532e2b
+
532e2b
+	if (crc > (size - sizeof(uint32_t))) {
532e2b
+		ELFDBG("CRC offset %"PRIu64" is too big, section %"PRIu16" size is %"PRIu64"\n",
532e2b
+		       crc, shndx, size);
532e2b
+		return (uint64_t)-1;
532e2b
+	}
532e2b
+
532e2b
+	crc = elf_get_uint(elf, off + crc, sizeof(uint32_t));
532e2b
+	return crc;
532e2b
+}
532e2b
+
532e2b
 /* array will be allocated with strings in a single malloc, just free *array */
532e2b
 int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **array)
532e2b
 {
532e2b
@@ -830,6 +855,7 @@ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **ar
532e2b
 		uint32_t name_off;
532e2b
 		uint64_t crc;
532e2b
 		uint8_t info, bind;
532e2b
+		uint16_t shndx;
532e2b
 
532e2b
 #define READV(field)							\
532e2b
 		elf_get_uint(elf, sym_off + offsetof(typeof(*s), field),\
532e2b
@@ -839,11 +865,13 @@ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **ar
532e2b
 			name_off = READV(st_name);
532e2b
 			crc = READV(st_value);
532e2b
 			info = READV(st_info);
532e2b
+			shndx = READV(st_shndx);
532e2b
 		} else {
532e2b
 			Elf64_Sym *s;
532e2b
 			name_off = READV(st_name);
532e2b
 			crc = READV(st_value);
532e2b
 			info = READV(st_info);
532e2b
+			shndx = READV(st_shndx);
532e2b
 		}
532e2b
 #undef READV
532e2b
 		name = elf_get_mem(elf, str_off + name_off);
532e2b
@@ -856,7 +884,7 @@ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **ar
532e2b
 		else
532e2b
 			bind = ELF64_ST_BIND(info);
532e2b
 
532e2b
-		a[count].crc = crc;
532e2b
+		a[count].crc = kmod_elf_resolve_crc(elf, crc, shndx);
532e2b
 		a[count].bind = kmod_symbol_bind_from_elf(bind);
532e2b
 		a[count].symbol = itr;
532e2b
 		slen = strlen(name);
532e2b
-- 
532e2b
2.14.0.rc0
532e2b