track all live threads in an AS-safe, fully-consistent linked list
[musl] / src / env / __init_tls.c
1 #include <elf.h>
2 #include <limits.h>
3 #include <sys/mman.h>
4 #include <string.h>
5 #include <stddef.h>
6 #include "pthread_impl.h"
7 #include "libc.h"
8 #include "atomic.h"
9 #include "syscall.h"
10
11 volatile int __thread_list_lock;
12
13 int __init_tp(void *p)
14 {
15         pthread_t td = p;
16         td->self = td;
17         int r = __set_thread_area(TP_ADJ(p));
18         if (r < 0) return -1;
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;
25         return 0;
26 }
27
28 static struct builtin_tls {
29         char c;
30         struct pthread pt;
31         void *space[16];
32 } builtin_tls[1];
33 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
34
35 static struct tls_module main_tls;
36
37 void *__copy_tls(unsigned char *mem)
38 {
39         pthread_t td;
40         struct tls_module *p;
41         size_t i;
42         uintptr_t *dtv;
43
44 #ifdef TLS_ABOVE_TP
45         dtv = (uintptr_t*)(mem + libc.tls_size) - (libc.tls_cnt + 1);
46
47         mem += -((uintptr_t)mem + sizeof(struct pthread)) & (libc.tls_align-1);
48         td = (pthread_t)mem;
49         mem += sizeof(struct pthread);
50
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);
54         }
55 #else
56         dtv = (uintptr_t *)mem;
57
58         mem += libc.tls_size - sizeof(struct pthread);
59         mem -= (uintptr_t)mem & (libc.tls_align-1);
60         td = (pthread_t)mem;
61
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);
65         }
66 #endif
67         dtv[0] = libc.tls_cnt;
68         td->dtv = td->dtv_copy = dtv;
69         return td;
70 }
71
72 #if ULONG_MAX == 0xffffffff
73 typedef Elf32_Phdr Phdr;
74 #else
75 typedef Elf64_Phdr Phdr;
76 #endif
77
78 extern weak hidden const size_t _DYNAMIC[];
79
80 static void static_init_tls(size_t *aux)
81 {
82         unsigned char *p;
83         size_t n;
84         Phdr *phdr, *tls_phdr=0;
85         size_t base = 0;
86         void *mem;
87
88         for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
89                 phdr = (void *)p;
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)
95                         tls_phdr = phdr;
96                 if (phdr->p_type == PT_GNU_STACK &&
97                     phdr->p_memsz > __default_stacksize)
98                         __default_stacksize =
99                                 phdr->p_memsz < DEFAULT_STACK_MAX ?
100                                 phdr->p_memsz : DEFAULT_STACK_MAX;
101         }
102
103         if (tls_phdr) {
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;
108                 libc.tls_cnt = 1;
109                 libc.tls_head = &main_tls;
110         }
111
112         main_tls.size += (-main_tls.size - (uintptr_t)main_tls.image)
113                 & (main_tls.align-1);
114 #ifdef TLS_ABOVE_TP
115         main_tls.offset = GAP_ABOVE_TP;
116         main_tls.offset += -GAP_ABOVE_TP & (main_tls.align-1);
117 #else
118         main_tls.offset = main_tls.size;
119 #endif
120         if (main_tls.align < MIN_TLS_ALIGN) main_tls.align = MIN_TLS_ALIGN;
121
122         libc.tls_align = main_tls.align;
123         libc.tls_size = 2*sizeof(void *) + sizeof(struct pthread)
124 #ifdef TLS_ABOVE_TP
125                 + main_tls.offset
126 #endif
127                 + main_tls.size + main_tls.align
128                 + MIN_TLS_ALIGN-1 & -MIN_TLS_ALIGN;
129
130         if (libc.tls_size > sizeof builtin_tls) {
131 #ifndef SYS_mmap2
132 #define SYS_mmap2 SYS_mmap
133 #endif
134                 mem = (void *)__syscall(
135                         SYS_mmap2,
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(). */
141         } else {
142                 mem = builtin_tls;
143         }
144
145         /* Failure to initialize thread pointer is always fatal. */
146         if (__init_tp(__copy_tls(mem)) < 0)
147                 a_crash();
148 }
149
150 weak_alias(static_init_tls, __init_tls);