blob: 0df9feb873aeb9a63c288b19aa1695216a894834 [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"
Dimitry Ivanovdf91dc22016-02-25 15:22:04 -080055#include "linker_dlwarning.h"
Dmitriy Ivanov18870d32015-04-22 13:10:04 -070056#include "linker_sleb128.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020057#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080058#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080059#include "linker_reloc_iterators.h"
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070060#include "linker_utils.h"
tony.ys_liub4474402015-07-29 18:00:22 +080061
Elliott Hughes939a7e02015-12-04 15:27:46 -080062#include "android-base/strings.h"
Simon Baldwinaef71952015-01-16 13:22:54 +000063#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
Josh Gao93c0f5e2015-10-06 11:08:13 -070065extern void __libc_init_globals(KernelArgumentBlock&);
Elliott Hughes1801db32015-06-08 18:04:00 -070066extern void __libc_init_AT_SECURE(KernelArgumentBlock&);
67
68// Override macros to use C++ style casts.
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080069#undef ELF_ST_TYPE
70#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
71
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070072struct android_namespace_t {
73 public:
74 android_namespace_t() : name_(nullptr), is_isolated_(false) {}
75
76 const char* get_name() const { return name_; }
77 void set_name(const char* name) { name_ = name; }
78
79 bool is_isolated() const { return is_isolated_; }
80 void set_isolated(bool isolated) { is_isolated_ = isolated; }
81
82 const std::vector<std::string>& get_ld_library_paths() const {
83 return ld_library_paths_;
84 }
85 void set_ld_library_paths(std::vector<std::string>&& library_paths) {
86 ld_library_paths_ = library_paths;
87 }
88
89 const std::vector<std::string>& get_default_library_paths() const {
90 return default_library_paths_;
91 }
92 void set_default_library_paths(std::vector<std::string>&& library_paths) {
93 default_library_paths_ = library_paths;
94 }
95
Dimitry Ivanov350bdad2016-03-01 13:11:28 -080096 const std::vector<std::string>& get_permitted_paths() const {
97 return permitted_paths_;
98 }
Dimitry Ivanov284ae352015-12-08 10:47:13 -080099 void set_permitted_paths(std::vector<std::string>&& permitted_paths) {
100 permitted_paths_ = permitted_paths;
101 }
102
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700103 soinfo::soinfo_list_t& soinfo_list() { return soinfo_list_; }
104
105 // For isolated namespaces - checks if the file is on the search path;
106 // always returns true for not isolated namespace.
107 bool is_accessible(const std::string& path);
108
109 private:
110 const char* name_;
111 bool is_isolated_;
112 std::vector<std::string> ld_library_paths_;
113 std::vector<std::string> default_library_paths_;
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800114 std::vector<std::string> permitted_paths_;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700115 soinfo::soinfo_list_t soinfo_list_;
116
117 DISALLOW_COPY_AND_ASSIGN(android_namespace_t);
118};
119
120android_namespace_t g_default_namespace;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800121android_namespace_t* g_anonymous_namespace = &g_default_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700122
Elliott Hughes0266ae52014-02-10 17:46:57 -0800123static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700125static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
126static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200127
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700128static LinkerTypeAllocator<android_namespace_t> g_namespace_allocator;
129
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700130static soinfo* solist;
131static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700132static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800133
Elliott Hughes1728b232014-05-14 10:02:03 -0700134static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700135#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700136 "/system/lib64",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800137 "/vendor/lib64",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700138#else
Elliott Hughes124fae92012-10-31 14:20:03 -0700139 "/system/lib",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800140 "/vendor/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700141#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700142 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700143};
David Bartleybc3a5c22009-06-02 18:27:28 -0700144
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700145static const char* const kAsanDefaultLdPaths[] = {
146#if defined(__LP64__)
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700147 "/data/lib64",
148 "/system/lib64",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800149 "/data/vendor/lib64",
150 "/vendor/lib64",
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700151#else
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700152 "/data/lib",
153 "/system/lib",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800154 "/data/vendor/lib",
155 "/vendor/lib",
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700156#endif
157 nullptr
158};
159
Dimitry Ivanov36ac45f2016-01-07 18:43:20 -0800160static bool is_system_library(const std::string& realpath) {
161 for (const auto& dir : g_default_namespace.get_default_library_paths()) {
162 if (file_is_in_dir(realpath, dir)) {
163 return true;
164 }
165 }
166 return false;
167}
168
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800169// TODO(dimitry): This is workaround for http://b/26394120 - it will be removed before the release
Dimitry Ivanovb8e37692016-02-12 13:33:19 -0800170#if defined(__LP64__)
171static const char* const kSystemLibDir = "/system/lib64";
172#else
173static const char* const kSystemLibDir = "/system/lib";
174#endif
175
176static std::string dirname(const char *path);
177
Dimitry Ivanov36ac45f2016-01-07 18:43:20 -0800178static bool is_greylisted(const char* name, const soinfo* needed_by) {
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800179 static const char* const kLibraryGreyList[] = {
Dimitry Ivanov36ac45f2016-01-07 18:43:20 -0800180 "libandroid_runtime.so",
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800181 "libbinder.so",
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800182 "libcrypto.so",
183 "libcutils.so",
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800184 "libmedia.so",
185 "libnativehelper.so",
Dimitry Ivanova2a05012016-01-25 15:48:44 -0800186 "libskia.so",
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800187 "libssl.so",
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800188 "libstagefright.so",
Dimitry Ivanov31e910c2016-03-03 18:13:18 -0800189 "libsqlite.so",
Dimitry Ivanov78dfc402016-01-12 10:37:38 -0800190 "libui.so",
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800191 "libutils.so",
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800192 nullptr
193 };
194
Dimitry Ivanovf1db47a2016-01-11 10:20:15 -0800195 // limit greylisting to apps targeting sdk version 23 and below
196 if (get_application_target_sdk_version() > 23) {
197 return false;
198 }
199
Dimitry Ivanov36ac45f2016-01-07 18:43:20 -0800200 // if the library needed by a system library - implicitly assume it
201 // is greylisted
202
203 if (needed_by != nullptr && is_system_library(needed_by->get_realpath())) {
204 return true;
205 }
206
Dimitry Ivanovb8e37692016-02-12 13:33:19 -0800207 // if this is an absolute path - make sure it points to /system/lib(64)
208 if (name[0] == '/' && dirname(name) == kSystemLibDir) {
209 // and reduce the path to basename
210 name = basename(name);
211 }
212
Dimitry Ivanova8bda262016-01-07 11:16:43 -0800213 for (size_t i = 0; kLibraryGreyList[i] != nullptr; ++i) {
214 if (strcmp(name, kLibraryGreyList[i]) == 0) {
215 return true;
216 }
217 }
218
219 return false;
220}
221// END OF WORKAROUND
222
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700223static const ElfW(Versym) kVersymNotNeeded = 0;
224static const ElfW(Versym) kVersymGlobal = 1;
225
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700226static const char* const* g_default_ld_paths;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700227static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800228
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700229static std::vector<soinfo*> g_ld_preloads;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600230
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700231static bool g_public_namespace_initialized;
232static soinfo::soinfo_list_t g_public_namespace;
233
Elliott Hughes1728b232014-05-14 10:02:03 -0700234__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800235
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700236__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700237
Evgenii Stepanov68650822015-06-10 13:38:39 -0700238static std::string dirname(const char *path) {
239 const char* last_slash = strrchr(path, '/');
240 if (last_slash == path) return "/";
241 else if (last_slash == nullptr) return ".";
242 else
243 return std::string(path, last_slash - path);
244}
245
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800246#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700247struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700248 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700249};
250
251static linker_stats_t linker_stats;
252
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800253void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700254 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700255}
256#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800257void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700258}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800259#endif
260
261#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800262uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800263#endif
264
Dima Zavin2e855792009-05-20 18:28:09 -0700265static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700266
Elliott Hughes650be4e2013-03-05 18:47:58 -0800267char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700268 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700269}
270
Elliott Hughes650be4e2013-03-05 18:47:58 -0800271size_t linker_get_error_buffer_size() {
272 return sizeof(__linker_dl_err_buf);
273}
274
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700275// This function is an empty stub where GDB locates a breakpoint to get notified
276// about linker activity.
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700277extern "C"
278void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800279
Elliott Hughes1728b232014-05-14 10:02:03 -0700280static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700281static r_debug _r_debug =
282 {1, nullptr, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
283
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800284static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800285
Dimitry Ivanov05b60b22016-02-16 13:43:35 -0800286static void insert_link_map_into_debug_map(link_map* map) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700287 // Stick the new library at the end of the list.
288 // gdb tends to care more about libc than it does
289 // about leaf libraries, and ordering it this way
290 // reduces the back-and-forth over the wire.
291 if (r_debug_tail) {
292 r_debug_tail->l_next = map;
293 map->l_prev = r_debug_tail;
294 map->l_next = 0;
295 } else {
296 _r_debug.r_map = map;
297 map->l_prev = 0;
298 map->l_next = 0;
299 }
300 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800301}
302
Dimitry Ivanov05b60b22016-02-16 13:43:35 -0800303static void insert_soinfo_into_debug_map(soinfo* info) {
304 // Copy the necessary fields into the debug structure.
305 link_map* map = &(info->link_map_head);
306 map->l_addr = info->load_bias;
307 // link_map l_name field is not const.
308 map->l_name = const_cast<char*>(info->get_realpath());
309 map->l_ld = info->dynamic;
310
311 insert_link_map_into_debug_map(map);
312}
313
Elliott Hughesbedfe382012-08-14 14:07:59 -0700314static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700315 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700316
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700317 if (r_debug_tail == map) {
318 r_debug_tail = map->l_prev;
319 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700320
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700321 if (map->l_prev) {
322 map->l_prev->l_next = map->l_next;
323 }
324 if (map->l_next) {
325 map->l_next->l_prev = map->l_prev;
326 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700327}
328
Elliott Hughesbedfe382012-08-14 14:07:59 -0700329static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800330 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700331 // GDB already knows about the main executable
332 return;
333 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800334
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700335 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800336
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700337 _r_debug.r_state = r_debug::RT_ADD;
338 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800339
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700340 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800341
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700342 _r_debug.r_state = r_debug::RT_CONSISTENT;
343 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700344}
345
Elliott Hughesbedfe382012-08-14 14:07:59 -0700346static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800347 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700348 // GDB already knows about the main executable
349 return;
350 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700351
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700352 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700353
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700354 _r_debug.r_state = r_debug::RT_DELETE;
355 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700356
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700357 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700358
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700359 _r_debug.r_state = r_debug::RT_CONSISTENT;
360 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800361}
362
Elliott Hughes18a206c2012-10-29 17:37:13 -0700363void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800364 _r_debug.r_state = r_debug::RT_ADD;
365 rtld_db_dlactivity();
366 _r_debug.r_state = r_debug::RT_CONSISTENT;
367 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800368}
369
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700370bool android_namespace_t::is_accessible(const std::string& file) {
371 if (!is_isolated_) {
372 return true;
373 }
374
375 for (const auto& dir : ld_library_paths_) {
376 if (file_is_in_dir(file, dir)) {
377 return true;
378 }
379 }
380
381 for (const auto& dir : default_library_paths_) {
382 if (file_is_in_dir(file, dir)) {
383 return true;
384 }
385 }
386
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800387 for (const auto& dir : permitted_paths_) {
388 if (file_is_under_dir(file, dir)) {
389 return true;
390 }
391 }
392
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700393 return false;
394}
395
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700396LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
397 return g_soinfo_links_allocator.alloc();
398}
399
400void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
401 g_soinfo_links_allocator.free(entry);
402}
403
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700404static soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
405 struct stat* file_stat, off64_t file_offset,
406 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700407 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200408 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700409 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200410 }
411
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700412 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
413 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700414
Magnus Malmbornba98d922012-09-12 13:00:55 +0200415 sonext->next = si;
416 sonext = si;
417
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700418 ns->soinfo_list().push_back(si);
419
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700420 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200421 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800422}
423
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800424static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700425 if (si == nullptr) {
426 return;
427 }
428
429 if (si->base != 0 && si->size != 0) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800430 if (!si->is_mapped_by_caller()) {
431 munmap(reinterpret_cast<void*>(si->base), si->size);
432 } else {
433 // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE
434 mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE,
435 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
436 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700437 }
438
439 soinfo *prev = nullptr, *trav;
440
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700441 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700442
443 for (trav = solist; trav != nullptr; trav = trav->next) {
444 if (trav == si) {
445 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700446 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700447 prev = trav;
448 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800449
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700450 if (trav == nullptr) {
451 // si was not in solist
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700452 DL_ERR("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700453 return;
454 }
Elliott Hughes46882792012-08-03 16:49:39 -0700455
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700456 // clear links to/from si
457 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700458
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700459 // prev will never be null, because the first entry in solist is
460 // always the static libdl_info.
461 prev->next = si->next;
462 if (si == sonext) {
463 sonext = prev;
464 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800465
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700466 // remove from the namespace
467 si->get_namespace()->soinfo_list().remove_if([&](soinfo* candidate) {
468 return si == candidate;
469 });
470
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700471 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700472 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800473}
474
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700475// For every path element this function checks of it exists, and is a directory,
476// and normalizes it:
477// 1. For regular path it converts it to realpath()
478// 2. For path in a zip file it uses realpath on the zipfile
479// normalizes entry name by calling normalize_path function.
480static void resolve_paths(std::vector<std::string>& paths,
481 std::vector<std::string>* resolved_paths) {
482 resolved_paths->clear();
483 for (const auto& path : paths) {
484 char resolved_path[PATH_MAX];
485 const char* original_path = path.c_str();
486 if (realpath(original_path, resolved_path) != nullptr) {
487 struct stat s;
488 if (stat(resolved_path, &s) == 0) {
489 if (S_ISDIR(s.st_mode)) {
490 resolved_paths->push_back(resolved_path);
491 } else {
492 DL_WARN("Warning: \"%s\" is not a directory (excluding from path)", resolved_path);
493 continue;
494 }
495 } else {
496 DL_WARN("Warning: cannot stat file \"%s\": %s", resolved_path, strerror(errno));
497 continue;
498 }
499 } else {
500 std::string zip_path;
501 std::string entry_path;
502
503 std::string normalized_path;
504
505 if (!normalize_path(original_path, &normalized_path)) {
506 DL_WARN("Warning: unable to normalize \"%s\"", original_path);
507 continue;
508 }
509
510 if (parse_zip_path(normalized_path.c_str(), &zip_path, &entry_path)) {
511 if (realpath(zip_path.c_str(), resolved_path) == nullptr) {
512 DL_WARN("Warning: unable to resolve \"%s\": %s", zip_path.c_str(), strerror(errno));
513 continue;
514 }
515
516 ZipArchiveHandle handle = nullptr;
517 if (OpenArchive(resolved_path, &handle) != 0) {
518 DL_WARN("Warning: unable to open zip archive: %s", resolved_path);
519 continue;
520 }
521
522 // Check if zip-file has a dir with entry_path name
523 void* cookie = nullptr;
524 std::string prefix_str = entry_path + "/";
525 ZipString prefix(prefix_str.c_str());
526
527 ZipEntry out_data;
528 ZipString out_name;
529
530 int32_t error_code;
531
532 if ((error_code = StartIteration(handle, &cookie, &prefix, nullptr)) != 0) {
533 DL_WARN("Unable to iterate over zip-archive entries \"%s\";"
534 " error code: %d", zip_path.c_str(), error_code);
535 continue;
536 }
537
538 if (Next(cookie, &out_data, &out_name) != 0) {
539 DL_WARN("Unable to find entries starting with \"%s\" in \"%s\"",
540 prefix_str.c_str(), zip_path.c_str());
541 continue;
542 }
543
544 auto zip_guard = make_scope_guard([&]() {
545 if (cookie != nullptr) {
546 EndIteration(cookie);
547 }
548 CloseArchive(handle);
549 });
550
551 resolved_paths->push_back(std::string(resolved_path) + kZipFileSeparator + entry_path);
552 }
553 }
554 }
555}
556
557static void split_path(const char* path, const char* delimiters,
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700558 std::vector<std::string>* paths) {
Dmitriy Ivanovfbfba642015-11-16 14:23:37 -0800559 if (path != nullptr && path[0] != 0) {
tony.ys_liub4474402015-07-29 18:00:22 +0800560 *paths = android::base::Split(path, delimiters);
Elliott Hughescade4c32012-12-20 14:42:14 -0800561 }
562}
563
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700564static void parse_path(const char* path, const char* delimiters,
565 std::vector<std::string>* resolved_paths) {
566 std::vector<std::string> paths;
567 split_path(path, delimiters, &paths);
568 resolve_paths(paths, resolved_paths);
569}
570
Elliott Hughescade4c32012-12-20 14:42:14 -0800571static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700572 std::vector<std::string> ld_libary_paths;
573 parse_path(path, ":", &ld_libary_paths);
574 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800575}
576
Evgenii Stepanov68650822015-06-10 13:38:39 -0700577void soinfo::set_dt_runpath(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700578 if (!has_min_version(3)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700579 return;
580 }
581
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700582 std::vector<std::string> runpaths;
583
584 split_path(path, ":", &runpaths);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700585
586 std::string origin = dirname(get_realpath());
587 // FIXME: add $LIB and $PLATFORM.
588 std::pair<std::string, std::string> substs[] = {{"ORIGIN", origin}};
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700589 for (auto&& s : runpaths) {
Evgenii Stepanov68650822015-06-10 13:38:39 -0700590 size_t pos = 0;
591 while (pos < s.size()) {
592 pos = s.find("$", pos);
593 if (pos == std::string::npos) break;
594 for (const auto& subst : substs) {
595 const std::string& token = subst.first;
596 const std::string& replacement = subst.second;
597 if (s.substr(pos + 1, token.size()) == token) {
598 s.replace(pos, token.size() + 1, replacement);
599 // -1 to compensate for the ++pos below.
600 pos += replacement.size() - 1;
601 break;
602 } else if (s.substr(pos + 1, token.size() + 2) == "{" + token + "}") {
603 s.replace(pos, token.size() + 3, replacement);
604 pos += replacement.size() - 1;
605 break;
606 }
607 }
608 // Skip $ in case it did not match any of the known substitutions.
609 ++pos;
610 }
611 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700612
613 resolve_paths(runpaths, &dt_runpath_);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700614}
615
Elliott Hughescade4c32012-12-20 14:42:14 -0800616static void parse_LD_PRELOAD(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700617 g_ld_preload_names.clear();
618 if (path != nullptr) {
619 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
620 g_ld_preload_names = android::base::Split(path, " :");
621 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800622}
623
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700624static bool realpath_fd(int fd, std::string* realpath) {
625 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700626 __libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700627 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -0700628 PRINT("readlink('%s') failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700629 return false;
630 }
631
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700632 *realpath = &buf[0];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700633 return true;
634}
635
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700636#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700637
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700638// For a given PC, find the .so that it belongs to.
639// Returns the base address of the .ARM.exidx section
640// for that .so, and the number of 8-byte entries
641// in that section (via *pcount).
642//
643// Intended to be called by libc's __gnu_Unwind_Find_exidx().
644//
645// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800646_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800647 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800648
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700649 for (soinfo* si = solist; si != 0; si = si->next) {
650 if ((addr >= si->base) && (addr < (si->base + si->size))) {
651 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800652 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800653 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700654 }
655 *pcount = 0;
656 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800657}
Elliott Hughes46882792012-08-03 16:49:39 -0700658
Christopher Ferris24053a42013-08-19 17:45:09 -0700659#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700660
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700661// Here, we only have to provide a callback to iterate across all the
662// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700663int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700664 int rv = 0;
665 for (soinfo* si = solist; si != nullptr; si = si->next) {
666 dl_phdr_info dl_info;
667 dl_info.dlpi_addr = si->link_map_head.l_addr;
668 dl_info.dlpi_name = si->link_map_head.l_name;
669 dl_info.dlpi_phdr = si->phdr;
670 dl_info.dlpi_phnum = si->phnum;
671 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
672 if (rv != 0) {
673 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800674 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700675 }
676 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800677}
Elliott Hughes46882792012-08-03 16:49:39 -0700678
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700679const ElfW(Versym)* soinfo::get_versym(size_t n) const {
680 if (has_min_version(2) && versym_ != nullptr) {
681 return versym_ + n;
682 }
683
684 return nullptr;
685}
686
687ElfW(Addr) soinfo::get_verneed_ptr() const {
688 if (has_min_version(2)) {
689 return verneed_ptr_;
690 }
691
692 return 0;
693}
694
695size_t soinfo::get_verneed_cnt() const {
696 if (has_min_version(2)) {
697 return verneed_cnt_;
698 }
699
700 return 0;
701}
702
703ElfW(Addr) soinfo::get_verdef_ptr() const {
704 if (has_min_version(2)) {
705 return verdef_ptr_;
706 }
707
708 return 0;
709}
710
711size_t soinfo::get_verdef_cnt() const {
712 if (has_min_version(2)) {
713 return verdef_cnt_;
714 }
715
716 return 0;
717}
718
719template<typename F>
720static bool for_each_verdef(const soinfo* si, F functor) {
721 if (!si->has_min_version(2)) {
722 return true;
723 }
724
725 uintptr_t verdef_ptr = si->get_verdef_ptr();
726 if (verdef_ptr == 0) {
727 return true;
728 }
729
730 size_t offset = 0;
731
732 size_t verdef_cnt = si->get_verdef_cnt();
733 for (size_t i = 0; i<verdef_cnt; ++i) {
734 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
735 size_t verdaux_offset = offset + verdef->vd_aux;
736 offset += verdef->vd_next;
737
738 if (verdef->vd_version != 1) {
Dmitriy Ivanov3d7bea12015-04-20 17:40:39 -0700739 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700740 i, verdef->vd_version, si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700741 return false;
742 }
743
744 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
745 // "this is the version of the file itself. It must not be used for
746 // matching a symbol. It can be used to match references."
747 //
748 // http://www.akkadia.org/drepper/symbol-versioning
749 continue;
750 }
751
752 if (verdef->vd_cnt == 0) {
753 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
754 return false;
755 }
756
757 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
758
759 if (functor(i, verdef, verdaux) == true) {
760 break;
761 }
762 }
763
764 return true;
765}
766
767bool soinfo::find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const {
768 if (vi == nullptr) {
769 *versym = kVersymNotNeeded;
770 return true;
771 }
772
773 *versym = kVersymGlobal;
774
775 return for_each_verdef(this,
776 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
777 if (verdef->vd_hash == vi->elf_hash &&
778 strcmp(vi->name, get_string(verdaux->vda_name)) == 0) {
779 *versym = verdef->vd_ndx;
780 return true;
781 }
782
783 return false;
784 }
785 );
786}
787
788bool soinfo::find_symbol_by_name(SymbolName& symbol_name,
789 const version_info* vi,
790 const ElfW(Sym)** symbol) const {
791 uint32_t symbol_index;
792 bool success =
793 is_gnu_hash() ?
794 gnu_lookup(symbol_name, vi, &symbol_index) :
795 elf_lookup(symbol_name, vi, &symbol_index);
796
797 if (success) {
798 *symbol = symbol_index == 0 ? nullptr : symtab_ + symbol_index;
799 }
800
801 return success;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800802}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800803
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800804static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
805 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
806 ELF_ST_BIND(s->st_info) == STB_WEAK) {
807 return s->st_shndx != SHN_UNDEF;
808 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
809 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700810 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800811 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800812
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800813 return false;
814}
815
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700816static const ElfW(Versym) kVersymHiddenBit = 0x8000;
817
818static inline bool is_versym_hidden(const ElfW(Versym)* versym) {
819 // the symbol is hidden if bit 15 of versym is set.
820 return versym != nullptr && (*versym & kVersymHiddenBit) != 0;
821}
822
823static inline bool check_symbol_version(const ElfW(Versym) verneed,
824 const ElfW(Versym)* verdef) {
825 return verneed == kVersymNotNeeded ||
826 verdef == nullptr ||
827 verneed == (*verdef & ~kVersymHiddenBit);
828}
829
830bool soinfo::gnu_lookup(SymbolName& symbol_name,
831 const version_info* vi,
832 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800833 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800834 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800835
836 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800837 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
838 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800839
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700840 *symbol_index = 0;
841
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700842 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700843 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700844
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800845 // test against bloom filter
846 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700847 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700848 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700849
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700850 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800851 }
852
853 // bloom test says "probably yes"...
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700854 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800855
856 if (n == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700857 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700858 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700859
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700860 return true;
861 }
862
863 // lookup versym for the version definition in this library
864 // note the difference between "version is not requested" (vi == nullptr)
865 // and "version not found". In the first case verneed is kVersymNotNeeded
866 // which implies that the default version can be accepted; the second case results in
867 // verneed = 1 (kVersymGlobal) and implies that we should ignore versioned symbols
868 // for this library and consider only *global* ones.
869 ElfW(Versym) verneed = 0;
870 if (!find_verdef_version_index(vi, &verneed)) {
871 return false;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800872 }
873
874 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800875 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700876 const ElfW(Versym)* verdef = get_versym(n);
877 // skip hidden versions when verneed == kVersymNotNeeded (0)
878 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
879 continue;
880 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700881 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700882 check_symbol_version(verneed, verdef) &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800883 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
884 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700885 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700886 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(s->st_value),
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700887 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700888 *symbol_index = n;
889 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800890 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700891 } while ((gnu_chain_[n++] & 1) == 0);
892
893 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700894 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800895
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700896 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800897}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800898
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700899bool soinfo::elf_lookup(SymbolName& symbol_name,
900 const version_info* vi,
901 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800902 uint32_t hash = symbol_name.elf_hash();
903
904 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700905 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700906 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800907
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700908 ElfW(Versym) verneed = 0;
909 if (!find_verdef_version_index(vi, &verneed)) {
910 return false;
911 }
912
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800913 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
914 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700915 const ElfW(Versym)* verdef = get_versym(n);
916
917 // skip hidden versions when verneed == 0
918 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
919 continue;
920 }
921
922 if (check_symbol_version(verneed, verdef) &&
923 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700924 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800925 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700926 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700927 reinterpret_cast<void*>(s->st_value),
928 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700929 *symbol_index = n;
930 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800931 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800932 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800933
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700934 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700935 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700936 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700937
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700938 *symbol_index = 0;
939 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800940}
941
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700942soinfo::soinfo(android_namespace_t* ns, const char* realpath,
943 const struct stat* file_stat, off64_t file_offset,
944 int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700945 memset(this, 0, sizeof(*this));
946
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700947 if (realpath != nullptr) {
948 realpath_ = realpath;
949 }
950
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800951 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800952 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700953
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700954 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800955 this->st_dev_ = file_stat->st_dev;
956 this->st_ino_ = file_stat->st_ino;
957 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700958 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700959
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800960 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700961 this->namespace_ = ns;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700962}
963
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800964static uint32_t calculate_elf_hash(const char* name) {
965 const uint8_t* name_bytes = reinterpret_cast<const uint8_t*>(name);
966 uint32_t h = 0, g;
967
968 while (*name_bytes) {
969 h = (h << 4) + *name_bytes++;
970 g = h & 0xf0000000;
971 h ^= g;
972 h ^= g >> 24;
973 }
974
975 return h;
976}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800977
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800978uint32_t SymbolName::elf_hash() {
979 if (!has_elf_hash_) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800980 elf_hash_ = calculate_elf_hash(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800981 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700982 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800983
984 return elf_hash_;
985}
986
987uint32_t SymbolName::gnu_hash() {
988 if (!has_gnu_hash_) {
989 uint32_t h = 5381;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700990 const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800991 while (*name != 0) {
992 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
993 }
994
995 gnu_hash_ = h;
996 has_gnu_hash_ = true;
997 }
998
999 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001000}
1001
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001002bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
1003 soinfo** si_found_in, const soinfo::soinfo_list_t& global_group,
1004 const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001005 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001006 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001007
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07001008 /* "This element's presence in a shared object library alters the dynamic linker's
1009 * symbol resolution algorithm for references within the library. Instead of starting
1010 * a symbol search with the executable file, the dynamic linker starts from the shared
1011 * object itself. If the shared object fails to supply the referenced symbol, the
1012 * dynamic linker then searches the executable file and other shared objects as usual."
1013 *
1014 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
1015 *
1016 * Note that this is unlikely since static linker avoids generating
1017 * relocations for -Bsymbolic linked dynamic executables.
1018 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001019 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001020 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001021 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
1022 return false;
1023 }
1024
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07001025 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001026 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07001027 }
1028 }
1029
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001030 // 1. Look for it in global_group
1031 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001032 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001033 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001034 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001035 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001036 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
1037 error = true;
1038 return false;
1039 }
1040
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07001041 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001042 *si_found_in = global_si;
1043 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07001044 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -07001045
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001046 return true;
1047 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001048
1049 if (error) {
1050 return false;
1051 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07001052 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001053
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001054 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001055 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001056 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001057 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001058 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -07001059 // we already did this - skip
1060 return true;
1061 }
1062
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001063 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001064 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001065 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
1066 error = true;
1067 return false;
1068 }
1069
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001070 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001071 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001072 return false;
1073 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001074
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001075 return true;
1076 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001077
1078 if (error) {
1079 return false;
1080 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001081 }
1082
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001083 if (s != nullptr) {
1084 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
1085 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001086 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
1087 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001088 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001089 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001090
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001091 *symbol = s;
1092 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001093}
1094
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001095class ProtectedDataGuard {
1096 public:
1097 ProtectedDataGuard() {
1098 if (ref_count_++ == 0) {
1099 protect_data(PROT_READ | PROT_WRITE);
1100 }
1101 }
1102
1103 ~ProtectedDataGuard() {
1104 if (ref_count_ == 0) { // overflow
1105 __libc_fatal("Too many nested calls to dlopen()");
1106 }
1107
1108 if (--ref_count_ == 0) {
1109 protect_data(PROT_READ);
1110 }
1111 }
1112 private:
1113 void protect_data(int protection) {
1114 g_soinfo_allocator.protect_all(protection);
1115 g_soinfo_links_allocator.protect_all(protection);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001116 g_namespace_allocator.protect_all(protection);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001117 }
1118
1119 static size_t ref_count_;
1120};
1121
1122size_t ProtectedDataGuard::ref_count_ = 0;
1123
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001124// Each size has it's own allocator.
1125template<size_t size>
1126class SizeBasedAllocator {
1127 public:
1128 static void* alloc() {
1129 return allocator_.alloc();
1130 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001131
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001132 static void free(void* ptr) {
1133 allocator_.free(ptr);
1134 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001135
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001136 private:
1137 static LinkerBlockAllocator allocator_;
1138};
1139
1140template<size_t size>
1141LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
1142
1143template<typename T>
1144class TypeBasedAllocator {
1145 public:
1146 static T* alloc() {
1147 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
1148 }
1149
1150 static void free(T* ptr) {
1151 SizeBasedAllocator<sizeof(T)>::free(ptr);
1152 }
1153};
1154
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001155class LoadTask {
1156 public:
1157 struct deleter_t {
1158 void operator()(LoadTask* t) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001159 t->~LoadTask();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001160 TypeBasedAllocator<LoadTask>::free(t);
1161 }
1162 };
1163
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001164 static deleter_t deleter;
1165
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001166 static LoadTask* create(const char* name, soinfo* needed_by,
1167 std::unordered_map<const soinfo*, ElfReader>* readers_map) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001168 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001169 return new (ptr) LoadTask(name, needed_by, readers_map);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001170 }
1171
1172 const char* get_name() const {
1173 return name_;
1174 }
1175
1176 soinfo* get_needed_by() const {
1177 return needed_by_;
1178 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001179
1180 soinfo* get_soinfo() const {
1181 return si_;
1182 }
1183
1184 void set_soinfo(soinfo* si) {
1185 si_ = si;
1186 }
1187
1188 off64_t get_file_offset() const {
1189 return file_offset_;
1190 }
1191
1192 void set_file_offset(off64_t offset) {
1193 file_offset_ = offset;
1194 }
1195
1196 int get_fd() const {
1197 return fd_;
1198 }
1199
1200 void set_fd(int fd, bool assume_ownership) {
1201 fd_ = fd;
1202 close_fd_ = assume_ownership;
1203 }
1204
1205 const android_dlextinfo* get_extinfo() const {
1206 return extinfo_;
1207 }
1208
1209 void set_extinfo(const android_dlextinfo* extinfo) {
1210 extinfo_ = extinfo;
1211 }
1212
Dimitry Ivanov10057482016-01-28 17:24:20 -08001213 bool is_dt_needed() const {
1214 return is_dt_needed_;
1215 }
1216
1217 void set_dt_needed(bool is_dt_needed) {
1218 is_dt_needed_ = is_dt_needed;
1219 }
1220
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001221 const ElfReader& get_elf_reader() const {
1222 CHECK(si_ != nullptr);
1223 return (*elf_readers_map_)[si_];
1224 }
1225
1226 ElfReader& get_elf_reader() {
1227 CHECK(si_ != nullptr);
1228 return (*elf_readers_map_)[si_];
1229 }
1230
1231 std::unordered_map<const soinfo*, ElfReader>* get_readers_map() {
1232 return elf_readers_map_;
1233 }
1234
1235 bool read(const char* realpath, off64_t file_size) {
1236 ElfReader& elf_reader = get_elf_reader();
1237 return elf_reader.Read(realpath, fd_, file_offset_, file_size);
1238 }
1239
1240 bool load() {
1241 ElfReader& elf_reader = get_elf_reader();
1242 if (!elf_reader.Load(extinfo_)) {
1243 return false;
1244 }
1245
1246 si_->base = elf_reader.load_start();
1247 si_->size = elf_reader.load_size();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -08001248 si_->set_mapped_by_caller(elf_reader.is_mapped_by_caller());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001249 si_->load_bias = elf_reader.load_bias();
1250 si_->phnum = elf_reader.phdr_count();
1251 si_->phdr = elf_reader.loaded_phdr();
1252
1253 return true;
1254 }
1255
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001256 private:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001257 LoadTask(const char* name, soinfo* needed_by,
1258 std::unordered_map<const soinfo*, ElfReader>* readers_map)
1259 : name_(name), needed_by_(needed_by), si_(nullptr),
Dimitry Ivanov10057482016-01-28 17:24:20 -08001260 fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map),
1261 is_dt_needed_(false) {}
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001262
1263 ~LoadTask() {
1264 if (fd_ != -1 && close_fd_) {
1265 close(fd_);
1266 }
1267 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001268
1269 const char* name_;
1270 soinfo* needed_by_;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001271 soinfo* si_;
1272 const android_dlextinfo* extinfo_;
1273 int fd_;
1274 bool close_fd_;
1275 off64_t file_offset_;
1276 std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_;
Dimitry Ivanov10057482016-01-28 17:24:20 -08001277 // TODO(dimitry): workaround for http://b/26394120 - will be removed before the release
1278 bool is_dt_needed_;
1279 // END OF WORKAROUND
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001280
1281 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
1282};
1283
Ningsheng Jiane93be992014-09-16 15:22:10 +08001284LoadTask::deleter_t LoadTask::deleter;
1285
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001286template <typename T>
1287using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
1288
1289typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001290typedef linked_list_t<const char> StringLinkedList;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001291typedef std::vector<LoadTask*> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001292
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001293
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001294// This function walks down the tree of soinfo dependencies
1295// in breadth-first order and
1296// * calls action(soinfo* si) for each node, and
1297// * terminates walk if action returns false.
1298//
1299// walk_dependencies_tree returns false if walk was terminated
1300// by the action and true otherwise.
1301template<typename F>
1302static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001303 SoinfoLinkedList visit_list;
1304 SoinfoLinkedList visited;
1305
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001306 for (size_t i = 0; i < root_soinfos_size; ++i) {
1307 visit_list.push_back(root_soinfos[i]);
1308 }
1309
1310 soinfo* si;
1311 while ((si = visit_list.pop_front()) != nullptr) {
1312 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -07001313 continue;
1314 }
1315
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001316 if (!action(si)) {
1317 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001318 }
1319
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001320 visited.push_back(si);
1321
1322 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001323 visit_list.push_back(child);
1324 });
1325 }
1326
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001327 return true;
1328}
1329
1330
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001331static const ElfW(Sym)* dlsym_handle_lookup(soinfo* root, soinfo* skip_until,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001332 soinfo** found, SymbolName& symbol_name,
1333 const version_info* vi) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001334 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001335 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001336
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001337 walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) {
1338 if (skip_lookup) {
1339 skip_lookup = current_soinfo != skip_until;
1340 return true;
1341 }
1342
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001343 if (!current_soinfo->find_symbol_by_name(symbol_name, vi, &result)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001344 result = nullptr;
1345 return false;
1346 }
1347
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001348 if (result != nullptr) {
1349 *found = current_soinfo;
1350 return false;
1351 }
1352
1353 return true;
1354 });
1355
1356 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001357}
1358
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001359static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
1360 const char* name,
1361 const version_info* vi,
1362 soinfo** found,
1363 soinfo* caller,
1364 void* handle);
1365
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001366// This is used by dlsym(3). It performs symbol lookup only within the
1367// specified soinfo object and its dependencies in breadth first order.
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001368static const ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found,
1369 const char* name, const version_info* vi) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -07001370 // According to man dlopen(3) and posix docs in the case when si is handle
1371 // of the main executable we need to search not only in the executable and its
1372 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
1373 //
1374 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
1375 // libraries and they are loaded in breath-first (correct) order we can just execute
1376 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
1377 if (si == somain) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001378 return dlsym_linear_lookup(&g_default_namespace, name, vi, found, nullptr, RTLD_DEFAULT);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -07001379 }
1380
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001381 SymbolName symbol_name(name);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001382 return dlsym_handle_lookup(si, nullptr, found, symbol_name, vi);
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001383}
1384
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001385/* This is used by dlsym(3) to performs a global symbol lookup. If the
1386 start value is null (for RTLD_DEFAULT), the search starts at the
1387 beginning of the global solist. Otherwise the search starts at the
1388 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001389 */
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001390static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
1391 const char* name,
1392 const version_info* vi,
1393 soinfo** found,
1394 soinfo* caller,
1395 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001396 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001397
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001398 soinfo::soinfo_list_t& soinfo_list = ns->soinfo_list();
1399 soinfo::soinfo_list_t::iterator start = soinfo_list.begin();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001400
1401 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -07001402 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001403 return nullptr;
1404 } else {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001405 soinfo::soinfo_list_t::iterator it = soinfo_list.find(caller);
1406 CHECK (it != soinfo_list.end());
1407 start = ++it;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001408 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001409 }
1410
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001411 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001412 for (soinfo::soinfo_list_t::iterator it = start, end = soinfo_list.end(); it != end; ++it) {
1413 soinfo* si = *it;
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001414 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
1415 // if the library is opened by application with target api level <= 22
1416 // See http://b/21565766
1417 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 && si->get_target_sdk_version() > 22) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001418 continue;
1419 }
1420
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001421 if (!si->find_symbol_by_name(symbol_name, vi, &s)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001422 return nullptr;
1423 }
1424
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001425 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -08001426 *found = si;
1427 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -06001428 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001429 }
Matt Fischer1698d9e2009-12-31 12:17:56 -06001430
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001431 // If not found - use dlsym_handle_lookup for caller's
1432 // local_group unless it is part of the global group in which
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001433 // case we already did it.
1434 if (s == nullptr && caller != nullptr &&
1435 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001436 return dlsym_handle_lookup(caller->get_local_group_root(),
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001437 (handle == RTLD_NEXT) ? caller : nullptr, found, symbol_name, vi);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001438 }
1439
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001440 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001441 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
1442 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -08001443 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001444
Elliott Hughescade4c32012-12-20 14:42:14 -08001445 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001446}
1447
Kito Chengfa8c05d2013-03-12 14:58:06 +08001448soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001449 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001450 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001451 if (address >= si->base && address - si->base < si->size) {
1452 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001453 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001454 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001455 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001456}
1457
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001458ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
1459 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
1460}
1461
1462static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
1463 return sym->st_shndx != SHN_UNDEF &&
1464 soaddr >= sym->st_value &&
1465 soaddr < sym->st_value + sym->st_size;
1466}
1467
1468ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001469 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001470
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001471 for (size_t i = 0; i < gnu_nbucket_; ++i) {
1472 uint32_t n = gnu_bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001473
1474 if (n == 0) {
1475 continue;
1476 }
1477
1478 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001479 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001480 if (symbol_matches_soaddr(sym, soaddr)) {
1481 return sym;
1482 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001483 } while ((gnu_chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001484 }
1485
1486 return nullptr;
1487}
1488
1489ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001490 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001491
Kito Chengfa8c05d2013-03-12 14:58:06 +08001492 // Search the library's symbol table for any defined symbol which
1493 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001494 for (size_t i = 0; i < nchain_; ++i) {
1495 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001496 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001497 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001498 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001499 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001500
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001501 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001502}
1503
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001504class ZipArchiveCache {
1505 public:
1506 ZipArchiveCache() {}
1507 ~ZipArchiveCache();
1508
1509 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
1510 private:
1511 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
1512
1513 std::unordered_map<std::string, ZipArchiveHandle> cache_;
1514};
1515
1516bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
1517 std::string key(zip_path);
1518
1519 auto it = cache_.find(key);
1520 if (it != cache_.end()) {
1521 *handle = it->second;
1522 return true;
1523 }
1524
1525 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1526 if (fd == -1) {
1527 return false;
1528 }
1529
1530 if (OpenArchiveFd(fd, "", handle) != 0) {
1531 // invalid zip-file (?)
1532 close(fd);
1533 return false;
1534 }
1535
1536 cache_[key] = *handle;
1537 return true;
1538}
1539
1540ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -07001541 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001542 CloseArchive(it.second);
1543 }
1544}
1545
1546static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001547 const char* const input_path,
1548 off64_t* file_offset, std::string* realpath) {
1549 std::string normalized_path;
1550 if (!normalize_path(input_path, &normalized_path)) {
1551 return -1;
1552 }
1553
1554 const char* const path = normalized_path.c_str();
1555 TRACE("Trying zip file open from path '%s' -> normalized '%s'", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +00001556
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001557 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +00001558 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001559 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +00001560 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001561 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +00001562 if (separator == nullptr) {
1563 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -07001564 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001565
1566 char buf[512];
1567 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
1568 PRINT("Warning: ignoring very long library path: %s", path);
1569 return -1;
1570 }
1571
1572 buf[separator - path] = '\0';
1573
1574 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001575 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +00001576 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1577 if (fd == -1) {
1578 return -1;
1579 }
1580
1581 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001582 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001583 // invalid zip-file (?)
1584 close(fd);
1585 return -1;
1586 }
1587
Simon Baldwinaef71952015-01-16 13:22:54 +00001588 ZipEntry entry;
1589
Yusuke Sato56f40fb2015-06-25 14:56:07 -07001590 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001591 // Entry was not found.
1592 close(fd);
1593 return -1;
1594 }
1595
1596 // Check if it is properly stored
1597 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
1598 close(fd);
1599 return -1;
1600 }
1601
1602 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001603
1604 if (realpath_fd(fd, realpath)) {
1605 *realpath += separator;
1606 } else {
1607 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1608 normalized_path.c_str());
1609 *realpath = normalized_path;
1610 }
1611
Simon Baldwinaef71952015-01-16 13:22:54 +00001612 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001613}
1614
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001615static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
1616 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
1617 if (n < 0 || n >= static_cast<int>(buf_size)) {
1618 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1619 return false;
1620 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001621
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001622 return true;
1623}
1624
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001625static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1626 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001627 const std::vector<std::string>& paths,
1628 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001629 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001630 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001631 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001632 continue;
1633 }
1634
1635 int fd = -1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001636 if (strstr(buf, kZipFileSeparator) != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001637 fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath);
Simon Baldwinaef71952015-01-16 13:22:54 +00001638 }
1639
1640 if (fd == -1) {
1641 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1642 if (fd != -1) {
1643 *file_offset = 0;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001644 if (!realpath_fd(fd, realpath)) {
1645 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
1646 *realpath = buf;
1647 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001648 }
1649 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001650
1651 if (fd != -1) {
1652 return fd;
1653 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001654 }
1655
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001656 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001657}
1658
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001659static int open_library(android_namespace_t* ns,
1660 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001661 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001662 off64_t* file_offset, std::string* realpath) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001663 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001664
Elliott Hughes124fae92012-10-31 14:20:03 -07001665 // 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 -07001666 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001667 int fd = -1;
1668
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001669 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001670 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1671 }
1672
1673 if (fd == -1) {
1674 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001675 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001676 *file_offset = 0;
1677 if (!realpath_fd(fd, realpath)) {
1678 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name);
1679 *realpath = name;
1680 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001681 }
1682 }
1683
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001684 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001685 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001686
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001687 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1688 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 -07001689 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001690 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001691 // Check if the library is accessible
1692 if (fd != -1 && !ns->is_accessible(*realpath)) {
1693 fd = -1;
1694 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001695 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001696
Elliott Hughes124fae92012-10-31 14:20:03 -07001697 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001698 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001699 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001700
Dimitry Ivanova8bda262016-01-07 11:16:43 -08001701 // TODO(dimitry): workaround for http://b/26394120 - will be removed before the release
Dimitry Ivanov36ac45f2016-01-07 18:43:20 -08001702 if (fd == -1 && ns != &g_default_namespace && is_greylisted(name, needed_by)) {
Dimitry Ivanova8bda262016-01-07 11:16:43 -08001703 // try searching for it on default_namespace default_library_path
1704 fd = open_library_on_paths(zip_archive_cache, name, file_offset,
1705 g_default_namespace.get_default_library_paths(), realpath);
1706 }
1707 // END OF WORKAROUND
1708
Elliott Hughes124fae92012-10-31 14:20:03 -07001709 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001710}
1711
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001712static const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
1713#if !defined(__LP64__)
1714 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001715 if (get_application_target_sdk_version() <= 22) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001716 const char* bname = basename(dt_needed);
1717 if (bname != dt_needed) {
1718 DL_WARN("'%s' library has invalid DT_NEEDED entry '%s'", sopath, dt_needed);
Dimitry Ivanovdf91dc22016-02-25 15:22:04 -08001719 add_dlwarning(sopath, "invalid DT_NEEDED entry", dt_needed);
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001720 }
1721
1722 return bname;
1723 }
1724#endif
1725 return dt_needed;
1726}
1727
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001728template<typename F>
1729static void for_each_dt_needed(const soinfo* si, F action) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001730 for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001731 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001732 action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
Dima Zavin2e855792009-05-20 18:28:09 -07001733 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001734 }
1735}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001736
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001737template<typename F>
1738static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
1739 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1740 if (d->d_tag == DT_NEEDED) {
1741 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1742 }
1743 }
1744}
1745
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001746static bool load_library(android_namespace_t* ns,
1747 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001748 LoadTaskList* load_tasks,
1749 int rtld_flags,
1750 const std::string& realpath) {
1751 off64_t file_offset = task->get_file_offset();
1752 const char* name = task->get_name();
1753 const android_dlextinfo* extinfo = task->get_extinfo();
1754
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001755 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001756 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001757 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001758 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001759 if (file_offset < 0) {
1760 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001761 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001762 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001763
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001764 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001765 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001766 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001767 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001768 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001769 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001770 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1771 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001772 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001773 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001774
1775 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001776 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1777 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001778 auto predicate = [&](soinfo* si) {
1779 return si->get_st_dev() != 0 &&
1780 si->get_st_ino() != 0 &&
1781 si->get_st_dev() == file_stat.st_dev &&
1782 si->get_st_ino() == file_stat.st_ino &&
1783 si->get_file_offset() == file_offset;
1784 };
1785
1786 soinfo* si = ns->soinfo_list().find_if(predicate);
1787
1788 // check public namespace
1789 if (si == nullptr) {
1790 si = g_public_namespace.find_if(predicate);
1791 if (si != nullptr) {
1792 ns->soinfo_list().push_back(si);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001793 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001794 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001795
1796 if (si != nullptr) {
1797 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
1798 "will return existing soinfo", name, si->get_realpath());
1799 task->set_soinfo(si);
1800 return true;
1801 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001802 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001803
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001804 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001805 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001806 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001807 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001808
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001809 if (!ns->is_accessible(realpath)) {
Dimitry Ivanova8bda262016-01-07 11:16:43 -08001810 // TODO(dimitry): workaround for http://b/26394120 - will be removed before the release
Dimitry Ivanov10057482016-01-28 17:24:20 -08001811 const soinfo* needed_by = task->is_dt_needed() ? task->get_needed_by() : nullptr;
Dimitry Ivanov36ac45f2016-01-07 18:43:20 -08001812 if (is_greylisted(name, needed_by)) {
1813 // print warning only if needed by non-system library
1814 if (needed_by == nullptr || !is_system_library(needed_by->get_realpath())) {
Dimitry Ivanovf53e7de2016-02-01 12:32:22 -08001815 const soinfo* needed_or_dlopened_by = task->get_needed_by();
Dimitry Ivanovdf91dc22016-02-25 15:22:04 -08001816 const char* sopath = needed_or_dlopened_by == nullptr ? "(unknown)" :
1817 needed_or_dlopened_by->get_realpath();
Dimitry Ivanovf53e7de2016-02-01 12:32:22 -08001818 DL_WARN("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the namespace \"%s\""
Dimitry Ivanovdc2bee92016-01-12 09:50:21 -08001819 " - the access is temporarily granted as a workaround for http://b/26394120",
Dimitry Ivanovdf91dc22016-02-25 15:22:04 -08001820 name, realpath.c_str(), sopath, ns->get_name());
1821 add_dlwarning(sopath, "unauthorized access to", name);
Dimitry Ivanov36ac45f2016-01-07 18:43:20 -08001822 }
Dimitry Ivanova8bda262016-01-07 11:16:43 -08001823 } else {
1824 // do not load libraries if they are not accessible for the specified namespace.
Dimitry Ivanov350bdad2016-03-01 13:11:28 -08001825 const char* needed_or_dlopened_by = task->get_needed_by() == nullptr ?
1826 "(unknown)" :
1827 task->get_needed_by()->get_realpath();
1828
1829 DL_ERR("library \"%s\" needed or dlopened by \"%s\" is not accessible for the namespace \"%s\"",
1830 name, needed_or_dlopened_by, ns->get_name());
1831
1832 PRINT("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the"
1833 " namespace: [name=\"%s\", ld_library_paths=\"%s\", default_library_paths=\"%s\","
1834 " permitted_paths=\"%s\"]",
1835 name, realpath.c_str(),
1836 needed_or_dlopened_by,
1837 ns->get_name(),
1838 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
1839 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
1840 android::base::Join(ns->get_permitted_paths(), ':').c_str());
Dimitry Ivanova8bda262016-01-07 11:16:43 -08001841 return false;
1842 }
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001843 }
1844
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001845 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001846 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001847 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001848 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001849
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001850 task->set_soinfo(si);
1851
1852 // Read the ELF header and some of the segments.
1853 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001854 soinfo_free(si);
1855 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001856 return false;
1857 }
1858
1859 // find and set DT_RUNPATH and dt_soname
1860 // Note that these field values are temporary and are
1861 // going to be overwritten on soinfo::prelink_image
1862 // with values from PT_LOAD segments.
1863 const ElfReader& elf_reader = task->get_elf_reader();
1864 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1865 if (d->d_tag == DT_RUNPATH) {
1866 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1867 }
1868 if (d->d_tag == DT_SONAME) {
1869 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1870 }
1871 }
1872
1873 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
1874 load_tasks->push_back(LoadTask::create(name, si, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001875 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001876
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001877 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001878}
1879
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001880static bool load_library(android_namespace_t* ns,
1881 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001882 ZipArchiveCache* zip_archive_cache,
1883 LoadTaskList* load_tasks,
1884 int rtld_flags) {
1885 const char* name = task->get_name();
1886 soinfo* needed_by = task->get_needed_by();
1887 const android_dlextinfo* extinfo = task->get_extinfo();
1888
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001889 off64_t file_offset;
1890 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001891 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001892 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001893 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1894 file_offset = extinfo->library_fd_offset;
1895 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001896
1897 if (!realpath_fd(extinfo->library_fd, &realpath)) {
1898 PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1899 "Will use given name.", name);
1900 realpath = name;
1901 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001902
1903 task->set_fd(extinfo->library_fd, false);
1904 task->set_file_offset(file_offset);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001905 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001906 }
1907
1908 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001909 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001910 if (fd == -1) {
1911 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001912 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001913 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001914
1915 task->set_fd(fd, true);
1916 task->set_file_offset(file_offset);
1917
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001918 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001919}
1920
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001921// Returns true if library was found and false in 2 cases
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001922// 1. (for default namespace only) The library was found but loaded under different
1923// target_sdk_version (*candidate != nullptr)
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001924// 2. The library was not found by soname (*candidate is nullptr)
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001925static bool find_loaded_library_by_soname(android_namespace_t* ns,
1926 const char* name, soinfo** candidate) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001927 *candidate = nullptr;
1928
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001929 // Ignore filename with path.
1930 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001931 return false;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001932 }
1933
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001934 uint32_t target_sdk_version = get_application_target_sdk_version();
1935
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001936 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001937 const char* soname = si->get_soname();
1938 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001939 // If the library was opened under different target sdk version
1940 // skip this step and try to reopen it. The exceptions are
1941 // "libdl.so" and global group. There is no point in skipping
1942 // them because relocation process is going to use them
1943 // in any case.
1944 bool is_libdl = si == solist;
1945 if (is_libdl || (si->get_dt_flags_1() & DF_1_GLOBAL) != 0 ||
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001946 !si->is_linked() || si->get_target_sdk_version() == target_sdk_version ||
1947 ns != &g_default_namespace) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001948 *candidate = si;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001949 return false;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001950 } else if (*candidate == nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001951 // for the different sdk version in the default namespace
1952 // remember the first library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001953 *candidate = si;
1954 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001955 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001956
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001957 return true;
1958 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001959}
1960
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001961static bool find_library_internal(android_namespace_t* ns,
1962 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001963 ZipArchiveCache* zip_archive_cache,
1964 LoadTaskList* load_tasks,
1965 int rtld_flags) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001966 soinfo* candidate;
1967
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001968 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001969 task->set_soinfo(candidate);
1970 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001971 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001972
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001973 if (ns != &g_default_namespace) {
1974 // check public namespace
1975 candidate = g_public_namespace.find_if([&](soinfo* si) {
1976 return strcmp(task->get_name(), si->get_soname()) == 0;
1977 });
1978
1979 if (candidate != nullptr) {
1980 ns->soinfo_list().push_back(candidate);
1981 task->set_soinfo(candidate);
1982 return true;
1983 }
1984 }
1985
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001986 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001987 // of this fact is done by load_library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001988 TRACE("[ '%s' find_loaded_library_by_soname returned false (*candidate=%s@%p). Trying harder...]",
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001989 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001990
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001991 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001992 return true;
1993 } else {
1994 // In case we were unable to load the library but there
1995 // is a candidate loaded under the same soname but different
1996 // sdk level - return it anyways.
1997 if (candidate != nullptr) {
1998 task->set_soinfo(candidate);
1999 return true;
2000 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002001 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002002
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002003 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002004}
2005
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002006static void soinfo_unload(soinfo* si);
2007
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002008// TODO: this is slightly unusual way to construct
2009// the global group for relocation. Not every RTLD_GLOBAL
2010// library is included in this group for backwards-compatibility
2011// reasons.
2012//
2013// This group consists of the main executable, LD_PRELOADs
2014// and libraries with the DF_1_GLOBAL flag set.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002015static soinfo::soinfo_list_t make_global_group(android_namespace_t* ns) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002016 soinfo::soinfo_list_t global_group;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002017 ns->soinfo_list().for_each([&](soinfo* si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002018 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
2019 global_group.push_back(si);
2020 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002021 });
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002022
2023 return global_group;
2024}
2025
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002026static void shuffle(std::vector<LoadTask*>* v) {
2027 for (size_t i = 0, size = v->size(); i < size; ++i) {
2028 size_t n = size - i;
2029 size_t r = arc4random_uniform(n);
2030 std::swap((*v)[n-1], (*v)[r]);
2031 }
2032}
2033
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002034// add_as_children - add first-level loaded libraries (i.e. library_names[], but
2035// not their transitive dependencies) as children of the start_with library.
2036// This is false when find_libraries is called for dlopen(), when newly loaded
2037// libraries must form a disjoint tree.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002038static bool find_libraries(android_namespace_t* ns,
2039 soinfo* start_with,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002040 const char* const library_names[],
2041 size_t library_names_count, soinfo* soinfos[],
2042 std::vector<soinfo*>* ld_preloads,
2043 size_t ld_preloads_count, int rtld_flags,
2044 const android_dlextinfo* extinfo,
2045 bool add_as_children) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002046 // Step 0: prepare.
2047 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002048 std::unordered_map<const soinfo*, ElfReader> readers_map;
2049
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002050 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002051 const char* name = library_names[i];
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002052 load_tasks.push_back(LoadTask::create(name, start_with, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002053 }
2054
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002055 // Construct global_group.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002056 soinfo::soinfo_list_t global_group = make_global_group(ns);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002057
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002058 // If soinfos array is null allocate one on stack.
2059 // The array is needed in case of failure; for example
2060 // when library_names[] = {libone.so, libtwo.so} and libone.so
2061 // is loaded correctly but libtwo.so failed for some reason.
2062 // In this case libone.so should be unloaded on return.
2063 // See also implementation of failure_guard below.
2064
2065 if (soinfos == nullptr) {
2066 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
2067 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
2068 memset(soinfos, 0, soinfos_size);
2069 }
2070
2071 // list of libraries to link - see step 2.
2072 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002073
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002074 auto scope_guard = make_scope_guard([&]() {
2075 for (LoadTask* t : load_tasks) {
2076 LoadTask::deleter(t);
2077 }
2078 });
2079
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07002080 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002081 // Housekeeping
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002082 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002083 soinfo_unload(soinfos[i]);
2084 }
2085 });
2086
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07002087 ZipArchiveCache zip_archive_cache;
2088
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002089 // Step 1: expand the list of load_tasks to include
2090 // all DT_NEEDED libraries (do not load them just yet)
2091 for (size_t i = 0; i<load_tasks.size(); ++i) {
2092 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07002093 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002094
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07002095 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002096 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dimitry Ivanov10057482016-01-28 17:24:20 -08002097 task->set_dt_needed(is_dt_needed);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07002098
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002099 if(!find_library_internal(ns, task, &zip_archive_cache, &load_tasks, rtld_flags)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002100 return false;
2101 }
2102
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002103 soinfo* si = task->get_soinfo();
2104
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07002105 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002106 needed_by->add_child(si);
2107 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002108
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002109 if (si->is_linked()) {
2110 si->increment_ref_count();
2111 }
2112
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002113 // When ld_preloads is not null, the first
2114 // ld_preloads_count libs are in fact ld_preloads.
2115 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07002116 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002117 }
2118
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002119 if (soinfos_count < library_names_count) {
2120 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002121 }
2122 }
2123
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002124 // Step 2: Load libraries in random order (see b/24047022)
2125 LoadTaskList load_list;
2126 for (auto&& task : load_tasks) {
2127 soinfo* si = task->get_soinfo();
2128 auto pred = [&](const LoadTask* t) {
2129 return t->get_soinfo() == si;
2130 };
2131
2132 if (!si->is_linked() &&
2133 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
2134 load_list.push_back(task);
2135 }
2136 }
2137 shuffle(&load_list);
2138
2139 for (auto&& task : load_list) {
2140 if (!task->load()) {
2141 return false;
2142 }
2143 }
2144
2145 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
2146 for (auto&& task : load_tasks) {
2147 soinfo* si = task->get_soinfo();
2148 if (!si->is_linked() && !si->prelink_image()) {
2149 return false;
2150 }
2151 }
2152
2153 // Step 4: Add LD_PRELOADed libraries to the global group for
2154 // future runs. There is no need to explicitly add them to
2155 // the global group for this run because they are going to
2156 // appear in the local group in the correct order.
2157 if (ld_preloads != nullptr) {
2158 for (auto&& si : *ld_preloads) {
2159 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2160 }
2161 }
2162
2163
2164 // Step 5: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002165 soinfo::soinfo_list_t local_group;
2166 walk_dependencies_tree(
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002167 (start_with != nullptr && add_as_children) ? &start_with : soinfos,
2168 (start_with != nullptr && add_as_children) ? 1 : soinfos_count,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002169 [&] (soinfo* si) {
2170 local_group.push_back(si);
2171 return true;
2172 });
2173
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002174 // We need to increment ref_count in case
2175 // the root of the local group was not linked.
2176 bool was_local_group_root_linked = local_group.front()->is_linked();
2177
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002178 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002179 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002180 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002181 return false;
2182 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002183 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002184 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002185
2186 return true;
2187 });
2188
2189 if (linked) {
2190 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002191 }
2192
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002193 if (!was_local_group_root_linked) {
2194 local_group.front()->increment_ref_count();
2195 }
2196
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002197 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002198}
2199
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002200static soinfo* find_library(android_namespace_t* ns,
2201 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002202 const android_dlextinfo* extinfo,
2203 soinfo* needed_by) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002204 soinfo* si;
2205
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002206 if (name == nullptr) {
2207 si = somain;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002208 } else if (!find_libraries(ns, needed_by, &name, 1, &si, nullptr, 0, rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002209 extinfo, /* add_as_children */ false)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002210 return nullptr;
2211 }
2212
Elliott Hughesd23736e2012-11-01 15:16:56 -07002213 return si;
2214}
Elliott Hughesbedfe382012-08-14 14:07:59 -07002215
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002216static void soinfo_unload(soinfo* root) {
2217 // Note that the library can be loaded but not linked;
2218 // in which case there is no root but we still need
2219 // to walk the tree and unload soinfos involved.
2220 //
2221 // This happens on unsuccessful dlopen, when one of
2222 // the DT_NEEDED libraries could not be linked/found.
2223 if (root->is_linked()) {
2224 root = root->get_local_group_root();
2225 }
2226
2227 if (!root->can_unload()) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002228 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002229 return;
2230 }
2231
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002232 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002233
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002234 if (ref_count == 0) {
2235 soinfo::soinfo_list_t local_unload_list;
2236 soinfo::soinfo_list_t external_unload_list;
2237 soinfo::soinfo_list_t depth_first_list;
2238 depth_first_list.push_back(root);
2239 soinfo* si = nullptr;
2240
2241 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002242 if (local_unload_list.contains(si)) {
2243 continue;
2244 }
2245
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002246 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002247
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002248 if (si->has_min_version(0)) {
2249 soinfo* child = nullptr;
2250 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002251 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
2252 child->get_realpath(), child);
2253
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002254 if (local_unload_list.contains(child)) {
2255 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002256 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002257 external_unload_list.push_back(child);
2258 } else {
2259 depth_first_list.push_front(child);
2260 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002261 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002262 } else {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07002263#if !defined(__work_around_b_24465209__)
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002264 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002265#else
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002266 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002267 for_each_dt_needed(si, [&] (const char* library_name) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002268 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002269 si->get_realpath(), library_name);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002270
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002271 soinfo* needed = find_library(si->get_namespace(),
2272 library_name, RTLD_NOLOAD, nullptr, nullptr);
2273
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002274 if (needed != nullptr) {
2275 // Not found: for example if symlink was deleted between dlopen and dlclose
2276 // Since we cannot really handle errors at this point - print and continue.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002277 PRINT("warning: couldn't find %s needed by %s on unload.",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002278 library_name, si->get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002279 return;
2280 } else if (local_unload_list.contains(needed)) {
2281 // already visited
2282 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002283 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002284 // external group
2285 external_unload_list.push_back(needed);
2286 } else {
2287 // local group
2288 depth_first_list.push_front(needed);
2289 }
2290 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002291#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002292 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002293 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002294
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002295 local_unload_list.for_each([](soinfo* si) {
2296 si->call_destructors();
2297 });
2298
2299 while ((si = local_unload_list.pop_front()) != nullptr) {
2300 notify_gdb_of_unload(si);
2301 soinfo_free(si);
2302 }
2303
2304 while ((si = external_unload_list.pop_front()) != nullptr) {
2305 soinfo_unload(si);
2306 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002307 } else {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002308 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
2309 root->get_realpath(), ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08002310 }
2311}
2312
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002313static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
2314 if (sym_ver == nullptr) {
2315 return sym_name;
2316 }
2317
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002318 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002319}
2320
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002321void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002322 // Use basic string manipulation calls to avoid snprintf.
2323 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
2324 // When debug malloc is enabled, this call returns 0. This in turn causes
2325 // snprintf to do nothing, which causes libraries to fail to load.
2326 // See b/17302493 for further details.
2327 // Once the above bug is fixed, this code can be modified to use
2328 // snprintf again.
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002329 size_t required_len = 0;
2330 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2331 required_len += strlen(g_default_ld_paths[i]) + 1;
2332 }
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002333 if (buffer_size < required_len) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002334 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
2335 "buffer len %zu, required len %zu", buffer_size, required_len);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002336 }
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002337 char* end = buffer;
2338 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2339 if (i > 0) *end++ = ':';
2340 end = stpcpy(end, g_default_ld_paths[i]);
2341 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002342}
2343
Elliott Hughescade4c32012-12-20 14:42:14 -08002344void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08002345 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08002346}
2347
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002348soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo,
2349 void* caller_addr) {
2350 soinfo* const caller = find_containing_library(caller_addr);
2351
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002352 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08002353 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002354 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08002355 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002356
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002357 android_namespace_t* ns = caller != nullptr ? caller->get_namespace() : g_anonymous_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002358
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002359 if (extinfo != nullptr) {
2360 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
2361 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
2362 return nullptr;
2363 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002364
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002365 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07002366 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002367 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
2368 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002369 return nullptr;
2370 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002371
2372 if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 &&
2373 (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) {
2374 DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not "
2375 "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT");
2376 return nullptr;
2377 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002378
2379 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
2380 if (extinfo->library_namespace == nullptr) {
2381 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
2382 return nullptr;
2383 }
2384 ns = extinfo->library_namespace;
2385 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00002386 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002387
2388 ProtectedDataGuard guard;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002389 soinfo* si = find_library(ns, name, flags, extinfo, caller);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002390 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002391 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07002392 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002393
Elliott Hughesd23736e2012-11-01 15:16:56 -07002394 return si;
2395}
2396
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002397int do_dladdr(const void* addr, Dl_info* info) {
2398 // Determine if this address can be found in any library currently mapped.
2399 soinfo* si = find_containing_library(addr);
2400 if (si == nullptr) {
2401 return 0;
2402 }
2403
2404 memset(info, 0, sizeof(Dl_info));
2405
2406 info->dli_fname = si->get_realpath();
2407 // Address at which the shared object is loaded.
2408 info->dli_fbase = reinterpret_cast<void*>(si->base);
2409
2410 // Determine if any symbol in the library contains the specified address.
2411 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
2412 if (sym != nullptr) {
2413 info->dli_sname = si->get_string(sym->st_name);
2414 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
2415 }
2416
2417 return 1;
2418}
2419
2420bool do_dlsym(void* handle, const char* sym_name, const char* sym_ver,
2421 void* caller_addr, void** symbol) {
2422#if !defined(__LP64__)
2423 if (handle == nullptr) {
2424 DL_ERR("dlsym failed: library handle is null");
2425 return false;
2426 }
2427#endif
2428
2429 if (sym_name == nullptr) {
2430 DL_ERR("dlsym failed: symbol name is null");
2431 return false;
2432 }
2433
2434 soinfo* found = nullptr;
2435 const ElfW(Sym)* sym = nullptr;
2436 soinfo* caller = find_containing_library(caller_addr);
2437 android_namespace_t* ns = caller != nullptr ? caller->get_namespace() : g_anonymous_namespace;
2438
2439 version_info vi_instance;
2440 version_info* vi = nullptr;
2441
2442 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002443 vi_instance.name = sym_ver;
2444 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002445 vi = &vi_instance;
2446 }
2447
2448 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2449 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2450 } else {
2451 sym = dlsym_handle_lookup(reinterpret_cast<soinfo*>(handle), &found, sym_name, vi);
2452 }
2453
2454 if (sym != nullptr) {
2455 uint32_t bind = ELF_ST_BIND(sym->st_info);
2456
2457 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2458 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
2459 return true;
2460 }
2461
2462 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2463 return false;
2464 }
2465
2466 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2467 return false;
2468}
2469
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002470void do_dlclose(soinfo* si) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002471 ProtectedDataGuard guard;
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002472 soinfo_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002473}
2474
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002475bool init_namespaces(const char* public_ns_sonames, const char* anon_ns_library_path) {
2476 CHECK(public_ns_sonames != nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002477 if (g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002478 DL_ERR("public namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002479 return false;
2480 }
2481
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002482 std::vector<std::string> sonames = android::base::Split(public_ns_sonames, ":");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002483
2484 ProtectedDataGuard guard;
2485
2486 auto failure_guard = make_scope_guard([&]() {
2487 g_public_namespace.clear();
2488 });
2489
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002490 for (const auto& soname : sonames) {
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -08002491 soinfo* candidate = nullptr;
2492
2493 find_loaded_library_by_soname(&g_default_namespace, soname.c_str(), &candidate);
2494
2495 if (candidate == nullptr) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002496 DL_ERR("error initializing public namespace: \"%s\" was not found"
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002497 " in the default namespace", soname.c_str());
2498 return false;
2499 }
2500
2501 candidate->set_nodelete();
2502 g_public_namespace.push_back(candidate);
2503 }
2504
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002505 g_public_namespace_initialized = true;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002506
2507 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002508 // When the caller is nullptr - create_namespace will take global group
2509 // from the anonymous namespace, which is fine because anonymous namespace
2510 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002511 android_namespace_t* anon_ns =
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002512 create_namespace(nullptr, "(anonymous)", nullptr, anon_ns_library_path,
2513 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002514
2515 if (anon_ns == nullptr) {
2516 g_public_namespace_initialized = false;
2517 return false;
2518 }
2519 g_anonymous_namespace = anon_ns;
2520 failure_guard.disable();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002521 return true;
2522}
2523
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002524android_namespace_t* create_namespace(const void* caller_addr,
2525 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002526 const char* ld_library_path,
2527 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002528 uint64_t type,
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002529 const char* permitted_when_isolated_path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002530 if (!g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002531 DL_ERR("cannot create namespace: public namespace is not initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002532 return nullptr;
2533 }
2534
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002535 soinfo* caller_soinfo = find_containing_library(caller_addr);
2536
2537 android_namespace_t* caller_ns = caller_soinfo != nullptr ?
2538 caller_soinfo->get_namespace() :
2539 g_anonymous_namespace;
2540
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002541 ProtectedDataGuard guard;
2542 std::vector<std::string> ld_library_paths;
2543 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002544 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002545
2546 parse_path(ld_library_path, ":", &ld_library_paths);
2547 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002548 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002549
2550 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2551 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002552 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002553 ns->set_ld_library_paths(std::move(ld_library_paths));
2554 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002555 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002556
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002557 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
2558 // If shared - clone the caller namespace
2559 auto& soinfo_list = caller_ns->soinfo_list();
2560 std::copy(soinfo_list.begin(), soinfo_list.end(), std::back_inserter(ns->soinfo_list()));
2561 } else {
2562 // If not shared - copy only the global group
2563 auto global_group = make_global_group(caller_ns);
2564 std::copy(global_group.begin(), global_group.end(), std::back_inserter(ns->soinfo_list()));
2565 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002566
2567 return ns;
2568}
2569
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002570static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
2571 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2572 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2573 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002574 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2575 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002576
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002577 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002578}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002579
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002580const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2581 if (source_symver < 2 ||
2582 source_symver >= version_infos.size() ||
2583 version_infos[source_symver].name == nullptr) {
2584 return nullptr;
2585 }
2586
2587 return &version_infos[source_symver];
2588}
2589
2590void VersionTracker::add_version_info(size_t source_index,
2591 ElfW(Word) elf_hash,
2592 const char* ver_name,
2593 const soinfo* target_si) {
2594 if (source_index >= version_infos.size()) {
2595 version_infos.resize(source_index+1);
2596 }
2597
2598 version_infos[source_index].elf_hash = elf_hash;
2599 version_infos[source_index].name = ver_name;
2600 version_infos[source_index].target_si = target_si;
2601}
2602
2603bool VersionTracker::init_verneed(const soinfo* si_from) {
2604 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2605
2606 if (verneed_ptr == 0) {
2607 return true;
2608 }
2609
2610 size_t verneed_cnt = si_from->get_verneed_cnt();
2611
2612 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2613 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2614 size_t vernaux_offset = offset + verneed->vn_aux;
2615 offset += verneed->vn_next;
2616
2617 if (verneed->vn_version != 1) {
2618 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2619 return false;
2620 }
2621
2622 const char* target_soname = si_from->get_string(verneed->vn_file);
2623 // find it in dependencies
2624 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002625 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002626 });
2627
2628 if (target_si == nullptr) {
2629 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002630 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002631 return false;
2632 }
2633
2634 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2635 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2636 vernaux_offset += vernaux->vna_next;
2637
2638 const ElfW(Word) elf_hash = vernaux->vna_hash;
2639 const char* ver_name = si_from->get_string(vernaux->vna_name);
2640 ElfW(Half) source_index = vernaux->vna_other;
2641
2642 add_version_info(source_index, elf_hash, ver_name, target_si);
2643 }
2644 }
2645
2646 return true;
2647}
2648
2649bool VersionTracker::init_verdef(const soinfo* si_from) {
2650 return for_each_verdef(si_from,
2651 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2652 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2653 si_from->get_string(verdaux->vda_name), si_from);
2654 return false;
2655 }
2656 );
2657}
2658
2659bool VersionTracker::init(const soinfo* si_from) {
2660 if (!si_from->has_min_version(2)) {
2661 return true;
2662 }
2663
2664 return init_verneed(si_from) && init_verdef(si_from);
2665}
2666
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002667bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2668 const char* sym_name, const version_info** vi) {
2669 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2670 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2671
2672 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2673 *vi = version_tracker.get_version_info(sym_ver);
2674
2675 if (*vi == nullptr) {
2676 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002677 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002678 return false;
2679 }
2680 } else {
2681 // there is no version info
2682 *vi = nullptr;
2683 }
2684
2685 return true;
2686}
2687
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002688#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002689#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002690static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2691 return rela->r_addend;
2692}
2693#else
2694static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002695 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2696 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002697 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2698 }
2699 return 0;
2700}
2701#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002702
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002703template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002704bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2705 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002706 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2707 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002708 if (rel == nullptr) {
2709 return false;
2710 }
2711
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002712 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2713 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2714
2715 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002716 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002717 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002718 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002719
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002720 DEBUG("Processing '%s' relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002721 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002722 continue;
2723 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002724
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002725 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002726 soinfo* lsi = nullptr;
2727
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002728 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002729 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002730 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002731
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002732 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2733 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002734 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002735
2736 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2737 return false;
2738 }
2739
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002740 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002741 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002742 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002743 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002744 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002745 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002746 }
2747
2748 /* IHI0044C AAELF 4.5.1.1:
2749
2750 Libraries are not searched to resolve weak references.
2751 It is not an error for a weak reference to remain unsatisfied.
2752
2753 During linking, the value of an undefined weak reference is:
2754 - Zero if the relocation type is absolute
2755 - The address of the place if the relocation is pc-relative
2756 - The address of nominal base address if the relocation
2757 type is base-relative.
2758 */
2759
2760 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002761 case R_GENERIC_JUMP_SLOT:
2762 case R_GENERIC_GLOB_DAT:
2763 case R_GENERIC_RELATIVE:
2764 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002765#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002766 case R_AARCH64_ABS64:
2767 case R_AARCH64_ABS32:
2768 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002769#elif defined(__x86_64__)
2770 case R_X86_64_32:
2771 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002772#elif defined(__arm__)
2773 case R_ARM_ABS32:
2774#elif defined(__i386__)
2775 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002776#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002777 /*
2778 * The sym_addr was initialized to be zero above, or the relocation
2779 * code below does not care about value of sym_addr.
2780 * No need to do anything.
2781 */
2782 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002783#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002784 case R_X86_64_PC32:
2785 sym_addr = reloc;
2786 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002787#elif defined(__i386__)
2788 case R_386_PC32:
2789 sym_addr = reloc;
2790 break;
2791#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002792 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002793 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002794 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002795 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002796 } else { // We got a definition.
2797#if !defined(__LP64__)
2798 // When relocating dso with text_relocation .text segment is
2799 // not executable. We need to restore elf flags before resolving
2800 // STT_GNU_IFUNC symbol.
2801 bool protect_segments = has_text_relocations &&
2802 lsi == this &&
2803 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2804 if (protect_segments) {
2805 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2806 DL_ERR("can't protect segments for \"%s\": %s",
2807 get_realpath(), strerror(errno));
2808 return false;
2809 }
2810 }
2811#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002812 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002813#if !defined(__LP64__)
2814 if (protect_segments) {
2815 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2816 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2817 get_realpath(), strerror(errno));
2818 return false;
2819 }
2820 }
2821#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002822 }
2823 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002824 }
2825
2826 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002827 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002828 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002829 MARK(rel->r_offset);
2830 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2831 reinterpret_cast<void*>(reloc),
2832 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2833
2834 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002835 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002836 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002837 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002838 MARK(rel->r_offset);
2839 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2840 reinterpret_cast<void*>(reloc),
2841 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2842 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002843 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002844 case R_GENERIC_RELATIVE:
2845 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002846 MARK(rel->r_offset);
2847 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2848 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002849 reinterpret_cast<void*>(load_bias + addend));
2850 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002851 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002852 case R_GENERIC_IRELATIVE:
2853 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002854 MARK(rel->r_offset);
2855 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2856 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002857 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002858 {
2859#if !defined(__LP64__)
2860 // When relocating dso with text_relocation .text segment is
2861 // not executable. We need to restore elf flags for this
2862 // particular call.
2863 if (has_text_relocations) {
2864 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2865 DL_ERR("can't protect segments for \"%s\": %s",
2866 get_realpath(), strerror(errno));
2867 return false;
2868 }
2869 }
2870#endif
2871 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2872#if !defined(__LP64__)
2873 // Unprotect it afterwards...
2874 if (has_text_relocations) {
2875 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2876 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2877 get_realpath(), strerror(errno));
2878 return false;
2879 }
2880 }
2881#endif
2882 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2883 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002884 break;
2885
2886#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002887 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002888 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002889 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002890 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002891 reloc, sym_addr + addend, sym_name);
2892 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002893 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002894 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002895 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002896 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002897 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002898 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002899 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002900 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2901 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002902 if ((min_value <= (sym_addr + addend)) &&
2903 ((sym_addr + addend) <= max_value)) {
2904 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002905 } else {
2906 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002907 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002908 return false;
2909 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002910 }
2911 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002912 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002913 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002914 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002915 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002916 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002917 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002918 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2919 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002920 if ((min_value <= (sym_addr + addend)) &&
2921 ((sym_addr + addend) <= max_value)) {
2922 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002923 } else {
2924 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002925 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002926 return false;
2927 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002928 }
2929 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002930 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002931 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002932 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002933 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002934 reloc, sym_addr + addend, rel->r_offset, sym_name);
2935 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002936 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002937 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002938 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002939 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002940 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002941 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002942 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002943 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2944 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002945 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2946 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2947 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002948 } else {
2949 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002950 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002951 return false;
2952 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002953 }
2954 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002955 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002956 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002957 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002958 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002959 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002960 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002961 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2962 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002963 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2964 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2965 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002966 } else {
2967 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002968 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002969 return false;
2970 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002971 }
2972 break;
2973
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002974 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002975 /*
2976 * ET_EXEC is not supported so this should not happen.
2977 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002978 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002979 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002980 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002981 * R_AARCH64_COPY may only appear in executable objects where e_type is
2982 * set to ET_EXEC.
2983 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002984 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002985 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002986 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002987 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002988 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002989 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002990 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002991 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002992 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002993 break;
2994#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002995 case R_X86_64_32:
2996 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002997 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002998 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2999 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09003000 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003001 break;
3002 case R_X86_64_64:
3003 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003004 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003005 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
3006 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09003007 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003008 break;
3009 case R_X86_64_PC32:
3010 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003011 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003012 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
3013 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
3014 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09003015 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003016 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003017#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003018 case R_ARM_ABS32:
3019 count_relocation(kRelocAbsolute);
3020 MARK(rel->r_offset);
3021 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
3022 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
3023 break;
3024 case R_ARM_REL32:
3025 count_relocation(kRelocRelative);
3026 MARK(rel->r_offset);
3027 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
3028 reloc, sym_addr, rel->r_offset, sym_name);
3029 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
3030 break;
3031 case R_ARM_COPY:
3032 /*
3033 * ET_EXEC is not supported so this should not happen.
3034 *
3035 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
3036 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003037 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003038 * R_ARM_COPY may only appear in executable objects where e_type is
3039 * set to ET_EXEC.
3040 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003041 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08003042 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003043#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003044 case R_386_32:
3045 count_relocation(kRelocRelative);
3046 MARK(rel->r_offset);
3047 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
3048 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
3049 break;
3050 case R_386_PC32:
3051 count_relocation(kRelocRelative);
3052 MARK(rel->r_offset);
3053 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
3054 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
3055 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
3056 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003057#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003058 default:
3059 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003060 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003061 }
3062 }
3063 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003064}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08003065#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003066
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003067void soinfo::call_array(const char* array_name __unused, linker_function_t* functions,
3068 size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003069 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07003070 return;
3071 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02003072
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003073 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, get_realpath());
Elliott Hughesca0c11b2013-03-12 10:40:45 -07003074
3075 int begin = reverse ? (count - 1) : 0;
3076 int end = reverse ? -1 : count;
3077 int step = reverse ? -1 : 1;
3078
3079 for (int i = begin; i != end; i += step) {
3080 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003081 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07003082 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02003083
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003084 TRACE("[ Done calling %s for '%s' ]", array_name, get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003085}
3086
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003087void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003088 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07003089 return;
3090 }
3091
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003092 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Elliott Hughesd23736e2012-11-01 15:16:56 -07003093 function();
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003094 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04003095}
3096
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003097void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003098 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
3099 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003100 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07003101}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003102
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003103void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07003104 if (constructors_called) {
3105 return;
3106 }
Jesse Hallf5d16932012-01-30 15:39:57 -08003107
Elliott Hughesd23736e2012-11-01 15:16:56 -07003108 // We set constructors_called before actually calling the constructors, otherwise it doesn't
3109 // protect against recursive constructor calls. One simple example of constructor recursion
3110 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
3111 // 1. The program depends on libc, so libc's constructor is called here.
3112 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
3113 // 3. dlopen() calls the constructors on the newly created
3114 // soinfo for libc_malloc_debug_leak.so.
3115 // 4. The debug .so depends on libc, so CallConstructors is
3116 // called again with the libc soinfo. If it doesn't trigger the early-
3117 // out above, the libc constructor will be called again (recursively!).
3118 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003119
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003120 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003121 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07003122 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003123 get_realpath(), preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07003124 }
3125
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003126 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003127 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003128 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003129
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003130 TRACE("\"%s\": calling constructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003131
3132 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003133 call_function("DT_INIT", init_func_);
3134 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003135}
David 'Digit' Turner82156792009-05-18 14:37:41 +02003136
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003137void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003138 if (!constructors_called) {
3139 return;
3140 }
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003141 TRACE("\"%s\": calling destructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003142
3143 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003144 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003145
3146 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003147 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07003148
3149 // This is needed on second call to dlopen
3150 // after library has been unloaded with RTLD_NODELETE
3151 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003152}
3153
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003154void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003155 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003156 child->parents_.push_back(this);
3157 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003158 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003159}
3160
3161void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003162 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003163 return;
3164 }
3165
3166 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003167 children_.for_each([&] (soinfo* child) {
3168 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003169 return parent == this;
3170 });
3171 });
3172
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003173 parents_.for_each([&] (soinfo* parent) {
3174 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003175 return child == this;
3176 });
3177 });
3178
3179 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003180 parents_.clear();
3181 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003182}
3183
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003184dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003185 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003186 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003187 }
3188
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003189 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003190};
3191
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003192ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003193 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003194 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003195 }
3196
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003197 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003198}
3199
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003200off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003201 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003202 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003203 }
3204
3205 return 0;
3206}
3207
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003208uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07003209 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003210 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07003211 }
3212
3213 return 0;
3214}
3215
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003216uint32_t soinfo::get_dt_flags_1() const {
3217 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003218 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003219 }
3220
3221 return 0;
3222}
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003223
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003224void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
3225 if (has_min_version(1)) {
3226 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003227 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003228 }
3229
3230 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003231 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003232 }
3233
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003234 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003235 }
3236}
3237
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003238void soinfo::set_nodelete() {
3239 rtld_flags_ |= RTLD_NODELETE;
3240}
3241
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003242const char* soinfo::get_realpath() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003243#if defined(__work_around_b_24465209__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003244 if (has_min_version(2)) {
3245 return realpath_.c_str();
3246 } else {
3247 return old_name_;
3248 }
3249#else
3250 return realpath_.c_str();
3251#endif
3252}
3253
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003254void soinfo::set_soname(const char* soname) {
3255#if defined(__work_around_b_24465209__)
3256 if (has_min_version(2)) {
3257 soname_ = soname;
3258 }
3259 strlcpy(old_name_, soname_, sizeof(old_name_));
3260#else
3261 soname_ = soname;
3262#endif
3263}
3264
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003265const char* soinfo::get_soname() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003266#if defined(__work_around_b_24465209__)
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003267 if (has_min_version(2)) {
3268 return soname_;
3269 } else {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003270 return old_name_;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003271 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003272#else
3273 return soname_;
3274#endif
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003275}
3276
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003277// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003278// 'this->flags' does not have FLAG_NEW_SOINFO set.
3279static soinfo::soinfo_list_t g_empty_list;
3280
3281soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003282 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003283 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003284 }
3285
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003286 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003287}
3288
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003289const soinfo::soinfo_list_t& soinfo::get_children() const {
3290 if (has_min_version(0)) {
3291 return children_;
3292 }
3293
3294 return g_empty_list;
3295}
3296
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003297soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003298 if (has_min_version(0)) {
3299 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003300 }
3301
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003302 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003303}
3304
Evgenii Stepanov68650822015-06-10 13:38:39 -07003305static std::vector<std::string> g_empty_runpath;
3306
3307const std::vector<std::string>& soinfo::get_dt_runpath() const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003308 if (has_min_version(3)) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003309 return dt_runpath_;
3310 }
3311
3312 return g_empty_runpath;
3313}
3314
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003315android_namespace_t* soinfo::get_namespace() {
3316 if (has_min_version(3)) {
3317 return namespace_;
3318 }
3319
3320 return &g_default_namespace;
3321}
3322
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003323ElfW(Addr) soinfo::resolve_symbol_address(const ElfW(Sym)* s) const {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003324 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
3325 return call_ifunc_resolver(s->st_value + load_bias);
3326 }
3327
3328 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
3329}
3330
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003331const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003332 if (has_min_version(1) && (index >= strtab_size_)) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003333 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003334 get_realpath(), strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003335 }
3336
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003337 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003338}
3339
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003340bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003341 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003342}
3343
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003344bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003345 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003346}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003347
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003348bool soinfo::is_linked() const {
3349 return (flags_ & FLAG_LINKED) != 0;
3350}
3351
3352bool soinfo::is_main_executable() const {
3353 return (flags_ & FLAG_EXE) != 0;
3354}
3355
3356void soinfo::set_linked() {
3357 flags_ |= FLAG_LINKED;
3358}
3359
3360void soinfo::set_linker_flag() {
3361 flags_ |= FLAG_LINKER;
3362}
3363
3364void soinfo::set_main_executable() {
3365 flags_ |= FLAG_EXE;
3366}
3367
3368void soinfo::increment_ref_count() {
3369 local_group_root_->ref_count_++;
3370}
3371
3372size_t soinfo::decrement_ref_count() {
3373 return --local_group_root_->ref_count_;
3374}
3375
3376soinfo* soinfo::get_local_group_root() const {
3377 return local_group_root_;
3378}
3379
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -08003380
3381void soinfo::set_mapped_by_caller(bool mapped_by_caller) {
3382 if (mapped_by_caller) {
3383 flags_ |= FLAG_MAPPED_BY_CALLER;
3384 } else {
3385 flags_ &= ~FLAG_MAPPED_BY_CALLER;
3386 }
3387}
3388
3389bool soinfo::is_mapped_by_caller() const {
3390 return (flags_ & FLAG_MAPPED_BY_CALLER) != 0;
3391}
3392
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003393// This function returns api-level at the time of
3394// dlopen/load. Note that libraries opened by system
3395// will always have 'current' api level.
3396uint32_t soinfo::get_target_sdk_version() const {
3397 if (!has_min_version(2)) {
3398 return __ANDROID_API__;
3399 }
3400
3401 return local_group_root_->target_sdk_version_;
3402}
3403
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003404bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08003405 /* Extract dynamic section */
3406 ElfW(Word) dynamic_flags = 0;
3407 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07003408
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003409 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003410 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003411 if (!relocating_linker) {
Elliott Hughes116b5692016-01-04 17:45:36 -08003412 INFO("[ Linking '%s' ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003413 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003414 }
3415
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003416 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003417 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003418 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003419 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003420 return false;
3421 } else {
3422 if (!relocating_linker) {
3423 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003424 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003425 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003426
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003427#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003428 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
3429 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003430#endif
3431
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003432 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003433 // Note that: "Except for the DT_NULL element at the end of the array,
3434 // and the relative order of DT_NEEDED elements, entries may appear in any order."
3435 //
3436 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003437 uint32_t needed_count = 0;
3438 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
3439 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
3440 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3441 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003442 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003443 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003444 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003445
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003446 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003447 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
3448 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
3449 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
3450 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003451 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003452
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003453 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003454 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003455 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003456 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
3457 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003458
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003459 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003460 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003461 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003462 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
3463 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003464
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003465 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003466 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003467 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003468 return false;
3469 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003470 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003471
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003472 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003473 break;
3474
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003475 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003476 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003477 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003478
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003479 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003480 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003481 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003482
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003483 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003484 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003485 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003486
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003487 case DT_SYMENT:
3488 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003489 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
3490 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003491 return false;
3492 }
3493 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003494
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003495 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003496#if defined(USE_RELA)
3497 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003498 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003499 return false;
3500 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003501#else
3502 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003503 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003504 return false;
3505 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003506#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003507 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003508
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003509 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003510#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003511 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003512#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003513 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003514#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003515 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003516
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003517 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003518#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003519 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003520#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003521 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003522#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003523 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003524
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003525 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003526#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003527 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003528 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003529#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003530 // Ignore for other platforms... (because RTLD_LAZY is not supported)
3531 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003532
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003533 case DT_DEBUG:
3534 // Set the DT_DEBUG entry to the address of _r_debug for GDB
3535 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08003536// FIXME: not working currently for N64
3537// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003538// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08003539// read-only, but the DYNAMIC header claims it is writable.
3540#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003541 if ((dynamic_flags & PF_W) != 0) {
3542 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
3543 }
Chris Dearman99186652014-02-06 20:36:51 -08003544#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08003545 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003546#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003547 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003548 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003549 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003550
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003551 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003552 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003553 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003554
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003555 case DT_ANDROID_RELA:
3556 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3557 break;
3558
3559 case DT_ANDROID_RELASZ:
3560 android_relocs_size_ = d->d_un.d_val;
3561 break;
3562
3563 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003564 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003565 return false;
3566
3567 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003568 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003569 return false;
3570
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003571 case DT_RELAENT:
3572 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003573 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003574 return false;
3575 }
3576 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003577
3578 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003579 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003580 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003581
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003582 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003583 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003584 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003585
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003586 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003587 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003588 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003589
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003590#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003591 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003592 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003593 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003594
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003595 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003596 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003597 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003598
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003599 case DT_RELENT:
3600 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003601 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003602 return false;
3603 }
3604 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003605
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003606 case DT_ANDROID_REL:
3607 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3608 break;
3609
3610 case DT_ANDROID_RELSZ:
3611 android_relocs_size_ = d->d_un.d_val;
3612 break;
3613
3614 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003615 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003616 return false;
3617
3618 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003619 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003620 return false;
3621
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003622 // "Indicates that all RELATIVE relocations have been concatenated together,
3623 // and specifies the RELATIVE relocation count."
3624 //
3625 // TODO: Spec also mentions that this can be used to optimize relocation process;
3626 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003627 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003628 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003629
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003630 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003631 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003632 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003633
3634 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003635 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003636 return false;
3637
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003638#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003639 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003640 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003641 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003642 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003643
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003644 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003645 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003646 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003647 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003648
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003649 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003650 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003651 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003652 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003653
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003654 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003655 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003656 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003657
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003658 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003659 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003660 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003661 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003662
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003663 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003664 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003665 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003666
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003667 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003668 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003669 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003670 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003671
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003672 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003673 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003674 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003675
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003676 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003677#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003678 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003679 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003680#else
3681 has_text_relocations = true;
3682 break;
3683#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003684
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003685 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003686 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003687 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003688
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003689 case DT_NEEDED:
3690 ++needed_count;
3691 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003692
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003693 case DT_FLAGS:
3694 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003695#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003696 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003697 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003698#else
3699 has_text_relocations = true;
3700#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003701 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003702 if (d->d_un.d_val & DF_SYMBOLIC) {
3703 has_DT_SYMBOLIC = true;
3704 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003705 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003706
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003707 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003708 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003709
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003710 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -07003711 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 -07003712 }
3713 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003714#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003715 case DT_MIPS_RLD_MAP:
3716 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3717 {
3718 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3719 *dp = &_r_debug;
3720 }
3721 break;
Raghu Gandham68815722014-12-18 19:12:19 -08003722 case DT_MIPS_RLD_MAP2:
3723 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
3724 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003725 r_debug** dp = reinterpret_cast<r_debug**>(
3726 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003727 *dp = &_r_debug;
3728 }
3729 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003730
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003731 case DT_MIPS_RLD_VERSION:
3732 case DT_MIPS_FLAGS:
3733 case DT_MIPS_BASE_ADDRESS:
3734 case DT_MIPS_UNREFEXTNO:
3735 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003736
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003737 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003738 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003739 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003740
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003741 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003742 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003743 break;
3744
3745 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003746 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003747 break;
3748#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003749 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3750 case DT_BIND_NOW:
3751 break;
3752
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003753 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003754 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3755 break;
3756
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003757 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003758 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3759 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003760 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003761 verdef_cnt_ = d->d_un.d_val;
3762 break;
3763
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003764 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003765 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3766 break;
3767
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003768 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003769 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003770 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003771
Evgenii Stepanov68650822015-06-10 13:38:39 -07003772 case DT_RUNPATH:
3773 // this is parsed after we have strtab initialized (see below).
3774 break;
3775
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003776 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003777 if (!relocating_linker) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003778 DL_WARN("%s: unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003779 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3780 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003781 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003782 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003783 }
3784
Duane Sandbc425c72015-06-01 16:29:14 -07003785#if defined(__mips__) && !defined(__LP64__)
3786 if (!mips_check_and_adjust_fp_modes()) {
3787 return false;
3788 }
3789#endif
3790
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003791 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003792 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003793
3794 // Sanity checks.
3795 if (relocating_linker && needed_count != 0) {
3796 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3797 return false;
3798 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003799 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003800 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003801 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003802 return false;
3803 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003804 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003805 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003806 return false;
3807 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003808 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003809 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003810 return false;
3811 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003812
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003813 // second pass - parse entries relying on strtab
3814 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003815 switch (d->d_tag) {
3816 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003817 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003818 break;
3819 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003820 set_dt_runpath(get_string(d->d_un.d_val));
3821 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003822 }
3823 }
3824
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003825 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003826 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003827 // because they can't find dt_needed library by soname.
3828 // This workaround should keep them working. (applies only
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003829 // for apps targeting sdk version <=22). Make an exception for
3830 // the main executable and linker; they do not need to have dt_soname
3831 if (soname_ == nullptr && this != somain && (flags_ & FLAG_LINKER) == 0 &&
3832 get_application_target_sdk_version() <= 22) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003833 soname_ = basename(realpath_.c_str());
3834 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3835 get_realpath(), soname_);
Dimitry Ivanov19930d52016-03-14 22:14:23 -07003836 // Don't call add_dlwarning because a missing DT_SONAME isn't important enough to show in the UI
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003837 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003838 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003839}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003840
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003841bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3842 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003843
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003844 local_group_root_ = local_group.front();
3845 if (local_group_root_ == nullptr) {
3846 local_group_root_ = this;
3847 }
3848
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003849 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3850 target_sdk_version_ = get_application_target_sdk_version();
3851 }
3852
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003853 VersionTracker version_tracker;
3854
3855 if (!version_tracker.init(this)) {
3856 return false;
3857 }
3858
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003859#if !defined(__LP64__)
3860 if (has_text_relocations) {
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003861 // Fail if app is targeting sdk version > 22
Dmitriy Ivanov80687862015-10-09 13:58:46 -07003862 if (get_application_target_sdk_version() > 22) {
Dmitriy Ivanovfae39d22015-10-13 11:07:56 -07003863 PRINT("%s: has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003864 DL_ERR("%s: has text relocations", get_realpath());
3865 return false;
3866 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003867 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003868 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003869 DL_WARN("%s has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003870 "security hardening. Please fix.", get_realpath());
Dimitry Ivanovdf91dc22016-02-25 15:22:04 -08003871 add_dlwarning(get_realpath(), "text relocations");
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003872 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3873 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003874 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003875 return false;
3876 }
3877 }
3878#endif
3879
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003880 if (android_relocs_ != nullptr) {
3881 // check signature
3882 if (android_relocs_size_ > 3 &&
3883 android_relocs_[0] == 'A' &&
3884 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003885 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003886 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003887 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003888
3889 bool relocated = false;
3890 const uint8_t* packed_relocs = android_relocs_ + 4;
3891 const size_t packed_relocs_size = android_relocs_size_ - 4;
3892
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003893 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003894 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003895 packed_reloc_iterator<sleb128_decoder>(
3896 sleb128_decoder(packed_relocs, packed_relocs_size)),
3897 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003898
3899 if (!relocated) {
3900 return false;
3901 }
3902 } else {
3903 DL_ERR("bad android relocation header.");
3904 return false;
3905 }
3906 }
3907
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003908#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003909 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003910 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003911 if (!relocate(version_tracker,
3912 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003913 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003914 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003915 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003916 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003917 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003918 if (!relocate(version_tracker,
3919 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003920 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003921 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003922 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003923#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003924 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003925 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003926 if (!relocate(version_tracker,
3927 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003928 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003929 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003930 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003931 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003932 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003933 if (!relocate(version_tracker,
3934 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003935 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003936 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003937 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003938#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003939
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003940#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003941 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003942 return false;
3943 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003944#endif
3945
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003946 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003947
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003948#if !defined(__LP64__)
3949 if (has_text_relocations) {
3950 // All relocations are done, we can protect our segments back to read-only.
3951 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3952 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003953 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003954 return false;
3955 }
3956 }
3957#endif
3958
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003959 /* We can also turn on GNU RELRO protection */
3960 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3961 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003962 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003963 return false;
3964 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003965
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003966 /* Handle serializing/sharing the RELRO segment */
3967 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3968 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3969 extinfo->relro_fd) < 0) {
3970 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003971 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003972 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003973 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003974 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3975 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3976 extinfo->relro_fd) < 0) {
3977 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003978 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003979 return false;
3980 }
3981 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003982
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003983 notify_gdb_of_load(this);
3984 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003985}
3986
Nick Kralevich468319c2011-11-11 15:53:17 -08003987/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003988 * This function add vdso to internal dso list.
3989 * It helps to stack unwinding through signal handlers.
3990 * Also, it makes bionic more like glibc.
3991 */
Kito Cheng812fd422014-03-25 22:53:56 +08003992static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003993#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08003994 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003995 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08003996 return;
3997 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003998
Dmitriy Ivanovd9b08a02015-11-16 13:17:27 -08003999 soinfo* si = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04004000
Elliott Hughes0266ae52014-02-10 17:46:57 -08004001 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
4002 si->phnum = ehdr_vdso->e_phnum;
4003 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
4004 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004005 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04004006
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004007 si->prelink_image();
4008 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04004009#endif
4010}
4011
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004012/* gdb expects the linker to be in the debug shared object list.
4013 * Without this, gdb has trouble locating the linker's ".text"
4014 * and ".plt" sections. Gdb could also potentially use this to
4015 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
Dimitry Ivanovfefb4d32016-02-17 14:13:06 -08004016 * Note that the linker shouldn't be on the soinfo list.
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004017 */
4018static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dimitry Ivanov05b60b22016-02-16 13:43:35 -08004019 static link_map linker_link_map_for_gdb;
4020#if defined(__LP64__)
4021 static char kLinkerPath[] = "/system/bin/linker64";
4022#else
4023 static char kLinkerPath[] = "/system/bin/linker";
4024#endif
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07004025
Dimitry Ivanov05b60b22016-02-16 13:43:35 -08004026 linker_link_map_for_gdb.l_addr = linker_base;
4027 linker_link_map_for_gdb.l_name = kLinkerPath;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004028
4029 /*
4030 * Set the dynamic field in the link map otherwise gdb will complain with
4031 * the following:
4032 * warning: .dynamic section for "/system/bin/linker" is not at the
4033 * expected address (wrong library or version mismatch?)
4034 */
4035 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
4036 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
4037 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Dimitry Ivanov05b60b22016-02-16 13:43:35 -08004038 &linker_link_map_for_gdb.l_ld, nullptr);
4039
4040 insert_link_map_into_debug_map(&linker_link_map_for_gdb);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004041}
4042
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004043static void init_default_namespace() {
4044 g_default_namespace.set_name("(default)");
4045 g_default_namespace.set_isolated(false);
4046
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004047 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
4048 somain->load_bias);
4049 const char* bname = basename(interp);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004050 if (bname && (strcmp(bname, "linker_asan") == 0 || strcmp(bname, "linker_asan64") == 0)) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004051 g_default_ld_paths = kAsanDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004052 } else {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004053 g_default_ld_paths = kDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004054 }
4055
4056 std::vector<std::string> ld_default_paths;
4057 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
4058 ld_default_paths.push_back(g_default_ld_paths[i]);
4059 }
4060
4061 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004062};
4063
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07004064extern "C" int __system_properties_init(void);
4065
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004066/*
Nick Kralevich468319c2011-11-11 15:53:17 -08004067 * This code is called after the linker has linked itself and
4068 * fixed it's own GOT. It is safe to make references to externs
4069 * and other non-local data at this point.
4070 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004071static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04004072#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004073 struct timeval t0, t1;
4074 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04004075#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004076
Elliott Hughes1801db32015-06-08 18:04:00 -07004077 // Sanitize the environment.
4078 __libc_init_AT_SECURE(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01004079
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07004080 // Initialize system properties
4081 __system_properties_init(); // may use 'environ'
4082
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004083 debuggerd_init();
4084
4085 // Get a few environment variables.
Elliott Hughes1801db32015-06-08 18:04:00 -07004086 const char* LD_DEBUG = getenv("LD_DEBUG");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004087 if (LD_DEBUG != nullptr) {
4088 g_ld_debug_verbosity = atoi(LD_DEBUG);
4089 }
4090
Elliott Hughes116b5692016-01-04 17:45:36 -08004091#if defined(__LP64__)
4092 INFO("[ Android dynamic linker (64-bit) ]");
4093#else
4094 INFO("[ Android dynamic linker (32-bit) ]");
4095#endif
4096
Elliott Hughes1801db32015-06-08 18:04:00 -07004097 // These should have been sanitized by __libc_init_AT_SECURE, but the test
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004098 // doesn't cost us anything.
4099 const char* ldpath_env = nullptr;
4100 const char* ldpreload_env = nullptr;
Elliott Hughes1801db32015-06-08 18:04:00 -07004101 if (!getauxval(AT_SECURE)) {
4102 ldpath_env = getenv("LD_LIBRARY_PATH");
Elliott Hughes116b5692016-01-04 17:45:36 -08004103 if (ldpath_env != nullptr) {
4104 INFO("[ LD_LIBRARY_PATH set to '%s' ]", ldpath_env);
4105 }
Elliott Hughes1801db32015-06-08 18:04:00 -07004106 ldpreload_env = getenv("LD_PRELOAD");
Elliott Hughes116b5692016-01-04 17:45:36 -08004107 if (ldpreload_env != nullptr) {
4108 INFO("[ LD_PRELOAD set to '%s' ]", ldpreload_env);
4109 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004110 }
4111
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004112 soinfo* si = soinfo_alloc(&g_default_namespace, args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004113 if (si == nullptr) {
4114 exit(EXIT_FAILURE);
4115 }
4116
4117 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004118 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004119 link_map* map = &(si->link_map_head);
4120
4121 map->l_addr = 0;
4122 map->l_name = args.argv[0];
4123 map->l_prev = nullptr;
4124 map->l_next = nullptr;
4125
4126 _r_debug.r_map = map;
4127 r_debug_tail = map;
4128
4129 init_linker_info_for_gdb(linker_base);
4130
4131 // Extract information passed from the kernel.
4132 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
4133 si->phnum = args.getauxval(AT_PHNUM);
4134 si->entry = args.getauxval(AT_ENTRY);
4135
4136 /* Compute the value of si->base. We can't rely on the fact that
4137 * the first entry is the PHDR because this will not be true
4138 * for certain executables (e.g. some in the NDK unit test suite)
4139 */
4140 si->base = 0;
4141 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
4142 si->load_bias = 0;
4143 for (size_t i = 0; i < si->phnum; ++i) {
4144 if (si->phdr[i].p_type == PT_PHDR) {
4145 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
4146 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
4147 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07004148 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004149 }
4150 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07004151
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004152 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
4153 if (elf_hdr->e_type != ET_DYN) {
4154 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
4155 exit(EXIT_FAILURE);
4156 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004157
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004158 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
4159 parse_LD_LIBRARY_PATH(ldpath_env);
4160 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01004161
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004162 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004163
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004164 init_default_namespace();
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004165
Dmitriy Ivanov67181252015-01-07 15:48:25 -08004166 if (!si->prelink_image()) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004167 __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer());
Dmitriy Ivanov67181252015-01-07 15:48:25 -08004168 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004169
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07004170 // add somain to global group
4171 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
4172
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004173 // Load ld_preloads and dependencies.
4174 StringLinkedList needed_library_name_list;
4175 size_t needed_libraries_count = 0;
4176 size_t ld_preloads_count = 0;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07004177
4178 for (const auto& ld_preload_name : g_ld_preload_names) {
4179 needed_library_name_list.push_back(ld_preload_name.c_str());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004180 ++needed_libraries_count;
Dmitriy Ivanovf8093a92015-04-28 18:09:53 -07004181 ++ld_preloads_count;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004182 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004183
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004184 for_each_dt_needed(si, [&](const char* name) {
4185 needed_library_name_list.push_back(name);
4186 ++needed_libraries_count;
4187 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004188
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004189 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004190
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004191 memset(needed_library_names, 0, sizeof(needed_library_names));
4192 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004193
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07004194 if (needed_libraries_count > 0 &&
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004195 !find_libraries(&g_default_namespace, si, needed_library_names, needed_libraries_count,
4196 nullptr, &g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07004197 /* add_as_children */ true)) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004198 __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004199 } else if (needed_libraries_count == 0) {
4200 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004201 __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004202 }
4203 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004204 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004205
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004206 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07004207
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08004208 {
4209 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06004210
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08004211 si->call_pre_init_constructors();
4212
4213 /* After the prelink_image, the si->load_bias is initialized.
4214 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
4215 * We need to update this value for so exe here. So Unwind_Backtrace
4216 * for some arch like x86 could work correctly within so exe.
4217 */
4218 map->l_addr = si->load_bias;
4219 si->call_constructors();
4220 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04004221
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004222#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004223 gettimeofday(&t1, nullptr);
4224 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
4225 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
4226 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004227#endif
4228#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004229 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
4230 linker_stats.count[kRelocAbsolute],
4231 linker_stats.count[kRelocRelative],
4232 linker_stats.count[kRelocCopy],
4233 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004234#endif
4235#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004236 {
4237 unsigned n;
4238 unsigned i;
4239 unsigned count = 0;
4240 for (n = 0; n < 4096; n++) {
4241 if (bitmask[n]) {
4242 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004243#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004244 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004245#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004246 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004247#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004248 if (x & 1) {
4249 count++;
4250 }
4251 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004252 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004253 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004254 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004255 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
4256 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004257#endif
4258
4259#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004260 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004261#endif
4262
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004263 TRACE("[ Ready to execute '%s' @ %p ]", si->get_realpath(), reinterpret_cast<void*>(si->entry));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004264 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004265}
Nick Kralevich468319c2011-11-11 15:53:17 -08004266
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004267/* Compute the load-bias of an existing executable. This shall only
4268 * be used to compute the load bias of an executable or shared library
4269 * that was loaded by the kernel itself.
4270 *
4271 * Input:
4272 * elf -> address of ELF header, assumed to be at the start of the file.
4273 * Return:
4274 * load bias, i.e. add the value of any p_vaddr in the file to get
4275 * the corresponding address in memory.
4276 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004277static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
4278 ElfW(Addr) offset = elf->e_phoff;
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004279 const ElfW(Phdr)* phdr_table =
4280 reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004281 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004282
Elliott Hughes0266ae52014-02-10 17:46:57 -08004283 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08004284 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08004285 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004286 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08004287 }
4288 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004289}
4290
Elliott Hughes42d949f2016-01-06 19:51:43 -08004291extern "C" int __set_tls(void*);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004292extern "C" void _start();
4293
Nick Kralevich468319c2011-11-11 15:53:17 -08004294/*
4295 * This is the entry point for the linker, called from begin.S. This
4296 * method is responsible for fixing the linker's own relocations, and
4297 * then calling __linker_init_post_relocation().
4298 *
4299 * Because this method is called before the linker has fixed it's own
4300 * relocations, any attempt to reference an extern variable, extern
4301 * function, or other GOT reference will generate a segfault.
4302 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004303extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004304 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08004305
Elliott Hughes42d949f2016-01-06 19:51:43 -08004306 void* tls[BIONIC_TLS_SLOTS];
4307 __set_tls(tls);
4308
Elliott Hughes0266ae52014-02-10 17:46:57 -08004309 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004310 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004311 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08004312 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08004313
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004314 soinfo linker_so(nullptr, nullptr, nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08004315
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004316 // If the linker is not acting as PT_INTERP entry_point is equal to
4317 // _start. Which means that the linker is running as an executable and
4318 // already linked by PT_INTERP.
4319 //
4320 // This happens when user tries to run 'adb shell /system/bin/linker'
4321 // see also https://code.google.com/p/android/issues/detail?id=63174
4322 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004323 __libc_fatal("This is %s, the helper program for shared library executables.", args.argv[0]);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004324 }
4325
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004326 linker_so.base = linker_addr;
4327 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
4328 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07004329 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004330 linker_so.phdr = phdr;
4331 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004332 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07004333
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07004334 // This might not be obvious... The reasons why we pass g_empty_list
4335 // in place of local_group here are (1) we do not really need it, because
4336 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
4337 // itself without having to look into local_group and (2) allocators
4338 // are not yet initialized, and therefore we cannot use linked_list.push_*
4339 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004340 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004341 __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer());
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004342 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07004343
Elliott Hughesd2948632015-07-21 11:57:09 -07004344 __libc_init_main_thread(args);
Dmitriy Ivanov14241402014-08-26 14:16:52 -07004345
Josh Gao93c0f5e2015-10-06 11:08:13 -07004346 // Initialize the linker's static libc's globals
4347 __libc_init_globals(args);
4348
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004349 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004350 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07004351
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004352 // Initialize static variables. Note that in order to
4353 // get correct libdl_info we need to call constructors
4354 // before get_libdl_info().
4355 solist = get_libdl_info();
4356 sonext = get_libdl_info();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004357 g_default_namespace.soinfo_list().push_back(get_libdl_info());
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004358
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004359 // We have successfully fixed our own relocations. It's safe to run
4360 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07004361 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08004362 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004363
Elliott Hughes116b5692016-01-04 17:45:36 -08004364 INFO("[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address));
Elliott Hughes611f9562015-01-23 10:43:58 -08004365
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004366 // Return the address that the calling assembly stub should jump to.
4367 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08004368}