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/env/common.mk | 33 ++++--- bsp/env/freedom-e300-arty/init.c | 1 + bsp/env/freedom-e300-hifive1/init.c | 1 + bsp/env/syscall.c | 190 ------------------------------------ 4 files changed, 24 insertions(+), 201 deletions(-) delete mode 100644 bsp/env/syscall.c (limited to 'bsp/env') diff --git a/bsp/env/common.mk b/bsp/env/common.mk index 8d91805..0f5e9a1 100644 --- a/bsp/env/common.mk +++ b/bsp/env/common.mk @@ -1,5 +1,13 @@ # See LICENSE for license details. +ifndef _SIFIVE_MK_COMMON +_SIFIVE_MK_COMMON := # defined + +.PHONY: all +all: $(TARGET) + +include $(BSP_BASE)/libwrap/libwrap.mk + BOARD ?= freedom-e300-hifive1 ENV_DIR = $(BSP_BASE)/env PLATFORM_DIR = $(ENV_DIR)/$(BOARD) @@ -7,28 +15,32 @@ PLATFORM_DIR = $(ENV_DIR)/$(BOARD) ASM_SRCS += $(PLATFORM_DIR)/start.S ASM_SRCS += $(PLATFORM_DIR)/entry.S C_SRCS += $(PLATFORM_DIR)/init.c -C_SRCS += $(ENV_DIR)/syscall.c LINKER_SCRIPT := $(PLATFORM_DIR)/link.lds -INCLUDES += -I. INCLUDES += -I$(BSP_BASE)/include INCLUDES += -I$(ENV_DIR) INCLUDES += -I$(PLATFORM_DIR) -CC := $(BSP_BASE)/../toolchain/bin/riscv32-unknown-elf-gcc +TOOL_DIR = $(BSP_BASE)/../toolchain/bin -LDFLAGS += -T $(LINKER_SCRIPT) -nostdlib -nostartfiles -lc -lgcc +CC := $(TOOL_DIR)/riscv32-unknown-elf-gcc +AR := $(TOOL_DIR)/riscv32-unknown-elf-ar + +LDFLAGS += -T $(LINKER_SCRIPT) -nostartfiles LDFLAGS += -L$(ENV_DIR) -ASM_OBJS := $(patsubst %.S,%.o,$(ASM_SRCS)) -C_OBJS := $(patsubst %.c,%.o,$(C_SRCS)) +ASM_OBJS := $(ASM_SRCS:.S=.o) +C_OBJS := $(C_SRCS:.c=.o) LINK_OBJS += $(ASM_OBJS) $(C_OBJS) +LINK_DEPS += $(LINKER_SCRIPT) + +CLEAN_OBJS += $(TARGET) $(LINK_OBJS) CFLAGS += -g -$(TARGET): $(LINK_OBJS) $(LINKER_SCRIPT) +$(TARGET): $(LINK_OBJS) $(LINK_DEPS) $(CC) $(CFLAGS) $(INCLUDES) $(LINK_OBJS) -o $@ $(LDFLAGS) $(ASM_OBJS): %.o: %.S $(HEADERS) @@ -37,9 +49,8 @@ $(ASM_OBJS): %.o: %.S $(HEADERS) $(C_OBJS): %.o: %.c $(HEADERS) $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< -all: $(TARGET) - +.PHONY: clean clean: - rm -f $(TARGET) $(LINK_OBJS) + rm -f $(CLEAN_OBJS) -.PHONY: all clean +endif # _SIFIVE_MK_COMMON diff --git a/bsp/env/freedom-e300-arty/init.c b/bsp/env/freedom-e300-arty/init.c index 1f26b19..a7e368e 100644 --- a/bsp/env/freedom-e300-arty/init.c +++ b/bsp/env/freedom-e300-arty/init.c @@ -1,5 +1,6 @@ //See LICENSE for license details. #include +#include #include #include "platform.h" diff --git a/bsp/env/freedom-e300-hifive1/init.c b/bsp/env/freedom-e300-hifive1/init.c index 268d41e..c088079 100644 --- a/bsp/env/freedom-e300-hifive1/init.c +++ b/bsp/env/freedom-e300-hifive1/init.c @@ -1,4 +1,5 @@ #include +#include #include #include "platform.h" diff --git a/bsp/env/syscall.c b/bsp/env/syscall.c deleted file mode 100644 index 1b5de50..0000000 --- a/bsp/env/syscall.c +++ /dev/null @@ -1,190 +0,0 @@ -// See LICENSE for license details. - -/* This is an incomplete version of a syscall library, - * which really only supports simple reads and writes over UART. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "platform.h" - -void write_hex(int fd, uint32_t hex) -{ - uint8_t ii; - uint8_t jj; - char towrite; - write(fd , "0x", 2); - for (ii = 8 ; ii > 0; ii--) { - jj = ii - 1; - uint8_t digit = ((hex & (0xF << (jj*4))) >> (jj*4)); - towrite = digit < 0xA ? ('0' + digit) : ('A' + (digit - 0xA)); - write(fd, &towrite, 1); - } -} - -void _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, strlen(message)); - write_hex(STDERR_FILENO, code); - write(STDERR_FILENO, "\n", 1); - - while (1) ; -} - -void *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; -} - -static int stub(int err) -{ - return -1; -} - -int open(const char* name, int flags, int mode) -{ - return stub(ENOENT); -} - -int openat(int dirfd, const char* name, int flags, int mode) -{ - return stub(ENOENT); -} - -int close(int fd) -{ - return stub(EBADF); -} - -int execve(const char* name, char* const argv[], char* const env[]) -{ - return stub(ENOMEM); -} - -int fork() -{ - return stub(EAGAIN); -} - -int fstat(int fd, struct stat *st) -{ - if (isatty(fd)) { - st->st_mode = S_IFCHR; - return 0; - } - - return stub(EBADF); -} - -int getpid() -{ - return 1; -} - -int isatty(int fd) -{ - if (fd == STDOUT_FILENO || fd == STDERR_FILENO) - return 1; - - return 0; -} - -int kill(int pid, int sig) -{ - return stub(EINVAL); -} - -int link(const char *old_name, const char *new_name) -{ - return stub(EMLINK); -} - -off_t lseek(int fd, off_t ptr, int dir) -{ - if (isatty(fd)) - return 0; - - return stub(EBADF); -} - -ssize_t 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); -} - -int stat(const char* file, struct stat* st) -{ - return stub(EACCES); -} - -clock_t times(struct tms* buf) -{ - return stub(EACCES); -} - -int unlink(const char* name) -{ - return stub(ENOENT); -} - -int wait(int* status) -{ - return stub(ECHILD); -} - -ssize_t 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