1 #define SYSCALL_NO_TLS 1
2 #include <elf.h>
3 #include <limits.h>
4 #include <sys/mman.h>
5 #include <string.h>
6 #include <stddef.h>
7 #include "pthread_impl.h"
8 #include "libc.h"
9 #include "atomic.h"
10 #include "syscall.h"
11
12 volatile int __thread_list_lock;
13
__init_tp(void * p)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->detach_state = DT_JOINABLE;
22 td->tid = td->pid = __syscall(SYS_set_tid_address, &__thread_list_lock);
23 td->proc_tid = -1;
24 td->locale = &libc.global_locale;
25 td->robust_list.head = &td->robust_list.head;
26 td->sysinfo = __sysinfo;
27 td->next = td->prev = td;
28 return 0;
29 }
30
31 static struct builtin_tls {
32 char c[8];
33 struct pthread pt;
34 void *space[16];
35 } builtin_tls[1];
36 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
37
38 static struct tls_module main_tls;
39
__copy_tls(unsigned char * mem)40 void *__copy_tls(unsigned char *mem)
41 {
42 pthread_t td;
43 struct tls_module *p;
44 size_t i;
45 uintptr_t *dtv;
46
47 #ifdef TLS_ABOVE_TP
48 dtv = (uintptr_t*)(mem + libc.tls_size) - (libc.tls_cnt + 1);
49
50 mem += -((uintptr_t)mem + sizeof(struct pthread)) & (libc.tls_align-1);
51 td = (pthread_t)mem;
52 mem += sizeof(struct pthread);
53
54 for (i=1, p=libc.tls_head; p; i++, p=p->next) {
55 dtv[i] = (uintptr_t)(mem + p->offset) + DTP_OFFSET;
56 if (p->image) {
57 memcpy(mem + p->offset, p->image, p->len);
58 }
59 }
60 #else
61 dtv = (uintptr_t *)mem;
62
63 mem += libc.tls_size - sizeof(struct pthread);
64 mem -= (uintptr_t)mem & (libc.tls_align-1);
65 td = (pthread_t)mem;
66
67 for (i=1, p=libc.tls_head; p; i++, p=p->next) {
68 dtv[i] = (uintptr_t)(mem - p->offset) + DTP_OFFSET;
69 if (p->image) {
70 memcpy(mem - p->offset, p->image, p->len);
71 }
72 }
73 #endif
74 dtv[0] = libc.tls_cnt;
75 td->dtv = dtv;
76 return td;
77 }
78
79 #if ULONG_MAX == 0xffffffff
80 typedef Elf32_Phdr Phdr;
81 #else
82 typedef Elf64_Phdr Phdr;
83 #endif
84
85 extern weak hidden const size_t _DYNAMIC[];
86
static_init_tls(size_t * aux)87 static void static_init_tls(size_t *aux)
88 {
89 unsigned char *p;
90 size_t n;
91 Phdr *phdr, *tls_phdr=0;
92 size_t base = 0;
93 void *mem;
94
95 for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
96 phdr = (void *)p;
97 if (phdr->p_type == PT_PHDR)
98 base = aux[AT_PHDR] - phdr->p_vaddr;
99 if (phdr->p_type == PT_DYNAMIC && _DYNAMIC)
100 base = (size_t)_DYNAMIC - phdr->p_vaddr;
101 if (phdr->p_type == PT_TLS)
102 tls_phdr = phdr;
103 if (phdr->p_type == PT_GNU_STACK &&
104 phdr->p_memsz > __default_stacksize)
105 __default_stacksize =
106 phdr->p_memsz < DEFAULT_STACK_MAX ?
107 phdr->p_memsz : DEFAULT_STACK_MAX;
108 }
109
110 if (tls_phdr) {
111 main_tls.image = (void *)(base + tls_phdr->p_vaddr);
112 main_tls.len = tls_phdr->p_filesz;
113 main_tls.size = tls_phdr->p_memsz;
114 main_tls.align = tls_phdr->p_align;
115 libc.tls_cnt = 1;
116 libc.tls_head = &main_tls;
117 }
118
119 main_tls.size += (-main_tls.size - (uintptr_t)main_tls.image)
120 & (main_tls.align-1);
121 #ifdef TLS_ABOVE_TP
122 main_tls.offset = GAP_ABOVE_TP;
123 main_tls.offset += (-GAP_ABOVE_TP + (uintptr_t)main_tls.image)
124 & (main_tls.align-1);
125 #else
126 main_tls.offset = main_tls.size;
127 #endif
128 if (main_tls.align < MIN_TLS_ALIGN) main_tls.align = MIN_TLS_ALIGN;
129
130 libc.tls_align = main_tls.align;
131 libc.tls_size = 2*sizeof(void *) + sizeof(struct pthread)
132 #ifdef TLS_ABOVE_TP
133 + main_tls.offset
134 #endif
135 + main_tls.size + main_tls.align
136 + MIN_TLS_ALIGN-1 & -MIN_TLS_ALIGN;
137
138 if (libc.tls_size > sizeof builtin_tls) {
139 #ifndef SYS_mmap2
140 #define SYS_mmap2 SYS_mmap
141 #endif
142 mem = (void *)__syscall(
143 SYS_mmap2,
144 0, libc.tls_size, PROT_READ|PROT_WRITE,
145 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
146 /* -4095...-1 cast to void * will crash on dereference anyway,
147 * so don't bloat the init code checking for error codes and
148 * explicitly calling a_crash(). */
149 } else {
150 mem = builtin_tls;
151 }
152
153 /* Failure to initialize thread pointer is always fatal. */
154 if (__init_tp(__copy_tls(mem)) < 0)
155 a_crash();
156 }
157
158 weak_alias(static_init_tls, __init_tls);
159