authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-01-20 00:14:00+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-20 02:21:58-05:00
log5eaac2fb99ad7357eb7bf2451adcaeec4108aec3
treef7c2037f8dc1e235a48e3d7777c2cc868d4b28bf
parent538c9e7bafd859148352e76c1d8053e1ba426a06

macho: do not write out ZEROFILL physically to file

Prior to this change, `__DATA,__bss` and `__DATA,__thread_bss` would get actually, physically written out to the output file, unnecessarily filling the output file with 0s.

1 files changed, 18 insertions(+), 34 deletions(-)

src/link/MachO.zig+18-34
......@@ -919,17 +919,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
919919 try self.parseDependentLibs(self.base.options.sysroot, &dependent_libs);
920920 }
921921
922 if (self.bss_section_index) |idx| {
923 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].segment;
924 const sect = &seg.sections.items[idx];
925 sect.offset = self.bss_file_offset;
926 }
927 if (self.tlv_bss_section_index) |idx| {
928 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].segment;
929 const sect = &seg.sections.items[idx];
930 sect.offset = self.tlv_bss_file_offset;
931 }
932
933922 try self.createMhExecuteHeaderAtom();
934923 for (self.objects.items) |*object, object_id| {
935924 if (object.analyzed) continue;
......@@ -1021,18 +1010,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
10211010 const sect = &seg.sections.items[id];
10221011 sect.size = self.rustc_section_size;
10231012 }
1024 if (self.bss_section_index) |idx| {
1025 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].segment;
1026 const sect = &seg.sections.items[idx];
1027 self.bss_file_offset = sect.offset;
1028 sect.offset = 0;
1029 }
1030 if (self.tlv_bss_section_index) |idx| {
1031 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].segment;
1032 const sect = &seg.sections.items[idx];
1033 self.tlv_bss_file_offset = sect.offset;
1034 sect.offset = 0;
1035 }
10361013
10371014 try self.setEntryPoint();
10381015 try self.updateSectionOrdinals();
......@@ -2127,6 +2104,8 @@ fn writeAllAtoms(self: *MachO) !void {
21272104 const sect = seg.sections.items[match.sect];
21282105 var atom: *Atom = entry.value_ptr.*;
21292106
2107 if (sect.flags == macho.S_ZEROFILL or sect.flags == macho.S_THREAD_LOCAL_ZEROFILL) continue;
2108
21302109 var buffer = std.ArrayList(u8).init(self.base.allocator);
21312110 defer buffer.deinit();
21322111 try buffer.ensureTotalCapacity(try math.cast(usize, sect.size));
......@@ -2179,6 +2158,8 @@ fn writeAtoms(self: *MachO) !void {
21792158 const sect = seg.sections.items[match.sect];
21802159 var atom: *Atom = entry.value_ptr.*;
21812160
2161 if (sect.flags == macho.S_ZEROFILL or sect.flags == macho.S_THREAD_LOCAL_ZEROFILL) continue;
2162
21822163 log.debug("writing atoms in {s},{s}", .{ sect.segName(), sect.sectName() });
21832164
21842165 while (atom.prev) |prev| {
......@@ -4463,9 +4444,6 @@ fn populateMissingMetadata(self: *MachO) !void {
44634444 .flags = macho.S_THREAD_LOCAL_ZEROFILL,
44644445 },
44654446 );
4466 const seg = self.load_commands.items[self.data_segment_cmd_index.?].segment;
4467 const sect = seg.sections.items[self.tlv_bss_section_index.?];
4468 self.tlv_bss_file_offset = sect.offset;
44694447 }
44704448
44714449 if (self.bss_section_index == null) {
......@@ -4480,9 +4458,6 @@ fn populateMissingMetadata(self: *MachO) !void {
44804458 .flags = macho.S_ZEROFILL,
44814459 },
44824460 );
4483 const seg = self.load_commands.items[self.data_segment_cmd_index.?].segment;
4484 const sect = seg.sections.items[self.bss_section_index.?];
4485 self.bss_file_offset = sect.offset;
44864461 }
44874462
44884463 if (self.linkedit_segment_cmd_index == null) {
......@@ -4782,9 +4757,10 @@ fn allocateSegment(self: *MachO, index: u16, offset: u64) !void {
47824757 // Allocate the sections according to their alignment at the beginning of the segment.
47834758 var start: u64 = offset;
47844759 for (seg.sections.items) |*sect, sect_id| {
4760 const is_zerofill = sect.flags == macho.S_ZEROFILL or sect.flags == macho.S_THREAD_LOCAL_ZEROFILL;
47854761 const alignment = try math.powi(u32, 2, sect.@"align");
47864762 const start_aligned = mem.alignForwardGeneric(u64, start, alignment);
4787 sect.offset = @intCast(u32, seg.inner.fileoff + start_aligned);
4763 sect.offset = if (is_zerofill) 0 else @intCast(u32, seg.inner.fileoff + start_aligned);
47884764 sect.addr = seg.inner.vmaddr + start_aligned;
47894765
47904766 // Recalculate section size given the allocated start address
......@@ -4811,11 +4787,15 @@ fn allocateSegment(self: *MachO, index: u16, offset: u64) !void {
48114787 } else 0;
48124788
48134789 start = start_aligned + sect.size;
4790
4791 if (!is_zerofill) {
4792 seg.inner.filesize = start;
4793 }
4794 seg.inner.vmsize = start;
48144795 }
48154796
4816 const seg_size_aligned = mem.alignForwardGeneric(u64, start, self.page_size);
4817 seg.inner.filesize = seg_size_aligned;
4818 seg.inner.vmsize = seg_size_aligned;
4797 seg.inner.filesize = mem.alignForwardGeneric(u64, seg.inner.filesize, self.page_size);
4798 seg.inner.vmsize = mem.alignForwardGeneric(u64, seg.inner.vmsize, self.page_size);
48194799}
48204800
48214801const InitSectionOpts = struct {
......@@ -4853,8 +4833,12 @@ fn initSection(
48534833 off,
48544834 off + size,
48554835 });
4836
48564837 sect.addr = seg.inner.vmaddr + off - seg.inner.fileoff;
4857 sect.offset = @intCast(u32, off);
4838
4839 if (opts.flags != macho.S_ZEROFILL and opts.flags != macho.S_THREAD_LOCAL_ZEROFILL) {
4840 sect.offset = @intCast(u32, off);
4841 }
48584842 }
48594843
48604844 const index = @intCast(u16, seg.sections.items.len);