5 #include "pthread_impl.h"
10 int __init_tp(void *p)
14 int r = __set_thread_area(TP_ADJ(p));
16 if (!r) libc.can_do_threads = 1;
17 libc.has_thread_pointer = 1;
18 td->tid = td->pid = __syscall(SYS_set_tid_address, &td->tid);
19 td->errno_ptr = &td->errno_val;
25 static long long builtin_tls[(sizeof(struct pthread) + 64)/sizeof(long long)];
29 size_t len, size, align;
30 } __static_tls ATTR_LIBC_VISIBILITY;
32 #define T __static_tls
34 void *__copy_tls(unsigned char *mem)
37 if (!T.image) return mem;
38 void **dtv = (void *)mem;
41 mem += sizeof(void *) * 2;
42 mem += -((uintptr_t)mem + sizeof(struct pthread)) & (T.align-1);
44 mem += sizeof(struct pthread);
46 mem += libc.tls_size - sizeof(struct pthread);
47 mem -= (uintptr_t)mem & (T.align-1);
53 memcpy(mem, T.image, T.len);
57 void *__tls_get_addr(size_t *v)
59 return (char *)__pthread_self()->dtv[1]+v[1];
62 #if ULONG_MAX == 0xffffffff
63 typedef Elf32_Phdr Phdr;
65 typedef Elf64_Phdr Phdr;
68 void __init_tls(size_t *aux)
72 Phdr *phdr, *tls_phdr=0;
76 libc.tls_size = sizeof(struct pthread);
78 for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
80 if (phdr->p_type == PT_PHDR)
81 base = aux[AT_PHDR] - phdr->p_vaddr;
82 if (phdr->p_type == PT_TLS)
87 T.image = (void *)(base + tls_phdr->p_vaddr);
88 T.len = tls_phdr->p_filesz;
89 T.size = tls_phdr->p_memsz;
90 T.align = tls_phdr->p_align;
93 T.size += (-T.size - (uintptr_t)T.image) & (T.align-1);
94 if (T.align < 4*sizeof(size_t)) T.align = 4*sizeof(size_t);
96 libc.tls_size = 2*sizeof(void *)+T.size+T.align+sizeof(struct pthread);
98 if (libc.tls_size > sizeof builtin_tls) {
99 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 fatal if TLS is used. */
115 if (__init_tp(__copy_tls(mem)) < 0 && tls_phdr)
119 void __init_tls(size_t *auxv) { }