authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-11-06 08:39:13+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-11-06 08:39:13+01:00
log5ef33e7c7ea58f3777b0acac2335bd940b8a1fa6
treeaf775c70c1a95ef8166f6ac129517a28e6c72397
parentc8a5ad6d9db529f3440bfd53b1bda8ded6a50a7c
parent76fb3e062161e84554e0665444135281f7bcaf74
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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...@@ -4300,24 +4300,21 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
4300 // segment and the beginning of __LINKEDIT segment is zerofilled as the loader will4300 // segment and the beginning of __LINKEDIT segment is zerofilled as the loader will
4301 // copy-paste this space into memory for quicker zerofill operation.4301 // copy-paste this space into memory for quicker zerofill operation.
4302 if (zld.getSegmentByName("__DATA")) |data_seg_id| blk: {4302 if (zld.getSegmentByName("__DATA")) |data_seg_id| blk: {
4303 var physical_zerofill_start: u64 = 0;4303 var physical_zerofill_start: ?u64 = null;
4304 const section_indexes = zld.getSectionIndexes(data_seg_id);4304 const section_indexes = zld.getSectionIndexes(data_seg_id);
4305 for (zld.sections.items(.header)[section_indexes.start..section_indexes.end]) |header| {4305 for (zld.sections.items(.header)[section_indexes.start..section_indexes.end]) |header| {
4306 if (header.isZerofill() and header.size > 0) break;4306 if (header.isZerofill() and header.size > 0) break;
4307 physical_zerofill_start = header.offset + header.size;4307 physical_zerofill_start = header.offset + header.size;
4308 } else break :blk;4308 } else break :blk;
4309 const start = physical_zerofill_start orelse break :blk;
4309 const linkedit = zld.getLinkeditSegmentPtr();4310 const linkedit = zld.getLinkeditSegmentPtr();
4310 const physical_zerofill_size = math.cast(usize, linkedit.fileoff - physical_zerofill_start) orelse4311 const size = math.cast(usize, linkedit.fileoff - start) orelse return error.Overflow;
4311 return error.Overflow;4312 if (size > 0) {
4312 if (physical_zerofill_size > 0) {4313 log.debug("zeroing out zerofill area of length {x} at {x}", .{ size, start });
4313 log.debug("zeroing out zerofill area of length {x} at {x}", .{4314 var padding = try zld.gpa.alloc(u8, size);
4314 physical_zerofill_size,
4315 physical_zerofill_start,
4316 });
4317 var padding = try zld.gpa.alloc(u8, physical_zerofill_size);
4318 defer zld.gpa.free(padding);4315 defer zld.gpa.free(padding);
4319 mem.set(u8, padding, 0);4316 mem.set(u8, padding, 0);
4320 try zld.file.pwriteAll(padding, physical_zerofill_start);4317 try zld.file.pwriteAll(padding, start);
4321 }4318 }
4322 }4319 }
43234320
test/link.zig+4
...@@ -79,6 +79,10 @@ fn addWasmCases(cases: *tests.StandaloneContext) void {...@@ -79,6 +79,10 @@ fn addWasmCases(cases: *tests.StandaloneContext) void {
79}79}
8080
81fn addMachOCases(cases: *tests.StandaloneContext) void {81fn addMachOCases(cases: *tests.StandaloneContext) void {
82 cases.addBuildFile("test/link/macho/bugs/13457/build.zig", .{
83 .build_modes = true,
84 });
85
82 cases.addBuildFile("test/link/macho/dead_strip/build.zig", .{86 cases.addBuildFile("test/link/macho/dead_strip/build.zig", .{
83 .build_modes = false,87 .build_modes = false,
84 });88 });
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 {}