blob: 93fab8f8a0fe0c9187db6932e9a540e07beea822 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002 * Copyright (C) 2008 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Dmitriy Ivanov19133522015-06-02 17:36:54 -070029#include <android/api-level.h>
Elliott Hughes46882792012-08-03 16:49:39 -070030#include <errno.h>
31#include <fcntl.h>
Elliott Hughes0266ae52014-02-10 17:46:57 -080032#include <inttypes.h>
Elliott Hughes46882792012-08-03 16:49:39 -070033#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
Elliott Hughes46882792012-08-03 16:49:39 -070037#include <sys/mman.h>
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -080038#include <sys/param.h>
Elliott Hughes46882792012-08-03 16:49:39 -070039#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070041#include <new>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070042#include <string>
Dmitriy Ivanovb4827502015-09-28 16:38:31 -070043#include <unordered_map>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070044#include <vector>
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070045
Elliott Hughes46882792012-08-03 16:49:39 -070046// Private C library headers.
Mingwei Shibe910522015-11-12 07:02:14 +000047#include "private/bionic_globals.h"
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"
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -080055#include "linker_gdb_support.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056#include "linker_debug.h"
Dmitriy Ivanov18870d32015-04-22 13:10:04 -070057#include "linker_sleb128.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020058#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080059#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080060#include "linker_reloc_iterators.h"
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070061#include "linker_utils.h"
tony.ys_liub4474402015-07-29 18:00:22 +080062
Elliott Hughes939a7e02015-12-04 15:27:46 -080063#include "android-base/strings.h"
Simon Baldwinaef71952015-01-16 13:22:54 +000064#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080065
Josh Gao93c0f5e2015-10-06 11:08:13 -070066extern void __libc_init_globals(KernelArgumentBlock&);
Elliott Hughes1801db32015-06-08 18:04:00 -070067extern void __libc_init_AT_SECURE(KernelArgumentBlock&);
68
Mingwei Shibe910522015-11-12 07:02:14 +000069extern "C" void _start();
70
Elliott Hughes1801db32015-06-08 18:04:00 -070071// Override macros to use C++ style casts.
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080072#undef ELF_ST_TYPE
73#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
74
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070075struct android_namespace_t {
76 public:
77 android_namespace_t() : name_(nullptr), is_isolated_(false) {}
78
79 const char* get_name() const { return name_; }
80 void set_name(const char* name) { name_ = name; }
81
82 bool is_isolated() const { return is_isolated_; }
83 void set_isolated(bool isolated) { is_isolated_ = isolated; }
84
85 const std::vector<std::string>& get_ld_library_paths() const {
86 return ld_library_paths_;
87 }
88 void set_ld_library_paths(std::vector<std::string>&& library_paths) {
89 ld_library_paths_ = library_paths;
90 }
91
92 const std::vector<std::string>& get_default_library_paths() const {
93 return default_library_paths_;
94 }
95 void set_default_library_paths(std::vector<std::string>&& library_paths) {
96 default_library_paths_ = library_paths;
97 }
98
Dimitry Ivanovd17a3772016-03-01 13:11:28 -080099 const std::vector<std::string>& get_permitted_paths() const {
100 return permitted_paths_;
101 }
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800102 void set_permitted_paths(std::vector<std::string>&& permitted_paths) {
103 permitted_paths_ = permitted_paths;
104 }
105
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700106 void add_soinfo(soinfo* si) {
107 soinfo_list_.push_back(si);
108 }
109
110 void add_soinfos(const soinfo::soinfo_list_t& soinfos) {
111 for (auto si : soinfos) {
112 add_soinfo(si);
113 }
114 }
115
116 void remove_soinfo(soinfo* si) {
117 soinfo_list_.remove_if([&](soinfo* candidate) {
118 return si == candidate;
119 });
120 }
121
122 const soinfo::soinfo_list_t& soinfo_list() const { return soinfo_list_; }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700123
124 // For isolated namespaces - checks if the file is on the search path;
125 // always returns true for not isolated namespace.
126 bool is_accessible(const std::string& path);
127
128 private:
129 const char* name_;
130 bool is_isolated_;
131 std::vector<std::string> ld_library_paths_;
132 std::vector<std::string> default_library_paths_;
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800133 std::vector<std::string> permitted_paths_;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700134 soinfo::soinfo_list_t soinfo_list_;
135
136 DISALLOW_COPY_AND_ASSIGN(android_namespace_t);
137};
138
139android_namespace_t g_default_namespace;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700140static std::unordered_map<uintptr_t, soinfo*> g_soinfo_handles_map;
141static android_namespace_t* g_anonymous_namespace = &g_default_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700142
Elliott Hughes0266ae52014-02-10 17:46:57 -0800143static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700145static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
146static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200147
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700148static LinkerTypeAllocator<android_namespace_t> g_namespace_allocator;
149
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700150static soinfo* solist;
151static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700152static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800153
Elliott Hughes1728b232014-05-14 10:02:03 -0700154static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700155#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700156 "/system/lib64",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800157 "/vendor/lib64",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700158#else
Elliott Hughes124fae92012-10-31 14:20:03 -0700159 "/system/lib",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800160 "/vendor/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700161#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700162 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700163};
David Bartleybc3a5c22009-06-02 18:27:28 -0700164
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700165static const char* const kAsanDefaultLdPaths[] = {
166#if defined(__LP64__)
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700167 "/data/lib64",
168 "/system/lib64",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800169 "/data/vendor/lib64",
170 "/vendor/lib64",
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700171#else
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700172 "/data/lib",
173 "/system/lib",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800174 "/data/vendor/lib",
175 "/vendor/lib",
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700176#endif
177 nullptr
178};
179
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700180static const ElfW(Versym) kVersymNotNeeded = 0;
181static const ElfW(Versym) kVersymGlobal = 1;
182
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700183static const char* const* g_default_ld_paths;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700184static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800185
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700186static std::vector<soinfo*> g_ld_preloads;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600187
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700188static bool g_public_namespace_initialized;
189static soinfo::soinfo_list_t g_public_namespace;
190
Elliott Hughes1728b232014-05-14 10:02:03 -0700191__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800192
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700193__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700194
Evgenii Stepanov68650822015-06-10 13:38:39 -0700195static std::string dirname(const char *path) {
196 const char* last_slash = strrchr(path, '/');
197 if (last_slash == path) return "/";
198 else if (last_slash == nullptr) return ".";
199 else
200 return std::string(path, last_slash - path);
201}
202
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700204struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700205 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700206};
207
208static linker_stats_t linker_stats;
209
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800210void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700211 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700212}
213#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800214void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700215}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800216#endif
217
218#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800219uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800220#endif
221
Dima Zavin2e855792009-05-20 18:28:09 -0700222static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700223
Elliott Hughes650be4e2013-03-05 18:47:58 -0800224char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700225 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700226}
227
Elliott Hughes650be4e2013-03-05 18:47:58 -0800228size_t linker_get_error_buffer_size() {
229 return sizeof(__linker_dl_err_buf);
230}
231
Elliott Hughesbedfe382012-08-14 14:07:59 -0700232static void notify_gdb_of_load(soinfo* info) {
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800233 if (info->is_linker() || info->is_main_executable()) {
234 // gdb already knows about the linker and the main executable.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700235 return;
236 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800238 link_map* map = &(info->link_map_head);
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000239
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800240 map->l_addr = info->load_bias;
241 // link_map l_name field is not const.
242 map->l_name = const_cast<char*>(info->get_realpath());
243 map->l_ld = info->dynamic;
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000244
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800245 CHECK(map->l_name != nullptr);
246 CHECK(map->l_name[0] != '\0');
247
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800248 notify_gdb_of_load(map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700249}
250
Elliott Hughesbedfe382012-08-14 14:07:59 -0700251static void notify_gdb_of_unload(soinfo* info) {
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800252 notify_gdb_of_unload(&(info->link_map_head));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800253}
254
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700255bool android_namespace_t::is_accessible(const std::string& file) {
256 if (!is_isolated_) {
257 return true;
258 }
259
260 for (const auto& dir : ld_library_paths_) {
261 if (file_is_in_dir(file, dir)) {
262 return true;
263 }
264 }
265
266 for (const auto& dir : default_library_paths_) {
267 if (file_is_in_dir(file, dir)) {
268 return true;
269 }
270 }
271
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800272 for (const auto& dir : permitted_paths_) {
273 if (file_is_under_dir(file, dir)) {
274 return true;
275 }
276 }
277
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700278 return false;
279}
280
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700281LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
282 return g_soinfo_links_allocator.alloc();
283}
284
285void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
286 g_soinfo_links_allocator.free(entry);
287}
288
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700289static soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
290 struct stat* file_stat, off64_t file_offset,
291 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700292 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200293 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700294 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200295 }
296
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700297 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
298 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700299
Magnus Malmbornba98d922012-09-12 13:00:55 +0200300 sonext->next = si;
301 sonext = si;
302
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700303 si->generate_handle();
304 ns->add_soinfo(si);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700305
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700306 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200307 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800308}
309
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800310static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700311 if (si == nullptr) {
312 return;
313 }
314
315 if (si->base != 0 && si->size != 0) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800316 if (!si->is_mapped_by_caller()) {
317 munmap(reinterpret_cast<void*>(si->base), si->size);
318 } else {
319 // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE
320 mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE,
321 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
322 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700323 }
324
325 soinfo *prev = nullptr, *trav;
326
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700327 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700328
329 for (trav = solist; trav != nullptr; trav = trav->next) {
330 if (trav == si) {
331 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700332 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700333 prev = trav;
334 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800335
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700336 if (trav == nullptr) {
337 // si was not in solist
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700338 DL_ERR("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700339 return;
340 }
Elliott Hughes46882792012-08-03 16:49:39 -0700341
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700342 // clear links to/from si
343 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700344
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700345 // prev will never be null, because the first entry in solist is
346 // always the static libdl_info.
347 prev->next = si->next;
348 if (si == sonext) {
349 sonext = prev;
350 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800351
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700352 // remove from the namespace
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700353 si->get_namespace()->remove_soinfo(si);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700354
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700355 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700356 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800357}
358
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700359// For every path element this function checks of it exists, and is a directory,
360// and normalizes it:
361// 1. For regular path it converts it to realpath()
362// 2. For path in a zip file it uses realpath on the zipfile
363// normalizes entry name by calling normalize_path function.
364static void resolve_paths(std::vector<std::string>& paths,
365 std::vector<std::string>* resolved_paths) {
366 resolved_paths->clear();
367 for (const auto& path : paths) {
368 char resolved_path[PATH_MAX];
369 const char* original_path = path.c_str();
370 if (realpath(original_path, resolved_path) != nullptr) {
371 struct stat s;
372 if (stat(resolved_path, &s) == 0) {
373 if (S_ISDIR(s.st_mode)) {
374 resolved_paths->push_back(resolved_path);
375 } else {
376 DL_WARN("Warning: \"%s\" is not a directory (excluding from path)", resolved_path);
377 continue;
378 }
379 } else {
380 DL_WARN("Warning: cannot stat file \"%s\": %s", resolved_path, strerror(errno));
381 continue;
382 }
383 } else {
384 std::string zip_path;
385 std::string entry_path;
386
387 std::string normalized_path;
388
389 if (!normalize_path(original_path, &normalized_path)) {
390 DL_WARN("Warning: unable to normalize \"%s\"", original_path);
391 continue;
392 }
393
394 if (parse_zip_path(normalized_path.c_str(), &zip_path, &entry_path)) {
395 if (realpath(zip_path.c_str(), resolved_path) == nullptr) {
396 DL_WARN("Warning: unable to resolve \"%s\": %s", zip_path.c_str(), strerror(errno));
397 continue;
398 }
399
400 ZipArchiveHandle handle = nullptr;
Yabin Cui722072d2016-03-21 17:10:12 -0700401 void* cookie = nullptr;
402 auto zip_guard = make_scope_guard([&]() {
403 if (cookie != nullptr) {
404 EndIteration(cookie);
405 }
406 if (handle != nullptr) {
407 CloseArchive(handle);
408 }
409 });
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700410 if (OpenArchive(resolved_path, &handle) != 0) {
411 DL_WARN("Warning: unable to open zip archive: %s", resolved_path);
412 continue;
413 }
414
415 // Check if zip-file has a dir with entry_path name
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700416 std::string prefix_str = entry_path + "/";
417 ZipString prefix(prefix_str.c_str());
418
419 ZipEntry out_data;
420 ZipString out_name;
421
422 int32_t error_code;
423
424 if ((error_code = StartIteration(handle, &cookie, &prefix, nullptr)) != 0) {
425 DL_WARN("Unable to iterate over zip-archive entries \"%s\";"
426 " error code: %d", zip_path.c_str(), error_code);
427 continue;
428 }
429
430 if (Next(cookie, &out_data, &out_name) != 0) {
431 DL_WARN("Unable to find entries starting with \"%s\" in \"%s\"",
432 prefix_str.c_str(), zip_path.c_str());
433 continue;
434 }
435
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700436 resolved_paths->push_back(std::string(resolved_path) + kZipFileSeparator + entry_path);
437 }
438 }
439 }
440}
441
442static void split_path(const char* path, const char* delimiters,
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700443 std::vector<std::string>* paths) {
Dmitriy Ivanovfbfba642015-11-16 14:23:37 -0800444 if (path != nullptr && path[0] != 0) {
tony.ys_liub4474402015-07-29 18:00:22 +0800445 *paths = android::base::Split(path, delimiters);
Elliott Hughescade4c32012-12-20 14:42:14 -0800446 }
447}
448
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700449static void parse_path(const char* path, const char* delimiters,
450 std::vector<std::string>* resolved_paths) {
451 std::vector<std::string> paths;
452 split_path(path, delimiters, &paths);
453 resolve_paths(paths, resolved_paths);
454}
455
Elliott Hughescade4c32012-12-20 14:42:14 -0800456static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700457 std::vector<std::string> ld_libary_paths;
458 parse_path(path, ":", &ld_libary_paths);
459 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800460}
461
Evgenii Stepanov68650822015-06-10 13:38:39 -0700462void soinfo::set_dt_runpath(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700463 if (!has_min_version(3)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700464 return;
465 }
466
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700467 std::vector<std::string> runpaths;
468
469 split_path(path, ":", &runpaths);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700470
471 std::string origin = dirname(get_realpath());
472 // FIXME: add $LIB and $PLATFORM.
473 std::pair<std::string, std::string> substs[] = {{"ORIGIN", origin}};
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700474 for (auto&& s : runpaths) {
Evgenii Stepanov68650822015-06-10 13:38:39 -0700475 size_t pos = 0;
476 while (pos < s.size()) {
477 pos = s.find("$", pos);
478 if (pos == std::string::npos) break;
479 for (const auto& subst : substs) {
480 const std::string& token = subst.first;
481 const std::string& replacement = subst.second;
482 if (s.substr(pos + 1, token.size()) == token) {
483 s.replace(pos, token.size() + 1, replacement);
484 // -1 to compensate for the ++pos below.
485 pos += replacement.size() - 1;
486 break;
487 } else if (s.substr(pos + 1, token.size() + 2) == "{" + token + "}") {
488 s.replace(pos, token.size() + 3, replacement);
489 pos += replacement.size() - 1;
490 break;
491 }
492 }
493 // Skip $ in case it did not match any of the known substitutions.
494 ++pos;
495 }
496 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700497
498 resolve_paths(runpaths, &dt_runpath_);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700499}
500
Elliott Hughescade4c32012-12-20 14:42:14 -0800501static void parse_LD_PRELOAD(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700502 g_ld_preload_names.clear();
503 if (path != nullptr) {
504 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
505 g_ld_preload_names = android::base::Split(path, " :");
506 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800507}
508
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700509static bool realpath_fd(int fd, std::string* realpath) {
510 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700511 __libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700512 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -0700513 PRINT("readlink('%s') failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700514 return false;
515 }
516
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700517 *realpath = &buf[0];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700518 return true;
519}
520
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700521#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700522
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700523// For a given PC, find the .so that it belongs to.
524// Returns the base address of the .ARM.exidx section
525// for that .so, and the number of 8-byte entries
526// in that section (via *pcount).
527//
528// Intended to be called by libc's __gnu_Unwind_Find_exidx().
529//
530// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800531_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800532 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800533
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700534 for (soinfo* si = solist; si != 0; si = si->next) {
535 if ((addr >= si->base) && (addr < (si->base + si->size))) {
536 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800537 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800538 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700539 }
540 *pcount = 0;
541 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800542}
Elliott Hughes46882792012-08-03 16:49:39 -0700543
Christopher Ferris24053a42013-08-19 17:45:09 -0700544#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700545
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700546// Here, we only have to provide a callback to iterate across all the
547// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700548int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700549 int rv = 0;
550 for (soinfo* si = solist; si != nullptr; si = si->next) {
551 dl_phdr_info dl_info;
552 dl_info.dlpi_addr = si->link_map_head.l_addr;
553 dl_info.dlpi_name = si->link_map_head.l_name;
554 dl_info.dlpi_phdr = si->phdr;
555 dl_info.dlpi_phnum = si->phnum;
556 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
557 if (rv != 0) {
558 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800559 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700560 }
561 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800562}
Elliott Hughes46882792012-08-03 16:49:39 -0700563
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700564const ElfW(Versym)* soinfo::get_versym(size_t n) const {
565 if (has_min_version(2) && versym_ != nullptr) {
566 return versym_ + n;
567 }
568
569 return nullptr;
570}
571
572ElfW(Addr) soinfo::get_verneed_ptr() const {
573 if (has_min_version(2)) {
574 return verneed_ptr_;
575 }
576
577 return 0;
578}
579
580size_t soinfo::get_verneed_cnt() const {
581 if (has_min_version(2)) {
582 return verneed_cnt_;
583 }
584
585 return 0;
586}
587
588ElfW(Addr) soinfo::get_verdef_ptr() const {
589 if (has_min_version(2)) {
590 return verdef_ptr_;
591 }
592
593 return 0;
594}
595
596size_t soinfo::get_verdef_cnt() const {
597 if (has_min_version(2)) {
598 return verdef_cnt_;
599 }
600
601 return 0;
602}
603
604template<typename F>
605static bool for_each_verdef(const soinfo* si, F functor) {
606 if (!si->has_min_version(2)) {
607 return true;
608 }
609
610 uintptr_t verdef_ptr = si->get_verdef_ptr();
611 if (verdef_ptr == 0) {
612 return true;
613 }
614
615 size_t offset = 0;
616
617 size_t verdef_cnt = si->get_verdef_cnt();
618 for (size_t i = 0; i<verdef_cnt; ++i) {
619 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
620 size_t verdaux_offset = offset + verdef->vd_aux;
621 offset += verdef->vd_next;
622
623 if (verdef->vd_version != 1) {
Dmitriy Ivanov3d7bea12015-04-20 17:40:39 -0700624 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700625 i, verdef->vd_version, si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700626 return false;
627 }
628
629 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
630 // "this is the version of the file itself. It must not be used for
631 // matching a symbol. It can be used to match references."
632 //
633 // http://www.akkadia.org/drepper/symbol-versioning
634 continue;
635 }
636
637 if (verdef->vd_cnt == 0) {
638 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
639 return false;
640 }
641
642 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
643
644 if (functor(i, verdef, verdaux) == true) {
645 break;
646 }
647 }
648
649 return true;
650}
651
652bool soinfo::find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const {
653 if (vi == nullptr) {
654 *versym = kVersymNotNeeded;
655 return true;
656 }
657
658 *versym = kVersymGlobal;
659
660 return for_each_verdef(this,
661 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
662 if (verdef->vd_hash == vi->elf_hash &&
663 strcmp(vi->name, get_string(verdaux->vda_name)) == 0) {
664 *versym = verdef->vd_ndx;
665 return true;
666 }
667
668 return false;
669 }
670 );
671}
672
673bool soinfo::find_symbol_by_name(SymbolName& symbol_name,
674 const version_info* vi,
675 const ElfW(Sym)** symbol) const {
676 uint32_t symbol_index;
677 bool success =
678 is_gnu_hash() ?
679 gnu_lookup(symbol_name, vi, &symbol_index) :
680 elf_lookup(symbol_name, vi, &symbol_index);
681
682 if (success) {
683 *symbol = symbol_index == 0 ? nullptr : symtab_ + symbol_index;
684 }
685
686 return success;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800687}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800688
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800689static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
690 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
691 ELF_ST_BIND(s->st_info) == STB_WEAK) {
692 return s->st_shndx != SHN_UNDEF;
693 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
694 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700695 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800696 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800697
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800698 return false;
699}
700
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700701static const ElfW(Versym) kVersymHiddenBit = 0x8000;
702
703static inline bool is_versym_hidden(const ElfW(Versym)* versym) {
704 // the symbol is hidden if bit 15 of versym is set.
705 return versym != nullptr && (*versym & kVersymHiddenBit) != 0;
706}
707
708static inline bool check_symbol_version(const ElfW(Versym) verneed,
709 const ElfW(Versym)* verdef) {
710 return verneed == kVersymNotNeeded ||
711 verdef == nullptr ||
712 verneed == (*verdef & ~kVersymHiddenBit);
713}
714
715bool soinfo::gnu_lookup(SymbolName& symbol_name,
716 const version_info* vi,
717 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800718 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800719 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800720
721 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800722 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
723 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800724
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700725 *symbol_index = 0;
726
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700727 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700728 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700729
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800730 // test against bloom filter
731 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700732 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700733 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700734
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700735 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800736 }
737
738 // bloom test says "probably yes"...
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700739 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800740
741 if (n == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700742 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700743 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700744
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700745 return true;
746 }
747
748 // lookup versym for the version definition in this library
749 // note the difference between "version is not requested" (vi == nullptr)
750 // and "version not found". In the first case verneed is kVersymNotNeeded
751 // which implies that the default version can be accepted; the second case results in
752 // verneed = 1 (kVersymGlobal) and implies that we should ignore versioned symbols
753 // for this library and consider only *global* ones.
754 ElfW(Versym) verneed = 0;
755 if (!find_verdef_version_index(vi, &verneed)) {
756 return false;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800757 }
758
759 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800760 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700761 const ElfW(Versym)* verdef = get_versym(n);
762 // skip hidden versions when verneed == kVersymNotNeeded (0)
763 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
764 continue;
765 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700766 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700767 check_symbol_version(verneed, verdef) &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800768 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
769 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700770 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700771 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(s->st_value),
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700772 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700773 *symbol_index = n;
774 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800775 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700776 } while ((gnu_chain_[n++] & 1) == 0);
777
778 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700779 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800780
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700781 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800782}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800783
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700784bool soinfo::elf_lookup(SymbolName& symbol_name,
785 const version_info* vi,
786 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800787 uint32_t hash = symbol_name.elf_hash();
788
789 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700790 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700791 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800792
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700793 ElfW(Versym) verneed = 0;
794 if (!find_verdef_version_index(vi, &verneed)) {
795 return false;
796 }
797
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800798 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
799 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700800 const ElfW(Versym)* verdef = get_versym(n);
801
802 // skip hidden versions when verneed == 0
803 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
804 continue;
805 }
806
807 if (check_symbol_version(verneed, verdef) &&
808 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700809 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800810 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700811 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700812 reinterpret_cast<void*>(s->st_value),
813 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700814 *symbol_index = n;
815 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800816 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800817 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800818
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700819 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700820 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700821 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700822
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700823 *symbol_index = 0;
824 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800825}
826
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700827soinfo::soinfo(android_namespace_t* ns, const char* realpath,
828 const struct stat* file_stat, off64_t file_offset,
829 int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700830 memset(this, 0, sizeof(*this));
831
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700832 if (realpath != nullptr) {
833 realpath_ = realpath;
834 }
835
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800836 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800837 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700838
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700839 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800840 this->st_dev_ = file_stat->st_dev;
841 this->st_ino_ = file_stat->st_ino;
842 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700843 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700844
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800845 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700846 this->namespace_ = ns;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700847}
848
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700849soinfo::~soinfo() {
850 g_soinfo_handles_map.erase(handle_);
851}
852
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800853static uint32_t calculate_elf_hash(const char* name) {
854 const uint8_t* name_bytes = reinterpret_cast<const uint8_t*>(name);
855 uint32_t h = 0, g;
856
857 while (*name_bytes) {
858 h = (h << 4) + *name_bytes++;
859 g = h & 0xf0000000;
860 h ^= g;
861 h ^= g >> 24;
862 }
863
864 return h;
865}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800866
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800867uint32_t SymbolName::elf_hash() {
868 if (!has_elf_hash_) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800869 elf_hash_ = calculate_elf_hash(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800870 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700871 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800872
873 return elf_hash_;
874}
875
876uint32_t SymbolName::gnu_hash() {
877 if (!has_gnu_hash_) {
878 uint32_t h = 5381;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700879 const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800880 while (*name != 0) {
881 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
882 }
883
884 gnu_hash_ = h;
885 has_gnu_hash_ = true;
886 }
887
888 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800889}
890
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700891bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
892 soinfo** si_found_in, const soinfo::soinfo_list_t& global_group,
893 const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800894 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700895 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700896
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700897 /* "This element's presence in a shared object library alters the dynamic linker's
898 * symbol resolution algorithm for references within the library. Instead of starting
899 * a symbol search with the executable file, the dynamic linker starts from the shared
900 * object itself. If the shared object fails to supply the referenced symbol, the
901 * dynamic linker then searches the executable file and other shared objects as usual."
902 *
903 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
904 *
905 * Note that this is unlikely since static linker avoids generating
906 * relocations for -Bsymbolic linked dynamic executables.
907 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700908 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700909 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700910 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
911 return false;
912 }
913
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700914 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700915 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700916 }
917 }
918
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700919 // 1. Look for it in global_group
920 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700921 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700922 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700923 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700924 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700925 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
926 error = true;
927 return false;
928 }
929
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700930 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700931 *si_found_in = global_si;
932 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700933 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700934
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700935 return true;
936 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700937
938 if (error) {
939 return false;
940 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700941 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700942
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700943 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700944 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700945 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700946 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700947 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700948 // we already did this - skip
949 return true;
950 }
951
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700952 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700953 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700954 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
955 error = true;
956 return false;
957 }
958
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700959 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700960 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700961 return false;
962 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700963
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700964 return true;
965 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700966
967 if (error) {
968 return false;
969 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700970 }
971
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700972 if (s != nullptr) {
973 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
974 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700975 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
976 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700977 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700978 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700979
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700980 *symbol = s;
981 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700982}
983
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800984class ProtectedDataGuard {
985 public:
986 ProtectedDataGuard() {
987 if (ref_count_++ == 0) {
988 protect_data(PROT_READ | PROT_WRITE);
989 }
990 }
991
992 ~ProtectedDataGuard() {
993 if (ref_count_ == 0) { // overflow
994 __libc_fatal("Too many nested calls to dlopen()");
995 }
996
997 if (--ref_count_ == 0) {
998 protect_data(PROT_READ);
999 }
1000 }
1001 private:
1002 void protect_data(int protection) {
1003 g_soinfo_allocator.protect_all(protection);
1004 g_soinfo_links_allocator.protect_all(protection);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001005 g_namespace_allocator.protect_all(protection);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001006 }
1007
1008 static size_t ref_count_;
1009};
1010
1011size_t ProtectedDataGuard::ref_count_ = 0;
1012
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001013// Each size has it's own allocator.
1014template<size_t size>
1015class SizeBasedAllocator {
1016 public:
1017 static void* alloc() {
1018 return allocator_.alloc();
1019 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001020
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001021 static void free(void* ptr) {
1022 allocator_.free(ptr);
1023 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001024
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001025 private:
1026 static LinkerBlockAllocator allocator_;
1027};
1028
1029template<size_t size>
1030LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
1031
1032template<typename T>
1033class TypeBasedAllocator {
1034 public:
1035 static T* alloc() {
1036 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
1037 }
1038
1039 static void free(T* ptr) {
1040 SizeBasedAllocator<sizeof(T)>::free(ptr);
1041 }
1042};
1043
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001044class LoadTask {
1045 public:
1046 struct deleter_t {
1047 void operator()(LoadTask* t) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001048 t->~LoadTask();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001049 TypeBasedAllocator<LoadTask>::free(t);
1050 }
1051 };
1052
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001053 static deleter_t deleter;
1054
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001055 static LoadTask* create(const char* name, soinfo* needed_by,
1056 std::unordered_map<const soinfo*, ElfReader>* readers_map) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001057 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001058 return new (ptr) LoadTask(name, needed_by, readers_map);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001059 }
1060
1061 const char* get_name() const {
1062 return name_;
1063 }
1064
1065 soinfo* get_needed_by() const {
1066 return needed_by_;
1067 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001068
1069 soinfo* get_soinfo() const {
1070 return si_;
1071 }
1072
1073 void set_soinfo(soinfo* si) {
1074 si_ = si;
1075 }
1076
1077 off64_t get_file_offset() const {
1078 return file_offset_;
1079 }
1080
1081 void set_file_offset(off64_t offset) {
1082 file_offset_ = offset;
1083 }
1084
1085 int get_fd() const {
1086 return fd_;
1087 }
1088
1089 void set_fd(int fd, bool assume_ownership) {
1090 fd_ = fd;
1091 close_fd_ = assume_ownership;
1092 }
1093
1094 const android_dlextinfo* get_extinfo() const {
1095 return extinfo_;
1096 }
1097
1098 void set_extinfo(const android_dlextinfo* extinfo) {
1099 extinfo_ = extinfo;
1100 }
1101
1102 const ElfReader& get_elf_reader() const {
1103 CHECK(si_ != nullptr);
1104 return (*elf_readers_map_)[si_];
1105 }
1106
1107 ElfReader& get_elf_reader() {
1108 CHECK(si_ != nullptr);
1109 return (*elf_readers_map_)[si_];
1110 }
1111
1112 std::unordered_map<const soinfo*, ElfReader>* get_readers_map() {
1113 return elf_readers_map_;
1114 }
1115
1116 bool read(const char* realpath, off64_t file_size) {
1117 ElfReader& elf_reader = get_elf_reader();
1118 return elf_reader.Read(realpath, fd_, file_offset_, file_size);
1119 }
1120
1121 bool load() {
1122 ElfReader& elf_reader = get_elf_reader();
1123 if (!elf_reader.Load(extinfo_)) {
1124 return false;
1125 }
1126
1127 si_->base = elf_reader.load_start();
1128 si_->size = elf_reader.load_size();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -08001129 si_->set_mapped_by_caller(elf_reader.is_mapped_by_caller());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001130 si_->load_bias = elf_reader.load_bias();
1131 si_->phnum = elf_reader.phdr_count();
1132 si_->phdr = elf_reader.loaded_phdr();
1133
1134 return true;
1135 }
1136
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001137 private:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001138 LoadTask(const char* name, soinfo* needed_by,
1139 std::unordered_map<const soinfo*, ElfReader>* readers_map)
1140 : name_(name), needed_by_(needed_by), si_(nullptr),
1141 fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map) {}
1142
1143 ~LoadTask() {
1144 if (fd_ != -1 && close_fd_) {
1145 close(fd_);
1146 }
1147 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001148
1149 const char* name_;
1150 soinfo* needed_by_;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001151 soinfo* si_;
1152 const android_dlextinfo* extinfo_;
1153 int fd_;
1154 bool close_fd_;
1155 off64_t file_offset_;
1156 std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001157
1158 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
1159};
1160
Ningsheng Jiane93be992014-09-16 15:22:10 +08001161LoadTask::deleter_t LoadTask::deleter;
1162
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001163template <typename T>
1164using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
1165
1166typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001167typedef linked_list_t<const char> StringLinkedList;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001168typedef std::vector<LoadTask*> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001169
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001170
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001171// This function walks down the tree of soinfo dependencies
1172// in breadth-first order and
1173// * calls action(soinfo* si) for each node, and
1174// * terminates walk if action returns false.
1175//
1176// walk_dependencies_tree returns false if walk was terminated
1177// by the action and true otherwise.
1178template<typename F>
1179static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001180 SoinfoLinkedList visit_list;
1181 SoinfoLinkedList visited;
1182
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001183 for (size_t i = 0; i < root_soinfos_size; ++i) {
1184 visit_list.push_back(root_soinfos[i]);
1185 }
1186
1187 soinfo* si;
1188 while ((si = visit_list.pop_front()) != nullptr) {
1189 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -07001190 continue;
1191 }
1192
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001193 if (!action(si)) {
1194 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001195 }
1196
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001197 visited.push_back(si);
1198
1199 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001200 visit_list.push_back(child);
1201 });
1202 }
1203
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001204 return true;
1205}
1206
1207
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001208static const ElfW(Sym)* dlsym_handle_lookup(soinfo* root, soinfo* skip_until,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001209 soinfo** found, SymbolName& symbol_name,
1210 const version_info* vi) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001211 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001212 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001213
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001214 walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) {
1215 if (skip_lookup) {
1216 skip_lookup = current_soinfo != skip_until;
1217 return true;
1218 }
1219
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001220 if (!current_soinfo->find_symbol_by_name(symbol_name, vi, &result)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001221 result = nullptr;
1222 return false;
1223 }
1224
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001225 if (result != nullptr) {
1226 *found = current_soinfo;
1227 return false;
1228 }
1229
1230 return true;
1231 });
1232
1233 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001234}
1235
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001236static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
1237 const char* name,
1238 const version_info* vi,
1239 soinfo** found,
1240 soinfo* caller,
1241 void* handle);
1242
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001243// This is used by dlsym(3). It performs symbol lookup only within the
1244// specified soinfo object and its dependencies in breadth first order.
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001245static const ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found,
1246 const char* name, const version_info* vi) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -07001247 // According to man dlopen(3) and posix docs in the case when si is handle
1248 // of the main executable we need to search not only in the executable and its
1249 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
1250 //
1251 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
1252 // libraries and they are loaded in breath-first (correct) order we can just execute
1253 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
1254 if (si == somain) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001255 return dlsym_linear_lookup(&g_default_namespace, name, vi, found, nullptr, RTLD_DEFAULT);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -07001256 }
1257
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001258 SymbolName symbol_name(name);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001259 return dlsym_handle_lookup(si, nullptr, found, symbol_name, vi);
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001260}
1261
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001262/* This is used by dlsym(3) to performs a global symbol lookup. If the
1263 start value is null (for RTLD_DEFAULT), the search starts at the
1264 beginning of the global solist. Otherwise the search starts at the
1265 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001266 */
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001267static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
1268 const char* name,
1269 const version_info* vi,
1270 soinfo** found,
1271 soinfo* caller,
1272 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001273 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001274
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001275 auto& soinfo_list = ns->soinfo_list();
1276 auto start = soinfo_list.begin();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001277
1278 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -07001279 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001280 return nullptr;
1281 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001282 auto it = soinfo_list.find(caller);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001283 CHECK (it != soinfo_list.end());
1284 start = ++it;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001285 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001286 }
1287
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001288 const ElfW(Sym)* s = nullptr;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001289 for (auto it = start, end = soinfo_list.end(); it != end; ++it) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001290 soinfo* si = *it;
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001291 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
1292 // if the library is opened by application with target api level <= 22
1293 // See http://b/21565766
1294 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 && si->get_target_sdk_version() > 22) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001295 continue;
1296 }
1297
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001298 if (!si->find_symbol_by_name(symbol_name, vi, &s)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001299 return nullptr;
1300 }
1301
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001302 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -08001303 *found = si;
1304 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -06001305 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001306 }
Matt Fischer1698d9e2009-12-31 12:17:56 -06001307
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001308 // If not found - use dlsym_handle_lookup for caller's
1309 // local_group unless it is part of the global group in which
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001310 // case we already did it.
1311 if (s == nullptr && caller != nullptr &&
1312 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001313 return dlsym_handle_lookup(caller->get_local_group_root(),
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001314 (handle == RTLD_NEXT) ? caller : nullptr, found, symbol_name, vi);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001315 }
1316
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001317 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001318 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
1319 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -08001320 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001321
Elliott Hughescade4c32012-12-20 14:42:14 -08001322 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001323}
1324
Kito Chengfa8c05d2013-03-12 14:58:06 +08001325soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001326 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001327 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001328 if (address >= si->base && address - si->base < si->size) {
1329 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001330 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001331 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001332 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001333}
1334
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001335ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
1336 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
1337}
1338
1339static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
1340 return sym->st_shndx != SHN_UNDEF &&
1341 soaddr >= sym->st_value &&
1342 soaddr < sym->st_value + sym->st_size;
1343}
1344
1345ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001346 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001347
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001348 for (size_t i = 0; i < gnu_nbucket_; ++i) {
1349 uint32_t n = gnu_bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001350
1351 if (n == 0) {
1352 continue;
1353 }
1354
1355 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001356 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001357 if (symbol_matches_soaddr(sym, soaddr)) {
1358 return sym;
1359 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001360 } while ((gnu_chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001361 }
1362
1363 return nullptr;
1364}
1365
1366ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001367 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001368
Kito Chengfa8c05d2013-03-12 14:58:06 +08001369 // Search the library's symbol table for any defined symbol which
1370 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001371 for (size_t i = 0; i < nchain_; ++i) {
1372 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001373 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001374 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001375 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001376 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001377
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001378 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001379}
1380
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001381class ZipArchiveCache {
1382 public:
1383 ZipArchiveCache() {}
1384 ~ZipArchiveCache();
1385
1386 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
1387 private:
1388 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
1389
1390 std::unordered_map<std::string, ZipArchiveHandle> cache_;
1391};
1392
1393bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
1394 std::string key(zip_path);
1395
1396 auto it = cache_.find(key);
1397 if (it != cache_.end()) {
1398 *handle = it->second;
1399 return true;
1400 }
1401
1402 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1403 if (fd == -1) {
1404 return false;
1405 }
1406
1407 if (OpenArchiveFd(fd, "", handle) != 0) {
1408 // invalid zip-file (?)
Yabin Cui722072d2016-03-21 17:10:12 -07001409 CloseArchive(handle);
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001410 close(fd);
1411 return false;
1412 }
1413
1414 cache_[key] = *handle;
1415 return true;
1416}
1417
1418ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -07001419 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001420 CloseArchive(it.second);
1421 }
1422}
1423
1424static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001425 const char* const input_path,
1426 off64_t* file_offset, std::string* realpath) {
1427 std::string normalized_path;
1428 if (!normalize_path(input_path, &normalized_path)) {
1429 return -1;
1430 }
1431
1432 const char* const path = normalized_path.c_str();
1433 TRACE("Trying zip file open from path '%s' -> normalized '%s'", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +00001434
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001435 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +00001436 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001437 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +00001438 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001439 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +00001440 if (separator == nullptr) {
1441 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -07001442 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001443
1444 char buf[512];
1445 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
1446 PRINT("Warning: ignoring very long library path: %s", path);
1447 return -1;
1448 }
1449
1450 buf[separator - path] = '\0';
1451
1452 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001453 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +00001454 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1455 if (fd == -1) {
1456 return -1;
1457 }
1458
1459 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001460 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001461 // invalid zip-file (?)
1462 close(fd);
1463 return -1;
1464 }
1465
Simon Baldwinaef71952015-01-16 13:22:54 +00001466 ZipEntry entry;
1467
Yusuke Sato56f40fb2015-06-25 14:56:07 -07001468 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001469 // Entry was not found.
1470 close(fd);
1471 return -1;
1472 }
1473
1474 // Check if it is properly stored
1475 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
1476 close(fd);
1477 return -1;
1478 }
1479
1480 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001481
1482 if (realpath_fd(fd, realpath)) {
1483 *realpath += separator;
1484 } else {
1485 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1486 normalized_path.c_str());
1487 *realpath = normalized_path;
1488 }
1489
Simon Baldwinaef71952015-01-16 13:22:54 +00001490 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001491}
1492
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001493static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
1494 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
1495 if (n < 0 || n >= static_cast<int>(buf_size)) {
1496 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1497 return false;
1498 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001499
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001500 return true;
1501}
1502
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001503static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1504 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001505 const std::vector<std::string>& paths,
1506 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001507 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001508 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001509 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001510 continue;
1511 }
1512
1513 int fd = -1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001514 if (strstr(buf, kZipFileSeparator) != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001515 fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath);
Simon Baldwinaef71952015-01-16 13:22:54 +00001516 }
1517
1518 if (fd == -1) {
1519 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1520 if (fd != -1) {
1521 *file_offset = 0;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001522 if (!realpath_fd(fd, realpath)) {
1523 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
1524 *realpath = buf;
1525 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001526 }
1527 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001528
1529 if (fd != -1) {
1530 return fd;
1531 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001532 }
1533
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001534 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001535}
1536
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001537static int open_library(android_namespace_t* ns,
1538 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001539 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001540 off64_t* file_offset, std::string* realpath) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001541 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001542
Elliott Hughes124fae92012-10-31 14:20:03 -07001543 // 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 -07001544 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001545 int fd = -1;
1546
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001547 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001548 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1549 }
1550
1551 if (fd == -1) {
1552 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001553 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001554 *file_offset = 0;
1555 if (!realpath_fd(fd, realpath)) {
1556 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name);
1557 *realpath = name;
1558 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001559 }
1560 }
1561
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001562 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001563 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001564
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001565 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1566 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 -07001567 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001568 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001569 // Check if the library is accessible
1570 if (fd != -1 && !ns->is_accessible(*realpath)) {
1571 fd = -1;
1572 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001573 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001574
Elliott Hughes124fae92012-10-31 14:20:03 -07001575 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001576 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001577 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001578
Elliott Hughes124fae92012-10-31 14:20:03 -07001579 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001580}
1581
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001582static const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
1583#if !defined(__LP64__)
1584 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001585 if (get_application_target_sdk_version() <= 22) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001586 const char* bname = basename(dt_needed);
1587 if (bname != dt_needed) {
1588 DL_WARN("'%s' library has invalid DT_NEEDED entry '%s'", sopath, dt_needed);
1589 }
1590
1591 return bname;
1592 }
1593#endif
1594 return dt_needed;
1595}
1596
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001597template<typename F>
1598static void for_each_dt_needed(const soinfo* si, F action) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001599 for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001600 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001601 action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
Dima Zavin2e855792009-05-20 18:28:09 -07001602 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001603 }
1604}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001605
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001606template<typename F>
1607static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
1608 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1609 if (d->d_tag == DT_NEEDED) {
1610 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1611 }
1612 }
1613}
1614
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001615static bool load_library(android_namespace_t* ns,
1616 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001617 LoadTaskList* load_tasks,
1618 int rtld_flags,
1619 const std::string& realpath) {
1620 off64_t file_offset = task->get_file_offset();
1621 const char* name = task->get_name();
1622 const android_dlextinfo* extinfo = task->get_extinfo();
1623
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001624 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001625 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001626 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001627 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001628 if (file_offset < 0) {
1629 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001630 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001631 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001632
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001633 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001634 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001635 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001636 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001637 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001638 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001639 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1640 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001641 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001642 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001643
1644 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001645 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1646 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001647 auto predicate = [&](soinfo* si) {
1648 return si->get_st_dev() != 0 &&
1649 si->get_st_ino() != 0 &&
1650 si->get_st_dev() == file_stat.st_dev &&
1651 si->get_st_ino() == file_stat.st_ino &&
1652 si->get_file_offset() == file_offset;
1653 };
1654
1655 soinfo* si = ns->soinfo_list().find_if(predicate);
1656
1657 // check public namespace
1658 if (si == nullptr) {
1659 si = g_public_namespace.find_if(predicate);
1660 if (si != nullptr) {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001661 ns->add_soinfo(si);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001662 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001663 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001664
1665 if (si != nullptr) {
1666 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
1667 "will return existing soinfo", name, si->get_realpath());
1668 task->set_soinfo(si);
1669 return true;
1670 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001671 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001672
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001673 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001674 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001675 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001676 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001677
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001678 if (!ns->is_accessible(realpath)) {
1679 // do not load libraries if they are not accessible for the specified namespace.
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001680 const char* needed_or_dlopened_by = task->get_needed_by() == nullptr ?
1681 "(unknown)" :
1682 task->get_needed_by()->get_realpath();
1683
1684 DL_ERR("library \"%s\" needed or dlopened by \"%s\" is not accessible for the namespace \"%s\"",
1685 name, needed_or_dlopened_by, ns->get_name());
1686
1687 PRINT("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the"
1688 " namespace: [name=\"%s\", ld_library_paths=\"%s\", default_library_paths=\"%s\","
1689 " permitted_paths=\"%s\"]",
1690 name, realpath.c_str(),
1691 needed_or_dlopened_by,
1692 ns->get_name(),
1693 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
1694 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
1695 android::base::Join(ns->get_permitted_paths(), ':').c_str());
1696
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001697 return false;
1698 }
1699
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001700 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001701 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001702 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001703 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001704
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001705 task->set_soinfo(si);
1706
1707 // Read the ELF header and some of the segments.
1708 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001709 soinfo_free(si);
1710 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001711 return false;
1712 }
1713
1714 // find and set DT_RUNPATH and dt_soname
1715 // Note that these field values are temporary and are
1716 // going to be overwritten on soinfo::prelink_image
1717 // with values from PT_LOAD segments.
1718 const ElfReader& elf_reader = task->get_elf_reader();
1719 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1720 if (d->d_tag == DT_RUNPATH) {
1721 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1722 }
1723 if (d->d_tag == DT_SONAME) {
1724 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1725 }
1726 }
1727
1728 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
1729 load_tasks->push_back(LoadTask::create(name, si, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001730 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001731
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001732 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001733}
1734
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001735static bool load_library(android_namespace_t* ns,
1736 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001737 ZipArchiveCache* zip_archive_cache,
1738 LoadTaskList* load_tasks,
1739 int rtld_flags) {
1740 const char* name = task->get_name();
1741 soinfo* needed_by = task->get_needed_by();
1742 const android_dlextinfo* extinfo = task->get_extinfo();
1743
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001744 off64_t file_offset;
1745 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001746 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001747 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001748 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1749 file_offset = extinfo->library_fd_offset;
1750 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001751
1752 if (!realpath_fd(extinfo->library_fd, &realpath)) {
1753 PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1754 "Will use given name.", name);
1755 realpath = name;
1756 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001757
1758 task->set_fd(extinfo->library_fd, false);
1759 task->set_file_offset(file_offset);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001760 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001761 }
1762
1763 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001764 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001765 if (fd == -1) {
1766 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001767 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001768 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001769
1770 task->set_fd(fd, true);
1771 task->set_file_offset(file_offset);
1772
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001773 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001774}
1775
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001776// Returns true if library was found and false in 2 cases
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001777// 1. (for default namespace only) The library was found but loaded under different
1778// target_sdk_version (*candidate != nullptr)
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001779// 2. The library was not found by soname (*candidate is nullptr)
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001780static bool find_loaded_library_by_soname(android_namespace_t* ns,
1781 const char* name, soinfo** candidate) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001782 *candidate = nullptr;
1783
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001784 // Ignore filename with path.
1785 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001786 return false;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001787 }
1788
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001789 uint32_t target_sdk_version = get_application_target_sdk_version();
1790
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001791 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001792 const char* soname = si->get_soname();
1793 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001794 // If the library was opened under different target sdk version
1795 // skip this step and try to reopen it. The exceptions are
1796 // "libdl.so" and global group. There is no point in skipping
1797 // them because relocation process is going to use them
1798 // in any case.
1799 bool is_libdl = si == solist;
1800 if (is_libdl || (si->get_dt_flags_1() & DF_1_GLOBAL) != 0 ||
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001801 !si->is_linked() || si->get_target_sdk_version() == target_sdk_version ||
1802 ns != &g_default_namespace) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001803 *candidate = si;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001804 return false;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001805 } else if (*candidate == nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001806 // for the different sdk version in the default namespace
1807 // remember the first library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001808 *candidate = si;
1809 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001810 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001811
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001812 return true;
1813 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001814}
1815
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001816static bool find_library_internal(android_namespace_t* ns,
1817 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001818 ZipArchiveCache* zip_archive_cache,
1819 LoadTaskList* load_tasks,
1820 int rtld_flags) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001821 soinfo* candidate;
1822
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001823 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001824 task->set_soinfo(candidate);
1825 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001826 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001827
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001828 if (ns != &g_default_namespace) {
1829 // check public namespace
1830 candidate = g_public_namespace.find_if([&](soinfo* si) {
1831 return strcmp(task->get_name(), si->get_soname()) == 0;
1832 });
1833
1834 if (candidate != nullptr) {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001835 ns->add_soinfo(candidate);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001836 task->set_soinfo(candidate);
1837 return true;
1838 }
1839 }
1840
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001841 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001842 // of this fact is done by load_library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001843 TRACE("[ '%s' find_loaded_library_by_soname returned false (*candidate=%s@%p). Trying harder...]",
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001844 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001845
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001846 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001847 return true;
1848 } else {
1849 // In case we were unable to load the library but there
1850 // is a candidate loaded under the same soname but different
1851 // sdk level - return it anyways.
1852 if (candidate != nullptr) {
1853 task->set_soinfo(candidate);
1854 return true;
1855 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001856 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001857
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001858 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001859}
1860
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001861static void soinfo_unload(soinfo* si);
1862
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001863// TODO: this is slightly unusual way to construct
1864// the global group for relocation. Not every RTLD_GLOBAL
1865// library is included in this group for backwards-compatibility
1866// reasons.
1867//
1868// This group consists of the main executable, LD_PRELOADs
1869// and libraries with the DF_1_GLOBAL flag set.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001870static soinfo::soinfo_list_t make_global_group(android_namespace_t* ns) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001871 soinfo::soinfo_list_t global_group;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001872 ns->soinfo_list().for_each([&](soinfo* si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001873 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1874 global_group.push_back(si);
1875 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001876 });
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001877
1878 return global_group;
1879}
1880
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001881static void shuffle(std::vector<LoadTask*>* v) {
1882 for (size_t i = 0, size = v->size(); i < size; ++i) {
1883 size_t n = size - i;
1884 size_t r = arc4random_uniform(n);
1885 std::swap((*v)[n-1], (*v)[r]);
1886 }
1887}
1888
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001889// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1890// not their transitive dependencies) as children of the start_with library.
1891// This is false when find_libraries is called for dlopen(), when newly loaded
1892// libraries must form a disjoint tree.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001893static bool find_libraries(android_namespace_t* ns,
1894 soinfo* start_with,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001895 const char* const library_names[],
1896 size_t library_names_count, soinfo* soinfos[],
1897 std::vector<soinfo*>* ld_preloads,
1898 size_t ld_preloads_count, int rtld_flags,
1899 const android_dlextinfo* extinfo,
1900 bool add_as_children) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001901 // Step 0: prepare.
1902 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001903 std::unordered_map<const soinfo*, ElfReader> readers_map;
1904
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001905 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001906 const char* name = library_names[i];
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001907 load_tasks.push_back(LoadTask::create(name, start_with, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001908 }
1909
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001910 // Construct global_group.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001911 soinfo::soinfo_list_t global_group = make_global_group(ns);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001912
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001913 // If soinfos array is null allocate one on stack.
1914 // The array is needed in case of failure; for example
1915 // when library_names[] = {libone.so, libtwo.so} and libone.so
1916 // is loaded correctly but libtwo.so failed for some reason.
1917 // In this case libone.so should be unloaded on return.
1918 // See also implementation of failure_guard below.
1919
1920 if (soinfos == nullptr) {
1921 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1922 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1923 memset(soinfos, 0, soinfos_size);
1924 }
1925
1926 // list of libraries to link - see step 2.
1927 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001928
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001929 auto scope_guard = make_scope_guard([&]() {
1930 for (LoadTask* t : load_tasks) {
1931 LoadTask::deleter(t);
1932 }
1933 });
1934
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001935 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001936 // Housekeeping
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001937 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001938 soinfo_unload(soinfos[i]);
1939 }
1940 });
1941
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001942 ZipArchiveCache zip_archive_cache;
1943
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001944 // Step 1: expand the list of load_tasks to include
1945 // all DT_NEEDED libraries (do not load them just yet)
1946 for (size_t i = 0; i<load_tasks.size(); ++i) {
1947 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07001948 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001949
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001950 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001951 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001952
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001953 if(!find_library_internal(ns, task, &zip_archive_cache, &load_tasks, rtld_flags)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001954 return false;
1955 }
1956
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001957 soinfo* si = task->get_soinfo();
1958
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001959 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001960 needed_by->add_child(si);
1961 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001962
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001963 if (si->is_linked()) {
1964 si->increment_ref_count();
1965 }
1966
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001967 // When ld_preloads is not null, the first
1968 // ld_preloads_count libs are in fact ld_preloads.
1969 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001970 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001971 }
1972
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001973 if (soinfos_count < library_names_count) {
1974 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001975 }
1976 }
1977
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001978 // Step 2: Load libraries in random order (see b/24047022)
1979 LoadTaskList load_list;
1980 for (auto&& task : load_tasks) {
1981 soinfo* si = task->get_soinfo();
1982 auto pred = [&](const LoadTask* t) {
1983 return t->get_soinfo() == si;
1984 };
1985
1986 if (!si->is_linked() &&
1987 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
1988 load_list.push_back(task);
1989 }
1990 }
1991 shuffle(&load_list);
1992
1993 for (auto&& task : load_list) {
1994 if (!task->load()) {
1995 return false;
1996 }
1997 }
1998
1999 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
2000 for (auto&& task : load_tasks) {
2001 soinfo* si = task->get_soinfo();
2002 if (!si->is_linked() && !si->prelink_image()) {
2003 return false;
2004 }
2005 }
2006
2007 // Step 4: Add LD_PRELOADed libraries to the global group for
2008 // future runs. There is no need to explicitly add them to
2009 // the global group for this run because they are going to
2010 // appear in the local group in the correct order.
2011 if (ld_preloads != nullptr) {
2012 for (auto&& si : *ld_preloads) {
2013 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2014 }
2015 }
2016
2017
2018 // Step 5: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002019 soinfo::soinfo_list_t local_group;
2020 walk_dependencies_tree(
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002021 (start_with != nullptr && add_as_children) ? &start_with : soinfos,
2022 (start_with != nullptr && add_as_children) ? 1 : soinfos_count,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002023 [&] (soinfo* si) {
2024 local_group.push_back(si);
2025 return true;
2026 });
2027
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002028 // We need to increment ref_count in case
2029 // the root of the local group was not linked.
2030 bool was_local_group_root_linked = local_group.front()->is_linked();
2031
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002032 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002033 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002034 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002035 return false;
2036 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002037 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002038 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002039
2040 return true;
2041 });
2042
2043 if (linked) {
2044 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002045 }
2046
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002047 if (!was_local_group_root_linked) {
2048 local_group.front()->increment_ref_count();
2049 }
2050
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002051 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002052}
2053
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002054static soinfo* find_library(android_namespace_t* ns,
2055 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002056 const android_dlextinfo* extinfo,
2057 soinfo* needed_by) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002058 soinfo* si;
2059
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002060 if (name == nullptr) {
2061 si = somain;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002062 } else if (!find_libraries(ns, needed_by, &name, 1, &si, nullptr, 0, rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002063 extinfo, /* add_as_children */ false)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002064 return nullptr;
2065 }
2066
Elliott Hughesd23736e2012-11-01 15:16:56 -07002067 return si;
2068}
Elliott Hughesbedfe382012-08-14 14:07:59 -07002069
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002070static void soinfo_unload(soinfo* root) {
2071 // Note that the library can be loaded but not linked;
2072 // in which case there is no root but we still need
2073 // to walk the tree and unload soinfos involved.
2074 //
2075 // This happens on unsuccessful dlopen, when one of
2076 // the DT_NEEDED libraries could not be linked/found.
2077 if (root->is_linked()) {
2078 root = root->get_local_group_root();
2079 }
2080
2081 if (!root->can_unload()) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002082 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002083 return;
2084 }
2085
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002086 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002087
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002088 if (ref_count == 0) {
2089 soinfo::soinfo_list_t local_unload_list;
2090 soinfo::soinfo_list_t external_unload_list;
2091 soinfo::soinfo_list_t depth_first_list;
2092 depth_first_list.push_back(root);
2093 soinfo* si = nullptr;
2094
2095 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002096 if (local_unload_list.contains(si)) {
2097 continue;
2098 }
2099
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002100 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002101
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002102 if (si->has_min_version(0)) {
2103 soinfo* child = nullptr;
2104 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002105 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
2106 child->get_realpath(), child);
2107
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002108 if (local_unload_list.contains(child)) {
2109 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002110 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002111 external_unload_list.push_back(child);
2112 } else {
2113 depth_first_list.push_front(child);
2114 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002115 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002116 } else {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07002117#if !defined(__work_around_b_24465209__)
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002118 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002119#else
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002120 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002121 for_each_dt_needed(si, [&] (const char* library_name) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002122 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002123 si->get_realpath(), library_name);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002124
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002125 soinfo* needed = find_library(si->get_namespace(),
2126 library_name, RTLD_NOLOAD, nullptr, nullptr);
2127
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002128 if (needed != nullptr) {
2129 // Not found: for example if symlink was deleted between dlopen and dlclose
2130 // Since we cannot really handle errors at this point - print and continue.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002131 PRINT("warning: couldn't find %s needed by %s on unload.",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002132 library_name, si->get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002133 return;
2134 } else if (local_unload_list.contains(needed)) {
2135 // already visited
2136 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002137 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002138 // external group
2139 external_unload_list.push_back(needed);
2140 } else {
2141 // local group
2142 depth_first_list.push_front(needed);
2143 }
2144 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002145#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002146 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002147 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002148
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002149 local_unload_list.for_each([](soinfo* si) {
2150 si->call_destructors();
2151 });
2152
2153 while ((si = local_unload_list.pop_front()) != nullptr) {
2154 notify_gdb_of_unload(si);
2155 soinfo_free(si);
2156 }
2157
2158 while ((si = external_unload_list.pop_front()) != nullptr) {
2159 soinfo_unload(si);
2160 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002161 } else {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002162 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
2163 root->get_realpath(), ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08002164 }
2165}
2166
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002167static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
2168 if (sym_ver == nullptr) {
2169 return sym_name;
2170 }
2171
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002172 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002173}
2174
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002175void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002176 // Use basic string manipulation calls to avoid snprintf.
2177 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
2178 // When debug malloc is enabled, this call returns 0. This in turn causes
2179 // snprintf to do nothing, which causes libraries to fail to load.
2180 // See b/17302493 for further details.
2181 // Once the above bug is fixed, this code can be modified to use
2182 // snprintf again.
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002183 size_t required_len = 0;
2184 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2185 required_len += strlen(g_default_ld_paths[i]) + 1;
2186 }
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002187 if (buffer_size < required_len) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002188 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
2189 "buffer len %zu, required len %zu", buffer_size, required_len);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002190 }
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002191 char* end = buffer;
2192 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2193 if (i > 0) *end++ = ':';
2194 end = stpcpy(end, g_default_ld_paths[i]);
2195 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002196}
2197
Elliott Hughescade4c32012-12-20 14:42:14 -08002198void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08002199 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08002200}
2201
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002202void* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002203 void* caller_addr) {
2204 soinfo* const caller = find_containing_library(caller_addr);
2205
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002206 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08002207 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002208 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08002209 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002210
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002211 android_namespace_t* ns = caller != nullptr ? caller->get_namespace() : g_anonymous_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002212
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002213 if (extinfo != nullptr) {
2214 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
2215 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
2216 return nullptr;
2217 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002218
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002219 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07002220 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002221 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
2222 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002223 return nullptr;
2224 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002225
2226 if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 &&
2227 (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) {
2228 DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not "
2229 "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT");
2230 return nullptr;
2231 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002232
2233 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
2234 if (extinfo->library_namespace == nullptr) {
2235 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
2236 return nullptr;
2237 }
2238 ns = extinfo->library_namespace;
2239 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00002240 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002241
2242 ProtectedDataGuard guard;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002243 soinfo* si = find_library(ns, name, flags, extinfo, caller);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002244 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002245 si->call_constructors();
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002246 return si->to_handle();
Elliott Hughesd23736e2012-11-01 15:16:56 -07002247 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002248
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002249 return nullptr;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002250}
2251
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002252int do_dladdr(const void* addr, Dl_info* info) {
2253 // Determine if this address can be found in any library currently mapped.
2254 soinfo* si = find_containing_library(addr);
2255 if (si == nullptr) {
2256 return 0;
2257 }
2258
2259 memset(info, 0, sizeof(Dl_info));
2260
2261 info->dli_fname = si->get_realpath();
2262 // Address at which the shared object is loaded.
2263 info->dli_fbase = reinterpret_cast<void*>(si->base);
2264
2265 // Determine if any symbol in the library contains the specified address.
2266 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
2267 if (sym != nullptr) {
2268 info->dli_sname = si->get_string(sym->st_name);
2269 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
2270 }
2271
2272 return 1;
2273}
2274
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002275static soinfo* soinfo_from_handle(void* handle) {
2276 if ((reinterpret_cast<uintptr_t>(handle) & 1) != 0) {
2277 auto it = g_soinfo_handles_map.find(reinterpret_cast<uintptr_t>(handle));
2278 if (it == g_soinfo_handles_map.end()) {
2279 return nullptr;
2280 } else {
2281 return it->second;
2282 }
2283 }
2284
2285 return static_cast<soinfo*>(handle);
2286}
2287
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002288bool do_dlsym(void* handle, const char* sym_name, const char* sym_ver,
2289 void* caller_addr, void** symbol) {
2290#if !defined(__LP64__)
2291 if (handle == nullptr) {
2292 DL_ERR("dlsym failed: library handle is null");
2293 return false;
2294 }
2295#endif
2296
2297 if (sym_name == nullptr) {
2298 DL_ERR("dlsym failed: symbol name is null");
2299 return false;
2300 }
2301
2302 soinfo* found = nullptr;
2303 const ElfW(Sym)* sym = nullptr;
2304 soinfo* caller = find_containing_library(caller_addr);
2305 android_namespace_t* ns = caller != nullptr ? caller->get_namespace() : g_anonymous_namespace;
2306
2307 version_info vi_instance;
2308 version_info* vi = nullptr;
2309
2310 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002311 vi_instance.name = sym_ver;
2312 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002313 vi = &vi_instance;
2314 }
2315
2316 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2317 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2318 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002319 soinfo* si = soinfo_from_handle(handle);
2320 if (si == nullptr) {
2321 DL_ERR("dlsym failed: invalid handle: %p", handle);
2322 return false;
2323 }
2324 sym = dlsym_handle_lookup(si, &found, sym_name, vi);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002325 }
2326
2327 if (sym != nullptr) {
2328 uint32_t bind = ELF_ST_BIND(sym->st_info);
2329
2330 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2331 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
2332 return true;
2333 }
2334
2335 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2336 return false;
2337 }
2338
2339 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2340 return false;
2341}
2342
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002343int do_dlclose(void* handle) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002344 ProtectedDataGuard guard;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002345 soinfo* si = soinfo_from_handle(handle);
2346 if (si == nullptr) {
2347 DL_ERR("invalid handle: %p", handle);
2348 return -1;
2349 }
2350
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002351 soinfo_unload(si);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002352 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002353}
2354
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002355bool init_namespaces(const char* public_ns_sonames, const char* anon_ns_library_path) {
2356 CHECK(public_ns_sonames != nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002357 if (g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002358 DL_ERR("public namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002359 return false;
2360 }
2361
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002362 std::vector<std::string> sonames = android::base::Split(public_ns_sonames, ":");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002363
2364 ProtectedDataGuard guard;
2365
2366 auto failure_guard = make_scope_guard([&]() {
2367 g_public_namespace.clear();
2368 });
2369
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002370 for (const auto& soname : sonames) {
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -08002371 soinfo* candidate = nullptr;
2372
2373 find_loaded_library_by_soname(&g_default_namespace, soname.c_str(), &candidate);
2374
2375 if (candidate == nullptr) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002376 DL_ERR("error initializing public namespace: \"%s\" was not found"
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002377 " in the default namespace", soname.c_str());
2378 return false;
2379 }
2380
2381 candidate->set_nodelete();
2382 g_public_namespace.push_back(candidate);
2383 }
2384
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002385 g_public_namespace_initialized = true;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002386
2387 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002388 // When the caller is nullptr - create_namespace will take global group
2389 // from the anonymous namespace, which is fine because anonymous namespace
2390 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002391 android_namespace_t* anon_ns =
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002392 create_namespace(nullptr, "(anonymous)", nullptr, anon_ns_library_path,
2393 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002394
2395 if (anon_ns == nullptr) {
2396 g_public_namespace_initialized = false;
2397 return false;
2398 }
2399 g_anonymous_namespace = anon_ns;
2400 failure_guard.disable();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002401 return true;
2402}
2403
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002404android_namespace_t* create_namespace(const void* caller_addr,
2405 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002406 const char* ld_library_path,
2407 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002408 uint64_t type,
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002409 const char* permitted_when_isolated_path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002410 if (!g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002411 DL_ERR("cannot create namespace: public namespace is not initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002412 return nullptr;
2413 }
2414
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002415 soinfo* caller_soinfo = find_containing_library(caller_addr);
2416
2417 android_namespace_t* caller_ns = caller_soinfo != nullptr ?
2418 caller_soinfo->get_namespace() :
2419 g_anonymous_namespace;
2420
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002421 ProtectedDataGuard guard;
2422 std::vector<std::string> ld_library_paths;
2423 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002424 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002425
2426 parse_path(ld_library_path, ":", &ld_library_paths);
2427 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002428 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002429
2430 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2431 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002432 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002433 ns->set_ld_library_paths(std::move(ld_library_paths));
2434 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002435 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002436
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002437 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
2438 // If shared - clone the caller namespace
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002439 ns->add_soinfos(caller_ns->soinfo_list());
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002440 } else {
2441 // If not shared - copy only the global group
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002442 ns->add_soinfos(make_global_group(caller_ns));
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002443 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002444
2445 return ns;
2446}
2447
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002448static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
2449 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2450 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2451 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002452 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2453 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002454
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002455 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002456}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002457
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002458const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2459 if (source_symver < 2 ||
2460 source_symver >= version_infos.size() ||
2461 version_infos[source_symver].name == nullptr) {
2462 return nullptr;
2463 }
2464
2465 return &version_infos[source_symver];
2466}
2467
2468void VersionTracker::add_version_info(size_t source_index,
2469 ElfW(Word) elf_hash,
2470 const char* ver_name,
2471 const soinfo* target_si) {
2472 if (source_index >= version_infos.size()) {
2473 version_infos.resize(source_index+1);
2474 }
2475
2476 version_infos[source_index].elf_hash = elf_hash;
2477 version_infos[source_index].name = ver_name;
2478 version_infos[source_index].target_si = target_si;
2479}
2480
2481bool VersionTracker::init_verneed(const soinfo* si_from) {
2482 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2483
2484 if (verneed_ptr == 0) {
2485 return true;
2486 }
2487
2488 size_t verneed_cnt = si_from->get_verneed_cnt();
2489
2490 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2491 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2492 size_t vernaux_offset = offset + verneed->vn_aux;
2493 offset += verneed->vn_next;
2494
2495 if (verneed->vn_version != 1) {
2496 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2497 return false;
2498 }
2499
2500 const char* target_soname = si_from->get_string(verneed->vn_file);
2501 // find it in dependencies
2502 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002503 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002504 });
2505
2506 if (target_si == nullptr) {
2507 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002508 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002509 return false;
2510 }
2511
2512 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2513 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2514 vernaux_offset += vernaux->vna_next;
2515
2516 const ElfW(Word) elf_hash = vernaux->vna_hash;
2517 const char* ver_name = si_from->get_string(vernaux->vna_name);
2518 ElfW(Half) source_index = vernaux->vna_other;
2519
2520 add_version_info(source_index, elf_hash, ver_name, target_si);
2521 }
2522 }
2523
2524 return true;
2525}
2526
2527bool VersionTracker::init_verdef(const soinfo* si_from) {
2528 return for_each_verdef(si_from,
2529 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2530 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2531 si_from->get_string(verdaux->vda_name), si_from);
2532 return false;
2533 }
2534 );
2535}
2536
2537bool VersionTracker::init(const soinfo* si_from) {
2538 if (!si_from->has_min_version(2)) {
2539 return true;
2540 }
2541
2542 return init_verneed(si_from) && init_verdef(si_from);
2543}
2544
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002545bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2546 const char* sym_name, const version_info** vi) {
2547 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2548 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2549
2550 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2551 *vi = version_tracker.get_version_info(sym_ver);
2552
2553 if (*vi == nullptr) {
2554 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002555 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002556 return false;
2557 }
2558 } else {
2559 // there is no version info
2560 *vi = nullptr;
2561 }
2562
2563 return true;
2564}
2565
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002566#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002567#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002568static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2569 return rela->r_addend;
2570}
2571#else
2572static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002573 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2574 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002575 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2576 }
2577 return 0;
2578}
2579#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002580
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002581template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002582bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2583 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002584 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2585 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002586 if (rel == nullptr) {
2587 return false;
2588 }
2589
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002590 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2591 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2592
2593 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002594 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002595 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002596 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002597
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002598 DEBUG("Processing '%s' relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002599 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002600 continue;
2601 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002602
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002603 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002604 soinfo* lsi = nullptr;
2605
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002606 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002607 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002608 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002609
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002610 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2611 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002612 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002613
2614 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2615 return false;
2616 }
2617
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002618 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002619 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002620 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002621 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002622 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002623 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002624 }
2625
2626 /* IHI0044C AAELF 4.5.1.1:
2627
2628 Libraries are not searched to resolve weak references.
2629 It is not an error for a weak reference to remain unsatisfied.
2630
2631 During linking, the value of an undefined weak reference is:
2632 - Zero if the relocation type is absolute
2633 - The address of the place if the relocation is pc-relative
2634 - The address of nominal base address if the relocation
2635 type is base-relative.
2636 */
2637
2638 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002639 case R_GENERIC_JUMP_SLOT:
2640 case R_GENERIC_GLOB_DAT:
2641 case R_GENERIC_RELATIVE:
2642 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002643#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002644 case R_AARCH64_ABS64:
2645 case R_AARCH64_ABS32:
2646 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002647#elif defined(__x86_64__)
2648 case R_X86_64_32:
2649 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002650#elif defined(__arm__)
2651 case R_ARM_ABS32:
2652#elif defined(__i386__)
2653 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002654#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002655 /*
2656 * The sym_addr was initialized to be zero above, or the relocation
2657 * code below does not care about value of sym_addr.
2658 * No need to do anything.
2659 */
2660 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002661#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002662 case R_X86_64_PC32:
2663 sym_addr = reloc;
2664 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002665#elif defined(__i386__)
2666 case R_386_PC32:
2667 sym_addr = reloc;
2668 break;
2669#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002670 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002671 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002672 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002673 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002674 } else { // We got a definition.
2675#if !defined(__LP64__)
2676 // When relocating dso with text_relocation .text segment is
2677 // not executable. We need to restore elf flags before resolving
2678 // STT_GNU_IFUNC symbol.
2679 bool protect_segments = has_text_relocations &&
2680 lsi == this &&
2681 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2682 if (protect_segments) {
2683 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2684 DL_ERR("can't protect segments for \"%s\": %s",
2685 get_realpath(), strerror(errno));
2686 return false;
2687 }
2688 }
2689#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002690 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002691#if !defined(__LP64__)
2692 if (protect_segments) {
2693 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2694 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2695 get_realpath(), strerror(errno));
2696 return false;
2697 }
2698 }
2699#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002700 }
2701 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002702 }
2703
2704 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002705 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002706 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002707 MARK(rel->r_offset);
2708 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2709 reinterpret_cast<void*>(reloc),
2710 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2711
2712 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002713 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002714 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002715 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002716 MARK(rel->r_offset);
2717 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2718 reinterpret_cast<void*>(reloc),
2719 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2720 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002721 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002722 case R_GENERIC_RELATIVE:
2723 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002724 MARK(rel->r_offset);
2725 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2726 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002727 reinterpret_cast<void*>(load_bias + addend));
2728 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002729 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002730 case R_GENERIC_IRELATIVE:
2731 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002732 MARK(rel->r_offset);
2733 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2734 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002735 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002736 {
2737#if !defined(__LP64__)
2738 // When relocating dso with text_relocation .text segment is
2739 // not executable. We need to restore elf flags for this
2740 // particular call.
2741 if (has_text_relocations) {
2742 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2743 DL_ERR("can't protect segments for \"%s\": %s",
2744 get_realpath(), strerror(errno));
2745 return false;
2746 }
2747 }
2748#endif
2749 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2750#if !defined(__LP64__)
2751 // Unprotect it afterwards...
2752 if (has_text_relocations) {
2753 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2754 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2755 get_realpath(), strerror(errno));
2756 return false;
2757 }
2758 }
2759#endif
2760 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2761 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002762 break;
2763
2764#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002765 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002766 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002767 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002768 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002769 reloc, sym_addr + addend, sym_name);
2770 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002771 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002772 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002773 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002774 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002775 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002776 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002777 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002778 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2779 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002780 if ((min_value <= (sym_addr + addend)) &&
2781 ((sym_addr + addend) <= max_value)) {
2782 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002783 } else {
2784 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002785 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002786 return false;
2787 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002788 }
2789 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002790 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002791 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002792 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002793 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002794 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002795 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002796 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2797 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002798 if ((min_value <= (sym_addr + addend)) &&
2799 ((sym_addr + addend) <= max_value)) {
2800 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002801 } else {
2802 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002803 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002804 return false;
2805 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002806 }
2807 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002808 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002809 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002810 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002811 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002812 reloc, sym_addr + addend, rel->r_offset, sym_name);
2813 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002814 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002815 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002816 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002817 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002818 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002819 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002820 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002821 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2822 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002823 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2824 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2825 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002826 } else {
2827 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002828 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002829 return false;
2830 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002831 }
2832 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002833 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002834 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002835 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002836 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002837 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002838 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002839 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2840 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002841 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2842 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2843 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002844 } else {
2845 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002846 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002847 return false;
2848 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002849 }
2850 break;
2851
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002852 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002853 /*
2854 * ET_EXEC is not supported so this should not happen.
2855 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002856 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002857 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002858 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002859 * R_AARCH64_COPY may only appear in executable objects where e_type is
2860 * set to ET_EXEC.
2861 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002862 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002863 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002864 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002865 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002866 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002867 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002868 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002869 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002870 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002871 break;
2872#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002873 case R_X86_64_32:
2874 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002875 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002876 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2877 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002878 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002879 break;
2880 case R_X86_64_64:
2881 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002882 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002883 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2884 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002885 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002886 break;
2887 case R_X86_64_PC32:
2888 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002889 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002890 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2891 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2892 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002893 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002894 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002895#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002896 case R_ARM_ABS32:
2897 count_relocation(kRelocAbsolute);
2898 MARK(rel->r_offset);
2899 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2900 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2901 break;
2902 case R_ARM_REL32:
2903 count_relocation(kRelocRelative);
2904 MARK(rel->r_offset);
2905 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2906 reloc, sym_addr, rel->r_offset, sym_name);
2907 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2908 break;
2909 case R_ARM_COPY:
2910 /*
2911 * ET_EXEC is not supported so this should not happen.
2912 *
2913 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2914 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002915 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002916 * R_ARM_COPY may only appear in executable objects where e_type is
2917 * set to ET_EXEC.
2918 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002919 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002920 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002921#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002922 case R_386_32:
2923 count_relocation(kRelocRelative);
2924 MARK(rel->r_offset);
2925 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2926 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2927 break;
2928 case R_386_PC32:
2929 count_relocation(kRelocRelative);
2930 MARK(rel->r_offset);
2931 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2932 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2933 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2934 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002935#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002936 default:
2937 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002938 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002939 }
2940 }
2941 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002942}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002943#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002944
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002945void soinfo::call_array(const char* array_name __unused, linker_function_t* functions,
2946 size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002947 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002948 return;
2949 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02002950
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002951 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, get_realpath());
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002952
2953 int begin = reverse ? (count - 1) : 0;
2954 int end = reverse ? -1 : count;
2955 int step = reverse ? -1 : 1;
2956
2957 for (int i = begin; i != end; i += step) {
2958 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002959 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002960 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02002961
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002962 TRACE("[ Done calling %s for '%s' ]", array_name, get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002963}
2964
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002965void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002966 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002967 return;
2968 }
2969
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002970 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Elliott Hughesd23736e2012-11-01 15:16:56 -07002971 function();
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002972 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04002973}
2974
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002975void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002976 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
2977 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002978 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002979}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002980
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002981void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002982 if (constructors_called) {
2983 return;
2984 }
Jesse Hallf5d16932012-01-30 15:39:57 -08002985
Elliott Hughesd23736e2012-11-01 15:16:56 -07002986 // We set constructors_called before actually calling the constructors, otherwise it doesn't
2987 // protect against recursive constructor calls. One simple example of constructor recursion
2988 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
2989 // 1. The program depends on libc, so libc's constructor is called here.
2990 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
2991 // 3. dlopen() calls the constructors on the newly created
2992 // soinfo for libc_malloc_debug_leak.so.
2993 // 4. The debug .so depends on libc, so CallConstructors is
2994 // called again with the libc soinfo. If it doesn't trigger the early-
2995 // out above, the libc constructor will be called again (recursively!).
2996 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002997
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002998 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002999 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07003000 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003001 get_realpath(), preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07003002 }
3003
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003004 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003005 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003006 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003007
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003008 TRACE("\"%s\": calling constructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003009
3010 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003011 call_function("DT_INIT", init_func_);
3012 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003013}
David 'Digit' Turner82156792009-05-18 14:37:41 +02003014
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003015void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003016 if (!constructors_called) {
3017 return;
3018 }
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003019 TRACE("\"%s\": calling destructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003020
3021 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003022 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003023
3024 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003025 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07003026
3027 // This is needed on second call to dlopen
3028 // after library has been unloaded with RTLD_NODELETE
3029 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003030}
3031
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003032void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003033 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003034 child->parents_.push_back(this);
3035 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003036 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003037}
3038
3039void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003040 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003041 return;
3042 }
3043
3044 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003045 children_.for_each([&] (soinfo* child) {
3046 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003047 return parent == this;
3048 });
3049 });
3050
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003051 parents_.for_each([&] (soinfo* parent) {
3052 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003053 return child == this;
3054 });
3055 });
3056
3057 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003058 parents_.clear();
3059 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003060}
3061
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003062dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003063 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003064 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003065 }
3066
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003067 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003068};
3069
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003070ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003071 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003072 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003073 }
3074
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003075 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003076}
3077
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003078off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003079 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003080 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003081 }
3082
3083 return 0;
3084}
3085
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003086uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07003087 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003088 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07003089 }
3090
3091 return 0;
3092}
3093
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003094uint32_t soinfo::get_dt_flags_1() const {
3095 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003096 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003097 }
3098
3099 return 0;
3100}
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003101
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003102void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
3103 if (has_min_version(1)) {
3104 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003105 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003106 }
3107
3108 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003109 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003110 }
3111
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003112 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003113 }
3114}
3115
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003116void soinfo::set_nodelete() {
3117 rtld_flags_ |= RTLD_NODELETE;
3118}
3119
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003120const char* soinfo::get_realpath() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003121#if defined(__work_around_b_24465209__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003122 if (has_min_version(2)) {
3123 return realpath_.c_str();
3124 } else {
3125 return old_name_;
3126 }
3127#else
3128 return realpath_.c_str();
3129#endif
3130}
3131
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003132void soinfo::set_soname(const char* soname) {
3133#if defined(__work_around_b_24465209__)
3134 if (has_min_version(2)) {
3135 soname_ = soname;
3136 }
3137 strlcpy(old_name_, soname_, sizeof(old_name_));
3138#else
3139 soname_ = soname;
3140#endif
3141}
3142
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003143const char* soinfo::get_soname() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003144#if defined(__work_around_b_24465209__)
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003145 if (has_min_version(2)) {
3146 return soname_;
3147 } else {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003148 return old_name_;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003149 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003150#else
3151 return soname_;
3152#endif
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003153}
3154
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003155// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003156// 'this->flags' does not have FLAG_NEW_SOINFO set.
3157static soinfo::soinfo_list_t g_empty_list;
3158
3159soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003160 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003161 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003162 }
3163
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003164 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003165}
3166
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003167const soinfo::soinfo_list_t& soinfo::get_children() const {
3168 if (has_min_version(0)) {
3169 return children_;
3170 }
3171
3172 return g_empty_list;
3173}
3174
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003175soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003176 if (has_min_version(0)) {
3177 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003178 }
3179
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003180 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003181}
3182
Evgenii Stepanov68650822015-06-10 13:38:39 -07003183static std::vector<std::string> g_empty_runpath;
3184
3185const std::vector<std::string>& soinfo::get_dt_runpath() const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003186 if (has_min_version(3)) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003187 return dt_runpath_;
3188 }
3189
3190 return g_empty_runpath;
3191}
3192
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003193android_namespace_t* soinfo::get_namespace() {
3194 if (has_min_version(3)) {
3195 return namespace_;
3196 }
3197
3198 return &g_default_namespace;
3199}
3200
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003201ElfW(Addr) soinfo::resolve_symbol_address(const ElfW(Sym)* s) const {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003202 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
3203 return call_ifunc_resolver(s->st_value + load_bias);
3204 }
3205
3206 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
3207}
3208
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003209const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003210 if (has_min_version(1) && (index >= strtab_size_)) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003211 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003212 get_realpath(), strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003213 }
3214
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003215 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003216}
3217
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003218bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003219 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003220}
3221
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003222bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003223 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003224}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003225
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003226bool soinfo::is_linked() const {
3227 return (flags_ & FLAG_LINKED) != 0;
3228}
3229
3230bool soinfo::is_main_executable() const {
3231 return (flags_ & FLAG_EXE) != 0;
3232}
3233
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -08003234bool soinfo::is_linker() const {
3235 return (flags_ & FLAG_LINKER) != 0;
3236}
3237
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003238void soinfo::set_linked() {
3239 flags_ |= FLAG_LINKED;
3240}
3241
3242void soinfo::set_linker_flag() {
3243 flags_ |= FLAG_LINKER;
3244}
3245
3246void soinfo::set_main_executable() {
3247 flags_ |= FLAG_EXE;
3248}
3249
3250void soinfo::increment_ref_count() {
3251 local_group_root_->ref_count_++;
3252}
3253
3254size_t soinfo::decrement_ref_count() {
3255 return --local_group_root_->ref_count_;
3256}
3257
3258soinfo* soinfo::get_local_group_root() const {
3259 return local_group_root_;
3260}
3261
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -08003262
3263void soinfo::set_mapped_by_caller(bool mapped_by_caller) {
3264 if (mapped_by_caller) {
3265 flags_ |= FLAG_MAPPED_BY_CALLER;
3266 } else {
3267 flags_ &= ~FLAG_MAPPED_BY_CALLER;
3268 }
3269}
3270
3271bool soinfo::is_mapped_by_caller() const {
3272 return (flags_ & FLAG_MAPPED_BY_CALLER) != 0;
3273}
3274
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003275// This function returns api-level at the time of
3276// dlopen/load. Note that libraries opened by system
3277// will always have 'current' api level.
3278uint32_t soinfo::get_target_sdk_version() const {
3279 if (!has_min_version(2)) {
3280 return __ANDROID_API__;
3281 }
3282
3283 return local_group_root_->target_sdk_version_;
3284}
3285
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07003286uintptr_t soinfo::get_handle() const {
3287 CHECK(has_min_version(3));
3288 CHECK(handle_ != 0);
3289 return handle_;
3290}
3291
3292void* soinfo::to_handle() {
3293 if (get_application_target_sdk_version() <= 23 || !has_min_version(3)) {
3294 return this;
3295 }
3296
3297 return reinterpret_cast<void*>(get_handle());
3298}
3299
3300void soinfo::generate_handle() {
3301 CHECK(has_min_version(3));
3302 CHECK(handle_ == 0); // Make sure this is the first call
3303
3304 // Make sure the handle is unique and does not collide
3305 // with special values which are RTLD_DEFAULT and RTLD_NEXT.
3306 do {
3307 arc4random_buf(&handle_, sizeof(handle_));
3308 // the least significant bit for the handle is always 1
3309 // making it easy to test the type of handle passed to
3310 // dl* functions.
3311 handle_ = handle_ | 1;
3312 } while (handle_ == reinterpret_cast<uintptr_t>(RTLD_DEFAULT) ||
3313 handle_ == reinterpret_cast<uintptr_t>(RTLD_NEXT) ||
3314 g_soinfo_handles_map.find(handle_) != g_soinfo_handles_map.end());
3315
3316 g_soinfo_handles_map[handle_] = this;
3317}
3318
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003319bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08003320 /* Extract dynamic section */
3321 ElfW(Word) dynamic_flags = 0;
3322 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07003323
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003324 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003325 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003326 if (!relocating_linker) {
Elliott Hughes116b5692016-01-04 17:45:36 -08003327 INFO("[ Linking '%s' ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003328 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003329 }
3330
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003331 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003332 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003333 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003334 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003335 return false;
3336 } else {
3337 if (!relocating_linker) {
3338 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003339 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003340 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003341
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003342#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003343 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
3344 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003345#endif
3346
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003347 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003348 // Note that: "Except for the DT_NULL element at the end of the array,
3349 // and the relative order of DT_NEEDED elements, entries may appear in any order."
3350 //
3351 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003352 uint32_t needed_count = 0;
3353 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
3354 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
3355 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3356 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003357 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003358 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003359 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003360
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003361 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003362 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
3363 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
3364 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
3365 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003366 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003367
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003368 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003369 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003370 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003371 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
3372 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003373
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003374 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003375 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003376 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003377 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
3378 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003379
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003380 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003381 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003382 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003383 return false;
3384 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003385 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003386
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003387 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003388 break;
3389
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003390 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003391 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003392 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003393
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003394 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003395 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003396 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003397
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003398 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003399 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003400 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003401
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003402 case DT_SYMENT:
3403 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003404 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
3405 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003406 return false;
3407 }
3408 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003409
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003410 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003411#if defined(USE_RELA)
3412 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003413 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003414 return false;
3415 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003416#else
3417 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003418 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003419 return false;
3420 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003421#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003422 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003423
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003424 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003425#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003426 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003427#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003428 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003429#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003430 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003431
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003432 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003433#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003434 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003435#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003436 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003437#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003438 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003439
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003440 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003441#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003442 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003443 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003444#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003445 // Ignore for other platforms... (because RTLD_LAZY is not supported)
3446 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003447
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003448 case DT_DEBUG:
3449 // Set the DT_DEBUG entry to the address of _r_debug for GDB
3450 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08003451// FIXME: not working currently for N64
3452// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003453// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08003454// read-only, but the DYNAMIC header claims it is writable.
3455#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003456 if ((dynamic_flags & PF_W) != 0) {
3457 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
3458 }
Chris Dearman99186652014-02-06 20:36:51 -08003459#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08003460 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003461#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003462 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003463 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003464 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003465
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003466 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003467 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003468 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003469
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003470 case DT_ANDROID_RELA:
3471 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3472 break;
3473
3474 case DT_ANDROID_RELASZ:
3475 android_relocs_size_ = d->d_un.d_val;
3476 break;
3477
3478 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003479 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003480 return false;
3481
3482 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003483 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003484 return false;
3485
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003486 case DT_RELAENT:
3487 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003488 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003489 return false;
3490 }
3491 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003492
3493 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003494 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003495 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003496
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003497 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003498 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003499 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003500
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003501 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003502 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003503 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003504
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003505#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003506 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003507 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003508 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003509
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003510 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003511 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003512 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003513
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003514 case DT_RELENT:
3515 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003516 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003517 return false;
3518 }
3519 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003520
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003521 case DT_ANDROID_REL:
3522 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3523 break;
3524
3525 case DT_ANDROID_RELSZ:
3526 android_relocs_size_ = d->d_un.d_val;
3527 break;
3528
3529 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003530 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003531 return false;
3532
3533 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003534 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003535 return false;
3536
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003537 // "Indicates that all RELATIVE relocations have been concatenated together,
3538 // and specifies the RELATIVE relocation count."
3539 //
3540 // TODO: Spec also mentions that this can be used to optimize relocation process;
3541 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003542 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003543 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003544
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003545 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003546 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003547 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003548
3549 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003550 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003551 return false;
3552
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003553#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003554 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003555 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003556 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003557 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003558
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003559 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003560 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003561 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003562 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003563
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003564 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003565 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003566 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003567 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003568
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003569 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003570 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003571 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003572
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003573 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003574 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003575 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003576 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003577
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003578 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003579 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003580 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003581
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003582 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003583 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003584 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003585 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003586
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003587 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003588 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003589 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003590
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003591 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003592#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003593 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003594 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003595#else
3596 has_text_relocations = true;
3597 break;
3598#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003599
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003600 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003601 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003602 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003603
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003604 case DT_NEEDED:
3605 ++needed_count;
3606 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003607
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003608 case DT_FLAGS:
3609 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003610#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003611 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003612 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003613#else
3614 has_text_relocations = true;
3615#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003616 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003617 if (d->d_un.d_val & DF_SYMBOLIC) {
3618 has_DT_SYMBOLIC = true;
3619 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003620 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003621
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003622 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003623 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003624
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003625 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -07003626 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 -07003627 }
3628 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003629#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003630 case DT_MIPS_RLD_MAP:
3631 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3632 {
3633 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3634 *dp = &_r_debug;
3635 }
3636 break;
Lazar Trsic83b44a92016-04-06 13:39:17 +02003637 case DT_MIPS_RLD_MAP_REL:
3638 // Set the DT_MIPS_RLD_MAP_REL entry to the address of _r_debug for GDB.
Raghu Gandham68815722014-12-18 19:12:19 -08003639 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003640 r_debug** dp = reinterpret_cast<r_debug**>(
3641 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003642 *dp = &_r_debug;
3643 }
3644 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003645
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003646 case DT_MIPS_RLD_VERSION:
3647 case DT_MIPS_FLAGS:
3648 case DT_MIPS_BASE_ADDRESS:
3649 case DT_MIPS_UNREFEXTNO:
3650 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003651
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003652 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003653 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003654 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003655
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003656 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003657 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003658 break;
3659
3660 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003661 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003662 break;
3663#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003664 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3665 case DT_BIND_NOW:
3666 break;
3667
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003668 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003669 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3670 break;
3671
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003672 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003673 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3674 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003675 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003676 verdef_cnt_ = d->d_un.d_val;
3677 break;
3678
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003679 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003680 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3681 break;
3682
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003683 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003684 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003685 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003686
Evgenii Stepanov68650822015-06-10 13:38:39 -07003687 case DT_RUNPATH:
3688 // this is parsed after we have strtab initialized (see below).
3689 break;
3690
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003691 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003692 if (!relocating_linker) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003693 DL_WARN("%s: unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003694 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3695 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003696 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003697 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003698 }
3699
Duane Sandbc425c72015-06-01 16:29:14 -07003700#if defined(__mips__) && !defined(__LP64__)
3701 if (!mips_check_and_adjust_fp_modes()) {
3702 return false;
3703 }
3704#endif
3705
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003706 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003707 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003708
3709 // Sanity checks.
3710 if (relocating_linker && needed_count != 0) {
3711 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3712 return false;
3713 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003714 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003715 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003716 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003717 return false;
3718 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003719 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003720 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003721 return false;
3722 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003723 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003724 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003725 return false;
3726 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003727
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003728 // second pass - parse entries relying on strtab
3729 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003730 switch (d->d_tag) {
3731 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003732 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003733 break;
3734 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003735 set_dt_runpath(get_string(d->d_un.d_val));
3736 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003737 }
3738 }
3739
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003740 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003741 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003742 // because they can't find dt_needed library by soname.
3743 // This workaround should keep them working. (applies only
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003744 // for apps targeting sdk version <=22). Make an exception for
3745 // the main executable and linker; they do not need to have dt_soname
3746 if (soname_ == nullptr && this != somain && (flags_ & FLAG_LINKER) == 0 &&
3747 get_application_target_sdk_version() <= 22) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003748 soname_ = basename(realpath_.c_str());
3749 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3750 get_realpath(), soname_);
3751 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003752 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003753}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003754
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003755bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3756 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003757
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003758 local_group_root_ = local_group.front();
3759 if (local_group_root_ == nullptr) {
3760 local_group_root_ = this;
3761 }
3762
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003763 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3764 target_sdk_version_ = get_application_target_sdk_version();
3765 }
3766
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003767 VersionTracker version_tracker;
3768
3769 if (!version_tracker.init(this)) {
3770 return false;
3771 }
3772
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003773#if !defined(__LP64__)
3774 if (has_text_relocations) {
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003775 // Fail if app is targeting sdk version > 22
Dmitriy Ivanov80687862015-10-09 13:58:46 -07003776 if (get_application_target_sdk_version() > 22) {
Dmitriy Ivanovfae39d22015-10-13 11:07:56 -07003777 PRINT("%s: has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003778 DL_ERR("%s: has text relocations", get_realpath());
3779 return false;
3780 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003781 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003782 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003783 DL_WARN("%s has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003784 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003785 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3786 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003787 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003788 return false;
3789 }
3790 }
3791#endif
3792
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003793 if (android_relocs_ != nullptr) {
3794 // check signature
3795 if (android_relocs_size_ > 3 &&
3796 android_relocs_[0] == 'A' &&
3797 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003798 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003799 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003800 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003801
3802 bool relocated = false;
3803 const uint8_t* packed_relocs = android_relocs_ + 4;
3804 const size_t packed_relocs_size = android_relocs_size_ - 4;
3805
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003806 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003807 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003808 packed_reloc_iterator<sleb128_decoder>(
3809 sleb128_decoder(packed_relocs, packed_relocs_size)),
3810 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003811
3812 if (!relocated) {
3813 return false;
3814 }
3815 } else {
3816 DL_ERR("bad android relocation header.");
3817 return false;
3818 }
3819 }
3820
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003821#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003822 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003823 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003824 if (!relocate(version_tracker,
3825 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003826 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003827 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003828 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003829 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003830 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003831 if (!relocate(version_tracker,
3832 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003833 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003834 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003835 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003836#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003837 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003838 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003839 if (!relocate(version_tracker,
3840 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003841 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003842 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003843 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003844 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003845 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003846 if (!relocate(version_tracker,
3847 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003848 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003849 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003850 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003851#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003852
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003853#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003854 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003855 return false;
3856 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003857#endif
3858
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003859 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003860
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003861#if !defined(__LP64__)
3862 if (has_text_relocations) {
3863 // All relocations are done, we can protect our segments back to read-only.
3864 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3865 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003866 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003867 return false;
3868 }
3869 }
3870#endif
3871
Mingwei Shibe910522015-11-12 07:02:14 +00003872 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
3873 // itself --- it can't make system calls yet, and will have to call protect_relro later.
3874 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003875 return false;
3876 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003877
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003878 /* Handle serializing/sharing the RELRO segment */
3879 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3880 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3881 extinfo->relro_fd) < 0) {
3882 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003883 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003884 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003885 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003886 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3887 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3888 extinfo->relro_fd) < 0) {
3889 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003890 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003891 return false;
3892 }
3893 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003894
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003895 notify_gdb_of_load(this);
3896 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003897}
3898
Mingwei Shibe910522015-11-12 07:02:14 +00003899bool soinfo::protect_relro() {
3900 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3901 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
3902 get_realpath(), strerror(errno));
3903 return false;
3904 }
3905 return true;
3906}
3907
Nick Kralevich468319c2011-11-11 15:53:17 -08003908/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003909 * This function add vdso to internal dso list.
3910 * It helps to stack unwinding through signal handlers.
3911 * Also, it makes bionic more like glibc.
3912 */
Kito Cheng812fd422014-03-25 22:53:56 +08003913static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003914#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08003915 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003916 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08003917 return;
3918 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003919
Dmitriy Ivanovd9b08a02015-11-16 13:17:27 -08003920 soinfo* si = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003921
Elliott Hughes0266ae52014-02-10 17:46:57 -08003922 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
3923 si->phnum = ehdr_vdso->e_phnum;
3924 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
3925 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003926 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003927
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003928 si->prelink_image();
3929 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003930#endif
3931}
3932
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003933/* gdb expects the linker to be in the debug shared object list.
3934 * Without this, gdb has trouble locating the linker's ".text"
3935 * and ".plt" sections. Gdb could also potentially use this to
3936 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
Dimitry Ivanov64001292016-02-17 14:13:06 -08003937 * Note that the linker shouldn't be on the soinfo list.
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003938 */
3939static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dimitry Ivanov8d22dd52016-02-16 13:43:35 -08003940 static link_map linker_link_map_for_gdb;
3941#if defined(__LP64__)
3942 static char kLinkerPath[] = "/system/bin/linker64";
3943#else
3944 static char kLinkerPath[] = "/system/bin/linker";
3945#endif
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003946
Dimitry Ivanov8d22dd52016-02-16 13:43:35 -08003947 linker_link_map_for_gdb.l_addr = linker_base;
3948 linker_link_map_for_gdb.l_name = kLinkerPath;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003949
3950 /*
3951 * Set the dynamic field in the link map otherwise gdb will complain with
3952 * the following:
3953 * warning: .dynamic section for "/system/bin/linker" is not at the
3954 * expected address (wrong library or version mismatch?)
3955 */
3956 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
3957 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
3958 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Dimitry Ivanov8d22dd52016-02-16 13:43:35 -08003959 &linker_link_map_for_gdb.l_ld, nullptr);
3960
3961 insert_link_map_into_debug_map(&linker_link_map_for_gdb);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003962}
3963
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003964static void init_default_namespace() {
3965 g_default_namespace.set_name("(default)");
3966 g_default_namespace.set_isolated(false);
3967
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003968 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3969 somain->load_bias);
3970 const char* bname = basename(interp);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003971 if (bname && (strcmp(bname, "linker_asan") == 0 || strcmp(bname, "linker_asan64") == 0)) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003972 g_default_ld_paths = kAsanDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003973 } else {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003974 g_default_ld_paths = kDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003975 }
3976
3977 std::vector<std::string> ld_default_paths;
3978 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
3979 ld_default_paths.push_back(g_default_ld_paths[i]);
3980 }
3981
3982 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003983};
3984
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07003985extern "C" int __system_properties_init(void);
3986
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003987/*
Nick Kralevich468319c2011-11-11 15:53:17 -08003988 * This code is called after the linker has linked itself and
3989 * fixed it's own GOT. It is safe to make references to externs
3990 * and other non-local data at this point.
3991 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08003992static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04003993#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003994 struct timeval t0, t1;
3995 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04003996#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003997
Elliott Hughes1801db32015-06-08 18:04:00 -07003998 // Sanitize the environment.
3999 __libc_init_AT_SECURE(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01004000
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07004001 // Initialize system properties
4002 __system_properties_init(); // may use 'environ'
4003
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004004 debuggerd_init();
4005
4006 // Get a few environment variables.
Elliott Hughes1801db32015-06-08 18:04:00 -07004007 const char* LD_DEBUG = getenv("LD_DEBUG");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004008 if (LD_DEBUG != nullptr) {
4009 g_ld_debug_verbosity = atoi(LD_DEBUG);
4010 }
4011
Elliott Hughes116b5692016-01-04 17:45:36 -08004012#if defined(__LP64__)
4013 INFO("[ Android dynamic linker (64-bit) ]");
4014#else
4015 INFO("[ Android dynamic linker (32-bit) ]");
4016#endif
4017
Elliott Hughes1801db32015-06-08 18:04:00 -07004018 // These should have been sanitized by __libc_init_AT_SECURE, but the test
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004019 // doesn't cost us anything.
4020 const char* ldpath_env = nullptr;
4021 const char* ldpreload_env = nullptr;
Elliott Hughes1801db32015-06-08 18:04:00 -07004022 if (!getauxval(AT_SECURE)) {
4023 ldpath_env = getenv("LD_LIBRARY_PATH");
Elliott Hughes116b5692016-01-04 17:45:36 -08004024 if (ldpath_env != nullptr) {
4025 INFO("[ LD_LIBRARY_PATH set to '%s' ]", ldpath_env);
4026 }
Elliott Hughes1801db32015-06-08 18:04:00 -07004027 ldpreload_env = getenv("LD_PRELOAD");
Elliott Hughes116b5692016-01-04 17:45:36 -08004028 if (ldpreload_env != nullptr) {
4029 INFO("[ LD_PRELOAD set to '%s' ]", ldpreload_env);
4030 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004031 }
4032
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004033 soinfo* si = soinfo_alloc(&g_default_namespace, args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004034 if (si == nullptr) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004035 __libc_fatal("Couldn't allocate soinfo: out of memory?");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004036 }
4037
4038 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004039 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004040 link_map* map = &(si->link_map_head);
4041
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -08004042 // Register the main executable and the linker upfront to have
4043 // gdb aware of them before loading the rest of the dependency
4044 // tree.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004045 map->l_addr = 0;
4046 map->l_name = args.argv[0];
Dimitry Ivanovf3064e42016-02-17 15:25:25 -08004047 insert_link_map_into_debug_map(map);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004048 init_linker_info_for_gdb(linker_base);
4049
4050 // Extract information passed from the kernel.
4051 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
4052 si->phnum = args.getauxval(AT_PHNUM);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004053
4054 /* Compute the value of si->base. We can't rely on the fact that
4055 * the first entry is the PHDR because this will not be true
4056 * for certain executables (e.g. some in the NDK unit test suite)
4057 */
4058 si->base = 0;
4059 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
4060 si->load_bias = 0;
4061 for (size_t i = 0; i < si->phnum; ++i) {
4062 if (si->phdr[i].p_type == PT_PHDR) {
4063 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
4064 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
4065 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07004066 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004067 }
4068 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07004069
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004070 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
4071 if (elf_hdr->e_type != ET_DYN) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004072 __libc_fatal("\"%s\": error: only position independent executables (PIE) are supported.",
4073 args.argv[0]);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004074 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004075
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004076 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
4077 parse_LD_LIBRARY_PATH(ldpath_env);
4078 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01004079
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004080 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004081
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004082 init_default_namespace();
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004083
Dmitriy Ivanov67181252015-01-07 15:48:25 -08004084 if (!si->prelink_image()) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004085 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
Dmitriy Ivanov67181252015-01-07 15:48:25 -08004086 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004087
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07004088 // add somain to global group
4089 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
4090
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004091 // Load ld_preloads and dependencies.
4092 StringLinkedList needed_library_name_list;
4093 size_t needed_libraries_count = 0;
4094 size_t ld_preloads_count = 0;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07004095
4096 for (const auto& ld_preload_name : g_ld_preload_names) {
4097 needed_library_name_list.push_back(ld_preload_name.c_str());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004098 ++needed_libraries_count;
Dmitriy Ivanovf8093a92015-04-28 18:09:53 -07004099 ++ld_preloads_count;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004100 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004101
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004102 for_each_dt_needed(si, [&](const char* name) {
4103 needed_library_name_list.push_back(name);
4104 ++needed_libraries_count;
4105 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004106
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004107 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004108
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004109 memset(needed_library_names, 0, sizeof(needed_library_names));
4110 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004111
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07004112 if (needed_libraries_count > 0 &&
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004113 !find_libraries(&g_default_namespace, si, needed_library_names, needed_libraries_count,
4114 nullptr, &g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07004115 /* add_as_children */ true)) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004116 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004117 } else if (needed_libraries_count == 0) {
4118 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004119 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004120 }
4121 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004122 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004123
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004124 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07004125
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08004126 {
4127 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06004128
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08004129 si->call_pre_init_constructors();
4130
4131 /* After the prelink_image, the si->load_bias is initialized.
4132 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
4133 * We need to update this value for so exe here. So Unwind_Backtrace
4134 * for some arch like x86 could work correctly within so exe.
4135 */
4136 map->l_addr = si->load_bias;
4137 si->call_constructors();
4138 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04004139
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004140#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004141 gettimeofday(&t1, nullptr);
4142 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
4143 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
4144 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004145#endif
4146#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004147 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
4148 linker_stats.count[kRelocAbsolute],
4149 linker_stats.count[kRelocRelative],
4150 linker_stats.count[kRelocCopy],
4151 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004152#endif
4153#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004154 {
4155 unsigned n;
4156 unsigned i;
4157 unsigned count = 0;
4158 for (n = 0; n < 4096; n++) {
4159 if (bitmask[n]) {
4160 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004161#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004162 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004163#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004164 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004165#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004166 if (x & 1) {
4167 count++;
4168 }
4169 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004170 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004171 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004172 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004173 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
4174 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004175#endif
4176
4177#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004178 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004179#endif
4180
Dimitry Ivanove687d062016-02-16 13:25:29 -08004181 ElfW(Addr) entry = args.getauxval(AT_ENTRY);
4182 TRACE("[ Ready to execute '%s' @ %p ]", si->get_realpath(), reinterpret_cast<void*>(entry));
4183 return entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004184}
Nick Kralevich468319c2011-11-11 15:53:17 -08004185
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004186/* Compute the load-bias of an existing executable. This shall only
4187 * be used to compute the load bias of an executable or shared library
4188 * that was loaded by the kernel itself.
4189 *
4190 * Input:
4191 * elf -> address of ELF header, assumed to be at the start of the file.
4192 * Return:
4193 * load bias, i.e. add the value of any p_vaddr in the file to get
4194 * the corresponding address in memory.
4195 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004196static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
4197 ElfW(Addr) offset = elf->e_phoff;
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004198 const ElfW(Phdr)* phdr_table =
4199 reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004200 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004201
Elliott Hughes0266ae52014-02-10 17:46:57 -08004202 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08004203 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08004204 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004205 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08004206 }
4207 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004208}
4209
Mingwei Shibe910522015-11-12 07:02:14 +00004210static void __linker_cannot_link(KernelArgumentBlock& args) {
4211 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
4212}
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004213
Nick Kralevich468319c2011-11-11 15:53:17 -08004214/*
4215 * This is the entry point for the linker, called from begin.S. This
4216 * method is responsible for fixing the linker's own relocations, and
4217 * then calling __linker_init_post_relocation().
4218 *
4219 * Because this method is called before the linker has fixed it's own
4220 * relocations, any attempt to reference an extern variable, extern
4221 * function, or other GOT reference will generate a segfault.
4222 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004223extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004224 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08004225
Elliott Hughes0266ae52014-02-10 17:46:57 -08004226 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004227 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004228 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08004229 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08004230
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004231 soinfo linker_so(nullptr, nullptr, nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08004232
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004233 // If the linker is not acting as PT_INTERP entry_point is equal to
4234 // _start. Which means that the linker is running as an executable and
4235 // already linked by PT_INTERP.
4236 //
4237 // This happens when user tries to run 'adb shell /system/bin/linker'
4238 // see also https://code.google.com/p/android/issues/detail?id=63174
4239 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004240 __libc_format_fd(STDOUT_FILENO,
4241 "This is %s, the helper program for shared library executables.\n",
4242 args.argv[0]);
4243 exit(0);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004244 }
4245
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004246 linker_so.base = linker_addr;
4247 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
4248 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07004249 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004250 linker_so.phdr = phdr;
4251 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004252 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07004253
Mingwei Shibe910522015-11-12 07:02:14 +00004254 // Prelink the linker so we can access linker globals.
4255 if (!linker_so.prelink_image()) __linker_cannot_link(args);
4256
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07004257 // This might not be obvious... The reasons why we pass g_empty_list
4258 // in place of local_group here are (1) we do not really need it, because
4259 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
4260 // itself without having to look into local_group and (2) allocators
4261 // are not yet initialized, and therefore we cannot use linked_list.push_*
4262 // functions at this point.
Mingwei Shibe910522015-11-12 07:02:14 +00004263 if (!linker_so.link_image(g_empty_list, g_empty_list, nullptr)) __linker_cannot_link(args);
Elliott Hughesd23736e2012-11-01 15:16:56 -07004264
Mingwei Shibe910522015-11-12 07:02:14 +00004265#if defined(__i386__)
4266 // On x86, we can't make system calls before this point.
4267 // We can't move this up because this needs to assign to a global.
4268 // Note that until we call __libc_init_main_thread below we have
4269 // no TLS, so you shouldn't make a system call that can fail, because
4270 // it will SEGV when it tries to set errno.
4271 __libc_init_sysinfo(args);
4272#endif
4273
4274 // Initialize the main thread (including TLS, so system calls really work).
Elliott Hughesd2948632015-07-21 11:57:09 -07004275 __libc_init_main_thread(args);
Dmitriy Ivanov14241402014-08-26 14:16:52 -07004276
Mingwei Shibe910522015-11-12 07:02:14 +00004277 // We didn't protect the linker's RELRO pages in link_image because we
4278 // couldn't make system calls on x86 at that point, but we can now...
4279 if (!linker_so.protect_relro()) __linker_cannot_link(args);
4280
Josh Gao93c0f5e2015-10-06 11:08:13 -07004281 // Initialize the linker's static libc's globals
4282 __libc_init_globals(args);
4283
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004284 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004285 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07004286
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004287 // Initialize static variables. Note that in order to
4288 // get correct libdl_info we need to call constructors
4289 // before get_libdl_info().
4290 solist = get_libdl_info();
4291 sonext = get_libdl_info();
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07004292 g_default_namespace.add_soinfo(get_libdl_info());
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004293
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004294 // We have successfully fixed our own relocations. It's safe to run
4295 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07004296 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08004297 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004298
Elliott Hughes116b5692016-01-04 17:45:36 -08004299 INFO("[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address));
Elliott Hughes611f9562015-01-23 10:43:58 -08004300
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004301 // Return the address that the calling assembly stub should jump to.
4302 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08004303}