authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-03-31 23:28:55+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-04-01 14:22:44+02:00
log5d0bb50e3d0e87d1d8aad864559e10c83199b608
tree047d6c99e2bc753805b003244612ea4981d3488d
parent3874df839d3deabec0e82d0ae19947d655f5713d

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


4 files changed, 68 insertions(+), 0 deletions(-)

test/link.zig+4
......@@ -108,6 +108,10 @@ pub const cases = [_]Case{
108108 .build_root = "test/link/macho/entry_in_archive",
109109 .import = @import("link/macho/entry_in_archive/build.zig"),
110110 },
111 .{
112 .build_root = "test/link/macho/entry_in_dylib",
113 .import = @import("link/macho/entry_in_dylib/build.zig"),
114 },
111115 .{
112116 .build_root = "test/link/macho/headerpad",
113117 .import = @import("link/macho/headerpad/build.zig"),
test/link/macho/entry_in_dylib/bootstrap.c created+5
......@@ -0,0 +1,5 @@
1extern int my_main();
2
3int bootstrap() {
4 return my_main();
5}
test/link/macho/entry_in_dylib/build.zig created+53
......@@ -0,0 +1,53 @@
1const std = @import("std");
2
3pub const requires_symlinks = true;
4
5pub fn build(b: *std.Build) void {
6 const test_step = b.step("test", "Test it");
7 b.default_step = test_step;
8
9 add(b, test_step, .Debug);
10 add(b, test_step, .ReleaseFast);
11 add(b, test_step, .ReleaseSmall);
12 add(b, test_step, .ReleaseSafe);
13}
14
15fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
16 const lib = b.addSharedLibrary(.{
17 .name = "bootstrap",
18 .optimize = optimize,
19 .target = .{ .os_tag = .macos },
20 });
21 lib.addCSourceFile("bootstrap.c", &.{});
22 lib.linkLibC();
23 lib.linker_allow_shlib_undefined = true;
24
25 const exe = b.addExecutable(.{
26 .name = "main",
27 .optimize = optimize,
28 .target = .{ .os_tag = .macos },
29 });
30 exe.addCSourceFile("main.c", &.{});
31 exe.linkLibrary(lib);
32 exe.linkLibC();
33 exe.entry_symbol_name = "_bootstrap";
34
35 const check_exe = exe.checkObject();
36 check_exe.checkStart("segname __TEXT");
37 check_exe.checkNext("vmaddr {text_vmaddr}");
38
39 check_exe.checkStart("sectname __stubs");
40 check_exe.checkNext("addr {stubs_vmaddr}");
41
42 check_exe.checkStart("cmd MAIN");
43 check_exe.checkNext("entryoff {entryoff}");
44
45 check_exe.checkComputeCompare("text_vmaddr entryoff +", .{
46 .op = .eq,
47 .value = .{ .variable = "stubs_vmaddr" }, // The entrypoint should be a synthetic stub
48 });
49
50 const run = check_exe.runAndCompare();
51 run.expectStdOutEqual("Hello!\n");
52 test_step.dependOn(&run.step);
53}
test/link/macho/entry_in_dylib/main.c created+6
......@@ -0,0 +1,6 @@
1#include <stdio.h>
2
3int my_main() {
4 fprintf(stdout, "Hello!\n");
5 return 0;
6}