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 {
1616 // elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false }));
1717
1818 // Exercise linker with LLVM backend
19 elf_step.dependOn(testCommonSymbols(b, .{ .target = musl_target }));
1920 elf_step.dependOn(testEmptyObject(b, .{ .target = musl_target }));
2021 elf_step.dependOn(testGcSections(b, .{ .target = musl_target }));
2122 elf_step.dependOn(testLinkingC(b, .{ .target = musl_target }));
......@@ -24,12 +25,39 @@ pub fn build(b: *Build) void {
2425 elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target }));
2526}
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
2755fn testEmptyObject(b: *Build, opts: Options) *Step {
2856 const test_step = addTestStep(b, "empty-object", opts);
2957
3058 const exe = addExecutable(b, opts);
31 addCSourceBytes(exe, "int main() { return 0; }");
32 addCSourceBytes(exe, "");
59 addCSourceBytes(exe, "int main() { return 0; }", &.{});
60 addCSourceBytes(exe, "", &.{});
3361 exe.is_linking_libc = true;
3462
3563 const run = addRunArtifact(exe);
......@@ -58,7 +86,7 @@ fn testGcSections(b: *Build, opts: Options) *Step {
5886 \\ printf("%d %d\n", live_var1, live_var2);
5987 \\ live_fn2();
6088 \\}
61 );
89 , &.{});
6290 obj.link_function_sections = true;
6391 obj.link_data_sections = true;
6492 obj.is_linking_libc = true;
......@@ -139,7 +167,7 @@ fn testLinkingC(b: *Build, opts: Options) *Step {
139167 \\ printf("Hello World!\n");
140168 \\ return 0;
141169 \\}
142 );
170 , &.{});
143171 exe.is_linking_libc = true;
144172
145173 const run = addRunArtifact(exe);
......@@ -168,7 +196,7 @@ fn testLinkingCpp(b: *Build, opts: Options) *Step {
168196 \\ std::cout << "Hello World!" << std::endl;
169197 \\ return 0;
170198 \\}
171 );
199 , &.{});
172200 exe.is_linking_libc = true;
173201 exe.is_linking_libcpp = true;
174202
......@@ -231,7 +259,7 @@ fn testTlsStatic(b: *Build, opts: Options) *Step {
231259 \\ printf("%d %d %c\n", a, b, c);
232260 \\ return 0;
233261 \\}
234 );
262 , &.{});
235263 exe.is_linking_libc = true;
236264
237265 const run = addRunArtifact(exe);
......@@ -297,16 +325,16 @@ fn addZigSourceBytes(comp: *Compile, comptime bytes: []const u8) void {
297325 comp.root_src = file;
298326}
299327
300fn addCSourceBytes(comp: *Compile, comptime bytes: []const u8) void {
328fn addCSourceBytes(comp: *Compile, comptime bytes: []const u8, flags: []const []const u8) void {
301329 const b = comp.step.owner;
302330 const file = WriteFile.create(b).add("a.c", bytes);
303 comp.addCSourceFile(.{ .file = file, .flags = &.{} });
331 comp.addCSourceFile(.{ .file = file, .flags = flags });
304332}
305333
306fn addCppSourceBytes(comp: *Compile, comptime bytes: []const u8) void {
334fn addCppSourceBytes(comp: *Compile, comptime bytes: []const u8, flags: []const []const u8) void {
307335 const b = comp.step.owner;
308336 const file = WriteFile.create(b).add("a.cpp", bytes);
309 comp.addCSourceFile(.{ .file = file, .flags = &.{} });
337 comp.addCSourceFile(.{ .file = file, .flags = flags });
310338}
311339
312340fn addAsmSourceBytes(comp: *Compile, comptime bytes: []const u8) void {