libc-tls.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #if !defined(__FDPIC__) && !defined(SHARED) && defined(STATIC_PIE)
  101. ElfW(Addr) _dl_load_base;
  102. #endif
  103. void __libc_setup_tls (size_t tcbsize, size_t tcbalign);
  104. void
  105. __libc_setup_tls (size_t tcbsize, size_t tcbalign)
  106. {
  107. void *tlsblock;
  108. size_t memsz = 0;
  109. size_t filesz = 0;
  110. void *initimage = NULL;
  111. size_t align = 0;
  112. size_t max_align = tcbalign;
  113. size_t tcb_offset;
  114. ElfW(Phdr) *phdr;
  115. /* Look through the TLS segment if there is any. */
  116. if (_dl_phdr != NULL)
  117. for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr)
  118. if (phdr->p_type == PT_TLS)
  119. {
  120. /* Remember the values we need. */
  121. memsz = phdr->p_memsz;
  122. filesz = phdr->p_filesz;
  123. #ifdef __FDPIC__
  124. initimage = (void *) &__tdata_start;
  125. #else
  126. initimage = (void *) phdr->p_vaddr;
  127. #if !defined(SHARED) && defined(STATIC_PIE)
  128. initimage += _dl_load_base;
  129. #endif
  130. #endif
  131. align = phdr->p_align;
  132. if (phdr->p_align > max_align)
  133. max_align = phdr->p_align;
  134. break;
  135. }
  136. /* We have to set up the TCB block which also (possibly) contains
  137. 'errno'. Therefore we avoid 'malloc' which might touch 'errno'.
  138. Instead we use 'sbrk' which would only uses 'errno' if it fails.
  139. In this case we are right away out of memory and the user gets
  140. what she/he deserves.
  141. The initialized value of _dl_tls_static_size is provided by dl-open.c
  142. to request some surplus that permits dynamic loading of modules with
  143. IE-model TLS. */
  144. /* Use mmap instead of sbrk since this commit disables sbrk area
  145. for FDPIC MMU-less platforms:
  146. fs/binfmt_elf_fdpic.c: fix brk area overlap with stack on NOMMU
  147. https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/fs/binfmt_elf_fdpic.c?id=4ac313111018cb44ecc250445de5ccb93026a980
  148. Loading static PIE ELFs on noMMU is possible since the linux kernel commit
  149. 1bde925d2354 ("fs/binfmt_elf_fdpic.c: provide NOMMU loader for regular ELF binaries")
  150. and it is subject to the same brk restriction.
  151. */
  152. # if defined(TLS_TCB_AT_TP)
  153. tcb_offset = roundup (memsz + GL(dl_tls_static_size), tcbalign);
  154. # if defined(__FDPIC__) || (!defined(__ARCH_USE_MMU__) && defined(STATIC_PIE))
  155. tlsblock = mmap (NULL, tcb_offset + tcbsize + max_align,
  156. PROT_READ|PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  157. # else
  158. tlsblock = sbrk (tcb_offset + tcbsize + max_align);
  159. # endif
  160. # elif defined(TLS_DTV_AT_TP)
  161. tcb_offset = roundup (tcbsize, align ?: 1);
  162. # if defined(__FDPIC__) || (!defined(__ARCH_USE_MMU__) && defined(STATIC_PIE))
  163. tlsblock = mmap (NULL, tcb_offset + memsz + max_align + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size),
  164. PROT_READ|PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  165. # else
  166. tlsblock = sbrk (tcb_offset + memsz + max_align
  167. + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size));
  168. # endif
  169. memset(tlsblock, '\0', tcb_offset + memsz + max_align + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size));
  170. tlsblock += TLS_PRE_TCB_SIZE;
  171. # else
  172. /* In case a model with a different layout for the TCB and DTV
  173. is defined add another #elif here and in the following #ifs. */
  174. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  175. # endif
  176. /* Align the TLS block. */
  177. tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
  178. & ~(max_align - 1));
  179. /* Initialize the dtv. [0] is the length, [1] the generation counter. */
  180. static_dtv[0].counter = (sizeof (static_dtv) / sizeof (static_dtv[0])) - 2;
  181. // static_dtv[1].counter = 0; would be needed if not already done
  182. /* Initialize the TLS block. */
  183. # if defined(TLS_TCB_AT_TP)
  184. static_dtv[2].pointer.val = ((char *) tlsblock + tcb_offset
  185. - roundup (memsz, align ?: 1));
  186. static_map.l_tls_offset = roundup (memsz, align ?: 1);
  187. # elif defined(TLS_DTV_AT_TP)
  188. static_dtv[2].pointer.val = (char *) tlsblock + tcb_offset;
  189. static_map.l_tls_offset = tcb_offset;
  190. # else
  191. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  192. # endif
  193. static_dtv[2].pointer.is_static = true;
  194. /* sbrk gives us zero'd memory, so we don't need to clear the remainder. */
  195. memcpy (static_dtv[2].pointer.val, initimage, filesz);
  196. /* Install the pointer to the dtv. */
  197. /* Initialize the thread pointer. */
  198. # if defined(TLS_TCB_AT_TP)
  199. INSTALL_DTV ((char *) tlsblock + tcb_offset, static_dtv);
  200. const char *lossage = TLS_INIT_TP ((char *) tlsblock + tcb_offset, 0);
  201. # elif defined(TLS_DTV_AT_TP)
  202. INSTALL_DTV (tlsblock, static_dtv);
  203. const char *lossage = (char *)TLS_INIT_TP (tlsblock, 0);
  204. # else
  205. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  206. # endif
  207. if (__builtin_expect (lossage != NULL, 0))
  208. abort();
  209. /* We have to create a fake link map which normally would be created
  210. by the dynamic linker. It just has to have enough information to
  211. make the TLS routines happy. */
  212. static_map.l_tls_align = align;
  213. static_map.l_tls_blocksize = memsz;
  214. static_map.l_tls_initimage = initimage;
  215. static_map.l_tls_initimage_size = filesz;
  216. static_map.l_tls_modid = 1;
  217. init_slotinfo ();
  218. // static_slotinfo.si.slotinfo[1].gen = 0; already zero
  219. static_slotinfo.si.slotinfo[1].map = &static_map;
  220. memsz = roundup (memsz, align ?: 1);
  221. # if defined(TLS_TCB_AT_TP)
  222. memsz += tcbsize;
  223. # elif defined(TLS_DTV_AT_TP)
  224. memsz += tcb_offset;
  225. # endif
  226. init_static_tls (memsz, MAX (TLS_TCB_ALIGN, max_align));
  227. }
  228. /* This is called only when the data structure setup was skipped at startup,
  229. when there was no need for it then. Now we have dynamically loaded
  230. something needing TLS, or libpthread needs it. */
  231. int
  232. internal_function
  233. _dl_tls_setup (void)
  234. {
  235. init_slotinfo ();
  236. init_static_tls (
  237. # if defined(TLS_TCB_AT_TP)
  238. TLS_TCB_SIZE,
  239. # else
  240. 0,
  241. # endif
  242. TLS_TCB_ALIGN);
  243. return 0;
  244. }
  245. extern void __pthread_initialize_minimal(void) __attribute__((weak));
  246. /* This is the minimal initialization function used when libpthread is
  247. not used. */
  248. void
  249. __attribute__ ((weak))
  250. __pthread_initialize_minimal (void)
  251. {
  252. __libc_setup_tls (TLS_INIT_TCB_SIZE, TLS_INIT_TCB_ALIGN);
  253. }
  254. #endif