about summary refs log tree commit diff
path: root/src/env
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2012-10-06 16:51:03 -0400
committerRich Felker <dalias@aerifal.cx>2012-10-06 16:51:03 -0400
commit6a2eaa3c5bbb8cc272cf6a4b7a9766a89e9b2c61 (patch)
treee42bb378305ba85e3bb608dbd5afebe6166eff67 /src/env
parentbd17431a2cba8aafd1e4f95f9b70e5800ee51d24 (diff)
downloadmusl-6a2eaa3c5bbb8cc272cf6a4b7a9766a89e9b2c61.tar.gz
musl-6a2eaa3c5bbb8cc272cf6a4b7a9766a89e9b2c61.tar.xz
musl-6a2eaa3c5bbb8cc272cf6a4b7a9766a89e9b2c61.zip
fix buggy TLS size/alignment computations in static-linked TLS
Diffstat (limited to 'src/env')
-rw-r--r--src/env/__init_tls.c27
1 files 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();