From 1d2a6f21294f8390b683e4e097cb49210ed832d1 Mon Sep 17 00:00:00 2001 From: johannst Date: Fri, 26 Mar 2021 23:17:46 +0100 Subject: Added dyn allocator + syscall wrappers + minor fixes. --- lib/src/syscalls.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 lib/src/syscalls.c (limited to 'lib/src/syscalls.c') diff --git a/lib/src/syscalls.c b/lib/src/syscalls.c new file mode 100644 index 0000000..d6ac1a5 --- /dev/null +++ b/lib/src/syscalls.c @@ -0,0 +1,22 @@ +// Copyright (c) 2021 Johannes Stoelp + +#include // __NR_* +#include +#include + +int open(const char* path, int flags) { + return syscall2(__NR_open, path, flags); +} + +ssize_t read(int fd, void* buf, size_t count) { + return syscall3(__NR_read, fd, buf, count); +} + +off_t lseek(int fd, off_t offset, int whence) { + return syscall3(__NR_lseek, fd, offset, whence); +} + +void _exit(int status) { + syscall1(__NR_exit, status); + __builtin_unreachable(); +} -- cgit v1.2.3 From 26088bf37ef85681cf2158fdff82233b1b0c9bfd Mon Sep 17 00:00:00 2001 From: johannst Date: Sat, 17 Apr 2021 23:41:17 +0200 Subject: added close,access,write,pread,mmap,munmap syscalls + syscall ret handler --- lib/src/syscalls.c | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) (limited to 'lib/src/syscalls.c') diff --git a/lib/src/syscalls.c b/lib/src/syscalls.c index d6ac1a5..074fced 100644 --- a/lib/src/syscalls.c +++ b/lib/src/syscalls.c @@ -4,16 +4,56 @@ #include #include +// Storage for `dynld_errno`. +int dynld_errno; + +// Convert return value to errno/ret. +static long syscall_ret(unsigned long ret) { + if (ret > (unsigned long)-4096ul) { + dynld_errno = -ret; + return -1; + } + return ret; +} + int open(const char* path, int flags) { - return syscall2(__NR_open, path, flags); + long ret = syscall2(__NR_open, path, flags); + return syscall_ret(ret); +} + +int close(int fd) { + long ret = syscall1(__NR_close, fd); + return syscall_ret(ret); +} + +int access(const char* path, int mode) { + long ret = syscall2(__NR_access, path, mode); + return syscall_ret(ret); +} + +ssize_t write(int fd, const void* buf, size_t count) { + long ret = syscall3(__NR_write, fd, buf, count); + return syscall_ret(ret); } ssize_t read(int fd, void* buf, size_t count) { - return syscall3(__NR_read, fd, buf, count); + long ret = syscall3(__NR_read, fd, buf, count); + return syscall_ret(ret); +} + +ssize_t pread(int fd, void* buf, size_t count, off_t offset) { + long ret = syscall4(__NR_read, fd, buf, count, offset); + return syscall_ret(ret); +} + +void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) { + long ret = syscall6(__NR_mmap, addr, length, prot, flags, fd, offset); + return (void*)syscall_ret(ret); } -off_t lseek(int fd, off_t offset, int whence) { - return syscall3(__NR_lseek, fd, offset, whence); +int munmap(void* addr, size_t length) { + long ret = syscall2(__NR_munmap, addr, length); + return syscall_ret(ret); } void _exit(int status) { -- cgit v1.2.3