__init_tls.c 3.0 KB

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