libc-tls.c 8.3 KB

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