authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-09 22:44:57+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-10 08:13:07+02:00
logf2bf1390a29a9decaa5ca49d3ae720b360583b35
treef040031c1ee692eef0b5d1e52fda6cfcdf265bbe
parent2ccd023c6ae590b4ff311814ccf5ff508c7669ef

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.

5 files changed, 54 insertions(+), 0 deletions(-)

src/link/MachO.zig+10
...@@ -1013,7 +1013,12 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const...@@ -1013,7 +1013,12 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const
1013 .syslibroot = syslibroot,1013 .syslibroot = syslibroot,
1014 })) |dylibs| {1014 })) |dylibs| {
1015 defer self.base.allocator.free(dylibs);1015 defer self.base.allocator.free(dylibs);
1016 const dylib_id = @intCast(u16, self.dylibs.items.len);
1016 try self.dylibs.appendSlice(self.base.allocator, dylibs);1017 try self.dylibs.appendSlice(self.base.allocator, dylibs);
1018 // We always have to add the dylib that was on the linker line.
1019 if (!self.referenced_dylibs.contains(dylib_id)) {
1020 try self.referenced_dylibs.putNoClobber(self.base.allocator, dylib_id, {});
1021 }
1017 continue;1022 continue;
1018 }1023 }
10191024
...@@ -1028,7 +1033,12 @@ fn parseLibs(self: *MachO, libs: []const []const u8, syslibroot: ?[]const u8) !v...@@ -1028,7 +1033,12 @@ fn parseLibs(self: *MachO, libs: []const []const u8, syslibroot: ?[]const u8) !v
1028 .syslibroot = syslibroot,1033 .syslibroot = syslibroot,
1029 })) |dylibs| {1034 })) |dylibs| {
1030 defer self.base.allocator.free(dylibs);1035 defer self.base.allocator.free(dylibs);
1036 const dylib_id = @intCast(u16, self.dylibs.items.len);
1031 try self.dylibs.appendSlice(self.base.allocator, dylibs);1037 try self.dylibs.appendSlice(self.base.allocator, dylibs);
1038 // We always have to add the dylib that was on the linker line.
1039 if (!self.referenced_dylibs.contains(dylib_id)) {
1040 try self.referenced_dylibs.putNoClobber(self.base.allocator, dylib_id, {});
1041 }
1032 continue;1042 continue;
1033 }1043 }
10341044
src/link/MachO/Dylib.zig+2
...@@ -193,6 +193,8 @@ pub fn createAndParseFromPath(...@@ -193,6 +193,8 @@ pub fn createAndParseFromPath(
193 defer dylibs.deinit();193 defer dylibs.deinit();
194194
195 try dylibs.append(dylib);195 try dylibs.append(dylib);
196 // TODO this should not be performed if the user specifies `-flat_namespace` flag.
197 // See ld64 manpages.
196 try dylib.parseDependentLibs(allocator, arch, &dylibs, opts.syslibroot);198 try dylib.parseDependentLibs(allocator, arch, &dylibs, opts.syslibroot);
197199
198 return dylibs.toOwnedSlice();200 return dylibs.toOwnedSlice();
test/standalone.zig+1
...@@ -16,6 +16,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void {...@@ -16,6 +16,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
16 cases.addBuildFile("test/standalone/link_interdependent_static_c_libs/build.zig", .{});16 cases.addBuildFile("test/standalone/link_interdependent_static_c_libs/build.zig", .{});
17 cases.addBuildFile("test/standalone/link_static_lib_as_system_lib/build.zig", .{});17 cases.addBuildFile("test/standalone/link_static_lib_as_system_lib/build.zig", .{});
18 cases.addBuildFile("test/standalone/link_common_symbols/build.zig", .{});18 cases.addBuildFile("test/standalone/link_common_symbols/build.zig", .{});
19 cases.addBuildFile("test/standalone/link_frameworks/build.zig", .{ .requires_macos_sdk = true });
19 cases.addBuildFile("test/standalone/issue_339/build.zig", .{});20 cases.addBuildFile("test/standalone/issue_339/build.zig", .{});
20 cases.addBuildFile("test/standalone/issue_8550/build.zig", .{});21 cases.addBuildFile("test/standalone/issue_8550/build.zig", .{});
21 cases.addBuildFile("test/standalone/issue_794/build.zig", .{});22 cases.addBuildFile("test/standalone/issue_794/build.zig", .{});
test/standalone/link_frameworks/build.zig created+34
...@@ -0,0 +1,34 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3const CrossTarget = std.zig.CrossTarget;
4
5fn isRunnableTarget(t: CrossTarget) bool {
6 // TODO I think we might be able to run this on Linux via Darling.
7 // Add a check for that here, and return true if Darling is available.
8 if (t.isNative() and t.getOsTag() == .macos)
9 return true
10 else
11 return false;
12}
13
14pub fn build(b: *Builder) void {
15 const mode = b.standardReleaseOptions();
16 const target = b.standardTargetOptions(.{});
17
18 const test_step = b.step("test", "Test the program");
19
20 const exe = b.addExecutable("test", null);
21 b.default_step.dependOn(&exe.step);
22 exe.addCSourceFile("main.c", &[0][]const u8{});
23 exe.setBuildMode(mode);
24 exe.setTarget(target);
25 exe.linkLibC();
26 // TODO when we figure out how to ship framework stubs for cross-compilation,
27 // populate paths to the sysroot here.
28 exe.linkFramework("Cocoa");
29
30 if (isRunnableTarget(target)) {
31 const run_cmd = exe.run();
32 test_step.dependOn(&run_cmd.step);
33 }
34}
test/standalone/link_frameworks/main.c created+7
...@@ -0,0 +1,7 @@
1#include <assert.h>
2#include <objc/runtime.h>
3
4int main() {
5 assert(objc_getClass("NSObject") > 0);
6 assert(objc_getClass("NSApplication") > 0);
7}