| ... | @@ -58,18 +58,51 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void | ... | @@ -58,18 +58,51 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 58 | self.header = header; | 58 | self.header = header; |
| 59 | self.header_dirty = true; | 59 | self.header_dirty = true; |
| 60 | } | 60 | } |
| | 61 | if (self.uuid_cmd_index == null) { |
| | 62 | const base_cmd = self.base.load_commands.items[self.base.uuid_cmd_index.?]; |
| | 63 | self.uuid_cmd_index = @intCast(u16, self.load_commands.items.len); |
| | 64 | try self.load_commands.append(allocator, base_cmd); |
| | 65 | self.header_dirty = true; |
| | 66 | self.load_commands_dirty = true; |
| | 67 | } |
| 61 | } | 68 | } |
| 62 | | 69 | |
| 63 | pub fn flush(self: *DebugSymbols) !void { | 70 | pub fn flush(self: *DebugSymbols, allocator: *Allocator) !void { |
| | 71 | try self.writeLoadCommands(allocator); |
| 64 | try self.writeHeader(); | 72 | try self.writeHeader(); |
| 65 | assert(!self.header_dirty); | 73 | assert(!self.header_dirty); |
| 66 | assert(!self.load_commands_dirty); | 74 | assert(!self.load_commands_dirty); |
| 67 | } | 75 | } |
| 68 | | 76 | |
| 69 | pub fn deinit(self: *DebugSymbols, allocator: *Allocator) void { | 77 | pub fn deinit(self: *DebugSymbols, allocator: *Allocator) void { |
| | 78 | for (self.load_commands.items) |*lc| { |
| | 79 | lc.deinit(allocator); |
| | 80 | } |
| 70 | self.file.close(); | 81 | self.file.close(); |
| 71 | } | 82 | } |
| 72 | | 83 | |
| | 84 | /// Writes all load commands and section headers. |
| | 85 | fn writeLoadCommands(self: *DebugSymbols, allocator: *Allocator) !void { |
| | 86 | if (!self.load_commands_dirty) return; |
| | 87 | |
| | 88 | var sizeofcmds: usize = 0; |
| | 89 | for (self.load_commands.items) |lc| { |
| | 90 | sizeofcmds += lc.cmdsize(); |
| | 91 | } |
| | 92 | |
| | 93 | var buffer = try allocator.alloc(u8, sizeofcmds); |
| | 94 | defer allocator.free(buffer); |
| | 95 | var writer = std.io.fixedBufferStream(buffer).writer(); |
| | 96 | for (self.load_commands.items) |lc| { |
| | 97 | try lc.write(writer); |
| | 98 | } |
| | 99 | |
| | 100 | const off = @sizeOf(macho.mach_header_64); |
| | 101 | log.debug("writing {} dSym load commands from 0x{x} to 0x{x}", .{ self.load_commands.items.len, off, off + sizeofcmds }); |
| | 102 | try self.file.pwriteAll(buffer, off); |
| | 103 | self.load_commands_dirty = false; |
| | 104 | } |
| | 105 | |
| 73 | fn writeHeader(self: *DebugSymbols) !void { | 106 | fn writeHeader(self: *DebugSymbols) !void { |
| 74 | if (!self.header_dirty) return; | 107 | if (!self.header_dirty) return; |
| 75 | | 108 | |