5 #include "pthread_impl.h"
13 size_t len, size, align;
14 } __static_tls ATTR_LIBC_VISIBILITY;
16 #define T __static_tls
18 void *__copy_tls(unsigned char *mem)
21 if (!T.image) return mem;
22 void **dtv = (void *)mem;
25 mem += sizeof(void *) * 2;
26 mem += -((uintptr_t)mem + sizeof(struct pthread)) & (T.align-1);
28 mem += sizeof(struct pthread);
30 mem += libc.tls_size - sizeof(struct pthread);
31 mem -= (uintptr_t)mem & (T.align-1);
37 memcpy(mem, T.image, T.len);
41 void *__tls_get_addr(size_t *v)
43 return (char *)__pthread_self()->dtv[1]+v[1];
46 static void *simple(void *p)
49 return __set_thread_area(TP_ADJ(p)) ? 0 : p;
52 weak_alias(simple, __install_initial_tls);
54 void *__mmap(void *, size_t, int, int, int, off_t);
56 #if ULONG_MAX == 0xffffffff
57 typedef Elf32_Phdr Phdr;
59 typedef Elf64_Phdr Phdr;
62 void __init_tls(size_t *aux)
64 unsigned char *p, *mem;
66 Phdr *phdr, *tls_phdr=0;
69 libc.tls_size = sizeof(struct pthread);
71 for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
73 if (phdr->p_type == PT_PHDR)
74 base = aux[AT_PHDR] - phdr->p_vaddr;
75 if (phdr->p_type == PT_TLS)
78 if (!tls_phdr) return;
80 T.image = (void *)(base + tls_phdr->p_vaddr);
81 T.len = tls_phdr->p_filesz;
82 T.size = tls_phdr->p_memsz;
83 T.align = tls_phdr->p_align;
85 T.size += (-T.size - (uintptr_t)T.image) & (T.align-1);
86 if (T.align < 4*sizeof(size_t)) T.align = 4*sizeof(size_t);
88 libc.tls_size = 2*sizeof(void *)+T.size+T.align+sizeof(struct pthread);
90 mem = __mmap(0, libc.tls_size, PROT_READ|PROT_WRITE,
91 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
92 if (!__install_initial_tls(__copy_tls(mem))) a_crash();
95 void __init_tls(size_t *auxv) { }