blob: 9eb3a65c7381109d693055f4b5a197424ce66f3b [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002 * Copyright (C) 2008 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Dmitriy Ivanov19133522015-06-02 17:36:54 -070029#include <android/api-level.h>
Elliott Hughes46882792012-08-03 16:49:39 -070030#include <errno.h>
31#include <fcntl.h>
Elliott Hughes0266ae52014-02-10 17:46:57 -080032#include <inttypes.h>
Elliott Hughes46882792012-08-03 16:49:39 -070033#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
Elliott Hughes46882792012-08-03 16:49:39 -070037#include <sys/mman.h>
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -080038#include <sys/param.h>
Elliott Hughes46882792012-08-03 16:49:39 -070039#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070041#include <new>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070042#include <string>
Dmitriy Ivanovb4827502015-09-28 16:38:31 -070043#include <unordered_map>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070044#include <vector>
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070045
Elliott Hughes46882792012-08-03 16:49:39 -070046// Private C library headers.
Mingwei Shibe910522015-11-12 07:02:14 +000047#include "private/bionic_globals.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070048#include "private/bionic_tls.h"
49#include "private/KernelArgumentBlock.h"
50#include "private/ScopedPthreadMutexLocker.h"
Dmitriy Ivanov14669a92014-09-05 16:42:53 -070051#include "private/ScopeGuard.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052
53#include "linker.h"
Dmitriy Ivanovc9ce70d2015-03-10 15:30:26 -070054#include "linker_block_allocator.h"
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -080055#include "linker_gdb_support.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056#include "linker_debug.h"
Dmitriy Ivanov18870d32015-04-22 13:10:04 -070057#include "linker_sleb128.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020058#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080059#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080060#include "linker_reloc_iterators.h"
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070061#include "linker_utils.h"
tony.ys_liub4474402015-07-29 18:00:22 +080062
Elliott Hughes939a7e02015-12-04 15:27:46 -080063#include "android-base/strings.h"
Simon Baldwinaef71952015-01-16 13:22:54 +000064#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080065
Josh Gao93c0f5e2015-10-06 11:08:13 -070066extern void __libc_init_globals(KernelArgumentBlock&);
Elliott Hughes1801db32015-06-08 18:04:00 -070067extern void __libc_init_AT_SECURE(KernelArgumentBlock&);
68
Mingwei Shibe910522015-11-12 07:02:14 +000069extern "C" void _start();
70
Elliott Hughes1801db32015-06-08 18:04:00 -070071// Override macros to use C++ style casts.
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080072#undef ELF_ST_TYPE
73#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
74
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070075struct android_namespace_t {
76 public:
77 android_namespace_t() : name_(nullptr), is_isolated_(false) {}
78
79 const char* get_name() const { return name_; }
80 void set_name(const char* name) { name_ = name; }
81
82 bool is_isolated() const { return is_isolated_; }
83 void set_isolated(bool isolated) { is_isolated_ = isolated; }
84
85 const std::vector<std::string>& get_ld_library_paths() const {
86 return ld_library_paths_;
87 }
88 void set_ld_library_paths(std::vector<std::string>&& library_paths) {
89 ld_library_paths_ = library_paths;
90 }
91
92 const std::vector<std::string>& get_default_library_paths() const {
93 return default_library_paths_;
94 }
95 void set_default_library_paths(std::vector<std::string>&& library_paths) {
96 default_library_paths_ = library_paths;
97 }
98
Dimitry Ivanovd17a3772016-03-01 13:11:28 -080099 const std::vector<std::string>& get_permitted_paths() const {
100 return permitted_paths_;
101 }
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800102 void set_permitted_paths(std::vector<std::string>&& permitted_paths) {
103 permitted_paths_ = permitted_paths;
104 }
105
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700106 void add_soinfo(soinfo* si) {
107 soinfo_list_.push_back(si);
108 }
109
110 void add_soinfos(const soinfo::soinfo_list_t& soinfos) {
111 for (auto si : soinfos) {
112 add_soinfo(si);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700113 si->add_secondary_namespace(this);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700114 }
115 }
116
117 void remove_soinfo(soinfo* si) {
118 soinfo_list_.remove_if([&](soinfo* candidate) {
119 return si == candidate;
120 });
121 }
122
123 const soinfo::soinfo_list_t& soinfo_list() const { return soinfo_list_; }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700124
125 // For isolated namespaces - checks if the file is on the search path;
126 // always returns true for not isolated namespace.
127 bool is_accessible(const std::string& path);
128
129 private:
130 const char* name_;
131 bool is_isolated_;
132 std::vector<std::string> ld_library_paths_;
133 std::vector<std::string> default_library_paths_;
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800134 std::vector<std::string> permitted_paths_;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700135 soinfo::soinfo_list_t soinfo_list_;
136
137 DISALLOW_COPY_AND_ASSIGN(android_namespace_t);
138};
139
140android_namespace_t g_default_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700141
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700142static std::unordered_map<uintptr_t, soinfo*> g_soinfo_handles_map;
143static android_namespace_t* g_anonymous_namespace = &g_default_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700144
Elliott Hughes0266ae52014-02-10 17:46:57 -0800145static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700147static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
148static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200149
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700150static LinkerTypeAllocator<android_namespace_t> g_namespace_allocator;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700151static LinkerTypeAllocator<LinkedListEntry<android_namespace_t>> g_namespace_list_allocator;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700152
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700153static soinfo* solist;
154static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700155static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800156
Elliott Hughes1728b232014-05-14 10:02:03 -0700157static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700158#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700159 "/system/lib64",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800160 "/vendor/lib64",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700161#else
Elliott Hughes124fae92012-10-31 14:20:03 -0700162 "/system/lib",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800163 "/vendor/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700164#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700165 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700166};
David Bartleybc3a5c22009-06-02 18:27:28 -0700167
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700168static const char* const kAsanDefaultLdPaths[] = {
169#if defined(__LP64__)
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700170 "/data/lib64",
171 "/system/lib64",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800172 "/data/vendor/lib64",
173 "/vendor/lib64",
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700174#else
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700175 "/data/lib",
176 "/system/lib",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800177 "/data/vendor/lib",
178 "/vendor/lib",
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700179#endif
180 nullptr
181};
182
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700183static const ElfW(Versym) kVersymNotNeeded = 0;
184static const ElfW(Versym) kVersymGlobal = 1;
185
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700186static const char* const* g_default_ld_paths;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700187static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800188
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700189static std::vector<soinfo*> g_ld_preloads;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600190
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700191static bool g_public_namespace_initialized;
192static soinfo::soinfo_list_t g_public_namespace;
193
Elliott Hughes1728b232014-05-14 10:02:03 -0700194__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800195
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700196__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700197
Evgenii Stepanov68650822015-06-10 13:38:39 -0700198static std::string dirname(const char *path) {
199 const char* last_slash = strrchr(path, '/');
200 if (last_slash == path) return "/";
201 else if (last_slash == nullptr) return ".";
202 else
203 return std::string(path, last_slash - path);
204}
205
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800206#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700207struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700208 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700209};
210
211static linker_stats_t linker_stats;
212
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800213void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700214 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700215}
216#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800217void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700218}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800219#endif
220
221#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800222uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800223#endif
224
Dima Zavin2e855792009-05-20 18:28:09 -0700225static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700226
Elliott Hughes650be4e2013-03-05 18:47:58 -0800227char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700228 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700229}
230
Elliott Hughes650be4e2013-03-05 18:47:58 -0800231size_t linker_get_error_buffer_size() {
232 return sizeof(__linker_dl_err_buf);
233}
234
Elliott Hughesbedfe382012-08-14 14:07:59 -0700235static void notify_gdb_of_load(soinfo* info) {
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800236 if (info->is_linker() || info->is_main_executable()) {
237 // gdb already knows about the linker and the main executable.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700238 return;
239 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800241 link_map* map = &(info->link_map_head);
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000242
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800243 map->l_addr = info->load_bias;
244 // link_map l_name field is not const.
245 map->l_name = const_cast<char*>(info->get_realpath());
246 map->l_ld = info->dynamic;
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000247
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800248 CHECK(map->l_name != nullptr);
249 CHECK(map->l_name[0] != '\0');
250
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800251 notify_gdb_of_load(map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700252}
253
Elliott Hughesbedfe382012-08-14 14:07:59 -0700254static void notify_gdb_of_unload(soinfo* info) {
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800255 notify_gdb_of_unload(&(info->link_map_head));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800256}
257
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700258bool android_namespace_t::is_accessible(const std::string& file) {
259 if (!is_isolated_) {
260 return true;
261 }
262
263 for (const auto& dir : ld_library_paths_) {
264 if (file_is_in_dir(file, dir)) {
265 return true;
266 }
267 }
268
269 for (const auto& dir : default_library_paths_) {
270 if (file_is_in_dir(file, dir)) {
271 return true;
272 }
273 }
274
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800275 for (const auto& dir : permitted_paths_) {
276 if (file_is_under_dir(file, dir)) {
277 return true;
278 }
279 }
280
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700281 return false;
282}
283
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700284LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
285 return g_soinfo_links_allocator.alloc();
286}
287
288void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
289 g_soinfo_links_allocator.free(entry);
290}
291
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700292LinkedListEntry<android_namespace_t>* NamespaceListAllocator::alloc() {
293 return g_namespace_list_allocator.alloc();
294}
295
296void NamespaceListAllocator::free(LinkedListEntry<android_namespace_t>* entry) {
297 g_namespace_list_allocator.free(entry);
298}
299
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700300static soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
301 struct stat* file_stat, off64_t file_offset,
302 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700303 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200304 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700305 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200306 }
307
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700308 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
309 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700310
Magnus Malmbornba98d922012-09-12 13:00:55 +0200311 sonext->next = si;
312 sonext = si;
313
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700314 si->generate_handle();
315 ns->add_soinfo(si);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700316
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700317 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200318 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800319}
320
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800321static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700322 if (si == nullptr) {
323 return;
324 }
325
326 if (si->base != 0 && si->size != 0) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800327 if (!si->is_mapped_by_caller()) {
328 munmap(reinterpret_cast<void*>(si->base), si->size);
329 } else {
330 // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE
331 mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE,
332 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
333 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700334 }
335
336 soinfo *prev = nullptr, *trav;
337
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700338 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700339
340 for (trav = solist; trav != nullptr; trav = trav->next) {
341 if (trav == si) {
342 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700343 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700344 prev = trav;
345 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800346
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700347 if (trav == nullptr) {
348 // si was not in solist
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700349 DL_ERR("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700350 return;
351 }
Elliott Hughes46882792012-08-03 16:49:39 -0700352
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700353 // clear links to/from si
354 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700355
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700356 // prev will never be null, because the first entry in solist is
357 // always the static libdl_info.
358 prev->next = si->next;
359 if (si == sonext) {
360 sonext = prev;
361 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800362
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700363 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700364 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800365}
366
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700367// For every path element this function checks of it exists, and is a directory,
368// and normalizes it:
369// 1. For regular path it converts it to realpath()
370// 2. For path in a zip file it uses realpath on the zipfile
371// normalizes entry name by calling normalize_path function.
372static void resolve_paths(std::vector<std::string>& paths,
373 std::vector<std::string>* resolved_paths) {
374 resolved_paths->clear();
375 for (const auto& path : paths) {
376 char resolved_path[PATH_MAX];
377 const char* original_path = path.c_str();
378 if (realpath(original_path, resolved_path) != nullptr) {
379 struct stat s;
380 if (stat(resolved_path, &s) == 0) {
381 if (S_ISDIR(s.st_mode)) {
382 resolved_paths->push_back(resolved_path);
383 } else {
384 DL_WARN("Warning: \"%s\" is not a directory (excluding from path)", resolved_path);
385 continue;
386 }
387 } else {
388 DL_WARN("Warning: cannot stat file \"%s\": %s", resolved_path, strerror(errno));
389 continue;
390 }
391 } else {
392 std::string zip_path;
393 std::string entry_path;
394
395 std::string normalized_path;
396
397 if (!normalize_path(original_path, &normalized_path)) {
398 DL_WARN("Warning: unable to normalize \"%s\"", original_path);
399 continue;
400 }
401
402 if (parse_zip_path(normalized_path.c_str(), &zip_path, &entry_path)) {
403 if (realpath(zip_path.c_str(), resolved_path) == nullptr) {
404 DL_WARN("Warning: unable to resolve \"%s\": %s", zip_path.c_str(), strerror(errno));
405 continue;
406 }
407
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700408 resolved_paths->push_back(std::string(resolved_path) + kZipFileSeparator + entry_path);
409 }
410 }
411 }
412}
413
414static void split_path(const char* path, const char* delimiters,
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700415 std::vector<std::string>* paths) {
Dmitriy Ivanovfbfba642015-11-16 14:23:37 -0800416 if (path != nullptr && path[0] != 0) {
tony.ys_liub4474402015-07-29 18:00:22 +0800417 *paths = android::base::Split(path, delimiters);
Elliott Hughescade4c32012-12-20 14:42:14 -0800418 }
419}
420
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700421static void parse_path(const char* path, const char* delimiters,
422 std::vector<std::string>* resolved_paths) {
423 std::vector<std::string> paths;
424 split_path(path, delimiters, &paths);
425 resolve_paths(paths, resolved_paths);
426}
427
Elliott Hughescade4c32012-12-20 14:42:14 -0800428static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700429 std::vector<std::string> ld_libary_paths;
430 parse_path(path, ":", &ld_libary_paths);
431 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800432}
433
Evgenii Stepanov68650822015-06-10 13:38:39 -0700434void soinfo::set_dt_runpath(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700435 if (!has_min_version(3)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700436 return;
437 }
438
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700439 std::vector<std::string> runpaths;
440
441 split_path(path, ":", &runpaths);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700442
443 std::string origin = dirname(get_realpath());
444 // FIXME: add $LIB and $PLATFORM.
445 std::pair<std::string, std::string> substs[] = {{"ORIGIN", origin}};
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700446 for (auto&& s : runpaths) {
Evgenii Stepanov68650822015-06-10 13:38:39 -0700447 size_t pos = 0;
448 while (pos < s.size()) {
449 pos = s.find("$", pos);
450 if (pos == std::string::npos) break;
451 for (const auto& subst : substs) {
452 const std::string& token = subst.first;
453 const std::string& replacement = subst.second;
454 if (s.substr(pos + 1, token.size()) == token) {
455 s.replace(pos, token.size() + 1, replacement);
456 // -1 to compensate for the ++pos below.
457 pos += replacement.size() - 1;
458 break;
459 } else if (s.substr(pos + 1, token.size() + 2) == "{" + token + "}") {
460 s.replace(pos, token.size() + 3, replacement);
461 pos += replacement.size() - 1;
462 break;
463 }
464 }
465 // Skip $ in case it did not match any of the known substitutions.
466 ++pos;
467 }
468 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700469
470 resolve_paths(runpaths, &dt_runpath_);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700471}
472
Elliott Hughescade4c32012-12-20 14:42:14 -0800473static void parse_LD_PRELOAD(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700474 g_ld_preload_names.clear();
475 if (path != nullptr) {
476 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
477 g_ld_preload_names = android::base::Split(path, " :");
Dimitry Ivanovd799b2b2016-05-24 14:29:56 -0700478 std::remove_if(g_ld_preload_names.begin(),
479 g_ld_preload_names.end(),
480 [] (const std::string& s) { return s.empty(); });
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700481 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800482}
483
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700484static bool realpath_fd(int fd, std::string* realpath) {
485 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700486 __libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700487 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -0700488 PRINT("readlink('%s') failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700489 return false;
490 }
491
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700492 *realpath = &buf[0];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700493 return true;
494}
495
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700496#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700497
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700498// For a given PC, find the .so that it belongs to.
499// Returns the base address of the .ARM.exidx section
500// for that .so, and the number of 8-byte entries
501// in that section (via *pcount).
502//
503// Intended to be called by libc's __gnu_Unwind_Find_exidx().
504//
505// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800506_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800507 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800508
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700509 for (soinfo* si = solist; si != 0; si = si->next) {
510 if ((addr >= si->base) && (addr < (si->base + si->size))) {
511 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800512 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800513 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700514 }
515 *pcount = 0;
516 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800517}
Elliott Hughes46882792012-08-03 16:49:39 -0700518
Christopher Ferris24053a42013-08-19 17:45:09 -0700519#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700520
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700521// Here, we only have to provide a callback to iterate across all the
522// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700523int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700524 int rv = 0;
525 for (soinfo* si = solist; si != nullptr; si = si->next) {
526 dl_phdr_info dl_info;
527 dl_info.dlpi_addr = si->link_map_head.l_addr;
528 dl_info.dlpi_name = si->link_map_head.l_name;
529 dl_info.dlpi_phdr = si->phdr;
530 dl_info.dlpi_phnum = si->phnum;
531 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
532 if (rv != 0) {
533 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800534 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700535 }
536 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800537}
Elliott Hughes46882792012-08-03 16:49:39 -0700538
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700539const ElfW(Versym)* soinfo::get_versym(size_t n) const {
540 if (has_min_version(2) && versym_ != nullptr) {
541 return versym_ + n;
542 }
543
544 return nullptr;
545}
546
547ElfW(Addr) soinfo::get_verneed_ptr() const {
548 if (has_min_version(2)) {
549 return verneed_ptr_;
550 }
551
552 return 0;
553}
554
555size_t soinfo::get_verneed_cnt() const {
556 if (has_min_version(2)) {
557 return verneed_cnt_;
558 }
559
560 return 0;
561}
562
563ElfW(Addr) soinfo::get_verdef_ptr() const {
564 if (has_min_version(2)) {
565 return verdef_ptr_;
566 }
567
568 return 0;
569}
570
571size_t soinfo::get_verdef_cnt() const {
572 if (has_min_version(2)) {
573 return verdef_cnt_;
574 }
575
576 return 0;
577}
578
579template<typename F>
580static bool for_each_verdef(const soinfo* si, F functor) {
581 if (!si->has_min_version(2)) {
582 return true;
583 }
584
585 uintptr_t verdef_ptr = si->get_verdef_ptr();
586 if (verdef_ptr == 0) {
587 return true;
588 }
589
590 size_t offset = 0;
591
592 size_t verdef_cnt = si->get_verdef_cnt();
593 for (size_t i = 0; i<verdef_cnt; ++i) {
594 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
595 size_t verdaux_offset = offset + verdef->vd_aux;
596 offset += verdef->vd_next;
597
598 if (verdef->vd_version != 1) {
Dmitriy Ivanov3d7bea12015-04-20 17:40:39 -0700599 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700600 i, verdef->vd_version, si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700601 return false;
602 }
603
604 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
605 // "this is the version of the file itself. It must not be used for
606 // matching a symbol. It can be used to match references."
607 //
608 // http://www.akkadia.org/drepper/symbol-versioning
609 continue;
610 }
611
612 if (verdef->vd_cnt == 0) {
613 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
614 return false;
615 }
616
617 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
618
619 if (functor(i, verdef, verdaux) == true) {
620 break;
621 }
622 }
623
624 return true;
625}
626
627bool soinfo::find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const {
628 if (vi == nullptr) {
629 *versym = kVersymNotNeeded;
630 return true;
631 }
632
633 *versym = kVersymGlobal;
634
635 return for_each_verdef(this,
636 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
637 if (verdef->vd_hash == vi->elf_hash &&
638 strcmp(vi->name, get_string(verdaux->vda_name)) == 0) {
639 *versym = verdef->vd_ndx;
640 return true;
641 }
642
643 return false;
644 }
645 );
646}
647
648bool soinfo::find_symbol_by_name(SymbolName& symbol_name,
649 const version_info* vi,
650 const ElfW(Sym)** symbol) const {
651 uint32_t symbol_index;
652 bool success =
653 is_gnu_hash() ?
654 gnu_lookup(symbol_name, vi, &symbol_index) :
655 elf_lookup(symbol_name, vi, &symbol_index);
656
657 if (success) {
658 *symbol = symbol_index == 0 ? nullptr : symtab_ + symbol_index;
659 }
660
661 return success;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800662}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800663
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800664static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
665 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
666 ELF_ST_BIND(s->st_info) == STB_WEAK) {
667 return s->st_shndx != SHN_UNDEF;
668 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
669 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700670 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800671 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800672
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800673 return false;
674}
675
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700676static const ElfW(Versym) kVersymHiddenBit = 0x8000;
677
678static inline bool is_versym_hidden(const ElfW(Versym)* versym) {
679 // the symbol is hidden if bit 15 of versym is set.
680 return versym != nullptr && (*versym & kVersymHiddenBit) != 0;
681}
682
683static inline bool check_symbol_version(const ElfW(Versym) verneed,
684 const ElfW(Versym)* verdef) {
685 return verneed == kVersymNotNeeded ||
686 verdef == nullptr ||
687 verneed == (*verdef & ~kVersymHiddenBit);
688}
689
690bool soinfo::gnu_lookup(SymbolName& symbol_name,
691 const version_info* vi,
692 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800693 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800694 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800695
696 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800697 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
698 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800699
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700700 *symbol_index = 0;
701
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700702 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700703 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700704
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800705 // test against bloom filter
706 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700707 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
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 Ivanov2a815362015-04-09 13:42:33 -0700710 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800711 }
712
713 // bloom test says "probably yes"...
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700714 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800715
716 if (n == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700717 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700718 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700719
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700720 return true;
721 }
722
723 // lookup versym for the version definition in this library
724 // note the difference between "version is not requested" (vi == nullptr)
725 // and "version not found". In the first case verneed is kVersymNotNeeded
726 // which implies that the default version can be accepted; the second case results in
727 // verneed = 1 (kVersymGlobal) and implies that we should ignore versioned symbols
728 // for this library and consider only *global* ones.
729 ElfW(Versym) verneed = 0;
730 if (!find_verdef_version_index(vi, &verneed)) {
731 return false;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800732 }
733
734 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800735 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700736 const ElfW(Versym)* verdef = get_versym(n);
737 // skip hidden versions when verneed == kVersymNotNeeded (0)
738 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
739 continue;
740 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700741 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700742 check_symbol_version(verneed, verdef) &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800743 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
744 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700745 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700746 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(s->st_value),
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700747 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700748 *symbol_index = n;
749 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800750 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700751 } while ((gnu_chain_[n++] & 1) == 0);
752
753 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700754 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800755
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700756 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800757}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800758
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700759bool soinfo::elf_lookup(SymbolName& symbol_name,
760 const version_info* vi,
761 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800762 uint32_t hash = symbol_name.elf_hash();
763
764 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700765 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700766 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800767
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700768 ElfW(Versym) verneed = 0;
769 if (!find_verdef_version_index(vi, &verneed)) {
770 return false;
771 }
772
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800773 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
774 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700775 const ElfW(Versym)* verdef = get_versym(n);
776
777 // skip hidden versions when verneed == 0
778 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
779 continue;
780 }
781
782 if (check_symbol_version(verneed, verdef) &&
783 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700784 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800785 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700786 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700787 reinterpret_cast<void*>(s->st_value),
788 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700789 *symbol_index = n;
790 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800791 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800792 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800793
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700794 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700795 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700796 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700797
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700798 *symbol_index = 0;
799 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800800}
801
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700802soinfo::soinfo(android_namespace_t* ns, const char* realpath,
803 const struct stat* file_stat, off64_t file_offset,
804 int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700805 memset(this, 0, sizeof(*this));
806
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700807 if (realpath != nullptr) {
808 realpath_ = realpath;
809 }
810
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800811 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800812 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700813
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700814 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800815 this->st_dev_ = file_stat->st_dev;
816 this->st_ino_ = file_stat->st_ino;
817 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700818 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700819
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800820 this->rtld_flags_ = rtld_flags;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700821 this->primary_namespace_ = ns;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700822}
823
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700824soinfo::~soinfo() {
825 g_soinfo_handles_map.erase(handle_);
826}
827
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800828static uint32_t calculate_elf_hash(const char* name) {
829 const uint8_t* name_bytes = reinterpret_cast<const uint8_t*>(name);
830 uint32_t h = 0, g;
831
832 while (*name_bytes) {
833 h = (h << 4) + *name_bytes++;
834 g = h & 0xf0000000;
835 h ^= g;
836 h ^= g >> 24;
837 }
838
839 return h;
840}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800841
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800842uint32_t SymbolName::elf_hash() {
843 if (!has_elf_hash_) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800844 elf_hash_ = calculate_elf_hash(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800845 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700846 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800847
848 return elf_hash_;
849}
850
851uint32_t SymbolName::gnu_hash() {
852 if (!has_gnu_hash_) {
853 uint32_t h = 5381;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700854 const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800855 while (*name != 0) {
856 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
857 }
858
859 gnu_hash_ = h;
860 has_gnu_hash_ = true;
861 }
862
863 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800864}
865
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700866bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
867 soinfo** si_found_in, const soinfo::soinfo_list_t& global_group,
868 const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800869 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700870 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700871
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700872 /* "This element's presence in a shared object library alters the dynamic linker's
873 * symbol resolution algorithm for references within the library. Instead of starting
874 * a symbol search with the executable file, the dynamic linker starts from the shared
875 * object itself. If the shared object fails to supply the referenced symbol, the
876 * dynamic linker then searches the executable file and other shared objects as usual."
877 *
878 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
879 *
880 * Note that this is unlikely since static linker avoids generating
881 * relocations for -Bsymbolic linked dynamic executables.
882 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700883 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700884 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700885 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
886 return false;
887 }
888
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700889 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700890 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700891 }
892 }
893
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700894 // 1. Look for it in global_group
895 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700896 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700897 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700898 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700899 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700900 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
901 error = true;
902 return false;
903 }
904
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700905 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700906 *si_found_in = global_si;
907 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700908 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700909
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700910 return true;
911 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700912
913 if (error) {
914 return false;
915 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700916 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700917
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700918 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700919 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700920 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700921 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700922 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700923 // we already did this - skip
924 return true;
925 }
926
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700927 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700928 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700929 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
930 error = true;
931 return false;
932 }
933
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700934 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700935 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700936 return false;
937 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700938
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700939 return true;
940 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700941
942 if (error) {
943 return false;
944 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700945 }
946
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700947 if (s != nullptr) {
948 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
949 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700950 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
951 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700952 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700953 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700954
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700955 *symbol = s;
956 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700957}
958
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800959class ProtectedDataGuard {
960 public:
961 ProtectedDataGuard() {
962 if (ref_count_++ == 0) {
963 protect_data(PROT_READ | PROT_WRITE);
964 }
965 }
966
967 ~ProtectedDataGuard() {
968 if (ref_count_ == 0) { // overflow
969 __libc_fatal("Too many nested calls to dlopen()");
970 }
971
972 if (--ref_count_ == 0) {
973 protect_data(PROT_READ);
974 }
975 }
976 private:
977 void protect_data(int protection) {
978 g_soinfo_allocator.protect_all(protection);
979 g_soinfo_links_allocator.protect_all(protection);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700980 g_namespace_allocator.protect_all(protection);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700981 g_namespace_list_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
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001251 auto& soinfo_list = ns->soinfo_list();
1252 auto 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 {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001258 auto it = soinfo_list.find(caller);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001259 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;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001265 for (auto it = start, end = soinfo_list.end(); it != end; ++it) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001266 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 (?)
Yabin Cui722072d2016-03-21 17:10:12 -07001385 CloseArchive(handle);
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001386 close(fd);
1387 return false;
1388 }
1389
1390 cache_[key] = *handle;
1391 return true;
1392}
1393
1394ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -07001395 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001396 CloseArchive(it.second);
1397 }
1398}
1399
1400static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001401 const char* const input_path,
1402 off64_t* file_offset, std::string* realpath) {
1403 std::string normalized_path;
1404 if (!normalize_path(input_path, &normalized_path)) {
1405 return -1;
1406 }
1407
1408 const char* const path = normalized_path.c_str();
1409 TRACE("Trying zip file open from path '%s' -> normalized '%s'", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +00001410
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001411 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +00001412 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001413 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +00001414 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001415 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +00001416 if (separator == nullptr) {
1417 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -07001418 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001419
1420 char buf[512];
1421 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
1422 PRINT("Warning: ignoring very long library path: %s", path);
1423 return -1;
1424 }
1425
1426 buf[separator - path] = '\0';
1427
1428 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001429 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +00001430 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1431 if (fd == -1) {
1432 return -1;
1433 }
1434
1435 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001436 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001437 // invalid zip-file (?)
1438 close(fd);
1439 return -1;
1440 }
1441
Simon Baldwinaef71952015-01-16 13:22:54 +00001442 ZipEntry entry;
1443
Yusuke Sato56f40fb2015-06-25 14:56:07 -07001444 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001445 // Entry was not found.
1446 close(fd);
1447 return -1;
1448 }
1449
1450 // Check if it is properly stored
1451 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
1452 close(fd);
1453 return -1;
1454 }
1455
1456 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001457
1458 if (realpath_fd(fd, realpath)) {
1459 *realpath += separator;
1460 } else {
1461 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1462 normalized_path.c_str());
1463 *realpath = normalized_path;
1464 }
1465
Simon Baldwinaef71952015-01-16 13:22:54 +00001466 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001467}
1468
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001469static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
1470 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
1471 if (n < 0 || n >= static_cast<int>(buf_size)) {
1472 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1473 return false;
1474 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001475
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001476 return true;
1477}
1478
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001479static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1480 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001481 const std::vector<std::string>& paths,
1482 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001483 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001484 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001485 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001486 continue;
1487 }
1488
1489 int fd = -1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001490 if (strstr(buf, kZipFileSeparator) != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001491 fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath);
Simon Baldwinaef71952015-01-16 13:22:54 +00001492 }
1493
1494 if (fd == -1) {
1495 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1496 if (fd != -1) {
1497 *file_offset = 0;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001498 if (!realpath_fd(fd, realpath)) {
1499 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
1500 *realpath = buf;
1501 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001502 }
1503 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001504
1505 if (fd != -1) {
1506 return fd;
1507 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001508 }
1509
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001510 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001511}
1512
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001513static int open_library(android_namespace_t* ns,
1514 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001515 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001516 off64_t* file_offset, std::string* realpath) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001517 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001518
Elliott Hughes124fae92012-10-31 14:20:03 -07001519 // 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 -07001520 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001521 int fd = -1;
1522
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001523 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001524 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1525 }
1526
1527 if (fd == -1) {
1528 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001529 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001530 *file_offset = 0;
1531 if (!realpath_fd(fd, realpath)) {
1532 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name);
1533 *realpath = name;
1534 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001535 }
1536 }
1537
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001538 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001539 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001540
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001541 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1542 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 -07001543 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001544 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001545 // Check if the library is accessible
1546 if (fd != -1 && !ns->is_accessible(*realpath)) {
1547 fd = -1;
1548 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001549 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001550
Elliott Hughes124fae92012-10-31 14:20:03 -07001551 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001552 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001553 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001554
Elliott Hughes124fae92012-10-31 14:20:03 -07001555 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001556}
1557
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001558static const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
1559#if !defined(__LP64__)
1560 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001561 if (get_application_target_sdk_version() <= 22) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001562 const char* bname = basename(dt_needed);
1563 if (bname != dt_needed) {
1564 DL_WARN("'%s' library has invalid DT_NEEDED entry '%s'", sopath, dt_needed);
1565 }
1566
1567 return bname;
1568 }
1569#endif
1570 return dt_needed;
1571}
1572
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001573template<typename F>
1574static void for_each_dt_needed(const soinfo* si, F action) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001575 for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001576 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001577 action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
Dima Zavin2e855792009-05-20 18:28:09 -07001578 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001579 }
1580}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001581
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001582template<typename F>
1583static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
1584 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1585 if (d->d_tag == DT_NEEDED) {
1586 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1587 }
1588 }
1589}
1590
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001591static bool load_library(android_namespace_t* ns,
1592 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001593 LoadTaskList* load_tasks,
1594 int rtld_flags,
1595 const std::string& realpath) {
1596 off64_t file_offset = task->get_file_offset();
1597 const char* name = task->get_name();
1598 const android_dlextinfo* extinfo = task->get_extinfo();
1599
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001600 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001601 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001602 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001603 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001604 if (file_offset < 0) {
1605 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001606 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001607 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001608
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001609 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001610 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001611 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001612 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001613 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001614 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001615 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1616 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001617 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001618 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001619
1620 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001621 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1622 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001623 auto predicate = [&](soinfo* si) {
1624 return si->get_st_dev() != 0 &&
1625 si->get_st_ino() != 0 &&
1626 si->get_st_dev() == file_stat.st_dev &&
1627 si->get_st_ino() == file_stat.st_ino &&
1628 si->get_file_offset() == file_offset;
1629 };
1630
1631 soinfo* si = ns->soinfo_list().find_if(predicate);
1632
1633 // check public namespace
1634 if (si == nullptr) {
1635 si = g_public_namespace.find_if(predicate);
1636 if (si != nullptr) {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001637 ns->add_soinfo(si);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001638 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001639 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001640
1641 if (si != nullptr) {
1642 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
1643 "will return existing soinfo", name, si->get_realpath());
1644 task->set_soinfo(si);
1645 return true;
1646 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001647 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001648
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001649 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001650 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001651 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001652 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001653
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001654 if (!ns->is_accessible(realpath)) {
1655 // do not load libraries if they are not accessible for the specified namespace.
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001656 const char* needed_or_dlopened_by = task->get_needed_by() == nullptr ?
1657 "(unknown)" :
1658 task->get_needed_by()->get_realpath();
1659
1660 DL_ERR("library \"%s\" needed or dlopened by \"%s\" is not accessible for the namespace \"%s\"",
1661 name, needed_or_dlopened_by, ns->get_name());
1662
1663 PRINT("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the"
1664 " namespace: [name=\"%s\", ld_library_paths=\"%s\", default_library_paths=\"%s\","
1665 " permitted_paths=\"%s\"]",
1666 name, realpath.c_str(),
1667 needed_or_dlopened_by,
1668 ns->get_name(),
1669 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
1670 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
1671 android::base::Join(ns->get_permitted_paths(), ':').c_str());
1672
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001673 return false;
1674 }
1675
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001676 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001677 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001678 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001679 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001680
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001681 task->set_soinfo(si);
1682
1683 // Read the ELF header and some of the segments.
1684 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001685 soinfo_free(si);
1686 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001687 return false;
1688 }
1689
1690 // find and set DT_RUNPATH and dt_soname
1691 // Note that these field values are temporary and are
1692 // going to be overwritten on soinfo::prelink_image
1693 // with values from PT_LOAD segments.
1694 const ElfReader& elf_reader = task->get_elf_reader();
1695 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1696 if (d->d_tag == DT_RUNPATH) {
1697 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1698 }
1699 if (d->d_tag == DT_SONAME) {
1700 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1701 }
1702 }
1703
1704 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
1705 load_tasks->push_back(LoadTask::create(name, si, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001706 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001707
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001708 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001709}
1710
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001711static bool load_library(android_namespace_t* ns,
1712 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001713 ZipArchiveCache* zip_archive_cache,
1714 LoadTaskList* load_tasks,
1715 int rtld_flags) {
1716 const char* name = task->get_name();
1717 soinfo* needed_by = task->get_needed_by();
1718 const android_dlextinfo* extinfo = task->get_extinfo();
1719
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001720 off64_t file_offset;
1721 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001722 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001723 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001724 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1725 file_offset = extinfo->library_fd_offset;
1726 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001727
1728 if (!realpath_fd(extinfo->library_fd, &realpath)) {
1729 PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1730 "Will use given name.", name);
1731 realpath = name;
1732 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001733
1734 task->set_fd(extinfo->library_fd, false);
1735 task->set_file_offset(file_offset);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001736 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001737 }
1738
1739 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001740 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001741 if (fd == -1) {
1742 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001743 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001744 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001745
1746 task->set_fd(fd, true);
1747 task->set_file_offset(file_offset);
1748
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001749 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001750}
1751
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001752// Returns true if library was found and false in 2 cases
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001753// 1. (for default namespace only) The library was found but loaded under different
1754// target_sdk_version (*candidate != nullptr)
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001755// 2. The library was not found by soname (*candidate is nullptr)
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001756static bool find_loaded_library_by_soname(android_namespace_t* ns,
1757 const char* name, soinfo** candidate) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001758 *candidate = nullptr;
1759
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001760 // Ignore filename with path.
1761 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001762 return false;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001763 }
1764
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001765 uint32_t target_sdk_version = get_application_target_sdk_version();
1766
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001767 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001768 const char* soname = si->get_soname();
1769 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001770 // If the library was opened under different target sdk version
1771 // skip this step and try to reopen it. The exceptions are
1772 // "libdl.so" and global group. There is no point in skipping
1773 // them because relocation process is going to use them
1774 // in any case.
1775 bool is_libdl = si == solist;
1776 if (is_libdl || (si->get_dt_flags_1() & DF_1_GLOBAL) != 0 ||
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001777 !si->is_linked() || si->get_target_sdk_version() == target_sdk_version ||
1778 ns != &g_default_namespace) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001779 *candidate = si;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001780 return false;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001781 } else if (*candidate == nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001782 // for the different sdk version in the default namespace
1783 // remember the first library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001784 *candidate = si;
1785 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001786 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001787
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001788 return true;
1789 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001790}
1791
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001792static bool find_library_internal(android_namespace_t* ns,
1793 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001794 ZipArchiveCache* zip_archive_cache,
1795 LoadTaskList* load_tasks,
1796 int rtld_flags) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001797 soinfo* candidate;
1798
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001799 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001800 task->set_soinfo(candidate);
1801 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001802 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001803
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001804 if (ns != &g_default_namespace) {
1805 // check public namespace
1806 candidate = g_public_namespace.find_if([&](soinfo* si) {
1807 return strcmp(task->get_name(), si->get_soname()) == 0;
1808 });
1809
1810 if (candidate != nullptr) {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001811 ns->add_soinfo(candidate);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001812 task->set_soinfo(candidate);
1813 return true;
1814 }
1815 }
1816
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001817 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001818 // of this fact is done by load_library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001819 TRACE("[ '%s' find_loaded_library_by_soname returned false (*candidate=%s@%p). Trying harder...]",
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001820 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001821
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001822 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001823 return true;
1824 } else {
1825 // In case we were unable to load the library but there
1826 // is a candidate loaded under the same soname but different
1827 // sdk level - return it anyways.
1828 if (candidate != nullptr) {
1829 task->set_soinfo(candidate);
1830 return true;
1831 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001832 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001833
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001834 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001835}
1836
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001837static void soinfo_unload(soinfo* si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001838static void soinfo_unload(soinfo* soinfos[], size_t count);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001839
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001840// TODO: this is slightly unusual way to construct
1841// the global group for relocation. Not every RTLD_GLOBAL
1842// library is included in this group for backwards-compatibility
1843// reasons.
1844//
1845// This group consists of the main executable, LD_PRELOADs
1846// and libraries with the DF_1_GLOBAL flag set.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001847static soinfo::soinfo_list_t make_global_group(android_namespace_t* ns) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001848 soinfo::soinfo_list_t global_group;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001849 ns->soinfo_list().for_each([&](soinfo* si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001850 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1851 global_group.push_back(si);
1852 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001853 });
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001854
1855 return global_group;
1856}
1857
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001858// This function provides a list of libraries to be shared
1859// by the namespace. For the default namespace this is the global
1860// group (see make_global_group). For all others this is a group
1861// of RTLD_GLOBAL libraries (which includes the global group from
1862// the default namespace).
1863static soinfo::soinfo_list_t get_shared_group(android_namespace_t* ns) {
1864 if (ns == &g_default_namespace) {
1865 return make_global_group(ns);
1866 }
1867
1868 soinfo::soinfo_list_t shared_group;
1869 ns->soinfo_list().for_each([&](soinfo* si) {
1870 if ((si->get_rtld_flags() & RTLD_GLOBAL) != 0) {
1871 shared_group.push_back(si);
1872 }
1873 });
1874
1875 return shared_group;
1876}
1877
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001878static void shuffle(std::vector<LoadTask*>* v) {
1879 for (size_t i = 0, size = v->size(); i < size; ++i) {
1880 size_t n = size - i;
1881 size_t r = arc4random_uniform(n);
1882 std::swap((*v)[n-1], (*v)[r]);
1883 }
1884}
1885
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001886// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1887// not their transitive dependencies) as children of the start_with library.
1888// This is false when find_libraries is called for dlopen(), when newly loaded
1889// libraries must form a disjoint tree.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001890static bool find_libraries(android_namespace_t* ns,
1891 soinfo* start_with,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001892 const char* const library_names[],
1893 size_t library_names_count, soinfo* soinfos[],
1894 std::vector<soinfo*>* ld_preloads,
1895 size_t ld_preloads_count, int rtld_flags,
1896 const android_dlextinfo* extinfo,
1897 bool add_as_children) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001898 // Step 0: prepare.
1899 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001900 std::unordered_map<const soinfo*, ElfReader> readers_map;
1901
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001902 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001903 const char* name = library_names[i];
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001904 load_tasks.push_back(LoadTask::create(name, start_with, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001905 }
1906
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001907 // Construct global_group.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001908 soinfo::soinfo_list_t global_group = make_global_group(ns);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001909
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001910 // If soinfos array is null allocate one on stack.
1911 // The array is needed in case of failure; for example
1912 // when library_names[] = {libone.so, libtwo.so} and libone.so
1913 // is loaded correctly but libtwo.so failed for some reason.
1914 // In this case libone.so should be unloaded on return.
1915 // See also implementation of failure_guard below.
1916
1917 if (soinfos == nullptr) {
1918 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1919 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1920 memset(soinfos, 0, soinfos_size);
1921 }
1922
1923 // list of libraries to link - see step 2.
1924 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001925
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001926 auto scope_guard = make_scope_guard([&]() {
1927 for (LoadTask* t : load_tasks) {
1928 LoadTask::deleter(t);
1929 }
1930 });
1931
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001932 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001933 // Housekeeping
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001934 soinfo_unload(soinfos, soinfos_count);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001935 });
1936
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001937 ZipArchiveCache zip_archive_cache;
1938
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001939 // Step 1: expand the list of load_tasks to include
1940 // all DT_NEEDED libraries (do not load them just yet)
1941 for (size_t i = 0; i<load_tasks.size(); ++i) {
1942 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07001943 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001944
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001945 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001946 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001947
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001948 if(!find_library_internal(ns, task, &zip_archive_cache, &load_tasks, rtld_flags)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001949 return false;
1950 }
1951
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001952 soinfo* si = task->get_soinfo();
1953
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001954 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001955 needed_by->add_child(si);
1956 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001957
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001958 if (si->is_linked()) {
1959 si->increment_ref_count();
1960 }
1961
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001962 // When ld_preloads is not null, the first
1963 // ld_preloads_count libs are in fact ld_preloads.
1964 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001965 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001966 }
1967
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001968 if (soinfos_count < library_names_count) {
1969 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001970 }
1971 }
1972
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001973 // Step 2: Load libraries in random order (see b/24047022)
1974 LoadTaskList load_list;
1975 for (auto&& task : load_tasks) {
1976 soinfo* si = task->get_soinfo();
1977 auto pred = [&](const LoadTask* t) {
1978 return t->get_soinfo() == si;
1979 };
1980
1981 if (!si->is_linked() &&
1982 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
1983 load_list.push_back(task);
1984 }
1985 }
1986 shuffle(&load_list);
1987
1988 for (auto&& task : load_list) {
1989 if (!task->load()) {
1990 return false;
1991 }
1992 }
1993
1994 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
1995 for (auto&& task : load_tasks) {
1996 soinfo* si = task->get_soinfo();
1997 if (!si->is_linked() && !si->prelink_image()) {
1998 return false;
1999 }
2000 }
2001
2002 // Step 4: Add LD_PRELOADed libraries to the global group for
2003 // future runs. There is no need to explicitly add them to
2004 // the global group for this run because they are going to
2005 // appear in the local group in the correct order.
2006 if (ld_preloads != nullptr) {
2007 for (auto&& si : *ld_preloads) {
2008 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2009 }
2010 }
2011
2012
2013 // Step 5: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002014 soinfo::soinfo_list_t local_group;
2015 walk_dependencies_tree(
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002016 (start_with != nullptr && add_as_children) ? &start_with : soinfos,
2017 (start_with != nullptr && add_as_children) ? 1 : soinfos_count,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002018 [&] (soinfo* si) {
2019 local_group.push_back(si);
2020 return true;
2021 });
2022
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002023 // We need to increment ref_count in case
2024 // the root of the local group was not linked.
2025 bool was_local_group_root_linked = local_group.front()->is_linked();
2026
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002027 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002028 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002029 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002030 return false;
2031 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002032 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002033
2034 return true;
2035 });
2036
2037 if (linked) {
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002038 local_group.for_each([](soinfo* si) {
2039 if (!si->is_linked()) {
2040 si->set_linked();
2041 }
2042 });
2043
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002044 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002045 }
2046
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002047 if (!was_local_group_root_linked) {
2048 local_group.front()->increment_ref_count();
2049 }
2050
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002051 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002052}
2053
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002054static soinfo* find_library(android_namespace_t* ns,
2055 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002056 const android_dlextinfo* extinfo,
2057 soinfo* needed_by) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002058 soinfo* si;
2059
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002060 if (name == nullptr) {
2061 si = somain;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002062 } else if (!find_libraries(ns, needed_by, &name, 1, &si, nullptr, 0, rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002063 extinfo, /* add_as_children */ false)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002064 return nullptr;
2065 }
2066
Elliott Hughesd23736e2012-11-01 15:16:56 -07002067 return si;
2068}
Elliott Hughesbedfe382012-08-14 14:07:59 -07002069
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002070static void soinfo_unload(soinfo* root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002071 if (root->is_linked()) {
2072 root = root->get_local_group_root();
2073 }
2074
2075 if (!root->can_unload()) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002076 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002077 return;
2078 }
2079
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002080 soinfo_unload(&root, 1);
2081}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002082
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002083static void soinfo_unload(soinfo* soinfos[], size_t count) {
2084 // Note that the library can be loaded but not linked;
2085 // in which case there is no root but we still need
2086 // to walk the tree and unload soinfos involved.
2087 //
2088 // This happens on unsuccessful dlopen, when one of
2089 // the DT_NEEDED libraries could not be linked/found.
2090 if (count == 0) {
2091 return;
2092 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002093
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002094 soinfo::soinfo_list_t unload_list;
2095 for (size_t i = 0; i < count; ++i) {
2096 soinfo* si = soinfos[i];
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002097
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002098 if (si->can_unload()) {
2099 size_t ref_count = si->is_linked() ? si->decrement_ref_count() : 0;
2100 if (ref_count == 0) {
2101 unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002102 } else {
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002103 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
2104 si->get_realpath(), ref_count);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002105 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002106 } else {
2107 TRACE("not unloading '%s' - the binary is flagged with NODELETE", si->get_realpath());
2108 return;
2109 }
2110 }
2111
2112 // This is used to identify soinfos outside of the load-group
2113 // note that we cannot have > 1 in the array and have any of them
2114 // linked. This is why we can safely use the first one.
2115 soinfo* root = soinfos[0];
2116
2117 soinfo::soinfo_list_t local_unload_list;
2118 soinfo::soinfo_list_t external_unload_list;
2119 soinfo* si = nullptr;
2120
2121 while ((si = unload_list.pop_front()) != nullptr) {
2122 if (local_unload_list.contains(si)) {
2123 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002124 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002125
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002126 local_unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002127
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002128 if (si->has_min_version(0)) {
2129 soinfo* child = nullptr;
2130 while ((child = si->get_children().pop_front()) != nullptr) {
2131 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
2132 child->get_realpath(), child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002133
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002134 if (local_unload_list.contains(child)) {
2135 continue;
2136 } else if (child->is_linked() && child->get_local_group_root() != root) {
2137 external_unload_list.push_back(child);
2138 } else {
2139 unload_list.push_front(child);
2140 }
2141 }
2142 } else {
2143#if !defined(__work_around_b_24465209__)
2144 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
2145#else
2146 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
2147 for_each_dt_needed(si, [&] (const char* library_name) {
2148 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
2149 si->get_realpath(), library_name);
2150
2151 soinfo* needed = find_library(si->get_primary_namespace(),
2152 library_name, RTLD_NOLOAD, nullptr, nullptr);
2153
2154 if (needed != nullptr) {
2155 // Not found: for example if symlink was deleted between dlopen and dlclose
2156 // Since we cannot really handle errors at this point - print and continue.
2157 PRINT("warning: couldn't find %s needed by %s on unload.",
2158 library_name, si->get_realpath());
2159 return;
2160 } else if (local_unload_list.contains(needed)) {
2161 // already visited
2162 return;
2163 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
2164 // external group
2165 external_unload_list.push_back(needed);
2166 } else {
2167 // local group
2168 unload_list.push_front(needed);
2169 }
2170 });
2171#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002172 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002173 }
2174
2175 local_unload_list.for_each([](soinfo* si) {
2176 si->call_destructors();
2177 });
2178
2179 while ((si = local_unload_list.pop_front()) != nullptr) {
2180 notify_gdb_of_unload(si);
2181 soinfo_free(si);
2182 }
2183
2184 while ((si = external_unload_list.pop_front()) != nullptr) {
2185 soinfo_unload(si);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08002186 }
2187}
2188
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002189static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
2190 if (sym_ver == nullptr) {
2191 return sym_name;
2192 }
2193
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002194 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002195}
2196
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002197static android_namespace_t* get_caller_namespace(soinfo* caller) {
2198 return caller != nullptr ? caller->get_primary_namespace() : g_anonymous_namespace;
2199}
2200
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002201void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002202 // Use basic string manipulation calls to avoid snprintf.
2203 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
2204 // When debug malloc is enabled, this call returns 0. This in turn causes
2205 // snprintf to do nothing, which causes libraries to fail to load.
2206 // See b/17302493 for further details.
2207 // Once the above bug is fixed, this code can be modified to use
2208 // snprintf again.
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002209 size_t required_len = 0;
2210 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2211 required_len += strlen(g_default_ld_paths[i]) + 1;
2212 }
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002213 if (buffer_size < required_len) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002214 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
2215 "buffer len %zu, required len %zu", buffer_size, required_len);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002216 }
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002217 char* end = buffer;
2218 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2219 if (i > 0) *end++ = ':';
2220 end = stpcpy(end, g_default_ld_paths[i]);
2221 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002222}
2223
Elliott Hughescade4c32012-12-20 14:42:14 -08002224void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08002225 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08002226}
2227
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002228void* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002229 void* caller_addr) {
2230 soinfo* const caller = find_containing_library(caller_addr);
2231
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002232 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08002233 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002234 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08002235 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002236
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002237 android_namespace_t* ns = get_caller_namespace(caller);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002238
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002239 if (extinfo != nullptr) {
2240 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
2241 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
2242 return nullptr;
2243 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002244
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002245 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07002246 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002247 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
2248 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002249 return nullptr;
2250 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002251
2252 if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 &&
2253 (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) {
2254 DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not "
2255 "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT");
2256 return nullptr;
2257 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002258
2259 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
2260 if (extinfo->library_namespace == nullptr) {
2261 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
2262 return nullptr;
2263 }
2264 ns = extinfo->library_namespace;
2265 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00002266 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002267
2268 ProtectedDataGuard guard;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002269 soinfo* si = find_library(ns, name, flags, extinfo, caller);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002270 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002271 si->call_constructors();
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002272 return si->to_handle();
Elliott Hughesd23736e2012-11-01 15:16:56 -07002273 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002274
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002275 return nullptr;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002276}
2277
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002278int do_dladdr(const void* addr, Dl_info* info) {
2279 // Determine if this address can be found in any library currently mapped.
2280 soinfo* si = find_containing_library(addr);
2281 if (si == nullptr) {
2282 return 0;
2283 }
2284
2285 memset(info, 0, sizeof(Dl_info));
2286
2287 info->dli_fname = si->get_realpath();
2288 // Address at which the shared object is loaded.
2289 info->dli_fbase = reinterpret_cast<void*>(si->base);
2290
2291 // Determine if any symbol in the library contains the specified address.
2292 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
2293 if (sym != nullptr) {
2294 info->dli_sname = si->get_string(sym->st_name);
2295 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
2296 }
2297
2298 return 1;
2299}
2300
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002301static soinfo* soinfo_from_handle(void* handle) {
2302 if ((reinterpret_cast<uintptr_t>(handle) & 1) != 0) {
2303 auto it = g_soinfo_handles_map.find(reinterpret_cast<uintptr_t>(handle));
2304 if (it == g_soinfo_handles_map.end()) {
2305 return nullptr;
2306 } else {
2307 return it->second;
2308 }
2309 }
2310
2311 return static_cast<soinfo*>(handle);
2312}
2313
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002314bool do_dlsym(void* handle, const char* sym_name, const char* sym_ver,
2315 void* caller_addr, void** symbol) {
2316#if !defined(__LP64__)
2317 if (handle == nullptr) {
2318 DL_ERR("dlsym failed: library handle is null");
2319 return false;
2320 }
2321#endif
2322
2323 if (sym_name == nullptr) {
2324 DL_ERR("dlsym failed: symbol name is null");
2325 return false;
2326 }
2327
2328 soinfo* found = nullptr;
2329 const ElfW(Sym)* sym = nullptr;
2330 soinfo* caller = find_containing_library(caller_addr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002331 android_namespace_t* ns = get_caller_namespace(caller);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002332
2333 version_info vi_instance;
2334 version_info* vi = nullptr;
2335
2336 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002337 vi_instance.name = sym_ver;
2338 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002339 vi = &vi_instance;
2340 }
2341
2342 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2343 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2344 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002345 soinfo* si = soinfo_from_handle(handle);
2346 if (si == nullptr) {
2347 DL_ERR("dlsym failed: invalid handle: %p", handle);
2348 return false;
2349 }
2350 sym = dlsym_handle_lookup(si, &found, sym_name, vi);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002351 }
2352
2353 if (sym != nullptr) {
2354 uint32_t bind = ELF_ST_BIND(sym->st_info);
2355
2356 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2357 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
2358 return true;
2359 }
2360
2361 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2362 return false;
2363 }
2364
2365 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2366 return false;
2367}
2368
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002369int do_dlclose(void* handle) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002370 ProtectedDataGuard guard;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002371 soinfo* si = soinfo_from_handle(handle);
2372 if (si == nullptr) {
2373 DL_ERR("invalid handle: %p", handle);
2374 return -1;
2375 }
2376
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002377 soinfo_unload(si);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002378 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002379}
2380
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002381bool init_namespaces(const char* public_ns_sonames, const char* anon_ns_library_path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002382 if (g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002383 DL_ERR("public namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002384 return false;
2385 }
2386
Dimitry Ivanov54807612016-04-21 14:57:38 -07002387 if (public_ns_sonames == nullptr || public_ns_sonames[0] == '\0') {
2388 DL_ERR("error initializing public namespace: the list of public libraries is empty.");
2389 return false;
2390 }
2391
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002392 std::vector<std::string> sonames = android::base::Split(public_ns_sonames, ":");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002393
2394 ProtectedDataGuard guard;
2395
2396 auto failure_guard = make_scope_guard([&]() {
2397 g_public_namespace.clear();
2398 });
2399
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002400 for (const auto& soname : sonames) {
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -08002401 soinfo* candidate = nullptr;
2402
2403 find_loaded_library_by_soname(&g_default_namespace, soname.c_str(), &candidate);
2404
2405 if (candidate == nullptr) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002406 DL_ERR("error initializing public namespace: \"%s\" was not found"
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002407 " in the default namespace", soname.c_str());
2408 return false;
2409 }
2410
2411 candidate->set_nodelete();
2412 g_public_namespace.push_back(candidate);
2413 }
2414
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002415 g_public_namespace_initialized = true;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002416
2417 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002418 // When the caller is nullptr - create_namespace will take global group
2419 // from the anonymous namespace, which is fine because anonymous namespace
2420 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002421 android_namespace_t* anon_ns =
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002422 create_namespace(nullptr, "(anonymous)", nullptr, anon_ns_library_path,
Dimitry Ivanov52408632016-05-23 10:31:11 -07002423 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, &g_default_namespace);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002424
2425 if (anon_ns == nullptr) {
2426 g_public_namespace_initialized = false;
2427 return false;
2428 }
2429 g_anonymous_namespace = anon_ns;
2430 failure_guard.disable();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002431 return true;
2432}
2433
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002434android_namespace_t* create_namespace(const void* caller_addr,
2435 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002436 const char* ld_library_path,
2437 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002438 uint64_t type,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002439 const char* permitted_when_isolated_path,
2440 android_namespace_t* parent_namespace) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002441 if (!g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002442 DL_ERR("cannot create namespace: public namespace is not initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002443 return nullptr;
2444 }
2445
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002446 if (parent_namespace == nullptr) {
Dimitry Ivanov52408632016-05-23 10:31:11 -07002447 // if parent_namespace is nullptr -> set it to the caller namespace
2448 soinfo* caller_soinfo = find_containing_library(caller_addr);
2449
2450 parent_namespace = caller_soinfo != nullptr ?
2451 caller_soinfo->get_primary_namespace() :
2452 g_anonymous_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002453 }
2454
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002455 ProtectedDataGuard guard;
2456 std::vector<std::string> ld_library_paths;
2457 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002458 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002459
2460 parse_path(ld_library_path, ":", &ld_library_paths);
2461 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002462 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002463
2464 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2465 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002466 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002467 ns->set_ld_library_paths(std::move(ld_library_paths));
2468 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002469 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002470
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002471 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002472 // If shared - clone the parent namespace
2473 ns->add_soinfos(parent_namespace->soinfo_list());
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002474 } else {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002475 // If not shared - copy only the shared group
2476 ns->add_soinfos(get_shared_group(parent_namespace));
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002477 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002478
2479 return ns;
2480}
2481
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002482static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
2483 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2484 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2485 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002486 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2487 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002488
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002489 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002490}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002491
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002492const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2493 if (source_symver < 2 ||
2494 source_symver >= version_infos.size() ||
2495 version_infos[source_symver].name == nullptr) {
2496 return nullptr;
2497 }
2498
2499 return &version_infos[source_symver];
2500}
2501
2502void VersionTracker::add_version_info(size_t source_index,
2503 ElfW(Word) elf_hash,
2504 const char* ver_name,
2505 const soinfo* target_si) {
2506 if (source_index >= version_infos.size()) {
2507 version_infos.resize(source_index+1);
2508 }
2509
2510 version_infos[source_index].elf_hash = elf_hash;
2511 version_infos[source_index].name = ver_name;
2512 version_infos[source_index].target_si = target_si;
2513}
2514
2515bool VersionTracker::init_verneed(const soinfo* si_from) {
2516 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2517
2518 if (verneed_ptr == 0) {
2519 return true;
2520 }
2521
2522 size_t verneed_cnt = si_from->get_verneed_cnt();
2523
2524 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2525 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2526 size_t vernaux_offset = offset + verneed->vn_aux;
2527 offset += verneed->vn_next;
2528
2529 if (verneed->vn_version != 1) {
2530 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2531 return false;
2532 }
2533
2534 const char* target_soname = si_from->get_string(verneed->vn_file);
2535 // find it in dependencies
2536 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002537 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002538 });
2539
2540 if (target_si == nullptr) {
2541 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002542 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002543 return false;
2544 }
2545
2546 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2547 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2548 vernaux_offset += vernaux->vna_next;
2549
2550 const ElfW(Word) elf_hash = vernaux->vna_hash;
2551 const char* ver_name = si_from->get_string(vernaux->vna_name);
2552 ElfW(Half) source_index = vernaux->vna_other;
2553
2554 add_version_info(source_index, elf_hash, ver_name, target_si);
2555 }
2556 }
2557
2558 return true;
2559}
2560
2561bool VersionTracker::init_verdef(const soinfo* si_from) {
2562 return for_each_verdef(si_from,
2563 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2564 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2565 si_from->get_string(verdaux->vda_name), si_from);
2566 return false;
2567 }
2568 );
2569}
2570
2571bool VersionTracker::init(const soinfo* si_from) {
2572 if (!si_from->has_min_version(2)) {
2573 return true;
2574 }
2575
2576 return init_verneed(si_from) && init_verdef(si_from);
2577}
2578
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002579bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2580 const char* sym_name, const version_info** vi) {
2581 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2582 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2583
2584 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2585 *vi = version_tracker.get_version_info(sym_ver);
2586
2587 if (*vi == nullptr) {
2588 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002589 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002590 return false;
2591 }
2592 } else {
2593 // there is no version info
2594 *vi = nullptr;
2595 }
2596
2597 return true;
2598}
2599
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002600#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002601#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002602static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2603 return rela->r_addend;
2604}
2605#else
2606static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002607 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2608 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002609 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2610 }
2611 return 0;
2612}
2613#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002614
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002615template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002616bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2617 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002618 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2619 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002620 if (rel == nullptr) {
2621 return false;
2622 }
2623
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002624 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2625 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2626
2627 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002628 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002629 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002630 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002631
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002632 DEBUG("Processing '%s' relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002633 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002634 continue;
2635 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002636
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002637 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002638 soinfo* lsi = nullptr;
2639
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002640 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002641 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002642 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002643
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002644 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2645 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002646 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002647
2648 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2649 return false;
2650 }
2651
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002652 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002653 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002654 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002655 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002656 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002657 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002658 }
2659
2660 /* IHI0044C AAELF 4.5.1.1:
2661
2662 Libraries are not searched to resolve weak references.
2663 It is not an error for a weak reference to remain unsatisfied.
2664
2665 During linking, the value of an undefined weak reference is:
2666 - Zero if the relocation type is absolute
2667 - The address of the place if the relocation is pc-relative
2668 - The address of nominal base address if the relocation
2669 type is base-relative.
2670 */
2671
2672 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002673 case R_GENERIC_JUMP_SLOT:
2674 case R_GENERIC_GLOB_DAT:
2675 case R_GENERIC_RELATIVE:
2676 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002677#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002678 case R_AARCH64_ABS64:
2679 case R_AARCH64_ABS32:
2680 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002681#elif defined(__x86_64__)
2682 case R_X86_64_32:
2683 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002684#elif defined(__arm__)
2685 case R_ARM_ABS32:
2686#elif defined(__i386__)
2687 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002688#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002689 /*
2690 * The sym_addr was initialized to be zero above, or the relocation
2691 * code below does not care about value of sym_addr.
2692 * No need to do anything.
2693 */
2694 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002695#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002696 case R_X86_64_PC32:
2697 sym_addr = reloc;
2698 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002699#elif defined(__i386__)
2700 case R_386_PC32:
2701 sym_addr = reloc;
2702 break;
2703#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002704 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002705 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002706 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002707 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002708 } else { // We got a definition.
2709#if !defined(__LP64__)
2710 // When relocating dso with text_relocation .text segment is
2711 // not executable. We need to restore elf flags before resolving
2712 // STT_GNU_IFUNC symbol.
2713 bool protect_segments = has_text_relocations &&
2714 lsi == this &&
2715 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2716 if (protect_segments) {
2717 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2718 DL_ERR("can't protect segments for \"%s\": %s",
2719 get_realpath(), strerror(errno));
2720 return false;
2721 }
2722 }
2723#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002724 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002725#if !defined(__LP64__)
2726 if (protect_segments) {
2727 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2728 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2729 get_realpath(), strerror(errno));
2730 return false;
2731 }
2732 }
2733#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002734 }
2735 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002736 }
2737
2738 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002739 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002740 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002741 MARK(rel->r_offset);
2742 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2743 reinterpret_cast<void*>(reloc),
2744 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2745
2746 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002747 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002748 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002749 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002750 MARK(rel->r_offset);
2751 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2752 reinterpret_cast<void*>(reloc),
2753 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2754 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002755 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002756 case R_GENERIC_RELATIVE:
2757 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002758 MARK(rel->r_offset);
2759 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2760 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002761 reinterpret_cast<void*>(load_bias + addend));
2762 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002763 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002764 case R_GENERIC_IRELATIVE:
2765 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002766 MARK(rel->r_offset);
2767 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2768 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002769 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002770 {
2771#if !defined(__LP64__)
2772 // When relocating dso with text_relocation .text segment is
2773 // not executable. We need to restore elf flags for this
2774 // particular call.
2775 if (has_text_relocations) {
2776 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2777 DL_ERR("can't protect segments for \"%s\": %s",
2778 get_realpath(), strerror(errno));
2779 return false;
2780 }
2781 }
2782#endif
2783 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2784#if !defined(__LP64__)
2785 // Unprotect it afterwards...
2786 if (has_text_relocations) {
2787 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2788 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2789 get_realpath(), strerror(errno));
2790 return false;
2791 }
2792 }
2793#endif
2794 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2795 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002796 break;
2797
2798#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002799 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002800 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002801 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002802 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002803 reloc, sym_addr + addend, sym_name);
2804 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002805 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002806 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002807 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002808 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002809 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002810 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002811 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002812 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2813 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002814 if ((min_value <= (sym_addr + addend)) &&
2815 ((sym_addr + addend) <= max_value)) {
2816 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002817 } else {
2818 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002819 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002820 return false;
2821 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002822 }
2823 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002824 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002825 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002826 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002827 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002828 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002829 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002830 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2831 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002832 if ((min_value <= (sym_addr + addend)) &&
2833 ((sym_addr + addend) <= max_value)) {
2834 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002835 } else {
2836 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002837 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002838 return false;
2839 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002840 }
2841 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002842 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002843 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002844 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002845 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002846 reloc, sym_addr + addend, rel->r_offset, sym_name);
2847 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002848 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002849 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002850 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002851 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002852 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002853 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002854 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002855 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2856 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002857 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2858 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2859 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002860 } else {
2861 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002862 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002863 return false;
2864 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002865 }
2866 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002867 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002868 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002869 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002870 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002871 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002872 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002873 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2874 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002875 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2876 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2877 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002878 } else {
2879 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002880 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002881 return false;
2882 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002883 }
2884 break;
2885
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002886 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002887 /*
2888 * ET_EXEC is not supported so this should not happen.
2889 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002890 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002891 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002892 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002893 * R_AARCH64_COPY may only appear in executable objects where e_type is
2894 * set to ET_EXEC.
2895 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002896 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002897 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002898 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002899 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002900 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002901 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002902 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002903 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002904 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002905 break;
2906#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002907 case R_X86_64_32:
2908 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002909 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002910 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2911 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002912 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002913 break;
2914 case R_X86_64_64:
2915 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002916 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002917 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2918 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002919 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002920 break;
2921 case R_X86_64_PC32:
2922 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002923 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002924 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2925 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2926 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002927 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002928 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002929#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002930 case R_ARM_ABS32:
2931 count_relocation(kRelocAbsolute);
2932 MARK(rel->r_offset);
2933 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2934 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2935 break;
2936 case R_ARM_REL32:
2937 count_relocation(kRelocRelative);
2938 MARK(rel->r_offset);
2939 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2940 reloc, sym_addr, rel->r_offset, sym_name);
2941 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2942 break;
2943 case R_ARM_COPY:
2944 /*
2945 * ET_EXEC is not supported so this should not happen.
2946 *
2947 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2948 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002949 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002950 * R_ARM_COPY may only appear in executable objects where e_type is
2951 * set to ET_EXEC.
2952 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002953 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002954 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002955#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002956 case R_386_32:
2957 count_relocation(kRelocRelative);
2958 MARK(rel->r_offset);
2959 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2960 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2961 break;
2962 case R_386_PC32:
2963 count_relocation(kRelocRelative);
2964 MARK(rel->r_offset);
2965 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2966 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2967 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2968 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002969#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002970 default:
2971 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002972 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002973 }
2974 }
2975 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002976}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002977#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002978
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002979void soinfo::call_array(const char* array_name __unused, linker_function_t* functions,
2980 size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002981 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002982 return;
2983 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02002984
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002985 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, get_realpath());
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002986
2987 int begin = reverse ? (count - 1) : 0;
2988 int end = reverse ? -1 : count;
2989 int step = reverse ? -1 : 1;
2990
2991 for (int i = begin; i != end; i += step) {
2992 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002993 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002994 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02002995
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002996 TRACE("[ Done calling %s for '%s' ]", array_name, get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002997}
2998
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002999void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003000 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07003001 return;
3002 }
3003
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003004 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Elliott Hughesd23736e2012-11-01 15:16:56 -07003005 function();
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003006 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04003007}
3008
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003009void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003010 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
3011 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003012 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07003013}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003014
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003015void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07003016 if (constructors_called) {
3017 return;
3018 }
Jesse Hallf5d16932012-01-30 15:39:57 -08003019
Elliott Hughesd23736e2012-11-01 15:16:56 -07003020 // We set constructors_called before actually calling the constructors, otherwise it doesn't
3021 // protect against recursive constructor calls. One simple example of constructor recursion
3022 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
3023 // 1. The program depends on libc, so libc's constructor is called here.
3024 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
3025 // 3. dlopen() calls the constructors on the newly created
3026 // soinfo for libc_malloc_debug_leak.so.
3027 // 4. The debug .so depends on libc, so CallConstructors is
3028 // called again with the libc soinfo. If it doesn't trigger the early-
3029 // out above, the libc constructor will be called again (recursively!).
3030 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003031
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003032 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003033 // The GNU dynamic linker silently ignores these, but we warn the developer.
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07003034 PRINT("\"%s\": ignoring DT_PREINIT_ARRAY in shared library!", get_realpath());
Elliott Hughesd23736e2012-11-01 15:16:56 -07003035 }
3036
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003037 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003038 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003039 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003040
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003041 TRACE("\"%s\": calling constructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003042
3043 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003044 call_function("DT_INIT", init_func_);
3045 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003046}
David 'Digit' Turner82156792009-05-18 14:37:41 +02003047
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003048void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003049 if (!constructors_called) {
3050 return;
3051 }
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003052 TRACE("\"%s\": calling destructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003053
3054 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003055 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003056
3057 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003058 call_function("DT_FINI", fini_func_);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003059}
3060
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003061void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003062 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003063 child->parents_.push_back(this);
3064 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003065 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003066}
3067
3068void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003069 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003070 return;
3071 }
3072
3073 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003074 children_.for_each([&] (soinfo* child) {
3075 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003076 return parent == this;
3077 });
3078 });
3079
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003080 parents_.for_each([&] (soinfo* parent) {
3081 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003082 return child == this;
3083 });
3084 });
3085
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07003086 // 2. Remove from the primary namespace
3087 primary_namespace_->remove_soinfo(this);
3088 primary_namespace_ = nullptr;
3089
3090 // 3. Remove from secondary namespaces
3091 secondary_namespaces_.for_each([&](android_namespace_t* ns) {
3092 ns->remove_soinfo(this);
3093 });
3094
3095
3096 // 4. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003097 parents_.clear();
3098 children_.clear();
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07003099 secondary_namespaces_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003100}
3101
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003102dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003103 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003104 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003105 }
3106
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003107 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003108};
3109
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003110ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003111 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003112 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003113 }
3114
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003115 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003116}
3117
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003118off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003119 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003120 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003121 }
3122
3123 return 0;
3124}
3125
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003126uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07003127 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003128 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07003129 }
3130
3131 return 0;
3132}
3133
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003134uint32_t soinfo::get_dt_flags_1() const {
3135 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003136 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003137 }
3138
3139 return 0;
3140}
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003141
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003142void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
3143 if (has_min_version(1)) {
3144 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003145 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003146 }
3147
3148 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003149 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003150 }
3151
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003152 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003153 }
3154}
3155
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003156void soinfo::set_nodelete() {
3157 rtld_flags_ |= RTLD_NODELETE;
3158}
3159
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003160const char* soinfo::get_realpath() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003161#if defined(__work_around_b_24465209__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003162 if (has_min_version(2)) {
3163 return realpath_.c_str();
3164 } else {
3165 return old_name_;
3166 }
3167#else
3168 return realpath_.c_str();
3169#endif
3170}
3171
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003172void soinfo::set_soname(const char* soname) {
3173#if defined(__work_around_b_24465209__)
3174 if (has_min_version(2)) {
3175 soname_ = soname;
3176 }
3177 strlcpy(old_name_, soname_, sizeof(old_name_));
3178#else
3179 soname_ = soname;
3180#endif
3181}
3182
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003183const char* soinfo::get_soname() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003184#if defined(__work_around_b_24465209__)
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003185 if (has_min_version(2)) {
3186 return soname_;
3187 } else {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003188 return old_name_;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003189 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003190#else
3191 return soname_;
3192#endif
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003193}
3194
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003195// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003196// 'this->flags' does not have FLAG_NEW_SOINFO set.
3197static soinfo::soinfo_list_t g_empty_list;
3198
3199soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003200 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003201 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003202 }
3203
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003204 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003205}
3206
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003207const soinfo::soinfo_list_t& soinfo::get_children() const {
3208 if (has_min_version(0)) {
3209 return children_;
3210 }
3211
3212 return g_empty_list;
3213}
3214
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003215soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003216 if (has_min_version(0)) {
3217 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003218 }
3219
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003220 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003221}
3222
Evgenii Stepanov68650822015-06-10 13:38:39 -07003223static std::vector<std::string> g_empty_runpath;
3224
3225const std::vector<std::string>& soinfo::get_dt_runpath() const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003226 if (has_min_version(3)) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003227 return dt_runpath_;
3228 }
3229
3230 return g_empty_runpath;
3231}
3232
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07003233android_namespace_t* soinfo::get_primary_namespace() {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003234 if (has_min_version(3)) {
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07003235 return primary_namespace_;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003236 }
3237
3238 return &g_default_namespace;
3239}
3240
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07003241void soinfo::add_secondary_namespace(android_namespace_t* secondary_ns) {
3242 CHECK(has_min_version(3));
3243 secondary_namespaces_.push_back(secondary_ns);
3244}
3245
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003246ElfW(Addr) soinfo::resolve_symbol_address(const ElfW(Sym)* s) const {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003247 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
3248 return call_ifunc_resolver(s->st_value + load_bias);
3249 }
3250
3251 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
3252}
3253
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003254const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003255 if (has_min_version(1) && (index >= strtab_size_)) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003256 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003257 get_realpath(), strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003258 }
3259
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003260 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003261}
3262
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003263bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003264 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003265}
3266
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003267bool soinfo::can_unload() const {
Dimitry Ivanovdd906d72016-04-13 11:46:59 -07003268 return !is_linked() || ((get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003269}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003270
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003271bool soinfo::is_linked() const {
3272 return (flags_ & FLAG_LINKED) != 0;
3273}
3274
3275bool soinfo::is_main_executable() const {
3276 return (flags_ & FLAG_EXE) != 0;
3277}
3278
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -08003279bool soinfo::is_linker() const {
3280 return (flags_ & FLAG_LINKER) != 0;
3281}
3282
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003283void soinfo::set_linked() {
3284 flags_ |= FLAG_LINKED;
3285}
3286
3287void soinfo::set_linker_flag() {
3288 flags_ |= FLAG_LINKER;
3289}
3290
3291void soinfo::set_main_executable() {
3292 flags_ |= FLAG_EXE;
3293}
3294
3295void soinfo::increment_ref_count() {
3296 local_group_root_->ref_count_++;
3297}
3298
3299size_t soinfo::decrement_ref_count() {
3300 return --local_group_root_->ref_count_;
3301}
3302
3303soinfo* soinfo::get_local_group_root() const {
3304 return local_group_root_;
3305}
3306
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -08003307
3308void soinfo::set_mapped_by_caller(bool mapped_by_caller) {
3309 if (mapped_by_caller) {
3310 flags_ |= FLAG_MAPPED_BY_CALLER;
3311 } else {
3312 flags_ &= ~FLAG_MAPPED_BY_CALLER;
3313 }
3314}
3315
3316bool soinfo::is_mapped_by_caller() const {
3317 return (flags_ & FLAG_MAPPED_BY_CALLER) != 0;
3318}
3319
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003320// This function returns api-level at the time of
3321// dlopen/load. Note that libraries opened by system
3322// will always have 'current' api level.
3323uint32_t soinfo::get_target_sdk_version() const {
3324 if (!has_min_version(2)) {
3325 return __ANDROID_API__;
3326 }
3327
3328 return local_group_root_->target_sdk_version_;
3329}
3330
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07003331uintptr_t soinfo::get_handle() const {
3332 CHECK(has_min_version(3));
3333 CHECK(handle_ != 0);
3334 return handle_;
3335}
3336
3337void* soinfo::to_handle() {
3338 if (get_application_target_sdk_version() <= 23 || !has_min_version(3)) {
3339 return this;
3340 }
3341
3342 return reinterpret_cast<void*>(get_handle());
3343}
3344
3345void soinfo::generate_handle() {
3346 CHECK(has_min_version(3));
3347 CHECK(handle_ == 0); // Make sure this is the first call
3348
3349 // Make sure the handle is unique and does not collide
3350 // with special values which are RTLD_DEFAULT and RTLD_NEXT.
3351 do {
3352 arc4random_buf(&handle_, sizeof(handle_));
3353 // the least significant bit for the handle is always 1
3354 // making it easy to test the type of handle passed to
3355 // dl* functions.
3356 handle_ = handle_ | 1;
3357 } while (handle_ == reinterpret_cast<uintptr_t>(RTLD_DEFAULT) ||
3358 handle_ == reinterpret_cast<uintptr_t>(RTLD_NEXT) ||
3359 g_soinfo_handles_map.find(handle_) != g_soinfo_handles_map.end());
3360
3361 g_soinfo_handles_map[handle_] = this;
3362}
3363
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003364bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08003365 /* Extract dynamic section */
3366 ElfW(Word) dynamic_flags = 0;
3367 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07003368
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003369 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003370 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003371 if (!relocating_linker) {
Elliott Hughes116b5692016-01-04 17:45:36 -08003372 INFO("[ Linking '%s' ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003373 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003374 }
3375
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003376 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003377 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003378 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003379 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003380 return false;
3381 } else {
3382 if (!relocating_linker) {
3383 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003384 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003385 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003386
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003387#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003388 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
3389 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003390#endif
3391
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003392 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003393 // Note that: "Except for the DT_NULL element at the end of the array,
3394 // and the relative order of DT_NEEDED elements, entries may appear in any order."
3395 //
3396 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003397 uint32_t needed_count = 0;
3398 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
3399 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
3400 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3401 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003402 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003403 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003404 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003405
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003406 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003407 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
3408 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
3409 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
3410 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003411 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003412
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003413 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003414 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003415 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003416 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
3417 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003418
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003419 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003420 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003421 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003422 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
3423 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003424
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003425 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003426 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003427 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003428 return false;
3429 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003430 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003431
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003432 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003433 break;
3434
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003435 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003436 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003437 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003438
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003439 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003440 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003441 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003442
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003443 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003444 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003445 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003446
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003447 case DT_SYMENT:
3448 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003449 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
3450 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003451 return false;
3452 }
3453 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003454
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003455 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003456#if defined(USE_RELA)
3457 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003458 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003459 return false;
3460 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003461#else
3462 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003463 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003464 return false;
3465 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003466#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003467 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003468
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003469 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003470#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003471 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003472#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003473 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003474#endif
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_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003478#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003479 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003480#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003481 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003482#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003483 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003484
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003485 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003486#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003487 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003488 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003489#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003490 // Ignore for other platforms... (because RTLD_LAZY is not supported)
3491 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003492
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003493 case DT_DEBUG:
3494 // Set the DT_DEBUG entry to the address of _r_debug for GDB
3495 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08003496// FIXME: not working currently for N64
3497// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003498// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08003499// read-only, but the DYNAMIC header claims it is writable.
3500#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003501 if ((dynamic_flags & PF_W) != 0) {
3502 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
3503 }
Chris Dearman99186652014-02-06 20:36:51 -08003504#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08003505 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003506#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003507 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003508 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003509 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003510
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003511 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003512 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003513 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003514
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003515 case DT_ANDROID_RELA:
3516 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3517 break;
3518
3519 case DT_ANDROID_RELASZ:
3520 android_relocs_size_ = d->d_un.d_val;
3521 break;
3522
3523 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003524 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003525 return false;
3526
3527 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003528 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003529 return false;
3530
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003531 case DT_RELAENT:
3532 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003533 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003534 return false;
3535 }
3536 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003537
3538 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003539 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003540 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003541
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003542 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003543 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003544 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003545
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003546 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003547 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003548 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003549
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003550#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003551 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003552 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003553 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003554
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003555 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003556 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003557 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003558
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003559 case DT_RELENT:
3560 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003561 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003562 return false;
3563 }
3564 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003565
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003566 case DT_ANDROID_REL:
3567 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3568 break;
3569
3570 case DT_ANDROID_RELSZ:
3571 android_relocs_size_ = d->d_un.d_val;
3572 break;
3573
3574 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003575 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003576 return false;
3577
3578 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003579 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003580 return false;
3581
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003582 // "Indicates that all RELATIVE relocations have been concatenated together,
3583 // and specifies the RELATIVE relocation count."
3584 //
3585 // TODO: Spec also mentions that this can be used to optimize relocation process;
3586 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003587 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003588 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003589
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003590 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003591 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003592 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003593
3594 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003595 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003596 return false;
3597
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003598#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003599 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003600 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003601 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003602 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003603
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003604 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003605 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003606 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003607 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003608
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003609 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003610 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003611 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003612 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003613
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003614 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003615 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003616 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003617
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003618 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003619 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003620 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003621 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003622
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003623 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003624 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003625 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003626
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003627 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003628 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003629 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003630 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003631
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003632 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003633 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003634 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003635
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003636 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003637#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003638 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003639 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003640#else
3641 has_text_relocations = true;
3642 break;
3643#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003644
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003645 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003646 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003647 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003648
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003649 case DT_NEEDED:
3650 ++needed_count;
3651 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003652
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003653 case DT_FLAGS:
3654 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003655#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003656 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003657 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003658#else
3659 has_text_relocations = true;
3660#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003661 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003662 if (d->d_un.d_val & DF_SYMBOLIC) {
3663 has_DT_SYMBOLIC = true;
3664 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003665 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003666
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003667 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003668 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003669
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003670 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -07003671 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 -07003672 }
3673 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003674#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003675 case DT_MIPS_RLD_MAP:
3676 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3677 {
3678 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3679 *dp = &_r_debug;
3680 }
3681 break;
Lazar Trsic83b44a92016-04-06 13:39:17 +02003682 case DT_MIPS_RLD_MAP_REL:
3683 // Set the DT_MIPS_RLD_MAP_REL entry to the address of _r_debug for GDB.
Raghu Gandham68815722014-12-18 19:12:19 -08003684 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003685 r_debug** dp = reinterpret_cast<r_debug**>(
3686 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003687 *dp = &_r_debug;
3688 }
3689 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003690
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003691 case DT_MIPS_RLD_VERSION:
3692 case DT_MIPS_FLAGS:
3693 case DT_MIPS_BASE_ADDRESS:
3694 case DT_MIPS_UNREFEXTNO:
3695 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003696
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003697 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003698 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003699 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003700
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003701 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003702 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003703 break;
3704
3705 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003706 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003707 break;
3708#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003709 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3710 case DT_BIND_NOW:
3711 break;
3712
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003713 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003714 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3715 break;
3716
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003717 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003718 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3719 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003720 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003721 verdef_cnt_ = d->d_un.d_val;
3722 break;
3723
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003724 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003725 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3726 break;
3727
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003728 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003729 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003730 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003731
Evgenii Stepanov68650822015-06-10 13:38:39 -07003732 case DT_RUNPATH:
3733 // this is parsed after we have strtab initialized (see below).
3734 break;
3735
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003736 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003737 if (!relocating_linker) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003738 DL_WARN("%s: unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003739 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3740 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003741 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003742 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003743 }
3744
Duane Sandbc425c72015-06-01 16:29:14 -07003745#if defined(__mips__) && !defined(__LP64__)
3746 if (!mips_check_and_adjust_fp_modes()) {
3747 return false;
3748 }
3749#endif
3750
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003751 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003752 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003753
3754 // Sanity checks.
3755 if (relocating_linker && needed_count != 0) {
3756 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3757 return false;
3758 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003759 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003760 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003761 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003762 return false;
3763 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003764 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003765 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003766 return false;
3767 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003768 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003769 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003770 return false;
3771 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003772
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003773 // second pass - parse entries relying on strtab
3774 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003775 switch (d->d_tag) {
3776 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003777 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003778 break;
3779 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003780 set_dt_runpath(get_string(d->d_un.d_val));
3781 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003782 }
3783 }
3784
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003785 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003786 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003787 // because they can't find dt_needed library by soname.
3788 // This workaround should keep them working. (applies only
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003789 // for apps targeting sdk version <=22). Make an exception for
3790 // the main executable and linker; they do not need to have dt_soname
3791 if (soname_ == nullptr && this != somain && (flags_ & FLAG_LINKER) == 0 &&
3792 get_application_target_sdk_version() <= 22) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003793 soname_ = basename(realpath_.c_str());
3794 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3795 get_realpath(), soname_);
3796 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003797 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003798}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003799
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003800bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3801 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003802
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003803 local_group_root_ = local_group.front();
3804 if (local_group_root_ == nullptr) {
3805 local_group_root_ = this;
3806 }
3807
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003808 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3809 target_sdk_version_ = get_application_target_sdk_version();
3810 }
3811
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003812 VersionTracker version_tracker;
3813
3814 if (!version_tracker.init(this)) {
3815 return false;
3816 }
3817
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003818#if !defined(__LP64__)
3819 if (has_text_relocations) {
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003820 // Fail if app is targeting sdk version > 22
Dmitriy Ivanov80687862015-10-09 13:58:46 -07003821 if (get_application_target_sdk_version() > 22) {
Dmitriy Ivanovfae39d22015-10-13 11:07:56 -07003822 PRINT("%s: has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003823 DL_ERR("%s: has text relocations", get_realpath());
3824 return false;
3825 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003826 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003827 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003828 DL_WARN("%s has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003829 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003830 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3831 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003832 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003833 return false;
3834 }
3835 }
3836#endif
3837
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003838 if (android_relocs_ != nullptr) {
3839 // check signature
3840 if (android_relocs_size_ > 3 &&
3841 android_relocs_[0] == 'A' &&
3842 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003843 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003844 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003845 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003846
3847 bool relocated = false;
3848 const uint8_t* packed_relocs = android_relocs_ + 4;
3849 const size_t packed_relocs_size = android_relocs_size_ - 4;
3850
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003851 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003852 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003853 packed_reloc_iterator<sleb128_decoder>(
3854 sleb128_decoder(packed_relocs, packed_relocs_size)),
3855 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003856
3857 if (!relocated) {
3858 return false;
3859 }
3860 } else {
3861 DL_ERR("bad android relocation header.");
3862 return false;
3863 }
3864 }
3865
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003866#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003867 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003868 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003869 if (!relocate(version_tracker,
3870 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003871 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003872 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003873 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003874 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003875 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003876 if (!relocate(version_tracker,
3877 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003878 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003879 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003880 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003881#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003882 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003883 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003884 if (!relocate(version_tracker,
3885 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003886 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003887 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003888 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003889 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003890 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003891 if (!relocate(version_tracker,
3892 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003893 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003894 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003895 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003896#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003897
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003898#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003899 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003900 return false;
3901 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003902#endif
3903
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003904 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003905
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003906#if !defined(__LP64__)
3907 if (has_text_relocations) {
3908 // All relocations are done, we can protect our segments back to read-only.
3909 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3910 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003911 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003912 return false;
3913 }
3914 }
3915#endif
3916
Mingwei Shibe910522015-11-12 07:02:14 +00003917 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
3918 // itself --- it can't make system calls yet, and will have to call protect_relro later.
3919 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003920 return false;
3921 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003922
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003923 /* Handle serializing/sharing the RELRO segment */
3924 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3925 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3926 extinfo->relro_fd) < 0) {
3927 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003928 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003929 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003930 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003931 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3932 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3933 extinfo->relro_fd) < 0) {
3934 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003935 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003936 return false;
3937 }
3938 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003939
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003940 notify_gdb_of_load(this);
3941 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003942}
3943
Mingwei Shibe910522015-11-12 07:02:14 +00003944bool soinfo::protect_relro() {
3945 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3946 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
3947 get_realpath(), strerror(errno));
3948 return false;
3949 }
3950 return true;
3951}
3952
Nick Kralevich468319c2011-11-11 15:53:17 -08003953/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003954 * This function add vdso to internal dso list.
3955 * It helps to stack unwinding through signal handlers.
3956 * Also, it makes bionic more like glibc.
3957 */
Kito Cheng812fd422014-03-25 22:53:56 +08003958static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003959#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08003960 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003961 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08003962 return;
3963 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003964
Dmitriy Ivanovd9b08a02015-11-16 13:17:27 -08003965 soinfo* si = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003966
Elliott Hughes0266ae52014-02-10 17:46:57 -08003967 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
3968 si->phnum = ehdr_vdso->e_phnum;
3969 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
3970 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003971 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003972
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003973 si->prelink_image();
3974 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003975#endif
3976}
3977
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003978/* gdb expects the linker to be in the debug shared object list.
3979 * Without this, gdb has trouble locating the linker's ".text"
3980 * and ".plt" sections. Gdb could also potentially use this to
3981 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
Dimitry Ivanov64001292016-02-17 14:13:06 -08003982 * Note that the linker shouldn't be on the soinfo list.
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003983 */
3984static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dimitry Ivanov8d22dd52016-02-16 13:43:35 -08003985 static link_map linker_link_map_for_gdb;
3986#if defined(__LP64__)
3987 static char kLinkerPath[] = "/system/bin/linker64";
3988#else
3989 static char kLinkerPath[] = "/system/bin/linker";
3990#endif
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003991
Dimitry Ivanov8d22dd52016-02-16 13:43:35 -08003992 linker_link_map_for_gdb.l_addr = linker_base;
3993 linker_link_map_for_gdb.l_name = kLinkerPath;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003994
3995 /*
3996 * Set the dynamic field in the link map otherwise gdb will complain with
3997 * the following:
3998 * warning: .dynamic section for "/system/bin/linker" is not at the
3999 * expected address (wrong library or version mismatch?)
4000 */
4001 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
4002 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
4003 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Dimitry Ivanov8d22dd52016-02-16 13:43:35 -08004004 &linker_link_map_for_gdb.l_ld, nullptr);
4005
4006 insert_link_map_into_debug_map(&linker_link_map_for_gdb);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004007}
4008
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004009static void init_default_namespace() {
4010 g_default_namespace.set_name("(default)");
4011 g_default_namespace.set_isolated(false);
4012
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004013 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
4014 somain->load_bias);
4015 const char* bname = basename(interp);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004016 if (bname && (strcmp(bname, "linker_asan") == 0 || strcmp(bname, "linker_asan64") == 0)) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004017 g_default_ld_paths = kAsanDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004018 } else {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004019 g_default_ld_paths = kDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004020 }
4021
4022 std::vector<std::string> ld_default_paths;
4023 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
4024 ld_default_paths.push_back(g_default_ld_paths[i]);
4025 }
4026
4027 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004028};
4029
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07004030extern "C" int __system_properties_init(void);
4031
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -07004032static const char* get_executable_path() {
4033 static std::string executable_path;
4034 if (executable_path.empty()) {
4035 char path[PATH_MAX];
4036 ssize_t path_len = readlink("/proc/self/exe", path, sizeof(path));
4037 if (path_len == -1 || path_len >= static_cast<ssize_t>(sizeof(path))) {
4038 __libc_fatal("readlink('/proc/self/exe') failed: %s", strerror(errno));
4039 }
4040 executable_path = std::string(path, path_len);
4041 }
4042
4043 return executable_path.c_str();
4044}
4045
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004046/*
Nick Kralevich468319c2011-11-11 15:53:17 -08004047 * This code is called after the linker has linked itself and
4048 * fixed it's own GOT. It is safe to make references to externs
4049 * and other non-local data at this point.
4050 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004051static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04004052#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004053 struct timeval t0, t1;
4054 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04004055#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004056
Elliott Hughes1801db32015-06-08 18:04:00 -07004057 // Sanitize the environment.
4058 __libc_init_AT_SECURE(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01004059
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07004060 // Initialize system properties
4061 __system_properties_init(); // may use 'environ'
4062
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004063 debuggerd_init();
4064
4065 // Get a few environment variables.
Elliott Hughes1801db32015-06-08 18:04:00 -07004066 const char* LD_DEBUG = getenv("LD_DEBUG");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004067 if (LD_DEBUG != nullptr) {
4068 g_ld_debug_verbosity = atoi(LD_DEBUG);
4069 }
4070
Elliott Hughes116b5692016-01-04 17:45:36 -08004071#if defined(__LP64__)
4072 INFO("[ Android dynamic linker (64-bit) ]");
4073#else
4074 INFO("[ Android dynamic linker (32-bit) ]");
4075#endif
4076
Elliott Hughes1801db32015-06-08 18:04:00 -07004077 // These should have been sanitized by __libc_init_AT_SECURE, but the test
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004078 // doesn't cost us anything.
4079 const char* ldpath_env = nullptr;
4080 const char* ldpreload_env = nullptr;
Elliott Hughes1801db32015-06-08 18:04:00 -07004081 if (!getauxval(AT_SECURE)) {
4082 ldpath_env = getenv("LD_LIBRARY_PATH");
Elliott Hughes116b5692016-01-04 17:45:36 -08004083 if (ldpath_env != nullptr) {
4084 INFO("[ LD_LIBRARY_PATH set to '%s' ]", ldpath_env);
4085 }
Elliott Hughes1801db32015-06-08 18:04:00 -07004086 ldpreload_env = getenv("LD_PRELOAD");
Elliott Hughes116b5692016-01-04 17:45:36 -08004087 if (ldpreload_env != nullptr) {
4088 INFO("[ LD_PRELOAD set to '%s' ]", ldpreload_env);
4089 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004090 }
4091
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -07004092 const char* executable_path = get_executable_path();
4093 struct stat file_stat;
4094 if (TEMP_FAILURE_RETRY(stat(executable_path, &file_stat)) != 0) {
4095 __libc_fatal("unable to stat file for the executable \"%s\": %s", executable_path, strerror(errno));
4096 }
4097
4098 soinfo* si = soinfo_alloc(&g_default_namespace, executable_path, &file_stat, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004099 if (si == nullptr) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004100 __libc_fatal("Couldn't allocate soinfo: out of memory?");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004101 }
4102
4103 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004104 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004105 link_map* map = &(si->link_map_head);
4106
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -08004107 // Register the main executable and the linker upfront to have
4108 // gdb aware of them before loading the rest of the dependency
4109 // tree.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004110 map->l_addr = 0;
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -07004111 map->l_name = const_cast<char*>(executable_path);
Dimitry Ivanovf3064e42016-02-17 15:25:25 -08004112 insert_link_map_into_debug_map(map);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004113 init_linker_info_for_gdb(linker_base);
4114
4115 // Extract information passed from the kernel.
4116 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
4117 si->phnum = args.getauxval(AT_PHNUM);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004118
4119 /* Compute the value of si->base. We can't rely on the fact that
4120 * the first entry is the PHDR because this will not be true
4121 * for certain executables (e.g. some in the NDK unit test suite)
4122 */
4123 si->base = 0;
4124 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
4125 si->load_bias = 0;
4126 for (size_t i = 0; i < si->phnum; ++i) {
4127 if (si->phdr[i].p_type == PT_PHDR) {
4128 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
4129 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
4130 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07004131 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004132 }
4133 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07004134
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004135 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
4136 if (elf_hdr->e_type != ET_DYN) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004137 __libc_fatal("\"%s\": error: only position independent executables (PIE) are supported.",
4138 args.argv[0]);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004139 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004140
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004141 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
4142 parse_LD_LIBRARY_PATH(ldpath_env);
4143 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01004144
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004145 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004146
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004147 init_default_namespace();
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004148
Dmitriy Ivanov67181252015-01-07 15:48:25 -08004149 if (!si->prelink_image()) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004150 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
Dmitriy Ivanov67181252015-01-07 15:48:25 -08004151 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004152
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07004153 // add somain to global group
4154 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
4155
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004156 // Load ld_preloads and dependencies.
4157 StringLinkedList needed_library_name_list;
4158 size_t needed_libraries_count = 0;
4159 size_t ld_preloads_count = 0;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07004160
4161 for (const auto& ld_preload_name : g_ld_preload_names) {
4162 needed_library_name_list.push_back(ld_preload_name.c_str());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004163 ++needed_libraries_count;
Dmitriy Ivanovf8093a92015-04-28 18:09:53 -07004164 ++ld_preloads_count;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004165 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004166
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004167 for_each_dt_needed(si, [&](const char* name) {
4168 needed_library_name_list.push_back(name);
4169 ++needed_libraries_count;
4170 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004171
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004172 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004173
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004174 memset(needed_library_names, 0, sizeof(needed_library_names));
4175 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004176
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07004177 if (needed_libraries_count > 0 &&
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004178 !find_libraries(&g_default_namespace, si, needed_library_names, needed_libraries_count,
4179 nullptr, &g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07004180 /* add_as_children */ true)) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004181 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004182 } else if (needed_libraries_count == 0) {
4183 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004184 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004185 }
4186 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004187 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004188
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004189 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07004190
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08004191 {
4192 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06004193
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08004194 si->call_pre_init_constructors();
4195
4196 /* After the prelink_image, the si->load_bias is initialized.
4197 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
4198 * We need to update this value for so exe here. So Unwind_Backtrace
4199 * for some arch like x86 could work correctly within so exe.
4200 */
4201 map->l_addr = si->load_bias;
4202 si->call_constructors();
4203 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04004204
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004205#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004206 gettimeofday(&t1, nullptr);
4207 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
4208 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
4209 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004210#endif
4211#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004212 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
4213 linker_stats.count[kRelocAbsolute],
4214 linker_stats.count[kRelocRelative],
4215 linker_stats.count[kRelocCopy],
4216 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004217#endif
4218#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004219 {
4220 unsigned n;
4221 unsigned i;
4222 unsigned count = 0;
4223 for (n = 0; n < 4096; n++) {
4224 if (bitmask[n]) {
4225 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004226#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004227 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004228#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004229 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004230#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004231 if (x & 1) {
4232 count++;
4233 }
4234 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004235 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004236 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004237 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004238 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
4239 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004240#endif
4241
4242#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004243 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004244#endif
4245
Dimitry Ivanove687d062016-02-16 13:25:29 -08004246 ElfW(Addr) entry = args.getauxval(AT_ENTRY);
4247 TRACE("[ Ready to execute '%s' @ %p ]", si->get_realpath(), reinterpret_cast<void*>(entry));
4248 return entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004249}
Nick Kralevich468319c2011-11-11 15:53:17 -08004250
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004251/* Compute the load-bias of an existing executable. This shall only
4252 * be used to compute the load bias of an executable or shared library
4253 * that was loaded by the kernel itself.
4254 *
4255 * Input:
4256 * elf -> address of ELF header, assumed to be at the start of the file.
4257 * Return:
4258 * load bias, i.e. add the value of any p_vaddr in the file to get
4259 * the corresponding address in memory.
4260 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004261static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
4262 ElfW(Addr) offset = elf->e_phoff;
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004263 const ElfW(Phdr)* phdr_table =
4264 reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004265 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004266
Elliott Hughes0266ae52014-02-10 17:46:57 -08004267 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08004268 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08004269 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004270 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08004271 }
4272 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004273}
4274
Mingwei Shibe910522015-11-12 07:02:14 +00004275static void __linker_cannot_link(KernelArgumentBlock& args) {
4276 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", args.argv[0], linker_get_error_buffer());
4277}
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004278
Nick Kralevich468319c2011-11-11 15:53:17 -08004279/*
4280 * This is the entry point for the linker, called from begin.S. This
4281 * method is responsible for fixing the linker's own relocations, and
4282 * then calling __linker_init_post_relocation().
4283 *
4284 * Because this method is called before the linker has fixed it's own
4285 * relocations, any attempt to reference an extern variable, extern
4286 * function, or other GOT reference will generate a segfault.
4287 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004288extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004289 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08004290
Elliott Hughes0266ae52014-02-10 17:46:57 -08004291 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004292 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004293 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08004294 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08004295
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004296 soinfo linker_so(nullptr, nullptr, nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08004297
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004298 // If the linker is not acting as PT_INTERP entry_point is equal to
4299 // _start. Which means that the linker is running as an executable and
4300 // already linked by PT_INTERP.
4301 //
4302 // This happens when user tries to run 'adb shell /system/bin/linker'
4303 // see also https://code.google.com/p/android/issues/detail?id=63174
4304 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004305 __libc_format_fd(STDOUT_FILENO,
4306 "This is %s, the helper program for shared library executables.\n",
4307 args.argv[0]);
4308 exit(0);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004309 }
4310
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004311 linker_so.base = linker_addr;
4312 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
4313 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07004314 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004315 linker_so.phdr = phdr;
4316 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004317 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07004318
Mingwei Shibe910522015-11-12 07:02:14 +00004319 // Prelink the linker so we can access linker globals.
4320 if (!linker_so.prelink_image()) __linker_cannot_link(args);
4321
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07004322 // This might not be obvious... The reasons why we pass g_empty_list
4323 // in place of local_group here are (1) we do not really need it, because
4324 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
4325 // itself without having to look into local_group and (2) allocators
4326 // are not yet initialized, and therefore we cannot use linked_list.push_*
4327 // functions at this point.
Mingwei Shibe910522015-11-12 07:02:14 +00004328 if (!linker_so.link_image(g_empty_list, g_empty_list, nullptr)) __linker_cannot_link(args);
Elliott Hughesd23736e2012-11-01 15:16:56 -07004329
Mingwei Shibe910522015-11-12 07:02:14 +00004330#if defined(__i386__)
4331 // On x86, we can't make system calls before this point.
4332 // We can't move this up because this needs to assign to a global.
4333 // Note that until we call __libc_init_main_thread below we have
4334 // no TLS, so you shouldn't make a system call that can fail, because
4335 // it will SEGV when it tries to set errno.
4336 __libc_init_sysinfo(args);
4337#endif
4338
4339 // Initialize the main thread (including TLS, so system calls really work).
Elliott Hughesd2948632015-07-21 11:57:09 -07004340 __libc_init_main_thread(args);
Dmitriy Ivanov14241402014-08-26 14:16:52 -07004341
Mingwei Shibe910522015-11-12 07:02:14 +00004342 // We didn't protect the linker's RELRO pages in link_image because we
4343 // couldn't make system calls on x86 at that point, but we can now...
4344 if (!linker_so.protect_relro()) __linker_cannot_link(args);
4345
Josh Gao93c0f5e2015-10-06 11:08:13 -07004346 // Initialize the linker's static libc's globals
4347 __libc_init_globals(args);
4348
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004349 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004350 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07004351
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004352 // Initialize static variables. Note that in order to
4353 // get correct libdl_info we need to call constructors
4354 // before get_libdl_info().
4355 solist = get_libdl_info();
4356 sonext = get_libdl_info();
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07004357 g_default_namespace.add_soinfo(get_libdl_info());
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004358
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004359 // We have successfully fixed our own relocations. It's safe to run
4360 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07004361 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08004362 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004363
Elliott Hughes116b5692016-01-04 17:45:36 -08004364 INFO("[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address));
Elliott Hughes611f9562015-01-23 10:43:58 -08004365
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004366 // Return the address that the calling assembly stub should jump to.
4367 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08004368}