define and use internal macros for hidden visibility, weak refs
[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 int __init_tp(void *p)
12 {
13         pthread_t td = p;
14         td->self = td;
15         int r = __set_thread_area(TP_ADJ(p));
16         if (r < 0) return -1;
17         if (!r) libc.can_do_threads = 1;
18         td->detach_state = DT_JOINABLE;
19         td->tid = __syscall(SYS_set_tid_address, &td->detach_state);
20         td->locale = &libc.global_locale;
21         td->robust_list.head = &td->robust_list.head;
22         return 0;
23 }
24
25 static struct builtin_tls {
26         char c;
27         struct pthread pt;
28         void *space[16];
29 } builtin_tls[1];
30 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
31
32 static struct tls_module main_tls;
33
34 void *__copy_tls(unsigned char *mem)
35 {
36         pthread_t td;
37         struct tls_module *p;
38         size_t i;
39         void **dtv;
40
41 #ifdef TLS_ABOVE_TP
42         dtv = (void **)(mem + libc.tls_size) - (libc.tls_cnt + 1);
43
44         mem += -((uintptr_t)mem + sizeof(struct pthread)) & (libc.tls_align-1);
45         td = (pthread_t)mem;
46         mem += sizeof(struct pthread);
47
48         for (i=1, p=libc.tls_head; p; i++, p=p->next) {
49                 dtv[i] = mem + p->offset;
50                 memcpy(dtv[i], p->image, p->len);
51         }
52 #else
53         dtv = (void **)mem;
54
55         mem += libc.tls_size - sizeof(struct pthread);
56         mem -= (uintptr_t)mem & (libc.tls_align-1);
57         td = (pthread_t)mem;
58
59         for (i=1, p=libc.tls_head; p; i++, p=p->next) {
60                 dtv[i] = mem - p->offset;
61                 memcpy(dtv[i], p->image, p->len);
62         }
63 #endif
64         dtv[0] = (void *)libc.tls_cnt;
65         td->dtv = td->dtv_copy = dtv;
66         return td;
67 }
68
69 #if ULONG_MAX == 0xffffffff
70 typedef Elf32_Phdr Phdr;
71 #else
72 typedef Elf64_Phdr Phdr;
73 #endif
74
75 extern weak hidden const size_t _DYNAMIC[];
76
77 static void static_init_tls(size_t *aux)
78 {
79         unsigned char *p;
80         size_t n;
81         Phdr *phdr, *tls_phdr=0;
82         size_t base = 0;
83         void *mem;
84
85         for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
86                 phdr = (void *)p;
87                 if (phdr->p_type == PT_PHDR)
88                         base = aux[AT_PHDR] - phdr->p_vaddr;
89                 if (phdr->p_type == PT_DYNAMIC && _DYNAMIC)
90                         base = (size_t)_DYNAMIC - phdr->p_vaddr;
91                 if (phdr->p_type == PT_TLS)
92                         tls_phdr = phdr;
93         }
94
95         if (tls_phdr) {
96                 main_tls.image = (void *)(base + tls_phdr->p_vaddr);
97                 main_tls.len = tls_phdr->p_filesz;
98                 main_tls.size = tls_phdr->p_memsz;
99                 main_tls.align = tls_phdr->p_align;
100                 libc.tls_cnt = 1;
101                 libc.tls_head = &main_tls;
102         }
103
104         main_tls.size += (-main_tls.size - (uintptr_t)main_tls.image)
105                 & (main_tls.align-1);
106 #ifdef TLS_ABOVE_TP
107         main_tls.offset = GAP_ABOVE_TP;
108         main_tls.offset += -GAP_ABOVE_TP & (main_tls.align-1);
109 #else
110         main_tls.offset = main_tls.size;
111 #endif
112         if (main_tls.align < MIN_TLS_ALIGN) main_tls.align = MIN_TLS_ALIGN;
113
114         libc.tls_align = main_tls.align;
115         libc.tls_size = 2*sizeof(void *) + sizeof(struct pthread)
116 #ifdef TLS_ABOVE_TP
117                 + main_tls.offset
118 #endif
119                 + main_tls.size + main_tls.align
120                 + MIN_TLS_ALIGN-1 & -MIN_TLS_ALIGN;
121
122         if (libc.tls_size > sizeof builtin_tls) {
123 #ifndef SYS_mmap2
124 #define SYS_mmap2 SYS_mmap
125 #endif
126                 mem = (void *)__syscall(
127                         SYS_mmap2,
128                         0, libc.tls_size, PROT_READ|PROT_WRITE,
129                         MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
130                 /* -4095...-1 cast to void * will crash on dereference anyway,
131                  * so don't bloat the init code checking for error codes and
132                  * explicitly calling a_crash(). */
133         } else {
134                 mem = builtin_tls;
135         }
136
137         /* Failure to initialize thread pointer is always fatal. */
138         if (__init_tp(__copy_tls(mem)) < 0)
139                 a_crash();
140 }
141
142 weak_alias(static_init_tls, __init_tls);