__init_tls.c 3.5 KB

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