test/link/macho: upgrade and migrate dead_strip test

This commit is contained in:
Jakub Konka 2024-01-13 18:09:32 +01:00
parent aa50bca151
commit 6cdcf61a5c
4 changed files with 82 additions and 76 deletions

View File

@ -107,10 +107,6 @@ pub const cases = [_]Case{
.build_root = "test/link/macho/bugs/16628",
.import = @import("link/macho/bugs/16628/build.zig"),
},
.{
.build_root = "test/link/macho/dead_strip",
.import = @import("link/macho/dead_strip/build.zig"),
},
.{
.build_root = "test/link/macho/dead_strip_dylibs",
.import = @import("link/macho/dead_strip_dylibs/build.zig"),

View File

@ -8,12 +8,94 @@ pub fn testAll(b: *std.Build) *Step {
.os_tag = .macos,
});
macho_step.dependOn(testDeadStrip(b, .{ .target = default_target }));
macho_step.dependOn(testEntryPointDylib(b, .{ .target = default_target }));
macho_step.dependOn(testSectionBoundarySymbols(b, .{ .target = default_target }));
return macho_step;
}
fn testDeadStrip(b: *std.Build, opts: Options) *Step {
const test_step = addTestStep(b, "macho-dead-strip", opts);
const obj = addObject(b, opts, .{ .name = "a", .cpp_source_bytes =
\\#include <stdio.h>
\\int two() { return 2; }
\\int live_var1 = 1;
\\int live_var2 = two();
\\int dead_var1 = 3;
\\int dead_var2 = 4;
\\void live_fn1() {}
\\void live_fn2() { live_fn1(); }
\\void dead_fn1() {}
\\void dead_fn2() { dead_fn1(); }
\\int main() {
\\ printf("%d %d\n", live_var1, live_var2);
\\ live_fn2();
\\}
});
{
const exe = addExecutable(b, opts, .{ .name = "no_dead_strip" });
exe.addObject(obj);
exe.link_gc_sections = false;
const check = exe.checkObject();
check.checkInSymtab();
check.checkContains("live_var1");
check.checkInSymtab();
check.checkContains("live_var2");
check.checkInSymtab();
check.checkContains("dead_var1");
check.checkInSymtab();
check.checkContains("dead_var2");
check.checkInSymtab();
check.checkContains("live_fn1");
check.checkInSymtab();
check.checkContains("live_fn2");
check.checkInSymtab();
check.checkContains("dead_fn1");
check.checkInSymtab();
check.checkContains("dead_fn2");
test_step.dependOn(&check.step);
const run = addRunArtifact(exe);
run.expectStdOutEqual("1 2\n");
test_step.dependOn(&run.step);
}
{
const exe = addExecutable(b, opts, .{ .name = "yes_dead_strip" });
exe.addObject(obj);
exe.link_gc_sections = true;
const check = exe.checkObject();
check.checkInSymtab();
check.checkContains("live_var1");
check.checkInSymtab();
check.checkContains("live_var2");
check.checkInSymtab();
check.checkNotPresent("dead_var1");
check.checkInSymtab();
check.checkNotPresent("dead_var2");
check.checkInSymtab();
check.checkContains("live_fn1");
check.checkInSymtab();
check.checkContains("live_fn2");
check.checkInSymtab();
check.checkNotPresent("dead_fn1");
check.checkInSymtab();
check.checkNotPresent("dead_fn2");
test_step.dependOn(&check.step);
const run = addRunArtifact(exe);
run.expectStdOutEqual("1 2\n");
test_step.dependOn(&run.step);
}
return test_step;
}
fn testEntryPointDylib(b: *std.Build, opts: Options) *Step {
const test_step = addTestStep(b, "macho-entry-point-dylib", opts);

View File

@ -1,58 +0,0 @@
const std = @import("std");
pub const requires_symlinks = true;
pub fn build(b: *std.Build) void {
const optimize: std.builtin.OptimizeMode = .Debug;
const target = b.resolveTargetQuery(.{ .os_tag = .macos });
const test_step = b.step("test", "Test the program");
b.default_step = test_step;
{
// Without -dead_strip, we expect `iAmUnused` symbol present
const exe = createScenario(b, optimize, target, "no-gc");
const check = exe.checkObject();
check.checkInSymtab();
check.checkContains("(__TEXT,__text) external _iAmUnused");
test_step.dependOn(&check.step);
const run = b.addRunArtifact(exe);
run.skip_foreign_checks = true;
run.expectStdOutEqual("Hello!\n");
test_step.dependOn(&run.step);
}
{
// With -dead_strip, no `iAmUnused` symbol should be present
const exe = createScenario(b, optimize, target, "yes-gc");
exe.link_gc_sections = true;
const check = exe.checkObject();
check.checkInSymtab();
check.checkNotPresent("(__TEXT,__text) external _iAmUnused");
test_step.dependOn(&check.step);
const run = b.addRunArtifact(exe);
run.skip_foreign_checks = true;
run.expectStdOutEqual("Hello!\n");
test_step.dependOn(&run.step);
}
}
fn createScenario(
b: *std.Build,
optimize: std.builtin.OptimizeMode,
target: std.Build.ResolvedTarget,
name: []const u8,
) *std.Build.Step.Compile {
const exe = b.addExecutable(.{
.name = name,
.optimize = optimize,
.target = target,
});
exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} });
exe.linkLibC();
return exe;
}

View File

@ -1,14 +0,0 @@
#include <stdio.h>
void printMe() {
printf("Hello!\n");
}
int main(int argc, char* argv[]) {
printMe();
return 0;
}
void iAmUnused() {
printf("YOU SHALL NOT PASS!\n");
}