| ... | ... | @@ -2346,9 +2346,7 @@ fn allocateSections(self: *MachO) !void { |
| 2346 | 2346 | new_offset + existing_size, |
| 2347 | 2347 | }); |
| 2348 | 2348 | |
| 2349 | | const amt = try self.base.file.?.copyRangeAll(header.offset, self.base.file.?, new_offset, existing_size); |
| 2350 | | // TODO figure out what to about this error condition - how to communicate it up. |
| 2351 | | if (amt != existing_size) return error.InputOutput; |
| 2349 | try self.copyRangeAllZeroOut(header.offset, new_offset, existing_size); |
| 2352 | 2350 | |
| 2353 | 2351 | header.offset = @intCast(new_offset); |
| 2354 | 2352 | header.size = existing_size; |
| ... | ... | @@ -3268,6 +3266,19 @@ fn findFreeSpace(self: *MachO, object_size: u64, min_alignment: u32) u64 { |
| 3268 | 3266 | return start; |
| 3269 | 3267 | } |
| 3270 | 3268 | |
| 3269 | /// Like File.copyRangeAll but also ensures the source region is zeroed out after copy. |
| 3270 | /// This is so that we guarantee zeroed out regions for mapping of zerofill sections by the loader. |
| 3271 | fn copyRangeAllZeroOut(self: *MachO, old_offset: u64, new_offset: u64, size: u64) !void { |
| 3272 | const gpa = self.base.comp.gpa; |
| 3273 | const file = self.base.file.?; |
| 3274 | const amt = try file.copyRangeAll(old_offset, file, new_offset, size); |
| 3275 | if (amt != size) return error.InputOutput; |
| 3276 | const zeroes = try gpa.alloc(u8, size); |
| 3277 | defer gpa.free(zeroes); |
| 3278 | @memset(zeroes, 0); |
| 3279 | try file.pwriteAll(zeroes, old_offset); |
| 3280 | } |
| 3281 | |
| 3271 | 3282 | const InitMetadataOptions = struct { |
| 3272 | 3283 | symbol_count_hint: u64, |
| 3273 | 3284 | program_code_size_hint: u64, |
| ... | ... | @@ -3408,9 +3419,7 @@ pub fn growSection(self: *MachO, sect_index: u8, needed_size: u64) !void { |
| 3408 | 3419 | new_offset + existing_size, |
| 3409 | 3420 | }); |
| 3410 | 3421 | |
| 3411 | | const amt = try self.base.file.?.copyRangeAll(sect.offset, self.base.file.?, new_offset, existing_size); |
| 3412 | | // TODO figure out what to about this error condition - how to communicate it up. |
| 3413 | | if (amt != existing_size) return error.InputOutput; |
| 3422 | try self.copyRangeAllZeroOut(sect.offset, new_offset, existing_size); |
| 3414 | 3423 | |
| 3415 | 3424 | sect.offset = @intCast(new_offset); |
| 3416 | 3425 | seg.fileoff = new_offset; |