Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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_soinfo.h" |
| 30 | |
| 31 | #include <dlfcn.h> |
| 32 | #include <elf.h> |
| 33 | #include <string.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <unistd.h> |
| 36 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 37 | #include <async_safe/log.h> |
| 38 | |
Ryan Prichard | 0e12cce | 2020-01-02 14:59:11 -0800 | [diff] [blame] | 39 | #include "linker.h" |
Ryan Prichard | 4d4087d | 2019-12-02 16:55:48 -0800 | [diff] [blame] | 40 | #include "linker_config.h" |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 41 | #include "linker_debug.h" |
| 42 | #include "linker_globals.h" |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 43 | #include "linker_gnu_hash.h" |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 44 | #include "linker_logger.h" |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 45 | #include "linker_relocate.h" |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 46 | #include "linker_utils.h" |
| 47 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 48 | // Enable the slow lookup path if symbol lookups should be logged. |
| 49 | static bool is_lookup_tracing_enabled() { |
| 50 | return g_ld_debug_verbosity > LINKER_VERBOSITY_TRACE && DO_TRACE_LOOKUP; |
| 51 | } |
| 52 | |
| 53 | SymbolLookupList::SymbolLookupList(soinfo* si) |
| 54 | : sole_lib_(si->get_lookup_lib()), begin_(&sole_lib_), end_(&sole_lib_ + 1) { |
| 55 | CHECK(si != nullptr); |
| 56 | slow_path_count_ += is_lookup_tracing_enabled(); |
| 57 | slow_path_count_ += sole_lib_.needs_sysv_lookup(); |
| 58 | } |
| 59 | |
| 60 | SymbolLookupList::SymbolLookupList(const soinfo_list_t& global_group, const soinfo_list_t& local_group) { |
| 61 | slow_path_count_ += is_lookup_tracing_enabled(); |
| 62 | libs_.reserve(1 + global_group.size() + local_group.size()); |
| 63 | |
| 64 | // Reserve a space in front for DT_SYMBOLIC lookup. |
| 65 | libs_.push_back(SymbolLookupLib {}); |
| 66 | |
| 67 | global_group.for_each([this](soinfo* si) { |
| 68 | libs_.push_back(si->get_lookup_lib()); |
| 69 | slow_path_count_ += libs_.back().needs_sysv_lookup(); |
| 70 | }); |
| 71 | |
| 72 | local_group.for_each([this](soinfo* si) { |
| 73 | libs_.push_back(si->get_lookup_lib()); |
| 74 | slow_path_count_ += libs_.back().needs_sysv_lookup(); |
| 75 | }); |
| 76 | |
| 77 | begin_ = &libs_[1]; |
| 78 | end_ = &libs_[0] + libs_.size(); |
| 79 | } |
| 80 | |
| 81 | /* "This element's presence in a shared object library alters the dynamic linker's |
| 82 | * symbol resolution algorithm for references within the library. Instead of starting |
| 83 | * a symbol search with the executable file, the dynamic linker starts from the shared |
| 84 | * object itself. If the shared object fails to supply the referenced symbol, the |
| 85 | * dynamic linker then searches the executable file and other shared objects as usual." |
| 86 | * |
| 87 | * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html |
| 88 | * |
| 89 | * Note that this is unlikely since static linker avoids generating |
| 90 | * relocations for -Bsymbolic linked dynamic executables. |
| 91 | */ |
| 92 | void SymbolLookupList::set_dt_symbolic_lib(soinfo* lib) { |
| 93 | CHECK(!libs_.empty()); |
| 94 | slow_path_count_ -= libs_[0].needs_sysv_lookup(); |
| 95 | libs_[0] = lib ? lib->get_lookup_lib() : SymbolLookupLib(); |
| 96 | slow_path_count_ += libs_[0].needs_sysv_lookup(); |
| 97 | begin_ = lib ? &libs_[0] : &libs_[1]; |
| 98 | } |
| 99 | |
| 100 | // Check whether a requested version matches the version on a symbol definition. There are a few |
| 101 | // special cases: |
| 102 | // - If the defining DSO has no version info at all, then any version matches. |
| 103 | // - If no version is requested (vi==nullptr, verneed==kVersymNotNeeded), then any non-hidden |
| 104 | // version matches. |
| 105 | // - If the requested version is not defined by the DSO, then verneed is kVersymGlobal, and only |
| 106 | // global symbol definitions match. (This special case is handled as part of the ordinary case |
| 107 | // where the version must match exactly.) |
| 108 | static inline bool check_symbol_version(const ElfW(Versym)* ver_table, uint32_t sym_idx, |
| 109 | const ElfW(Versym) verneed) { |
| 110 | if (ver_table == nullptr) return true; |
| 111 | const uint32_t verdef = ver_table[sym_idx]; |
| 112 | return (verneed == kVersymNotNeeded) ? |
| 113 | !(verdef & kVersymHiddenBit) : |
| 114 | verneed == (verdef & ~kVersymHiddenBit); |
| 115 | } |
| 116 | |
| 117 | template <bool IsGeneral> |
| 118 | __attribute__((noinline)) static const ElfW(Sym)* |
| 119 | soinfo_do_lookup_impl(const char* name, const version_info* vi, |
| 120 | soinfo** si_found_in, const SymbolLookupList& lookup_list) { |
| 121 | const auto [ hash, name_len ] = calculate_gnu_hash(name); |
| 122 | constexpr uint32_t kBloomMaskBits = sizeof(ElfW(Addr)) * 8; |
| 123 | SymbolName elf_symbol_name(name); |
| 124 | |
| 125 | const SymbolLookupLib* end = lookup_list.end(); |
| 126 | const SymbolLookupLib* it = lookup_list.begin(); |
| 127 | |
| 128 | while (true) { |
| 129 | const SymbolLookupLib* lib; |
| 130 | uint32_t sym_idx; |
| 131 | |
| 132 | // Iterate over libraries until we find one whose Bloom filter matches the symbol we're |
| 133 | // searching for. |
| 134 | while (true) { |
| 135 | if (it == end) return nullptr; |
| 136 | lib = it++; |
| 137 | |
| 138 | if (IsGeneral && lib->needs_sysv_lookup()) { |
| 139 | if (const ElfW(Sym)* sym = lib->si_->find_symbol_by_name(elf_symbol_name, vi)) { |
| 140 | *si_found_in = lib->si_; |
| 141 | return sym; |
| 142 | } |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | if (IsGeneral) { |
| 147 | TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)", |
| 148 | name, lib->si_->get_realpath(), reinterpret_cast<void*>(lib->si_->base)); |
| 149 | } |
| 150 | |
| 151 | const uint32_t word_num = (hash / kBloomMaskBits) & lib->gnu_maskwords_; |
| 152 | const ElfW(Addr) bloom_word = lib->gnu_bloom_filter_[word_num]; |
| 153 | const uint32_t h1 = hash % kBloomMaskBits; |
| 154 | const uint32_t h2 = (hash >> lib->gnu_shift2_) % kBloomMaskBits; |
| 155 | |
| 156 | if ((1 & (bloom_word >> h1) & (bloom_word >> h2)) == 1) { |
| 157 | sym_idx = lib->gnu_bucket_[hash % lib->gnu_nbucket_]; |
| 158 | if (sym_idx != 0) { |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if (IsGeneral) { |
| 164 | TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p", |
| 165 | name, lib->si_->get_realpath(), reinterpret_cast<void*>(lib->si_->base)); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // Search the library's hash table chain. |
| 170 | ElfW(Versym) verneed = kVersymNotNeeded; |
| 171 | bool calculated_verneed = false; |
| 172 | |
| 173 | uint32_t chain_value = 0; |
| 174 | const ElfW(Sym)* sym = nullptr; |
| 175 | |
| 176 | do { |
| 177 | sym = lib->symtab_ + sym_idx; |
| 178 | chain_value = lib->gnu_chain_[sym_idx]; |
| 179 | if ((chain_value >> 1) == (hash >> 1)) { |
| 180 | if (vi != nullptr && !calculated_verneed) { |
| 181 | calculated_verneed = true; |
| 182 | verneed = find_verdef_version_index(lib->si_, vi); |
| 183 | } |
| 184 | if (check_symbol_version(lib->versym_, sym_idx, verneed) && |
| 185 | static_cast<size_t>(sym->st_name) + name_len + 1 <= lib->strtab_size_ && |
| 186 | memcmp(lib->strtab_ + sym->st_name, name, name_len + 1) == 0 && |
| 187 | is_symbol_global_and_defined(lib->si_, sym)) { |
| 188 | *si_found_in = lib->si_; |
| 189 | if (IsGeneral) { |
| 190 | TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd", |
| 191 | name, lib->si_->get_realpath(), reinterpret_cast<void*>(sym->st_value), |
| 192 | static_cast<size_t>(sym->st_size)); |
| 193 | } |
| 194 | return sym; |
| 195 | } |
| 196 | } |
| 197 | ++sym_idx; |
| 198 | } while ((chain_value & 1) == 0); |
| 199 | |
| 200 | if (IsGeneral) { |
| 201 | TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p", |
| 202 | name, lib->si_->get_realpath(), reinterpret_cast<void*>(lib->si_->base)); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | const ElfW(Sym)* soinfo_do_lookup(const char* name, const version_info* vi, |
| 208 | soinfo** si_found_in, const SymbolLookupList& lookup_list) { |
| 209 | return lookup_list.needs_slow_path() ? |
| 210 | soinfo_do_lookup_impl<true>(name, vi, si_found_in, lookup_list) : |
| 211 | soinfo_do_lookup_impl<false>(name, vi, si_found_in, lookup_list); |
| 212 | } |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 213 | |
| 214 | soinfo::soinfo(android_namespace_t* ns, const char* realpath, |
| 215 | const struct stat* file_stat, off64_t file_offset, |
| 216 | int rtld_flags) { |
| 217 | memset(this, 0, sizeof(*this)); |
| 218 | |
| 219 | if (realpath != nullptr) { |
| 220 | realpath_ = realpath; |
| 221 | } |
| 222 | |
| 223 | flags_ = FLAG_NEW_SOINFO; |
| 224 | version_ = SOINFO_VERSION; |
| 225 | |
| 226 | if (file_stat != nullptr) { |
| 227 | this->st_dev_ = file_stat->st_dev; |
| 228 | this->st_ino_ = file_stat->st_ino; |
| 229 | this->file_offset_ = file_offset; |
| 230 | } |
| 231 | |
| 232 | this->rtld_flags_ = rtld_flags; |
| 233 | this->primary_namespace_ = ns; |
| 234 | } |
| 235 | |
| 236 | soinfo::~soinfo() { |
| 237 | g_soinfo_handles_map.erase(handle_); |
| 238 | } |
| 239 | |
| 240 | void soinfo::set_dt_runpath(const char* path) { |
| 241 | if (!has_min_version(3)) { |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | std::vector<std::string> runpaths; |
| 246 | |
| 247 | split_path(path, ":", &runpaths); |
| 248 | |
| 249 | std::string origin = dirname(get_realpath()); |
Jiyong Park | 57b9d1e | 2019-01-17 03:14:45 +0900 | [diff] [blame] | 250 | // FIXME: add $PLATFORM. |
| 251 | std::vector<std::pair<std::string, std::string>> params = { |
| 252 | {"ORIGIN", origin}, |
Ryan Prichard | 4d4087d | 2019-12-02 16:55:48 -0800 | [diff] [blame] | 253 | {"LIB", kLibPath}, |
Jiyong Park | 57b9d1e | 2019-01-17 03:14:45 +0900 | [diff] [blame] | 254 | }; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 255 | for (auto&& s : runpaths) { |
Dimitry Ivanov | 2a6d9b2 | 2017-03-11 14:35:38 -0800 | [diff] [blame] | 256 | format_string(&s, params); |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | resolve_paths(runpaths, &dt_runpath_); |
| 260 | } |
| 261 | |
| 262 | const ElfW(Versym)* soinfo::get_versym(size_t n) const { |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 263 | auto table = get_versym_table(); |
| 264 | return table ? table + n : nullptr; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | ElfW(Addr) soinfo::get_verneed_ptr() const { |
| 268 | if (has_min_version(2)) { |
| 269 | return verneed_ptr_; |
| 270 | } |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | size_t soinfo::get_verneed_cnt() const { |
| 276 | if (has_min_version(2)) { |
| 277 | return verneed_cnt_; |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | ElfW(Addr) soinfo::get_verdef_ptr() const { |
| 284 | if (has_min_version(2)) { |
| 285 | return verdef_ptr_; |
| 286 | } |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | size_t soinfo::get_verdef_cnt() const { |
| 292 | if (has_min_version(2)) { |
| 293 | return verdef_cnt_; |
| 294 | } |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 299 | SymbolLookupLib soinfo::get_lookup_lib() { |
| 300 | SymbolLookupLib result {}; |
| 301 | result.si_ = this; |
| 302 | |
| 303 | // For libs that only have SysV hashes, leave the gnu_bloom_filter_ field NULL to signal that |
| 304 | // the fallback code path is needed. |
| 305 | if (!is_gnu_hash()) { |
| 306 | return result; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 309 | result.gnu_maskwords_ = gnu_maskwords_; |
| 310 | result.gnu_shift2_ = gnu_shift2_; |
| 311 | result.gnu_bloom_filter_ = gnu_bloom_filter_; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 312 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 313 | result.strtab_ = strtab_; |
| 314 | result.strtab_size_ = strtab_size_; |
| 315 | result.symtab_ = symtab_; |
| 316 | result.versym_ = get_versym_table(); |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 317 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 318 | result.gnu_chain_ = gnu_chain_; |
| 319 | result.gnu_nbucket_ = gnu_nbucket_; |
| 320 | result.gnu_bucket_ = gnu_bucket_; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 321 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 322 | return result; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Ryan Prichard | 0e12cce | 2020-01-02 14:59:11 -0800 | [diff] [blame] | 325 | const ElfW(Sym)* soinfo::find_symbol_by_name(SymbolName& symbol_name, |
| 326 | const version_info* vi) const { |
| 327 | return is_gnu_hash() ? gnu_lookup(symbol_name, vi) : elf_lookup(symbol_name, vi); |
| 328 | } |
| 329 | |
| 330 | const ElfW(Sym)* soinfo::gnu_lookup(SymbolName& symbol_name, const version_info* vi) const { |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 331 | const uint32_t hash = symbol_name.gnu_hash(); |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 332 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 333 | constexpr uint32_t kBloomMaskBits = sizeof(ElfW(Addr)) * 8; |
| 334 | const uint32_t word_num = (hash / kBloomMaskBits) & gnu_maskwords_; |
| 335 | const ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num]; |
| 336 | const uint32_t h1 = hash % kBloomMaskBits; |
| 337 | const uint32_t h2 = (hash >> gnu_shift2_) % kBloomMaskBits; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 338 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 339 | TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)", |
| 340 | symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base)); |
| 341 | |
| 342 | // test against bloom filter |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 343 | if ((1 & (bloom_word >> h1) & (bloom_word >> h2)) == 0) { |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 344 | TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p", |
| 345 | symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base)); |
| 346 | |
Ryan Prichard | 0e12cce | 2020-01-02 14:59:11 -0800 | [diff] [blame] | 347 | return nullptr; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // bloom test says "probably yes"... |
| 351 | uint32_t n = gnu_bucket_[hash % gnu_nbucket_]; |
| 352 | |
| 353 | if (n == 0) { |
| 354 | TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p", |
| 355 | symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base)); |
| 356 | |
Ryan Prichard | 0e12cce | 2020-01-02 14:59:11 -0800 | [diff] [blame] | 357 | return nullptr; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Ryan Prichard | 0e12cce | 2020-01-02 14:59:11 -0800 | [diff] [blame] | 360 | const ElfW(Versym) verneed = find_verdef_version_index(this, vi); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 361 | const ElfW(Versym)* versym = get_versym_table(); |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 362 | |
| 363 | do { |
| 364 | ElfW(Sym)* s = symtab_ + n; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 365 | if (((gnu_chain_[n] ^ hash) >> 1) == 0 && |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 366 | check_symbol_version(versym, n, verneed) && |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 367 | strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 && |
| 368 | is_symbol_global_and_defined(this, s)) { |
| 369 | TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd", |
| 370 | symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(s->st_value), |
| 371 | static_cast<size_t>(s->st_size)); |
Ryan Prichard | 0e12cce | 2020-01-02 14:59:11 -0800 | [diff] [blame] | 372 | return symtab_ + n; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 373 | } |
| 374 | } while ((gnu_chain_[n++] & 1) == 0); |
| 375 | |
| 376 | TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p", |
| 377 | symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base)); |
| 378 | |
Ryan Prichard | 0e12cce | 2020-01-02 14:59:11 -0800 | [diff] [blame] | 379 | return nullptr; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Ryan Prichard | 0e12cce | 2020-01-02 14:59:11 -0800 | [diff] [blame] | 382 | const ElfW(Sym)* soinfo::elf_lookup(SymbolName& symbol_name, const version_info* vi) const { |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 383 | uint32_t hash = symbol_name.elf_hash(); |
| 384 | |
| 385 | TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd", |
| 386 | symbol_name.get_name(), get_realpath(), |
| 387 | reinterpret_cast<void*>(base), hash, hash % nbucket_); |
| 388 | |
Ryan Prichard | 0e12cce | 2020-01-02 14:59:11 -0800 | [diff] [blame] | 389 | const ElfW(Versym) verneed = find_verdef_version_index(this, vi); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 390 | const ElfW(Versym)* versym = get_versym_table(); |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 391 | |
| 392 | for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) { |
| 393 | ElfW(Sym)* s = symtab_ + n; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 394 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 395 | if (check_symbol_version(versym, n, verneed) && |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 396 | strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 && |
| 397 | is_symbol_global_and_defined(this, s)) { |
| 398 | TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd", |
| 399 | symbol_name.get_name(), get_realpath(), |
| 400 | reinterpret_cast<void*>(s->st_value), |
| 401 | static_cast<size_t>(s->st_size)); |
Ryan Prichard | 0e12cce | 2020-01-02 14:59:11 -0800 | [diff] [blame] | 402 | return symtab_ + n; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
| 406 | TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd", |
| 407 | symbol_name.get_name(), get_realpath(), |
| 408 | reinterpret_cast<void*>(base), hash, hash % nbucket_); |
| 409 | |
Ryan Prichard | 0e12cce | 2020-01-02 14:59:11 -0800 | [diff] [blame] | 410 | return nullptr; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) { |
| 414 | return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr); |
| 415 | } |
| 416 | |
| 417 | static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) { |
Ryan Prichard | e4d620b | 2019-04-01 17:42:14 -0700 | [diff] [blame] | 418 | // Skip TLS symbols. A TLS symbol's value is relative to the start of the TLS segment rather than |
| 419 | // to the start of the solib. The solib only reserves space for the initialized part of the TLS |
| 420 | // segment. (i.e. .tdata is followed by .tbss, and .tbss overlaps other sections.) |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 421 | return sym->st_shndx != SHN_UNDEF && |
Ryan Prichard | e4d620b | 2019-04-01 17:42:14 -0700 | [diff] [blame] | 422 | ELF_ST_TYPE(sym->st_info) != STT_TLS && |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 423 | soaddr >= sym->st_value && |
| 424 | soaddr < sym->st_value + sym->st_size; |
| 425 | } |
| 426 | |
| 427 | ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) { |
| 428 | ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias; |
| 429 | |
| 430 | for (size_t i = 0; i < gnu_nbucket_; ++i) { |
| 431 | uint32_t n = gnu_bucket_[i]; |
| 432 | |
| 433 | if (n == 0) { |
| 434 | continue; |
| 435 | } |
| 436 | |
| 437 | do { |
| 438 | ElfW(Sym)* sym = symtab_ + n; |
| 439 | if (symbol_matches_soaddr(sym, soaddr)) { |
| 440 | return sym; |
| 441 | } |
| 442 | } while ((gnu_chain_[n++] & 1) == 0); |
| 443 | } |
| 444 | |
| 445 | return nullptr; |
| 446 | } |
| 447 | |
| 448 | ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) { |
| 449 | ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias; |
| 450 | |
| 451 | // Search the library's symbol table for any defined symbol which |
| 452 | // contains this address. |
| 453 | for (size_t i = 0; i < nchain_; ++i) { |
| 454 | ElfW(Sym)* sym = symtab_ + i; |
| 455 | if (symbol_matches_soaddr(sym, soaddr)) { |
| 456 | return sym; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | return nullptr; |
| 461 | } |
| 462 | |
| 463 | static void call_function(const char* function_name __unused, |
| 464 | linker_ctor_function_t function, |
| 465 | const char* realpath __unused) { |
| 466 | if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) { |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | TRACE("[ Calling c-tor %s @ %p for '%s' ]", function_name, function, realpath); |
| 471 | function(g_argc, g_argv, g_envp); |
| 472 | TRACE("[ Done calling c-tor %s @ %p for '%s' ]", function_name, function, realpath); |
| 473 | } |
| 474 | |
| 475 | static void call_function(const char* function_name __unused, |
| 476 | linker_dtor_function_t function, |
| 477 | const char* realpath __unused) { |
| 478 | if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) { |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | TRACE("[ Calling d-tor %s @ %p for '%s' ]", function_name, function, realpath); |
| 483 | function(); |
| 484 | TRACE("[ Done calling d-tor %s @ %p for '%s' ]", function_name, function, realpath); |
| 485 | } |
| 486 | |
| 487 | template <typename F> |
| 488 | static void call_array(const char* array_name __unused, |
| 489 | F* functions, |
| 490 | size_t count, |
| 491 | bool reverse, |
| 492 | const char* realpath) { |
| 493 | if (functions == nullptr) { |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, realpath); |
| 498 | |
| 499 | int begin = reverse ? (count - 1) : 0; |
| 500 | int end = reverse ? -1 : count; |
| 501 | int step = reverse ? -1 : 1; |
| 502 | |
| 503 | for (int i = begin; i != end; i += step) { |
| 504 | TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]); |
| 505 | call_function("function", functions[i], realpath); |
| 506 | } |
| 507 | |
| 508 | TRACE("[ Done calling %s for '%s' ]", array_name, realpath); |
| 509 | } |
| 510 | |
| 511 | void soinfo::call_pre_init_constructors() { |
Elliott Hughes | 90f96b9 | 2019-05-09 15:56:39 -0700 | [diff] [blame] | 512 | if (g_is_ldd) return; |
| 513 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 514 | // DT_PREINIT_ARRAY functions are called before any other constructors for executables, |
| 515 | // but ignored in a shared library. |
| 516 | call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false, get_realpath()); |
| 517 | } |
| 518 | |
| 519 | void soinfo::call_constructors() { |
Elliott Hughes | 90f96b9 | 2019-05-09 15:56:39 -0700 | [diff] [blame] | 520 | if (constructors_called || g_is_ldd) { |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 521 | return; |
| 522 | } |
| 523 | |
| 524 | // We set constructors_called before actually calling the constructors, otherwise it doesn't |
| 525 | // protect against recursive constructor calls. One simple example of constructor recursion |
| 526 | // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so: |
| 527 | // 1. The program depends on libc, so libc's constructor is called here. |
| 528 | // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so. |
| 529 | // 3. dlopen() calls the constructors on the newly created |
| 530 | // soinfo for libc_malloc_debug_leak.so. |
| 531 | // 4. The debug .so depends on libc, so CallConstructors is |
| 532 | // called again with the libc soinfo. If it doesn't trigger the early- |
| 533 | // out above, the libc constructor will be called again (recursively!). |
| 534 | constructors_called = true; |
| 535 | |
| 536 | if (!is_main_executable() && preinit_array_ != nullptr) { |
| 537 | // The GNU dynamic linker silently ignores these, but we warn the developer. |
| 538 | PRINT("\"%s\": ignoring DT_PREINIT_ARRAY in shared library!", get_realpath()); |
| 539 | } |
| 540 | |
| 541 | get_children().for_each([] (soinfo* si) { |
| 542 | si->call_constructors(); |
| 543 | }); |
| 544 | |
Dimitry Ivanov | 5c4a580 | 2017-03-17 16:41:34 -0700 | [diff] [blame] | 545 | if (!is_linker()) { |
| 546 | bionic_trace_begin((std::string("calling constructors: ") + get_realpath()).c_str()); |
| 547 | } |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 548 | |
| 549 | // DT_INIT should be called before DT_INIT_ARRAY if both are present. |
| 550 | call_function("DT_INIT", init_func_, get_realpath()); |
| 551 | call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false, get_realpath()); |
Dimitry Ivanov | 5c4a580 | 2017-03-17 16:41:34 -0700 | [diff] [blame] | 552 | |
| 553 | if (!is_linker()) { |
| 554 | bionic_trace_end(); |
| 555 | } |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | void soinfo::call_destructors() { |
| 559 | if (!constructors_called) { |
| 560 | return; |
| 561 | } |
Dimitry Ivanov | 6705e8c | 2017-03-21 10:29:06 -0700 | [diff] [blame] | 562 | |
| 563 | ScopedTrace trace((std::string("calling destructors: ") + get_realpath()).c_str()); |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 564 | |
| 565 | // DT_FINI_ARRAY must be parsed in reverse order. |
| 566 | call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true, get_realpath()); |
| 567 | |
| 568 | // DT_FINI should be called after DT_FINI_ARRAY if both are present. |
| 569 | call_function("DT_FINI", fini_func_, get_realpath()); |
| 570 | } |
| 571 | |
| 572 | void soinfo::add_child(soinfo* child) { |
| 573 | if (has_min_version(0)) { |
| 574 | child->parents_.push_back(this); |
| 575 | this->children_.push_back(child); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | void soinfo::remove_all_links() { |
| 580 | if (!has_min_version(0)) { |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | // 1. Untie connected soinfos from 'this'. |
| 585 | children_.for_each([&] (soinfo* child) { |
| 586 | child->parents_.remove_if([&] (const soinfo* parent) { |
| 587 | return parent == this; |
| 588 | }); |
| 589 | }); |
| 590 | |
| 591 | parents_.for_each([&] (soinfo* parent) { |
| 592 | parent->children_.remove_if([&] (const soinfo* child) { |
| 593 | return child == this; |
| 594 | }); |
| 595 | }); |
| 596 | |
| 597 | // 2. Remove from the primary namespace |
| 598 | primary_namespace_->remove_soinfo(this); |
| 599 | primary_namespace_ = nullptr; |
| 600 | |
| 601 | // 3. Remove from secondary namespaces |
| 602 | secondary_namespaces_.for_each([&](android_namespace_t* ns) { |
| 603 | ns->remove_soinfo(this); |
| 604 | }); |
| 605 | |
| 606 | |
| 607 | // 4. Once everything untied - clear local lists. |
| 608 | parents_.clear(); |
| 609 | children_.clear(); |
| 610 | secondary_namespaces_.clear(); |
| 611 | } |
| 612 | |
| 613 | dev_t soinfo::get_st_dev() const { |
| 614 | if (has_min_version(0)) { |
| 615 | return st_dev_; |
| 616 | } |
| 617 | |
| 618 | return 0; |
| 619 | }; |
| 620 | |
| 621 | ino_t soinfo::get_st_ino() const { |
| 622 | if (has_min_version(0)) { |
| 623 | return st_ino_; |
| 624 | } |
| 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | off64_t soinfo::get_file_offset() const { |
| 630 | if (has_min_version(1)) { |
| 631 | return file_offset_; |
| 632 | } |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | uint32_t soinfo::get_rtld_flags() const { |
| 638 | if (has_min_version(1)) { |
| 639 | return rtld_flags_; |
| 640 | } |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | uint32_t soinfo::get_dt_flags_1() const { |
| 646 | if (has_min_version(1)) { |
| 647 | return dt_flags_1_; |
| 648 | } |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | void soinfo::set_dt_flags_1(uint32_t dt_flags_1) { |
| 654 | if (has_min_version(1)) { |
| 655 | if ((dt_flags_1 & DF_1_GLOBAL) != 0) { |
| 656 | rtld_flags_ |= RTLD_GLOBAL; |
| 657 | } |
| 658 | |
| 659 | if ((dt_flags_1 & DF_1_NODELETE) != 0) { |
| 660 | rtld_flags_ |= RTLD_NODELETE; |
| 661 | } |
| 662 | |
| 663 | dt_flags_1_ = dt_flags_1; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | void soinfo::set_nodelete() { |
| 668 | rtld_flags_ |= RTLD_NODELETE; |
| 669 | } |
| 670 | |
Ryan Prichard | cf9ed12 | 2019-06-04 20:56:56 -0700 | [diff] [blame] | 671 | void soinfo::set_realpath(const char* path) { |
| 672 | #if defined(__work_around_b_24465209__) |
| 673 | if (has_min_version(2)) { |
| 674 | realpath_ = path; |
| 675 | } |
| 676 | #else |
| 677 | realpath_ = path; |
| 678 | #endif |
| 679 | } |
| 680 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 681 | const char* soinfo::get_realpath() const { |
| 682 | #if defined(__work_around_b_24465209__) |
| 683 | if (has_min_version(2)) { |
| 684 | return realpath_.c_str(); |
| 685 | } else { |
| 686 | return old_name_; |
| 687 | } |
| 688 | #else |
| 689 | return realpath_.c_str(); |
| 690 | #endif |
| 691 | } |
| 692 | |
| 693 | void soinfo::set_soname(const char* soname) { |
| 694 | #if defined(__work_around_b_24465209__) |
| 695 | if (has_min_version(2)) { |
| 696 | soname_ = soname; |
| 697 | } |
Elliott Hughes | f9dd1a7 | 2021-01-11 09:04:58 -0800 | [diff] [blame] | 698 | strlcpy(old_name_, soname_.c_str(), sizeof(old_name_)); |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 699 | #else |
| 700 | soname_ = soname; |
| 701 | #endif |
| 702 | } |
| 703 | |
| 704 | const char* soinfo::get_soname() const { |
| 705 | #if defined(__work_around_b_24465209__) |
| 706 | if (has_min_version(2)) { |
Elliott Hughes | f9dd1a7 | 2021-01-11 09:04:58 -0800 | [diff] [blame] | 707 | return soname_.c_str(); |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 708 | } else { |
| 709 | return old_name_; |
| 710 | } |
| 711 | #else |
Elliott Hughes | f9dd1a7 | 2021-01-11 09:04:58 -0800 | [diff] [blame] | 712 | return soname_.c_str(); |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 713 | #endif |
| 714 | } |
| 715 | |
| 716 | // This is a return on get_children()/get_parents() if |
| 717 | // 'this->flags' does not have FLAG_NEW_SOINFO set. |
| 718 | static soinfo_list_t g_empty_list; |
| 719 | |
| 720 | soinfo_list_t& soinfo::get_children() { |
| 721 | if (has_min_version(0)) { |
| 722 | return children_; |
| 723 | } |
| 724 | |
| 725 | return g_empty_list; |
| 726 | } |
| 727 | |
| 728 | const soinfo_list_t& soinfo::get_children() const { |
| 729 | if (has_min_version(0)) { |
| 730 | return children_; |
| 731 | } |
| 732 | |
| 733 | return g_empty_list; |
| 734 | } |
| 735 | |
| 736 | soinfo_list_t& soinfo::get_parents() { |
| 737 | if (has_min_version(0)) { |
| 738 | return parents_; |
| 739 | } |
| 740 | |
| 741 | return g_empty_list; |
| 742 | } |
| 743 | |
| 744 | static std::vector<std::string> g_empty_runpath; |
| 745 | |
| 746 | const std::vector<std::string>& soinfo::get_dt_runpath() const { |
| 747 | if (has_min_version(3)) { |
| 748 | return dt_runpath_; |
| 749 | } |
| 750 | |
| 751 | return g_empty_runpath; |
| 752 | } |
| 753 | |
| 754 | android_namespace_t* soinfo::get_primary_namespace() { |
| 755 | if (has_min_version(3)) { |
| 756 | return primary_namespace_; |
| 757 | } |
| 758 | |
| 759 | return &g_default_namespace; |
| 760 | } |
| 761 | |
| 762 | void soinfo::add_secondary_namespace(android_namespace_t* secondary_ns) { |
| 763 | CHECK(has_min_version(3)); |
| 764 | secondary_namespaces_.push_back(secondary_ns); |
| 765 | } |
| 766 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 767 | android_namespace_list_t& soinfo::get_secondary_namespaces() { |
| 768 | CHECK(has_min_version(3)); |
| 769 | return secondary_namespaces_; |
| 770 | } |
| 771 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 772 | const char* soinfo::get_string(ElfW(Word) index) const { |
| 773 | if (has_min_version(1) && (index >= strtab_size_)) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 774 | async_safe_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 775 | get_realpath(), strtab_size_, index); |
| 776 | } |
| 777 | |
| 778 | return strtab_ + index; |
| 779 | } |
| 780 | |
| 781 | bool soinfo::is_gnu_hash() const { |
| 782 | return (flags_ & FLAG_GNU_HASH) != 0; |
| 783 | } |
| 784 | |
| 785 | bool soinfo::can_unload() const { |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 786 | return !is_linked() || |
| 787 | ( |
dimitry | 55547db | 2018-05-25 14:17:37 +0200 | [diff] [blame] | 788 | (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0 |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 789 | ); |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | bool soinfo::is_linked() const { |
| 793 | return (flags_ & FLAG_LINKED) != 0; |
| 794 | } |
| 795 | |
dimitry | 965d06d | 2017-11-28 16:03:07 +0100 | [diff] [blame] | 796 | bool soinfo::is_image_linked() const { |
| 797 | return (flags_ & FLAG_IMAGE_LINKED) != 0; |
| 798 | } |
| 799 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 800 | bool soinfo::is_main_executable() const { |
| 801 | return (flags_ & FLAG_EXE) != 0; |
| 802 | } |
| 803 | |
| 804 | bool soinfo::is_linker() const { |
| 805 | return (flags_ & FLAG_LINKER) != 0; |
| 806 | } |
| 807 | |
| 808 | void soinfo::set_linked() { |
| 809 | flags_ |= FLAG_LINKED; |
| 810 | } |
| 811 | |
dimitry | 965d06d | 2017-11-28 16:03:07 +0100 | [diff] [blame] | 812 | void soinfo::set_image_linked() { |
| 813 | flags_ |= FLAG_IMAGE_LINKED; |
| 814 | } |
| 815 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 816 | void soinfo::set_linker_flag() { |
| 817 | flags_ |= FLAG_LINKER; |
| 818 | } |
| 819 | |
| 820 | void soinfo::set_main_executable() { |
| 821 | flags_ |= FLAG_EXE; |
| 822 | } |
| 823 | |
dimitry | 965d06d | 2017-11-28 16:03:07 +0100 | [diff] [blame] | 824 | size_t soinfo::increment_ref_count() { |
| 825 | return ++local_group_root_->ref_count_; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | size_t soinfo::decrement_ref_count() { |
| 829 | return --local_group_root_->ref_count_; |
| 830 | } |
| 831 | |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 832 | size_t soinfo::get_ref_count() const { |
| 833 | return local_group_root_->ref_count_; |
| 834 | } |
| 835 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 836 | soinfo* soinfo::get_local_group_root() const { |
| 837 | return local_group_root_; |
| 838 | } |
| 839 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 840 | void soinfo::set_mapped_by_caller(bool mapped_by_caller) { |
| 841 | if (mapped_by_caller) { |
| 842 | flags_ |= FLAG_MAPPED_BY_CALLER; |
| 843 | } else { |
| 844 | flags_ &= ~FLAG_MAPPED_BY_CALLER; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | bool soinfo::is_mapped_by_caller() const { |
| 849 | return (flags_ & FLAG_MAPPED_BY_CALLER) != 0; |
| 850 | } |
| 851 | |
| 852 | // This function returns api-level at the time of |
| 853 | // dlopen/load. Note that libraries opened by system |
| 854 | // will always have 'current' api level. |
Elliott Hughes | ff1428a | 2018-11-12 16:01:37 -0800 | [diff] [blame] | 855 | int soinfo::get_target_sdk_version() const { |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 856 | if (!has_min_version(2)) { |
| 857 | return __ANDROID_API__; |
| 858 | } |
| 859 | |
| 860 | return local_group_root_->target_sdk_version_; |
| 861 | } |
| 862 | |
| 863 | uintptr_t soinfo::get_handle() const { |
| 864 | CHECK(has_min_version(3)); |
| 865 | CHECK(handle_ != 0); |
| 866 | return handle_; |
| 867 | } |
| 868 | |
| 869 | void* soinfo::to_handle() { |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 870 | if (get_application_target_sdk_version() < 24 || !has_min_version(3)) { |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 871 | return this; |
| 872 | } |
| 873 | |
| 874 | return reinterpret_cast<void*>(get_handle()); |
| 875 | } |
| 876 | |
| 877 | void soinfo::generate_handle() { |
| 878 | CHECK(has_min_version(3)); |
| 879 | CHECK(handle_ == 0); // Make sure this is the first call |
| 880 | |
| 881 | // Make sure the handle is unique and does not collide |
| 882 | // with special values which are RTLD_DEFAULT and RTLD_NEXT. |
| 883 | do { |
Tom Cherry | 66bc428 | 2018-11-08 13:40:52 -0800 | [diff] [blame] | 884 | if (!is_first_stage_init()) { |
Jiyong Park | 31cd08f | 2018-06-01 19:18:56 +0900 | [diff] [blame] | 885 | arc4random_buf(&handle_, sizeof(handle_)); |
| 886 | } else { |
| 887 | // arc4random* is not available in init because /dev/urandom hasn't yet been |
| 888 | // created. So, when running with init, use the monotonically increasing |
| 889 | // numbers as handles |
| 890 | handle_ += 2; |
| 891 | } |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 892 | // the least significant bit for the handle is always 1 |
| 893 | // making it easy to test the type of handle passed to |
| 894 | // dl* functions. |
| 895 | handle_ = handle_ | 1; |
| 896 | } while (handle_ == reinterpret_cast<uintptr_t>(RTLD_DEFAULT) || |
| 897 | handle_ == reinterpret_cast<uintptr_t>(RTLD_NEXT) || |
| 898 | g_soinfo_handles_map.find(handle_) != g_soinfo_handles_map.end()); |
| 899 | |
| 900 | g_soinfo_handles_map[handle_] = this; |
| 901 | } |
| 902 | |
Evgenii Stepanov | e0848bb | 2020-07-14 16:44:57 -0700 | [diff] [blame] | 903 | void soinfo::set_gap_start(ElfW(Addr) gap_start) { |
| 904 | CHECK(has_min_version(6)); |
| 905 | gap_start_ = gap_start; |
| 906 | } |
| 907 | ElfW(Addr) soinfo::get_gap_start() const { |
| 908 | CHECK(has_min_version(6)); |
| 909 | return gap_start_; |
| 910 | } |
| 911 | |
| 912 | void soinfo::set_gap_size(size_t gap_size) { |
| 913 | CHECK(has_min_version(6)); |
| 914 | gap_size_ = gap_size; |
| 915 | } |
| 916 | size_t soinfo::get_gap_size() const { |
| 917 | CHECK(has_min_version(6)); |
| 918 | return gap_size_; |
| 919 | } |
| 920 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 921 | // TODO(dimitry): Move SymbolName methods to a separate file. |
| 922 | |
| 923 | uint32_t calculate_elf_hash(const char* name) { |
| 924 | const uint8_t* name_bytes = reinterpret_cast<const uint8_t*>(name); |
| 925 | uint32_t h = 0, g; |
| 926 | |
| 927 | while (*name_bytes) { |
| 928 | h = (h << 4) + *name_bytes++; |
| 929 | g = h & 0xf0000000; |
| 930 | h ^= g; |
| 931 | h ^= g >> 24; |
| 932 | } |
| 933 | |
| 934 | return h; |
| 935 | } |
| 936 | |
| 937 | uint32_t SymbolName::elf_hash() { |
| 938 | if (!has_elf_hash_) { |
| 939 | elf_hash_ = calculate_elf_hash(name_); |
| 940 | has_elf_hash_ = true; |
| 941 | } |
| 942 | |
| 943 | return elf_hash_; |
| 944 | } |
| 945 | |
| 946 | uint32_t SymbolName::gnu_hash() { |
| 947 | if (!has_gnu_hash_) { |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 948 | gnu_hash_ = calculate_gnu_hash(name_).first; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 949 | has_gnu_hash_ = true; |
| 950 | } |
| 951 | |
| 952 | return gnu_hash_; |
| 953 | } |