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,...@@ -93,11 +93,11 @@ zig_data_seg_index: ?u8 = null,
93zig_bss_seg_index: ?u8 = null,93zig_bss_seg_index: ?u8 = null,
9494
95/// Tracked section headers with incremental updates to Zig object.95/// Tracked section headers with incremental updates to Zig object.
96zig_text_section_index: ?u8 = null,96zig_text_sect_index: ?u8 = null,
97zig_got_section_index: ?u8 = null,97zig_got_sect_index: ?u8 = null,
98zig_const_section_index: ?u8 = null,98zig_const_sect_index: ?u8 = null,
99zig_data_section_index: ?u8 = null,99zig_data_sect_index: ?u8 = null,
100zig_bss_section_index: ?u8 = null,100zig_bss_sect_index: ?u8 = null,
101101
102has_tlv: bool = false,102has_tlv: bool = false,
103binds_to_weak: bool = false,103binds_to_weak: bool = false,
...@@ -2423,6 +2423,7 @@ fn initDyldInfoSections(self: *MachO) !void {...@@ -2423,6 +2423,7 @@ fn initDyldInfoSections(self: *MachO) !void {
24232423
2424 const gpa = self.base.comp.gpa;2424 const gpa = self.base.comp.gpa;
24252425
2426 if (self.zig_got_sect_index != null) try self.zig_got.addDyldRelocs(self);
2426 if (self.got_sect_index != null) try self.got.addDyldRelocs(self);2427 if (self.got_sect_index != null) try self.got.addDyldRelocs(self);
2427 if (self.tlv_ptr_sect_index != null) try self.tlv_ptr.addDyldRelocs(self);2428 if (self.tlv_ptr_sect_index != null) try self.tlv_ptr.addDyldRelocs(self);
2428 if (self.la_symbol_ptr_sect_index != null) try self.la_symbol_ptr.addDyldRelocs(self);2429 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 {...@@ -3291,7 +3292,7 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {
3291 }.appendSect;3292 }.appendSect;
32923293
3293 {3294 {
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", .{
3295 .alignment = switch (self.getTarget().cpu.arch) {3296 .alignment = switch (self.getTarget().cpu.arch) {
3296 .aarch64 => 2,3297 .aarch64 => 2,
3297 .x86_64 => 0,3298 .x86_64 => 0,
...@@ -3299,31 +3300,31 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {...@@ -3299,31 +3300,31 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {
3299 },3300 },
3300 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,3301 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
3301 });3302 });
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.?);
3303 }3304 }
33043305
3305 if (!self.base.isRelocatable()) {3306 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", .{
3307 .alignment = 3,3308 .alignment = 3,
3308 });3309 });
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.?);
3310 }3311 }
33113312
3312 {3313 {
3313 self.zig_const_section_index = try self.addSection("__CONST_ZIG", "__const_zig", .{});3314 self.zig_const_sect_index = try self.addSection("__CONST_ZIG", "__const_zig", .{});
3314 appendSect(self, self.zig_const_section_index.?, self.zig_const_seg_index.?);3315 appendSect(self, self.zig_const_sect_index.?, self.zig_const_seg_index.?);
3315 }3316 }
33163317
3317 {3318 {
3318 self.zig_data_section_index = try self.addSection("__DATA_ZIG", "__data_zig", .{});3319 self.zig_data_sect_index = try self.addSection("__DATA_ZIG", "__data_zig", .{});
3319 appendSect(self, self.zig_data_section_index.?, self.zig_data_seg_index.?);3320 appendSect(self, self.zig_data_sect_index.?, self.zig_data_seg_index.?);
3320 }3321 }
33213322
3322 {3323 {
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", .{
3324 .flags = macho.S_ZEROFILL,3325 .flags = macho.S_ZEROFILL,
3325 });3326 });
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.?);
3327 }3328 }
3328}3329}
33293330
src/link/MachO/ZigObject.zig+11-11
...@@ -416,7 +416,7 @@ pub fn lowerAnonDecl(...@@ -416,7 +416,7 @@ pub fn lowerAnonDecl(
416 name,416 name,
417 tv,417 tv,
418 decl_alignment,418 decl_alignment,
419 macho_file.zig_const_section_index.?,419 macho_file.zig_const_sect_index.?,
420 src_loc,420 src_loc,
421 ) catch |err| switch (err) {421 ) catch |err| switch (err) {
422 error.OutOfMemory => return error.OutOfMemory,422 error.OutOfMemory => return error.OutOfMemory,
...@@ -712,7 +712,7 @@ fn getDeclOutputSection(...@@ -712,7 +712,7 @@ fn getDeclOutputSection(
712 const any_non_single_threaded = macho_file.base.comp.config.any_non_single_threaded;712 const any_non_single_threaded = macho_file.base.comp.config.any_non_single_threaded;
713 _ = any_non_single_threaded;713 _ = any_non_single_threaded;
714 const sect_id: u8 = switch (decl.ty.zigTypeTag(mod)) {714 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.?,
716 else => blk: {716 else => blk: {
717 if (decl.getOwnedVariable(mod)) |variable| {717 if (decl.getOwnedVariable(mod)) |variable| {
718 // if (variable.is_threadlocal and any_non_single_threaded) {718 // if (variable.is_threadlocal and any_non_single_threaded) {
...@@ -731,13 +731,13 @@ fn getDeclOutputSection(...@@ -731,13 +731,13 @@ fn getDeclOutputSection(
731 // );731 // );
732 // }732 // }
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.?;
735 if (Value.fromInterned(variable.init).isUndefDeep(mod)) {735 if (Value.fromInterned(variable.init).isUndefDeep(mod)) {
736 // TODO: get the optimize_mode from the Module that owns the decl instead736 // TODO: get the optimize_mode from the Module that owns the decl instead
737 // of using the root module here.737 // of using the root module here.
738 break :blk switch (macho_file.base.comp.root_mod.optimize_mode) {738 break :blk switch (macho_file.base.comp.root_mod.optimize_mode) {
739 .Debug, .ReleaseSafe => macho_file.zig_data_section_index.?,739 .Debug, .ReleaseSafe => macho_file.zig_data_sect_index.?,
740 .ReleaseFast, .ReleaseSmall => macho_file.zig_bss_section_index.?,740 .ReleaseFast, .ReleaseSmall => macho_file.zig_bss_sect_index.?,
741 };741 };
742 }742 }
743743
...@@ -746,10 +746,10 @@ fn getDeclOutputSection(...@@ -746,10 +746,10 @@ fn getDeclOutputSection(
746 const is_all_zeroes = for (code) |byte| {746 const is_all_zeroes = for (code) |byte| {
747 if (byte != 0) break false;747 if (byte != 0) break false;
748 } else true;748 } else true;
749 if (is_all_zeroes) break :blk macho_file.zig_bss_section_index.?;749 if (is_all_zeroes) break :blk macho_file.zig_bss_sect_index.?;
750 break :blk macho_file.zig_data_section_index.?;750 break :blk macho_file.zig_data_sect_index.?;
751 }751 }
752 break :blk macho_file.zig_const_section_index.?;752 break :blk macho_file.zig_const_sect_index.?;
753 },753 },
754 };754 };
755 return sect_id;755 return sect_id;
...@@ -778,7 +778,7 @@ pub fn lowerUnnamedConst(...@@ -778,7 +778,7 @@ pub fn lowerUnnamedConst(
778 name,778 name,
779 typed_value,779 typed_value,
780 typed_value.ty.abiAlignment(mod),780 typed_value.ty.abiAlignment(mod),
781 macho_file.zig_const_section_index.?,781 macho_file.zig_const_sect_index.?,
782 decl.srcLoc(mod),782 decl.srcLoc(mod),
783 )) {783 )) {
784 .ok => |sym_index| sym_index,784 .ok => |sym_index| sym_index,
...@@ -995,8 +995,8 @@ fn updateLazySymbol(...@@ -995,8 +995,8 @@ fn updateLazySymbol(
995 };995 };
996996
997 const output_section_index = switch (lazy_sym.kind) {997 const output_section_index = switch (lazy_sym.kind) {
998 .code => macho_file.zig_text_section_index.?,998 .code => macho_file.zig_text_sect_index.?,
999 .const_data => macho_file.zig_const_section_index.?,999 .const_data => macho_file.zig_const_sect_index.?,
1000 };1000 };
1001 const sym = macho_file.getSymbol(symbol_index);1001 const sym = macho_file.getSymbol(symbol_index);
1002 sym.name = name_str_index;1002 sym.name = name_str_index;
src/link/MachO/synthetic.zig+4-4
...@@ -32,13 +32,13 @@ pub const ZigGotSection = struct {...@@ -32,13 +32,13 @@ pub const ZigGotSection = struct {
3232
33 pub fn entryOffset(zig_got: ZigGotSection, index: Index, macho_file: *MachO) u64 {33 pub fn entryOffset(zig_got: ZigGotSection, index: Index, macho_file: *MachO) u64 {
34 _ = zig_got;34 _ = 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.?];
36 return sect.offset + @sizeOf(u64) * index;36 return sect.offset + @sizeOf(u64) * index;
37 }37 }
3838
39 pub fn entryAddress(zig_got: ZigGotSection, index: Index, macho_file: *MachO) u64 {39 pub fn entryAddress(zig_got: ZigGotSection, index: Index, macho_file: *MachO) u64 {
40 _ = zig_got;40 _ = 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.?];
42 return sect.addr + @sizeOf(u64) * index;42 return sect.addr + @sizeOf(u64) * index;
43 }43 }
4444
...@@ -50,7 +50,7 @@ pub const ZigGotSection = struct {...@@ -50,7 +50,7 @@ pub const ZigGotSection = struct {
50 pub fn writeOne(zig_got: *ZigGotSection, macho_file: *MachO, index: Index) !void {50 pub fn writeOne(zig_got: *ZigGotSection, macho_file: *MachO, index: Index) !void {
51 if (zig_got.dirty) {51 if (zig_got.dirty) {
52 const needed_size = zig_got.size(macho_file);52 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);
54 zig_got.dirty = false;54 zig_got.dirty = false;
55 }55 }
56 const off = zig_got.entryOffset(index, macho_file);56 const off = zig_got.entryOffset(index, macho_file);
...@@ -77,7 +77,7 @@ pub const ZigGotSection = struct {...@@ -77,7 +77,7 @@ pub const ZigGotSection = struct {
77 const seg_id = macho_file.sections.items(.segment_id)[macho_file.zig_got_sect_index.?];77 const seg_id = macho_file.sections.items(.segment_id)[macho_file.zig_got_sect_index.?];
78 const seg = macho_file.segments.items[seg_id];78 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| {
81 const addr = zig_got.entryAddress(@intCast(idx), macho_file);81 const addr = zig_got.entryAddress(@intCast(idx), macho_file);
82 try macho_file.rebase.entries.append(gpa, .{82 try macho_file.rebase.entries.append(gpa, .{
83 .offset = addr - seg.vmaddr,83 .offset = addr - seg.vmaddr,