Commit 4209ab90a8

LemonBoy <thatlemon@gmail.com>
2020-04-01 12:00:37
std: Use the versioned libc symbols if needed
Many symbols on NetBSD and some on OSX require the definition of an alias.
1 parent 070ace4
Changed files (3)
lib/std/c/netbsd.zig
@@ -21,7 +21,7 @@ pub extern "c" fn __sigprocmask14(how: c_int, noalias set: ?*const sigset_t, noa
 pub extern "c" fn __socket30(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
 pub extern "c" fn __gettimeofday50(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int;
 pub extern "c" fn __getrusage50(who: c_int, usage: *rusage) c_int;
-// instead of sched_yield
+// libc aliases this as sched_yield
 pub extern "c" fn __libc_thr_yield() c_int;
 
 pub const pthread_mutex_t = extern struct {
lib/std/c.zig
@@ -73,7 +73,6 @@ pub extern "c" fn abort() noreturn;
 pub extern "c" fn exit(code: c_int) noreturn;
 pub extern "c" fn isatty(fd: fd_t) c_int;
 pub extern "c" fn close(fd: fd_t) c_int;
-pub extern "c" fn fstat(fd: fd_t, buf: *Stat) c_int;
 pub extern "c" fn fstatat(dirfd: fd_t, path: [*:0]const u8, stat_buf: *Stat, flags: u32) c_int;
 pub extern "c" fn lseek(fd: fd_t, offset: off_t, whence: c_int) off_t;
 pub extern "c" fn open(path: [*:0]const u8, oflag: c_uint, ...) c_int;
@@ -86,7 +85,6 @@ pub extern "c" fn pread(fd: fd_t, buf: [*]u8, nbyte: usize, offset: u64) isize;
 pub extern "c" fn preadv(fd: c_int, iov: [*]const iovec, iovcnt: c_uint, offset: u64) isize;
 pub extern "c" fn writev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint) isize;
 pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: u64) isize;
-pub extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *Stat) c_int;
 pub extern "c" fn write(fd: fd_t, buf: [*]const u8, nbyte: usize) isize;
 pub extern "c" fn pwrite(fd: fd_t, buf: [*]const u8, nbyte: usize, offset: u64) isize;
 pub extern "c" fn mmap(addr: ?*align(page_size) c_void, len: usize, prot: c_uint, flags: c_uint, fd: fd_t, offset: u64) *c_void;
@@ -114,15 +112,10 @@ pub extern "c" fn dup2(old_fd: fd_t, new_fd: fd_t) c_int;
 pub extern "c" fn readlink(noalias path: [*:0]const u8, noalias buf: [*]u8, bufsize: usize) isize;
 pub extern "c" fn readlinkat(dirfd: fd_t, noalias path: [*:0]const u8, noalias buf: [*]u8, bufsize: usize) isize;
 pub extern "c" fn realpath(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8;
-pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int;
-pub extern "c" fn gettimeofday(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int;
-pub extern "c" fn sigaction(sig: c_int, noalias act: *const Sigaction, noalias oact: ?*Sigaction) c_int;
-pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int;
 pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int;
 pub extern "c" fn setregid(rgid: c_uint, egid: c_uint) c_int;
 pub extern "c" fn rmdir(path: [*:0]const u8) c_int;
 pub extern "c" fn getenv(name: [*:0]const u8) ?[*:0]u8;
-pub extern "c" fn getrusage(who: c_int, usage: *rusage) c_int;
 pub extern "c" fn sysctl(name: [*]const c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
 pub extern "c" fn sysctlbyname(name: [*:0]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
 pub extern "c" fn sysctlnametomib(name: [*:0]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;
@@ -133,7 +126,6 @@ pub extern "c" fn uname(buf: *utsname) c_int;
 
 pub extern "c" fn gethostname(name: [*]u8, len: usize) c_int;
 pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int;
-pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
 pub extern "c" fn socketpair(domain: c_uint, sock_type: c_uint, protocol: c_uint, sv: *[2]fd_t) c_int;
 pub extern "c" fn listen(sockfd: fd_t, backlog: c_uint) c_int;
 pub extern "c" fn getsockname(sockfd: fd_t, noalias addr: *sockaddr, noalias addrlen: *socklen_t) c_int;
@@ -161,12 +153,55 @@ pub extern fn recvfrom(
     noalias addrlen: ?*socklen_t,
 ) isize;
 
+pub usingnamespace switch (builtin.os.tag) {
+    .netbsd => struct {
+        pub const clock_getres = __clock_getres50;
+        pub const clock_gettime = __clock_gettime50;
+        pub const fstat = __fstat50;
+        pub const getdents = __getdents30;
+        pub const getrusage = __getrusage50;
+        pub const gettimeofday = __gettimeofday50;
+        pub const nanosleep = __nanosleep50;
+        pub const sched_yield = __libc_thr_yield;
+        pub const sigaction = __sigaction14;
+        pub const sigaltstack = __sigaltstack14;
+        pub const sigprocmask = __sigprocmask14;
+        pub const stat = __stat50;
+    },
+    .macosx, .ios, .watchos, .tvos => struct {
+        // XXX: close -> close$NOCANCEL
+        // XXX: getdirentries -> _getdirentries64
+        pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int;
+        pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int;
+        pub const fstat = @"fstat$INODE64";
+        pub extern "c" fn getrusage(who: c_int, usage: *rusage) c_int;
+        pub extern "c" fn gettimeofday(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int;
+        pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int;
+        pub extern "c" fn sched_yield() c_int;
+        pub extern "c" fn sigaction(sig: c_int, noalias act: *const Sigaction, noalias oact: ?*Sigaction) c_int;
+        pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int;
+        pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
+        pub extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *Stat) c_int;
+    },
+    else => struct {
+        pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int;
+        pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int;
+        pub extern "c" fn fstat(fd: fd_t, buf: *Stat) c_int;
+        pub extern "c" fn getrusage(who: c_int, usage: *rusage) c_int;
+        pub extern "c" fn gettimeofday(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int;
+        pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int;
+        pub extern "c" fn sched_yield() c_int;
+        pub extern "c" fn sigaction(sig: c_int, noalias act: *const Sigaction, noalias oact: ?*Sigaction) c_int;
+        pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int;
+        pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
+        pub extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *Stat) c_int;
+    },
+};
+
 pub extern "c" fn kill(pid: pid_t, sig: c_int) c_int;
 pub extern "c" fn getdirentries(fd: fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize;
 pub extern "c" fn setgid(ruid: c_uint, euid: c_uint) c_int;
 pub extern "c" fn setuid(uid: c_uint) c_int;
-pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int;
-pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int;
 
 pub extern "c" fn aligned_alloc(alignment: usize, size: usize) ?*c_void;
 pub extern "c" fn malloc(usize) ?*c_void;
@@ -229,8 +264,6 @@ pub extern "c" fn dn_expand(
     length: c_int,
 ) c_int;
 
-pub extern "c" fn sched_yield() c_int;
-
 pub const PTHREAD_MUTEX_INITIALIZER = pthread_mutex_t{};
 pub extern "c" fn pthread_mutex_lock(mutex: *pthread_mutex_t) c_int;
 pub extern "c" fn pthread_mutex_unlock(mutex: *pthread_mutex_t) c_int;
lib/std/os.zig
@@ -2553,14 +2553,7 @@ pub const FStatError = error{
 pub fn fstat(fd: fd_t) FStatError!Stat {
     var stat: Stat = undefined;
 
-    const symbol_name = if (comptime std.Target.current.isDarwin())
-        "fstat$INODE64"
-    else if (std.Target.current.os.tag == .netbsd)
-        "__fstat50"
-    else
-        "fstat";
-
-    switch (errno(@field(system, symbol_name)(fd, &stat))) {
+    switch (errno(system.fstat(fd, &stat))) {
         0 => return stat,
         EINVAL => unreachable,
         EBADF => unreachable, // Always a race condition.
@@ -3427,12 +3420,7 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void {
         return;
     }
 
-    const symbol_name = if (std.Target.current.os.tag == .netbsd)
-        "__clock_gettime50"
-    else
-        "clock_gettime";
-
-    switch (errno(@field(system, symbol_name)(clk_id, tp))) {
+    switch (errno(system.clock_gettime(clk_id, tp))) {
         0 => return,
         EFAULT => unreachable,
         EINVAL => return error.UnsupportedClock,
@@ -3454,12 +3442,7 @@ pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void {
         return;
     }
 
-    const symbol_name = if (std.Target.current.os.tag == .netbsd)
-        "__clock_getres50"
-    else
-        "clock_getres";
-
-    switch (errno(@field(system, symbol_name)(clk_id, res))) {
+    switch (errno(system.clock_getres(clk_id, res))) {
         0 => return,
         EFAULT => unreachable,
         EINVAL => return error.UnsupportedClock,
@@ -3525,12 +3508,7 @@ pub const SigaltstackError = error{
 } || UnexpectedError;
 
 pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {
-    const symbol_name = if (std.Target.current.os.tag == .netbsd)
-        "__sigaltstack14"
-    else
-        "sigaltstack";
-
-    switch (errno(@field(system, symbol_name)(ss, old_ss))) {
+    switch (errno(system.sigaltstack(ss, old_ss))) {
         0 => return,
         EFAULT => unreachable,
         EINVAL => unreachable,