From 6a2eaa3c5bbb8cc272cf6a4b7a9766a89e9b2c61 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sat, 6 Oct 2012 16:51:03 -0400 Subject: [PATCH] fix buggy TLS size/alignment computations in static-linked TLS --- src/env/__init_tls.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/env/__init_tls.c b/src/env/__init_tls.c index b19bdb64..6b3cd28b 100644 --- a/src/env/__init_tls.c +++ b/src/env/__init_tls.c @@ -11,10 +11,23 @@ static size_t len, size, align; void *__copy_tls(unsigned char *mem) { - mem += -size & (4*sizeof(size_t)-1); - mem += ((uintptr_t)image - (uintptr_t)mem) & (align-1); + pthread_t td; + if (!image) return mem; + void **dtv = (void *)mem; + dtv[0] = (void *)1; + mem += __libc.tls_size - sizeof(struct pthread); + mem -= (uintptr_t)mem & (align-1); + td = (pthread_t)mem; + td->dtv = dtv; + mem -= size; + dtv[1] = mem; memcpy(mem, image, len); - return mem + size; + return td; +} + +void *__tls_get_addr(size_t *v) +{ + return (char *)__pthread_self()->dtv[1]+v[1]; } static void *simple(void *p) @@ -54,12 +67,16 @@ void __init_tls(size_t *auxv) } if (!tls_phdr) return; - libc.tls_size = size+align+8*sizeof(size_t)+sizeof(struct pthread); - image = (void *)(base + tls_phdr->p_vaddr); len = tls_phdr->p_filesz; size = tls_phdr->p_memsz; align = tls_phdr->p_align; + + size += (-size - (uintptr_t)image) & (align-1); + if (align < 4*sizeof(size_t)) align = 4*sizeof(size_t); + + libc.tls_size = 2*sizeof(void *)+size+align+sizeof(struct pthread); + mem = __mmap(0, libc.tls_size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); if (mem == MAP_FAILED) a_crash();