From 5d0bb50e3d0e87d1d8aad864559e10c83199b608 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Fri, 31 Mar 2023 23:28:55 +0200 Subject: [PATCH] link-test: add test for entry in a dynamic library for MachO --- test/link.zig | 4 ++ test/link/macho/entry_in_dylib/bootstrap.c | 5 ++ test/link/macho/entry_in_dylib/build.zig | 53 ++++++++++++++++++++++ test/link/macho/entry_in_dylib/main.c | 6 +++ 4 files changed, 68 insertions(+) create mode 100644 test/link/macho/entry_in_dylib/bootstrap.c create mode 100644 test/link/macho/entry_in_dylib/build.zig create mode 100644 test/link/macho/entry_in_dylib/main.c diff --git a/test/link.zig b/test/link.zig index 2b098c9b5d..d319d7fa61 100644 --- a/test/link.zig +++ b/test/link.zig @@ -108,6 +108,10 @@ pub const cases = [_]Case{ .build_root = "test/link/macho/entry_in_archive", .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 new file mode 100644 index 0000000000..6e9a2b830c --- /dev/null +++ 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 new file mode 100644 index 0000000000..feaa8541b7 --- /dev/null +++ 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 new file mode 100644 index 0000000000..26173b80ba --- /dev/null +++ b/test/link/macho/entry_in_dylib/main.c @@ -0,0 +1,6 @@ +#include + +int my_main() { + fprintf(stdout, "Hello!\n"); + return 0; +}