authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-09 11:40:13+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:05+02:00
log44f30858515935a8b02c23e8d6663ac9a090e251
tree25909cbad47a70c2b74b4dd905414f19a5c6b3d6
parentdf656c4eefa81f689d831deb17c17b08b8041627

elf: refactor tests and disable currently impossible combinations


1 files changed, 39 insertions(+), 47 deletions(-)

test/link/elf.zig+39-47
......@@ -17,16 +17,6 @@ pub fn build(b: *Build) void {
1717 .abi = .gnu,
1818 };
1919
20 var dynamic_linker: ?[]const u8 = null;
21 if (std.zig.system.NativeTargetInfo.detect(.{})) |host| blk: {
22 if (host.target.cpu.arch != glibc_target.cpu_arch.? or
23 host.target.os.tag != glibc_target.os_tag.? or
24 host.target.abi != glibc_target.abi.?) break :blk;
25 if (host.dynamic_linker.get()) |path| {
26 dynamic_linker = b.dupe(path);
27 }
28 } else |_| {}
29
3020 // Exercise linker with self-hosted backend (no LLVM)
3121 // elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false }));
3222
......@@ -42,17 +32,20 @@ pub fn build(b: *Build) void {
4232 elf_step.dependOn(testLinkingZig(b, .{ .target = musl_target }));
4333 elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target }));
4434
45 elf_step.dependOn(testAsNeeded(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));
46 elf_step.dependOn(testCanonicalPlt(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));
47 elf_step.dependOn(testCopyrel(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));
48 elf_step.dependOn(testCopyrelAlias(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));
49 elf_step.dependOn(testCopyrelAlignment(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));
50 elf_step.dependOn(testDsoPlt(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));
51 elf_step.dependOn(testDsoUndef(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));
52 elf_step.dependOn(testExportDynamic(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));
53 elf_step.dependOn(testExportSymbolsFromExe(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));
54 elf_step.dependOn(testLargeAlignmentDso(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));
55 elf_step.dependOn(testLargeAlignmentExe(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));
35 elf_step.dependOn(testAsNeeded(b, .{ .target = glibc_target }));
36 // https://github.com/ziglang/zig/issues/17430
37 // elf_step.dependOn(testCanonicalPlt(b, .{ .target = glibc_target }));
38 elf_step.dependOn(testCopyrel(b, .{ .target = glibc_target }));
39 // https://github.com/ziglang/zig/issues/17430
40 // elf_step.dependOn(testCopyrelAlias(b, .{ .target = glibc_target }));
41 // https://github.com/ziglang/zig/issues/17430
42 // elf_step.dependOn(testCopyrelAlignment(b, .{ .target = glibc_target }));
43 elf_step.dependOn(testDsoPlt(b, .{ .target = glibc_target }));
44 elf_step.dependOn(testDsoUndef(b, .{ .target = glibc_target }));
45 elf_step.dependOn(testExportDynamic(b, .{ .target = glibc_target }));
46 elf_step.dependOn(testExportSymbolsFromExe(b, .{ .target = glibc_target }));
47 elf_step.dependOn(testLargeAlignmentDso(b, .{ .target = glibc_target }));
48 elf_step.dependOn(testLargeAlignmentExe(b, .{ .target = glibc_target }));
5649}
5750
5851fn testAbsSymbols(b: *Build, opts: Options) *Step {
......@@ -110,16 +103,16 @@ fn testAsNeeded(b: *Build, opts: Options) *Step {
110103 main_o.linkLibC();
111104
112105 const libfoo = addSharedLibrary(b, "foo", opts);
113 addCSourceBytes(libfoo, "int foo() { return 42; }", &.{"-fPIC"});
106 addCSourceBytes(libfoo, "int foo() { return 42; }", &.{});
114107
115108 const libbar = addSharedLibrary(b, "bar", opts);
116 addCSourceBytes(libbar, "int bar() { return 42; }", &.{"-fPIC"});
109 addCSourceBytes(libbar, "int bar() { return 42; }", &.{});
117110
118111 const libbaz = addSharedLibrary(b, "baz", opts);
119112 addCSourceBytes(libbaz,
120113 \\int foo();
121114 \\int baz() { return foo(); }
122 , &.{"-fPIC"});
115 , &.{});
123116
124117 {
125118 const exe = addExecutable(b, "test", opts);
......@@ -188,7 +181,7 @@ fn testCanonicalPlt(b: *Build, opts: Options) *Step {
188181 \\void *bar() {
189182 \\ return bar;
190183 \\}
191 , &.{"-fPIC"});
184 , &.{});
192185
193186 const b_o = addObject(b, "obj", opts);
194187 addCSourceBytes(b_o,
......@@ -196,7 +189,8 @@ fn testCanonicalPlt(b: *Build, opts: Options) *Step {
196189 \\void *baz() {
197190 \\ return bar;
198191 \\}
199 , &.{"-fPIC"});
192 , &.{});
193 b_o.force_pic = true;
200194
201195 const main_o = addObject(b, "main", opts);
202196 addCSourceBytes(main_o,
......@@ -210,8 +204,9 @@ fn testCanonicalPlt(b: *Build, opts: Options) *Step {
210204 \\ assert(bar == baz());
211205 \\ return 0;
212206 \\}
213 , &.{"-fno-PIC"});
207 , &.{});
214208 main_o.linkLibC();
209 main_o.force_pic = false;
215210
216211 const exe = addExecutable(b, "main", opts);
217212 exe.addObject(main_o);
......@@ -330,7 +325,7 @@ fn testCopyrel(b: *Build, opts: Options) *Step {
330325 addCSourceBytes(dso,
331326 \\int foo = 3;
332327 \\int bar = 5;
333 , &.{"-fPIC"});
328 , &.{});
334329
335330 const exe = addExecutable(b, "main", opts);
336331 addCSourceBytes(exe,
......@@ -360,7 +355,7 @@ fn testCopyrelAlias(b: *Build, opts: Options) *Step {
360355 \\int foo = 42;
361356 \\extern int bar __attribute__((alias("foo")));
362357 \\extern int baz __attribute__((alias("foo")));
363 , &.{"-fPIC"});
358 , &.{});
364359
365360 const exe = addExecutable(b, "main", opts);
366361 addCSourceBytes(exe,
......@@ -371,13 +366,14 @@ fn testCopyrelAlias(b: *Build, opts: Options) *Step {
371366 \\ printf("%d %d %d\n", foo, *get_bar(), &foo == get_bar());
372367 \\ return 0;
373368 \\}
374 , &.{"-fno-PIC"});
369 , &.{});
375370 addCSourceBytes(exe,
376371 \\extern int bar;
377372 \\int *get_bar() { return &bar; }
378 , &.{"-fno-PIC"});
373 , &.{});
379374 exe.linkLibrary(dso);
380375 exe.linkLibC();
376 exe.force_pic = false;
381377 exe.pie = false;
382378
383379 const run = addRunArtifact(exe);
......@@ -391,21 +387,22 @@ fn testCopyrelAlignment(b: *Build, opts: Options) *Step {
391387 const test_step = addTestStep(b, "copyrel-alignment", opts);
392388
393389 const a_so = addSharedLibrary(b, "a", opts);
394 addCSourceBytes(a_so, "__attribute__((aligned(32))) int foo = 5;", &.{"-fPIC"});
390 addCSourceBytes(a_so, "__attribute__((aligned(32))) int foo = 5;", &.{});
395391
396392 const b_so = addSharedLibrary(b, "b", opts);
397 addCSourceBytes(b_so, "__attribute__((aligned(8))) int foo = 5;", &.{"-fPIC"});
393 addCSourceBytes(b_so, "__attribute__((aligned(8))) int foo = 5;", &.{});
398394
399395 const c_so = addSharedLibrary(b, "c", opts);
400 addCSourceBytes(c_so, "__attribute__((aligned(256))) int foo = 5;", &.{"-fPIC"});
396 addCSourceBytes(c_so, "__attribute__((aligned(256))) int foo = 5;", &.{});
401397
402398 const obj = addObject(b, "main", opts);
403399 addCSourceBytes(obj,
404400 \\#include <stdio.h>
405401 \\extern int foo;
406402 \\int main() { printf("%d\n", foo); }
407 , &.{"-fno-PIE"});
403 , &.{});
408404 obj.linkLibC();
405 obj.force_pic = false;
409406
410407 const exp_stdout = "5\n";
411408
......@@ -485,7 +482,7 @@ fn testDsoPlt(b: *Build, opts: Options) *Step {
485482 \\void hello() {
486483 \\ real_hello();
487484 \\}
488 , &.{"-fPIC"});
485 , &.{});
489486 dso.linkLibC();
490487
491488 const exe = addExecutable(b, "test", opts);
......@@ -517,7 +514,7 @@ fn testDsoUndef(b: *Build, opts: Options) *Step {
517514 \\extern int foo;
518515 \\int bar = 5;
519516 \\int baz() { return foo; }
520 , &.{"-fPIC"});
517 , &.{});
521518 dso.linkLibC();
522519
523520 const obj = addObject(b, "obj", opts);
......@@ -628,7 +625,7 @@ fn testExportDynamic(b: *Build, opts: Options) *Step {
628625 );
629626
630627 const dso = addSharedLibrary(b, "a", opts);
631 addCSourceBytes(dso, "int baz = 10;", &.{"-fPIC"});
628 addCSourceBytes(dso, "int baz = 10;", &.{});
632629
633630 const exe = addExecutable(b, "main", opts);
634631 addCSourceBytes(exe,
......@@ -662,7 +659,7 @@ fn testExportSymbolsFromExe(b: *Build, opts: Options) *Step {
662659 \\void foo() {
663660 \\ expfn1();
664661 \\}
665 , &.{"-fPIC"});
662 , &.{});
666663
667664 const exe = addExecutable(b, "main", opts);
668665 addCSourceBytes(exe,
......@@ -798,7 +795,7 @@ fn testLargeAlignmentDso(b: *Build, opts: Options) *Step {
798795 \\ hello();
799796 \\ world();
800797 \\}
801 , &.{"-fPIC"});
798 , &.{});
802799 dso.link_function_sections = true;
803800 dso.linkLibC();
804801
......@@ -989,7 +986,6 @@ const Options = struct {
989986 target: CrossTarget = .{ .cpu_arch = .x86_64, .os_tag = .linux },
990987 optimize: std.builtin.OptimizeMode = .Debug,
991988 use_llvm: bool = true,
992 dynamic_linker: ?[]const u8 = null,
993989};
994990
995991fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {
......@@ -1005,15 +1001,13 @@ fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {
10051001}
10061002
10071003fn addExecutable(b: *Build, name: []const u8, opts: Options) *Compile {
1008 const exe = b.addExecutable(.{
1004 return b.addExecutable(.{
10091005 .name = name,
10101006 .target = opts.target,
10111007 .optimize = opts.optimize,
10121008 .use_llvm = opts.use_llvm,
10131009 .use_lld = false,
10141010 });
1015 exe.link_dynamic_linker = opts.dynamic_linker;
1016 return exe;
10171011}
10181012
10191013fn addObject(b: *Build, name: []const u8, opts: Options) *Compile {
......@@ -1037,15 +1031,13 @@ fn addStaticLibrary(b: *Build, name: []const u8, opts: Options) *Compile {
10371031}
10381032
10391033fn addSharedLibrary(b: *Build, name: []const u8, opts: Options) *Compile {
1040 const dso = b.addSharedLibrary(.{
1034 return b.addSharedLibrary(.{
10411035 .name = name,
10421036 .target = opts.target,
10431037 .optimize = opts.optimize,
10441038 .use_llvm = opts.use_llvm,
10451039 .use_lld = false,
10461040 });
1047 dso.link_dynamic_linker = opts.dynamic_linker;
1048 return dso;
10491041}
10501042
10511043fn addRunArtifact(comp: *Compile) *Run {