blob: 3e7506cbb48b96b0784e2e0789c711f676025c10 [file] [log] [blame]
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07001/*
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 Ferris7a3681e2017-04-24 17:48:32 -070037#include <async_safe/log.h>
38
Ryan Prichard0e12cce2020-01-02 14:59:11 -080039#include "linker.h"
Ryan Prichard4d4087d2019-12-02 16:55:48 -080040#include "linker_config.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070041#include "linker_debug.h"
42#include "linker_globals.h"
Ryan Prichard339ecef2020-01-02 16:36:06 -080043#include "linker_gnu_hash.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070044#include "linker_logger.h"
Ryan Prichard339ecef2020-01-02 16:36:06 -080045#include "linker_relocate.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070046#include "linker_utils.h"
47
Ryan Prichard339ecef2020-01-02 16:36:06 -080048// Enable the slow lookup path if symbol lookups should be logged.
49static bool is_lookup_tracing_enabled() {
50 return g_ld_debug_verbosity > LINKER_VERBOSITY_TRACE && DO_TRACE_LOOKUP;
51}
52
53SymbolLookupList::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
60SymbolLookupList::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 */
92void 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.)
108static 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
117template <bool IsGeneral>
118__attribute__((noinline)) static const ElfW(Sym)*
119soinfo_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
207const 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 Ivanov48ec2882016-08-04 11:50:36 -0700213
Elliott Hughesb003e102023-09-29 17:07:27 +0000214soinfo::soinfo(android_namespace_t* ns, const char* realpath, const struct stat* file_stat,
215 off64_t file_offset, int rtld_flags) {
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700216 if (realpath != nullptr) {
217 realpath_ = realpath;
218 }
219
220 flags_ = FLAG_NEW_SOINFO;
221 version_ = SOINFO_VERSION;
222
223 if (file_stat != nullptr) {
224 this->st_dev_ = file_stat->st_dev;
225 this->st_ino_ = file_stat->st_ino;
226 this->file_offset_ = file_offset;
227 }
228
229 this->rtld_flags_ = rtld_flags;
230 this->primary_namespace_ = ns;
231}
232
233soinfo::~soinfo() {
234 g_soinfo_handles_map.erase(handle_);
235}
236
237void soinfo::set_dt_runpath(const char* path) {
238 if (!has_min_version(3)) {
239 return;
240 }
241
242 std::vector<std::string> runpaths;
243
244 split_path(path, ":", &runpaths);
245
246 std::string origin = dirname(get_realpath());
Jiyong Park57b9d1e2019-01-17 03:14:45 +0900247 // FIXME: add $PLATFORM.
248 std::vector<std::pair<std::string, std::string>> params = {
249 {"ORIGIN", origin},
Ryan Prichard4d4087d2019-12-02 16:55:48 -0800250 {"LIB", kLibPath},
Jiyong Park57b9d1e2019-01-17 03:14:45 +0900251 };
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700252 for (auto&& s : runpaths) {
Dimitry Ivanov2a6d9b22017-03-11 14:35:38 -0800253 format_string(&s, params);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700254 }
255
256 resolve_paths(runpaths, &dt_runpath_);
257}
258
259const ElfW(Versym)* soinfo::get_versym(size_t n) const {
Ryan Prichard339ecef2020-01-02 16:36:06 -0800260 auto table = get_versym_table();
261 return table ? table + n : nullptr;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700262}
263
264ElfW(Addr) soinfo::get_verneed_ptr() const {
265 if (has_min_version(2)) {
266 return verneed_ptr_;
267 }
268
269 return 0;
270}
271
272size_t soinfo::get_verneed_cnt() const {
273 if (has_min_version(2)) {
274 return verneed_cnt_;
275 }
276
277 return 0;
278}
279
280ElfW(Addr) soinfo::get_verdef_ptr() const {
281 if (has_min_version(2)) {
282 return verdef_ptr_;
283 }
284
285 return 0;
286}
287
288size_t soinfo::get_verdef_cnt() const {
289 if (has_min_version(2)) {
290 return verdef_cnt_;
291 }
292
293 return 0;
294}
295
Ryan Prichard339ecef2020-01-02 16:36:06 -0800296SymbolLookupLib soinfo::get_lookup_lib() {
297 SymbolLookupLib result {};
298 result.si_ = this;
299
300 // For libs that only have SysV hashes, leave the gnu_bloom_filter_ field NULL to signal that
301 // the fallback code path is needed.
302 if (!is_gnu_hash()) {
303 return result;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700304 }
305
Ryan Prichard339ecef2020-01-02 16:36:06 -0800306 result.gnu_maskwords_ = gnu_maskwords_;
307 result.gnu_shift2_ = gnu_shift2_;
308 result.gnu_bloom_filter_ = gnu_bloom_filter_;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700309
Ryan Prichard339ecef2020-01-02 16:36:06 -0800310 result.strtab_ = strtab_;
311 result.strtab_size_ = strtab_size_;
312 result.symtab_ = symtab_;
313 result.versym_ = get_versym_table();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700314
Ryan Prichard339ecef2020-01-02 16:36:06 -0800315 result.gnu_chain_ = gnu_chain_;
316 result.gnu_nbucket_ = gnu_nbucket_;
317 result.gnu_bucket_ = gnu_bucket_;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700318
Ryan Prichard339ecef2020-01-02 16:36:06 -0800319 return result;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700320}
321
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800322const ElfW(Sym)* soinfo::find_symbol_by_name(SymbolName& symbol_name,
323 const version_info* vi) const {
324 return is_gnu_hash() ? gnu_lookup(symbol_name, vi) : elf_lookup(symbol_name, vi);
325}
326
327const ElfW(Sym)* soinfo::gnu_lookup(SymbolName& symbol_name, const version_info* vi) const {
Ryan Prichard339ecef2020-01-02 16:36:06 -0800328 const uint32_t hash = symbol_name.gnu_hash();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700329
Ryan Prichard339ecef2020-01-02 16:36:06 -0800330 constexpr uint32_t kBloomMaskBits = sizeof(ElfW(Addr)) * 8;
331 const uint32_t word_num = (hash / kBloomMaskBits) & gnu_maskwords_;
332 const ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
333 const uint32_t h1 = hash % kBloomMaskBits;
334 const uint32_t h2 = (hash >> gnu_shift2_) % kBloomMaskBits;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700335
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700336 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
337 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
338
339 // test against bloom filter
Ryan Prichard339ecef2020-01-02 16:36:06 -0800340 if ((1 & (bloom_word >> h1) & (bloom_word >> h2)) == 0) {
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700341 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
342 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
343
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800344 return nullptr;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700345 }
346
347 // bloom test says "probably yes"...
348 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
349
350 if (n == 0) {
351 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
352 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
353
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800354 return nullptr;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700355 }
356
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800357 const ElfW(Versym) verneed = find_verdef_version_index(this, vi);
Ryan Prichard339ecef2020-01-02 16:36:06 -0800358 const ElfW(Versym)* versym = get_versym_table();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700359
360 do {
361 ElfW(Sym)* s = symtab_ + n;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700362 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Ryan Prichard339ecef2020-01-02 16:36:06 -0800363 check_symbol_version(versym, n, verneed) &&
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700364 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
365 is_symbol_global_and_defined(this, s)) {
366 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
367 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(s->st_value),
368 static_cast<size_t>(s->st_size));
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800369 return symtab_ + n;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700370 }
371 } while ((gnu_chain_[n++] & 1) == 0);
372
373 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
374 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
375
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800376 return nullptr;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700377}
378
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800379const ElfW(Sym)* soinfo::elf_lookup(SymbolName& symbol_name, const version_info* vi) const {
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700380 uint32_t hash = symbol_name.elf_hash();
381
382 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
383 symbol_name.get_name(), get_realpath(),
384 reinterpret_cast<void*>(base), hash, hash % nbucket_);
385
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800386 const ElfW(Versym) verneed = find_verdef_version_index(this, vi);
Ryan Prichard339ecef2020-01-02 16:36:06 -0800387 const ElfW(Versym)* versym = get_versym_table();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700388
389 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
390 ElfW(Sym)* s = symtab_ + n;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700391
Ryan Prichard339ecef2020-01-02 16:36:06 -0800392 if (check_symbol_version(versym, n, verneed) &&
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700393 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
394 is_symbol_global_and_defined(this, s)) {
395 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
396 symbol_name.get_name(), get_realpath(),
397 reinterpret_cast<void*>(s->st_value),
398 static_cast<size_t>(s->st_size));
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800399 return symtab_ + n;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700400 }
401 }
402
403 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
404 symbol_name.get_name(), get_realpath(),
405 reinterpret_cast<void*>(base), hash, hash % nbucket_);
406
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800407 return nullptr;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700408}
409
410ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
411 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
412}
413
414static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
Ryan Pricharde4d620b2019-04-01 17:42:14 -0700415 // Skip TLS symbols. A TLS symbol's value is relative to the start of the TLS segment rather than
416 // to the start of the solib. The solib only reserves space for the initialized part of the TLS
417 // segment. (i.e. .tdata is followed by .tbss, and .tbss overlaps other sections.)
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700418 return sym->st_shndx != SHN_UNDEF &&
Ryan Pricharde4d620b2019-04-01 17:42:14 -0700419 ELF_ST_TYPE(sym->st_info) != STT_TLS &&
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700420 soaddr >= sym->st_value &&
421 soaddr < sym->st_value + sym->st_size;
422}
423
424ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
425 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
426
427 for (size_t i = 0; i < gnu_nbucket_; ++i) {
428 uint32_t n = gnu_bucket_[i];
429
430 if (n == 0) {
431 continue;
432 }
433
434 do {
435 ElfW(Sym)* sym = symtab_ + n;
436 if (symbol_matches_soaddr(sym, soaddr)) {
437 return sym;
438 }
439 } while ((gnu_chain_[n++] & 1) == 0);
440 }
441
442 return nullptr;
443}
444
445ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
446 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
447
448 // Search the library's symbol table for any defined symbol which
449 // contains this address.
450 for (size_t i = 0; i < nchain_; ++i) {
451 ElfW(Sym)* sym = symtab_ + i;
452 if (symbol_matches_soaddr(sym, soaddr)) {
453 return sym;
454 }
455 }
456
457 return nullptr;
458}
459
460static void call_function(const char* function_name __unused,
461 linker_ctor_function_t function,
462 const char* realpath __unused) {
463 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
464 return;
465 }
466
467 TRACE("[ Calling c-tor %s @ %p for '%s' ]", function_name, function, realpath);
468 function(g_argc, g_argv, g_envp);
469 TRACE("[ Done calling c-tor %s @ %p for '%s' ]", function_name, function, realpath);
470}
471
472static void call_function(const char* function_name __unused,
473 linker_dtor_function_t function,
474 const char* realpath __unused) {
475 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
476 return;
477 }
478
479 TRACE("[ Calling d-tor %s @ %p for '%s' ]", function_name, function, realpath);
480 function();
481 TRACE("[ Done calling d-tor %s @ %p for '%s' ]", function_name, function, realpath);
482}
483
484template <typename F>
Elliott Hughes01be44d2021-01-19 09:41:23 -0800485static inline void call_array(const char* array_name __unused, F* functions, size_t count,
486 bool reverse, const char* realpath) {
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700487 if (functions == nullptr) {
488 return;
489 }
490
491 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, realpath);
492
493 int begin = reverse ? (count - 1) : 0;
494 int end = reverse ? -1 : count;
495 int step = reverse ? -1 : 1;
496
497 for (int i = begin; i != end; i += step) {
498 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
499 call_function("function", functions[i], realpath);
500 }
501
502 TRACE("[ Done calling %s for '%s' ]", array_name, realpath);
503}
504
505void soinfo::call_pre_init_constructors() {
Elliott Hughes90f96b92019-05-09 15:56:39 -0700506 if (g_is_ldd) return;
507
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700508 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
509 // but ignored in a shared library.
510 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false, get_realpath());
511}
512
513void soinfo::call_constructors() {
Elliott Hughes90f96b92019-05-09 15:56:39 -0700514 if (constructors_called || g_is_ldd) {
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700515 return;
516 }
517
518 // We set constructors_called before actually calling the constructors, otherwise it doesn't
519 // protect against recursive constructor calls. One simple example of constructor recursion
520 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
521 // 1. The program depends on libc, so libc's constructor is called here.
522 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
523 // 3. dlopen() calls the constructors on the newly created
524 // soinfo for libc_malloc_debug_leak.so.
525 // 4. The debug .so depends on libc, so CallConstructors is
526 // called again with the libc soinfo. If it doesn't trigger the early-
527 // out above, the libc constructor will be called again (recursively!).
528 constructors_called = true;
529
530 if (!is_main_executable() && preinit_array_ != nullptr) {
531 // The GNU dynamic linker silently ignores these, but we warn the developer.
532 PRINT("\"%s\": ignoring DT_PREINIT_ARRAY in shared library!", get_realpath());
533 }
534
535 get_children().for_each([] (soinfo* si) {
536 si->call_constructors();
537 });
538
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -0700539 if (!is_linker()) {
540 bionic_trace_begin((std::string("calling constructors: ") + get_realpath()).c_str());
541 }
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700542
543 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
544 call_function("DT_INIT", init_func_, get_realpath());
545 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false, get_realpath());
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -0700546
547 if (!is_linker()) {
548 bionic_trace_end();
549 }
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700550}
551
552void soinfo::call_destructors() {
553 if (!constructors_called) {
554 return;
555 }
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -0700556
557 ScopedTrace trace((std::string("calling destructors: ") + get_realpath()).c_str());
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700558
559 // DT_FINI_ARRAY must be parsed in reverse order.
560 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true, get_realpath());
561
562 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
563 call_function("DT_FINI", fini_func_, get_realpath());
564}
565
566void soinfo::add_child(soinfo* child) {
567 if (has_min_version(0)) {
568 child->parents_.push_back(this);
569 this->children_.push_back(child);
570 }
571}
572
573void soinfo::remove_all_links() {
574 if (!has_min_version(0)) {
575 return;
576 }
577
578 // 1. Untie connected soinfos from 'this'.
579 children_.for_each([&] (soinfo* child) {
580 child->parents_.remove_if([&] (const soinfo* parent) {
581 return parent == this;
582 });
583 });
584
585 parents_.for_each([&] (soinfo* parent) {
586 parent->children_.remove_if([&] (const soinfo* child) {
587 return child == this;
588 });
589 });
590
591 // 2. Remove from the primary namespace
592 primary_namespace_->remove_soinfo(this);
593 primary_namespace_ = nullptr;
594
595 // 3. Remove from secondary namespaces
596 secondary_namespaces_.for_each([&](android_namespace_t* ns) {
597 ns->remove_soinfo(this);
598 });
599
600
601 // 4. Once everything untied - clear local lists.
602 parents_.clear();
603 children_.clear();
604 secondary_namespaces_.clear();
605}
606
607dev_t soinfo::get_st_dev() const {
608 if (has_min_version(0)) {
609 return st_dev_;
610 }
611
612 return 0;
613};
614
615ino_t soinfo::get_st_ino() const {
616 if (has_min_version(0)) {
617 return st_ino_;
618 }
619
620 return 0;
621}
622
623off64_t soinfo::get_file_offset() const {
624 if (has_min_version(1)) {
625 return file_offset_;
626 }
627
628 return 0;
629}
630
631uint32_t soinfo::get_rtld_flags() const {
632 if (has_min_version(1)) {
633 return rtld_flags_;
634 }
635
636 return 0;
637}
638
639uint32_t soinfo::get_dt_flags_1() const {
640 if (has_min_version(1)) {
641 return dt_flags_1_;
642 }
643
644 return 0;
645}
646
647void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
648 if (has_min_version(1)) {
649 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
650 rtld_flags_ |= RTLD_GLOBAL;
651 }
652
653 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
654 rtld_flags_ |= RTLD_NODELETE;
655 }
656
657 dt_flags_1_ = dt_flags_1;
658 }
659}
660
661void soinfo::set_nodelete() {
662 rtld_flags_ |= RTLD_NODELETE;
663}
664
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700665void soinfo::set_realpath(const char* path) {
666#if defined(__work_around_b_24465209__)
667 if (has_min_version(2)) {
668 realpath_ = path;
669 }
670#else
671 realpath_ = path;
672#endif
673}
674
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700675const char* soinfo::get_realpath() const {
676#if defined(__work_around_b_24465209__)
677 if (has_min_version(2)) {
678 return realpath_.c_str();
679 } else {
680 return old_name_;
681 }
682#else
683 return realpath_.c_str();
684#endif
685}
686
687void soinfo::set_soname(const char* soname) {
688#if defined(__work_around_b_24465209__)
689 if (has_min_version(2)) {
690 soname_ = soname;
691 }
Elliott Hughesf9dd1a72021-01-11 09:04:58 -0800692 strlcpy(old_name_, soname_.c_str(), sizeof(old_name_));
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700693#else
694 soname_ = soname;
695#endif
696}
697
698const char* soinfo::get_soname() const {
699#if defined(__work_around_b_24465209__)
700 if (has_min_version(2)) {
Elliott Hughesf9dd1a72021-01-11 09:04:58 -0800701 return soname_.c_str();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700702 } else {
703 return old_name_;
704 }
705#else
Elliott Hughesf9dd1a72021-01-11 09:04:58 -0800706 return soname_.c_str();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700707#endif
708}
709
710// This is a return on get_children()/get_parents() if
711// 'this->flags' does not have FLAG_NEW_SOINFO set.
712static soinfo_list_t g_empty_list;
713
714soinfo_list_t& soinfo::get_children() {
715 if (has_min_version(0)) {
716 return children_;
717 }
718
719 return g_empty_list;
720}
721
722const soinfo_list_t& soinfo::get_children() const {
723 if (has_min_version(0)) {
724 return children_;
725 }
726
727 return g_empty_list;
728}
729
730soinfo_list_t& soinfo::get_parents() {
731 if (has_min_version(0)) {
732 return parents_;
733 }
734
735 return g_empty_list;
736}
737
738static std::vector<std::string> g_empty_runpath;
739
740const std::vector<std::string>& soinfo::get_dt_runpath() const {
741 if (has_min_version(3)) {
742 return dt_runpath_;
743 }
744
745 return g_empty_runpath;
746}
747
748android_namespace_t* soinfo::get_primary_namespace() {
749 if (has_min_version(3)) {
750 return primary_namespace_;
751 }
752
753 return &g_default_namespace;
754}
755
756void soinfo::add_secondary_namespace(android_namespace_t* secondary_ns) {
757 CHECK(has_min_version(3));
758 secondary_namespaces_.push_back(secondary_ns);
759}
760
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800761android_namespace_list_t& soinfo::get_secondary_namespaces() {
762 CHECK(has_min_version(3));
763 return secondary_namespaces_;
764}
765
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700766const char* soinfo::get_string(ElfW(Word) index) const {
767 if (has_min_version(1) && (index >= strtab_size_)) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700768 async_safe_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700769 get_realpath(), strtab_size_, index);
770 }
771
772 return strtab_ + index;
773}
774
775bool soinfo::is_gnu_hash() const {
776 return (flags_ & FLAG_GNU_HASH) != 0;
777}
778
779bool soinfo::can_unload() const {
dimitry06016f22018-01-05 11:39:28 +0100780 return !is_linked() ||
781 (
dimitry55547db2018-05-25 14:17:37 +0200782 (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0
dimitry06016f22018-01-05 11:39:28 +0100783 );
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700784}
785
786bool soinfo::is_linked() const {
787 return (flags_ & FLAG_LINKED) != 0;
788}
789
dimitry965d06d2017-11-28 16:03:07 +0100790bool soinfo::is_image_linked() const {
791 return (flags_ & FLAG_IMAGE_LINKED) != 0;
792}
793
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700794bool soinfo::is_main_executable() const {
795 return (flags_ & FLAG_EXE) != 0;
796}
797
798bool soinfo::is_linker() const {
799 return (flags_ & FLAG_LINKER) != 0;
800}
801
802void soinfo::set_linked() {
803 flags_ |= FLAG_LINKED;
804}
805
dimitry965d06d2017-11-28 16:03:07 +0100806void soinfo::set_image_linked() {
807 flags_ |= FLAG_IMAGE_LINKED;
808}
809
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700810void soinfo::set_linker_flag() {
811 flags_ |= FLAG_LINKER;
812}
813
814void soinfo::set_main_executable() {
815 flags_ |= FLAG_EXE;
816}
817
dimitry965d06d2017-11-28 16:03:07 +0100818size_t soinfo::increment_ref_count() {
819 return ++local_group_root_->ref_count_;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700820}
821
822size_t soinfo::decrement_ref_count() {
823 return --local_group_root_->ref_count_;
824}
825
dimitry06016f22018-01-05 11:39:28 +0100826size_t soinfo::get_ref_count() const {
827 return local_group_root_->ref_count_;
828}
829
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700830soinfo* soinfo::get_local_group_root() const {
831 return local_group_root_;
832}
833
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700834void soinfo::set_mapped_by_caller(bool mapped_by_caller) {
835 if (mapped_by_caller) {
836 flags_ |= FLAG_MAPPED_BY_CALLER;
837 } else {
838 flags_ &= ~FLAG_MAPPED_BY_CALLER;
839 }
840}
841
842bool soinfo::is_mapped_by_caller() const {
843 return (flags_ & FLAG_MAPPED_BY_CALLER) != 0;
844}
845
846// This function returns api-level at the time of
847// dlopen/load. Note that libraries opened by system
848// will always have 'current' api level.
Elliott Hughesff1428a2018-11-12 16:01:37 -0800849int soinfo::get_target_sdk_version() const {
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700850 if (!has_min_version(2)) {
851 return __ANDROID_API__;
852 }
853
854 return local_group_root_->target_sdk_version_;
855}
856
857uintptr_t soinfo::get_handle() const {
858 CHECK(has_min_version(3));
859 CHECK(handle_ != 0);
860 return handle_;
861}
862
863void* soinfo::to_handle() {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800864 if (get_application_target_sdk_version() < 24 || !has_min_version(3)) {
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700865 return this;
866 }
867
868 return reinterpret_cast<void*>(get_handle());
869}
870
871void soinfo::generate_handle() {
872 CHECK(has_min_version(3));
873 CHECK(handle_ == 0); // Make sure this is the first call
874
875 // Make sure the handle is unique and does not collide
876 // with special values which are RTLD_DEFAULT and RTLD_NEXT.
877 do {
Tom Cherry66bc4282018-11-08 13:40:52 -0800878 if (!is_first_stage_init()) {
Jiyong Park31cd08f2018-06-01 19:18:56 +0900879 arc4random_buf(&handle_, sizeof(handle_));
880 } else {
881 // arc4random* is not available in init because /dev/urandom hasn't yet been
882 // created. So, when running with init, use the monotonically increasing
883 // numbers as handles
884 handle_ += 2;
885 }
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700886 // the least significant bit for the handle is always 1
887 // making it easy to test the type of handle passed to
888 // dl* functions.
889 handle_ = handle_ | 1;
890 } while (handle_ == reinterpret_cast<uintptr_t>(RTLD_DEFAULT) ||
891 handle_ == reinterpret_cast<uintptr_t>(RTLD_NEXT) ||
892 g_soinfo_handles_map.find(handle_) != g_soinfo_handles_map.end());
893
894 g_soinfo_handles_map[handle_] = this;
895}
896
Evgenii Stepanove0848bb2020-07-14 16:44:57 -0700897void soinfo::set_gap_start(ElfW(Addr) gap_start) {
898 CHECK(has_min_version(6));
899 gap_start_ = gap_start;
900}
901ElfW(Addr) soinfo::get_gap_start() const {
902 CHECK(has_min_version(6));
903 return gap_start_;
904}
905
906void soinfo::set_gap_size(size_t gap_size) {
907 CHECK(has_min_version(6));
908 gap_size_ = gap_size;
909}
910size_t soinfo::get_gap_size() const {
911 CHECK(has_min_version(6));
912 return gap_size_;
913}
914
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700915// TODO(dimitry): Move SymbolName methods to a separate file.
916
917uint32_t calculate_elf_hash(const char* name) {
918 const uint8_t* name_bytes = reinterpret_cast<const uint8_t*>(name);
919 uint32_t h = 0, g;
920
921 while (*name_bytes) {
922 h = (h << 4) + *name_bytes++;
923 g = h & 0xf0000000;
924 h ^= g;
925 h ^= g >> 24;
926 }
927
928 return h;
929}
930
931uint32_t SymbolName::elf_hash() {
932 if (!has_elf_hash_) {
933 elf_hash_ = calculate_elf_hash(name_);
934 has_elf_hash_ = true;
935 }
936
937 return elf_hash_;
938}
939
940uint32_t SymbolName::gnu_hash() {
941 if (!has_gnu_hash_) {
Ryan Prichard339ecef2020-01-02 16:36:06 -0800942 gnu_hash_ = calculate_gnu_hash(name_).first;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700943 has_gnu_hash_ = true;
944 }
945
946 return gnu_hash_;
947}