5 #include "pthread_impl.h"
12 static size_t len, size, align;
14 void *__copy_tls(unsigned char *mem)
17 if (!image) return mem;
18 void **dtv = (void *)mem;
21 mem += sizeof(void *) * 2;
22 mem += -((uintptr_t)mem + sizeof(struct pthread)) & (align-1);
24 mem += sizeof(struct pthread);
26 mem += libc.tls_size - sizeof(struct pthread);
27 mem -= (uintptr_t)mem & (align-1);
33 memcpy(mem, image, len);
37 void *__tls_get_addr(size_t *v)
39 return (char *)__pthread_self()->dtv[1]+v[1];
42 static void *simple(void *p)
45 return __set_thread_area(TP_ADJ(p)) ? 0 : p;
48 weak_alias(simple, __install_initial_tls);
50 void *__mmap(void *, size_t, int, int, int, off_t);
52 #if ULONG_MAX == 0xffffffff
53 typedef Elf32_Phdr Phdr;
55 typedef Elf64_Phdr Phdr;
58 void __init_tls(size_t *aux)
60 unsigned char *p, *mem;
62 Phdr *phdr, *tls_phdr=0;
65 libc.tls_size = sizeof(struct pthread);
67 for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
69 if (phdr->p_type == PT_PHDR)
70 base = aux[AT_PHDR] - phdr->p_vaddr;
71 if (phdr->p_type == PT_TLS)
74 if (!tls_phdr) return;
76 image = (void *)(base + tls_phdr->p_vaddr);
77 len = tls_phdr->p_filesz;
78 size = tls_phdr->p_memsz;
79 align = tls_phdr->p_align;
81 size += (-size - (uintptr_t)image) & (align-1);
82 if (align < 4*sizeof(size_t)) align = 4*sizeof(size_t);
84 libc.tls_size = 2*sizeof(void *)+size+align+sizeof(struct pthread);
86 mem = __mmap(0, libc.tls_size, PROT_READ|PROT_WRITE,
87 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
88 if (!__install_initial_tls(__copy_tls(mem))) a_crash();
91 void __init_tls(size_t *auxv) { }