commit 5d0bb50e3d0e87d1d8aad864559e10c83199b608 (tree)
parent 3874df839d3deabec0e82d0ae19947d655f5713d
Author: Jakub Konka <kubkon@jakubkonka.com>
Date: Fri, 31 Mar 2023 23:28:55 +0200
link-test: add test for entry in a dynamic library for MachO
Diffstat:
4 files changed, 68 insertions(+), 0 deletions(-)
diff --git a/test/link.zig b/test/link.zig
@@ -109,6 +109,10 @@ pub const cases = [_]Case{
.import = @import("link/macho/entry_in_archive/build.zig"),
},
.{
+ .build_root = "test/link/macho/entry_in_dylib",
+ .import = @import("link/macho/entry_in_dylib/build.zig"),
+ },
+ .{
.build_root = "test/link/macho/headerpad",
.import = @import("link/macho/headerpad/build.zig"),
},
diff --git a/test/link/macho/entry_in_dylib/bootstrap.c b/test/link/macho/entry_in_dylib/bootstrap.c
@@ -0,0 +1,5 @@
+extern int my_main();
+
+int bootstrap() {
+ return my_main();
+}
diff --git a/test/link/macho/entry_in_dylib/build.zig b/test/link/macho/entry_in_dylib/build.zig
@@ -0,0 +1,53 @@
+const std = @import("std");
+
+pub const requires_symlinks = true;
+
+pub fn build(b: *std.Build) void {
+ const test_step = b.step("test", "Test it");
+ b.default_step = test_step;
+
+ add(b, test_step, .Debug);
+ add(b, test_step, .ReleaseFast);
+ add(b, test_step, .ReleaseSmall);
+ add(b, test_step, .ReleaseSafe);
+}
+
+fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
+ const lib = b.addSharedLibrary(.{
+ .name = "bootstrap",
+ .optimize = optimize,
+ .target = .{ .os_tag = .macos },
+ });
+ lib.addCSourceFile("bootstrap.c", &.{});
+ lib.linkLibC();
+ lib.linker_allow_shlib_undefined = true;
+
+ const exe = b.addExecutable(.{
+ .name = "main",
+ .optimize = optimize,
+ .target = .{ .os_tag = .macos },
+ });
+ exe.addCSourceFile("main.c", &.{});
+ exe.linkLibrary(lib);
+ exe.linkLibC();
+ exe.entry_symbol_name = "_bootstrap";
+
+ const check_exe = exe.checkObject();
+ check_exe.checkStart("segname __TEXT");
+ check_exe.checkNext("vmaddr {text_vmaddr}");
+
+ check_exe.checkStart("sectname __stubs");
+ check_exe.checkNext("addr {stubs_vmaddr}");
+
+ check_exe.checkStart("cmd MAIN");
+ check_exe.checkNext("entryoff {entryoff}");
+
+ check_exe.checkComputeCompare("text_vmaddr entryoff +", .{
+ .op = .eq,
+ .value = .{ .variable = "stubs_vmaddr" }, // The entrypoint should be a synthetic stub
+ });
+
+ const run = check_exe.runAndCompare();
+ run.expectStdOutEqual("Hello!\n");
+ test_step.dependOn(&run.step);
+}
diff --git a/test/link/macho/entry_in_dylib/main.c b/test/link/macho/entry_in_dylib/main.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+int my_main() {
+ fprintf(stdout, "Hello!\n");
+ return 0;
+}