From f2bf1390a29a9decaa5ca49d3ae720b360583b35 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Mon, 9 Aug 2021 22:44:57 +0200 Subject: macho: fix linking of dylibs and frameworks Previously, I have incorrectly assumed that with two-level namespace we only need to link in dylibs/frameworks that actually export symbols which are undefined in the linked image. Turns out, regardless of whether we link with two-level namespace (default on macOS) or a flat namespace (more common on other platforms), we always need to put the dylibs/frameworks as specified by the user from the linker line into the final linked image. --- src/link/MachO.zig | 10 ++++++++++ src/link/MachO/Dylib.zig | 2 ++ 2 files changed, 12 insertions(+) (limited to 'src') diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 36d5bc6a87..5d9b0e6fe1 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -1013,7 +1013,12 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const .syslibroot = syslibroot, })) |dylibs| { defer self.base.allocator.free(dylibs); + const dylib_id = @intCast(u16, self.dylibs.items.len); try self.dylibs.appendSlice(self.base.allocator, dylibs); + // We always have to add the dylib that was on the linker line. + if (!self.referenced_dylibs.contains(dylib_id)) { + try self.referenced_dylibs.putNoClobber(self.base.allocator, dylib_id, {}); + } continue; } @@ -1028,7 +1033,12 @@ fn parseLibs(self: *MachO, libs: []const []const u8, syslibroot: ?[]const u8) !v .syslibroot = syslibroot, })) |dylibs| { defer self.base.allocator.free(dylibs); + const dylib_id = @intCast(u16, self.dylibs.items.len); try self.dylibs.appendSlice(self.base.allocator, dylibs); + // We always have to add the dylib that was on the linker line. + if (!self.referenced_dylibs.contains(dylib_id)) { + try self.referenced_dylibs.putNoClobber(self.base.allocator, dylib_id, {}); + } continue; } diff --git a/src/link/MachO/Dylib.zig b/src/link/MachO/Dylib.zig index 4763203c3b..71301ccbbf 100644 --- a/src/link/MachO/Dylib.zig +++ b/src/link/MachO/Dylib.zig @@ -193,6 +193,8 @@ pub fn createAndParseFromPath( defer dylibs.deinit(); try dylibs.append(dylib); + // TODO this should not be performed if the user specifies `-flat_namespace` flag. + // See ld64 manpages. try dylib.parseDependentLibs(allocator, arch, &dylibs, opts.syslibroot); return dylibs.toOwnedSlice(); -- cgit v1.2.3