link-test: add test for entry in a dynamic library for MachO

This commit is contained in:
Jakub Konka 2023-03-31 23:28:55 +02:00
parent 3874df839d
commit 5d0bb50e3d
4 changed files with 68 additions and 0 deletions

View File

@ -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"),

View File

@ -0,0 +1,5 @@
extern int my_main();
int bootstrap() {
return my_main();
}

View File

@ -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);
}

View File

@ -0,0 +1,6 @@
#include <stdio.h>
int my_main() {
fprintf(stdout, "Hello!\n");
return 0;
}