From 3dbaad0a1f27b96462541e1189efe37ffa913e9a Mon Sep 17 00:00:00 2001 From: Albert Ou Date: Tue, 13 Dec 2016 15:05:19 -0800 Subject: Refactor libc stubs into libwrap The --wrap feature of GNU ld supports a cleaner framework for linking in alternative implementations of libc functions without cpp hacks. Place wrappers in separate object files to reduce static code size. --- bsp/libwrap/sys/_exit.c | 17 +++++++++++++++++ bsp/libwrap/sys/close.c | 9 +++++++++ bsp/libwrap/sys/execve.c | 9 +++++++++ bsp/libwrap/sys/fork.c | 9 +++++++++ bsp/libwrap/sys/fstat.c | 16 ++++++++++++++++ bsp/libwrap/sys/getpid.c | 6 ++++++ bsp/libwrap/sys/isatty.c | 11 +++++++++++ bsp/libwrap/sys/kill.c | 9 +++++++++ bsp/libwrap/sys/link.c | 9 +++++++++ bsp/libwrap/sys/lseek.c | 14 ++++++++++++++ bsp/libwrap/sys/open.c | 9 +++++++++ bsp/libwrap/sys/openat.c | 9 +++++++++ bsp/libwrap/sys/read.c | 30 ++++++++++++++++++++++++++++++ bsp/libwrap/sys/sbrk.c | 16 ++++++++++++++++ bsp/libwrap/sys/stat.c | 10 ++++++++++ bsp/libwrap/sys/stub.h | 10 ++++++++++ bsp/libwrap/sys/times.c | 10 ++++++++++ bsp/libwrap/sys/unlink.c | 9 +++++++++ bsp/libwrap/sys/wait.c | 9 +++++++++ bsp/libwrap/sys/write.c | 29 +++++++++++++++++++++++++++++ 20 files changed, 250 insertions(+) create mode 100644 bsp/libwrap/sys/_exit.c create mode 100644 bsp/libwrap/sys/close.c create mode 100644 bsp/libwrap/sys/execve.c create mode 100644 bsp/libwrap/sys/fork.c create mode 100644 bsp/libwrap/sys/fstat.c create mode 100644 bsp/libwrap/sys/getpid.c create mode 100644 bsp/libwrap/sys/isatty.c create mode 100644 bsp/libwrap/sys/kill.c create mode 100644 bsp/libwrap/sys/link.c create mode 100644 bsp/libwrap/sys/lseek.c create mode 100644 bsp/libwrap/sys/open.c create mode 100644 bsp/libwrap/sys/openat.c create mode 100644 bsp/libwrap/sys/read.c create mode 100644 bsp/libwrap/sys/sbrk.c create mode 100644 bsp/libwrap/sys/stat.c create mode 100644 bsp/libwrap/sys/stub.h create mode 100644 bsp/libwrap/sys/times.c create mode 100644 bsp/libwrap/sys/unlink.c create mode 100644 bsp/libwrap/sys/wait.c create mode 100644 bsp/libwrap/sys/write.c (limited to 'bsp/libwrap/sys') diff --git a/bsp/libwrap/sys/_exit.c b/bsp/libwrap/sys/_exit.c new file mode 100644 index 0000000..7261891 --- /dev/null +++ b/bsp/libwrap/sys/_exit.c @@ -0,0 +1,17 @@ +/* See LICENSE of license details. */ + +#include +#include "platform.h" + +void __wrap__exit(int code) +{ +//volatile uint32_t* leds = (uint32_t*) (GPIO_BASE_ADDR + GPIO_OUT_OFFSET); + const char message[] = "\nProgam has exited with code:"; +//*leds = (~(code)); + + write(STDERR_FILENO, message, sizeof(message) - 1); + write_hex(STDERR_FILENO, code); + write(STDERR_FILENO, "\n", 1); + + for (;;); +} diff --git a/bsp/libwrap/sys/close.c b/bsp/libwrap/sys/close.c new file mode 100644 index 0000000..e4f8e14 --- /dev/null +++ b/bsp/libwrap/sys/close.c @@ -0,0 +1,9 @@ +/* See LICENSE of license details. */ + +#include +#include "stub.h" + +int __wrap_close(int fd) +{ + return _stub(EBADF); +} diff --git a/bsp/libwrap/sys/execve.c b/bsp/libwrap/sys/execve.c new file mode 100644 index 0000000..6178a01 --- /dev/null +++ b/bsp/libwrap/sys/execve.c @@ -0,0 +1,9 @@ +/* See LICENSE of license details. */ + +#include +#include "stub.h" + +int __wrap_execve(const char* name, char* const argv[], char* const env[]) +{ + return _stub(ENOMEM); +} diff --git a/bsp/libwrap/sys/fork.c b/bsp/libwrap/sys/fork.c new file mode 100644 index 0000000..13a3e65 --- /dev/null +++ b/bsp/libwrap/sys/fork.c @@ -0,0 +1,9 @@ +/* See LICENSE of license details. */ + +#include +#include "stub.h" + +int fork(void) +{ + return _stub(EAGAIN); +} diff --git a/bsp/libwrap/sys/fstat.c b/bsp/libwrap/sys/fstat.c new file mode 100644 index 0000000..6ea3e6a --- /dev/null +++ b/bsp/libwrap/sys/fstat.c @@ -0,0 +1,16 @@ +/* See LICENSE of license details. */ + +#include +#include +#include +#include "stub.h" + +int __wrap_fstat(int fd, struct stat* st) +{ + if (isatty(fd)) { + st->st_mode = S_IFCHR; + return 0; + } + + return _stub(EBADF); +} diff --git a/bsp/libwrap/sys/getpid.c b/bsp/libwrap/sys/getpid.c new file mode 100644 index 0000000..5aa510b --- /dev/null +++ b/bsp/libwrap/sys/getpid.c @@ -0,0 +1,6 @@ +/* See LICENSE of license details. */ + +int __wrap_getpid(void) +{ + return 1; +} diff --git a/bsp/libwrap/sys/isatty.c b/bsp/libwrap/sys/isatty.c new file mode 100644 index 0000000..55eab0a --- /dev/null +++ b/bsp/libwrap/sys/isatty.c @@ -0,0 +1,11 @@ +/* See LICENSE of license details. */ + +#include + +int __wrap_isatty(int fd) +{ + if (fd == STDOUT_FILENO || fd == STDERR_FILENO) + return 1; + + return 0; +} diff --git a/bsp/libwrap/sys/kill.c b/bsp/libwrap/sys/kill.c new file mode 100644 index 0000000..9c56632 --- /dev/null +++ b/bsp/libwrap/sys/kill.c @@ -0,0 +1,9 @@ +/* See LICENSE of license details. */ + +#include +#include "stub.h" + +int __wrap_kill(int pid, int sig) +{ + return _stub(EINVAL); +} diff --git a/bsp/libwrap/sys/link.c b/bsp/libwrap/sys/link.c new file mode 100644 index 0000000..9340cad --- /dev/null +++ b/bsp/libwrap/sys/link.c @@ -0,0 +1,9 @@ +/* See LICENSE of license details. */ + +#include +#include "stub.h" + +int __wrap_link(const char *old_name, const char *new_name) +{ + return _stub(EMLINK); +} diff --git a/bsp/libwrap/sys/lseek.c b/bsp/libwrap/sys/lseek.c new file mode 100644 index 0000000..46f58fa --- /dev/null +++ b/bsp/libwrap/sys/lseek.c @@ -0,0 +1,14 @@ +/* See LICENSE of license details. */ + +#include +#include +#include +#include "stub.h" + +off_t __wrap_lseek(int fd, off_t ptr, int dir) +{ + if (isatty(fd)) + return 0; + + return _stub(EBADF); +} diff --git a/bsp/libwrap/sys/open.c b/bsp/libwrap/sys/open.c new file mode 100644 index 0000000..d1871f9 --- /dev/null +++ b/bsp/libwrap/sys/open.c @@ -0,0 +1,9 @@ +/* See LICENSE of license details. */ + +#include +#include "stub.h" + +int __wrap_open(const char* name, int flags, int mode) +{ + return _stub(ENOENT); +} diff --git a/bsp/libwrap/sys/openat.c b/bsp/libwrap/sys/openat.c new file mode 100644 index 0000000..7f1c945 --- /dev/null +++ b/bsp/libwrap/sys/openat.c @@ -0,0 +1,9 @@ +/* See LICENSE of license details. */ + +#include +#include "stub.h" + +int __wrap_openat(int dirfd, const char* name, int flags, int mode) +{ + return _stub(ENOENT); +} diff --git a/bsp/libwrap/sys/read.c b/bsp/libwrap/sys/read.c new file mode 100644 index 0000000..4e57f08 --- /dev/null +++ b/bsp/libwrap/sys/read.c @@ -0,0 +1,30 @@ +/* See LICENSE of license details. */ + +#include +#include +#include +#include + +#include "platform.h" +#include "stub.h" + +ssize_t __wrap_read(int fd, void* ptr, size_t len) +{ + uint8_t * current = (uint8_t *)ptr; + volatile uint32_t * uart_rx = (uint32_t *)(UART0_BASE_ADDR + UART_REG_RXFIFO); + volatile uint8_t * uart_rx_cnt = (uint8_t *)(UART0_BASE_ADDR + UART_REG_RXCTRL + 2); + + ssize_t result = 0; + + if (isatty(fd)) { + for (current = (uint8_t *)ptr; + (current < ((uint8_t *)ptr) + len) && (*uart_rx_cnt > 0); + current ++) { + *current = *uart_rx; + result++; + } + return result; + } + + return _stub(EBADF); +} diff --git a/bsp/libwrap/sys/sbrk.c b/bsp/libwrap/sys/sbrk.c new file mode 100644 index 0000000..6e6b36a --- /dev/null +++ b/bsp/libwrap/sys/sbrk.c @@ -0,0 +1,16 @@ +/* See LICENSE of license details. */ + +#include + +void *__wrap_sbrk(ptrdiff_t incr) +{ + extern char _end[]; + extern char _heap_end[]; + static char *curbrk = _end; + + if ((curbrk + incr < _end) || (curbrk + incr > _heap_end)) + return NULL - 1; + + curbrk += incr; + return curbrk - incr; +} diff --git a/bsp/libwrap/sys/stat.c b/bsp/libwrap/sys/stat.c new file mode 100644 index 0000000..1ccc2f4 --- /dev/null +++ b/bsp/libwrap/sys/stat.c @@ -0,0 +1,10 @@ +/* See LICENSE of license details. */ + +#include +#include +#include "stub.h" + +int __wrap_stat(const char* file, struct stat* st) +{ + return _stub(EACCES); +} diff --git a/bsp/libwrap/sys/stub.h b/bsp/libwrap/sys/stub.h new file mode 100644 index 0000000..fb5e5be --- /dev/null +++ b/bsp/libwrap/sys/stub.h @@ -0,0 +1,10 @@ +/* See LICENSE of license details. */ +#ifndef _SIFIVE_SYS_STUB_H +#define _SIFIVE_SYS_STUB_H + +static inline int _stub(int err) +{ + return -1; +} + +#endif /* _SIFIVE_SYS_STUB_H */ diff --git a/bsp/libwrap/sys/times.c b/bsp/libwrap/sys/times.c new file mode 100644 index 0000000..26a9566 --- /dev/null +++ b/bsp/libwrap/sys/times.c @@ -0,0 +1,10 @@ +/* See LICENSE of license details. */ + +#include +#include +#include "stub.h" + +clock_t __wrap_times(struct tms* buf) +{ + return _stub(EACCES); +} diff --git a/bsp/libwrap/sys/unlink.c b/bsp/libwrap/sys/unlink.c new file mode 100644 index 0000000..b62b1ba --- /dev/null +++ b/bsp/libwrap/sys/unlink.c @@ -0,0 +1,9 @@ +/* See LICENSE of license details. */ + +#include +#include "stub.h" + +int __wrap_unlink(const char* name) +{ + return _stub(ENOENT); +} diff --git a/bsp/libwrap/sys/wait.c b/bsp/libwrap/sys/wait.c new file mode 100644 index 0000000..ea3225b --- /dev/null +++ b/bsp/libwrap/sys/wait.c @@ -0,0 +1,9 @@ +/* See LICENSE of license details. */ + +#include +#include "stub.h" + +int wait(int* status) +{ + return _stub(ECHILD); +} diff --git a/bsp/libwrap/sys/write.c b/bsp/libwrap/sys/write.c new file mode 100644 index 0000000..d00eb17 --- /dev/null +++ b/bsp/libwrap/sys/write.c @@ -0,0 +1,29 @@ +/* See LICENSE of license details. */ + +#include +#include +#include +#include + +#include "platform.h" +#include "stub.h" + +ssize_t __wrap_write(int fd, const void* ptr, size_t len) +{ + const uint8_t * current = (const char *)ptr; + + if (isatty(fd)) { + for (size_t jj = 0; jj < len; jj++) { + while (UART0_REG(UART_REG_TXFIFO) & 0x80000000) ; + UART0_REG(UART_REG_TXFIFO) = current[jj]; + + if (current[jj] == '\n') { + while (UART0_REG(UART_REG_TXFIFO) & 0x80000000) ; + UART0_REG(UART_REG_TXFIFO) = '\r'; + } + } + return len; + } + + return _stub(EBADF); +} -- cgit v1.2.1-18-gbd029