6 #include "pthread_impl.h"
14 int __init_tp(void *p)
18 int r = __set_thread_area(TP_ADJ(p));
20 if (!r) libc.can_do_threads = 1;
21 td->tid = __syscall(SYS_set_tid_address, &td->tid);
22 td->locale = &libc.global_locale;
23 td->robust_list.head = &td->robust_list.head;
29 static struct builtin_tls {
34 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
38 size_t len, size, align;
41 #define T __static_tls
43 void *__copy_tls(unsigned char *mem)
46 if (!T.image) return mem;
47 void **dtv = (void *)mem;
50 mem += sizeof(void *) * 2;
51 mem += -((uintptr_t)mem + sizeof(struct pthread)) & (T.align-1);
53 mem += sizeof(struct pthread);
55 mem += libc.tls_size - sizeof(struct pthread);
56 mem -= (uintptr_t)mem & (T.align-1);
60 td->dtv = td->dtv_copy = dtv;
62 memcpy(mem, T.image, T.len);
66 #if ULONG_MAX == 0xffffffff
67 typedef Elf32_Phdr Phdr;
69 typedef Elf64_Phdr Phdr;
72 void __init_tls(size_t *aux)
76 Phdr *phdr, *tls_phdr=0;
80 for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
82 if (phdr->p_type == PT_PHDR)
83 base = aux[AT_PHDR] - phdr->p_vaddr;
84 if (phdr->p_type == PT_TLS)
89 T.image = (void *)(base + tls_phdr->p_vaddr);
90 T.len = tls_phdr->p_filesz;
91 T.size = tls_phdr->p_memsz;
92 T.align = tls_phdr->p_align;
95 T.size += (-T.size - (uintptr_t)T.image) & (T.align-1);
96 if (T.align < MIN_TLS_ALIGN) T.align = MIN_TLS_ALIGN;
98 libc.tls_size = 2*sizeof(void *)+T.size+T.align+sizeof(struct pthread)
99 + MIN_TLS_ALIGN-1 & -MIN_TLS_ALIGN;
101 if (libc.tls_size > sizeof builtin_tls) {
103 #define SYS_mmap2 SYS_mmap
105 mem = (void *)__syscall(
107 0, libc.tls_size, PROT_READ|PROT_WRITE,
108 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
109 /* -4095...-1 cast to void * will crash on dereference anyway,
110 * so don't bloat the init code checking for error codes and
111 * explicitly calling a_crash(). */
116 /* Failure to initialize thread pointer is always fatal. */
117 if (__init_tp(__copy_tls(mem)) < 0)
121 void __init_tls(size_t *auxv) { }