diff --git a/Makefile b/Makefile index acbcfeab..dbdc21b6 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,9 @@ endif CFLAGS += -Iinclude -fno-builtin-malloc +ifneq ($(LIBPHOENIX_NOPIC), y) +CFLAGS += -fpic +endif OBJS := # crt0.o should have all necessary initialization + call to main() diff --git a/arch/armv7a/Makefile b/arch/armv7a/Makefile index 9be5def5..18d77f0f 100644 --- a/arch/armv7a/Makefile +++ b/arch/armv7a/Makefile @@ -5,5 +5,5 @@ # Author: Pawel Pisarczyk # -OBJS += $(addprefix $(PREFIX_O)arch/armv7a/, syscalls.o jmp.o signal.o string.o reboot.o) +OBJS += $(addprefix $(PREFIX_O)arch/armv7a/, syscalls.o jmp.o signal.o string.o reboot.o tls.o) CRT0_OBJS += $(addprefix $(PREFIX_O)arch/armv7a/, _start.o) diff --git a/arch/armv7a/tls.c b/arch/armv7a/tls.c new file mode 100644 index 00000000..ba0e5280 --- /dev/null +++ b/arch/armv7a/tls.c @@ -0,0 +1,36 @@ +/* + * Phoenix-RTOS + * + * libphoenix + * + * tls access function + * + * Copyright 2023 Phoenix Systems + * Author: Hubert Badocha + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + + +#define TLS_TCB_SIZE 8 + + +typedef struct { + unsigned long ti_moduleid; + unsigned long ti_tlsoffset; +} TLS_index; + + +/* When libphoenix is compiled as a PIC, even when linked into a static NOPIC binary, + * compiler generates accesses to TLS as calls to this function. + * This functions is simple version just for static binaries purposes, + * in dynamic binaries dynamic linker handles TLS accesses. */ +void *__tls_get_addr(TLS_index *ti) +{ + void *ptr; + __asm__ volatile ("mrc p15, 0, %0, cr13, cr0, 3" + : "=r"(ptr)); + return ((char *)ptr) + TLS_TCB_SIZE + ti->ti_tlsoffset; +} diff --git a/arch/riscv64/Makefile b/arch/riscv64/Makefile index 54310c15..5d3600c2 100644 --- a/arch/riscv64/Makefile +++ b/arch/riscv64/Makefile @@ -5,5 +5,5 @@ # Author: Pawel Pisarczyk # -OBJS += $(addprefix $(PREFIX_O)arch/riscv64/, syscalls.o string.o signal.o reboot.o jmp.o) +OBJS += $(addprefix $(PREFIX_O)arch/riscv64/, syscalls.o string.o signal.o reboot.o jmp.o tls.o) CRT0_OBJS += $(addprefix $(PREFIX_O)arch/riscv64/, _start.o) diff --git a/arch/riscv64/tls.c b/arch/riscv64/tls.c new file mode 100644 index 00000000..a3f1ebd6 --- /dev/null +++ b/arch/riscv64/tls.c @@ -0,0 +1,34 @@ +/* + * Phoenix-RTOS + * + * libphoenix + * + * tls access function + * + * Copyright 2023 Phoenix Systems + * Author: Hubert Badocha + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + + +typedef struct { + unsigned long ti_moduleid; + unsigned long ti_tlsoffset; +} TLS_index; + + +/* When libphoenix is compiled as a PIC, even when linked into a static NOPIC binary, + * compiler generates accesses to TLS as calls to this function. + * This functions is simple version just for static binaries purposes, + * in dynamic binaries dynamic linker handles TLS accesses. */ +void *__tls_get_addr(TLS_index *ti) +{ + void *ptr; + __asm__ volatile ("mv %0, tp" + : "=r"(ptr)); + + return ((char *)ptr) + ti->ti_tlsoffset; +}