libc-tls.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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
  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. /* This is the minimal initialization function used when libpthread is
  208. not used. */
  209. void
  210. __attribute__ ((weak))
  211. __pthread_initialize_minimal (void)
  212. {
  213. __libc_setup_tls (TLS_INIT_TCB_SIZE, TLS_INIT_TCB_ALIGN);
  214. }
  215. #elif defined NONTLS_INIT_TP
  216. /* This is the minimal initialization function used when libpthread is
  217. not used. */
  218. void
  219. __attribute__ ((weak))
  220. __pthread_initialize_minimal (void)
  221. {
  222. NONTLS_INIT_TP;
  223. }
  224. #endif