blob: c07b21d76bd2bd170e1b63dd2bd73606baac0fb5 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002 * Copyright (C) 2008 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Dmitriy Ivanov19133522015-06-02 17:36:54 -070029#include <android/api-level.h>
Elliott Hughes46882792012-08-03 16:49:39 -070030#include <errno.h>
31#include <fcntl.h>
Elliott Hughes0266ae52014-02-10 17:46:57 -080032#include <inttypes.h>
Elliott Hughes46882792012-08-03 16:49:39 -070033#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
Elliott Hughes46882792012-08-03 16:49:39 -070037#include <sys/mman.h>
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -080038#include <sys/param.h>
Elliott Hughes46882792012-08-03 16:49:39 -070039#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070041#include <new>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070042#include <string>
Dmitriy Ivanovb4827502015-09-28 16:38:31 -070043#include <unordered_map>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070044#include <vector>
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070045
Elliott Hughes46882792012-08-03 16:49:39 -070046// Private C library headers.
Elliott Hugheseb847bc2013-10-09 15:50:50 -070047#include "private/bionic_tls.h"
48#include "private/KernelArgumentBlock.h"
49#include "private/ScopedPthreadMutexLocker.h"
Dmitriy Ivanov14669a92014-09-05 16:42:53 -070050#include "private/ScopeGuard.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051
52#include "linker.h"
Dmitriy Ivanovc9ce70d2015-03-10 15:30:26 -070053#include "linker_block_allocator.h"
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -080054#include "linker_gdb_support.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055#include "linker_debug.h"
Dmitriy Ivanov18870d32015-04-22 13:10:04 -070056#include "linker_sleb128.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020057#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080058#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080059#include "linker_reloc_iterators.h"
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070060#include "linker_utils.h"
tony.ys_liub4474402015-07-29 18:00:22 +080061
Elliott Hughes939a7e02015-12-04 15:27:46 -080062#include "android-base/strings.h"
Simon Baldwinaef71952015-01-16 13:22:54 +000063#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
Josh Gao93c0f5e2015-10-06 11:08:13 -070065extern void __libc_init_globals(KernelArgumentBlock&);
Elliott Hughes1801db32015-06-08 18:04:00 -070066extern void __libc_init_AT_SECURE(KernelArgumentBlock&);
67
68// Override macros to use C++ style casts.
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080069#undef ELF_ST_TYPE
70#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
71
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070072struct android_namespace_t {
73 public:
74 android_namespace_t() : name_(nullptr), is_isolated_(false) {}
75
76 const char* get_name() const { return name_; }
77 void set_name(const char* name) { name_ = name; }
78
79 bool is_isolated() const { return is_isolated_; }
80 void set_isolated(bool isolated) { is_isolated_ = isolated; }
81
82 const std::vector<std::string>& get_ld_library_paths() const {
83 return ld_library_paths_;
84 }
85 void set_ld_library_paths(std::vector<std::string>&& library_paths) {
86 ld_library_paths_ = library_paths;
87 }
88
89 const std::vector<std::string>& get_default_library_paths() const {
90 return default_library_paths_;
91 }
92 void set_default_library_paths(std::vector<std::string>&& library_paths) {
93 default_library_paths_ = library_paths;
94 }
95
Dimitry Ivanovd17a3772016-03-01 13:11:28 -080096 const std::vector<std::string>& get_permitted_paths() const {
97 return permitted_paths_;
98 }
Dimitry Ivanov284ae352015-12-08 10:47:13 -080099 void set_permitted_paths(std::vector<std::string>&& permitted_paths) {
100 permitted_paths_ = permitted_paths;
101 }
102
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700103 soinfo::soinfo_list_t& soinfo_list() { return soinfo_list_; }
104
105 // For isolated namespaces - checks if the file is on the search path;
106 // always returns true for not isolated namespace.
107 bool is_accessible(const std::string& path);
108
109 private:
110 const char* name_;
111 bool is_isolated_;
112 std::vector<std::string> ld_library_paths_;
113 std::vector<std::string> default_library_paths_;
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800114 std::vector<std::string> permitted_paths_;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700115 soinfo::soinfo_list_t soinfo_list_;
116
117 DISALLOW_COPY_AND_ASSIGN(android_namespace_t);
118};
119
120android_namespace_t g_default_namespace;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800121android_namespace_t* g_anonymous_namespace = &g_default_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700122
Elliott Hughes0266ae52014-02-10 17:46:57 -0800123static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700125static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
126static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200127
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700128static LinkerTypeAllocator<android_namespace_t> g_namespace_allocator;
129
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700130static soinfo* solist;
131static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700132static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800133
Elliott Hughes1728b232014-05-14 10:02:03 -0700134static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700135#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700136 "/system/lib64",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800137 "/vendor/lib64",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700138#else
Elliott Hughes124fae92012-10-31 14:20:03 -0700139 "/system/lib",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800140 "/vendor/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700141#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700142 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700143};
David Bartleybc3a5c22009-06-02 18:27:28 -0700144
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700145static const char* const kAsanDefaultLdPaths[] = {
146#if defined(__LP64__)
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700147 "/data/lib64",
148 "/system/lib64",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800149 "/data/vendor/lib64",
150 "/vendor/lib64",
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700151#else
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700152 "/data/lib",
153 "/system/lib",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800154 "/data/vendor/lib",
155 "/vendor/lib",
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700156#endif
157 nullptr
158};
159
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700160static const ElfW(Versym) kVersymNotNeeded = 0;
161static const ElfW(Versym) kVersymGlobal = 1;
162
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700163static const char* const* g_default_ld_paths;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700164static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800165
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700166static std::vector<soinfo*> g_ld_preloads;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600167
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700168static bool g_public_namespace_initialized;
169static soinfo::soinfo_list_t g_public_namespace;
170
Elliott Hughes1728b232014-05-14 10:02:03 -0700171__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800172
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700173__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700174
Evgenii Stepanov68650822015-06-10 13:38:39 -0700175static std::string dirname(const char *path) {
176 const char* last_slash = strrchr(path, '/');
177 if (last_slash == path) return "/";
178 else if (last_slash == nullptr) return ".";
179 else
180 return std::string(path, last_slash - path);
181}
182
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700184struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700185 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700186};
187
188static linker_stats_t linker_stats;
189
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800190void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700191 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700192}
193#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800194void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700195}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196#endif
197
198#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800199uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200#endif
201
Dima Zavin2e855792009-05-20 18:28:09 -0700202static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700203
Elliott Hughes650be4e2013-03-05 18:47:58 -0800204char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700205 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700206}
207
Elliott Hughes650be4e2013-03-05 18:47:58 -0800208size_t linker_get_error_buffer_size() {
209 return sizeof(__linker_dl_err_buf);
210}
211
Elliott Hughesbedfe382012-08-14 14:07:59 -0700212static void notify_gdb_of_load(soinfo* info) {
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800213 if (info->is_linker() || info->is_main_executable()) {
214 // gdb already knows about the linker and the main executable.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700215 return;
216 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800217
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800218 link_map* map = &(info->link_map_head);
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000219
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800220 map->l_addr = info->load_bias;
221 // link_map l_name field is not const.
222 map->l_name = const_cast<char*>(info->get_realpath());
223 map->l_ld = info->dynamic;
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000224
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800225 CHECK(map->l_name != nullptr);
226 CHECK(map->l_name[0] != '\0');
227
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800228 notify_gdb_of_load(map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700229}
230
Elliott Hughesbedfe382012-08-14 14:07:59 -0700231static void notify_gdb_of_unload(soinfo* info) {
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800232 notify_gdb_of_unload(&(info->link_map_head));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233}
234
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700235bool android_namespace_t::is_accessible(const std::string& file) {
236 if (!is_isolated_) {
237 return true;
238 }
239
240 for (const auto& dir : ld_library_paths_) {
241 if (file_is_in_dir(file, dir)) {
242 return true;
243 }
244 }
245
246 for (const auto& dir : default_library_paths_) {
247 if (file_is_in_dir(file, dir)) {
248 return true;
249 }
250 }
251
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800252 for (const auto& dir : permitted_paths_) {
253 if (file_is_under_dir(file, dir)) {
254 return true;
255 }
256 }
257
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700258 return false;
259}
260
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700261LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
262 return g_soinfo_links_allocator.alloc();
263}
264
265void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
266 g_soinfo_links_allocator.free(entry);
267}
268
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700269static soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
270 struct stat* file_stat, off64_t file_offset,
271 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700272 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200273 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700274 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200275 }
276
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700277 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
278 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700279
Magnus Malmbornba98d922012-09-12 13:00:55 +0200280 sonext->next = si;
281 sonext = si;
282
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700283 ns->soinfo_list().push_back(si);
284
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700285 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200286 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800287}
288
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800289static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700290 if (si == nullptr) {
291 return;
292 }
293
294 if (si->base != 0 && si->size != 0) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800295 if (!si->is_mapped_by_caller()) {
296 munmap(reinterpret_cast<void*>(si->base), si->size);
297 } else {
298 // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE
299 mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE,
300 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
301 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700302 }
303
304 soinfo *prev = nullptr, *trav;
305
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700306 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700307
308 for (trav = solist; trav != nullptr; trav = trav->next) {
309 if (trav == si) {
310 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700311 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700312 prev = trav;
313 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800314
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700315 if (trav == nullptr) {
316 // si was not in solist
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700317 DL_ERR("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700318 return;
319 }
Elliott Hughes46882792012-08-03 16:49:39 -0700320
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700321 // clear links to/from si
322 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700323
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700324 // prev will never be null, because the first entry in solist is
325 // always the static libdl_info.
326 prev->next = si->next;
327 if (si == sonext) {
328 sonext = prev;
329 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800330
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700331 // remove from the namespace
332 si->get_namespace()->soinfo_list().remove_if([&](soinfo* candidate) {
333 return si == candidate;
334 });
335
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700336 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700337 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800338}
339
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700340// For every path element this function checks of it exists, and is a directory,
341// and normalizes it:
342// 1. For regular path it converts it to realpath()
343// 2. For path in a zip file it uses realpath on the zipfile
344// normalizes entry name by calling normalize_path function.
345static void resolve_paths(std::vector<std::string>& paths,
346 std::vector<std::string>* resolved_paths) {
347 resolved_paths->clear();
348 for (const auto& path : paths) {
349 char resolved_path[PATH_MAX];
350 const char* original_path = path.c_str();
351 if (realpath(original_path, resolved_path) != nullptr) {
352 struct stat s;
353 if (stat(resolved_path, &s) == 0) {
354 if (S_ISDIR(s.st_mode)) {
355 resolved_paths->push_back(resolved_path);
356 } else {
357 DL_WARN("Warning: \"%s\" is not a directory (excluding from path)", resolved_path);
358 continue;
359 }
360 } else {
361 DL_WARN("Warning: cannot stat file \"%s\": %s", resolved_path, strerror(errno));
362 continue;
363 }
364 } else {
365 std::string zip_path;
366 std::string entry_path;
367
368 std::string normalized_path;
369
370 if (!normalize_path(original_path, &normalized_path)) {
371 DL_WARN("Warning: unable to normalize \"%s\"", original_path);
372 continue;
373 }
374
375 if (parse_zip_path(normalized_path.c_str(), &zip_path, &entry_path)) {
376 if (realpath(zip_path.c_str(), resolved_path) == nullptr) {
377 DL_WARN("Warning: unable to resolve \"%s\": %s", zip_path.c_str(), strerror(errno));
378 continue;
379 }
380
381 ZipArchiveHandle handle = nullptr;
382 if (OpenArchive(resolved_path, &handle) != 0) {
383 DL_WARN("Warning: unable to open zip archive: %s", resolved_path);
384 continue;
385 }
386
387 // Check if zip-file has a dir with entry_path name
388 void* cookie = nullptr;
389 std::string prefix_str = entry_path + "/";
390 ZipString prefix(prefix_str.c_str());
391
392 ZipEntry out_data;
393 ZipString out_name;
394
395 int32_t error_code;
396
397 if ((error_code = StartIteration(handle, &cookie, &prefix, nullptr)) != 0) {
398 DL_WARN("Unable to iterate over zip-archive entries \"%s\";"
399 " error code: %d", zip_path.c_str(), error_code);
400 continue;
401 }
402
403 if (Next(cookie, &out_data, &out_name) != 0) {
404 DL_WARN("Unable to find entries starting with \"%s\" in \"%s\"",
405 prefix_str.c_str(), zip_path.c_str());
406 continue;
407 }
408
409 auto zip_guard = make_scope_guard([&]() {
410 if (cookie != nullptr) {
411 EndIteration(cookie);
412 }
413 CloseArchive(handle);
414 });
415
416 resolved_paths->push_back(std::string(resolved_path) + kZipFileSeparator + entry_path);
417 }
418 }
419 }
420}
421
422static void split_path(const char* path, const char* delimiters,
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700423 std::vector<std::string>* paths) {
Dmitriy Ivanovfbfba642015-11-16 14:23:37 -0800424 if (path != nullptr && path[0] != 0) {
tony.ys_liub4474402015-07-29 18:00:22 +0800425 *paths = android::base::Split(path, delimiters);
Elliott Hughescade4c32012-12-20 14:42:14 -0800426 }
427}
428
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700429static void parse_path(const char* path, const char* delimiters,
430 std::vector<std::string>* resolved_paths) {
431 std::vector<std::string> paths;
432 split_path(path, delimiters, &paths);
433 resolve_paths(paths, resolved_paths);
434}
435
Elliott Hughescade4c32012-12-20 14:42:14 -0800436static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700437 std::vector<std::string> ld_libary_paths;
438 parse_path(path, ":", &ld_libary_paths);
439 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800440}
441
Evgenii Stepanov68650822015-06-10 13:38:39 -0700442void soinfo::set_dt_runpath(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700443 if (!has_min_version(3)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700444 return;
445 }
446
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700447 std::vector<std::string> runpaths;
448
449 split_path(path, ":", &runpaths);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700450
451 std::string origin = dirname(get_realpath());
452 // FIXME: add $LIB and $PLATFORM.
453 std::pair<std::string, std::string> substs[] = {{"ORIGIN", origin}};
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700454 for (auto&& s : runpaths) {
Evgenii Stepanov68650822015-06-10 13:38:39 -0700455 size_t pos = 0;
456 while (pos < s.size()) {
457 pos = s.find("$", pos);
458 if (pos == std::string::npos) break;
459 for (const auto& subst : substs) {
460 const std::string& token = subst.first;
461 const std::string& replacement = subst.second;
462 if (s.substr(pos + 1, token.size()) == token) {
463 s.replace(pos, token.size() + 1, replacement);
464 // -1 to compensate for the ++pos below.
465 pos += replacement.size() - 1;
466 break;
467 } else if (s.substr(pos + 1, token.size() + 2) == "{" + token + "}") {
468 s.replace(pos, token.size() + 3, replacement);
469 pos += replacement.size() - 1;
470 break;
471 }
472 }
473 // Skip $ in case it did not match any of the known substitutions.
474 ++pos;
475 }
476 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700477
478 resolve_paths(runpaths, &dt_runpath_);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700479}
480
Elliott Hughescade4c32012-12-20 14:42:14 -0800481static void parse_LD_PRELOAD(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700482 g_ld_preload_names.clear();
483 if (path != nullptr) {
484 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
485 g_ld_preload_names = android::base::Split(path, " :");
486 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800487}
488
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700489static bool realpath_fd(int fd, std::string* realpath) {
490 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700491 __libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700492 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -0700493 PRINT("readlink('%s') failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700494 return false;
495 }
496
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700497 *realpath = &buf[0];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700498 return true;
499}
500
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700501#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700502
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700503// For a given PC, find the .so that it belongs to.
504// Returns the base address of the .ARM.exidx section
505// for that .so, and the number of 8-byte entries
506// in that section (via *pcount).
507//
508// Intended to be called by libc's __gnu_Unwind_Find_exidx().
509//
510// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800511_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800512 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800513
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700514 for (soinfo* si = solist; si != 0; si = si->next) {
515 if ((addr >= si->base) && (addr < (si->base + si->size))) {
516 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800517 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800518 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700519 }
520 *pcount = 0;
521 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800522}
Elliott Hughes46882792012-08-03 16:49:39 -0700523
Christopher Ferris24053a42013-08-19 17:45:09 -0700524#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700525
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700526// Here, we only have to provide a callback to iterate across all the
527// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700528int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700529 int rv = 0;
530 for (soinfo* si = solist; si != nullptr; si = si->next) {
531 dl_phdr_info dl_info;
532 dl_info.dlpi_addr = si->link_map_head.l_addr;
533 dl_info.dlpi_name = si->link_map_head.l_name;
534 dl_info.dlpi_phdr = si->phdr;
535 dl_info.dlpi_phnum = si->phnum;
536 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
537 if (rv != 0) {
538 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800539 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700540 }
541 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800542}
Elliott Hughes46882792012-08-03 16:49:39 -0700543
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700544const ElfW(Versym)* soinfo::get_versym(size_t n) const {
545 if (has_min_version(2) && versym_ != nullptr) {
546 return versym_ + n;
547 }
548
549 return nullptr;
550}
551
552ElfW(Addr) soinfo::get_verneed_ptr() const {
553 if (has_min_version(2)) {
554 return verneed_ptr_;
555 }
556
557 return 0;
558}
559
560size_t soinfo::get_verneed_cnt() const {
561 if (has_min_version(2)) {
562 return verneed_cnt_;
563 }
564
565 return 0;
566}
567
568ElfW(Addr) soinfo::get_verdef_ptr() const {
569 if (has_min_version(2)) {
570 return verdef_ptr_;
571 }
572
573 return 0;
574}
575
576size_t soinfo::get_verdef_cnt() const {
577 if (has_min_version(2)) {
578 return verdef_cnt_;
579 }
580
581 return 0;
582}
583
584template<typename F>
585static bool for_each_verdef(const soinfo* si, F functor) {
586 if (!si->has_min_version(2)) {
587 return true;
588 }
589
590 uintptr_t verdef_ptr = si->get_verdef_ptr();
591 if (verdef_ptr == 0) {
592 return true;
593 }
594
595 size_t offset = 0;
596
597 size_t verdef_cnt = si->get_verdef_cnt();
598 for (size_t i = 0; i<verdef_cnt; ++i) {
599 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
600 size_t verdaux_offset = offset + verdef->vd_aux;
601 offset += verdef->vd_next;
602
603 if (verdef->vd_version != 1) {
Dmitriy Ivanov3d7bea12015-04-20 17:40:39 -0700604 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700605 i, verdef->vd_version, si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700606 return false;
607 }
608
609 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
610 // "this is the version of the file itself. It must not be used for
611 // matching a symbol. It can be used to match references."
612 //
613 // http://www.akkadia.org/drepper/symbol-versioning
614 continue;
615 }
616
617 if (verdef->vd_cnt == 0) {
618 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
619 return false;
620 }
621
622 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
623
624 if (functor(i, verdef, verdaux) == true) {
625 break;
626 }
627 }
628
629 return true;
630}
631
632bool soinfo::find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const {
633 if (vi == nullptr) {
634 *versym = kVersymNotNeeded;
635 return true;
636 }
637
638 *versym = kVersymGlobal;
639
640 return for_each_verdef(this,
641 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
642 if (verdef->vd_hash == vi->elf_hash &&
643 strcmp(vi->name, get_string(verdaux->vda_name)) == 0) {
644 *versym = verdef->vd_ndx;
645 return true;
646 }
647
648 return false;
649 }
650 );
651}
652
653bool soinfo::find_symbol_by_name(SymbolName& symbol_name,
654 const version_info* vi,
655 const ElfW(Sym)** symbol) const {
656 uint32_t symbol_index;
657 bool success =
658 is_gnu_hash() ?
659 gnu_lookup(symbol_name, vi, &symbol_index) :
660 elf_lookup(symbol_name, vi, &symbol_index);
661
662 if (success) {
663 *symbol = symbol_index == 0 ? nullptr : symtab_ + symbol_index;
664 }
665
666 return success;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800667}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800668
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800669static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
670 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
671 ELF_ST_BIND(s->st_info) == STB_WEAK) {
672 return s->st_shndx != SHN_UNDEF;
673 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
674 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700675 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800676 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800677
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800678 return false;
679}
680
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700681static const ElfW(Versym) kVersymHiddenBit = 0x8000;
682
683static inline bool is_versym_hidden(const ElfW(Versym)* versym) {
684 // the symbol is hidden if bit 15 of versym is set.
685 return versym != nullptr && (*versym & kVersymHiddenBit) != 0;
686}
687
688static inline bool check_symbol_version(const ElfW(Versym) verneed,
689 const ElfW(Versym)* verdef) {
690 return verneed == kVersymNotNeeded ||
691 verdef == nullptr ||
692 verneed == (*verdef & ~kVersymHiddenBit);
693}
694
695bool soinfo::gnu_lookup(SymbolName& symbol_name,
696 const version_info* vi,
697 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800698 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800699 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800700
701 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800702 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
703 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800704
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700705 *symbol_index = 0;
706
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700707 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700708 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700709
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800710 // test against bloom filter
711 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700712 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700713 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700714
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700715 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800716 }
717
718 // bloom test says "probably yes"...
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700719 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800720
721 if (n == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700722 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700723 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700724
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700725 return true;
726 }
727
728 // lookup versym for the version definition in this library
729 // note the difference between "version is not requested" (vi == nullptr)
730 // and "version not found". In the first case verneed is kVersymNotNeeded
731 // which implies that the default version can be accepted; the second case results in
732 // verneed = 1 (kVersymGlobal) and implies that we should ignore versioned symbols
733 // for this library and consider only *global* ones.
734 ElfW(Versym) verneed = 0;
735 if (!find_verdef_version_index(vi, &verneed)) {
736 return false;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800737 }
738
739 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800740 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700741 const ElfW(Versym)* verdef = get_versym(n);
742 // skip hidden versions when verneed == kVersymNotNeeded (0)
743 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
744 continue;
745 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700746 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700747 check_symbol_version(verneed, verdef) &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800748 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
749 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700750 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700751 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(s->st_value),
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700752 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700753 *symbol_index = n;
754 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800755 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700756 } while ((gnu_chain_[n++] & 1) == 0);
757
758 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700759 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800760
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700761 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800762}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800763
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700764bool soinfo::elf_lookup(SymbolName& symbol_name,
765 const version_info* vi,
766 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800767 uint32_t hash = symbol_name.elf_hash();
768
769 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700770 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700771 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800772
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700773 ElfW(Versym) verneed = 0;
774 if (!find_verdef_version_index(vi, &verneed)) {
775 return false;
776 }
777
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800778 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
779 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700780 const ElfW(Versym)* verdef = get_versym(n);
781
782 // skip hidden versions when verneed == 0
783 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
784 continue;
785 }
786
787 if (check_symbol_version(verneed, verdef) &&
788 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700789 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800790 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700791 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700792 reinterpret_cast<void*>(s->st_value),
793 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700794 *symbol_index = n;
795 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800796 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800797 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800798
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700799 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700800 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700801 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700802
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700803 *symbol_index = 0;
804 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800805}
806
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700807soinfo::soinfo(android_namespace_t* ns, const char* realpath,
808 const struct stat* file_stat, off64_t file_offset,
809 int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700810 memset(this, 0, sizeof(*this));
811
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700812 if (realpath != nullptr) {
813 realpath_ = realpath;
814 }
815
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800816 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800817 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700818
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700819 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800820 this->st_dev_ = file_stat->st_dev;
821 this->st_ino_ = file_stat->st_ino;
822 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700823 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700824
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800825 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700826 this->namespace_ = ns;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700827}
828
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800829static uint32_t calculate_elf_hash(const char* name) {
830 const uint8_t* name_bytes = reinterpret_cast<const uint8_t*>(name);
831 uint32_t h = 0, g;
832
833 while (*name_bytes) {
834 h = (h << 4) + *name_bytes++;
835 g = h & 0xf0000000;
836 h ^= g;
837 h ^= g >> 24;
838 }
839
840 return h;
841}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800842
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800843uint32_t SymbolName::elf_hash() {
844 if (!has_elf_hash_) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800845 elf_hash_ = calculate_elf_hash(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800846 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700847 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800848
849 return elf_hash_;
850}
851
852uint32_t SymbolName::gnu_hash() {
853 if (!has_gnu_hash_) {
854 uint32_t h = 5381;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700855 const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800856 while (*name != 0) {
857 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
858 }
859
860 gnu_hash_ = h;
861 has_gnu_hash_ = true;
862 }
863
864 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800865}
866
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700867bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
868 soinfo** si_found_in, const soinfo::soinfo_list_t& global_group,
869 const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800870 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700871 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700872
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700873 /* "This element's presence in a shared object library alters the dynamic linker's
874 * symbol resolution algorithm for references within the library. Instead of starting
875 * a symbol search with the executable file, the dynamic linker starts from the shared
876 * object itself. If the shared object fails to supply the referenced symbol, the
877 * dynamic linker then searches the executable file and other shared objects as usual."
878 *
879 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
880 *
881 * Note that this is unlikely since static linker avoids generating
882 * relocations for -Bsymbolic linked dynamic executables.
883 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700884 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700885 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700886 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
887 return false;
888 }
889
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700890 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700891 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700892 }
893 }
894
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700895 // 1. Look for it in global_group
896 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700897 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700898 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700899 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700900 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700901 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
902 error = true;
903 return false;
904 }
905
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700906 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700907 *si_found_in = global_si;
908 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700909 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700910
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700911 return true;
912 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700913
914 if (error) {
915 return false;
916 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700917 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700918
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700919 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700920 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700921 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700922 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700923 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700924 // we already did this - skip
925 return true;
926 }
927
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700928 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700929 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700930 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
931 error = true;
932 return false;
933 }
934
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700935 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700936 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700937 return false;
938 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700939
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700940 return true;
941 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700942
943 if (error) {
944 return false;
945 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700946 }
947
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700948 if (s != nullptr) {
949 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
950 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700951 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
952 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700953 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700954 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700955
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700956 *symbol = s;
957 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700958}
959
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800960class ProtectedDataGuard {
961 public:
962 ProtectedDataGuard() {
963 if (ref_count_++ == 0) {
964 protect_data(PROT_READ | PROT_WRITE);
965 }
966 }
967
968 ~ProtectedDataGuard() {
969 if (ref_count_ == 0) { // overflow
970 __libc_fatal("Too many nested calls to dlopen()");
971 }
972
973 if (--ref_count_ == 0) {
974 protect_data(PROT_READ);
975 }
976 }
977 private:
978 void protect_data(int protection) {
979 g_soinfo_allocator.protect_all(protection);
980 g_soinfo_links_allocator.protect_all(protection);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700981 g_namespace_allocator.protect_all(protection);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800982 }
983
984 static size_t ref_count_;
985};
986
987size_t ProtectedDataGuard::ref_count_ = 0;
988
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700989// Each size has it's own allocator.
990template<size_t size>
991class SizeBasedAllocator {
992 public:
993 static void* alloc() {
994 return allocator_.alloc();
995 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700996
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700997 static void free(void* ptr) {
998 allocator_.free(ptr);
999 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001000
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001001 private:
1002 static LinkerBlockAllocator allocator_;
1003};
1004
1005template<size_t size>
1006LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
1007
1008template<typename T>
1009class TypeBasedAllocator {
1010 public:
1011 static T* alloc() {
1012 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
1013 }
1014
1015 static void free(T* ptr) {
1016 SizeBasedAllocator<sizeof(T)>::free(ptr);
1017 }
1018};
1019
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001020class LoadTask {
1021 public:
1022 struct deleter_t {
1023 void operator()(LoadTask* t) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001024 t->~LoadTask();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001025 TypeBasedAllocator<LoadTask>::free(t);
1026 }
1027 };
1028
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001029 static deleter_t deleter;
1030
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001031 static LoadTask* create(const char* name, soinfo* needed_by,
1032 std::unordered_map<const soinfo*, ElfReader>* readers_map) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001033 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001034 return new (ptr) LoadTask(name, needed_by, readers_map);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001035 }
1036
1037 const char* get_name() const {
1038 return name_;
1039 }
1040
1041 soinfo* get_needed_by() const {
1042 return needed_by_;
1043 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001044
1045 soinfo* get_soinfo() const {
1046 return si_;
1047 }
1048
1049 void set_soinfo(soinfo* si) {
1050 si_ = si;
1051 }
1052
1053 off64_t get_file_offset() const {
1054 return file_offset_;
1055 }
1056
1057 void set_file_offset(off64_t offset) {
1058 file_offset_ = offset;
1059 }
1060
1061 int get_fd() const {
1062 return fd_;
1063 }
1064
1065 void set_fd(int fd, bool assume_ownership) {
1066 fd_ = fd;
1067 close_fd_ = assume_ownership;
1068 }
1069
1070 const android_dlextinfo* get_extinfo() const {
1071 return extinfo_;
1072 }
1073
1074 void set_extinfo(const android_dlextinfo* extinfo) {
1075 extinfo_ = extinfo;
1076 }
1077
1078 const ElfReader& get_elf_reader() const {
1079 CHECK(si_ != nullptr);
1080 return (*elf_readers_map_)[si_];
1081 }
1082
1083 ElfReader& get_elf_reader() {
1084 CHECK(si_ != nullptr);
1085 return (*elf_readers_map_)[si_];
1086 }
1087
1088 std::unordered_map<const soinfo*, ElfReader>* get_readers_map() {
1089 return elf_readers_map_;
1090 }
1091
1092 bool read(const char* realpath, off64_t file_size) {
1093 ElfReader& elf_reader = get_elf_reader();
1094 return elf_reader.Read(realpath, fd_, file_offset_, file_size);
1095 }
1096
1097 bool load() {
1098 ElfReader& elf_reader = get_elf_reader();
1099 if (!elf_reader.Load(extinfo_)) {
1100 return false;
1101 }
1102
1103 si_->base = elf_reader.load_start();
1104 si_->size = elf_reader.load_size();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -08001105 si_->set_mapped_by_caller(elf_reader.is_mapped_by_caller());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001106 si_->load_bias = elf_reader.load_bias();
1107 si_->phnum = elf_reader.phdr_count();
1108 si_->phdr = elf_reader.loaded_phdr();
1109
1110 return true;
1111 }
1112
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001113 private:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001114 LoadTask(const char* name, soinfo* needed_by,
1115 std::unordered_map<const soinfo*, ElfReader>* readers_map)
1116 : name_(name), needed_by_(needed_by), si_(nullptr),
1117 fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map) {}
1118
1119 ~LoadTask() {
1120 if (fd_ != -1 && close_fd_) {
1121 close(fd_);
1122 }
1123 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001124
1125 const char* name_;
1126 soinfo* needed_by_;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001127 soinfo* si_;
1128 const android_dlextinfo* extinfo_;
1129 int fd_;
1130 bool close_fd_;
1131 off64_t file_offset_;
1132 std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001133
1134 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
1135};
1136
Ningsheng Jiane93be992014-09-16 15:22:10 +08001137LoadTask::deleter_t LoadTask::deleter;
1138
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001139template <typename T>
1140using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
1141
1142typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001143typedef linked_list_t<const char> StringLinkedList;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001144typedef std::vector<LoadTask*> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001145
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001146
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001147// This function walks down the tree of soinfo dependencies
1148// in breadth-first order and
1149// * calls action(soinfo* si) for each node, and
1150// * terminates walk if action returns false.
1151//
1152// walk_dependencies_tree returns false if walk was terminated
1153// by the action and true otherwise.
1154template<typename F>
1155static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001156 SoinfoLinkedList visit_list;
1157 SoinfoLinkedList visited;
1158
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001159 for (size_t i = 0; i < root_soinfos_size; ++i) {
1160 visit_list.push_back(root_soinfos[i]);
1161 }
1162
1163 soinfo* si;
1164 while ((si = visit_list.pop_front()) != nullptr) {
1165 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -07001166 continue;
1167 }
1168
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001169 if (!action(si)) {
1170 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001171 }
1172
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001173 visited.push_back(si);
1174
1175 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001176 visit_list.push_back(child);
1177 });
1178 }
1179
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001180 return true;
1181}
1182
1183
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001184static const ElfW(Sym)* dlsym_handle_lookup(soinfo* root, soinfo* skip_until,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001185 soinfo** found, SymbolName& symbol_name,
1186 const version_info* vi) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001187 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001188 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001189
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001190 walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) {
1191 if (skip_lookup) {
1192 skip_lookup = current_soinfo != skip_until;
1193 return true;
1194 }
1195
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001196 if (!current_soinfo->find_symbol_by_name(symbol_name, vi, &result)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001197 result = nullptr;
1198 return false;
1199 }
1200
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001201 if (result != nullptr) {
1202 *found = current_soinfo;
1203 return false;
1204 }
1205
1206 return true;
1207 });
1208
1209 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001210}
1211
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001212static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
1213 const char* name,
1214 const version_info* vi,
1215 soinfo** found,
1216 soinfo* caller,
1217 void* handle);
1218
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001219// This is used by dlsym(3). It performs symbol lookup only within the
1220// specified soinfo object and its dependencies in breadth first order.
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001221static const ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found,
1222 const char* name, const version_info* vi) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -07001223 // According to man dlopen(3) and posix docs in the case when si is handle
1224 // of the main executable we need to search not only in the executable and its
1225 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
1226 //
1227 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
1228 // libraries and they are loaded in breath-first (correct) order we can just execute
1229 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
1230 if (si == somain) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001231 return dlsym_linear_lookup(&g_default_namespace, name, vi, found, nullptr, RTLD_DEFAULT);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -07001232 }
1233
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001234 SymbolName symbol_name(name);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001235 return dlsym_handle_lookup(si, nullptr, found, symbol_name, vi);
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001236}
1237
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001238/* This is used by dlsym(3) to performs a global symbol lookup. If the
1239 start value is null (for RTLD_DEFAULT), the search starts at the
1240 beginning of the global solist. Otherwise the search starts at the
1241 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001242 */
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001243static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
1244 const char* name,
1245 const version_info* vi,
1246 soinfo** found,
1247 soinfo* caller,
1248 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001249 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001250
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001251 soinfo::soinfo_list_t& soinfo_list = ns->soinfo_list();
1252 soinfo::soinfo_list_t::iterator start = soinfo_list.begin();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001253
1254 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -07001255 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001256 return nullptr;
1257 } else {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001258 soinfo::soinfo_list_t::iterator it = soinfo_list.find(caller);
1259 CHECK (it != soinfo_list.end());
1260 start = ++it;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001261 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001262 }
1263
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001264 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001265 for (soinfo::soinfo_list_t::iterator it = start, end = soinfo_list.end(); it != end; ++it) {
1266 soinfo* si = *it;
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001267 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
1268 // if the library is opened by application with target api level <= 22
1269 // See http://b/21565766
1270 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 && si->get_target_sdk_version() > 22) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001271 continue;
1272 }
1273
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001274 if (!si->find_symbol_by_name(symbol_name, vi, &s)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001275 return nullptr;
1276 }
1277
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001278 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -08001279 *found = si;
1280 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -06001281 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001282 }
Matt Fischer1698d9e2009-12-31 12:17:56 -06001283
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001284 // If not found - use dlsym_handle_lookup for caller's
1285 // local_group unless it is part of the global group in which
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001286 // case we already did it.
1287 if (s == nullptr && caller != nullptr &&
1288 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001289 return dlsym_handle_lookup(caller->get_local_group_root(),
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001290 (handle == RTLD_NEXT) ? caller : nullptr, found, symbol_name, vi);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001291 }
1292
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001293 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001294 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
1295 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -08001296 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001297
Elliott Hughescade4c32012-12-20 14:42:14 -08001298 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001299}
1300
Kito Chengfa8c05d2013-03-12 14:58:06 +08001301soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001302 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001303 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001304 if (address >= si->base && address - si->base < si->size) {
1305 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001306 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001307 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001308 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001309}
1310
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001311ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
1312 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
1313}
1314
1315static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
1316 return sym->st_shndx != SHN_UNDEF &&
1317 soaddr >= sym->st_value &&
1318 soaddr < sym->st_value + sym->st_size;
1319}
1320
1321ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001322 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001323
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001324 for (size_t i = 0; i < gnu_nbucket_; ++i) {
1325 uint32_t n = gnu_bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001326
1327 if (n == 0) {
1328 continue;
1329 }
1330
1331 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001332 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001333 if (symbol_matches_soaddr(sym, soaddr)) {
1334 return sym;
1335 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001336 } while ((gnu_chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001337 }
1338
1339 return nullptr;
1340}
1341
1342ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001343 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001344
Kito Chengfa8c05d2013-03-12 14:58:06 +08001345 // Search the library's symbol table for any defined symbol which
1346 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001347 for (size_t i = 0; i < nchain_; ++i) {
1348 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001349 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001350 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001351 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001352 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001353
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001354 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001355}
1356
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001357class ZipArchiveCache {
1358 public:
1359 ZipArchiveCache() {}
1360 ~ZipArchiveCache();
1361
1362 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
1363 private:
1364 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
1365
1366 std::unordered_map<std::string, ZipArchiveHandle> cache_;
1367};
1368
1369bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
1370 std::string key(zip_path);
1371
1372 auto it = cache_.find(key);
1373 if (it != cache_.end()) {
1374 *handle = it->second;
1375 return true;
1376 }
1377
1378 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1379 if (fd == -1) {
1380 return false;
1381 }
1382
1383 if (OpenArchiveFd(fd, "", handle) != 0) {
1384 // invalid zip-file (?)
1385 close(fd);
1386 return false;
1387 }
1388
1389 cache_[key] = *handle;
1390 return true;
1391}
1392
1393ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -07001394 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001395 CloseArchive(it.second);
1396 }
1397}
1398
1399static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001400 const char* const input_path,
1401 off64_t* file_offset, std::string* realpath) {
1402 std::string normalized_path;
1403 if (!normalize_path(input_path, &normalized_path)) {
1404 return -1;
1405 }
1406
1407 const char* const path = normalized_path.c_str();
1408 TRACE("Trying zip file open from path '%s' -> normalized '%s'", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +00001409
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001410 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +00001411 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001412 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +00001413 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001414 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +00001415 if (separator == nullptr) {
1416 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -07001417 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001418
1419 char buf[512];
1420 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
1421 PRINT("Warning: ignoring very long library path: %s", path);
1422 return -1;
1423 }
1424
1425 buf[separator - path] = '\0';
1426
1427 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001428 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +00001429 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1430 if (fd == -1) {
1431 return -1;
1432 }
1433
1434 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001435 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001436 // invalid zip-file (?)
1437 close(fd);
1438 return -1;
1439 }
1440
Simon Baldwinaef71952015-01-16 13:22:54 +00001441 ZipEntry entry;
1442
Yusuke Sato56f40fb2015-06-25 14:56:07 -07001443 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001444 // Entry was not found.
1445 close(fd);
1446 return -1;
1447 }
1448
1449 // Check if it is properly stored
1450 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
1451 close(fd);
1452 return -1;
1453 }
1454
1455 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001456
1457 if (realpath_fd(fd, realpath)) {
1458 *realpath += separator;
1459 } else {
1460 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1461 normalized_path.c_str());
1462 *realpath = normalized_path;
1463 }
1464
Simon Baldwinaef71952015-01-16 13:22:54 +00001465 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001466}
1467
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001468static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
1469 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
1470 if (n < 0 || n >= static_cast<int>(buf_size)) {
1471 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1472 return false;
1473 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001474
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001475 return true;
1476}
1477
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001478static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1479 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001480 const std::vector<std::string>& paths,
1481 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001482 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001483 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001484 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001485 continue;
1486 }
1487
1488 int fd = -1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001489 if (strstr(buf, kZipFileSeparator) != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001490 fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath);
Simon Baldwinaef71952015-01-16 13:22:54 +00001491 }
1492
1493 if (fd == -1) {
1494 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1495 if (fd != -1) {
1496 *file_offset = 0;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001497 if (!realpath_fd(fd, realpath)) {
1498 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
1499 *realpath = buf;
1500 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001501 }
1502 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001503
1504 if (fd != -1) {
1505 return fd;
1506 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001507 }
1508
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001509 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001510}
1511
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001512static int open_library(android_namespace_t* ns,
1513 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001514 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001515 off64_t* file_offset, std::string* realpath) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001516 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001517
Elliott Hughes124fae92012-10-31 14:20:03 -07001518 // 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 -07001519 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001520 int fd = -1;
1521
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001522 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001523 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1524 }
1525
1526 if (fd == -1) {
1527 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001528 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001529 *file_offset = 0;
1530 if (!realpath_fd(fd, realpath)) {
1531 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name);
1532 *realpath = name;
1533 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001534 }
1535 }
1536
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001537 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001538 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001539
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001540 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1541 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 -07001542 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001543 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001544 // Check if the library is accessible
1545 if (fd != -1 && !ns->is_accessible(*realpath)) {
1546 fd = -1;
1547 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001548 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001549
Elliott Hughes124fae92012-10-31 14:20:03 -07001550 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001551 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001552 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001553
Elliott Hughes124fae92012-10-31 14:20:03 -07001554 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001555}
1556
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001557static const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
1558#if !defined(__LP64__)
1559 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001560 if (get_application_target_sdk_version() <= 22) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001561 const char* bname = basename(dt_needed);
1562 if (bname != dt_needed) {
1563 DL_WARN("'%s' library has invalid DT_NEEDED entry '%s'", sopath, dt_needed);
1564 }
1565
1566 return bname;
1567 }
1568#endif
1569 return dt_needed;
1570}
1571
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001572template<typename F>
1573static void for_each_dt_needed(const soinfo* si, F action) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001574 for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001575 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001576 action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
Dima Zavin2e855792009-05-20 18:28:09 -07001577 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001578 }
1579}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001580
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001581template<typename F>
1582static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
1583 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1584 if (d->d_tag == DT_NEEDED) {
1585 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1586 }
1587 }
1588}
1589
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001590static bool load_library(android_namespace_t* ns,
1591 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001592 LoadTaskList* load_tasks,
1593 int rtld_flags,
1594 const std::string& realpath) {
1595 off64_t file_offset = task->get_file_offset();
1596 const char* name = task->get_name();
1597 const android_dlextinfo* extinfo = task->get_extinfo();
1598
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001599 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001600 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001601 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001602 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001603 if (file_offset < 0) {
1604 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001605 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001606 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001607
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001608 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001609 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001610 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001611 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001612 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001613 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001614 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1615 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001616 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001617 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001618
1619 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001620 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1621 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001622 auto predicate = [&](soinfo* si) {
1623 return si->get_st_dev() != 0 &&
1624 si->get_st_ino() != 0 &&
1625 si->get_st_dev() == file_stat.st_dev &&
1626 si->get_st_ino() == file_stat.st_ino &&
1627 si->get_file_offset() == file_offset;
1628 };
1629
1630 soinfo* si = ns->soinfo_list().find_if(predicate);
1631
1632 // check public namespace
1633 if (si == nullptr) {
1634 si = g_public_namespace.find_if(predicate);
1635 if (si != nullptr) {
1636 ns->soinfo_list().push_back(si);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001637 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001638 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001639
1640 if (si != nullptr) {
1641 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
1642 "will return existing soinfo", name, si->get_realpath());
1643 task->set_soinfo(si);
1644 return true;
1645 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001646 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001647
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001648 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001649 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001650 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001651 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001652
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001653 if (!ns->is_accessible(realpath)) {
1654 // do not load libraries if they are not accessible for the specified namespace.
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001655 const char* needed_or_dlopened_by = task->get_needed_by() == nullptr ?
1656 "(unknown)" :
1657 task->get_needed_by()->get_realpath();
1658
1659 DL_ERR("library \"%s\" needed or dlopened by \"%s\" is not accessible for the namespace \"%s\"",
1660 name, needed_or_dlopened_by, ns->get_name());
1661
1662 PRINT("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the"
1663 " namespace: [name=\"%s\", ld_library_paths=\"%s\", default_library_paths=\"%s\","
1664 " permitted_paths=\"%s\"]",
1665 name, realpath.c_str(),
1666 needed_or_dlopened_by,
1667 ns->get_name(),
1668 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
1669 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
1670 android::base::Join(ns->get_permitted_paths(), ':').c_str());
1671
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001672 return false;
1673 }
1674
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001675 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001676 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001677 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001678 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001679
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001680 task->set_soinfo(si);
1681
1682 // Read the ELF header and some of the segments.
1683 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001684 soinfo_free(si);
1685 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001686 return false;
1687 }
1688
1689 // find and set DT_RUNPATH and dt_soname
1690 // Note that these field values are temporary and are
1691 // going to be overwritten on soinfo::prelink_image
1692 // with values from PT_LOAD segments.
1693 const ElfReader& elf_reader = task->get_elf_reader();
1694 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1695 if (d->d_tag == DT_RUNPATH) {
1696 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1697 }
1698 if (d->d_tag == DT_SONAME) {
1699 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1700 }
1701 }
1702
1703 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
1704 load_tasks->push_back(LoadTask::create(name, si, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001705 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001706
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001707 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001708}
1709
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001710static bool load_library(android_namespace_t* ns,
1711 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001712 ZipArchiveCache* zip_archive_cache,
1713 LoadTaskList* load_tasks,
1714 int rtld_flags) {
1715 const char* name = task->get_name();
1716 soinfo* needed_by = task->get_needed_by();
1717 const android_dlextinfo* extinfo = task->get_extinfo();
1718
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001719 off64_t file_offset;
1720 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001721 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001722 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001723 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1724 file_offset = extinfo->library_fd_offset;
1725 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001726
1727 if (!realpath_fd(extinfo->library_fd, &realpath)) {
1728 PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1729 "Will use given name.", name);
1730 realpath = name;
1731 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001732
1733 task->set_fd(extinfo->library_fd, false);
1734 task->set_file_offset(file_offset);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001735 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001736 }
1737
1738 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001739 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001740 if (fd == -1) {
1741 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001742 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001743 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001744
1745 task->set_fd(fd, true);
1746 task->set_file_offset(file_offset);
1747
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001748 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001749}
1750
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001751// Returns true if library was found and false in 2 cases
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001752// 1. (for default namespace only) The library was found but loaded under different
1753// target_sdk_version (*candidate != nullptr)
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001754// 2. The library was not found by soname (*candidate is nullptr)
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001755static bool find_loaded_library_by_soname(android_namespace_t* ns,
1756 const char* name, soinfo** candidate) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001757 *candidate = nullptr;
1758
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001759 // Ignore filename with path.
1760 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001761 return false;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001762 }
1763
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001764 uint32_t target_sdk_version = get_application_target_sdk_version();
1765
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001766 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001767 const char* soname = si->get_soname();
1768 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001769 // If the library was opened under different target sdk version
1770 // skip this step and try to reopen it. The exceptions are
1771 // "libdl.so" and global group. There is no point in skipping
1772 // them because relocation process is going to use them
1773 // in any case.
1774 bool is_libdl = si == solist;
1775 if (is_libdl || (si->get_dt_flags_1() & DF_1_GLOBAL) != 0 ||
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001776 !si->is_linked() || si->get_target_sdk_version() == target_sdk_version ||
1777 ns != &g_default_namespace) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001778 *candidate = si;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001779 return false;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001780 } else if (*candidate == nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001781 // for the different sdk version in the default namespace
1782 // remember the first library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001783 *candidate = si;
1784 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001785 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001786
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001787 return true;
1788 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001789}
1790
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001791static bool find_library_internal(android_namespace_t* ns,
1792 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001793 ZipArchiveCache* zip_archive_cache,
1794 LoadTaskList* load_tasks,
1795 int rtld_flags) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001796 soinfo* candidate;
1797
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001798 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001799 task->set_soinfo(candidate);
1800 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001801 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001802
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001803 if (ns != &g_default_namespace) {
1804 // check public namespace
1805 candidate = g_public_namespace.find_if([&](soinfo* si) {
1806 return strcmp(task->get_name(), si->get_soname()) == 0;
1807 });
1808
1809 if (candidate != nullptr) {
1810 ns->soinfo_list().push_back(candidate);
1811 task->set_soinfo(candidate);
1812 return true;
1813 }
1814 }
1815
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001816 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001817 // of this fact is done by load_library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001818 TRACE("[ '%s' find_loaded_library_by_soname returned false (*candidate=%s@%p). Trying harder...]",
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001819 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001820
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001821 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001822 return true;
1823 } else {
1824 // In case we were unable to load the library but there
1825 // is a candidate loaded under the same soname but different
1826 // sdk level - return it anyways.
1827 if (candidate != nullptr) {
1828 task->set_soinfo(candidate);
1829 return true;
1830 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001831 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001832
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001833 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001834}
1835
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001836static void soinfo_unload(soinfo* si);
1837
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001838// TODO: this is slightly unusual way to construct
1839// the global group for relocation. Not every RTLD_GLOBAL
1840// library is included in this group for backwards-compatibility
1841// reasons.
1842//
1843// This group consists of the main executable, LD_PRELOADs
1844// and libraries with the DF_1_GLOBAL flag set.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001845static soinfo::soinfo_list_t make_global_group(android_namespace_t* ns) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001846 soinfo::soinfo_list_t global_group;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001847 ns->soinfo_list().for_each([&](soinfo* si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001848 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1849 global_group.push_back(si);
1850 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001851 });
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001852
1853 return global_group;
1854}
1855
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001856static void shuffle(std::vector<LoadTask*>* v) {
1857 for (size_t i = 0, size = v->size(); i < size; ++i) {
1858 size_t n = size - i;
1859 size_t r = arc4random_uniform(n);
1860 std::swap((*v)[n-1], (*v)[r]);
1861 }
1862}
1863
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001864// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1865// not their transitive dependencies) as children of the start_with library.
1866// This is false when find_libraries is called for dlopen(), when newly loaded
1867// libraries must form a disjoint tree.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001868static bool find_libraries(android_namespace_t* ns,
1869 soinfo* start_with,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001870 const char* const library_names[],
1871 size_t library_names_count, soinfo* soinfos[],
1872 std::vector<soinfo*>* ld_preloads,
1873 size_t ld_preloads_count, int rtld_flags,
1874 const android_dlextinfo* extinfo,
1875 bool add_as_children) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001876 // Step 0: prepare.
1877 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001878 std::unordered_map<const soinfo*, ElfReader> readers_map;
1879
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001880 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001881 const char* name = library_names[i];
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001882 load_tasks.push_back(LoadTask::create(name, start_with, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001883 }
1884
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001885 // Construct global_group.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001886 soinfo::soinfo_list_t global_group = make_global_group(ns);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001887
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001888 // If soinfos array is null allocate one on stack.
1889 // The array is needed in case of failure; for example
1890 // when library_names[] = {libone.so, libtwo.so} and libone.so
1891 // is loaded correctly but libtwo.so failed for some reason.
1892 // In this case libone.so should be unloaded on return.
1893 // See also implementation of failure_guard below.
1894
1895 if (soinfos == nullptr) {
1896 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1897 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1898 memset(soinfos, 0, soinfos_size);
1899 }
1900
1901 // list of libraries to link - see step 2.
1902 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001903
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001904 auto scope_guard = make_scope_guard([&]() {
1905 for (LoadTask* t : load_tasks) {
1906 LoadTask::deleter(t);
1907 }
1908 });
1909
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001910 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001911 // Housekeeping
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001912 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001913 soinfo_unload(soinfos[i]);
1914 }
1915 });
1916
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001917 ZipArchiveCache zip_archive_cache;
1918
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001919 // Step 1: expand the list of load_tasks to include
1920 // all DT_NEEDED libraries (do not load them just yet)
1921 for (size_t i = 0; i<load_tasks.size(); ++i) {
1922 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07001923 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001924
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001925 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001926 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001927
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001928 if(!find_library_internal(ns, task, &zip_archive_cache, &load_tasks, rtld_flags)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001929 return false;
1930 }
1931
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001932 soinfo* si = task->get_soinfo();
1933
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001934 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001935 needed_by->add_child(si);
1936 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001937
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001938 if (si->is_linked()) {
1939 si->increment_ref_count();
1940 }
1941
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001942 // When ld_preloads is not null, the first
1943 // ld_preloads_count libs are in fact ld_preloads.
1944 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001945 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001946 }
1947
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001948 if (soinfos_count < library_names_count) {
1949 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001950 }
1951 }
1952
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001953 // Step 2: Load libraries in random order (see b/24047022)
1954 LoadTaskList load_list;
1955 for (auto&& task : load_tasks) {
1956 soinfo* si = task->get_soinfo();
1957 auto pred = [&](const LoadTask* t) {
1958 return t->get_soinfo() == si;
1959 };
1960
1961 if (!si->is_linked() &&
1962 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
1963 load_list.push_back(task);
1964 }
1965 }
1966 shuffle(&load_list);
1967
1968 for (auto&& task : load_list) {
1969 if (!task->load()) {
1970 return false;
1971 }
1972 }
1973
1974 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
1975 for (auto&& task : load_tasks) {
1976 soinfo* si = task->get_soinfo();
1977 if (!si->is_linked() && !si->prelink_image()) {
1978 return false;
1979 }
1980 }
1981
1982 // Step 4: Add LD_PRELOADed libraries to the global group for
1983 // future runs. There is no need to explicitly add them to
1984 // the global group for this run because they are going to
1985 // appear in the local group in the correct order.
1986 if (ld_preloads != nullptr) {
1987 for (auto&& si : *ld_preloads) {
1988 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
1989 }
1990 }
1991
1992
1993 // Step 5: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001994 soinfo::soinfo_list_t local_group;
1995 walk_dependencies_tree(
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001996 (start_with != nullptr && add_as_children) ? &start_with : soinfos,
1997 (start_with != nullptr && add_as_children) ? 1 : soinfos_count,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001998 [&] (soinfo* si) {
1999 local_group.push_back(si);
2000 return true;
2001 });
2002
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002003 // We need to increment ref_count in case
2004 // the root of the local group was not linked.
2005 bool was_local_group_root_linked = local_group.front()->is_linked();
2006
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002007 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002008 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002009 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002010 return false;
2011 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002012 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002013 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002014
2015 return true;
2016 });
2017
2018 if (linked) {
2019 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002020 }
2021
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002022 if (!was_local_group_root_linked) {
2023 local_group.front()->increment_ref_count();
2024 }
2025
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002026 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002027}
2028
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002029static soinfo* find_library(android_namespace_t* ns,
2030 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002031 const android_dlextinfo* extinfo,
2032 soinfo* needed_by) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002033 soinfo* si;
2034
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002035 if (name == nullptr) {
2036 si = somain;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002037 } else if (!find_libraries(ns, needed_by, &name, 1, &si, nullptr, 0, rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002038 extinfo, /* add_as_children */ false)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002039 return nullptr;
2040 }
2041
Elliott Hughesd23736e2012-11-01 15:16:56 -07002042 return si;
2043}
Elliott Hughesbedfe382012-08-14 14:07:59 -07002044
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002045static void soinfo_unload(soinfo* root) {
2046 // Note that the library can be loaded but not linked;
2047 // in which case there is no root but we still need
2048 // to walk the tree and unload soinfos involved.
2049 //
2050 // This happens on unsuccessful dlopen, when one of
2051 // the DT_NEEDED libraries could not be linked/found.
2052 if (root->is_linked()) {
2053 root = root->get_local_group_root();
2054 }
2055
2056 if (!root->can_unload()) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002057 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002058 return;
2059 }
2060
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002061 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002062
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002063 if (ref_count == 0) {
2064 soinfo::soinfo_list_t local_unload_list;
2065 soinfo::soinfo_list_t external_unload_list;
2066 soinfo::soinfo_list_t depth_first_list;
2067 depth_first_list.push_back(root);
2068 soinfo* si = nullptr;
2069
2070 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002071 if (local_unload_list.contains(si)) {
2072 continue;
2073 }
2074
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002075 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002076
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002077 if (si->has_min_version(0)) {
2078 soinfo* child = nullptr;
2079 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002080 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
2081 child->get_realpath(), child);
2082
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002083 if (local_unload_list.contains(child)) {
2084 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002085 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002086 external_unload_list.push_back(child);
2087 } else {
2088 depth_first_list.push_front(child);
2089 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002090 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002091 } else {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07002092#if !defined(__work_around_b_24465209__)
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002093 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002094#else
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002095 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002096 for_each_dt_needed(si, [&] (const char* library_name) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002097 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002098 si->get_realpath(), library_name);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002099
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002100 soinfo* needed = find_library(si->get_namespace(),
2101 library_name, RTLD_NOLOAD, nullptr, nullptr);
2102
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002103 if (needed != nullptr) {
2104 // Not found: for example if symlink was deleted between dlopen and dlclose
2105 // Since we cannot really handle errors at this point - print and continue.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002106 PRINT("warning: couldn't find %s needed by %s on unload.",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002107 library_name, si->get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002108 return;
2109 } else if (local_unload_list.contains(needed)) {
2110 // already visited
2111 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002112 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002113 // external group
2114 external_unload_list.push_back(needed);
2115 } else {
2116 // local group
2117 depth_first_list.push_front(needed);
2118 }
2119 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002120#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002121 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002122 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002123
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002124 local_unload_list.for_each([](soinfo* si) {
2125 si->call_destructors();
2126 });
2127
2128 while ((si = local_unload_list.pop_front()) != nullptr) {
2129 notify_gdb_of_unload(si);
2130 soinfo_free(si);
2131 }
2132
2133 while ((si = external_unload_list.pop_front()) != nullptr) {
2134 soinfo_unload(si);
2135 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002136 } else {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002137 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
2138 root->get_realpath(), ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08002139 }
2140}
2141
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002142static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
2143 if (sym_ver == nullptr) {
2144 return sym_name;
2145 }
2146
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002147 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002148}
2149
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002150void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002151 // Use basic string manipulation calls to avoid snprintf.
2152 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
2153 // When debug malloc is enabled, this call returns 0. This in turn causes
2154 // snprintf to do nothing, which causes libraries to fail to load.
2155 // See b/17302493 for further details.
2156 // Once the above bug is fixed, this code can be modified to use
2157 // snprintf again.
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002158 size_t required_len = 0;
2159 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2160 required_len += strlen(g_default_ld_paths[i]) + 1;
2161 }
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002162 if (buffer_size < required_len) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002163 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
2164 "buffer len %zu, required len %zu", buffer_size, required_len);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002165 }
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002166 char* end = buffer;
2167 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2168 if (i > 0) *end++ = ':';
2169 end = stpcpy(end, g_default_ld_paths[i]);
2170 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002171}
2172
Elliott Hughescade4c32012-12-20 14:42:14 -08002173void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08002174 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08002175}
2176
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002177soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo,
2178 void* caller_addr) {
2179 soinfo* const caller = find_containing_library(caller_addr);
2180
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002181 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08002182 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002183 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08002184 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002185
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002186 android_namespace_t* ns = caller != nullptr ? caller->get_namespace() : g_anonymous_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002187
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002188 if (extinfo != nullptr) {
2189 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
2190 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
2191 return nullptr;
2192 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002193
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002194 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07002195 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002196 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
2197 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002198 return nullptr;
2199 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002200
2201 if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 &&
2202 (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) {
2203 DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not "
2204 "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT");
2205 return nullptr;
2206 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002207
2208 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
2209 if (extinfo->library_namespace == nullptr) {
2210 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
2211 return nullptr;
2212 }
2213 ns = extinfo->library_namespace;
2214 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00002215 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002216
2217 ProtectedDataGuard guard;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002218 soinfo* si = find_library(ns, name, flags, extinfo, caller);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002219 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002220 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07002221 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002222
Elliott Hughesd23736e2012-11-01 15:16:56 -07002223 return si;
2224}
2225
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002226int do_dladdr(const void* addr, Dl_info* info) {
2227 // Determine if this address can be found in any library currently mapped.
2228 soinfo* si = find_containing_library(addr);
2229 if (si == nullptr) {
2230 return 0;
2231 }
2232
2233 memset(info, 0, sizeof(Dl_info));
2234
2235 info->dli_fname = si->get_realpath();
2236 // Address at which the shared object is loaded.
2237 info->dli_fbase = reinterpret_cast<void*>(si->base);
2238
2239 // Determine if any symbol in the library contains the specified address.
2240 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
2241 if (sym != nullptr) {
2242 info->dli_sname = si->get_string(sym->st_name);
2243 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
2244 }
2245
2246 return 1;
2247}
2248
2249bool do_dlsym(void* handle, const char* sym_name, const char* sym_ver,
2250 void* caller_addr, void** symbol) {
2251#if !defined(__LP64__)
2252 if (handle == nullptr) {
2253 DL_ERR("dlsym failed: library handle is null");
2254 return false;
2255 }
2256#endif
2257
2258 if (sym_name == nullptr) {
2259 DL_ERR("dlsym failed: symbol name is null");
2260 return false;
2261 }
2262
2263 soinfo* found = nullptr;
2264 const ElfW(Sym)* sym = nullptr;
2265 soinfo* caller = find_containing_library(caller_addr);
2266 android_namespace_t* ns = caller != nullptr ? caller->get_namespace() : g_anonymous_namespace;
2267
2268 version_info vi_instance;
2269 version_info* vi = nullptr;
2270
2271 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002272 vi_instance.name = sym_ver;
2273 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002274 vi = &vi_instance;
2275 }
2276
2277 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2278 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2279 } else {
2280 sym = dlsym_handle_lookup(reinterpret_cast<soinfo*>(handle), &found, sym_name, vi);
2281 }
2282
2283 if (sym != nullptr) {
2284 uint32_t bind = ELF_ST_BIND(sym->st_info);
2285
2286 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2287 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
2288 return true;
2289 }
2290
2291 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2292 return false;
2293 }
2294
2295 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2296 return false;
2297}
2298
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002299void do_dlclose(soinfo* si) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002300 ProtectedDataGuard guard;
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002301 soinfo_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002302}
2303
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002304bool init_namespaces(const char* public_ns_sonames, const char* anon_ns_library_path) {
2305 CHECK(public_ns_sonames != nullptr);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002306 if (g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002307 DL_ERR("public namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002308 return false;
2309 }
2310
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002311 std::vector<std::string> sonames = android::base::Split(public_ns_sonames, ":");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002312
2313 ProtectedDataGuard guard;
2314
2315 auto failure_guard = make_scope_guard([&]() {
2316 g_public_namespace.clear();
2317 });
2318
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002319 for (const auto& soname : sonames) {
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -08002320 soinfo* candidate = nullptr;
2321
2322 find_loaded_library_by_soname(&g_default_namespace, soname.c_str(), &candidate);
2323
2324 if (candidate == nullptr) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002325 DL_ERR("error initializing public namespace: \"%s\" was not found"
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002326 " in the default namespace", soname.c_str());
2327 return false;
2328 }
2329
2330 candidate->set_nodelete();
2331 g_public_namespace.push_back(candidate);
2332 }
2333
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002334 g_public_namespace_initialized = true;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002335
2336 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002337 // When the caller is nullptr - create_namespace will take global group
2338 // from the anonymous namespace, which is fine because anonymous namespace
2339 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002340 android_namespace_t* anon_ns =
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002341 create_namespace(nullptr, "(anonymous)", nullptr, anon_ns_library_path,
2342 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002343
2344 if (anon_ns == nullptr) {
2345 g_public_namespace_initialized = false;
2346 return false;
2347 }
2348 g_anonymous_namespace = anon_ns;
2349 failure_guard.disable();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002350 return true;
2351}
2352
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002353android_namespace_t* create_namespace(const void* caller_addr,
2354 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002355 const char* ld_library_path,
2356 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002357 uint64_t type,
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002358 const char* permitted_when_isolated_path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002359 if (!g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002360 DL_ERR("cannot create namespace: public namespace is not initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002361 return nullptr;
2362 }
2363
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002364 soinfo* caller_soinfo = find_containing_library(caller_addr);
2365
2366 android_namespace_t* caller_ns = caller_soinfo != nullptr ?
2367 caller_soinfo->get_namespace() :
2368 g_anonymous_namespace;
2369
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002370 ProtectedDataGuard guard;
2371 std::vector<std::string> ld_library_paths;
2372 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002373 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002374
2375 parse_path(ld_library_path, ":", &ld_library_paths);
2376 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002377 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002378
2379 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2380 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002381 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002382 ns->set_ld_library_paths(std::move(ld_library_paths));
2383 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002384 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002385
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002386 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
2387 // If shared - clone the caller namespace
2388 auto& soinfo_list = caller_ns->soinfo_list();
2389 std::copy(soinfo_list.begin(), soinfo_list.end(), std::back_inserter(ns->soinfo_list()));
2390 } else {
2391 // If not shared - copy only the global group
2392 auto global_group = make_global_group(caller_ns);
2393 std::copy(global_group.begin(), global_group.end(), std::back_inserter(ns->soinfo_list()));
2394 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002395
2396 return ns;
2397}
2398
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002399static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
2400 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2401 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2402 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002403 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2404 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002405
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002406 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002407}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002408
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002409const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2410 if (source_symver < 2 ||
2411 source_symver >= version_infos.size() ||
2412 version_infos[source_symver].name == nullptr) {
2413 return nullptr;
2414 }
2415
2416 return &version_infos[source_symver];
2417}
2418
2419void VersionTracker::add_version_info(size_t source_index,
2420 ElfW(Word) elf_hash,
2421 const char* ver_name,
2422 const soinfo* target_si) {
2423 if (source_index >= version_infos.size()) {
2424 version_infos.resize(source_index+1);
2425 }
2426
2427 version_infos[source_index].elf_hash = elf_hash;
2428 version_infos[source_index].name = ver_name;
2429 version_infos[source_index].target_si = target_si;
2430}
2431
2432bool VersionTracker::init_verneed(const soinfo* si_from) {
2433 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2434
2435 if (verneed_ptr == 0) {
2436 return true;
2437 }
2438
2439 size_t verneed_cnt = si_from->get_verneed_cnt();
2440
2441 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2442 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2443 size_t vernaux_offset = offset + verneed->vn_aux;
2444 offset += verneed->vn_next;
2445
2446 if (verneed->vn_version != 1) {
2447 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2448 return false;
2449 }
2450
2451 const char* target_soname = si_from->get_string(verneed->vn_file);
2452 // find it in dependencies
2453 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002454 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002455 });
2456
2457 if (target_si == nullptr) {
2458 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002459 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002460 return false;
2461 }
2462
2463 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2464 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2465 vernaux_offset += vernaux->vna_next;
2466
2467 const ElfW(Word) elf_hash = vernaux->vna_hash;
2468 const char* ver_name = si_from->get_string(vernaux->vna_name);
2469 ElfW(Half) source_index = vernaux->vna_other;
2470
2471 add_version_info(source_index, elf_hash, ver_name, target_si);
2472 }
2473 }
2474
2475 return true;
2476}
2477
2478bool VersionTracker::init_verdef(const soinfo* si_from) {
2479 return for_each_verdef(si_from,
2480 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2481 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2482 si_from->get_string(verdaux->vda_name), si_from);
2483 return false;
2484 }
2485 );
2486}
2487
2488bool VersionTracker::init(const soinfo* si_from) {
2489 if (!si_from->has_min_version(2)) {
2490 return true;
2491 }
2492
2493 return init_verneed(si_from) && init_verdef(si_from);
2494}
2495
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002496bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2497 const char* sym_name, const version_info** vi) {
2498 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2499 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2500
2501 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2502 *vi = version_tracker.get_version_info(sym_ver);
2503
2504 if (*vi == nullptr) {
2505 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002506 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002507 return false;
2508 }
2509 } else {
2510 // there is no version info
2511 *vi = nullptr;
2512 }
2513
2514 return true;
2515}
2516
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002517#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002518#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002519static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2520 return rela->r_addend;
2521}
2522#else
2523static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002524 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2525 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002526 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2527 }
2528 return 0;
2529}
2530#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002531
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002532template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002533bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2534 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002535 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2536 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002537 if (rel == nullptr) {
2538 return false;
2539 }
2540
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002541 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2542 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2543
2544 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002545 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002546 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002547 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002548
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002549 DEBUG("Processing '%s' relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002550 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002551 continue;
2552 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002553
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002554 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002555 soinfo* lsi = nullptr;
2556
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002557 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002558 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002559 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002560
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002561 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2562 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002563 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002564
2565 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2566 return false;
2567 }
2568
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002569 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002570 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002571 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002572 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002573 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002574 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002575 }
2576
2577 /* IHI0044C AAELF 4.5.1.1:
2578
2579 Libraries are not searched to resolve weak references.
2580 It is not an error for a weak reference to remain unsatisfied.
2581
2582 During linking, the value of an undefined weak reference is:
2583 - Zero if the relocation type is absolute
2584 - The address of the place if the relocation is pc-relative
2585 - The address of nominal base address if the relocation
2586 type is base-relative.
2587 */
2588
2589 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002590 case R_GENERIC_JUMP_SLOT:
2591 case R_GENERIC_GLOB_DAT:
2592 case R_GENERIC_RELATIVE:
2593 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002594#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002595 case R_AARCH64_ABS64:
2596 case R_AARCH64_ABS32:
2597 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002598#elif defined(__x86_64__)
2599 case R_X86_64_32:
2600 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002601#elif defined(__arm__)
2602 case R_ARM_ABS32:
2603#elif defined(__i386__)
2604 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002605#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002606 /*
2607 * The sym_addr was initialized to be zero above, or the relocation
2608 * code below does not care about value of sym_addr.
2609 * No need to do anything.
2610 */
2611 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002612#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002613 case R_X86_64_PC32:
2614 sym_addr = reloc;
2615 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002616#elif defined(__i386__)
2617 case R_386_PC32:
2618 sym_addr = reloc;
2619 break;
2620#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002621 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002622 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002623 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002624 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002625 } else { // We got a definition.
2626#if !defined(__LP64__)
2627 // When relocating dso with text_relocation .text segment is
2628 // not executable. We need to restore elf flags before resolving
2629 // STT_GNU_IFUNC symbol.
2630 bool protect_segments = has_text_relocations &&
2631 lsi == this &&
2632 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2633 if (protect_segments) {
2634 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2635 DL_ERR("can't protect segments for \"%s\": %s",
2636 get_realpath(), strerror(errno));
2637 return false;
2638 }
2639 }
2640#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002641 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002642#if !defined(__LP64__)
2643 if (protect_segments) {
2644 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2645 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2646 get_realpath(), strerror(errno));
2647 return false;
2648 }
2649 }
2650#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002651 }
2652 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002653 }
2654
2655 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002656 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002657 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002658 MARK(rel->r_offset);
2659 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2660 reinterpret_cast<void*>(reloc),
2661 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2662
2663 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002664 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002665 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002666 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002667 MARK(rel->r_offset);
2668 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2669 reinterpret_cast<void*>(reloc),
2670 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2671 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002672 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002673 case R_GENERIC_RELATIVE:
2674 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002675 MARK(rel->r_offset);
2676 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2677 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002678 reinterpret_cast<void*>(load_bias + addend));
2679 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002680 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002681 case R_GENERIC_IRELATIVE:
2682 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002683 MARK(rel->r_offset);
2684 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2685 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002686 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002687 {
2688#if !defined(__LP64__)
2689 // When relocating dso with text_relocation .text segment is
2690 // not executable. We need to restore elf flags for this
2691 // particular call.
2692 if (has_text_relocations) {
2693 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2694 DL_ERR("can't protect segments for \"%s\": %s",
2695 get_realpath(), strerror(errno));
2696 return false;
2697 }
2698 }
2699#endif
2700 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2701#if !defined(__LP64__)
2702 // Unprotect it afterwards...
2703 if (has_text_relocations) {
2704 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2705 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2706 get_realpath(), strerror(errno));
2707 return false;
2708 }
2709 }
2710#endif
2711 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2712 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002713 break;
2714
2715#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002716 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002717 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002718 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002719 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002720 reloc, sym_addr + addend, sym_name);
2721 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002722 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002723 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002724 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002725 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002726 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002727 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002728 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002729 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2730 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002731 if ((min_value <= (sym_addr + addend)) &&
2732 ((sym_addr + addend) <= max_value)) {
2733 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002734 } else {
2735 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002736 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002737 return false;
2738 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002739 }
2740 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002741 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002742 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002743 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002744 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002745 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002746 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002747 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2748 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002749 if ((min_value <= (sym_addr + addend)) &&
2750 ((sym_addr + addend) <= max_value)) {
2751 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002752 } else {
2753 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002754 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002755 return false;
2756 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002757 }
2758 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002759 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002760 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002761 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002762 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002763 reloc, sym_addr + addend, rel->r_offset, sym_name);
2764 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002765 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002766 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002767 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002768 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002769 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002770 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002771 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002772 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2773 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002774 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2775 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2776 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002777 } else {
2778 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002779 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002780 return false;
2781 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002782 }
2783 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002784 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002785 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002786 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002787 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002788 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002789 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002790 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2791 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002792 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2793 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2794 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002795 } else {
2796 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002797 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002798 return false;
2799 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002800 }
2801 break;
2802
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002803 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002804 /*
2805 * ET_EXEC is not supported so this should not happen.
2806 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002807 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002808 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002809 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002810 * R_AARCH64_COPY may only appear in executable objects where e_type is
2811 * set to ET_EXEC.
2812 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002813 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002814 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002815 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002816 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002817 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002818 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002819 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002820 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002821 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002822 break;
2823#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002824 case R_X86_64_32:
2825 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002826 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002827 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2828 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002829 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002830 break;
2831 case R_X86_64_64:
2832 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002833 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002834 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2835 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002836 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002837 break;
2838 case R_X86_64_PC32:
2839 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002840 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002841 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2842 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2843 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002844 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002845 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002846#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002847 case R_ARM_ABS32:
2848 count_relocation(kRelocAbsolute);
2849 MARK(rel->r_offset);
2850 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2851 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2852 break;
2853 case R_ARM_REL32:
2854 count_relocation(kRelocRelative);
2855 MARK(rel->r_offset);
2856 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2857 reloc, sym_addr, rel->r_offset, sym_name);
2858 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2859 break;
2860 case R_ARM_COPY:
2861 /*
2862 * ET_EXEC is not supported so this should not happen.
2863 *
2864 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2865 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002866 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002867 * R_ARM_COPY may only appear in executable objects where e_type is
2868 * set to ET_EXEC.
2869 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002870 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002871 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002872#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002873 case R_386_32:
2874 count_relocation(kRelocRelative);
2875 MARK(rel->r_offset);
2876 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2877 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2878 break;
2879 case R_386_PC32:
2880 count_relocation(kRelocRelative);
2881 MARK(rel->r_offset);
2882 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2883 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2884 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2885 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002886#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002887 default:
2888 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002889 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002890 }
2891 }
2892 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002893}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002894#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002895
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002896void soinfo::call_array(const char* array_name __unused, linker_function_t* functions,
2897 size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002898 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002899 return;
2900 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02002901
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002902 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, get_realpath());
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002903
2904 int begin = reverse ? (count - 1) : 0;
2905 int end = reverse ? -1 : count;
2906 int step = reverse ? -1 : 1;
2907
2908 for (int i = begin; i != end; i += step) {
2909 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002910 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002911 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02002912
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002913 TRACE("[ Done calling %s for '%s' ]", array_name, get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002914}
2915
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002916void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002917 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002918 return;
2919 }
2920
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002921 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Elliott Hughesd23736e2012-11-01 15:16:56 -07002922 function();
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002923 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04002924}
2925
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002926void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002927 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
2928 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002929 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002930}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002931
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002932void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002933 if (constructors_called) {
2934 return;
2935 }
Jesse Hallf5d16932012-01-30 15:39:57 -08002936
Elliott Hughesd23736e2012-11-01 15:16:56 -07002937 // We set constructors_called before actually calling the constructors, otherwise it doesn't
2938 // protect against recursive constructor calls. One simple example of constructor recursion
2939 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
2940 // 1. The program depends on libc, so libc's constructor is called here.
2941 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
2942 // 3. dlopen() calls the constructors on the newly created
2943 // soinfo for libc_malloc_debug_leak.so.
2944 // 4. The debug .so depends on libc, so CallConstructors is
2945 // called again with the libc soinfo. If it doesn't trigger the early-
2946 // out above, the libc constructor will be called again (recursively!).
2947 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002948
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002949 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002950 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07002951 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002952 get_realpath(), preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002953 }
2954
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002955 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002956 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002957 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002958
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002959 TRACE("\"%s\": calling constructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002960
2961 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002962 call_function("DT_INIT", init_func_);
2963 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002964}
David 'Digit' Turner82156792009-05-18 14:37:41 +02002965
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002966void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002967 if (!constructors_called) {
2968 return;
2969 }
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002970 TRACE("\"%s\": calling destructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002971
2972 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002973 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002974
2975 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002976 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002977
2978 // This is needed on second call to dlopen
2979 // after library has been unloaded with RTLD_NODELETE
2980 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002981}
2982
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002983void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002984 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002985 child->parents_.push_back(this);
2986 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002987 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002988}
2989
2990void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002991 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002992 return;
2993 }
2994
2995 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002996 children_.for_each([&] (soinfo* child) {
2997 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002998 return parent == this;
2999 });
3000 });
3001
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003002 parents_.for_each([&] (soinfo* parent) {
3003 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003004 return child == this;
3005 });
3006 });
3007
3008 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003009 parents_.clear();
3010 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003011}
3012
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003013dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003014 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003015 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003016 }
3017
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003018 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003019};
3020
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003021ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003022 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003023 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003024 }
3025
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003026 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003027}
3028
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003029off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003030 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003031 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003032 }
3033
3034 return 0;
3035}
3036
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003037uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07003038 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003039 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07003040 }
3041
3042 return 0;
3043}
3044
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003045uint32_t soinfo::get_dt_flags_1() const {
3046 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003047 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003048 }
3049
3050 return 0;
3051}
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003052
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003053void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
3054 if (has_min_version(1)) {
3055 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003056 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003057 }
3058
3059 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003060 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003061 }
3062
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003063 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003064 }
3065}
3066
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003067void soinfo::set_nodelete() {
3068 rtld_flags_ |= RTLD_NODELETE;
3069}
3070
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003071const char* soinfo::get_realpath() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003072#if defined(__work_around_b_24465209__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003073 if (has_min_version(2)) {
3074 return realpath_.c_str();
3075 } else {
3076 return old_name_;
3077 }
3078#else
3079 return realpath_.c_str();
3080#endif
3081}
3082
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003083void soinfo::set_soname(const char* soname) {
3084#if defined(__work_around_b_24465209__)
3085 if (has_min_version(2)) {
3086 soname_ = soname;
3087 }
3088 strlcpy(old_name_, soname_, sizeof(old_name_));
3089#else
3090 soname_ = soname;
3091#endif
3092}
3093
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003094const char* soinfo::get_soname() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003095#if defined(__work_around_b_24465209__)
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003096 if (has_min_version(2)) {
3097 return soname_;
3098 } else {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003099 return old_name_;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003100 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003101#else
3102 return soname_;
3103#endif
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003104}
3105
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003106// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003107// 'this->flags' does not have FLAG_NEW_SOINFO set.
3108static soinfo::soinfo_list_t g_empty_list;
3109
3110soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003111 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003112 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003113 }
3114
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003115 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003116}
3117
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003118const soinfo::soinfo_list_t& soinfo::get_children() const {
3119 if (has_min_version(0)) {
3120 return children_;
3121 }
3122
3123 return g_empty_list;
3124}
3125
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003126soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003127 if (has_min_version(0)) {
3128 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003129 }
3130
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003131 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003132}
3133
Evgenii Stepanov68650822015-06-10 13:38:39 -07003134static std::vector<std::string> g_empty_runpath;
3135
3136const std::vector<std::string>& soinfo::get_dt_runpath() const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003137 if (has_min_version(3)) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003138 return dt_runpath_;
3139 }
3140
3141 return g_empty_runpath;
3142}
3143
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003144android_namespace_t* soinfo::get_namespace() {
3145 if (has_min_version(3)) {
3146 return namespace_;
3147 }
3148
3149 return &g_default_namespace;
3150}
3151
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003152ElfW(Addr) soinfo::resolve_symbol_address(const ElfW(Sym)* s) const {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003153 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
3154 return call_ifunc_resolver(s->st_value + load_bias);
3155 }
3156
3157 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
3158}
3159
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003160const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003161 if (has_min_version(1) && (index >= strtab_size_)) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003162 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003163 get_realpath(), strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003164 }
3165
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003166 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003167}
3168
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003169bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003170 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003171}
3172
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003173bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003174 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003175}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003176
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003177bool soinfo::is_linked() const {
3178 return (flags_ & FLAG_LINKED) != 0;
3179}
3180
3181bool soinfo::is_main_executable() const {
3182 return (flags_ & FLAG_EXE) != 0;
3183}
3184
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -08003185bool soinfo::is_linker() const {
3186 return (flags_ & FLAG_LINKER) != 0;
3187}
3188
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003189void soinfo::set_linked() {
3190 flags_ |= FLAG_LINKED;
3191}
3192
3193void soinfo::set_linker_flag() {
3194 flags_ |= FLAG_LINKER;
3195}
3196
3197void soinfo::set_main_executable() {
3198 flags_ |= FLAG_EXE;
3199}
3200
3201void soinfo::increment_ref_count() {
3202 local_group_root_->ref_count_++;
3203}
3204
3205size_t soinfo::decrement_ref_count() {
3206 return --local_group_root_->ref_count_;
3207}
3208
3209soinfo* soinfo::get_local_group_root() const {
3210 return local_group_root_;
3211}
3212
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -08003213
3214void soinfo::set_mapped_by_caller(bool mapped_by_caller) {
3215 if (mapped_by_caller) {
3216 flags_ |= FLAG_MAPPED_BY_CALLER;
3217 } else {
3218 flags_ &= ~FLAG_MAPPED_BY_CALLER;
3219 }
3220}
3221
3222bool soinfo::is_mapped_by_caller() const {
3223 return (flags_ & FLAG_MAPPED_BY_CALLER) != 0;
3224}
3225
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003226// This function returns api-level at the time of
3227// dlopen/load. Note that libraries opened by system
3228// will always have 'current' api level.
3229uint32_t soinfo::get_target_sdk_version() const {
3230 if (!has_min_version(2)) {
3231 return __ANDROID_API__;
3232 }
3233
3234 return local_group_root_->target_sdk_version_;
3235}
3236
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003237bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08003238 /* Extract dynamic section */
3239 ElfW(Word) dynamic_flags = 0;
3240 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07003241
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003242 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003243 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003244 if (!relocating_linker) {
Elliott Hughes116b5692016-01-04 17:45:36 -08003245 INFO("[ Linking '%s' ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003246 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003247 }
3248
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003249 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003250 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003251 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003252 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003253 return false;
3254 } else {
3255 if (!relocating_linker) {
3256 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003257 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003258 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003259
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003260#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003261 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
3262 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003263#endif
3264
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003265 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003266 // Note that: "Except for the DT_NULL element at the end of the array,
3267 // and the relative order of DT_NEEDED elements, entries may appear in any order."
3268 //
3269 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003270 uint32_t needed_count = 0;
3271 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
3272 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
3273 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3274 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003275 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003276 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003277 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003278
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003279 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003280 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
3281 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
3282 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
3283 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003284 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003285
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003286 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003287 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003288 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003289 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
3290 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003291
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003292 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003293 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003294 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003295 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
3296 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003297
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003298 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003299 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003300 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003301 return false;
3302 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003303 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003304
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003305 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003306 break;
3307
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003308 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003309 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003310 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003311
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003312 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003313 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003314 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003315
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003316 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003317 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003318 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003319
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003320 case DT_SYMENT:
3321 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003322 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
3323 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003324 return false;
3325 }
3326 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003327
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003328 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003329#if defined(USE_RELA)
3330 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003331 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003332 return false;
3333 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003334#else
3335 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003336 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003337 return false;
3338 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003339#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003340 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003341
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003342 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003343#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003344 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003345#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003346 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003347#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003348 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003349
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003350 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003351#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003352 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003353#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003354 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003355#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003356 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003357
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003358 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003359#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003360 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003361 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003362#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003363 // Ignore for other platforms... (because RTLD_LAZY is not supported)
3364 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003365
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003366 case DT_DEBUG:
3367 // Set the DT_DEBUG entry to the address of _r_debug for GDB
3368 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08003369// FIXME: not working currently for N64
3370// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003371// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08003372// read-only, but the DYNAMIC header claims it is writable.
3373#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003374 if ((dynamic_flags & PF_W) != 0) {
3375 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
3376 }
Chris Dearman99186652014-02-06 20:36:51 -08003377#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08003378 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003379#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003380 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003381 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003382 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003383
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003384 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003385 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003386 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003387
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003388 case DT_ANDROID_RELA:
3389 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3390 break;
3391
3392 case DT_ANDROID_RELASZ:
3393 android_relocs_size_ = d->d_un.d_val;
3394 break;
3395
3396 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003397 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003398 return false;
3399
3400 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003401 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003402 return false;
3403
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003404 case DT_RELAENT:
3405 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003406 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003407 return false;
3408 }
3409 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003410
3411 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003412 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003413 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003414
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003415 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003416 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003417 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003418
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003419 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003420 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003421 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003422
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003423#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003424 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003425 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003426 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003427
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003428 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003429 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003430 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003431
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003432 case DT_RELENT:
3433 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003434 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003435 return false;
3436 }
3437 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003438
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003439 case DT_ANDROID_REL:
3440 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3441 break;
3442
3443 case DT_ANDROID_RELSZ:
3444 android_relocs_size_ = d->d_un.d_val;
3445 break;
3446
3447 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003448 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003449 return false;
3450
3451 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003452 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003453 return false;
3454
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003455 // "Indicates that all RELATIVE relocations have been concatenated together,
3456 // and specifies the RELATIVE relocation count."
3457 //
3458 // TODO: Spec also mentions that this can be used to optimize relocation process;
3459 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003460 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003461 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003462
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003463 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003464 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003465 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003466
3467 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003468 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003469 return false;
3470
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003471#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003472 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003473 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003474 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003475 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003476
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003477 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003478 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003479 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_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_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003483 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003484 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
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_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003488 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003489 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003490
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003491 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003492 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003493 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
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_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003497 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003498 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003499
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003500 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003501 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003502 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
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_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003506 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003507 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003508
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003509 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003510#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003511 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003512 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003513#else
3514 has_text_relocations = true;
3515 break;
3516#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003517
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003518 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003519 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003520 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003521
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003522 case DT_NEEDED:
3523 ++needed_count;
3524 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003525
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003526 case DT_FLAGS:
3527 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003528#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003529 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003530 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003531#else
3532 has_text_relocations = true;
3533#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003534 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003535 if (d->d_un.d_val & DF_SYMBOLIC) {
3536 has_DT_SYMBOLIC = true;
3537 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003538 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003539
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003540 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003541 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003542
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003543 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -07003544 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 -07003545 }
3546 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003547#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003548 case DT_MIPS_RLD_MAP:
3549 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3550 {
3551 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3552 *dp = &_r_debug;
3553 }
3554 break;
Raghu Gandham68815722014-12-18 19:12:19 -08003555 case DT_MIPS_RLD_MAP2:
3556 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
3557 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003558 r_debug** dp = reinterpret_cast<r_debug**>(
3559 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003560 *dp = &_r_debug;
3561 }
3562 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003563
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003564 case DT_MIPS_RLD_VERSION:
3565 case DT_MIPS_FLAGS:
3566 case DT_MIPS_BASE_ADDRESS:
3567 case DT_MIPS_UNREFEXTNO:
3568 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003569
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003570 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003571 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003572 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003573
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003574 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003575 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003576 break;
3577
3578 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003579 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003580 break;
3581#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003582 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3583 case DT_BIND_NOW:
3584 break;
3585
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003586 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003587 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3588 break;
3589
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003590 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003591 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3592 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003593 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003594 verdef_cnt_ = d->d_un.d_val;
3595 break;
3596
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003597 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003598 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3599 break;
3600
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003601 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003602 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003603 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003604
Evgenii Stepanov68650822015-06-10 13:38:39 -07003605 case DT_RUNPATH:
3606 // this is parsed after we have strtab initialized (see below).
3607 break;
3608
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003609 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003610 if (!relocating_linker) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003611 DL_WARN("%s: unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003612 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3613 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003614 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003615 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003616 }
3617
Duane Sandbc425c72015-06-01 16:29:14 -07003618#if defined(__mips__) && !defined(__LP64__)
3619 if (!mips_check_and_adjust_fp_modes()) {
3620 return false;
3621 }
3622#endif
3623
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003624 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003625 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003626
3627 // Sanity checks.
3628 if (relocating_linker && needed_count != 0) {
3629 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3630 return false;
3631 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003632 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003633 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003634 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003635 return false;
3636 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003637 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003638 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003639 return false;
3640 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003641 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003642 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003643 return false;
3644 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003645
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003646 // second pass - parse entries relying on strtab
3647 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003648 switch (d->d_tag) {
3649 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003650 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003651 break;
3652 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003653 set_dt_runpath(get_string(d->d_un.d_val));
3654 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003655 }
3656 }
3657
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003658 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003659 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003660 // because they can't find dt_needed library by soname.
3661 // This workaround should keep them working. (applies only
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003662 // for apps targeting sdk version <=22). Make an exception for
3663 // the main executable and linker; they do not need to have dt_soname
3664 if (soname_ == nullptr && this != somain && (flags_ & FLAG_LINKER) == 0 &&
3665 get_application_target_sdk_version() <= 22) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003666 soname_ = basename(realpath_.c_str());
3667 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3668 get_realpath(), soname_);
3669 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003670 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003671}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003672
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003673bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3674 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003675
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003676 local_group_root_ = local_group.front();
3677 if (local_group_root_ == nullptr) {
3678 local_group_root_ = this;
3679 }
3680
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003681 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3682 target_sdk_version_ = get_application_target_sdk_version();
3683 }
3684
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003685 VersionTracker version_tracker;
3686
3687 if (!version_tracker.init(this)) {
3688 return false;
3689 }
3690
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003691#if !defined(__LP64__)
3692 if (has_text_relocations) {
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003693 // Fail if app is targeting sdk version > 22
Dmitriy Ivanov80687862015-10-09 13:58:46 -07003694 if (get_application_target_sdk_version() > 22) {
Dmitriy Ivanovfae39d22015-10-13 11:07:56 -07003695 PRINT("%s: has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003696 DL_ERR("%s: has text relocations", get_realpath());
3697 return false;
3698 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003699 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003700 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003701 DL_WARN("%s has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003702 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003703 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3704 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003705 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003706 return false;
3707 }
3708 }
3709#endif
3710
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003711 if (android_relocs_ != nullptr) {
3712 // check signature
3713 if (android_relocs_size_ > 3 &&
3714 android_relocs_[0] == 'A' &&
3715 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003716 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003717 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003718 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003719
3720 bool relocated = false;
3721 const uint8_t* packed_relocs = android_relocs_ + 4;
3722 const size_t packed_relocs_size = android_relocs_size_ - 4;
3723
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003724 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003725 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003726 packed_reloc_iterator<sleb128_decoder>(
3727 sleb128_decoder(packed_relocs, packed_relocs_size)),
3728 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003729
3730 if (!relocated) {
3731 return false;
3732 }
3733 } else {
3734 DL_ERR("bad android relocation header.");
3735 return false;
3736 }
3737 }
3738
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003739#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003740 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003741 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003742 if (!relocate(version_tracker,
3743 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003744 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003745 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003746 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003747 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003748 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003749 if (!relocate(version_tracker,
3750 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003751 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003752 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003753 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003754#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003755 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003756 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003757 if (!relocate(version_tracker,
3758 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003759 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003760 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003761 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003762 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003763 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003764 if (!relocate(version_tracker,
3765 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003766 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003767 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003768 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003769#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003770
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003771#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003772 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003773 return false;
3774 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003775#endif
3776
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003777 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003778
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003779#if !defined(__LP64__)
3780 if (has_text_relocations) {
3781 // All relocations are done, we can protect our segments back to read-only.
3782 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3783 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003784 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003785 return false;
3786 }
3787 }
3788#endif
3789
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003790 /* We can also turn on GNU RELRO protection */
3791 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3792 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003793 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003794 return false;
3795 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003796
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003797 /* Handle serializing/sharing the RELRO segment */
3798 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3799 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3800 extinfo->relro_fd) < 0) {
3801 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003802 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003803 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003804 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003805 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3806 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3807 extinfo->relro_fd) < 0) {
3808 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003809 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003810 return false;
3811 }
3812 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003813
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003814 notify_gdb_of_load(this);
3815 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003816}
3817
Nick Kralevich468319c2011-11-11 15:53:17 -08003818/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003819 * This function add vdso to internal dso list.
3820 * It helps to stack unwinding through signal handlers.
3821 * Also, it makes bionic more like glibc.
3822 */
Kito Cheng812fd422014-03-25 22:53:56 +08003823static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003824#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08003825 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003826 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08003827 return;
3828 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003829
Dmitriy Ivanovd9b08a02015-11-16 13:17:27 -08003830 soinfo* si = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003831
Elliott Hughes0266ae52014-02-10 17:46:57 -08003832 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
3833 si->phnum = ehdr_vdso->e_phnum;
3834 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
3835 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003836 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003837
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003838 si->prelink_image();
3839 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003840#endif
3841}
3842
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003843/* gdb expects the linker to be in the debug shared object list.
3844 * Without this, gdb has trouble locating the linker's ".text"
3845 * and ".plt" sections. Gdb could also potentially use this to
3846 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
Dimitry Ivanov64001292016-02-17 14:13:06 -08003847 * Note that the linker shouldn't be on the soinfo list.
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003848 */
3849static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dimitry Ivanov8d22dd52016-02-16 13:43:35 -08003850 static link_map linker_link_map_for_gdb;
3851#if defined(__LP64__)
3852 static char kLinkerPath[] = "/system/bin/linker64";
3853#else
3854 static char kLinkerPath[] = "/system/bin/linker";
3855#endif
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003856
Dimitry Ivanov8d22dd52016-02-16 13:43:35 -08003857 linker_link_map_for_gdb.l_addr = linker_base;
3858 linker_link_map_for_gdb.l_name = kLinkerPath;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003859
3860 /*
3861 * Set the dynamic field in the link map otherwise gdb will complain with
3862 * the following:
3863 * warning: .dynamic section for "/system/bin/linker" is not at the
3864 * expected address (wrong library or version mismatch?)
3865 */
3866 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
3867 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
3868 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Dimitry Ivanov8d22dd52016-02-16 13:43:35 -08003869 &linker_link_map_for_gdb.l_ld, nullptr);
3870
3871 insert_link_map_into_debug_map(&linker_link_map_for_gdb);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003872}
3873
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003874static void init_default_namespace() {
3875 g_default_namespace.set_name("(default)");
3876 g_default_namespace.set_isolated(false);
3877
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003878 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3879 somain->load_bias);
3880 const char* bname = basename(interp);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003881 if (bname && (strcmp(bname, "linker_asan") == 0 || strcmp(bname, "linker_asan64") == 0)) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003882 g_default_ld_paths = kAsanDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003883 } else {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003884 g_default_ld_paths = kDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003885 }
3886
3887 std::vector<std::string> ld_default_paths;
3888 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
3889 ld_default_paths.push_back(g_default_ld_paths[i]);
3890 }
3891
3892 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003893};
3894
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07003895extern "C" int __system_properties_init(void);
3896
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003897/*
Nick Kralevich468319c2011-11-11 15:53:17 -08003898 * This code is called after the linker has linked itself and
3899 * fixed it's own GOT. It is safe to make references to externs
3900 * and other non-local data at this point.
3901 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08003902static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04003903#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003904 struct timeval t0, t1;
3905 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04003906#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003907
Elliott Hughes1801db32015-06-08 18:04:00 -07003908 // Sanitize the environment.
3909 __libc_init_AT_SECURE(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01003910
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07003911 // Initialize system properties
3912 __system_properties_init(); // may use 'environ'
3913
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003914 debuggerd_init();
3915
3916 // Get a few environment variables.
Elliott Hughes1801db32015-06-08 18:04:00 -07003917 const char* LD_DEBUG = getenv("LD_DEBUG");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003918 if (LD_DEBUG != nullptr) {
3919 g_ld_debug_verbosity = atoi(LD_DEBUG);
3920 }
3921
Elliott Hughes116b5692016-01-04 17:45:36 -08003922#if defined(__LP64__)
3923 INFO("[ Android dynamic linker (64-bit) ]");
3924#else
3925 INFO("[ Android dynamic linker (32-bit) ]");
3926#endif
3927
Elliott Hughes1801db32015-06-08 18:04:00 -07003928 // These should have been sanitized by __libc_init_AT_SECURE, but the test
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003929 // doesn't cost us anything.
3930 const char* ldpath_env = nullptr;
3931 const char* ldpreload_env = nullptr;
Elliott Hughes1801db32015-06-08 18:04:00 -07003932 if (!getauxval(AT_SECURE)) {
3933 ldpath_env = getenv("LD_LIBRARY_PATH");
Elliott Hughes116b5692016-01-04 17:45:36 -08003934 if (ldpath_env != nullptr) {
3935 INFO("[ LD_LIBRARY_PATH set to '%s' ]", ldpath_env);
3936 }
Elliott Hughes1801db32015-06-08 18:04:00 -07003937 ldpreload_env = getenv("LD_PRELOAD");
Elliott Hughes116b5692016-01-04 17:45:36 -08003938 if (ldpreload_env != nullptr) {
3939 INFO("[ LD_PRELOAD set to '%s' ]", ldpreload_env);
3940 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003941 }
3942
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003943 soinfo* si = soinfo_alloc(&g_default_namespace, args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003944 if (si == nullptr) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08003945 __libc_fatal("Couldn't allocate soinfo: out of memory?");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003946 }
3947
3948 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003949 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003950 link_map* map = &(si->link_map_head);
3951
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -08003952 // Register the main executable and the linker upfront to have
3953 // gdb aware of them before loading the rest of the dependency
3954 // tree.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003955 map->l_addr = 0;
3956 map->l_name = args.argv[0];
Dimitry Ivanovf3064e42016-02-17 15:25:25 -08003957 insert_link_map_into_debug_map(map);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003958 init_linker_info_for_gdb(linker_base);
3959
3960 // Extract information passed from the kernel.
3961 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
3962 si->phnum = args.getauxval(AT_PHNUM);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003963
3964 /* Compute the value of si->base. We can't rely on the fact that
3965 * the first entry is the PHDR because this will not be true
3966 * for certain executables (e.g. some in the NDK unit test suite)
3967 */
3968 si->base = 0;
3969 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
3970 si->load_bias = 0;
3971 for (size_t i = 0; i < si->phnum; ++i) {
3972 if (si->phdr[i].p_type == PT_PHDR) {
3973 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
3974 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
3975 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07003976 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003977 }
3978 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07003979
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003980 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
3981 if (elf_hdr->e_type != ET_DYN) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08003982 __libc_fatal("\"%s\": error: only position independent executables (PIE) are supported.",
3983 args.argv[0]);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003984 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003985
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003986 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
3987 parse_LD_LIBRARY_PATH(ldpath_env);
3988 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01003989
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003990 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003991
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003992 init_default_namespace();
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003993
Dmitriy Ivanov67181252015-01-07 15:48:25 -08003994 if (!si->prelink_image()) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08003995 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
Dmitriy Ivanov67181252015-01-07 15:48:25 -08003996 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003997
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003998 // add somain to global group
3999 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
4000
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004001 // Load ld_preloads and dependencies.
4002 StringLinkedList needed_library_name_list;
4003 size_t needed_libraries_count = 0;
4004 size_t ld_preloads_count = 0;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07004005
4006 for (const auto& ld_preload_name : g_ld_preload_names) {
4007 needed_library_name_list.push_back(ld_preload_name.c_str());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004008 ++needed_libraries_count;
Dmitriy Ivanovf8093a92015-04-28 18:09:53 -07004009 ++ld_preloads_count;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004010 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004011
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004012 for_each_dt_needed(si, [&](const char* name) {
4013 needed_library_name_list.push_back(name);
4014 ++needed_libraries_count;
4015 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004016
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004017 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004018
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004019 memset(needed_library_names, 0, sizeof(needed_library_names));
4020 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004021
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07004022 if (needed_libraries_count > 0 &&
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004023 !find_libraries(&g_default_namespace, si, needed_library_names, needed_libraries_count,
4024 nullptr, &g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07004025 /* add_as_children */ true)) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004026 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004027 } else if (needed_libraries_count == 0) {
4028 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004029 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004030 }
4031 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004032 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004033
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004034 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07004035
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08004036 {
4037 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06004038
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08004039 si->call_pre_init_constructors();
4040
4041 /* After the prelink_image, the si->load_bias is initialized.
4042 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
4043 * We need to update this value for so exe here. So Unwind_Backtrace
4044 * for some arch like x86 could work correctly within so exe.
4045 */
4046 map->l_addr = si->load_bias;
4047 si->call_constructors();
4048 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04004049
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004050#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004051 gettimeofday(&t1, nullptr);
4052 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
4053 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
4054 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004055#endif
4056#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004057 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
4058 linker_stats.count[kRelocAbsolute],
4059 linker_stats.count[kRelocRelative],
4060 linker_stats.count[kRelocCopy],
4061 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004062#endif
4063#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004064 {
4065 unsigned n;
4066 unsigned i;
4067 unsigned count = 0;
4068 for (n = 0; n < 4096; n++) {
4069 if (bitmask[n]) {
4070 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004071#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004072 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004073#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004074 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004075#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004076 if (x & 1) {
4077 count++;
4078 }
4079 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004080 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004081 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004082 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004083 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
4084 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004085#endif
4086
4087#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004088 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004089#endif
4090
Dimitry Ivanove687d062016-02-16 13:25:29 -08004091 ElfW(Addr) entry = args.getauxval(AT_ENTRY);
4092 TRACE("[ Ready to execute '%s' @ %p ]", si->get_realpath(), reinterpret_cast<void*>(entry));
4093 return entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004094}
Nick Kralevich468319c2011-11-11 15:53:17 -08004095
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004096/* Compute the load-bias of an existing executable. This shall only
4097 * be used to compute the load bias of an executable or shared library
4098 * that was loaded by the kernel itself.
4099 *
4100 * Input:
4101 * elf -> address of ELF header, assumed to be at the start of the file.
4102 * Return:
4103 * load bias, i.e. add the value of any p_vaddr in the file to get
4104 * the corresponding address in memory.
4105 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004106static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
4107 ElfW(Addr) offset = elf->e_phoff;
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004108 const ElfW(Phdr)* phdr_table =
4109 reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004110 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004111
Elliott Hughes0266ae52014-02-10 17:46:57 -08004112 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08004113 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08004114 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004115 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08004116 }
4117 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004118}
4119
Elliott Hughes42d949f2016-01-06 19:51:43 -08004120extern "C" int __set_tls(void*);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004121extern "C" void _start();
4122
Nick Kralevich468319c2011-11-11 15:53:17 -08004123/*
4124 * This is the entry point for the linker, called from begin.S. This
4125 * method is responsible for fixing the linker's own relocations, and
4126 * then calling __linker_init_post_relocation().
4127 *
4128 * Because this method is called before the linker has fixed it's own
4129 * relocations, any attempt to reference an extern variable, extern
4130 * function, or other GOT reference will generate a segfault.
4131 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004132extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004133 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08004134
Elliott Hughes42d949f2016-01-06 19:51:43 -08004135 void* tls[BIONIC_TLS_SLOTS];
4136 __set_tls(tls);
4137
Elliott Hughes0266ae52014-02-10 17:46:57 -08004138 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004139 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004140 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08004141 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08004142
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004143 soinfo linker_so(nullptr, nullptr, nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08004144
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004145 // If the linker is not acting as PT_INTERP entry_point is equal to
4146 // _start. Which means that the linker is running as an executable and
4147 // already linked by PT_INTERP.
4148 //
4149 // This happens when user tries to run 'adb shell /system/bin/linker'
4150 // see also https://code.google.com/p/android/issues/detail?id=63174
4151 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004152 __libc_format_fd(STDOUT_FILENO,
4153 "This is %s, the helper program for shared library executables.\n",
4154 args.argv[0]);
4155 exit(0);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004156 }
4157
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004158 linker_so.base = linker_addr;
4159 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
4160 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07004161 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004162 linker_so.phdr = phdr;
4163 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004164 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07004165
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07004166 // This might not be obvious... The reasons why we pass g_empty_list
4167 // in place of local_group here are (1) we do not really need it, because
4168 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
4169 // itself without having to look into local_group and (2) allocators
4170 // are not yet initialized, and therefore we cannot use linked_list.push_*
4171 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004172 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004173 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004174 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07004175
Elliott Hughesd2948632015-07-21 11:57:09 -07004176 __libc_init_main_thread(args);
Dmitriy Ivanov14241402014-08-26 14:16:52 -07004177
Josh Gao93c0f5e2015-10-06 11:08:13 -07004178 // Initialize the linker's static libc's globals
4179 __libc_init_globals(args);
4180
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004181 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004182 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07004183
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004184 // Initialize static variables. Note that in order to
4185 // get correct libdl_info we need to call constructors
4186 // before get_libdl_info().
4187 solist = get_libdl_info();
4188 sonext = get_libdl_info();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004189 g_default_namespace.soinfo_list().push_back(get_libdl_info());
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004190
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004191 // We have successfully fixed our own relocations. It's safe to run
4192 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07004193 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08004194 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004195
Elliott Hughes116b5692016-01-04 17:45:36 -08004196 INFO("[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address));
Elliott Hughes611f9562015-01-23 10:43:58 -08004197
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004198 // Return the address that the calling assembly stub should jump to.
4199 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08004200}