-
Notifications
You must be signed in to change notification settings - Fork 919
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
darwin: use custom syscall pkg that uses libsystem
Go 1.12 switched to using libSystem.dylib for system calls, because Apple recommends against doing direct system calls that Go 1.11 and earlier did. For more information, see: golang/go#17490 https://developer.apple.com/library/archive/qa/qa1118/_index.html While the old syscall package was relatively easy to support in TinyGo (just implement syscall.Syscall*), this got a whole lot harder with Go 1.12 as all syscalls now go through CGo magic to call the underlying libSystem functions. Therefore, this commit overrides the stdlib syscall package with a custom package that performs calls with libc (libSystem). This may be useful not just for darwin but for other platforms as well that do not place the stable ABI at the syscall boundary like Linux but at the libc boundary. Only a very minimal part of the syscall package has been implemented, to get the tests to pass. More calls can easily be added in the future.
- Loading branch information
1 parent
2523772
commit 86f8778
Showing
8 changed files
with
143 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package syscall | ||
|
||
// Most code here has been copied from the Go sources: | ||
// https://github.com/golang/go/blob/go1.12/src/syscall/syscall_js.go | ||
// It has the following copyright note: | ||
// | ||
// Copyright 2018 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// An Errno is an unsigned number describing an error condition. | ||
// It implements the error interface. The zero Errno is by convention | ||
// a non-error, so code to convert from Errno to error should use: | ||
// err = nil | ||
// if errno != 0 { | ||
// err = errno | ||
// } | ||
type Errno uintptr | ||
|
||
func (e Errno) Error() string { | ||
return "errno " + itoa(int(e)) | ||
} | ||
|
||
func (e Errno) Temporary() bool { | ||
return e == EINTR || e == EMFILE || e.Timeout() | ||
} | ||
|
||
func (e Errno) Timeout() bool { | ||
return e == EAGAIN || e == EWOULDBLOCK || e == ETIMEDOUT | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package syscall | ||
|
||
// This file defines errno and constants to match the darwin libsystem ABI. | ||
// Values have been determined experimentally by compiling some C code on macOS | ||
// with Clang and looking at the resulting LLVM IR. | ||
|
||
// This function returns the error location in the darwin ABI. | ||
// Discovered by compiling the following code using Clang: | ||
// | ||
// #include <errno.h> | ||
// int getErrno() { | ||
// return errno; | ||
// } | ||
// | ||
//go:export __error | ||
func libc___error() *int32 | ||
|
||
// getErrno returns the current C errno. It may not have been caused by the last | ||
// call, so it should only be relied upon when the last call indicates an error | ||
// (for example, by returning -1). | ||
func getErrno() Errno { | ||
errptr := libc___error() | ||
return Errno(uintptr(*errptr)) | ||
} | ||
|
||
const ( | ||
ENOENT Errno = 2 | ||
EINTR Errno = 4 | ||
EMFILE Errno = 24 | ||
EAGAIN Errno = 35 | ||
ETIMEDOUT Errno = 60 | ||
ENOSYS Errno = 78 | ||
EWOULDBLOCK Errno = EAGAIN | ||
) | ||
|
||
type Signal int | ||
|
||
const ( | ||
SIGCHLD Signal = 20 | ||
SIGINT Signal = 2 | ||
SIGKILL Signal = 9 | ||
SIGTRAP Signal = 5 | ||
SIGQUIT Signal = 3 | ||
SIGTERM Signal = 15 | ||
) | ||
|
||
const ( | ||
O_RDONLY = 0 | ||
O_WRONLY = 1 | ||
O_RDWR = 2 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// +build darwin | ||
|
||
package syscall | ||
|
||
import ( | ||
"unsafe" | ||
) | ||
|
||
func Close(fd int) (err error) { | ||
return ENOSYS // TODO | ||
} | ||
|
||
func Write(fd int, p []byte) (n int, err error) { | ||
buf, count := splitSlice(p) | ||
n = libc_write(int32(fd), buf, uint(count)) | ||
if n < 0 { | ||
err = getErrno() | ||
} | ||
return | ||
} | ||
|
||
func Read(fd int, p []byte) (n int, err error) { | ||
return 0, ENOSYS // TODO | ||
} | ||
|
||
func Seek(fd int, offset int64, whence int) (off int64, err error) { | ||
return 0, ENOSYS // TODO | ||
} | ||
|
||
func Open(path string, mode int, perm uint32) (fd int, err error) { | ||
return 0, ENOSYS // TODO | ||
} | ||
|
||
func Kill(pid int, sig Signal) (err error) { | ||
return ENOSYS // TODO | ||
} | ||
|
||
func Getpid() (pid int) { | ||
panic("unimplemented: getpid") // TODO | ||
} | ||
|
||
func Getenv(key string) (value string, found bool) { | ||
return "", false // TODO | ||
} | ||
|
||
func splitSlice(p []byte) (buf *byte, len uintptr) { | ||
slice := (*struct { | ||
buf *byte | ||
len uintptr | ||
cap uintptr | ||
})(unsafe.Pointer(&p)) | ||
return slice.buf, slice.len | ||
} | ||
|
||
// ssize_t write(int fd, const void *buf, size_t count) | ||
//go:export write | ||
func libc_write(fd int32, buf *byte, count uint) int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters