libc-tls.c 9.1 KB

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