alexk / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone
ed184b
From 68d383c39cef8d58b80940b13dd132d3f41a03f0 Mon Sep 17 00:00:00 2001
ed184b
Message-Id: <68d383c39cef8d58b80940b13dd132d3f41a03f0.1571917458.git.pmatilai@redhat.com>
ed184b
From: Panu Matilainen <pmatilai@redhat.com>
ed184b
Date: Tue, 2 Apr 2019 15:22:07 +0300
ed184b
Subject: [PATCH 1/2] Detect kernel modules by .modinfo section presence for
ed184b
 build-id generation
ed184b
ed184b
File extension based heuristics only work so far at best, and break
ed184b
completely on compressed files with arbitrary .gz/.xz etc extension.
ed184b
This isn't supposed to change any behavior as such, only provide more
ed184b
reliable detection of kernel modules.
ed184b
---
ed184b
 build/files.c | 27 ++++++++++++++++++++++++---
ed184b
 1 file changed, 24 insertions(+), 3 deletions(-)
ed184b
ed184b
diff --git a/build/files.c b/build/files.c
ed184b
index dbad9a7f3..3822be3d3 100644
ed184b
--- a/build/files.c
ed184b
+++ b/build/files.c
ed184b
@@ -1739,6 +1739,28 @@ static int addNewIDSymlink(ARGV_t *files,
ed184b
     return rc;
ed184b
 }
ed184b
 
ed184b
+static int haveModinfo(Elf *elf)
ed184b
+{
ed184b
+    Elf_Scn * scn = NULL;
ed184b
+    size_t shstrndx;
ed184b
+    int have_modinfo = 0;
ed184b
+    const char *sname;
ed184b
+
ed184b
+    if (elf_getshdrstrndx(elf, &shstrndx) == 0) {
ed184b
+	while ((scn = elf_nextscn(elf, scn)) != NULL) {
ed184b
+	    GElf_Shdr shdr_mem, *shdr = gelf_getshdr(scn, &shdr_mem);
ed184b
+	    if (shdr == NULL)
ed184b
+		continue;
ed184b
+	    sname = elf_strptr(elf, shstrndx, shdr->sh_name);
ed184b
+	    if (sname && rstreq(sname, ".modinfo")) {
ed184b
+		have_modinfo = 1;
ed184b
+		break;
ed184b
+	    }
ed184b
+	}
ed184b
+    }
ed184b
+    return have_modinfo;
ed184b
+}
ed184b
+
ed184b
 static int generateBuildIDs(FileList fl, ARGV_t *files)
ed184b
 {
ed184b
     int rc = 0;
ed184b
@@ -1803,15 +1825,14 @@ static int generateBuildIDs(FileList fl, ARGV_t *files)
ed184b
 	    int fd = open (flp->diskPath, O_RDONLY);
ed184b
 	    if (fd >= 0) {
ed184b
 		/* Only real ELF files, that are ET_EXEC, ET_DYN or
ed184b
-		   kernel modules (ET_REL files with names ending in .ko)
ed184b
+		   kernel modules (ET_REL files with .modinfo section)
ed184b
 		   should have build-ids. */
ed184b
 		GElf_Ehdr ehdr;
ed184b
 		Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
ed184b
 		if (elf != NULL && elf_kind(elf) == ELF_K_ELF
ed184b
 		    && gelf_getehdr(elf, &ehdr) != NULL
ed184b
 		    && (ehdr.e_type == ET_EXEC || ehdr.e_type == ET_DYN
ed184b
-			|| (ehdr.e_type == ET_REL
ed184b
-			    && rpmFileHasSuffix (flp->diskPath, ".ko")))) {
ed184b
+			|| (ehdr.e_type == ET_REL && haveModinfo(elf)))) {
ed184b
 		    const void *build_id;
ed184b
 		    ssize_t len = dwelf_elf_gnu_build_id (elf, &build_id);
ed184b
 		    /* len == -1 means error. Zero means no
ed184b
-- 
ed184b
2.21.0
ed184b