1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2019 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker *
5*8d67ca89SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker * are met:
8*8d67ca89SAndroid Build Coastguard Worker * * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker * * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker * the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker * distribution.
14*8d67ca89SAndroid Build Coastguard Worker *
15*8d67ca89SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker */
28*8d67ca89SAndroid Build Coastguard Worker
29*8d67ca89SAndroid Build Coastguard Worker #include "linker_tls.h"
30*8d67ca89SAndroid Build Coastguard Worker
31*8d67ca89SAndroid Build Coastguard Worker #include <vector>
32*8d67ca89SAndroid Build Coastguard Worker
33*8d67ca89SAndroid Build Coastguard Worker #include "async_safe/CHECK.h"
34*8d67ca89SAndroid Build Coastguard Worker #include "linker_globals.h"
35*8d67ca89SAndroid Build Coastguard Worker #include "linker_main.h"
36*8d67ca89SAndroid Build Coastguard Worker #include "linker_soinfo.h"
37*8d67ca89SAndroid Build Coastguard Worker #include "private/ScopedRWLock.h"
38*8d67ca89SAndroid Build Coastguard Worker #include "private/ScopedSignalBlocker.h"
39*8d67ca89SAndroid Build Coastguard Worker #include "private/bionic_defs.h"
40*8d67ca89SAndroid Build Coastguard Worker #include "private/bionic_elf_tls.h"
41*8d67ca89SAndroid Build Coastguard Worker #include "private/bionic_globals.h"
42*8d67ca89SAndroid Build Coastguard Worker #include "private/linker_native_bridge.h"
43*8d67ca89SAndroid Build Coastguard Worker
44*8d67ca89SAndroid Build Coastguard Worker static bool g_static_tls_finished;
45*8d67ca89SAndroid Build Coastguard Worker static std::vector<TlsModule> g_tls_modules;
46*8d67ca89SAndroid Build Coastguard Worker
get_unused_module_index()47*8d67ca89SAndroid Build Coastguard Worker static size_t get_unused_module_index() {
48*8d67ca89SAndroid Build Coastguard Worker for (size_t i = 0; i < g_tls_modules.size(); ++i) {
49*8d67ca89SAndroid Build Coastguard Worker if (g_tls_modules[i].soinfo_ptr == nullptr) {
50*8d67ca89SAndroid Build Coastguard Worker return i;
51*8d67ca89SAndroid Build Coastguard Worker }
52*8d67ca89SAndroid Build Coastguard Worker }
53*8d67ca89SAndroid Build Coastguard Worker g_tls_modules.push_back({});
54*8d67ca89SAndroid Build Coastguard Worker __libc_shared_globals()->tls_modules.module_count = g_tls_modules.size();
55*8d67ca89SAndroid Build Coastguard Worker __libc_shared_globals()->tls_modules.module_table = g_tls_modules.data();
56*8d67ca89SAndroid Build Coastguard Worker return g_tls_modules.size() - 1;
57*8d67ca89SAndroid Build Coastguard Worker }
58*8d67ca89SAndroid Build Coastguard Worker
register_tls_module(soinfo * si,size_t static_offset)59*8d67ca89SAndroid Build Coastguard Worker static void register_tls_module(soinfo* si, size_t static_offset) {
60*8d67ca89SAndroid Build Coastguard Worker TlsModules& libc_modules = __libc_shared_globals()->tls_modules;
61*8d67ca89SAndroid Build Coastguard Worker
62*8d67ca89SAndroid Build Coastguard Worker // The global TLS module table points at the std::vector of modules declared
63*8d67ca89SAndroid Build Coastguard Worker // in this file, so acquire a write lock before modifying the std::vector.
64*8d67ca89SAndroid Build Coastguard Worker ScopedSignalBlocker ssb;
65*8d67ca89SAndroid Build Coastguard Worker ScopedWriteLock locker(&libc_modules.rwlock);
66*8d67ca89SAndroid Build Coastguard Worker
67*8d67ca89SAndroid Build Coastguard Worker size_t module_idx = get_unused_module_index();
68*8d67ca89SAndroid Build Coastguard Worker
69*8d67ca89SAndroid Build Coastguard Worker soinfo_tls* si_tls = si->get_tls();
70*8d67ca89SAndroid Build Coastguard Worker si_tls->module_id = __tls_module_idx_to_id(module_idx);
71*8d67ca89SAndroid Build Coastguard Worker
72*8d67ca89SAndroid Build Coastguard Worker const size_t new_generation = ++libc_modules.generation;
73*8d67ca89SAndroid Build Coastguard Worker __libc_tls_generation_copy = new_generation;
74*8d67ca89SAndroid Build Coastguard Worker if (libc_modules.generation_libc_so != nullptr) {
75*8d67ca89SAndroid Build Coastguard Worker *libc_modules.generation_libc_so = new_generation;
76*8d67ca89SAndroid Build Coastguard Worker }
77*8d67ca89SAndroid Build Coastguard Worker
78*8d67ca89SAndroid Build Coastguard Worker g_tls_modules[module_idx] = {
79*8d67ca89SAndroid Build Coastguard Worker .segment = si_tls->segment,
80*8d67ca89SAndroid Build Coastguard Worker .static_offset = static_offset,
81*8d67ca89SAndroid Build Coastguard Worker .first_generation = new_generation,
82*8d67ca89SAndroid Build Coastguard Worker .soinfo_ptr = si,
83*8d67ca89SAndroid Build Coastguard Worker };
84*8d67ca89SAndroid Build Coastguard Worker }
85*8d67ca89SAndroid Build Coastguard Worker
unregister_tls_module(soinfo * si)86*8d67ca89SAndroid Build Coastguard Worker static void unregister_tls_module(soinfo* si) {
87*8d67ca89SAndroid Build Coastguard Worker ScopedSignalBlocker ssb;
88*8d67ca89SAndroid Build Coastguard Worker ScopedWriteLock locker(&__libc_shared_globals()->tls_modules.rwlock);
89*8d67ca89SAndroid Build Coastguard Worker
90*8d67ca89SAndroid Build Coastguard Worker soinfo_tls* si_tls = si->get_tls();
91*8d67ca89SAndroid Build Coastguard Worker TlsModule& mod = g_tls_modules[__tls_module_id_to_idx(si_tls->module_id)];
92*8d67ca89SAndroid Build Coastguard Worker CHECK(mod.static_offset == SIZE_MAX);
93*8d67ca89SAndroid Build Coastguard Worker CHECK(mod.soinfo_ptr == si);
94*8d67ca89SAndroid Build Coastguard Worker mod = {};
95*8d67ca89SAndroid Build Coastguard Worker si_tls->module_id = kTlsUninitializedModuleId;
96*8d67ca89SAndroid Build Coastguard Worker }
97*8d67ca89SAndroid Build Coastguard Worker
98*8d67ca89SAndroid Build Coastguard Worker // The reference is valid until a TLS module is registered or unregistered.
get_tls_module(size_t module_id)99*8d67ca89SAndroid Build Coastguard Worker const TlsModule& get_tls_module(size_t module_id) {
100*8d67ca89SAndroid Build Coastguard Worker size_t module_idx = __tls_module_id_to_idx(module_id);
101*8d67ca89SAndroid Build Coastguard Worker CHECK(module_idx < g_tls_modules.size());
102*8d67ca89SAndroid Build Coastguard Worker return g_tls_modules[module_idx];
103*8d67ca89SAndroid Build Coastguard Worker }
104*8d67ca89SAndroid Build Coastguard Worker
105*8d67ca89SAndroid Build Coastguard Worker __BIONIC_WEAK_FOR_NATIVE_BRIDGE
__linker_reserve_bionic_tls_in_static_tls()106*8d67ca89SAndroid Build Coastguard Worker extern "C" void __linker_reserve_bionic_tls_in_static_tls() {
107*8d67ca89SAndroid Build Coastguard Worker __libc_shared_globals()->static_tls_layout.reserve_bionic_tls();
108*8d67ca89SAndroid Build Coastguard Worker }
109*8d67ca89SAndroid Build Coastguard Worker
linker_setup_exe_static_tls(const char * progname)110*8d67ca89SAndroid Build Coastguard Worker void linker_setup_exe_static_tls(const char* progname) {
111*8d67ca89SAndroid Build Coastguard Worker soinfo* somain = solist_get_somain();
112*8d67ca89SAndroid Build Coastguard Worker StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout;
113*8d67ca89SAndroid Build Coastguard Worker
114*8d67ca89SAndroid Build Coastguard Worker // For ldd, don't add the executable's TLS segment to the static TLS layout.
115*8d67ca89SAndroid Build Coastguard Worker // It is likely to trigger the underaligned TLS segment error on arm32/arm64
116*8d67ca89SAndroid Build Coastguard Worker // when the ldd argument is actually a shared object.
117*8d67ca89SAndroid Build Coastguard Worker if (somain->get_tls() == nullptr || g_is_ldd) {
118*8d67ca89SAndroid Build Coastguard Worker layout.reserve_exe_segment_and_tcb(nullptr, progname);
119*8d67ca89SAndroid Build Coastguard Worker } else {
120*8d67ca89SAndroid Build Coastguard Worker register_tls_module(somain, layout.reserve_exe_segment_and_tcb(&somain->get_tls()->segment, progname));
121*8d67ca89SAndroid Build Coastguard Worker }
122*8d67ca89SAndroid Build Coastguard Worker
123*8d67ca89SAndroid Build Coastguard Worker // The pthread key data is located at the very front of bionic_tls. As a
124*8d67ca89SAndroid Build Coastguard Worker // temporary workaround, allocate bionic_tls just after the thread pointer so
125*8d67ca89SAndroid Build Coastguard Worker // Golang can find its pthread key, as long as the executable's TLS segment is
126*8d67ca89SAndroid Build Coastguard Worker // small enough. Specifically, Golang scans forward 384 words from the TP on
127*8d67ca89SAndroid Build Coastguard Worker // ARM.
128*8d67ca89SAndroid Build Coastguard Worker // - http://b/118381796
129*8d67ca89SAndroid Build Coastguard Worker // - https://github.com/golang/go/issues/29674
130*8d67ca89SAndroid Build Coastguard Worker __linker_reserve_bionic_tls_in_static_tls();
131*8d67ca89SAndroid Build Coastguard Worker }
132*8d67ca89SAndroid Build Coastguard Worker
linker_finalize_static_tls()133*8d67ca89SAndroid Build Coastguard Worker void linker_finalize_static_tls() {
134*8d67ca89SAndroid Build Coastguard Worker g_static_tls_finished = true;
135*8d67ca89SAndroid Build Coastguard Worker __libc_shared_globals()->static_tls_layout.finish_layout();
136*8d67ca89SAndroid Build Coastguard Worker TlsModules& modules = __libc_shared_globals()->tls_modules;
137*8d67ca89SAndroid Build Coastguard Worker modules.static_module_count = modules.module_count;
138*8d67ca89SAndroid Build Coastguard Worker }
139*8d67ca89SAndroid Build Coastguard Worker
register_soinfo_tls(soinfo * si)140*8d67ca89SAndroid Build Coastguard Worker void register_soinfo_tls(soinfo* si) {
141*8d67ca89SAndroid Build Coastguard Worker // ldd skips registration of the executable's TLS segment above to avoid the
142*8d67ca89SAndroid Build Coastguard Worker // arm32/arm64 underalignment error. For consistency, also skip registration
143*8d67ca89SAndroid Build Coastguard Worker // of TLS segments here, for shared objects.
144*8d67ca89SAndroid Build Coastguard Worker if (g_is_ldd) return;
145*8d67ca89SAndroid Build Coastguard Worker
146*8d67ca89SAndroid Build Coastguard Worker soinfo_tls* si_tls = si->get_tls();
147*8d67ca89SAndroid Build Coastguard Worker if (si_tls == nullptr || si_tls->module_id != kTlsUninitializedModuleId) {
148*8d67ca89SAndroid Build Coastguard Worker return;
149*8d67ca89SAndroid Build Coastguard Worker }
150*8d67ca89SAndroid Build Coastguard Worker size_t static_offset = SIZE_MAX;
151*8d67ca89SAndroid Build Coastguard Worker if (!g_static_tls_finished) {
152*8d67ca89SAndroid Build Coastguard Worker StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout;
153*8d67ca89SAndroid Build Coastguard Worker static_offset = layout.reserve_solib_segment(si_tls->segment);
154*8d67ca89SAndroid Build Coastguard Worker }
155*8d67ca89SAndroid Build Coastguard Worker register_tls_module(si, static_offset);
156*8d67ca89SAndroid Build Coastguard Worker }
157*8d67ca89SAndroid Build Coastguard Worker
unregister_soinfo_tls(soinfo * si)158*8d67ca89SAndroid Build Coastguard Worker void unregister_soinfo_tls(soinfo* si) {
159*8d67ca89SAndroid Build Coastguard Worker soinfo_tls* si_tls = si->get_tls();
160*8d67ca89SAndroid Build Coastguard Worker if (si_tls == nullptr || si_tls->module_id == kTlsUninitializedModuleId) {
161*8d67ca89SAndroid Build Coastguard Worker return;
162*8d67ca89SAndroid Build Coastguard Worker }
163*8d67ca89SAndroid Build Coastguard Worker return unregister_tls_module(si);
164*8d67ca89SAndroid Build Coastguard Worker }
165