]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
stripper: Actually strip binaries
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 31 Dec 2024 12:11:53 +0000 (12:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 31 Dec 2024 12:11:53 +0000 (12:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/stripper.c

index e135fb71e76fdd807f3fab975e2002a13e632ecb..916f09fbe42fa31139a1b36cfddfd422b7e44190 100644 (file)
@@ -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;