| 1 | /* Initialization code for TLS in statically linked application. | 
|---|
| 2 | Copyright (C) 2002-2020 Free Software Foundation, Inc. | 
|---|
| 3 | This file is part of the GNU C Library. | 
|---|
| 4 |  | 
|---|
| 5 | The GNU C Library is free software; you can redistribute it and/or | 
|---|
| 6 | modify it under the terms of the GNU Lesser General Public | 
|---|
| 7 | License as published by the Free Software Foundation; either | 
|---|
| 8 | version 2.1 of the License, or (at your option) any later version. | 
|---|
| 9 |  | 
|---|
| 10 | The GNU C Library is distributed in the hope that it will be useful, | 
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 13 | Lesser General Public License for more details. | 
|---|
| 14 |  | 
|---|
| 15 | You should have received a copy of the GNU Lesser General Public | 
|---|
| 16 | License along with the GNU C Library; if not, see | 
|---|
| 17 | <https://www.gnu.org/licenses/>.  */ | 
|---|
| 18 |  | 
|---|
| 19 | #include <startup.h> | 
|---|
| 20 | #include <errno.h> | 
|---|
| 21 | #include <ldsodefs.h> | 
|---|
| 22 | #include <tls.h> | 
|---|
| 23 | #include <unistd.h> | 
|---|
| 24 | #include <stdio.h> | 
|---|
| 25 | #include <sys/param.h> | 
|---|
| 26 | #include <array_length.h> | 
|---|
| 27 |  | 
|---|
| 28 | #ifdef SHARED | 
|---|
| 29 | #error makefile bug, this file is for static only | 
|---|
| 30 | #endif | 
|---|
| 31 |  | 
|---|
| 32 | dtv_t _dl_static_dtv[2 + TLS_SLOTINFO_SURPLUS]; | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | static struct dtv_slotinfo_list static_slotinfo = | 
|---|
| 36 | { | 
|---|
| 37 | /* Allocate an array of 2 + TLS_SLOTINFO_SURPLUS elements.  */ | 
|---|
| 38 | .slotinfo =  { [array_length (_dl_static_dtv) - 1] = { 0 } }, | 
|---|
| 39 | }; | 
|---|
| 40 |  | 
|---|
| 41 | /* Highest dtv index currently needed.  */ | 
|---|
| 42 | size_t _dl_tls_max_dtv_idx; | 
|---|
| 43 | /* Flag signalling whether there are gaps in the module ID allocation.  */ | 
|---|
| 44 | bool _dl_tls_dtv_gaps; | 
|---|
| 45 | /* Information about the dtv slots.  */ | 
|---|
| 46 | struct dtv_slotinfo_list *_dl_tls_dtv_slotinfo_list; | 
|---|
| 47 | /* Number of modules in the static TLS block.  */ | 
|---|
| 48 | size_t _dl_tls_static_nelem; | 
|---|
| 49 | /* Size of the static TLS block.  */ | 
|---|
| 50 | size_t _dl_tls_static_size; | 
|---|
| 51 | /* Size actually allocated in the static TLS block.  */ | 
|---|
| 52 | size_t _dl_tls_static_used; | 
|---|
| 53 | /* Alignment requirement of the static TLS block.  */ | 
|---|
| 54 | size_t _dl_tls_static_align; | 
|---|
| 55 | /* Size of surplus space in the static TLS area for dynamically | 
|---|
| 56 | loaded modules with IE-model TLS or for TLSDESC optimization. | 
|---|
| 57 | See comments in elf/dl-tls.c where it is initialized.  */ | 
|---|
| 58 | size_t _dl_tls_static_surplus; | 
|---|
| 59 | /* Remaining amount of static TLS that may be used for optimizing | 
|---|
| 60 | dynamic TLS access (e.g. with TLSDESC).  */ | 
|---|
| 61 | size_t _dl_tls_static_optional; | 
|---|
| 62 |  | 
|---|
| 63 | /* Generation counter for the dtv.  */ | 
|---|
| 64 | size_t _dl_tls_generation; | 
|---|
| 65 |  | 
|---|
| 66 |  | 
|---|
| 67 | /* Additional definitions needed by TLS initialization.  */ | 
|---|
| 68 | #ifdef TLS_INIT_HELPER | 
|---|
| 69 | TLS_INIT_HELPER | 
|---|
| 70 | #endif | 
|---|
| 71 |  | 
|---|
| 72 | static void | 
|---|
| 73 | init_slotinfo (void) | 
|---|
| 74 | { | 
|---|
| 75 | /* Create the slotinfo list.  Note that the type of static_slotinfo | 
|---|
| 76 | has effectively a zero-length array, so we cannot use the size of | 
|---|
| 77 | static_slotinfo to determine the array length.  */ | 
|---|
| 78 | static_slotinfo.len = array_length (_dl_static_dtv); | 
|---|
| 79 | /* static_slotinfo.next = NULL; -- Already zero.  */ | 
|---|
| 80 |  | 
|---|
| 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; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | static void | 
|---|
| 88 | init_static_tls (size_t memsz, size_t align) | 
|---|
| 89 | { | 
|---|
| 90 | /* That is the size of the TLS memory for this object.  */ | 
|---|
| 91 | GL(dl_tls_static_size) = roundup (memsz + GLRO(dl_tls_static_surplus), | 
|---|
| 92 | TLS_TCB_ALIGN); | 
|---|
| 93 | #if TLS_TCB_AT_TP | 
|---|
| 94 | GL(dl_tls_static_size) += TLS_TCB_SIZE; | 
|---|
| 95 | #endif | 
|---|
| 96 | GL(dl_tls_static_used) = memsz; | 
|---|
| 97 | /* The alignment requirement for the static TLS block.  */ | 
|---|
| 98 | GL(dl_tls_static_align) = align; | 
|---|
| 99 | /* Number of elements in the static TLS block.  */ | 
|---|
| 100 | GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | void | 
|---|
| 104 | __libc_setup_tls (void) | 
|---|
| 105 | { | 
|---|
| 106 | void *tlsblock; | 
|---|
| 107 | size_t memsz = 0; | 
|---|
| 108 | size_t filesz = 0; | 
|---|
| 109 | void *initimage = NULL; | 
|---|
| 110 | size_t align = 0; | 
|---|
| 111 | size_t max_align = TCB_ALIGNMENT; | 
|---|
| 112 | size_t tcb_offset; | 
|---|
| 113 | const ElfW(Phdr) *phdr; | 
|---|
| 114 |  | 
|---|
| 115 | struct link_map *main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded; | 
|---|
| 116 |  | 
|---|
| 117 | /* Look through the TLS segment if there is any.  */ | 
|---|
| 118 | if (_dl_phdr != NULL) | 
|---|
| 119 | for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr) | 
|---|
| 120 | if (phdr->p_type == PT_TLS) | 
|---|
| 121 | { | 
|---|
| 122 | /* Remember the values we need.  */ | 
|---|
| 123 | memsz = phdr->p_memsz; | 
|---|
| 124 | filesz = phdr->p_filesz; | 
|---|
| 125 | initimage = (void *) phdr->p_vaddr + main_map->l_addr; | 
|---|
| 126 | align = phdr->p_align; | 
|---|
| 127 | if (phdr->p_align > max_align) | 
|---|
| 128 | max_align = phdr->p_align; | 
|---|
| 129 | break; | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | /* Calculate the size of the static TLS surplus, with 0 auditors.  */ | 
|---|
| 133 | _dl_tls_static_surplus_init (0); | 
|---|
| 134 |  | 
|---|
| 135 | /* We have to set up the TCB block which also (possibly) contains | 
|---|
| 136 | 'errno'.  Therefore we avoid 'malloc' which might touch 'errno'. | 
|---|
| 137 | Instead we use 'sbrk' which would only uses 'errno' if it fails. | 
|---|
| 138 | In this case we are right away out of memory and the user gets | 
|---|
| 139 | what she/he deserves.  */ | 
|---|
| 140 | #if TLS_TCB_AT_TP | 
|---|
| 141 | /* Align the TCB offset to the maximum alignment, as | 
|---|
| 142 | _dl_allocate_tls_storage (in elf/dl-tls.c) does using __libc_memalign | 
|---|
| 143 | and dl_tls_static_align.  */ | 
|---|
| 144 | tcb_offset = roundup (memsz + GLRO(dl_tls_static_surplus), max_align); | 
|---|
| 145 | tlsblock = __sbrk (tcb_offset + TLS_INIT_TCB_SIZE + max_align); | 
|---|
| 146 | #elif TLS_DTV_AT_TP | 
|---|
| 147 | tcb_offset = roundup (TLS_INIT_TCB_SIZE, align ?: 1); | 
|---|
| 148 | tlsblock = __sbrk (tcb_offset + memsz + max_align | 
|---|
| 149 | + TLS_PRE_TCB_SIZE + GLRO(dl_tls_static_surplus)); | 
|---|
| 150 | tlsblock += TLS_PRE_TCB_SIZE; | 
|---|
| 151 | #else | 
|---|
| 152 | /* In case a model with a different layout for the TCB and DTV | 
|---|
| 153 | is defined add another #elif here and in the following #ifs.  */ | 
|---|
| 154 | # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" | 
|---|
| 155 | #endif | 
|---|
| 156 |  | 
|---|
| 157 | /* Align the TLS block.  */ | 
|---|
| 158 | tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1) | 
|---|
| 159 | & ~(max_align - 1)); | 
|---|
| 160 |  | 
|---|
| 161 | /* Initialize the dtv.  [0] is the length, [1] the generation counter.  */ | 
|---|
| 162 | _dl_static_dtv[0].counter = (sizeof (_dl_static_dtv) / sizeof (_dl_static_dtv[0])) - 2; | 
|---|
| 163 | // _dl_static_dtv[1].counter = 0;		would be needed if not already done | 
|---|
| 164 |  | 
|---|
| 165 | /* Initialize the TLS block.  */ | 
|---|
| 166 | #if TLS_TCB_AT_TP | 
|---|
| 167 | _dl_static_dtv[2].pointer.val = ((char *) tlsblock + tcb_offset | 
|---|
| 168 | - roundup (memsz, align ?: 1)); | 
|---|
| 169 | main_map->l_tls_offset = roundup (memsz, align ?: 1); | 
|---|
| 170 | #elif TLS_DTV_AT_TP | 
|---|
| 171 | _dl_static_dtv[2].pointer.val = (char *) tlsblock + tcb_offset; | 
|---|
| 172 | main_map->l_tls_offset = tcb_offset; | 
|---|
| 173 | #else | 
|---|
| 174 | # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" | 
|---|
| 175 | #endif | 
|---|
| 176 | _dl_static_dtv[2].pointer.to_free = NULL; | 
|---|
| 177 | /* sbrk gives us zero'd memory, so we don't need to clear the remainder.  */ | 
|---|
| 178 | memcpy (_dl_static_dtv[2].pointer.val, initimage, filesz); | 
|---|
| 179 |  | 
|---|
| 180 | /* Install the pointer to the dtv.  */ | 
|---|
| 181 |  | 
|---|
| 182 | /* Initialize the thread pointer.  */ | 
|---|
| 183 | #if TLS_TCB_AT_TP | 
|---|
| 184 | INSTALL_DTV ((char *) tlsblock + tcb_offset, _dl_static_dtv); | 
|---|
| 185 |  | 
|---|
| 186 | const char *lossage = TLS_INIT_TP ((char *) tlsblock + tcb_offset); | 
|---|
| 187 | #elif TLS_DTV_AT_TP | 
|---|
| 188 | INSTALL_DTV (tlsblock, _dl_static_dtv); | 
|---|
| 189 | const char *lossage = TLS_INIT_TP (tlsblock); | 
|---|
| 190 | #else | 
|---|
| 191 | # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" | 
|---|
| 192 | #endif | 
|---|
| 193 | if (__builtin_expect (lossage != NULL, 0)) | 
|---|
| 194 | _startup_fatal (lossage); | 
|---|
| 195 |  | 
|---|
| 196 | /* Update the executable's link map with enough information to make | 
|---|
| 197 | the TLS routines happy.  */ | 
|---|
| 198 | main_map->l_tls_align = align; | 
|---|
| 199 | main_map->l_tls_blocksize = memsz; | 
|---|
| 200 | main_map->l_tls_initimage = initimage; | 
|---|
| 201 | main_map->l_tls_initimage_size = filesz; | 
|---|
| 202 | main_map->l_tls_modid = 1; | 
|---|
| 203 |  | 
|---|
| 204 | init_slotinfo (); | 
|---|
| 205 | /* static_slotinfo.slotinfo[1].gen = 0; -- Already zero.  */ | 
|---|
| 206 | static_slotinfo.slotinfo[1].map = main_map; | 
|---|
| 207 |  | 
|---|
| 208 | memsz = roundup (memsz, align ?: 1); | 
|---|
| 209 |  | 
|---|
| 210 | #if TLS_DTV_AT_TP | 
|---|
| 211 | memsz += tcb_offset; | 
|---|
| 212 | #endif | 
|---|
| 213 |  | 
|---|
| 214 | init_static_tls (memsz, MAX (TLS_TCB_ALIGN, max_align)); | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|