Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 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 | |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
Elliott Hughes | 3019d78 | 2019-02-13 12:39:07 -0800 | [diff] [blame] | 33 | #include "async_safe/CHECK.h" |
Ryan Prichard | 32bb367 | 2024-03-08 16:53:06 -0800 | [diff] [blame^] | 34 | #include "linker_globals.h" |
| 35 | #include "linker_main.h" |
| 36 | #include "linker_soinfo.h" |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 37 | #include "private/ScopedRWLock.h" |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 38 | #include "private/ScopedSignalBlocker.h" |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 39 | #include "private/bionic_defs.h" |
| 40 | #include "private/bionic_elf_tls.h" |
| 41 | #include "private/bionic_globals.h" |
| 42 | #include "private/linker_native_bridge.h" |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 43 | |
| 44 | static bool g_static_tls_finished; |
| 45 | static std::vector<TlsModule> g_tls_modules; |
| 46 | |
| 47 | static size_t get_unused_module_index() { |
| 48 | for (size_t i = 0; i < g_tls_modules.size(); ++i) { |
| 49 | if (g_tls_modules[i].soinfo_ptr == nullptr) { |
| 50 | return i; |
| 51 | } |
| 52 | } |
| 53 | g_tls_modules.push_back({}); |
| 54 | __libc_shared_globals()->tls_modules.module_count = g_tls_modules.size(); |
| 55 | __libc_shared_globals()->tls_modules.module_table = g_tls_modules.data(); |
| 56 | return g_tls_modules.size() - 1; |
| 57 | } |
| 58 | |
| 59 | static void register_tls_module(soinfo* si, size_t static_offset) { |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 60 | TlsModules& libc_modules = __libc_shared_globals()->tls_modules; |
| 61 | |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 62 | // The global TLS module table points at the std::vector of modules declared |
| 63 | // in this file, so acquire a write lock before modifying the std::vector. |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 64 | ScopedSignalBlocker ssb; |
| 65 | ScopedWriteLock locker(&libc_modules.rwlock); |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 66 | |
| 67 | size_t module_idx = get_unused_module_index(); |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 68 | |
| 69 | soinfo_tls* si_tls = si->get_tls(); |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 70 | si_tls->module_id = __tls_module_idx_to_id(module_idx); |
| 71 | |
| 72 | const size_t new_generation = ++libc_modules.generation; |
| 73 | __libc_tls_generation_copy = new_generation; |
| 74 | if (libc_modules.generation_libc_so != nullptr) { |
| 75 | *libc_modules.generation_libc_so = new_generation; |
| 76 | } |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 77 | |
Ryan Prichard | bf427f4 | 2019-01-17 16:37:11 -0800 | [diff] [blame] | 78 | g_tls_modules[module_idx] = { |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 79 | .segment = si_tls->segment, |
| 80 | .static_offset = static_offset, |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 81 | .first_generation = new_generation, |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 82 | .soinfo_ptr = si, |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | static void unregister_tls_module(soinfo* si) { |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 87 | ScopedSignalBlocker ssb; |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 88 | ScopedWriteLock locker(&__libc_shared_globals()->tls_modules.rwlock); |
| 89 | |
| 90 | soinfo_tls* si_tls = si->get_tls(); |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 91 | TlsModule& mod = g_tls_modules[__tls_module_id_to_idx(si_tls->module_id)]; |
Ryan Prichard | bf427f4 | 2019-01-17 16:37:11 -0800 | [diff] [blame] | 92 | CHECK(mod.static_offset == SIZE_MAX); |
| 93 | CHECK(mod.soinfo_ptr == si); |
| 94 | mod = {}; |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 95 | si_tls->module_id = kTlsUninitializedModuleId; |
Ryan Prichard | bf427f4 | 2019-01-17 16:37:11 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // The reference is valid until a TLS module is registered or unregistered. |
| 99 | const TlsModule& get_tls_module(size_t module_id) { |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 100 | size_t module_idx = __tls_module_id_to_idx(module_id); |
Ryan Prichard | bf427f4 | 2019-01-17 16:37:11 -0800 | [diff] [blame] | 101 | CHECK(module_idx < g_tls_modules.size()); |
| 102 | return g_tls_modules[module_idx]; |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 103 | } |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 104 | |
| 105 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 106 | extern "C" void __linker_reserve_bionic_tls_in_static_tls() { |
| 107 | __libc_shared_globals()->static_tls_layout.reserve_bionic_tls(); |
| 108 | } |
| 109 | |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 110 | void linker_setup_exe_static_tls(const char* progname) { |
| 111 | soinfo* somain = solist_get_somain(); |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 112 | StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout; |
Ryan Prichard | 32bb367 | 2024-03-08 16:53:06 -0800 | [diff] [blame^] | 113 | |
| 114 | // For ldd, don't add the executable's TLS segment to the static TLS layout. |
| 115 | // It is likely to trigger the underaligned TLS segment error on arm32/arm64 |
| 116 | // when the ldd argument is actually a shared object. |
| 117 | if (somain->get_tls() == nullptr || g_is_ldd) { |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 118 | layout.reserve_exe_segment_and_tcb(nullptr, progname); |
| 119 | } else { |
| 120 | register_tls_module(somain, layout.reserve_exe_segment_and_tcb(&somain->get_tls()->segment, progname)); |
| 121 | } |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 122 | |
| 123 | // The pthread key data is located at the very front of bionic_tls. As a |
| 124 | // temporary workaround, allocate bionic_tls just after the thread pointer so |
| 125 | // Golang can find its pthread key, as long as the executable's TLS segment is |
| 126 | // small enough. Specifically, Golang scans forward 384 words from the TP on |
| 127 | // ARM. |
| 128 | // - http://b/118381796 |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 129 | // - https://github.com/golang/go/issues/29674 |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 130 | __linker_reserve_bionic_tls_in_static_tls(); |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 131 | } |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 132 | |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 133 | void linker_finalize_static_tls() { |
| 134 | g_static_tls_finished = true; |
| 135 | __libc_shared_globals()->static_tls_layout.finish_layout(); |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 136 | TlsModules& modules = __libc_shared_globals()->tls_modules; |
| 137 | modules.static_module_count = modules.module_count; |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void register_soinfo_tls(soinfo* si) { |
Ryan Prichard | 32bb367 | 2024-03-08 16:53:06 -0800 | [diff] [blame^] | 141 | // ldd skips registration of the executable's TLS segment above to avoid the |
| 142 | // arm32/arm64 underalignment error. For consistency, also skip registration |
| 143 | // of TLS segments here, for shared objects. |
| 144 | if (g_is_ldd) return; |
| 145 | |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 146 | soinfo_tls* si_tls = si->get_tls(); |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 147 | if (si_tls == nullptr || si_tls->module_id != kTlsUninitializedModuleId) { |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | size_t static_offset = SIZE_MAX; |
| 151 | if (!g_static_tls_finished) { |
| 152 | StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout; |
| 153 | static_offset = layout.reserve_solib_segment(si_tls->segment); |
| 154 | } |
| 155 | register_tls_module(si, static_offset); |
| 156 | } |
| 157 | |
| 158 | void unregister_soinfo_tls(soinfo* si) { |
| 159 | soinfo_tls* si_tls = si->get_tls(); |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 160 | if (si_tls == nullptr || si_tls->module_id == kTlsUninitializedModuleId) { |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | return unregister_tls_module(si); |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 164 | } |