authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-06 00:30:20+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:04+02:00
logac03a35e8201f0b1cc5f71699c29820317534914
tree97aa29447cb1ce0b427c0ef68b915348d279d7bc
parentcf2c8c07897a229eb6081d27856b6d2d47ab9eca

elf: test common symbols in archives


1 files changed, 80 insertions(+), 0 deletions(-)

test/link/elf.zig+80
...@@ -17,6 +17,7 @@ pub fn build(b: *Build) void {...@@ -17,6 +17,7 @@ pub fn build(b: *Build) void {
1717
18 // Exercise linker with LLVM backend18 // Exercise linker with LLVM backend
19 elf_step.dependOn(testCommonSymbols(b, .{ .target = musl_target }));19 elf_step.dependOn(testCommonSymbols(b, .{ .target = musl_target }));
20 elf_step.dependOn(testCommonSymbolsInArchive(b, .{ .target = musl_target }));
20 elf_step.dependOn(testEmptyObject(b, .{ .target = musl_target }));21 elf_step.dependOn(testEmptyObject(b, .{ .target = musl_target }));
21 elf_step.dependOn(testGcSections(b, .{ .target = musl_target }));22 elf_step.dependOn(testGcSections(b, .{ .target = musl_target }));
22 elf_step.dependOn(testLinkingC(b, .{ .target = musl_target }));23 elf_step.dependOn(testLinkingC(b, .{ .target = musl_target }));
...@@ -52,6 +53,75 @@ fn testCommonSymbols(b: *Build, opts: Options) *Step {...@@ -52,6 +53,75 @@ fn testCommonSymbols(b: *Build, opts: Options) *Step {
52 return test_step;53 return test_step;
53}54}
5455
56fn testCommonSymbolsInArchive(b: *Build, opts: Options) *Step {
57 const test_step = addTestStep(b, "common-symbols-in-archive", opts);
58
59 const a_o = addObject(b, opts);
60 addCSourceBytes(a_o,
61 \\#include <stdio.h>
62 \\int foo;
63 \\int bar;
64 \\extern int baz;
65 \\__attribute__((weak)) int two();
66 \\int main() {
67 \\ printf("%d %d %d %d\n", foo, bar, baz, two ? two() : -1);
68 \\}
69 , &.{"-fcommon"});
70 a_o.is_linking_libc = true;
71
72 const b_o = addObject(b, opts);
73 addCSourceBytes(b_o, "int foo = 5;", &.{"-fcommon"});
74
75 {
76 const c_o = addObject(b, opts);
77 addCSourceBytes(c_o,
78 \\int bar;
79 \\int two() { return 2; }
80 , &.{"-fcommon"});
81
82 const d_o = addObject(b, opts);
83 addCSourceBytes(d_o, "int baz;", &.{"-fcommon"});
84
85 const lib = addStaticLibrary(b, opts);
86 lib.addObject(b_o);
87 lib.addObject(c_o);
88 lib.addObject(d_o);
89
90 const exe = addExecutable(b, opts);
91 exe.addObject(a_o);
92 exe.linkLibrary(lib);
93 exe.is_linking_libc = true;
94
95 const run = addRunArtifact(exe);
96 run.expectStdOutEqual("5 0 0 -1\n");
97 test_step.dependOn(&run.step);
98 }
99
100 {
101 const e_o = addObject(b, opts);
102 addCSourceBytes(e_o,
103 \\int bar = 0;
104 \\int baz = 7;
105 \\int two() { return 2; }
106 , &.{"-fcommon"});
107
108 const lib = addStaticLibrary(b, opts);
109 lib.addObject(b_o);
110 lib.addObject(e_o);
111
112 const exe = addExecutable(b, opts);
113 exe.addObject(a_o);
114 exe.linkLibrary(lib);
115 exe.is_linking_libc = true;
116
117 const run = addRunArtifact(exe);
118 run.expectStdOutEqual("5 0 7 2\n");
119 test_step.dependOn(&run.step);
120 }
121
122 return test_step;
123}
124
55fn testEmptyObject(b: *Build, opts: Options) *Step {125fn testEmptyObject(b: *Build, opts: Options) *Step {
56 const test_step = addTestStep(b, "empty-object", opts);126 const test_step = addTestStep(b, "empty-object", opts);
57127
...@@ -311,6 +381,16 @@ fn addObject(b: *Build, opts: Options) *Compile {...@@ -311,6 +381,16 @@ fn addObject(b: *Build, opts: Options) *Compile {
311 });381 });
312}382}
313383
384fn addStaticLibrary(b: *Build, opts: Options) *Compile {
385 return b.addStaticLibrary(.{
386 .name = "a.a",
387 .target = opts.target,
388 .optimize = opts.optimize,
389 .use_llvm = opts.use_llvm,
390 .use_lld = true,
391 });
392}
393
314fn addRunArtifact(comp: *Compile) *Run {394fn addRunArtifact(comp: *Compile) *Run {
315 const b = comp.step.owner;395 const b = comp.step.owner;
316 const run = b.addRunArtifact(comp);396 const run = b.addRunArtifact(comp);