authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-06 00:13:19+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:04+02:00
logcf2c8c07897a229eb6081d27856b6d2d47ab9eca
tree24a9913649c30bc7217b551ea8dd478c8fad56a0
parentdef7190e845eab3e0bf0f746129b737c8000b2b8

elf: test common symbols handling


1 files changed, 38 insertions(+), 10 deletions(-)

test/link/elf.zig+38-10
...@@ -16,6 +16,7 @@ pub fn build(b: *Build) void {...@@ -16,6 +16,7 @@ pub fn build(b: *Build) void {
16 // elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false }));16 // elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false }));
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(testEmptyObject(b, .{ .target = musl_target }));20 elf_step.dependOn(testEmptyObject(b, .{ .target = musl_target }));
20 elf_step.dependOn(testGcSections(b, .{ .target = musl_target }));21 elf_step.dependOn(testGcSections(b, .{ .target = musl_target }));
21 elf_step.dependOn(testLinkingC(b, .{ .target = musl_target }));22 elf_step.dependOn(testLinkingC(b, .{ .target = musl_target }));
...@@ -24,12 +25,39 @@ pub fn build(b: *Build) void {...@@ -24,12 +25,39 @@ pub fn build(b: *Build) void {
24 elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target }));25 elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target }));
25}26}
2627
28fn testCommonSymbols(b: *Build, opts: Options) *Step {
29 const test_step = addTestStep(b, "common-symbols", opts);
30
31 const exe = addExecutable(b, opts);
32 addCSourceBytes(exe,
33 \\int foo;
34 \\int bar;
35 \\int baz = 42;
36 , &.{"-fcommon"});
37 addCSourceBytes(exe,
38 \\#include<stdio.h>
39 \\int foo;
40 \\int bar = 5;
41 \\int baz;
42 \\int main() {
43 \\ printf("%d %d %d\n", foo, bar, baz);
44 \\}
45 , &.{"-fcommon"});
46 exe.is_linking_libc = true;
47
48 const run = addRunArtifact(exe);
49 run.expectStdOutEqual("0 5 42\n");
50 test_step.dependOn(&run.step);
51
52 return test_step;
53}
54
27fn testEmptyObject(b: *Build, opts: Options) *Step {55fn testEmptyObject(b: *Build, opts: Options) *Step {
28 const test_step = addTestStep(b, "empty-object", opts);56 const test_step = addTestStep(b, "empty-object", opts);
2957
30 const exe = addExecutable(b, opts);58 const exe = addExecutable(b, opts);
31 addCSourceBytes(exe, "int main() { return 0; }");59 addCSourceBytes(exe, "int main() { return 0; }", &.{});
32 addCSourceBytes(exe, "");60 addCSourceBytes(exe, "", &.{});
33 exe.is_linking_libc = true;61 exe.is_linking_libc = true;
3462
35 const run = addRunArtifact(exe);63 const run = addRunArtifact(exe);
...@@ -58,7 +86,7 @@ fn testGcSections(b: *Build, opts: Options) *Step {...@@ -58,7 +86,7 @@ fn testGcSections(b: *Build, opts: Options) *Step {
58 \\ printf("%d %d\n", live_var1, live_var2);86 \\ printf("%d %d\n", live_var1, live_var2);
59 \\ live_fn2();87 \\ live_fn2();
60 \\}88 \\}
61 );89 , &.{});
62 obj.link_function_sections = true;90 obj.link_function_sections = true;
63 obj.link_data_sections = true;91 obj.link_data_sections = true;
64 obj.is_linking_libc = true;92 obj.is_linking_libc = true;
...@@ -139,7 +167,7 @@ fn testLinkingC(b: *Build, opts: Options) *Step {...@@ -139,7 +167,7 @@ fn testLinkingC(b: *Build, opts: Options) *Step {
139 \\ printf("Hello World!\n");167 \\ printf("Hello World!\n");
140 \\ return 0;168 \\ return 0;
141 \\}169 \\}
142 );170 , &.{});
143 exe.is_linking_libc = true;171 exe.is_linking_libc = true;
144172
145 const run = addRunArtifact(exe);173 const run = addRunArtifact(exe);
...@@ -168,7 +196,7 @@ fn testLinkingCpp(b: *Build, opts: Options) *Step {...@@ -168,7 +196,7 @@ fn testLinkingCpp(b: *Build, opts: Options) *Step {
168 \\ std::cout << "Hello World!" << std::endl;196 \\ std::cout << "Hello World!" << std::endl;
169 \\ return 0;197 \\ return 0;
170 \\}198 \\}
171 );199 , &.{});
172 exe.is_linking_libc = true;200 exe.is_linking_libc = true;
173 exe.is_linking_libcpp = true;201 exe.is_linking_libcpp = true;
174202
...@@ -231,7 +259,7 @@ fn testTlsStatic(b: *Build, opts: Options) *Step {...@@ -231,7 +259,7 @@ fn testTlsStatic(b: *Build, opts: Options) *Step {
231 \\ printf("%d %d %c\n", a, b, c);259 \\ printf("%d %d %c\n", a, b, c);
232 \\ return 0;260 \\ return 0;
233 \\}261 \\}
234 );262 , &.{});
235 exe.is_linking_libc = true;263 exe.is_linking_libc = true;
236264
237 const run = addRunArtifact(exe);265 const run = addRunArtifact(exe);
...@@ -297,16 +325,16 @@ fn addZigSourceBytes(comp: *Compile, comptime bytes: []const u8) void {...@@ -297,16 +325,16 @@ fn addZigSourceBytes(comp: *Compile, comptime bytes: []const u8) void {
297 comp.root_src = file;325 comp.root_src = file;
298}326}
299327
300fn addCSourceBytes(comp: *Compile, comptime bytes: []const u8) void {328fn addCSourceBytes(comp: *Compile, comptime bytes: []const u8, flags: []const []const u8) void {
301 const b = comp.step.owner;329 const b = comp.step.owner;
302 const file = WriteFile.create(b).add("a.c", bytes);330 const file = WriteFile.create(b).add("a.c", bytes);
303 comp.addCSourceFile(.{ .file = file, .flags = &.{} });331 comp.addCSourceFile(.{ .file = file, .flags = flags });
304}332}
305333
306fn addCppSourceBytes(comp: *Compile, comptime bytes: []const u8) void {334fn addCppSourceBytes(comp: *Compile, comptime bytes: []const u8, flags: []const []const u8) void {
307 const b = comp.step.owner;335 const b = comp.step.owner;
308 const file = WriteFile.create(b).add("a.cpp", bytes);336 const file = WriteFile.create(b).add("a.cpp", bytes);
309 comp.addCSourceFile(.{ .file = file, .flags = &.{} });337 comp.addCSourceFile(.{ .file = file, .flags = flags });
310}338}
311339
312fn addAsmSourceBytes(comp: *Compile, comptime bytes: []const u8) void {340fn addAsmSourceBytes(comp: *Compile, comptime bytes: []const u8) void {