From 46bc91ade533ad2f51ae32962b057952f90e8d2a Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Mon, 22 Jan 2024 19:24:58 +0100 Subject: [PATCH] macho: skip -r when single input object file This is to ensure we don't unnecessarily strip debug info from the final relocatable input file, so just copy the file out for now. --- src/link/MachO/relocatable.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/link/MachO/relocatable.zig b/src/link/MachO/relocatable.zig index dcda59d607..1bcbe1f3ab 100644 --- a/src/link/MachO/relocatable.zig +++ b/src/link/MachO/relocatable.zig @@ -12,6 +12,20 @@ pub fn flush(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]const u if (module_obj_path) |path| try positionals.append(.{ .path = path }); + if (positionals.items.len == 1) { + // Instead of invoking a full-blown `-r` mode on the input which sadly will strip all + // debug info segments/sections (this is apparently by design by Apple), we copy + // the *only* input file over. + // TODO: in the future, when we implement `dsymutil` alternative directly in the Zig + // compiler, investigate if we can get rid of this `if` prong here. + const path = positionals.items[0].path; + const in_file = try std.fs.cwd().openFile(path, .{}); + const stat = try in_file.stat(); + const amt = try in_file.copyRangeAll(0, macho_file.base.file.?, 0, stat.size); + if (amt != stat.size) return error.InputOutput; // TODO: report an actual user error + return; + } + for (positionals.items) |obj| { macho_file.parsePositional(obj.path, obj.must_link) catch |err| switch (err) { error.MalformedObject,