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
// 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),
};
// 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));
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;
}
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;