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 {...@@ -17,16 +17,6 @@ pub fn build(b: *Build) void {
17 .abi = .gnu,17 .abi = .gnu,
18 };18 };
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
30 // Exercise linker with self-hosted backend (no LLVM)20 // Exercise linker with self-hosted backend (no LLVM)
31 // elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false }));21 // elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false }));
3222
...@@ -42,17 +32,20 @@ pub fn build(b: *Build) void {...@@ -42,17 +32,20 @@ pub fn build(b: *Build) void {
42 elf_step.dependOn(testLinkingZig(b, .{ .target = musl_target }));32 elf_step.dependOn(testLinkingZig(b, .{ .target = musl_target }));
43 elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target }));33 elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target }));
4434
45 elf_step.dependOn(testAsNeeded(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));35 elf_step.dependOn(testAsNeeded(b, .{ .target = glibc_target }));
46 elf_step.dependOn(testCanonicalPlt(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));36 // https://github.com/ziglang/zig/issues/17430
47 elf_step.dependOn(testCopyrel(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));37 // elf_step.dependOn(testCanonicalPlt(b, .{ .target = glibc_target }));
48 elf_step.dependOn(testCopyrelAlias(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));38 elf_step.dependOn(testCopyrel(b, .{ .target = glibc_target }));
49 elf_step.dependOn(testCopyrelAlignment(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));39 // https://github.com/ziglang/zig/issues/17430
50 elf_step.dependOn(testDsoPlt(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));40 // elf_step.dependOn(testCopyrelAlias(b, .{ .target = glibc_target }));
51 elf_step.dependOn(testDsoUndef(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));41 // https://github.com/ziglang/zig/issues/17430
52 elf_step.dependOn(testExportDynamic(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));42 // elf_step.dependOn(testCopyrelAlignment(b, .{ .target = glibc_target }));
53 elf_step.dependOn(testExportSymbolsFromExe(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));43 elf_step.dependOn(testDsoPlt(b, .{ .target = glibc_target }));
54 elf_step.dependOn(testLargeAlignmentDso(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));44 elf_step.dependOn(testDsoUndef(b, .{ .target = glibc_target }));
55 elf_step.dependOn(testLargeAlignmentExe(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));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 }));
56}49}
5750
58fn testAbsSymbols(b: *Build, opts: Options) *Step {51fn testAbsSymbols(b: *Build, opts: Options) *Step {
...@@ -110,16 +103,16 @@ fn testAsNeeded(b: *Build, opts: Options) *Step {...@@ -110,16 +103,16 @@ fn testAsNeeded(b: *Build, opts: Options) *Step {
110 main_o.linkLibC();103 main_o.linkLibC();
111104
112 const libfoo = addSharedLibrary(b, "foo", opts);105 const libfoo = addSharedLibrary(b, "foo", opts);
113 addCSourceBytes(libfoo, "int foo() { return 42; }", &.{"-fPIC"});106 addCSourceBytes(libfoo, "int foo() { return 42; }", &.{});
114107
115 const libbar = addSharedLibrary(b, "bar", opts);108 const libbar = addSharedLibrary(b, "bar", opts);
116 addCSourceBytes(libbar, "int bar() { return 42; }", &.{"-fPIC"});109 addCSourceBytes(libbar, "int bar() { return 42; }", &.{});
117110
118 const libbaz = addSharedLibrary(b, "baz", opts);111 const libbaz = addSharedLibrary(b, "baz", opts);
119 addCSourceBytes(libbaz,112 addCSourceBytes(libbaz,
120 \\int foo();113 \\int foo();
121 \\int baz() { return foo(); }114 \\int baz() { return foo(); }
122 , &.{"-fPIC"});115 , &.{});
123116
124 {117 {
125 const exe = addExecutable(b, "test", opts);118 const exe = addExecutable(b, "test", opts);
...@@ -188,7 +181,7 @@ fn testCanonicalPlt(b: *Build, opts: Options) *Step {...@@ -188,7 +181,7 @@ fn testCanonicalPlt(b: *Build, opts: Options) *Step {
188 \\void *bar() {181 \\void *bar() {
189 \\ return bar;182 \\ return bar;
190 \\}183 \\}
191 , &.{"-fPIC"});184 , &.{});
192185
193 const b_o = addObject(b, "obj", opts);186 const b_o = addObject(b, "obj", opts);
194 addCSourceBytes(b_o,187 addCSourceBytes(b_o,
...@@ -196,7 +189,8 @@ fn testCanonicalPlt(b: *Build, opts: Options) *Step {...@@ -196,7 +189,8 @@ fn testCanonicalPlt(b: *Build, opts: Options) *Step {
196 \\void *baz() {189 \\void *baz() {
197 \\ return bar;190 \\ return bar;
198 \\}191 \\}
199 , &.{"-fPIC"});192 , &.{});
193 b_o.force_pic = true;
200194
201 const main_o = addObject(b, "main", opts);195 const main_o = addObject(b, "main", opts);
202 addCSourceBytes(main_o,196 addCSourceBytes(main_o,
...@@ -210,8 +204,9 @@ fn testCanonicalPlt(b: *Build, opts: Options) *Step {...@@ -210,8 +204,9 @@ fn testCanonicalPlt(b: *Build, opts: Options) *Step {
210 \\ assert(bar == baz());204 \\ assert(bar == baz());
211 \\ return 0;205 \\ return 0;
212 \\}206 \\}
213 , &.{"-fno-PIC"});207 , &.{});
214 main_o.linkLibC();208 main_o.linkLibC();
209 main_o.force_pic = false;
215210
216 const exe = addExecutable(b, "main", opts);211 const exe = addExecutable(b, "main", opts);
217 exe.addObject(main_o);212 exe.addObject(main_o);
...@@ -330,7 +325,7 @@ fn testCopyrel(b: *Build, opts: Options) *Step {...@@ -330,7 +325,7 @@ fn testCopyrel(b: *Build, opts: Options) *Step {
330 addCSourceBytes(dso,325 addCSourceBytes(dso,
331 \\int foo = 3;326 \\int foo = 3;
332 \\int bar = 5;327 \\int bar = 5;
333 , &.{"-fPIC"});328 , &.{});
334329
335 const exe = addExecutable(b, "main", opts);330 const exe = addExecutable(b, "main", opts);
336 addCSourceBytes(exe,331 addCSourceBytes(exe,
...@@ -360,7 +355,7 @@ fn testCopyrelAlias(b: *Build, opts: Options) *Step {...@@ -360,7 +355,7 @@ fn testCopyrelAlias(b: *Build, opts: Options) *Step {
360 \\int foo = 42;355 \\int foo = 42;
361 \\extern int bar __attribute__((alias("foo")));356 \\extern int bar __attribute__((alias("foo")));
362 \\extern int baz __attribute__((alias("foo")));357 \\extern int baz __attribute__((alias("foo")));
363 , &.{"-fPIC"});358 , &.{});
364359
365 const exe = addExecutable(b, "main", opts);360 const exe = addExecutable(b, "main", opts);
366 addCSourceBytes(exe,361 addCSourceBytes(exe,
...@@ -371,13 +366,14 @@ fn testCopyrelAlias(b: *Build, opts: Options) *Step {...@@ -371,13 +366,14 @@ fn testCopyrelAlias(b: *Build, opts: Options) *Step {
371 \\ printf("%d %d %d\n", foo, *get_bar(), &foo == get_bar());366 \\ printf("%d %d %d\n", foo, *get_bar(), &foo == get_bar());
372 \\ return 0;367 \\ return 0;
373 \\}368 \\}
374 , &.{"-fno-PIC"});369 , &.{});
375 addCSourceBytes(exe,370 addCSourceBytes(exe,
376 \\extern int bar;371 \\extern int bar;
377 \\int *get_bar() { return &bar; }372 \\int *get_bar() { return &bar; }
378 , &.{"-fno-PIC"});373 , &.{});
379 exe.linkLibrary(dso);374 exe.linkLibrary(dso);
380 exe.linkLibC();375 exe.linkLibC();
376 exe.force_pic = false;
381 exe.pie = false;377 exe.pie = false;
382378
383 const run = addRunArtifact(exe);379 const run = addRunArtifact(exe);
...@@ -391,21 +387,22 @@ fn testCopyrelAlignment(b: *Build, opts: Options) *Step {...@@ -391,21 +387,22 @@ fn testCopyrelAlignment(b: *Build, opts: Options) *Step {
391 const test_step = addTestStep(b, "copyrel-alignment", opts);387 const test_step = addTestStep(b, "copyrel-alignment", opts);
392388
393 const a_so = addSharedLibrary(b, "a", opts);389 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
396 const b_so = addSharedLibrary(b, "b", opts);392 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
399 const c_so = addSharedLibrary(b, "c", opts);395 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
402 const obj = addObject(b, "main", opts);398 const obj = addObject(b, "main", opts);
403 addCSourceBytes(obj,399 addCSourceBytes(obj,
404 \\#include <stdio.h>400 \\#include <stdio.h>
405 \\extern int foo;401 \\extern int foo;
406 \\int main() { printf("%d\n", foo); }402 \\int main() { printf("%d\n", foo); }
407 , &.{"-fno-PIE"});403 , &.{});
408 obj.linkLibC();404 obj.linkLibC();
405 obj.force_pic = false;
409406
410 const exp_stdout = "5\n";407 const exp_stdout = "5\n";
411408
...@@ -485,7 +482,7 @@ fn testDsoPlt(b: *Build, opts: Options) *Step {...@@ -485,7 +482,7 @@ fn testDsoPlt(b: *Build, opts: Options) *Step {
485 \\void hello() {482 \\void hello() {
486 \\ real_hello();483 \\ real_hello();
487 \\}484 \\}
488 , &.{"-fPIC"});485 , &.{});
489 dso.linkLibC();486 dso.linkLibC();
490487
491 const exe = addExecutable(b, "test", opts);488 const exe = addExecutable(b, "test", opts);
...@@ -517,7 +514,7 @@ fn testDsoUndef(b: *Build, opts: Options) *Step {...@@ -517,7 +514,7 @@ fn testDsoUndef(b: *Build, opts: Options) *Step {
517 \\extern int foo;514 \\extern int foo;
518 \\int bar = 5;515 \\int bar = 5;
519 \\int baz() { return foo; }516 \\int baz() { return foo; }
520 , &.{"-fPIC"});517 , &.{});
521 dso.linkLibC();518 dso.linkLibC();
522519
523 const obj = addObject(b, "obj", opts);520 const obj = addObject(b, "obj", opts);
...@@ -628,7 +625,7 @@ fn testExportDynamic(b: *Build, opts: Options) *Step {...@@ -628,7 +625,7 @@ fn testExportDynamic(b: *Build, opts: Options) *Step {
628 );625 );
629626
630 const dso = addSharedLibrary(b, "a", opts);627 const dso = addSharedLibrary(b, "a", opts);
631 addCSourceBytes(dso, "int baz = 10;", &.{"-fPIC"});628 addCSourceBytes(dso, "int baz = 10;", &.{});
632629
633 const exe = addExecutable(b, "main", opts);630 const exe = addExecutable(b, "main", opts);
634 addCSourceBytes(exe,631 addCSourceBytes(exe,
...@@ -662,7 +659,7 @@ fn testExportSymbolsFromExe(b: *Build, opts: Options) *Step {...@@ -662,7 +659,7 @@ fn testExportSymbolsFromExe(b: *Build, opts: Options) *Step {
662 \\void foo() {659 \\void foo() {
663 \\ expfn1();660 \\ expfn1();
664 \\}661 \\}
665 , &.{"-fPIC"});662 , &.{});
666663
667 const exe = addExecutable(b, "main", opts);664 const exe = addExecutable(b, "main", opts);
668 addCSourceBytes(exe,665 addCSourceBytes(exe,
...@@ -798,7 +795,7 @@ fn testLargeAlignmentDso(b: *Build, opts: Options) *Step {...@@ -798,7 +795,7 @@ fn testLargeAlignmentDso(b: *Build, opts: Options) *Step {
798 \\ hello();795 \\ hello();
799 \\ world();796 \\ world();
800 \\}797 \\}
801 , &.{"-fPIC"});798 , &.{});
802 dso.link_function_sections = true;799 dso.link_function_sections = true;
803 dso.linkLibC();800 dso.linkLibC();
804801
...@@ -989,7 +986,6 @@ const Options = struct {...@@ -989,7 +986,6 @@ const Options = struct {
989 target: CrossTarget = .{ .cpu_arch = .x86_64, .os_tag = .linux },986 target: CrossTarget = .{ .cpu_arch = .x86_64, .os_tag = .linux },
990 optimize: std.builtin.OptimizeMode = .Debug,987 optimize: std.builtin.OptimizeMode = .Debug,
991 use_llvm: bool = true,988 use_llvm: bool = true,
992 dynamic_linker: ?[]const u8 = null,
993};989};
994990
995fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {991fn 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 {...@@ -1005,15 +1001,13 @@ fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {
1005}1001}
10061002
1007fn addExecutable(b: *Build, name: []const u8, opts: Options) *Compile {1003fn addExecutable(b: *Build, name: []const u8, opts: Options) *Compile {
1008 const exe = b.addExecutable(.{1004 return b.addExecutable(.{
1009 .name = name,1005 .name = name,
1010 .target = opts.target,1006 .target = opts.target,
1011 .optimize = opts.optimize,1007 .optimize = opts.optimize,
1012 .use_llvm = opts.use_llvm,1008 .use_llvm = opts.use_llvm,
1013 .use_lld = false,1009 .use_lld = false,
1014 });1010 });
1015 exe.link_dynamic_linker = opts.dynamic_linker;
1016 return exe;
1017}1011}
10181012
1019fn addObject(b: *Build, name: []const u8, opts: Options) *Compile {1013fn addObject(b: *Build, name: []const u8, opts: Options) *Compile {
...@@ -1037,15 +1031,13 @@ fn addStaticLibrary(b: *Build, name: []const u8, opts: Options) *Compile {...@@ -1037,15 +1031,13 @@ fn addStaticLibrary(b: *Build, name: []const u8, opts: Options) *Compile {
1037}1031}
10381032
1039fn addSharedLibrary(b: *Build, name: []const u8, opts: Options) *Compile {1033fn addSharedLibrary(b: *Build, name: []const u8, opts: Options) *Compile {
1040 const dso = b.addSharedLibrary(.{1034 return b.addSharedLibrary(.{
1041 .name = name,1035 .name = name,
1042 .target = opts.target,1036 .target = opts.target,
1043 .optimize = opts.optimize,1037 .optimize = opts.optimize,
1044 .use_llvm = opts.use_llvm,1038 .use_llvm = opts.use_llvm,
1045 .use_lld = false,1039 .use_lld = false,
1046 });1040 });
1047 dso.link_dynamic_linker = opts.dynamic_linker;
1048 return dso;
1049}1041}
10501042
1051fn addRunArtifact(comp: *Compile) *Run {1043fn addRunArtifact(comp: *Compile) *Run {