Commit 9bcb432a0e

Jakub Konka <kubkon@jakubkonka.com>
2023-11-08 18:41:09
elf: test emitting relocatable
1 parent 29d7727
Changed files (1)
test
link
test/link/elf.zig
@@ -21,6 +21,10 @@ pub fn build(b: *Build) void {
         .abi = .gnu,
     };
 
+    // Exercise linker in -r mode
+    elf_step.dependOn(testEmitRelocatable(b, .{ .use_llvm = false, .target = musl_target }));
+    elf_step.dependOn(testEmitRelocatable(b, .{ .target = musl_target }));
+
     // Exercise linker in ar mode
     elf_step.dependOn(testEmitStaticLib(b, .{ .target = musl_target }));
 
@@ -629,6 +633,53 @@ fn testDsoUndef(b: *Build, opts: Options) *Step {
     return test_step;
 }
 
+fn testEmitRelocatable(b: *Build, opts: Options) *Step {
+    const test_step = addTestStep(b, "emit-relocatable", opts);
+
+    const obj1 = addObject(b, "obj1", opts);
+    addZigSourceBytes(obj1,
+        \\const std = @import("std");
+        \\extern var bar: i32;
+        \\export fn foo() i32 {
+        \\   return bar;
+        \\}
+        \\export fn printFoo() void {
+        \\    std.debug.print("foo={d}\n", .{foo()});
+        \\}
+    );
+    addCSourceBytes(obj1,
+        \\#include <stdio.h>
+        \\int bar = 42;
+        \\void printBar() {
+        \\  fprintf(stderr, "bar=%d\n", bar);
+        \\}
+    , &.{});
+    obj1.linkLibC();
+
+    const exe = addExecutable(b, "test", opts);
+    addZigSourceBytes(exe,
+        \\const std = @import("std");
+        \\extern fn printFoo() void;
+        \\extern fn printBar() void;
+        \\pub fn main() void {
+        \\    printFoo();
+        \\    printBar();
+        \\}
+    );
+    exe.addObject(obj1);
+    exe.linkLibC();
+
+    const run = addRunArtifact(exe);
+    run.expectStdErrEqual(
+        \\foo=42
+        \\bar=42
+        \\
+    );
+    test_step.dependOn(&run.step);
+
+    return test_step;
+}
+
 fn testEmitStaticLib(b: *Build, opts: Options) *Step {
     const test_step = addTestStep(b, "emit-static-lib", opts);
 
@@ -951,7 +1002,6 @@ fn testGcSectionsZig(b: *Build, opts: Options) *Step {
     const obj = addObject(b, "obj", .{
         .target = opts.target,
         .use_llvm = true,
-        .use_lld = true,
     });
     addCSourceBytes(obj,
         \\int live_var1 = 1;