| ... | ... | @@ -1157,6 +1157,28 @@ fn linkOneShot(self: *MachO, comp: *Compilation, prog_node: *std.Progress.Node) |
| 1157 | 1157 | var ncmds: u32 = 0; |
| 1158 | 1158 | |
| 1159 | 1159 | try self.writeLinkeditSegmentData(&ncmds, lc_writer); |
| 1160 | |
| 1161 | // If the last section of __DATA segment is zerofill section, we need to ensure |
| 1162 | // that the free space between the end of the last non-zerofill section of __DATA |
| 1163 | // segment and the beginning of __LINKEDIT segment is zerofilled as the loader will |
| 1164 | // copy-paste this space into memory for quicker zerofill operation. |
| 1165 | if (self.data_segment_cmd_index) |data_seg_id| blk: { |
| 1166 | var physical_zerofill_start: u64 = 0; |
| 1167 | const section_indexes = self.getSectionIndexes(data_seg_id); |
| 1168 | for (self.sections.items(.header)[section_indexes.start..section_indexes.end]) |header| { |
| 1169 | if (header.isZerofill() and header.size > 0) break; |
| 1170 | physical_zerofill_start = header.offset + header.size; |
| 1171 | } else break :blk; |
| 1172 | const linkedit = self.segments.items[self.linkedit_segment_cmd_index.?]; |
| 1173 | const physical_zerofill_size = linkedit.fileoff - physical_zerofill_start; |
| 1174 | if (physical_zerofill_size > 0) { |
| 1175 | var padding = try self.base.allocator.alloc(u8, physical_zerofill_size); |
| 1176 | defer self.base.allocator.free(padding); |
| 1177 | mem.set(u8, padding, 0); |
| 1178 | try self.base.file.?.pwriteAll(padding, physical_zerofill_start); |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1160 | 1182 | try writeDylinkerLC(&ncmds, lc_writer); |
| 1161 | 1183 | try self.writeMainLC(&ncmds, lc_writer); |
| 1162 | 1184 | try self.writeDylibIdLC(&ncmds, lc_writer); |
| ... | ... | @@ -5690,8 +5712,10 @@ fn writeHeader(self: *MachO, ncmds: u32, sizeofcmds: u32) !void { |
| 5690 | 5712 | else => unreachable, |
| 5691 | 5713 | } |
| 5692 | 5714 | |
| 5693 | | if (self.getSectionByName("__DATA", "__thread_vars")) |_| { |
| 5694 | | header.flags |= macho.MH_HAS_TLV_DESCRIPTORS; |
| 5715 | if (self.getSectionByName("__DATA", "__thread_vars")) |sect_id| { |
| 5716 | if (self.sections.items(.header)[sect_id].size > 0) { |
| 5717 | header.flags |= macho.MH_HAS_TLV_DESCRIPTORS; |
| 5718 | } |
| 5695 | 5719 | } |
| 5696 | 5720 | |
| 5697 | 5721 | header.ncmds = ncmds; |