diff --git a/lib/std/os/linux/arm64.zig b/lib/std/os/linux/arm64.zig index 8571b26071..c3b9f09028 100644 --- a/lib/std/os/linux/arm64.zig +++ b/lib/std/os/linux/arm64.zig @@ -106,11 +106,20 @@ pub extern fn clone(func: CloneFn, stack: usize, flags: u32, arg: usize, ptid: * pub const restore = restore_rt; pub fn restore_rt() callconv(.Naked) void { - return asm volatile ("svc #0" - : - : [number] "{x8}" (@enumToInt(SYS.rt_sigreturn)), - : "memory", "cc" - ); + switch (@import("builtin").zig_backend) { + .stage2_c => return asm volatile ( + \\ mov x8, %[number] + \\ svc #0 + : + : [number] "i" (@enumToInt(SYS.rt_sigreturn)), + : "memory", "cc" + ), + else => return asm volatile ("svc #0" + : + : [number] "{x8}" (@enumToInt(SYS.rt_sigreturn)), + : "memory", "cc" + ), + } } pub const O = struct { diff --git a/lib/std/os/linux/i386.zig b/lib/std/os/linux/i386.zig index 2766393487..9357002519 100644 --- a/lib/std/os/linux/i386.zig +++ b/lib/std/os/linux/i386.zig @@ -124,19 +124,37 @@ const CloneFn = std.meta.FnPtr(fn (arg: usize) callconv(.C) u8); pub extern fn clone(func: CloneFn, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize; pub fn restore() callconv(.Naked) void { - return asm volatile ("int $0x80" - : - : [number] "{eax}" (@enumToInt(SYS.sigreturn)), - : "memory" - ); + switch (@import("builtin").zig_backend) { + .stage2_c => return asm volatile ( + \\ movl %[number], %%eax + \\ int $0x80 + : + : [number] "i" (@enumToInt(SYS.sigreturn)), + : "memory" + ), + else => return asm volatile ("int $0x80" + : + : [number] "{eax}" (@enumToInt(SYS.sigreturn)), + : "memory" + ), + } } pub fn restore_rt() callconv(.Naked) void { - return asm volatile ("int $0x80" - : - : [number] "{eax}" (@enumToInt(SYS.rt_sigreturn)), - : "memory" - ); + switch (@import("builtin").zig_backend) { + .stage2_c => return asm volatile ( + \\ movl %[number], %%eax + \\ int $0x80 + : + : [number] "i" (@enumToInt(SYS.rt_sigreturn)), + : "memory" + ), + else => return asm volatile ("int $0x80" + : + : [number] "{eax}" (@enumToInt(SYS.rt_sigreturn)), + : "memory" + ), + } } pub const O = struct { diff --git a/lib/std/os/linux/x86_64.zig b/lib/std/os/linux/x86_64.zig index 7f320c010a..90aae99082 100644 --- a/lib/std/os/linux/x86_64.zig +++ b/lib/std/os/linux/x86_64.zig @@ -109,11 +109,14 @@ pub const restore = restore_rt; pub fn restore_rt() callconv(.Naked) void { switch (@import("builtin").zig_backend) { - .stage2_c => return asm volatile (std.fmt.comptimePrint( - \\ movl ${d}, %%eax - \\ syscall - \\ retq - , .{@enumToInt(SYS.rt_sigreturn)}) ::: "rcx", "r11", "memory"), + .stage2_c => return asm volatile ( + \\ movl %[number], %%eax + \\ syscall + \\ retq + : + : [number] "i" (@enumToInt(SYS.rt_sigreturn)), + : "rcx", "r11", "memory" + ), else => return asm volatile ("syscall" : : [number] "{rax}" (@enumToInt(SYS.rt_sigreturn)), diff --git a/lib/std/start.zig b/lib/std/start.zig index 0563e2adad..cc8a92d7b6 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -265,23 +265,37 @@ fn EfiMain(handle: uefi.Handle, system_table: *uefi.tables.SystemTable) callconv fn _start() callconv(.Naked) noreturn { switch (builtin.zig_backend) { - .stage2_c => switch (native_arch) { - .x86_64 => { - @export(argc_argv_ptr, .{ .name = "argc_argv_ptr" }); - @export(posixCallMainAndExit, .{ .name = "_posixCallMainAndExit" }); - asm volatile ( - \\ xor %%rbp, %%rbp - \\ mov %%rsp, argc_argv_ptr + .stage2_c => { + @export(argc_argv_ptr, .{ .name = "argc_argv_ptr" }); + @export(posixCallMainAndExit, .{ .name = "_posixCallMainAndExit" }); + switch (native_arch) { + .x86_64 => asm volatile ( + \\ xorl %%ebp, %%ebp + \\ movq %%rsp, argc_argv_ptr + \\ andq $-16, %%rsp \\ call _posixCallMainAndExit - ); - unreachable; - }, - else => @compileError("unsupported arch"), + ), + .i386 => asm volatile ( + \\ xorl %%ebp, %%ebp + \\ movl %%esp, argc_argv_ptr + \\ andl $-16, %%esp + \\ jmp _posixCallMainAndExit + ), + .aarch64, .aarch64_be, .arm, .armeb, .thumb => asm volatile ( + \\ mov fp, #0 + \\ mov lr, #0 + \\ str sp, argc_argv_ptr + \\ and sp, #-16 + \\ b _posixCallMainAndExit + ), + else => @compileError("unsupported arch"), + } + unreachable; }, else => switch (native_arch) { .x86_64 => { argc_argv_ptr = asm volatile ( - \\ xor %%rbp, %%rbp + \\ xor %%ebp, %%ebp : [argc] "={rsp}" (-> [*]usize), ); }, diff --git a/test/cases/aarch64-macos/hello_world_with_updates.0.zig b/test/cases/aarch64-macos/hello_world_with_updates.0.zig index 9f516ff139..a3bcb8527f 100644 --- a/test/cases/aarch64-macos/hello_world_with_updates.0.zig +++ b/test/cases/aarch64-macos/hello_world_with_updates.0.zig @@ -2,4 +2,4 @@ // output_mode=Exe // target=aarch64-macos // -// :109:9: error: root struct of file 'tmp' has no member named 'main' +// :108:9: error: root struct of file 'tmp' has no member named 'main' diff --git a/test/cases/x86_64-linux/hello_world_with_updates.0.zig b/test/cases/x86_64-linux/hello_world_with_updates.0.zig index 40abdd6c1f..70cec703da 100644 --- a/test/cases/x86_64-linux/hello_world_with_updates.0.zig +++ b/test/cases/x86_64-linux/hello_world_with_updates.0.zig @@ -2,4 +2,4 @@ // output_mode=Exe // target=x86_64-linux // -// :109:9: error: root struct of file 'tmp' has no member named 'main' +// :108:9: error: root struct of file 'tmp' has no member named 'main' diff --git a/test/cases/x86_64-macos/hello_world_with_updates.0.zig b/test/cases/x86_64-macos/hello_world_with_updates.0.zig index e0680c81d7..3b8758a0e5 100644 --- a/test/cases/x86_64-macos/hello_world_with_updates.0.zig +++ b/test/cases/x86_64-macos/hello_world_with_updates.0.zig @@ -2,4 +2,4 @@ // output_mode=Exe // target=x86_64-macos // -// :109:9: error: root struct of file 'tmp' has no member named 'main' +// :108:9: error: root struct of file 'tmp' has no member named 'main' diff --git a/test/cases/x86_64-windows/hello_world_with_updates.0.zig b/test/cases/x86_64-windows/hello_world_with_updates.0.zig index 04e1d4cfad..e9a55f6061 100644 --- a/test/cases/x86_64-windows/hello_world_with_updates.0.zig +++ b/test/cases/x86_64-windows/hello_world_with_updates.0.zig @@ -2,4 +2,4 @@ // output_mode=Exe // target=x86_64-windows // -// :130:9: error: root struct of file 'tmp' has no member named 'main' +// :129:9: error: root struct of file 'tmp' has no member named 'main'