1 /* $NetBSD: tls.c,v 1.34 2026/07/22 15:23:33 riastradh Exp $ */ 2 /*- 3 * Copyright (c) 2011 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Joerg Sonnenberger. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __RCSID("$NetBSD: tls.c,v 1.34 2026/07/22 15:23:33 riastradh Exp $"); 33 34 /* 35 * Thread-local storage 36 * 37 * Reference: 38 * 39 * [ELFTLS] Ulrich Drepper, `ELF Handling For Thread-Local 40 * Storage', Version 0.21, 2023-08-22. 41 * https://akkadia.org/drepper/tls.pdf 42 * https://web.archive.org/web/20240718081934/https://akkadia.org/drepper/tls.pdf 43 */ 44 45 #include <sys/param.h> 46 #include <sys/ucontext.h> 47 #include <lwp.h> 48 #include <stdalign.h> 49 #include <stddef.h> 50 #include <string.h> 51 #include "debug.h" 52 #include "rtld.h" 53 54 #include <machine/lwp_private.h> 55 56 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 57 58 static struct tls_tcb *_rtld_tls_allocate_locked(void); 59 static void *_rtld_tls_module_allocate(struct tls_tcb *, size_t); 60 61 /* A macro to test correct alignment of a pointer. */ 62 #define ALIGNED_P(ptr, algnmt) ((algnmt) == 0 || ((uintptr_t)(ptr) & ((algnmt) - 1)) == 0) 63 64 /* 65 * DTV offset 66 * 67 * On some architectures (m68k, mips, or1k, powerpc, and riscv), 68 * the DTV offsets passed to __tls_get_addr have a bias relative 69 * to the start of the DTV, in order to maximize the range of TLS 70 * offsets that can be used by instruction encodings with signed 71 * displacements. 72 */ 73 #ifndef TLS_DTV_OFFSET 74 #define TLS_DTV_OFFSET 0 75 #endif 76 77 /* 78 * Alignment of the static data 79 * 80 * In Variant I, the thread pointer (TP) can be anchored in three ways 81 * depending on the architecture. It either points 82 * 83 * - directly to the TCB (e.g. Arm, and AArch64); or 84 * - at a specific "biased" offset within the data (e.g. PowerPC), or 85 * directly at the data (e.g. RISC-V). These architectures define 86 * __HAVE___LWP_SETTCB to handle the offset 87 * 88 * An area of memory with the correct alignment is allocated and the 89 * struct tcb placed as follows for each case above 90 * 91 * - at the start of the aligned memory with data starting at the first 92 * object's required alignment. 93 * - just below the second max alignment boundary so that data starts 94 * on the second max alignment boundary. 95 * 96 * The code is written such that obj->tlsoffset is always relative to 97 * the end of the struct tcb. Maybe this is suboptimal? 98 * 99 */ 100 101 #if defined(__HAVE_TLS_VARIANT_I) && !defined(__HAVE___LWP_SETTCB) 102 #define _RTLD_TLS_INITIAL_OFFSET sizeof(struct tls_tcb) 103 #endif 104 105 #ifndef _RTLD_TLS_INITIAL_OFFSET 106 #define _RTLD_TLS_INITIAL_OFFSET 0 107 #endif 108 109 static size_t _rtld_tls_static_space; /* Static TLS space allocated */ 110 static size_t _rtld_tls_static_offset = 111 _RTLD_TLS_INITIAL_OFFSET; /* Next offset for static TLS to use */ 112 static size_t _rtld_tls_static_max_align = 113 MAX(alignof(max_align_t), alignof(struct tls_tcb)); 114 115 size_t _rtld_tls_dtv_generation = 1; /* Bumped on each load of obj w/ TLS */ 116 size_t _rtld_tls_max_index = 1; /* Max index into up-to-date DTV */ 117 118 /* 119 * DTV -- Dynamic Thread Vector 120 * 121 * The DTV is a per-thread array that maps each module with 122 * thread-local storage to a pointer into part of the thread's TCB 123 * (thread control block), or dynamically loaded TLS blocks, 124 * reserved for that module's storage. 125 * 126 * The TCB itself, struct tls_tcb, has a pointer to the DTV at 127 * tcb->tcb_dtv. 128 * 129 * The layout is: 130 * 131 * +---------------+ 132 * | max index | -1 max index i for which dtv[i] is alloced 133 * +---------------+ 134 * | generation | 0 void **dtv points here 135 * +---------------+ 136 * | obj 1 tls ptr | 1 TLS pointer for obj w/ obj->tlsindex 1 137 * +---------------+ 138 * | obj 2 tls ptr | 2 TLS pointer for obj w/ obj->tlsindex 2 139 * +---------------+ 140 * . 141 * . 142 * . 143 * 144 * The values of obj->tlsindex start at 1; this way, 145 * dtv[obj->tlsindex] works, when dtv[0] is the generation. The 146 * TLS pointers go either into the static thread-local storage, 147 * for the initial objects (i.e., those loaded at startup), or 148 * into TLS blocks dynamically allocated for objects that 149 * dynamically loaded by dlopen. 150 * 151 * The generation field is a cache of the global generation number 152 * _rtld_tls_dtv_generation, which is bumped every time an object 153 * with TLS is loaded in _rtld_map_object, and cached by 154 * __tls_get_addr (via _rtld_tls_get_addr) when a newly loaded 155 * module lies outside the bounds of the current DTV. 156 * 157 * XXX Why do we keep max index and generation separately? They 158 * appear to be initialized the same, always incremented together, 159 * and always stored together. 160 * 161 * XXX Why is this not a struct? 162 * 163 * struct dtv { 164 * size_t dtv_gen; 165 * void *dtv_module[]; 166 * }; 167 */ 168 #define DTV_GENERATION(dtv) ((size_t)((dtv)[0])) 169 #define DTV_MAX_INDEX(dtv) ((size_t)((dtv)[-1])) 170 #define SET_DTV_GENERATION(dtv, val) (dtv)[0] = (void *)(size_t)(val) 171 #define SET_DTV_MAX_INDEX(dtv, val) (dtv)[-1] = (void *)(size_t)(val) 172 173 /* 174 * _rtld_tls_get_addr(tcb, idx, offset) 175 * 176 * Slow path for __tls_get_addr (see below), called to allocate 177 * TLS space if needed for the object obj with obj->tlsindex idx, 178 * at offset, which must be below obj->tlssize. 179 * 180 * This may allocate a DTV if the current one is too old, and it 181 * may allocate a dynamically loaded TLS block if there isn't one 182 * already allocated for it. 183 * 184 * XXX Why is the first argument passed as `void *tls' instead of 185 * just `struct tls_tcb *tcb'? 186 */ 187 void * 188 _rtld_tls_get_addr(void *tls, size_t idx, size_t offset) 189 { 190 struct tls_tcb *tcb = tls; 191 void **dtv, **new_dtv; 192 sigset_t mask; 193 194 _rtld_exclusive_enter(&mask); 195 196 dtv = tcb->tcb_dtv; 197 198 /* 199 * If the generation number has changed, we have to allocate a 200 * new DTV. 201 * 202 * XXX Do we really? Isn't it enough to check whether idx <= 203 * DTV_MAX_INDEX(dtv)? 204 */ 205 if (__predict_false(DTV_GENERATION(dtv) != _rtld_tls_dtv_generation)) { 206 size_t to_copy = DTV_MAX_INDEX(dtv); 207 208 /* 209 * "2 +" because the first element is the generation and 210 * the second one is the maximum index. 211 */ 212 new_dtv = xcalloc((2 + _rtld_tls_max_index) * sizeof(*dtv)); 213 ++new_dtv; /* advance past DTV_MAX_INDEX */ 214 if (to_copy > _rtld_tls_max_index) /* XXX How? */ 215 to_copy = _rtld_tls_max_index; 216 memcpy(new_dtv + 1, dtv + 1, to_copy * sizeof(*dtv)); 217 xfree(dtv - 1); /* retreat back to DTV_MAX_INDEX */ 218 dtv = tcb->tcb_dtv = new_dtv; 219 SET_DTV_MAX_INDEX(dtv, _rtld_tls_max_index); 220 SET_DTV_GENERATION(dtv, _rtld_tls_dtv_generation); 221 } 222 223 if (__predict_false(dtv[idx] == NULL)) 224 dtv[idx] = _rtld_tls_module_allocate(tcb, idx); 225 226 _rtld_exclusive_exit(&mask); 227 228 return (uint8_t *)dtv[idx] + offset; 229 } 230 231 /* 232 * _rtld_tls_initial_allocation() 233 * 234 * Allocate the TCB (thread control block) for the initial thread, 235 * once the static TLS space usage has been determined (plus some 236 * slop to allow certain special cases like Mesa to be dlopened). 237 * 238 * This must be done _after_ all initial objects (i.e., those 239 * loaded at startup, as opposed to objects dynamically loaded by 240 * dlopen) have had TLS offsets allocated if need be by 241 * _rtld_tls_offset_allocate, and have had relocations processed. 242 */ 243 void 244 _rtld_tls_initial_allocation(void) 245 { 246 struct tls_tcb *tcb; 247 248 _rtld_tls_static_space = _rtld_tls_static_offset + 249 RTLD_STATIC_TLS_RESERVATION; 250 251 #ifdef __HAVE_TLS_VARIANT_II 252 _rtld_tls_static_space = roundup2(_rtld_tls_static_space, 253 _rtld_tls_static_max_align); 254 assert(ALIGNED_P(_rtld_tls_static_space, _rtld_tls_static_max_align)); 255 #endif 256 257 dbg(("_rtld_tls_static_space %zu", _rtld_tls_static_space)); 258 259 tcb = _rtld_tls_allocate_locked(); 260 #ifdef __HAVE___LWP_SETTCB 261 __lwp_settcb(tcb); 262 #else 263 _lwp_setprivate(tcb); 264 #endif 265 } 266 267 /* 268 * _rtld_tls_allocate_locked() 269 * 270 * Internal subroutine to allocate a TCB (thread control block) 271 * for the current thread. 272 * 273 * This allocates a DTV and a TCB that points to it, including 274 * static space in the TCB for the TLS of the initial objects. 275 * TLS blocks for dynamically loaded objects are allocated lazily. 276 * 277 * Caller must either be single-threaded (at startup via 278 * _rtld_tls_initial_allocation) or hold the rtld exclusive lock 279 * (via _rtld_tls_allocate). 280 */ 281 static struct tls_tcb * 282 _rtld_tls_allocate_locked(void) 283 { 284 Obj_Entry *obj; 285 struct tls_tcb *tcb; 286 uint8_t *p, *q; 287 uint8_t *lo __debugused, *hi __debugused; /* bounds of TLS space */ 288 289 #ifdef __HAVE_TLS_VARIANT_II 290 assert(ALIGNED_P(_rtld_tls_static_space, _rtld_tls_static_max_align)); 291 #endif 292 293 p = xmalloc_aligned(_rtld_tls_static_space + sizeof(struct tls_tcb), 294 _rtld_tls_static_max_align, 0); 295 assert(ALIGNED_P(p, _rtld_tls_static_max_align)); 296 297 memset(p, 0, _rtld_tls_static_space + sizeof(struct tls_tcb)); 298 #ifdef __HAVE_TLS_VARIANT_I 299 #ifdef __HAVE___LWP_SETTCB 300 if (_rtld_tls_static_max_align > sizeof(struct tls_tcb)) 301 p += _rtld_tls_static_max_align - sizeof(struct tls_tcb); 302 #endif 303 assert(ALIGNED_P(p, alignof(struct tls_tcb))); 304 tcb = (struct tls_tcb *)p; 305 p += sizeof(struct tls_tcb); 306 #ifdef __HAVE___LWP_SETTCB 307 assert(ALIGNED_P(p, _rtld_tls_static_max_align)); 308 #else 309 assert((uintptr_t)p % _rtld_tls_static_max_align == 310 sizeof(struct tls_tcb) % _rtld_tls_static_max_align); 311 #endif 312 lo = p; 313 #else 314 lo = p; 315 p += _rtld_tls_static_space; 316 assert(ALIGNED_P(p, _rtld_tls_static_max_align)); 317 assert(ALIGNED_P(p, alignof(struct tls_tcb))); 318 tcb = (struct tls_tcb *)p; 319 tcb->tcb_self = tcb; 320 #endif 321 hi = lo + _rtld_tls_static_space; 322 dbg(("lwp %d tls tcb %p p %p", _lwp_self(), tcb, p)); 323 dbg(("tls range [%p,%p)", lo, hi)); 324 /* 325 * "2 +" because the first element is the generation and the second 326 * one is the maximum index. 327 */ 328 tcb->tcb_dtv = xcalloc(sizeof(*tcb->tcb_dtv) * (2 + _rtld_tls_max_index)); 329 ++tcb->tcb_dtv; /* advance past DTV_MAX_INDEX */ 330 SET_DTV_MAX_INDEX(tcb->tcb_dtv, _rtld_tls_max_index); 331 SET_DTV_GENERATION(tcb->tcb_dtv, _rtld_tls_dtv_generation); 332 333 for (obj = _rtld_objlist; obj != NULL; obj = obj->next) { 334 if (obj->tls_static) { 335 dbg(("%s: [lwp %d] tls offset=0x%zx size=0x%zx" 336 " initsize=0x%zx align=0x%zx", 337 obj->path, _lwp_self(), 338 obj->tlsoffset, obj->tlssize, 339 obj->tlsinitsize, obj->tlsalign)); 340 assert(obj->tlsinitsize <= obj->tlssize); 341 assert(obj->tlsoffset <= _rtld_tls_static_space); 342 assert(obj->tlssize <= _rtld_tls_static_space); 343 #ifdef __HAVE_TLS_VARIANT_I 344 assert(obj->tlsoffset <= _rtld_tls_static_space - 345 obj->tlssize); 346 q = p + obj->tlsoffset; 347 #else 348 assert(obj->tlssize <= obj->tlsoffset); 349 q = p - obj->tlsoffset; 350 #endif 351 dbg(("%s: [lwp %d] tls dtv %p-%p index %zu " 352 "offset 0x%zx alignment 0x%zx tlsinit %p%s", 353 obj->path, _lwp_self(), 354 q, q + obj->tlsinitsize, obj->tlsindex, 355 obj->tlsoffset, obj->tlsalign, obj->tlsinit, 356 (obj->tlssize == 0 || ALIGNED_P(q, obj->tlsalign)) 357 ? "" : " BAD ALIGNMENT")); 358 359 assert(lo <= q); 360 assert(q + obj->tlssize <= hi); 361 assert(obj->tlssize == 0 || obj->tlsalign != 0); 362 assert(obj->tlssize == 0 || 363 (obj->tlsalign & (obj->tlsalign - 1)) == 0); 364 assert(obj->tlssize == 0 || 365 ALIGNED_P(q, obj->tlsalign)); 366 367 if (obj->tlsinitsize) 368 memcpy(q, obj->tlsinit, obj->tlsinitsize); 369 tcb->tcb_dtv[obj->tlsindex] = q; 370 } 371 } 372 373 return tcb; 374 } 375 376 /* 377 * _rtld_tls_allocate() 378 * 379 * Allocate a TCB (thread control block) for the current thread. 380 * 381 * Called by pthread_create for non-initial threads. (The initial 382 * thread's TCB is allocated by _rtld_tls_initial_allocation.) 383 */ 384 struct tls_tcb * 385 _rtld_tls_allocate(void) 386 { 387 struct tls_tcb *tcb; 388 sigset_t mask; 389 390 _rtld_exclusive_enter(&mask); 391 tcb = _rtld_tls_allocate_locked(); 392 _rtld_exclusive_exit(&mask); 393 394 return tcb; 395 } 396 397 /* 398 * _rtld_tls_free(tcb) 399 * 400 * Free a TCB allocated with _rtld_tls_allocate. 401 * 402 * Frees any TLS blocks for dynamically loaded objects that tcb's 403 * DTV points to, and frees tcb's DTV, and frees tcb. 404 */ 405 void 406 _rtld_tls_free(struct tls_tcb *tcb) 407 { 408 size_t i, max_index; 409 uint8_t *p, *p_end; 410 sigset_t mask; 411 412 _rtld_exclusive_enter(&mask); 413 414 #ifdef __HAVE_TLS_VARIANT_I 415 p = (uint8_t *)tcb; 416 #else 417 p = (uint8_t *)tcb - _rtld_tls_static_space; 418 #endif 419 p_end = p + _rtld_tls_static_space; 420 421 max_index = DTV_MAX_INDEX(tcb->tcb_dtv); 422 for (i = 1; i <= max_index; ++i) { 423 if ((uint8_t *)tcb->tcb_dtv[i] < p || 424 (uint8_t *)tcb->tcb_dtv[i] >= p_end) 425 xfree(tcb->tcb_dtv[i]); 426 } 427 xfree(tcb->tcb_dtv - 1); /* retreat back to DTV_MAX_INDEX */ 428 xfree(p); 429 430 _rtld_exclusive_exit(&mask); 431 } 432 433 /* 434 * _rtld_tls_module_allocate(tcb, idx) 435 * 436 * Allocate thread-local storage in the thread with the given TCB 437 * (thread control block) for the object obj whose obj->tlsindex 438 * is idx. 439 * 440 * If obj has had space in static TLS reserved (obj->tls_static), 441 * return a pointer into that. Otherwise, allocate a TLS block, 442 * mark obj as having a TLS block allocated (obj->tls_dynamic), 443 * and return it. 444 * 445 * Called by _rtld_tls_get_addr to get the thread-local storage 446 * for an object the first time around. 447 */ 448 static void * 449 _rtld_tls_module_allocate(struct tls_tcb *tcb, size_t idx) 450 { 451 Obj_Entry *obj; 452 uint8_t *p; 453 454 for (obj = _rtld_objlist; obj != NULL; obj = obj->next) { 455 if (obj->tlsindex == idx) 456 break; 457 } 458 if (obj == NULL) { 459 _rtld_error("Module for TLS index %zu missing", idx); 460 _rtld_die(); 461 } 462 if (obj->tls_static) { 463 uint8_t *lo __debugused, *hi __debugused; 464 465 assert(obj->tlsoffset <= _rtld_tls_static_space); 466 assert(obj->tlssize <= _rtld_tls_static_space); 467 #ifdef __HAVE_TLS_VARIANT_I 468 assert(obj->tlsoffset <= _rtld_tls_static_space - 469 obj->tlssize); 470 p = (uint8_t *)tcb + obj->tlsoffset + sizeof(struct tls_tcb); 471 lo = (uint8_t *)tcb + sizeof(struct tls_tcb); 472 hi = lo + _rtld_tls_static_space; 473 #else 474 assert(obj->tlssize <= obj->tlsoffset); 475 p = (uint8_t *)tcb - obj->tlsoffset; 476 hi = (uint8_t *)tcb; 477 lo = hi - _rtld_tls_static_space; 478 #endif 479 assert(ALIGNED_P(p, obj->tlsalign)); 480 assert(lo <= p); 481 assert(p + obj->tlssize <= hi); 482 return p; 483 } 484 485 assert(obj->tlsinitsize <= obj->tlssize); 486 assert(obj->tlssize == 0 || obj->tlsalign != 0); 487 assert(obj->tlssize == 0 || 488 (obj->tlsalign & (obj->tlsalign - 1)) == 0); 489 490 p = xmalloc_aligned(obj->tlssize, obj->tlsalign, 0); 491 memcpy(p, obj->tlsinit, obj->tlsinitsize); 492 memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize); 493 494 obj->tls_dynamic = 1; 495 496 return p; 497 } 498 499 /* 500 * _rtld_tls_offset_allocate(obj) 501 * 502 * Allocate a static thread-local storage offset for obj. 503 * 504 * Called by _rtld at startup for all initial objects. Called 505 * also by MD relocation logic, which is allowed (for Mesa) to 506 * allocate an additional 64 bytes (RTLD_STATIC_TLS_RESERVATION) 507 * of static thread-local storage in dlopened objects. 508 */ 509 int 510 _rtld_tls_offset_allocate(Obj_Entry *obj) 511 { 512 size_t offset, next_offset; 513 514 /* 515 * If this object uses dynamic TLS only, the caller shouldn't 516 * be trying to allocate a static TLS offset. 517 */ 518 if (obj->tls_dynamic) 519 return -1; 520 521 /* 522 * If we have already allocated a static TLS offset, nothing 523 * more to do. 524 */ 525 if (obj->tls_static) 526 return 0; 527 528 /* 529 * If the TLS size is zero, not much to do here -- choose zero 530 * offset (always valid) and mark the object as having 531 * allocated a static TLS offset. 532 * 533 * XXX Is it a problem for multiple objects to have the same 534 * TLS offset? 535 */ 536 if (obj->tlssize == 0) { 537 obj->tlsoffset = 0; 538 obj->tls_static = 1; 539 return 0; 540 } 541 542 #ifdef __HAVE_TLS_VARIANT_I 543 offset = roundup2(_rtld_tls_static_offset, obj->tlsalign); 544 next_offset = offset + obj->tlssize; 545 #ifdef __HAVE___LWP_GETTCB_FAST 546 assert(ALIGNED_P(offset, obj->tlsalign)); 547 #else 548 offset -= sizeof(struct tls_tcb); 549 assert(obj->tlsalign == 0 || 550 offset % obj->tlsalign == 551 (-(ptrdiff_t)sizeof(struct tls_tcb)) % obj->tlsalign); 552 assert(ALIGNED_P(offset + sizeof(struct tls_tcb), obj->tlsalign)); 553 #endif 554 #else 555 offset = roundup2(_rtld_tls_static_offset + obj->tlssize, 556 obj->tlsalign); 557 next_offset = offset; 558 assert(ALIGNED_P(offset, obj->tlsalign)); 559 #endif 560 561 /* 562 * Check if the static allocation was already done. 563 * This happens if dynamically loaded modules want to use 564 * static TLS space. 565 * 566 * XXX Keep an actual free list and callbacks for initialisation. 567 */ 568 if (_rtld_tls_static_space) { 569 if (obj->tlsinitsize) { 570 _rtld_error("%s: Use of initialized " 571 "Thread Local Storage with model initial-exec " 572 "and dlopen is not supported", 573 obj->path); 574 return -1; 575 } 576 if (next_offset > _rtld_tls_static_space) { 577 _rtld_error("%s: No space available " 578 "for static Thread Local Storage", 579 obj->path); 580 return -1; 581 } 582 } 583 if (obj->tlsalign > _rtld_tls_static_max_align) { 584 _rtld_tls_static_max_align = obj->tlsalign; 585 } 586 assert(_rtld_tls_static_max_align != 0); 587 assert((_rtld_tls_static_max_align & (_rtld_tls_static_max_align - 1)) 588 == 0); 589 assert(ALIGNED_P(_rtld_tls_static_max_align, obj->tlsalign)); 590 assert(ALIGNED_P(_rtld_tls_static_max_align, alignof(max_align_t))); 591 assert(ALIGNED_P(_rtld_tls_static_max_align, alignof(struct tls_tcb))); 592 593 obj->tlsoffset = offset; 594 dbg(("%s: static tls offset 0x%zx size %zu align %zu (%zx/%zx)", 595 obj->path, obj->tlsoffset, obj->tlssize, obj->tlsalign, 596 _rtld_tls_static_offset, next_offset)); 597 _rtld_tls_static_offset = next_offset; 598 obj->tls_static = 1; 599 600 return 0; 601 } 602 603 /* 604 * _rtld_tls_offset_free(obj) 605 * 606 * Free a static thread-local storage offset for obj. 607 * 608 * Called by dlclose (via _rtld_unload_object -> _rtld_obj_free). 609 * 610 * Since static thread-local storage is normally not used by 611 * dlopened objects (with the exception of Mesa), this doesn't do 612 * anything to recycle the space right now. 613 */ 614 void 615 _rtld_tls_offset_free(Obj_Entry *obj) 616 { 617 618 /* 619 * XXX See above. 620 */ 621 obj->tls_static = 0; 622 return; 623 } 624 625 #if defined(__HAVE_COMMON___TLS_GET_ADDR) && defined(RTLD_LOADER) 626 /* 627 * __tls_get_addr(tlsindex) 628 * 629 * Symbol directly called by code generated by the compiler for 630 * references thread-local storage in the general-dynamic or 631 * local-dynamic TLS models (but not initial-exec or local-exec). 632 * 633 * The argument is a pointer to 634 * 635 * struct { 636 * unsigned long int ti_module; 637 * unsigned long int ti_offset; 638 * }; 639 * 640 * as in, e.g., [ELFTLS] Sec. 3.4.3. This coincides with the 641 * type size_t[2] on all architectures that use this common 642 * __tls_get_addr definition (XXX but why do we write it as 643 * size_t[2]?). 644 * 645 * ti_module, i.e., arg[0], is the obj->tlsindex assigned at 646 * load-time by _rtld_map_object, and ti_offset, i.e., arg[1], is 647 * assigned at link-time by ld(1), possibly adjusted by 648 * TLS_DTV_OFFSET. 649 * 650 * Some architectures -- specifically IA-64 -- use a different 651 * calling convention. Some architectures -- specifically i386 652 * -- also use another entry point ___tls_get_addr (that's three 653 * leading underscores) with a different calling convention. 654 */ 655 void * 656 __tls_get_addr(void *arg_) 657 { 658 size_t *arg = (size_t *)arg_; 659 void **dtv; 660 #ifdef __HAVE___LWP_GETTCB_FAST 661 struct tls_tcb * const tcb = __lwp_gettcb_fast(); 662 #else 663 struct tls_tcb * const tcb = __lwp_getprivate_fast(); 664 #endif 665 size_t idx = arg[0], offset = arg[1] + TLS_DTV_OFFSET; 666 667 dtv = tcb->tcb_dtv; 668 669 /* 670 * Fast path: access to an already allocated DTV entry. This 671 * checks the current limit and the entry without needing any 672 * locking. Entries are only freed on dlclose() and it is an 673 * application bug if code of the module is still running at 674 * that point. 675 */ 676 if (__predict_true(idx <= DTV_MAX_INDEX(dtv) && dtv[idx] != NULL)) 677 return (uint8_t *)dtv[idx] + offset; 678 679 return _rtld_tls_get_addr(tcb, idx, offset); 680 } 681 #endif 682 683 #endif /* __HAVE_TLS_VARIANT_I || __HAVE_TLS_VARIANT_II */ 684