__init_tls.c 3.7 KB

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