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