track all live threads in an AS-safe, fully-consistent linked list
[musl] / src / env / __init_tls.c
index b125eb1..f1874f2 100644 (file)
@@ -8,6 +8,8 @@
 #include "atomic.h"
 #include "syscall.h"
 
+volatile int __thread_list_lock;
+
 int __init_tp(void *p)
 {
        pthread_t td = p;
@@ -15,9 +17,11 @@ int __init_tp(void *p)
        int r = __set_thread_area(TP_ADJ(p));
        if (r < 0) return -1;
        if (!r) libc.can_do_threads = 1;
-       td->tid = __syscall(SYS_set_tid_address, &td->tid);
+       td->detach_state = DT_JOINABLE;
+       td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
        td->locale = &libc.global_locale;
        td->robust_list.head = &td->robust_list.head;
+       td->next = td->prev = td;
        return 0;
 }
 
@@ -35,32 +39,32 @@ void *__copy_tls(unsigned char *mem)
        pthread_t td;
        struct tls_module *p;
        size_t i;
-       void **dtv;
+       uintptr_t *dtv;
 
 #ifdef TLS_ABOVE_TP
-       dtv = (void **)(mem + libc.tls_size) - (libc.tls_cnt + 1);
+       dtv = (uintptr_t*)(mem + libc.tls_size) - (libc.tls_cnt + 1);
 
        mem += -((uintptr_t)mem + sizeof(struct pthread)) & (libc.tls_align-1);
        td = (pthread_t)mem;
        mem += sizeof(struct pthread);
 
        for (i=1, p=libc.tls_head; p; i++, p=p->next) {
-               dtv[i] = mem + p->offset;
-               memcpy(dtv[i], p->image, p->len);
+               dtv[i] = (uintptr_t)(mem + p->offset) + DTP_OFFSET;
+               memcpy(mem + p->offset, p->image, p->len);
        }
 #else
-       dtv = (void **)mem;
+       dtv = (uintptr_t *)mem;
 
        mem += libc.tls_size - sizeof(struct pthread);
        mem -= (uintptr_t)mem & (libc.tls_align-1);
        td = (pthread_t)mem;
 
        for (i=1, p=libc.tls_head; p; i++, p=p->next) {
-               dtv[i] = mem - p->offset;
-               memcpy(dtv[i], p->image, p->len);
+               dtv[i] = (uintptr_t)(mem - p->offset) + DTP_OFFSET;
+               memcpy(mem - p->offset, p->image, p->len);
        }
 #endif
-       dtv[0] = (void *)libc.tls_cnt;
+       dtv[0] = libc.tls_cnt;
        td->dtv = td->dtv_copy = dtv;
        return td;
 }
@@ -71,8 +75,7 @@ typedef Elf32_Phdr Phdr;
 typedef Elf64_Phdr Phdr;
 #endif
 
-__attribute__((__weak__, __visibility__("hidden")))
-extern const size_t _DYNAMIC[];
+extern weak hidden const size_t _DYNAMIC[];
 
 static void static_init_tls(size_t *aux)
 {
@@ -90,6 +93,11 @@ static void static_init_tls(size_t *aux)
                        base = (size_t)_DYNAMIC - phdr->p_vaddr;
                if (phdr->p_type == PT_TLS)
                        tls_phdr = phdr;
+               if (phdr->p_type == PT_GNU_STACK &&
+                   phdr->p_memsz > __default_stacksize)
+                       __default_stacksize =
+                               phdr->p_memsz < DEFAULT_STACK_MAX ?
+                               phdr->p_memsz : DEFAULT_STACK_MAX;
        }
 
        if (tls_phdr) {
@@ -103,13 +111,19 @@ static void static_init_tls(size_t *aux)
 
        main_tls.size += (-main_tls.size - (uintptr_t)main_tls.image)
                & (main_tls.align-1);
-       if (main_tls.align < MIN_TLS_ALIGN) main_tls.align = MIN_TLS_ALIGN;
-#ifndef TLS_ABOVE_TP
+#ifdef TLS_ABOVE_TP
+       main_tls.offset = GAP_ABOVE_TP;
+       main_tls.offset += -GAP_ABOVE_TP & (main_tls.align-1);
+#else
        main_tls.offset = main_tls.size;
 #endif
+       if (main_tls.align < MIN_TLS_ALIGN) main_tls.align = MIN_TLS_ALIGN;
 
        libc.tls_align = main_tls.align;
        libc.tls_size = 2*sizeof(void *) + sizeof(struct pthread)
+#ifdef TLS_ABOVE_TP
+               + main_tls.offset
+#endif
                + main_tls.size + main_tls.align
                + MIN_TLS_ALIGN-1 & -MIN_TLS_ALIGN;