3 #include "pthread_impl.h"
10 static size_t len, size, align;
12 void *__copy_tls(unsigned char *mem)
15 if (!image) return mem;
16 void **dtv = (void *)mem;
18 mem += __libc.tls_size - sizeof(struct pthread);
19 mem -= (uintptr_t)mem & (align-1);
24 memcpy(mem, image, len);
28 void *__tls_get_addr(size_t *v)
30 return (char *)__pthread_self()->dtv[1]+v[1];
33 static void *simple(void *p)
36 return __set_thread_area(p) ? 0 : p;
39 weak_alias(simple, __install_initial_tls);
41 void *__mmap(void *, size_t, int, int, int, off_t);
43 #if ULONG_MAX == 0xffffffff
44 typedef Elf32_Phdr Phdr;
46 typedef Elf64_Phdr Phdr;
49 void __init_tls(size_t *aux)
51 unsigned char *p, *mem;
53 Phdr *phdr, *tls_phdr=0;
56 for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
58 if (phdr->p_type == PT_PHDR)
59 base = aux[AT_PHDR] - phdr->p_vaddr;
60 if (phdr->p_type == PT_TLS)
63 if (!tls_phdr) return;
65 image = (void *)(base + tls_phdr->p_vaddr);
66 len = tls_phdr->p_filesz;
67 size = tls_phdr->p_memsz;
68 align = tls_phdr->p_align;
70 size += (-size - (uintptr_t)image) & (align-1);
71 if (align < 4*sizeof(size_t)) align = 4*sizeof(size_t);
73 libc.tls_size = 2*sizeof(void *)+size+align+sizeof(struct pthread);
75 mem = __mmap(0, libc.tls_size, PROT_READ|PROT_WRITE,
76 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
77 if (!__install_initial_tls(__copy_tls(mem))) a_crash();
80 void __init_tls(size_t *auxv) { }