blob: 72e7759e85425edc1f54045b319ef978567c848f [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002 * Copyright (C) 2008 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 * 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
Dmitriy Ivanov19133522015-06-02 17:36:54 -070029#include <android/api-level.h>
Elliott Hughes46882792012-08-03 16:49:39 -070030#include <errno.h>
31#include <fcntl.h>
Elliott Hughes0266ae52014-02-10 17:46:57 -080032#include <inttypes.h>
Elliott Hughes46882792012-08-03 16:49:39 -070033#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
Elliott Hughes46882792012-08-03 16:49:39 -070037#include <sys/mman.h>
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -080038#include <sys/param.h>
Elliott Hughes46882792012-08-03 16:49:39 -070039#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070041#include <new>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070042#include <string>
Dmitriy Ivanovb4827502015-09-28 16:38:31 -070043#include <unordered_map>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070044#include <vector>
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070045
Elliott Hughes46882792012-08-03 16:49:39 -070046// Private C library headers.
Elliott Hugheseb847bc2013-10-09 15:50:50 -070047#include "private/bionic_tls.h"
48#include "private/KernelArgumentBlock.h"
49#include "private/ScopedPthreadMutexLocker.h"
Dmitriy Ivanov14669a92014-09-05 16:42:53 -070050#include "private/ScopeGuard.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051
52#include "linker.h"
Dmitriy Ivanovc9ce70d2015-03-10 15:30:26 -070053#include "linker_block_allocator.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054#include "linker_debug.h"
Dmitriy Ivanov18870d32015-04-22 13:10:04 -070055#include "linker_sleb128.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020056#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080057#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080058#include "linker_reloc_iterators.h"
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070059#include "linker_utils.h"
tony.ys_liub4474402015-07-29 18:00:22 +080060
Elliott Hughes939a7e02015-12-04 15:27:46 -080061#include "android-base/strings.h"
Simon Baldwinaef71952015-01-16 13:22:54 +000062#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063
Josh Gao93c0f5e2015-10-06 11:08:13 -070064extern void __libc_init_globals(KernelArgumentBlock&);
Elliott Hughes1801db32015-06-08 18:04:00 -070065extern void __libc_init_AT_SECURE(KernelArgumentBlock&);
66
67// Override macros to use C++ style casts.
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080068#undef ELF_ST_TYPE
69#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
70
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070071struct android_namespace_t {
72 public:
73 android_namespace_t() : name_(nullptr), is_isolated_(false) {}
74
75 const char* get_name() const { return name_; }
76 void set_name(const char* name) { name_ = name; }
77
78 bool is_isolated() const { return is_isolated_; }
79 void set_isolated(bool isolated) { is_isolated_ = isolated; }
80
81 const std::vector<std::string>& get_ld_library_paths() const {
82 return ld_library_paths_;
83 }
84 void set_ld_library_paths(std::vector<std::string>&& library_paths) {
85 ld_library_paths_ = library_paths;
86 }
87
88 const std::vector<std::string>& get_default_library_paths() const {
89 return default_library_paths_;
90 }
91 void set_default_library_paths(std::vector<std::string>&& library_paths) {
92 default_library_paths_ = library_paths;
93 }
94
Dimitry Ivanov284ae352015-12-08 10:47:13 -080095 void set_permitted_paths(std::vector<std::string>&& permitted_paths) {
96 permitted_paths_ = permitted_paths;
97 }
98
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070099 soinfo::soinfo_list_t& soinfo_list() { return soinfo_list_; }
100
101 // For isolated namespaces - checks if the file is on the search path;
102 // always returns true for not isolated namespace.
103 bool is_accessible(const std::string& path);
104
105 private:
106 const char* name_;
107 bool is_isolated_;
108 std::vector<std::string> ld_library_paths_;
109 std::vector<std::string> default_library_paths_;
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800110 std::vector<std::string> permitted_paths_;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700111 soinfo::soinfo_list_t soinfo_list_;
112
113 DISALLOW_COPY_AND_ASSIGN(android_namespace_t);
114};
115
116android_namespace_t g_default_namespace;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800117android_namespace_t* g_anonymous_namespace = &g_default_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700118
Elliott Hughes0266ae52014-02-10 17:46:57 -0800119static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700121static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
122static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200123
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700124static LinkerTypeAllocator<android_namespace_t> g_namespace_allocator;
125
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700126static soinfo* solist;
127static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700128static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129
Elliott Hughes1728b232014-05-14 10:02:03 -0700130static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700131#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700132 "/vendor/lib64",
133 "/system/lib64",
134#else
Elliott Hughes124fae92012-10-31 14:20:03 -0700135 "/vendor/lib",
136 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700137#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700138 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700139};
David Bartleybc3a5c22009-06-02 18:27:28 -0700140
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700141static const char* const kAsanDefaultLdPaths[] = {
142#if defined(__LP64__)
143 "/data/vendor/lib64",
144 "/vendor/lib64",
145 "/data/lib64",
146 "/system/lib64",
147#else
148 "/data/vendor/lib",
149 "/vendor/lib",
150 "/data/lib",
151 "/system/lib",
152#endif
153 nullptr
154};
155
Dimitry Ivanov36ac45f2016-01-07 18:43:20 -0800156static bool is_system_library(const std::string& realpath) {
157 for (const auto& dir : g_default_namespace.get_default_library_paths()) {
158 if (file_is_in_dir(realpath, dir)) {
159 return true;
160 }
161 }
162 return false;
163}
164
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800165// TODO(dimitry): This is workaround for http://b/26394120 - it will be removed before the release
Dimitry Ivanov36ac45f2016-01-07 18:43:20 -0800166static bool is_greylisted(const char* name, const soinfo* needed_by) {
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800167 static const char* const kLibraryGreyList[] = {
Dimitry Ivanov36ac45f2016-01-07 18:43:20 -0800168 "libandroid_runtime.so",
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800169 "libbinder.so",
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800170 "libcrypto.so",
171 "libcutils.so",
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800172 "libmedia.so",
173 "libnativehelper.so",
Dimitry Ivanova2a05012016-01-25 15:48:44 -0800174 "libskia.so",
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800175 "libssl.so",
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800176 "libstagefright.so",
Dimitry Ivanov78dfc402016-01-12 10:37:38 -0800177 "libui.so",
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800178 "libutils.so",
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800179 nullptr
180 };
181
Dimitry Ivanovf1db47a2016-01-11 10:20:15 -0800182 // limit greylisting to apps targeting sdk version 23 and below
183 if (get_application_target_sdk_version() > 23) {
184 return false;
185 }
186
Dimitry Ivanov36ac45f2016-01-07 18:43:20 -0800187 // if the library needed by a system library - implicitly assume it
188 // is greylisted
189
190 if (needed_by != nullptr && is_system_library(needed_by->get_realpath())) {
191 return true;
192 }
193
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800194 for (size_t i = 0; kLibraryGreyList[i] != nullptr; ++i) {
195 if (strcmp(name, kLibraryGreyList[i]) == 0) {
196 return true;
197 }
198 }
199
200 return false;
201}
202// END OF WORKAROUND
203
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700204static const ElfW(Versym) kVersymNotNeeded = 0;
205static const ElfW(Versym) kVersymGlobal = 1;
206
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700207static const char* const* g_default_ld_paths;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700208static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800209
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700210static std::vector<soinfo*> g_ld_preloads;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600211
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700212static bool g_public_namespace_initialized;
213static soinfo::soinfo_list_t g_public_namespace;
214
Elliott Hughes1728b232014-05-14 10:02:03 -0700215__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800216
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700217__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700218
Evgenii Stepanov68650822015-06-10 13:38:39 -0700219static std::string dirname(const char *path) {
220 const char* last_slash = strrchr(path, '/');
221 if (last_slash == path) return "/";
222 else if (last_slash == nullptr) return ".";
223 else
224 return std::string(path, last_slash - path);
225}
226
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800227#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700228struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700229 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700230};
231
232static linker_stats_t linker_stats;
233
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800234void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700235 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700236}
237#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800238void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700239}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240#endif
241
242#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800243uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244#endif
245
Dima Zavin2e855792009-05-20 18:28:09 -0700246static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700247
Elliott Hughes650be4e2013-03-05 18:47:58 -0800248char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700249 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700250}
251
Elliott Hughes650be4e2013-03-05 18:47:58 -0800252size_t linker_get_error_buffer_size() {
253 return sizeof(__linker_dl_err_buf);
254}
255
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700256// This function is an empty stub where GDB locates a breakpoint to get notified
257// about linker activity.
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700258extern "C"
259void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800260
Elliott Hughes1728b232014-05-14 10:02:03 -0700261static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700262static r_debug _r_debug =
263 {1, nullptr, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
264
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800265static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800266
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800267static void insert_soinfo_into_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700268 // Copy the necessary fields into the debug structure.
269 link_map* map = &(info->link_map_head);
270 map->l_addr = info->load_bias;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700271 // link_map l_name field is not const.
272 map->l_name = const_cast<char*>(info->get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700273 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700275 // Stick the new library at the end of the list.
276 // gdb tends to care more about libc than it does
277 // about leaf libraries, and ordering it this way
278 // reduces the back-and-forth over the wire.
279 if (r_debug_tail) {
280 r_debug_tail->l_next = map;
281 map->l_prev = r_debug_tail;
282 map->l_next = 0;
283 } else {
284 _r_debug.r_map = map;
285 map->l_prev = 0;
286 map->l_next = 0;
287 }
288 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800289}
290
Elliott Hughesbedfe382012-08-14 14:07:59 -0700291static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700292 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700293
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700294 if (r_debug_tail == map) {
295 r_debug_tail = map->l_prev;
296 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700297
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700298 if (map->l_prev) {
299 map->l_prev->l_next = map->l_next;
300 }
301 if (map->l_next) {
302 map->l_next->l_prev = map->l_prev;
303 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700304}
305
Elliott Hughesbedfe382012-08-14 14:07:59 -0700306static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800307 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700308 // GDB already knows about the main executable
309 return;
310 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800311
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700312 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800313
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700314 _r_debug.r_state = r_debug::RT_ADD;
315 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800316
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700317 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800318
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700319 _r_debug.r_state = r_debug::RT_CONSISTENT;
320 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700321}
322
Elliott Hughesbedfe382012-08-14 14:07:59 -0700323static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800324 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700325 // GDB already knows about the main executable
326 return;
327 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700328
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700329 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700330
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700331 _r_debug.r_state = r_debug::RT_DELETE;
332 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700333
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700334 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700335
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700336 _r_debug.r_state = r_debug::RT_CONSISTENT;
337 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800338}
339
Elliott Hughes18a206c2012-10-29 17:37:13 -0700340void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800341 _r_debug.r_state = r_debug::RT_ADD;
342 rtld_db_dlactivity();
343 _r_debug.r_state = r_debug::RT_CONSISTENT;
344 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800345}
346
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700347bool android_namespace_t::is_accessible(const std::string& file) {
348 if (!is_isolated_) {
349 return true;
350 }
351
352 for (const auto& dir : ld_library_paths_) {
353 if (file_is_in_dir(file, dir)) {
354 return true;
355 }
356 }
357
358 for (const auto& dir : default_library_paths_) {
359 if (file_is_in_dir(file, dir)) {
360 return true;
361 }
362 }
363
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800364 for (const auto& dir : permitted_paths_) {
365 if (file_is_under_dir(file, dir)) {
366 return true;
367 }
368 }
369
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700370 return false;
371}
372
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700373LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
374 return g_soinfo_links_allocator.alloc();
375}
376
377void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
378 g_soinfo_links_allocator.free(entry);
379}
380
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700381static soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
382 struct stat* file_stat, off64_t file_offset,
383 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700384 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200385 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700386 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200387 }
388
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700389 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
390 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700391
Magnus Malmbornba98d922012-09-12 13:00:55 +0200392 sonext->next = si;
393 sonext = si;
394
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700395 ns->soinfo_list().push_back(si);
396
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700397 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200398 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800399}
400
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800401static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700402 if (si == nullptr) {
403 return;
404 }
405
406 if (si->base != 0 && si->size != 0) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800407 if (!si->is_mapped_by_caller()) {
408 munmap(reinterpret_cast<void*>(si->base), si->size);
409 } else {
410 // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE
411 mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE,
412 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
413 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700414 }
415
416 soinfo *prev = nullptr, *trav;
417
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700418 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700419
420 for (trav = solist; trav != nullptr; trav = trav->next) {
421 if (trav == si) {
422 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700423 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700424 prev = trav;
425 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800426
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700427 if (trav == nullptr) {
428 // si was not in solist
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700429 DL_ERR("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700430 return;
431 }
Elliott Hughes46882792012-08-03 16:49:39 -0700432
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700433 // clear links to/from si
434 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700435
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700436 // prev will never be null, because the first entry in solist is
437 // always the static libdl_info.
438 prev->next = si->next;
439 if (si == sonext) {
440 sonext = prev;
441 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800442
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700443 // remove from the namespace
444 si->get_namespace()->soinfo_list().remove_if([&](soinfo* candidate) {
445 return si == candidate;
446 });
447
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700448 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700449 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800450}
451
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700452// For every path element this function checks of it exists, and is a directory,
453// and normalizes it:
454// 1. For regular path it converts it to realpath()
455// 2. For path in a zip file it uses realpath on the zipfile
456// normalizes entry name by calling normalize_path function.
457static void resolve_paths(std::vector<std::string>& paths,
458 std::vector<std::string>* resolved_paths) {
459 resolved_paths->clear();
460 for (const auto& path : paths) {
461 char resolved_path[PATH_MAX];
462 const char* original_path = path.c_str();
463 if (realpath(original_path, resolved_path) != nullptr) {
464 struct stat s;
465 if (stat(resolved_path, &s) == 0) {
466 if (S_ISDIR(s.st_mode)) {
467 resolved_paths->push_back(resolved_path);
468 } else {
469 DL_WARN("Warning: \"%s\" is not a directory (excluding from path)", resolved_path);
470 continue;
471 }
472 } else {
473 DL_WARN("Warning: cannot stat file \"%s\": %s", resolved_path, strerror(errno));
474 continue;
475 }
476 } else {
477 std::string zip_path;
478 std::string entry_path;
479
480 std::string normalized_path;
481
482 if (!normalize_path(original_path, &normalized_path)) {
483 DL_WARN("Warning: unable to normalize \"%s\"", original_path);
484 continue;
485 }
486
487 if (parse_zip_path(normalized_path.c_str(), &zip_path, &entry_path)) {
488 if (realpath(zip_path.c_str(), resolved_path) == nullptr) {
489 DL_WARN("Warning: unable to resolve \"%s\": %s", zip_path.c_str(), strerror(errno));
490 continue;
491 }
492
493 ZipArchiveHandle handle = nullptr;
494 if (OpenArchive(resolved_path, &handle) != 0) {
495 DL_WARN("Warning: unable to open zip archive: %s", resolved_path);
496 continue;
497 }
498
499 // Check if zip-file has a dir with entry_path name
500 void* cookie = nullptr;
501 std::string prefix_str = entry_path + "/";
502 ZipString prefix(prefix_str.c_str());
503
504 ZipEntry out_data;
505 ZipString out_name;
506
507 int32_t error_code;
508
509 if ((error_code = StartIteration(handle, &cookie, &prefix, nullptr)) != 0) {
510 DL_WARN("Unable to iterate over zip-archive entries \"%s\";"
511 " error code: %d", zip_path.c_str(), error_code);
512 continue;
513 }
514
515 if (Next(cookie, &out_data, &out_name) != 0) {
516 DL_WARN("Unable to find entries starting with \"%s\" in \"%s\"",
517 prefix_str.c_str(), zip_path.c_str());
518 continue;
519 }
520
521 auto zip_guard = make_scope_guard([&]() {
522 if (cookie != nullptr) {
523 EndIteration(cookie);
524 }
525 CloseArchive(handle);
526 });
527
528 resolved_paths->push_back(std::string(resolved_path) + kZipFileSeparator + entry_path);
529 }
530 }
531 }
532}
533
534static void split_path(const char* path, const char* delimiters,
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700535 std::vector<std::string>* paths) {
Dmitriy Ivanovfbfba642015-11-16 14:23:37 -0800536 if (path != nullptr && path[0] != 0) {
tony.ys_liub4474402015-07-29 18:00:22 +0800537 *paths = android::base::Split(path, delimiters);
Elliott Hughescade4c32012-12-20 14:42:14 -0800538 }
539}
540
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700541static void parse_path(const char* path, const char* delimiters,
542 std::vector<std::string>* resolved_paths) {
543 std::vector<std::string> paths;
544 split_path(path, delimiters, &paths);
545 resolve_paths(paths, resolved_paths);
546}
547
Elliott Hughescade4c32012-12-20 14:42:14 -0800548static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700549 std::vector<std::string> ld_libary_paths;
550 parse_path(path, ":", &ld_libary_paths);
551 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800552}
553
Evgenii Stepanov68650822015-06-10 13:38:39 -0700554void soinfo::set_dt_runpath(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700555 if (!has_min_version(3)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700556 return;
557 }
558
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700559 std::vector<std::string> runpaths;
560
561 split_path(path, ":", &runpaths);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700562
563 std::string origin = dirname(get_realpath());
564 // FIXME: add $LIB and $PLATFORM.
565 std::pair<std::string, std::string> substs[] = {{"ORIGIN", origin}};
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700566 for (auto&& s : runpaths) {
Evgenii Stepanov68650822015-06-10 13:38:39 -0700567 size_t pos = 0;
568 while (pos < s.size()) {
569 pos = s.find("$", pos);
570 if (pos == std::string::npos) break;
571 for (const auto& subst : substs) {
572 const std::string& token = subst.first;
573 const std::string& replacement = subst.second;
574 if (s.substr(pos + 1, token.size()) == token) {
575 s.replace(pos, token.size() + 1, replacement);
576 // -1 to compensate for the ++pos below.
577 pos += replacement.size() - 1;
578 break;
579 } else if (s.substr(pos + 1, token.size() + 2) == "{" + token + "}") {
580 s.replace(pos, token.size() + 3, replacement);
581 pos += replacement.size() - 1;
582 break;
583 }
584 }
585 // Skip $ in case it did not match any of the known substitutions.
586 ++pos;
587 }
588 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700589
590 resolve_paths(runpaths, &dt_runpath_);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700591}
592
Elliott Hughescade4c32012-12-20 14:42:14 -0800593static void parse_LD_PRELOAD(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700594 g_ld_preload_names.clear();
595 if (path != nullptr) {
596 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
597 g_ld_preload_names = android::base::Split(path, " :");
598 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800599}
600
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700601static bool realpath_fd(int fd, std::string* realpath) {
602 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700603 __libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700604 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -0700605 PRINT("readlink('%s') failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700606 return false;
607 }
608
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700609 *realpath = &buf[0];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700610 return true;
611}
612
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700613#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700614
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700615// For a given PC, find the .so that it belongs to.
616// Returns the base address of the .ARM.exidx section
617// for that .so, and the number of 8-byte entries
618// in that section (via *pcount).
619//
620// Intended to be called by libc's __gnu_Unwind_Find_exidx().
621//
622// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800623_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800624 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800625
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700626 for (soinfo* si = solist; si != 0; si = si->next) {
627 if ((addr >= si->base) && (addr < (si->base + si->size))) {
628 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800629 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800630 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700631 }
632 *pcount = 0;
633 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800634}
Elliott Hughes46882792012-08-03 16:49:39 -0700635
Christopher Ferris24053a42013-08-19 17:45:09 -0700636#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700637
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700638// Here, we only have to provide a callback to iterate across all the
639// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700640int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700641 int rv = 0;
642 for (soinfo* si = solist; si != nullptr; si = si->next) {
643 dl_phdr_info dl_info;
644 dl_info.dlpi_addr = si->link_map_head.l_addr;
645 dl_info.dlpi_name = si->link_map_head.l_name;
646 dl_info.dlpi_phdr = si->phdr;
647 dl_info.dlpi_phnum = si->phnum;
648 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
649 if (rv != 0) {
650 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800651 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700652 }
653 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800654}
Elliott Hughes46882792012-08-03 16:49:39 -0700655
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700656const ElfW(Versym)* soinfo::get_versym(size_t n) const {
657 if (has_min_version(2) && versym_ != nullptr) {
658 return versym_ + n;
659 }
660
661 return nullptr;
662}
663
664ElfW(Addr) soinfo::get_verneed_ptr() const {
665 if (has_min_version(2)) {
666 return verneed_ptr_;
667 }
668
669 return 0;
670}
671
672size_t soinfo::get_verneed_cnt() const {
673 if (has_min_version(2)) {
674 return verneed_cnt_;
675 }
676
677 return 0;
678}
679
680ElfW(Addr) soinfo::get_verdef_ptr() const {
681 if (has_min_version(2)) {
682 return verdef_ptr_;
683 }
684
685 return 0;
686}
687
688size_t soinfo::get_verdef_cnt() const {
689 if (has_min_version(2)) {
690 return verdef_cnt_;
691 }
692
693 return 0;
694}
695
696template<typename F>
697static bool for_each_verdef(const soinfo* si, F functor) {
698 if (!si->has_min_version(2)) {
699 return true;
700 }
701
702 uintptr_t verdef_ptr = si->get_verdef_ptr();
703 if (verdef_ptr == 0) {
704 return true;
705 }
706
707 size_t offset = 0;
708
709 size_t verdef_cnt = si->get_verdef_cnt();
710 for (size_t i = 0; i<verdef_cnt; ++i) {
711 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
712 size_t verdaux_offset = offset + verdef->vd_aux;
713 offset += verdef->vd_next;
714
715 if (verdef->vd_version != 1) {
Dmitriy Ivanov3d7bea12015-04-20 17:40:39 -0700716 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700717 i, verdef->vd_version, si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700718 return false;
719 }
720
721 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
722 // "this is the version of the file itself. It must not be used for
723 // matching a symbol. It can be used to match references."
724 //
725 // http://www.akkadia.org/drepper/symbol-versioning
726 continue;
727 }
728
729 if (verdef->vd_cnt == 0) {
730 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
731 return false;
732 }
733
734 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
735
736 if (functor(i, verdef, verdaux) == true) {
737 break;
738 }
739 }
740
741 return true;
742}
743
744bool soinfo::find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const {
745 if (vi == nullptr) {
746 *versym = kVersymNotNeeded;
747 return true;
748 }
749
750 *versym = kVersymGlobal;
751
752 return for_each_verdef(this,
753 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
754 if (verdef->vd_hash == vi->elf_hash &&
755 strcmp(vi->name, get_string(verdaux->vda_name)) == 0) {
756 *versym = verdef->vd_ndx;
757 return true;
758 }
759
760 return false;
761 }
762 );
763}
764
765bool soinfo::find_symbol_by_name(SymbolName& symbol_name,
766 const version_info* vi,
767 const ElfW(Sym)** symbol) const {
768 uint32_t symbol_index;
769 bool success =
770 is_gnu_hash() ?
771 gnu_lookup(symbol_name, vi, &symbol_index) :
772 elf_lookup(symbol_name, vi, &symbol_index);
773
774 if (success) {
775 *symbol = symbol_index == 0 ? nullptr : symtab_ + symbol_index;
776 }
777
778 return success;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800779}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800780
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800781static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
782 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
783 ELF_ST_BIND(s->st_info) == STB_WEAK) {
784 return s->st_shndx != SHN_UNDEF;
785 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
786 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700787 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800788 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800789
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800790 return false;
791}
792
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700793static const ElfW(Versym) kVersymHiddenBit = 0x8000;
794
795static inline bool is_versym_hidden(const ElfW(Versym)* versym) {
796 // the symbol is hidden if bit 15 of versym is set.
797 return versym != nullptr && (*versym & kVersymHiddenBit) != 0;
798}
799
800static inline bool check_symbol_version(const ElfW(Versym) verneed,
801 const ElfW(Versym)* verdef) {
802 return verneed == kVersymNotNeeded ||
803 verdef == nullptr ||
804 verneed == (*verdef & ~kVersymHiddenBit);
805}
806
807bool soinfo::gnu_lookup(SymbolName& symbol_name,
808 const version_info* vi,
809 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800810 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800811 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800812
813 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800814 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
815 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800816
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700817 *symbol_index = 0;
818
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700819 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700820 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700821
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800822 // test against bloom filter
823 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700824 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700825 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700826
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700827 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800828 }
829
830 // bloom test says "probably yes"...
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700831 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800832
833 if (n == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700834 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700835 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700836
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700837 return true;
838 }
839
840 // lookup versym for the version definition in this library
841 // note the difference between "version is not requested" (vi == nullptr)
842 // and "version not found". In the first case verneed is kVersymNotNeeded
843 // which implies that the default version can be accepted; the second case results in
844 // verneed = 1 (kVersymGlobal) and implies that we should ignore versioned symbols
845 // for this library and consider only *global* ones.
846 ElfW(Versym) verneed = 0;
847 if (!find_verdef_version_index(vi, &verneed)) {
848 return false;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800849 }
850
851 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800852 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700853 const ElfW(Versym)* verdef = get_versym(n);
854 // skip hidden versions when verneed == kVersymNotNeeded (0)
855 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
856 continue;
857 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700858 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700859 check_symbol_version(verneed, verdef) &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800860 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
861 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700862 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700863 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(s->st_value),
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700864 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700865 *symbol_index = n;
866 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800867 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700868 } while ((gnu_chain_[n++] & 1) == 0);
869
870 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700871 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800872
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700873 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800874}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800875
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700876bool soinfo::elf_lookup(SymbolName& symbol_name,
877 const version_info* vi,
878 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800879 uint32_t hash = symbol_name.elf_hash();
880
881 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700882 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700883 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800884
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700885 ElfW(Versym) verneed = 0;
886 if (!find_verdef_version_index(vi, &verneed)) {
887 return false;
888 }
889
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800890 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
891 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700892 const ElfW(Versym)* verdef = get_versym(n);
893
894 // skip hidden versions when verneed == 0
895 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
896 continue;
897 }
898
899 if (check_symbol_version(verneed, verdef) &&
900 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700901 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800902 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700903 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700904 reinterpret_cast<void*>(s->st_value),
905 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700906 *symbol_index = n;
907 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800908 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800909 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800910
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700911 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700912 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700913 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700914
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700915 *symbol_index = 0;
916 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800917}
918
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700919soinfo::soinfo(android_namespace_t* ns, const char* realpath,
920 const struct stat* file_stat, off64_t file_offset,
921 int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700922 memset(this, 0, sizeof(*this));
923
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700924 if (realpath != nullptr) {
925 realpath_ = realpath;
926 }
927
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800928 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800929 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700930
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700931 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800932 this->st_dev_ = file_stat->st_dev;
933 this->st_ino_ = file_stat->st_ino;
934 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700935 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700936
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800937 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700938 this->namespace_ = ns;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700939}
940
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800941static uint32_t calculate_elf_hash(const char* name) {
942 const uint8_t* name_bytes = reinterpret_cast<const uint8_t*>(name);
943 uint32_t h = 0, g;
944
945 while (*name_bytes) {
946 h = (h << 4) + *name_bytes++;
947 g = h & 0xf0000000;
948 h ^= g;
949 h ^= g >> 24;
950 }
951
952 return h;
953}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800954
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800955uint32_t SymbolName::elf_hash() {
956 if (!has_elf_hash_) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800957 elf_hash_ = calculate_elf_hash(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800958 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700959 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800960
961 return elf_hash_;
962}
963
964uint32_t SymbolName::gnu_hash() {
965 if (!has_gnu_hash_) {
966 uint32_t h = 5381;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700967 const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800968 while (*name != 0) {
969 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
970 }
971
972 gnu_hash_ = h;
973 has_gnu_hash_ = true;
974 }
975
976 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800977}
978
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700979bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
980 soinfo** si_found_in, const soinfo::soinfo_list_t& global_group,
981 const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800982 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700983 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700984
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700985 /* "This element's presence in a shared object library alters the dynamic linker's
986 * symbol resolution algorithm for references within the library. Instead of starting
987 * a symbol search with the executable file, the dynamic linker starts from the shared
988 * object itself. If the shared object fails to supply the referenced symbol, the
989 * dynamic linker then searches the executable file and other shared objects as usual."
990 *
991 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
992 *
993 * Note that this is unlikely since static linker avoids generating
994 * relocations for -Bsymbolic linked dynamic executables.
995 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700996 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700997 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700998 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
999 return false;
1000 }
1001
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07001002 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001003 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07001004 }
1005 }
1006
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001007 // 1. Look for it in global_group
1008 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001009 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001010 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001011 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001012 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001013 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
1014 error = true;
1015 return false;
1016 }
1017
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07001018 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001019 *si_found_in = global_si;
1020 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07001021 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -07001022
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001023 return true;
1024 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001025
1026 if (error) {
1027 return false;
1028 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07001029 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001030
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001031 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001032 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001033 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001034 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001035 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -07001036 // we already did this - skip
1037 return true;
1038 }
1039
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001040 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001041 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001042 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
1043 error = true;
1044 return false;
1045 }
1046
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001047 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001048 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001049 return false;
1050 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001051
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001052 return true;
1053 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001054
1055 if (error) {
1056 return false;
1057 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001058 }
1059
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001060 if (s != nullptr) {
1061 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
1062 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001063 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
1064 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001065 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001066 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001067
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001068 *symbol = s;
1069 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001070}
1071
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001072class ProtectedDataGuard {
1073 public:
1074 ProtectedDataGuard() {
1075 if (ref_count_++ == 0) {
1076 protect_data(PROT_READ | PROT_WRITE);
1077 }
1078 }
1079
1080 ~ProtectedDataGuard() {
1081 if (ref_count_ == 0) { // overflow
1082 __libc_fatal("Too many nested calls to dlopen()");
1083 }
1084
1085 if (--ref_count_ == 0) {
1086 protect_data(PROT_READ);
1087 }
1088 }
1089 private:
1090 void protect_data(int protection) {
1091 g_soinfo_allocator.protect_all(protection);
1092 g_soinfo_links_allocator.protect_all(protection);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001093 g_namespace_allocator.protect_all(protection);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001094 }
1095
1096 static size_t ref_count_;
1097};
1098
1099size_t ProtectedDataGuard::ref_count_ = 0;
1100
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001101// Each size has it's own allocator.
1102template<size_t size>
1103class SizeBasedAllocator {
1104 public:
1105 static void* alloc() {
1106 return allocator_.alloc();
1107 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001108
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001109 static void free(void* ptr) {
1110 allocator_.free(ptr);
1111 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001112
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001113 private:
1114 static LinkerBlockAllocator allocator_;
1115};
1116
1117template<size_t size>
1118LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
1119
1120template<typename T>
1121class TypeBasedAllocator {
1122 public:
1123 static T* alloc() {
1124 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
1125 }
1126
1127 static void free(T* ptr) {
1128 SizeBasedAllocator<sizeof(T)>::free(ptr);
1129 }
1130};
1131
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001132class LoadTask {
1133 public:
1134 struct deleter_t {
1135 void operator()(LoadTask* t) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001136 t->~LoadTask();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001137 TypeBasedAllocator<LoadTask>::free(t);
1138 }
1139 };
1140
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001141 static deleter_t deleter;
1142
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001143 static LoadTask* create(const char* name, soinfo* needed_by,
1144 std::unordered_map<const soinfo*, ElfReader>* readers_map) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001145 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001146 return new (ptr) LoadTask(name, needed_by, readers_map);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001147 }
1148
1149 const char* get_name() const {
1150 return name_;
1151 }
1152
1153 soinfo* get_needed_by() const {
1154 return needed_by_;
1155 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001156
1157 soinfo* get_soinfo() const {
1158 return si_;
1159 }
1160
1161 void set_soinfo(soinfo* si) {
1162 si_ = si;
1163 }
1164
1165 off64_t get_file_offset() const {
1166 return file_offset_;
1167 }
1168
1169 void set_file_offset(off64_t offset) {
1170 file_offset_ = offset;
1171 }
1172
1173 int get_fd() const {
1174 return fd_;
1175 }
1176
1177 void set_fd(int fd, bool assume_ownership) {
1178 fd_ = fd;
1179 close_fd_ = assume_ownership;
1180 }
1181
1182 const android_dlextinfo* get_extinfo() const {
1183 return extinfo_;
1184 }
1185
1186 void set_extinfo(const android_dlextinfo* extinfo) {
1187 extinfo_ = extinfo;
1188 }
1189
Dimitry Ivanov10057482016-01-28 17:24:20 -08001190 bool is_dt_needed() const {
1191 return is_dt_needed_;
1192 }
1193
1194 void set_dt_needed(bool is_dt_needed) {
1195 is_dt_needed_ = is_dt_needed;
1196 }
1197
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001198 const ElfReader& get_elf_reader() const {
1199 CHECK(si_ != nullptr);
1200 return (*elf_readers_map_)[si_];
1201 }
1202
1203 ElfReader& get_elf_reader() {
1204 CHECK(si_ != nullptr);
1205 return (*elf_readers_map_)[si_];
1206 }
1207
1208 std::unordered_map<const soinfo*, ElfReader>* get_readers_map() {
1209 return elf_readers_map_;
1210 }
1211
1212 bool read(const char* realpath, off64_t file_size) {
1213 ElfReader& elf_reader = get_elf_reader();
1214 return elf_reader.Read(realpath, fd_, file_offset_, file_size);
1215 }
1216
1217 bool load() {
1218 ElfReader& elf_reader = get_elf_reader();
1219 if (!elf_reader.Load(extinfo_)) {
1220 return false;
1221 }
1222
1223 si_->base = elf_reader.load_start();
1224 si_->size = elf_reader.load_size();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -08001225 si_->set_mapped_by_caller(elf_reader.is_mapped_by_caller());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001226 si_->load_bias = elf_reader.load_bias();
1227 si_->phnum = elf_reader.phdr_count();
1228 si_->phdr = elf_reader.loaded_phdr();
1229
1230 return true;
1231 }
1232
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001233 private:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001234 LoadTask(const char* name, soinfo* needed_by,
1235 std::unordered_map<const soinfo*, ElfReader>* readers_map)
1236 : name_(name), needed_by_(needed_by), si_(nullptr),
Dimitry Ivanov10057482016-01-28 17:24:20 -08001237 fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map),
1238 is_dt_needed_(false) {}
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001239
1240 ~LoadTask() {
1241 if (fd_ != -1 && close_fd_) {
1242 close(fd_);
1243 }
1244 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001245
1246 const char* name_;
1247 soinfo* needed_by_;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001248 soinfo* si_;
1249 const android_dlextinfo* extinfo_;
1250 int fd_;
1251 bool close_fd_;
1252 off64_t file_offset_;
1253 std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_;
Dimitry Ivanov10057482016-01-28 17:24:20 -08001254 // TODO(dimitry): workaround for http://b/26394120 - will be removed before the release
1255 bool is_dt_needed_;
1256 // END OF WORKAROUND
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001257
1258 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
1259};
1260
Ningsheng Jiane93be992014-09-16 15:22:10 +08001261LoadTask::deleter_t LoadTask::deleter;
1262
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001263template <typename T>
1264using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
1265
1266typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001267typedef linked_list_t<const char> StringLinkedList;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001268typedef std::vector<LoadTask*> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001269
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001270
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001271// This function walks down the tree of soinfo dependencies
1272// in breadth-first order and
1273// * calls action(soinfo* si) for each node, and
1274// * terminates walk if action returns false.
1275//
1276// walk_dependencies_tree returns false if walk was terminated
1277// by the action and true otherwise.
1278template<typename F>
1279static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001280 SoinfoLinkedList visit_list;
1281 SoinfoLinkedList visited;
1282
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001283 for (size_t i = 0; i < root_soinfos_size; ++i) {
1284 visit_list.push_back(root_soinfos[i]);
1285 }
1286
1287 soinfo* si;
1288 while ((si = visit_list.pop_front()) != nullptr) {
1289 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -07001290 continue;
1291 }
1292
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001293 if (!action(si)) {
1294 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001295 }
1296
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001297 visited.push_back(si);
1298
1299 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001300 visit_list.push_back(child);
1301 });
1302 }
1303
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001304 return true;
1305}
1306
1307
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001308static const ElfW(Sym)* dlsym_handle_lookup(soinfo* root, soinfo* skip_until,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001309 soinfo** found, SymbolName& symbol_name,
1310 const version_info* vi) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001311 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001312 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001313
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001314 walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) {
1315 if (skip_lookup) {
1316 skip_lookup = current_soinfo != skip_until;
1317 return true;
1318 }
1319
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001320 if (!current_soinfo->find_symbol_by_name(symbol_name, vi, &result)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001321 result = nullptr;
1322 return false;
1323 }
1324
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001325 if (result != nullptr) {
1326 *found = current_soinfo;
1327 return false;
1328 }
1329
1330 return true;
1331 });
1332
1333 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001334}
1335
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001336static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
1337 const char* name,
1338 const version_info* vi,
1339 soinfo** found,
1340 soinfo* caller,
1341 void* handle);
1342
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001343// This is used by dlsym(3). It performs symbol lookup only within the
1344// specified soinfo object and its dependencies in breadth first order.
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001345static const ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found,
1346 const char* name, const version_info* vi) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -07001347 // According to man dlopen(3) and posix docs in the case when si is handle
1348 // of the main executable we need to search not only in the executable and its
1349 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
1350 //
1351 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
1352 // libraries and they are loaded in breath-first (correct) order we can just execute
1353 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
1354 if (si == somain) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001355 return dlsym_linear_lookup(&g_default_namespace, name, vi, found, nullptr, RTLD_DEFAULT);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -07001356 }
1357
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001358 SymbolName symbol_name(name);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001359 return dlsym_handle_lookup(si, nullptr, found, symbol_name, vi);
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001360}
1361
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001362/* This is used by dlsym(3) to performs a global symbol lookup. If the
1363 start value is null (for RTLD_DEFAULT), the search starts at the
1364 beginning of the global solist. Otherwise the search starts at the
1365 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001366 */
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001367static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
1368 const char* name,
1369 const version_info* vi,
1370 soinfo** found,
1371 soinfo* caller,
1372 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001373 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001374
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001375 soinfo::soinfo_list_t& soinfo_list = ns->soinfo_list();
1376 soinfo::soinfo_list_t::iterator start = soinfo_list.begin();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001377
1378 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -07001379 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001380 return nullptr;
1381 } else {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001382 soinfo::soinfo_list_t::iterator it = soinfo_list.find(caller);
1383 CHECK (it != soinfo_list.end());
1384 start = ++it;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001385 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001386 }
1387
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001388 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001389 for (soinfo::soinfo_list_t::iterator it = start, end = soinfo_list.end(); it != end; ++it) {
1390 soinfo* si = *it;
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001391 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
1392 // if the library is opened by application with target api level <= 22
1393 // See http://b/21565766
1394 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 && si->get_target_sdk_version() > 22) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001395 continue;
1396 }
1397
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001398 if (!si->find_symbol_by_name(symbol_name, vi, &s)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001399 return nullptr;
1400 }
1401
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001402 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -08001403 *found = si;
1404 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -06001405 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001406 }
Matt Fischer1698d9e2009-12-31 12:17:56 -06001407
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001408 // If not found - use dlsym_handle_lookup for caller's
1409 // local_group unless it is part of the global group in which
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001410 // case we already did it.
1411 if (s == nullptr && caller != nullptr &&
1412 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001413 return dlsym_handle_lookup(caller->get_local_group_root(),
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001414 (handle == RTLD_NEXT) ? caller : nullptr, found, symbol_name, vi);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001415 }
1416
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001417 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001418 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
1419 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -08001420 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001421
Elliott Hughescade4c32012-12-20 14:42:14 -08001422 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001423}
1424
Kito Chengfa8c05d2013-03-12 14:58:06 +08001425soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001426 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001427 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001428 if (address >= si->base && address - si->base < si->size) {
1429 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001430 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001431 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001432 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001433}
1434
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001435ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
1436 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
1437}
1438
1439static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
1440 return sym->st_shndx != SHN_UNDEF &&
1441 soaddr >= sym->st_value &&
1442 soaddr < sym->st_value + sym->st_size;
1443}
1444
1445ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001446 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001447
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001448 for (size_t i = 0; i < gnu_nbucket_; ++i) {
1449 uint32_t n = gnu_bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001450
1451 if (n == 0) {
1452 continue;
1453 }
1454
1455 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001456 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001457 if (symbol_matches_soaddr(sym, soaddr)) {
1458 return sym;
1459 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001460 } while ((gnu_chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001461 }
1462
1463 return nullptr;
1464}
1465
1466ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001467 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001468
Kito Chengfa8c05d2013-03-12 14:58:06 +08001469 // Search the library's symbol table for any defined symbol which
1470 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001471 for (size_t i = 0; i < nchain_; ++i) {
1472 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001473 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001474 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001475 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001476 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001477
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001478 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001479}
1480
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001481class ZipArchiveCache {
1482 public:
1483 ZipArchiveCache() {}
1484 ~ZipArchiveCache();
1485
1486 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
1487 private:
1488 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
1489
1490 std::unordered_map<std::string, ZipArchiveHandle> cache_;
1491};
1492
1493bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
1494 std::string key(zip_path);
1495
1496 auto it = cache_.find(key);
1497 if (it != cache_.end()) {
1498 *handle = it->second;
1499 return true;
1500 }
1501
1502 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1503 if (fd == -1) {
1504 return false;
1505 }
1506
1507 if (OpenArchiveFd(fd, "", handle) != 0) {
1508 // invalid zip-file (?)
1509 close(fd);
1510 return false;
1511 }
1512
1513 cache_[key] = *handle;
1514 return true;
1515}
1516
1517ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -07001518 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001519 CloseArchive(it.second);
1520 }
1521}
1522
1523static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001524 const char* const input_path,
1525 off64_t* file_offset, std::string* realpath) {
1526 std::string normalized_path;
1527 if (!normalize_path(input_path, &normalized_path)) {
1528 return -1;
1529 }
1530
1531 const char* const path = normalized_path.c_str();
1532 TRACE("Trying zip file open from path '%s' -> normalized '%s'", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +00001533
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001534 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +00001535 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001536 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +00001537 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001538 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +00001539 if (separator == nullptr) {
1540 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -07001541 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001542
1543 char buf[512];
1544 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
1545 PRINT("Warning: ignoring very long library path: %s", path);
1546 return -1;
1547 }
1548
1549 buf[separator - path] = '\0';
1550
1551 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001552 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +00001553 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1554 if (fd == -1) {
1555 return -1;
1556 }
1557
1558 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001559 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001560 // invalid zip-file (?)
1561 close(fd);
1562 return -1;
1563 }
1564
Simon Baldwinaef71952015-01-16 13:22:54 +00001565 ZipEntry entry;
1566
Yusuke Sato56f40fb2015-06-25 14:56:07 -07001567 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001568 // Entry was not found.
1569 close(fd);
1570 return -1;
1571 }
1572
1573 // Check if it is properly stored
1574 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
1575 close(fd);
1576 return -1;
1577 }
1578
1579 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001580
1581 if (realpath_fd(fd, realpath)) {
1582 *realpath += separator;
1583 } else {
1584 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1585 normalized_path.c_str());
1586 *realpath = normalized_path;
1587 }
1588
Simon Baldwinaef71952015-01-16 13:22:54 +00001589 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001590}
1591
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001592static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
1593 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
1594 if (n < 0 || n >= static_cast<int>(buf_size)) {
1595 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1596 return false;
1597 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001598
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001599 return true;
1600}
1601
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001602static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1603 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001604 const std::vector<std::string>& paths,
1605 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001606 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001607 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001608 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001609 continue;
1610 }
1611
1612 int fd = -1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001613 if (strstr(buf, kZipFileSeparator) != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001614 fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath);
Simon Baldwinaef71952015-01-16 13:22:54 +00001615 }
1616
1617 if (fd == -1) {
1618 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1619 if (fd != -1) {
1620 *file_offset = 0;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001621 if (!realpath_fd(fd, realpath)) {
1622 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
1623 *realpath = buf;
1624 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001625 }
1626 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001627
1628 if (fd != -1) {
1629 return fd;
1630 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001631 }
1632
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001633 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001634}
1635
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001636static int open_library(android_namespace_t* ns,
1637 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001638 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001639 off64_t* file_offset, std::string* realpath) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001640 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001641
Elliott Hughes124fae92012-10-31 14:20:03 -07001642 // If the name contains a slash, we should attempt to open it directly and not search the paths.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001643 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001644 int fd = -1;
1645
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001646 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001647 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1648 }
1649
1650 if (fd == -1) {
1651 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001652 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001653 *file_offset = 0;
1654 if (!realpath_fd(fd, realpath)) {
1655 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name);
1656 *realpath = name;
1657 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001658 }
1659 }
1660
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001661 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001662 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001663
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001664 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1665 int fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_ld_library_paths(), realpath);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001666 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001667 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001668 // Check if the library is accessible
1669 if (fd != -1 && !ns->is_accessible(*realpath)) {
1670 fd = -1;
1671 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001672 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001673
Elliott Hughes124fae92012-10-31 14:20:03 -07001674 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001675 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001676 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001677
Dimitry Ivanova8bda262016-01-07 11:16:43 -08001678 // TODO(dimitry): workaround for http://b/26394120 - will be removed before the release
Dimitry Ivanov36ac45f2016-01-07 18:43:20 -08001679 if (fd == -1 && ns != &g_default_namespace && is_greylisted(name, needed_by)) {
Dimitry Ivanova8bda262016-01-07 11:16:43 -08001680 // try searching for it on default_namespace default_library_path
1681 fd = open_library_on_paths(zip_archive_cache, name, file_offset,
1682 g_default_namespace.get_default_library_paths(), realpath);
1683 }
1684 // END OF WORKAROUND
1685
Elliott Hughes124fae92012-10-31 14:20:03 -07001686 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001687}
1688
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001689static const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
1690#if !defined(__LP64__)
1691 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001692 if (get_application_target_sdk_version() <= 22) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001693 const char* bname = basename(dt_needed);
1694 if (bname != dt_needed) {
1695 DL_WARN("'%s' library has invalid DT_NEEDED entry '%s'", sopath, dt_needed);
1696 }
1697
1698 return bname;
1699 }
1700#endif
1701 return dt_needed;
1702}
1703
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001704template<typename F>
1705static void for_each_dt_needed(const soinfo* si, F action) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001706 for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001707 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001708 action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
Dima Zavin2e855792009-05-20 18:28:09 -07001709 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001710 }
1711}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001712
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001713template<typename F>
1714static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
1715 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1716 if (d->d_tag == DT_NEEDED) {
1717 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1718 }
1719 }
1720}
1721
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001722static bool load_library(android_namespace_t* ns,
1723 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001724 LoadTaskList* load_tasks,
1725 int rtld_flags,
1726 const std::string& realpath) {
1727 off64_t file_offset = task->get_file_offset();
1728 const char* name = task->get_name();
1729 const android_dlextinfo* extinfo = task->get_extinfo();
1730
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001731 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001732 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001733 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001734 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001735 if (file_offset < 0) {
1736 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001737 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001738 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001739
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001740 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001741 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001742 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001743 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001744 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001745 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001746 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1747 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001748 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001749 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001750
1751 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001752 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1753 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001754 auto predicate = [&](soinfo* si) {
1755 return si->get_st_dev() != 0 &&
1756 si->get_st_ino() != 0 &&
1757 si->get_st_dev() == file_stat.st_dev &&
1758 si->get_st_ino() == file_stat.st_ino &&
1759 si->get_file_offset() == file_offset;
1760 };
1761
1762 soinfo* si = ns->soinfo_list().find_if(predicate);
1763
1764 // check public namespace
1765 if (si == nullptr) {
1766 si = g_public_namespace.find_if(predicate);
1767 if (si != nullptr) {
1768 ns->soinfo_list().push_back(si);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001769 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001770 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001771
1772 if (si != nullptr) {
1773 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
1774 "will return existing soinfo", name, si->get_realpath());
1775 task->set_soinfo(si);
1776 return true;
1777 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001778 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001779
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001780 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001781 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001782 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001783 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001784
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001785 if (!ns->is_accessible(realpath)) {
Dimitry Ivanova8bda262016-01-07 11:16:43 -08001786 // TODO(dimitry): workaround for http://b/26394120 - will be removed before the release
Dimitry Ivanov10057482016-01-28 17:24:20 -08001787 const soinfo* needed_by = task->is_dt_needed() ? task->get_needed_by() : nullptr;
Dimitry Ivanov36ac45f2016-01-07 18:43:20 -08001788 if (is_greylisted(name, needed_by)) {
1789 // print warning only if needed by non-system library
1790 if (needed_by == nullptr || !is_system_library(needed_by->get_realpath())) {
Dimitry Ivanovdc2bee92016-01-12 09:50:21 -08001791 DL_WARN("library \"%s\" (\"%s\") needed by \"%s\" is not accessible for the namespace \"%s\""
1792 " - the access is temporarily granted as a workaround for http://b/26394120",
1793 name, realpath.c_str(), needed_by->get_realpath(), ns->get_name());
Dimitry Ivanov36ac45f2016-01-07 18:43:20 -08001794 }
Dimitry Ivanova8bda262016-01-07 11:16:43 -08001795 } else {
1796 // do not load libraries if they are not accessible for the specified namespace.
1797 DL_ERR("library \"%s\" is not accessible for the namespace \"%s\"",
1798 name, ns->get_name());
1799 return false;
1800 }
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001801 }
1802
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001803 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001804 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001805 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001806 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001807
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001808 task->set_soinfo(si);
1809
1810 // Read the ELF header and some of the segments.
1811 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001812 soinfo_free(si);
1813 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001814 return false;
1815 }
1816
1817 // find and set DT_RUNPATH and dt_soname
1818 // Note that these field values are temporary and are
1819 // going to be overwritten on soinfo::prelink_image
1820 // with values from PT_LOAD segments.
1821 const ElfReader& elf_reader = task->get_elf_reader();
1822 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1823 if (d->d_tag == DT_RUNPATH) {
1824 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1825 }
1826 if (d->d_tag == DT_SONAME) {
1827 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1828 }
1829 }
1830
1831 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
1832 load_tasks->push_back(LoadTask::create(name, si, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001833 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001834
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001835 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001836}
1837
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001838static bool load_library(android_namespace_t* ns,
1839 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001840 ZipArchiveCache* zip_archive_cache,
1841 LoadTaskList* load_tasks,
1842 int rtld_flags) {
1843 const char* name = task->get_name();
1844 soinfo* needed_by = task->get_needed_by();
1845 const android_dlextinfo* extinfo = task->get_extinfo();
1846
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001847 off64_t file_offset;
1848 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001849 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001850 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001851 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1852 file_offset = extinfo->library_fd_offset;
1853 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001854
1855 if (!realpath_fd(extinfo->library_fd, &realpath)) {
1856 PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1857 "Will use given name.", name);
1858 realpath = name;
1859 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001860
1861 task->set_fd(extinfo->library_fd, false);
1862 task->set_file_offset(file_offset);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001863 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001864 }
1865
1866 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001867 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001868 if (fd == -1) {
1869 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001870 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001871 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001872
1873 task->set_fd(fd, true);
1874 task->set_file_offset(file_offset);
1875
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001876 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001877}
1878
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001879// Returns true if library was found and false in 2 cases
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001880// 1. (for default namespace only) The library was found but loaded under different
1881// target_sdk_version (*candidate != nullptr)
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001882// 2. The library was not found by soname (*candidate is nullptr)
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001883static bool find_loaded_library_by_soname(android_namespace_t* ns,
1884 const char* name, soinfo** candidate) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001885 *candidate = nullptr;
1886
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001887 // Ignore filename with path.
1888 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001889 return false;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001890 }
1891
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001892 uint32_t target_sdk_version = get_application_target_sdk_version();
1893
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001894 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001895 const char* soname = si->get_soname();
1896 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001897 // If the library was opened under different target sdk version
1898 // skip this step and try to reopen it. The exceptions are
1899 // "libdl.so" and global group. There is no point in skipping
1900 // them because relocation process is going to use them
1901 // in any case.
1902 bool is_libdl = si == solist;
1903 if (is_libdl || (si->get_dt_flags_1() & DF_1_GLOBAL) != 0 ||
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001904 !si->is_linked() || si->get_target_sdk_version() == target_sdk_version ||
1905 ns != &g_default_namespace) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001906 *candidate = si;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001907 return false;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001908 } else if (*candidate == nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001909 // for the different sdk version in the default namespace
1910 // remember the first library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001911 *candidate = si;
1912 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001913 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001914
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001915 return true;
1916 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001917}
1918
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001919static bool find_library_internal(android_namespace_t* ns,
1920 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001921 ZipArchiveCache* zip_archive_cache,
1922 LoadTaskList* load_tasks,
1923 int rtld_flags) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001924 soinfo* candidate;
1925
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001926 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001927 task->set_soinfo(candidate);
1928 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001929 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001930
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001931 if (ns != &g_default_namespace) {
1932 // check public namespace
1933 candidate = g_public_namespace.find_if([&](soinfo* si) {
1934 return strcmp(task->get_name(), si->get_soname()) == 0;
1935 });
1936
1937 if (candidate != nullptr) {
1938 ns->soinfo_list().push_back(candidate);
1939 task->set_soinfo(candidate);
1940 return true;
1941 }
1942 }
1943
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001944 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001945 // of this fact is done by load_library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001946 TRACE("[ '%s' find_loaded_library_by_soname returned false (*candidate=%s@%p). Trying harder...]",
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001947 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001948
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001949 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001950 return true;
1951 } else {
1952 // In case we were unable to load the library but there
1953 // is a candidate loaded under the same soname but different
1954 // sdk level - return it anyways.
1955 if (candidate != nullptr) {
1956 task->set_soinfo(candidate);
1957 return true;
1958 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001959 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001960
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001961 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001962}
1963
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001964static void soinfo_unload(soinfo* si);
1965
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001966// TODO: this is slightly unusual way to construct
1967// the global group for relocation. Not every RTLD_GLOBAL
1968// library is included in this group for backwards-compatibility
1969// reasons.
1970//
1971// This group consists of the main executable, LD_PRELOADs
1972// and libraries with the DF_1_GLOBAL flag set.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001973static soinfo::soinfo_list_t make_global_group(android_namespace_t* ns) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001974 soinfo::soinfo_list_t global_group;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001975 ns->soinfo_list().for_each([&](soinfo* si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001976 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1977 global_group.push_back(si);
1978 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001979 });
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001980
1981 return global_group;
1982}
1983
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001984static void shuffle(std::vector<LoadTask*>* v) {
1985 for (size_t i = 0, size = v->size(); i < size; ++i) {
1986 size_t n = size - i;
1987 size_t r = arc4random_uniform(n);
1988 std::swap((*v)[n-1], (*v)[r]);
1989 }
1990}
1991
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001992// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1993// not their transitive dependencies) as children of the start_with library.
1994// This is false when find_libraries is called for dlopen(), when newly loaded
1995// libraries must form a disjoint tree.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001996static bool find_libraries(android_namespace_t* ns,
1997 soinfo* start_with,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001998 const char* const library_names[],
1999 size_t library_names_count, soinfo* soinfos[],
2000 std::vector<soinfo*>* ld_preloads,
2001 size_t ld_preloads_count, int rtld_flags,
2002 const android_dlextinfo* extinfo,
2003 bool add_as_children) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002004 // Step 0: prepare.
2005 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002006 std::unordered_map<const soinfo*, ElfReader> readers_map;
2007
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002008 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002009 const char* name = library_names[i];
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002010 load_tasks.push_back(LoadTask::create(name, start_with, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002011 }
2012
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002013 // Construct global_group.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002014 soinfo::soinfo_list_t global_group = make_global_group(ns);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002015
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002016 // If soinfos array is null allocate one on stack.
2017 // The array is needed in case of failure; for example
2018 // when library_names[] = {libone.so, libtwo.so} and libone.so
2019 // is loaded correctly but libtwo.so failed for some reason.
2020 // In this case libone.so should be unloaded on return.
2021 // See also implementation of failure_guard below.
2022
2023 if (soinfos == nullptr) {
2024 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
2025 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
2026 memset(soinfos, 0, soinfos_size);
2027 }
2028
2029 // list of libraries to link - see step 2.
2030 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002031
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002032 auto scope_guard = make_scope_guard([&]() {
2033 for (LoadTask* t : load_tasks) {
2034 LoadTask::deleter(t);
2035 }
2036 });
2037
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07002038 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002039 // Housekeeping
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002040 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002041 soinfo_unload(soinfos[i]);
2042 }
2043 });
2044
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07002045 ZipArchiveCache zip_archive_cache;
2046
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002047 // Step 1: expand the list of load_tasks to include
2048 // all DT_NEEDED libraries (do not load them just yet)
2049 for (size_t i = 0; i<load_tasks.size(); ++i) {
2050 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07002051 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002052
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07002053 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002054 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dimitry Ivanov10057482016-01-28 17:24:20 -08002055 task->set_dt_needed(is_dt_needed);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07002056
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002057 if(!find_library_internal(ns, task, &zip_archive_cache, &load_tasks, rtld_flags)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002058 return false;
2059 }
2060
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002061 soinfo* si = task->get_soinfo();
2062
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07002063 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002064 needed_by->add_child(si);
2065 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002066
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002067 if (si->is_linked()) {
2068 si->increment_ref_count();
2069 }
2070
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002071 // When ld_preloads is not null, the first
2072 // ld_preloads_count libs are in fact ld_preloads.
2073 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07002074 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002075 }
2076
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002077 if (soinfos_count < library_names_count) {
2078 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002079 }
2080 }
2081
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002082 // Step 2: Load libraries in random order (see b/24047022)
2083 LoadTaskList load_list;
2084 for (auto&& task : load_tasks) {
2085 soinfo* si = task->get_soinfo();
2086 auto pred = [&](const LoadTask* t) {
2087 return t->get_soinfo() == si;
2088 };
2089
2090 if (!si->is_linked() &&
2091 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
2092 load_list.push_back(task);
2093 }
2094 }
2095 shuffle(&load_list);
2096
2097 for (auto&& task : load_list) {
2098 if (!task->load()) {
2099 return false;
2100 }
2101 }
2102
2103 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
2104 for (auto&& task : load_tasks) {
2105 soinfo* si = task->get_soinfo();
2106 if (!si->is_linked() && !si->prelink_image()) {
2107 return false;
2108 }
2109 }
2110
2111 // Step 4: Add LD_PRELOADed libraries to the global group for
2112 // future runs. There is no need to explicitly add them to
2113 // the global group for this run because they are going to
2114 // appear in the local group in the correct order.
2115 if (ld_preloads != nullptr) {
2116 for (auto&& si : *ld_preloads) {
2117 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2118 }
2119 }
2120
2121
2122 // Step 5: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002123 soinfo::soinfo_list_t local_group;
2124 walk_dependencies_tree(
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002125 (start_with != nullptr && add_as_children) ? &start_with : soinfos,
2126 (start_with != nullptr && add_as_children) ? 1 : soinfos_count,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002127 [&] (soinfo* si) {
2128 local_group.push_back(si);
2129 return true;
2130 });
2131
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002132 // We need to increment ref_count in case
2133 // the root of the local group was not linked.
2134 bool was_local_group_root_linked = local_group.front()->is_linked();
2135
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002136 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002137 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002138 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002139 return false;
2140 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002141 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002142 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002143
2144 return true;
2145 });
2146
2147 if (linked) {
2148 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002149 }
2150
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002151 if (!was_local_group_root_linked) {
2152 local_group.front()->increment_ref_count();
2153 }
2154
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002155 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002156}
2157
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002158static soinfo* find_library(android_namespace_t* ns,
2159 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002160 const android_dlextinfo* extinfo,
2161 soinfo* needed_by) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002162 soinfo* si;
2163
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002164 if (name == nullptr) {
2165 si = somain;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002166 } else if (!find_libraries(ns, needed_by, &name, 1, &si, nullptr, 0, rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002167 extinfo, /* add_as_children */ false)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002168 return nullptr;
2169 }
2170
Elliott Hughesd23736e2012-11-01 15:16:56 -07002171 return si;
2172}
Elliott Hughesbedfe382012-08-14 14:07:59 -07002173
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002174static void soinfo_unload(soinfo* root) {
2175 // Note that the library can be loaded but not linked;
2176 // in which case there is no root but we still need
2177 // to walk the tree and unload soinfos involved.
2178 //
2179 // This happens on unsuccessful dlopen, when one of
2180 // the DT_NEEDED libraries could not be linked/found.
2181 if (root->is_linked()) {
2182 root = root->get_local_group_root();
2183 }
2184
2185 if (!root->can_unload()) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002186 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002187 return;
2188 }
2189
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002190 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002191
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002192 if (ref_count == 0) {
2193 soinfo::soinfo_list_t local_unload_list;
2194 soinfo::soinfo_list_t external_unload_list;
2195 soinfo::soinfo_list_t depth_first_list;
2196 depth_first_list.push_back(root);
2197 soinfo* si = nullptr;
2198
2199 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002200 if (local_unload_list.contains(si)) {
2201 continue;
2202 }
2203
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002204 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002205
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002206 if (si->has_min_version(0)) {
2207 soinfo* child = nullptr;
2208 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002209 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
2210 child->get_realpath(), child);
2211
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002212 if (local_unload_list.contains(child)) {
2213 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002214 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002215 external_unload_list.push_back(child);
2216 } else {
2217 depth_first_list.push_front(child);
2218 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002219 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002220 } else {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07002221#if !defined(__work_around_b_24465209__)
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002222 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002223#else
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002224 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002225 for_each_dt_needed(si, [&] (const char* library_name) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002226 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002227 si->get_realpath(), library_name);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002228
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002229 soinfo* needed = find_library(si->get_namespace(),
2230 library_name, RTLD_NOLOAD, nullptr, nullptr);
2231
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002232 if (needed != nullptr) {
2233 // Not found: for example if symlink was deleted between dlopen and dlclose
2234 // Since we cannot really handle errors at this point - print and continue.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002235 PRINT("warning: couldn't find %s needed by %s on unload.",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002236 library_name, si->get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002237 return;
2238 } else if (local_unload_list.contains(needed)) {
2239 // already visited
2240 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002241 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002242 // external group
2243 external_unload_list.push_back(needed);
2244 } else {
2245 // local group
2246 depth_first_list.push_front(needed);
2247 }
2248 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002249#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002250 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002251 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002252
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002253 local_unload_list.for_each([](soinfo* si) {
2254 si->call_destructors();
2255 });
2256
2257 while ((si = local_unload_list.pop_front()) != nullptr) {
2258 notify_gdb_of_unload(si);
2259 soinfo_free(si);
2260 }
2261
2262 while ((si = external_unload_list.pop_front()) != nullptr) {
2263 soinfo_unload(si);
2264 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002265 } else {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002266 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
2267 root->get_realpath(), ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08002268 }
2269}
2270
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002271static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
2272 if (sym_ver == nullptr) {
2273 return sym_name;
2274 }
2275
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002276 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002277}
2278
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002279void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002280 // Use basic string manipulation calls to avoid snprintf.
2281 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
2282 // When debug malloc is enabled, this call returns 0. This in turn causes
2283 // snprintf to do nothing, which causes libraries to fail to load.
2284 // See b/17302493 for further details.
2285 // Once the above bug is fixed, this code can be modified to use
2286 // snprintf again.
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002287 size_t required_len = 0;
2288 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2289 required_len += strlen(g_default_ld_paths[i]) + 1;
2290 }
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002291 if (buffer_size < required_len) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002292 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
2293 "buffer len %zu, required len %zu", buffer_size, required_len);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002294 }
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002295 char* end = buffer;
2296 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2297 if (i > 0) *end++ = ':';
2298 end = stpcpy(end, g_default_ld_paths[i]);
2299 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002300}
2301
Elliott Hughescade4c32012-12-20 14:42:14 -08002302void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08002303 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08002304}
2305
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002306soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo,
2307 void* caller_addr) {
2308 soinfo* const caller = find_containing_library(caller_addr);
2309
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002310 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08002311 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002312 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08002313 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002314
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002315 android_namespace_t* ns = caller != nullptr ? caller->get_namespace() : g_anonymous_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002316
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002317 if (extinfo != nullptr) {
2318 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
2319 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
2320 return nullptr;
2321 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002322
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002323 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07002324 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002325 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
2326 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002327 return nullptr;
2328 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002329
2330 if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 &&
2331 (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) {
2332 DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not "
2333 "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT");
2334 return nullptr;
2335 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002336
2337 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
2338 if (extinfo->library_namespace == nullptr) {
2339 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
2340 return nullptr;
2341 }
2342 ns = extinfo->library_namespace;
2343 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00002344 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002345
2346 ProtectedDataGuard guard;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002347 soinfo* si = find_library(ns, name, flags, extinfo, caller);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002348 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002349 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07002350 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002351
Elliott Hughesd23736e2012-11-01 15:16:56 -07002352 return si;
2353}
2354
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002355int do_dladdr(const void* addr, Dl_info* info) {
2356 // Determine if this address can be found in any library currently mapped.
2357 soinfo* si = find_containing_library(addr);
2358 if (si == nullptr) {
2359 return 0;
2360 }
2361
2362 memset(info, 0, sizeof(Dl_info));
2363
2364 info->dli_fname = si->get_realpath();
2365 // Address at which the shared object is loaded.
2366 info->dli_fbase = reinterpret_cast<void*>(si->base);
2367
2368 // Determine if any symbol in the library contains the specified address.
2369 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
2370 if (sym != nullptr) {
2371 info->dli_sname = si->get_string(sym->st_name);
2372 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
2373 }
2374
2375 return 1;
2376}
2377
2378bool do_dlsym(void* handle, const char* sym_name, const char* sym_ver,
2379 void* caller_addr, void** symbol) {
2380#if !defined(__LP64__)
2381 if (handle == nullptr) {
2382 DL_ERR("dlsym failed: library handle is null");
2383 return false;
2384 }
2385#endif
2386
2387 if (sym_name == nullptr) {
2388 DL_ERR("dlsym failed: symbol name is null");
2389 return false;
2390 }
2391
2392 soinfo* found = nullptr;
2393 const ElfW(Sym)* sym = nullptr;
2394 soinfo* caller = find_containing_library(caller_addr);
2395 android_namespace_t* ns = caller != nullptr ? caller->get_namespace() : g_anonymous_namespace;
2396
2397 version_info vi_instance;
2398 version_info* vi = nullptr;
2399
2400 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002401 vi_instance.name = sym_ver;
2402 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002403 vi = &vi_instance;
2404 }
2405
2406 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2407 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2408 } else {
2409 sym = dlsym_handle_lookup(reinterpret_cast<soinfo*>(handle), &found, sym_name, vi);
2410 }
2411
2412 if (sym != nullptr) {
2413 uint32_t bind = ELF_ST_BIND(sym->st_info);
2414
2415 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2416 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
2417 return true;
2418 }
2419
2420 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2421 return false;
2422 }
2423
2424 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2425 return false;
2426}
2427
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002428void do_dlclose(soinfo* si) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002429 ProtectedDataGuard guard;
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002430 soinfo_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002431}
2432
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002433bool init_namespaces(const char* public_ns_sonames, const char* anon_ns_library_path) {
2434 CHECK(public_ns_sonames != nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002435 if (g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002436 DL_ERR("public namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002437 return false;
2438 }
2439
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002440 std::vector<std::string> sonames = android::base::Split(public_ns_sonames, ":");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002441
2442 ProtectedDataGuard guard;
2443
2444 auto failure_guard = make_scope_guard([&]() {
2445 g_public_namespace.clear();
2446 });
2447
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002448 for (const auto& soname : sonames) {
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -08002449 soinfo* candidate = nullptr;
2450
2451 find_loaded_library_by_soname(&g_default_namespace, soname.c_str(), &candidate);
2452
2453 if (candidate == nullptr) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002454 DL_ERR("error initializing public namespace: \"%s\" was not found"
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002455 " in the default namespace", soname.c_str());
2456 return false;
2457 }
2458
2459 candidate->set_nodelete();
2460 g_public_namespace.push_back(candidate);
2461 }
2462
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002463 g_public_namespace_initialized = true;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002464
2465 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002466 // When the caller is nullptr - create_namespace will take global group
2467 // from the anonymous namespace, which is fine because anonymous namespace
2468 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002469 android_namespace_t* anon_ns =
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002470 create_namespace(nullptr, "(anonymous)", nullptr, anon_ns_library_path,
2471 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002472
2473 if (anon_ns == nullptr) {
2474 g_public_namespace_initialized = false;
2475 return false;
2476 }
2477 g_anonymous_namespace = anon_ns;
2478 failure_guard.disable();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002479 return true;
2480}
2481
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002482android_namespace_t* create_namespace(const void* caller_addr,
2483 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002484 const char* ld_library_path,
2485 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002486 uint64_t type,
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002487 const char* permitted_when_isolated_path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002488 if (!g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002489 DL_ERR("cannot create namespace: public namespace is not initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002490 return nullptr;
2491 }
2492
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002493 soinfo* caller_soinfo = find_containing_library(caller_addr);
2494
2495 android_namespace_t* caller_ns = caller_soinfo != nullptr ?
2496 caller_soinfo->get_namespace() :
2497 g_anonymous_namespace;
2498
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002499 ProtectedDataGuard guard;
2500 std::vector<std::string> ld_library_paths;
2501 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002502 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002503
2504 parse_path(ld_library_path, ":", &ld_library_paths);
2505 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002506 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002507
2508 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2509 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002510 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002511 ns->set_ld_library_paths(std::move(ld_library_paths));
2512 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002513 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002514
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002515 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
2516 // If shared - clone the caller namespace
2517 auto& soinfo_list = caller_ns->soinfo_list();
2518 std::copy(soinfo_list.begin(), soinfo_list.end(), std::back_inserter(ns->soinfo_list()));
2519 } else {
2520 // If not shared - copy only the global group
2521 auto global_group = make_global_group(caller_ns);
2522 std::copy(global_group.begin(), global_group.end(), std::back_inserter(ns->soinfo_list()));
2523 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002524
2525 return ns;
2526}
2527
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002528static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
2529 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2530 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2531 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002532 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2533 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002534
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002535 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002536}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002537
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002538const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2539 if (source_symver < 2 ||
2540 source_symver >= version_infos.size() ||
2541 version_infos[source_symver].name == nullptr) {
2542 return nullptr;
2543 }
2544
2545 return &version_infos[source_symver];
2546}
2547
2548void VersionTracker::add_version_info(size_t source_index,
2549 ElfW(Word) elf_hash,
2550 const char* ver_name,
2551 const soinfo* target_si) {
2552 if (source_index >= version_infos.size()) {
2553 version_infos.resize(source_index+1);
2554 }
2555
2556 version_infos[source_index].elf_hash = elf_hash;
2557 version_infos[source_index].name = ver_name;
2558 version_infos[source_index].target_si = target_si;
2559}
2560
2561bool VersionTracker::init_verneed(const soinfo* si_from) {
2562 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2563
2564 if (verneed_ptr == 0) {
2565 return true;
2566 }
2567
2568 size_t verneed_cnt = si_from->get_verneed_cnt();
2569
2570 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2571 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2572 size_t vernaux_offset = offset + verneed->vn_aux;
2573 offset += verneed->vn_next;
2574
2575 if (verneed->vn_version != 1) {
2576 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2577 return false;
2578 }
2579
2580 const char* target_soname = si_from->get_string(verneed->vn_file);
2581 // find it in dependencies
2582 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002583 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002584 });
2585
2586 if (target_si == nullptr) {
2587 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002588 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002589 return false;
2590 }
2591
2592 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2593 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2594 vernaux_offset += vernaux->vna_next;
2595
2596 const ElfW(Word) elf_hash = vernaux->vna_hash;
2597 const char* ver_name = si_from->get_string(vernaux->vna_name);
2598 ElfW(Half) source_index = vernaux->vna_other;
2599
2600 add_version_info(source_index, elf_hash, ver_name, target_si);
2601 }
2602 }
2603
2604 return true;
2605}
2606
2607bool VersionTracker::init_verdef(const soinfo* si_from) {
2608 return for_each_verdef(si_from,
2609 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2610 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2611 si_from->get_string(verdaux->vda_name), si_from);
2612 return false;
2613 }
2614 );
2615}
2616
2617bool VersionTracker::init(const soinfo* si_from) {
2618 if (!si_from->has_min_version(2)) {
2619 return true;
2620 }
2621
2622 return init_verneed(si_from) && init_verdef(si_from);
2623}
2624
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002625bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2626 const char* sym_name, const version_info** vi) {
2627 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2628 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2629
2630 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2631 *vi = version_tracker.get_version_info(sym_ver);
2632
2633 if (*vi == nullptr) {
2634 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002635 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002636 return false;
2637 }
2638 } else {
2639 // there is no version info
2640 *vi = nullptr;
2641 }
2642
2643 return true;
2644}
2645
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002646#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002647#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002648static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2649 return rela->r_addend;
2650}
2651#else
2652static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002653 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2654 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002655 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2656 }
2657 return 0;
2658}
2659#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002660
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002661template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002662bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2663 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002664 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2665 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002666 if (rel == nullptr) {
2667 return false;
2668 }
2669
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002670 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2671 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2672
2673 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002674 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002675 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002676 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002677
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002678 DEBUG("Processing '%s' relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002679 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002680 continue;
2681 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002682
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002683 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002684 soinfo* lsi = nullptr;
2685
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002686 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002687 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002688 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002689
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002690 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2691 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002692 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002693
2694 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2695 return false;
2696 }
2697
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002698 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002699 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002700 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002701 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002702 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002703 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002704 }
2705
2706 /* IHI0044C AAELF 4.5.1.1:
2707
2708 Libraries are not searched to resolve weak references.
2709 It is not an error for a weak reference to remain unsatisfied.
2710
2711 During linking, the value of an undefined weak reference is:
2712 - Zero if the relocation type is absolute
2713 - The address of the place if the relocation is pc-relative
2714 - The address of nominal base address if the relocation
2715 type is base-relative.
2716 */
2717
2718 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002719 case R_GENERIC_JUMP_SLOT:
2720 case R_GENERIC_GLOB_DAT:
2721 case R_GENERIC_RELATIVE:
2722 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002723#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002724 case R_AARCH64_ABS64:
2725 case R_AARCH64_ABS32:
2726 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002727#elif defined(__x86_64__)
2728 case R_X86_64_32:
2729 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002730#elif defined(__arm__)
2731 case R_ARM_ABS32:
2732#elif defined(__i386__)
2733 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002734#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002735 /*
2736 * The sym_addr was initialized to be zero above, or the relocation
2737 * code below does not care about value of sym_addr.
2738 * No need to do anything.
2739 */
2740 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002741#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002742 case R_X86_64_PC32:
2743 sym_addr = reloc;
2744 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002745#elif defined(__i386__)
2746 case R_386_PC32:
2747 sym_addr = reloc;
2748 break;
2749#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002750 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002751 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002752 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002753 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002754 } else { // We got a definition.
2755#if !defined(__LP64__)
2756 // When relocating dso with text_relocation .text segment is
2757 // not executable. We need to restore elf flags before resolving
2758 // STT_GNU_IFUNC symbol.
2759 bool protect_segments = has_text_relocations &&
2760 lsi == this &&
2761 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2762 if (protect_segments) {
2763 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2764 DL_ERR("can't protect segments for \"%s\": %s",
2765 get_realpath(), strerror(errno));
2766 return false;
2767 }
2768 }
2769#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002770 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002771#if !defined(__LP64__)
2772 if (protect_segments) {
2773 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2774 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2775 get_realpath(), strerror(errno));
2776 return false;
2777 }
2778 }
2779#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002780 }
2781 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002782 }
2783
2784 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002785 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002786 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002787 MARK(rel->r_offset);
2788 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2789 reinterpret_cast<void*>(reloc),
2790 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2791
2792 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002793 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002794 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002795 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002796 MARK(rel->r_offset);
2797 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2798 reinterpret_cast<void*>(reloc),
2799 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2800 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002801 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002802 case R_GENERIC_RELATIVE:
2803 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002804 MARK(rel->r_offset);
2805 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2806 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002807 reinterpret_cast<void*>(load_bias + addend));
2808 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002809 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002810 case R_GENERIC_IRELATIVE:
2811 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002812 MARK(rel->r_offset);
2813 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2814 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002815 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002816 {
2817#if !defined(__LP64__)
2818 // When relocating dso with text_relocation .text segment is
2819 // not executable. We need to restore elf flags for this
2820 // particular call.
2821 if (has_text_relocations) {
2822 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2823 DL_ERR("can't protect segments for \"%s\": %s",
2824 get_realpath(), strerror(errno));
2825 return false;
2826 }
2827 }
2828#endif
2829 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2830#if !defined(__LP64__)
2831 // Unprotect it afterwards...
2832 if (has_text_relocations) {
2833 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2834 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2835 get_realpath(), strerror(errno));
2836 return false;
2837 }
2838 }
2839#endif
2840 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2841 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002842 break;
2843
2844#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002845 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002846 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002847 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002848 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002849 reloc, sym_addr + addend, sym_name);
2850 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002851 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002852 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002853 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002854 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002855 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002856 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002857 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002858 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2859 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002860 if ((min_value <= (sym_addr + addend)) &&
2861 ((sym_addr + addend) <= max_value)) {
2862 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002863 } else {
2864 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002865 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002866 return false;
2867 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002868 }
2869 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002870 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002871 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002872 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002873 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002874 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002875 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002876 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2877 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002878 if ((min_value <= (sym_addr + addend)) &&
2879 ((sym_addr + addend) <= max_value)) {
2880 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002881 } else {
2882 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002883 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002884 return false;
2885 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002886 }
2887 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002888 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002889 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002890 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002891 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002892 reloc, sym_addr + addend, rel->r_offset, sym_name);
2893 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002894 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002895 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002896 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002897 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002898 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002899 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002900 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002901 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2902 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002903 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2904 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2905 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002906 } else {
2907 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002908 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002909 return false;
2910 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002911 }
2912 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002913 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002914 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002915 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002916 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002917 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002918 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002919 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2920 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002921 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2922 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2923 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002924 } else {
2925 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002926 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002927 return false;
2928 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002929 }
2930 break;
2931
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002932 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002933 /*
2934 * ET_EXEC is not supported so this should not happen.
2935 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002936 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002937 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002938 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002939 * R_AARCH64_COPY may only appear in executable objects where e_type is
2940 * set to ET_EXEC.
2941 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002942 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002943 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002944 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002945 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002946 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002947 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002948 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002949 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002950 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002951 break;
2952#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002953 case R_X86_64_32:
2954 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002955 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002956 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2957 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002958 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002959 break;
2960 case R_X86_64_64:
2961 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002962 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002963 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2964 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002965 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002966 break;
2967 case R_X86_64_PC32:
2968 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002969 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002970 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2971 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2972 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002973 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002974 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002975#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002976 case R_ARM_ABS32:
2977 count_relocation(kRelocAbsolute);
2978 MARK(rel->r_offset);
2979 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2980 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2981 break;
2982 case R_ARM_REL32:
2983 count_relocation(kRelocRelative);
2984 MARK(rel->r_offset);
2985 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2986 reloc, sym_addr, rel->r_offset, sym_name);
2987 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2988 break;
2989 case R_ARM_COPY:
2990 /*
2991 * ET_EXEC is not supported so this should not happen.
2992 *
2993 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2994 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002995 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002996 * R_ARM_COPY may only appear in executable objects where e_type is
2997 * set to ET_EXEC.
2998 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002999 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08003000 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003001#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003002 case R_386_32:
3003 count_relocation(kRelocRelative);
3004 MARK(rel->r_offset);
3005 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
3006 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
3007 break;
3008 case R_386_PC32:
3009 count_relocation(kRelocRelative);
3010 MARK(rel->r_offset);
3011 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
3012 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
3013 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
3014 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003015#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003016 default:
3017 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003018 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003019 }
3020 }
3021 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003022}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08003023#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003024
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003025void soinfo::call_array(const char* array_name __unused, linker_function_t* functions,
3026 size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003027 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07003028 return;
3029 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02003030
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003031 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, get_realpath());
Elliott Hughesca0c11b2013-03-12 10:40:45 -07003032
3033 int begin = reverse ? (count - 1) : 0;
3034 int end = reverse ? -1 : count;
3035 int step = reverse ? -1 : 1;
3036
3037 for (int i = begin; i != end; i += step) {
3038 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003039 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07003040 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02003041
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003042 TRACE("[ Done calling %s for '%s' ]", array_name, get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003043}
3044
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003045void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003046 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07003047 return;
3048 }
3049
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003050 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Elliott Hughesd23736e2012-11-01 15:16:56 -07003051 function();
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003052 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04003053}
3054
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003055void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003056 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
3057 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003058 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07003059}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003060
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003061void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07003062 if (constructors_called) {
3063 return;
3064 }
Jesse Hallf5d16932012-01-30 15:39:57 -08003065
Elliott Hughesd23736e2012-11-01 15:16:56 -07003066 // We set constructors_called before actually calling the constructors, otherwise it doesn't
3067 // protect against recursive constructor calls. One simple example of constructor recursion
3068 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
3069 // 1. The program depends on libc, so libc's constructor is called here.
3070 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
3071 // 3. dlopen() calls the constructors on the newly created
3072 // soinfo for libc_malloc_debug_leak.so.
3073 // 4. The debug .so depends on libc, so CallConstructors is
3074 // called again with the libc soinfo. If it doesn't trigger the early-
3075 // out above, the libc constructor will be called again (recursively!).
3076 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003077
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003078 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003079 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07003080 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003081 get_realpath(), preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07003082 }
3083
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003084 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003085 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003086 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003087
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003088 TRACE("\"%s\": calling constructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003089
3090 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003091 call_function("DT_INIT", init_func_);
3092 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003093}
David 'Digit' Turner82156792009-05-18 14:37:41 +02003094
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003095void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003096 if (!constructors_called) {
3097 return;
3098 }
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003099 TRACE("\"%s\": calling destructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003100
3101 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003102 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003103
3104 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003105 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07003106
3107 // This is needed on second call to dlopen
3108 // after library has been unloaded with RTLD_NODELETE
3109 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003110}
3111
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003112void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003113 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003114 child->parents_.push_back(this);
3115 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003116 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003117}
3118
3119void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003120 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003121 return;
3122 }
3123
3124 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003125 children_.for_each([&] (soinfo* child) {
3126 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003127 return parent == this;
3128 });
3129 });
3130
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003131 parents_.for_each([&] (soinfo* parent) {
3132 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003133 return child == this;
3134 });
3135 });
3136
3137 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003138 parents_.clear();
3139 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003140}
3141
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003142dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003143 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003144 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003145 }
3146
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003147 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003148};
3149
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003150ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003151 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003152 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003153 }
3154
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003155 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003156}
3157
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003158off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003159 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003160 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003161 }
3162
3163 return 0;
3164}
3165
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003166uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07003167 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003168 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07003169 }
3170
3171 return 0;
3172}
3173
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003174uint32_t soinfo::get_dt_flags_1() const {
3175 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003176 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003177 }
3178
3179 return 0;
3180}
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003181
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003182void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
3183 if (has_min_version(1)) {
3184 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003185 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003186 }
3187
3188 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003189 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003190 }
3191
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003192 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003193 }
3194}
3195
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003196void soinfo::set_nodelete() {
3197 rtld_flags_ |= RTLD_NODELETE;
3198}
3199
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003200const char* soinfo::get_realpath() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003201#if defined(__work_around_b_24465209__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003202 if (has_min_version(2)) {
3203 return realpath_.c_str();
3204 } else {
3205 return old_name_;
3206 }
3207#else
3208 return realpath_.c_str();
3209#endif
3210}
3211
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003212void soinfo::set_soname(const char* soname) {
3213#if defined(__work_around_b_24465209__)
3214 if (has_min_version(2)) {
3215 soname_ = soname;
3216 }
3217 strlcpy(old_name_, soname_, sizeof(old_name_));
3218#else
3219 soname_ = soname;
3220#endif
3221}
3222
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003223const char* soinfo::get_soname() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003224#if defined(__work_around_b_24465209__)
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003225 if (has_min_version(2)) {
3226 return soname_;
3227 } else {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003228 return old_name_;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003229 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003230#else
3231 return soname_;
3232#endif
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003233}
3234
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003235// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003236// 'this->flags' does not have FLAG_NEW_SOINFO set.
3237static soinfo::soinfo_list_t g_empty_list;
3238
3239soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003240 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003241 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003242 }
3243
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003244 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003245}
3246
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003247const soinfo::soinfo_list_t& soinfo::get_children() const {
3248 if (has_min_version(0)) {
3249 return children_;
3250 }
3251
3252 return g_empty_list;
3253}
3254
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003255soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003256 if (has_min_version(0)) {
3257 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003258 }
3259
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003260 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003261}
3262
Evgenii Stepanov68650822015-06-10 13:38:39 -07003263static std::vector<std::string> g_empty_runpath;
3264
3265const std::vector<std::string>& soinfo::get_dt_runpath() const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003266 if (has_min_version(3)) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003267 return dt_runpath_;
3268 }
3269
3270 return g_empty_runpath;
3271}
3272
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003273android_namespace_t* soinfo::get_namespace() {
3274 if (has_min_version(3)) {
3275 return namespace_;
3276 }
3277
3278 return &g_default_namespace;
3279}
3280
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003281ElfW(Addr) soinfo::resolve_symbol_address(const ElfW(Sym)* s) const {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003282 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
3283 return call_ifunc_resolver(s->st_value + load_bias);
3284 }
3285
3286 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
3287}
3288
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003289const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003290 if (has_min_version(1) && (index >= strtab_size_)) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003291 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003292 get_realpath(), strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003293 }
3294
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003295 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003296}
3297
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003298bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003299 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003300}
3301
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003302bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003303 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003304}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003305
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003306bool soinfo::is_linked() const {
3307 return (flags_ & FLAG_LINKED) != 0;
3308}
3309
3310bool soinfo::is_main_executable() const {
3311 return (flags_ & FLAG_EXE) != 0;
3312}
3313
3314void soinfo::set_linked() {
3315 flags_ |= FLAG_LINKED;
3316}
3317
3318void soinfo::set_linker_flag() {
3319 flags_ |= FLAG_LINKER;
3320}
3321
3322void soinfo::set_main_executable() {
3323 flags_ |= FLAG_EXE;
3324}
3325
3326void soinfo::increment_ref_count() {
3327 local_group_root_->ref_count_++;
3328}
3329
3330size_t soinfo::decrement_ref_count() {
3331 return --local_group_root_->ref_count_;
3332}
3333
3334soinfo* soinfo::get_local_group_root() const {
3335 return local_group_root_;
3336}
3337
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -08003338
3339void soinfo::set_mapped_by_caller(bool mapped_by_caller) {
3340 if (mapped_by_caller) {
3341 flags_ |= FLAG_MAPPED_BY_CALLER;
3342 } else {
3343 flags_ &= ~FLAG_MAPPED_BY_CALLER;
3344 }
3345}
3346
3347bool soinfo::is_mapped_by_caller() const {
3348 return (flags_ & FLAG_MAPPED_BY_CALLER) != 0;
3349}
3350
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003351// This function returns api-level at the time of
3352// dlopen/load. Note that libraries opened by system
3353// will always have 'current' api level.
3354uint32_t soinfo::get_target_sdk_version() const {
3355 if (!has_min_version(2)) {
3356 return __ANDROID_API__;
3357 }
3358
3359 return local_group_root_->target_sdk_version_;
3360}
3361
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003362bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08003363 /* Extract dynamic section */
3364 ElfW(Word) dynamic_flags = 0;
3365 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07003366
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003367 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003368 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003369 if (!relocating_linker) {
Elliott Hughes116b5692016-01-04 17:45:36 -08003370 INFO("[ Linking '%s' ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003371 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003372 }
3373
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003374 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003375 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003376 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003377 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003378 return false;
3379 } else {
3380 if (!relocating_linker) {
3381 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003382 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003383 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003384
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003385#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003386 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
3387 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003388#endif
3389
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003390 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003391 // Note that: "Except for the DT_NULL element at the end of the array,
3392 // and the relative order of DT_NEEDED elements, entries may appear in any order."
3393 //
3394 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003395 uint32_t needed_count = 0;
3396 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
3397 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
3398 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3399 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003400 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003401 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003402 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003403
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003404 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003405 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
3406 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
3407 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
3408 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003409 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003410
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003411 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003412 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003413 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003414 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
3415 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003416
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003417 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003418 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003419 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003420 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
3421 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003422
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003423 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003424 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003425 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003426 return false;
3427 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003428 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003429
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003430 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003431 break;
3432
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003433 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003434 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003435 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003436
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003437 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003438 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003439 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003440
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003441 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003442 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003443 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003444
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003445 case DT_SYMENT:
3446 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003447 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
3448 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003449 return false;
3450 }
3451 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003452
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003453 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003454#if defined(USE_RELA)
3455 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003456 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003457 return false;
3458 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003459#else
3460 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003461 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003462 return false;
3463 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003464#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003465 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003466
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003467 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003468#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003469 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003470#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003471 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003472#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003473 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003474
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003475 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003476#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003477 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003478#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003479 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003480#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003481 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003482
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003483 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003484#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003485 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003486 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003487#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003488 // Ignore for other platforms... (because RTLD_LAZY is not supported)
3489 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003490
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003491 case DT_DEBUG:
3492 // Set the DT_DEBUG entry to the address of _r_debug for GDB
3493 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08003494// FIXME: not working currently for N64
3495// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003496// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08003497// read-only, but the DYNAMIC header claims it is writable.
3498#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003499 if ((dynamic_flags & PF_W) != 0) {
3500 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
3501 }
Chris Dearman99186652014-02-06 20:36:51 -08003502#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08003503 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003504#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003505 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003506 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003507 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003508
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003509 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003510 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003511 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003512
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003513 case DT_ANDROID_RELA:
3514 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3515 break;
3516
3517 case DT_ANDROID_RELASZ:
3518 android_relocs_size_ = d->d_un.d_val;
3519 break;
3520
3521 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003522 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003523 return false;
3524
3525 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003526 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003527 return false;
3528
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003529 case DT_RELAENT:
3530 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003531 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003532 return false;
3533 }
3534 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003535
3536 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003537 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003538 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003539
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003540 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003541 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003542 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003543
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003544 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003545 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003546 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003547
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003548#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003549 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003550 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003551 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003552
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003553 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003554 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003555 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003556
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003557 case DT_RELENT:
3558 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003559 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003560 return false;
3561 }
3562 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003563
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003564 case DT_ANDROID_REL:
3565 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3566 break;
3567
3568 case DT_ANDROID_RELSZ:
3569 android_relocs_size_ = d->d_un.d_val;
3570 break;
3571
3572 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003573 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003574 return false;
3575
3576 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003577 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003578 return false;
3579
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003580 // "Indicates that all RELATIVE relocations have been concatenated together,
3581 // and specifies the RELATIVE relocation count."
3582 //
3583 // TODO: Spec also mentions that this can be used to optimize relocation process;
3584 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003585 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003586 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003587
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003588 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003589 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003590 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003591
3592 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003593 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003594 return false;
3595
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003596#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003597 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003598 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003599 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003600 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003601
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003602 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003603 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003604 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003605 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003606
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003607 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003608 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003609 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003610 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003611
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003612 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003613 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003614 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003615
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003616 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003617 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003618 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003619 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003620
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003621 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003622 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003623 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003624
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003625 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003626 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003627 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003628 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003629
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003630 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003631 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003632 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003633
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003634 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003635#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003636 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003637 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003638#else
3639 has_text_relocations = true;
3640 break;
3641#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003642
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003643 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003644 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003645 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003646
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003647 case DT_NEEDED:
3648 ++needed_count;
3649 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003650
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003651 case DT_FLAGS:
3652 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003653#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003654 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003655 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003656#else
3657 has_text_relocations = true;
3658#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003659 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003660 if (d->d_un.d_val & DF_SYMBOLIC) {
3661 has_DT_SYMBOLIC = true;
3662 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003663 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003664
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003665 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003666 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003667
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003668 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -07003669 DL_WARN("%s: unsupported flags DT_FLAGS_1=%p", get_realpath(), reinterpret_cast<void*>(d->d_un.d_val));
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003670 }
3671 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003672#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003673 case DT_MIPS_RLD_MAP:
3674 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3675 {
3676 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3677 *dp = &_r_debug;
3678 }
3679 break;
Raghu Gandham68815722014-12-18 19:12:19 -08003680 case DT_MIPS_RLD_MAP2:
3681 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
3682 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003683 r_debug** dp = reinterpret_cast<r_debug**>(
3684 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003685 *dp = &_r_debug;
3686 }
3687 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003688
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003689 case DT_MIPS_RLD_VERSION:
3690 case DT_MIPS_FLAGS:
3691 case DT_MIPS_BASE_ADDRESS:
3692 case DT_MIPS_UNREFEXTNO:
3693 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003694
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003695 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003696 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003697 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003698
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003699 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003700 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003701 break;
3702
3703 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003704 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003705 break;
3706#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003707 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3708 case DT_BIND_NOW:
3709 break;
3710
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003711 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003712 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3713 break;
3714
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003715 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003716 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3717 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003718 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003719 verdef_cnt_ = d->d_un.d_val;
3720 break;
3721
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003722 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003723 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3724 break;
3725
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003726 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003727 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003728 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003729
Evgenii Stepanov68650822015-06-10 13:38:39 -07003730 case DT_RUNPATH:
3731 // this is parsed after we have strtab initialized (see below).
3732 break;
3733
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003734 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003735 if (!relocating_linker) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003736 DL_WARN("%s: unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003737 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3738 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003739 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003740 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003741 }
3742
Duane Sandbc425c72015-06-01 16:29:14 -07003743#if defined(__mips__) && !defined(__LP64__)
3744 if (!mips_check_and_adjust_fp_modes()) {
3745 return false;
3746 }
3747#endif
3748
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003749 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003750 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003751
3752 // Sanity checks.
3753 if (relocating_linker && needed_count != 0) {
3754 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3755 return false;
3756 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003757 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003758 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003759 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003760 return false;
3761 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003762 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003763 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003764 return false;
3765 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003766 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003767 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003768 return false;
3769 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003770
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003771 // second pass - parse entries relying on strtab
3772 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003773 switch (d->d_tag) {
3774 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003775 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003776 break;
3777 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003778 set_dt_runpath(get_string(d->d_un.d_val));
3779 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003780 }
3781 }
3782
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003783 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003784 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003785 // because they can't find dt_needed library by soname.
3786 // This workaround should keep them working. (applies only
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003787 // for apps targeting sdk version <=22). Make an exception for
3788 // the main executable and linker; they do not need to have dt_soname
3789 if (soname_ == nullptr && this != somain && (flags_ & FLAG_LINKER) == 0 &&
3790 get_application_target_sdk_version() <= 22) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003791 soname_ = basename(realpath_.c_str());
3792 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3793 get_realpath(), soname_);
3794 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003795 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003796}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003797
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003798bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3799 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003800
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003801 local_group_root_ = local_group.front();
3802 if (local_group_root_ == nullptr) {
3803 local_group_root_ = this;
3804 }
3805
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003806 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3807 target_sdk_version_ = get_application_target_sdk_version();
3808 }
3809
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003810 VersionTracker version_tracker;
3811
3812 if (!version_tracker.init(this)) {
3813 return false;
3814 }
3815
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003816#if !defined(__LP64__)
3817 if (has_text_relocations) {
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003818 // Fail if app is targeting sdk version > 22
Dmitriy Ivanov80687862015-10-09 13:58:46 -07003819 if (get_application_target_sdk_version() > 22) {
Dmitriy Ivanovfae39d22015-10-13 11:07:56 -07003820 PRINT("%s: has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003821 DL_ERR("%s: has text relocations", get_realpath());
3822 return false;
3823 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003824 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003825 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003826 DL_WARN("%s has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003827 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003828 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3829 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003830 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003831 return false;
3832 }
3833 }
3834#endif
3835
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003836 if (android_relocs_ != nullptr) {
3837 // check signature
3838 if (android_relocs_size_ > 3 &&
3839 android_relocs_[0] == 'A' &&
3840 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003841 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003842 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003843 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003844
3845 bool relocated = false;
3846 const uint8_t* packed_relocs = android_relocs_ + 4;
3847 const size_t packed_relocs_size = android_relocs_size_ - 4;
3848
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003849 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003850 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003851 packed_reloc_iterator<sleb128_decoder>(
3852 sleb128_decoder(packed_relocs, packed_relocs_size)),
3853 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003854
3855 if (!relocated) {
3856 return false;
3857 }
3858 } else {
3859 DL_ERR("bad android relocation header.");
3860 return false;
3861 }
3862 }
3863
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003864#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003865 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003866 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003867 if (!relocate(version_tracker,
3868 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003869 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003870 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003871 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003872 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003873 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003874 if (!relocate(version_tracker,
3875 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003876 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003877 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003878 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003879#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003880 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003881 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003882 if (!relocate(version_tracker,
3883 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003884 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003885 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003886 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003887 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003888 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003889 if (!relocate(version_tracker,
3890 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003891 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003892 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003893 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003894#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003895
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003896#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003897 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003898 return false;
3899 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003900#endif
3901
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003902 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003903
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003904#if !defined(__LP64__)
3905 if (has_text_relocations) {
3906 // All relocations are done, we can protect our segments back to read-only.
3907 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3908 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003909 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003910 return false;
3911 }
3912 }
3913#endif
3914
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003915 /* We can also turn on GNU RELRO protection */
3916 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3917 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003918 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003919 return false;
3920 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003921
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003922 /* Handle serializing/sharing the RELRO segment */
3923 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3924 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3925 extinfo->relro_fd) < 0) {
3926 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003927 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003928 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003929 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003930 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3931 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3932 extinfo->relro_fd) < 0) {
3933 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003934 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003935 return false;
3936 }
3937 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003938
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003939 notify_gdb_of_load(this);
3940 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003941}
3942
Nick Kralevich468319c2011-11-11 15:53:17 -08003943/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003944 * This function add vdso to internal dso list.
3945 * It helps to stack unwinding through signal handlers.
3946 * Also, it makes bionic more like glibc.
3947 */
Kito Cheng812fd422014-03-25 22:53:56 +08003948static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003949#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08003950 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003951 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08003952 return;
3953 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003954
Dmitriy Ivanovd9b08a02015-11-16 13:17:27 -08003955 soinfo* si = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003956
Elliott Hughes0266ae52014-02-10 17:46:57 -08003957 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
3958 si->phnum = ehdr_vdso->e_phnum;
3959 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
3960 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003961 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003962
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003963 si->prelink_image();
3964 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003965#endif
3966}
3967
3968/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003969 * This is linker soinfo for GDB. See details below.
3970 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003971#if defined(__LP64__)
3972#define LINKER_PATH "/system/bin/linker64"
3973#else
3974#define LINKER_PATH "/system/bin/linker"
3975#endif
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003976
3977// This is done to avoid calling c-tor prematurely
3978// because soinfo c-tor needs memory allocator
3979// which might be initialized after global variables.
3980static uint8_t linker_soinfo_for_gdb_buf[sizeof(soinfo)] __attribute__((aligned(8)));
3981static soinfo* linker_soinfo_for_gdb = nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003982
3983/* gdb expects the linker to be in the debug shared object list.
3984 * Without this, gdb has trouble locating the linker's ".text"
3985 * and ".plt" sections. Gdb could also potentially use this to
3986 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
3987 * Don't use soinfo_alloc(), because the linker shouldn't
3988 * be on the soinfo list.
3989 */
3990static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003991 linker_soinfo_for_gdb = new (linker_soinfo_for_gdb_buf) soinfo(nullptr, LINKER_PATH,
3992 nullptr, 0, 0);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003993
Dmitriy Ivanov175dae92015-06-10 19:46:19 -07003994 linker_soinfo_for_gdb->load_bias = linker_base;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003995
3996 /*
3997 * Set the dynamic field in the link map otherwise gdb will complain with
3998 * the following:
3999 * warning: .dynamic section for "/system/bin/linker" is not at the
4000 * expected address (wrong library or version mismatch?)
4001 */
4002 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
4003 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
4004 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07004005 &linker_soinfo_for_gdb->dynamic, nullptr);
4006 insert_soinfo_into_debug_map(linker_soinfo_for_gdb);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004007}
4008
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004009static void init_default_namespace() {
4010 g_default_namespace.set_name("(default)");
4011 g_default_namespace.set_isolated(false);
4012
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004013 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
4014 somain->load_bias);
4015 const char* bname = basename(interp);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004016 if (bname && (strcmp(bname, "linker_asan") == 0 || strcmp(bname, "linker_asan64") == 0)) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004017 g_default_ld_paths = kAsanDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004018 } else {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004019 g_default_ld_paths = kDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004020 }
4021
4022 std::vector<std::string> ld_default_paths;
4023 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
4024 ld_default_paths.push_back(g_default_ld_paths[i]);
4025 }
4026
4027 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004028};
4029
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07004030extern "C" int __system_properties_init(void);
4031
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004032/*
Nick Kralevich468319c2011-11-11 15:53:17 -08004033 * This code is called after the linker has linked itself and
4034 * fixed it's own GOT. It is safe to make references to externs
4035 * and other non-local data at this point.
4036 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004037static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04004038#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004039 struct timeval t0, t1;
4040 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04004041#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004042
Elliott Hughes1801db32015-06-08 18:04:00 -07004043 // Sanitize the environment.
4044 __libc_init_AT_SECURE(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01004045
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07004046 // Initialize system properties
4047 __system_properties_init(); // may use 'environ'
4048
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004049 debuggerd_init();
4050
4051 // Get a few environment variables.
Elliott Hughes1801db32015-06-08 18:04:00 -07004052 const char* LD_DEBUG = getenv("LD_DEBUG");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004053 if (LD_DEBUG != nullptr) {
4054 g_ld_debug_verbosity = atoi(LD_DEBUG);
4055 }
4056
Elliott Hughes116b5692016-01-04 17:45:36 -08004057#if defined(__LP64__)
4058 INFO("[ Android dynamic linker (64-bit) ]");
4059#else
4060 INFO("[ Android dynamic linker (32-bit) ]");
4061#endif
4062
Elliott Hughes1801db32015-06-08 18:04:00 -07004063 // These should have been sanitized by __libc_init_AT_SECURE, but the test
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004064 // doesn't cost us anything.
4065 const char* ldpath_env = nullptr;
4066 const char* ldpreload_env = nullptr;
Elliott Hughes1801db32015-06-08 18:04:00 -07004067 if (!getauxval(AT_SECURE)) {
4068 ldpath_env = getenv("LD_LIBRARY_PATH");
Elliott Hughes116b5692016-01-04 17:45:36 -08004069 if (ldpath_env != nullptr) {
4070 INFO("[ LD_LIBRARY_PATH set to '%s' ]", ldpath_env);
4071 }
Elliott Hughes1801db32015-06-08 18:04:00 -07004072 ldpreload_env = getenv("LD_PRELOAD");
Elliott Hughes116b5692016-01-04 17:45:36 -08004073 if (ldpreload_env != nullptr) {
4074 INFO("[ LD_PRELOAD set to '%s' ]", ldpreload_env);
4075 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004076 }
4077
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004078 soinfo* si = soinfo_alloc(&g_default_namespace, args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004079 if (si == nullptr) {
4080 exit(EXIT_FAILURE);
4081 }
4082
4083 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004084 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004085 link_map* map = &(si->link_map_head);
4086
4087 map->l_addr = 0;
4088 map->l_name = args.argv[0];
4089 map->l_prev = nullptr;
4090 map->l_next = nullptr;
4091
4092 _r_debug.r_map = map;
4093 r_debug_tail = map;
4094
4095 init_linker_info_for_gdb(linker_base);
4096
4097 // Extract information passed from the kernel.
4098 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
4099 si->phnum = args.getauxval(AT_PHNUM);
4100 si->entry = args.getauxval(AT_ENTRY);
4101
4102 /* Compute the value of si->base. We can't rely on the fact that
4103 * the first entry is the PHDR because this will not be true
4104 * for certain executables (e.g. some in the NDK unit test suite)
4105 */
4106 si->base = 0;
4107 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
4108 si->load_bias = 0;
4109 for (size_t i = 0; i < si->phnum; ++i) {
4110 if (si->phdr[i].p_type == PT_PHDR) {
4111 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
4112 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
4113 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07004114 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004115 }
4116 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07004117
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004118 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
4119 if (elf_hdr->e_type != ET_DYN) {
4120 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
4121 exit(EXIT_FAILURE);
4122 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004123
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004124 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
4125 parse_LD_LIBRARY_PATH(ldpath_env);
4126 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01004127
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004128 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004129
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004130 init_default_namespace();
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004131
Dmitriy Ivanov67181252015-01-07 15:48:25 -08004132 if (!si->prelink_image()) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004133 __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer());
Dmitriy Ivanov67181252015-01-07 15:48:25 -08004134 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004135
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07004136 // add somain to global group
4137 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
4138
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004139 // Load ld_preloads and dependencies.
4140 StringLinkedList needed_library_name_list;
4141 size_t needed_libraries_count = 0;
4142 size_t ld_preloads_count = 0;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07004143
4144 for (const auto& ld_preload_name : g_ld_preload_names) {
4145 needed_library_name_list.push_back(ld_preload_name.c_str());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004146 ++needed_libraries_count;
Dmitriy Ivanovf8093a92015-04-28 18:09:53 -07004147 ++ld_preloads_count;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004148 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004149
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004150 for_each_dt_needed(si, [&](const char* name) {
4151 needed_library_name_list.push_back(name);
4152 ++needed_libraries_count;
4153 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004154
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004155 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004156
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004157 memset(needed_library_names, 0, sizeof(needed_library_names));
4158 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004159
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07004160 if (needed_libraries_count > 0 &&
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004161 !find_libraries(&g_default_namespace, si, needed_library_names, needed_libraries_count,
4162 nullptr, &g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07004163 /* add_as_children */ true)) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004164 __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004165 } else if (needed_libraries_count == 0) {
4166 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004167 __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004168 }
4169 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004170 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004171
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004172 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07004173
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08004174 {
4175 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06004176
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08004177 si->call_pre_init_constructors();
4178
4179 /* After the prelink_image, the si->load_bias is initialized.
4180 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
4181 * We need to update this value for so exe here. So Unwind_Backtrace
4182 * for some arch like x86 could work correctly within so exe.
4183 */
4184 map->l_addr = si->load_bias;
4185 si->call_constructors();
4186 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04004187
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004188#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004189 gettimeofday(&t1, nullptr);
4190 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
4191 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
4192 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004193#endif
4194#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004195 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
4196 linker_stats.count[kRelocAbsolute],
4197 linker_stats.count[kRelocRelative],
4198 linker_stats.count[kRelocCopy],
4199 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004200#endif
4201#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004202 {
4203 unsigned n;
4204 unsigned i;
4205 unsigned count = 0;
4206 for (n = 0; n < 4096; n++) {
4207 if (bitmask[n]) {
4208 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004209#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004210 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004211#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004212 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004213#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004214 if (x & 1) {
4215 count++;
4216 }
4217 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004218 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004219 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004220 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004221 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
4222 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004223#endif
4224
4225#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004226 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004227#endif
4228
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004229 TRACE("[ Ready to execute '%s' @ %p ]", si->get_realpath(), reinterpret_cast<void*>(si->entry));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004230 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004231}
Nick Kralevich468319c2011-11-11 15:53:17 -08004232
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004233/* Compute the load-bias of an existing executable. This shall only
4234 * be used to compute the load bias of an executable or shared library
4235 * that was loaded by the kernel itself.
4236 *
4237 * Input:
4238 * elf -> address of ELF header, assumed to be at the start of the file.
4239 * Return:
4240 * load bias, i.e. add the value of any p_vaddr in the file to get
4241 * the corresponding address in memory.
4242 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004243static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
4244 ElfW(Addr) offset = elf->e_phoff;
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004245 const ElfW(Phdr)* phdr_table =
4246 reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004247 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004248
Elliott Hughes0266ae52014-02-10 17:46:57 -08004249 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08004250 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08004251 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004252 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08004253 }
4254 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004255}
4256
Elliott Hughes42d949f2016-01-06 19:51:43 -08004257extern "C" int __set_tls(void*);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004258extern "C" void _start();
4259
Nick Kralevich468319c2011-11-11 15:53:17 -08004260/*
4261 * This is the entry point for the linker, called from begin.S. This
4262 * method is responsible for fixing the linker's own relocations, and
4263 * then calling __linker_init_post_relocation().
4264 *
4265 * Because this method is called before the linker has fixed it's own
4266 * relocations, any attempt to reference an extern variable, extern
4267 * function, or other GOT reference will generate a segfault.
4268 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004269extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004270 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08004271
Elliott Hughes42d949f2016-01-06 19:51:43 -08004272 void* tls[BIONIC_TLS_SLOTS];
4273 __set_tls(tls);
4274
Elliott Hughes0266ae52014-02-10 17:46:57 -08004275 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004276 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004277 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08004278 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08004279
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004280 soinfo linker_so(nullptr, nullptr, nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08004281
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004282 // If the linker is not acting as PT_INTERP entry_point is equal to
4283 // _start. Which means that the linker is running as an executable and
4284 // already linked by PT_INTERP.
4285 //
4286 // This happens when user tries to run 'adb shell /system/bin/linker'
4287 // see also https://code.google.com/p/android/issues/detail?id=63174
4288 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004289 __libc_fatal("This is %s, the helper program for shared library executables.", args.argv[0]);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004290 }
4291
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004292 linker_so.base = linker_addr;
4293 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
4294 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07004295 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004296 linker_so.phdr = phdr;
4297 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004298 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07004299
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07004300 // This might not be obvious... The reasons why we pass g_empty_list
4301 // in place of local_group here are (1) we do not really need it, because
4302 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
4303 // itself without having to look into local_group and (2) allocators
4304 // are not yet initialized, and therefore we cannot use linked_list.push_*
4305 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004306 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004307 __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer());
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004308 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07004309
Elliott Hughesd2948632015-07-21 11:57:09 -07004310 __libc_init_main_thread(args);
Dmitriy Ivanov14241402014-08-26 14:16:52 -07004311
Josh Gao93c0f5e2015-10-06 11:08:13 -07004312 // Initialize the linker's static libc's globals
4313 __libc_init_globals(args);
4314
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004315 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004316 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07004317
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004318 // Initialize static variables. Note that in order to
4319 // get correct libdl_info we need to call constructors
4320 // before get_libdl_info().
4321 solist = get_libdl_info();
4322 sonext = get_libdl_info();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004323 g_default_namespace.soinfo_list().push_back(get_libdl_info());
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004324
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004325 // We have successfully fixed our own relocations. It's safe to run
4326 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07004327 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08004328 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004329
Elliott Hughes116b5692016-01-04 17:45:36 -08004330 INFO("[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address));
Elliott Hughes611f9562015-01-23 10:43:58 -08004331
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004332 // Return the address that the calling assembly stub should jump to.
4333 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08004334}