From: Michael Tremer Date: Wed, 1 Jan 2025 16:55:22 +0000 (+0000) Subject: stripper: Use the new ELF abstraction to read the Build ID X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dc63c4cc30270925de0e510490f6367421c1593f;p=people%2Fric9%2Fpakfire.git stripper: Use the new ELF abstraction to read the Build ID Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/stripper.c b/src/libpakfire/stripper.c index daa083fec..9d851d832 100644 --- a/src/libpakfire/stripper.c +++ b/src/libpakfire/stripper.c @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -165,42 +166,6 @@ ERROR: return r; } -static int pakfire_stripper_extract_build_id(struct pakfire_stripper* stripper, - struct pakfire_file* file, int fd, char** build_id) { - Elf* elf = NULL; - const void* buffer = NULL; - ssize_t length = -1; - int r = 0; - - elf = elf_begin(fd, ELF_C_READ, NULL); - if (!elf) { - ERROR(stripper->ctx, "Could not open ELF file %s: %s\n", - pakfire_file_get_path(file), elf_errmsg(-1)); - r = -ENOTSUP; - goto ERROR; - } - - // Extract the GNU Build ID - length = dwelf_elf_gnu_build_id(elf, &buffer); - if (length < 0) { - ERROR(stripper->ctx, "Could not read the GNU Build ID from %s: %s\n", - pakfire_file_get_path(file), elf_errmsg(-1)); - r = -ENOTSUP; - goto ERROR; - } - - // Convert the Build ID to hex - *build_id = __pakfire_hexlify(buffer, length); - - DEBUG(stripper->ctx, "%s has Build ID %s\n", pakfire_file_get_path(file), *build_id); - -ERROR: - if (elf) - elf_end(elf); - - return r; -} - static int pakfire_stripper_copy_source_file( struct pakfire_stripper* stripper, const char* filename) { char srcpath[PATH_MAX]; @@ -407,7 +372,8 @@ static int __pakfire_stripper_build_id_path(struct pakfire_stripper* stripper, } static int pakfire_stripper_strip_debug_sections(struct pakfire_stripper* stripper, - struct pakfire_file* file, const char* build_id) { + struct pakfire_file* file, struct pakfire_elf* elf) { + const char* build_id = NULL; char build_id_path[PATH_MAX]; char tmppath[PATH_MAX] = ""; const char* path = NULL; @@ -417,6 +383,14 @@ static int pakfire_stripper_strip_debug_sections(struct pakfire_stripper* stripp // Fetch the path path = pakfire_file_get_abspath(file); + // Fetch the Build ID + build_id = pakfire_elf_build_id(elf); + if (!build_id) { + ERROR(stripper->ctx, "No Build ID\n"); + r = -ENOTSUP; + goto ERROR; + } + // Make Build ID path r = pakfire_stripper_build_id_path(stripper, build_id_path, build_id); if (r < 0) @@ -515,7 +489,7 @@ ERROR: static int pakfire_stripper_strip( struct pakfire_ctx* ctx, struct pakfire_file* file, void* data) { struct pakfire_stripper* stripper = data; - char* build_id = NULL; + struct pakfire_elf* elf = NULL; int fd = -EBADF; int r; @@ -526,8 +500,8 @@ static int pakfire_stripper_strip( goto ERROR; } - // Extract the Build ID - r = pakfire_stripper_extract_build_id(stripper, file, fd, &build_id); + // Open the ELF file + r = pakfire_elf_open_file(&elf, stripper->ctx, file); if (r < 0) goto ERROR; @@ -537,13 +511,13 @@ static int pakfire_stripper_strip( goto ERROR; // Strip debug information - r = pakfire_stripper_strip_debug_sections(stripper, file, build_id); + r = pakfire_stripper_strip_debug_sections(stripper, file, elf); if (r < 0) goto ERROR; ERROR: - if (build_id) - free(build_id); + if (elf) + pakfire_elf_unref(elf); if (fd >= 0) close(fd);