From 19659c3bd347471d3d55597bdc1c141e9e4a1ae1 Mon Sep 17 00:00:00 2001 From: Marc Tiehuis Date: Wed, 11 Jul 2018 21:53:59 +1200 Subject: [PATCH] freebsd: Fix argc resolution in _start FreeBSD appears to use rdi instead of rsp as in other posix systems. According to some loose documentation, x86 passes values on the stack, so amd64 freebsd may be the only exception. --- std/os/freebsd.zig | 8 ++++---- std/special/bootstrap.zig | 15 +++++++++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/std/os/freebsd.zig b/std/os/freebsd.zig index ee7e93d653..8a582db92f 100644 --- a/std/os/freebsd.zig +++ b/std/os/freebsd.zig @@ -348,7 +348,7 @@ pub fn WIFEXITED(s: i32) bool { return WTERMSIG(s) == 0; } pub fn WIFSTOPPED(s: i32) bool { - return (u16)(((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00; + return @intCast(u16, ((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00; } pub fn WIFSIGNALED(s: i32) bool { return (unsigned(s) & 0xffff) -% 1 < 0xff; @@ -368,7 +368,7 @@ pub fn getErrno(r: usize) usize { } pub fn dup2(old: i32, new: i32) usize { - return arch.syscall2(arch.SYS_dup2, usize(old), usize(new)); + return arch.syscall2(arch.SYS_dup2, @intCast(usize, old), @intCast(usize, new)); } pub fn chdir(path: [*]const u8) usize { @@ -388,7 +388,7 @@ pub fn getcwd(buf: [*]u8, size: usize) usize { } pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize { - return arch.syscall3(arch.SYS_getdents, usize(fd), @ptrToInt(dirp), count); + return arch.syscall3(arch.SYS_getdents, @intCast(usize, fd), @ptrToInt(dirp), count); } pub fn isatty(fd: i32) bool { @@ -425,7 +425,7 @@ pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { } pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize { - return arch.syscall4(arch.SYS_pread, usize(fd), @ptrToInt(buf), count, offset); + return arch.syscall4(arch.SYS_pread, @intCast(usize, fd), @ptrToInt(buf), count, offset); } pub fn pipe(fd: *[2]i32) usize { diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index 53c646abdc..1fc80f2439 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -20,10 +20,17 @@ comptime { nakedcc fn _start() noreturn { switch (builtin.arch) { - builtin.Arch.x86_64 => { - argc_ptr = asm ("lea (%%rsp), %[argc]" - : [argc] "=r" (-> [*]usize) - ); + builtin.Arch.x86_64 => switch (builtin.os) { + builtin.Os.freebsd => { + argc_ptr = asm ("lea (%%rdi), %[argc]" + : [argc] "=r" (-> [*]usize) + ); + }, + else => { + argc_ptr = asm ("lea (%%rsp), %[argc]" + : [argc] "=r" (-> [*]usize) + ); + }, }, builtin.Arch.i386 => { argc_ptr = asm ("lea (%%esp), %[argc]"