From f9364c667bba2e893a1514f684879343f4867c71 Mon Sep 17 00:00:00 2001 From: Eyck Jentzsch Date: Tue, 16 Jan 2024 12:26:47 +0100 Subject: [PATCH] adds newlib-nano settings --- env/common-gcc.mk | 22 ++++++++---- libwrap/libwrap.mk | 6 ++-- newlib-nano/.gitignore | 1 + newlib-nano/lib.mk | 16 +++++++++ newlib-nano/read.c | 22 ++++++++++++ newlib-nano/stub.h | 8 +++++ newlib-nano/syscalls.c | 79 ++++++++++++++++++++++++++++++++++++++++++ newlib-nano/write.c | 24 +++++++++++++ 8 files changed, 168 insertions(+), 10 deletions(-) create mode 100644 newlib-nano/.gitignore create mode 100644 newlib-nano/lib.mk create mode 100644 newlib-nano/read.c create mode 100644 newlib-nano/stub.h create mode 100644 newlib-nano/syscalls.c create mode 100644 newlib-nano/write.c diff --git a/env/common-gcc.mk b/env/common-gcc.mk index 92ebf02..02ffed8 100644 --- a/env/common-gcc.mk +++ b/env/common-gcc.mk @@ -6,15 +6,16 @@ TL_TARGET?=all .PHONY: $(TL_TARGET) $(TL_TARGET): $(TARGET) +ENV_DIR:=$(dir $(lastword $(MAKEFILE_LIST))) +BSP_BASE=$(ENV_DIR)/.. +PLATFORM_DIR = $(ENV_DIR)/$(BOARD) + include $(BSP_BASE)/libwrap/libwrap.mk BOARD?=iss -ENV_DIR = $(BSP_BASE)/env -PLATFORM_DIR = $(ENV_DIR)/$(BOARD) -ASM_SRCS += $(ENV_DIR)/start.S -ASM_SRCS += $(ENV_DIR)/entry.S -C_SRCS += $(PLATFORM_DIR)/init.c +ASM_SRCS += $(ENV_DIR)/start.S $(ENV_DIR)/entry.S +C_SRCS += $(PLATFORM_DIR)/init.c LINKER_SCRIPT ?= $(PLATFORM_DIR)/$(LINK_TARGET).lds @@ -26,7 +27,13 @@ INCLUDES += -I$(PLATFORM_DIR) LDFLAGS += -march=$(RISCV_ARCH) -mabi=$(RISCV_ABI) LDFLAGS += -L$(ENV_DIR) LD_SCRIPT += -T $(LINKER_SCRIPT) -Wl,--no-warn-rwx-segments -Wl,-Map=$(TARGET).map -nostartfiles -# --specs=nano.specs + +ifneq (,$(findstring specs=nano.specs,$(LDFLAGS), LD_SCRIPT)) + # Found +else + # Not found +endif + ASM_OBJS := $(ASM_SRCS:.S=.o) C_OBJS := $(C_SRCS:.c=.o) @@ -57,7 +64,8 @@ OBJCOPY := $(TOOL_DIR)$(TRIPLET)-objcopy ifndef NO_DEFAULT_LINK $(TARGET).elf: $(LINK_OBJS) $(LINK_DEPS) - $(LD) $(LINK_OBJS) $(LDFLAGS) $(LIBWRAP) $(LD_SCRIPT) -o $@ + echo LINK_OBJS: $(LINK_OBJS) + $(LD) $(LINK_OBJS) $(LDFLAGS) $(LIBWRAP_LDFLAGS) $(LIBWRAP) $(LD_SCRIPT) -o $@ $(OBJDUMP) -d -S $@ > $(TARGET).dis endif diff --git a/libwrap/libwrap.mk b/libwrap/libwrap.mk index 8464a3c..aecf019 100644 --- a/libwrap/libwrap.mk +++ b/libwrap/libwrap.mk @@ -59,9 +59,9 @@ LIBWRAP := libwrap.a LINK_DEPS += $(LIBWRAP) -LDFLAGS += $(foreach s,$(LIBWRAP_SYMS),-Wl,--wrap=$(s)) -#LDFLAGS += $(foreach s,$(LIBWRAP_SYMS),-Wl,--wrap=_$(s)) -LDFLAGS += -L. -Wl,--start-group -lwrap -lc -Wl,--end-group +LIBWRAP_LDFLAGS += $(foreach s,$(LIBWRAP_SYMS),-Wl,--wrap=$(s)) +#LIBWRAP_LDFLAGS += $(foreach s,$(LIBWRAP_SYMS),-Wl,--wrap=_$(s)) +LIBWRAP_LDFLAGS += -L. -Wl,--start-group -lwrap -lc -Wl,--end-group CLEAN_OBJS += $(LIBWRAP_OBJS) diff --git a/newlib-nano/.gitignore b/newlib-nano/.gitignore new file mode 100644 index 0000000..cc5bb74 --- /dev/null +++ b/newlib-nano/.gitignore @@ -0,0 +1 @@ +/*.o diff --git a/newlib-nano/lib.mk b/newlib-nano/lib.mk new file mode 100644 index 0000000..986132d --- /dev/null +++ b/newlib-nano/lib.mk @@ -0,0 +1,16 @@ +#CFLAGS+=--specs=nano.specs +#LDFLAGS+=--specs=nano.specs +#CFLAGS+=--specs=rv32imac/ilp32/nano.specs +#LDFLAGS+=--specs=rv32imac/ilp32/nano.specs + +NANO_LIB_DIR := $(dir $(lastword $(MAKEFILE_LIST))) +NANO_LIB_SYMS := read write syscalls + +NANO_LIB_SRCS := $(foreach s,$(NANO_LIB_SYMS),$(NANO_LIB_DIR)/$(s).c) +#NANO_LIB_SRCS := $(foreach f,$(LIB_SRCS),$(LIB_DIR)/$(f)) +NANO_LIB_OBJS := $(NANO_LIB_SRCS:.c=.o) + +CLEAN_OBJS += $(NANO_LIB_OBJS) + +$(NANO_LIB_OBJS): %.o: %.c $(HEADERS) + $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< diff --git a/newlib-nano/read.c b/newlib-nano/read.c new file mode 100644 index 0000000..7c17e80 --- /dev/null +++ b/newlib-nano/read.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include "stub.h" +#include + +ssize_t _read(int fd, void* ptr, size_t len); + +ssize_t _read(int fd, void* ptr, size_t len) { + uint8_t * current = (uint8_t *)ptr; + ssize_t result = 0; + if (isatty(fd)) { + for (current = (uint8_t *)ptr; + (current < ((uint8_t *)ptr) + len) && (uart_get_rx_avail(uart) > 0); + current ++) { + *current = uart_read(uart); + result++; + } + return result; + } + return _stub(EBADF); +} diff --git a/newlib-nano/stub.h b/newlib-nano/stub.h new file mode 100644 index 0000000..c53b542 --- /dev/null +++ b/newlib-nano/stub.h @@ -0,0 +1,8 @@ +#ifndef _LIBWRAP_STUB_H_ +#define _LIBWRAP_STUB_H_ + +static inline int _stub(int err) { + return err?-1:-1; +} + +#endif /* _LIBWRAP_STUB_H_ */ diff --git a/newlib-nano/syscalls.c b/newlib-nano/syscalls.c new file mode 100644 index 0000000..e43328c --- /dev/null +++ b/newlib-nano/syscalls.c @@ -0,0 +1,79 @@ +#include +#include + +extern int _end; +void* _sbrk(int incr); +int _close(int file); +int _fstat(int file, struct stat *st); +int _isatty(int fd); +int _lseek(int file, int ptr, int dir); + +void _kill(int pid, int sig); +int _getpid(void); +void write_hex(int fd, unsigned long int hex); + +void *_sbrk(int incr) { + static unsigned char *heap = NULL; + unsigned char *prev_heap; + if (heap == NULL) { + heap = (unsigned char *)&_end; + } + prev_heap = heap; + heap += incr; + return prev_heap; +} + +int _close(int file) { + (void)file; + return -1; +} + +int _fstat(int file, struct stat *st) { + (void)file; + st->st_mode = S_IFCHR; + return 0; +} + +int _isatty(int fd) { + if (fd == STDOUT_FILENO || fd == STDERR_FILENO) + return 1; + return 0; +} + +int _lseek(int file, int ptr, int dir) { + (void)file; + (void)ptr; + (void)dir; + return 0; +} + +void _exit(int status) { + const char message[] = "\nProgam has exited with code:"; + write(STDERR_FILENO, message, sizeof(message) - 1); + write_hex(STDERR_FILENO, status); + write(STDERR_FILENO, "\n", 1); + for (;;); +} + +void _kill(int pid, int sig) { + (void)pid; + (void)sig; + return; +} + +int _getpid(void) { + return -1; +} + +void write_hex(int fd, unsigned long int hex){ + uint8_t ii; + uint8_t jj; + char towrite; + write(fd , "0x", 2); + for (ii = sizeof(unsigned long int) * 2 ; 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); + } +} diff --git a/newlib-nano/write.c b/newlib-nano/write.c new file mode 100644 index 0000000..edc786c --- /dev/null +++ b/newlib-nano/write.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include "stub.h" +#include + +ssize_t _write(int fd, const void* ptr, size_t len); + +ssize_t _write(int fd, const void* ptr, size_t len) { + const char * current = (const char *)ptr; + if (isatty(fd)) { + for (size_t jj = 0; jj < len; jj++) { + while (uart_get_tx_free(uart)==0) ; + uart_write(uart, current[jj]); + if (current[jj] == '\n') { + while (uart_get_tx_free(uart)==0) ; + uart_write(uart, '\r'); + } + } + return len; + } + return _stub(EBADF); +}