diff --git a/test/link.zig b/test/link.zig index f79b4a478c..56b1cf415d 100644 --- a/test/link.zig +++ b/test/link.zig @@ -96,6 +96,10 @@ pub const cases = [_]Case{ .build_root = "test/link/macho/bugs/16308", .import = @import("link/macho/bugs/16308/build.zig"), }, + .{ + .build_root = "test/link/macho/bugs/16628", + .import = @import("link/macho/bugs/16628/build.zig"), + }, .{ .build_root = "test/link/macho/dead_strip", .import = @import("link/macho/dead_strip/build.zig"), diff --git a/test/link/macho/bugs/16628/a_arm64.s b/test/link/macho/bugs/16628/a_arm64.s new file mode 100644 index 0000000000..215deb8514 --- /dev/null +++ b/test/link/macho/bugs/16628/a_arm64.s @@ -0,0 +1,37 @@ +.globl _foo +.align 4 +_foo: + .cfi_startproc + stp x29, x30, [sp, #-32]! + .cfi_def_cfa_offset 32 + .cfi_offset w30, -24 + .cfi_offset w29, -32 + mov x29, sp + .cfi_def_cfa w29, 32 + bl _bar + ldp x29, x30, [sp], #32 + .cfi_restore w29 + .cfi_restore w30 + .cfi_def_cfa_offset 0 + ret + .cfi_endproc + +.globl _bar +.align 4 +_bar: + .cfi_startproc + sub sp, sp, #32 + .cfi_def_cfa_offset -32 + stp x29, x30, [sp, #16] + .cfi_offset w30, -24 + .cfi_offset w29, -32 + mov x29, sp + .cfi_def_cfa w29, 32 + mov w0, #4 + ldp x29, x30, [sp, #16] + .cfi_restore w29 + .cfi_restore w30 + add sp, sp, #32 + .cfi_def_cfa_offset 0 + ret + .cfi_endproc diff --git a/test/link/macho/bugs/16628/a_x64.s b/test/link/macho/bugs/16628/a_x64.s new file mode 100644 index 0000000000..cc1712585e --- /dev/null +++ b/test/link/macho/bugs/16628/a_x64.s @@ -0,0 +1,29 @@ +.globl _foo +_foo: + .cfi_startproc + push %rbp + .cfi_def_cfa_offset 8 + .cfi_offset %rbp, -8 + mov %rsp, %rbp + .cfi_def_cfa_register %rbp + call _bar + pop %rbp + .cfi_restore %rbp + .cfi_def_cfa_offset 0 + ret + .cfi_endproc + +.globl _bar +_bar: + .cfi_startproc + push %rbp + .cfi_def_cfa_offset 8 + .cfi_offset %rbp, -8 + mov %rsp, %rbp + .cfi_def_cfa_register %rbp + mov $4, %rax + pop %rbp + .cfi_restore %rbp + .cfi_def_cfa_offset 0 + ret + .cfi_endproc diff --git a/test/link/macho/bugs/16628/build.zig b/test/link/macho/bugs/16628/build.zig new file mode 100644 index 0000000000..aa396f7a3b --- /dev/null +++ b/test/link/macho/bugs/16628/build.zig @@ -0,0 +1,42 @@ +const std = @import("std"); +const builtin = @import("builtin"); + +pub const requires_symlinks = true; +pub const requires_macos_sdk = false; + +pub fn build(b: *std.Build) void { + const test_step = b.step("test", "Test it"); + b.default_step = test_step; + + add(b, test_step, .Debug); + add(b, test_step, .ReleaseFast); + add(b, test_step, .ReleaseSmall); + add(b, test_step, .ReleaseSafe); +} + +fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const target: std.zig.CrossTarget = .{ .os_tag = .macos }; + + const exe = b.addExecutable(.{ + .name = "test", + .optimize = optimize, + .target = target, + }); + exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} }); + switch (builtin.cpu.arch) { + .aarch64 => { + exe.addCSourceFile(.{ .file = .{ .path = "a_arm64.s" }, .flags = &[0][]const u8{} }); + }, + .x86_64 => { + exe.addCSourceFile(.{ .file = .{ .path = "a_x64.s" }, .flags = &[0][]const u8{} }); + }, + else => unreachable, + } + exe.linkLibC(); + + const run = b.addRunArtifact(exe); + run.skip_foreign_checks = true; + run.expectStdOutEqual("4\n"); + + test_step.dependOn(&run.step); +} diff --git a/test/link/macho/bugs/16628/main.c b/test/link/macho/bugs/16628/main.c new file mode 100644 index 0000000000..3ac8481dca --- /dev/null +++ b/test/link/macho/bugs/16628/main.c @@ -0,0 +1,8 @@ +#include + +int foo(); + +int main() { + printf("%d\n", foo()); + return 0; +}