authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-06 12:40:59+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:04+02:00
log62c6c4a46d9af921398de4dd5f93935f0b09c753
tree2582e7ebaced23731599b056717158b26b8aed19
parent2f497f9f0553f2738e05a511f3774cb18efb2304

elf: more DSO tests


1 files changed, 51 insertions(+), 8 deletions(-)

test/link/elf.zig+51-8
......@@ -32,6 +32,7 @@ pub fn build(b: *Build) void {
3232
3333 for (&[_]CrossTarget{ glibc_target, musl_target }) |target| {
3434 elf_step.dependOn(testDsoPlt(b, .{ .target = target }));
35 elf_step.dependOn(testDsoUndef(b, .{ .target = target }));
3536 }
3637}
3738
......@@ -171,6 +172,47 @@ fn testDsoPlt(b: *Build, opts: Options) *Step {
171172 return test_step;
172173}
173174
175fn testDsoUndef(b: *Build, opts: Options) *Step {
176 const test_step = addTestStep(b, "dso-undef", opts);
177
178 const dso = addSharedLibrary(b, opts);
179 addCSourceBytes(dso,
180 \\extern int foo;
181 \\int bar = 5;
182 \\int baz() { return foo; }
183 , &.{"-fPIC"});
184 dso.is_linking_libc = true;
185
186 const obj = addObject(b, opts);
187 addCSourceBytes(obj, "int foo = 3;", &.{});
188
189 const lib = addStaticLibrary(b, opts);
190 lib.addObject(obj);
191
192 const exe = addExecutable(b, opts);
193 exe.linkLibrary(dso);
194 exe.linkLibrary(lib);
195 addCSourceBytes(exe,
196 \\extern int bar;
197 \\int main() {
198 \\ return bar - 5;
199 \\}
200 , &.{});
201 exe.is_linking_libc = true;
202 exe.verbose_link = true;
203
204 const run = addRunArtifact(exe);
205 run.expectExitCode(0);
206 test_step.dependOn(&run.step);
207
208 const check = exe.checkObject();
209 check.checkInDynamicSymtab();
210 check.checkContains("foo");
211 test_step.dependOn(&check.step);
212
213 return test_step;
214}
215
174216fn testEmptyObject(b: *Build, opts: Options) *Step {
175217 const test_step = addTestStep(b, "empty-object", opts);
176218
......@@ -422,7 +464,7 @@ fn addExecutable(b: *Build, opts: Options) *Compile {
422464
423465fn addObject(b: *Build, opts: Options) *Compile {
424466 return b.addObject(.{
425 .name = "a.o",
467 .name = "a",
426468 .target = opts.target,
427469 .optimize = opts.optimize,
428470 .use_llvm = opts.use_llvm,
......@@ -432,7 +474,7 @@ fn addObject(b: *Build, opts: Options) *Compile {
432474
433475fn addStaticLibrary(b: *Build, opts: Options) *Compile {
434476 return b.addStaticLibrary(.{
435 .name = "a.a",
477 .name = "a",
436478 .target = opts.target,
437479 .optimize = opts.optimize,
438480 .use_llvm = opts.use_llvm,
......@@ -442,7 +484,7 @@ fn addStaticLibrary(b: *Build, opts: Options) *Compile {
442484
443485fn addSharedLibrary(b: *Build, opts: Options) *Compile {
444486 return b.addSharedLibrary(.{
445 .name = "a.so",
487 .name = "a",
446488 .target = opts.target,
447489 .optimize = opts.optimize,
448490 .use_llvm = opts.use_llvm,
......@@ -457,28 +499,29 @@ fn addRunArtifact(comp: *Compile) *Run {
457499 return run;
458500}
459501
460fn addZigSourceBytes(comp: *Compile, comptime bytes: []const u8) void {
502fn addZigSourceBytes(comp: *Compile, bytes: []const u8) void {
461503 const b = comp.step.owner;
462504 const file = WriteFile.create(b).add("a.zig", bytes);
463505 file.addStepDependencies(&comp.step);
464506 comp.root_src = file;
465507}
466508
467fn addCSourceBytes(comp: *Compile, comptime bytes: []const u8, flags: []const []const u8) void {
509fn addCSourceBytes(comp: *Compile, bytes: []const u8, flags: []const []const u8) void {
468510 const b = comp.step.owner;
469511 const file = WriteFile.create(b).add("a.c", bytes);
470512 comp.addCSourceFile(.{ .file = file, .flags = flags });
471513}
472514
473fn addCppSourceBytes(comp: *Compile, comptime bytes: []const u8, flags: []const []const u8) void {
515fn addCppSourceBytes(comp: *Compile, bytes: []const u8, flags: []const []const u8) void {
474516 const b = comp.step.owner;
475517 const file = WriteFile.create(b).add("a.cpp", bytes);
476518 comp.addCSourceFile(.{ .file = file, .flags = flags });
477519}
478520
479fn addAsmSourceBytes(comp: *Compile, comptime bytes: []const u8) void {
521fn addAsmSourceBytes(comp: *Compile, bytes: []const u8) void {
480522 const b = comp.step.owner;
481 const file = WriteFile.create(b).add("a.s", bytes ++ "\n");
523 const actual_bytes = std.fmt.allocPrint(b.allocator, "{s}\n", .{bytes}) catch @panic("OOM");
524 const file = WriteFile.create(b).add("a.s", actual_bytes);
482525 comp.addAssemblyFile(file);
483526}
484527