From f5a0b9315b4920c5e9e9f886e29101dcc8ae7cc7 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Fri, 11 Dec 2020 17:11:40 +0100 Subject: [PATCH] macho: calculate next available dylib ordinal --- src/link/MachO.zig | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 8f3852b61c..681c06b06d 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -107,7 +107,6 @@ offset_table: std.ArrayListUnmanaged(u64) = .{}, error_flags: File.ErrorFlags = File.ErrorFlags{}, cmd_table_dirty: bool = false, -other_dylibs_present: bool = false, /// A list of text blocks that have surplus capacity. This list can have false /// positives, as functions grow and shrink over time, only sometimes being added @@ -755,8 +754,8 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { // binaries up! const out_file = try directory.handle.openFile(self.base.options.emit.?.sub_path, .{ .write = true }); try self.parseFromFile(out_file); + if (self.libsystem_cmd_index == null) { - if (self.other_dylibs_present) return; // TODO We cannot handle this situation yet. const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment; const text_section = text_segment.sections.items[self.text_section_index.?]; const after_last_cmd_offset = self.header.?.sizeofcmds + @sizeOf(macho.mach_header_64); @@ -769,6 +768,18 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { return error.NotEnoughPadding; } + // Calculate next available dylib ordinal. + const next_ordinal = blk: { + var ordinal: u32 = 1; + for (self.load_commands.items) |cmd| { + switch (cmd) { + .Dylib => ordinal += 1, + else => {}, + } + } + break :blk ordinal; + }; + // Add load dylib load command self.libsystem_cmd_index = @intCast(u16, self.load_commands.items.len); 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 { const x = cmd.Dylib; if (parseAndCmpName(x.data, mem.spanZ(LIB_SYSTEM_PATH))) { self.libsystem_cmd_index = i; - } else { - self.other_dylibs_present = true; } }, macho.LC_FUNCTION_STARTS => {