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