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