authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-08-27 11:07:07+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-08-29 11:40:20+02:00
log9d62ebc0ce2c12e991f3016e71a8569e262c26c6
tree6df3de9755a95d062f84107474101c5d56d80e4c
parent2c68fb3d7ce077fba711747ee7b05b2fa0df6bcc

macho: fix compilation issues


3 files changed, 35 insertions(+), 32 deletions(-)

src/link/MachO.zig+14-15
...@@ -1455,7 +1455,7 @@ pub fn createTentativeDefAtoms(self: *MachO) !void {...@@ -1455,7 +1455,7 @@ pub fn createTentativeDefAtoms(self: *MachO) !void {
1455 }1455 }
1456}1456}
14571457
1458fn createDyldPrivateAtom(self: *MachO) !void {1458pub fn createDyldPrivateAtom(self: *MachO) !void {
1459 if (self.dyld_private_atom_index != null) return;1459 if (self.dyld_private_atom_index != null) return;
14601460
1461 const sym_index = try self.allocateSymbol();1461 const sym_index = try self.allocateSymbol();
...@@ -2015,7 +2015,7 @@ fn growAtom(self: *MachO, atom_index: Atom.Index, new_atom_size: u64, alignment:...@@ -2015,7 +2015,7 @@ fn growAtom(self: *MachO, atom_index: Atom.Index, new_atom_size: u64, alignment:
2015 return self.allocateAtom(atom_index, new_atom_size, alignment);2015 return self.allocateAtom(atom_index, new_atom_size, alignment);
2016}2016}
20172017
2018fn allocateSymbol(self: *MachO) !u32 {2018pub fn allocateSymbol(self: *MachO) !u32 {
2019 try self.locals.ensureUnusedCapacity(self.base.allocator, 1);2019 try self.locals.ensureUnusedCapacity(self.base.allocator, 1);
20202020
2021 const index = blk: {2021 const index = blk: {
...@@ -2104,7 +2104,7 @@ pub fn addStubEntry(self: *MachO, target: SymbolWithLoc) !void {...@@ -2104,7 +2104,7 @@ pub fn addStubEntry(self: *MachO, target: SymbolWithLoc) !void {
21042104
2105pub fn addTlvPtrEntry(self: *MachO, target: SymbolWithLoc) !void {2105pub fn addTlvPtrEntry(self: *MachO, target: SymbolWithLoc) !void {
2106 if (self.tlv_ptr_table.lookup.contains(target)) return;2106 if (self.tlv_ptr_table.lookup.contains(target)) return;
2107 _ = try self.tlv_ptr_table.allocateEntry(self.gpa, target);2107 _ = try self.tlv_ptr_table.allocateEntry(self.base.allocator, target);
2108 if (self.tlv_ptr_section_index == null) {2108 if (self.tlv_ptr_section_index == null) {
2109 self.tlv_ptr_section_index = try self.initSection("__DATA", "__thread_ptrs", .{2109 self.tlv_ptr_section_index = try self.initSection("__DATA", "__thread_ptrs", .{
2110 .flags = macho.S_THREAD_LOCAL_VARIABLE_POINTERS,2110 .flags = macho.S_THREAD_LOCAL_VARIABLE_POINTERS,
...@@ -3490,7 +3490,7 @@ fn collectRebaseData(self: *MachO, rebase: *Rebase) !void {...@@ -3490,7 +3490,7 @@ fn collectRebaseData(self: *MachO, rebase: *Rebase) !void {
3490 const offset = @as(u64, @intCast(base_offset + rel_offset));3490 const offset = @as(u64, @intCast(base_offset + rel_offset));
3491 log.debug(" | rebase at {x}", .{offset});3491 log.debug(" | rebase at {x}", .{offset});
34923492
3493 try rebase.entries.append(self.gpa, .{3493 try rebase.entries.append(gpa, .{
3494 .offset = offset,3494 .offset = offset,
3495 .segment_id = segment_id,3495 .segment_id = segment_id,
3496 });3496 });
...@@ -3656,7 +3656,7 @@ fn collectBindData(self: *MachO, bind: anytype, raw_bindings: anytype) !void {...@@ -3656,7 +3656,7 @@ fn collectBindData(self: *MachO, bind: anytype, raw_bindings: anytype) !void {
3656 if (bind_sym.weakRef()) {3656 if (bind_sym.weakRef()) {
3657 log.debug(" | marking as weak ref ", .{});3657 log.debug(" | marking as weak ref ", .{});
3658 }3658 }
3659 try bind.entries.append(self.gpa, .{3659 try bind.entries.append(gpa, .{
3660 .target = global,3660 .target = global,
3661 .offset = offset,3661 .offset = offset,
3662 .segment_id = segment_id,3662 .segment_id = segment_id,
...@@ -4004,8 +4004,8 @@ fn writeSymtab(self: *MachO) !SymtabCtx {...@@ -4004,8 +4004,8 @@ fn writeSymtab(self: *MachO) !SymtabCtx {
4004 var locals = std.ArrayList(macho.nlist_64).init(gpa);4004 var locals = std.ArrayList(macho.nlist_64).init(gpa);
4005 defer locals.deinit();4005 defer locals.deinit();
40064006
4007 for (0..self.locals.items) |sym_id| {4007 for (0..self.locals.items.len) |sym_id| {
4008 try self.addLocalToSymtab(.{ .sym_index = @intCast(sym_id) });4008 try self.addLocalToSymtab(.{ .sym_index = @intCast(sym_id) }, &locals);
4009 }4009 }
40104010
4011 for (self.objects.items) |object| {4011 for (self.objects.items) |object| {
...@@ -4611,7 +4611,7 @@ pub fn makeStaticString(bytes: []const u8) [16]u8 {...@@ -4611,7 +4611,7 @@ pub fn makeStaticString(bytes: []const u8) [16]u8 {
4611 return buf;4611 return buf;
4612}4612}
46134613
4614fn getSegmentByName(self: MachO, segname: []const u8) ?u8 {4614pub fn getSegmentByName(self: MachO, segname: []const u8) ?u8 {
4615 for (self.segments.items, 0..) |seg, i| {4615 for (self.segments.items, 0..) |seg, i| {
4616 if (mem.eql(u8, segname, seg.segName())) return @as(u8, @intCast(i));4616 if (mem.eql(u8, segname, seg.segName())) return @as(u8, @intCast(i));
4617 } else return null;4617 } else return null;
...@@ -5024,7 +5024,7 @@ pub fn logSymtab(self: *MachO) void {...@@ -5024,7 +5024,7 @@ pub fn logSymtab(self: *MachO) void {
5024 scoped_log.debug("{}", .{self.tlv_ptr_table});5024 scoped_log.debug("{}", .{self.tlv_ptr_table});
50255025
5026 scoped_log.debug("stubs entries:", .{});5026 scoped_log.debug("stubs entries:", .{});
5027 scoped_log.debug("{}", .{self.stubs_table});5027 scoped_log.debug("{}", .{self.stub_table});
50285028
5029 scoped_log.debug("thunks:", .{});5029 scoped_log.debug("thunks:", .{});
5030 for (self.thunks.items, 0..) |thunk, i| {5030 for (self.thunks.items, 0..) |thunk, i| {
...@@ -5151,7 +5151,7 @@ const Cache = std.Build.Cache;...@@ -5151,7 +5151,7 @@ const Cache = std.Build.Cache;
5151const CodeSignature = @import("MachO/CodeSignature.zig");5151const CodeSignature = @import("MachO/CodeSignature.zig");
5152const Compilation = @import("../Compilation.zig");5152const Compilation = @import("../Compilation.zig");
5153const Dwarf = File.Dwarf;5153const Dwarf = File.Dwarf;
5154const DwarfInfo = @import("DwarfInfo.zig");5154const DwarfInfo = @import("MachO/DwarfInfo.zig");
5155const Dylib = @import("MachO/Dylib.zig");5155const Dylib = @import("MachO/Dylib.zig");
5156const File = link.File;5156const File = link.File;
5157const Object = @import("MachO/Object.zig");5157const Object = @import("MachO/Object.zig");
...@@ -5170,10 +5170,9 @@ const TypedValue = @import("../TypedValue.zig");...@@ -5170,10 +5170,9 @@ const TypedValue = @import("../TypedValue.zig");
5170const Value = @import("../value.zig").Value;5170const Value = @import("../value.zig").Value;
51715171
5172pub const DebugSymbols = @import("MachO/DebugSymbols.zig");5172pub const DebugSymbols = @import("MachO/DebugSymbols.zig");
51735173pub const Bind = @import("MachO/dyld_info/bind.zig").Bind(*const MachO, SymbolWithLoc);
5174const Bind = @import("MachO/dyld_info/bind.zig").Bind(*const MachO, SymbolWithLoc);5174pub const LazyBind = @import("MachO/dyld_info/bind.zig").LazyBind(*const MachO, SymbolWithLoc);
5175const LazyBind = @import("MachO/dyld_info/bind.zig").LazyBind(*const MachO, SymbolWithLoc);5175pub const Rebase = @import("MachO/dyld_info/Rebase.zig");
5176const Rebase = @import("MachO/dyld_info/Rebase.zig");
51775176
5178pub const base_tag: File.Tag = File.Tag.macho;5177pub const base_tag: File.Tag = File.Tag.macho;
5179pub const N_DEAD: u16 = @as(u16, @bitCast(@as(i16, -1)));5178pub const N_DEAD: u16 = @as(u16, @bitCast(@as(i16, -1)));
...@@ -5257,7 +5256,7 @@ const UnnamedConstTable = std.AutoArrayHashMapUnmanaged(Module.Decl.Index, std.A...@@ -5257,7 +5256,7 @@ const UnnamedConstTable = std.AutoArrayHashMapUnmanaged(Module.Decl.Index, std.A
5257const RebaseTable = std.AutoArrayHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(u32));5256const RebaseTable = std.AutoArrayHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(u32));
5258const RelocationTable = std.AutoArrayHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Relocation));5257const RelocationTable = std.AutoArrayHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Relocation));
52595258
5260const ResolveAction = struct {5259pub const ResolveAction = struct {
5261 kind: Kind,5260 kind: Kind,
5262 target: SymbolWithLoc,5261 target: SymbolWithLoc,
52635262
src/link/MachO/dead_strip.zig+7-6
...@@ -36,11 +36,12 @@ fn collectRoots(macho_file: *MachO, roots: *AtomTable) !void {...@@ -36,11 +36,12 @@ fn collectRoots(macho_file: *MachO, roots: *AtomTable) !void {
36 switch (macho_file.base.options.output_mode) {36 switch (macho_file.base.options.output_mode) {
37 .Exe => {37 .Exe => {
38 // Add entrypoint as GC root38 // Add entrypoint as GC root
39 const global: SymbolWithLoc = macho_file.getEntryPoint();39 if (macho_file.getEntryPoint()) |global| {
40 if (global.getFile()) |file| {40 if (global.getFile()) |file| {
41 try addRoot(macho_file, roots, file, global);41 try addRoot(macho_file, roots, file, global);
42 } else {42 } else {
43 assert(macho_file.getSymbol(global).undf()); // Stub as our entrypoint is in a dylib.43 assert(macho_file.getSymbol(global).undf()); // Stub as our entrypoint is in a dylib.
44 }
44 }45 }
45 },46 },
46 else => |other| {47 else => |other| {
...@@ -116,7 +117,7 @@ fn markLive(macho_file: *MachO, atom_index: Atom.Index, alive: *AtomTable) void...@@ -116,7 +117,7 @@ fn markLive(macho_file: *MachO, atom_index: Atom.Index, alive: *AtomTable) void
116117
117 alive.putAssumeCapacityNoClobber(atom_index, {});118 alive.putAssumeCapacityNoClobber(atom_index, {});
118119
119 const cpu_arch = macho_file.options.target.cpu.arch;120 const cpu_arch = macho_file.base.options.target.cpu.arch;
120121
121 const sym = macho_file.getSymbol(atom.getSymbolWithLoc());122 const sym = macho_file.getSymbol(atom.getSymbolWithLoc());
122 const header = macho_file.sections.items(.header)[sym.n_sect - 1];123 const header = macho_file.sections.items(.header)[sym.n_sect - 1];
src/link/MachO/zld.zig+14-11
...@@ -401,7 +401,7 @@ pub fn linkWithZld(...@@ -401,7 +401,7 @@ pub fn linkWithZld(
401 try macho_file.createDyldPrivateAtom();401 try macho_file.createDyldPrivateAtom();
402 try macho_file.createTentativeDefAtoms();402 try macho_file.createTentativeDefAtoms();
403403
404 if (macho_file.options.output_mode == .Exe) {404 if (macho_file.base.options.output_mode == .Exe) {
405 const global = macho_file.getEntryPoint().?;405 const global = macho_file.getEntryPoint().?;
406 if (macho_file.getSymbol(global).undf()) {406 if (macho_file.getSymbol(global).undf()) {
407 // We do one additional check here in case the entry point was found in one of the dylibs.407 // 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(...@@ -429,7 +429,7 @@ pub fn linkWithZld(
429 if (macho_file.dyld_stub_binder_index) |index|429 if (macho_file.dyld_stub_binder_index) |index|
430 try macho_file.addGotEntry(macho_file.globals.items[index]);430 try macho_file.addGotEntry(macho_file.globals.items[index]);
431431
432 try macho_file.calcSectionSizes();432 try calcSectionSizes(macho_file);
433433
434 var unwind_info = UnwindInfo{ .gpa = gpa };434 var unwind_info = UnwindInfo{ .gpa = gpa };
435 defer unwind_info.deinit();435 defer unwind_info.deinit();
...@@ -461,9 +461,9 @@ pub fn linkWithZld(...@@ -461,9 +461,9 @@ pub fn linkWithZld(
461 try writeLaSymbolPtrs(macho_file);461 try writeLaSymbolPtrs(macho_file);
462 }462 }
463 if (macho_file.got_section_index) |sect_id|463 if (macho_file.got_section_index) |sect_id|
464 try macho_file.writePointerEntries(sect_id, &macho_file.got_table);464 try writePointerEntries(macho_file, sect_id, &macho_file.got_table);
465 if (macho_file.tlv_ptr_section_index) |sect_id|465 if (macho_file.tlv_ptr_section_index) |sect_id|
466 try macho_file.writePointerEntries(sect_id, &macho_file.tlv_ptr_table);466 try writePointerEntries(macho_file, sect_id, &macho_file.tlv_ptr_table);
467467
468 try eh_frame.write(macho_file, &unwind_info);468 try eh_frame.write(macho_file, &unwind_info);
469 try unwind_info.write(macho_file);469 try unwind_info.write(macho_file);
...@@ -546,11 +546,11 @@ pub fn linkWithZld(...@@ -546,11 +546,11 @@ pub fn linkWithZld(
546 else => {},546 else => {},
547 }547 }
548548
549 try load_commands.writeRpathLCs(gpa, macho_file.base.options, lc_writer);549 try load_commands.writeRpathLCs(gpa, &macho_file.base.options, lc_writer);
550 try lc_writer.writeStruct(macho.source_version_command{550 try lc_writer.writeStruct(macho.source_version_command{
551 .version = 0,551 .version = 0,
552 });552 });
553 try load_commands.writeBuildVersionLC(macho_file.base.options, lc_writer);553 try load_commands.writeBuildVersionLC(&macho_file.base.options, lc_writer);
554554
555 const uuid_cmd_offset = @sizeOf(macho.mach_header_64) + @as(u32, @intCast(lc_buffer.items.len));555 const uuid_cmd_offset = @sizeOf(macho.mach_header_64) + @as(u32, @intCast(lc_buffer.items.len));
556 try lc_writer.writeStruct(macho_file.uuid_cmd);556 try lc_writer.writeStruct(macho_file.uuid_cmd);
...@@ -1053,11 +1053,14 @@ fn allocateSegments(macho_file: *MachO) !void {...@@ -1053,11 +1053,14 @@ fn allocateSegments(macho_file: *MachO) !void {
1053 const gpa = macho_file.base.allocator;1053 const gpa = macho_file.base.allocator;
1054 for (macho_file.segments.items, 0..) |*segment, segment_index| {1054 for (macho_file.segments.items, 0..) |*segment, segment_index| {
1055 const is_text_segment = mem.eql(u8, segment.segName(), "__TEXT");1055 const is_text_segment = mem.eql(u8, segment.segName(), "__TEXT");
1056 const base_size = if (is_text_segment) try load_commands.calcMinHeaderPad(gpa, macho_file.base.options, .{1056 const base_size = if (is_text_segment)
1057 .segments = macho_file.segments.items,1057 try load_commands.calcMinHeaderPad(gpa, &macho_file.base.options, .{
1058 .dylibs = macho_file.dylibs.items,1058 .segments = macho_file.segments.items,
1059 .referenced_dylibs = macho_file.referenced_dylibs.keys(),1059 .dylibs = macho_file.dylibs.items,
1060 }) else 0;1060 .referenced_dylibs = macho_file.referenced_dylibs.keys(),
1061 })
1062 else
1063 0;
1061 try allocateSegment(macho_file, @as(u8, @intCast(segment_index)), base_size);1064 try allocateSegment(macho_file, @as(u8, @intCast(segment_index)), base_size);
1062 }1065 }
1063}1066}