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 #if ULONG_MAX == 0xffffffff
68 typedef Elf32_Phdr Phdr;
70 typedef Elf64_Phdr Phdr;
73 void __init_tls(size_t *aux)
77 Phdr *phdr, *tls_phdr=0;
81 libc.tls_size = sizeof(struct pthread);
83 for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
85 if (phdr->p_type == PT_PHDR)
86 base = aux[AT_PHDR] - phdr->p_vaddr;
87 if (phdr->p_type == PT_TLS)
92 T.image = (void *)(base + tls_phdr->p_vaddr);
93 T.len = tls_phdr->p_filesz;
94 T.size = tls_phdr->p_memsz;
95 T.align = tls_phdr->p_align;
98 T.size += (-T.size - (uintptr_t)T.image) & (T.align-1);
99 if (T.align < 4*sizeof(size_t)) T.align = 4*sizeof(size_t);
101 libc.tls_size = 2*sizeof(void *)+T.size+T.align+sizeof(struct pthread);
103 if (libc.tls_size > sizeof builtin_tls) {
104 mem = (void *)__syscall(
110 0, libc.tls_size, PROT_READ|PROT_WRITE,
111 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
112 /* -4095...-1 cast to void * will crash on dereference anyway,
113 * so don't bloat the init code checking for error codes and
114 * explicitly calling a_crash(). */
119 /* Failure to initialize thread pointer is fatal if TLS is used. */
120 if (__init_tp(__copy_tls(mem)) < 0 && tls_phdr)
124 void __init_tls(size_t *auxv) { }