• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
31 #include <vector>
32 
33 #include "async_safe/CHECK.h"
34 #include "private/ScopedRWLock.h"
35 #include "private/ScopedSignalBlocker.h"
36 #include "private/bionic_defs.h"
37 #include "private/bionic_elf_tls.h"
38 #include "private/bionic_globals.h"
39 #include "private/linker_native_bridge.h"
40 #include "linker_main.h"
41 #include "linker_soinfo.h"
42 
43 static bool g_static_tls_finished;
44 static std::vector<TlsModule> g_tls_modules;
45 
get_unused_module_index()46 static size_t get_unused_module_index() {
47   for (size_t i = 0; i < g_tls_modules.size(); ++i) {
48     if (g_tls_modules[i].soinfo_ptr == nullptr) {
49       return i;
50     }
51   }
52   g_tls_modules.push_back({});
53   __libc_shared_globals()->tls_modules.module_count = g_tls_modules.size();
54   __libc_shared_globals()->tls_modules.module_table = g_tls_modules.data();
55   return g_tls_modules.size() - 1;
56 }
57 
register_tls_module(soinfo * si,size_t static_offset)58 static void register_tls_module(soinfo* si, size_t static_offset) {
59   TlsModules& libc_modules = __libc_shared_globals()->tls_modules;
60 
61   // The global TLS module table points at the std::vector of modules declared
62   // in this file, so acquire a write lock before modifying the std::vector.
63   ScopedSignalBlocker ssb;
64   ScopedWriteLock locker(&libc_modules.rwlock);
65 
66   size_t module_idx = get_unused_module_index();
67 
68   soinfo_tls* si_tls = si->get_tls();
69   si_tls->module_id = __tls_module_idx_to_id(module_idx);
70 
71   const size_t new_generation = ++libc_modules.generation;
72   __libc_tls_generation_copy = new_generation;
73   if (libc_modules.generation_libc_so != nullptr) {
74     *libc_modules.generation_libc_so = new_generation;
75   }
76 
77   g_tls_modules[module_idx] = {
78     .segment = si_tls->segment,
79     .static_offset = static_offset,
80     .first_generation = new_generation,
81     .soinfo_ptr = si,
82   };
83 }
84 
unregister_tls_module(soinfo * si)85 static void unregister_tls_module(soinfo* si) {
86   ScopedSignalBlocker ssb;
87   ScopedWriteLock locker(&__libc_shared_globals()->tls_modules.rwlock);
88 
89   soinfo_tls* si_tls = si->get_tls();
90   TlsModule& mod = g_tls_modules[__tls_module_id_to_idx(si_tls->module_id)];
91   CHECK(mod.static_offset == SIZE_MAX);
92   CHECK(mod.soinfo_ptr == si);
93   mod = {};
94   si_tls->module_id = kTlsUninitializedModuleId;
95 }
96 
97 // The reference is valid until a TLS module is registered or unregistered.
get_tls_module(size_t module_id)98 const TlsModule& get_tls_module(size_t module_id) {
99   size_t module_idx = __tls_module_id_to_idx(module_id);
100   CHECK(module_idx < g_tls_modules.size());
101   return g_tls_modules[module_idx];
102 }
103 
104 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
__linker_reserve_bionic_tls_in_static_tls()105 extern "C" void __linker_reserve_bionic_tls_in_static_tls() {
106   __libc_shared_globals()->static_tls_layout.reserve_bionic_tls();
107 }
108 
linker_setup_exe_static_tls(const char * progname)109 void linker_setup_exe_static_tls(const char* progname) {
110   soinfo* somain = solist_get_somain();
111   StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout;
112   if (somain->get_tls() == nullptr) {
113     layout.reserve_exe_segment_and_tcb(nullptr, progname);
114   } else {
115     register_tls_module(somain, layout.reserve_exe_segment_and_tcb(&somain->get_tls()->segment, progname));
116   }
117 
118   // The pthread key data is located at the very front of bionic_tls. As a
119   // temporary workaround, allocate bionic_tls just after the thread pointer so
120   // Golang can find its pthread key, as long as the executable's TLS segment is
121   // small enough. Specifically, Golang scans forward 384 words from the TP on
122   // ARM.
123   //  - http://b/118381796
124   //  - https://github.com/golang/go/issues/29674
125   __linker_reserve_bionic_tls_in_static_tls();
126 }
127 
linker_finalize_static_tls()128 void linker_finalize_static_tls() {
129   g_static_tls_finished = true;
130   __libc_shared_globals()->static_tls_layout.finish_layout();
131 }
132 
register_soinfo_tls(soinfo * si)133 void register_soinfo_tls(soinfo* si) {
134   soinfo_tls* si_tls = si->get_tls();
135   if (si_tls == nullptr || si_tls->module_id != kTlsUninitializedModuleId) {
136     return;
137   }
138   size_t static_offset = SIZE_MAX;
139   if (!g_static_tls_finished) {
140     StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout;
141     static_offset = layout.reserve_solib_segment(si_tls->segment);
142   }
143   register_tls_module(si, static_offset);
144 }
145 
unregister_soinfo_tls(soinfo * si)146 void unregister_soinfo_tls(soinfo* si) {
147   soinfo_tls* si_tls = si->get_tls();
148   if (si_tls == nullptr || si_tls->module_id == kTlsUninitializedModuleId) {
149     return;
150   }
151   return unregister_tls_module(si);
152 }
153