Commit d53cb284de

Jakub Konka <kubkon@jakubkonka.com>
2024-01-28 11:31:23
macho: re-enable emitting empty dSYM bundle
1 parent 3ae4c3c
Changed files (2)
src/link/MachO/DebugSymbols.zig
@@ -36,15 +36,13 @@ pub const Reloc = struct {
     prev_vaddr: u64,
 };
 
-/// You must call this function *after* `MachO.populateMissingMetadata()`
+/// You must call this function *after* `ZigObject.initMetadata()`
 /// has been called to get a viable debug symbols output.
-pub fn populateMissingMetadata(self: *DebugSymbols, macho_file: *MachO) !void {
-    const target = macho_file.base.comp.root_mod.resolved_target.result;
-
+pub fn initMetadata(self: *DebugSymbols, macho_file: *MachO) !void {
     if (self.dwarf_segment_cmd_index == null) {
         self.dwarf_segment_cmd_index = @as(u8, @intCast(self.segments.items.len));
 
-        const page_size = MachO.getPageSize(target.cpu.arch);
+        const page_size = macho_file.getPageSize();
         const off = @as(u64, @intCast(page_size));
         const ideal_size: u16 = 200 + 128 + 160 + 250;
         const needed_size = mem.alignForward(u64, padToIdeal(ideal_size), page_size);
src/link/MachO.zig
@@ -256,32 +256,36 @@ pub fn createEmpty(
                 .program_code_size_hint = options.program_code_size_hint,
             });
 
-            // TODO init dwarf
-
-            // if (comp.config.debug_format != .strip) {
-            //     // Create dSYM bundle.
-            //     log.debug("creating {s}.dSYM bundle", .{emit.sub_path});
-
-            //     const d_sym_path = try std.fmt.allocPrint(
-            //         arena,
-            //         "{s}.dSYM" ++ fs.path.sep_str ++ "Contents" ++ fs.path.sep_str ++ "Resources" ++ fs.path.sep_str ++ "DWARF",
-            //         .{emit.sub_path},
-            //     );
-
-            //     var d_sym_bundle = try emit.directory.handle.makeOpenPath(d_sym_path, .{});
-            //     defer d_sym_bundle.close();
-
-            //     const d_sym_file = try d_sym_bundle.createFile(emit.sub_path, .{
-            //         .truncate = false,
-            //         .read = true,
-            //     });
-
-            //     self.d_sym = .{
-            //         .allocator = gpa,
-            //         .dwarf = link.File.Dwarf.init(&self.base, .dwarf32),
-            //         .file = d_sym_file,
-            //     };
-            // }
+            switch (comp.config.debug_format) {
+                .strip => {},
+                .dwarf => {
+                    // Create dSYM bundle.
+                    log.debug("creating {s}.dSYM bundle", .{emit.sub_path});
+
+                    const sep = fs.path.sep_str;
+                    const d_sym_path = try std.fmt.allocPrint(
+                        arena,
+                        "{s}.dSYM" ++ sep ++ "Contents" ++ sep ++ "Resources" ++ sep ++ "DWARF",
+                        .{emit.sub_path},
+                    );
+
+                    var d_sym_bundle = try emit.directory.handle.makeOpenPath(d_sym_path, .{});
+                    defer d_sym_bundle.close();
+
+                    const d_sym_file = try d_sym_bundle.createFile(emit.sub_path, .{
+                        .truncate = false,
+                        .read = true,
+                    });
+
+                    self.d_sym = .{
+                        .allocator = gpa,
+                        .dwarf = link.File.Dwarf.init(&self.base, .dwarf32),
+                        .file = d_sym_file,
+                    };
+                    try self.d_sym.?.initMetadata(self);
+                },
+                .code_view => unreachable,
+            }
         }
     }
 
@@ -3906,9 +3910,8 @@ fn reportDuplicates(self: *MachO, dupes: anytype) error{ HasDuplicates, OutOfMem
 }
 
 pub fn getDebugSymbols(self: *MachO) ?*DebugSymbols {
-    if (self.d_sym) |*ds| {
-        return ds;
-    } else return null;
+    if (self.d_sym) |*ds| return ds;
+    return null;
 }
 
 pub fn ptraceAttach(self: *MachO, pid: std.os.pid_t) !void {