remove cruft left behind when lazy thread pointer init was removed
[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 void *__mmap(void *, size_t, int, int, int, off_t);
68
69 #if ULONG_MAX == 0xffffffff
70 typedef Elf32_Phdr Phdr;
71 #else
72 typedef Elf64_Phdr Phdr;
73 #endif
74
75 void __init_tls(size_t *aux)
76 {
77         unsigned char *p;
78         size_t n;
79         Phdr *phdr, *tls_phdr=0;
80         size_t base = 0;
81         void *mem;
82
83         libc.tls_size = sizeof(struct pthread);
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_TLS)
90                         tls_phdr = phdr;
91         }
92
93         if (tls_phdr) {
94                 T.image = (void *)(base + tls_phdr->p_vaddr);
95                 T.len = tls_phdr->p_filesz;
96                 T.size = tls_phdr->p_memsz;
97                 T.align = tls_phdr->p_align;
98         }
99
100         T.size += (-T.size - (uintptr_t)T.image) & (T.align-1);
101         if (T.align < 4*sizeof(size_t)) T.align = 4*sizeof(size_t);
102
103         libc.tls_size = 2*sizeof(void *)+T.size+T.align+sizeof(struct pthread);
104
105         if (libc.tls_size > sizeof builtin_tls) {
106                 mem = (void *)__syscall(
107 #ifdef SYS_mmap2
108                         SYS_mmap2,
109 #else
110                         SYS_mmap,
111 #endif
112                         0, libc.tls_size, PROT_READ|PROT_WRITE,
113                         MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
114                 /* -4095...-1 cast to void * will crash on dereference anyway,
115                  * so don't bloat the init code checking for error codes and
116                  * explicitly calling a_crash(). */
117         } else {
118                 mem = builtin_tls;
119         }
120
121         /* Failure to initialize thread pointer is fatal if TLS is used. */
122         if (__init_tp(__copy_tls(mem)) < 0 && tls_phdr)
123                 a_crash();
124 }
125 #else
126 void __init_tls(size_t *auxv) { }
127 #endif