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