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 {...@@ -3403,6 +3403,15 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {
3403 }3403 }
3404 }.appendSect;3404 }.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
3406 {3415 {
3407 self.zig_text_sect_index = try self.addSection("__TEXT_ZIG", "__text_zig", .{3416 self.zig_text_sect_index = try self.addSection("__TEXT_ZIG", "__text_zig", .{
3408 .alignment = switch (self.getTarget().cpu.arch) {3417 .alignment = switch (self.getTarget().cpu.arch) {
...@@ -3412,7 +3421,11 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {...@@ -3412,7 +3421,11 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {
3412 },3421 },
3413 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,3422 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
3414 });3423 });
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 }
3416 }3429 }
34173430
3418 if (!self.base.isRelocatable()) {3431 if (!self.base.isRelocatable()) {
...@@ -3424,19 +3437,31 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {...@@ -3424,19 +3437,31 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {
34243437
3425 {3438 {
3426 self.zig_const_sect_index = try self.addSection("__CONST_ZIG", "__const_zig", .{});3439 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 }
3428 }3445 }
34293446
3430 {3447 {
3431 self.zig_data_sect_index = try self.addSection("__DATA_ZIG", "__data_zig", .{});3448 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 }
3433 }3454 }
34343455
3435 {3456 {
3436 self.zig_bss_sect_index = try self.addSection("__BSS_ZIG", "__bss_zig", .{3457 self.zig_bss_sect_index = try self.addSection("__BSS_ZIG", "__bss_zig", .{
3437 .flags = macho.S_ZEROFILL,3458 .flags = macho.S_ZEROFILL,
3438 });3459 });
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 }
3440 }3465 }
3441}3466}
34423467