From e2f3878a252af4d819ae33c0a9e77ab552f7a1a9 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 1 Jan 2025 16:54:22 +0000 Subject: [PATCH] ELF: Add function to read the Build ID Signed-off-by: Michael Tremer --- src/libpakfire/elf.c | 33 ++++++++++++++++++++++++++++ src/libpakfire/include/pakfire/elf.h | 1 + 2 files changed, 34 insertions(+) diff --git a/src/libpakfire/elf.c b/src/libpakfire/elf.c index 7db5e2792..5e59f74fe 100644 --- a/src/libpakfire/elf.c +++ b/src/libpakfire/elf.c @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include // 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; +} diff --git a/src/libpakfire/include/pakfire/elf.h b/src/libpakfire/include/pakfire/elf.h index fbfcf3091..a5038030a 100644 --- a/src/libpakfire/include/pakfire/elf.h +++ b/src/libpakfire/include/pakfire/elf.h @@ -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 */ -- 2.47.3