blob: 09e1958ef6e611cfe6bcd7ea88c7a5dd72a6f71d [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
37struct TlsSegment {
38 size_t size = 0;
39 size_t alignment = 1;
40 const void* init_ptr = ""; // Field is non-null even when init_size is 0.
41 size_t init_size = 0;
42};
43
44__LIBC_HIDDEN__ bool __bionic_get_tls_segment(const ElfW(Phdr)* phdr_table, size_t phdr_count,
Ryan Prichard19883502019-01-16 23:13:38 -080045 ElfW(Addr) load_bias, TlsSegment* out);
46
47__LIBC_HIDDEN__ bool __bionic_check_tls_alignment(size_t* alignment);
Ryan Prichard45d13492019-01-03 02:51:30 -080048
49struct StaticTlsLayout {
50 constexpr StaticTlsLayout() {}
51
52private:
53 size_t offset_ = 0;
54 size_t alignment_ = 1;
55 bool overflowed_ = false;
56
57 // Offsets to various Bionic TLS structs from the beginning of static TLS.
58 size_t offset_bionic_tcb_ = SIZE_MAX;
59 size_t offset_bionic_tls_ = SIZE_MAX;
60
61public:
62 size_t offset_bionic_tcb() const { return offset_bionic_tcb_; }
63 size_t offset_bionic_tls() const { return offset_bionic_tls_; }
Ryan Prichardfb8730d2019-01-15 00:11:37 -080064 size_t offset_thread_pointer() const;
Ryan Prichard45d13492019-01-03 02:51:30 -080065
66 size_t size() const { return offset_; }
67 size_t alignment() const { return alignment_; }
68 bool overflowed() const { return overflowed_; }
69
Ryan Prichard977e47d2019-01-14 21:52:14 -080070 size_t reserve_exe_segment_and_tcb(const TlsSegment* exe_segment, const char* progname);
Ryan Prichard45d13492019-01-03 02:51:30 -080071 void reserve_bionic_tls();
Ryan Prichard977e47d2019-01-14 21:52:14 -080072 size_t reserve_solib_segment(const TlsSegment& segment) {
73 return reserve(segment.size, segment.alignment);
74 }
Ryan Prichard45d13492019-01-03 02:51:30 -080075 void finish_layout();
76
77private:
78 size_t reserve(size_t size, size_t alignment);
79
80 template <typename T> size_t reserve_type() {
81 return reserve(sizeof(T), alignof(T));
82 }
83
84 size_t round_up_with_overflow_check(size_t value, size_t alignment);
85};
Ryan Pricharde5e69e02019-01-01 18:53:48 -080086
87// A descriptor for a single ELF TLS module.
88struct TlsModule {
89 TlsSegment segment;
90
91 // Offset into the static TLS block or SIZE_MAX for a dynamic module.
92 size_t static_offset = SIZE_MAX;
93
94 // The generation in which this module was loaded. Dynamic TLS lookups use
95 // this field to detect when a module has been unloaded.
96 size_t first_generation = 0;
97
98 // Used by the dynamic linker to track the associated soinfo* object.
99 void* soinfo_ptr = nullptr;
100};
101
102// Table of the ELF TLS modules. Either the dynamic linker or the static
103// initialization code prepares this table, and it's then used during thread
104// creation and for dynamic TLS lookups.
105struct TlsModules {
106 constexpr TlsModules() {}
107
108 // A generation counter. The value is incremented each time an solib is loaded
109 // or unloaded.
110 _Atomic(size_t) generation = 0;
111
112 // Access to the TlsModule[] table requires taking this lock.
113 pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
114
115 // Pointer to a block of TlsModule objects. The first module has ID 1 and
116 // is stored at index 0 in this table.
117 size_t module_count = 0;
118 TlsModule* module_table = nullptr;
119};
Ryan Prichard361c1b42019-01-15 13:45:27 -0800120
121void __init_static_tls(void* static_tls);