authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-02-21 14:27:07+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-21 19:56:55+01:00
log0b7af25637ce88f79461e6b855a8d59318b942ce
treed1b40ada5599451599e3444f37296fb9a2738db5
parent955fd65cb1705d8279eb195bdbc69810df1b1d98

MachO: fix `calcLoadCommandsSize` computation

Closes #19026

6 files changed, 34 insertions(+), 45 deletions(-)

src/link/MachO.zig+5-6
......@@ -2250,7 +2250,7 @@ fn initSegments(self: *MachO) !void {
22502250}
22512251
22522252fn allocateSections(self: *MachO) !void {
2253 const headerpad = load_commands.calcMinHeaderPadSize(self);
2253 const headerpad = try load_commands.calcMinHeaderPadSize(self);
22542254 var vmaddr: u64 = if (self.pagezero_seg_index) |index|
22552255 self.segments.items[index].vmaddr + self.segments.items[index].vmsize
22562256 else
......@@ -2904,13 +2904,12 @@ pub fn writeStrtab(self: *MachO, off: u32) !u32 {
29042904
29052905fn writeLoadCommands(self: *MachO) !struct { usize, usize, u64 } {
29062906 const gpa = self.base.comp.gpa;
2907 const needed_size = load_commands.calcLoadCommandsSize(self, false);
2907 const needed_size = try load_commands.calcLoadCommandsSize(self, false);
29082908 const buffer = try gpa.alloc(u8, needed_size);
29092909 defer gpa.free(buffer);
29102910
29112911 var stream = std.io.fixedBufferStream(buffer);
2912 var cwriter = std.io.countingWriter(stream.writer());
2913 const writer = cwriter.writer();
2912 const writer = stream.writer();
29142913
29152914 var ncmds: usize = 0;
29162915
......@@ -2974,7 +2973,7 @@ fn writeLoadCommands(self: *MachO) !struct { usize, usize, u64 } {
29742973 ncmds += 1;
29752974 }
29762975
2977 const uuid_cmd_offset = @sizeOf(macho.mach_header_64) + cwriter.bytes_written;
2976 const uuid_cmd_offset = @sizeOf(macho.mach_header_64) + stream.pos;
29782977 try writer.writeStruct(self.uuid_cmd);
29792978 ncmds += 1;
29802979
......@@ -3002,7 +3001,7 @@ fn writeLoadCommands(self: *MachO) !struct { usize, usize, u64 } {
30023001 ncmds += 1;
30033002 }
30043003
3005 assert(cwriter.bytes_written == needed_size);
3004 assert(stream.pos == needed_size);
30063005
30073006 try self.base.file.?.pwriteAll(buffer, @sizeOf(macho.mach_header_64));
30083007
src/link/MachO/DebugSymbols.zig+2-3
......@@ -269,8 +269,7 @@ fn writeLoadCommands(self: *DebugSymbols, macho_file: *MachO) !struct { usize, u
269269 defer gpa.free(buffer);
270270
271271 var stream = std.io.fixedBufferStream(buffer);
272 var cwriter = std.io.countingWriter(stream.writer());
273 const writer = cwriter.writer();
272 const writer = stream.writer();
274273
275274 var ncmds: usize = 0;
276275
......@@ -314,7 +313,7 @@ fn writeLoadCommands(self: *DebugSymbols, macho_file: *MachO) !struct { usize, u
314313 try writer.writeStruct(self.symtab_cmd);
315314 ncmds += 1;
316315
317 assert(cwriter.bytes_written == needed_size);
316 assert(stream.pos == needed_size);
318317
319318 try self.file.pwriteAll(buffer, @sizeOf(macho.mach_header_64));
320319
src/link/MachO/UnwindInfo.zig+4-9
......@@ -271,8 +271,7 @@ pub fn write(info: UnwindInfo, macho_file: *MachO, buffer: []u8) !void {
271271 const header = macho_file.sections.items(.header)[macho_file.unwind_info_sect_index.?];
272272
273273 var stream = std.io.fixedBufferStream(buffer);
274 var cwriter = std.io.countingWriter(stream.writer());
275 const writer = cwriter.writer();
274 const writer = stream.writer();
276275
277276 const common_encodings_offset: u32 = @sizeOf(macho.unwind_info_section_header);
278277 const common_encodings_count: u32 = info.common_encodings_count;
......@@ -329,20 +328,16 @@ pub fn write(info: UnwindInfo, macho_file: *MachO, buffer: []u8) !void {
329328 }
330329
331330 for (info.pages.items) |page| {
332 const start = cwriter.bytes_written;
331 const start = stream.pos;
333332 try page.write(info, macho_file, writer);
334 const nwritten = cwriter.bytes_written - start;
333 const nwritten = stream.pos - start;
335334 if (nwritten < second_level_page_bytes) {
336335 const padding = math.cast(usize, second_level_page_bytes - nwritten) orelse return error.Overflow;
337336 try writer.writeByteNTimes(0, padding);
338337 }
339338 }
340339
341 const padding = buffer.len - cwriter.bytes_written;
342 if (padding > 0) {
343 const off = math.cast(usize, cwriter.bytes_written) orelse return error.Overflow;
344 @memset(buffer[off..], 0);
345 }
340 @memset(buffer[stream.pos..], 0);
346341}
347342
348343fn getOrPutPersonalityFunction(info: *UnwindInfo, sym_index: Symbol.Index) error{TooManyPersonalities}!u2 {
src/link/MachO/dyld_info/bind.zig+7-11
......@@ -40,7 +40,7 @@ pub const Bind = struct {
4040 }
4141
4242 pub fn size(self: Self) u64 {
43 return @as(u64, @intCast(self.buffer.items.len));
43 return @intCast(self.buffer.items.len);
4444 }
4545
4646 pub fn finalize(self: *Self, gpa: Allocator, ctx: *MachO) !void {
......@@ -124,7 +124,7 @@ pub const Bind = struct {
124124 switch (state) {
125125 .start => {
126126 if (current.offset < offset) {
127 try addAddr(@as(u64, @bitCast(@as(i64, @intCast(current.offset)) - @as(i64, @intCast(offset)))), writer);
127 try addAddr(@bitCast(@as(i64, @intCast(current.offset)) - @as(i64, @intCast(offset))), writer);
128128 offset = offset - (offset - current.offset);
129129 } else if (current.offset > offset) {
130130 const delta = current.offset - offset;
......@@ -195,7 +195,7 @@ pub const WeakBind = struct {
195195 }
196196
197197 pub fn size(self: Self) u64 {
198 return @as(u64, @intCast(self.buffer.items.len));
198 return @intCast(self.buffer.items.len);
199199 }
200200
201201 pub fn finalize(self: *Self, gpa: Allocator, ctx: *MachO) !void {
......@@ -286,7 +286,7 @@ pub const WeakBind = struct {
286286 } else if (current.offset > offset) {
287287 const delta = current.offset - offset;
288288 state = .bind_times_skip;
289 skip = @as(u64, @intCast(delta));
289 skip = @intCast(delta);
290290 offset += skip;
291291 } else unreachable;
292292 i -= 1;
......@@ -341,23 +341,20 @@ pub const LazyBind = struct {
341341 }
342342
343343 pub fn size(self: Self) u64 {
344 return @as(u64, @intCast(self.buffer.items.len));
344 return @intCast(self.buffer.items.len);
345345 }
346346
347347 pub fn finalize(self: *Self, gpa: Allocator, ctx: *MachO) !void {
348 if (self.entries.items.len == 0) return;
349
350348 try self.offsets.ensureTotalCapacityPrecise(gpa, self.entries.items.len);
351349
352 var cwriter = std.io.countingWriter(self.buffer.writer(gpa));
353 const writer = cwriter.writer();
350 const writer = self.buffer.writer(gpa);
354351
355352 log.debug("lazy bind opcodes", .{});
356353
357354 var addend: i64 = 0;
358355
359356 for (self.entries.items) |entry| {
360 self.offsets.appendAssumeCapacity(@as(u32, @intCast(cwriter.bytes_written)));
357 self.offsets.appendAssumeCapacity(@intCast(self.buffer.items.len));
361358
362359 const sym = ctx.getSymbol(entry.target);
363360 const name = sym.getName(ctx);
......@@ -388,7 +385,6 @@ pub const LazyBind = struct {
388385 }
389386
390387 pub fn write(self: Self, writer: anytype) !void {
391 if (self.size() == 0) return;
392388 try writer.writeAll(self.buffer.items);
393389 }
394390};
src/link/MachO/load_commands.zig+14-13
......@@ -17,7 +17,7 @@ fn calcInstallNameLen(cmd_size: u64, name: []const u8, assume_max_path_len: bool
1717 return mem.alignForward(u64, cmd_size + name_len, @alignOf(u64));
1818}
1919
20pub fn calcLoadCommandsSize(macho_file: *MachO, assume_max_path_len: bool) u32 {
20pub fn calcLoadCommandsSize(macho_file: *MachO, assume_max_path_len: bool) !u32 {
2121 var sizeofcmds: u64 = 0;
2222
2323 // LC_SEGMENT_64
......@@ -48,15 +48,16 @@ pub fn calcLoadCommandsSize(macho_file: *MachO, assume_max_path_len: bool) u32 {
4848 }
4949 // LC_ID_DYLIB
5050 if (macho_file.base.isDynLib()) {
51 sizeofcmds += blk: {
52 const emit = macho_file.base.emit;
53 const install_name = macho_file.install_name orelse emit.sub_path;
54 break :blk calcInstallNameLen(
55 @sizeOf(macho.dylib_command),
56 install_name,
57 assume_max_path_len,
58 );
59 };
51 const gpa = macho_file.base.comp.gpa;
52 const emit = macho_file.base.emit;
53 const install_name = macho_file.install_name orelse
54 try emit.directory.join(gpa, &.{emit.sub_path});
55 defer if (macho_file.install_name == null) gpa.free(install_name);
56 sizeofcmds += calcInstallNameLen(
57 @sizeOf(macho.dylib_command),
58 install_name,
59 assume_max_path_len,
60 );
6061 }
6162 // LC_RPATH
6263 {
......@@ -148,12 +149,12 @@ pub fn calcLoadCommandsSizeObject(macho_file: *MachO) u32 {
148149 return @as(u32, @intCast(sizeofcmds));
149150}
150151
151pub fn calcMinHeaderPadSize(macho_file: *MachO) u32 {
152 var padding: u32 = calcLoadCommandsSize(macho_file, false) + (macho_file.headerpad_size orelse 0);
152pub fn calcMinHeaderPadSize(macho_file: *MachO) !u32 {
153 var padding: u32 = (try calcLoadCommandsSize(macho_file, false)) + (macho_file.headerpad_size orelse 0);
153154 log.debug("minimum requested headerpad size 0x{x}", .{padding + @sizeOf(macho.mach_header_64)});
154155
155156 if (macho_file.headerpad_max_install_names) {
156 const min_headerpad_size: u32 = calcLoadCommandsSize(macho_file, true);
157 const min_headerpad_size: u32 = try calcLoadCommandsSize(macho_file, true);
157158 log.debug("headerpad_max_install_names minimum headerpad size 0x{x}", .{
158159 min_headerpad_size + @sizeOf(macho.mach_header_64),
159160 });
src/link/MachO/relocatable.zig+2-3
......@@ -748,8 +748,7 @@ fn writeLoadCommands(macho_file: *MachO) !struct { usize, usize } {
748748 defer gpa.free(buffer);
749749
750750 var stream = std.io.fixedBufferStream(buffer);
751 var cwriter = std.io.countingWriter(stream.writer());
752 const writer = cwriter.writer();
751 const writer = stream.writer();
753752
754753 var ncmds: usize = 0;
755754
......@@ -779,7 +778,7 @@ fn writeLoadCommands(macho_file: *MachO) !struct { usize, usize } {
779778 ncmds += 1;
780779 }
781780
782 assert(cwriter.bytes_written == needed_size);
781 assert(stream.pos == needed_size);
783782
784783 try macho_file.base.file.?.pwriteAll(buffer, @sizeOf(macho.mach_header_64));
785784