From 88a4bd6cf63b44b1992ffb6f1b47c287242c07c0 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 1 Feb 2024 12:56:54 +0100 Subject: [PATCH] macho: pre-alloc sections in -r mode --- src/link/MachO.zig | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 5baccb51fa7ce4ccf5d62f5e2418b1dcd8038271..62fbaad50bb753b8cb3c516f19935c54bcfe3843 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -3403,6 +3403,15 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void { } }.appendSect; + const allocSect = struct { + fn allocSect(macho_file: *MachO, sect_id: u8, size: u64) !void { + const sect = &macho_file.sections.items(.header)[sect_id]; + const alignment = try math.powi(u32, 2, sect.@"align"); + sect.offset = @intCast(macho_file.findFreeSpace(size, alignment)); + sect.size = size; + } + }.allocSect; + { self.zig_text_sect_index = try self.addSection("__TEXT_ZIG", "__text_zig", .{ .alignment = switch (self.getTarget().cpu.arch) { @@ -3412,7 +3421,11 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void { }, .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, }); - if (!self.base.isRelocatable()) appendSect(self, self.zig_text_sect_index.?, self.zig_text_seg_index.?); + if (self.base.isRelocatable()) { + try allocSect(self, self.zig_text_sect_index.?, options.program_code_size_hint); + } else { + appendSect(self, self.zig_text_sect_index.?, self.zig_text_seg_index.?); + } } if (!self.base.isRelocatable()) { @@ -3424,19 +3437,31 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void { { self.zig_const_sect_index = try self.addSection("__CONST_ZIG", "__const_zig", .{}); - if (!self.base.isRelocatable()) appendSect(self, self.zig_const_sect_index.?, self.zig_const_seg_index.?); + if (self.base.isRelocatable()) { + try allocSect(self, self.zig_const_sect_index.?, 1024); + } else { + appendSect(self, self.zig_const_sect_index.?, self.zig_const_seg_index.?); + } } { self.zig_data_sect_index = try self.addSection("__DATA_ZIG", "__data_zig", .{}); - if (!self.base.isRelocatable()) appendSect(self, self.zig_data_sect_index.?, self.zig_data_seg_index.?); + if (self.base.isRelocatable()) { + try allocSect(self, self.zig_data_sect_index.?, 1024); + } else { + appendSect(self, self.zig_data_sect_index.?, self.zig_data_seg_index.?); + } } { self.zig_bss_sect_index = try self.addSection("__BSS_ZIG", "__bss_zig", .{ .flags = macho.S_ZEROFILL, }); - if (!self.base.isRelocatable()) appendSect(self, self.zig_bss_sect_index.?, self.zig_bss_seg_index.?); + if (self.base.isRelocatable()) { + self.sections.items(.header)[self.zig_bss_sect_index.?].size = 1024; + } else { + appendSect(self, self.zig_bss_sect_index.?, self.zig_bss_seg_index.?); + } } } -- 2.54.0