authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-19 18:19:36+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-24 12:34:41+01:00
log7647db327328bffea7dc8d7286cb6be9265080be
tree3bc09c167f68ff1763ab90728dc7e14576639a5b
parent16b66588f02743aafeb5972644271d92bdcf1051

macho: reserve space for __got_zig rebase opcodes


3 files changed, 31 insertions(+), 30 deletions(-)

src/link/MachO.zig+16-15
......@@ -93,11 +93,11 @@ zig_data_seg_index: ?u8 = null,
9393zig_bss_seg_index: ?u8 = null,
9494
9595/// Tracked section headers with incremental updates to Zig object.
96zig_text_section_index: ?u8 = null,
97zig_got_section_index: ?u8 = null,
98zig_const_section_index: ?u8 = null,
99zig_data_section_index: ?u8 = null,
100zig_bss_section_index: ?u8 = null,
96zig_text_sect_index: ?u8 = null,
97zig_got_sect_index: ?u8 = null,
98zig_const_sect_index: ?u8 = null,
99zig_data_sect_index: ?u8 = null,
100zig_bss_sect_index: ?u8 = null,
101101
102102has_tlv: bool = false,
103103binds_to_weak: bool = false,
......@@ -2423,6 +2423,7 @@ fn initDyldInfoSections(self: *MachO) !void {
24232423
24242424 const gpa = self.base.comp.gpa;
24252425
2426 if (self.zig_got_sect_index != null) try self.zig_got.addDyldRelocs(self);
24262427 if (self.got_sect_index != null) try self.got.addDyldRelocs(self);
24272428 if (self.tlv_ptr_sect_index != null) try self.tlv_ptr.addDyldRelocs(self);
24282429 if (self.la_symbol_ptr_sect_index != null) try self.la_symbol_ptr.addDyldRelocs(self);
......@@ -3291,7 +3292,7 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {
32913292 }.appendSect;
32923293
32933294 {
3294 self.zig_text_section_index = try self.addSection("__TEXT_ZIG", "__text_zig", .{
3295 self.zig_text_sect_index = try self.addSection("__TEXT_ZIG", "__text_zig", .{
32953296 .alignment = switch (self.getTarget().cpu.arch) {
32963297 .aarch64 => 2,
32973298 .x86_64 => 0,
......@@ -3299,31 +3300,31 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {
32993300 },
33003301 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
33013302 });
3302 appendSect(self, self.zig_text_section_index.?, self.zig_text_seg_index.?);
3303 appendSect(self, self.zig_text_sect_index.?, self.zig_text_seg_index.?);
33033304 }
33043305
33053306 if (!self.base.isRelocatable()) {
3306 self.zig_got_section_index = try self.addSection("__GOT_ZIG", "__got_zig", .{
3307 self.zig_got_sect_index = try self.addSection("__GOT_ZIG", "__got_zig", .{
33073308 .alignment = 3,
33083309 });
3309 appendSect(self, self.zig_got_section_index.?, self.zig_got_seg_index.?);
3310 appendSect(self, self.zig_got_sect_index.?, self.zig_got_seg_index.?);
33103311 }
33113312
33123313 {
3313 self.zig_const_section_index = try self.addSection("__CONST_ZIG", "__const_zig", .{});
3314 appendSect(self, self.zig_const_section_index.?, self.zig_const_seg_index.?);
3314 self.zig_const_sect_index = try self.addSection("__CONST_ZIG", "__const_zig", .{});
3315 appendSect(self, self.zig_const_sect_index.?, self.zig_const_seg_index.?);
33153316 }
33163317
33173318 {
3318 self.zig_data_section_index = try self.addSection("__DATA_ZIG", "__data_zig", .{});
3319 appendSect(self, self.zig_data_section_index.?, self.zig_data_seg_index.?);
3319 self.zig_data_sect_index = try self.addSection("__DATA_ZIG", "__data_zig", .{});
3320 appendSect(self, self.zig_data_sect_index.?, self.zig_data_seg_index.?);
33203321 }
33213322
33223323 {
3323 self.zig_bss_section_index = try self.addSection("__BSS_ZIG", "__bss_zig", .{
3324 self.zig_bss_sect_index = try self.addSection("__BSS_ZIG", "__bss_zig", .{
33243325 .flags = macho.S_ZEROFILL,
33253326 });
3326 appendSect(self, self.zig_bss_section_index.?, self.zig_bss_seg_index.?);
3327 appendSect(self, self.zig_bss_sect_index.?, self.zig_bss_seg_index.?);
33273328 }
33283329}
33293330
src/link/MachO/ZigObject.zig+11-11
......@@ -416,7 +416,7 @@ pub fn lowerAnonDecl(
416416 name,
417417 tv,
418418 decl_alignment,
419 macho_file.zig_const_section_index.?,
419 macho_file.zig_const_sect_index.?,
420420 src_loc,
421421 ) catch |err| switch (err) {
422422 error.OutOfMemory => return error.OutOfMemory,
......@@ -712,7 +712,7 @@ fn getDeclOutputSection(
712712 const any_non_single_threaded = macho_file.base.comp.config.any_non_single_threaded;
713713 _ = any_non_single_threaded;
714714 const sect_id: u8 = switch (decl.ty.zigTypeTag(mod)) {
715 .Fn => macho_file.zig_text_section_index.?,
715 .Fn => macho_file.zig_text_sect_index.?,
716716 else => blk: {
717717 if (decl.getOwnedVariable(mod)) |variable| {
718718 // if (variable.is_threadlocal and any_non_single_threaded) {
......@@ -731,13 +731,13 @@ fn getDeclOutputSection(
731731 // );
732732 // }
733733
734 if (variable.is_const) break :blk macho_file.zig_const_section_index.?;
734 if (variable.is_const) break :blk macho_file.zig_const_sect_index.?;
735735 if (Value.fromInterned(variable.init).isUndefDeep(mod)) {
736736 // TODO: get the optimize_mode from the Module that owns the decl instead
737737 // of using the root module here.
738738 break :blk switch (macho_file.base.comp.root_mod.optimize_mode) {
739 .Debug, .ReleaseSafe => macho_file.zig_data_section_index.?,
740 .ReleaseFast, .ReleaseSmall => macho_file.zig_bss_section_index.?,
739 .Debug, .ReleaseSafe => macho_file.zig_data_sect_index.?,
740 .ReleaseFast, .ReleaseSmall => macho_file.zig_bss_sect_index.?,
741741 };
742742 }
743743
......@@ -746,10 +746,10 @@ fn getDeclOutputSection(
746746 const is_all_zeroes = for (code) |byte| {
747747 if (byte != 0) break false;
748748 } else true;
749 if (is_all_zeroes) break :blk macho_file.zig_bss_section_index.?;
750 break :blk macho_file.zig_data_section_index.?;
749 if (is_all_zeroes) break :blk macho_file.zig_bss_sect_index.?;
750 break :blk macho_file.zig_data_sect_index.?;
751751 }
752 break :blk macho_file.zig_const_section_index.?;
752 break :blk macho_file.zig_const_sect_index.?;
753753 },
754754 };
755755 return sect_id;
......@@ -778,7 +778,7 @@ pub fn lowerUnnamedConst(
778778 name,
779779 typed_value,
780780 typed_value.ty.abiAlignment(mod),
781 macho_file.zig_const_section_index.?,
781 macho_file.zig_const_sect_index.?,
782782 decl.srcLoc(mod),
783783 )) {
784784 .ok => |sym_index| sym_index,
......@@ -995,8 +995,8 @@ fn updateLazySymbol(
995995 };
996996
997997 const output_section_index = switch (lazy_sym.kind) {
998 .code => macho_file.zig_text_section_index.?,
999 .const_data => macho_file.zig_const_section_index.?,
998 .code => macho_file.zig_text_sect_index.?,
999 .const_data => macho_file.zig_const_sect_index.?,
10001000 };
10011001 const sym = macho_file.getSymbol(symbol_index);
10021002 sym.name = name_str_index;
src/link/MachO/synthetic.zig+4-4
......@@ -32,13 +32,13 @@ pub const ZigGotSection = struct {
3232
3333 pub fn entryOffset(zig_got: ZigGotSection, index: Index, macho_file: *MachO) u64 {
3434 _ = zig_got;
35 const sect = macho_file.sections.items(.header)[macho_file.zig_got_section_index.?];
35 const sect = macho_file.sections.items(.header)[macho_file.zig_got_sect_index.?];
3636 return sect.offset + @sizeOf(u64) * index;
3737 }
3838
3939 pub fn entryAddress(zig_got: ZigGotSection, index: Index, macho_file: *MachO) u64 {
4040 _ = zig_got;
41 const sect = macho_file.sections.items(.header)[macho_file.zig_got_section_index.?];
41 const sect = macho_file.sections.items(.header)[macho_file.zig_got_sect_index.?];
4242 return sect.addr + @sizeOf(u64) * index;
4343 }
4444
......@@ -50,7 +50,7 @@ pub const ZigGotSection = struct {
5050 pub fn writeOne(zig_got: *ZigGotSection, macho_file: *MachO, index: Index) !void {
5151 if (zig_got.dirty) {
5252 const needed_size = zig_got.size(macho_file);
53 try macho_file.growSection(macho_file.zig_got_section_index.?, needed_size);
53 try macho_file.growSection(macho_file.zig_got_sect_index.?, needed_size);
5454 zig_got.dirty = false;
5555 }
5656 const off = zig_got.entryOffset(index, macho_file);
......@@ -77,7 +77,7 @@ pub const ZigGotSection = struct {
7777 const seg_id = macho_file.sections.items(.segment_id)[macho_file.zig_got_sect_index.?];
7878 const seg = macho_file.segments.items[seg_id];
7979
80 for (0..zig_got.symbols.items.len) |idx| {
80 for (0..zig_got.entries.items.len) |idx| {
8181 const addr = zig_got.entryAddress(@intCast(idx), macho_file);
8282 try macho_file.rebase.entries.append(gpa, .{
8383 .offset = addr - seg.vmaddr,