6 #include "pthread_impl.h"
11 int __init_tp(void *p)
15 int r = __set_thread_area(TP_ADJ(p));
17 if (!r) libc.can_do_threads = 1;
18 td->tid = __syscall(SYS_set_tid_address, &td->tid);
19 td->locale = &libc.global_locale;
20 td->robust_list.head = &td->robust_list.head;
26 static struct builtin_tls {
31 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
35 size_t len, size, align;
36 } __static_tls ATTR_LIBC_VISIBILITY;
38 #define T __static_tls
40 void *__copy_tls(unsigned char *mem)
43 if (!T.image) return mem;
44 void **dtv = (void *)mem;
47 mem += sizeof(void *) * 2;
48 mem += -((uintptr_t)mem + sizeof(struct pthread)) & (T.align-1);
50 mem += sizeof(struct pthread);
52 mem += libc.tls_size - sizeof(struct pthread);
53 mem -= (uintptr_t)mem & (T.align-1);
57 td->dtv = td->dtv_copy = dtv;
59 memcpy(mem, T.image, T.len);
63 #if ULONG_MAX == 0xffffffff
64 typedef Elf32_Phdr Phdr;
66 typedef Elf64_Phdr Phdr;
69 void __init_tls(size_t *aux)
73 Phdr *phdr, *tls_phdr=0;
77 libc.tls_size = sizeof(struct pthread);
79 for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
81 if (phdr->p_type == PT_PHDR)
82 base = aux[AT_PHDR] - phdr->p_vaddr;
83 if (phdr->p_type == PT_TLS)
88 T.image = (void *)(base + tls_phdr->p_vaddr);
89 T.len = tls_phdr->p_filesz;
90 T.size = tls_phdr->p_memsz;
91 T.align = tls_phdr->p_align;
94 T.size += (-T.size - (uintptr_t)T.image) & (T.align-1);
95 if (T.align < MIN_TLS_ALIGN) T.align = MIN_TLS_ALIGN;
97 libc.tls_size = 2*sizeof(void *)+T.size+T.align+sizeof(struct pthread);
99 if (libc.tls_size > sizeof builtin_tls) {
101 #define SYS_mmap2 SYS_mmap
103 mem = (void *)__syscall(
105 0, libc.tls_size, PROT_READ|PROT_WRITE,
106 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
107 /* -4095...-1 cast to void * will crash on dereference anyway,
108 * so don't bloat the init code checking for error codes and
109 * explicitly calling a_crash(). */
114 /* Failure to initialize thread pointer is always fatal. */
115 if (__init_tp(__copy_tls(mem)) < 0)
119 void __init_tls(size_t *auxv) { }