remove some cruft from libc/tls init code
[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 "pthread_impl.h"
6 #include "libc.h"
7 #include "atomic.h"
8 #include "syscall.h"
9
10 int __init_tp(void *p)
11 {
12         pthread_t td = p;
13         td->self = td;
14         if (__set_thread_area(TP_ADJ(p)) < 0)
15                 return -1;
16         td->tid = td->pid = __syscall(SYS_set_tid_address, &td->tid);
17         td->errno_ptr = &td->errno_val;
18         /* Currently, both of these predicates depend in the same thing:
19          * successful initialization of the thread pointer. However, in
20          * the future, we may support setups where setting the thread
21          * pointer is possible but threads other than the main thread
22          * cannot work, so it's best to keep the predicates separate. */
23         libc.has_thread_pointer = 1;
24         libc.can_do_threads = 1;
25         return 0;
26 }
27
28 #ifndef SHARED
29
30 static long long builtin_tls[(sizeof(struct pthread) + 64)/sizeof(long long)];
31
32 struct tls_image {
33         void *image;
34         size_t len, size, align;
35 } __static_tls ATTR_LIBC_VISIBILITY;
36
37 #define T __static_tls
38
39 void *__copy_tls(unsigned char *mem)
40 {
41         pthread_t td;
42         if (!T.image) return mem;
43         void **dtv = (void *)mem;
44         dtv[0] = (void *)1;
45 #ifdef TLS_ABOVE_TP
46         mem += sizeof(void *) * 2;
47         mem += -((uintptr_t)mem + sizeof(struct pthread)) & (T.align-1);
48         td = (pthread_t)mem;
49         mem += sizeof(struct pthread);
50 #else
51         mem += libc.tls_size - sizeof(struct pthread);
52         mem -= (uintptr_t)mem & (T.align-1);
53         td = (pthread_t)mem;
54         mem -= T.size;
55 #endif
56         td->dtv = dtv;
57         dtv[1] = mem;
58         memcpy(mem, T.image, T.len);
59         return td;
60 }
61
62 void *__tls_get_addr(size_t *v)
63 {
64         return (char *)__pthread_self()->dtv[1]+v[1];
65 }
66
67 #if ULONG_MAX == 0xffffffff
68 typedef Elf32_Phdr Phdr;
69 #else
70 typedef Elf64_Phdr Phdr;
71 #endif
72
73 void __init_tls(size_t *aux)
74 {
75         unsigned char *p;
76         size_t n;
77         Phdr *phdr, *tls_phdr=0;
78         size_t base = 0;
79         void *mem;
80
81         libc.tls_size = sizeof(struct pthread);
82
83         for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
84                 phdr = (void *)p;
85                 if (phdr->p_type == PT_PHDR)
86                         base = aux[AT_PHDR] - phdr->p_vaddr;
87                 if (phdr->p_type == PT_TLS)
88                         tls_phdr = phdr;
89         }
90
91         if (tls_phdr) {
92                 T.image = (void *)(base + tls_phdr->p_vaddr);
93                 T.len = tls_phdr->p_filesz;
94                 T.size = tls_phdr->p_memsz;
95                 T.align = tls_phdr->p_align;
96         }
97
98         T.size += (-T.size - (uintptr_t)T.image) & (T.align-1);
99         if (T.align < 4*sizeof(size_t)) T.align = 4*sizeof(size_t);
100
101         libc.tls_size = 2*sizeof(void *)+T.size+T.align+sizeof(struct pthread);
102
103         if (libc.tls_size > sizeof builtin_tls) {
104                 mem = (void *)__syscall(
105 #ifdef SYS_mmap2
106                         SYS_mmap2,
107 #else
108                         SYS_mmap,
109 #endif
110                         0, libc.tls_size, PROT_READ|PROT_WRITE,
111                         MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
112                 /* -4095...-1 cast to void * will crash on dereference anyway,
113                  * so don't bloat the init code checking for error codes and
114                  * explicitly calling a_crash(). */
115         } else {
116                 mem = builtin_tls;
117         }
118
119         /* Failure to initialize thread pointer is fatal if TLS is used. */
120         if (__init_tp(__copy_tls(mem)) < 0 && tls_phdr)
121                 a_crash();
122 }
123 #else
124 void __init_tls(size_t *auxv) { }
125 #endif