blob: 3a7b38135b092e50bee184c4533efecf69115c78 [file] [log] [blame]
Ryan Prichard45d13492019-01-03 02:51:30 -08001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#pragma once
30
Ryan Prichard48097552019-01-06 18:24:10 -080031#include <link.h>
Ryan Pricharde5e69e02019-01-01 18:53:48 -080032#include <pthread.h>
33#include <stdatomic.h>
Ryan Prichard45d13492019-01-03 02:51:30 -080034#include <stdint.h>
Ryan Prichard48097552019-01-06 18:24:10 -080035#include <sys/cdefs.h>
36
Ryan Prichard16455b52019-01-18 01:00:59 -080037__LIBC_HIDDEN__ extern _Atomic(size_t) __libc_tls_generation_copy;
38
Ryan Prichard43963922024-03-14 16:51:27 -070039struct TlsAlign {
40 size_t value = 1;
41 size_t skew = 0; // p_vaddr % p_align
42
43 template <typename T>
44 static constexpr TlsAlign of_type() {
45 return TlsAlign{.value = alignof(T)};
46 }
47};
48
49struct TlsAlignedSize {
Ryan Prichard48097552019-01-06 18:24:10 -080050 size_t size = 0;
Ryan Prichard43963922024-03-14 16:51:27 -070051 TlsAlign align;
52
53 template <typename T>
54 static constexpr TlsAlignedSize of_type() {
55 return TlsAlignedSize{.size = sizeof(T), .align = TlsAlign::of_type<T>()};
56 }
57};
58
59struct TlsSegment {
60 TlsAlignedSize aligned_size;
Ryan Prichard48097552019-01-06 18:24:10 -080061 const void* init_ptr = ""; // Field is non-null even when init_size is 0.
62 size_t init_size = 0;
63};
64
65__LIBC_HIDDEN__ bool __bionic_get_tls_segment(const ElfW(Phdr)* phdr_table, size_t phdr_count,
Ryan Prichard19883502019-01-16 23:13:38 -080066 ElfW(Addr) load_bias, TlsSegment* out);
67
Ryan Prichard43963922024-03-14 16:51:27 -070068__LIBC_HIDDEN__ bool __bionic_check_tls_align(size_t align);
Ryan Prichard45d13492019-01-03 02:51:30 -080069
70struct StaticTlsLayout {
71 constexpr StaticTlsLayout() {}
72
Ryan Prichard45d13492019-01-03 02:51:30 -080073public:
74 size_t offset_bionic_tcb() const { return offset_bionic_tcb_; }
75 size_t offset_bionic_tls() const { return offset_bionic_tls_; }
Ryan Prichardfb8730d2019-01-15 00:11:37 -080076 size_t offset_thread_pointer() const;
Ryan Prichard43963922024-03-14 16:51:27 -070077 size_t offset_exe() const { return offset_exe_; }
Ryan Prichard45d13492019-01-03 02:51:30 -080078
Ryan Prichard43963922024-03-14 16:51:27 -070079 size_t size() const { return cursor_; }
Ryan Prichard45d13492019-01-03 02:51:30 -080080
Ryan Prichard977e47d2019-01-14 21:52:14 -080081 size_t reserve_exe_segment_and_tcb(const TlsSegment* exe_segment, const char* progname);
Ryan Prichard43963922024-03-14 16:51:27 -070082 size_t reserve_bionic_tls();
83 size_t reserve_solib_segment(const TlsSegment& segment) { return reserve(segment.aligned_size); }
Ryan Prichard45d13492019-01-03 02:51:30 -080084 void finish_layout();
85
Ryan Prichard43963922024-03-14 16:51:27 -070086#if !defined(STATIC_TLS_LAYOUT_TEST)
87 private:
88#endif
89 size_t cursor_ = 0;
90 size_t align_ = 1;
91
92 // Offsets to various Bionic TLS structs from the beginning of static TLS.
93 size_t offset_bionic_tcb_ = SIZE_MAX;
94 size_t offset_bionic_tls_ = SIZE_MAX;
95
96 size_t offset_exe_ = SIZE_MAX;
97
98 struct TpAllocations {
99 size_t before;
100 size_t tp;
101 size_t after;
102 };
103
104 size_t align_cursor(TlsAlign align);
105 size_t align_cursor_unskewed(size_t align);
106 size_t reserve(TlsAlignedSize aligned_size);
107 TpAllocations reserve_tp_pair(TlsAlignedSize before, TlsAlignedSize after);
Ryan Prichard45d13492019-01-03 02:51:30 -0800108
109 template <typename T> size_t reserve_type() {
Ryan Prichard43963922024-03-14 16:51:27 -0700110 return reserve(TlsAlignedSize::of_type<T>());
Ryan Prichard45d13492019-01-03 02:51:30 -0800111 }
Ryan Prichard45d13492019-01-03 02:51:30 -0800112};
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800113
Ryan Prichard16455b52019-01-18 01:00:59 -0800114static constexpr size_t kTlsGenerationNone = 0;
115static constexpr size_t kTlsGenerationFirst = 1;
116
117// The first ELF TLS module has ID 1. Zero is reserved for the first word of
118// the DTV, a generation count. Unresolved weak symbols also use module ID 0.
119static constexpr size_t kTlsUninitializedModuleId = 0;
120
121static inline size_t __tls_module_id_to_idx(size_t id) { return id - 1; }
122static inline size_t __tls_module_idx_to_id(size_t idx) { return idx + 1; }
123
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800124// A descriptor for a single ELF TLS module.
125struct TlsModule {
126 TlsSegment segment;
127
128 // Offset into the static TLS block or SIZE_MAX for a dynamic module.
129 size_t static_offset = SIZE_MAX;
130
131 // The generation in which this module was loaded. Dynamic TLS lookups use
132 // this field to detect when a module has been unloaded.
Ryan Prichard16455b52019-01-18 01:00:59 -0800133 size_t first_generation = kTlsGenerationNone;
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800134
135 // Used by the dynamic linker to track the associated soinfo* object.
136 void* soinfo_ptr = nullptr;
137};
138
Vy Nguyend5007512020-07-14 17:37:04 -0400139// Signature of the callbacks that will be called after DTLS creation and
140// before DTLS destruction.
141typedef void (*dtls_listener_t)(void* dynamic_tls_begin, void* dynamic_tls_end);
142
143// Signature of the thread-exit callbacks.
144typedef void (*thread_exit_cb_t)(void);
145
146struct CallbackHolder {
147 thread_exit_cb_t cb;
148 CallbackHolder* prev;
149};
150
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800151// Table of the ELF TLS modules. Either the dynamic linker or the static
152// initialization code prepares this table, and it's then used during thread
153// creation and for dynamic TLS lookups.
154struct TlsModules {
155 constexpr TlsModules() {}
156
Ryan Prichard16455b52019-01-18 01:00:59 -0800157 // A pointer to the TLS generation counter in libc.so. The counter is
158 // incremented each time an solib is loaded or unloaded.
159 _Atomic(size_t) generation = kTlsGenerationFirst;
160 _Atomic(size_t) *generation_libc_so = nullptr;
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800161
162 // Access to the TlsModule[] table requires taking this lock.
163 pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
164
165 // Pointer to a block of TlsModule objects. The first module has ID 1 and
166 // is stored at index 0 in this table.
167 size_t module_count = 0;
Vy Nguyend5007512020-07-14 17:37:04 -0400168 size_t static_module_count = 0;
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800169 TlsModule* module_table = nullptr;
Vy Nguyend5007512020-07-14 17:37:04 -0400170
171 // Callback to be invoked after a dynamic TLS allocation.
172 dtls_listener_t on_creation_cb = nullptr;
173
174 // Callback to be invoked before a dynamic TLS deallocation.
175 dtls_listener_t on_destruction_cb = nullptr;
176
177 // The first thread-exit callback; inlined to avoid allocation.
178 thread_exit_cb_t first_thread_exit_callback = nullptr;
179
180 // The additional callbacks, if any.
181 CallbackHolder* thread_exit_callback_tail_node = nullptr;
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800182};
Ryan Prichard361c1b42019-01-15 13:45:27 -0800183
184void __init_static_tls(void* static_tls);
Ryan Prichard16455b52019-01-18 01:00:59 -0800185
186// Dynamic Thread Vector. Each thread has a different DTV. For each module
187// (executable or solib), the DTV has a pointer to that module's TLS memory. The
188// DTV is initially empty and is allocated on-demand. It grows as more modules
189// are dlopen'ed. See https://www.akkadia.org/drepper/tls.pdf.
190//
191// The layout of the DTV is specified in various documents, but it is not part
192// of Bionic's public ABI. A compiler can't generate code to access it directly,
193// because it can't access libc's global generation counter.
194struct TlsDtv {
195 // Number of elements in this object's modules field.
196 size_t count;
197
198 // A pointer to an older TlsDtv object that should be freed when the thread
199 // exits. The objects aren't immediately freed because a DTV could be
200 // reallocated by a signal handler that interrupted __tls_get_addr's fast
201 // path.
202 TlsDtv* next;
203
204 // The DTV slot points at this field, which allows omitting an add instruction
205 // on the fast path for a TLS lookup. The arm64 tlsdesc_resolver.S depends on
206 // the layout of fields past this point.
207 size_t generation;
208 void* modules[];
209};
210
211struct TlsIndex {
212 size_t module_id;
213 size_t offset;
214};
215
216#if defined(__i386__)
217#define TLS_GET_ADDR_CCONV __attribute__((regparm(1)))
218#define TLS_GET_ADDR ___tls_get_addr
219#else
220#define TLS_GET_ADDR_CCONV
221#define TLS_GET_ADDR __tls_get_addr
222#endif
223
224extern "C" void* TLS_GET_ADDR(const TlsIndex* ti) TLS_GET_ADDR_CCONV;
225
226struct bionic_tcb;
227void __free_dynamic_tls(bionic_tcb* tcb);
Vy Nguyend5007512020-07-14 17:37:04 -0400228void __notify_thread_exit_callbacks();
Elliott Hughes43462702022-10-10 19:21:44 +0000229
230#if defined(__riscv)
231// TLS_DTV_OFFSET is a constant used in relocation fields, defined in RISC-V ELF Specification[1]
232// The front of the TCB contains a pointer to the DTV, and each pointer in DTV
233// points to 0x800 past the start of a TLS block to make full use of the range
234// of load/store instructions, refer to [2].
235//
236// [1]: RISC-V ELF Specification.
237// https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc#constants
238// [2]: Documentation of TLS data structures
239// https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/53
240#define TLS_DTV_OFFSET 0x800
241#else
242#define TLS_DTV_OFFSET 0
243#endif