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