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