blob: a3aa9bfacd6aef3bc1bcbdbe19ae9a7dffb438d5 [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#include "linker_tls.h"
30
Ryan Pricharde5e69e02019-01-01 18:53:48 -080031#include <vector>
32
33#include "private/ScopedRWLock.h"
Ryan Prichard16455b52019-01-18 01:00:59 -080034#include "private/ScopedSignalBlocker.h"
Ryan Prichard45d13492019-01-03 02:51:30 -080035#include "private/bionic_defs.h"
36#include "private/bionic_elf_tls.h"
37#include "private/bionic_globals.h"
38#include "private/linker_native_bridge.h"
Ryan Pricharde5e69e02019-01-01 18:53:48 -080039#include "linker_main.h"
40#include "linker_soinfo.h"
41
42static bool g_static_tls_finished;
43static std::vector<TlsModule> g_tls_modules;
44
45static size_t get_unused_module_index() {
46 for (size_t i = 0; i < g_tls_modules.size(); ++i) {
47 if (g_tls_modules[i].soinfo_ptr == nullptr) {
48 return i;
49 }
50 }
51 g_tls_modules.push_back({});
52 __libc_shared_globals()->tls_modules.module_count = g_tls_modules.size();
53 __libc_shared_globals()->tls_modules.module_table = g_tls_modules.data();
54 return g_tls_modules.size() - 1;
55}
56
57static void register_tls_module(soinfo* si, size_t static_offset) {
Ryan Prichard16455b52019-01-18 01:00:59 -080058 TlsModules& libc_modules = __libc_shared_globals()->tls_modules;
59
Ryan Pricharde5e69e02019-01-01 18:53:48 -080060 // The global TLS module table points at the std::vector of modules declared
61 // in this file, so acquire a write lock before modifying the std::vector.
Ryan Prichard16455b52019-01-18 01:00:59 -080062 ScopedSignalBlocker ssb;
63 ScopedWriteLock locker(&libc_modules.rwlock);
Ryan Pricharde5e69e02019-01-01 18:53:48 -080064
65 size_t module_idx = get_unused_module_index();
Ryan Pricharde5e69e02019-01-01 18:53:48 -080066
67 soinfo_tls* si_tls = si->get_tls();
Ryan Prichard16455b52019-01-18 01:00:59 -080068 si_tls->module_id = __tls_module_idx_to_id(module_idx);
69
70 const size_t new_generation = ++libc_modules.generation;
71 __libc_tls_generation_copy = new_generation;
72 if (libc_modules.generation_libc_so != nullptr) {
73 *libc_modules.generation_libc_so = new_generation;
74 }
Ryan Pricharde5e69e02019-01-01 18:53:48 -080075
Ryan Prichardbf427f42019-01-17 16:37:11 -080076 g_tls_modules[module_idx] = {
Ryan Pricharde5e69e02019-01-01 18:53:48 -080077 .segment = si_tls->segment,
78 .static_offset = static_offset,
Ryan Prichard16455b52019-01-18 01:00:59 -080079 .first_generation = new_generation,
Ryan Pricharde5e69e02019-01-01 18:53:48 -080080 .soinfo_ptr = si,
81 };
82}
83
84static void unregister_tls_module(soinfo* si) {
Ryan Prichard16455b52019-01-18 01:00:59 -080085 ScopedSignalBlocker ssb;
Ryan Pricharde5e69e02019-01-01 18:53:48 -080086 ScopedWriteLock locker(&__libc_shared_globals()->tls_modules.rwlock);
87
88 soinfo_tls* si_tls = si->get_tls();
Ryan Prichard16455b52019-01-18 01:00:59 -080089 TlsModule& mod = g_tls_modules[__tls_module_id_to_idx(si_tls->module_id)];
Ryan Prichardbf427f42019-01-17 16:37:11 -080090 CHECK(mod.static_offset == SIZE_MAX);
91 CHECK(mod.soinfo_ptr == si);
92 mod = {};
Ryan Prichard16455b52019-01-18 01:00:59 -080093 si_tls->module_id = kTlsUninitializedModuleId;
Ryan Prichardbf427f42019-01-17 16:37:11 -080094}
95
96// The reference is valid until a TLS module is registered or unregistered.
97const TlsModule& get_tls_module(size_t module_id) {
Ryan Prichard16455b52019-01-18 01:00:59 -080098 size_t module_idx = __tls_module_id_to_idx(module_id);
Ryan Prichardbf427f42019-01-17 16:37:11 -080099 CHECK(module_idx < g_tls_modules.size());
100 return g_tls_modules[module_idx];
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800101}
Ryan Prichard45d13492019-01-03 02:51:30 -0800102
103__BIONIC_WEAK_FOR_NATIVE_BRIDGE
104extern "C" void __linker_reserve_bionic_tls_in_static_tls() {
105 __libc_shared_globals()->static_tls_layout.reserve_bionic_tls();
106}
107
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800108void linker_setup_exe_static_tls(const char* progname) {
109 soinfo* somain = solist_get_somain();
Ryan Prichard45d13492019-01-03 02:51:30 -0800110 StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout;
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800111 if (somain->get_tls() == nullptr) {
112 layout.reserve_exe_segment_and_tcb(nullptr, progname);
113 } else {
114 register_tls_module(somain, layout.reserve_exe_segment_and_tcb(&somain->get_tls()->segment, progname));
115 }
Ryan Prichard45d13492019-01-03 02:51:30 -0800116
117 // The pthread key data is located at the very front of bionic_tls. As a
118 // temporary workaround, allocate bionic_tls just after the thread pointer so
119 // Golang can find its pthread key, as long as the executable's TLS segment is
120 // small enough. Specifically, Golang scans forward 384 words from the TP on
121 // ARM.
122 // - http://b/118381796
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800123 // - https://github.com/golang/go/issues/29674
Ryan Prichard45d13492019-01-03 02:51:30 -0800124 __linker_reserve_bionic_tls_in_static_tls();
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800125}
Ryan Prichard45d13492019-01-03 02:51:30 -0800126
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800127void linker_finalize_static_tls() {
128 g_static_tls_finished = true;
129 __libc_shared_globals()->static_tls_layout.finish_layout();
130}
131
132void register_soinfo_tls(soinfo* si) {
133 soinfo_tls* si_tls = si->get_tls();
Ryan Prichard16455b52019-01-18 01:00:59 -0800134 if (si_tls == nullptr || si_tls->module_id != kTlsUninitializedModuleId) {
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800135 return;
136 }
137 size_t static_offset = SIZE_MAX;
138 if (!g_static_tls_finished) {
139 StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout;
140 static_offset = layout.reserve_solib_segment(si_tls->segment);
141 }
142 register_tls_module(si, static_offset);
143}
144
145void unregister_soinfo_tls(soinfo* si) {
146 soinfo_tls* si_tls = si->get_tls();
Ryan Prichard16455b52019-01-18 01:00:59 -0800147 if (si_tls == nullptr || si_tls->module_id == kTlsUninitializedModuleId) {
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800148 return;
149 }
150 return unregister_tls_module(si);
Ryan Prichard45d13492019-01-03 02:51:30 -0800151}