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
11 volatile int __thread_list_lock;
12
__init_tp(void * p)13 int __init_tp(void *p)
14 {
15 pthread_t td = p;
16 td->self = td;
17 int r = __set_thread_area(TP_ADJ(p));
18 if (r < 0) return -1;
19 if (!r) libc.can_do_threads = 1;
20 td->detach_state = DT_JOINABLE;
21 td->tid = 0;
22 /* TRUSTY - no equivalent.
23 * td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
24 */
25 td->locale = &libc.global_locale;
26 td->robust_list.head = &td->robust_list.head;
27 td->sysinfo = __sysinfo;
28 td->next = td->prev = td;
29 return 0;
30 }
31
32 static struct builtin_tls {
33 char c;
34 struct pthread pt;
35 void *space[16];
36 } builtin_tls[1];
37 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
38
39 static struct tls_module main_tls;
40
__copy_tls(unsigned char * mem)41 void *__copy_tls(unsigned char *mem)
42 {
43 pthread_t td;
44 struct tls_module *p;
45 size_t i;
46 uintptr_t *dtv;
47
48 #ifdef TLS_ABOVE_TP
49 dtv = (uintptr_t*)(mem + libc.tls_size) - (libc.tls_cnt + 1);
50
51 mem += -((uintptr_t)mem + sizeof(struct pthread)) & (libc.tls_align-1);
52 td = (pthread_t)mem;
53 mem += sizeof(struct pthread);
54
55 for (i=1, p=libc.tls_head; p; i++, p=p->next) {
56 dtv[i] = (uintptr_t)(mem + p->offset) + DTP_OFFSET;
57 memcpy(mem + p->offset, p->image, p->len);
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 memcpy(mem - p->offset, p->image, p->len);
69 }
70 #endif
71 dtv[0] = libc.tls_cnt;
72 td->dtv = td->dtv_copy = dtv;
73 return td;
74 }
75
76 #if ULONG_MAX == 0xffffffff
77 typedef Elf32_Phdr Phdr;
78 #else
79 typedef Elf64_Phdr Phdr;
80 #endif
81
82 extern weak hidden const size_t _DYNAMIC[];
83
static_init_tls(size_t * aux)84 static void static_init_tls(size_t *aux)
85 {
86 unsigned char *p;
87 size_t n;
88 Phdr *phdr, *tls_phdr=0;
89 size_t base = 0;
90 void *mem;
91
92 for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
93 phdr = (void *)p;
94 if (phdr->p_type == PT_PHDR)
95 base = aux[AT_PHDR] - phdr->p_vaddr;
96 if (phdr->p_type == PT_DYNAMIC && _DYNAMIC)
97 base = (size_t)_DYNAMIC - phdr->p_vaddr;
98 if (phdr->p_type == PT_TLS)
99 tls_phdr = phdr;
100 if (phdr->p_type == PT_GNU_STACK &&
101 phdr->p_memsz > __default_stacksize)
102 __default_stacksize =
103 phdr->p_memsz < DEFAULT_STACK_MAX ?
104 phdr->p_memsz : DEFAULT_STACK_MAX;
105 }
106
107 if (tls_phdr) {
108 main_tls.image = (void *)(base + tls_phdr->p_vaddr);
109 main_tls.len = tls_phdr->p_filesz;
110 main_tls.size = tls_phdr->p_memsz;
111 main_tls.align = tls_phdr->p_align;
112 libc.tls_cnt = 1;
113 libc.tls_head = &main_tls;
114 }
115
116 main_tls.size += (-main_tls.size - (uintptr_t)main_tls.image)
117 & (main_tls.align-1);
118 #ifdef TLS_ABOVE_TP
119 main_tls.offset = GAP_ABOVE_TP;
120 main_tls.offset += (-GAP_ABOVE_TP + (uintptr_t)main_tls.image)
121 & (main_tls.align-1);
122 #else
123 main_tls.offset = main_tls.size;
124 #endif
125 if (main_tls.align < MIN_TLS_ALIGN) main_tls.align = MIN_TLS_ALIGN;
126
127 libc.tls_align = main_tls.align;
128 libc.tls_size = 2*sizeof(void *) + sizeof(struct pthread)
129 #ifdef TLS_ABOVE_TP
130 + main_tls.offset
131 #endif
132 + main_tls.size + main_tls.align
133 + MIN_TLS_ALIGN-1 & -MIN_TLS_ALIGN;
134
135 if (libc.tls_size > sizeof builtin_tls) {
136 /* TRUSTY - no mmap. */
137 mem = 0;
138 a_crash();
139 } else {
140 mem = builtin_tls;
141 }
142
143 /* Failure to initialize thread pointer is always fatal. */
144 if (__init_tp(__copy_tls(mem)) < 0)
145 a_crash();
146 }
147
148 weak_alias(static_init_tls, __init_tls);
149