blob: 2cdfd2c3b0db1fcda9d70f3ce28d2620b629bd87 [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
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700106 soinfo::soinfo_list_t& soinfo_list() { return soinfo_list_; }
107
108 // For isolated namespaces - checks if the file is on the search path;
109 // always returns true for not isolated namespace.
110 bool is_accessible(const std::string& path);
111
112 private:
113 const char* name_;
114 bool is_isolated_;
115 std::vector<std::string> ld_library_paths_;
116 std::vector<std::string> default_library_paths_;
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800117 std::vector<std::string> permitted_paths_;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700118 soinfo::soinfo_list_t soinfo_list_;
119
120 DISALLOW_COPY_AND_ASSIGN(android_namespace_t);
121};
122
123android_namespace_t g_default_namespace;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800124android_namespace_t* g_anonymous_namespace = &g_default_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700125
Elliott Hughes0266ae52014-02-10 17:46:57 -0800126static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700128static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
129static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200130
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700131static LinkerTypeAllocator<android_namespace_t> g_namespace_allocator;
132
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700133static soinfo* solist;
134static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700135static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136
Elliott Hughes1728b232014-05-14 10:02:03 -0700137static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700138#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700139 "/system/lib64",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800140 "/vendor/lib64",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700141#else
Elliott Hughes124fae92012-10-31 14:20:03 -0700142 "/system/lib",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800143 "/vendor/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700144#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700145 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700146};
David Bartleybc3a5c22009-06-02 18:27:28 -0700147
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700148static const char* const kAsanDefaultLdPaths[] = {
149#if defined(__LP64__)
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700150 "/data/lib64",
151 "/system/lib64",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800152 "/data/vendor/lib64",
153 "/vendor/lib64",
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700154#else
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700155 "/data/lib",
156 "/system/lib",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800157 "/data/vendor/lib",
158 "/vendor/lib",
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700159#endif
160 nullptr
161};
162
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700163static const ElfW(Versym) kVersymNotNeeded = 0;
164static const ElfW(Versym) kVersymGlobal = 1;
165
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700166static const char* const* g_default_ld_paths;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700167static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800168
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700169static std::vector<soinfo*> g_ld_preloads;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600170
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700171static bool g_public_namespace_initialized;
172static soinfo::soinfo_list_t g_public_namespace;
173
Elliott Hughes1728b232014-05-14 10:02:03 -0700174__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700176__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700177
Evgenii Stepanov68650822015-06-10 13:38:39 -0700178static std::string dirname(const char *path) {
179 const char* last_slash = strrchr(path, '/');
180 if (last_slash == path) return "/";
181 else if (last_slash == nullptr) return ".";
182 else
183 return std::string(path, last_slash - path);
184}
185
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700187struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700188 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700189};
190
191static linker_stats_t linker_stats;
192
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800193void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700194 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700195}
196#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800197void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700198}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800199#endif
200
201#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800202uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203#endif
204
Dima Zavin2e855792009-05-20 18:28:09 -0700205static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700206
Elliott Hughes650be4e2013-03-05 18:47:58 -0800207char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700208 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700209}
210
Elliott Hughes650be4e2013-03-05 18:47:58 -0800211size_t linker_get_error_buffer_size() {
212 return sizeof(__linker_dl_err_buf);
213}
214
Elliott Hughesbedfe382012-08-14 14:07:59 -0700215static void notify_gdb_of_load(soinfo* info) {
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800216 if (info->is_linker() || info->is_main_executable()) {
217 // gdb already knows about the linker and the main executable.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700218 return;
219 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800220
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800221 link_map* map = &(info->link_map_head);
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000222
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800223 map->l_addr = info->load_bias;
224 // link_map l_name field is not const.
225 map->l_name = const_cast<char*>(info->get_realpath());
226 map->l_ld = info->dynamic;
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000227
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800228 CHECK(map->l_name != nullptr);
229 CHECK(map->l_name[0] != '\0');
230
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800231 notify_gdb_of_load(map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700232}
233
Elliott Hughesbedfe382012-08-14 14:07:59 -0700234static void notify_gdb_of_unload(soinfo* info) {
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800235 notify_gdb_of_unload(&(info->link_map_head));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800236}
237
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700238bool android_namespace_t::is_accessible(const std::string& file) {
239 if (!is_isolated_) {
240 return true;
241 }
242
243 for (const auto& dir : ld_library_paths_) {
244 if (file_is_in_dir(file, dir)) {
245 return true;
246 }
247 }
248
249 for (const auto& dir : default_library_paths_) {
250 if (file_is_in_dir(file, dir)) {
251 return true;
252 }
253 }
254
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800255 for (const auto& dir : permitted_paths_) {
256 if (file_is_under_dir(file, dir)) {
257 return true;
258 }
259 }
260
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700261 return false;
262}
263
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700264LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
265 return g_soinfo_links_allocator.alloc();
266}
267
268void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
269 g_soinfo_links_allocator.free(entry);
270}
271
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700272static soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
273 struct stat* file_stat, off64_t file_offset,
274 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700275 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200276 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700277 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200278 }
279
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700280 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
281 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700282
Magnus Malmbornba98d922012-09-12 13:00:55 +0200283 sonext->next = si;
284 sonext = si;
285
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700286 ns->soinfo_list().push_back(si);
287
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700288 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200289 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800290}
291
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800292static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700293 if (si == nullptr) {
294 return;
295 }
296
297 if (si->base != 0 && si->size != 0) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800298 if (!si->is_mapped_by_caller()) {
299 munmap(reinterpret_cast<void*>(si->base), si->size);
300 } else {
301 // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE
302 mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE,
303 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
304 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700305 }
306
307 soinfo *prev = nullptr, *trav;
308
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700309 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700310
311 for (trav = solist; trav != nullptr; trav = trav->next) {
312 if (trav == si) {
313 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700314 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700315 prev = trav;
316 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800317
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700318 if (trav == nullptr) {
319 // si was not in solist
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700320 DL_ERR("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700321 return;
322 }
Elliott Hughes46882792012-08-03 16:49:39 -0700323
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700324 // clear links to/from si
325 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700326
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700327 // prev will never be null, because the first entry in solist is
328 // always the static libdl_info.
329 prev->next = si->next;
330 if (si == sonext) {
331 sonext = prev;
332 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800333
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700334 // remove from the namespace
335 si->get_namespace()->soinfo_list().remove_if([&](soinfo* candidate) {
336 return si == candidate;
337 });
338
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700339 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700340 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800341}
342
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700343// For every path element this function checks of it exists, and is a directory,
344// and normalizes it:
345// 1. For regular path it converts it to realpath()
346// 2. For path in a zip file it uses realpath on the zipfile
347// normalizes entry name by calling normalize_path function.
348static void resolve_paths(std::vector<std::string>& paths,
349 std::vector<std::string>* resolved_paths) {
350 resolved_paths->clear();
351 for (const auto& path : paths) {
352 char resolved_path[PATH_MAX];
353 const char* original_path = path.c_str();
354 if (realpath(original_path, resolved_path) != nullptr) {
355 struct stat s;
356 if (stat(resolved_path, &s) == 0) {
357 if (S_ISDIR(s.st_mode)) {
358 resolved_paths->push_back(resolved_path);
359 } else {
360 DL_WARN("Warning: \"%s\" is not a directory (excluding from path)", resolved_path);
361 continue;
362 }
363 } else {
364 DL_WARN("Warning: cannot stat file \"%s\": %s", resolved_path, strerror(errno));
365 continue;
366 }
367 } else {
368 std::string zip_path;
369 std::string entry_path;
370
371 std::string normalized_path;
372
373 if (!normalize_path(original_path, &normalized_path)) {
374 DL_WARN("Warning: unable to normalize \"%s\"", original_path);
375 continue;
376 }
377
378 if (parse_zip_path(normalized_path.c_str(), &zip_path, &entry_path)) {
379 if (realpath(zip_path.c_str(), resolved_path) == nullptr) {
380 DL_WARN("Warning: unable to resolve \"%s\": %s", zip_path.c_str(), strerror(errno));
381 continue;
382 }
383
384 ZipArchiveHandle handle = nullptr;
Yabin Cui722072d2016-03-21 17:10:12 -0700385 void* cookie = nullptr;
386 auto zip_guard = make_scope_guard([&]() {
387 if (cookie != nullptr) {
388 EndIteration(cookie);
389 }
390 if (handle != nullptr) {
391 CloseArchive(handle);
392 }
393 });
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700394 if (OpenArchive(resolved_path, &handle) != 0) {
395 DL_WARN("Warning: unable to open zip archive: %s", resolved_path);
396 continue;
397 }
398
399 // Check if zip-file has a dir with entry_path name
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700400 std::string prefix_str = entry_path + "/";
401 ZipString prefix(prefix_str.c_str());
402
403 ZipEntry out_data;
404 ZipString out_name;
405
406 int32_t error_code;
407
408 if ((error_code = StartIteration(handle, &cookie, &prefix, nullptr)) != 0) {
409 DL_WARN("Unable to iterate over zip-archive entries \"%s\";"
410 " error code: %d", zip_path.c_str(), error_code);
411 continue;
412 }
413
414 if (Next(cookie, &out_data, &out_name) != 0) {
415 DL_WARN("Unable to find entries starting with \"%s\" in \"%s\"",
416 prefix_str.c_str(), zip_path.c_str());
417 continue;
418 }
419
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700420 resolved_paths->push_back(std::string(resolved_path) + kZipFileSeparator + entry_path);
421 }
422 }
423 }
424}
425
426static void split_path(const char* path, const char* delimiters,
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700427 std::vector<std::string>* paths) {
Dmitriy Ivanovfbfba642015-11-16 14:23:37 -0800428 if (path != nullptr && path[0] != 0) {
tony.ys_liub4474402015-07-29 18:00:22 +0800429 *paths = android::base::Split(path, delimiters);
Elliott Hughescade4c32012-12-20 14:42:14 -0800430 }
431}
432
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700433static void parse_path(const char* path, const char* delimiters,
434 std::vector<std::string>* resolved_paths) {
435 std::vector<std::string> paths;
436 split_path(path, delimiters, &paths);
437 resolve_paths(paths, resolved_paths);
438}
439
Elliott Hughescade4c32012-12-20 14:42:14 -0800440static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700441 std::vector<std::string> ld_libary_paths;
442 parse_path(path, ":", &ld_libary_paths);
443 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800444}
445
Evgenii Stepanov68650822015-06-10 13:38:39 -0700446void soinfo::set_dt_runpath(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700447 if (!has_min_version(3)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700448 return;
449 }
450
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700451 std::vector<std::string> runpaths;
452
453 split_path(path, ":", &runpaths);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700454
455 std::string origin = dirname(get_realpath());
456 // FIXME: add $LIB and $PLATFORM.
457 std::pair<std::string, std::string> substs[] = {{"ORIGIN", origin}};
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700458 for (auto&& s : runpaths) {
Evgenii Stepanov68650822015-06-10 13:38:39 -0700459 size_t pos = 0;
460 while (pos < s.size()) {
461 pos = s.find("$", pos);
462 if (pos == std::string::npos) break;
463 for (const auto& subst : substs) {
464 const std::string& token = subst.first;
465 const std::string& replacement = subst.second;
466 if (s.substr(pos + 1, token.size()) == token) {
467 s.replace(pos, token.size() + 1, replacement);
468 // -1 to compensate for the ++pos below.
469 pos += replacement.size() - 1;
470 break;
471 } else if (s.substr(pos + 1, token.size() + 2) == "{" + token + "}") {
472 s.replace(pos, token.size() + 3, replacement);
473 pos += replacement.size() - 1;
474 break;
475 }
476 }
477 // Skip $ in case it did not match any of the known substitutions.
478 ++pos;
479 }
480 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700481
482 resolve_paths(runpaths, &dt_runpath_);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700483}
484
Elliott Hughescade4c32012-12-20 14:42:14 -0800485static void parse_LD_PRELOAD(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700486 g_ld_preload_names.clear();
487 if (path != nullptr) {
488 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
489 g_ld_preload_names = android::base::Split(path, " :");
490 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800491}
492
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700493static bool realpath_fd(int fd, std::string* realpath) {
494 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700495 __libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700496 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -0700497 PRINT("readlink('%s') failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700498 return false;
499 }
500
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700501 *realpath = &buf[0];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700502 return true;
503}
504
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700505#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700506
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700507// For a given PC, find the .so that it belongs to.
508// Returns the base address of the .ARM.exidx section
509// for that .so, and the number of 8-byte entries
510// in that section (via *pcount).
511//
512// Intended to be called by libc's __gnu_Unwind_Find_exidx().
513//
514// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800515_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800516 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800517
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700518 for (soinfo* si = solist; si != 0; si = si->next) {
519 if ((addr >= si->base) && (addr < (si->base + si->size))) {
520 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800521 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800522 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700523 }
524 *pcount = 0;
525 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800526}
Elliott Hughes46882792012-08-03 16:49:39 -0700527
Christopher Ferris24053a42013-08-19 17:45:09 -0700528#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700529
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700530// Here, we only have to provide a callback to iterate across all the
531// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700532int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700533 int rv = 0;
534 for (soinfo* si = solist; si != nullptr; si = si->next) {
535 dl_phdr_info dl_info;
536 dl_info.dlpi_addr = si->link_map_head.l_addr;
537 dl_info.dlpi_name = si->link_map_head.l_name;
538 dl_info.dlpi_phdr = si->phdr;
539 dl_info.dlpi_phnum = si->phnum;
540 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
541 if (rv != 0) {
542 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800543 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700544 }
545 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800546}
Elliott Hughes46882792012-08-03 16:49:39 -0700547
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700548const ElfW(Versym)* soinfo::get_versym(size_t n) const {
549 if (has_min_version(2) && versym_ != nullptr) {
550 return versym_ + n;
551 }
552
553 return nullptr;
554}
555
556ElfW(Addr) soinfo::get_verneed_ptr() const {
557 if (has_min_version(2)) {
558 return verneed_ptr_;
559 }
560
561 return 0;
562}
563
564size_t soinfo::get_verneed_cnt() const {
565 if (has_min_version(2)) {
566 return verneed_cnt_;
567 }
568
569 return 0;
570}
571
572ElfW(Addr) soinfo::get_verdef_ptr() const {
573 if (has_min_version(2)) {
574 return verdef_ptr_;
575 }
576
577 return 0;
578}
579
580size_t soinfo::get_verdef_cnt() const {
581 if (has_min_version(2)) {
582 return verdef_cnt_;
583 }
584
585 return 0;
586}
587
588template<typename F>
589static bool for_each_verdef(const soinfo* si, F functor) {
590 if (!si->has_min_version(2)) {
591 return true;
592 }
593
594 uintptr_t verdef_ptr = si->get_verdef_ptr();
595 if (verdef_ptr == 0) {
596 return true;
597 }
598
599 size_t offset = 0;
600
601 size_t verdef_cnt = si->get_verdef_cnt();
602 for (size_t i = 0; i<verdef_cnt; ++i) {
603 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
604 size_t verdaux_offset = offset + verdef->vd_aux;
605 offset += verdef->vd_next;
606
607 if (verdef->vd_version != 1) {
Dmitriy Ivanov3d7bea12015-04-20 17:40:39 -0700608 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700609 i, verdef->vd_version, si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700610 return false;
611 }
612
613 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
614 // "this is the version of the file itself. It must not be used for
615 // matching a symbol. It can be used to match references."
616 //
617 // http://www.akkadia.org/drepper/symbol-versioning
618 continue;
619 }
620
621 if (verdef->vd_cnt == 0) {
622 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
623 return false;
624 }
625
626 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
627
628 if (functor(i, verdef, verdaux) == true) {
629 break;
630 }
631 }
632
633 return true;
634}
635
636bool soinfo::find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const {
637 if (vi == nullptr) {
638 *versym = kVersymNotNeeded;
639 return true;
640 }
641
642 *versym = kVersymGlobal;
643
644 return for_each_verdef(this,
645 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
646 if (verdef->vd_hash == vi->elf_hash &&
647 strcmp(vi->name, get_string(verdaux->vda_name)) == 0) {
648 *versym = verdef->vd_ndx;
649 return true;
650 }
651
652 return false;
653 }
654 );
655}
656
657bool soinfo::find_symbol_by_name(SymbolName& symbol_name,
658 const version_info* vi,
659 const ElfW(Sym)** symbol) const {
660 uint32_t symbol_index;
661 bool success =
662 is_gnu_hash() ?
663 gnu_lookup(symbol_name, vi, &symbol_index) :
664 elf_lookup(symbol_name, vi, &symbol_index);
665
666 if (success) {
667 *symbol = symbol_index == 0 ? nullptr : symtab_ + symbol_index;
668 }
669
670 return success;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800671}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800672
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800673static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
674 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
675 ELF_ST_BIND(s->st_info) == STB_WEAK) {
676 return s->st_shndx != SHN_UNDEF;
677 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
678 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700679 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800680 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800681
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800682 return false;
683}
684
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700685static const ElfW(Versym) kVersymHiddenBit = 0x8000;
686
687static inline bool is_versym_hidden(const ElfW(Versym)* versym) {
688 // the symbol is hidden if bit 15 of versym is set.
689 return versym != nullptr && (*versym & kVersymHiddenBit) != 0;
690}
691
692static inline bool check_symbol_version(const ElfW(Versym) verneed,
693 const ElfW(Versym)* verdef) {
694 return verneed == kVersymNotNeeded ||
695 verdef == nullptr ||
696 verneed == (*verdef & ~kVersymHiddenBit);
697}
698
699bool soinfo::gnu_lookup(SymbolName& symbol_name,
700 const version_info* vi,
701 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800702 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800703 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800704
705 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800706 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
707 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800708
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700709 *symbol_index = 0;
710
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700711 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700712 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700713
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800714 // test against bloom filter
715 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700716 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700717 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700718
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700719 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800720 }
721
722 // bloom test says "probably yes"...
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700723 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800724
725 if (n == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700726 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700727 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700728
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700729 return true;
730 }
731
732 // lookup versym for the version definition in this library
733 // note the difference between "version is not requested" (vi == nullptr)
734 // and "version not found". In the first case verneed is kVersymNotNeeded
735 // which implies that the default version can be accepted; the second case results in
736 // verneed = 1 (kVersymGlobal) and implies that we should ignore versioned symbols
737 // for this library and consider only *global* ones.
738 ElfW(Versym) verneed = 0;
739 if (!find_verdef_version_index(vi, &verneed)) {
740 return false;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800741 }
742
743 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800744 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700745 const ElfW(Versym)* verdef = get_versym(n);
746 // skip hidden versions when verneed == kVersymNotNeeded (0)
747 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
748 continue;
749 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700750 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700751 check_symbol_version(verneed, verdef) &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800752 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
753 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700754 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700755 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(s->st_value),
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700756 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700757 *symbol_index = n;
758 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800759 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700760 } while ((gnu_chain_[n++] & 1) == 0);
761
762 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700763 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800764
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700765 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800766}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800767
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700768bool soinfo::elf_lookup(SymbolName& symbol_name,
769 const version_info* vi,
770 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800771 uint32_t hash = symbol_name.elf_hash();
772
773 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700774 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700775 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800776
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700777 ElfW(Versym) verneed = 0;
778 if (!find_verdef_version_index(vi, &verneed)) {
779 return false;
780 }
781
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800782 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
783 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700784 const ElfW(Versym)* verdef = get_versym(n);
785
786 // skip hidden versions when verneed == 0
787 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
788 continue;
789 }
790
791 if (check_symbol_version(verneed, verdef) &&
792 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700793 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800794 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700795 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700796 reinterpret_cast<void*>(s->st_value),
797 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700798 *symbol_index = n;
799 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800800 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800801 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800802
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700803 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700804 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700805 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700806
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700807 *symbol_index = 0;
808 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800809}
810
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700811soinfo::soinfo(android_namespace_t* ns, const char* realpath,
812 const struct stat* file_stat, off64_t file_offset,
813 int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700814 memset(this, 0, sizeof(*this));
815
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700816 if (realpath != nullptr) {
817 realpath_ = realpath;
818 }
819
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800820 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800821 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700822
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700823 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800824 this->st_dev_ = file_stat->st_dev;
825 this->st_ino_ = file_stat->st_ino;
826 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700827 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700828
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800829 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700830 this->namespace_ = ns;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700831}
832
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800833static uint32_t calculate_elf_hash(const char* name) {
834 const uint8_t* name_bytes = reinterpret_cast<const uint8_t*>(name);
835 uint32_t h = 0, g;
836
837 while (*name_bytes) {
838 h = (h << 4) + *name_bytes++;
839 g = h & 0xf0000000;
840 h ^= g;
841 h ^= g >> 24;
842 }
843
844 return h;
845}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800846
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800847uint32_t SymbolName::elf_hash() {
848 if (!has_elf_hash_) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800849 elf_hash_ = calculate_elf_hash(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800850 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700851 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800852
853 return elf_hash_;
854}
855
856uint32_t SymbolName::gnu_hash() {
857 if (!has_gnu_hash_) {
858 uint32_t h = 5381;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700859 const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800860 while (*name != 0) {
861 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
862 }
863
864 gnu_hash_ = h;
865 has_gnu_hash_ = true;
866 }
867
868 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800869}
870
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700871bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
872 soinfo** si_found_in, const soinfo::soinfo_list_t& global_group,
873 const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800874 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700875 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700876
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700877 /* "This element's presence in a shared object library alters the dynamic linker's
878 * symbol resolution algorithm for references within the library. Instead of starting
879 * a symbol search with the executable file, the dynamic linker starts from the shared
880 * object itself. If the shared object fails to supply the referenced symbol, the
881 * dynamic linker then searches the executable file and other shared objects as usual."
882 *
883 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
884 *
885 * Note that this is unlikely since static linker avoids generating
886 * relocations for -Bsymbolic linked dynamic executables.
887 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700888 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700889 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700890 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
891 return false;
892 }
893
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700894 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700895 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700896 }
897 }
898
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700899 // 1. Look for it in global_group
900 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700901 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700902 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700903 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700904 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700905 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
906 error = true;
907 return false;
908 }
909
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700910 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700911 *si_found_in = global_si;
912 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700913 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700914
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700915 return true;
916 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700917
918 if (error) {
919 return false;
920 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700921 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700922
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700923 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700924 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700925 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700926 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700927 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700928 // we already did this - skip
929 return true;
930 }
931
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700932 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700933 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700934 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
935 error = true;
936 return false;
937 }
938
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700939 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700940 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700941 return false;
942 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700943
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700944 return true;
945 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700946
947 if (error) {
948 return false;
949 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700950 }
951
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700952 if (s != nullptr) {
953 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
954 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700955 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
956 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700957 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700958 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700959
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700960 *symbol = s;
961 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700962}
963
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800964class ProtectedDataGuard {
965 public:
966 ProtectedDataGuard() {
967 if (ref_count_++ == 0) {
968 protect_data(PROT_READ | PROT_WRITE);
969 }
970 }
971
972 ~ProtectedDataGuard() {
973 if (ref_count_ == 0) { // overflow
974 __libc_fatal("Too many nested calls to dlopen()");
975 }
976
977 if (--ref_count_ == 0) {
978 protect_data(PROT_READ);
979 }
980 }
981 private:
982 void protect_data(int protection) {
983 g_soinfo_allocator.protect_all(protection);
984 g_soinfo_links_allocator.protect_all(protection);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700985 g_namespace_allocator.protect_all(protection);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800986 }
987
988 static size_t ref_count_;
989};
990
991size_t ProtectedDataGuard::ref_count_ = 0;
992
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700993// Each size has it's own allocator.
994template<size_t size>
995class SizeBasedAllocator {
996 public:
997 static void* alloc() {
998 return allocator_.alloc();
999 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001000
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001001 static void free(void* ptr) {
1002 allocator_.free(ptr);
1003 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001004
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001005 private:
1006 static LinkerBlockAllocator allocator_;
1007};
1008
1009template<size_t size>
1010LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
1011
1012template<typename T>
1013class TypeBasedAllocator {
1014 public:
1015 static T* alloc() {
1016 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
1017 }
1018
1019 static void free(T* ptr) {
1020 SizeBasedAllocator<sizeof(T)>::free(ptr);
1021 }
1022};
1023
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001024class LoadTask {
1025 public:
1026 struct deleter_t {
1027 void operator()(LoadTask* t) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001028 t->~LoadTask();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001029 TypeBasedAllocator<LoadTask>::free(t);
1030 }
1031 };
1032
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001033 static deleter_t deleter;
1034
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001035 static LoadTask* create(const char* name, soinfo* needed_by,
1036 std::unordered_map<const soinfo*, ElfReader>* readers_map) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001037 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001038 return new (ptr) LoadTask(name, needed_by, readers_map);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001039 }
1040
1041 const char* get_name() const {
1042 return name_;
1043 }
1044
1045 soinfo* get_needed_by() const {
1046 return needed_by_;
1047 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001048
1049 soinfo* get_soinfo() const {
1050 return si_;
1051 }
1052
1053 void set_soinfo(soinfo* si) {
1054 si_ = si;
1055 }
1056
1057 off64_t get_file_offset() const {
1058 return file_offset_;
1059 }
1060
1061 void set_file_offset(off64_t offset) {
1062 file_offset_ = offset;
1063 }
1064
1065 int get_fd() const {
1066 return fd_;
1067 }
1068
1069 void set_fd(int fd, bool assume_ownership) {
1070 fd_ = fd;
1071 close_fd_ = assume_ownership;
1072 }
1073
1074 const android_dlextinfo* get_extinfo() const {
1075 return extinfo_;
1076 }
1077
1078 void set_extinfo(const android_dlextinfo* extinfo) {
1079 extinfo_ = extinfo;
1080 }
1081
1082 const ElfReader& get_elf_reader() const {
1083 CHECK(si_ != nullptr);
1084 return (*elf_readers_map_)[si_];
1085 }
1086
1087 ElfReader& get_elf_reader() {
1088 CHECK(si_ != nullptr);
1089 return (*elf_readers_map_)[si_];
1090 }
1091
1092 std::unordered_map<const soinfo*, ElfReader>* get_readers_map() {
1093 return elf_readers_map_;
1094 }
1095
1096 bool read(const char* realpath, off64_t file_size) {
1097 ElfReader& elf_reader = get_elf_reader();
1098 return elf_reader.Read(realpath, fd_, file_offset_, file_size);
1099 }
1100
1101 bool load() {
1102 ElfReader& elf_reader = get_elf_reader();
1103 if (!elf_reader.Load(extinfo_)) {
1104 return false;
1105 }
1106
1107 si_->base = elf_reader.load_start();
1108 si_->size = elf_reader.load_size();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -08001109 si_->set_mapped_by_caller(elf_reader.is_mapped_by_caller());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001110 si_->load_bias = elf_reader.load_bias();
1111 si_->phnum = elf_reader.phdr_count();
1112 si_->phdr = elf_reader.loaded_phdr();
1113
1114 return true;
1115 }
1116
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001117 private:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001118 LoadTask(const char* name, soinfo* needed_by,
1119 std::unordered_map<const soinfo*, ElfReader>* readers_map)
1120 : name_(name), needed_by_(needed_by), si_(nullptr),
1121 fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map) {}
1122
1123 ~LoadTask() {
1124 if (fd_ != -1 && close_fd_) {
1125 close(fd_);
1126 }
1127 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001128
1129 const char* name_;
1130 soinfo* needed_by_;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001131 soinfo* si_;
1132 const android_dlextinfo* extinfo_;
1133 int fd_;
1134 bool close_fd_;
1135 off64_t file_offset_;
1136 std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001137
1138 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
1139};
1140
Ningsheng Jiane93be992014-09-16 15:22:10 +08001141LoadTask::deleter_t LoadTask::deleter;
1142
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001143template <typename T>
1144using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
1145
1146typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001147typedef linked_list_t<const char> StringLinkedList;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001148typedef std::vector<LoadTask*> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001149
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001150
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001151// This function walks down the tree of soinfo dependencies
1152// in breadth-first order and
1153// * calls action(soinfo* si) for each node, and
1154// * terminates walk if action returns false.
1155//
1156// walk_dependencies_tree returns false if walk was terminated
1157// by the action and true otherwise.
1158template<typename F>
1159static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001160 SoinfoLinkedList visit_list;
1161 SoinfoLinkedList visited;
1162
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001163 for (size_t i = 0; i < root_soinfos_size; ++i) {
1164 visit_list.push_back(root_soinfos[i]);
1165 }
1166
1167 soinfo* si;
1168 while ((si = visit_list.pop_front()) != nullptr) {
1169 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -07001170 continue;
1171 }
1172
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001173 if (!action(si)) {
1174 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001175 }
1176
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001177 visited.push_back(si);
1178
1179 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001180 visit_list.push_back(child);
1181 });
1182 }
1183
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001184 return true;
1185}
1186
1187
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001188static const ElfW(Sym)* dlsym_handle_lookup(soinfo* root, soinfo* skip_until,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001189 soinfo** found, SymbolName& symbol_name,
1190 const version_info* vi) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001191 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001192 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001193
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001194 walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) {
1195 if (skip_lookup) {
1196 skip_lookup = current_soinfo != skip_until;
1197 return true;
1198 }
1199
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001200 if (!current_soinfo->find_symbol_by_name(symbol_name, vi, &result)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001201 result = nullptr;
1202 return false;
1203 }
1204
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001205 if (result != nullptr) {
1206 *found = current_soinfo;
1207 return false;
1208 }
1209
1210 return true;
1211 });
1212
1213 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001214}
1215
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001216static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
1217 const char* name,
1218 const version_info* vi,
1219 soinfo** found,
1220 soinfo* caller,
1221 void* handle);
1222
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001223// This is used by dlsym(3). It performs symbol lookup only within the
1224// specified soinfo object and its dependencies in breadth first order.
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001225static const ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found,
1226 const char* name, const version_info* vi) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -07001227 // According to man dlopen(3) and posix docs in the case when si is handle
1228 // of the main executable we need to search not only in the executable and its
1229 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
1230 //
1231 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
1232 // libraries and they are loaded in breath-first (correct) order we can just execute
1233 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
1234 if (si == somain) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001235 return dlsym_linear_lookup(&g_default_namespace, name, vi, found, nullptr, RTLD_DEFAULT);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -07001236 }
1237
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001238 SymbolName symbol_name(name);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001239 return dlsym_handle_lookup(si, nullptr, found, symbol_name, vi);
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001240}
1241
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001242/* This is used by dlsym(3) to performs a global symbol lookup. If the
1243 start value is null (for RTLD_DEFAULT), the search starts at the
1244 beginning of the global solist. Otherwise the search starts at the
1245 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001246 */
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001247static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
1248 const char* name,
1249 const version_info* vi,
1250 soinfo** found,
1251 soinfo* caller,
1252 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001253 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001254
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001255 soinfo::soinfo_list_t& soinfo_list = ns->soinfo_list();
1256 soinfo::soinfo_list_t::iterator start = soinfo_list.begin();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001257
1258 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -07001259 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001260 return nullptr;
1261 } else {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001262 soinfo::soinfo_list_t::iterator it = soinfo_list.find(caller);
1263 CHECK (it != soinfo_list.end());
1264 start = ++it;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001265 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001266 }
1267
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001268 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001269 for (soinfo::soinfo_list_t::iterator it = start, end = soinfo_list.end(); it != end; ++it) {
1270 soinfo* si = *it;
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001271 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
1272 // if the library is opened by application with target api level <= 22
1273 // See http://b/21565766
1274 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 && si->get_target_sdk_version() > 22) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001275 continue;
1276 }
1277
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001278 if (!si->find_symbol_by_name(symbol_name, vi, &s)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001279 return nullptr;
1280 }
1281
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001282 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -08001283 *found = si;
1284 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -06001285 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001286 }
Matt Fischer1698d9e2009-12-31 12:17:56 -06001287
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001288 // If not found - use dlsym_handle_lookup for caller's
1289 // local_group unless it is part of the global group in which
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001290 // case we already did it.
1291 if (s == nullptr && caller != nullptr &&
1292 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001293 return dlsym_handle_lookup(caller->get_local_group_root(),
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001294 (handle == RTLD_NEXT) ? caller : nullptr, found, symbol_name, vi);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001295 }
1296
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001297 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001298 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
1299 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -08001300 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001301
Elliott Hughescade4c32012-12-20 14:42:14 -08001302 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001303}
1304
Kito Chengfa8c05d2013-03-12 14:58:06 +08001305soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001306 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001307 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001308 if (address >= si->base && address - si->base < si->size) {
1309 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001310 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001311 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001312 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001313}
1314
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001315ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
1316 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
1317}
1318
1319static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
1320 return sym->st_shndx != SHN_UNDEF &&
1321 soaddr >= sym->st_value &&
1322 soaddr < sym->st_value + sym->st_size;
1323}
1324
1325ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001326 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001327
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001328 for (size_t i = 0; i < gnu_nbucket_; ++i) {
1329 uint32_t n = gnu_bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001330
1331 if (n == 0) {
1332 continue;
1333 }
1334
1335 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001336 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001337 if (symbol_matches_soaddr(sym, soaddr)) {
1338 return sym;
1339 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001340 } while ((gnu_chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001341 }
1342
1343 return nullptr;
1344}
1345
1346ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001347 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001348
Kito Chengfa8c05d2013-03-12 14:58:06 +08001349 // Search the library's symbol table for any defined symbol which
1350 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001351 for (size_t i = 0; i < nchain_; ++i) {
1352 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001353 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001354 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001355 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001356 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001357
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001358 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001359}
1360
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001361class ZipArchiveCache {
1362 public:
1363 ZipArchiveCache() {}
1364 ~ZipArchiveCache();
1365
1366 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
1367 private:
1368 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
1369
1370 std::unordered_map<std::string, ZipArchiveHandle> cache_;
1371};
1372
1373bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
1374 std::string key(zip_path);
1375
1376 auto it = cache_.find(key);
1377 if (it != cache_.end()) {
1378 *handle = it->second;
1379 return true;
1380 }
1381
1382 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1383 if (fd == -1) {
1384 return false;
1385 }
1386
1387 if (OpenArchiveFd(fd, "", handle) != 0) {
1388 // invalid zip-file (?)
Yabin Cui722072d2016-03-21 17:10:12 -07001389 CloseArchive(handle);
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001390 close(fd);
1391 return false;
1392 }
1393
1394 cache_[key] = *handle;
1395 return true;
1396}
1397
1398ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -07001399 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001400 CloseArchive(it.second);
1401 }
1402}
1403
1404static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001405 const char* const input_path,
1406 off64_t* file_offset, std::string* realpath) {
1407 std::string normalized_path;
1408 if (!normalize_path(input_path, &normalized_path)) {
1409 return -1;
1410 }
1411
1412 const char* const path = normalized_path.c_str();
1413 TRACE("Trying zip file open from path '%s' -> normalized '%s'", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +00001414
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001415 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +00001416 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001417 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +00001418 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001419 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +00001420 if (separator == nullptr) {
1421 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -07001422 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001423
1424 char buf[512];
1425 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
1426 PRINT("Warning: ignoring very long library path: %s", path);
1427 return -1;
1428 }
1429
1430 buf[separator - path] = '\0';
1431
1432 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001433 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +00001434 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1435 if (fd == -1) {
1436 return -1;
1437 }
1438
1439 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001440 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001441 // invalid zip-file (?)
1442 close(fd);
1443 return -1;
1444 }
1445
Simon Baldwinaef71952015-01-16 13:22:54 +00001446 ZipEntry entry;
1447
Yusuke Sato56f40fb2015-06-25 14:56:07 -07001448 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001449 // Entry was not found.
1450 close(fd);
1451 return -1;
1452 }
1453
1454 // Check if it is properly stored
1455 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
1456 close(fd);
1457 return -1;
1458 }
1459
1460 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001461
1462 if (realpath_fd(fd, realpath)) {
1463 *realpath += separator;
1464 } else {
1465 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1466 normalized_path.c_str());
1467 *realpath = normalized_path;
1468 }
1469
Simon Baldwinaef71952015-01-16 13:22:54 +00001470 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001471}
1472
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001473static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
1474 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
1475 if (n < 0 || n >= static_cast<int>(buf_size)) {
1476 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1477 return false;
1478 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001479
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001480 return true;
1481}
1482
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001483static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1484 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001485 const std::vector<std::string>& paths,
1486 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001487 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001488 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001489 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001490 continue;
1491 }
1492
1493 int fd = -1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001494 if (strstr(buf, kZipFileSeparator) != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001495 fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath);
Simon Baldwinaef71952015-01-16 13:22:54 +00001496 }
1497
1498 if (fd == -1) {
1499 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1500 if (fd != -1) {
1501 *file_offset = 0;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001502 if (!realpath_fd(fd, realpath)) {
1503 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
1504 *realpath = buf;
1505 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001506 }
1507 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001508
1509 if (fd != -1) {
1510 return fd;
1511 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001512 }
1513
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001514 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001515}
1516
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001517static int open_library(android_namespace_t* ns,
1518 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001519 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001520 off64_t* file_offset, std::string* realpath) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001521 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001522
Elliott Hughes124fae92012-10-31 14:20:03 -07001523 // 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 -07001524 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001525 int fd = -1;
1526
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001527 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001528 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1529 }
1530
1531 if (fd == -1) {
1532 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001533 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001534 *file_offset = 0;
1535 if (!realpath_fd(fd, realpath)) {
1536 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name);
1537 *realpath = name;
1538 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001539 }
1540 }
1541
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001542 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001543 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001544
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001545 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1546 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 -07001547 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001548 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001549 // Check if the library is accessible
1550 if (fd != -1 && !ns->is_accessible(*realpath)) {
1551 fd = -1;
1552 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001553 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001554
Elliott Hughes124fae92012-10-31 14:20:03 -07001555 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001556 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001557 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001558
Elliott Hughes124fae92012-10-31 14:20:03 -07001559 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001560}
1561
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001562static const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
1563#if !defined(__LP64__)
1564 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001565 if (get_application_target_sdk_version() <= 22) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001566 const char* bname = basename(dt_needed);
1567 if (bname != dt_needed) {
1568 DL_WARN("'%s' library has invalid DT_NEEDED entry '%s'", sopath, dt_needed);
1569 }
1570
1571 return bname;
1572 }
1573#endif
1574 return dt_needed;
1575}
1576
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001577template<typename F>
1578static void for_each_dt_needed(const soinfo* si, F action) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001579 for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001580 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001581 action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
Dima Zavin2e855792009-05-20 18:28:09 -07001582 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001583 }
1584}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001585
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001586template<typename F>
1587static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
1588 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1589 if (d->d_tag == DT_NEEDED) {
1590 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1591 }
1592 }
1593}
1594
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001595static bool load_library(android_namespace_t* ns,
1596 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001597 LoadTaskList* load_tasks,
1598 int rtld_flags,
1599 const std::string& realpath) {
1600 off64_t file_offset = task->get_file_offset();
1601 const char* name = task->get_name();
1602 const android_dlextinfo* extinfo = task->get_extinfo();
1603
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001604 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001605 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001606 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001607 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001608 if (file_offset < 0) {
1609 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001610 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001611 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001612
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001613 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001614 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001615 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001616 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001617 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001618 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001619 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1620 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001621 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001622 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001623
1624 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001625 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1626 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001627 auto predicate = [&](soinfo* si) {
1628 return si->get_st_dev() != 0 &&
1629 si->get_st_ino() != 0 &&
1630 si->get_st_dev() == file_stat.st_dev &&
1631 si->get_st_ino() == file_stat.st_ino &&
1632 si->get_file_offset() == file_offset;
1633 };
1634
1635 soinfo* si = ns->soinfo_list().find_if(predicate);
1636
1637 // check public namespace
1638 if (si == nullptr) {
1639 si = g_public_namespace.find_if(predicate);
1640 if (si != nullptr) {
1641 ns->soinfo_list().push_back(si);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001642 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001643 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001644
1645 if (si != nullptr) {
1646 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
1647 "will return existing soinfo", name, si->get_realpath());
1648 task->set_soinfo(si);
1649 return true;
1650 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001651 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001652
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001653 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001654 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001655 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001656 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001657
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001658 if (!ns->is_accessible(realpath)) {
1659 // do not load libraries if they are not accessible for the specified namespace.
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001660 const char* needed_or_dlopened_by = task->get_needed_by() == nullptr ?
1661 "(unknown)" :
1662 task->get_needed_by()->get_realpath();
1663
1664 DL_ERR("library \"%s\" needed or dlopened by \"%s\" is not accessible for the namespace \"%s\"",
1665 name, needed_or_dlopened_by, ns->get_name());
1666
1667 PRINT("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the"
1668 " namespace: [name=\"%s\", ld_library_paths=\"%s\", default_library_paths=\"%s\","
1669 " permitted_paths=\"%s\"]",
1670 name, realpath.c_str(),
1671 needed_or_dlopened_by,
1672 ns->get_name(),
1673 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
1674 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
1675 android::base::Join(ns->get_permitted_paths(), ':').c_str());
1676
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001677 return false;
1678 }
1679
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001680 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001681 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001682 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001683 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001684
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001685 task->set_soinfo(si);
1686
1687 // Read the ELF header and some of the segments.
1688 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001689 soinfo_free(si);
1690 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001691 return false;
1692 }
1693
1694 // find and set DT_RUNPATH and dt_soname
1695 // Note that these field values are temporary and are
1696 // going to be overwritten on soinfo::prelink_image
1697 // with values from PT_LOAD segments.
1698 const ElfReader& elf_reader = task->get_elf_reader();
1699 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1700 if (d->d_tag == DT_RUNPATH) {
1701 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1702 }
1703 if (d->d_tag == DT_SONAME) {
1704 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1705 }
1706 }
1707
1708 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
1709 load_tasks->push_back(LoadTask::create(name, si, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001710 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001711
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001712 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001713}
1714
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001715static bool load_library(android_namespace_t* ns,
1716 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001717 ZipArchiveCache* zip_archive_cache,
1718 LoadTaskList* load_tasks,
1719 int rtld_flags) {
1720 const char* name = task->get_name();
1721 soinfo* needed_by = task->get_needed_by();
1722 const android_dlextinfo* extinfo = task->get_extinfo();
1723
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001724 off64_t file_offset;
1725 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001726 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001727 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001728 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1729 file_offset = extinfo->library_fd_offset;
1730 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001731
1732 if (!realpath_fd(extinfo->library_fd, &realpath)) {
1733 PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1734 "Will use given name.", name);
1735 realpath = name;
1736 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001737
1738 task->set_fd(extinfo->library_fd, false);
1739 task->set_file_offset(file_offset);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001740 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001741 }
1742
1743 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001744 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001745 if (fd == -1) {
1746 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001747 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001748 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001749
1750 task->set_fd(fd, true);
1751 task->set_file_offset(file_offset);
1752
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001753 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001754}
1755
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001756// Returns true if library was found and false in 2 cases
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001757// 1. (for default namespace only) The library was found but loaded under different
1758// target_sdk_version (*candidate != nullptr)
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001759// 2. The library was not found by soname (*candidate is nullptr)
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001760static bool find_loaded_library_by_soname(android_namespace_t* ns,
1761 const char* name, soinfo** candidate) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001762 *candidate = nullptr;
1763
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001764 // Ignore filename with path.
1765 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001766 return false;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001767 }
1768
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001769 uint32_t target_sdk_version = get_application_target_sdk_version();
1770
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001771 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001772 const char* soname = si->get_soname();
1773 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001774 // If the library was opened under different target sdk version
1775 // skip this step and try to reopen it. The exceptions are
1776 // "libdl.so" and global group. There is no point in skipping
1777 // them because relocation process is going to use them
1778 // in any case.
1779 bool is_libdl = si == solist;
1780 if (is_libdl || (si->get_dt_flags_1() & DF_1_GLOBAL) != 0 ||
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001781 !si->is_linked() || si->get_target_sdk_version() == target_sdk_version ||
1782 ns != &g_default_namespace) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001783 *candidate = si;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001784 return false;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001785 } else if (*candidate == nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001786 // for the different sdk version in the default namespace
1787 // remember the first library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001788 *candidate = si;
1789 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001790 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001791
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001792 return true;
1793 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001794}
1795
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001796static bool find_library_internal(android_namespace_t* ns,
1797 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001798 ZipArchiveCache* zip_archive_cache,
1799 LoadTaskList* load_tasks,
1800 int rtld_flags) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001801 soinfo* candidate;
1802
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001803 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001804 task->set_soinfo(candidate);
1805 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001806 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001807
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001808 if (ns != &g_default_namespace) {
1809 // check public namespace
1810 candidate = g_public_namespace.find_if([&](soinfo* si) {
1811 return strcmp(task->get_name(), si->get_soname()) == 0;
1812 });
1813
1814 if (candidate != nullptr) {
1815 ns->soinfo_list().push_back(candidate);
1816 task->set_soinfo(candidate);
1817 return true;
1818 }
1819 }
1820
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001821 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001822 // of this fact is done by load_library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001823 TRACE("[ '%s' find_loaded_library_by_soname returned false (*candidate=%s@%p). Trying harder...]",
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001824 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001825
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001826 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001827 return true;
1828 } else {
1829 // In case we were unable to load the library but there
1830 // is a candidate loaded under the same soname but different
1831 // sdk level - return it anyways.
1832 if (candidate != nullptr) {
1833 task->set_soinfo(candidate);
1834 return true;
1835 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001836 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001837
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001838 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001839}
1840
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001841static void soinfo_unload(soinfo* si);
1842
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001843// TODO: this is slightly unusual way to construct
1844// the global group for relocation. Not every RTLD_GLOBAL
1845// library is included in this group for backwards-compatibility
1846// reasons.
1847//
1848// This group consists of the main executable, LD_PRELOADs
1849// and libraries with the DF_1_GLOBAL flag set.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001850static soinfo::soinfo_list_t make_global_group(android_namespace_t* ns) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001851 soinfo::soinfo_list_t global_group;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001852 ns->soinfo_list().for_each([&](soinfo* si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001853 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1854 global_group.push_back(si);
1855 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001856 });
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001857
1858 return global_group;
1859}
1860
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001861static void shuffle(std::vector<LoadTask*>* v) {
1862 for (size_t i = 0, size = v->size(); i < size; ++i) {
1863 size_t n = size - i;
1864 size_t r = arc4random_uniform(n);
1865 std::swap((*v)[n-1], (*v)[r]);
1866 }
1867}
1868
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001869// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1870// not their transitive dependencies) as children of the start_with library.
1871// This is false when find_libraries is called for dlopen(), when newly loaded
1872// libraries must form a disjoint tree.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001873static bool find_libraries(android_namespace_t* ns,
1874 soinfo* start_with,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001875 const char* const library_names[],
1876 size_t library_names_count, soinfo* soinfos[],
1877 std::vector<soinfo*>* ld_preloads,
1878 size_t ld_preloads_count, int rtld_flags,
1879 const android_dlextinfo* extinfo,
1880 bool add_as_children) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001881 // Step 0: prepare.
1882 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001883 std::unordered_map<const soinfo*, ElfReader> readers_map;
1884
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001885 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001886 const char* name = library_names[i];
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001887 load_tasks.push_back(LoadTask::create(name, start_with, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001888 }
1889
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001890 // Construct global_group.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001891 soinfo::soinfo_list_t global_group = make_global_group(ns);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001892
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001893 // If soinfos array is null allocate one on stack.
1894 // The array is needed in case of failure; for example
1895 // when library_names[] = {libone.so, libtwo.so} and libone.so
1896 // is loaded correctly but libtwo.so failed for some reason.
1897 // In this case libone.so should be unloaded on return.
1898 // See also implementation of failure_guard below.
1899
1900 if (soinfos == nullptr) {
1901 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1902 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1903 memset(soinfos, 0, soinfos_size);
1904 }
1905
1906 // list of libraries to link - see step 2.
1907 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001908
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001909 auto scope_guard = make_scope_guard([&]() {
1910 for (LoadTask* t : load_tasks) {
1911 LoadTask::deleter(t);
1912 }
1913 });
1914
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001915 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001916 // Housekeeping
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001917 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001918 soinfo_unload(soinfos[i]);
1919 }
1920 });
1921
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001922 ZipArchiveCache zip_archive_cache;
1923
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001924 // Step 1: expand the list of load_tasks to include
1925 // all DT_NEEDED libraries (do not load them just yet)
1926 for (size_t i = 0; i<load_tasks.size(); ++i) {
1927 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07001928 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001929
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001930 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001931 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001932
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001933 if(!find_library_internal(ns, task, &zip_archive_cache, &load_tasks, rtld_flags)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001934 return false;
1935 }
1936
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001937 soinfo* si = task->get_soinfo();
1938
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001939 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001940 needed_by->add_child(si);
1941 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001942
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001943 if (si->is_linked()) {
1944 si->increment_ref_count();
1945 }
1946
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001947 // When ld_preloads is not null, the first
1948 // ld_preloads_count libs are in fact ld_preloads.
1949 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001950 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001951 }
1952
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001953 if (soinfos_count < library_names_count) {
1954 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001955 }
1956 }
1957
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001958 // Step 2: Load libraries in random order (see b/24047022)
1959 LoadTaskList load_list;
1960 for (auto&& task : load_tasks) {
1961 soinfo* si = task->get_soinfo();
1962 auto pred = [&](const LoadTask* t) {
1963 return t->get_soinfo() == si;
1964 };
1965
1966 if (!si->is_linked() &&
1967 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
1968 load_list.push_back(task);
1969 }
1970 }
1971 shuffle(&load_list);
1972
1973 for (auto&& task : load_list) {
1974 if (!task->load()) {
1975 return false;
1976 }
1977 }
1978
1979 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
1980 for (auto&& task : load_tasks) {
1981 soinfo* si = task->get_soinfo();
1982 if (!si->is_linked() && !si->prelink_image()) {
1983 return false;
1984 }
1985 }
1986
1987 // Step 4: Add LD_PRELOADed libraries to the global group for
1988 // future runs. There is no need to explicitly add them to
1989 // the global group for this run because they are going to
1990 // appear in the local group in the correct order.
1991 if (ld_preloads != nullptr) {
1992 for (auto&& si : *ld_preloads) {
1993 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
1994 }
1995 }
1996
1997
1998 // Step 5: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001999 soinfo::soinfo_list_t local_group;
2000 walk_dependencies_tree(
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002001 (start_with != nullptr && add_as_children) ? &start_with : soinfos,
2002 (start_with != nullptr && add_as_children) ? 1 : soinfos_count,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002003 [&] (soinfo* si) {
2004 local_group.push_back(si);
2005 return true;
2006 });
2007
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002008 // We need to increment ref_count in case
2009 // the root of the local group was not linked.
2010 bool was_local_group_root_linked = local_group.front()->is_linked();
2011
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002012 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002013 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002014 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002015 return false;
2016 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002017 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002018 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002019
2020 return true;
2021 });
2022
2023 if (linked) {
2024 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002025 }
2026
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002027 if (!was_local_group_root_linked) {
2028 local_group.front()->increment_ref_count();
2029 }
2030
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002031 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002032}
2033
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002034static soinfo* find_library(android_namespace_t* ns,
2035 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002036 const android_dlextinfo* extinfo,
2037 soinfo* needed_by) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002038 soinfo* si;
2039
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002040 if (name == nullptr) {
2041 si = somain;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002042 } else if (!find_libraries(ns, needed_by, &name, 1, &si, nullptr, 0, rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002043 extinfo, /* add_as_children */ false)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002044 return nullptr;
2045 }
2046
Elliott Hughesd23736e2012-11-01 15:16:56 -07002047 return si;
2048}
Elliott Hughesbedfe382012-08-14 14:07:59 -07002049
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002050static void soinfo_unload(soinfo* root) {
2051 // Note that the library can be loaded but not linked;
2052 // in which case there is no root but we still need
2053 // to walk the tree and unload soinfos involved.
2054 //
2055 // This happens on unsuccessful dlopen, when one of
2056 // the DT_NEEDED libraries could not be linked/found.
2057 if (root->is_linked()) {
2058 root = root->get_local_group_root();
2059 }
2060
2061 if (!root->can_unload()) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002062 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002063 return;
2064 }
2065
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002066 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002067
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002068 if (ref_count == 0) {
2069 soinfo::soinfo_list_t local_unload_list;
2070 soinfo::soinfo_list_t external_unload_list;
2071 soinfo::soinfo_list_t depth_first_list;
2072 depth_first_list.push_back(root);
2073 soinfo* si = nullptr;
2074
2075 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002076 if (local_unload_list.contains(si)) {
2077 continue;
2078 }
2079
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002080 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002081
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002082 if (si->has_min_version(0)) {
2083 soinfo* child = nullptr;
2084 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002085 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
2086 child->get_realpath(), child);
2087
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002088 if (local_unload_list.contains(child)) {
2089 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002090 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002091 external_unload_list.push_back(child);
2092 } else {
2093 depth_first_list.push_front(child);
2094 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002095 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002096 } else {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07002097#if !defined(__work_around_b_24465209__)
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002098 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002099#else
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002100 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002101 for_each_dt_needed(si, [&] (const char* library_name) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002102 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002103 si->get_realpath(), library_name);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002104
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002105 soinfo* needed = find_library(si->get_namespace(),
2106 library_name, RTLD_NOLOAD, nullptr, nullptr);
2107
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002108 if (needed != nullptr) {
2109 // Not found: for example if symlink was deleted between dlopen and dlclose
2110 // Since we cannot really handle errors at this point - print and continue.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002111 PRINT("warning: couldn't find %s needed by %s on unload.",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002112 library_name, si->get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002113 return;
2114 } else if (local_unload_list.contains(needed)) {
2115 // already visited
2116 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002117 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002118 // external group
2119 external_unload_list.push_back(needed);
2120 } else {
2121 // local group
2122 depth_first_list.push_front(needed);
2123 }
2124 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002125#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002126 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002127 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002128
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002129 local_unload_list.for_each([](soinfo* si) {
2130 si->call_destructors();
2131 });
2132
2133 while ((si = local_unload_list.pop_front()) != nullptr) {
2134 notify_gdb_of_unload(si);
2135 soinfo_free(si);
2136 }
2137
2138 while ((si = external_unload_list.pop_front()) != nullptr) {
2139 soinfo_unload(si);
2140 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002141 } else {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002142 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
2143 root->get_realpath(), ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08002144 }
2145}
2146
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002147static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
2148 if (sym_ver == nullptr) {
2149 return sym_name;
2150 }
2151
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002152 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002153}
2154
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002155void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002156 // Use basic string manipulation calls to avoid snprintf.
2157 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
2158 // When debug malloc is enabled, this call returns 0. This in turn causes
2159 // snprintf to do nothing, which causes libraries to fail to load.
2160 // See b/17302493 for further details.
2161 // Once the above bug is fixed, this code can be modified to use
2162 // snprintf again.
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002163 size_t required_len = 0;
2164 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2165 required_len += strlen(g_default_ld_paths[i]) + 1;
2166 }
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002167 if (buffer_size < required_len) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002168 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
2169 "buffer len %zu, required len %zu", buffer_size, required_len);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002170 }
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002171 char* end = buffer;
2172 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2173 if (i > 0) *end++ = ':';
2174 end = stpcpy(end, g_default_ld_paths[i]);
2175 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002176}
2177
Elliott Hughescade4c32012-12-20 14:42:14 -08002178void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08002179 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08002180}
2181
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002182soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo,
2183 void* caller_addr) {
2184 soinfo* const caller = find_containing_library(caller_addr);
2185
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002186 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08002187 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002188 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08002189 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002190
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002191 android_namespace_t* ns = caller != nullptr ? caller->get_namespace() : g_anonymous_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002192
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002193 if (extinfo != nullptr) {
2194 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
2195 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
2196 return nullptr;
2197 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002198
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002199 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07002200 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002201 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
2202 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002203 return nullptr;
2204 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002205
2206 if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 &&
2207 (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) {
2208 DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not "
2209 "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT");
2210 return nullptr;
2211 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002212
2213 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
2214 if (extinfo->library_namespace == nullptr) {
2215 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
2216 return nullptr;
2217 }
2218 ns = extinfo->library_namespace;
2219 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00002220 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002221
2222 ProtectedDataGuard guard;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002223 soinfo* si = find_library(ns, name, flags, extinfo, caller);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002224 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002225 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07002226 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002227
Elliott Hughesd23736e2012-11-01 15:16:56 -07002228 return si;
2229}
2230
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002231int do_dladdr(const void* addr, Dl_info* info) {
2232 // Determine if this address can be found in any library currently mapped.
2233 soinfo* si = find_containing_library(addr);
2234 if (si == nullptr) {
2235 return 0;
2236 }
2237
2238 memset(info, 0, sizeof(Dl_info));
2239
2240 info->dli_fname = si->get_realpath();
2241 // Address at which the shared object is loaded.
2242 info->dli_fbase = reinterpret_cast<void*>(si->base);
2243
2244 // Determine if any symbol in the library contains the specified address.
2245 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
2246 if (sym != nullptr) {
2247 info->dli_sname = si->get_string(sym->st_name);
2248 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
2249 }
2250
2251 return 1;
2252}
2253
2254bool do_dlsym(void* handle, const char* sym_name, const char* sym_ver,
2255 void* caller_addr, void** symbol) {
2256#if !defined(__LP64__)
2257 if (handle == nullptr) {
2258 DL_ERR("dlsym failed: library handle is null");
2259 return false;
2260 }
2261#endif
2262
2263 if (sym_name == nullptr) {
2264 DL_ERR("dlsym failed: symbol name is null");
2265 return false;
2266 }
2267
2268 soinfo* found = nullptr;
2269 const ElfW(Sym)* sym = nullptr;
2270 soinfo* caller = find_containing_library(caller_addr);
2271 android_namespace_t* ns = caller != nullptr ? caller->get_namespace() : g_anonymous_namespace;
2272
2273 version_info vi_instance;
2274 version_info* vi = nullptr;
2275
2276 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002277 vi_instance.name = sym_ver;
2278 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002279 vi = &vi_instance;
2280 }
2281
2282 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2283 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2284 } else {
2285 sym = dlsym_handle_lookup(reinterpret_cast<soinfo*>(handle), &found, sym_name, vi);
2286 }
2287
2288 if (sym != nullptr) {
2289 uint32_t bind = ELF_ST_BIND(sym->st_info);
2290
2291 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2292 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
2293 return true;
2294 }
2295
2296 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2297 return false;
2298 }
2299
2300 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2301 return false;
2302}
2303
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002304void do_dlclose(soinfo* si) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002305 ProtectedDataGuard guard;
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002306 soinfo_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002307}
2308
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002309bool init_namespaces(const char* public_ns_sonames, const char* anon_ns_library_path) {
2310 CHECK(public_ns_sonames != nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002311 if (g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002312 DL_ERR("public namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002313 return false;
2314 }
2315
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002316 std::vector<std::string> sonames = android::base::Split(public_ns_sonames, ":");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002317
2318 ProtectedDataGuard guard;
2319
2320 auto failure_guard = make_scope_guard([&]() {
2321 g_public_namespace.clear();
2322 });
2323
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002324 for (const auto& soname : sonames) {
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -08002325 soinfo* candidate = nullptr;
2326
2327 find_loaded_library_by_soname(&g_default_namespace, soname.c_str(), &candidate);
2328
2329 if (candidate == nullptr) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002330 DL_ERR("error initializing public namespace: \"%s\" was not found"
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002331 " in the default namespace", soname.c_str());
2332 return false;
2333 }
2334
2335 candidate->set_nodelete();
2336 g_public_namespace.push_back(candidate);
2337 }
2338
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002339 g_public_namespace_initialized = true;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002340
2341 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002342 // When the caller is nullptr - create_namespace will take global group
2343 // from the anonymous namespace, which is fine because anonymous namespace
2344 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002345 android_namespace_t* anon_ns =
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002346 create_namespace(nullptr, "(anonymous)", nullptr, anon_ns_library_path,
2347 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002348
2349 if (anon_ns == nullptr) {
2350 g_public_namespace_initialized = false;
2351 return false;
2352 }
2353 g_anonymous_namespace = anon_ns;
2354 failure_guard.disable();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002355 return true;
2356}
2357
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002358android_namespace_t* create_namespace(const void* caller_addr,
2359 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002360 const char* ld_library_path,
2361 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002362 uint64_t type,
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002363 const char* permitted_when_isolated_path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002364 if (!g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002365 DL_ERR("cannot create namespace: public namespace is not initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002366 return nullptr;
2367 }
2368
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002369 soinfo* caller_soinfo = find_containing_library(caller_addr);
2370
2371 android_namespace_t* caller_ns = caller_soinfo != nullptr ?
2372 caller_soinfo->get_namespace() :
2373 g_anonymous_namespace;
2374
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002375 ProtectedDataGuard guard;
2376 std::vector<std::string> ld_library_paths;
2377 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002378 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002379
2380 parse_path(ld_library_path, ":", &ld_library_paths);
2381 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002382 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002383
2384 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2385 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002386 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002387 ns->set_ld_library_paths(std::move(ld_library_paths));
2388 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002389 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002390
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002391 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
2392 // If shared - clone the caller namespace
2393 auto& soinfo_list = caller_ns->soinfo_list();
2394 std::copy(soinfo_list.begin(), soinfo_list.end(), std::back_inserter(ns->soinfo_list()));
2395 } else {
2396 // If not shared - copy only the global group
2397 auto global_group = make_global_group(caller_ns);
2398 std::copy(global_group.begin(), global_group.end(), std::back_inserter(ns->soinfo_list()));
2399 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002400
2401 return ns;
2402}
2403
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002404static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
2405 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2406 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2407 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002408 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2409 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002410
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002411 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002412}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002413
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002414const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2415 if (source_symver < 2 ||
2416 source_symver >= version_infos.size() ||
2417 version_infos[source_symver].name == nullptr) {
2418 return nullptr;
2419 }
2420
2421 return &version_infos[source_symver];
2422}
2423
2424void VersionTracker::add_version_info(size_t source_index,
2425 ElfW(Word) elf_hash,
2426 const char* ver_name,
2427 const soinfo* target_si) {
2428 if (source_index >= version_infos.size()) {
2429 version_infos.resize(source_index+1);
2430 }
2431
2432 version_infos[source_index].elf_hash = elf_hash;
2433 version_infos[source_index].name = ver_name;
2434 version_infos[source_index].target_si = target_si;
2435}
2436
2437bool VersionTracker::init_verneed(const soinfo* si_from) {
2438 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2439
2440 if (verneed_ptr == 0) {
2441 return true;
2442 }
2443
2444 size_t verneed_cnt = si_from->get_verneed_cnt();
2445
2446 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2447 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2448 size_t vernaux_offset = offset + verneed->vn_aux;
2449 offset += verneed->vn_next;
2450
2451 if (verneed->vn_version != 1) {
2452 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2453 return false;
2454 }
2455
2456 const char* target_soname = si_from->get_string(verneed->vn_file);
2457 // find it in dependencies
2458 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002459 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002460 });
2461
2462 if (target_si == nullptr) {
2463 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002464 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002465 return false;
2466 }
2467
2468 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2469 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2470 vernaux_offset += vernaux->vna_next;
2471
2472 const ElfW(Word) elf_hash = vernaux->vna_hash;
2473 const char* ver_name = si_from->get_string(vernaux->vna_name);
2474 ElfW(Half) source_index = vernaux->vna_other;
2475
2476 add_version_info(source_index, elf_hash, ver_name, target_si);
2477 }
2478 }
2479
2480 return true;
2481}
2482
2483bool VersionTracker::init_verdef(const soinfo* si_from) {
2484 return for_each_verdef(si_from,
2485 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2486 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2487 si_from->get_string(verdaux->vda_name), si_from);
2488 return false;
2489 }
2490 );
2491}
2492
2493bool VersionTracker::init(const soinfo* si_from) {
2494 if (!si_from->has_min_version(2)) {
2495 return true;
2496 }
2497
2498 return init_verneed(si_from) && init_verdef(si_from);
2499}
2500
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002501bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2502 const char* sym_name, const version_info** vi) {
2503 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2504 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2505
2506 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2507 *vi = version_tracker.get_version_info(sym_ver);
2508
2509 if (*vi == nullptr) {
2510 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002511 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002512 return false;
2513 }
2514 } else {
2515 // there is no version info
2516 *vi = nullptr;
2517 }
2518
2519 return true;
2520}
2521
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002522#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002523#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002524static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2525 return rela->r_addend;
2526}
2527#else
2528static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002529 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2530 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002531 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2532 }
2533 return 0;
2534}
2535#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002536
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002537template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002538bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2539 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002540 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2541 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002542 if (rel == nullptr) {
2543 return false;
2544 }
2545
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002546 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2547 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2548
2549 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002550 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002551 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002552 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002553
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002554 DEBUG("Processing '%s' relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002555 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002556 continue;
2557 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002558
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002559 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002560 soinfo* lsi = nullptr;
2561
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002562 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002563 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002564 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002565
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002566 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2567 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002568 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002569
2570 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2571 return false;
2572 }
2573
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002574 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002575 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002576 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002577 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002578 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002579 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002580 }
2581
2582 /* IHI0044C AAELF 4.5.1.1:
2583
2584 Libraries are not searched to resolve weak references.
2585 It is not an error for a weak reference to remain unsatisfied.
2586
2587 During linking, the value of an undefined weak reference is:
2588 - Zero if the relocation type is absolute
2589 - The address of the place if the relocation is pc-relative
2590 - The address of nominal base address if the relocation
2591 type is base-relative.
2592 */
2593
2594 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002595 case R_GENERIC_JUMP_SLOT:
2596 case R_GENERIC_GLOB_DAT:
2597 case R_GENERIC_RELATIVE:
2598 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002599#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002600 case R_AARCH64_ABS64:
2601 case R_AARCH64_ABS32:
2602 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002603#elif defined(__x86_64__)
2604 case R_X86_64_32:
2605 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002606#elif defined(__arm__)
2607 case R_ARM_ABS32:
2608#elif defined(__i386__)
2609 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002610#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002611 /*
2612 * The sym_addr was initialized to be zero above, or the relocation
2613 * code below does not care about value of sym_addr.
2614 * No need to do anything.
2615 */
2616 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002617#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002618 case R_X86_64_PC32:
2619 sym_addr = reloc;
2620 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002621#elif defined(__i386__)
2622 case R_386_PC32:
2623 sym_addr = reloc;
2624 break;
2625#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002626 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002627 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002628 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002629 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002630 } else { // We got a definition.
2631#if !defined(__LP64__)
2632 // When relocating dso with text_relocation .text segment is
2633 // not executable. We need to restore elf flags before resolving
2634 // STT_GNU_IFUNC symbol.
2635 bool protect_segments = has_text_relocations &&
2636 lsi == this &&
2637 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2638 if (protect_segments) {
2639 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2640 DL_ERR("can't protect segments for \"%s\": %s",
2641 get_realpath(), strerror(errno));
2642 return false;
2643 }
2644 }
2645#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002646 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002647#if !defined(__LP64__)
2648 if (protect_segments) {
2649 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2650 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2651 get_realpath(), strerror(errno));
2652 return false;
2653 }
2654 }
2655#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002656 }
2657 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002658 }
2659
2660 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002661 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002662 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002663 MARK(rel->r_offset);
2664 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2665 reinterpret_cast<void*>(reloc),
2666 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2667
2668 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002669 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002670 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002671 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002672 MARK(rel->r_offset);
2673 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2674 reinterpret_cast<void*>(reloc),
2675 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2676 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002677 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002678 case R_GENERIC_RELATIVE:
2679 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002680 MARK(rel->r_offset);
2681 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2682 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002683 reinterpret_cast<void*>(load_bias + addend));
2684 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002685 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002686 case R_GENERIC_IRELATIVE:
2687 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002688 MARK(rel->r_offset);
2689 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2690 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002691 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002692 {
2693#if !defined(__LP64__)
2694 // When relocating dso with text_relocation .text segment is
2695 // not executable. We need to restore elf flags for this
2696 // particular call.
2697 if (has_text_relocations) {
2698 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2699 DL_ERR("can't protect segments for \"%s\": %s",
2700 get_realpath(), strerror(errno));
2701 return false;
2702 }
2703 }
2704#endif
2705 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2706#if !defined(__LP64__)
2707 // Unprotect it afterwards...
2708 if (has_text_relocations) {
2709 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2710 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2711 get_realpath(), strerror(errno));
2712 return false;
2713 }
2714 }
2715#endif
2716 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2717 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002718 break;
2719
2720#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002721 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002722 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002723 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002724 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002725 reloc, sym_addr + addend, sym_name);
2726 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002727 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002728 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002729 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002730 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002731 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002732 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002733 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002734 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2735 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002736 if ((min_value <= (sym_addr + addend)) &&
2737 ((sym_addr + addend) <= max_value)) {
2738 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002739 } else {
2740 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002741 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002742 return false;
2743 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002744 }
2745 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002746 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002747 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002748 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002749 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002750 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002751 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002752 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2753 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002754 if ((min_value <= (sym_addr + addend)) &&
2755 ((sym_addr + addend) <= max_value)) {
2756 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002757 } else {
2758 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002759 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002760 return false;
2761 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002762 }
2763 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002764 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002765 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002766 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002767 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002768 reloc, sym_addr + addend, rel->r_offset, sym_name);
2769 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002770 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002771 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002772 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002773 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002774 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002775 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002776 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002777 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2778 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002779 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2780 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2781 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002782 } else {
2783 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002784 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002785 return false;
2786 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002787 }
2788 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002789 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002790 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002791 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002792 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002793 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002794 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002795 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2796 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002797 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2798 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2799 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002800 } else {
2801 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002802 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002803 return false;
2804 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002805 }
2806 break;
2807
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002808 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002809 /*
2810 * ET_EXEC is not supported so this should not happen.
2811 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002812 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002813 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002814 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002815 * R_AARCH64_COPY may only appear in executable objects where e_type is
2816 * set to ET_EXEC.
2817 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002818 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002819 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002820 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002821 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002822 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002823 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002824 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002825 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002826 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002827 break;
2828#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002829 case R_X86_64_32:
2830 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002831 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002832 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2833 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002834 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002835 break;
2836 case R_X86_64_64:
2837 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002838 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002839 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2840 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002841 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002842 break;
2843 case R_X86_64_PC32:
2844 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002845 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002846 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2847 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2848 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002849 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002850 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002851#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002852 case R_ARM_ABS32:
2853 count_relocation(kRelocAbsolute);
2854 MARK(rel->r_offset);
2855 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2856 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2857 break;
2858 case R_ARM_REL32:
2859 count_relocation(kRelocRelative);
2860 MARK(rel->r_offset);
2861 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2862 reloc, sym_addr, rel->r_offset, sym_name);
2863 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2864 break;
2865 case R_ARM_COPY:
2866 /*
2867 * ET_EXEC is not supported so this should not happen.
2868 *
2869 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2870 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002871 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002872 * R_ARM_COPY may only appear in executable objects where e_type is
2873 * set to ET_EXEC.
2874 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002875 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002876 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002877#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002878 case R_386_32:
2879 count_relocation(kRelocRelative);
2880 MARK(rel->r_offset);
2881 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2882 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2883 break;
2884 case R_386_PC32:
2885 count_relocation(kRelocRelative);
2886 MARK(rel->r_offset);
2887 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2888 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2889 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2890 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002891#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002892 default:
2893 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002894 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002895 }
2896 }
2897 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002898}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002899#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002900
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002901void soinfo::call_array(const char* array_name __unused, linker_function_t* functions,
2902 size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002903 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002904 return;
2905 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02002906
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002907 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, get_realpath());
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002908
2909 int begin = reverse ? (count - 1) : 0;
2910 int end = reverse ? -1 : count;
2911 int step = reverse ? -1 : 1;
2912
2913 for (int i = begin; i != end; i += step) {
2914 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002915 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002916 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02002917
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002918 TRACE("[ Done calling %s for '%s' ]", array_name, get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002919}
2920
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002921void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002922 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002923 return;
2924 }
2925
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002926 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Elliott Hughesd23736e2012-11-01 15:16:56 -07002927 function();
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002928 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04002929}
2930
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002931void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002932 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
2933 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002934 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002935}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002936
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002937void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002938 if (constructors_called) {
2939 return;
2940 }
Jesse Hallf5d16932012-01-30 15:39:57 -08002941
Elliott Hughesd23736e2012-11-01 15:16:56 -07002942 // We set constructors_called before actually calling the constructors, otherwise it doesn't
2943 // protect against recursive constructor calls. One simple example of constructor recursion
2944 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
2945 // 1. The program depends on libc, so libc's constructor is called here.
2946 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
2947 // 3. dlopen() calls the constructors on the newly created
2948 // soinfo for libc_malloc_debug_leak.so.
2949 // 4. The debug .so depends on libc, so CallConstructors is
2950 // called again with the libc soinfo. If it doesn't trigger the early-
2951 // out above, the libc constructor will be called again (recursively!).
2952 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002953
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002954 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002955 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07002956 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002957 get_realpath(), preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002958 }
2959
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002960 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002961 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002962 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002963
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002964 TRACE("\"%s\": calling constructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002965
2966 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002967 call_function("DT_INIT", init_func_);
2968 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002969}
David 'Digit' Turner82156792009-05-18 14:37:41 +02002970
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002971void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002972 if (!constructors_called) {
2973 return;
2974 }
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002975 TRACE("\"%s\": calling destructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002976
2977 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002978 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002979
2980 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002981 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002982
2983 // This is needed on second call to dlopen
2984 // after library has been unloaded with RTLD_NODELETE
2985 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002986}
2987
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002988void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002989 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002990 child->parents_.push_back(this);
2991 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002992 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002993}
2994
2995void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002996 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002997 return;
2998 }
2999
3000 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003001 children_.for_each([&] (soinfo* child) {
3002 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003003 return parent == this;
3004 });
3005 });
3006
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003007 parents_.for_each([&] (soinfo* parent) {
3008 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003009 return child == this;
3010 });
3011 });
3012
3013 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003014 parents_.clear();
3015 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003016}
3017
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003018dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003019 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003020 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003021 }
3022
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003023 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003024};
3025
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003026ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003027 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003028 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003029 }
3030
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003031 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003032}
3033
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003034off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003035 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003036 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003037 }
3038
3039 return 0;
3040}
3041
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003042uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07003043 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003044 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07003045 }
3046
3047 return 0;
3048}
3049
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003050uint32_t soinfo::get_dt_flags_1() const {
3051 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003052 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003053 }
3054
3055 return 0;
3056}
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003057
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003058void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
3059 if (has_min_version(1)) {
3060 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003061 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003062 }
3063
3064 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003065 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003066 }
3067
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003068 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003069 }
3070}
3071
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003072void soinfo::set_nodelete() {
3073 rtld_flags_ |= RTLD_NODELETE;
3074}
3075
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003076const char* soinfo::get_realpath() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003077#if defined(__work_around_b_24465209__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003078 if (has_min_version(2)) {
3079 return realpath_.c_str();
3080 } else {
3081 return old_name_;
3082 }
3083#else
3084 return realpath_.c_str();
3085#endif
3086}
3087
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003088void soinfo::set_soname(const char* soname) {
3089#if defined(__work_around_b_24465209__)
3090 if (has_min_version(2)) {
3091 soname_ = soname;
3092 }
3093 strlcpy(old_name_, soname_, sizeof(old_name_));
3094#else
3095 soname_ = soname;
3096#endif
3097}
3098
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003099const char* soinfo::get_soname() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003100#if defined(__work_around_b_24465209__)
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003101 if (has_min_version(2)) {
3102 return soname_;
3103 } else {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003104 return old_name_;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003105 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003106#else
3107 return soname_;
3108#endif
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003109}
3110
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003111// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003112// 'this->flags' does not have FLAG_NEW_SOINFO set.
3113static soinfo::soinfo_list_t g_empty_list;
3114
3115soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003116 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003117 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003118 }
3119
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003120 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003121}
3122
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003123const soinfo::soinfo_list_t& soinfo::get_children() const {
3124 if (has_min_version(0)) {
3125 return children_;
3126 }
3127
3128 return g_empty_list;
3129}
3130
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003131soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003132 if (has_min_version(0)) {
3133 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003134 }
3135
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003136 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003137}
3138
Evgenii Stepanov68650822015-06-10 13:38:39 -07003139static std::vector<std::string> g_empty_runpath;
3140
3141const std::vector<std::string>& soinfo::get_dt_runpath() const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003142 if (has_min_version(3)) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003143 return dt_runpath_;
3144 }
3145
3146 return g_empty_runpath;
3147}
3148
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003149android_namespace_t* soinfo::get_namespace() {
3150 if (has_min_version(3)) {
3151 return namespace_;
3152 }
3153
3154 return &g_default_namespace;
3155}
3156
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003157ElfW(Addr) soinfo::resolve_symbol_address(const ElfW(Sym)* s) const {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003158 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
3159 return call_ifunc_resolver(s->st_value + load_bias);
3160 }
3161
3162 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
3163}
3164
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003165const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003166 if (has_min_version(1) && (index >= strtab_size_)) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003167 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003168 get_realpath(), strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003169 }
3170
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003171 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003172}
3173
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003174bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003175 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003176}
3177
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003178bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003179 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003180}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003181
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003182bool soinfo::is_linked() const {
3183 return (flags_ & FLAG_LINKED) != 0;
3184}
3185
3186bool soinfo::is_main_executable() const {
3187 return (flags_ & FLAG_EXE) != 0;
3188}
3189
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -08003190bool soinfo::is_linker() const {
3191 return (flags_ & FLAG_LINKER) != 0;
3192}
3193
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003194void soinfo::set_linked() {
3195 flags_ |= FLAG_LINKED;
3196}
3197
3198void soinfo::set_linker_flag() {
3199 flags_ |= FLAG_LINKER;
3200}
3201
3202void soinfo::set_main_executable() {
3203 flags_ |= FLAG_EXE;
3204}
3205
3206void soinfo::increment_ref_count() {
3207 local_group_root_->ref_count_++;
3208}
3209
3210size_t soinfo::decrement_ref_count() {
3211 return --local_group_root_->ref_count_;
3212}
3213
3214soinfo* soinfo::get_local_group_root() const {
3215 return local_group_root_;
3216}
3217
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -08003218
3219void soinfo::set_mapped_by_caller(bool mapped_by_caller) {
3220 if (mapped_by_caller) {
3221 flags_ |= FLAG_MAPPED_BY_CALLER;
3222 } else {
3223 flags_ &= ~FLAG_MAPPED_BY_CALLER;
3224 }
3225}
3226
3227bool soinfo::is_mapped_by_caller() const {
3228 return (flags_ & FLAG_MAPPED_BY_CALLER) != 0;
3229}
3230
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003231// This function returns api-level at the time of
3232// dlopen/load. Note that libraries opened by system
3233// will always have 'current' api level.
3234uint32_t soinfo::get_target_sdk_version() const {
3235 if (!has_min_version(2)) {
3236 return __ANDROID_API__;
3237 }
3238
3239 return local_group_root_->target_sdk_version_;
3240}
3241
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003242bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08003243 /* Extract dynamic section */
3244 ElfW(Word) dynamic_flags = 0;
3245 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07003246
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003247 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003248 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003249 if (!relocating_linker) {
Elliott Hughes116b5692016-01-04 17:45:36 -08003250 INFO("[ Linking '%s' ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003251 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003252 }
3253
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003254 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003255 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003256 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003257 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003258 return false;
3259 } else {
3260 if (!relocating_linker) {
3261 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003262 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003263 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003264
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003265#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003266 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
3267 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003268#endif
3269
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003270 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003271 // Note that: "Except for the DT_NULL element at the end of the array,
3272 // and the relative order of DT_NEEDED elements, entries may appear in any order."
3273 //
3274 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003275 uint32_t needed_count = 0;
3276 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
3277 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
3278 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3279 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003280 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003281 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003282 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003283
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003284 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003285 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
3286 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
3287 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
3288 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003289 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003290
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003291 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003292 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003293 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003294 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
3295 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003296
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003297 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003298 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003299 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003300 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
3301 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003302
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003303 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003304 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003305 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003306 return false;
3307 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003308 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003309
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003310 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003311 break;
3312
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003313 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003314 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003315 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003316
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003317 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003318 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003319 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003320
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003321 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003322 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003323 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003324
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003325 case DT_SYMENT:
3326 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003327 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
3328 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003329 return false;
3330 }
3331 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003332
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003333 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003334#if defined(USE_RELA)
3335 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003336 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003337 return false;
3338 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003339#else
3340 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003341 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003342 return false;
3343 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003344#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003345 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003346
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003347 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003348#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003349 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003350#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003351 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003352#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003353 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003354
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003355 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003356#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003357 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003358#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003359 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003360#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003361 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003362
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003363 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003364#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003365 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003366 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003367#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003368 // Ignore for other platforms... (because RTLD_LAZY is not supported)
3369 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003370
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003371 case DT_DEBUG:
3372 // Set the DT_DEBUG entry to the address of _r_debug for GDB
3373 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08003374// FIXME: not working currently for N64
3375// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003376// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08003377// read-only, but the DYNAMIC header claims it is writable.
3378#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003379 if ((dynamic_flags & PF_W) != 0) {
3380 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
3381 }
Chris Dearman99186652014-02-06 20:36:51 -08003382#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08003383 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003384#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003385 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003386 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003387 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003388
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003389 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003390 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003391 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003392
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003393 case DT_ANDROID_RELA:
3394 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3395 break;
3396
3397 case DT_ANDROID_RELASZ:
3398 android_relocs_size_ = d->d_un.d_val;
3399 break;
3400
3401 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003402 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003403 return false;
3404
3405 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003406 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003407 return false;
3408
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003409 case DT_RELAENT:
3410 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003411 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003412 return false;
3413 }
3414 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003415
3416 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003417 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003418 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003419
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003420 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003421 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003422 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003423
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003424 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003425 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003426 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003427
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003428#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003429 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003430 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003431 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003432
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003433 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003434 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003435 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003436
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003437 case DT_RELENT:
3438 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003439 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003440 return false;
3441 }
3442 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003443
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003444 case DT_ANDROID_REL:
3445 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3446 break;
3447
3448 case DT_ANDROID_RELSZ:
3449 android_relocs_size_ = d->d_un.d_val;
3450 break;
3451
3452 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003453 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003454 return false;
3455
3456 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003457 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003458 return false;
3459
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003460 // "Indicates that all RELATIVE relocations have been concatenated together,
3461 // and specifies the RELATIVE relocation count."
3462 //
3463 // TODO: Spec also mentions that this can be used to optimize relocation process;
3464 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003465 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003466 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003467
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003468 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003469 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003470 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003471
3472 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003473 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003474 return false;
3475
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003476#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003477 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003478 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003479 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003480 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003481
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003482 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003483 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003484 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003485 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003486
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003487 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003488 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003489 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003490 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003491
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003492 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003493 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003494 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003495
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003496 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003497 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003498 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003499 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003500
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003501 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003502 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003503 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003504
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003505 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003506 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003507 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
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_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003511 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003512 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003513
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003514 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003515#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003516 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003517 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003518#else
3519 has_text_relocations = true;
3520 break;
3521#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003522
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003523 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003524 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003525 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003526
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003527 case DT_NEEDED:
3528 ++needed_count;
3529 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003530
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003531 case DT_FLAGS:
3532 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003533#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003534 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003535 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003536#else
3537 has_text_relocations = true;
3538#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003539 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003540 if (d->d_un.d_val & DF_SYMBOLIC) {
3541 has_DT_SYMBOLIC = true;
3542 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003543 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003544
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003545 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003546 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003547
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003548 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -07003549 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 -07003550 }
3551 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003552#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003553 case DT_MIPS_RLD_MAP:
3554 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3555 {
3556 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3557 *dp = &_r_debug;
3558 }
3559 break;
Raghu Gandham68815722014-12-18 19:12:19 -08003560 case DT_MIPS_RLD_MAP2:
3561 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
3562 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003563 r_debug** dp = reinterpret_cast<r_debug**>(
3564 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003565 *dp = &_r_debug;
3566 }
3567 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003568
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003569 case DT_MIPS_RLD_VERSION:
3570 case DT_MIPS_FLAGS:
3571 case DT_MIPS_BASE_ADDRESS:
3572 case DT_MIPS_UNREFEXTNO:
3573 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003574
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003575 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003576 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003577 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003578
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003579 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003580 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003581 break;
3582
3583 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003584 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003585 break;
3586#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003587 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3588 case DT_BIND_NOW:
3589 break;
3590
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003591 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003592 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3593 break;
3594
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003595 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003596 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3597 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003598 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003599 verdef_cnt_ = d->d_un.d_val;
3600 break;
3601
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003602 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003603 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3604 break;
3605
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003606 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003607 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003608 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003609
Evgenii Stepanov68650822015-06-10 13:38:39 -07003610 case DT_RUNPATH:
3611 // this is parsed after we have strtab initialized (see below).
3612 break;
3613
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003614 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003615 if (!relocating_linker) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003616 DL_WARN("%s: unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003617 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3618 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003619 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003620 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003621 }
3622
Duane Sandbc425c72015-06-01 16:29:14 -07003623#if defined(__mips__) && !defined(__LP64__)
3624 if (!mips_check_and_adjust_fp_modes()) {
3625 return false;
3626 }
3627#endif
3628
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003629 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003630 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003631
3632 // Sanity checks.
3633 if (relocating_linker && needed_count != 0) {
3634 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3635 return false;
3636 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003637 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003638 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003639 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003640 return false;
3641 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003642 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003643 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003644 return false;
3645 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003646 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003647 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003648 return false;
3649 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003650
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003651 // second pass - parse entries relying on strtab
3652 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003653 switch (d->d_tag) {
3654 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003655 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003656 break;
3657 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003658 set_dt_runpath(get_string(d->d_un.d_val));
3659 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003660 }
3661 }
3662
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003663 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003664 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003665 // because they can't find dt_needed library by soname.
3666 // This workaround should keep them working. (applies only
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003667 // for apps targeting sdk version <=22). Make an exception for
3668 // the main executable and linker; they do not need to have dt_soname
3669 if (soname_ == nullptr && this != somain && (flags_ & FLAG_LINKER) == 0 &&
3670 get_application_target_sdk_version() <= 22) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003671 soname_ = basename(realpath_.c_str());
3672 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3673 get_realpath(), soname_);
3674 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003675 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003676}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003677
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003678bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3679 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003680
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003681 local_group_root_ = local_group.front();
3682 if (local_group_root_ == nullptr) {
3683 local_group_root_ = this;
3684 }
3685
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003686 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3687 target_sdk_version_ = get_application_target_sdk_version();
3688 }
3689
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003690 VersionTracker version_tracker;
3691
3692 if (!version_tracker.init(this)) {
3693 return false;
3694 }
3695
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003696#if !defined(__LP64__)
3697 if (has_text_relocations) {
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003698 // Fail if app is targeting sdk version > 22
Dmitriy Ivanov80687862015-10-09 13:58:46 -07003699 if (get_application_target_sdk_version() > 22) {
Dmitriy Ivanovfae39d22015-10-13 11:07:56 -07003700 PRINT("%s: has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003701 DL_ERR("%s: has text relocations", get_realpath());
3702 return false;
3703 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003704 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003705 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003706 DL_WARN("%s has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003707 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003708 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3709 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003710 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003711 return false;
3712 }
3713 }
3714#endif
3715
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003716 if (android_relocs_ != nullptr) {
3717 // check signature
3718 if (android_relocs_size_ > 3 &&
3719 android_relocs_[0] == 'A' &&
3720 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003721 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003722 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003723 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003724
3725 bool relocated = false;
3726 const uint8_t* packed_relocs = android_relocs_ + 4;
3727 const size_t packed_relocs_size = android_relocs_size_ - 4;
3728
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003729 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003730 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003731 packed_reloc_iterator<sleb128_decoder>(
3732 sleb128_decoder(packed_relocs, packed_relocs_size)),
3733 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003734
3735 if (!relocated) {
3736 return false;
3737 }
3738 } else {
3739 DL_ERR("bad android relocation header.");
3740 return false;
3741 }
3742 }
3743
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003744#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003745 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003746 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003747 if (!relocate(version_tracker,
3748 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003749 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003750 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003751 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003752 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003753 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003754 if (!relocate(version_tracker,
3755 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003756 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003757 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003758 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003759#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003760 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003761 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003762 if (!relocate(version_tracker,
3763 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003764 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003765 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003766 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003767 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003768 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003769 if (!relocate(version_tracker,
3770 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003771 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003772 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003773 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003774#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003775
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003776#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003777 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003778 return false;
3779 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003780#endif
3781
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003782 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003783
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003784#if !defined(__LP64__)
3785 if (has_text_relocations) {
3786 // All relocations are done, we can protect our segments back to read-only.
3787 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3788 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003789 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003790 return false;
3791 }
3792 }
3793#endif
3794
Mingwei Shibe910522015-11-12 07:02:14 +00003795 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
3796 // itself --- it can't make system calls yet, and will have to call protect_relro later.
3797 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003798 return false;
3799 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003800
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003801 /* Handle serializing/sharing the RELRO segment */
3802 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3803 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3804 extinfo->relro_fd) < 0) {
3805 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003806 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003807 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003808 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003809 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3810 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3811 extinfo->relro_fd) < 0) {
3812 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003813 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003814 return false;
3815 }
3816 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003817
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003818 notify_gdb_of_load(this);
3819 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003820}
3821
Mingwei Shibe910522015-11-12 07:02:14 +00003822bool soinfo::protect_relro() {
3823 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3824 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
3825 get_realpath(), strerror(errno));
3826 return false;
3827 }
3828 return true;
3829}
3830
Nick Kralevich468319c2011-11-11 15:53:17 -08003831/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003832 * This function add vdso to internal dso list.
3833 * It helps to stack unwinding through signal handlers.
3834 * Also, it makes bionic more like glibc.
3835 */
Kito Cheng812fd422014-03-25 22:53:56 +08003836static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003837#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08003838 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003839 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08003840 return;
3841 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003842
Dmitriy Ivanovd9b08a02015-11-16 13:17:27 -08003843 soinfo* si = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003844
Elliott Hughes0266ae52014-02-10 17:46:57 -08003845 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
3846 si->phnum = ehdr_vdso->e_phnum;
3847 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
3848 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003849 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003850
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003851 si->prelink_image();
3852 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003853#endif
3854}
3855
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003856/* gdb expects the linker to be in the debug shared object list.
3857 * Without this, gdb has trouble locating the linker's ".text"
3858 * and ".plt" sections. Gdb could also potentially use this to
3859 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
Dimitry Ivanov64001292016-02-17 14:13:06 -08003860 * Note that the linker shouldn't be on the soinfo list.
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003861 */
3862static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dimitry Ivanov8d22dd52016-02-16 13:43:35 -08003863 static link_map linker_link_map_for_gdb;
3864#if defined(__LP64__)
3865 static char kLinkerPath[] = "/system/bin/linker64";
3866#else
3867 static char kLinkerPath[] = "/system/bin/linker";
3868#endif
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003869
Dimitry Ivanov8d22dd52016-02-16 13:43:35 -08003870 linker_link_map_for_gdb.l_addr = linker_base;
3871 linker_link_map_for_gdb.l_name = kLinkerPath;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003872
3873 /*
3874 * Set the dynamic field in the link map otherwise gdb will complain with
3875 * the following:
3876 * warning: .dynamic section for "/system/bin/linker" is not at the
3877 * expected address (wrong library or version mismatch?)
3878 */
3879 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
3880 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
3881 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Dimitry Ivanov8d22dd52016-02-16 13:43:35 -08003882 &linker_link_map_for_gdb.l_ld, nullptr);
3883
3884 insert_link_map_into_debug_map(&linker_link_map_for_gdb);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003885}
3886
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003887static void init_default_namespace() {
3888 g_default_namespace.set_name("(default)");
3889 g_default_namespace.set_isolated(false);
3890
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003891 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3892 somain->load_bias);
3893 const char* bname = basename(interp);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003894 if (bname && (strcmp(bname, "linker_asan") == 0 || strcmp(bname, "linker_asan64") == 0)) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003895 g_default_ld_paths = kAsanDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003896 } else {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003897 g_default_ld_paths = kDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003898 }
3899
3900 std::vector<std::string> ld_default_paths;
3901 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
3902 ld_default_paths.push_back(g_default_ld_paths[i]);
3903 }
3904
3905 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003906};
3907
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07003908extern "C" int __system_properties_init(void);
3909
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003910/*
Nick Kralevich468319c2011-11-11 15:53:17 -08003911 * This code is called after the linker has linked itself and
3912 * fixed it's own GOT. It is safe to make references to externs
3913 * and other non-local data at this point.
3914 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08003915static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04003916#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003917 struct timeval t0, t1;
3918 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04003919#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003920
Elliott Hughes1801db32015-06-08 18:04:00 -07003921 // Sanitize the environment.
3922 __libc_init_AT_SECURE(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01003923
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07003924 // Initialize system properties
3925 __system_properties_init(); // may use 'environ'
3926
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003927 debuggerd_init();
3928
3929 // Get a few environment variables.
Elliott Hughes1801db32015-06-08 18:04:00 -07003930 const char* LD_DEBUG = getenv("LD_DEBUG");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003931 if (LD_DEBUG != nullptr) {
3932 g_ld_debug_verbosity = atoi(LD_DEBUG);
3933 }
3934
Elliott Hughes116b5692016-01-04 17:45:36 -08003935#if defined(__LP64__)
3936 INFO("[ Android dynamic linker (64-bit) ]");
3937#else
3938 INFO("[ Android dynamic linker (32-bit) ]");
3939#endif
3940
Elliott Hughes1801db32015-06-08 18:04:00 -07003941 // These should have been sanitized by __libc_init_AT_SECURE, but the test
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003942 // doesn't cost us anything.
3943 const char* ldpath_env = nullptr;
3944 const char* ldpreload_env = nullptr;
Elliott Hughes1801db32015-06-08 18:04:00 -07003945 if (!getauxval(AT_SECURE)) {
3946 ldpath_env = getenv("LD_LIBRARY_PATH");
Elliott Hughes116b5692016-01-04 17:45:36 -08003947 if (ldpath_env != nullptr) {
3948 INFO("[ LD_LIBRARY_PATH set to '%s' ]", ldpath_env);
3949 }
Elliott Hughes1801db32015-06-08 18:04:00 -07003950 ldpreload_env = getenv("LD_PRELOAD");
Elliott Hughes116b5692016-01-04 17:45:36 -08003951 if (ldpreload_env != nullptr) {
3952 INFO("[ LD_PRELOAD set to '%s' ]", ldpreload_env);
3953 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003954 }
3955
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003956 soinfo* si = soinfo_alloc(&g_default_namespace, args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003957 if (si == nullptr) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08003958 __libc_fatal("Couldn't allocate soinfo: out of memory?");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003959 }
3960
3961 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003962 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003963 link_map* map = &(si->link_map_head);
3964
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -08003965 // Register the main executable and the linker upfront to have
3966 // gdb aware of them before loading the rest of the dependency
3967 // tree.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003968 map->l_addr = 0;
3969 map->l_name = args.argv[0];
Dimitry Ivanovf3064e42016-02-17 15:25:25 -08003970 insert_link_map_into_debug_map(map);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003971 init_linker_info_for_gdb(linker_base);
3972
3973 // Extract information passed from the kernel.
3974 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
3975 si->phnum = args.getauxval(AT_PHNUM);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003976
3977 /* Compute the value of si->base. We can't rely on the fact that
3978 * the first entry is the PHDR because this will not be true
3979 * for certain executables (e.g. some in the NDK unit test suite)
3980 */
3981 si->base = 0;
3982 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
3983 si->load_bias = 0;
3984 for (size_t i = 0; i < si->phnum; ++i) {
3985 if (si->phdr[i].p_type == PT_PHDR) {
3986 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
3987 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
3988 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07003989 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003990 }
3991 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07003992
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003993 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
3994 if (elf_hdr->e_type != ET_DYN) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08003995 __libc_fatal("\"%s\": error: only position independent executables (PIE) are supported.",
3996 args.argv[0]);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003997 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003998
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003999 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
4000 parse_LD_LIBRARY_PATH(ldpath_env);
4001 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01004002
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004003 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004004
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004005 init_default_namespace();
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004006
Dmitriy Ivanov67181252015-01-07 15:48:25 -08004007 if (!si->prelink_image()) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004008 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
Dmitriy Ivanov67181252015-01-07 15:48:25 -08004009 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004010
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07004011 // add somain to global group
4012 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
4013
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004014 // Load ld_preloads and dependencies.
4015 StringLinkedList needed_library_name_list;
4016 size_t needed_libraries_count = 0;
4017 size_t ld_preloads_count = 0;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07004018
4019 for (const auto& ld_preload_name : g_ld_preload_names) {
4020 needed_library_name_list.push_back(ld_preload_name.c_str());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004021 ++needed_libraries_count;
Dmitriy Ivanovf8093a92015-04-28 18:09:53 -07004022 ++ld_preloads_count;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004023 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004024
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004025 for_each_dt_needed(si, [&](const char* name) {
4026 needed_library_name_list.push_back(name);
4027 ++needed_libraries_count;
4028 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004029
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004030 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004031
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004032 memset(needed_library_names, 0, sizeof(needed_library_names));
4033 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004034
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07004035 if (needed_libraries_count > 0 &&
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004036 !find_libraries(&g_default_namespace, si, needed_library_names, needed_libraries_count,
4037 nullptr, &g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07004038 /* add_as_children */ true)) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004039 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004040 } else if (needed_libraries_count == 0) {
4041 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004042 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004043 }
4044 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004045 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004046
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004047 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07004048
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08004049 {
4050 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06004051
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08004052 si->call_pre_init_constructors();
4053
4054 /* After the prelink_image, the si->load_bias is initialized.
4055 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
4056 * We need to update this value for so exe here. So Unwind_Backtrace
4057 * for some arch like x86 could work correctly within so exe.
4058 */
4059 map->l_addr = si->load_bias;
4060 si->call_constructors();
4061 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04004062
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004063#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004064 gettimeofday(&t1, nullptr);
4065 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
4066 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
4067 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004068#endif
4069#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004070 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
4071 linker_stats.count[kRelocAbsolute],
4072 linker_stats.count[kRelocRelative],
4073 linker_stats.count[kRelocCopy],
4074 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004075#endif
4076#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004077 {
4078 unsigned n;
4079 unsigned i;
4080 unsigned count = 0;
4081 for (n = 0; n < 4096; n++) {
4082 if (bitmask[n]) {
4083 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004084#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004085 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004086#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004087 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004088#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004089 if (x & 1) {
4090 count++;
4091 }
4092 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004093 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004094 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004095 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004096 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
4097 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004098#endif
4099
4100#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004101 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004102#endif
4103
Dimitry Ivanove687d062016-02-16 13:25:29 -08004104 ElfW(Addr) entry = args.getauxval(AT_ENTRY);
4105 TRACE("[ Ready to execute '%s' @ %p ]", si->get_realpath(), reinterpret_cast<void*>(entry));
4106 return entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004107}
Nick Kralevich468319c2011-11-11 15:53:17 -08004108
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004109/* Compute the load-bias of an existing executable. This shall only
4110 * be used to compute the load bias of an executable or shared library
4111 * that was loaded by the kernel itself.
4112 *
4113 * Input:
4114 * elf -> address of ELF header, assumed to be at the start of the file.
4115 * Return:
4116 * load bias, i.e. add the value of any p_vaddr in the file to get
4117 * the corresponding address in memory.
4118 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004119static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
4120 ElfW(Addr) offset = elf->e_phoff;
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004121 const ElfW(Phdr)* phdr_table =
4122 reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004123 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004124
Elliott Hughes0266ae52014-02-10 17:46:57 -08004125 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08004126 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08004127 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004128 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08004129 }
4130 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004131}
4132
Mingwei Shibe910522015-11-12 07:02:14 +00004133static void __linker_cannot_link(KernelArgumentBlock& args) {
4134 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
4135}
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004136
Nick Kralevich468319c2011-11-11 15:53:17 -08004137/*
4138 * This is the entry point for the linker, called from begin.S. This
4139 * method is responsible for fixing the linker's own relocations, and
4140 * then calling __linker_init_post_relocation().
4141 *
4142 * Because this method is called before the linker has fixed it's own
4143 * relocations, any attempt to reference an extern variable, extern
4144 * function, or other GOT reference will generate a segfault.
4145 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004146extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004147 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08004148
Elliott Hughes0266ae52014-02-10 17:46:57 -08004149 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004150 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004151 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08004152 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08004153
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004154 soinfo linker_so(nullptr, nullptr, nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08004155
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004156 // If the linker is not acting as PT_INTERP entry_point is equal to
4157 // _start. Which means that the linker is running as an executable and
4158 // already linked by PT_INTERP.
4159 //
4160 // This happens when user tries to run 'adb shell /system/bin/linker'
4161 // see also https://code.google.com/p/android/issues/detail?id=63174
4162 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004163 __libc_format_fd(STDOUT_FILENO,
4164 "This is %s, the helper program for shared library executables.\n",
4165 args.argv[0]);
4166 exit(0);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004167 }
4168
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004169 linker_so.base = linker_addr;
4170 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
4171 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07004172 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004173 linker_so.phdr = phdr;
4174 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004175 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07004176
Mingwei Shibe910522015-11-12 07:02:14 +00004177 // Prelink the linker so we can access linker globals.
4178 if (!linker_so.prelink_image()) __linker_cannot_link(args);
4179
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07004180 // This might not be obvious... The reasons why we pass g_empty_list
4181 // in place of local_group here are (1) we do not really need it, because
4182 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
4183 // itself without having to look into local_group and (2) allocators
4184 // are not yet initialized, and therefore we cannot use linked_list.push_*
4185 // functions at this point.
Mingwei Shibe910522015-11-12 07:02:14 +00004186 if (!linker_so.link_image(g_empty_list, g_empty_list, nullptr)) __linker_cannot_link(args);
Elliott Hughesd23736e2012-11-01 15:16:56 -07004187
Mingwei Shibe910522015-11-12 07:02:14 +00004188#if defined(__i386__)
4189 // On x86, we can't make system calls before this point.
4190 // We can't move this up because this needs to assign to a global.
4191 // Note that until we call __libc_init_main_thread below we have
4192 // no TLS, so you shouldn't make a system call that can fail, because
4193 // it will SEGV when it tries to set errno.
4194 __libc_init_sysinfo(args);
4195#endif
4196
4197 // Initialize the main thread (including TLS, so system calls really work).
Elliott Hughesd2948632015-07-21 11:57:09 -07004198 __libc_init_main_thread(args);
Dmitriy Ivanov14241402014-08-26 14:16:52 -07004199
Mingwei Shibe910522015-11-12 07:02:14 +00004200 // We didn't protect the linker's RELRO pages in link_image because we
4201 // couldn't make system calls on x86 at that point, but we can now...
4202 if (!linker_so.protect_relro()) __linker_cannot_link(args);
4203
Josh Gao93c0f5e2015-10-06 11:08:13 -07004204 // Initialize the linker's static libc's globals
4205 __libc_init_globals(args);
4206
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004207 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004208 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07004209
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004210 // Initialize static variables. Note that in order to
4211 // get correct libdl_info we need to call constructors
4212 // before get_libdl_info().
4213 solist = get_libdl_info();
4214 sonext = get_libdl_info();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004215 g_default_namespace.soinfo_list().push_back(get_libdl_info());
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004216
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004217 // We have successfully fixed our own relocations. It's safe to run
4218 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07004219 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08004220 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004221
Elliott Hughes116b5692016-01-04 17:45:36 -08004222 INFO("[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address));
Elliott Hughes611f9562015-01-23 10:43:58 -08004223
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004224 // Return the address that the calling assembly stub should jump to.
4225 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08004226}