From 7647db327328bffea7dc8d7286cb6be9265080be Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Fri, 19 Jan 2024 18:19:36 +0100 Subject: [PATCH] macho: reserve space for __got_zig rebase opcodes --- src/link/MachO.zig | 31 ++++++++++++++++--------------- src/link/MachO/ZigObject.zig | 22 +++++++++++----------- src/link/MachO/synthetic.zig | 8 ++++---- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 385a84832..f4ba60a9d 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -93,11 +93,11 @@ zig_data_seg_index: ?u8 = null, zig_bss_seg_index: ?u8 = null, /// Tracked section headers with incremental updates to Zig object. -zig_text_section_index: ?u8 = null, -zig_got_section_index: ?u8 = null, -zig_const_section_index: ?u8 = null, -zig_data_section_index: ?u8 = null, -zig_bss_section_index: ?u8 = null, +zig_text_sect_index: ?u8 = null, +zig_got_sect_index: ?u8 = null, +zig_const_sect_index: ?u8 = null, +zig_data_sect_index: ?u8 = null, +zig_bss_sect_index: ?u8 = null, has_tlv: bool = false, binds_to_weak: bool = false, @@ -2423,6 +2423,7 @@ fn initDyldInfoSections(self: *MachO) !void { const gpa = self.base.comp.gpa; + if (self.zig_got_sect_index != null) try self.zig_got.addDyldRelocs(self); if (self.got_sect_index != null) try self.got.addDyldRelocs(self); if (self.tlv_ptr_sect_index != null) try self.tlv_ptr.addDyldRelocs(self); 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 { }.appendSect; { - self.zig_text_section_index = try self.addSection("__TEXT_ZIG", "__text_zig", .{ + self.zig_text_sect_index = try self.addSection("__TEXT_ZIG", "__text_zig", .{ .alignment = switch (self.getTarget().cpu.arch) { .aarch64 => 2, .x86_64 => 0, @@ -3299,31 +3300,31 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void { }, .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, }); - appendSect(self, self.zig_text_section_index.?, self.zig_text_seg_index.?); + appendSect(self, self.zig_text_sect_index.?, self.zig_text_seg_index.?); } if (!self.base.isRelocatable()) { - self.zig_got_section_index = try self.addSection("__GOT_ZIG", "__got_zig", .{ + self.zig_got_sect_index = try self.addSection("__GOT_ZIG", "__got_zig", .{ .alignment = 3, }); - appendSect(self, self.zig_got_section_index.?, self.zig_got_seg_index.?); + appendSect(self, self.zig_got_sect_index.?, self.zig_got_seg_index.?); } { - self.zig_const_section_index = try self.addSection("__CONST_ZIG", "__const_zig", .{}); - appendSect(self, self.zig_const_section_index.?, self.zig_const_seg_index.?); + self.zig_const_sect_index = try self.addSection("__CONST_ZIG", "__const_zig", .{}); + appendSect(self, self.zig_const_sect_index.?, self.zig_const_seg_index.?); } { - self.zig_data_section_index = try self.addSection("__DATA_ZIG", "__data_zig", .{}); - appendSect(self, self.zig_data_section_index.?, self.zig_data_seg_index.?); + self.zig_data_sect_index = try self.addSection("__DATA_ZIG", "__data_zig", .{}); + appendSect(self, self.zig_data_sect_index.?, self.zig_data_seg_index.?); } { - self.zig_bss_section_index = try self.addSection("__BSS_ZIG", "__bss_zig", .{ + self.zig_bss_sect_index = try self.addSection("__BSS_ZIG", "__bss_zig", .{ .flags = macho.S_ZEROFILL, }); - appendSect(self, self.zig_bss_section_index.?, self.zig_bss_seg_index.?); + appendSect(self, self.zig_bss_sect_index.?, self.zig_bss_seg_index.?); } } diff --git a/src/link/MachO/ZigObject.zig b/src/link/MachO/ZigObject.zig index f2e80f69c..f57a11d3a 100644 --- a/src/link/MachO/ZigObject.zig +++ b/src/link/MachO/ZigObject.zig @@ -416,7 +416,7 @@ pub fn lowerAnonDecl( name, tv, decl_alignment, - macho_file.zig_const_section_index.?, + macho_file.zig_const_sect_index.?, src_loc, ) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, @@ -712,7 +712,7 @@ fn getDeclOutputSection( const any_non_single_threaded = macho_file.base.comp.config.any_non_single_threaded; _ = any_non_single_threaded; const sect_id: u8 = switch (decl.ty.zigTypeTag(mod)) { - .Fn => macho_file.zig_text_section_index.?, + .Fn => macho_file.zig_text_sect_index.?, else => blk: { if (decl.getOwnedVariable(mod)) |variable| { // if (variable.is_threadlocal and any_non_single_threaded) { @@ -731,13 +731,13 @@ fn getDeclOutputSection( // ); // } - if (variable.is_const) break :blk macho_file.zig_const_section_index.?; + if (variable.is_const) break :blk macho_file.zig_const_sect_index.?; if (Value.fromInterned(variable.init).isUndefDeep(mod)) { // TODO: get the optimize_mode from the Module that owns the decl instead // of using the root module here. break :blk switch (macho_file.base.comp.root_mod.optimize_mode) { - .Debug, .ReleaseSafe => macho_file.zig_data_section_index.?, - .ReleaseFast, .ReleaseSmall => macho_file.zig_bss_section_index.?, + .Debug, .ReleaseSafe => macho_file.zig_data_sect_index.?, + .ReleaseFast, .ReleaseSmall => macho_file.zig_bss_sect_index.?, }; } @@ -746,10 +746,10 @@ fn getDeclOutputSection( const is_all_zeroes = for (code) |byte| { if (byte != 0) break false; } else true; - if (is_all_zeroes) break :blk macho_file.zig_bss_section_index.?; - break :blk macho_file.zig_data_section_index.?; + if (is_all_zeroes) break :blk macho_file.zig_bss_sect_index.?; + break :blk macho_file.zig_data_sect_index.?; } - break :blk macho_file.zig_const_section_index.?; + break :blk macho_file.zig_const_sect_index.?; }, }; return sect_id; @@ -778,7 +778,7 @@ pub fn lowerUnnamedConst( name, typed_value, typed_value.ty.abiAlignment(mod), - macho_file.zig_const_section_index.?, + macho_file.zig_const_sect_index.?, decl.srcLoc(mod), )) { .ok => |sym_index| sym_index, @@ -995,8 +995,8 @@ fn updateLazySymbol( }; const output_section_index = switch (lazy_sym.kind) { - .code => macho_file.zig_text_section_index.?, - .const_data => macho_file.zig_const_section_index.?, + .code => macho_file.zig_text_sect_index.?, + .const_data => macho_file.zig_const_sect_index.?, }; const sym = macho_file.getSymbol(symbol_index); sym.name = name_str_index; diff --git a/src/link/MachO/synthetic.zig b/src/link/MachO/synthetic.zig index b749d7dae..882db5d41 100644 --- a/src/link/MachO/synthetic.zig +++ b/src/link/MachO/synthetic.zig @@ -32,13 +32,13 @@ pub const ZigGotSection = struct { pub fn entryOffset(zig_got: ZigGotSection, index: Index, macho_file: *MachO) u64 { _ = zig_got; - const sect = macho_file.sections.items(.header)[macho_file.zig_got_section_index.?]; + const sect = macho_file.sections.items(.header)[macho_file.zig_got_sect_index.?]; return sect.offset + @sizeOf(u64) * index; } pub fn entryAddress(zig_got: ZigGotSection, index: Index, macho_file: *MachO) u64 { _ = zig_got; - const sect = macho_file.sections.items(.header)[macho_file.zig_got_section_index.?]; + const sect = macho_file.sections.items(.header)[macho_file.zig_got_sect_index.?]; return sect.addr + @sizeOf(u64) * index; } @@ -50,7 +50,7 @@ pub const ZigGotSection = struct { pub fn writeOne(zig_got: *ZigGotSection, macho_file: *MachO, index: Index) !void { if (zig_got.dirty) { const needed_size = zig_got.size(macho_file); - try macho_file.growSection(macho_file.zig_got_section_index.?, needed_size); + try macho_file.growSection(macho_file.zig_got_sect_index.?, needed_size); zig_got.dirty = false; } const off = zig_got.entryOffset(index, macho_file); @@ -77,7 +77,7 @@ pub const ZigGotSection = struct { const seg_id = macho_file.sections.items(.segment_id)[macho_file.zig_got_sect_index.?]; const seg = macho_file.segments.items[seg_id]; - for (0..zig_got.symbols.items.len) |idx| { + for (0..zig_got.entries.items.len) |idx| { const addr = zig_got.entryAddress(@intCast(idx), macho_file); try macho_file.rebase.entries.append(gpa, .{ .offset = addr - seg.vmaddr,