From: Michael Tremer Date: Mon, 30 Dec 2024 13:29:37 +0000 (+0000) Subject: Revert "stripper: Use libbfd to extract the Build ID" X-Git-Tag: 0.9.30~649 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6940c9c460db7d91aa4efca7ecafadb9be0b36ae;p=pakfire.git Revert "stripper: Use libbfd to extract the Build ID" This reverts commit 44ffbd6f1f47115072e8c8d4ea751f6eacb05dba. Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/stripper.c b/src/libpakfire/stripper.c index 70e333400..3dcde4620 100644 --- a/src/libpakfire/stripper.c +++ b/src/libpakfire/stripper.c @@ -23,9 +23,6 @@ #include #include -// libbfd -#include - // libdw #include #include @@ -168,6 +165,42 @@ 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) { struct stat st = {}; @@ -357,17 +390,10 @@ ERROR: static int pakfire_stripper_strip( struct pakfire_ctx* ctx, struct pakfire_file* file, void* data) { struct pakfire_stripper* stripper = data; - bfd* abfd = NULL; char* build_id = NULL; int fd = -EBADF; int r; - // Initialize libbfd - bfd_init(); - - // Fetch path - const char* path = pakfire_file_get_path(file); - // Open the file fd = pakfire_file_open(file); if (fd < 0) { @@ -375,31 +401,10 @@ static int pakfire_stripper_strip( goto ERROR; } - // Open the ELF object - abfd = bfd_fdopenr(path, NULL, fd); - if (!abfd) { - ERROR(stripper->ctx, "Could not open ELF file %s: %s\n", - path, bfd_errmsg(bfd_get_error())); - r = -EINVAL; - goto ERROR; - } - - // Check if this is an ELF object - if (!bfd_check_format(abfd, bfd_object)) { - ERROR(stripper->ctx, "%s is not an ELF object\n", path); - r = -ENOTSUP; - goto ERROR; - } - // Extract the Build ID - if (abfd->build_id) { - build_id = __pakfire_hexlify(abfd->build_id->data, abfd->build_id->size); - if (!build_id) { - ERROR(stripper->ctx, "Could not convert the Build ID\n"); - r = -EINVAL; - goto ERROR; - } - } + r = pakfire_stripper_extract_build_id(stripper, file, fd, &build_id); + if (r < 0) + goto ERROR; // Copy sources r = pakfire_stripper_copy_sources(stripper, file, fd); @@ -407,8 +412,6 @@ static int pakfire_stripper_strip( goto ERROR; ERROR: - if (abfd) - bfd_close(abfd); if (build_id) free(build_id); if (fd >= 0)