authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-04-01 10:00:37+00:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-04-01 12:23:18+02:00
log4209ab90a8a9922bcc407e12b0e58b827a179a91
tree6ac13422bc623d261d90ef1a64ab70375dc2538e
parent070ace4b22810116f7816eeccbc1ce600be5b2a2

std: Use the versioned libc symbols if needed

Many symbols on NetBSD and some on OSX require the definition of an alias.

3 files changed, 50 insertions(+), 39 deletions(-)

lib/std/c.zig+45-12
...@@ -73,7 +73,6 @@ pub extern "c" fn abort() noreturn;...@@ -73,7 +73,6 @@ pub extern "c" fn abort() noreturn;
73pub extern "c" fn exit(code: c_int) noreturn;73pub extern "c" fn exit(code: c_int) noreturn;
74pub extern "c" fn isatty(fd: fd_t) c_int;74pub extern "c" fn isatty(fd: fd_t) c_int;
75pub extern "c" fn close(fd: fd_t) c_int;75pub extern "c" fn close(fd: fd_t) c_int;
76pub extern "c" fn fstat(fd: fd_t, buf: *Stat) c_int;
77pub extern "c" fn fstatat(dirfd: fd_t, path: [*:0]const u8, stat_buf: *Stat, flags: u32) c_int;76pub extern "c" fn fstatat(dirfd: fd_t, path: [*:0]const u8, stat_buf: *Stat, flags: u32) c_int;
78pub extern "c" fn lseek(fd: fd_t, offset: off_t, whence: c_int) off_t;77pub extern "c" fn lseek(fd: fd_t, offset: off_t, whence: c_int) off_t;
79pub extern "c" fn open(path: [*:0]const u8, oflag: c_uint, ...) c_int;78pub 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;...@@ -86,7 +85,6 @@ pub extern "c" fn pread(fd: fd_t, buf: [*]u8, nbyte: usize, offset: u64) isize;
86pub extern "c" fn preadv(fd: c_int, iov: [*]const iovec, iovcnt: c_uint, offset: u64) isize;85pub extern "c" fn preadv(fd: c_int, iov: [*]const iovec, iovcnt: c_uint, offset: u64) isize;
87pub extern "c" fn writev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint) isize;86pub extern "c" fn writev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint) isize;
88pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: u64) isize;87pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: u64) isize;
89pub extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *Stat) c_int;
90pub extern "c" fn write(fd: fd_t, buf: [*]const u8, nbyte: usize) isize;88pub extern "c" fn write(fd: fd_t, buf: [*]const u8, nbyte: usize) isize;
91pub extern "c" fn pwrite(fd: fd_t, buf: [*]const u8, nbyte: usize, offset: u64) isize;89pub extern "c" fn pwrite(fd: fd_t, buf: [*]const u8, nbyte: usize, offset: u64) isize;
92pub 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;90pub 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;...@@ -114,15 +112,10 @@ pub extern "c" fn dup2(old_fd: fd_t, new_fd: fd_t) c_int;
114pub extern "c" fn readlink(noalias path: [*:0]const u8, noalias buf: [*]u8, bufsize: usize) isize;112pub extern "c" fn readlink(noalias path: [*:0]const u8, noalias buf: [*]u8, bufsize: usize) isize;
115pub extern "c" fn readlinkat(dirfd: fd_t, noalias path: [*:0]const u8, noalias buf: [*]u8, bufsize: usize) isize;113pub extern "c" fn readlinkat(dirfd: fd_t, noalias path: [*:0]const u8, noalias buf: [*]u8, bufsize: usize) isize;
116pub extern "c" fn realpath(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8;114pub extern "c" fn realpath(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8;
117pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int;
118pub extern "c" fn gettimeofday(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int;
119pub extern "c" fn sigaction(sig: c_int, noalias act: *const Sigaction, noalias oact: ?*Sigaction) c_int;
120pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int;
121pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int;115pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int;
122pub extern "c" fn setregid(rgid: c_uint, egid: c_uint) c_int;116pub extern "c" fn setregid(rgid: c_uint, egid: c_uint) c_int;
123pub extern "c" fn rmdir(path: [*:0]const u8) c_int;117pub extern "c" fn rmdir(path: [*:0]const u8) c_int;
124pub extern "c" fn getenv(name: [*:0]const u8) ?[*:0]u8;118pub extern "c" fn getenv(name: [*:0]const u8) ?[*:0]u8;
125pub extern "c" fn getrusage(who: c_int, usage: *rusage) c_int;
126pub extern "c" fn sysctl(name: [*]const c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;119pub extern "c" fn sysctl(name: [*]const c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
127pub extern "c" fn sysctlbyname(name: [*:0]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;120pub extern "c" fn sysctlbyname(name: [*:0]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
128pub extern "c" fn sysctlnametomib(name: [*:0]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;121pub 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;...@@ -133,7 +126,6 @@ pub extern "c" fn uname(buf: *utsname) c_int;
133126
134pub extern "c" fn gethostname(name: [*]u8, len: usize) c_int;127pub extern "c" fn gethostname(name: [*]u8, len: usize) c_int;
135pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int;128pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int;
136pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
137pub extern "c" fn socketpair(domain: c_uint, sock_type: c_uint, protocol: c_uint, sv: *[2]fd_t) c_int;129pub extern "c" fn socketpair(domain: c_uint, sock_type: c_uint, protocol: c_uint, sv: *[2]fd_t) c_int;
138pub extern "c" fn listen(sockfd: fd_t, backlog: c_uint) c_int;130pub extern "c" fn listen(sockfd: fd_t, backlog: c_uint) c_int;
139pub extern "c" fn getsockname(sockfd: fd_t, noalias addr: *sockaddr, noalias addrlen: *socklen_t) c_int;131pub extern "c" fn getsockname(sockfd: fd_t, noalias addr: *sockaddr, noalias addrlen: *socklen_t) c_int;
...@@ -161,12 +153,55 @@ pub extern fn recvfrom(...@@ -161,12 +153,55 @@ pub extern fn recvfrom(
161 noalias addrlen: ?*socklen_t,153 noalias addrlen: ?*socklen_t,
162) isize;154) isize;
163155
156pub usingnamespace switch (builtin.os.tag) {
157 .netbsd => struct {
158 pub const clock_getres = __clock_getres50;
159 pub const clock_gettime = __clock_gettime50;
160 pub const fstat = __fstat50;
161 pub const getdents = __getdents30;
162 pub const getrusage = __getrusage50;
163 pub const gettimeofday = __gettimeofday50;
164 pub const nanosleep = __nanosleep50;
165 pub const sched_yield = __libc_thr_yield;
166 pub const sigaction = __sigaction14;
167 pub const sigaltstack = __sigaltstack14;
168 pub const sigprocmask = __sigprocmask14;
169 pub const stat = __stat50;
170 },
171 .macosx, .ios, .watchos, .tvos => struct {
172 // XXX: close -> close$NOCANCEL
173 // XXX: getdirentries -> _getdirentries64
174 pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int;
175 pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int;
176 pub const fstat = @"fstat$INODE64";
177 pub extern "c" fn getrusage(who: c_int, usage: *rusage) c_int;
178 pub extern "c" fn gettimeofday(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int;
179 pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int;
180 pub extern "c" fn sched_yield() c_int;
181 pub extern "c" fn sigaction(sig: c_int, noalias act: *const Sigaction, noalias oact: ?*Sigaction) c_int;
182 pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int;
183 pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
184 pub extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *Stat) c_int;
185 },
186 else => struct {
187 pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int;
188 pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int;
189 pub extern "c" fn fstat(fd: fd_t, buf: *Stat) c_int;
190 pub extern "c" fn getrusage(who: c_int, usage: *rusage) c_int;
191 pub extern "c" fn gettimeofday(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int;
192 pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int;
193 pub extern "c" fn sched_yield() c_int;
194 pub extern "c" fn sigaction(sig: c_int, noalias act: *const Sigaction, noalias oact: ?*Sigaction) c_int;
195 pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int;
196 pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
197 pub extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *Stat) c_int;
198 },
199};
200
164pub extern "c" fn kill(pid: pid_t, sig: c_int) c_int;201pub extern "c" fn kill(pid: pid_t, sig: c_int) c_int;
165pub extern "c" fn getdirentries(fd: fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize;202pub extern "c" fn getdirentries(fd: fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize;
166pub extern "c" fn setgid(ruid: c_uint, euid: c_uint) c_int;203pub extern "c" fn setgid(ruid: c_uint, euid: c_uint) c_int;
167pub extern "c" fn setuid(uid: c_uint) c_int;204pub extern "c" fn setuid(uid: c_uint) c_int;
168pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int;
169pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int;
170205
171pub extern "c" fn aligned_alloc(alignment: usize, size: usize) ?*c_void;206pub extern "c" fn aligned_alloc(alignment: usize, size: usize) ?*c_void;
172pub extern "c" fn malloc(usize) ?*c_void;207pub extern "c" fn malloc(usize) ?*c_void;
...@@ -229,8 +264,6 @@ pub extern "c" fn dn_expand(...@@ -229,8 +264,6 @@ pub extern "c" fn dn_expand(
229 length: c_int,264 length: c_int,
230) c_int;265) c_int;
231266
232pub extern "c" fn sched_yield() c_int;
233
234pub const PTHREAD_MUTEX_INITIALIZER = pthread_mutex_t{};267pub const PTHREAD_MUTEX_INITIALIZER = pthread_mutex_t{};
235pub extern "c" fn pthread_mutex_lock(mutex: *pthread_mutex_t) c_int;268pub extern "c" fn pthread_mutex_lock(mutex: *pthread_mutex_t) c_int;
236pub extern "c" fn pthread_mutex_unlock(mutex: *pthread_mutex_t) c_int;269pub extern "c" fn pthread_mutex_unlock(mutex: *pthread_mutex_t) c_int;
lib/std/c/netbsd.zig+1-1
...@@ -21,7 +21,7 @@ pub extern "c" fn __sigprocmask14(how: c_int, noalias set: ?*const sigset_t, noa...@@ -21,7 +21,7 @@ pub extern "c" fn __sigprocmask14(how: c_int, noalias set: ?*const sigset_t, noa
21pub extern "c" fn __socket30(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;21pub extern "c" fn __socket30(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
22pub extern "c" fn __gettimeofday50(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int;22pub extern "c" fn __gettimeofday50(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int;
23pub extern "c" fn __getrusage50(who: c_int, usage: *rusage) c_int;23pub extern "c" fn __getrusage50(who: c_int, usage: *rusage) c_int;
24// instead of sched_yield24// libc aliases this as sched_yield
25pub extern "c" fn __libc_thr_yield() c_int;25pub extern "c" fn __libc_thr_yield() c_int;
2626
27pub const pthread_mutex_t = extern struct {27pub const pthread_mutex_t = extern struct {
lib/std/os.zig+4-26
...@@ -2553,14 +2553,7 @@ pub const FStatError = error{...@@ -2553,14 +2553,7 @@ pub const FStatError = error{
2553pub fn fstat(fd: fd_t) FStatError!Stat {2553pub fn fstat(fd: fd_t) FStatError!Stat {
2554 var stat: Stat = undefined;2554 var stat: Stat = undefined;
25552555
2556 const symbol_name = if (comptime std.Target.current.isDarwin())2556 switch (errno(system.fstat(fd, &stat))) {
2557 "fstat$INODE64"
2558 else if (std.Target.current.os.tag == .netbsd)
2559 "__fstat50"
2560 else
2561 "fstat";
2562
2563 switch (errno(@field(system, symbol_name)(fd, &stat))) {
2564 0 => return stat,2557 0 => return stat,
2565 EINVAL => unreachable,2558 EINVAL => unreachable,
2566 EBADF => unreachable, // Always a race condition.2559 EBADF => unreachable, // Always a race condition.
...@@ -3427,12 +3420,7 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void {...@@ -3427,12 +3420,7 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void {
3427 return;3420 return;
3428 }3421 }
34293422
3430 const symbol_name = if (std.Target.current.os.tag == .netbsd)3423 switch (errno(system.clock_gettime(clk_id, tp))) {
3431 "__clock_gettime50"
3432 else
3433 "clock_gettime";
3434
3435 switch (errno(@field(system, symbol_name)(clk_id, tp))) {
3436 0 => return,3424 0 => return,
3437 EFAULT => unreachable,3425 EFAULT => unreachable,
3438 EINVAL => return error.UnsupportedClock,3426 EINVAL => return error.UnsupportedClock,
...@@ -3454,12 +3442,7 @@ pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void {...@@ -3454,12 +3442,7 @@ pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void {
3454 return;3442 return;
3455 }3443 }
34563444
3457 const symbol_name = if (std.Target.current.os.tag == .netbsd)3445 switch (errno(system.clock_getres(clk_id, res))) {
3458 "__clock_getres50"
3459 else
3460 "clock_getres";
3461
3462 switch (errno(@field(system, symbol_name)(clk_id, res))) {
3463 0 => return,3446 0 => return,
3464 EFAULT => unreachable,3447 EFAULT => unreachable,
3465 EINVAL => return error.UnsupportedClock,3448 EINVAL => return error.UnsupportedClock,
...@@ -3525,12 +3508,7 @@ pub const SigaltstackError = error{...@@ -3525,12 +3508,7 @@ pub const SigaltstackError = error{
3525} || UnexpectedError;3508} || UnexpectedError;
35263509
3527pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {3510pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {
3528 const symbol_name = if (std.Target.current.os.tag == .netbsd)3511 switch (errno(system.sigaltstack(ss, old_ss))) {
3529 "__sigaltstack14"
3530 else
3531 "sigaltstack";
3532
3533 switch (errno(@field(system, symbol_name)(ss, old_ss))) {
3534 0 => return,3512 0 => return,
3535 EFAULT => unreachable,3513 EFAULT => unreachable,
3536 EINVAL => unreachable,3514 EINVAL => unreachable,