Skip to content

Commit

Permalink
elf: add very simplistic build.zig-based test harness
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Jun 29, 2023
1 parent f5acd61 commit 8433d2e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ pub fn build(b: *std.Build.Builder) void {

const test_step = b.step("test", "Run library and end-to-end tests");
test_step.dependOn(&b.addRunArtifact(tests).step);

const elf_step = @import("test/test.zig").addElfTests(b);
elf_step.dependOn(&symlinks.step);
test_step.dependOn(elf_step);
}

fn addSymlinks(
Expand Down
11 changes: 11 additions & 0 deletions test/elf.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub const Case = struct {
build_root: []const u8,
import: type,
};

pub const cases = [_]Case{
.{
.build_root = "test/elf/dso-ifunc",
.import = @import("elf/dso-ifunc/build.zig"),
},
};
14 changes: 14 additions & 0 deletions test/elf/dso-ifunc/a.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>

__attribute__((ifunc("resolve_foobar")))
void foobar(void);

static void real_foobar(void) {
printf("Hello world\n");
}

typedef void Func();

static Func *resolve_foobar(void) {
return real_foobar;
}
25 changes: 25 additions & 0 deletions test/elf/dso-ifunc/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const test_step = b.step("test", "Test it");
b.default_step = test_step;

const lib_step = b.addSystemCommand(&.{
"cc", "-fno-lto", "-fPIC", "-shared", "-o",
"liba.so", "a.c", "-B../../../zig-out/bin",
});
test_step.dependOn(&lib_step.step);

const exe_step = b.addSystemCommand(&.{
"cc", "-fno-lto", "main.c",
"-la", "-L.", "-Wl,-rpath,.",
"-B../../../zig-out/bin",
});
exe_step.step.dependOn(&lib_step.step);
test_step.dependOn(&exe_step.step);

const run_step = b.addSystemCommand(&.{"./a.out"});
run_step.expectStdOutEqual("Hello world\n");
run_step.step.dependOn(&exe_step.step);
test_step.dependOn(&run_step.step);
}
5 changes: 5 additions & 0 deletions test/elf/dso-ifunc/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void foobar(void);

int main() {
foobar();
}
18 changes: 18 additions & 0 deletions test/test.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const std = @import("std");
const builtin = @import("builtin");
const CrossTarget = std.zig.CrossTarget;
const TestContext = @import("../src/test.zig").TestContext;

const elf = @import("elf.zig");

const linux_x86_64 = CrossTarget{
.cpu_arch = .x86_64,
.os_tag = .linux,
Expand Down Expand Up @@ -201,3 +204,18 @@ pub fn addCases(ctx: *TestContext) !void {
}
}
}

pub fn addElfTests(b: *std.Build) *std.Build.Step {
const step = b.step("test-elf", "Run ELF tests");

if (builtin.target.ofmt == .elf)
inline for (elf.cases) |case| {
const dep = b.anonymousDependency(case.build_root, case.import, .{});
const dep_step = dep.builder.default_step;
const dep_prefix_adjusted = dep.builder.dep_prefix["test".len..];
dep_step.name = b.fmt("{s}{s}", .{ dep_prefix_adjusted, dep_step.name });
step.dependOn(dep_step);
};

return step;
}

0 comments on commit 8433d2e

Please sign in to comment.