Rich Felker | 8431d79 | 2012-10-04 16:35:46 -0400 | [diff] [blame] | 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; |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 10 | static size_t len, size, align; |
Rich Felker | 8431d79 | 2012-10-04 16:35:46 -0400 | [diff] [blame] | 11 | |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame^] | 12 | void *__copy_tls(unsigned char *mem) |
Rich Felker | 8431d79 | 2012-10-04 16:35:46 -0400 | [diff] [blame] | 13 | { |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 14 | mem += -size & (4*sizeof(size_t)-1); |
Rich Felker | 8431d79 | 2012-10-04 16:35:46 -0400 | [diff] [blame] | 15 | mem += ((uintptr_t)image - (uintptr_t)mem) & (align-1); |
| 16 | memcpy(mem, image, len); |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 17 | return mem + size; |
Rich Felker | 8431d79 | 2012-10-04 16:35:46 -0400 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | static void *simple(void *p) |
| 21 | { |
| 22 | *(void **)p = p; |
| 23 | return __set_thread_area(p) ? 0 : p; |
| 24 | } |
| 25 | |
| 26 | weak_alias(simple, __install_initial_tls); |
| 27 | |
| 28 | void *__mmap(void *, size_t, int, int, int, off_t); |
| 29 | |
| 30 | #if ULONG_MAX == 0xffffffff |
| 31 | typedef Elf32_Phdr Phdr; |
| 32 | #else |
| 33 | typedef Elf64_Phdr Phdr; |
| 34 | #endif |
| 35 | |
| 36 | #define AUX_CNT 6 |
| 37 | |
| 38 | void __init_tls(size_t *auxv) |
| 39 | { |
| 40 | size_t i, aux[AUX_CNT] = { 0 }; |
| 41 | unsigned char *p, *mem; |
| 42 | size_t n, d; |
| 43 | Phdr *phdr, *tls_phdr=0; |
| 44 | size_t base = 0; |
| 45 | |
| 46 | for (; auxv[0]; auxv+=2) if (auxv[0]<AUX_CNT) aux[auxv[0]] = auxv[1]; |
| 47 | p = (void *)aux[AT_PHDR]; |
| 48 | for (p=(void *)aux[AT_PHDR]; aux[AT_PHNUM]--; p+=aux[AT_PHENT]) { |
| 49 | phdr = (void *)p; |
| 50 | if (phdr->p_type == PT_PHDR) |
| 51 | base = aux[AT_PHDR] - phdr->p_vaddr; |
| 52 | if (phdr->p_type == PT_TLS) |
| 53 | tls_phdr = phdr; |
| 54 | } |
| 55 | if (!tls_phdr) return; |
| 56 | |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 57 | libc.tls_size = size+align+8*sizeof(size_t)+sizeof(struct pthread); |
Rich Felker | 8431d79 | 2012-10-04 16:35:46 -0400 | [diff] [blame] | 58 | |
| 59 | image = (void *)(base + tls_phdr->p_vaddr); |
| 60 | len = tls_phdr->p_filesz; |
Rich Felker | bc6a35f | 2012-10-04 20:04:13 -0400 | [diff] [blame] | 61 | size = tls_phdr->p_memsz; |
Rich Felker | 8431d79 | 2012-10-04 16:35:46 -0400 | [diff] [blame] | 62 | align = tls_phdr->p_align; |
Rich Felker | 8431d79 | 2012-10-04 16:35:46 -0400 | [diff] [blame] | 63 | mem = __mmap(0, libc.tls_size, PROT_READ|PROT_WRITE, |
| 64 | MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); |
| 65 | if (mem == MAP_FAILED) a_crash(); |
| 66 | |
Rich Felker | dcd6037 | 2012-10-05 11:51:50 -0400 | [diff] [blame^] | 67 | if (!__install_initial_tls(__copy_tls(mem))) a_crash(); |
Rich Felker | 8431d79 | 2012-10-04 16:35:46 -0400 | [diff] [blame] | 68 | } |
| 69 | #else |
| 70 | void __init_tls(size_t *auxv) { } |
| 71 | #endif |