+#define SYSCALL_NO_TLS 1
#include <elf.h>
#include <limits.h>
#include <sys/mman.h>
#include "atomic.h"
#include "syscall.h"
+volatile int __thread_list_lock;
+
int __init_tp(void *p)
{
pthread_t td = p;
int r = __set_thread_area(TP_ADJ(p));
if (r < 0) return -1;
if (!r) libc.can_do_threads = 1;
- td->join_futex = -1;
- td->tid = __syscall(SYS_set_tid_address, &td->join_futex);
+ td->detach_state = DT_JOINABLE;
+ td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
td->locale = &libc.global_locale;
td->robust_list.head = &td->robust_list.head;
+ td->sysinfo = __sysinfo;
+ td->next = td->prev = td;
return 0;
}
pthread_t td;
struct tls_module *p;
size_t i;
- void **dtv;
+ uintptr_t *dtv;
#ifdef TLS_ABOVE_TP
- dtv = (void **)(mem + libc.tls_size) - (libc.tls_cnt + 1);
+ dtv = (uintptr_t*)(mem + libc.tls_size) - (libc.tls_cnt + 1);
mem += -((uintptr_t)mem + sizeof(struct pthread)) & (libc.tls_align-1);
td = (pthread_t)mem;
mem += sizeof(struct pthread);
for (i=1, p=libc.tls_head; p; i++, p=p->next) {
- dtv[i] = mem + p->offset;
- memcpy(dtv[i], p->image, p->len);
+ dtv[i] = (uintptr_t)(mem + p->offset) + DTP_OFFSET;
+ memcpy(mem + p->offset, p->image, p->len);
}
#else
- dtv = (void **)mem;
+ dtv = (uintptr_t *)mem;
mem += libc.tls_size - sizeof(struct pthread);
mem -= (uintptr_t)mem & (libc.tls_align-1);
td = (pthread_t)mem;
for (i=1, p=libc.tls_head; p; i++, p=p->next) {
- dtv[i] = mem - p->offset;
- memcpy(dtv[i], p->image, p->len);
+ dtv[i] = (uintptr_t)(mem - p->offset) + DTP_OFFSET;
+ memcpy(mem - p->offset, p->image, p->len);
}
#endif
- dtv[0] = (void *)libc.tls_cnt;
- td->dtv = td->dtv_copy = dtv;
+ dtv[0] = libc.tls_cnt;
+ td->dtv = dtv;
return td;
}
typedef Elf64_Phdr Phdr;
#endif
-__attribute__((__weak__, __visibility__("hidden")))
-extern const size_t _DYNAMIC[];
+extern weak hidden const size_t _DYNAMIC[];
static void static_init_tls(size_t *aux)
{
base = (size_t)_DYNAMIC - phdr->p_vaddr;
if (phdr->p_type == PT_TLS)
tls_phdr = phdr;
+ if (phdr->p_type == PT_GNU_STACK &&
+ phdr->p_memsz > __default_stacksize)
+ __default_stacksize =
+ phdr->p_memsz < DEFAULT_STACK_MAX ?
+ phdr->p_memsz : DEFAULT_STACK_MAX;
}
if (tls_phdr) {
main_tls.size += (-main_tls.size - (uintptr_t)main_tls.image)
& (main_tls.align-1);
- if (main_tls.align < MIN_TLS_ALIGN) main_tls.align = MIN_TLS_ALIGN;
-#ifndef TLS_ABOVE_TP
+#ifdef TLS_ABOVE_TP
+ main_tls.offset = GAP_ABOVE_TP;
+ main_tls.offset += (-GAP_ABOVE_TP + (uintptr_t)main_tls.image)
+ & (main_tls.align-1);
+#else
main_tls.offset = main_tls.size;
#endif
+ if (main_tls.align < MIN_TLS_ALIGN) main_tls.align = MIN_TLS_ALIGN;
libc.tls_align = main_tls.align;
libc.tls_size = 2*sizeof(void *) + sizeof(struct pthread)
+#ifdef TLS_ABOVE_TP
+ + main_tls.offset
+#endif
+ main_tls.size + main_tls.align
+ MIN_TLS_ALIGN-1 & -MIN_TLS_ALIGN;