| ... | @@ -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 will | 4300 | // 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) orelse | 4311 | 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 | } |
| 4323 | | 4320 | |