123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- #include <errno.h>
- #include <ldsodefs.h>
- #include <tls.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/param.h>
- #include <elf.h>
- #include <link.h>
- #include <string.h>
- #include <stdlib.h>
- #ifdef SHARED
- #error makefile bug, this file is for static only
- #endif
- #if USE_TLS
- extern ElfW(Phdr) *_dl_phdr;
- extern size_t _dl_phnum;
- static dtv_t static_dtv[2 + TLS_SLOTINFO_SURPLUS];
- static struct
- {
- struct dtv_slotinfo_list si;
-
- struct dtv_slotinfo info[2 + TLS_SLOTINFO_SURPLUS];
- } static_slotinfo;
- static struct link_map static_map;
- size_t _dl_tls_max_dtv_idx;
- bool _dl_tls_dtv_gaps;
- struct dtv_slotinfo_list *_dl_tls_dtv_slotinfo_list;
- size_t _dl_tls_static_nelem;
- size_t _dl_tls_static_size;
- size_t _dl_tls_static_used;
- size_t _dl_tls_static_align;
- size_t _dl_tls_generation;
- #ifdef TLS_INIT_HELPER
- TLS_INIT_HELPER
- #endif
- static inline void
- init_slotinfo (void)
- {
-
- static_slotinfo.si.len = (((char *) (&static_slotinfo + 1)
- - (char *) &static_slotinfo.si.slotinfo[0])
- / sizeof static_slotinfo.si.slotinfo[0]);
-
-
- GL(dl_tls_max_dtv_idx) = 1;
- GL(dl_tls_dtv_slotinfo_list) = &static_slotinfo.si;
- }
- static inline void
- init_static_tls (size_t memsz, size_t align)
- {
-
- GL(dl_tls_static_size) = roundup (memsz + GL(dl_tls_static_size),
- TLS_TCB_ALIGN);
- GL(dl_tls_static_used) = memsz;
-
- GL(dl_tls_static_align) = align;
-
- GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
- }
- void
- __libc_setup_tls (size_t tcbsize, size_t tcbalign)
- {
- void *tlsblock;
- size_t memsz = 0;
- size_t filesz = 0;
- void *initimage = NULL;
- size_t align = 0;
- size_t max_align = tcbalign;
- size_t tcb_offset;
- ElfW(Phdr) *phdr;
-
- if (_dl_phdr != NULL)
- for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr)
- if (phdr->p_type == PT_TLS)
- {
-
- memsz = phdr->p_memsz;
- filesz = phdr->p_filesz;
- initimage = (void *) phdr->p_vaddr;
- align = phdr->p_align;
- if (phdr->p_align > max_align)
- max_align = phdr->p_align;
- break;
- }
-
- # if defined(TLS_TCB_AT_TP)
- tcb_offset = roundup (memsz + GL(dl_tls_static_size), tcbalign);
- tlsblock = sbrk (tcb_offset + tcbsize + max_align);
- # elif defined(TLS_DTV_AT_TP)
- tcb_offset = roundup (tcbsize, align ?: 1);
- tlsblock = sbrk (tcb_offset + memsz + max_align
- + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size));
- tlsblock += TLS_PRE_TCB_SIZE;
- # else
-
- # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
- # endif
-
- tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
- & ~(max_align - 1));
-
- static_dtv[0].counter = (sizeof (static_dtv) / sizeof (static_dtv[0])) - 2;
-
-
- # if defined(TLS_TCB_AT_TP)
- static_dtv[2].pointer.val = ((char *) tlsblock + tcb_offset
- - roundup (memsz, align ?: 1));
- static_map.l_tls_offset = roundup (memsz, align ?: 1);
- # elif defined(TLS_DTV_AT_TP)
- static_dtv[2].pointer.val = (char *) tlsblock + tcb_offset;
- static_map.l_tls_offset = tcb_offset;
- # else
- # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
- # endif
- static_dtv[2].pointer.is_static = true;
-
- memcpy (static_dtv[2].pointer.val, initimage, filesz);
-
-
- # if defined(TLS_TCB_AT_TP)
- INSTALL_DTV ((char *) tlsblock + tcb_offset, static_dtv);
- const char *lossage = TLS_INIT_TP ((char *) tlsblock + tcb_offset, 0);
- # elif defined(TLS_DTV_AT_TP)
- INSTALL_DTV (tlsblock, static_dtv);
- const char *lossage = (char *)TLS_INIT_TP (tlsblock, 0);
- # else
- # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
- # endif
- if (__builtin_expect (lossage != NULL, 0))
- abort();
-
- static_map.l_tls_align = align;
- static_map.l_tls_blocksize = memsz;
- static_map.l_tls_initimage = initimage;
- static_map.l_tls_initimage_size = filesz;
- static_map.l_tls_modid = 1;
- init_slotinfo ();
-
- static_slotinfo.si.slotinfo[1].map = &static_map;
- memsz = roundup (memsz, align ?: 1);
- # if defined(TLS_TCB_AT_TP)
- memsz += tcbsize;
- # elif defined(TLS_DTV_AT_TP)
- memsz += tcb_offset;
- # endif
- init_static_tls (memsz, MAX (TLS_TCB_ALIGN, max_align));
- }
- int
- internal_function
- _dl_tls_setup (void)
- {
- init_slotinfo ();
- init_static_tls (
- # if defined(TLS_TCB_AT_TP)
- TLS_TCB_SIZE,
- # else
- 0,
- # endif
- TLS_TCB_ALIGN);
- return 0;
- }
- void
- __attribute__ ((weak))
- __pthread_initialize_minimal (void)
- {
- __libc_setup_tls (TLS_INIT_TCB_SIZE, TLS_INIT_TCB_ALIGN);
- }
- #elif defined NONTLS_INIT_TP
- void
- __attribute__ ((weak))
- __pthread_initialize_minimal (void)
- {
- NONTLS_INIT_TP;
- }
- #endif
|