lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Initialization code for TLS in statically linked application. |
| 2 | Copyright (C) 2002-2015 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 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <ldsodefs.h> |
| 21 | #include <tls.h> |
| 22 | #include <unistd.h> |
| 23 | #include <stdio.h> |
| 24 | #include <sys/param.h> |
| 25 | |
| 26 | |
| 27 | #ifdef SHARED |
| 28 | #error makefile bug, this file is for static only |
| 29 | #endif |
| 30 | |
| 31 | dtv_t _dl_static_dtv[2 + TLS_SLOTINFO_SURPLUS]; |
| 32 | |
| 33 | |
| 34 | static struct |
| 35 | { |
| 36 | struct dtv_slotinfo_list si; |
| 37 | /* The dtv_slotinfo_list data structure does not include the actual |
| 38 | information since it is defined as an array of size zero. We define |
| 39 | here the necessary entries. Note that it is not important whether |
| 40 | there is padding or not since we will always access the information |
| 41 | through the 'si' element. */ |
| 42 | struct dtv_slotinfo info[2 + TLS_SLOTINFO_SURPLUS]; |
| 43 | } static_slotinfo; |
| 44 | |
| 45 | |
| 46 | /* Highest dtv index currently needed. */ |
| 47 | size_t _dl_tls_max_dtv_idx; |
| 48 | /* Flag signalling whether there are gaps in the module ID allocation. */ |
| 49 | bool _dl_tls_dtv_gaps; |
| 50 | /* Information about the dtv slots. */ |
| 51 | struct dtv_slotinfo_list *_dl_tls_dtv_slotinfo_list; |
| 52 | /* Number of modules in the static TLS block. */ |
| 53 | size_t _dl_tls_static_nelem; |
| 54 | /* Size of the static TLS block. Giving this initialized value |
| 55 | preallocates some surplus bytes in the static TLS area. */ |
| 56 | size_t _dl_tls_static_size = 2048; |
| 57 | /* Size actually allocated in the static TLS block. */ |
| 58 | size_t _dl_tls_static_used; |
| 59 | /* Alignment requirement of the static TLS block. */ |
| 60 | size_t _dl_tls_static_align; |
| 61 | |
| 62 | /* Generation counter for the dtv. */ |
| 63 | size_t _dl_tls_generation; |
| 64 | |
| 65 | |
| 66 | /* Additional definitions needed by TLS initialization. */ |
| 67 | #ifdef TLS_INIT_HELPER |
| 68 | TLS_INIT_HELPER |
| 69 | #endif |
| 70 | |
| 71 | static void |
| 72 | init_slotinfo (void) |
| 73 | { |
| 74 | /* Create the slotinfo list. */ |
| 75 | static_slotinfo.si.len = (((char *) (&static_slotinfo + 1) |
| 76 | - (char *) &static_slotinfo.si.slotinfo[0]) |
| 77 | / sizeof static_slotinfo.si.slotinfo[0]); |
| 78 | // static_slotinfo.si.next = NULL; already zero |
| 79 | |
| 80 | /* The slotinfo list. Will be extended by the code doing dynamic |
| 81 | linking. */ |
| 82 | GL(dl_tls_max_dtv_idx) = 1; |
| 83 | GL(dl_tls_dtv_slotinfo_list) = &static_slotinfo.si; |
| 84 | } |
| 85 | |
| 86 | static 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 | #if TLS_TCB_AT_TP |
| 95 | GL(dl_tls_static_size) += TLS_TCB_SIZE; |
| 96 | #endif |
| 97 | GL(dl_tls_static_used) = memsz; |
| 98 | /* The alignment requirement for the static TLS block. */ |
| 99 | GL(dl_tls_static_align) = align; |
| 100 | /* Number of elements in the static TLS block. */ |
| 101 | GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx); |
| 102 | } |
| 103 | |
| 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 | const ElfW(Phdr) *phdr; |
| 115 | |
| 116 | /* Look through the TLS segment if there is any. */ |
| 117 | if (_dl_phdr != NULL) |
| 118 | for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr) |
| 119 | if (phdr->p_type == PT_TLS) |
| 120 | { |
| 121 | /* Remember the values we need. */ |
| 122 | memsz = phdr->p_memsz; |
| 123 | filesz = phdr->p_filesz; |
| 124 | initimage = (void *) phdr->p_vaddr; |
| 125 | align = phdr->p_align; |
| 126 | if (phdr->p_align > max_align) |
| 127 | max_align = phdr->p_align; |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | /* We have to set up the TCB block which also (possibly) contains |
| 132 | 'errno'. Therefore we avoid 'malloc' which might touch 'errno'. |
| 133 | Instead we use 'sbrk' which would only uses 'errno' if it fails. |
| 134 | In this case we are right away out of memory and the user gets |
| 135 | what she/he deserves. |
| 136 | |
| 137 | The initialized value of _dl_tls_static_size is provided by dl-open.c |
| 138 | to request some surplus that permits dynamic loading of modules with |
| 139 | IE-model TLS. */ |
| 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 + GL(dl_tls_static_size), max_align); |
| 145 | tlsblock = __sbrk (tcb_offset + tcbsize + max_align); |
| 146 | #elif TLS_DTV_AT_TP |
| 147 | tcb_offset = roundup (tcbsize, align ?: 1); |
| 148 | tlsblock = __sbrk (tcb_offset + memsz + max_align |
| 149 | + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size)); |
| 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 | struct link_map *main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded; |
| 166 | |
| 167 | /* Initialize the TLS block. */ |
| 168 | #if TLS_TCB_AT_TP |
| 169 | _dl_static_dtv[2].pointer.val = ((char *) tlsblock + tcb_offset |
| 170 | - roundup (memsz, align ?: 1)); |
| 171 | main_map->l_tls_offset = roundup (memsz, align ?: 1); |
| 172 | #elif TLS_DTV_AT_TP |
| 173 | _dl_static_dtv[2].pointer.val = (char *) tlsblock + tcb_offset; |
| 174 | main_map->l_tls_offset = tcb_offset; |
| 175 | #else |
| 176 | # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" |
| 177 | #endif |
| 178 | _dl_static_dtv[2].pointer.is_static = true; |
| 179 | /* sbrk gives us zero'd memory, so we don't need to clear the remainder. */ |
| 180 | memcpy (_dl_static_dtv[2].pointer.val, initimage, filesz); |
| 181 | |
| 182 | /* Install the pointer to the dtv. */ |
| 183 | |
| 184 | /* Initialize the thread pointer. */ |
| 185 | #if TLS_TCB_AT_TP |
| 186 | INSTALL_DTV ((char *) tlsblock + tcb_offset, _dl_static_dtv); |
| 187 | |
| 188 | const char *lossage = TLS_INIT_TP ((char *) tlsblock + tcb_offset); |
| 189 | #elif TLS_DTV_AT_TP |
| 190 | INSTALL_DTV (tlsblock, _dl_static_dtv); |
| 191 | const char *lossage = TLS_INIT_TP (tlsblock); |
| 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 | __libc_fatal (lossage); |
| 197 | |
| 198 | /* Update the executable's link map with enough information to make |
| 199 | the TLS routines happy. */ |
| 200 | main_map->l_tls_align = align; |
| 201 | main_map->l_tls_blocksize = memsz; |
| 202 | main_map->l_tls_initimage = initimage; |
| 203 | main_map->l_tls_initimage_size = filesz; |
| 204 | main_map->l_tls_modid = 1; |
| 205 | |
| 206 | init_slotinfo (); |
| 207 | // static_slotinfo.si.slotinfo[1].gen = 0; already zero |
| 208 | static_slotinfo.si.slotinfo[1].map = main_map; |
| 209 | |
| 210 | memsz = roundup (memsz, align ?: 1); |
| 211 | |
| 212 | #if TLS_DTV_AT_TP |
| 213 | memsz += tcb_offset; |
| 214 | #endif |
| 215 | |
| 216 | init_static_tls (memsz, MAX (TLS_TCB_ALIGN, max_align)); |
| 217 | } |
| 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 TLS_TCB_AT_TP |
| 229 | TLS_TCB_SIZE, |
| 230 | #else |
| 231 | 0, |
| 232 | #endif |
| 233 | TLS_TCB_ALIGN); |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /* This is the minimal initialization function used when libpthread is |
| 239 | not used. */ |
| 240 | void |
| 241 | __attribute__ ((weak)) |
| 242 | __pthread_initialize_minimal (void) |
| 243 | { |
| 244 | __libc_setup_tls (TLS_INIT_TCB_SIZE, TLS_INIT_TCB_ALIGN); |
| 245 | } |