authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-11-06 08:39:13+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-09 13:50:08-07:00
log2593d4d7d69c1a8df80907c1a40219be22f475b9
treeb33d68c1798717c7657d321f551028a71ca436cc
parent3608b1d036e6b16cbea3dac59961b203e3532901

Merge pull request #13459 from ziglang/issue-13457

macho: do not zero-out file if there are no nonzerofill sects

4 files changed, 29 insertions(+), 10 deletions(-)

src/link/MachO/zld.zig+7-10
......@@ -4300,24 +4300,21 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
43004300 // segment and the beginning of __LINKEDIT segment is zerofilled as the loader will
43014301 // copy-paste this space into memory for quicker zerofill operation.
43024302 if (zld.getSegmentByName("__DATA")) |data_seg_id| blk: {
4303 var physical_zerofill_start: u64 = 0;
4303 var physical_zerofill_start: ?u64 = null;
43044304 const section_indexes = zld.getSectionIndexes(data_seg_id);
43054305 for (zld.sections.items(.header)[section_indexes.start..section_indexes.end]) |header| {
43064306 if (header.isZerofill() and header.size > 0) break;
43074307 physical_zerofill_start = header.offset + header.size;
43084308 } else break :blk;
4309 const start = physical_zerofill_start orelse break :blk;
43094310 const linkedit = zld.getLinkeditSegmentPtr();
4310 const physical_zerofill_size = math.cast(usize, linkedit.fileoff - physical_zerofill_start) orelse
4311 return error.Overflow;
4312 if (physical_zerofill_size > 0) {
4313 log.debug("zeroing out zerofill area of length {x} at {x}", .{
4314 physical_zerofill_size,
4315 physical_zerofill_start,
4316 });
4317 var padding = try zld.gpa.alloc(u8, physical_zerofill_size);
4311 const size = math.cast(usize, linkedit.fileoff - start) orelse return error.Overflow;
4312 if (size > 0) {
4313 log.debug("zeroing out zerofill area of length {x} at {x}", .{ size, start });
4314 var padding = try zld.gpa.alloc(u8, size);
43184315 defer zld.gpa.free(padding);
43194316 mem.set(u8, padding, 0);
4320 try zld.file.pwriteAll(padding, physical_zerofill_start);
4317 try zld.file.pwriteAll(padding, start);
43214318 }
43224319 }
43234320
test/link.zig+4
......@@ -79,6 +79,10 @@ fn addWasmCases(cases: *tests.StandaloneContext) void {
7979}
8080
8181fn addMachOCases(cases: *tests.StandaloneContext) void {
82 cases.addBuildFile("test/link/macho/bugs/13457/build.zig", .{
83 .build_modes = true,
84 });
85
8286 cases.addBuildFile("test/link/macho/dead_strip/build.zig", .{
8387 .build_modes = false,
8488 });
test/link/macho/bugs/13457/build.zig created+17
......@@ -0,0 +1,17 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3const LibExeObjectStep = std.build.LibExeObjStep;
4
5pub fn build(b: *Builder) void {
6 const mode = b.standardReleaseOptions();
7 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
8
9 const test_step = b.step("test", "Test the program");
10
11 const exe = b.addExecutable("test", "main.zig");
12 exe.setBuildMode(mode);
13 exe.setTarget(target);
14
15 const run = exe.runEmulatable();
16 test_step.dependOn(&run.step);
17}
test/link/macho/bugs/13457/main.zig created+1
......@@ -0,0 +1 @@
1pub fn main() void {}