remove useless visibility application from static-linking-only code
[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 int __init_tp(void *p)
12 {
13         pthread_t td = p;
14         td->self = td;
15         int r = __set_thread_area(TP_ADJ(p));
16         if (r < 0) return -1;
17         if (!r) libc.can_do_threads = 1;
18         td->tid = __syscall(SYS_set_tid_address, &td->tid);
19         td->locale = &libc.global_locale;
20         td->robust_list.head = &td->robust_list.head;
21         return 0;
22 }
23
24 #ifndef SHARED
25
26 static struct builtin_tls {
27         char c;
28         struct pthread pt;
29         void *space[16];
30 } builtin_tls[1];
31 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
32
33 struct tls_image {
34         void *image;
35         size_t len, size, align;
36 } __static_tls;
37
38 #define T __static_tls
39
40 void *__copy_tls(unsigned char *mem)
41 {
42         pthread_t td;
43         if (!T.image) return mem;
44         void **dtv = (void *)mem;
45         dtv[0] = (void *)1;
46 #ifdef TLS_ABOVE_TP
47         mem += sizeof(void *) * 2;
48         mem += -((uintptr_t)mem + sizeof(struct pthread)) & (T.align-1);
49         td = (pthread_t)mem;
50         mem += sizeof(struct pthread);
51 #else
52         mem += libc.tls_size - sizeof(struct pthread);
53         mem -= (uintptr_t)mem & (T.align-1);
54         td = (pthread_t)mem;
55         mem -= T.size;
56 #endif
57         td->dtv = td->dtv_copy = dtv;
58         dtv[1] = mem;
59         memcpy(mem, T.image, T.len);
60         return td;
61 }
62
63 #if ULONG_MAX == 0xffffffff
64 typedef Elf32_Phdr Phdr;
65 #else
66 typedef Elf64_Phdr Phdr;
67 #endif
68
69 void __init_tls(size_t *aux)
70 {
71         unsigned char *p;
72         size_t n;
73         Phdr *phdr, *tls_phdr=0;
74         size_t base = 0;
75         void *mem;
76
77         libc.tls_size = sizeof(struct pthread);
78
79         for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
80                 phdr = (void *)p;
81                 if (phdr->p_type == PT_PHDR)
82                         base = aux[AT_PHDR] - phdr->p_vaddr;
83                 if (phdr->p_type == PT_TLS)
84                         tls_phdr = phdr;
85         }
86
87         if (tls_phdr) {
88                 T.image = (void *)(base + tls_phdr->p_vaddr);
89                 T.len = tls_phdr->p_filesz;
90                 T.size = tls_phdr->p_memsz;
91                 T.align = tls_phdr->p_align;
92         }
93
94         T.size += (-T.size - (uintptr_t)T.image) & (T.align-1);
95         if (T.align < MIN_TLS_ALIGN) T.align = MIN_TLS_ALIGN;
96
97         libc.tls_size = 2*sizeof(void *)+T.size+T.align+sizeof(struct pthread);
98
99         if (libc.tls_size > sizeof builtin_tls) {
100 #ifndef SYS_mmap2
101 #define SYS_mmap2 SYS_mmap
102 #endif
103                 mem = (void *)__syscall(
104                         SYS_mmap2,
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 always fatal. */
115         if (__init_tp(__copy_tls(mem)) < 0)
116                 a_crash();
117 }
118 #else
119 void __init_tls(size_t *auxv) { }
120 #endif