authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-04-20 18:41:50+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-04-20 23:36:42+02:00
log457d84f45af28362869cc4e91f0c56c3bd052365
treeb54b18e1c6cc49b37171af14eb601253e372de90
parent2cc16239259a62502a5c67535de162e89fbfc4a9

link/elf: remove link.link.build as unused; add some merge strings tests


2 files changed, 96 insertions(+), 18 deletions(-)

test/link/elf.zig+96
...@@ -72,6 +72,8 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {...@@ -72,6 +72,8 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
72 elf_step.dependOn(testLinkingC(b, .{ .target = musl_target }));72 elf_step.dependOn(testLinkingC(b, .{ .target = musl_target }));
73 elf_step.dependOn(testLinkingCpp(b, .{ .target = musl_target }));73 elf_step.dependOn(testLinkingCpp(b, .{ .target = musl_target }));
74 elf_step.dependOn(testLinkingZig(b, .{ .target = musl_target }));74 elf_step.dependOn(testLinkingZig(b, .{ .target = musl_target }));
75 elf_step.dependOn(testMergeStrings(b, .{ .target = musl_target }));
76 elf_step.dependOn(testMergeStrings2(b, .{ .target = musl_target }));
75 // https://github.com/ziglang/zig/issues/1745177 // https://github.com/ziglang/zig/issues/17451
76 // elf_step.dependOn(testNoEhFrameHdr(b, .{ .target = musl_target }));78 // elf_step.dependOn(testNoEhFrameHdr(b, .{ .target = musl_target }));
77 elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target }));79 elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target }));
...@@ -2267,6 +2269,100 @@ fn testLinkingZig(b: *Build, opts: Options) *Step {...@@ -2267,6 +2269,100 @@ fn testLinkingZig(b: *Build, opts: Options) *Step {
2267 return test_step;2269 return test_step;
2268}2270}
22692271
2272// Adapted from https://github.com/rui314/mold/blob/main/test/elf/mergeable-strings.sh
2273fn testMergeStrings(b: *Build, opts: Options) *Step {
2274 const test_step = addTestStep(b, "merge-strings", opts);
2275
2276 const obj1 = addObject(b, opts, .{ .name = "a.o" });
2277 addCSourceBytes(obj1,
2278 \\#include <uchar.h>
2279 \\#include <wchar.h>
2280 \\char *cstr1 = "foo";
2281 \\wchar_t *wide1 = L"foo";
2282 \\char16_t *utf16_1 = u"foo";
2283 \\char32_t *utf32_1 = U"foo";
2284 , &.{"-O2"});
2285 obj1.linkLibC();
2286
2287 const obj2 = addObject(b, opts, .{ .name = "b.o" });
2288 addCSourceBytes(obj2,
2289 \\#include <stdio.h>
2290 \\#include <assert.h>
2291 \\#include <uchar.h>
2292 \\#include <wchar.h>
2293 \\extern char *cstr1;
2294 \\extern wchar_t *wide1;
2295 \\extern char16_t *utf16_1;
2296 \\extern char32_t *utf32_1;
2297 \\char *cstr2 = "foo";
2298 \\wchar_t *wide2 = L"foo";
2299 \\char16_t *utf16_2 = u"foo";
2300 \\char32_t *utf32_2 = U"foo";
2301 \\int main() {
2302 \\ printf("%p %p %p %p %p %p %p %p\n",
2303 \\ cstr1, cstr2, wide1, wide2, utf16_1, utf16_2, utf32_1, utf32_2);
2304 \\ assert((void*)cstr1 == (void*)cstr2);
2305 \\ assert((void*)wide1 == (void*)wide2);
2306 \\ assert((void*)utf16_1 == (void*)utf16_2);
2307 \\ assert((void*)utf32_1 == (void*)utf32_2);
2308 \\ assert((void*)wide1 == (void*)utf32_1);
2309 \\ assert((void*)cstr1 != (void*)wide1);
2310 \\ assert((void*)cstr1 != (void*)utf32_1);
2311 \\ assert((void*)wide1 != (void*)utf16_1);
2312 \\}
2313 , &.{"-O2"});
2314 obj2.linkLibC();
2315
2316 const exe = addExecutable(b, opts, .{ .name = "main" });
2317 exe.addObject(obj1);
2318 exe.addObject(obj2);
2319 exe.linkLibC();
2320
2321 const run = addRunArtifact(exe);
2322 run.expectExitCode(0);
2323 test_step.dependOn(&run.step);
2324
2325 return test_step;
2326}
2327
2328fn testMergeStrings2(b: *Build, opts: Options) *Step {
2329 const test_step = addTestStep(b, "merge-strings2", opts);
2330
2331 const obj1 = addObject(b, opts, .{ .name = "a.o", .zig_source_bytes =
2332 \\const std = @import("std");
2333 \\export fn foo() void {
2334 \\ var arr: [5:0]u16 = [_:0]u16{ 1, 2, 3, 4, 5 };
2335 \\ const slice = std.mem.sliceTo(&arr, 3);
2336 \\ std.testing.expectEqualSlices(u16, arr[0..2], slice) catch unreachable;
2337 \\}
2338 });
2339
2340 const exe = addExecutable(b, opts, .{ .name = "main", .zig_source_bytes =
2341 \\const std = @import("std");
2342 \\extern fn foo() void;
2343 \\pub fn main() void {
2344 \\ foo();
2345 \\ var arr: [5:0]u16 = [_:0]u16{ 5, 4, 3, 2, 1 };
2346 \\ const slice = std.mem.sliceTo(&arr, 3);
2347 \\ std.testing.expectEqualSlices(u16, arr[0..2], slice) catch unreachable;
2348 \\}
2349 });
2350 exe.addObject(obj1);
2351
2352 const run = addRunArtifact(exe);
2353 run.expectExitCode(0);
2354 test_step.dependOn(&run.step);
2355
2356 const check = exe.checkObject();
2357 check.dumpSection(".rodata.str");
2358 check.checkContains("\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x00\x00");
2359 check.dumpSection(".rodata.str");
2360 check.checkContains("\x05\x00\x04\x00\x03\x00\x02\x00\x01\x00\x00\x00");
2361 test_step.dependOn(&check.step);
2362
2363 return test_step;
2364}
2365
2270fn testNoEhFrameHdr(b: *Build, opts: Options) *Step {2366fn testNoEhFrameHdr(b: *Build, opts: Options) *Step {
2271 const test_step = addTestStep(b, "no-eh-frame-hdr", opts);2367 const test_step = addTestStep(b, "no-eh-frame-hdr", opts);
22722368
test/link/link.zig-18
...@@ -1,21 +1,3 @@...@@ -1,21 +1,3 @@
1pub fn build(b: *Build) void {
2 const test_step = b.step("test-link", "Run link tests");
3 b.default_step = test_step;
4
5 const has_macos_sdk = b.option(bool, "has_macos_sdk", "whether the host provides a macOS SDK in system path");
6 const has_ios_sdk = b.option(bool, "has_ios_sdk", "whether the host provides a iOS SDK in system path");
7 const has_symlinks_windows = b.option(bool, "has_symlinks_windows", "whether the host is windows and has symlinks enabled");
8
9 const build_opts: BuildOptions = .{
10 .has_macos_sdk = has_macos_sdk orelse false,
11 .has_ios_sdk = has_ios_sdk orelse false,
12 .has_symlinks_windows = has_symlinks_windows orelse false,
13 };
14
15 test_step.dependOn(@import("elf.zig").testAll(b, build_opts));
16 test_step.dependOn(@import("macho.zig").testAll(b, build_opts));
17}
18
19pub const BuildOptions = struct {1pub const BuildOptions = struct {
20 has_macos_sdk: bool,2 has_macos_sdk: bool,
21 has_ios_sdk: bool,3 has_ios_sdk: bool,