authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-01 12:56:54+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-03 09:25:42+01:00
log88a4bd6cf63b44b1992ffb6f1b47c287242c07c0
tree629a17a1dd67b8cdcd57e949636a34f25036fb9e
parent352941b0308057b434f6db664e66a9ea793fac95

macho: pre-alloc sections in -r mode


1 files changed, 29 insertions(+), 4 deletions(-)

src/link/MachO.zig+29-4
......@@ -3403,6 +3403,15 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {
34033403 }
34043404 }.appendSect;
34053405
3406 const allocSect = struct {
3407 fn allocSect(macho_file: *MachO, sect_id: u8, size: u64) !void {
3408 const sect = &macho_file.sections.items(.header)[sect_id];
3409 const alignment = try math.powi(u32, 2, sect.@"align");
3410 sect.offset = @intCast(macho_file.findFreeSpace(size, alignment));
3411 sect.size = size;
3412 }
3413 }.allocSect;
3414
34063415 {
34073416 self.zig_text_sect_index = try self.addSection("__TEXT_ZIG", "__text_zig", .{
34083417 .alignment = switch (self.getTarget().cpu.arch) {
......@@ -3412,7 +3421,11 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {
34123421 },
34133422 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
34143423 });
3415 if (!self.base.isRelocatable()) appendSect(self, self.zig_text_sect_index.?, self.zig_text_seg_index.?);
3424 if (self.base.isRelocatable()) {
3425 try allocSect(self, self.zig_text_sect_index.?, options.program_code_size_hint);
3426 } else {
3427 appendSect(self, self.zig_text_sect_index.?, self.zig_text_seg_index.?);
3428 }
34163429 }
34173430
34183431 if (!self.base.isRelocatable()) {
......@@ -3424,19 +3437,31 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {
34243437
34253438 {
34263439 self.zig_const_sect_index = try self.addSection("__CONST_ZIG", "__const_zig", .{});
3427 if (!self.base.isRelocatable()) appendSect(self, self.zig_const_sect_index.?, self.zig_const_seg_index.?);
3440 if (self.base.isRelocatable()) {
3441 try allocSect(self, self.zig_const_sect_index.?, 1024);
3442 } else {
3443 appendSect(self, self.zig_const_sect_index.?, self.zig_const_seg_index.?);
3444 }
34283445 }
34293446
34303447 {
34313448 self.zig_data_sect_index = try self.addSection("__DATA_ZIG", "__data_zig", .{});
3432 if (!self.base.isRelocatable()) appendSect(self, self.zig_data_sect_index.?, self.zig_data_seg_index.?);
3449 if (self.base.isRelocatable()) {
3450 try allocSect(self, self.zig_data_sect_index.?, 1024);
3451 } else {
3452 appendSect(self, self.zig_data_sect_index.?, self.zig_data_seg_index.?);
3453 }
34333454 }
34343455
34353456 {
34363457 self.zig_bss_sect_index = try self.addSection("__BSS_ZIG", "__bss_zig", .{
34373458 .flags = macho.S_ZEROFILL,
34383459 });
3439 if (!self.base.isRelocatable()) appendSect(self, self.zig_bss_sect_index.?, self.zig_bss_seg_index.?);
3460 if (self.base.isRelocatable()) {
3461 self.sections.items(.header)[self.zig_bss_sect_index.?].size = 1024;
3462 } else {
3463 appendSect(self, self.zig_bss_sect_index.?, self.zig_bss_seg_index.?);
3464 }
34403465 }
34413466}
34423467