From: Michael Tremer Date: Tue, 31 Dec 2024 12:11:53 +0000 (+0000) Subject: stripper: Actually strip binaries X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3c7ca51fe6ee849ff515d608c120994e735288c9;p=people%2Fric9%2Fpakfire.git stripper: Actually strip binaries Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/stripper.c b/src/libpakfire/stripper.c index e135fb71e..916f09fbe 100644 --- a/src/libpakfire/stripper.c +++ b/src/libpakfire/stripper.c @@ -399,10 +399,12 @@ static int __pakfire_stripper_build_id_path(struct pakfire_stripper* stripper, buildroot, build_id[0], build_id[1], build_id + 2); } -static int pakfire_stripper_extract_debug_sections(struct pakfire_stripper* stripper, +static int pakfire_stripper_strip_debug_sections(struct pakfire_stripper* stripper, struct pakfire_file* file, const char* build_id) { char build_id_path[PATH_MAX]; const char* path = NULL; + char tmppath[PATH_MAX]; + FILE* f = NULL; int r; // Fetch the path @@ -411,14 +413,26 @@ static int pakfire_stripper_extract_debug_sections(struct pakfire_stripper* stri // Make Build ID path r = pakfire_stripper_build_id_path(stripper, build_id_path, build_id); if (r < 0) - return r; + goto ERROR; // Create the directories r = pakfire_mkparentdir(build_id_path, 0755); if (r < 0) - return r; + goto ERROR; + + // Make a path for a new temporary file + r = pakfire_path(stripper->pakfire, tmppath, "%s", PAKFIRE_TMP_DIR "/.pakfire-strip.XXXXXXX"); + if (r < 0) + goto ERROR; + + // Create a new temporary file + f = pakfire_mktemp(tmppath, 0644); + if (!f) { + r = -errno; + goto ERROR; + } - const char* argv[] = { + const char* extract_argv[] = { "objcopy", "--only-keep-debug", pakfire_relpath(stripper->pakfire, path), @@ -427,7 +441,7 @@ static int pakfire_stripper_extract_debug_sections(struct pakfire_stripper* stri }; // Run the command - r = pakfire_jail_exec_command(stripper->jail, argv, NULL, 0); + r = pakfire_jail_exec_command(stripper->jail, extract_argv, NULL, 0); if (r < 0) { ERROR(stripper->ctx, "Could not extract debug sections from %s: %s\n", pakfire_file_get_path(file), strerror(-r)); @@ -440,6 +454,48 @@ static int pakfire_stripper_extract_debug_sections(struct pakfire_stripper* stri return -ENOTSUP; } + const char* strip_argv[] = { + "objcopy", + + // Ask to strip all debug information + "--strip-debug", + + // Strip anything unneeded + "--strip-unneeded", + + // Add the debuglink + "--add-gnu-debuglink", pakfire_relpath(stripper->pakfire, build_id_path), + + pakfire_relpath(stripper->pakfire, path), + pakfire_relpath(stripper->pakfire, tmppath), + NULL, + }; + + // Run the command + r = pakfire_jail_exec_command(stripper->jail, strip_argv, NULL, 0); + if (r < 0) { + ERROR(stripper->ctx, "Could not strip debug sections from %s: %s\n", + pakfire_file_get_path(file), strerror(-r)); + goto ERROR; + + // The command returned an error + } else if (r > 0) { + ERROR(stripper->ctx, "Could not strip debug sections from %s\n", + pakfire_file_get_path(file)); + r = -ENOTSUP; + goto ERROR; + } + + // Replace the original file with the stripped version + r = pakfire_file_consume(file, fileno(f)); + if (r < 0) + goto ERROR; + +ERROR: + if (*tmppath) + unlink(tmppath); + if (f) + fclose(f); return r; } @@ -468,8 +524,8 @@ static int pakfire_stripper_strip( if (r < 0) goto ERROR; - // Extract debug information - r = pakfire_stripper_extract_debug_sections(stripper, file, build_id); + // Strip debug information + r = pakfire_stripper_strip_debug_sections(stripper, file, build_id); if (r < 0) goto ERROR;