diff --git a/src/syscall/syscall_libc.go b/src/syscall/syscall_libc.go index bc635963..4767703a 100644 --- a/src/syscall/syscall_libc.go +++ b/src/syscall/syscall_libc.go @@ -80,6 +80,22 @@ func Getenv(key string) (value string, found bool) { } } +func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) { + addr := libc_mmap(nil, uintptr(length), int32(prot), int32(flags), int32(fd), uintptr(offset)) + if addr == unsafe.Pointer(^uintptr(0)) { + return nil, getErrno() + } + return (*[30]byte)(addr)[:length], nil +} + +func Mprotect(b []byte, prot int) (err error) { + errCode := libc_mprotect(unsafe.Pointer(&b[0]), uintptr(len(b)), int32(prot)) + if errCode != 0 { + err = getErrno() + } + return +} + func splitSlice(p []byte) (buf *byte, len uintptr) { slice := (*sliceHeader)(unsafe.Pointer(&p)) return slice.buf, slice.len @@ -104,3 +120,11 @@ func libc_open(pathname *byte, flags int32, mode uint32) int32 // int close(int fd) //export close func libc_close(fd int32) int32 + +// void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); +//export mmap +func libc_mmap(addr unsafe.Pointer, length uintptr, prot, flags, fd int32, offset uintptr) unsafe.Pointer + +// int mprotect(void *addr, size_t len, int prot); +//export mprotect +func libc_mprotect(addr unsafe.Pointer, len uintptr, prot int32) int32 diff --git a/src/syscall/syscall_libc_darwin.go b/src/syscall/syscall_libc_darwin.go index fb8655c8..6ca95f53 100644 --- a/src/syscall/syscall_libc_darwin.go +++ b/src/syscall/syscall_libc_darwin.go @@ -78,3 +78,18 @@ const ( O_TRUNC = 0x400 O_EXCL = 0x800 ) + +// Source: https://opensource.apple.com/source/xnu/xnu-7195.81.3/bsd/sys/mman.h.auto.html +const ( + PROT_NONE = 0x00 // no permissions + PROT_READ = 0x01 // pages can be read + PROT_WRITE = 0x02 // pages can be written + PROT_EXEC = 0x04 // pages can be executed + + MAP_SHARED = 0x0001 // share changes + MAP_PRIVATE = 0x0002 // changes are private + + MAP_FILE = 0x0000 // map from file (default) + MAP_ANON = 0x1000 // allocated from memory, swap space + MAP_ANONYMOUS = MAP_ANON +)