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