fix omission of dtv setup in static linked programs on TLS variant I archs
[musl] / src / env / __init_tls.c
index ab5992c..a4704f0 100644 (file)
@@ -1,5 +1,7 @@
 #include <elf.h>
 #include <limits.h>
+#include <sys/mman.h>
+#include <string.h>
 #include "pthread_impl.h"
 #include "libc.h"
 #include "atomic.h"
@@ -21,12 +23,12 @@ void *__copy_tls(unsigned char *mem)
        td = (pthread_t)mem;
        mem += sizeof(struct pthread);
 #else
-       mem += __libc.tls_size - sizeof(struct pthread);
+       mem += libc.tls_size - sizeof(struct pthread);
        mem -= (uintptr_t)mem & (align-1);
        td = (pthread_t)mem;
-       td->dtv = dtv;
        mem -= size;
 #endif
+       td->dtv = dtv;
        dtv[1] = mem;
        memcpy(mem, image, len);
        return td;
@@ -56,10 +58,12 @@ typedef Elf64_Phdr Phdr;
 void __init_tls(size_t *aux)
 {
        unsigned char *p, *mem;
-       size_t n, d;
+       size_t n;
        Phdr *phdr, *tls_phdr=0;
        size_t base = 0;
 
+       libc.tls_size = sizeof(struct pthread);
+
        for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
                phdr = (void *)p;
                if (phdr->p_type == PT_PHDR)