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