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 libc.has_thread_pointer = 1;
19 td->tid = __syscall(SYS_set_tid_address, &td->tid);
20 td->locale = &libc.global_locale;
21 td->robust_list.head = &td->robust_list.head;
27 static struct builtin_tls {
32 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
36 size_t len, size, align;
37 } __static_tls ATTR_LIBC_VISIBILITY;
39 #define T __static_tls
41 void *__copy_tls(unsigned char *mem)
44 if (!T.image) return mem;
45 void **dtv = (void *)mem;
48 mem += sizeof(void *) * 2;
49 mem += -((uintptr_t)mem + sizeof(struct pthread)) & (T.align-1);
51 mem += sizeof(struct pthread);
53 mem += libc.tls_size - sizeof(struct pthread);
54 mem -= (uintptr_t)mem & (T.align-1);
58 td->dtv = td->dtv_copy = dtv;
60 memcpy(mem, T.image, T.len);
64 #if ULONG_MAX == 0xffffffff
65 typedef Elf32_Phdr Phdr;
67 typedef Elf64_Phdr Phdr;
70 void __init_tls(size_t *aux)
74 Phdr *phdr, *tls_phdr=0;
78 libc.tls_size = sizeof(struct pthread);
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);
100 if (libc.tls_size > sizeof builtin_tls) {
102 #define SYS_mmap2 SYS_mmap
104 mem = (void *)__syscall(
106 0, libc.tls_size, PROT_READ|PROT_WRITE,
107 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
108 /* -4095...-1 cast to void * will crash on dereference anyway,
109 * so don't bloat the init code checking for error codes and
110 * explicitly calling a_crash(). */
115 /* Failure to initialize thread pointer is fatal if TLS is used. */
116 if (__init_tp(__copy_tls(mem)) < 0 && tls_phdr)
120 void __init_tls(size_t *auxv) { }