clean up and refactor program initialization
[musl] / src / env / __init_tls.c
1 #include <elf.h>
2 #include <limits.h>
3 #include "pthread_impl.h"
4 #include "libc.h"
5 #include "atomic.h"
6
7 #ifndef SHARED
8
9 static void *image;
10 static size_t len, size, align;
11
12 void *__copy_tls(unsigned char *mem)
13 {
14         pthread_t td;
15         if (!image) return mem;
16         void **dtv = (void *)mem;
17         dtv[0] = (void *)1;
18         mem += __libc.tls_size - sizeof(struct pthread);
19         mem -= (uintptr_t)mem & (align-1);
20         td = (pthread_t)mem;
21         td->dtv = dtv;
22         mem -= size;
23         dtv[1] = mem;
24         memcpy(mem, image, len);
25         return td;
26 }
27
28 void *__tls_get_addr(size_t *v)
29 {
30         return (char *)__pthread_self()->dtv[1]+v[1];
31 }
32
33 static void *simple(void *p)
34 {
35         *(void **)p = p;
36         return __set_thread_area(p) ? 0 : p;
37 }
38
39 weak_alias(simple, __install_initial_tls);
40
41 void *__mmap(void *, size_t, int, int, int, off_t);
42
43 #if ULONG_MAX == 0xffffffff
44 typedef Elf32_Phdr Phdr;
45 #else
46 typedef Elf64_Phdr Phdr;
47 #endif
48
49 void __init_tls(size_t *aux)
50 {
51         unsigned char *p, *mem;
52         size_t n, d;
53         Phdr *phdr, *tls_phdr=0;
54         size_t base = 0;
55
56         for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
57                 phdr = (void *)p;
58                 if (phdr->p_type == PT_PHDR)
59                         base = aux[AT_PHDR] - phdr->p_vaddr;
60                 if (phdr->p_type == PT_TLS)
61                         tls_phdr = phdr;
62         }
63         if (!tls_phdr) return;
64
65         image = (void *)(base + tls_phdr->p_vaddr);
66         len = tls_phdr->p_filesz;
67         size = tls_phdr->p_memsz;
68         align = tls_phdr->p_align;
69
70         size += (-size - (uintptr_t)image) & (align-1);
71         if (align < 4*sizeof(size_t)) align = 4*sizeof(size_t);
72
73         libc.tls_size = 2*sizeof(void *)+size+align+sizeof(struct pthread);
74
75         mem = __mmap(0, libc.tls_size, PROT_READ|PROT_WRITE,
76                 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
77         if (!__install_initial_tls(__copy_tls(mem))) a_crash();
78 }
79 #else
80 void __init_tls(size_t *auxv) { }
81 #endif