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;
19 mem += sizeof(void *) * 2;
20 mem += -((uintptr_t)mem + sizeof(struct pthread)) & (align-1);
22 mem += sizeof(struct pthread);
24 mem += __libc.tls_size - sizeof(struct pthread);
25 mem -= (uintptr_t)mem & (align-1);
31 memcpy(mem, image, len);
35 void *__tls_get_addr(size_t *v)
37 return (char *)__pthread_self()->dtv[1]+v[1];
40 static void *simple(void *p)
43 return __set_thread_area(TP_ADJ(p)) ? 0 : p;
46 weak_alias(simple, __install_initial_tls);
48 void *__mmap(void *, size_t, int, int, int, off_t);
50 #if ULONG_MAX == 0xffffffff
51 typedef Elf32_Phdr Phdr;
53 typedef Elf64_Phdr Phdr;
56 void __init_tls(size_t *aux)
58 unsigned char *p, *mem;
60 Phdr *phdr, *tls_phdr=0;
63 libc.tls_size = sizeof(struct pthread);
65 for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
67 if (phdr->p_type == PT_PHDR)
68 base = aux[AT_PHDR] - phdr->p_vaddr;
69 if (phdr->p_type == PT_TLS)
72 if (!tls_phdr) return;
74 image = (void *)(base + tls_phdr->p_vaddr);
75 len = tls_phdr->p_filesz;
76 size = tls_phdr->p_memsz;
77 align = tls_phdr->p_align;
79 size += (-size - (uintptr_t)image) & (align-1);
80 if (align < 4*sizeof(size_t)) align = 4*sizeof(size_t);
82 libc.tls_size = 2*sizeof(void *)+size+align+sizeof(struct pthread);
84 mem = __mmap(0, libc.tls_size, PROT_READ|PROT_WRITE,
85 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
86 if (!__install_initial_tls(__copy_tls(mem))) a_crash();
89 void __init_tls(size_t *auxv) { }