6 #include "pthread_impl.h"
11 volatile int __thread_list_lock;
13 int __init_tp(void *p)
17 int r = __set_thread_area(TP_ADJ(p));
19 if (!r) libc.can_do_threads = 1;
20 td->detach_state = DT_JOINABLE;
21 td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
22 td->locale = &libc.global_locale;
23 td->robust_list.head = &td->robust_list.head;
24 td->next = td->prev = td;
28 static struct builtin_tls {
33 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
35 static struct tls_module main_tls;
37 void *__copy_tls(unsigned char *mem)
45 dtv = (uintptr_t*)(mem + libc.tls_size) - (libc.tls_cnt + 1);
47 mem += -((uintptr_t)mem + sizeof(struct pthread)) & (libc.tls_align-1);
49 mem += sizeof(struct pthread);
51 for (i=1, p=libc.tls_head; p; i++, p=p->next) {
52 dtv[i] = (uintptr_t)(mem + p->offset) + DTP_OFFSET;
53 memcpy(mem + p->offset, p->image, p->len);
56 dtv = (uintptr_t *)mem;
58 mem += libc.tls_size - sizeof(struct pthread);
59 mem -= (uintptr_t)mem & (libc.tls_align-1);
62 for (i=1, p=libc.tls_head; p; i++, p=p->next) {
63 dtv[i] = (uintptr_t)(mem - p->offset) + DTP_OFFSET;
64 memcpy(mem - p->offset, p->image, p->len);
67 dtv[0] = libc.tls_cnt;
68 td->dtv = td->dtv_copy = dtv;
72 #if ULONG_MAX == 0xffffffff
73 typedef Elf32_Phdr Phdr;
75 typedef Elf64_Phdr Phdr;
78 extern weak hidden const size_t _DYNAMIC[];
80 static void static_init_tls(size_t *aux)
84 Phdr *phdr, *tls_phdr=0;
88 for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
90 if (phdr->p_type == PT_PHDR)
91 base = aux[AT_PHDR] - phdr->p_vaddr;
92 if (phdr->p_type == PT_DYNAMIC && _DYNAMIC)
93 base = (size_t)_DYNAMIC - phdr->p_vaddr;
94 if (phdr->p_type == PT_TLS)
96 if (phdr->p_type == PT_GNU_STACK &&
97 phdr->p_memsz > __default_stacksize)
99 phdr->p_memsz < DEFAULT_STACK_MAX ?
100 phdr->p_memsz : DEFAULT_STACK_MAX;
104 main_tls.image = (void *)(base + tls_phdr->p_vaddr);
105 main_tls.len = tls_phdr->p_filesz;
106 main_tls.size = tls_phdr->p_memsz;
107 main_tls.align = tls_phdr->p_align;
109 libc.tls_head = &main_tls;
112 main_tls.size += (-main_tls.size - (uintptr_t)main_tls.image)
113 & (main_tls.align-1);
115 main_tls.offset = GAP_ABOVE_TP;
116 main_tls.offset += -GAP_ABOVE_TP & (main_tls.align-1);
118 main_tls.offset = main_tls.size;
120 if (main_tls.align < MIN_TLS_ALIGN) main_tls.align = MIN_TLS_ALIGN;
122 libc.tls_align = main_tls.align;
123 libc.tls_size = 2*sizeof(void *) + sizeof(struct pthread)
127 + main_tls.size + main_tls.align
128 + MIN_TLS_ALIGN-1 & -MIN_TLS_ALIGN;
130 if (libc.tls_size > sizeof builtin_tls) {
132 #define SYS_mmap2 SYS_mmap
134 mem = (void *)__syscall(
136 0, libc.tls_size, PROT_READ|PROT_WRITE,
137 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
138 /* -4095...-1 cast to void * will crash on dereference anyway,
139 * so don't bloat the init code checking for error codes and
140 * explicitly calling a_crash(). */
145 /* Failure to initialize thread pointer is always fatal. */
146 if (__init_tp(__copy_tls(mem)) < 0)
150 weak_alias(static_init_tls, __init_tls);