Blob Blame History Raw
From 68d383c39cef8d58b80940b13dd132d3f41a03f0 Mon Sep 17 00:00:00 2001
Message-Id: <68d383c39cef8d58b80940b13dd132d3f41a03f0.1571917458.git.pmatilai@redhat.com>
From: Panu Matilainen <pmatilai@redhat.com>
Date: Tue, 2 Apr 2019 15:22:07 +0300
Subject: [PATCH 1/2] Detect kernel modules by .modinfo section presence for
 build-id generation

File extension based heuristics only work so far at best, and break
completely on compressed files with arbitrary .gz/.xz etc extension.
This isn't supposed to change any behavior as such, only provide more
reliable detection of kernel modules.
---
 build/files.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/build/files.c b/build/files.c
index dbad9a7f3..3822be3d3 100644
--- a/build/files.c
+++ b/build/files.c
@@ -1739,6 +1739,28 @@ static int addNewIDSymlink(ARGV_t *files,
     return rc;
 }
 
+static int haveModinfo(Elf *elf)
+{
+    Elf_Scn * scn = NULL;
+    size_t shstrndx;
+    int have_modinfo = 0;
+    const char *sname;
+
+    if (elf_getshdrstrndx(elf, &shstrndx) == 0) {
+	while ((scn = elf_nextscn(elf, scn)) != NULL) {
+	    GElf_Shdr shdr_mem, *shdr = gelf_getshdr(scn, &shdr_mem);
+	    if (shdr == NULL)
+		continue;
+	    sname = elf_strptr(elf, shstrndx, shdr->sh_name);
+	    if (sname && rstreq(sname, ".modinfo")) {
+		have_modinfo = 1;
+		break;
+	    }
+	}
+    }
+    return have_modinfo;
+}
+
 static int generateBuildIDs(FileList fl, ARGV_t *files)
 {
     int rc = 0;
@@ -1803,15 +1825,14 @@ static int generateBuildIDs(FileList fl, ARGV_t *files)
 	    int fd = open (flp->diskPath, O_RDONLY);
 	    if (fd >= 0) {
 		/* Only real ELF files, that are ET_EXEC, ET_DYN or
-		   kernel modules (ET_REL files with names ending in .ko)
+		   kernel modules (ET_REL files with .modinfo section)
 		   should have build-ids. */
 		GElf_Ehdr ehdr;
 		Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
 		if (elf != NULL && elf_kind(elf) == ELF_K_ELF
 		    && gelf_getehdr(elf, &ehdr) != NULL
 		    && (ehdr.e_type == ET_EXEC || ehdr.e_type == ET_DYN
-			|| (ehdr.e_type == ET_REL
-			    && rpmFileHasSuffix (flp->diskPath, ".ko")))) {
+			|| (ehdr.e_type == ET_REL && haveModinfo(elf)))) {
 		    const void *build_id;
 		    ssize_t len = dwelf_elf_gnu_build_id (elf, &build_id);
 		    /* len == -1 means error. Zero means no
-- 
2.21.0