diff --git a/libc/bionic/__libc_init_main_thread.cpp b/libc/bionic/__libc_init_main_thread.cpp index 2643eeef9..b5a83f410 100644 --- a/libc/bionic/__libc_init_main_thread.cpp +++ b/libc/bionic/__libc_init_main_thread.cpp @@ -69,7 +69,9 @@ void __libc_init_main_thread(KernelArgumentBlock& args) { // The -fstack-protector implementation uses TLS, so make sure that's // set up before we call any function that might get a stack check inserted. + // TLS also needs to be set up before errno (and therefore syscalls) can be used. __set_tls(main_thread.tls); + __init_tls(&main_thread); // Tell the kernel to clear our tid field when we exit, so we're like any other pthread. // As a side-effect, this tells us our pid (which is the same as the main thread's tid). @@ -91,9 +93,9 @@ void __libc_init_main_thread(KernelArgumentBlock& args) { // before we initialize the TLS. Dynamic executables will initialize their copy of the global // stack protector from the one in the main thread's TLS. __libc_init_global_stack_chk_guard(args); + __init_thread_stack_guard(&main_thread); __init_thread(&main_thread); - __init_tls(&main_thread); // Store a pointer to the kernel argument block in a TLS slot to be // picked up by the libc constructor. diff --git a/libc/bionic/pthread_create.cpp b/libc/bionic/pthread_create.cpp index 1956a8ffb..58aa6f18e 100644 --- a/libc/bionic/pthread_create.cpp +++ b/libc/bionic/pthread_create.cpp @@ -56,7 +56,6 @@ void __init_tls(pthread_internal_t* thread) { // Slot 0 must point to itself. The x86 Linux kernel reads the TLS from %fs:0. thread->tls[TLS_SLOT_SELF] = thread->tls; thread->tls[TLS_SLOT_THREAD_ID] = thread; - __init_thread_stack_guard(thread); } void __init_thread_stack_guard(pthread_internal_t* thread) { @@ -182,6 +181,7 @@ static int __allocate_thread(pthread_attr_t* attr, pthread_internal_t** threadp, thread->mmap_size = mmap_size; thread->attr = *attr; __init_tls(thread); + __init_thread_stack_guard(thread); *threadp = thread; *child_stack = stack_top;