]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
ELF: Add function to read the Build ID
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Jan 2025 16:54:22 +0000 (16:54 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Jan 2025 16:54:22 +0000 (16:54 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/elf.c
src/libpakfire/include/pakfire/elf.h

index 7db5e27927f90b53d728375de0eafd4a8658579a..5e59f74fe2b16d48ffeed189baa26bd031d5d212 100644 (file)
@@ -24,6 +24,8 @@
 #include <pakfire/ctx.h>
 #include <pakfire/elf.h>
 #include <pakfire/file.h>
+#include <pakfire/hex.h>
+#include <pakfire/logging.h>
 #include <pakfire/string.h>
 
 // libelf
@@ -45,6 +47,9 @@ struct pakfire_elf {
 
        // ELF Header
        GElf_Ehdr ehdr;
+
+       // GNU Build ID
+       char* build_id;
 };
 
 static int pakfire_elf_init_libelf(struct pakfire_ctx* ctx) {
@@ -94,6 +99,8 @@ static int pakfire_elf_open_elf(struct pakfire_elf* self) {
 }
 
 static void pakfire_elf_free(struct pakfire_elf* self) {
+       if (self->build_id)
+               free(self->build_id);
        if (self->elf)
                elf_end(self->elf);
        if (self->fd >= 0)
@@ -199,3 +206,29 @@ const char* pakfire_elf_path(struct pakfire_elf* self) {
 int pakfire_elf_type(struct pakfire_elf* self) {
        return self->ehdr.e_type;
 }
+
+const char* pakfire_elf_build_id(struct pakfire_elf* self) {
+       const void* buffer = NULL;
+       ssize_t length;
+
+       if (!self->build_id) {
+               // Extract the GNU Build ID
+               length = dwelf_elf_gnu_build_id(self->elf, &buffer);
+               if (length < 0) {
+                       ERROR(self->ctx, "Could not read the GNU Build ID from %s: %s\n",
+                               self->path, elf_errmsg(-1));
+                       return NULL;
+               }
+
+               // Convert the Build ID to hex
+               self->build_id = __pakfire_hexlify(buffer, length);
+               if (!self->build_id) {
+                       ERROR(self->ctx, "Could not convert the Build ID into hex format: %m\n");
+                       return NULL;
+               }
+
+               DEBUG(self->ctx, "%s has Build ID %s\n", self->path, self->build_id);
+       }
+
+       return self->build_id;
+}
index fbfcf3091fd9fda504eae7e3ad802c9cf7606785..a5038030a7194443bf72ee3606667d562b99b9ab 100644 (file)
@@ -39,6 +39,7 @@ struct pakfire_elf* pakfire_elf_unref(struct pakfire_elf* self);
 
 const char* pakfire_elf_path(struct pakfire_elf* self);
 int pakfire_elf_type(struct pakfire_elf* self);
+const char* pakfire_elf_build_id(struct pakfire_elf* self);
 
 #endif /* PAKFIRE_PRIVATE */