authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-07 00:08:50+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:05+02:00
log149dd82d5465abc136d9ef2f4270ed041a3b6e3e
tree4455233c8efc0900b35e586a17ba06bdcc243207
parent7ff9461b88678d4bc02ee31fc5159f57a1d7e2b2

elf: add --as-needed test


1 files changed, 118 insertions(+), 35 deletions(-)

test/link/elf.zig+118-35
......@@ -40,6 +40,7 @@ pub fn build(b: *Build) void {
4040 elf_step.dependOn(testLinkingZig(b, .{ .target = musl_target }));
4141 elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target }));
4242
43 elf_step.dependOn(testAsNeeded(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));
4344 elf_step.dependOn(testDsoPlt(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));
4445 elf_step.dependOn(testDsoUndef(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));
4546 elf_step.dependOn(testLargeAlignmentDso(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker }));
......@@ -49,10 +50,92 @@ pub fn build(b: *Build) void {
4950 }
5051}
5152
53fn testAsNeeded(b: *Build, opts: Options) *Step {
54 const test_step = addTestStep(b, "as-needed", opts);
55
56 const main_o = addObject(b, "main", opts);
57 addCSourceBytes(main_o,
58 \\#include <stdio.h>
59 \\int baz();
60 \\int main() {
61 \\ printf("%d\n", baz());
62 \\ return 0;
63 \\}
64 , &.{});
65 main_o.is_linking_libc = true;
66
67 const libfoo = addSharedLibrary(b, "foo", opts);
68 addCSourceBytes(libfoo, "int foo() { return 42; }", &.{"-fPIC"});
69
70 const libbar = addSharedLibrary(b, "bar", opts);
71 addCSourceBytes(libbar, "int bar() { return 42; }", &.{"-fPIC"});
72
73 const libbaz = addSharedLibrary(b, "baz", opts);
74 addCSourceBytes(libbaz,
75 \\int foo();
76 \\int baz() { return foo(); }
77 , &.{"-fPIC"});
78
79 {
80 const exe = addExecutable(b, "test", opts);
81 exe.addObject(main_o);
82 exe.linkSystemLibrary2("foo", .{ .needed = true });
83 exe.addLibraryPath(libfoo.getEmittedBinDirectory());
84 exe.addRPath(libfoo.getEmittedBinDirectory());
85 exe.linkSystemLibrary2("bar", .{ .needed = true });
86 exe.addLibraryPath(libbar.getEmittedBinDirectory());
87 exe.addRPath(libbar.getEmittedBinDirectory());
88 exe.linkSystemLibrary2("baz", .{ .needed = true });
89 exe.addLibraryPath(libbaz.getEmittedBinDirectory());
90 exe.addRPath(libbaz.getEmittedBinDirectory());
91 exe.is_linking_libc = true;
92
93 const run = addRunArtifact(exe);
94 run.expectStdOutEqual("42\n");
95 test_step.dependOn(&run.step);
96
97 const check = exe.checkObject();
98 check.checkInDynamicSection();
99 check.checkExact("NEEDED libfoo.so");
100 check.checkExact("NEEDED libbar.so");
101 check.checkExact("NEEDED libbaz.so");
102 test_step.dependOn(&check.step);
103 }
104
105 {
106 const exe = addExecutable(b, "test", opts);
107 exe.addObject(main_o);
108 exe.linkSystemLibrary2("foo", .{ .needed = false });
109 exe.addLibraryPath(libfoo.getEmittedBinDirectory());
110 exe.addRPath(libfoo.getEmittedBinDirectory());
111 exe.linkSystemLibrary2("bar", .{ .needed = false });
112 exe.addLibraryPath(libbar.getEmittedBinDirectory());
113 exe.addRPath(libbar.getEmittedBinDirectory());
114 exe.linkSystemLibrary2("baz", .{ .needed = false });
115 exe.addLibraryPath(libbaz.getEmittedBinDirectory());
116 exe.addRPath(libbaz.getEmittedBinDirectory());
117 exe.is_linking_libc = true;
118
119 const run = addRunArtifact(exe);
120 run.expectStdOutEqual("42\n");
121 test_step.dependOn(&run.step);
122
123 const check = exe.checkObject();
124 check.checkInDynamicSection();
125 check.checkNotPresent("NEEDED libbar.so");
126 check.checkInDynamicSection();
127 check.checkExact("NEEDED libfoo.so");
128 check.checkExact("NEEDED libbaz.so");
129 test_step.dependOn(&check.step);
130 }
131
132 return test_step;
133}
134
52135fn testCommonSymbols(b: *Build, opts: Options) *Step {
53136 const test_step = addTestStep(b, "common-symbols", opts);
54137
55 const exe = addExecutable(b, opts);
138 const exe = addExecutable(b, "test", opts);
56139 addCSourceBytes(exe,
57140 \\int foo;
58141 \\int bar;
......@@ -79,7 +162,7 @@ fn testCommonSymbols(b: *Build, opts: Options) *Step {
79162fn testCommonSymbolsInArchive(b: *Build, opts: Options) *Step {
80163 const test_step = addTestStep(b, "common-symbols-in-archive", opts);
81164
82 const a_o = addObject(b, opts);
165 const a_o = addObject(b, "a", opts);
83166 addCSourceBytes(a_o,
84167 \\#include <stdio.h>
85168 \\int foo;
......@@ -92,25 +175,25 @@ fn testCommonSymbolsInArchive(b: *Build, opts: Options) *Step {
92175 , &.{"-fcommon"});
93176 a_o.is_linking_libc = true;
94177
95 const b_o = addObject(b, opts);
178 const b_o = addObject(b, "b", opts);
96179 addCSourceBytes(b_o, "int foo = 5;", &.{"-fcommon"});
97180
98181 {
99 const c_o = addObject(b, opts);
182 const c_o = addObject(b, "c", opts);
100183 addCSourceBytes(c_o,
101184 \\int bar;
102185 \\int two() { return 2; }
103186 , &.{"-fcommon"});
104187
105 const d_o = addObject(b, opts);
188 const d_o = addObject(b, "d", opts);
106189 addCSourceBytes(d_o, "int baz;", &.{"-fcommon"});
107190
108 const lib = addStaticLibrary(b, opts);
191 const lib = addStaticLibrary(b, "lib", opts);
109192 lib.addObject(b_o);
110193 lib.addObject(c_o);
111194 lib.addObject(d_o);
112195
113 const exe = addExecutable(b, opts);
196 const exe = addExecutable(b, "test", opts);
114197 exe.addObject(a_o);
115198 exe.linkLibrary(lib);
116199 exe.is_linking_libc = true;
......@@ -121,18 +204,18 @@ fn testCommonSymbolsInArchive(b: *Build, opts: Options) *Step {
121204 }
122205
123206 {
124 const e_o = addObject(b, opts);
207 const e_o = addObject(b, "e", opts);
125208 addCSourceBytes(e_o,
126209 \\int bar = 0;
127210 \\int baz = 7;
128211 \\int two() { return 2; }
129212 , &.{"-fcommon"});
130213
131 const lib = addStaticLibrary(b, opts);
214 const lib = addStaticLibrary(b, "lib", opts);
132215 lib.addObject(b_o);
133216 lib.addObject(e_o);
134217
135 const exe = addExecutable(b, opts);
218 const exe = addExecutable(b, "test", opts);
136219 exe.addObject(a_o);
137220 exe.linkLibrary(lib);
138221 exe.is_linking_libc = true;
......@@ -148,7 +231,7 @@ fn testCommonSymbolsInArchive(b: *Build, opts: Options) *Step {
148231fn testDsoPlt(b: *Build, opts: Options) *Step {
149232 const test_step = addTestStep(b, "dso-plt", opts);
150233
151 const dso = addSharedLibrary(b, opts);
234 const dso = addSharedLibrary(b, "dso", opts);
152235 addCSourceBytes(dso,
153236 \\#include<stdio.h>
154237 \\void world() {
......@@ -164,7 +247,7 @@ fn testDsoPlt(b: *Build, opts: Options) *Step {
164247 , &.{"-fPIC"});
165248 dso.is_linking_libc = true;
166249
167 const exe = addExecutable(b, opts);
250 const exe = addExecutable(b, "test", opts);
168251 addCSourceBytes(exe,
169252 \\#include<stdio.h>
170253 \\void world() {
......@@ -188,7 +271,7 @@ fn testDsoPlt(b: *Build, opts: Options) *Step {
188271fn testDsoUndef(b: *Build, opts: Options) *Step {
189272 const test_step = addTestStep(b, "dso-undef", opts);
190273
191 const dso = addSharedLibrary(b, opts);
274 const dso = addSharedLibrary(b, "dso", opts);
192275 addCSourceBytes(dso,
193276 \\extern int foo;
194277 \\int bar = 5;
......@@ -196,13 +279,13 @@ fn testDsoUndef(b: *Build, opts: Options) *Step {
196279 , &.{"-fPIC"});
197280 dso.is_linking_libc = true;
198281
199 const obj = addObject(b, opts);
282 const obj = addObject(b, "obj", opts);
200283 addCSourceBytes(obj, "int foo = 3;", &.{});
201284
202 const lib = addStaticLibrary(b, opts);
285 const lib = addStaticLibrary(b, "lib", opts);
203286 lib.addObject(obj);
204287
205 const exe = addExecutable(b, opts);
288 const exe = addExecutable(b, "test", opts);
206289 exe.linkLibrary(dso);
207290 exe.linkLibrary(lib);
208291 addCSourceBytes(exe,
......@@ -228,7 +311,7 @@ fn testDsoUndef(b: *Build, opts: Options) *Step {
228311fn testEmptyObject(b: *Build, opts: Options) *Step {
229312 const test_step = addTestStep(b, "empty-object", opts);
230313
231 const exe = addExecutable(b, opts);
314 const exe = addExecutable(b, "test", opts);
232315 addCSourceBytes(exe, "int main() { return 0; }", &.{});
233316 addCSourceBytes(exe, "", &.{});
234317 exe.is_linking_libc = true;
......@@ -243,7 +326,7 @@ fn testEmptyObject(b: *Build, opts: Options) *Step {
243326fn testGcSections(b: *Build, opts: Options) *Step {
244327 const test_step = addTestStep(b, "gc-sections", opts);
245328
246 const obj = addObject(b, opts);
329 const obj = addObject(b, "obj", opts);
247330 addCppSourceBytes(obj,
248331 \\#include <stdio.h>
249332 \\int two() { return 2; }
......@@ -266,7 +349,7 @@ fn testGcSections(b: *Build, opts: Options) *Step {
266349 obj.is_linking_libcpp = true;
267350
268351 {
269 const exe = addExecutable(b, opts);
352 const exe = addExecutable(b, "test", opts);
270353 exe.addObject(obj);
271354 exe.link_gc_sections = false;
272355 exe.is_linking_libc = true;
......@@ -297,7 +380,7 @@ fn testGcSections(b: *Build, opts: Options) *Step {
297380 }
298381
299382 {
300 const exe = addExecutable(b, opts);
383 const exe = addExecutable(b, "test", opts);
301384 exe.addObject(obj);
302385 exe.link_gc_sections = true;
303386 exe.is_linking_libc = true;
......@@ -333,7 +416,7 @@ fn testGcSections(b: *Build, opts: Options) *Step {
333416fn testLargeAlignmentDso(b: *Build, opts: Options) *Step {
334417 const test_step = addTestStep(b, "large-alignment-dso", opts);
335418
336 const dso = addSharedLibrary(b, opts);
419 const dso = addSharedLibrary(b, "dso", opts);
337420 addCSourceBytes(dso,
338421 \\#include <stdio.h>
339422 \\#include <stdint.h>
......@@ -362,7 +445,7 @@ fn testLargeAlignmentDso(b: *Build, opts: Options) *Step {
362445 check.checkComputeCompare("addr2 16 %", .{ .op = .eq, .value = .{ .literal = 0 } });
363446 test_step.dependOn(&check.step);
364447
365 const exe = addExecutable(b, opts);
448 const exe = addExecutable(b, "test", opts);
366449 addCSourceBytes(exe,
367450 \\void greet();
368451 \\int main() { greet(); }
......@@ -380,7 +463,7 @@ fn testLargeAlignmentDso(b: *Build, opts: Options) *Step {
380463fn testLargeAlignmentExe(b: *Build, opts: Options) *Step {
381464 const test_step = addTestStep(b, "large-alignment-exe", opts);
382465
383 const exe = addExecutable(b, opts);
466 const exe = addExecutable(b, "test", opts);
384467 addCSourceBytes(exe,
385468 \\#include <stdio.h>
386469 \\#include <stdint.h>
......@@ -423,7 +506,7 @@ fn testLargeAlignmentExe(b: *Build, opts: Options) *Step {
423506fn testLinkingC(b: *Build, opts: Options) *Step {
424507 const test_step = addTestStep(b, "linking-c", opts);
425508
426 const exe = addExecutable(b, opts);
509 const exe = addExecutable(b, "test", opts);
427510 addCSourceBytes(exe,
428511 \\#include <stdio.h>
429512 \\int main() {
......@@ -452,7 +535,7 @@ fn testLinkingC(b: *Build, opts: Options) *Step {
452535fn testLinkingCpp(b: *Build, opts: Options) *Step {
453536 const test_step = addTestStep(b, "linking-cpp", opts);
454537
455 const exe = addExecutable(b, opts);
538 const exe = addExecutable(b, "test", opts);
456539 addCppSourceBytes(exe,
457540 \\#include <iostream>
458541 \\int main() {
......@@ -482,7 +565,7 @@ fn testLinkingCpp(b: *Build, opts: Options) *Step {
482565fn testLinkingZig(b: *Build, opts: Options) *Step {
483566 const test_step = addTestStep(b, "linking-zig-static", opts);
484567
485 const exe = addExecutable(b, opts);
568 const exe = addExecutable(b, "test", opts);
486569 addZigSourceBytes(exe,
487570 \\pub fn main() void {
488571 \\ @import("std").debug.print("Hello World!\n", .{});
......@@ -508,7 +591,7 @@ fn testLinkingZig(b: *Build, opts: Options) *Step {
508591fn testTlsStatic(b: *Build, opts: Options) *Step {
509592 const test_step = addTestStep(b, "tls-static", opts);
510593
511 const exe = addExecutable(b, opts);
594 const exe = addExecutable(b, "test", opts);
512595 addCSourceBytes(exe,
513596 \\#include <stdio.h>
514597 \\_Thread_local int a = 10;
......@@ -555,9 +638,9 @@ fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {
555638 return b.step(name, "");
556639}
557640
558fn addExecutable(b: *Build, opts: Options) *Compile {
641fn addExecutable(b: *Build, name: []const u8, opts: Options) *Compile {
559642 const exe = b.addExecutable(.{
560 .name = "test",
643 .name = name,
561644 .target = opts.target,
562645 .optimize = opts.optimize,
563646 .use_llvm = opts.use_llvm,
......@@ -567,9 +650,9 @@ fn addExecutable(b: *Build, opts: Options) *Compile {
567650 return exe;
568651}
569652
570fn addObject(b: *Build, opts: Options) *Compile {
653fn addObject(b: *Build, name: []const u8, opts: Options) *Compile {
571654 return b.addObject(.{
572 .name = "a",
655 .name = name,
573656 .target = opts.target,
574657 .optimize = opts.optimize,
575658 .use_llvm = opts.use_llvm,
......@@ -577,9 +660,9 @@ fn addObject(b: *Build, opts: Options) *Compile {
577660 });
578661}
579662
580fn addStaticLibrary(b: *Build, opts: Options) *Compile {
663fn addStaticLibrary(b: *Build, name: []const u8, opts: Options) *Compile {
581664 return b.addStaticLibrary(.{
582 .name = "a",
665 .name = name,
583666 .target = opts.target,
584667 .optimize = opts.optimize,
585668 .use_llvm = opts.use_llvm,
......@@ -587,9 +670,9 @@ fn addStaticLibrary(b: *Build, opts: Options) *Compile {
587670 });
588671}
589672
590fn addSharedLibrary(b: *Build, opts: Options) *Compile {
673fn addSharedLibrary(b: *Build, name: []const u8, opts: Options) *Compile {
591674 const dso = b.addSharedLibrary(.{
592 .name = "a",
675 .name = name,
593676 .target = opts.target,
594677 .optimize = opts.optimize,
595678 .use_llvm = opts.use_llvm,