From 9d62ebc0ce2c12e991f3016e71a8569e262c26c6 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Sun, 27 Aug 2023 11:07:07 +0200 Subject: [PATCH] macho: fix compilation issues --- src/link/MachO.zig | 29 ++++++++++++++--------------- src/link/MachO/dead_strip.zig | 13 +++++++------ src/link/MachO/zld.zig | 25 ++++++++++++++----------- 3 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/link/MachO.zig b/src/link/MachO.zig index ac7b4af988..d662266158 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -1455,7 +1455,7 @@ pub fn createTentativeDefAtoms(self: *MachO) !void { } } -fn createDyldPrivateAtom(self: *MachO) !void { +pub fn createDyldPrivateAtom(self: *MachO) !void { if (self.dyld_private_atom_index != null) return; const sym_index = try self.allocateSymbol(); @@ -2015,7 +2015,7 @@ fn growAtom(self: *MachO, atom_index: Atom.Index, new_atom_size: u64, alignment: return self.allocateAtom(atom_index, new_atom_size, alignment); } -fn allocateSymbol(self: *MachO) !u32 { +pub fn allocateSymbol(self: *MachO) !u32 { try self.locals.ensureUnusedCapacity(self.base.allocator, 1); const index = blk: { @@ -2104,7 +2104,7 @@ pub fn addStubEntry(self: *MachO, target: SymbolWithLoc) !void { pub fn addTlvPtrEntry(self: *MachO, target: SymbolWithLoc) !void { if (self.tlv_ptr_table.lookup.contains(target)) return; - _ = try self.tlv_ptr_table.allocateEntry(self.gpa, target); + _ = try self.tlv_ptr_table.allocateEntry(self.base.allocator, target); if (self.tlv_ptr_section_index == null) { self.tlv_ptr_section_index = try self.initSection("__DATA", "__thread_ptrs", .{ .flags = macho.S_THREAD_LOCAL_VARIABLE_POINTERS, @@ -3490,7 +3490,7 @@ fn collectRebaseData(self: *MachO, rebase: *Rebase) !void { const offset = @as(u64, @intCast(base_offset + rel_offset)); log.debug(" | rebase at {x}", .{offset}); - try rebase.entries.append(self.gpa, .{ + try rebase.entries.append(gpa, .{ .offset = offset, .segment_id = segment_id, }); @@ -3656,7 +3656,7 @@ fn collectBindData(self: *MachO, bind: anytype, raw_bindings: anytype) !void { if (bind_sym.weakRef()) { log.debug(" | marking as weak ref ", .{}); } - try bind.entries.append(self.gpa, .{ + try bind.entries.append(gpa, .{ .target = global, .offset = offset, .segment_id = segment_id, @@ -4004,8 +4004,8 @@ fn writeSymtab(self: *MachO) !SymtabCtx { var locals = std.ArrayList(macho.nlist_64).init(gpa); defer locals.deinit(); - for (0..self.locals.items) |sym_id| { - try self.addLocalToSymtab(.{ .sym_index = @intCast(sym_id) }); + for (0..self.locals.items.len) |sym_id| { + try self.addLocalToSymtab(.{ .sym_index = @intCast(sym_id) }, &locals); } for (self.objects.items) |object| { @@ -4611,7 +4611,7 @@ pub fn makeStaticString(bytes: []const u8) [16]u8 { return buf; } -fn getSegmentByName(self: MachO, segname: []const u8) ?u8 { +pub fn getSegmentByName(self: MachO, segname: []const u8) ?u8 { for (self.segments.items, 0..) |seg, i| { if (mem.eql(u8, segname, seg.segName())) return @as(u8, @intCast(i)); } else return null; @@ -5024,7 +5024,7 @@ pub fn logSymtab(self: *MachO) void { scoped_log.debug("{}", .{self.tlv_ptr_table}); scoped_log.debug("stubs entries:", .{}); - scoped_log.debug("{}", .{self.stubs_table}); + scoped_log.debug("{}", .{self.stub_table}); scoped_log.debug("thunks:", .{}); for (self.thunks.items, 0..) |thunk, i| { @@ -5151,7 +5151,7 @@ const Cache = std.Build.Cache; const CodeSignature = @import("MachO/CodeSignature.zig"); const Compilation = @import("../Compilation.zig"); const Dwarf = File.Dwarf; -const DwarfInfo = @import("DwarfInfo.zig"); +const DwarfInfo = @import("MachO/DwarfInfo.zig"); const Dylib = @import("MachO/Dylib.zig"); const File = link.File; const Object = @import("MachO/Object.zig"); @@ -5170,10 +5170,9 @@ const TypedValue = @import("../TypedValue.zig"); const Value = @import("../value.zig").Value; pub const DebugSymbols = @import("MachO/DebugSymbols.zig"); - -const Bind = @import("MachO/dyld_info/bind.zig").Bind(*const MachO, SymbolWithLoc); -const LazyBind = @import("MachO/dyld_info/bind.zig").LazyBind(*const MachO, SymbolWithLoc); -const Rebase = @import("MachO/dyld_info/Rebase.zig"); +pub const Bind = @import("MachO/dyld_info/bind.zig").Bind(*const MachO, SymbolWithLoc); +pub const LazyBind = @import("MachO/dyld_info/bind.zig").LazyBind(*const MachO, SymbolWithLoc); +pub const Rebase = @import("MachO/dyld_info/Rebase.zig"); pub const base_tag: File.Tag = File.Tag.macho; pub const N_DEAD: u16 = @as(u16, @bitCast(@as(i16, -1))); @@ -5257,7 +5256,7 @@ const UnnamedConstTable = std.AutoArrayHashMapUnmanaged(Module.Decl.Index, std.A const RebaseTable = std.AutoArrayHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(u32)); const RelocationTable = std.AutoArrayHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Relocation)); -const ResolveAction = struct { +pub const ResolveAction = struct { kind: Kind, target: SymbolWithLoc, diff --git a/src/link/MachO/dead_strip.zig b/src/link/MachO/dead_strip.zig index 5e99ad2270..26053cb83d 100644 --- a/src/link/MachO/dead_strip.zig +++ b/src/link/MachO/dead_strip.zig @@ -36,11 +36,12 @@ fn collectRoots(macho_file: *MachO, roots: *AtomTable) !void { switch (macho_file.base.options.output_mode) { .Exe => { // Add entrypoint as GC root - const global: SymbolWithLoc = macho_file.getEntryPoint(); - if (global.getFile()) |file| { - try addRoot(macho_file, roots, file, global); - } else { - assert(macho_file.getSymbol(global).undf()); // Stub as our entrypoint is in a dylib. + if (macho_file.getEntryPoint()) |global| { + if (global.getFile()) |file| { + try addRoot(macho_file, roots, file, global); + } else { + assert(macho_file.getSymbol(global).undf()); // Stub as our entrypoint is in a dylib. + } } }, else => |other| { @@ -116,7 +117,7 @@ fn markLive(macho_file: *MachO, atom_index: Atom.Index, alive: *AtomTable) void alive.putAssumeCapacityNoClobber(atom_index, {}); - const cpu_arch = macho_file.options.target.cpu.arch; + const cpu_arch = macho_file.base.options.target.cpu.arch; const sym = macho_file.getSymbol(atom.getSymbolWithLoc()); const header = macho_file.sections.items(.header)[sym.n_sect - 1]; diff --git a/src/link/MachO/zld.zig b/src/link/MachO/zld.zig index 86bf14bdb2..a28df3c4e7 100644 --- a/src/link/MachO/zld.zig +++ b/src/link/MachO/zld.zig @@ -401,7 +401,7 @@ pub fn linkWithZld( try macho_file.createDyldPrivateAtom(); try macho_file.createTentativeDefAtoms(); - if (macho_file.options.output_mode == .Exe) { + if (macho_file.base.options.output_mode == .Exe) { const global = macho_file.getEntryPoint().?; if (macho_file.getSymbol(global).undf()) { // We do one additional check here in case the entry point was found in one of the dylibs. @@ -429,7 +429,7 @@ pub fn linkWithZld( if (macho_file.dyld_stub_binder_index) |index| try macho_file.addGotEntry(macho_file.globals.items[index]); - try macho_file.calcSectionSizes(); + try calcSectionSizes(macho_file); var unwind_info = UnwindInfo{ .gpa = gpa }; defer unwind_info.deinit(); @@ -461,9 +461,9 @@ pub fn linkWithZld( try writeLaSymbolPtrs(macho_file); } if (macho_file.got_section_index) |sect_id| - try macho_file.writePointerEntries(sect_id, &macho_file.got_table); + try writePointerEntries(macho_file, sect_id, &macho_file.got_table); if (macho_file.tlv_ptr_section_index) |sect_id| - try macho_file.writePointerEntries(sect_id, &macho_file.tlv_ptr_table); + try writePointerEntries(macho_file, sect_id, &macho_file.tlv_ptr_table); try eh_frame.write(macho_file, &unwind_info); try unwind_info.write(macho_file); @@ -546,11 +546,11 @@ pub fn linkWithZld( else => {}, } - try load_commands.writeRpathLCs(gpa, macho_file.base.options, lc_writer); + try load_commands.writeRpathLCs(gpa, &macho_file.base.options, lc_writer); try lc_writer.writeStruct(macho.source_version_command{ .version = 0, }); - try load_commands.writeBuildVersionLC(macho_file.base.options, lc_writer); + try load_commands.writeBuildVersionLC(&macho_file.base.options, lc_writer); const uuid_cmd_offset = @sizeOf(macho.mach_header_64) + @as(u32, @intCast(lc_buffer.items.len)); try lc_writer.writeStruct(macho_file.uuid_cmd); @@ -1053,11 +1053,14 @@ fn allocateSegments(macho_file: *MachO) !void { const gpa = macho_file.base.allocator; for (macho_file.segments.items, 0..) |*segment, segment_index| { const is_text_segment = mem.eql(u8, segment.segName(), "__TEXT"); - const base_size = if (is_text_segment) try load_commands.calcMinHeaderPad(gpa, macho_file.base.options, .{ - .segments = macho_file.segments.items, - .dylibs = macho_file.dylibs.items, - .referenced_dylibs = macho_file.referenced_dylibs.keys(), - }) else 0; + const base_size = if (is_text_segment) + try load_commands.calcMinHeaderPad(gpa, &macho_file.base.options, .{ + .segments = macho_file.segments.items, + .dylibs = macho_file.dylibs.items, + .referenced_dylibs = macho_file.referenced_dylibs.keys(), + }) + else + 0; try allocateSegment(macho_file, @as(u8, @intCast(segment_index)), base_size); } }