5 #include "pthread_impl.h"
10 int __init_tp(void *p)
14 if (__set_thread_area(TP_ADJ(p)) < 0)
16 td->tid = td->pid = __syscall(SYS_set_tid_address, &td->tid);
17 td->errno_ptr = &td->errno_val;
18 /* Currently, both of these predicates depend in the same thing:
19 * successful initialization of the thread pointer. However, in
20 * the future, we may support setups where setting the thread
21 * pointer is possible but threads other than the main thread
22 * cannot work, so it's best to keep the predicates separate. */
23 libc.has_thread_pointer = 1;
24 libc.can_do_threads = 1;
30 static long long builtin_tls[(sizeof(struct pthread) + 64)/sizeof(long long)];
34 size_t len, size, align;
35 } __static_tls ATTR_LIBC_VISIBILITY;
37 #define T __static_tls
39 void *__copy_tls(unsigned char *mem)
42 if (!T.image) return mem;
43 void **dtv = (void *)mem;
46 mem += sizeof(void *) * 2;
47 mem += -((uintptr_t)mem + sizeof(struct pthread)) & (T.align-1);
49 mem += sizeof(struct pthread);
51 mem += libc.tls_size - sizeof(struct pthread);
52 mem -= (uintptr_t)mem & (T.align-1);
58 memcpy(mem, T.image, T.len);
62 void *__tls_get_addr(size_t *v)
64 return (char *)__pthread_self()->dtv[1]+v[1];
67 void *__mmap(void *, size_t, int, int, int, off_t);
69 #if ULONG_MAX == 0xffffffff
70 typedef Elf32_Phdr Phdr;
72 typedef Elf64_Phdr Phdr;
75 void __init_tls(size_t *aux)
79 Phdr *phdr, *tls_phdr=0;
83 libc.tls_size = sizeof(struct pthread);
85 for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
87 if (phdr->p_type == PT_PHDR)
88 base = aux[AT_PHDR] - phdr->p_vaddr;
89 if (phdr->p_type == PT_TLS)
94 T.image = (void *)(base + tls_phdr->p_vaddr);
95 T.len = tls_phdr->p_filesz;
96 T.size = tls_phdr->p_memsz;
97 T.align = tls_phdr->p_align;
100 T.size += (-T.size - (uintptr_t)T.image) & (T.align-1);
101 if (T.align < 4*sizeof(size_t)) T.align = 4*sizeof(size_t);
103 libc.tls_size = 2*sizeof(void *)+T.size+T.align+sizeof(struct pthread);
105 if (libc.tls_size > sizeof builtin_tls) {
106 mem = (void *)__syscall(
112 0, libc.tls_size, PROT_READ|PROT_WRITE,
113 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
114 /* -4095...-1 cast to void * will crash on dereference anyway,
115 * so don't bloat the init code checking for error codes and
116 * explicitly calling a_crash(). */
121 /* Failure to initialize thread pointer is fatal if TLS is used. */
122 if (__init_tp(__copy_tls(mem)) < 0 && tls_phdr)
126 void __init_tls(size_t *auxv) { }