blob: 0549d36ef4ac3437707c393d4e2b2110529b6cb5 [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 -080048SymbolLookupList::SymbolLookupList(soinfo* si)
49 : sole_lib_(si->get_lookup_lib()), begin_(&sole_lib_), end_(&sole_lib_ + 1) {
50 CHECK(si != nullptr);
Elliott Hughesf08d0eb2024-09-03 17:31:29 -040051 slow_path_count_ += !!g_linker_debug_config.lookup;
Ryan Prichard339ecef2020-01-02 16:36:06 -080052 slow_path_count_ += sole_lib_.needs_sysv_lookup();
53}
54
55SymbolLookupList::SymbolLookupList(const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Elliott Hughesf08d0eb2024-09-03 17:31:29 -040056 slow_path_count_ += !!g_linker_debug_config.lookup;
Ryan Prichard339ecef2020-01-02 16:36:06 -080057 libs_.reserve(1 + global_group.size() + local_group.size());
58
59 // Reserve a space in front for DT_SYMBOLIC lookup.
60 libs_.push_back(SymbolLookupLib {});
61
62 global_group.for_each([this](soinfo* si) {
63 libs_.push_back(si->get_lookup_lib());
64 slow_path_count_ += libs_.back().needs_sysv_lookup();
65 });
66
67 local_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 begin_ = &libs_[1];
73 end_ = &libs_[0] + libs_.size();
74}
75
76/* "This element's presence in a shared object library alters the dynamic linker's
77 * symbol resolution algorithm for references within the library. Instead of starting
78 * a symbol search with the executable file, the dynamic linker starts from the shared
79 * object itself. If the shared object fails to supply the referenced symbol, the
80 * dynamic linker then searches the executable file and other shared objects as usual."
81 *
82 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
83 *
84 * Note that this is unlikely since static linker avoids generating
85 * relocations for -Bsymbolic linked dynamic executables.
86 */
87void SymbolLookupList::set_dt_symbolic_lib(soinfo* lib) {
88 CHECK(!libs_.empty());
89 slow_path_count_ -= libs_[0].needs_sysv_lookup();
90 libs_[0] = lib ? lib->get_lookup_lib() : SymbolLookupLib();
91 slow_path_count_ += libs_[0].needs_sysv_lookup();
92 begin_ = lib ? &libs_[0] : &libs_[1];
93}
94
95// Check whether a requested version matches the version on a symbol definition. There are a few
96// special cases:
97// - If the defining DSO has no version info at all, then any version matches.
98// - If no version is requested (vi==nullptr, verneed==kVersymNotNeeded), then any non-hidden
99// version matches.
100// - If the requested version is not defined by the DSO, then verneed is kVersymGlobal, and only
101// global symbol definitions match. (This special case is handled as part of the ordinary case
102// where the version must match exactly.)
103static inline bool check_symbol_version(const ElfW(Versym)* ver_table, uint32_t sym_idx,
104 const ElfW(Versym) verneed) {
105 if (ver_table == nullptr) return true;
106 const uint32_t verdef = ver_table[sym_idx];
107 return (verneed == kVersymNotNeeded) ?
108 !(verdef & kVersymHiddenBit) :
109 verneed == (verdef & ~kVersymHiddenBit);
110}
111
112template <bool IsGeneral>
113__attribute__((noinline)) static const ElfW(Sym)*
114soinfo_do_lookup_impl(const char* name, const version_info* vi,
115 soinfo** si_found_in, const SymbolLookupList& lookup_list) {
116 const auto [ hash, name_len ] = calculate_gnu_hash(name);
117 constexpr uint32_t kBloomMaskBits = sizeof(ElfW(Addr)) * 8;
118 SymbolName elf_symbol_name(name);
119
120 const SymbolLookupLib* end = lookup_list.end();
121 const SymbolLookupLib* it = lookup_list.begin();
122
123 while (true) {
124 const SymbolLookupLib* lib;
125 uint32_t sym_idx;
126
127 // Iterate over libraries until we find one whose Bloom filter matches the symbol we're
128 // searching for.
129 while (true) {
130 if (it == end) return nullptr;
131 lib = it++;
132
133 if (IsGeneral && lib->needs_sysv_lookup()) {
134 if (const ElfW(Sym)* sym = lib->si_->find_symbol_by_name(elf_symbol_name, vi)) {
135 *si_found_in = lib->si_;
136 return sym;
137 }
138 continue;
139 }
140
141 if (IsGeneral) {
Elliott Hughesf08d0eb2024-09-03 17:31:29 -0400142 LD_DEBUG(lookup, "SEARCH %s in %s@%p (gnu)",
143 name, lib->si_->get_realpath(), reinterpret_cast<void*>(lib->si_->base));
Ryan Prichard339ecef2020-01-02 16:36:06 -0800144 }
145
146 const uint32_t word_num = (hash / kBloomMaskBits) & lib->gnu_maskwords_;
147 const ElfW(Addr) bloom_word = lib->gnu_bloom_filter_[word_num];
148 const uint32_t h1 = hash % kBloomMaskBits;
149 const uint32_t h2 = (hash >> lib->gnu_shift2_) % kBloomMaskBits;
150
151 if ((1 & (bloom_word >> h1) & (bloom_word >> h2)) == 1) {
152 sym_idx = lib->gnu_bucket_[hash % lib->gnu_nbucket_];
153 if (sym_idx != 0) {
154 break;
155 }
156 }
Ryan Prichard339ecef2020-01-02 16:36:06 -0800157 }
158
159 // Search the library's hash table chain.
160 ElfW(Versym) verneed = kVersymNotNeeded;
161 bool calculated_verneed = false;
162
163 uint32_t chain_value = 0;
164 const ElfW(Sym)* sym = nullptr;
165
166 do {
167 sym = lib->symtab_ + sym_idx;
168 chain_value = lib->gnu_chain_[sym_idx];
169 if ((chain_value >> 1) == (hash >> 1)) {
170 if (vi != nullptr && !calculated_verneed) {
171 calculated_verneed = true;
172 verneed = find_verdef_version_index(lib->si_, vi);
173 }
174 if (check_symbol_version(lib->versym_, sym_idx, verneed) &&
175 static_cast<size_t>(sym->st_name) + name_len + 1 <= lib->strtab_size_ &&
176 memcmp(lib->strtab_ + sym->st_name, name, name_len + 1) == 0 &&
177 is_symbol_global_and_defined(lib->si_, sym)) {
178 *si_found_in = lib->si_;
Ryan Prichard339ecef2020-01-02 16:36:06 -0800179 return sym;
180 }
181 }
182 ++sym_idx;
183 } while ((chain_value & 1) == 0);
Ryan Prichard339ecef2020-01-02 16:36:06 -0800184 }
185}
186
187const ElfW(Sym)* soinfo_do_lookup(const char* name, const version_info* vi,
188 soinfo** si_found_in, const SymbolLookupList& lookup_list) {
189 return lookup_list.needs_slow_path() ?
190 soinfo_do_lookup_impl<true>(name, vi, si_found_in, lookup_list) :
191 soinfo_do_lookup_impl<false>(name, vi, si_found_in, lookup_list);
192}
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700193
Elliott Hughesb003e102023-09-29 17:07:27 +0000194soinfo::soinfo(android_namespace_t* ns, const char* realpath, const struct stat* file_stat,
195 off64_t file_offset, int rtld_flags) {
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700196 if (realpath != nullptr) {
197 realpath_ = realpath;
198 }
199
200 flags_ = FLAG_NEW_SOINFO;
201 version_ = SOINFO_VERSION;
202
203 if (file_stat != nullptr) {
204 this->st_dev_ = file_stat->st_dev;
205 this->st_ino_ = file_stat->st_ino;
206 this->file_offset_ = file_offset;
207 }
208
209 this->rtld_flags_ = rtld_flags;
210 this->primary_namespace_ = ns;
211}
212
213soinfo::~soinfo() {
214 g_soinfo_handles_map.erase(handle_);
215}
216
217void soinfo::set_dt_runpath(const char* path) {
218 if (!has_min_version(3)) {
219 return;
220 }
221
222 std::vector<std::string> runpaths;
223
224 split_path(path, ":", &runpaths);
225
226 std::string origin = dirname(get_realpath());
Jiyong Park57b9d1e2019-01-17 03:14:45 +0900227 // FIXME: add $PLATFORM.
228 std::vector<std::pair<std::string, std::string>> params = {
229 {"ORIGIN", origin},
Ryan Prichard4d4087d2019-12-02 16:55:48 -0800230 {"LIB", kLibPath},
Jiyong Park57b9d1e2019-01-17 03:14:45 +0900231 };
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700232 for (auto&& s : runpaths) {
Dimitry Ivanov2a6d9b22017-03-11 14:35:38 -0800233 format_string(&s, params);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700234 }
235
236 resolve_paths(runpaths, &dt_runpath_);
237}
238
239const ElfW(Versym)* soinfo::get_versym(size_t n) const {
Ryan Prichard339ecef2020-01-02 16:36:06 -0800240 auto table = get_versym_table();
241 return table ? table + n : nullptr;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700242}
243
244ElfW(Addr) soinfo::get_verneed_ptr() const {
245 if (has_min_version(2)) {
246 return verneed_ptr_;
247 }
248
249 return 0;
250}
251
252size_t soinfo::get_verneed_cnt() const {
253 if (has_min_version(2)) {
254 return verneed_cnt_;
255 }
256
257 return 0;
258}
259
260ElfW(Addr) soinfo::get_verdef_ptr() const {
261 if (has_min_version(2)) {
262 return verdef_ptr_;
263 }
264
265 return 0;
266}
267
268size_t soinfo::get_verdef_cnt() const {
269 if (has_min_version(2)) {
270 return verdef_cnt_;
271 }
272
273 return 0;
274}
275
Ryan Prichard339ecef2020-01-02 16:36:06 -0800276SymbolLookupLib soinfo::get_lookup_lib() {
277 SymbolLookupLib result {};
278 result.si_ = this;
279
280 // For libs that only have SysV hashes, leave the gnu_bloom_filter_ field NULL to signal that
281 // the fallback code path is needed.
282 if (!is_gnu_hash()) {
283 return result;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700284 }
285
Ryan Prichard339ecef2020-01-02 16:36:06 -0800286 result.gnu_maskwords_ = gnu_maskwords_;
287 result.gnu_shift2_ = gnu_shift2_;
288 result.gnu_bloom_filter_ = gnu_bloom_filter_;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700289
Ryan Prichard339ecef2020-01-02 16:36:06 -0800290 result.strtab_ = strtab_;
291 result.strtab_size_ = strtab_size_;
292 result.symtab_ = symtab_;
293 result.versym_ = get_versym_table();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700294
Ryan Prichard339ecef2020-01-02 16:36:06 -0800295 result.gnu_chain_ = gnu_chain_;
296 result.gnu_nbucket_ = gnu_nbucket_;
297 result.gnu_bucket_ = gnu_bucket_;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700298
Ryan Prichard339ecef2020-01-02 16:36:06 -0800299 return result;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700300}
301
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800302const ElfW(Sym)* soinfo::find_symbol_by_name(SymbolName& symbol_name,
303 const version_info* vi) const {
304 return is_gnu_hash() ? gnu_lookup(symbol_name, vi) : elf_lookup(symbol_name, vi);
305}
306
307const ElfW(Sym)* soinfo::gnu_lookup(SymbolName& symbol_name, const version_info* vi) const {
Ryan Prichard339ecef2020-01-02 16:36:06 -0800308 const uint32_t hash = symbol_name.gnu_hash();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700309
Ryan Prichard339ecef2020-01-02 16:36:06 -0800310 constexpr uint32_t kBloomMaskBits = sizeof(ElfW(Addr)) * 8;
311 const uint32_t word_num = (hash / kBloomMaskBits) & gnu_maskwords_;
312 const ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
313 const uint32_t h1 = hash % kBloomMaskBits;
314 const uint32_t h2 = (hash >> gnu_shift2_) % kBloomMaskBits;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700315
Elliott Hughesf08d0eb2024-09-03 17:31:29 -0400316 LD_DEBUG(lookup, "SEARCH %s in %s@%p (gnu)",
317 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700318
319 // test against bloom filter
Ryan Prichard339ecef2020-01-02 16:36:06 -0800320 if ((1 & (bloom_word >> h1) & (bloom_word >> h2)) == 0) {
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800321 return nullptr;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700322 }
323
324 // bloom test says "probably yes"...
325 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
326
327 if (n == 0) {
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800328 return nullptr;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700329 }
330
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800331 const ElfW(Versym) verneed = find_verdef_version_index(this, vi);
Ryan Prichard339ecef2020-01-02 16:36:06 -0800332 const ElfW(Versym)* versym = get_versym_table();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700333
334 do {
335 ElfW(Sym)* s = symtab_ + n;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700336 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Ryan Prichard339ecef2020-01-02 16:36:06 -0800337 check_symbol_version(versym, n, verneed) &&
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700338 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
339 is_symbol_global_and_defined(this, s)) {
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800340 return symtab_ + n;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700341 }
342 } while ((gnu_chain_[n++] & 1) == 0);
343
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800344 return nullptr;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700345}
346
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800347const ElfW(Sym)* soinfo::elf_lookup(SymbolName& symbol_name, const version_info* vi) const {
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700348 uint32_t hash = symbol_name.elf_hash();
349
Elliott Hughesf08d0eb2024-09-03 17:31:29 -0400350 LD_DEBUG(lookup, "SEARCH %s in %s@%p h=%x(elf) %zd",
351 symbol_name.get_name(), get_realpath(),
352 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700353
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800354 const ElfW(Versym) verneed = find_verdef_version_index(this, vi);
Ryan Prichard339ecef2020-01-02 16:36:06 -0800355 const ElfW(Versym)* versym = get_versym_table();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700356
357 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
358 ElfW(Sym)* s = symtab_ + n;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700359
Ryan Prichard339ecef2020-01-02 16:36:06 -0800360 if (check_symbol_version(versym, n, verneed) &&
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700361 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
362 is_symbol_global_and_defined(this, s)) {
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800363 return symtab_ + n;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700364 }
365 }
366
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800367 return nullptr;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700368}
369
370ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
371 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
372}
373
374static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
Ryan Pricharde4d620b2019-04-01 17:42:14 -0700375 // Skip TLS symbols. A TLS symbol's value is relative to the start of the TLS segment rather than
376 // to the start of the solib. The solib only reserves space for the initialized part of the TLS
377 // segment. (i.e. .tdata is followed by .tbss, and .tbss overlaps other sections.)
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700378 return sym->st_shndx != SHN_UNDEF &&
Ryan Pricharde4d620b2019-04-01 17:42:14 -0700379 ELF_ST_TYPE(sym->st_info) != STT_TLS &&
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700380 soaddr >= sym->st_value &&
381 soaddr < sym->st_value + sym->st_size;
382}
383
384ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
385 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
386
387 for (size_t i = 0; i < gnu_nbucket_; ++i) {
388 uint32_t n = gnu_bucket_[i];
389
390 if (n == 0) {
391 continue;
392 }
393
394 do {
395 ElfW(Sym)* sym = symtab_ + n;
396 if (symbol_matches_soaddr(sym, soaddr)) {
397 return sym;
398 }
399 } while ((gnu_chain_[n++] & 1) == 0);
400 }
401
402 return nullptr;
403}
404
405ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
406 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
407
408 // Search the library's symbol table for any defined symbol which
409 // contains this address.
410 for (size_t i = 0; i < nchain_; ++i) {
411 ElfW(Sym)* sym = symtab_ + i;
412 if (symbol_matches_soaddr(sym, soaddr)) {
413 return sym;
414 }
415 }
416
417 return nullptr;
418}
419
420static void call_function(const char* function_name __unused,
421 linker_ctor_function_t function,
422 const char* realpath __unused) {
423 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
424 return;
425 }
426
Elliott Hughesf08d0eb2024-09-03 17:31:29 -0400427 LD_DEBUG(calls, "[ Calling c-tor %s @ %p for '%s' ]", function_name, function, realpath);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700428 function(g_argc, g_argv, g_envp);
Elliott Hughesf08d0eb2024-09-03 17:31:29 -0400429 LD_DEBUG(calls, "[ Done calling c-tor %s @ %p for '%s' ]", function_name, function, realpath);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700430}
431
432static void call_function(const char* function_name __unused,
433 linker_dtor_function_t function,
434 const char* realpath __unused) {
435 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
436 return;
437 }
438
Elliott Hughesf08d0eb2024-09-03 17:31:29 -0400439 LD_DEBUG(calls, "[ Calling d-tor %s @ %p for '%s' ]", function_name, function, realpath);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700440 function();
Elliott Hughesf08d0eb2024-09-03 17:31:29 -0400441 LD_DEBUG(calls, "[ Done calling d-tor %s @ %p for '%s' ]", function_name, function, realpath);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700442}
443
444template <typename F>
Elliott Hughes01be44d2021-01-19 09:41:23 -0800445static inline void call_array(const char* array_name __unused, F* functions, size_t count,
446 bool reverse, const char* realpath) {
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700447 if (functions == nullptr) {
448 return;
449 }
450
Elliott Hughesf08d0eb2024-09-03 17:31:29 -0400451 LD_DEBUG(calls, "[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, realpath);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700452
453 int begin = reverse ? (count - 1) : 0;
454 int end = reverse ? -1 : count;
455 int step = reverse ? -1 : 1;
456
457 for (int i = begin; i != end; i += step) {
Elliott Hughesf08d0eb2024-09-03 17:31:29 -0400458 LD_DEBUG(calls, "[ %s[%d] == %p ]", array_name, i, functions[i]);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700459 call_function("function", functions[i], realpath);
460 }
461
Elliott Hughesf08d0eb2024-09-03 17:31:29 -0400462 LD_DEBUG(calls, "[ Done calling %s for '%s' ]", array_name, realpath);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700463}
464
465void soinfo::call_pre_init_constructors() {
466 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
467 // but ignored in a shared library.
468 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false, get_realpath());
469}
470
471void soinfo::call_constructors() {
Ryan Prichard32bb3672024-03-08 16:53:06 -0800472 if (constructors_called) {
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700473 return;
474 }
475
476 // We set constructors_called before actually calling the constructors, otherwise it doesn't
477 // protect against recursive constructor calls. One simple example of constructor recursion
478 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
479 // 1. The program depends on libc, so libc's constructor is called here.
480 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
481 // 3. dlopen() calls the constructors on the newly created
482 // soinfo for libc_malloc_debug_leak.so.
483 // 4. The debug .so depends on libc, so CallConstructors is
484 // called again with the libc soinfo. If it doesn't trigger the early-
485 // out above, the libc constructor will be called again (recursively!).
486 constructors_called = true;
487
488 if (!is_main_executable() && preinit_array_ != nullptr) {
489 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesf08d0eb2024-09-03 17:31:29 -0400490 DL_WARN("\"%s\": ignoring DT_PREINIT_ARRAY in shared library!", get_realpath());
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700491 }
492
493 get_children().for_each([] (soinfo* si) {
494 si->call_constructors();
495 });
496
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -0700497 if (!is_linker()) {
498 bionic_trace_begin((std::string("calling constructors: ") + get_realpath()).c_str());
499 }
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700500
501 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
502 call_function("DT_INIT", init_func_, get_realpath());
503 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false, get_realpath());
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -0700504
505 if (!is_linker()) {
506 bionic_trace_end();
507 }
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700508}
509
510void soinfo::call_destructors() {
511 if (!constructors_called) {
512 return;
513 }
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -0700514
515 ScopedTrace trace((std::string("calling destructors: ") + get_realpath()).c_str());
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700516
517 // DT_FINI_ARRAY must be parsed in reverse order.
518 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true, get_realpath());
519
520 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
521 call_function("DT_FINI", fini_func_, get_realpath());
522}
523
524void soinfo::add_child(soinfo* child) {
525 if (has_min_version(0)) {
526 child->parents_.push_back(this);
527 this->children_.push_back(child);
528 }
529}
530
531void soinfo::remove_all_links() {
532 if (!has_min_version(0)) {
533 return;
534 }
535
536 // 1. Untie connected soinfos from 'this'.
537 children_.for_each([&] (soinfo* child) {
538 child->parents_.remove_if([&] (const soinfo* parent) {
539 return parent == this;
540 });
541 });
542
543 parents_.for_each([&] (soinfo* parent) {
544 parent->children_.remove_if([&] (const soinfo* child) {
545 return child == this;
546 });
547 });
548
549 // 2. Remove from the primary namespace
550 primary_namespace_->remove_soinfo(this);
551 primary_namespace_ = nullptr;
552
553 // 3. Remove from secondary namespaces
554 secondary_namespaces_.for_each([&](android_namespace_t* ns) {
555 ns->remove_soinfo(this);
556 });
557
558
559 // 4. Once everything untied - clear local lists.
560 parents_.clear();
561 children_.clear();
562 secondary_namespaces_.clear();
563}
564
565dev_t soinfo::get_st_dev() const {
566 if (has_min_version(0)) {
567 return st_dev_;
568 }
569
570 return 0;
571};
572
573ino_t soinfo::get_st_ino() const {
574 if (has_min_version(0)) {
575 return st_ino_;
576 }
577
578 return 0;
579}
580
581off64_t soinfo::get_file_offset() const {
582 if (has_min_version(1)) {
583 return file_offset_;
584 }
585
586 return 0;
587}
588
589uint32_t soinfo::get_rtld_flags() const {
590 if (has_min_version(1)) {
591 return rtld_flags_;
592 }
593
594 return 0;
595}
596
597uint32_t soinfo::get_dt_flags_1() const {
598 if (has_min_version(1)) {
599 return dt_flags_1_;
600 }
601
602 return 0;
603}
604
605void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
606 if (has_min_version(1)) {
607 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
608 rtld_flags_ |= RTLD_GLOBAL;
609 }
610
611 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
612 rtld_flags_ |= RTLD_NODELETE;
613 }
614
615 dt_flags_1_ = dt_flags_1;
616 }
617}
618
619void soinfo::set_nodelete() {
620 rtld_flags_ |= RTLD_NODELETE;
621}
622
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700623void soinfo::set_realpath(const char* path) {
624#if defined(__work_around_b_24465209__)
625 if (has_min_version(2)) {
626 realpath_ = path;
627 }
628#else
629 realpath_ = path;
630#endif
631}
632
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700633const char* soinfo::get_realpath() const {
634#if defined(__work_around_b_24465209__)
635 if (has_min_version(2)) {
636 return realpath_.c_str();
637 } else {
638 return old_name_;
639 }
640#else
641 return realpath_.c_str();
642#endif
643}
644
645void soinfo::set_soname(const char* soname) {
646#if defined(__work_around_b_24465209__)
647 if (has_min_version(2)) {
648 soname_ = soname;
649 }
Elliott Hughesf9dd1a72021-01-11 09:04:58 -0800650 strlcpy(old_name_, soname_.c_str(), sizeof(old_name_));
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700651#else
652 soname_ = soname;
653#endif
654}
655
656const char* soinfo::get_soname() const {
657#if defined(__work_around_b_24465209__)
658 if (has_min_version(2)) {
Elliott Hughesf9dd1a72021-01-11 09:04:58 -0800659 return soname_.c_str();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700660 } else {
661 return old_name_;
662 }
663#else
Elliott Hughesf9dd1a72021-01-11 09:04:58 -0800664 return soname_.c_str();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700665#endif
666}
667
668// This is a return on get_children()/get_parents() if
669// 'this->flags' does not have FLAG_NEW_SOINFO set.
670static soinfo_list_t g_empty_list;
671
672soinfo_list_t& soinfo::get_children() {
673 if (has_min_version(0)) {
674 return children_;
675 }
676
677 return g_empty_list;
678}
679
680const soinfo_list_t& soinfo::get_children() const {
681 if (has_min_version(0)) {
682 return children_;
683 }
684
685 return g_empty_list;
686}
687
688soinfo_list_t& soinfo::get_parents() {
689 if (has_min_version(0)) {
690 return parents_;
691 }
692
693 return g_empty_list;
694}
695
696static std::vector<std::string> g_empty_runpath;
697
698const std::vector<std::string>& soinfo::get_dt_runpath() const {
699 if (has_min_version(3)) {
700 return dt_runpath_;
701 }
702
703 return g_empty_runpath;
704}
705
706android_namespace_t* soinfo::get_primary_namespace() {
707 if (has_min_version(3)) {
708 return primary_namespace_;
709 }
710
711 return &g_default_namespace;
712}
713
714void soinfo::add_secondary_namespace(android_namespace_t* secondary_ns) {
715 CHECK(has_min_version(3));
716 secondary_namespaces_.push_back(secondary_ns);
717}
718
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800719android_namespace_list_t& soinfo::get_secondary_namespaces() {
720 CHECK(has_min_version(3));
721 return secondary_namespaces_;
722}
723
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700724const char* soinfo::get_string(ElfW(Word) index) const {
725 if (has_min_version(1) && (index >= strtab_size_)) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700726 async_safe_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700727 get_realpath(), strtab_size_, index);
728 }
729
730 return strtab_ + index;
731}
732
733bool soinfo::is_gnu_hash() const {
734 return (flags_ & FLAG_GNU_HASH) != 0;
735}
736
737bool soinfo::can_unload() const {
dimitry06016f22018-01-05 11:39:28 +0100738 return !is_linked() ||
739 (
dimitry55547db2018-05-25 14:17:37 +0200740 (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0
dimitry06016f22018-01-05 11:39:28 +0100741 );
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700742}
743
744bool soinfo::is_linked() const {
745 return (flags_ & FLAG_LINKED) != 0;
746}
747
dimitry965d06d2017-11-28 16:03:07 +0100748bool soinfo::is_image_linked() const {
749 return (flags_ & FLAG_IMAGE_LINKED) != 0;
750}
751
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700752bool soinfo::is_main_executable() const {
753 return (flags_ & FLAG_EXE) != 0;
754}
755
756bool soinfo::is_linker() const {
757 return (flags_ & FLAG_LINKER) != 0;
758}
759
760void soinfo::set_linked() {
761 flags_ |= FLAG_LINKED;
762}
763
dimitry965d06d2017-11-28 16:03:07 +0100764void soinfo::set_image_linked() {
765 flags_ |= FLAG_IMAGE_LINKED;
766}
767
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700768void soinfo::set_linker_flag() {
769 flags_ |= FLAG_LINKER;
770}
771
772void soinfo::set_main_executable() {
773 flags_ |= FLAG_EXE;
774}
775
dimitry965d06d2017-11-28 16:03:07 +0100776size_t soinfo::increment_ref_count() {
777 return ++local_group_root_->ref_count_;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700778}
779
780size_t soinfo::decrement_ref_count() {
781 return --local_group_root_->ref_count_;
782}
783
dimitry06016f22018-01-05 11:39:28 +0100784size_t soinfo::get_ref_count() const {
785 return local_group_root_->ref_count_;
786}
787
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700788soinfo* soinfo::get_local_group_root() const {
789 return local_group_root_;
790}
791
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700792void soinfo::set_mapped_by_caller(bool mapped_by_caller) {
793 if (mapped_by_caller) {
794 flags_ |= FLAG_MAPPED_BY_CALLER;
795 } else {
796 flags_ &= ~FLAG_MAPPED_BY_CALLER;
797 }
798}
799
800bool soinfo::is_mapped_by_caller() const {
801 return (flags_ & FLAG_MAPPED_BY_CALLER) != 0;
802}
803
804// This function returns api-level at the time of
805// dlopen/load. Note that libraries opened by system
806// will always have 'current' api level.
Elliott Hughesff1428a2018-11-12 16:01:37 -0800807int soinfo::get_target_sdk_version() const {
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700808 if (!has_min_version(2)) {
809 return __ANDROID_API__;
810 }
811
812 return local_group_root_->target_sdk_version_;
813}
814
815uintptr_t soinfo::get_handle() const {
816 CHECK(has_min_version(3));
817 CHECK(handle_ != 0);
818 return handle_;
819}
820
821void* soinfo::to_handle() {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800822 if (get_application_target_sdk_version() < 24 || !has_min_version(3)) {
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700823 return this;
824 }
825
826 return reinterpret_cast<void*>(get_handle());
827}
828
829void soinfo::generate_handle() {
830 CHECK(has_min_version(3));
831 CHECK(handle_ == 0); // Make sure this is the first call
832
833 // Make sure the handle is unique and does not collide
834 // with special values which are RTLD_DEFAULT and RTLD_NEXT.
835 do {
Tom Cherry66bc4282018-11-08 13:40:52 -0800836 if (!is_first_stage_init()) {
Jiyong Park31cd08f2018-06-01 19:18:56 +0900837 arc4random_buf(&handle_, sizeof(handle_));
838 } else {
839 // arc4random* is not available in init because /dev/urandom hasn't yet been
840 // created. So, when running with init, use the monotonically increasing
841 // numbers as handles
842 handle_ += 2;
843 }
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700844 // the least significant bit for the handle is always 1
845 // making it easy to test the type of handle passed to
846 // dl* functions.
847 handle_ = handle_ | 1;
848 } while (handle_ == reinterpret_cast<uintptr_t>(RTLD_DEFAULT) ||
849 handle_ == reinterpret_cast<uintptr_t>(RTLD_NEXT) ||
Elliott Hughes192f3cf2024-06-25 11:21:41 +0000850 g_soinfo_handles_map.contains(handle_));
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700851
852 g_soinfo_handles_map[handle_] = this;
853}
854
Evgenii Stepanove0848bb2020-07-14 16:44:57 -0700855void soinfo::set_gap_start(ElfW(Addr) gap_start) {
856 CHECK(has_min_version(6));
857 gap_start_ = gap_start;
858}
859ElfW(Addr) soinfo::get_gap_start() const {
860 CHECK(has_min_version(6));
861 return gap_start_;
862}
863
864void soinfo::set_gap_size(size_t gap_size) {
865 CHECK(has_min_version(6));
866 gap_size_ = gap_size;
867}
868size_t soinfo::get_gap_size() const {
869 CHECK(has_min_version(6));
870 return gap_size_;
871}
872
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700873// TODO(dimitry): Move SymbolName methods to a separate file.
874
875uint32_t calculate_elf_hash(const char* name) {
876 const uint8_t* name_bytes = reinterpret_cast<const uint8_t*>(name);
877 uint32_t h = 0, g;
878
879 while (*name_bytes) {
880 h = (h << 4) + *name_bytes++;
881 g = h & 0xf0000000;
882 h ^= g;
883 h ^= g >> 24;
884 }
885
886 return h;
887}
888
889uint32_t SymbolName::elf_hash() {
890 if (!has_elf_hash_) {
891 elf_hash_ = calculate_elf_hash(name_);
892 has_elf_hash_ = true;
893 }
894
895 return elf_hash_;
896}
897
898uint32_t SymbolName::gnu_hash() {
899 if (!has_gnu_hash_) {
Ryan Prichard339ecef2020-01-02 16:36:06 -0800900 gnu_hash_ = calculate_gnu_hash(name_).first;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700901 has_gnu_hash_ = true;
902 }
903
904 return gnu_hash_;
905}