]> git.ipfire.org Git - pakfire.git/commitdiff
stripper: Extract debug information into a separate file
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 30 Dec 2024 14:39:48 +0000 (14:39 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 30 Dec 2024 14:39:48 +0000 (14:39 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/stripper.c

index cb3e7db08d91a571bc40a159c75c7b981d4a4837..b2ff941b368cdd90b457bf67cdabb2a894ac289d 100644 (file)
@@ -387,6 +387,88 @@ ERROR:
        return r;
 }
 
+#define pakfire_stripper_build_id_path(stripper, buffer, build_id) \
+       __pakfire_stripper_build_id_path(stripper, buffer, sizeof(buffer), build_id)
+
+static int __pakfire_stripper_build_id_path(struct pakfire_stripper* stripper,
+               char* buffer, size_t length, const char* build_id) {
+       const char* buildroot = pakfire_relpath(stripper->pakfire, stripper->path);
+
+       return __pakfire_path(stripper->pakfire, buffer, length,
+               "%s/usr/lib/debug/.build-id/%c%c/%s.debug",
+               buildroot, build_id[0], build_id[1], build_id + 2);
+}
+
+#define pakfire_stripper_debug_path(stripper, buffer, file) \
+       __pakfire_stripper_debug_path(stripper, buffer, sizeof(buffer), file)
+
+static int __pakfire_stripper_debug_path(struct pakfire_stripper* stripper,
+               char* buffer, size_t length, struct pakfire_file* file) {
+       const char* buildroot = pakfire_relpath(stripper->pakfire, stripper->path);
+
+       return __pakfire_path(stripper->pakfire, buffer, length,
+               "%s/usr/lib/debug%s.debug", buildroot, pakfire_file_get_path(file));
+}
+
+static int pakfire_stripper_extract_debug_sections(struct pakfire_stripper* stripper,
+               struct pakfire_file* file, const char* build_id) {
+       char build_id_path[PATH_MAX];
+       char debug_path[PATH_MAX];
+       const char* path = NULL;
+       int r;
+
+       // Fetch the path
+       path = pakfire_file_get_abspath(file);
+
+       // Make the debug path
+       r = pakfire_stripper_debug_path(stripper, debug_path, file);
+       if (r < 0)
+               return r;
+
+       // Make Build ID path
+       r = pakfire_stripper_build_id_path(stripper, build_id_path, build_id);
+       if (r < 0)
+               return r;
+
+       // Create the directories
+       r = pakfire_mkparentdir(debug_path, 0755);
+       if (r < 0)
+               return r;
+
+       r = pakfire_mkparentdir(build_id_path, 0755);
+       if (r < 0)
+               return r;
+
+       const char* argv[] = {
+               "objcopy",
+               "--only-keep-debug",
+               pakfire_relpath(stripper->pakfire, path),
+               pakfire_relpath(stripper->pakfire, debug_path),
+               NULL,
+       };
+
+       // Run the command
+       r = pakfire_jail_exec_command(stripper->jail, 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));
+               return r;
+
+       // 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));
+               return -ENOTSUP;
+       }
+
+       // Create a symlink
+       r = pakfire_symlink(stripper->ctx, debug_path, build_id_path);
+       if (r < 0)
+               return r;
+
+       return r;
+}
+
 static int pakfire_stripper_strip(
                struct pakfire_ctx* ctx, struct pakfire_file* file, void* data) {
        struct pakfire_stripper* stripper = data;
@@ -411,6 +493,11 @@ static int pakfire_stripper_strip(
        if (r < 0)
                goto ERROR;
 
+       // Extract debug information
+       r = pakfire_stripper_extract_debug_sections(stripper, file, build_id);
+       if (r < 0)
+               goto ERROR;
+
 ERROR:
        if (build_id)
                free(build_id);