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