implement PT_GNU_RELRO support
[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 static void *simple(void *p)
68 {
69         *(void **)p = p;
70         return __set_thread_area(TP_ADJ(p)) ? 0 : p;
71 }
72
73 weak_alias(simple, __install_initial_tls);
74
75 void *__mmap(void *, size_t, int, int, int, off_t);
76
77 #if ULONG_MAX == 0xffffffff
78 typedef Elf32_Phdr Phdr;
79 #else
80 typedef Elf64_Phdr Phdr;
81 #endif
82
83 void __init_tls(size_t *aux)
84 {
85         unsigned char *p;
86         size_t n;
87         Phdr *phdr, *tls_phdr=0;
88         size_t base = 0;
89         void *mem;
90
91         libc.tls_size = sizeof(struct pthread);
92
93         for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
94                 phdr = (void *)p;
95                 if (phdr->p_type == PT_PHDR)
96                         base = aux[AT_PHDR] - phdr->p_vaddr;
97                 if (phdr->p_type == PT_TLS)
98                         tls_phdr = phdr;
99         }
100
101         if (tls_phdr) {
102                 T.image = (void *)(base + tls_phdr->p_vaddr);
103                 T.len = tls_phdr->p_filesz;
104                 T.size = tls_phdr->p_memsz;
105                 T.align = tls_phdr->p_align;
106         }
107
108         T.size += (-T.size - (uintptr_t)T.image) & (T.align-1);
109         if (T.align < 4*sizeof(size_t)) T.align = 4*sizeof(size_t);
110
111         libc.tls_size = 2*sizeof(void *)+T.size+T.align+sizeof(struct pthread);
112
113         if (libc.tls_size > sizeof builtin_tls) {
114                 mem = (void *)__syscall(
115 #ifdef SYS_mmap2
116                         SYS_mmap2,
117 #else
118                         SYS_mmap,
119 #endif
120                         0, libc.tls_size, PROT_READ|PROT_WRITE,
121                         MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
122                 /* -4095...-1 cast to void * will crash on dereference anyway,
123                  * so don't bloat the init code checking for error codes and
124                  * explicitly calling a_crash(). */
125         } else {
126                 mem = builtin_tls;
127         }
128
129         /* Failure to initialize thread pointer is fatal if TLS is used. */
130         if (__init_tp(__copy_tls(mem)) < 0 && tls_phdr)
131                 a_crash();
132 }
133 #else
134 void __init_tls(size_t *auxv) { }
135 #endif