]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
stripper: Support stripping binaries without a Build ID
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 Jan 2025 15:22:51 +0000 (15:22 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 Jan 2025 15:22:51 +0000 (15:22 +0000)
We should not have these in practise, but there might be cases where we
cannot extract the ID correctly.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/stripper.c

index 3b93ca9bda97c02ff1b01152499ecc8480de1adb..b7b02d05f9a08dcd0720f4841e3afb37924743b9 100644 (file)
@@ -325,6 +325,33 @@ ERROR:
        return r;
 }
 
+#define pakfire_stripper_debug_path(stripper, s, filename, build_id) \
+       __pakfire_stripper_debug_path(stripper, s, sizeof(s), filename, build_id)
+
+static int __pakfire_stripper_debug_path(struct pakfire_stripper* self,
+               char* buffer, size_t length, struct pakfire_file* file, const char* build_id) {
+       char debugpath[PATH_MAX];
+       int r;
+
+       // Make the path to the debug directory
+       r = pakfire_path_append(debugpath, self->path, "usr/lib/debug");
+       if (r < 0)
+               return r;
+
+       // If we have a Build ID, we will make the path using that
+       if (build_id)
+               return __pakfire_string_format(buffer, length,
+                       "%s/.build-id/%c%c/%s.debug", debugpath, build_id[0], build_id[1], build_id + 2);
+
+       // Fetch the path of the file
+       const char* path = pakfire_file_get_path(file);
+       if (!path)
+               return -EINVAL;
+
+       // Otherwise we will have to fall pack to the name of the file
+       return __pakfire_string_format(buffer, length, "%s/%s.debug", debugpath, path);
+}
+
 static int pakfire_stripper_extract_debuginfo(struct pakfire_stripper* self,
                struct pakfire_file* file, struct pakfire_elf* elf, const char* build_id_path) {
        int r;
@@ -370,17 +397,20 @@ static int pakfire_stripper_extract_debuginfo(struct pakfire_stripper* self,
 
 static int pakfire_stripper_strip_debuginfo(struct pakfire_stripper* self,
                struct pakfire_file* file, struct pakfire_elf* elf) {
-       const char* build_id = NULL;
-       char build_id_path[PATH_MAX];
+       char debugpath[PATH_MAX];
        char tmppath[PATH_MAX] = "";
-       const char* path = NULL;
+       char** argv = NULL;
        FILE* f = NULL;
        int r;
 
-       char** argv = NULL;
-
        int extract_debuginfo = 1;
 
+       // Fetch the path
+       const char* path = pakfire_file_get_abspath(file);
+
+       // Fetch the Build ID
+       const char* build_id = pakfire_elf_build_id(elf);
+
        // Check whether we should extract debug information
        switch (pakfire_elf_type(elf)) {
                // Relocatable objects
@@ -393,23 +423,13 @@ static int pakfire_stripper_strip_debuginfo(struct pakfire_stripper* self,
                        break;
        }
 
-       // Fetch the path
-       path = pakfire_file_get_abspath(file);
-
-       // Fetch the Build ID
-       build_id = pakfire_elf_build_id(elf);
-       if (!build_id) {
-               ERROR(self->ctx, "No Build ID\n");
-               r = -ENOTSUP;
+       // pakfire_stripper_debug_path
+       r = pakfire_stripper_debug_path(self, debugpath, file, build_id);
+       if (r < 0) {
+               ERROR(self->ctx, "Could not make the debug path for %s: %s\n", path, strerror(-r));
                goto ERROR;
        }
 
-       // Make Build ID path
-       r = pakfire_string_format(build_id_path, "%s/usr/lib/debug/.build-id/%c%c/%s.debug",
-                       self->path, build_id[0], build_id[1], build_id + 2);
-       if (r < 0)
-               goto ERROR;
-
        // Make a path for a new temporary file
        r = pakfire_path(self->pakfire, tmppath, "%s", PAKFIRE_TMP_DIR "/.pakfire-strip.XXXXXXX");
        if (r < 0)
@@ -454,12 +474,12 @@ static int pakfire_stripper_strip_debuginfo(struct pakfire_stripper* self,
 
        // Extract the debug information
        if (extract_debuginfo) {
-               r = pakfire_stripper_extract_debuginfo(self, file, elf, build_id_path);
+               r = pakfire_stripper_extract_debuginfo(self, file, elf, debugpath);
                if (r < 0)
                        goto ERROR;
 
                // Add the debuglink
-               r = pakfire_strings_appendf(&argv, "--add-gnu-debuglink=%s", build_id_path);
+               r = pakfire_strings_appendf(&argv, "--add-gnu-debuglink=%s", debugpath);
                if (r < 0)
                        goto ERROR;
        }