blob: 66955da15df86f31e89ce1bc61b4252c4662585e [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 <dlfcn.h>
31#include <errno.h>
32#include <fcntl.h>
Elliott Hughes0266ae52014-02-10 17:46:57 -080033#include <inttypes.h>
Elliott Hughes46882792012-08-03 16:49:39 -070034#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
Elliott Hughes46882792012-08-03 16:49:39 -070038#include <sys/mman.h>
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -080039#include <sys/param.h>
Elliott Hughes46882792012-08-03 16:49:39 -070040#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070042#include <new>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070043#include <string>
Dmitriy Ivanovb4827502015-09-28 16:38:31 -070044#include <unordered_map>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070045#include <vector>
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070046
Elliott Hughes46882792012-08-03 16:49:39 -070047// Private C library headers.
Elliott Hugheseb847bc2013-10-09 15:50:50 -070048#include "private/bionic_tls.h"
49#include "private/KernelArgumentBlock.h"
50#include "private/ScopedPthreadMutexLocker.h"
Dmitriy Ivanov14669a92014-09-05 16:42:53 -070051#include "private/ScopeGuard.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052
53#include "linker.h"
Dmitriy Ivanovc9ce70d2015-03-10 15:30:26 -070054#include "linker_block_allocator.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055#include "linker_debug.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 Ivanov284ae352015-12-08 10:47:13 -080096 void set_permitted_paths(std::vector<std::string>&& permitted_paths) {
97 permitted_paths_ = permitted_paths;
98 }
99
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700100 soinfo::soinfo_list_t& soinfo_list() { return soinfo_list_; }
101
102 // For isolated namespaces - checks if the file is on the search path;
103 // always returns true for not isolated namespace.
104 bool is_accessible(const std::string& path);
105
106 private:
107 const char* name_;
108 bool is_isolated_;
109 std::vector<std::string> ld_library_paths_;
110 std::vector<std::string> default_library_paths_;
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800111 std::vector<std::string> permitted_paths_;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700112 soinfo::soinfo_list_t soinfo_list_;
113
114 DISALLOW_COPY_AND_ASSIGN(android_namespace_t);
115};
116
117android_namespace_t g_default_namespace;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800118android_namespace_t* g_anonymous_namespace = &g_default_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700119
Elliott Hughes0266ae52014-02-10 17:46:57 -0800120static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700122static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
123static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200124
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700125static LinkerTypeAllocator<android_namespace_t> g_namespace_allocator;
126
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700127static soinfo* solist;
128static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700129static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130
Elliott Hughes1728b232014-05-14 10:02:03 -0700131static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700132#if defined(__LP64__)
Hung-ying Tyanf74b0412015-11-12 11:48:19 +0800133 "/odm/lib64",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700134 "/vendor/lib64",
135 "/system/lib64",
136#else
Hung-ying Tyanf74b0412015-11-12 11:48:19 +0800137 "/odm/lib",
Elliott Hughes124fae92012-10-31 14:20:03 -0700138 "/vendor/lib",
139 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700140#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700141 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700142};
David Bartleybc3a5c22009-06-02 18:27:28 -0700143
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700144static const char* const kAsanDefaultLdPaths[] = {
145#if defined(__LP64__)
Hung-ying Tyanf74b0412015-11-12 11:48:19 +0800146 "/data/odm/lib64",
147 "/odm/lib64",
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700148 "/data/vendor/lib64",
149 "/vendor/lib64",
150 "/data/lib64",
151 "/system/lib64",
152#else
Hung-ying Tyanf74b0412015-11-12 11:48:19 +0800153 "/data/odm/lib",
154 "/odm/lib",
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700155 "/data/vendor/lib",
156 "/vendor/lib",
157 "/data/lib",
158 "/system/lib",
159#endif
160 nullptr
161};
162
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700163static const ElfW(Versym) kVersymNotNeeded = 0;
164static const ElfW(Versym) kVersymGlobal = 1;
165
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700166static const char* const* g_default_ld_paths;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700167static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800168
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700169static std::vector<soinfo*> g_ld_preloads;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600170
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700171static bool g_public_namespace_initialized;
172static soinfo::soinfo_list_t g_public_namespace;
173
Elliott Hughes1728b232014-05-14 10:02:03 -0700174__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700176__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700177
Evgenii Stepanov68650822015-06-10 13:38:39 -0700178static std::string dirname(const char *path) {
179 const char* last_slash = strrchr(path, '/');
180 if (last_slash == path) return "/";
181 else if (last_slash == nullptr) return ".";
182 else
183 return std::string(path, last_slash - path);
184}
185
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700187struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700188 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700189};
190
191static linker_stats_t linker_stats;
192
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800193void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700194 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700195}
196#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800197void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700198}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800199#endif
200
201#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800202uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203#endif
204
Dima Zavin2e855792009-05-20 18:28:09 -0700205static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700206
Elliott Hughes650be4e2013-03-05 18:47:58 -0800207char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700208 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700209}
210
Elliott Hughes650be4e2013-03-05 18:47:58 -0800211size_t linker_get_error_buffer_size() {
212 return sizeof(__linker_dl_err_buf);
213}
214
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700215// This function is an empty stub where GDB locates a breakpoint to get notified
216// about linker activity.
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700217extern "C"
218void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800219
Elliott Hughes1728b232014-05-14 10:02:03 -0700220static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700221static r_debug _r_debug =
222 {1, nullptr, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
223
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800224static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800225
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800226static void insert_soinfo_into_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700227 // Copy the necessary fields into the debug structure.
228 link_map* map = &(info->link_map_head);
229 map->l_addr = info->load_bias;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700230 // link_map l_name field is not const.
231 map->l_name = const_cast<char*>(info->get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700232 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700234 // Stick the new library at the end of the list.
235 // gdb tends to care more about libc than it does
236 // about leaf libraries, and ordering it this way
237 // reduces the back-and-forth over the wire.
238 if (r_debug_tail) {
239 r_debug_tail->l_next = map;
240 map->l_prev = r_debug_tail;
241 map->l_next = 0;
242 } else {
243 _r_debug.r_map = map;
244 map->l_prev = 0;
245 map->l_next = 0;
246 }
247 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248}
249
Elliott Hughesbedfe382012-08-14 14:07:59 -0700250static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700251 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700252
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700253 if (r_debug_tail == map) {
254 r_debug_tail = map->l_prev;
255 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700256
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700257 if (map->l_prev) {
258 map->l_prev->l_next = map->l_next;
259 }
260 if (map->l_next) {
261 map->l_next->l_prev = map->l_prev;
262 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700263}
264
Elliott Hughesbedfe382012-08-14 14:07:59 -0700265static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800266 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700267 // GDB already knows about the main executable
268 return;
269 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800270
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700271 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800272
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700273 _r_debug.r_state = r_debug::RT_ADD;
274 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800275
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700276 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800277
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700278 _r_debug.r_state = r_debug::RT_CONSISTENT;
279 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700280}
281
Elliott Hughesbedfe382012-08-14 14:07:59 -0700282static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800283 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700284 // GDB already knows about the main executable
285 return;
286 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700287
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700288 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700289
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700290 _r_debug.r_state = r_debug::RT_DELETE;
291 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700292
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700293 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700294
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700295 _r_debug.r_state = r_debug::RT_CONSISTENT;
296 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800297}
298
Elliott Hughes18a206c2012-10-29 17:37:13 -0700299void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800300 _r_debug.r_state = r_debug::RT_ADD;
301 rtld_db_dlactivity();
302 _r_debug.r_state = r_debug::RT_CONSISTENT;
303 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800304}
305
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700306bool android_namespace_t::is_accessible(const std::string& file) {
307 if (!is_isolated_) {
308 return true;
309 }
310
311 for (const auto& dir : ld_library_paths_) {
312 if (file_is_in_dir(file, dir)) {
313 return true;
314 }
315 }
316
317 for (const auto& dir : default_library_paths_) {
318 if (file_is_in_dir(file, dir)) {
319 return true;
320 }
321 }
322
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800323 for (const auto& dir : permitted_paths_) {
324 if (file_is_under_dir(file, dir)) {
325 return true;
326 }
327 }
328
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700329 return false;
330}
331
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700332LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
333 return g_soinfo_links_allocator.alloc();
334}
335
336void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
337 g_soinfo_links_allocator.free(entry);
338}
339
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700340static soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
341 struct stat* file_stat, off64_t file_offset,
342 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700343 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200344 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700345 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200346 }
347
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700348 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
349 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700350
Magnus Malmbornba98d922012-09-12 13:00:55 +0200351 sonext->next = si;
352 sonext = si;
353
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700354 ns->soinfo_list().push_back(si);
355
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700356 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200357 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800358}
359
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800360static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700361 if (si == nullptr) {
362 return;
363 }
364
365 if (si->base != 0 && si->size != 0) {
366 munmap(reinterpret_cast<void*>(si->base), si->size);
367 }
368
369 soinfo *prev = nullptr, *trav;
370
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700371 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700372
373 for (trav = solist; trav != nullptr; trav = trav->next) {
374 if (trav == si) {
375 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700376 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700377 prev = trav;
378 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800379
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700380 if (trav == nullptr) {
381 // si was not in solist
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700382 DL_ERR("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700383 return;
384 }
Elliott Hughes46882792012-08-03 16:49:39 -0700385
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700386 // clear links to/from si
387 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700388
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700389 // prev will never be null, because the first entry in solist is
390 // always the static libdl_info.
391 prev->next = si->next;
392 if (si == sonext) {
393 sonext = prev;
394 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800395
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700396 // remove from the namespace
397 si->get_namespace()->soinfo_list().remove_if([&](soinfo* candidate) {
398 return si == candidate;
399 });
400
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700401 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700402 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800403}
404
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700405// For every path element this function checks of it exists, and is a directory,
406// and normalizes it:
407// 1. For regular path it converts it to realpath()
408// 2. For path in a zip file it uses realpath on the zipfile
409// normalizes entry name by calling normalize_path function.
410static void resolve_paths(std::vector<std::string>& paths,
411 std::vector<std::string>* resolved_paths) {
412 resolved_paths->clear();
413 for (const auto& path : paths) {
414 char resolved_path[PATH_MAX];
415 const char* original_path = path.c_str();
416 if (realpath(original_path, resolved_path) != nullptr) {
417 struct stat s;
418 if (stat(resolved_path, &s) == 0) {
419 if (S_ISDIR(s.st_mode)) {
420 resolved_paths->push_back(resolved_path);
421 } else {
422 DL_WARN("Warning: \"%s\" is not a directory (excluding from path)", resolved_path);
423 continue;
424 }
425 } else {
426 DL_WARN("Warning: cannot stat file \"%s\": %s", resolved_path, strerror(errno));
427 continue;
428 }
429 } else {
430 std::string zip_path;
431 std::string entry_path;
432
433 std::string normalized_path;
434
435 if (!normalize_path(original_path, &normalized_path)) {
436 DL_WARN("Warning: unable to normalize \"%s\"", original_path);
437 continue;
438 }
439
440 if (parse_zip_path(normalized_path.c_str(), &zip_path, &entry_path)) {
441 if (realpath(zip_path.c_str(), resolved_path) == nullptr) {
442 DL_WARN("Warning: unable to resolve \"%s\": %s", zip_path.c_str(), strerror(errno));
443 continue;
444 }
445
446 ZipArchiveHandle handle = nullptr;
447 if (OpenArchive(resolved_path, &handle) != 0) {
448 DL_WARN("Warning: unable to open zip archive: %s", resolved_path);
449 continue;
450 }
451
452 // Check if zip-file has a dir with entry_path name
453 void* cookie = nullptr;
454 std::string prefix_str = entry_path + "/";
455 ZipString prefix(prefix_str.c_str());
456
457 ZipEntry out_data;
458 ZipString out_name;
459
460 int32_t error_code;
461
462 if ((error_code = StartIteration(handle, &cookie, &prefix, nullptr)) != 0) {
463 DL_WARN("Unable to iterate over zip-archive entries \"%s\";"
464 " error code: %d", zip_path.c_str(), error_code);
465 continue;
466 }
467
468 if (Next(cookie, &out_data, &out_name) != 0) {
469 DL_WARN("Unable to find entries starting with \"%s\" in \"%s\"",
470 prefix_str.c_str(), zip_path.c_str());
471 continue;
472 }
473
474 auto zip_guard = make_scope_guard([&]() {
475 if (cookie != nullptr) {
476 EndIteration(cookie);
477 }
478 CloseArchive(handle);
479 });
480
481 resolved_paths->push_back(std::string(resolved_path) + kZipFileSeparator + entry_path);
482 }
483 }
484 }
485}
486
487static void split_path(const char* path, const char* delimiters,
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700488 std::vector<std::string>* paths) {
Dmitriy Ivanovfbfba642015-11-16 14:23:37 -0800489 if (path != nullptr && path[0] != 0) {
tony.ys_liub4474402015-07-29 18:00:22 +0800490 *paths = android::base::Split(path, delimiters);
Elliott Hughescade4c32012-12-20 14:42:14 -0800491 }
492}
493
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700494static void parse_path(const char* path, const char* delimiters,
495 std::vector<std::string>* resolved_paths) {
496 std::vector<std::string> paths;
497 split_path(path, delimiters, &paths);
498 resolve_paths(paths, resolved_paths);
499}
500
Elliott Hughescade4c32012-12-20 14:42:14 -0800501static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700502 std::vector<std::string> ld_libary_paths;
503 parse_path(path, ":", &ld_libary_paths);
504 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800505}
506
Evgenii Stepanov68650822015-06-10 13:38:39 -0700507void soinfo::set_dt_runpath(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700508 if (!has_min_version(3)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700509 return;
510 }
511
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700512 std::vector<std::string> runpaths;
513
514 split_path(path, ":", &runpaths);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700515
516 std::string origin = dirname(get_realpath());
517 // FIXME: add $LIB and $PLATFORM.
518 std::pair<std::string, std::string> substs[] = {{"ORIGIN", origin}};
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700519 for (auto&& s : runpaths) {
Evgenii Stepanov68650822015-06-10 13:38:39 -0700520 size_t pos = 0;
521 while (pos < s.size()) {
522 pos = s.find("$", pos);
523 if (pos == std::string::npos) break;
524 for (const auto& subst : substs) {
525 const std::string& token = subst.first;
526 const std::string& replacement = subst.second;
527 if (s.substr(pos + 1, token.size()) == token) {
528 s.replace(pos, token.size() + 1, replacement);
529 // -1 to compensate for the ++pos below.
530 pos += replacement.size() - 1;
531 break;
532 } else if (s.substr(pos + 1, token.size() + 2) == "{" + token + "}") {
533 s.replace(pos, token.size() + 3, replacement);
534 pos += replacement.size() - 1;
535 break;
536 }
537 }
538 // Skip $ in case it did not match any of the known substitutions.
539 ++pos;
540 }
541 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700542
543 resolve_paths(runpaths, &dt_runpath_);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700544}
545
Elliott Hughescade4c32012-12-20 14:42:14 -0800546static void parse_LD_PRELOAD(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700547 g_ld_preload_names.clear();
548 if (path != nullptr) {
549 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
550 g_ld_preload_names = android::base::Split(path, " :");
551 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800552}
553
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700554static bool realpath_fd(int fd, std::string* realpath) {
555 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700556 __libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700557 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -0700558 PRINT("readlink('%s') failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700559 return false;
560 }
561
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700562 *realpath = &buf[0];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700563 return true;
564}
565
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700566#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700567
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700568// For a given PC, find the .so that it belongs to.
569// Returns the base address of the .ARM.exidx section
570// for that .so, and the number of 8-byte entries
571// in that section (via *pcount).
572//
573// Intended to be called by libc's __gnu_Unwind_Find_exidx().
574//
575// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800576_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800577 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800578
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700579 for (soinfo* si = solist; si != 0; si = si->next) {
580 if ((addr >= si->base) && (addr < (si->base + si->size))) {
581 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800582 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800583 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700584 }
585 *pcount = 0;
586 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800587}
Elliott Hughes46882792012-08-03 16:49:39 -0700588
Christopher Ferris24053a42013-08-19 17:45:09 -0700589#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700590
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700591// Here, we only have to provide a callback to iterate across all the
592// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700593int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700594 int rv = 0;
595 for (soinfo* si = solist; si != nullptr; si = si->next) {
596 dl_phdr_info dl_info;
597 dl_info.dlpi_addr = si->link_map_head.l_addr;
598 dl_info.dlpi_name = si->link_map_head.l_name;
599 dl_info.dlpi_phdr = si->phdr;
600 dl_info.dlpi_phnum = si->phnum;
601 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
602 if (rv != 0) {
603 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800604 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700605 }
606 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800607}
Elliott Hughes46882792012-08-03 16:49:39 -0700608
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700609const ElfW(Versym)* soinfo::get_versym(size_t n) const {
610 if (has_min_version(2) && versym_ != nullptr) {
611 return versym_ + n;
612 }
613
614 return nullptr;
615}
616
617ElfW(Addr) soinfo::get_verneed_ptr() const {
618 if (has_min_version(2)) {
619 return verneed_ptr_;
620 }
621
622 return 0;
623}
624
625size_t soinfo::get_verneed_cnt() const {
626 if (has_min_version(2)) {
627 return verneed_cnt_;
628 }
629
630 return 0;
631}
632
633ElfW(Addr) soinfo::get_verdef_ptr() const {
634 if (has_min_version(2)) {
635 return verdef_ptr_;
636 }
637
638 return 0;
639}
640
641size_t soinfo::get_verdef_cnt() const {
642 if (has_min_version(2)) {
643 return verdef_cnt_;
644 }
645
646 return 0;
647}
648
649template<typename F>
650static bool for_each_verdef(const soinfo* si, F functor) {
651 if (!si->has_min_version(2)) {
652 return true;
653 }
654
655 uintptr_t verdef_ptr = si->get_verdef_ptr();
656 if (verdef_ptr == 0) {
657 return true;
658 }
659
660 size_t offset = 0;
661
662 size_t verdef_cnt = si->get_verdef_cnt();
663 for (size_t i = 0; i<verdef_cnt; ++i) {
664 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
665 size_t verdaux_offset = offset + verdef->vd_aux;
666 offset += verdef->vd_next;
667
668 if (verdef->vd_version != 1) {
Dmitriy Ivanov3d7bea12015-04-20 17:40:39 -0700669 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700670 i, verdef->vd_version, si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700671 return false;
672 }
673
674 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
675 // "this is the version of the file itself. It must not be used for
676 // matching a symbol. It can be used to match references."
677 //
678 // http://www.akkadia.org/drepper/symbol-versioning
679 continue;
680 }
681
682 if (verdef->vd_cnt == 0) {
683 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
684 return false;
685 }
686
687 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
688
689 if (functor(i, verdef, verdaux) == true) {
690 break;
691 }
692 }
693
694 return true;
695}
696
697bool soinfo::find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const {
698 if (vi == nullptr) {
699 *versym = kVersymNotNeeded;
700 return true;
701 }
702
703 *versym = kVersymGlobal;
704
705 return for_each_verdef(this,
706 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
707 if (verdef->vd_hash == vi->elf_hash &&
708 strcmp(vi->name, get_string(verdaux->vda_name)) == 0) {
709 *versym = verdef->vd_ndx;
710 return true;
711 }
712
713 return false;
714 }
715 );
716}
717
718bool soinfo::find_symbol_by_name(SymbolName& symbol_name,
719 const version_info* vi,
720 const ElfW(Sym)** symbol) const {
721 uint32_t symbol_index;
722 bool success =
723 is_gnu_hash() ?
724 gnu_lookup(symbol_name, vi, &symbol_index) :
725 elf_lookup(symbol_name, vi, &symbol_index);
726
727 if (success) {
728 *symbol = symbol_index == 0 ? nullptr : symtab_ + symbol_index;
729 }
730
731 return success;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800732}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800733
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800734static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
735 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
736 ELF_ST_BIND(s->st_info) == STB_WEAK) {
737 return s->st_shndx != SHN_UNDEF;
738 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
739 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700740 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800741 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800742
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800743 return false;
744}
745
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700746static const ElfW(Versym) kVersymHiddenBit = 0x8000;
747
748static inline bool is_versym_hidden(const ElfW(Versym)* versym) {
749 // the symbol is hidden if bit 15 of versym is set.
750 return versym != nullptr && (*versym & kVersymHiddenBit) != 0;
751}
752
753static inline bool check_symbol_version(const ElfW(Versym) verneed,
754 const ElfW(Versym)* verdef) {
755 return verneed == kVersymNotNeeded ||
756 verdef == nullptr ||
757 verneed == (*verdef & ~kVersymHiddenBit);
758}
759
760bool soinfo::gnu_lookup(SymbolName& symbol_name,
761 const version_info* vi,
762 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800763 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800764 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800765
766 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800767 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
768 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800769
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700770 *symbol_index = 0;
771
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700772 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700773 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700774
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800775 // test against bloom filter
776 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700777 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700778 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700779
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700780 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800781 }
782
783 // bloom test says "probably yes"...
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700784 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800785
786 if (n == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700787 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700788 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700789
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700790 return true;
791 }
792
793 // lookup versym for the version definition in this library
794 // note the difference between "version is not requested" (vi == nullptr)
795 // and "version not found". In the first case verneed is kVersymNotNeeded
796 // which implies that the default version can be accepted; the second case results in
797 // verneed = 1 (kVersymGlobal) and implies that we should ignore versioned symbols
798 // for this library and consider only *global* ones.
799 ElfW(Versym) verneed = 0;
800 if (!find_verdef_version_index(vi, &verneed)) {
801 return false;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800802 }
803
804 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800805 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700806 const ElfW(Versym)* verdef = get_versym(n);
807 // skip hidden versions when verneed == kVersymNotNeeded (0)
808 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
809 continue;
810 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700811 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700812 check_symbol_version(verneed, verdef) &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800813 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
814 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700815 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700816 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(s->st_value),
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700817 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700818 *symbol_index = n;
819 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800820 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700821 } while ((gnu_chain_[n++] & 1) == 0);
822
823 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700824 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800825
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700826 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800827}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800828
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700829bool soinfo::elf_lookup(SymbolName& symbol_name,
830 const version_info* vi,
831 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800832 uint32_t hash = symbol_name.elf_hash();
833
834 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700835 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700836 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800837
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700838 ElfW(Versym) verneed = 0;
839 if (!find_verdef_version_index(vi, &verneed)) {
840 return false;
841 }
842
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800843 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
844 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700845 const ElfW(Versym)* verdef = get_versym(n);
846
847 // skip hidden versions when verneed == 0
848 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
849 continue;
850 }
851
852 if (check_symbol_version(verneed, verdef) &&
853 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700854 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800855 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700856 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700857 reinterpret_cast<void*>(s->st_value),
858 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700859 *symbol_index = n;
860 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800861 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800862 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800863
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700864 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700865 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700866 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700867
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700868 *symbol_index = 0;
869 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800870}
871
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700872soinfo::soinfo(android_namespace_t* ns, const char* realpath,
873 const struct stat* file_stat, off64_t file_offset,
874 int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700875 memset(this, 0, sizeof(*this));
876
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700877 if (realpath != nullptr) {
878 realpath_ = realpath;
879 }
880
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800881 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800882 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700883
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700884 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800885 this->st_dev_ = file_stat->st_dev;
886 this->st_ino_ = file_stat->st_ino;
887 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700888 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700889
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800890 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700891 this->namespace_ = ns;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700892}
893
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800894
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800895uint32_t SymbolName::elf_hash() {
896 if (!has_elf_hash_) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700897 const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800898 uint32_t h = 0, g;
899
900 while (*name) {
901 h = (h << 4) + *name++;
902 g = h & 0xf0000000;
903 h ^= g;
904 h ^= g >> 24;
905 }
906
907 elf_hash_ = h;
908 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700909 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800910
911 return elf_hash_;
912}
913
914uint32_t SymbolName::gnu_hash() {
915 if (!has_gnu_hash_) {
916 uint32_t h = 5381;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700917 const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800918 while (*name != 0) {
919 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
920 }
921
922 gnu_hash_ = h;
923 has_gnu_hash_ = true;
924 }
925
926 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800927}
928
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700929bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
930 soinfo** si_found_in, const soinfo::soinfo_list_t& global_group,
931 const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800932 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700933 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700934
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700935 /* "This element's presence in a shared object library alters the dynamic linker's
936 * symbol resolution algorithm for references within the library. Instead of starting
937 * a symbol search with the executable file, the dynamic linker starts from the shared
938 * object itself. If the shared object fails to supply the referenced symbol, the
939 * dynamic linker then searches the executable file and other shared objects as usual."
940 *
941 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
942 *
943 * Note that this is unlikely since static linker avoids generating
944 * relocations for -Bsymbolic linked dynamic executables.
945 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700946 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700947 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700948 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
949 return false;
950 }
951
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700952 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700953 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700954 }
955 }
956
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700957 // 1. Look for it in global_group
958 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700959 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700960 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700961 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700962 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700963 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
964 error = true;
965 return false;
966 }
967
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700968 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700969 *si_found_in = global_si;
970 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700971 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700972
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700973 return true;
974 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700975
976 if (error) {
977 return false;
978 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700979 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700980
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700981 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700982 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700983 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700984 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700985 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700986 // we already did this - skip
987 return true;
988 }
989
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700990 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700991 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700992 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
993 error = true;
994 return false;
995 }
996
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700997 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700998 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700999 return false;
1000 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001001
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001002 return true;
1003 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001004
1005 if (error) {
1006 return false;
1007 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001008 }
1009
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001010 if (s != nullptr) {
1011 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
1012 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001013 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
1014 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001015 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001016 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001017
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001018 *symbol = s;
1019 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001020}
1021
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001022class ProtectedDataGuard {
1023 public:
1024 ProtectedDataGuard() {
1025 if (ref_count_++ == 0) {
1026 protect_data(PROT_READ | PROT_WRITE);
1027 }
1028 }
1029
1030 ~ProtectedDataGuard() {
1031 if (ref_count_ == 0) { // overflow
1032 __libc_fatal("Too many nested calls to dlopen()");
1033 }
1034
1035 if (--ref_count_ == 0) {
1036 protect_data(PROT_READ);
1037 }
1038 }
1039 private:
1040 void protect_data(int protection) {
1041 g_soinfo_allocator.protect_all(protection);
1042 g_soinfo_links_allocator.protect_all(protection);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001043 g_namespace_allocator.protect_all(protection);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001044 }
1045
1046 static size_t ref_count_;
1047};
1048
1049size_t ProtectedDataGuard::ref_count_ = 0;
1050
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001051// Each size has it's own allocator.
1052template<size_t size>
1053class SizeBasedAllocator {
1054 public:
1055 static void* alloc() {
1056 return allocator_.alloc();
1057 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001058
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001059 static void free(void* ptr) {
1060 allocator_.free(ptr);
1061 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001062
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001063 private:
1064 static LinkerBlockAllocator allocator_;
1065};
1066
1067template<size_t size>
1068LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
1069
1070template<typename T>
1071class TypeBasedAllocator {
1072 public:
1073 static T* alloc() {
1074 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
1075 }
1076
1077 static void free(T* ptr) {
1078 SizeBasedAllocator<sizeof(T)>::free(ptr);
1079 }
1080};
1081
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001082class LoadTask {
1083 public:
1084 struct deleter_t {
1085 void operator()(LoadTask* t) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001086 t->~LoadTask();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001087 TypeBasedAllocator<LoadTask>::free(t);
1088 }
1089 };
1090
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001091 static deleter_t deleter;
1092
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001093 static LoadTask* create(const char* name, soinfo* needed_by,
1094 std::unordered_map<const soinfo*, ElfReader>* readers_map) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001095 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001096 return new (ptr) LoadTask(name, needed_by, readers_map);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001097 }
1098
1099 const char* get_name() const {
1100 return name_;
1101 }
1102
1103 soinfo* get_needed_by() const {
1104 return needed_by_;
1105 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001106
1107 soinfo* get_soinfo() const {
1108 return si_;
1109 }
1110
1111 void set_soinfo(soinfo* si) {
1112 si_ = si;
1113 }
1114
1115 off64_t get_file_offset() const {
1116 return file_offset_;
1117 }
1118
1119 void set_file_offset(off64_t offset) {
1120 file_offset_ = offset;
1121 }
1122
1123 int get_fd() const {
1124 return fd_;
1125 }
1126
1127 void set_fd(int fd, bool assume_ownership) {
1128 fd_ = fd;
1129 close_fd_ = assume_ownership;
1130 }
1131
1132 const android_dlextinfo* get_extinfo() const {
1133 return extinfo_;
1134 }
1135
1136 void set_extinfo(const android_dlextinfo* extinfo) {
1137 extinfo_ = extinfo;
1138 }
1139
1140 const ElfReader& get_elf_reader() const {
1141 CHECK(si_ != nullptr);
1142 return (*elf_readers_map_)[si_];
1143 }
1144
1145 ElfReader& get_elf_reader() {
1146 CHECK(si_ != nullptr);
1147 return (*elf_readers_map_)[si_];
1148 }
1149
1150 std::unordered_map<const soinfo*, ElfReader>* get_readers_map() {
1151 return elf_readers_map_;
1152 }
1153
1154 bool read(const char* realpath, off64_t file_size) {
1155 ElfReader& elf_reader = get_elf_reader();
1156 return elf_reader.Read(realpath, fd_, file_offset_, file_size);
1157 }
1158
1159 bool load() {
1160 ElfReader& elf_reader = get_elf_reader();
1161 if (!elf_reader.Load(extinfo_)) {
1162 return false;
1163 }
1164
1165 si_->base = elf_reader.load_start();
1166 si_->size = elf_reader.load_size();
1167 si_->load_bias = elf_reader.load_bias();
1168 si_->phnum = elf_reader.phdr_count();
1169 si_->phdr = elf_reader.loaded_phdr();
1170
1171 return true;
1172 }
1173
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001174 private:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001175 LoadTask(const char* name, soinfo* needed_by,
1176 std::unordered_map<const soinfo*, ElfReader>* readers_map)
1177 : name_(name), needed_by_(needed_by), si_(nullptr),
1178 fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map) {}
1179
1180 ~LoadTask() {
1181 if (fd_ != -1 && close_fd_) {
1182 close(fd_);
1183 }
1184 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001185
1186 const char* name_;
1187 soinfo* needed_by_;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001188 soinfo* si_;
1189 const android_dlextinfo* extinfo_;
1190 int fd_;
1191 bool close_fd_;
1192 off64_t file_offset_;
1193 std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001194
1195 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
1196};
1197
Ningsheng Jiane93be992014-09-16 15:22:10 +08001198LoadTask::deleter_t LoadTask::deleter;
1199
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001200template <typename T>
1201using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
1202
1203typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001204typedef linked_list_t<const char> StringLinkedList;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001205typedef std::vector<LoadTask*> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001206
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001207
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001208// This function walks down the tree of soinfo dependencies
1209// in breadth-first order and
1210// * calls action(soinfo* si) for each node, and
1211// * terminates walk if action returns false.
1212//
1213// walk_dependencies_tree returns false if walk was terminated
1214// by the action and true otherwise.
1215template<typename F>
1216static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001217 SoinfoLinkedList visit_list;
1218 SoinfoLinkedList visited;
1219
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001220 for (size_t i = 0; i < root_soinfos_size; ++i) {
1221 visit_list.push_back(root_soinfos[i]);
1222 }
1223
1224 soinfo* si;
1225 while ((si = visit_list.pop_front()) != nullptr) {
1226 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -07001227 continue;
1228 }
1229
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001230 if (!action(si)) {
1231 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001232 }
1233
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001234 visited.push_back(si);
1235
1236 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001237 visit_list.push_back(child);
1238 });
1239 }
1240
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001241 return true;
1242}
1243
1244
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001245static const ElfW(Sym)* dlsym_handle_lookup(soinfo* root, soinfo* skip_until,
1246 soinfo** found, SymbolName& symbol_name) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001247 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001248 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001249
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001250 walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) {
1251 if (skip_lookup) {
1252 skip_lookup = current_soinfo != skip_until;
1253 return true;
1254 }
1255
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001256 if (!current_soinfo->find_symbol_by_name(symbol_name, nullptr, &result)) {
1257 result = nullptr;
1258 return false;
1259 }
1260
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001261 if (result != nullptr) {
1262 *found = current_soinfo;
1263 return false;
1264 }
1265
1266 return true;
1267 });
1268
1269 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001270}
1271
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001272// This is used by dlsym(3). It performs symbol lookup only within the
1273// specified soinfo object and its dependencies in breadth first order.
1274const ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -07001275 // According to man dlopen(3) and posix docs in the case when si is handle
1276 // of the main executable we need to search not only in the executable and its
1277 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
1278 //
1279 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
1280 // libraries and they are loaded in breath-first (correct) order we can just execute
1281 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
1282 if (si == somain) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001283 return dlsym_linear_lookup(&g_default_namespace, name, found, nullptr, RTLD_DEFAULT);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -07001284 }
1285
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001286 SymbolName symbol_name(name);
1287 return dlsym_handle_lookup(si, nullptr, found, symbol_name);
1288}
1289
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001290/* This is used by dlsym(3) to performs a global symbol lookup. If the
1291 start value is null (for RTLD_DEFAULT), the search starts at the
1292 beginning of the global solist. Otherwise the search starts at the
1293 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001294 */
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001295const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
1296 const char* name,
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001297 soinfo** found,
1298 soinfo* caller,
1299 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001300 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001301
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001302 soinfo::soinfo_list_t& soinfo_list = ns->soinfo_list();
1303 soinfo::soinfo_list_t::iterator start = soinfo_list.begin();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001304
1305 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -07001306 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001307 return nullptr;
1308 } else {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001309 soinfo::soinfo_list_t::iterator it = soinfo_list.find(caller);
1310 CHECK (it != soinfo_list.end());
1311 start = ++it;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001312 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001313 }
1314
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001315 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001316 for (soinfo::soinfo_list_t::iterator it = start, end = soinfo_list.end(); it != end; ++it) {
1317 soinfo* si = *it;
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001318 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
1319 // if the library is opened by application with target api level <= 22
1320 // See http://b/21565766
1321 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 && si->get_target_sdk_version() > 22) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001322 continue;
1323 }
1324
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001325 if (!si->find_symbol_by_name(symbol_name, nullptr, &s)) {
1326 return nullptr;
1327 }
1328
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001329 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -08001330 *found = si;
1331 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -06001332 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001333 }
Matt Fischer1698d9e2009-12-31 12:17:56 -06001334
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001335 // If not found - use dlsym_handle_lookup for caller's
1336 // local_group unless it is part of the global group in which
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001337 // case we already did it.
1338 if (s == nullptr && caller != nullptr &&
1339 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001340 return dlsym_handle_lookup(caller->get_local_group_root(),
1341 (handle == RTLD_NEXT) ? caller : nullptr, found, symbol_name);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001342 }
1343
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001344 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001345 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
1346 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -08001347 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001348
Elliott Hughescade4c32012-12-20 14:42:14 -08001349 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001350}
1351
Kito Chengfa8c05d2013-03-12 14:58:06 +08001352soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001353 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001354 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001355 if (address >= si->base && address - si->base < si->size) {
1356 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001357 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001358 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001359 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001360}
1361
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001362ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
1363 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
1364}
1365
1366static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
1367 return sym->st_shndx != SHN_UNDEF &&
1368 soaddr >= sym->st_value &&
1369 soaddr < sym->st_value + sym->st_size;
1370}
1371
1372ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001373 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001374
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001375 for (size_t i = 0; i < gnu_nbucket_; ++i) {
1376 uint32_t n = gnu_bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001377
1378 if (n == 0) {
1379 continue;
1380 }
1381
1382 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001383 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001384 if (symbol_matches_soaddr(sym, soaddr)) {
1385 return sym;
1386 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001387 } while ((gnu_chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001388 }
1389
1390 return nullptr;
1391}
1392
1393ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001394 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001395
Kito Chengfa8c05d2013-03-12 14:58:06 +08001396 // Search the library's symbol table for any defined symbol which
1397 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001398 for (size_t i = 0; i < nchain_; ++i) {
1399 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001400 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001401 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001402 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001403 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001404
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001405 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001406}
1407
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001408class ZipArchiveCache {
1409 public:
1410 ZipArchiveCache() {}
1411 ~ZipArchiveCache();
1412
1413 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
1414 private:
1415 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
1416
1417 std::unordered_map<std::string, ZipArchiveHandle> cache_;
1418};
1419
1420bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
1421 std::string key(zip_path);
1422
1423 auto it = cache_.find(key);
1424 if (it != cache_.end()) {
1425 *handle = it->second;
1426 return true;
1427 }
1428
1429 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1430 if (fd == -1) {
1431 return false;
1432 }
1433
1434 if (OpenArchiveFd(fd, "", handle) != 0) {
1435 // invalid zip-file (?)
1436 close(fd);
1437 return false;
1438 }
1439
1440 cache_[key] = *handle;
1441 return true;
1442}
1443
1444ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -07001445 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001446 CloseArchive(it.second);
1447 }
1448}
1449
1450static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001451 const char* const input_path,
1452 off64_t* file_offset, std::string* realpath) {
1453 std::string normalized_path;
1454 if (!normalize_path(input_path, &normalized_path)) {
1455 return -1;
1456 }
1457
1458 const char* const path = normalized_path.c_str();
1459 TRACE("Trying zip file open from path '%s' -> normalized '%s'", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +00001460
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001461 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +00001462 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001463 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +00001464 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001465 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +00001466 if (separator == nullptr) {
1467 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -07001468 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001469
1470 char buf[512];
1471 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
1472 PRINT("Warning: ignoring very long library path: %s", path);
1473 return -1;
1474 }
1475
1476 buf[separator - path] = '\0';
1477
1478 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001479 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +00001480 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1481 if (fd == -1) {
1482 return -1;
1483 }
1484
1485 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001486 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001487 // invalid zip-file (?)
1488 close(fd);
1489 return -1;
1490 }
1491
Simon Baldwinaef71952015-01-16 13:22:54 +00001492 ZipEntry entry;
1493
Yusuke Sato56f40fb2015-06-25 14:56:07 -07001494 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001495 // Entry was not found.
1496 close(fd);
1497 return -1;
1498 }
1499
1500 // Check if it is properly stored
1501 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
1502 close(fd);
1503 return -1;
1504 }
1505
1506 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001507
1508 if (realpath_fd(fd, realpath)) {
1509 *realpath += separator;
1510 } else {
1511 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1512 normalized_path.c_str());
1513 *realpath = normalized_path;
1514 }
1515
Simon Baldwinaef71952015-01-16 13:22:54 +00001516 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001517}
1518
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001519static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
1520 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
1521 if (n < 0 || n >= static_cast<int>(buf_size)) {
1522 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1523 return false;
1524 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001525
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001526 return true;
1527}
1528
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001529static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1530 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001531 const std::vector<std::string>& paths,
1532 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001533 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001534 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001535 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001536 continue;
1537 }
1538
1539 int fd = -1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001540 if (strstr(buf, kZipFileSeparator) != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001541 fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath);
Simon Baldwinaef71952015-01-16 13:22:54 +00001542 }
1543
1544 if (fd == -1) {
1545 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1546 if (fd != -1) {
1547 *file_offset = 0;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001548 if (!realpath_fd(fd, realpath)) {
1549 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
1550 *realpath = buf;
1551 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001552 }
1553 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001554
1555 if (fd != -1) {
1556 return fd;
1557 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001558 }
1559
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001560 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001561}
1562
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001563static int open_library(android_namespace_t* ns,
1564 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001565 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001566 off64_t* file_offset, std::string* realpath) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001567 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001568
Elliott Hughes124fae92012-10-31 14:20:03 -07001569 // 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 -07001570 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001571 int fd = -1;
1572
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001573 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001574 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1575 }
1576
1577 if (fd == -1) {
1578 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001579 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001580 *file_offset = 0;
1581 if (!realpath_fd(fd, realpath)) {
1582 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name);
1583 *realpath = name;
1584 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001585 }
1586 }
1587
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001588 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001589 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001590
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001591 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1592 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 -07001593 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001594 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001595 // Check if the library is accessible
1596 if (fd != -1 && !ns->is_accessible(*realpath)) {
1597 fd = -1;
1598 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001599 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001600
Elliott Hughes124fae92012-10-31 14:20:03 -07001601 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001602 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001603 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001604
Elliott Hughes124fae92012-10-31 14:20:03 -07001605 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001606}
1607
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001608static const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
1609#if !defined(__LP64__)
1610 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001611 if (get_application_target_sdk_version() <= 22) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001612 const char* bname = basename(dt_needed);
1613 if (bname != dt_needed) {
1614 DL_WARN("'%s' library has invalid DT_NEEDED entry '%s'", sopath, dt_needed);
1615 }
1616
1617 return bname;
1618 }
1619#endif
1620 return dt_needed;
1621}
1622
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001623template<typename F>
1624static void for_each_dt_needed(const soinfo* si, F action) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001625 for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001626 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001627 action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
Dima Zavin2e855792009-05-20 18:28:09 -07001628 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001629 }
1630}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001631
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001632template<typename F>
1633static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
1634 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1635 if (d->d_tag == DT_NEEDED) {
1636 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1637 }
1638 }
1639}
1640
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001641static bool load_library(android_namespace_t* ns,
1642 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001643 LoadTaskList* load_tasks,
1644 int rtld_flags,
1645 const std::string& realpath) {
1646 off64_t file_offset = task->get_file_offset();
1647 const char* name = task->get_name();
1648 const android_dlextinfo* extinfo = task->get_extinfo();
1649
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001650 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001651 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001652 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001653 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001654 if (file_offset < 0) {
1655 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001656 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001657 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001658
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001659 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001660 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001661 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001662 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001663 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001664 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001665 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1666 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001667 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001668 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001669
1670 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001671 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1672 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001673 auto predicate = [&](soinfo* si) {
1674 return si->get_st_dev() != 0 &&
1675 si->get_st_ino() != 0 &&
1676 si->get_st_dev() == file_stat.st_dev &&
1677 si->get_st_ino() == file_stat.st_ino &&
1678 si->get_file_offset() == file_offset;
1679 };
1680
1681 soinfo* si = ns->soinfo_list().find_if(predicate);
1682
1683 // check public namespace
1684 if (si == nullptr) {
1685 si = g_public_namespace.find_if(predicate);
1686 if (si != nullptr) {
1687 ns->soinfo_list().push_back(si);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001688 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001689 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001690
1691 if (si != nullptr) {
1692 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
1693 "will return existing soinfo", name, si->get_realpath());
1694 task->set_soinfo(si);
1695 return true;
1696 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001697 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001698
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001699 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001700 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001701 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001702 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001703
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001704 if (!ns->is_accessible(realpath)) {
1705 // do not load libraries if they are not accessible for the specified namespace.
1706 DL_ERR("library \"%s\" is not accessible for the namespace \"%s\"",
1707 name, ns->get_name());
1708 return false;
1709 }
1710
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001711 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001712 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001713 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001714 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001715
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001716 task->set_soinfo(si);
1717
1718 // Read the ELF header and some of the segments.
1719 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001720 soinfo_free(si);
1721 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001722 return false;
1723 }
1724
1725 // find and set DT_RUNPATH and dt_soname
1726 // Note that these field values are temporary and are
1727 // going to be overwritten on soinfo::prelink_image
1728 // with values from PT_LOAD segments.
1729 const ElfReader& elf_reader = task->get_elf_reader();
1730 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1731 if (d->d_tag == DT_RUNPATH) {
1732 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1733 }
1734 if (d->d_tag == DT_SONAME) {
1735 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1736 }
1737 }
1738
1739 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
1740 load_tasks->push_back(LoadTask::create(name, si, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001741 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001742
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001743 return true;
1744
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001745}
1746
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001747static bool load_library(android_namespace_t* ns,
1748 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001749 ZipArchiveCache* zip_archive_cache,
1750 LoadTaskList* load_tasks,
1751 int rtld_flags) {
1752 const char* name = task->get_name();
1753 soinfo* needed_by = task->get_needed_by();
1754 const android_dlextinfo* extinfo = task->get_extinfo();
1755
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001756 off64_t file_offset;
1757 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001758 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001759 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001760 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1761 file_offset = extinfo->library_fd_offset;
1762 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001763
1764 if (!realpath_fd(extinfo->library_fd, &realpath)) {
1765 PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1766 "Will use given name.", name);
1767 realpath = name;
1768 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001769
1770 task->set_fd(extinfo->library_fd, false);
1771 task->set_file_offset(file_offset);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001772 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001773 }
1774
1775 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001776 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001777 if (fd == -1) {
1778 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001779 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001780 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001781
1782 task->set_fd(fd, true);
1783 task->set_file_offset(file_offset);
1784
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001785 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001786}
1787
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001788// Returns true if library was found and false in 2 cases
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001789// 1. (for default namespace only) The library was found but loaded under different
1790// target_sdk_version (*candidate != nullptr)
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001791// 2. The library was not found by soname (*candidate is nullptr)
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001792static bool find_loaded_library_by_soname(android_namespace_t* ns,
1793 const char* name, soinfo** candidate) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001794 *candidate = nullptr;
1795
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001796 // Ignore filename with path.
1797 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001798 return false;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001799 }
1800
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001801 uint32_t target_sdk_version = get_application_target_sdk_version();
1802
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001803 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001804 const char* soname = si->get_soname();
1805 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001806 // If the library was opened under different target sdk version
1807 // skip this step and try to reopen it. The exceptions are
1808 // "libdl.so" and global group. There is no point in skipping
1809 // them because relocation process is going to use them
1810 // in any case.
1811 bool is_libdl = si == solist;
1812 if (is_libdl || (si->get_dt_flags_1() & DF_1_GLOBAL) != 0 ||
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001813 !si->is_linked() || si->get_target_sdk_version() == target_sdk_version ||
1814 ns != &g_default_namespace) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001815 *candidate = si;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001816 return false;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001817 } else if (*candidate == nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001818 // for the different sdk version in the default namespace
1819 // remember the first library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001820 *candidate = si;
1821 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001822 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001823
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001824 return true;
1825 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001826}
1827
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001828static bool find_library_internal(android_namespace_t* ns,
1829 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001830 ZipArchiveCache* zip_archive_cache,
1831 LoadTaskList* load_tasks,
1832 int rtld_flags) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001833 soinfo* candidate;
1834
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001835 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001836 task->set_soinfo(candidate);
1837 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001838 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001839
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001840 if (ns != &g_default_namespace) {
1841 // check public namespace
1842 candidate = g_public_namespace.find_if([&](soinfo* si) {
1843 return strcmp(task->get_name(), si->get_soname()) == 0;
1844 });
1845
1846 if (candidate != nullptr) {
1847 ns->soinfo_list().push_back(candidate);
1848 task->set_soinfo(candidate);
1849 return true;
1850 }
1851 }
1852
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001853 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001854 // of this fact is done by load_library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001855 TRACE("[ '%s' find_loaded_library_by_soname returned false (*candidate=%s@%p). Trying harder...]",
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001856 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001857
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001858 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001859 return true;
1860 } else {
1861 // In case we were unable to load the library but there
1862 // is a candidate loaded under the same soname but different
1863 // sdk level - return it anyways.
1864 if (candidate != nullptr) {
1865 task->set_soinfo(candidate);
1866 return true;
1867 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001868 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001869
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001870 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001871}
1872
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001873static void soinfo_unload(soinfo* si);
1874
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001875// TODO: this is slightly unusual way to construct
1876// the global group for relocation. Not every RTLD_GLOBAL
1877// library is included in this group for backwards-compatibility
1878// reasons.
1879//
1880// This group consists of the main executable, LD_PRELOADs
1881// and libraries with the DF_1_GLOBAL flag set.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001882static soinfo::soinfo_list_t make_global_group(android_namespace_t* ns) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001883 soinfo::soinfo_list_t global_group;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001884 ns->soinfo_list().for_each([&](soinfo* si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001885 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1886 global_group.push_back(si);
1887 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001888 });
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001889
1890 return global_group;
1891}
1892
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001893static void shuffle(std::vector<LoadTask*>* v) {
1894 for (size_t i = 0, size = v->size(); i < size; ++i) {
1895 size_t n = size - i;
1896 size_t r = arc4random_uniform(n);
1897 std::swap((*v)[n-1], (*v)[r]);
1898 }
1899}
1900
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001901// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1902// not their transitive dependencies) as children of the start_with library.
1903// This is false when find_libraries is called for dlopen(), when newly loaded
1904// libraries must form a disjoint tree.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001905static bool find_libraries(android_namespace_t* ns,
1906 soinfo* start_with,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001907 const char* const library_names[],
1908 size_t library_names_count, soinfo* soinfos[],
1909 std::vector<soinfo*>* ld_preloads,
1910 size_t ld_preloads_count, int rtld_flags,
1911 const android_dlextinfo* extinfo,
1912 bool add_as_children) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001913 // Step 0: prepare.
1914 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001915 std::unordered_map<const soinfo*, ElfReader> readers_map;
1916
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001917 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001918 const char* name = library_names[i];
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001919 load_tasks.push_back(LoadTask::create(name, start_with, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001920 }
1921
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001922 // Construct global_group.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001923 soinfo::soinfo_list_t global_group = make_global_group(ns);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001924
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001925 // If soinfos array is null allocate one on stack.
1926 // The array is needed in case of failure; for example
1927 // when library_names[] = {libone.so, libtwo.so} and libone.so
1928 // is loaded correctly but libtwo.so failed for some reason.
1929 // In this case libone.so should be unloaded on return.
1930 // See also implementation of failure_guard below.
1931
1932 if (soinfos == nullptr) {
1933 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1934 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1935 memset(soinfos, 0, soinfos_size);
1936 }
1937
1938 // list of libraries to link - see step 2.
1939 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001940
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001941 auto scope_guard = make_scope_guard([&]() {
1942 for (LoadTask* t : load_tasks) {
1943 LoadTask::deleter(t);
1944 }
1945 });
1946
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001947 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001948 // Housekeeping
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001949 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001950 soinfo_unload(soinfos[i]);
1951 }
1952 });
1953
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001954 ZipArchiveCache zip_archive_cache;
1955
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001956 // Step 1: expand the list of load_tasks to include
1957 // all DT_NEEDED libraries (do not load them just yet)
1958 for (size_t i = 0; i<load_tasks.size(); ++i) {
1959 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07001960 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001961
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001962 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001963 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001964
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001965 if(!find_library_internal(ns, task, &zip_archive_cache, &load_tasks, rtld_flags)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001966 return false;
1967 }
1968
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001969 soinfo* si = task->get_soinfo();
1970
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001971 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001972 needed_by->add_child(si);
1973 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001974
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001975 if (si->is_linked()) {
1976 si->increment_ref_count();
1977 }
1978
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001979 // When ld_preloads is not null, the first
1980 // ld_preloads_count libs are in fact ld_preloads.
1981 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001982 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001983 }
1984
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001985 if (soinfos_count < library_names_count) {
1986 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001987 }
1988 }
1989
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001990 // Step 2: Load libraries in random order (see b/24047022)
1991 LoadTaskList load_list;
1992 for (auto&& task : load_tasks) {
1993 soinfo* si = task->get_soinfo();
1994 auto pred = [&](const LoadTask* t) {
1995 return t->get_soinfo() == si;
1996 };
1997
1998 if (!si->is_linked() &&
1999 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
2000 load_list.push_back(task);
2001 }
2002 }
2003 shuffle(&load_list);
2004
2005 for (auto&& task : load_list) {
2006 if (!task->load()) {
2007 return false;
2008 }
2009 }
2010
2011 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
2012 for (auto&& task : load_tasks) {
2013 soinfo* si = task->get_soinfo();
2014 if (!si->is_linked() && !si->prelink_image()) {
2015 return false;
2016 }
2017 }
2018
2019 // Step 4: Add LD_PRELOADed libraries to the global group for
2020 // future runs. There is no need to explicitly add them to
2021 // the global group for this run because they are going to
2022 // appear in the local group in the correct order.
2023 if (ld_preloads != nullptr) {
2024 for (auto&& si : *ld_preloads) {
2025 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2026 }
2027 }
2028
2029
2030 // Step 5: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002031 soinfo::soinfo_list_t local_group;
2032 walk_dependencies_tree(
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002033 (start_with != nullptr && add_as_children) ? &start_with : soinfos,
2034 (start_with != nullptr && add_as_children) ? 1 : soinfos_count,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002035 [&] (soinfo* si) {
2036 local_group.push_back(si);
2037 return true;
2038 });
2039
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002040 // We need to increment ref_count in case
2041 // the root of the local group was not linked.
2042 bool was_local_group_root_linked = local_group.front()->is_linked();
2043
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002044 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002045 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002046 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002047 return false;
2048 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002049 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002050 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002051
2052 return true;
2053 });
2054
2055 if (linked) {
2056 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002057 }
2058
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002059 if (!was_local_group_root_linked) {
2060 local_group.front()->increment_ref_count();
2061 }
2062
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002063 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002064}
2065
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002066static soinfo* find_library(android_namespace_t* ns,
2067 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002068 const android_dlextinfo* extinfo,
2069 soinfo* needed_by) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002070 soinfo* si;
2071
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002072 if (name == nullptr) {
2073 si = somain;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002074 } else if (!find_libraries(ns, needed_by, &name, 1, &si, nullptr, 0, rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002075 extinfo, /* add_as_children */ false)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002076 return nullptr;
2077 }
2078
Elliott Hughesd23736e2012-11-01 15:16:56 -07002079 return si;
2080}
Elliott Hughesbedfe382012-08-14 14:07:59 -07002081
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002082static void soinfo_unload(soinfo* root) {
2083 // Note that the library can be loaded but not linked;
2084 // in which case there is no root but we still need
2085 // to walk the tree and unload soinfos involved.
2086 //
2087 // This happens on unsuccessful dlopen, when one of
2088 // the DT_NEEDED libraries could not be linked/found.
2089 if (root->is_linked()) {
2090 root = root->get_local_group_root();
2091 }
2092
2093 if (!root->can_unload()) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002094 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002095 return;
2096 }
2097
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002098 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002099
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002100 if (ref_count == 0) {
2101 soinfo::soinfo_list_t local_unload_list;
2102 soinfo::soinfo_list_t external_unload_list;
2103 soinfo::soinfo_list_t depth_first_list;
2104 depth_first_list.push_back(root);
2105 soinfo* si = nullptr;
2106
2107 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002108 if (local_unload_list.contains(si)) {
2109 continue;
2110 }
2111
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002112 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002113
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002114 if (si->has_min_version(0)) {
2115 soinfo* child = nullptr;
2116 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002117 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
2118 child->get_realpath(), child);
2119
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002120 if (local_unload_list.contains(child)) {
2121 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002122 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002123 external_unload_list.push_back(child);
2124 } else {
2125 depth_first_list.push_front(child);
2126 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002127 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002128 } else {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07002129#if !defined(__work_around_b_24465209__)
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002130 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002131#else
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002132 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002133 for_each_dt_needed(si, [&] (const char* library_name) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002134 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002135 si->get_realpath(), library_name);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002136
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002137 soinfo* needed = find_library(si->get_namespace(),
2138 library_name, RTLD_NOLOAD, nullptr, nullptr);
2139
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002140 if (needed != nullptr) {
2141 // Not found: for example if symlink was deleted between dlopen and dlclose
2142 // Since we cannot really handle errors at this point - print and continue.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002143 PRINT("warning: couldn't find %s needed by %s on unload.",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002144 library_name, si->get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002145 return;
2146 } else if (local_unload_list.contains(needed)) {
2147 // already visited
2148 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002149 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002150 // external group
2151 external_unload_list.push_back(needed);
2152 } else {
2153 // local group
2154 depth_first_list.push_front(needed);
2155 }
2156 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002157#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002158 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002159 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002160
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002161 local_unload_list.for_each([](soinfo* si) {
2162 si->call_destructors();
2163 });
2164
2165 while ((si = local_unload_list.pop_front()) != nullptr) {
2166 notify_gdb_of_unload(si);
2167 soinfo_free(si);
2168 }
2169
2170 while ((si = external_unload_list.pop_front()) != nullptr) {
2171 soinfo_unload(si);
2172 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002173 } else {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002174 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
2175 root->get_realpath(), ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08002176 }
2177}
2178
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002179void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002180 // Use basic string manipulation calls to avoid snprintf.
2181 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
2182 // When debug malloc is enabled, this call returns 0. This in turn causes
2183 // snprintf to do nothing, which causes libraries to fail to load.
2184 // See b/17302493 for further details.
2185 // Once the above bug is fixed, this code can be modified to use
2186 // snprintf again.
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002187 size_t required_len = 0;
2188 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2189 required_len += strlen(g_default_ld_paths[i]) + 1;
2190 }
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002191 if (buffer_size < required_len) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002192 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
2193 "buffer len %zu, required len %zu", buffer_size, required_len);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002194 }
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002195 char* end = buffer;
2196 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2197 if (i > 0) *end++ = ':';
2198 end = stpcpy(end, g_default_ld_paths[i]);
2199 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002200}
2201
Elliott Hughescade4c32012-12-20 14:42:14 -08002202void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08002203 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08002204}
2205
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002206soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo, soinfo *caller) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002207 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08002208 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002209 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08002210 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002211
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002212 android_namespace_t* ns = caller != nullptr ? caller->get_namespace() : g_anonymous_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002213
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002214 if (extinfo != nullptr) {
2215 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
2216 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
2217 return nullptr;
2218 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002219
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002220 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07002221 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002222 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
2223 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002224 return nullptr;
2225 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002226
2227 if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 &&
2228 (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) {
2229 DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not "
2230 "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT");
2231 return nullptr;
2232 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002233
2234 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
2235 if (extinfo->library_namespace == nullptr) {
2236 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
2237 return nullptr;
2238 }
2239 ns = extinfo->library_namespace;
2240 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00002241 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002242
2243 ProtectedDataGuard guard;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002244 soinfo* si = find_library(ns, name, flags, extinfo, caller);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002245 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002246 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07002247 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002248
Elliott Hughesd23736e2012-11-01 15:16:56 -07002249 return si;
2250}
2251
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002252void do_dlclose(soinfo* si) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002253 ProtectedDataGuard guard;
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002254 soinfo_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002255}
2256
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002257bool init_namespaces(const char* public_ns_sonames, const char* anon_ns_library_path) {
2258 CHECK(public_ns_sonames != nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002259 if (g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002260 DL_ERR("public namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002261 return false;
2262 }
2263
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002264 std::vector<std::string> sonames = android::base::Split(public_ns_sonames, ":");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002265
2266 ProtectedDataGuard guard;
2267
2268 auto failure_guard = make_scope_guard([&]() {
2269 g_public_namespace.clear();
2270 });
2271
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002272 for (const auto& soname : sonames) {
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -08002273 soinfo* candidate = nullptr;
2274
2275 find_loaded_library_by_soname(&g_default_namespace, soname.c_str(), &candidate);
2276
2277 if (candidate == nullptr) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002278 DL_ERR("error initializing public namespace: \"%s\" was not found"
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002279 " in the default namespace", soname.c_str());
2280 return false;
2281 }
2282
2283 candidate->set_nodelete();
2284 g_public_namespace.push_back(candidate);
2285 }
2286
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002287 g_public_namespace_initialized = true;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002288
2289 // create anonymous namespace
2290 android_namespace_t* anon_ns =
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002291 create_namespace("(anonymous)", nullptr, anon_ns_library_path, false, nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002292
2293 if (anon_ns == nullptr) {
2294 g_public_namespace_initialized = false;
2295 return false;
2296 }
2297 g_anonymous_namespace = anon_ns;
2298 failure_guard.disable();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002299 return true;
2300}
2301
2302android_namespace_t* create_namespace(const char* name,
2303 const char* ld_library_path,
2304 const char* default_library_path,
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002305 bool is_isolated,
2306 const char* permitted_when_isolated_path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002307 if (!g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002308 DL_ERR("cannot create namespace: public namespace is not initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002309 return nullptr;
2310 }
2311
2312 ProtectedDataGuard guard;
2313 std::vector<std::string> ld_library_paths;
2314 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002315 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002316
2317 parse_path(ld_library_path, ":", &ld_library_paths);
2318 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002319 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002320
2321 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2322 ns->set_name(name);
2323 ns->set_isolated(is_isolated);
2324 ns->set_ld_library_paths(std::move(ld_library_paths));
2325 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002326 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002327
2328 // TODO(dimtiry): Should this be global group of caller's namespace?
2329 auto global_group = make_global_group(&g_default_namespace);
2330 std::copy(global_group.begin(), global_group.end(), std::back_inserter(ns->soinfo_list()));
2331
2332 return ns;
2333}
2334
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002335static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
2336 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2337 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2338 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002339 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2340 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002341
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002342 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002343}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002344
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002345const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2346 if (source_symver < 2 ||
2347 source_symver >= version_infos.size() ||
2348 version_infos[source_symver].name == nullptr) {
2349 return nullptr;
2350 }
2351
2352 return &version_infos[source_symver];
2353}
2354
2355void VersionTracker::add_version_info(size_t source_index,
2356 ElfW(Word) elf_hash,
2357 const char* ver_name,
2358 const soinfo* target_si) {
2359 if (source_index >= version_infos.size()) {
2360 version_infos.resize(source_index+1);
2361 }
2362
2363 version_infos[source_index].elf_hash = elf_hash;
2364 version_infos[source_index].name = ver_name;
2365 version_infos[source_index].target_si = target_si;
2366}
2367
2368bool VersionTracker::init_verneed(const soinfo* si_from) {
2369 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2370
2371 if (verneed_ptr == 0) {
2372 return true;
2373 }
2374
2375 size_t verneed_cnt = si_from->get_verneed_cnt();
2376
2377 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2378 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2379 size_t vernaux_offset = offset + verneed->vn_aux;
2380 offset += verneed->vn_next;
2381
2382 if (verneed->vn_version != 1) {
2383 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2384 return false;
2385 }
2386
2387 const char* target_soname = si_from->get_string(verneed->vn_file);
2388 // find it in dependencies
2389 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002390 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002391 });
2392
2393 if (target_si == nullptr) {
2394 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002395 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002396 return false;
2397 }
2398
2399 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2400 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2401 vernaux_offset += vernaux->vna_next;
2402
2403 const ElfW(Word) elf_hash = vernaux->vna_hash;
2404 const char* ver_name = si_from->get_string(vernaux->vna_name);
2405 ElfW(Half) source_index = vernaux->vna_other;
2406
2407 add_version_info(source_index, elf_hash, ver_name, target_si);
2408 }
2409 }
2410
2411 return true;
2412}
2413
2414bool VersionTracker::init_verdef(const soinfo* si_from) {
2415 return for_each_verdef(si_from,
2416 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2417 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2418 si_from->get_string(verdaux->vda_name), si_from);
2419 return false;
2420 }
2421 );
2422}
2423
2424bool VersionTracker::init(const soinfo* si_from) {
2425 if (!si_from->has_min_version(2)) {
2426 return true;
2427 }
2428
2429 return init_verneed(si_from) && init_verdef(si_from);
2430}
2431
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002432bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2433 const char* sym_name, const version_info** vi) {
2434 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2435 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2436
2437 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2438 *vi = version_tracker.get_version_info(sym_ver);
2439
2440 if (*vi == nullptr) {
2441 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002442 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002443 return false;
2444 }
2445 } else {
2446 // there is no version info
2447 *vi = nullptr;
2448 }
2449
2450 return true;
2451}
2452
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002453#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002454#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002455static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2456 return rela->r_addend;
2457}
2458#else
2459static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002460 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2461 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002462 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2463 }
2464 return 0;
2465}
2466#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002467
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002468template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002469bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2470 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002471 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2472 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002473 if (rel == nullptr) {
2474 return false;
2475 }
2476
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002477 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2478 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2479
2480 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002481 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002482 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002483 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002484
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002485 DEBUG("Processing '%s' relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002486 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002487 continue;
2488 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002489
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002490 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002491 soinfo* lsi = nullptr;
2492
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002493 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002494 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002495 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002496
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002497 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2498 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002499 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002500
2501 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2502 return false;
2503 }
2504
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002505 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002506 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002507 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002508 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002509 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002510 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002511 }
2512
2513 /* IHI0044C AAELF 4.5.1.1:
2514
2515 Libraries are not searched to resolve weak references.
2516 It is not an error for a weak reference to remain unsatisfied.
2517
2518 During linking, the value of an undefined weak reference is:
2519 - Zero if the relocation type is absolute
2520 - The address of the place if the relocation is pc-relative
2521 - The address of nominal base address if the relocation
2522 type is base-relative.
2523 */
2524
2525 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002526 case R_GENERIC_JUMP_SLOT:
2527 case R_GENERIC_GLOB_DAT:
2528 case R_GENERIC_RELATIVE:
2529 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002530#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002531 case R_AARCH64_ABS64:
2532 case R_AARCH64_ABS32:
2533 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002534#elif defined(__x86_64__)
2535 case R_X86_64_32:
2536 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002537#elif defined(__arm__)
2538 case R_ARM_ABS32:
2539#elif defined(__i386__)
2540 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002541#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002542 /*
2543 * The sym_addr was initialized to be zero above, or the relocation
2544 * code below does not care about value of sym_addr.
2545 * No need to do anything.
2546 */
2547 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002548#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002549 case R_X86_64_PC32:
2550 sym_addr = reloc;
2551 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002552#elif defined(__i386__)
2553 case R_386_PC32:
2554 sym_addr = reloc;
2555 break;
2556#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002557 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002558 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002559 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002560 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002561 } else { // We got a definition.
2562#if !defined(__LP64__)
2563 // When relocating dso with text_relocation .text segment is
2564 // not executable. We need to restore elf flags before resolving
2565 // STT_GNU_IFUNC symbol.
2566 bool protect_segments = has_text_relocations &&
2567 lsi == this &&
2568 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2569 if (protect_segments) {
2570 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2571 DL_ERR("can't protect segments for \"%s\": %s",
2572 get_realpath(), strerror(errno));
2573 return false;
2574 }
2575 }
2576#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002577 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002578#if !defined(__LP64__)
2579 if (protect_segments) {
2580 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2581 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2582 get_realpath(), strerror(errno));
2583 return false;
2584 }
2585 }
2586#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002587 }
2588 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002589 }
2590
2591 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002592 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002593 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002594 MARK(rel->r_offset);
2595 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2596 reinterpret_cast<void*>(reloc),
2597 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2598
2599 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002600 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002601 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002602 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002603 MARK(rel->r_offset);
2604 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2605 reinterpret_cast<void*>(reloc),
2606 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2607 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002608 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002609 case R_GENERIC_RELATIVE:
2610 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002611 MARK(rel->r_offset);
2612 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2613 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002614 reinterpret_cast<void*>(load_bias + addend));
2615 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002616 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002617 case R_GENERIC_IRELATIVE:
2618 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002619 MARK(rel->r_offset);
2620 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2621 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002622 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002623 {
2624#if !defined(__LP64__)
2625 // When relocating dso with text_relocation .text segment is
2626 // not executable. We need to restore elf flags for this
2627 // particular call.
2628 if (has_text_relocations) {
2629 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2630 DL_ERR("can't protect segments for \"%s\": %s",
2631 get_realpath(), strerror(errno));
2632 return false;
2633 }
2634 }
2635#endif
2636 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2637#if !defined(__LP64__)
2638 // Unprotect it afterwards...
2639 if (has_text_relocations) {
2640 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2641 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2642 get_realpath(), strerror(errno));
2643 return false;
2644 }
2645 }
2646#endif
2647 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2648 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002649 break;
2650
2651#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002652 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002653 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002654 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002655 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002656 reloc, sym_addr + addend, sym_name);
2657 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002658 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002659 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002660 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002661 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002662 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002663 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002664 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002665 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2666 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002667 if ((min_value <= (sym_addr + addend)) &&
2668 ((sym_addr + addend) <= max_value)) {
2669 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002670 } else {
2671 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002672 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002673 return false;
2674 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002675 }
2676 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002677 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002678 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002679 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002680 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002681 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002682 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002683 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2684 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002685 if ((min_value <= (sym_addr + addend)) &&
2686 ((sym_addr + addend) <= max_value)) {
2687 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002688 } else {
2689 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002690 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002691 return false;
2692 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002693 }
2694 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002695 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002696 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002697 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002698 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002699 reloc, sym_addr + addend, rel->r_offset, sym_name);
2700 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002701 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002702 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002703 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002704 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002705 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002706 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002707 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002708 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2709 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002710 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2711 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2712 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002713 } else {
2714 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002715 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002716 return false;
2717 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002718 }
2719 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002720 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002721 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002722 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002723 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002724 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002725 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002726 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2727 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002728 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2729 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2730 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002731 } else {
2732 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002733 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002734 return false;
2735 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002736 }
2737 break;
2738
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002739 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002740 /*
2741 * ET_EXEC is not supported so this should not happen.
2742 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002743 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002744 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002745 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002746 * R_AARCH64_COPY may only appear in executable objects where e_type is
2747 * set to ET_EXEC.
2748 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002749 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002750 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002751 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002752 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002753 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002754 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002755 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002756 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002757 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002758 break;
2759#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002760 case R_X86_64_32:
2761 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002762 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002763 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2764 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002765 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002766 break;
2767 case R_X86_64_64:
2768 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002769 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002770 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2771 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002772 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002773 break;
2774 case R_X86_64_PC32:
2775 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002776 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002777 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2778 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2779 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002780 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002781 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002782#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002783 case R_ARM_ABS32:
2784 count_relocation(kRelocAbsolute);
2785 MARK(rel->r_offset);
2786 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2787 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2788 break;
2789 case R_ARM_REL32:
2790 count_relocation(kRelocRelative);
2791 MARK(rel->r_offset);
2792 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2793 reloc, sym_addr, rel->r_offset, sym_name);
2794 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2795 break;
2796 case R_ARM_COPY:
2797 /*
2798 * ET_EXEC is not supported so this should not happen.
2799 *
2800 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2801 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002802 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002803 * R_ARM_COPY may only appear in executable objects where e_type is
2804 * set to ET_EXEC.
2805 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002806 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002807 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002808#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002809 case R_386_32:
2810 count_relocation(kRelocRelative);
2811 MARK(rel->r_offset);
2812 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2813 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2814 break;
2815 case R_386_PC32:
2816 count_relocation(kRelocRelative);
2817 MARK(rel->r_offset);
2818 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2819 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2820 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2821 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002822#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002823 default:
2824 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002825 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002826 }
2827 }
2828 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002829}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002830#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002831
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002832void soinfo::call_array(const char* array_name __unused, linker_function_t* functions,
2833 size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002834 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002835 return;
2836 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02002837
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002838 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, get_realpath());
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002839
2840 int begin = reverse ? (count - 1) : 0;
2841 int end = reverse ? -1 : count;
2842 int step = reverse ? -1 : 1;
2843
2844 for (int i = begin; i != end; i += step) {
2845 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002846 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002847 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02002848
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002849 TRACE("[ Done calling %s for '%s' ]", array_name, get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002850}
2851
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002852void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002853 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002854 return;
2855 }
2856
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002857 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Elliott Hughesd23736e2012-11-01 15:16:56 -07002858 function();
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002859 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04002860}
2861
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002862void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002863 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
2864 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002865 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002866}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002867
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002868void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002869 if (constructors_called) {
2870 return;
2871 }
Jesse Hallf5d16932012-01-30 15:39:57 -08002872
Elliott Hughesd23736e2012-11-01 15:16:56 -07002873 // We set constructors_called before actually calling the constructors, otherwise it doesn't
2874 // protect against recursive constructor calls. One simple example of constructor recursion
2875 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
2876 // 1. The program depends on libc, so libc's constructor is called here.
2877 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
2878 // 3. dlopen() calls the constructors on the newly created
2879 // soinfo for libc_malloc_debug_leak.so.
2880 // 4. The debug .so depends on libc, so CallConstructors is
2881 // called again with the libc soinfo. If it doesn't trigger the early-
2882 // out above, the libc constructor will be called again (recursively!).
2883 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002884
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002885 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002886 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07002887 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002888 get_realpath(), preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002889 }
2890
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002891 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002892 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002893 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002894
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002895 TRACE("\"%s\": calling constructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002896
2897 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002898 call_function("DT_INIT", init_func_);
2899 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002900}
David 'Digit' Turner82156792009-05-18 14:37:41 +02002901
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002902void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002903 if (!constructors_called) {
2904 return;
2905 }
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002906 TRACE("\"%s\": calling destructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002907
2908 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002909 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002910
2911 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002912 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002913
2914 // This is needed on second call to dlopen
2915 // after library has been unloaded with RTLD_NODELETE
2916 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002917}
2918
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002919void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002920 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002921 child->parents_.push_back(this);
2922 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002923 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002924}
2925
2926void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002927 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002928 return;
2929 }
2930
2931 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002932 children_.for_each([&] (soinfo* child) {
2933 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002934 return parent == this;
2935 });
2936 });
2937
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002938 parents_.for_each([&] (soinfo* parent) {
2939 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002940 return child == this;
2941 });
2942 });
2943
2944 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002945 parents_.clear();
2946 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002947}
2948
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002949dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002950 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002951 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002952 }
2953
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002954 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002955};
2956
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002957ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002958 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002959 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002960 }
2961
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002962 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002963}
2964
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002965off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002966 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002967 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002968 }
2969
2970 return 0;
2971}
2972
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002973uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07002974 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002975 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07002976 }
2977
2978 return 0;
2979}
2980
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002981uint32_t soinfo::get_dt_flags_1() const {
2982 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002983 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002984 }
2985
2986 return 0;
2987}
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002988
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002989void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
2990 if (has_min_version(1)) {
2991 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002992 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002993 }
2994
2995 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002996 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002997 }
2998
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002999 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003000 }
3001}
3002
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003003void soinfo::set_nodelete() {
3004 rtld_flags_ |= RTLD_NODELETE;
3005}
3006
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003007const char* soinfo::get_realpath() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003008#if defined(__work_around_b_24465209__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003009 if (has_min_version(2)) {
3010 return realpath_.c_str();
3011 } else {
3012 return old_name_;
3013 }
3014#else
3015 return realpath_.c_str();
3016#endif
3017}
3018
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003019void soinfo::set_soname(const char* soname) {
3020#if defined(__work_around_b_24465209__)
3021 if (has_min_version(2)) {
3022 soname_ = soname;
3023 }
3024 strlcpy(old_name_, soname_, sizeof(old_name_));
3025#else
3026 soname_ = soname;
3027#endif
3028}
3029
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003030const char* soinfo::get_soname() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003031#if defined(__work_around_b_24465209__)
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003032 if (has_min_version(2)) {
3033 return soname_;
3034 } else {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003035 return old_name_;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003036 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003037#else
3038 return soname_;
3039#endif
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003040}
3041
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003042// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003043// 'this->flags' does not have FLAG_NEW_SOINFO set.
3044static soinfo::soinfo_list_t g_empty_list;
3045
3046soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003047 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003048 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003049 }
3050
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003051 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003052}
3053
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003054const soinfo::soinfo_list_t& soinfo::get_children() const {
3055 if (has_min_version(0)) {
3056 return children_;
3057 }
3058
3059 return g_empty_list;
3060}
3061
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003062soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003063 if (has_min_version(0)) {
3064 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003065 }
3066
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003067 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003068}
3069
Evgenii Stepanov68650822015-06-10 13:38:39 -07003070static std::vector<std::string> g_empty_runpath;
3071
3072const std::vector<std::string>& soinfo::get_dt_runpath() const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003073 if (has_min_version(3)) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003074 return dt_runpath_;
3075 }
3076
3077 return g_empty_runpath;
3078}
3079
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003080android_namespace_t* soinfo::get_namespace() {
3081 if (has_min_version(3)) {
3082 return namespace_;
3083 }
3084
3085 return &g_default_namespace;
3086}
3087
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003088ElfW(Addr) soinfo::resolve_symbol_address(const ElfW(Sym)* s) const {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003089 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
3090 return call_ifunc_resolver(s->st_value + load_bias);
3091 }
3092
3093 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
3094}
3095
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003096const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003097 if (has_min_version(1) && (index >= strtab_size_)) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003098 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003099 get_realpath(), strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003100 }
3101
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003102 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003103}
3104
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003105bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003106 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003107}
3108
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003109bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003110 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003111}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003112
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003113bool soinfo::is_linked() const {
3114 return (flags_ & FLAG_LINKED) != 0;
3115}
3116
3117bool soinfo::is_main_executable() const {
3118 return (flags_ & FLAG_EXE) != 0;
3119}
3120
3121void soinfo::set_linked() {
3122 flags_ |= FLAG_LINKED;
3123}
3124
3125void soinfo::set_linker_flag() {
3126 flags_ |= FLAG_LINKER;
3127}
3128
3129void soinfo::set_main_executable() {
3130 flags_ |= FLAG_EXE;
3131}
3132
3133void soinfo::increment_ref_count() {
3134 local_group_root_->ref_count_++;
3135}
3136
3137size_t soinfo::decrement_ref_count() {
3138 return --local_group_root_->ref_count_;
3139}
3140
3141soinfo* soinfo::get_local_group_root() const {
3142 return local_group_root_;
3143}
3144
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003145// This function returns api-level at the time of
3146// dlopen/load. Note that libraries opened by system
3147// will always have 'current' api level.
3148uint32_t soinfo::get_target_sdk_version() const {
3149 if (!has_min_version(2)) {
3150 return __ANDROID_API__;
3151 }
3152
3153 return local_group_root_->target_sdk_version_;
3154}
3155
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003156bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08003157 /* Extract dynamic section */
3158 ElfW(Word) dynamic_flags = 0;
3159 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07003160
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003161 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003162 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003163 if (!relocating_linker) {
Dmitriy Ivanovf8093a92015-04-28 18:09:53 -07003164 INFO("[ linking %s ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003165 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003166 }
3167
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003168 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003169 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003170 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003171 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003172 return false;
3173 } else {
3174 if (!relocating_linker) {
3175 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003176 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003177 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003178
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003179#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003180 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
3181 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003182#endif
3183
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003184 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003185 // Note that: "Except for the DT_NULL element at the end of the array,
3186 // and the relative order of DT_NEEDED elements, entries may appear in any order."
3187 //
3188 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003189 uint32_t needed_count = 0;
3190 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
3191 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
3192 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3193 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003194 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003195 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003196 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003197
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003198 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003199 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
3200 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
3201 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
3202 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003203 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003204
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003205 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003206 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003207 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003208 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
3209 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003210
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003211 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003212 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003213 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003214 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
3215 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003216
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003217 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003218 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003219 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003220 return false;
3221 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003222 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003223
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003224 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003225 break;
3226
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003227 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003228 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003229 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003230
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003231 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003232 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003233 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003234
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003235 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003236 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003237 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003238
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003239 case DT_SYMENT:
3240 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003241 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
3242 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003243 return false;
3244 }
3245 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003246
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003247 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003248#if defined(USE_RELA)
3249 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003250 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003251 return false;
3252 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003253#else
3254 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003255 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003256 return false;
3257 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003258#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003259 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003260
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003261 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003262#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003263 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003264#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003265 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003266#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003267 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003268
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003269 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003270#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003271 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003272#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003273 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003274#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003275 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003276
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003277 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003278#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003279 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003280 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003281#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003282 // Ignore for other platforms... (because RTLD_LAZY is not supported)
3283 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003284
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003285 case DT_DEBUG:
3286 // Set the DT_DEBUG entry to the address of _r_debug for GDB
3287 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08003288// FIXME: not working currently for N64
3289// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003290// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08003291// read-only, but the DYNAMIC header claims it is writable.
3292#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003293 if ((dynamic_flags & PF_W) != 0) {
3294 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
3295 }
Chris Dearman99186652014-02-06 20:36:51 -08003296#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08003297 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003298#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003299 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003300 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003301 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003302
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003303 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003304 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003305 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003306
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003307 case DT_ANDROID_RELA:
3308 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3309 break;
3310
3311 case DT_ANDROID_RELASZ:
3312 android_relocs_size_ = d->d_un.d_val;
3313 break;
3314
3315 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003316 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003317 return false;
3318
3319 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003320 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003321 return false;
3322
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003323 case DT_RELAENT:
3324 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003325 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003326 return false;
3327 }
3328 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003329
3330 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003331 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003332 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003333
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003334 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003335 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003336 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003337
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003338 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003339 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003340 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003341
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003342#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003343 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003344 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003345 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003346
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003347 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003348 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003349 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003350
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003351 case DT_RELENT:
3352 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003353 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003354 return false;
3355 }
3356 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003357
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003358 case DT_ANDROID_REL:
3359 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3360 break;
3361
3362 case DT_ANDROID_RELSZ:
3363 android_relocs_size_ = d->d_un.d_val;
3364 break;
3365
3366 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003367 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003368 return false;
3369
3370 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003371 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003372 return false;
3373
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003374 // "Indicates that all RELATIVE relocations have been concatenated together,
3375 // and specifies the RELATIVE relocation count."
3376 //
3377 // TODO: Spec also mentions that this can be used to optimize relocation process;
3378 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003379 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003380 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003381
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003382 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003383 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003384 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003385
3386 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003387 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003388 return false;
3389
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003390#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003391 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003392 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003393 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003394 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003395
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003396 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003397 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003398 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003399 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003400
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003401 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003402 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003403 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003404 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003405
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003406 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003407 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003408 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003409
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003410 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003411 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003412 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003413 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003414
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003415 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003416 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003417 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003418
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003419 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003420 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003421 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003422 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003423
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003424 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003425 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003426 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003427
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003428 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003429#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003430 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003431 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003432#else
3433 has_text_relocations = true;
3434 break;
3435#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003436
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003437 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003438 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003439 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003440
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003441 case DT_NEEDED:
3442 ++needed_count;
3443 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003444
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003445 case DT_FLAGS:
3446 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003447#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003448 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003449 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003450#else
3451 has_text_relocations = true;
3452#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003453 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003454 if (d->d_un.d_val & DF_SYMBOLIC) {
3455 has_DT_SYMBOLIC = true;
3456 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003457 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003458
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003459 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003460 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003461
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003462 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -07003463 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 -07003464 }
3465 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003466#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003467 case DT_MIPS_RLD_MAP:
3468 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3469 {
3470 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3471 *dp = &_r_debug;
3472 }
3473 break;
Raghu Gandham68815722014-12-18 19:12:19 -08003474 case DT_MIPS_RLD_MAP2:
3475 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
3476 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003477 r_debug** dp = reinterpret_cast<r_debug**>(
3478 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003479 *dp = &_r_debug;
3480 }
3481 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003482
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003483 case DT_MIPS_RLD_VERSION:
3484 case DT_MIPS_FLAGS:
3485 case DT_MIPS_BASE_ADDRESS:
3486 case DT_MIPS_UNREFEXTNO:
3487 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003488
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003489 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003490 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003491 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003492
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003493 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003494 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003495 break;
3496
3497 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003498 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003499 break;
3500#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003501 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3502 case DT_BIND_NOW:
3503 break;
3504
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003505 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003506 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3507 break;
3508
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003509 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003510 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3511 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003512 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003513 verdef_cnt_ = d->d_un.d_val;
3514 break;
3515
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003516 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003517 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3518 break;
3519
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003520 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003521 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003522 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003523
Evgenii Stepanov68650822015-06-10 13:38:39 -07003524 case DT_RUNPATH:
3525 // this is parsed after we have strtab initialized (see below).
3526 break;
3527
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003528 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003529 if (!relocating_linker) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003530 DL_WARN("%s: unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003531 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3532 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003533 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003534 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003535 }
3536
Duane Sandbc425c72015-06-01 16:29:14 -07003537#if defined(__mips__) && !defined(__LP64__)
3538 if (!mips_check_and_adjust_fp_modes()) {
3539 return false;
3540 }
3541#endif
3542
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003543 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003544 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003545
3546 // Sanity checks.
3547 if (relocating_linker && needed_count != 0) {
3548 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3549 return false;
3550 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003551 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003552 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003553 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003554 return false;
3555 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003556 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003557 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003558 return false;
3559 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003560 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003561 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003562 return false;
3563 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003564
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003565 // second pass - parse entries relying on strtab
3566 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003567 switch (d->d_tag) {
3568 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003569 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003570 break;
3571 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003572 set_dt_runpath(get_string(d->d_un.d_val));
3573 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003574 }
3575 }
3576
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003577 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003578 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003579 // because they can't find dt_needed library by soname.
3580 // This workaround should keep them working. (applies only
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003581 // for apps targeting sdk version <=22). Make an exception for
3582 // the main executable and linker; they do not need to have dt_soname
3583 if (soname_ == nullptr && this != somain && (flags_ & FLAG_LINKER) == 0 &&
3584 get_application_target_sdk_version() <= 22) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003585 soname_ = basename(realpath_.c_str());
3586 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3587 get_realpath(), soname_);
3588 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003589 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003590}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003591
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003592bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3593 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003594
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003595 local_group_root_ = local_group.front();
3596 if (local_group_root_ == nullptr) {
3597 local_group_root_ = this;
3598 }
3599
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003600 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3601 target_sdk_version_ = get_application_target_sdk_version();
3602 }
3603
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003604 VersionTracker version_tracker;
3605
3606 if (!version_tracker.init(this)) {
3607 return false;
3608 }
3609
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003610#if !defined(__LP64__)
3611 if (has_text_relocations) {
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003612 // Fail if app is targeting sdk version > 22
Dmitriy Ivanov80687862015-10-09 13:58:46 -07003613 if (get_application_target_sdk_version() > 22) {
Dmitriy Ivanovfae39d22015-10-13 11:07:56 -07003614 PRINT("%s: has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003615 DL_ERR("%s: has text relocations", get_realpath());
3616 return false;
3617 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003618 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003619 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003620 DL_WARN("%s has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003621 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003622 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3623 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003624 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003625 return false;
3626 }
3627 }
3628#endif
3629
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003630 if (android_relocs_ != nullptr) {
3631 // check signature
3632 if (android_relocs_size_ > 3 &&
3633 android_relocs_[0] == 'A' &&
3634 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003635 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003636 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003637 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003638
3639 bool relocated = false;
3640 const uint8_t* packed_relocs = android_relocs_ + 4;
3641 const size_t packed_relocs_size = android_relocs_size_ - 4;
3642
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003643 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003644 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003645 packed_reloc_iterator<sleb128_decoder>(
3646 sleb128_decoder(packed_relocs, packed_relocs_size)),
3647 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003648
3649 if (!relocated) {
3650 return false;
3651 }
3652 } else {
3653 DL_ERR("bad android relocation header.");
3654 return false;
3655 }
3656 }
3657
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003658#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003659 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003660 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003661 if (!relocate(version_tracker,
3662 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003663 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003664 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003665 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003666 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003667 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003668 if (!relocate(version_tracker,
3669 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003670 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003671 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003672 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003673#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003674 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003675 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003676 if (!relocate(version_tracker,
3677 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003678 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003679 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003680 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003681 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003682 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003683 if (!relocate(version_tracker,
3684 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003685 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003686 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003687 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003688#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003689
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003690#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003691 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003692 return false;
3693 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003694#endif
3695
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003696 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003697
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003698#if !defined(__LP64__)
3699 if (has_text_relocations) {
3700 // All relocations are done, we can protect our segments back to read-only.
3701 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3702 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003703 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003704 return false;
3705 }
3706 }
3707#endif
3708
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003709 /* We can also turn on GNU RELRO protection */
3710 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3711 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003712 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003713 return false;
3714 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003715
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003716 /* Handle serializing/sharing the RELRO segment */
3717 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3718 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3719 extinfo->relro_fd) < 0) {
3720 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003721 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003722 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003723 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003724 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3725 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3726 extinfo->relro_fd) < 0) {
3727 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003728 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003729 return false;
3730 }
3731 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003732
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003733 notify_gdb_of_load(this);
3734 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003735}
3736
Nick Kralevich468319c2011-11-11 15:53:17 -08003737/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003738 * This function add vdso to internal dso list.
3739 * It helps to stack unwinding through signal handlers.
3740 * Also, it makes bionic more like glibc.
3741 */
Kito Cheng812fd422014-03-25 22:53:56 +08003742static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003743#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08003744 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003745 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08003746 return;
3747 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003748
Dmitriy Ivanovd9b08a02015-11-16 13:17:27 -08003749 soinfo* si = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003750
Elliott Hughes0266ae52014-02-10 17:46:57 -08003751 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
3752 si->phnum = ehdr_vdso->e_phnum;
3753 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
3754 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003755 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003756
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003757 si->prelink_image();
3758 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003759#endif
3760}
3761
3762/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003763 * This is linker soinfo for GDB. See details below.
3764 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003765#if defined(__LP64__)
3766#define LINKER_PATH "/system/bin/linker64"
3767#else
3768#define LINKER_PATH "/system/bin/linker"
3769#endif
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003770
3771// This is done to avoid calling c-tor prematurely
3772// because soinfo c-tor needs memory allocator
3773// which might be initialized after global variables.
3774static uint8_t linker_soinfo_for_gdb_buf[sizeof(soinfo)] __attribute__((aligned(8)));
3775static soinfo* linker_soinfo_for_gdb = nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003776
3777/* gdb expects the linker to be in the debug shared object list.
3778 * Without this, gdb has trouble locating the linker's ".text"
3779 * and ".plt" sections. Gdb could also potentially use this to
3780 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
3781 * Don't use soinfo_alloc(), because the linker shouldn't
3782 * be on the soinfo list.
3783 */
3784static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003785 linker_soinfo_for_gdb = new (linker_soinfo_for_gdb_buf) soinfo(nullptr, LINKER_PATH,
3786 nullptr, 0, 0);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003787
Dmitriy Ivanov175dae92015-06-10 19:46:19 -07003788 linker_soinfo_for_gdb->load_bias = linker_base;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003789
3790 /*
3791 * Set the dynamic field in the link map otherwise gdb will complain with
3792 * the following:
3793 * warning: .dynamic section for "/system/bin/linker" is not at the
3794 * expected address (wrong library or version mismatch?)
3795 */
3796 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
3797 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
3798 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003799 &linker_soinfo_for_gdb->dynamic, nullptr);
3800 insert_soinfo_into_debug_map(linker_soinfo_for_gdb);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003801}
3802
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003803static void init_default_namespace() {
3804 g_default_namespace.set_name("(default)");
3805 g_default_namespace.set_isolated(false);
3806
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003807 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3808 somain->load_bias);
3809 const char* bname = basename(interp);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003810 if (bname && (strcmp(bname, "linker_asan") == 0 || strcmp(bname, "linker_asan64") == 0)) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003811 g_default_ld_paths = kAsanDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003812 } else {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003813 g_default_ld_paths = kDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003814 }
3815
3816 std::vector<std::string> ld_default_paths;
3817 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
3818 ld_default_paths.push_back(g_default_ld_paths[i]);
3819 }
3820
3821 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003822};
3823
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07003824extern "C" int __system_properties_init(void);
3825
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003826/*
Nick Kralevich468319c2011-11-11 15:53:17 -08003827 * This code is called after the linker has linked itself and
3828 * fixed it's own GOT. It is safe to make references to externs
3829 * and other non-local data at this point.
3830 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08003831static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04003832#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003833 struct timeval t0, t1;
3834 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04003835#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003836
Elliott Hughes1801db32015-06-08 18:04:00 -07003837 // Sanitize the environment.
3838 __libc_init_AT_SECURE(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01003839
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07003840 // Initialize system properties
3841 __system_properties_init(); // may use 'environ'
3842
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003843 debuggerd_init();
3844
3845 // Get a few environment variables.
Elliott Hughes1801db32015-06-08 18:04:00 -07003846 const char* LD_DEBUG = getenv("LD_DEBUG");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003847 if (LD_DEBUG != nullptr) {
3848 g_ld_debug_verbosity = atoi(LD_DEBUG);
3849 }
3850
Elliott Hughes1801db32015-06-08 18:04:00 -07003851 // These should have been sanitized by __libc_init_AT_SECURE, but the test
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003852 // doesn't cost us anything.
3853 const char* ldpath_env = nullptr;
3854 const char* ldpreload_env = nullptr;
Elliott Hughes1801db32015-06-08 18:04:00 -07003855 if (!getauxval(AT_SECURE)) {
3856 ldpath_env = getenv("LD_LIBRARY_PATH");
3857 ldpreload_env = getenv("LD_PRELOAD");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003858 }
3859
3860 INFO("[ android linker & debugger ]");
3861
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003862 soinfo* si = soinfo_alloc(&g_default_namespace, args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003863 if (si == nullptr) {
3864 exit(EXIT_FAILURE);
3865 }
3866
3867 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003868 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003869 link_map* map = &(si->link_map_head);
3870
3871 map->l_addr = 0;
3872 map->l_name = args.argv[0];
3873 map->l_prev = nullptr;
3874 map->l_next = nullptr;
3875
3876 _r_debug.r_map = map;
3877 r_debug_tail = map;
3878
3879 init_linker_info_for_gdb(linker_base);
3880
3881 // Extract information passed from the kernel.
3882 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
3883 si->phnum = args.getauxval(AT_PHNUM);
3884 si->entry = args.getauxval(AT_ENTRY);
3885
3886 /* Compute the value of si->base. We can't rely on the fact that
3887 * the first entry is the PHDR because this will not be true
3888 * for certain executables (e.g. some in the NDK unit test suite)
3889 */
3890 si->base = 0;
3891 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
3892 si->load_bias = 0;
3893 for (size_t i = 0; i < si->phnum; ++i) {
3894 if (si->phdr[i].p_type == PT_PHDR) {
3895 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
3896 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
3897 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07003898 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003899 }
3900 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07003901
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003902 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
3903 if (elf_hdr->e_type != ET_DYN) {
3904 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
3905 exit(EXIT_FAILURE);
3906 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003907
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003908 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
3909 parse_LD_LIBRARY_PATH(ldpath_env);
3910 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01003911
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003912 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003913
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003914 init_default_namespace();
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003915
Dmitriy Ivanov67181252015-01-07 15:48:25 -08003916 if (!si->prelink_image()) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003917 __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer());
Dmitriy Ivanov67181252015-01-07 15:48:25 -08003918 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003919
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003920 // add somain to global group
3921 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
3922
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003923 // Load ld_preloads and dependencies.
3924 StringLinkedList needed_library_name_list;
3925 size_t needed_libraries_count = 0;
3926 size_t ld_preloads_count = 0;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07003927
3928 for (const auto& ld_preload_name : g_ld_preload_names) {
3929 needed_library_name_list.push_back(ld_preload_name.c_str());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003930 ++needed_libraries_count;
Dmitriy Ivanovf8093a92015-04-28 18:09:53 -07003931 ++ld_preloads_count;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003932 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003933
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003934 for_each_dt_needed(si, [&](const char* name) {
3935 needed_library_name_list.push_back(name);
3936 ++needed_libraries_count;
3937 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003938
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003939 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003940
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003941 memset(needed_library_names, 0, sizeof(needed_library_names));
3942 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003943
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07003944 if (needed_libraries_count > 0 &&
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003945 !find_libraries(&g_default_namespace, si, needed_library_names, needed_libraries_count,
3946 nullptr, &g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07003947 /* add_as_children */ true)) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003948 __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003949 } else if (needed_libraries_count == 0) {
3950 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003951 __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003952 }
3953 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003954 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003955
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003956 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07003957
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08003958 {
3959 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06003960
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08003961 si->call_pre_init_constructors();
3962
3963 /* After the prelink_image, the si->load_bias is initialized.
3964 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
3965 * We need to update this value for so exe here. So Unwind_Backtrace
3966 * for some arch like x86 could work correctly within so exe.
3967 */
3968 map->l_addr = si->load_bias;
3969 si->call_constructors();
3970 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003971
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003972#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003973 gettimeofday(&t1, nullptr);
3974 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
3975 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
3976 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003977#endif
3978#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003979 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
3980 linker_stats.count[kRelocAbsolute],
3981 linker_stats.count[kRelocRelative],
3982 linker_stats.count[kRelocCopy],
3983 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003984#endif
3985#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003986 {
3987 unsigned n;
3988 unsigned i;
3989 unsigned count = 0;
3990 for (n = 0; n < 4096; n++) {
3991 if (bitmask[n]) {
3992 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003993#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003994 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003995#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003996 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003997#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003998 if (x & 1) {
3999 count++;
4000 }
4001 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004002 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004003 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004004 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004005 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
4006 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004007#endif
4008
4009#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004010 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004011#endif
4012
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004013 TRACE("[ Ready to execute '%s' @ %p ]", si->get_realpath(), reinterpret_cast<void*>(si->entry));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004014 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004015}
Nick Kralevich468319c2011-11-11 15:53:17 -08004016
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004017/* Compute the load-bias of an existing executable. This shall only
4018 * be used to compute the load bias of an executable or shared library
4019 * that was loaded by the kernel itself.
4020 *
4021 * Input:
4022 * elf -> address of ELF header, assumed to be at the start of the file.
4023 * Return:
4024 * load bias, i.e. add the value of any p_vaddr in the file to get
4025 * the corresponding address in memory.
4026 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004027static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
4028 ElfW(Addr) offset = elf->e_phoff;
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004029 const ElfW(Phdr)* phdr_table =
4030 reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004031 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004032
Elliott Hughes0266ae52014-02-10 17:46:57 -08004033 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08004034 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08004035 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004036 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08004037 }
4038 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004039}
4040
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004041extern "C" void _start();
4042
Nick Kralevich468319c2011-11-11 15:53:17 -08004043/*
4044 * This is the entry point for the linker, called from begin.S. This
4045 * method is responsible for fixing the linker's own relocations, and
4046 * then calling __linker_init_post_relocation().
4047 *
4048 * Because this method is called before the linker has fixed it's own
4049 * relocations, any attempt to reference an extern variable, extern
4050 * function, or other GOT reference will generate a segfault.
4051 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004052extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004053 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08004054
Elliott Hughes0266ae52014-02-10 17:46:57 -08004055 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004056 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004057 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08004058 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08004059
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004060 soinfo linker_so(nullptr, nullptr, nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08004061
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004062 // If the linker is not acting as PT_INTERP entry_point is equal to
4063 // _start. Which means that the linker is running as an executable and
4064 // already linked by PT_INTERP.
4065 //
4066 // This happens when user tries to run 'adb shell /system/bin/linker'
4067 // see also https://code.google.com/p/android/issues/detail?id=63174
4068 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004069 __libc_fatal("This is %s, the helper program for shared library executables.", args.argv[0]);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004070 }
4071
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004072 linker_so.base = linker_addr;
4073 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
4074 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07004075 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004076 linker_so.phdr = phdr;
4077 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004078 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07004079
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07004080 // This might not be obvious... The reasons why we pass g_empty_list
4081 // in place of local_group here are (1) we do not really need it, because
4082 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
4083 // itself without having to look into local_group and (2) allocators
4084 // are not yet initialized, and therefore we cannot use linked_list.push_*
4085 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004086 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004087 __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer());
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004088 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07004089
Elliott Hughesd2948632015-07-21 11:57:09 -07004090 __libc_init_main_thread(args);
Dmitriy Ivanov14241402014-08-26 14:16:52 -07004091
Josh Gao93c0f5e2015-10-06 11:08:13 -07004092 // Initialize the linker's static libc's globals
4093 __libc_init_globals(args);
4094
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004095 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004096 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07004097
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004098 // Initialize static variables. Note that in order to
4099 // get correct libdl_info we need to call constructors
4100 // before get_libdl_info().
4101 solist = get_libdl_info();
4102 sonext = get_libdl_info();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004103 g_default_namespace.soinfo_list().push_back(get_libdl_info());
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004104
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004105 // We have successfully fixed our own relocations. It's safe to run
4106 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07004107 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08004108 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004109
Elliott Hughes611f9562015-01-23 10:43:58 -08004110 INFO("[ jumping to _start ]");
4111
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004112 // Return the address that the calling assembly stub should jump to.
4113 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08004114}