libc-tls.c 9.1 KB

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