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