| ... | @@ -1,7 +1,9 @@ | ... | @@ -1,7 +1,9 @@ |
| 1 | const DebugSymbols = @This(); | 1 | const DebugSymbols = @This(); |
| 2 | | 2 | |
| 3 | const std = @import("std"); | 3 | const std = @import("std"); |
| | 4 | const assert = std.debug.assert; |
| 4 | const fs = std.fs; | 5 | const fs = std.fs; |
| | 6 | const log = std.log.scoped(.link); |
| 5 | const macho = std.macho; | 7 | const macho = std.macho; |
| 6 | const mem = std.mem; | 8 | const mem = std.mem; |
| 7 | const DW = std.dwarf; | 9 | const DW = std.dwarf; |
| ... | @@ -35,6 +37,49 @@ symtab_cmd_index: ?u16 = null, | ... | @@ -35,6 +37,49 @@ symtab_cmd_index: ?u16 = null, |
| 35 | /// UUID load command | 37 | /// UUID load command |
| 36 | uuid_cmd_index: ?u16 = null, | 38 | uuid_cmd_index: ?u16 = null, |
| 37 | | 39 | |
| | 40 | header_dirty: bool = false, |
| | 41 | load_commands_dirty: bool = false, |
| | 42 | |
| | 43 | /// You must call this function *after* `MachO.populateMissingMetadata()` |
| | 44 | /// has been called to get a viable debug symbols output. |
| | 45 | pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void { |
| | 46 | if (self.header == null) { |
| | 47 | const base_header = self.base.header.?; |
| | 48 | var header: macho.mach_header_64 = undefined; |
| | 49 | header.magic = macho.MH_MAGIC_64; |
| | 50 | header.cputype = base_header.cputype; |
| | 51 | header.cpusubtype = base_header.cpusubtype; |
| | 52 | header.filetype = macho.MH_DSYM; |
| | 53 | // These will get populated at the end of flushing the results to file. |
| | 54 | header.ncmds = 0; |
| | 55 | header.sizeofcmds = 0; |
| | 56 | header.flags = 0; |
| | 57 | header.reserved = 0; |
| | 58 | self.header = header; |
| | 59 | self.header_dirty = true; |
| | 60 | } |
| | 61 | } |
| | 62 | |
| | 63 | pub fn flush(self: *DebugSymbols) !void { |
| | 64 | try self.writeHeader(); |
| | 65 | assert(!self.header_dirty); |
| | 66 | assert(!self.load_commands_dirty); |
| | 67 | } |
| | 68 | |
| 38 | pub fn deinit(self: *DebugSymbols, allocator: *Allocator) void { | 69 | pub fn deinit(self: *DebugSymbols, allocator: *Allocator) void { |
| 39 | self.file.close(); | 70 | self.file.close(); |
| 40 | } | 71 | } |
| | 72 | |
| | 73 | fn writeHeader(self: *DebugSymbols) !void { |
| | 74 | if (!self.header_dirty) return; |
| | 75 | |
| | 76 | self.header.?.ncmds = @intCast(u32, self.load_commands.items.len); |
| | 77 | var sizeofcmds: u32 = 0; |
| | 78 | for (self.load_commands.items) |cmd| { |
| | 79 | sizeofcmds += cmd.cmdsize(); |
| | 80 | } |
| | 81 | self.header.?.sizeofcmds = sizeofcmds; |
| | 82 | log.debug("writing Mach-O dSym header {}", .{self.header.?}); |
| | 83 | try self.file.pwriteAll(mem.asBytes(&self.header.?), 0); |
| | 84 | self.header_dirty = false; |
| | 85 | } |