commit 403e539669deae8d4aeb11baa5f30aad5dfe6589 (tree)
parent 89563f07e3956a44e37eb59e618d926bd401055c
Author: Jakub Konka <kubkon@jakubkonka.com>
Date: Tue, 24 Oct 2023 22:07:07 +0200
elf: test unresolved symbol reference error
Diffstat:
1 file changed, 45 insertions(+), 0 deletions(-)
diff --git a/test/link/elf.zig b/test/link/elf.zig
@@ -99,6 +99,7 @@ pub fn build(b: *Build) void {
elf_step.dependOn(testTlsOffsetAlignment(b, .{ .target = glibc_target }));
elf_step.dependOn(testTlsPic(b, .{ .target = glibc_target }));
elf_step.dependOn(testTlsSmallAlignment(b, .{ .target = glibc_target }));
+ elf_step.dependOn(testUnresolved(b, .{ .target = glibc_target }));
elf_step.dependOn(testWeakExports(b, .{ .target = glibc_target }));
elf_step.dependOn(testWeakUndefsDso(b, .{ .target = glibc_target }));
elf_step.dependOn(testZNow(b, .{ .target = glibc_target }));
@@ -2783,6 +2784,44 @@ fn testTlsStatic(b: *Build, opts: Options) *Step {
return test_step;
}
+fn testUnresolved(b: *Build, opts: Options) *Step {
+ const test_step = addTestStep(b, "unresolved", opts);
+
+ const obj1 = addObject(b, "a", opts);
+ addCSourceBytes(obj1,
+ \\#include <stdio.h>
+ \\int foo();
+ \\int bar() {
+ \\ return foo() + 1;
+ \\}
+ , &.{"-ffunction-sections"});
+ obj1.linkLibC();
+
+ const obj2 = addObject(b, "b", opts);
+ addCSourceBytes(obj2,
+ \\#include <stdio.h>
+ \\int foo();
+ \\int bar();
+ \\int main() {
+ \\ return foo() + bar();
+ \\}
+ , &.{"-ffunction-sections"});
+ obj2.linkLibC();
+
+ const exe = addExecutable(b, "main", opts);
+ exe.addObject(obj1);
+ exe.addObject(obj2);
+ exe.linkLibC();
+
+ expectLinkErrors(exe, test_step, &.{
+ "error: undefined symbol: foo",
+ "note: referenced by /?/a.o:.text.bar",
+ "note: referenced by /?/b.o:.text.main",
+ });
+
+ return test_step;
+}
+
fn testWeakExports(b: *Build, opts: Options) *Step {
const test_step = addTestStep(b, "weak-exports", opts);
@@ -3081,6 +3120,12 @@ fn addAsmSourceBytes(comp: *Compile, bytes: []const u8) void {
comp.addAssemblyFile(file);
}
+fn expectLinkErrors(comp: *Compile, test_step: *Step, expected_errors: []const []const u8) void {
+ comp.expect_errors = expected_errors;
+ const bin_file = comp.getEmittedBin();
+ bin_file.addStepDependencies(test_step);
+}
+
const std = @import("std");
const Build = std.Build;