libc-tls.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* Initialization code for TLS in statically linked application.
  2. Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <ldsodefs.h>
  17. #include <tls.h>
  18. #include <unistd.h>
  19. #include <stdio.h>
  20. #include <sys/param.h>
  21. #include <elf.h>
  22. #include <link.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #ifdef SHARED
  26. #error makefile bug, this file is for static only
  27. #endif
  28. #if USE_TLS
  29. extern ElfW(Phdr) *_dl_phdr;
  30. extern size_t _dl_phnum;
  31. static dtv_t static_dtv[2 + TLS_SLOTINFO_SURPLUS];
  32. static struct
  33. {
  34. struct dtv_slotinfo_list si;
  35. /* The dtv_slotinfo_list data structure does not include the actual
  36. information since it is defined as an array of size zero. We define
  37. here the necessary entries. Note that it is not important whether
  38. there is padding or not since we will always access the information
  39. through the 'si' element. */
  40. struct dtv_slotinfo info[2 + TLS_SLOTINFO_SURPLUS];
  41. } static_slotinfo;
  42. /* Fake link map for the application. */
  43. static struct link_map static_map;
  44. /* Highest dtv index currently needed. */
  45. size_t _dl_tls_max_dtv_idx;
  46. /* Flag signalling whether there are gaps in the module ID allocation. */
  47. bool _dl_tls_dtv_gaps;
  48. /* Information about the dtv slots. */
  49. struct dtv_slotinfo_list *_dl_tls_dtv_slotinfo_list;
  50. /* Number of modules in the static TLS block. */
  51. size_t _dl_tls_static_nelem;
  52. /* Size of the static TLS block. */
  53. size_t _dl_tls_static_size;
  54. /* Size actually allocated in the static TLS block. */
  55. size_t _dl_tls_static_used;
  56. /* Alignment requirement of the static TLS block. */
  57. size_t _dl_tls_static_align;
  58. /* Generation counter for the dtv. */
  59. size_t _dl_tls_generation;
  60. /* Additional definitions needed by TLS initialization. */
  61. #ifdef TLS_INIT_HELPER
  62. TLS_INIT_HELPER
  63. #endif
  64. static inline void
  65. init_slotinfo (void)
  66. {
  67. /* Create the slotinfo list. */
  68. static_slotinfo.si.len = (((char *) (&static_slotinfo + 1)
  69. - (char *) &static_slotinfo.si.slotinfo[0])
  70. / sizeof static_slotinfo.si.slotinfo[0]);
  71. // static_slotinfo.si.next = NULL; already zero
  72. /* The slotinfo list. Will be extended by the code doing dynamic
  73. linking. */
  74. GL(dl_tls_max_dtv_idx) = 1;
  75. GL(dl_tls_dtv_slotinfo_list) = &static_slotinfo.si;
  76. }
  77. static inline void
  78. init_static_tls (size_t memsz, size_t align)
  79. {
  80. /* That is the size of the TLS memory for this object. The initialized
  81. value of _dl_tls_static_size is provided by dl-open.c to request some
  82. surplus that permits dynamic loading of modules with IE-model TLS. */
  83. GL(dl_tls_static_size) = roundup (memsz + GL(dl_tls_static_size),
  84. TLS_TCB_ALIGN);
  85. GL(dl_tls_static_used) = memsz;
  86. /* The alignment requirement for the static TLS block. */
  87. GL(dl_tls_static_align) = align;
  88. /* Number of elements in the static TLS block. */
  89. GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
  90. }
  91. void __libc_setup_tls (size_t tcbsize, size_t tcbalign);
  92. void
  93. __libc_setup_tls (size_t tcbsize, size_t tcbalign)
  94. {
  95. void *tlsblock;
  96. size_t memsz = 0;
  97. size_t filesz = 0;
  98. void *initimage = NULL;
  99. size_t align = 0;
  100. size_t max_align = tcbalign;
  101. size_t tcb_offset;
  102. ElfW(Phdr) *phdr;
  103. /* Look through the TLS segment if there is any. */
  104. if (_dl_phdr != NULL)
  105. for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr)
  106. if (phdr->p_type == PT_TLS)
  107. {
  108. /* Remember the values we need. */
  109. memsz = phdr->p_memsz;
  110. filesz = phdr->p_filesz;
  111. initimage = (void *) phdr->p_vaddr;
  112. align = phdr->p_align;
  113. if (phdr->p_align > max_align)
  114. max_align = phdr->p_align;
  115. break;
  116. }
  117. /* We have to set up the TCB block which also (possibly) contains
  118. 'errno'. Therefore we avoid 'malloc' which might touch 'errno'.
  119. Instead we use 'sbrk' which would only uses 'errno' if it fails.
  120. In this case we are right away out of memory and the user gets
  121. what she/he deserves.
  122. The initialized value of _dl_tls_static_size is provided by dl-open.c
  123. to request some surplus that permits dynamic loading of modules with
  124. IE-model TLS. */
  125. # if defined(TLS_TCB_AT_TP)
  126. tcb_offset = roundup (memsz + GL(dl_tls_static_size), tcbalign);
  127. tlsblock = sbrk (tcb_offset + tcbsize + max_align);
  128. # elif defined(TLS_DTV_AT_TP)
  129. tcb_offset = roundup (tcbsize, align ?: 1);
  130. tlsblock = sbrk (tcb_offset + memsz + max_align
  131. + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size));
  132. tlsblock += TLS_PRE_TCB_SIZE;
  133. # else
  134. /* In case a model with a different layout for the TCB and DTV
  135. is defined add another #elif here and in the following #ifs. */
  136. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  137. # endif
  138. /* Align the TLS block. */
  139. tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
  140. & ~(max_align - 1));
  141. /* Initialize the dtv. [0] is the length, [1] the generation counter. */
  142. static_dtv[0].counter = (sizeof (static_dtv) / sizeof (static_dtv[0])) - 2;
  143. // static_dtv[1].counter = 0; would be needed if not already done
  144. /* Initialize the TLS block. */
  145. # if defined(TLS_TCB_AT_TP)
  146. static_dtv[2].pointer.val = ((char *) tlsblock + tcb_offset
  147. - roundup (memsz, align ?: 1));
  148. static_map.l_tls_offset = roundup (memsz, align ?: 1);
  149. # elif defined(TLS_DTV_AT_TP)
  150. static_dtv[2].pointer.val = (char *) tlsblock + tcb_offset;
  151. static_map.l_tls_offset = tcb_offset;
  152. # else
  153. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  154. # endif
  155. static_dtv[2].pointer.is_static = true;
  156. /* sbrk gives us zero'd memory, so we don't need to clear the remainder. */
  157. memcpy (static_dtv[2].pointer.val, initimage, filesz);
  158. /* Install the pointer to the dtv. */
  159. /* Initialize the thread pointer. */
  160. # if defined(TLS_TCB_AT_TP)
  161. INSTALL_DTV ((char *) tlsblock + tcb_offset, static_dtv);
  162. const char *lossage = TLS_INIT_TP ((char *) tlsblock + tcb_offset, 0);
  163. # elif defined(TLS_DTV_AT_TP)
  164. INSTALL_DTV (tlsblock, static_dtv);
  165. const char *lossage = (char *)TLS_INIT_TP (tlsblock, 0);
  166. # else
  167. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  168. # endif
  169. if (__builtin_expect (lossage != NULL, 0))
  170. abort();
  171. /* We have to create a fake link map which normally would be created
  172. by the dynamic linker. It just has to have enough information to
  173. make the TLS routines happy. */
  174. static_map.l_tls_align = align;
  175. static_map.l_tls_blocksize = memsz;
  176. static_map.l_tls_initimage = initimage;
  177. static_map.l_tls_initimage_size = filesz;
  178. static_map.l_tls_modid = 1;
  179. init_slotinfo ();
  180. // static_slotinfo.si.slotinfo[1].gen = 0; already zero
  181. static_slotinfo.si.slotinfo[1].map = &static_map;
  182. memsz = roundup (memsz, align ?: 1);
  183. # if defined(TLS_TCB_AT_TP)
  184. memsz += tcbsize;
  185. # elif defined(TLS_DTV_AT_TP)
  186. memsz += tcb_offset;
  187. # endif
  188. init_static_tls (memsz, MAX (TLS_TCB_ALIGN, max_align));
  189. }
  190. /* This is called only when the data structure setup was skipped at startup,
  191. when there was no need for it then. Now we have dynamically loaded
  192. something needing TLS, or libpthread needs it. */
  193. int
  194. internal_function
  195. _dl_tls_setup (void)
  196. {
  197. init_slotinfo ();
  198. init_static_tls (
  199. # if defined(TLS_TCB_AT_TP)
  200. TLS_TCB_SIZE,
  201. # else
  202. 0,
  203. # endif
  204. TLS_TCB_ALIGN);
  205. return 0;
  206. }
  207. extern void __pthread_initialize_minimal(void) __attribute__((weak));
  208. /* This is the minimal initialization function used when libpthread is
  209. not used. */
  210. void
  211. __attribute__ ((weak))
  212. __pthread_initialize_minimal (void)
  213. {
  214. __libc_setup_tls (TLS_INIT_TCB_SIZE, TLS_INIT_TCB_ALIGN);
  215. }
  216. #endif