Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tls: add dynamic linker TLS support
Browse files Browse the repository at this point in the history
JIRA: RTOS-664
badochov committed Aug 16, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 9aae465 commit 9fe5f55
Showing 5 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -66,6 +66,7 @@ INSTALL_TARGETS := install-headers install-libs
ifeq ($(LIBPHOENIX_PIC), y)
CFLAGS += $(TARGET_PIC_FLAG)
ifeq ($(LIBPHOENIX_SHARED), y)
CPPFLAGS += -DLIBPHOENIX_SHARED
include shared.mk
endif
endif
5 changes: 5 additions & 0 deletions crt0-common.c
Original file line number Diff line number Diff line change
@@ -72,6 +72,8 @@ extern int main(int argc, char **argv);

char **environ;
const char *argv_progname;
/* TLS cannot be used before initialized by dynamic linker in dynamically linked binaries. */
int can_use_tls;


__attribute__((noreturn)) void _startc(void (*cleanup)(void), int argc, char **argv, char **env)
@@ -81,6 +83,9 @@ __attribute__((noreturn)) void _startc(void (*cleanup)(void), int argc, char **a

_libc_init();

/* At this point TLS is setup, as dynamic linker calls _startc after dealing with TLS. */
can_use_tls = 1;

/* cleanup function is not NULL when the dynamic linker is used */
if (cleanup != NULL) {
atexit(cleanup);
5 changes: 4 additions & 1 deletion errno/errno.c
Original file line number Diff line number Diff line change
@@ -23,6 +23,9 @@

static __thread int __errno_tls;

extern int can_use_tls;
static int errno_single = 0;

#else

static int errno_global;
@@ -58,7 +61,7 @@ int *__errno_location(void)

return &errno_global;
#else
return &__errno_tls;
return can_use_tls == 0 ? &errno_single : &__errno_tls;
#endif
}

7 changes: 7 additions & 0 deletions include/sys/threads.h
Original file line number Diff line number Diff line change
@@ -70,6 +70,13 @@ extern int spawnSyspage(const char *imap, const char *dmap, const char *name, ch
extern int threadJoin(int tid, time_t timeout);


extern int beginthreadexsvc(void (*start)(void *), unsigned int priority, void *stack, unsigned int stacksz, void *arg, handle_t *id);


__attribute__((noreturn))
extern void endthreadsvc(void);


extern int beginthreadex(void (*start)(void *), unsigned int priority, void *stack, unsigned int stacksz, void *arg, handle_t *id);


42 changes: 42 additions & 0 deletions sys/threads.c
Original file line number Diff line number Diff line change
@@ -17,6 +17,48 @@
#include <errno.h>


#include <sys/debug.h>
#include <stdlib.h>

/* Provide stub definition of tls managing functions for statically linked programs. */

static void _libphoenix_stub(void)
{
}

extern void __attribute__((weak, alias("_libphoenix_stub"))) _rtld_tls_free_curr(void);
extern void __attribute__((weak, alias("_libphoenix_stub"))) _rtld_tls_allocate_curr(void);


/* FIXME: Hack to provide tls support in dynamically loaded binaries. */
void beginthreadex_wrapper(void *arg)
{
_rtld_tls_allocate_curr();

void *(*start)(void *) = ((void **)arg)[0];
void *startArg = ((void **)arg)[1];

free(arg);

start(startArg);
}

int beginthreadex(void (*start)(void *), unsigned int priority, void *stack, unsigned int stacksz, void *arg, handle_t *id)
{
void **startAndArg = (void **)malloc(sizeof(void *) * 2);
startAndArg[0] = start;
startAndArg[1] = arg;
return beginthreadexsvc(beginthreadex_wrapper, priority, stack, stacksz, startAndArg, id);
}


__attribute__((noreturn)) void endthread(void)
{
_rtld_tls_free_curr();
endthreadsvc();
}


int mutexCreate(handle_t *h)
{
static const struct lockAttr defaultAttr = { .type = PH_LOCK_NORMAL };

0 comments on commit 9fe5f55

Please sign in to comment.