1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2004 Doug Rabson 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 /* 32 * Define stubs for TLS internals so that programs and libraries can 33 * link. These functions will be replaced by functional versions at 34 * runtime from ld-elf.so.1. 35 */ 36 37 #include <sys/cdefs.h> 38 #include <sys/param.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <elf.h> 42 #include <unistd.h> 43 44 #include "rtld.h" 45 #include "libc_private.h" 46 47 #define tls_assert(cond) ((cond) ? (void) 0 : \ 48 (tls_msg(#cond ": assert failed: " __FILE__ ":" \ 49 __XSTRING(__LINE__) "\n"), abort())) 50 #define tls_msg(s) write(STDOUT_FILENO, s, strlen(s)) 51 52 /* Provided by jemalloc to avoid bootstrapping issues. */ 53 void *__je_bootstrap_malloc(size_t size); 54 void *__je_bootstrap_calloc(size_t num, size_t size); 55 void __je_bootstrap_free(void *ptr); 56 57 __weak_reference(__libc_allocate_tls, _rtld_allocate_tls); 58 __weak_reference(__libc_free_tls, _rtld_free_tls); 59 60 #ifdef __i386__ 61 62 __weak_reference(___libc_tls_get_addr, ___tls_get_addr); 63 __attribute__((__regparm__(1))) void * ___libc_tls_get_addr(void *); 64 65 #endif 66 67 void * __libc_tls_get_addr(void *); 68 __weak_reference(__libc_tls_get_addr, __tls_get_addr); 69 70 void *_rtld_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign); 71 void _rtld_free_tls(void *tls, size_t tcbsize, size_t tcbalign); 72 void *__libc_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign); 73 void __libc_free_tls(void *tls, size_t tcbsize, size_t tcbalign); 74 75 #if defined(__amd64__) 76 #define TLS_TCB_ALIGN 16 77 #elif defined(__aarch64__) || defined(__arm__) || defined(__i386__) || \ 78 defined(__mips__) || defined(__powerpc__) || defined(__riscv) 79 #define TLS_TCB_ALIGN sizeof(void *) 80 #else 81 #error TLS_TCB_ALIGN undefined for target architecture 82 #endif 83 84 #ifndef PIC 85 86 static size_t libc_tls_static_space; 87 static size_t libc_tls_init_size; 88 static size_t libc_tls_init_align; 89 static void *libc_tls_init; 90 #endif 91 92 void * 93 __libc_tls_get_addr(void *vti) 94 { 95 Elf_Addr **dtvp, *dtv; 96 tls_index *ti; 97 98 dtvp = _get_tp(); 99 dtv = *dtvp; 100 ti = vti; 101 return ((char *)(dtv[ti->ti_module + 1] + ti->ti_offset) + 102 TLS_DTV_OFFSET); 103 } 104 105 #ifdef __i386__ 106 107 /* GNU ABI */ 108 109 __attribute__((__regparm__(1))) 110 void * 111 ___libc_tls_get_addr(void *vti) 112 { 113 return (__libc_tls_get_addr(vti)); 114 } 115 116 #endif 117 118 #ifndef PIC 119 120 static void * 121 libc_malloc_aligned(size_t size, size_t align) 122 { 123 void *mem, *res; 124 125 if (align < sizeof(void *)) 126 align = sizeof(void *); 127 128 mem = __je_bootstrap_malloc(size + sizeof(void *) + align - 1); 129 res = (void *)roundup2((uintptr_t)mem + sizeof(void *), align); 130 *(void **)((uintptr_t)res - sizeof(void *)) = mem; 131 return (res); 132 } 133 134 static void 135 libc_free_aligned(void *ptr) 136 { 137 void *mem; 138 uintptr_t x; 139 140 if (ptr == NULL) 141 return; 142 143 x = (uintptr_t)ptr; 144 x -= sizeof(void *); 145 mem = *(void **)x; 146 __je_bootstrap_free(mem); 147 } 148 149 #ifdef TLS_VARIANT_I 150 151 /* 152 * There are two versions of variant I of TLS 153 * 154 * - ARM and aarch64 uses original variant I as is described in [1] and [2], 155 * where TP points to start of TCB followed by aligned TLS segment. 156 * Both TCB and TLS must be aligned to alignment of TLS section. The TCB[0] 157 * points to DTV vector and DTV values are real addresses (without bias). 158 * Note: for Local Exec TLS Model, the offsets from TP (TCB in this case) to 159 * TLS variables are computed by linker, so we cannot overalign TLS section. 160 * 161 * - MIPS, PowerPC and RISC-V use modified version of variant I, 162 * described in [3] where TP points (with bias) to TLS and TCB immediately 163 * precedes TLS without any alignment gap[4]. Only TLS should be aligned. 164 * The TCB[0] points to DTV vector and DTV values are biased by constant 165 * value (0x8000) from real addresses[5]. 166 * 167 * [1] Ulrich Drepper: ELF Handling for Thread-Local Storage 168 * www.akkadia.org/drepper/tls.pdf 169 * 170 * [2] ARM IHI 0045E: Addenda to, and Errata in, the ABI for the ARM(r) 171 * Architecture 172 * infocenter.arm.com/help/topic/com.arm.doc.ihi0045e/IHI0045E_ABI_addenda.pdf 173 * 174 * [3] OpenPOWER: Power Architecture 64-Bit ELF V2 ABI Specification 175 * https://members.openpowerfoundation.org/document/dl/576 176 * 177 * [4] Its unclear if "without any alignment gap" is hard ABI requirement, 178 * but we must follow this rule due to suboptimal _set_tp() 179 * (aka <ARCH>_SET_TP) implementation. This function doesn't expect TP but 180 * TCB as argument. 181 * 182 * [5] I'm not able to validate "values are biased" assertions. 183 */ 184 185 /* 186 * Return pointer to allocated TLS block 187 */ 188 static void * 189 get_tls_block_ptr(void *tcb, size_t tcbsize) 190 { 191 size_t extra_size, post_size, pre_size, tls_block_size; 192 193 /* Compute fragments sizes. */ 194 extra_size = tcbsize - TLS_TCB_SIZE; 195 #if defined(__aarch64__) || defined(__arm__) 196 post_size = roundup2(TLS_TCB_SIZE, libc_tls_init_align) - TLS_TCB_SIZE; 197 #else 198 post_size = 0; 199 #endif 200 tls_block_size = tcbsize + post_size; 201 pre_size = roundup2(tls_block_size, libc_tls_init_align) - 202 tls_block_size; 203 204 return ((char *)tcb - pre_size - extra_size); 205 } 206 207 /* 208 * Free Static TLS using the Variant I method. The tcbsize 209 * and tcbalign parameters must be the same as those used to allocate 210 * the block. 211 */ 212 void 213 __libc_free_tls(void *tcb, size_t tcbsize, size_t tcbalign __unused) 214 { 215 Elf_Addr *dtv; 216 Elf_Addr **tls; 217 218 tls = (Elf_Addr **)tcb; 219 dtv = tls[0]; 220 __je_bootstrap_free(dtv); 221 libc_free_aligned(get_tls_block_ptr(tcb, tcbsize)); 222 } 223 224 /* 225 * Allocate Static TLS using the Variant I method. 226 * 227 * To handle all above requirements, we setup the following layout for 228 * TLS block: 229 * (whole memory block is aligned with MAX(TLS_TCB_ALIGN, tls_init_align)) 230 * 231 * +----------+--------------+--------------+-----------+------------------+ 232 * | pre gap | extended TCB | TCB | post gap | TLS segment | 233 * | pre_size | extra_size | TLS_TCB_SIZE | post_size | tls_static_space | 234 * +----------+--------------+--------------+-----------+------------------+ 235 * 236 * where: 237 * extra_size is tcbsize - TLS_TCB_SIZE 238 * post_size is used to adjust TCB to TLS alignment for first version of TLS 239 * layout and is always 0 for second version. 240 * pre_size is used to adjust TCB alignment for first version and to adjust 241 * TLS alignment for second version. 242 * 243 */ 244 void * 245 __libc_allocate_tls(void *oldtcb, size_t tcbsize, size_t tcbalign) 246 { 247 Elf_Addr *dtv, **tcb; 248 char *tls_block, *tls; 249 size_t extra_size, maxalign, post_size, pre_size, tls_block_size; 250 251 if (oldtcb != NULL && tcbsize == TLS_TCB_SIZE) 252 return (oldtcb); 253 254 tls_assert(tcbalign >= TLS_TCB_ALIGN); 255 maxalign = MAX(tcbalign, libc_tls_init_align); 256 257 /* Compute fragmets sizes. */ 258 extra_size = tcbsize - TLS_TCB_SIZE; 259 #if defined(__aarch64__) || defined(__arm__) 260 post_size = roundup2(TLS_TCB_SIZE, libc_tls_init_align) - TLS_TCB_SIZE; 261 #else 262 post_size = 0; 263 #endif 264 tls_block_size = tcbsize + post_size; 265 pre_size = roundup2(tls_block_size, libc_tls_init_align) - 266 tls_block_size; 267 tls_block_size += pre_size + libc_tls_static_space; 268 269 /* Allocate whole TLS block */ 270 tls_block = libc_malloc_aligned(tls_block_size, maxalign); 271 if (tls_block == NULL) { 272 tls_msg("__libc_allocate_tls: Out of memory.\n"); 273 abort(); 274 } 275 memset(tls_block, 0, tls_block_size); 276 tcb = (Elf_Addr **)(tls_block + pre_size + extra_size); 277 tls = (char *)tcb + TLS_TCB_SIZE + post_size; 278 279 if (oldtcb != NULL) { 280 memcpy(tls_block, get_tls_block_ptr(oldtcb, tcbsize), 281 tls_block_size); 282 libc_free_aligned(oldtcb); 283 284 /* Adjust the DTV. */ 285 dtv = tcb[0]; 286 dtv[2] = (Elf_Addr)(tls + TLS_DTV_OFFSET); 287 } else { 288 dtv = __je_bootstrap_malloc(3 * sizeof(Elf_Addr)); 289 if (dtv == NULL) { 290 tls_msg("__libc_allocate_tls: Out of memory.\n"); 291 abort(); 292 } 293 /* Build the DTV. */ 294 tcb[0] = dtv; 295 dtv[0] = 1; /* Generation. */ 296 dtv[1] = 1; /* Segments count. */ 297 dtv[2] = (Elf_Addr)(tls + TLS_DTV_OFFSET); 298 299 if (libc_tls_init_size > 0) 300 memcpy(tls, libc_tls_init, libc_tls_init_size); 301 } 302 303 return (tcb); 304 } 305 306 #endif 307 308 #ifdef TLS_VARIANT_II 309 310 #define TLS_TCB_SIZE (3 * sizeof(Elf_Addr)) 311 312 /* 313 * Free Static TLS using the Variant II method. 314 */ 315 void 316 __libc_free_tls(void *tcb, size_t tcbsize __unused, size_t tcbalign) 317 { 318 size_t size; 319 Elf_Addr* dtv; 320 Elf_Addr tlsstart, tlsend; 321 322 /* 323 * Figure out the size of the initial TLS block so that we can 324 * find stuff which ___tls_get_addr() allocated dynamically. 325 */ 326 tcbalign = MAX(tcbalign, libc_tls_init_align); 327 size = roundup2(libc_tls_static_space, tcbalign); 328 329 dtv = ((Elf_Addr**)tcb)[1]; 330 tlsend = (Elf_Addr) tcb; 331 tlsstart = tlsend - size; 332 libc_free_aligned((void*)tlsstart); 333 __je_bootstrap_free(dtv); 334 } 335 336 /* 337 * Allocate Static TLS using the Variant II method. 338 */ 339 void * 340 __libc_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign) 341 { 342 size_t size; 343 char *tls; 344 Elf_Addr *dtv; 345 Elf_Addr segbase, oldsegbase; 346 347 tcbalign = MAX(tcbalign, libc_tls_init_align); 348 size = roundup2(libc_tls_static_space, tcbalign); 349 350 if (tcbsize < 2 * sizeof(Elf_Addr)) 351 tcbsize = 2 * sizeof(Elf_Addr); 352 tls = libc_malloc_aligned(size + tcbsize, tcbalign); 353 if (tls == NULL) { 354 tls_msg("__libc_allocate_tls: Out of memory.\n"); 355 abort(); 356 } 357 memset(tls, 0, size + tcbsize); 358 dtv = __je_bootstrap_malloc(3 * sizeof(Elf_Addr)); 359 if (dtv == NULL) { 360 tls_msg("__libc_allocate_tls: Out of memory.\n"); 361 abort(); 362 } 363 364 segbase = (Elf_Addr)(tls + size); 365 ((Elf_Addr*)segbase)[0] = segbase; 366 ((Elf_Addr*)segbase)[1] = (Elf_Addr) dtv; 367 368 dtv[0] = 1; 369 dtv[1] = 1; 370 dtv[2] = segbase - libc_tls_static_space; 371 372 if (oldtls) { 373 /* 374 * Copy the static TLS block over whole. 375 */ 376 oldsegbase = (Elf_Addr) oldtls; 377 memcpy((void *)(segbase - libc_tls_static_space), 378 (const void *)(oldsegbase - libc_tls_static_space), 379 libc_tls_static_space); 380 381 /* 382 * We assume that this block was the one we created with 383 * allocate_initial_tls(). 384 */ 385 _rtld_free_tls(oldtls, 2*sizeof(Elf_Addr), sizeof(Elf_Addr)); 386 } else { 387 memcpy((void *)(segbase - libc_tls_static_space), 388 libc_tls_init, libc_tls_init_size); 389 memset((void *)(segbase - libc_tls_static_space + 390 libc_tls_init_size), 0, 391 libc_tls_static_space - libc_tls_init_size); 392 } 393 394 return (void*) segbase; 395 } 396 397 #endif /* TLS_VARIANT_II */ 398 399 #else 400 401 void * 402 __libc_allocate_tls(void *oldtls __unused, size_t tcbsize __unused, 403 size_t tcbalign __unused) 404 { 405 return (0); 406 } 407 408 void 409 __libc_free_tls(void *tcb __unused, size_t tcbsize __unused, 410 size_t tcbalign __unused) 411 { 412 } 413 414 #endif /* PIC */ 415 416 extern char **environ; 417 418 void 419 _init_tls(void) 420 { 421 #ifndef PIC 422 Elf_Addr *sp; 423 Elf_Auxinfo *aux, *auxp; 424 Elf_Phdr *phdr; 425 size_t phent, phnum; 426 int i; 427 void *tls; 428 429 sp = (Elf_Addr *) environ; 430 while (*sp++ != 0) 431 ; 432 aux = (Elf_Auxinfo *) sp; 433 phdr = NULL; 434 phent = phnum = 0; 435 for (auxp = aux; auxp->a_type != AT_NULL; auxp++) { 436 switch (auxp->a_type) { 437 case AT_PHDR: 438 phdr = auxp->a_un.a_ptr; 439 break; 440 441 case AT_PHENT: 442 phent = auxp->a_un.a_val; 443 break; 444 445 case AT_PHNUM: 446 phnum = auxp->a_un.a_val; 447 break; 448 } 449 } 450 if (phdr == NULL || phent != sizeof(Elf_Phdr) || phnum == 0) 451 return; 452 453 for (i = 0; (unsigned) i < phnum; i++) { 454 if (phdr[i].p_type == PT_TLS) { 455 libc_tls_static_space = roundup2(phdr[i].p_memsz, 456 phdr[i].p_align); 457 libc_tls_init_size = phdr[i].p_filesz; 458 libc_tls_init_align = phdr[i].p_align; 459 libc_tls_init = (void *)phdr[i].p_vaddr; 460 break; 461 } 462 } 463 tls = _rtld_allocate_tls(NULL, TLS_TCB_SIZE, TLS_TCB_ALIGN); 464 465 _set_tp(tls); 466 #endif 467 } 468