aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2022-11-06 00:36:27 +0100
committerJakub Konka <kubkon@jakubkonka.com>2022-11-06 00:36:27 +0100
commit76fb3e062161e84554e0665444135281f7bcaf74 (patch)
tree95ba509db81c7e25678dad4debde931de93b91b1 /src
parentaaaa7df15264edd38d755eb77253d54073e9f192 (diff)
downloadzig-76fb3e062161e84554e0665444135281f7bcaf74.tar.gz
zig-76fb3e062161e84554e0665444135281f7bcaf74.zip
macho: do not zero-out file if there are no nonzerofill sects
If the `__DATA` segment comprises of only zerofill sections, do not accidentally zero out all of file.
Diffstat (limited to 'src')
-rw-r--r--src/link/MachO/zld.zig17
1 files changed, 7 insertions, 10 deletions
diff --git a/src/link/MachO/zld.zig b/src/link/MachO/zld.zig
index 9508d68895..7aaa3887bb 100644
--- a/src/link/MachO/zld.zig
+++ b/src/link/MachO/zld.zig
@@ -4305,24 +4305,21 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
// segment and the beginning of __LINKEDIT segment is zerofilled as the loader will
// copy-paste this space into memory for quicker zerofill operation.
if (zld.getSegmentByName("__DATA")) |data_seg_id| blk: {
- var physical_zerofill_start: u64 = 0;
+ var physical_zerofill_start: ?u64 = null;
const section_indexes = zld.getSectionIndexes(data_seg_id);
for (zld.sections.items(.header)[section_indexes.start..section_indexes.end]) |header| {
if (header.isZerofill() and header.size > 0) break;
physical_zerofill_start = header.offset + header.size;
} else break :blk;
+ const start = physical_zerofill_start orelse break :blk;
const linkedit = zld.getLinkeditSegmentPtr();
- const physical_zerofill_size = math.cast(usize, linkedit.fileoff - physical_zerofill_start) orelse
- return error.Overflow;
- if (physical_zerofill_size > 0) {
- log.debug("zeroing out zerofill area of length {x} at {x}", .{
- physical_zerofill_size,
- physical_zerofill_start,
- });
- var padding = try zld.gpa.alloc(u8, physical_zerofill_size);
+ const size = math.cast(usize, linkedit.fileoff - start) orelse return error.Overflow;
+ if (size > 0) {
+ log.debug("zeroing out zerofill area of length {x} at {x}", .{ size, start });
+ var padding = try zld.gpa.alloc(u8, size);
defer zld.gpa.free(padding);
mem.set(u8, padding, 0);
- try zld.file.pwriteAll(padding, physical_zerofill_start);
+ try zld.file.pwriteAll(padding, start);
}
}