optimize out setting up robust list with kernel when not needed
[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         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;
22         return 0;
23 }
24
25 #ifndef SHARED
26
27 static struct builtin_tls {
28         char c;
29         struct pthread pt;
30         void *space[16];
31 } builtin_tls[1];
32 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
33
34 struct tls_image {
35         void *image;
36         size_t len, size, align;
37 } __static_tls ATTR_LIBC_VISIBILITY;
38
39 #define T __static_tls
40
41 void *__copy_tls(unsigned char *mem)
42 {
43         pthread_t td;
44         if (!T.image) return mem;
45         void **dtv = (void *)mem;
46         dtv[0] = (void *)1;
47 #ifdef TLS_ABOVE_TP
48         mem += sizeof(void *) * 2;
49         mem += -((uintptr_t)mem + sizeof(struct pthread)) & (T.align-1);
50         td = (pthread_t)mem;
51         mem += sizeof(struct pthread);
52 #else
53         mem += libc.tls_size - sizeof(struct pthread);
54         mem -= (uintptr_t)mem & (T.align-1);
55         td = (pthread_t)mem;
56         mem -= T.size;
57 #endif
58         td->dtv = td->dtv_copy = dtv;
59         dtv[1] = mem;
60         memcpy(mem, T.image, T.len);
61         return td;
62 }
63
64 #if ULONG_MAX == 0xffffffff
65 typedef Elf32_Phdr Phdr;
66 #else
67 typedef Elf64_Phdr Phdr;
68 #endif
69
70 void __init_tls(size_t *aux)
71 {
72         unsigned char *p;
73         size_t n;
74         Phdr *phdr, *tls_phdr=0;
75         size_t base = 0;
76         void *mem;
77
78         libc.tls_size = sizeof(struct pthread);
79
80         for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
81                 phdr = (void *)p;
82                 if (phdr->p_type == PT_PHDR)
83                         base = aux[AT_PHDR] - phdr->p_vaddr;
84                 if (phdr->p_type == PT_TLS)
85                         tls_phdr = phdr;
86         }
87
88         if (tls_phdr) {
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;
93         }
94
95         T.size += (-T.size - (uintptr_t)T.image) & (T.align-1);
96         if (T.align < MIN_TLS_ALIGN) T.align = MIN_TLS_ALIGN;
97
98         libc.tls_size = 2*sizeof(void *)+T.size+T.align+sizeof(struct pthread);
99
100         if (libc.tls_size > sizeof builtin_tls) {
101 #ifndef SYS_mmap2
102 #define SYS_mmap2 SYS_mmap
103 #endif
104                 mem = (void *)__syscall(
105                         SYS_mmap2,
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(). */
111         } else {
112                 mem = builtin_tls;
113         }
114
115         /* Failure to initialize thread pointer is fatal if TLS is used. */
116         if (__init_tp(__copy_tls(mem)) < 0 && tls_phdr)
117                 a_crash();
118 }
119 #else
120 void __init_tls(size_t *auxv) { }
121 #endif