fix #ifdef inside a macro argument list in __init_tls.c
[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 = __syscall(SYS_set_tid_address, &td->tid);
19         td->locale = &libc.global_locale;
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 #if ULONG_MAX == 0xffffffff
58 typedef Elf32_Phdr Phdr;
59 #else
60 typedef Elf64_Phdr Phdr;
61 #endif
62
63 void __init_tls(size_t *aux)
64 {
65         unsigned char *p;
66         size_t n;
67         Phdr *phdr, *tls_phdr=0;
68         size_t base = 0;
69         void *mem;
70
71         libc.tls_size = sizeof(struct pthread);
72
73         for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
74                 phdr = (void *)p;
75                 if (phdr->p_type == PT_PHDR)
76                         base = aux[AT_PHDR] - phdr->p_vaddr;
77                 if (phdr->p_type == PT_TLS)
78                         tls_phdr = phdr;
79         }
80
81         if (tls_phdr) {
82                 T.image = (void *)(base + tls_phdr->p_vaddr);
83                 T.len = tls_phdr->p_filesz;
84                 T.size = tls_phdr->p_memsz;
85                 T.align = tls_phdr->p_align;
86         }
87
88         T.size += (-T.size - (uintptr_t)T.image) & (T.align-1);
89         if (T.align < 4*sizeof(size_t)) T.align = 4*sizeof(size_t);
90
91         libc.tls_size = 2*sizeof(void *)+T.size+T.align+sizeof(struct pthread);
92
93         if (libc.tls_size > sizeof builtin_tls) {
94 #ifndef SYS_mmap2
95 #define SYS_mmap2 SYS_mmap
96 #endif
97                 mem = (void *)__syscall(
98                         SYS_mmap2,
99                         0, libc.tls_size, PROT_READ|PROT_WRITE,
100                         MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
101                 /* -4095...-1 cast to void * will crash on dereference anyway,
102                  * so don't bloat the init code checking for error codes and
103                  * explicitly calling a_crash(). */
104         } else {
105                 mem = builtin_tls;
106         }
107
108         /* Failure to initialize thread pointer is fatal if TLS is used. */
109         if (__init_tp(__copy_tls(mem)) < 0 && tls_phdr)
110                 a_crash();
111 }
112 #else
113 void __init_tls(size_t *auxv) { }
114 #endif