authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-12-11 17:11:40+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-12-17 10:04:53+01:00
logf5a0b9315b4920c5e9e9f886e29101dcc8ae7cc7
treefcd3fc913712ef910ba6823b76d781d474d0bdc3
parentcc2592969df1c0b2ea6d62eb9384b0022b98305b

macho: calculate next available dylib ordinal


1 files changed, 13 insertions(+), 4 deletions(-)

src/link/MachO.zig+13-4
...@@ -107,7 +107,6 @@ offset_table: std.ArrayListUnmanaged(u64) = .{},...@@ -107,7 +107,6 @@ offset_table: std.ArrayListUnmanaged(u64) = .{},
107error_flags: File.ErrorFlags = File.ErrorFlags{},107error_flags: File.ErrorFlags = File.ErrorFlags{},
108108
109cmd_table_dirty: bool = false,109cmd_table_dirty: bool = false,
110other_dylibs_present: bool = false,
111110
112/// A list of text blocks that have surplus capacity. This list can have false111/// A list of text blocks that have surplus capacity. This list can have false
113/// positives, as functions grow and shrink over time, only sometimes being added112/// positives, as functions grow and shrink over time, only sometimes being added
...@@ -755,8 +754,8 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {...@@ -755,8 +754,8 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
755 // binaries up!754 // binaries up!
756 const out_file = try directory.handle.openFile(self.base.options.emit.?.sub_path, .{ .write = true });755 const out_file = try directory.handle.openFile(self.base.options.emit.?.sub_path, .{ .write = true });
757 try self.parseFromFile(out_file);756 try self.parseFromFile(out_file);
757
758 if (self.libsystem_cmd_index == null) {758 if (self.libsystem_cmd_index == null) {
759 if (self.other_dylibs_present) return; // TODO We cannot handle this situation yet.
760 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;759 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
761 const text_section = text_segment.sections.items[self.text_section_index.?];760 const text_section = text_segment.sections.items[self.text_section_index.?];
762 const after_last_cmd_offset = self.header.?.sizeofcmds + @sizeOf(macho.mach_header_64);761 const after_last_cmd_offset = self.header.?.sizeofcmds + @sizeOf(macho.mach_header_64);
...@@ -769,6 +768,18 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {...@@ -769,6 +768,18 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
769 return error.NotEnoughPadding;768 return error.NotEnoughPadding;
770 }769 }
771770
771 // Calculate next available dylib ordinal.
772 const next_ordinal = blk: {
773 var ordinal: u32 = 1;
774 for (self.load_commands.items) |cmd| {
775 switch (cmd) {
776 .Dylib => ordinal += 1,
777 else => {},
778 }
779 }
780 break :blk ordinal;
781 };
782
772 // Add load dylib load command783 // Add load dylib load command
773 self.libsystem_cmd_index = @intCast(u16, self.load_commands.items.len);784 self.libsystem_cmd_index = @intCast(u16, self.load_commands.items.len);
774 const cmdsize = mem.alignForwardGeneric(u64, @sizeOf(macho.dylib_command) + mem.lenZ(LIB_SYSTEM_PATH), @sizeOf(u64));785 const cmdsize = mem.alignForwardGeneric(u64, @sizeOf(macho.dylib_command) + mem.lenZ(LIB_SYSTEM_PATH), @sizeOf(u64));
...@@ -2007,8 +2018,6 @@ fn parseFromFile(self: *MachO, file: fs.File) !void {...@@ -2007,8 +2018,6 @@ fn parseFromFile(self: *MachO, file: fs.File) !void {
2007 const x = cmd.Dylib;2018 const x = cmd.Dylib;
2008 if (parseAndCmpName(x.data, mem.spanZ(LIB_SYSTEM_PATH))) {2019 if (parseAndCmpName(x.data, mem.spanZ(LIB_SYSTEM_PATH))) {
2009 self.libsystem_cmd_index = i;2020 self.libsystem_cmd_index = i;
2010 } else {
2011 self.other_dylibs_present = true;
2012 }2021 }
2013 },2022 },
2014 macho.LC_FUNCTION_STARTS => {2023 macho.LC_FUNCTION_STARTS => {