blob: 83bc2c1592dea57b7704caf8c779e20eacdd78e2 [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>
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -070039#include <sys/vfs.h>
Elliott Hughes46882792012-08-03 16:49:39 -070040#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070042#include <new>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070043#include <string>
Dmitriy Ivanovb4827502015-09-28 16:38:31 -070044#include <unordered_map>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070045#include <vector>
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070046
Tom Cherryb8ab6182017-04-05 16:20:29 -070047#include <android-base/scopeguard.h>
48
Christopher Ferris7a3681e2017-04-24 17:48:32 -070049#include <async_safe/log.h>
50
Elliott Hughes46882792012-08-03 16:49:39 -070051// Private C library headers.
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"
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070055#include "linker_cfi.h"
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080056#include "linker_config.h"
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -080057#include "linker_gdb_support.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070058#include "linker_globals.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059#include "linker_debug.h"
Dimitry Ivanov769b33f2016-07-21 11:33:40 -070060#include "linker_dlwarning.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070061#include "linker_main.h"
Dimitry Ivanovb943f302016-08-03 16:00:10 -070062#include "linker_namespaces.h"
Dmitriy Ivanov18870d32015-04-22 13:10:04 -070063#include "linker_sleb128.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020064#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080065#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080066#include "linker_reloc_iterators.h"
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070067#include "linker_utils.h"
tony.ys_liub4474402015-07-29 18:00:22 +080068
Elliott Hughes939a7e02015-12-04 15:27:46 -080069#include "android-base/strings.h"
Dimitry Ivanovb996d602016-07-11 18:11:39 -070070#include "android-base/stringprintf.h"
Simon Baldwinaef71952015-01-16 13:22:54 +000071#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072
Elliott Hughes1801db32015-06-08 18:04:00 -070073// Override macros to use C++ style casts.
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080074#undef ELF_ST_TYPE
75#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
76
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -070077static android_namespace_t* g_anonymous_namespace = &g_default_namespace;
Jiyong Park01de74e2017-04-03 23:10:37 +090078static std::unordered_map<std::string, android_namespace_t*> g_exported_namespaces;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070079
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070080static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
81static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020082
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070083static LinkerTypeAllocator<android_namespace_t> g_namespace_allocator;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -070084static LinkerTypeAllocator<LinkedListEntry<android_namespace_t>> g_namespace_list_allocator;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070085
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080086static const char* const kLdConfigFilePath = "/system/etc/ld.config.txt";
87
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070088#if defined(__LP64__)
Dimitry Ivanova1446972017-03-17 00:08:23 +000089static const char* const kSystemLibDir = "/system/lib64";
90static const char* const kVendorLibDir = "/vendor/lib64";
Alin Jerpelea56549062017-09-25 17:47:49 +020091static const char* const kOdmLibDir = "/odm/lib64";
Vishwath Mohan4113def2017-03-29 15:31:34 -070092static const char* const kAsanSystemLibDir = "/data/asan/system/lib64";
93static const char* const kAsanVendorLibDir = "/data/asan/vendor/lib64";
Alin Jerpelea56549062017-09-25 17:47:49 +020094static const char* const kAsanOdmLibDir = "/data/asan/odm/lib64";
Elliott Hughes011bc0b2013-10-08 14:27:10 -070095#else
Dimitry Ivanova1446972017-03-17 00:08:23 +000096static const char* const kSystemLibDir = "/system/lib";
97static const char* const kVendorLibDir = "/vendor/lib";
Alin Jerpelea56549062017-09-25 17:47:49 +020098static const char* const kOdmLibDir = "/odm/lib";
Vishwath Mohan4113def2017-03-29 15:31:34 -070099static const char* const kAsanSystemLibDir = "/data/asan/system/lib";
100static const char* const kAsanVendorLibDir = "/data/asan/vendor/lib";
Alin Jerpelea56549062017-09-25 17:47:49 +0200101static const char* const kAsanOdmLibDir = "/data/asan/odm/lib";
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700102#endif
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700103
Vishwath Mohan4113def2017-03-29 15:31:34 -0700104static const char* const kAsanLibDirPrefix = "/data/asan";
105
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700106static const char* const kDefaultLdPaths[] = {
107 kSystemLibDir,
108 kVendorLibDir,
Alin Jerpelea56549062017-09-25 17:47:49 +0200109 kOdmLibDir,
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700110 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700111};
David Bartleybc3a5c22009-06-02 18:27:28 -0700112
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700113static const char* const kAsanDefaultLdPaths[] = {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700114 kAsanSystemLibDir,
115 kSystemLibDir,
116 kAsanVendorLibDir,
117 kVendorLibDir,
Alin Jerpelea56549062017-09-25 17:47:49 +0200118 kAsanOdmLibDir,
119 kOdmLibDir,
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700120 nullptr
121};
122
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700123// Is ASAN enabled?
124static bool g_is_asan = false;
125
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700126static CFIShadowWriter g_cfi_shadow;
127
128CFIShadowWriter* get_cfi_shadow() {
129 return &g_cfi_shadow;
130}
131
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700132static bool is_system_library(const std::string& realpath) {
133 for (const auto& dir : g_default_namespace.get_default_library_paths()) {
134 if (file_is_in_dir(realpath, dir)) {
135 return true;
136 }
137 }
138 return false;
139}
140
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700141// Checks if the file exists and not a directory.
142static bool file_exists(const char* path) {
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700143 struct stat s;
144
145 if (stat(path, &s) != 0) {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700146 return false;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700147 }
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700148
149 return S_ISREG(s.st_mode);
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700150}
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700151
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800152static std::string resolve_soname(const std::string& name) {
153 // We assume that soname equals to basename here
154
155 // TODO(dimitry): consider having honest absolute-path -> soname resolution
156 // note that since we might end up refusing to load this library because
157 // it is not in shared libs list we need to get the soname without actually loading
158 // the library.
159 //
160 // On the other hand there are several places where we already assume that
161 // soname == basename in particular for any not-loaded library mentioned
162 // in DT_NEEDED list.
163 return basename(name.c_str());
164}
165
166static bool maybe_accessible_via_namespace_links(android_namespace_t* ns, const char* name) {
167 std::string soname = resolve_soname(name);
168 for (auto& ns_link : ns->linked_namespaces()) {
169 if (ns_link.is_accessible(soname.c_str())) {
170 return true;
171 }
172 }
173
174 return false;
175}
176
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700177// TODO(dimitry): The grey-list is a workaround for http://b/26394120 ---
178// gradually remove libraries from this list until it is gone.
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800179static bool is_greylisted(android_namespace_t* ns, const char* name, const soinfo* needed_by) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700180 static const char* const kLibraryGreyList[] = {
181 "libandroid_runtime.so",
182 "libbinder.so",
183 "libcrypto.so",
184 "libcutils.so",
185 "libexpat.so",
186 "libgui.so",
187 "libmedia.so",
188 "libnativehelper.so",
189 "libskia.so",
190 "libssl.so",
191 "libstagefright.so",
192 "libsqlite.so",
193 "libui.so",
194 "libutils.so",
195 "libvorbisidec.so",
196 nullptr
197 };
198
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800199 // If you're targeting N, you don't get the greylist.
Elliott Hughes9e27e582017-03-23 17:42:49 -0700200 if (g_greylist_disabled || get_application_target_sdk_version() >= __ANDROID_API_N__) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700201 return false;
202 }
203
204 // if the library needed by a system library - implicitly assume it
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800205 // is greylisted unless it is in the list of shared libraries for one or
206 // more linked namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700207 if (needed_by != nullptr && is_system_library(needed_by->get_realpath())) {
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800208 return !maybe_accessible_via_namespace_links(ns, name);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700209 }
210
211 // if this is an absolute path - make sure it points to /system/lib(64)
212 if (name[0] == '/' && dirname(name) == kSystemLibDir) {
213 // and reduce the path to basename
214 name = basename(name);
215 }
216
217 for (size_t i = 0; kLibraryGreyList[i] != nullptr; ++i) {
218 if (strcmp(name, kLibraryGreyList[i]) == 0) {
219 return true;
220 }
221 }
222
223 return false;
224}
225// END OF WORKAROUND
226
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700227static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800228
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800229static bool g_anonymous_namespace_initialized;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700230
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800231#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700232struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700233 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700234};
235
236static linker_stats_t linker_stats;
237
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800238void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700239 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700240}
241#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800242void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700243}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244#endif
245
246#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800247uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248#endif
249
Elliott Hughesbedfe382012-08-14 14:07:59 -0700250static void notify_gdb_of_load(soinfo* info) {
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800251 if (info->is_linker() || info->is_main_executable()) {
252 // gdb already knows about the linker and the main executable.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700253 return;
254 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800255
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800256 link_map* map = &(info->link_map_head);
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000257
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800258 map->l_addr = info->load_bias;
259 // link_map l_name field is not const.
260 map->l_name = const_cast<char*>(info->get_realpath());
261 map->l_ld = info->dynamic;
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000262
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800263 CHECK(map->l_name != nullptr);
264 CHECK(map->l_name[0] != '\0');
265
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800266 notify_gdb_of_load(map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700267}
268
Elliott Hughesbedfe382012-08-14 14:07:59 -0700269static void notify_gdb_of_unload(soinfo* info) {
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800270 notify_gdb_of_unload(&(info->link_map_head));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800271}
272
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700273LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
274 return g_soinfo_links_allocator.alloc();
275}
276
277void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
278 g_soinfo_links_allocator.free(entry);
279}
280
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700281LinkedListEntry<android_namespace_t>* NamespaceListAllocator::alloc() {
282 return g_namespace_list_allocator.alloc();
283}
284
285void NamespaceListAllocator::free(LinkedListEntry<android_namespace_t>* entry) {
286 g_namespace_list_allocator.free(entry);
287}
288
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700289soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
290 struct stat* file_stat, off64_t file_offset,
291 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700292 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200293 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700294 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200295 }
296
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700297 TRACE("name %s: allocating soinfo for ns=%p", name, ns);
298
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700299 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
300 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700301
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700302 solist_add_soinfo(si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200303
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700304 si->generate_handle();
305 ns->add_soinfo(si);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700306
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700307 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200308 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800309}
310
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800311static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700312 if (si == nullptr) {
313 return;
314 }
315
316 if (si->base != 0 && si->size != 0) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800317 if (!si->is_mapped_by_caller()) {
318 munmap(reinterpret_cast<void*>(si->base), si->size);
319 } else {
320 // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE
321 mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE,
322 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
323 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700324 }
325
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700326 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700327
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700328 if (!solist_remove_soinfo(si)) {
329 // TODO (dimitry): revisit this - for now preserving the logic
330 // but it does not look right, abort if soinfo is not in the list instead?
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700331 return;
332 }
Elliott Hughes46882792012-08-03 16:49:39 -0700333
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700334 // clear links to/from si
335 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700336
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700337 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700338 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800339}
340
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700341static void parse_path(const char* path, const char* delimiters,
342 std::vector<std::string>* resolved_paths) {
343 std::vector<std::string> paths;
344 split_path(path, delimiters, &paths);
345 resolve_paths(paths, resolved_paths);
346}
347
Elliott Hughescade4c32012-12-20 14:42:14 -0800348static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700349 std::vector<std::string> ld_libary_paths;
350 parse_path(path, ":", &ld_libary_paths);
351 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800352}
353
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700354static bool realpath_fd(int fd, std::string* realpath) {
355 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700356 async_safe_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700357 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700358 PRINT("readlink(\"%s\") failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700359 return false;
360 }
361
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700362 *realpath = &buf[0];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700363 return true;
364}
365
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700366#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700367
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700368// For a given PC, find the .so that it belongs to.
369// Returns the base address of the .ARM.exidx section
370// for that .so, and the number of 8-byte entries
371// in that section (via *pcount).
372//
373// Intended to be called by libc's __gnu_Unwind_Find_exidx().
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800374_Unwind_Ptr do_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700375 for (soinfo* si = solist_get_head(); si != 0; si = si->next) {
Elliott Hughesf2c6ad62017-04-21 10:25:56 -0700376 if ((pc >= si->base) && (pc < (si->base + si->size))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700377 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800378 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800379 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700380 }
381 *pcount = 0;
Elliott Hughesf2c6ad62017-04-21 10:25:56 -0700382 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800383}
Elliott Hughes46882792012-08-03 16:49:39 -0700384
Christopher Ferris24053a42013-08-19 17:45:09 -0700385#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700386
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700387// Here, we only have to provide a callback to iterate across all the
388// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700389int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700390 int rv = 0;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700391 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700392 dl_phdr_info dl_info;
393 dl_info.dlpi_addr = si->link_map_head.l_addr;
394 dl_info.dlpi_name = si->link_map_head.l_name;
395 dl_info.dlpi_phdr = si->phdr;
396 dl_info.dlpi_phnum = si->phnum;
397 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
398 if (rv != 0) {
399 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800400 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700401 }
402 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800403}
Elliott Hughes46882792012-08-03 16:49:39 -0700404
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800405
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700406bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700407 soinfo** si_found_in, const soinfo_list_t& global_group,
408 const soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800409 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700410 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700411
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700412 /* "This element's presence in a shared object library alters the dynamic linker's
413 * symbol resolution algorithm for references within the library. Instead of starting
414 * a symbol search with the executable file, the dynamic linker starts from the shared
415 * object itself. If the shared object fails to supply the referenced symbol, the
416 * dynamic linker then searches the executable file and other shared objects as usual."
417 *
418 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
419 *
420 * Note that this is unlikely since static linker avoids generating
421 * relocations for -Bsymbolic linked dynamic executables.
422 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700423 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700424 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700425 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
426 return false;
427 }
428
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700429 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700430 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700431 }
432 }
433
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700434 // 1. Look for it in global_group
435 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700436 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700437 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700438 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700439 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700440 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
441 error = true;
442 return false;
443 }
444
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700445 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700446 *si_found_in = global_si;
447 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700448 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700449
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700450 return true;
451 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700452
453 if (error) {
454 return false;
455 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700456 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700457
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700458 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700459 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700460 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700461 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700462 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700463 // we already did this - skip
464 return true;
465 }
466
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700467 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700468 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700469 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
470 error = true;
471 return false;
472 }
473
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700474 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700475 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700476 return false;
477 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700478
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700479 return true;
480 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700481
482 if (error) {
483 return false;
484 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700485 }
486
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700487 if (s != nullptr) {
488 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
489 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700490 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
491 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700492 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700493 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700494
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700495 *symbol = s;
496 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700497}
498
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700499ProtectedDataGuard::ProtectedDataGuard() {
500 if (ref_count_++ == 0) {
501 protect_data(PROT_READ | PROT_WRITE);
502 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700503
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700504 if (ref_count_ == 0) { // overflow
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700505 async_safe_fatal("Too many nested calls to dlopen()");
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800506 }
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800507}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800508
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800509ProtectedDataGuard::~ProtectedDataGuard() {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700510 if (--ref_count_ == 0) {
511 protect_data(PROT_READ);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800512 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700513}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800514
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700515void ProtectedDataGuard::protect_data(int protection) {
516 g_soinfo_allocator.protect_all(protection);
517 g_soinfo_links_allocator.protect_all(protection);
518 g_namespace_allocator.protect_all(protection);
519 g_namespace_list_allocator.protect_all(protection);
520}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800521
522size_t ProtectedDataGuard::ref_count_ = 0;
523
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700524// Each size has it's own allocator.
525template<size_t size>
526class SizeBasedAllocator {
527 public:
528 static void* alloc() {
529 return allocator_.alloc();
530 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700531
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700532 static void free(void* ptr) {
533 allocator_.free(ptr);
534 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700535
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700536 private:
537 static LinkerBlockAllocator allocator_;
538};
539
540template<size_t size>
541LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
542
543template<typename T>
544class TypeBasedAllocator {
545 public:
546 static T* alloc() {
547 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
548 }
549
550 static void free(T* ptr) {
551 SizeBasedAllocator<sizeof(T)>::free(ptr);
552 }
553};
554
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700555class LoadTask {
556 public:
557 struct deleter_t {
558 void operator()(LoadTask* t) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700559 t->~LoadTask();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700560 TypeBasedAllocator<LoadTask>::free(t);
561 }
562 };
563
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700564 static deleter_t deleter;
565
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800566 static LoadTask* create(const char* name,
567 soinfo* needed_by,
Jiyong Park34a4acd2017-05-20 01:01:24 +0900568 android_namespace_t* start_from,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700569 std::unordered_map<const soinfo*, ElfReader>* readers_map) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700570 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
Jiyong Park34a4acd2017-05-20 01:01:24 +0900571 return new (ptr) LoadTask(name, needed_by, start_from, readers_map);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700572 }
573
574 const char* get_name() const {
575 return name_;
576 }
577
578 soinfo* get_needed_by() const {
579 return needed_by_;
580 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700581
582 soinfo* get_soinfo() const {
583 return si_;
584 }
585
586 void set_soinfo(soinfo* si) {
587 si_ = si;
588 }
589
590 off64_t get_file_offset() const {
591 return file_offset_;
592 }
593
594 void set_file_offset(off64_t offset) {
595 file_offset_ = offset;
596 }
597
598 int get_fd() const {
599 return fd_;
600 }
601
602 void set_fd(int fd, bool assume_ownership) {
603 fd_ = fd;
604 close_fd_ = assume_ownership;
605 }
606
607 const android_dlextinfo* get_extinfo() const {
608 return extinfo_;
609 }
610
611 void set_extinfo(const android_dlextinfo* extinfo) {
612 extinfo_ = extinfo;
613 }
614
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700615 bool is_dt_needed() const {
616 return is_dt_needed_;
617 }
618
619 void set_dt_needed(bool is_dt_needed) {
620 is_dt_needed_ = is_dt_needed;
621 }
622
Jiyong Park34a4acd2017-05-20 01:01:24 +0900623 // returns the namespace from where we need to start loading this.
624 const android_namespace_t* get_start_from() const {
625 return start_from_;
626 }
627
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700628 const ElfReader& get_elf_reader() const {
629 CHECK(si_ != nullptr);
630 return (*elf_readers_map_)[si_];
631 }
632
633 ElfReader& get_elf_reader() {
634 CHECK(si_ != nullptr);
635 return (*elf_readers_map_)[si_];
636 }
637
638 std::unordered_map<const soinfo*, ElfReader>* get_readers_map() {
639 return elf_readers_map_;
640 }
641
642 bool read(const char* realpath, off64_t file_size) {
643 ElfReader& elf_reader = get_elf_reader();
644 return elf_reader.Read(realpath, fd_, file_offset_, file_size);
645 }
646
647 bool load() {
648 ElfReader& elf_reader = get_elf_reader();
649 if (!elf_reader.Load(extinfo_)) {
650 return false;
651 }
652
653 si_->base = elf_reader.load_start();
654 si_->size = elf_reader.load_size();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800655 si_->set_mapped_by_caller(elf_reader.is_mapped_by_caller());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700656 si_->load_bias = elf_reader.load_bias();
657 si_->phnum = elf_reader.phdr_count();
658 si_->phdr = elf_reader.loaded_phdr();
659
660 return true;
661 }
662
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700663 private:
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800664 LoadTask(const char* name,
665 soinfo* needed_by,
Jiyong Park34a4acd2017-05-20 01:01:24 +0900666 android_namespace_t* start_from,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700667 std::unordered_map<const soinfo*, ElfReader>* readers_map)
668 : name_(name), needed_by_(needed_by), si_(nullptr),
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700669 fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map),
Jiyong Park34a4acd2017-05-20 01:01:24 +0900670 is_dt_needed_(false), start_from_(start_from) {}
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700671
672 ~LoadTask() {
673 if (fd_ != -1 && close_fd_) {
674 close(fd_);
675 }
676 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700677
678 const char* name_;
679 soinfo* needed_by_;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700680 soinfo* si_;
681 const android_dlextinfo* extinfo_;
682 int fd_;
683 bool close_fd_;
684 off64_t file_offset_;
685 std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_;
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700686 // TODO(dimitry): needed by workaround for http://b/26394120 (the grey-list)
687 bool is_dt_needed_;
688 // END OF WORKAROUND
Jiyong Park34a4acd2017-05-20 01:01:24 +0900689 const android_namespace_t* const start_from_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700690
691 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
692};
693
Ningsheng Jiane93be992014-09-16 15:22:10 +0800694LoadTask::deleter_t LoadTask::deleter;
695
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700696template <typename T>
697using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
698
699typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700700typedef linked_list_t<const char> StringLinkedList;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700701typedef std::vector<LoadTask*> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700702
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800703enum walk_action_result_t : uint32_t {
704 kWalkStop = 0,
705 kWalkContinue = 1,
706 kWalkSkip = 2
707};
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700708
Paul Keithd559fee2018-02-15 21:57:33 +0100709#ifdef LD_SHIM_LIBS
Adrian DC8befe0c2017-04-08 22:40:01 +0200710static soinfo* find_library(android_namespace_t* ns,
711 const char* name, int rtld_flags,
712 const android_dlextinfo* extinfo,
713 soinfo* needed_by);
714
715// g_ld_all_shim_libs maintains the references to memory as it used
716// in the soinfo structures and in the g_active_shim_libs list.
717
718typedef std::pair<std::string, std::string> ShimDescriptor;
719static std::vector<ShimDescriptor> g_ld_all_shim_libs;
720
721// g_active_shim_libs are all shim libs that are still eligible
722// to be loaded. We must remove a shim lib from the list before
723// we load the library to avoid recursive loops (load shim libA
724// for libB where libA also links against libB).
725
726static linked_list_t<const ShimDescriptor> g_active_shim_libs;
727
728static void reset_g_active_shim_libs(void) {
729 g_active_shim_libs.clear();
730 for (const auto& pair : g_ld_all_shim_libs) {
731 g_active_shim_libs.push_back(&pair);
732 }
733}
734
735void parse_LD_SHIM_LIBS(const char* path) {
736 g_ld_all_shim_libs.clear();
737 if (path != nullptr) {
738 // We have historically supported ':' as well as ' ' in LD_SHIM_LIBS.
739 for (const auto& pair : android::base::Split(path, " :")) {
740 size_t pos = pair.find('|');
741 if (pos > 0 && pos < pair.length() - 1) {
742 auto desc = std::pair<std::string, std::string>(pair.substr(0, pos), pair.substr(pos + 1));
743 g_ld_all_shim_libs.push_back(desc);
744 }
745 }
746 }
747 reset_g_active_shim_libs();
748}
749
750template<typename F>
751static void for_each_matching_shim(const char *const path, F action) {
752 if (path == nullptr) return;
753 INFO("Finding shim libs for \"%s\"\n", path);
754 std::vector<const ShimDescriptor *> matched;
755
756 g_active_shim_libs.for_each([&](const ShimDescriptor *a_pair) {
757 if (a_pair->first == path) {
758 matched.push_back(a_pair);
759 }
760 });
761
762 g_active_shim_libs.remove_if([&](const ShimDescriptor *a_pair) {
763 return a_pair->first == path;
764 });
765
766 for (const auto& one_pair : matched) {
767 INFO("Injecting shim lib \"%s\" as needed for %s", one_pair->second.c_str(), path);
768 action(one_pair->second.c_str());
769 }
770}
Paul Keithd559fee2018-02-15 21:57:33 +0100771#endif
Adrian DC8befe0c2017-04-08 22:40:01 +0200772
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700773// This function walks down the tree of soinfo dependencies
774// in breadth-first order and
775// * calls action(soinfo* si) for each node, and
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800776// * terminates walk if action returns kWalkStop
777// * skips children of the node if action
778// return kWalkSkip
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700779//
780// walk_dependencies_tree returns false if walk was terminated
781// by the action and true otherwise.
782template<typename F>
783static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700784 SoinfoLinkedList visit_list;
785 SoinfoLinkedList visited;
786
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700787 for (size_t i = 0; i < root_soinfos_size; ++i) {
788 visit_list.push_back(root_soinfos[i]);
789 }
790
791 soinfo* si;
792 while ((si = visit_list.pop_front()) != nullptr) {
793 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700794 continue;
795 }
796
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800797 walk_action_result_t result = action(si);
798
799 if (result == kWalkStop) {
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700800 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700801 }
802
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700803 visited.push_back(si);
804
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800805 if (result != kWalkSkip) {
806 si->get_children().for_each([&](soinfo* child) {
807 visit_list.push_back(child);
808 });
809 }
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700810 }
811
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700812 return true;
813}
814
815
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800816static const ElfW(Sym)* dlsym_handle_lookup(android_namespace_t* ns,
817 soinfo* root,
818 soinfo* skip_until,
819 soinfo** found,
820 SymbolName& symbol_name,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800821 const version_info* vi) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700822 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700823 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700824
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700825 walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) {
826 if (skip_lookup) {
827 skip_lookup = current_soinfo != skip_until;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800828 return kWalkContinue;
829 }
830
831 if (!ns->is_accessible(current_soinfo)) {
832 return kWalkSkip;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700833 }
834
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800835 if (!current_soinfo->find_symbol_by_name(symbol_name, vi, &result)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700836 result = nullptr;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800837 return kWalkStop;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700838 }
839
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700840 if (result != nullptr) {
841 *found = current_soinfo;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800842 return kWalkStop;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700843 }
844
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800845 return kWalkContinue;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700846 });
847
848 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800849}
850
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800851static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
852 const char* name,
853 const version_info* vi,
854 soinfo** found,
855 soinfo* caller,
856 void* handle);
857
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700858// This is used by dlsym(3). It performs symbol lookup only within the
859// specified soinfo object and its dependencies in breadth first order.
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800860static const ElfW(Sym)* dlsym_handle_lookup(soinfo* si,
861 soinfo** found,
862 const char* name,
863 const version_info* vi) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700864 // According to man dlopen(3) and posix docs in the case when si is handle
865 // of the main executable we need to search not only in the executable and its
866 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
867 //
868 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
869 // libraries and they are loaded in breath-first (correct) order we can just execute
870 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700871 if (si == solist_get_somain()) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800872 return dlsym_linear_lookup(&g_default_namespace, name, vi, found, nullptr, RTLD_DEFAULT);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700873 }
874
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700875 SymbolName symbol_name(name);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800876 // note that the namespace is not the namespace associated with caller_addr
877 // we use ns associated with root si intentionally here. Using caller_ns
878 // causes problems when user uses dlopen_ext to open a library in the separate
879 // namespace and then calls dlsym() on the handle.
880 return dlsym_handle_lookup(si->get_primary_namespace(), si, nullptr, found, symbol_name, vi);
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700881}
882
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800883/* This is used by dlsym(3) to performs a global symbol lookup. If the
884 start value is null (for RTLD_DEFAULT), the search starts at the
885 beginning of the global solist. Otherwise the search starts at the
886 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700887 */
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800888static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
889 const char* name,
890 const version_info* vi,
891 soinfo** found,
892 soinfo* caller,
893 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800894 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800895
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700896 auto& soinfo_list = ns->soinfo_list();
897 auto start = soinfo_list.begin();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700898
899 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -0700900 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700901 return nullptr;
902 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700903 auto it = soinfo_list.find(caller);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700904 CHECK (it != soinfo_list.end());
905 start = ++it;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700906 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800907 }
908
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700909 const ElfW(Sym)* s = nullptr;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700910 for (auto it = start, end = soinfo_list.end(); it != end; ++it) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700911 soinfo* si = *it;
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700912 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800913 // if the library is opened by application with target api level < M.
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700914 // See http://b/21565766
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800915 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 &&
916 si->get_target_sdk_version() >= __ANDROID_API_M__) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700917 continue;
918 }
919
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800920 if (!si->find_symbol_by_name(symbol_name, vi, &s)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700921 return nullptr;
922 }
923
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700924 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800925 *found = si;
926 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600927 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800928 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600929
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700930 // If not found - use dlsym_handle_lookup for caller's
931 // local_group unless it is part of the global group in which
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700932 // case we already did it.
933 if (s == nullptr && caller != nullptr &&
934 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800935 soinfo* local_group_root = caller->get_local_group_root();
936
937 return dlsym_handle_lookup(local_group_root->get_primary_namespace(),
938 local_group_root,
939 (handle == RTLD_NEXT) ? caller : nullptr,
940 found,
941 symbol_name,
942 vi);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700943 }
944
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700945 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700946 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
947 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800948 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800949
Elliott Hughescade4c32012-12-20 14:42:14 -0800950 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800951}
952
Kito Chengfa8c05d2013-03-12 14:58:06 +0800953soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800954 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700955 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800956 if (address >= si->base && address - si->base < si->size) {
957 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600958 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800959 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700960 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600961}
962
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700963class ZipArchiveCache {
964 public:
965 ZipArchiveCache() {}
966 ~ZipArchiveCache();
967
968 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
969 private:
970 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
971
972 std::unordered_map<std::string, ZipArchiveHandle> cache_;
973};
974
975bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
976 std::string key(zip_path);
977
978 auto it = cache_.find(key);
979 if (it != cache_.end()) {
980 *handle = it->second;
981 return true;
982 }
983
984 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
985 if (fd == -1) {
986 return false;
987 }
988
989 if (OpenArchiveFd(fd, "", handle) != 0) {
990 // invalid zip-file (?)
Yabin Cui722072d2016-03-21 17:10:12 -0700991 CloseArchive(handle);
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700992 close(fd);
993 return false;
994 }
995
996 cache_[key] = *handle;
997 return true;
998}
999
1000ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -07001001 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001002 CloseArchive(it.second);
1003 }
1004}
1005
1006static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001007 const char* const input_path,
1008 off64_t* file_offset, std::string* realpath) {
1009 std::string normalized_path;
1010 if (!normalize_path(input_path, &normalized_path)) {
1011 return -1;
1012 }
1013
1014 const char* const path = normalized_path.c_str();
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001015 TRACE("Trying zip file open from path \"%s\" -> normalized \"%s\"", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +00001016
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001017 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +00001018 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001019 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +00001020 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001021 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +00001022 if (separator == nullptr) {
1023 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -07001024 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001025
1026 char buf[512];
1027 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
1028 PRINT("Warning: ignoring very long library path: %s", path);
1029 return -1;
1030 }
1031
1032 buf[separator - path] = '\0';
1033
1034 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001035 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +00001036 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1037 if (fd == -1) {
1038 return -1;
1039 }
1040
1041 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001042 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001043 // invalid zip-file (?)
1044 close(fd);
1045 return -1;
1046 }
1047
Simon Baldwinaef71952015-01-16 13:22:54 +00001048 ZipEntry entry;
1049
Yusuke Sato56f40fb2015-06-25 14:56:07 -07001050 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001051 // Entry was not found.
1052 close(fd);
1053 return -1;
1054 }
1055
1056 // Check if it is properly stored
1057 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
1058 close(fd);
1059 return -1;
1060 }
1061
1062 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001063
1064 if (realpath_fd(fd, realpath)) {
1065 *realpath += separator;
1066 } else {
1067 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1068 normalized_path.c_str());
1069 *realpath = normalized_path;
1070 }
1071
Simon Baldwinaef71952015-01-16 13:22:54 +00001072 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001073}
1074
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001075static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001076 int n = async_safe_format_buffer(buf, buf_size, "%s/%s", path, name);
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001077 if (n < 0 || n >= static_cast<int>(buf_size)) {
1078 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1079 return false;
1080 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001081
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001082 return true;
1083}
1084
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001085static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1086 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001087 const std::vector<std::string>& paths,
1088 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001089 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001090 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001091 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001092 continue;
1093 }
1094
1095 int fd = -1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001096 if (strstr(buf, kZipFileSeparator) != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001097 fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath);
Simon Baldwinaef71952015-01-16 13:22:54 +00001098 }
1099
1100 if (fd == -1) {
1101 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1102 if (fd != -1) {
1103 *file_offset = 0;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001104 if (!realpath_fd(fd, realpath)) {
1105 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
1106 *realpath = buf;
1107 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001108 }
1109 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001110
1111 if (fd != -1) {
1112 return fd;
1113 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001114 }
1115
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001116 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001117}
1118
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001119static int open_library(android_namespace_t* ns,
1120 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001121 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001122 off64_t* file_offset, std::string* realpath) {
Jiyong Park34a4acd2017-05-20 01:01:24 +09001123 TRACE("[ opening %s at namespace %s]", name, ns->get_name());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001124
Elliott Hughes124fae92012-10-31 14:20:03 -07001125 // 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 -07001126 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001127 int fd = -1;
1128
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001129 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001130 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1131 }
1132
1133 if (fd == -1) {
1134 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001135 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001136 *file_offset = 0;
1137 if (!realpath_fd(fd, realpath)) {
1138 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name);
1139 *realpath = name;
1140 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001141 }
1142 }
1143
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001144 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001145 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001146
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001147 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1148 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 -07001149 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001150 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001151 // Check if the library is accessible
1152 if (fd != -1 && !ns->is_accessible(*realpath)) {
1153 fd = -1;
1154 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001155 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001156
Elliott Hughes124fae92012-10-31 14:20:03 -07001157 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001158 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001159 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001160
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001161 // TODO(dimitry): workaround for http://b/26394120 (the grey-list)
Jiyong Park37b91af2017-05-05 22:07:05 +09001162 if (fd == -1 && ns->is_greylist_enabled() && is_greylisted(ns, name, needed_by)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001163 // try searching for it on default_namespace default_library_path
1164 fd = open_library_on_paths(zip_archive_cache, name, file_offset,
1165 g_default_namespace.get_default_library_paths(), realpath);
1166 }
1167 // END OF WORKAROUND
1168
Elliott Hughes124fae92012-10-31 14:20:03 -07001169 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001170}
1171
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001172const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001173#if !defined(__LP64__)
1174 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Elliott Hughes5bc78c82016-11-16 11:35:43 -08001175 if (get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001176 const char* bname = basename(dt_needed);
1177 if (bname != dt_needed) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001178 DL_WARN("library \"%s\" has invalid DT_NEEDED entry \"%s\"", sopath, dt_needed);
1179 add_dlwarning(sopath, "invalid DT_NEEDED entry", dt_needed);
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001180 }
1181
1182 return bname;
1183 }
1184#endif
1185 return dt_needed;
1186}
1187
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001188template<typename F>
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001189static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
Paul Keithd559fee2018-02-15 21:57:33 +01001190#ifdef LD_SHIM_LIBS
LuK13377149c792017-09-20 03:05:49 -03001191 for_each_matching_shim(get_executable_path(), action);
Adrian DC8befe0c2017-04-08 22:40:01 +02001192 for_each_matching_shim(elf_reader.name(), action);
Paul Keithd559fee2018-02-15 21:57:33 +01001193#endif
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001194 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1195 if (d->d_tag == DT_NEEDED) {
1196 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1197 }
1198 }
1199}
1200
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001201static bool find_loaded_library_by_inode(android_namespace_t* ns,
1202 const struct stat& file_stat,
1203 off64_t file_offset,
1204 bool search_linked_namespaces,
1205 soinfo** candidate) {
1206
1207 auto predicate = [&](soinfo* si) {
1208 return si->get_st_dev() != 0 &&
1209 si->get_st_ino() != 0 &&
1210 si->get_st_dev() == file_stat.st_dev &&
1211 si->get_st_ino() == file_stat.st_ino &&
1212 si->get_file_offset() == file_offset;
1213 };
1214
1215 *candidate = ns->soinfo_list().find_if(predicate);
1216
1217 if (*candidate == nullptr && search_linked_namespaces) {
1218 for (auto& link : ns->linked_namespaces()) {
1219 android_namespace_t* linked_ns = link.linked_namespace();
1220 soinfo* si = linked_ns->soinfo_list().find_if(predicate);
1221
1222 if (si != nullptr && link.is_accessible(si->get_soname())) {
1223 *candidate = si;
1224 return true;
1225 }
1226 }
1227 }
1228
1229 return *candidate != nullptr;
1230}
1231
Evgenii Stepanov9e77a642017-07-27 14:55:44 -07001232static bool find_loaded_library_by_realpath(android_namespace_t* ns, const char* realpath,
1233 bool search_linked_namespaces, soinfo** candidate) {
1234 auto predicate = [&](soinfo* si) { return strcmp(realpath, si->get_realpath()) == 0; };
1235
1236 *candidate = ns->soinfo_list().find_if(predicate);
1237
1238 if (*candidate == nullptr && search_linked_namespaces) {
1239 for (auto& link : ns->linked_namespaces()) {
1240 android_namespace_t* linked_ns = link.linked_namespace();
1241 soinfo* si = linked_ns->soinfo_list().find_if(predicate);
1242
1243 if (si != nullptr && link.is_accessible(si->get_soname())) {
1244 *candidate = si;
1245 return true;
1246 }
1247 }
1248 }
1249
1250 return *candidate != nullptr;
1251}
1252
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001253static bool load_library(android_namespace_t* ns,
1254 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001255 LoadTaskList* load_tasks,
1256 int rtld_flags,
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001257 const std::string& realpath,
1258 bool search_linked_namespaces) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001259 off64_t file_offset = task->get_file_offset();
1260 const char* name = task->get_name();
1261 const android_dlextinfo* extinfo = task->get_extinfo();
1262
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001263 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001264 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001265 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001266 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001267 if (file_offset < 0) {
1268 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001269 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001270 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001271
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001272 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001273 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001274 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001275 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001276 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001277 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001278 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1279 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001280 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001281 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001282
1283 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001284 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1285 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001286 soinfo* si = nullptr;
1287 if (find_loaded_library_by_inode(ns, file_stat, file_offset, search_linked_namespaces, &si)) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001288 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
1289 "will return existing soinfo", name, si->get_realpath());
1290 task->set_soinfo(si);
1291 return true;
1292 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001293 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001294
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001295 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001296 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001297 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001298 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001299
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -07001300 struct statfs fs_stat;
1301 if (TEMP_FAILURE_RETRY(fstatfs(task->get_fd(), &fs_stat)) != 0) {
1302 DL_ERR("unable to fstatfs file for the library \"%s\": %s", name, strerror(errno));
1303 return false;
1304 }
1305
1306 // do not check accessibility using realpath if fd is located on tmpfs
1307 // this enables use of memfd_create() for apps
1308 if ((fs_stat.f_type != TMPFS_MAGIC) && (!ns->is_accessible(realpath))) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001309 // TODO(dimitry): workaround for http://b/26394120 - the grey-list
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001310
1311 // TODO(dimitry) before O release: add a namespace attribute to have this enabled
1312 // only for classloader-namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001313 const soinfo* needed_by = task->is_dt_needed() ? task->get_needed_by() : nullptr;
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001314 if (is_greylisted(ns, name, needed_by)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001315 // print warning only if needed by non-system library
1316 if (needed_by == nullptr || !is_system_library(needed_by->get_realpath())) {
1317 const soinfo* needed_or_dlopened_by = task->get_needed_by();
1318 const char* sopath = needed_or_dlopened_by == nullptr ? "(unknown)" :
1319 needed_or_dlopened_by->get_realpath();
1320 DL_WARN("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the namespace \"%s\""
1321 " - the access is temporarily granted as a workaround for http://b/26394120, note that the access"
1322 " will be removed in future releases of Android.",
1323 name, realpath.c_str(), sopath, ns->get_name());
1324 add_dlwarning(sopath, "unauthorized access to", name);
1325 }
1326 } else {
1327 // do not load libraries if they are not accessible for the specified namespace.
1328 const char* needed_or_dlopened_by = task->get_needed_by() == nullptr ?
1329 "(unknown)" :
1330 task->get_needed_by()->get_realpath();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001331
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001332 DL_ERR("library \"%s\" needed or dlopened by \"%s\" is not accessible for the namespace \"%s\"",
1333 name, needed_or_dlopened_by, ns->get_name());
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001334
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001335 // do not print this if a library is in the list of shared libraries for linked namespaces
1336 if (!maybe_accessible_via_namespace_links(ns, name)) {
1337 PRINT("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the"
1338 " namespace: [name=\"%s\", ld_library_paths=\"%s\", default_library_paths=\"%s\","
1339 " permitted_paths=\"%s\"]",
1340 name, realpath.c_str(),
1341 needed_or_dlopened_by,
1342 ns->get_name(),
1343 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
1344 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
1345 android::base::Join(ns->get_permitted_paths(), ':').c_str());
1346 }
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001347 return false;
1348 }
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001349 }
1350
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001351 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001352 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001353 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001354 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001355
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001356 task->set_soinfo(si);
1357
1358 // Read the ELF header and some of the segments.
1359 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001360 soinfo_free(si);
1361 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001362 return false;
1363 }
1364
1365 // find and set DT_RUNPATH and dt_soname
1366 // Note that these field values are temporary and are
1367 // going to be overwritten on soinfo::prelink_image
1368 // with values from PT_LOAD segments.
1369 const ElfReader& elf_reader = task->get_elf_reader();
1370 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1371 if (d->d_tag == DT_RUNPATH) {
1372 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1373 }
1374 if (d->d_tag == DT_SONAME) {
1375 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1376 }
1377 }
1378
1379 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
Jiyong Park34a4acd2017-05-20 01:01:24 +09001380 load_tasks->push_back(LoadTask::create(name, si, ns, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001381 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001382
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001383 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001384}
1385
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001386static bool load_library(android_namespace_t* ns,
1387 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001388 ZipArchiveCache* zip_archive_cache,
1389 LoadTaskList* load_tasks,
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001390 int rtld_flags,
1391 bool search_linked_namespaces) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001392 const char* name = task->get_name();
1393 soinfo* needed_by = task->get_needed_by();
1394 const android_dlextinfo* extinfo = task->get_extinfo();
1395
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001396 off64_t file_offset;
1397 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001398 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001399 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001400 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1401 file_offset = extinfo->library_fd_offset;
1402 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001403
1404 if (!realpath_fd(extinfo->library_fd, &realpath)) {
1405 PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1406 "Will use given name.", name);
1407 realpath = name;
1408 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001409
1410 task->set_fd(extinfo->library_fd, false);
1411 task->set_file_offset(file_offset);
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001412 return load_library(ns, task, load_tasks, rtld_flags, realpath, search_linked_namespaces);
Spencer Low0346ad72015-04-22 18:06:51 -07001413 }
1414
1415 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001416 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001417 if (fd == -1) {
1418 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001419 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001420 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001421
1422 task->set_fd(fd, true);
1423 task->set_file_offset(file_offset);
1424
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001425 return load_library(ns, task, load_tasks, rtld_flags, realpath, search_linked_namespaces);
Spencer Low0346ad72015-04-22 18:06:51 -07001426}
1427
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001428static bool find_loaded_library_by_soname(android_namespace_t* ns,
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001429 const char* name,
1430 soinfo** candidate) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001431 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001432 const char* soname = si->get_soname();
1433 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dimitry Ivanov3bd90612017-02-01 08:54:43 -08001434 *candidate = si;
1435 return false;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001436 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001437
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001438 return true;
1439 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001440}
1441
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001442// Returns true if library was found and false otherwise
1443static bool find_loaded_library_by_soname(android_namespace_t* ns,
1444 const char* name,
1445 bool search_linked_namespaces,
1446 soinfo** candidate) {
1447 *candidate = nullptr;
1448
1449 // Ignore filename with path.
1450 if (strchr(name, '/') != nullptr) {
1451 return false;
1452 }
1453
1454 bool found = find_loaded_library_by_soname(ns, name, candidate);
1455
1456 if (!found && search_linked_namespaces) {
1457 // if a library was not found - look into linked namespaces
1458 for (auto& link : ns->linked_namespaces()) {
1459 if (!link.is_accessible(name)) {
1460 continue;
1461 }
1462
1463 android_namespace_t* linked_ns = link.linked_namespace();
1464
1465 if (find_loaded_library_by_soname(linked_ns, name, candidate)) {
1466 return true;
1467 }
1468 }
1469 }
1470
1471 return found;
1472}
1473
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001474static bool find_library_in_linked_namespace(const android_namespace_link_t& namespace_link,
Jiyong Park34a4acd2017-05-20 01:01:24 +09001475 LoadTask* task) {
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001476 android_namespace_t* ns = namespace_link.linked_namespace();
1477
1478 soinfo* candidate;
1479 bool loaded = false;
1480
1481 std::string soname;
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001482 if (find_loaded_library_by_soname(ns, task->get_name(), false, &candidate)) {
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001483 loaded = true;
1484 soname = candidate->get_soname();
1485 } else {
1486 soname = resolve_soname(task->get_name());
1487 }
1488
1489 if (!namespace_link.is_accessible(soname.c_str())) {
1490 // the library is not accessible via namespace_link
1491 return false;
1492 }
1493
1494 // if library is already loaded - return it
1495 if (loaded) {
1496 task->set_soinfo(candidate);
1497 return true;
1498 }
1499
Jiyong Park34a4acd2017-05-20 01:01:24 +09001500 // returning true with empty soinfo means that the library is okay to be
1501 // loaded in the namespace buy has not yet been loaded there before.
1502 task->set_soinfo(nullptr);
1503 return true;
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001504}
1505
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001506static bool find_library_internal(android_namespace_t* ns,
1507 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001508 ZipArchiveCache* zip_archive_cache,
1509 LoadTaskList* load_tasks,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001510 int rtld_flags,
1511 bool search_linked_namespaces) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001512 soinfo* candidate;
1513
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001514 if (find_loaded_library_by_soname(ns, task->get_name(), search_linked_namespaces, &candidate)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001515 task->set_soinfo(candidate);
1516 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001517 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001518
1519 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001520 // of this fact is done by load_library.
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001521 TRACE("[ \"%s\" find_loaded_library_by_soname failed (*candidate=%s@%p). Trying harder...]",
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001522 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001523
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001524 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags, search_linked_namespaces)) {
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001525 return true;
1526 }
1527
1528 if (search_linked_namespaces) {
1529 // if a library was not found - look into linked namespaces
1530 for (auto& linked_namespace : ns->linked_namespaces()) {
1531 if (find_library_in_linked_namespace(linked_namespace,
Jiyong Park34a4acd2017-05-20 01:01:24 +09001532 task)) {
1533 if (task->get_soinfo() == nullptr) {
1534 // try to load the library - once namespace boundary is crossed
1535 // we need to load a library within separate load_group
1536 // to avoid using symbols from foreign namespace while.
1537 //
1538 // However, actual linking is deferred until when the global group
1539 // is fully identified and is applied to all namespaces.
1540 // Otherwise, the libs in the linked namespace won't get symbols from
1541 // the global group.
1542 if (load_library(linked_namespace.linked_namespace(), task, zip_archive_cache, load_tasks, rtld_flags, false)) {
1543 return true;
1544 }
1545 // lib was not found in the namespace. Try next linked namespace.
1546 } else {
1547 // lib is already loaded
1548 return true;
1549 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001550 }
1551 }
1552 }
1553
1554 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001555}
1556
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001557static void soinfo_unload(soinfo* si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001558static void soinfo_unload(soinfo* soinfos[], size_t count);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001559
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001560static void shuffle(std::vector<LoadTask*>* v) {
1561 for (size_t i = 0, size = v->size(); i < size; ++i) {
1562 size_t n = size - i;
1563 size_t r = arc4random_uniform(n);
1564 std::swap((*v)[n-1], (*v)[r]);
1565 }
1566}
1567
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001568// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1569// not their transitive dependencies) as children of the start_with library.
1570// This is false when find_libraries is called for dlopen(), when newly loaded
1571// libraries must form a disjoint tree.
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001572bool find_libraries(android_namespace_t* ns,
1573 soinfo* start_with,
1574 const char* const library_names[],
1575 size_t library_names_count,
1576 soinfo* soinfos[],
1577 std::vector<soinfo*>* ld_preloads,
1578 size_t ld_preloads_count,
1579 int rtld_flags,
1580 const android_dlextinfo* extinfo,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001581 bool add_as_children,
Jiyong Park34a4acd2017-05-20 01:01:24 +09001582 bool search_linked_namespaces,
1583 std::unordered_map<const soinfo*, ElfReader>& readers_map,
1584 std::vector<android_namespace_t*>* namespaces) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001585 // Step 0: prepare.
1586 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001587
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001588 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001589 const char* name = library_names[i];
Jiyong Park34a4acd2017-05-20 01:01:24 +09001590 load_tasks.push_back(LoadTask::create(name, start_with, ns, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001591 }
1592
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001593 // If soinfos array is null allocate one on stack.
1594 // The array is needed in case of failure; for example
1595 // when library_names[] = {libone.so, libtwo.so} and libone.so
1596 // is loaded correctly but libtwo.so failed for some reason.
1597 // In this case libone.so should be unloaded on return.
1598 // See also implementation of failure_guard below.
1599
1600 if (soinfos == nullptr) {
1601 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1602 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1603 memset(soinfos, 0, soinfos_size);
1604 }
1605
1606 // list of libraries to link - see step 2.
1607 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001608
Tom Cherryb8ab6182017-04-05 16:20:29 -07001609 auto scope_guard = android::base::make_scope_guard([&]() {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001610 for (LoadTask* t : load_tasks) {
1611 LoadTask::deleter(t);
1612 }
1613 });
1614
Tom Cherryb8ab6182017-04-05 16:20:29 -07001615 auto failure_guard = android::base::make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001616 // Housekeeping
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001617 soinfo_unload(soinfos, soinfos_count);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001618 });
1619
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001620 ZipArchiveCache zip_archive_cache;
1621
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001622 // Step 1: expand the list of load_tasks to include
1623 // all DT_NEEDED libraries (do not load them just yet)
1624 for (size_t i = 0; i<load_tasks.size(); ++i) {
1625 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07001626 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001627
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001628 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001629 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001630 task->set_dt_needed(is_dt_needed);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001631
Jiyong Park34a4acd2017-05-20 01:01:24 +09001632 // try to find the load.
1633 // Note: start from the namespace that is stored in the LoadTask. This namespace
1634 // is different from the current namespace when the LoadTask is for a transitive
1635 // dependency and the lib that created the LoadTask is not found in the
1636 // current namespace but in one of the linked namespace.
1637 if (!find_library_internal(const_cast<android_namespace_t*>(task->get_start_from()),
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001638 task,
1639 &zip_archive_cache,
1640 &load_tasks,
1641 rtld_flags,
1642 search_linked_namespaces || is_dt_needed)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001643 return false;
1644 }
1645
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001646 soinfo* si = task->get_soinfo();
1647
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001648 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001649 needed_by->add_child(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001650
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001651 if (si->is_linked()) {
1652 si->increment_ref_count();
1653 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001654 }
1655
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001656 // When ld_preloads is not null, the first
1657 // ld_preloads_count libs are in fact ld_preloads.
1658 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001659 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001660 }
1661
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001662 if (soinfos_count < library_names_count) {
1663 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001664 }
1665 }
1666
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001667 // Step 2: Load libraries in random order (see b/24047022)
1668 LoadTaskList load_list;
1669 for (auto&& task : load_tasks) {
1670 soinfo* si = task->get_soinfo();
1671 auto pred = [&](const LoadTask* t) {
1672 return t->get_soinfo() == si;
1673 };
1674
1675 if (!si->is_linked() &&
1676 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
1677 load_list.push_back(task);
1678 }
1679 }
1680 shuffle(&load_list);
1681
1682 for (auto&& task : load_list) {
1683 if (!task->load()) {
1684 return false;
1685 }
1686 }
1687
1688 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
1689 for (auto&& task : load_tasks) {
1690 soinfo* si = task->get_soinfo();
1691 if (!si->is_linked() && !si->prelink_image()) {
1692 return false;
1693 }
1694 }
1695
Jiyong Park34a4acd2017-05-20 01:01:24 +09001696 // Step 4: Construct the global group. Note: DF_1_GLOBAL bit of a library is
1697 // determined at step 3.
1698
1699 // Step 4-1: DF_1_GLOBAL bit is force set for LD_PRELOADed libs because they
1700 // must be added to the global group
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001701 if (ld_preloads != nullptr) {
1702 for (auto&& si : *ld_preloads) {
1703 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
1704 }
1705 }
1706
Jiyong Park34a4acd2017-05-20 01:01:24 +09001707 // Step 4-2: Gather all DF_1_GLOBAL libs which were newly loaded during this
1708 // run. These will be the new member of the global group
1709 soinfo_list_t new_global_group_members;
1710 for (auto&& task : load_tasks) {
1711 soinfo* si = task->get_soinfo();
1712 if (!si->is_linked() && (si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1713 new_global_group_members.push_back(si);
1714 }
1715 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001716
Jiyong Park34a4acd2017-05-20 01:01:24 +09001717 // Step 4-3: Add the new global group members to all the linked namespaces
1718 for (auto si : new_global_group_members) {
1719 for (auto linked_ns : *namespaces) {
1720 if (si->get_primary_namespace() != linked_ns) {
1721 linked_ns->add_soinfo(si);
1722 si->add_secondary_namespace(linked_ns);
1723 }
1724 }
1725 }
1726
1727 // Step 5: link libraries that are not destined to this namespace.
1728 // Do this by recursively calling find_libraries on the namespace where the lib
1729 // was found during Step 1.
1730 for (auto&& task : load_tasks) {
1731 soinfo* si = task->get_soinfo();
1732 if (si->get_primary_namespace() != ns) {
1733 const char* name = task->get_name();
1734 if (find_libraries(si->get_primary_namespace(), task->get_needed_by(), &name, 1,
1735 nullptr /* soinfos */, nullptr /* ld_preloads */, 0 /* ld_preload_count */,
1736 rtld_flags, nullptr /* extinfo */, false /* add_as_children */,
1737 false /* search_linked_namespaces */, readers_map, namespaces)) {
1738 // If this lib is directly needed by one of the libs in this namespace,
1739 // then increment the count
1740 soinfo* needed_by = task->get_needed_by();
1741 if (needed_by != nullptr && needed_by->get_primary_namespace() == ns && si->is_linked()) {
1742 si->increment_ref_count();
1743 }
1744 } else {
1745 return false;
1746 }
1747 }
1748 }
1749
1750 // Step 6: link libraries in this namespace
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001751 soinfo_list_t local_group;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001752 walk_dependencies_tree(
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001753 (start_with != nullptr && add_as_children) ? &start_with : soinfos,
1754 (start_with != nullptr && add_as_children) ? 1 : soinfos_count,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001755 [&] (soinfo* si) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001756 if (ns->is_accessible(si)) {
1757 local_group.push_back(si);
1758 return kWalkContinue;
1759 } else {
1760 return kWalkSkip;
1761 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001762 });
1763
Jiyong Park34a4acd2017-05-20 01:01:24 +09001764 soinfo_list_t global_group = ns->get_global_group();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001765 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001766 if (!si->is_linked()) {
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001767 if (!si->link_image(global_group, local_group, extinfo) ||
1768 !get_cfi_shadow()->AfterLoad(si, solist_get_head())) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001769 return false;
1770 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001771 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001772
1773 return true;
1774 });
1775
1776 if (linked) {
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001777 local_group.for_each([](soinfo* si) {
1778 if (!si->is_linked()) {
1779 si->set_linked();
1780 }
1781 });
1782
Tom Cherryb8ab6182017-04-05 16:20:29 -07001783 failure_guard.Disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001784 }
1785
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001786 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001787}
1788
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001789static soinfo* find_library(android_namespace_t* ns,
1790 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001791 const android_dlextinfo* extinfo,
1792 soinfo* needed_by) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001793 soinfo* si;
1794
Jiyong Park34a4acd2017-05-20 01:01:24 +09001795 // readers_map is shared across recursive calls to find_libraries.
1796 // However, the map is not shared across different threads.
1797 std::unordered_map<const soinfo*, ElfReader> readers_map;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001798 if (name == nullptr) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001799 si = solist_get_somain();
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001800 } else if (!find_libraries(ns,
1801 needed_by,
1802 &name,
1803 1,
1804 &si,
1805 nullptr,
1806 0,
1807 rtld_flags,
1808 extinfo,
1809 false /* add_as_children */,
Jiyong Park34a4acd2017-05-20 01:01:24 +09001810 true /* search_linked_namespaces */,
1811 readers_map)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001812 return nullptr;
1813 }
1814
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001815 si->increment_ref_count();
1816
Elliott Hughesd23736e2012-11-01 15:16:56 -07001817 return si;
1818}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001819
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001820static void soinfo_unload(soinfo* root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001821 if (root->is_linked()) {
1822 root = root->get_local_group_root();
1823 }
1824
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07001825 ScopedTrace trace((std::string("unload ") + root->get_realpath()).c_str());
1826
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001827 if (!root->can_unload()) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001828 TRACE("not unloading \"%s\" - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001829 return;
1830 }
1831
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001832 soinfo_unload(&root, 1);
1833}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001834
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001835static void soinfo_unload(soinfo* soinfos[], size_t count) {
1836 // Note that the library can be loaded but not linked;
1837 // in which case there is no root but we still need
1838 // to walk the tree and unload soinfos involved.
1839 //
1840 // This happens on unsuccessful dlopen, when one of
1841 // the DT_NEEDED libraries could not be linked/found.
1842 if (count == 0) {
1843 return;
1844 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001845
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001846 soinfo_list_t unload_list;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001847 for (size_t i = 0; i < count; ++i) {
1848 soinfo* si = soinfos[i];
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001849
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001850 if (si->can_unload()) {
1851 size_t ref_count = si->is_linked() ? si->decrement_ref_count() : 0;
1852 if (ref_count == 0) {
1853 unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001854 } else {
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001855 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
1856 si->get_realpath(), ref_count);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001857 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001858 } else {
1859 TRACE("not unloading '%s' - the binary is flagged with NODELETE", si->get_realpath());
1860 return;
1861 }
1862 }
1863
1864 // This is used to identify soinfos outside of the load-group
1865 // note that we cannot have > 1 in the array and have any of them
1866 // linked. This is why we can safely use the first one.
1867 soinfo* root = soinfos[0];
1868
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001869 soinfo_list_t local_unload_list;
1870 soinfo_list_t external_unload_list;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001871 soinfo* si = nullptr;
1872
1873 while ((si = unload_list.pop_front()) != nullptr) {
1874 if (local_unload_list.contains(si)) {
1875 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001876 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001877
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001878 local_unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001879
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001880 if (si->has_min_version(0)) {
1881 soinfo* child = nullptr;
1882 while ((child = si->get_children().pop_front()) != nullptr) {
1883 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
1884 child->get_realpath(), child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001885
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001886 child->get_parents().remove(si);
1887
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001888 if (local_unload_list.contains(child)) {
1889 continue;
1890 } else if (child->is_linked() && child->get_local_group_root() != root) {
1891 external_unload_list.push_back(child);
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001892 } else if (child->get_parents().empty()) {
1893 unload_list.push_back(child);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001894 }
1895 }
1896 } else {
1897#if !defined(__work_around_b_24465209__)
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001898 async_safe_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001899#else
1900 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
1901 for_each_dt_needed(si, [&] (const char* library_name) {
1902 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
1903 si->get_realpath(), library_name);
1904
1905 soinfo* needed = find_library(si->get_primary_namespace(),
1906 library_name, RTLD_NOLOAD, nullptr, nullptr);
1907
1908 if (needed != nullptr) {
1909 // Not found: for example if symlink was deleted between dlopen and dlclose
1910 // Since we cannot really handle errors at this point - print and continue.
1911 PRINT("warning: couldn't find %s needed by %s on unload.",
1912 library_name, si->get_realpath());
1913 return;
1914 } else if (local_unload_list.contains(needed)) {
1915 // already visited
1916 return;
1917 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
1918 // external group
1919 external_unload_list.push_back(needed);
1920 } else {
1921 // local group
1922 unload_list.push_front(needed);
1923 }
1924 });
1925#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001926 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001927 }
1928
1929 local_unload_list.for_each([](soinfo* si) {
1930 si->call_destructors();
1931 });
1932
1933 while ((si = local_unload_list.pop_front()) != nullptr) {
1934 notify_gdb_of_unload(si);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001935 get_cfi_shadow()->BeforeUnload(si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001936 soinfo_free(si);
1937 }
1938
1939 while ((si = external_unload_list.pop_front()) != nullptr) {
1940 soinfo_unload(si);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001941 }
1942}
1943
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001944static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
1945 if (sym_ver == nullptr) {
1946 return sym_name;
1947 }
1948
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001949 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001950}
1951
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001952static android_namespace_t* get_caller_namespace(soinfo* caller) {
1953 return caller != nullptr ? caller->get_primary_namespace() : g_anonymous_namespace;
1954}
1955
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001956void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001957 // Use basic string manipulation calls to avoid snprintf.
1958 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1959 // When debug malloc is enabled, this call returns 0. This in turn causes
1960 // snprintf to do nothing, which causes libraries to fail to load.
1961 // See b/17302493 for further details.
1962 // Once the above bug is fixed, this code can be modified to use
1963 // snprintf again.
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001964 const auto& default_ld_paths = g_default_namespace.get_default_library_paths();
1965
1966 size_t required_size = 0;
1967 for (const auto& path : default_ld_paths) {
1968 required_size += path.size() + 1;
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001969 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001970
1971 if (buffer_size < required_size) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001972 async_safe_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
1973 "buffer len %zu, required len %zu", buffer_size, required_size);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001974 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001975
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001976 char* end = buffer;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001977 for (size_t i = 0; i < default_ld_paths.size(); ++i) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001978 if (i > 0) *end++ = ':';
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001979 end = stpcpy(end, default_ld_paths[i].c_str());
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001980 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001981}
1982
Elliott Hughescade4c32012-12-20 14:42:14 -08001983void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08001984 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08001985}
1986
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001987static std::string android_dlextinfo_to_string(const android_dlextinfo* info) {
1988 if (info == nullptr) {
1989 return "(null)";
1990 }
1991
1992 return android::base::StringPrintf("[flags=0x%" PRIx64 ","
1993 " reserved_addr=%p,"
1994 " reserved_size=0x%zx,"
1995 " relro_fd=%d,"
1996 " library_fd=%d,"
1997 " library_fd_offset=0x%" PRIx64 ","
1998 " library_namespace=%s@%p]",
1999 info->flags,
2000 info->reserved_addr,
2001 info->reserved_size,
2002 info->relro_fd,
2003 info->library_fd,
2004 info->library_fd_offset,
2005 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
2006 (info->library_namespace != nullptr ?
2007 info->library_namespace->get_name() : "(null)") : "(n/a)",
2008 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
2009 info->library_namespace : nullptr);
2010}
2011
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08002012void* do_dlopen(const char* name, int flags,
2013 const android_dlextinfo* extinfo,
2014 const void* caller_addr) {
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -07002015 std::string trace_prefix = std::string("dlopen: ") + (name == nullptr ? "(nullptr)" : name);
2016 ScopedTrace trace(trace_prefix.c_str());
2017 ScopedTrace loading_trace((trace_prefix + " - loading and linking").c_str());
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002018 soinfo* const caller = find_containing_library(caller_addr);
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002019 android_namespace_t* ns = get_caller_namespace(caller);
2020
2021 LD_LOG(kLogDlopen,
2022 "dlopen(name=\"%s\", flags=0x%x, extinfo=%s, caller=\"%s\", caller_ns=%s@%p) ...",
2023 name,
2024 flags,
2025 android_dlextinfo_to_string(extinfo).c_str(),
2026 caller == nullptr ? "(null)" : caller->get_realpath(),
2027 ns == nullptr ? "(null)" : ns->get_name(),
2028 ns);
2029
Tom Cherryb8ab6182017-04-05 16:20:29 -07002030 auto failure_guard = android::base::make_scope_guard(
2031 [&]() { LD_LOG(kLogDlopen, "... dlopen failed: %s", linker_get_error_buffer()); });
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002032
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002033 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08002034 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002035 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08002036 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002037
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002038 if (extinfo != nullptr) {
2039 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
2040 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
2041 return nullptr;
2042 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002043
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002044 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07002045 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002046 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
2047 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002048 return nullptr;
2049 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002050
2051 if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 &&
2052 (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) {
2053 DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not "
2054 "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT");
2055 return nullptr;
2056 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002057
2058 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
2059 if (extinfo->library_namespace == nullptr) {
2060 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
2061 return nullptr;
2062 }
2063 ns = extinfo->library_namespace;
2064 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00002065 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002066
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07002067 std::string asan_name_holder;
2068
2069 const char* translated_name = name;
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08002070 if (g_is_asan && translated_name != nullptr && translated_name[0] == '/') {
Evgenii Stepanov9e77a642017-07-27 14:55:44 -07002071 char original_path[PATH_MAX];
2072 if (realpath(name, original_path) != nullptr) {
2073 asan_name_holder = std::string(kAsanLibDirPrefix) + original_path;
Vishwath Mohan4113def2017-03-29 15:31:34 -07002074 if (file_exists(asan_name_holder.c_str())) {
Evgenii Stepanov9e77a642017-07-27 14:55:44 -07002075 soinfo* si = nullptr;
2076 if (find_loaded_library_by_realpath(ns, original_path, true, &si)) {
2077 PRINT("linker_asan dlopen NOT translating \"%s\" -> \"%s\": library already loaded", name,
2078 asan_name_holder.c_str());
2079 } else {
2080 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
2081 translated_name = asan_name_holder.c_str();
2082 }
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07002083 }
2084 }
2085 }
2086
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002087 ProtectedDataGuard guard;
Paul Keithd559fee2018-02-15 21:57:33 +01002088#ifdef LD_SHIM_LIBS
Adrian DC8befe0c2017-04-08 22:40:01 +02002089 reset_g_active_shim_libs();
Paul Keithd559fee2018-02-15 21:57:33 +01002090#endif
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07002091 soinfo* si = find_library(ns, translated_name, flags, extinfo, caller);
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -07002092 loading_trace.End();
2093
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002094 if (si != nullptr) {
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002095 void* handle = si->to_handle();
2096 LD_LOG(kLogDlopen,
Dimitry Ivanovae4a0c12016-11-21 10:44:35 -08002097 "... dlopen calling constructors: realpath=\"%s\", soname=\"%s\", handle=%p",
2098 si->get_realpath(), si->get_soname(), handle);
2099 si->call_constructors();
Tom Cherryb8ab6182017-04-05 16:20:29 -07002100 failure_guard.Disable();
Dimitry Ivanovae4a0c12016-11-21 10:44:35 -08002101 LD_LOG(kLogDlopen,
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002102 "... dlopen successful: realpath=\"%s\", soname=\"%s\", handle=%p",
2103 si->get_realpath(), si->get_soname(), handle);
2104 return handle;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002105 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002106
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002107 return nullptr;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002108}
2109
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002110int do_dladdr(const void* addr, Dl_info* info) {
2111 // Determine if this address can be found in any library currently mapped.
2112 soinfo* si = find_containing_library(addr);
2113 if (si == nullptr) {
2114 return 0;
2115 }
2116
2117 memset(info, 0, sizeof(Dl_info));
2118
2119 info->dli_fname = si->get_realpath();
2120 // Address at which the shared object is loaded.
2121 info->dli_fbase = reinterpret_cast<void*>(si->base);
2122
2123 // Determine if any symbol in the library contains the specified address.
2124 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
2125 if (sym != nullptr) {
2126 info->dli_sname = si->get_string(sym->st_name);
2127 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
2128 }
2129
2130 return 1;
2131}
2132
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002133static soinfo* soinfo_from_handle(void* handle) {
2134 if ((reinterpret_cast<uintptr_t>(handle) & 1) != 0) {
2135 auto it = g_soinfo_handles_map.find(reinterpret_cast<uintptr_t>(handle));
2136 if (it == g_soinfo_handles_map.end()) {
2137 return nullptr;
2138 } else {
2139 return it->second;
2140 }
2141 }
2142
2143 return static_cast<soinfo*>(handle);
2144}
2145
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08002146bool do_dlsym(void* handle,
2147 const char* sym_name,
2148 const char* sym_ver,
2149 const void* caller_addr,
2150 void** symbol) {
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07002151 ScopedTrace trace("dlsym");
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002152#if !defined(__LP64__)
2153 if (handle == nullptr) {
2154 DL_ERR("dlsym failed: library handle is null");
2155 return false;
2156 }
2157#endif
2158
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002159 soinfo* found = nullptr;
2160 const ElfW(Sym)* sym = nullptr;
2161 soinfo* caller = find_containing_library(caller_addr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002162 android_namespace_t* ns = get_caller_namespace(caller);
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002163 soinfo* si = nullptr;
2164 if (handle != RTLD_DEFAULT && handle != RTLD_NEXT) {
2165 si = soinfo_from_handle(handle);
2166 }
2167
2168 LD_LOG(kLogDlsym,
2169 "dlsym(handle=%p(\"%s\"), sym_name=\"%s\", sym_ver=\"%s\", caller=\"%s\", caller_ns=%s@%p) ...",
2170 handle,
2171 si != nullptr ? si->get_realpath() : "n/a",
2172 sym_name,
2173 sym_ver,
2174 caller == nullptr ? "(null)" : caller->get_realpath(),
2175 ns == nullptr ? "(null)" : ns->get_name(),
2176 ns);
2177
Tom Cherryb8ab6182017-04-05 16:20:29 -07002178 auto failure_guard = android::base::make_scope_guard(
2179 [&]() { LD_LOG(kLogDlsym, "... dlsym failed: %s", linker_get_error_buffer()); });
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002180
2181 if (sym_name == nullptr) {
2182 DL_ERR("dlsym failed: symbol name is null");
2183 return false;
2184 }
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002185
2186 version_info vi_instance;
2187 version_info* vi = nullptr;
2188
2189 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002190 vi_instance.name = sym_ver;
2191 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002192 vi = &vi_instance;
2193 }
2194
2195 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2196 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2197 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002198 if (si == nullptr) {
2199 DL_ERR("dlsym failed: invalid handle: %p", handle);
2200 return false;
2201 }
2202 sym = dlsym_handle_lookup(si, &found, sym_name, vi);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002203 }
2204
2205 if (sym != nullptr) {
2206 uint32_t bind = ELF_ST_BIND(sym->st_info);
2207
2208 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2209 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
Tom Cherryb8ab6182017-04-05 16:20:29 -07002210 failure_guard.Disable();
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002211 LD_LOG(kLogDlsym,
2212 "... dlsym successful: sym_name=\"%s\", sym_ver=\"%s\", found in=\"%s\", address=%p",
2213 sym_name, sym_ver, found->get_soname(), *symbol);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002214 return true;
2215 }
2216
2217 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2218 return false;
2219 }
2220
2221 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2222 return false;
2223}
2224
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002225int do_dlclose(void* handle) {
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07002226 ScopedTrace trace("dlclose");
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002227 ProtectedDataGuard guard;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002228 soinfo* si = soinfo_from_handle(handle);
2229 if (si == nullptr) {
2230 DL_ERR("invalid handle: %p", handle);
2231 return -1;
2232 }
2233
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002234 soinfo_unload(si);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002235 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002236}
2237
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002238bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path) {
2239 if (g_anonymous_namespace_initialized) {
2240 DL_ERR("anonymous namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002241 return false;
2242 }
2243
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002244 ProtectedDataGuard guard;
2245
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002246 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002247 // When the caller is nullptr - create_namespace will take global group
2248 // from the anonymous namespace, which is fine because anonymous namespace
2249 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002250 android_namespace_t* anon_ns =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002251 create_namespace(nullptr,
2252 "(anonymous)",
2253 nullptr,
2254 library_search_path,
Dimitry Ivanovc9dced22017-03-27 15:42:17 -07002255 ANDROID_NAMESPACE_TYPE_ISOLATED,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002256 nullptr,
2257 &g_default_namespace);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002258
2259 if (anon_ns == nullptr) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002260 return false;
2261 }
2262
2263 if (!link_namespaces(anon_ns, &g_default_namespace, shared_lib_sonames)) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002264 return false;
2265 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08002266
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002267 g_anonymous_namespace = anon_ns;
Dimitry Ivanov3e0821d2017-03-07 11:02:10 -08002268 g_anonymous_namespace_initialized = true;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002269
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002270 return true;
2271}
2272
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002273static void add_soinfos_to_namespace(const soinfo_list_t& soinfos, android_namespace_t* ns) {
2274 ns->add_soinfos(soinfos);
2275 for (auto si : soinfos) {
2276 si->add_secondary_namespace(ns);
2277 }
2278}
2279
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002280android_namespace_t* create_namespace(const void* caller_addr,
2281 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002282 const char* ld_library_path,
2283 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002284 uint64_t type,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002285 const char* permitted_when_isolated_path,
2286 android_namespace_t* parent_namespace) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002287 if (parent_namespace == nullptr) {
Dimitry Ivanov52408632016-05-23 10:31:11 -07002288 // if parent_namespace is nullptr -> set it to the caller namespace
2289 soinfo* caller_soinfo = find_containing_library(caller_addr);
2290
2291 parent_namespace = caller_soinfo != nullptr ?
2292 caller_soinfo->get_primary_namespace() :
2293 g_anonymous_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002294 }
2295
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002296 ProtectedDataGuard guard;
2297 std::vector<std::string> ld_library_paths;
2298 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002299 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002300
2301 parse_path(ld_library_path, ":", &ld_library_paths);
2302 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002303 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002304
2305 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2306 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002307 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Jiyong Park37b91af2017-05-05 22:07:05 +09002308 ns->set_greylist_enabled((type & ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002309
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002310 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07002311 // append parent namespace paths.
2312 std::copy(parent_namespace->get_ld_library_paths().begin(),
2313 parent_namespace->get_ld_library_paths().end(),
2314 back_inserter(ld_library_paths));
2315
2316 std::copy(parent_namespace->get_default_library_paths().begin(),
2317 parent_namespace->get_default_library_paths().end(),
2318 back_inserter(default_library_paths));
2319
2320 std::copy(parent_namespace->get_permitted_paths().begin(),
2321 parent_namespace->get_permitted_paths().end(),
2322 back_inserter(permitted_paths));
2323
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002324 // If shared - clone the parent namespace
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002325 add_soinfos_to_namespace(parent_namespace->soinfo_list(), ns);
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07002326 // and copy parent namespace links
2327 for (auto& link : parent_namespace->linked_namespaces()) {
2328 ns->add_linked_namespace(link.linked_namespace(), link.shared_lib_sonames());
2329 }
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002330 } else {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002331 // If not shared - copy only the shared group
Jiyong Park34a4acd2017-05-20 01:01:24 +09002332 add_soinfos_to_namespace(parent_namespace->get_shared_group(), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002333 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002334
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07002335 ns->set_ld_library_paths(std::move(ld_library_paths));
2336 ns->set_default_library_paths(std::move(default_library_paths));
2337 ns->set_permitted_paths(std::move(permitted_paths));
2338
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002339 return ns;
2340}
2341
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002342bool link_namespaces(android_namespace_t* namespace_from,
2343 android_namespace_t* namespace_to,
2344 const char* shared_lib_sonames) {
2345 if (namespace_to == nullptr) {
2346 namespace_to = &g_default_namespace;
2347 }
2348
2349 if (namespace_from == nullptr) {
2350 DL_ERR("error linking namespaces: namespace_from is null.");
2351 return false;
2352 }
2353
2354 if (shared_lib_sonames == nullptr || shared_lib_sonames[0] == '\0') {
2355 DL_ERR("error linking namespaces \"%s\"->\"%s\": the list of shared libraries is empty.",
2356 namespace_from->get_name(), namespace_to->get_name());
2357 return false;
2358 }
2359
2360 auto sonames = android::base::Split(shared_lib_sonames, ":");
2361 std::unordered_set<std::string> sonames_set(sonames.begin(), sonames.end());
2362
2363 ProtectedDataGuard guard;
2364 namespace_from->add_linked_namespace(namespace_to, sonames_set);
2365
2366 return true;
2367}
2368
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002369ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002370 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2371 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2372 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002373 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2374 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002375
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002376 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002377}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002378
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002379const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2380 if (source_symver < 2 ||
2381 source_symver >= version_infos.size() ||
2382 version_infos[source_symver].name == nullptr) {
2383 return nullptr;
2384 }
2385
2386 return &version_infos[source_symver];
2387}
2388
2389void VersionTracker::add_version_info(size_t source_index,
2390 ElfW(Word) elf_hash,
2391 const char* ver_name,
2392 const soinfo* target_si) {
2393 if (source_index >= version_infos.size()) {
2394 version_infos.resize(source_index+1);
2395 }
2396
2397 version_infos[source_index].elf_hash = elf_hash;
2398 version_infos[source_index].name = ver_name;
2399 version_infos[source_index].target_si = target_si;
2400}
2401
2402bool VersionTracker::init_verneed(const soinfo* si_from) {
2403 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2404
2405 if (verneed_ptr == 0) {
2406 return true;
2407 }
2408
2409 size_t verneed_cnt = si_from->get_verneed_cnt();
2410
2411 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2412 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2413 size_t vernaux_offset = offset + verneed->vn_aux;
2414 offset += verneed->vn_next;
2415
2416 if (verneed->vn_version != 1) {
2417 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2418 return false;
2419 }
2420
2421 const char* target_soname = si_from->get_string(verneed->vn_file);
2422 // find it in dependencies
2423 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002424 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002425 });
2426
2427 if (target_si == nullptr) {
2428 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002429 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002430 return false;
2431 }
2432
2433 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2434 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2435 vernaux_offset += vernaux->vna_next;
2436
2437 const ElfW(Word) elf_hash = vernaux->vna_hash;
2438 const char* ver_name = si_from->get_string(vernaux->vna_name);
2439 ElfW(Half) source_index = vernaux->vna_other;
2440
2441 add_version_info(source_index, elf_hash, ver_name, target_si);
2442 }
2443 }
2444
2445 return true;
2446}
2447
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002448template <typename F>
2449static bool for_each_verdef(const soinfo* si, F functor) {
2450 if (!si->has_min_version(2)) {
2451 return true;
2452 }
2453
2454 uintptr_t verdef_ptr = si->get_verdef_ptr();
2455 if (verdef_ptr == 0) {
2456 return true;
2457 }
2458
2459 size_t offset = 0;
2460
2461 size_t verdef_cnt = si->get_verdef_cnt();
2462 for (size_t i = 0; i<verdef_cnt; ++i) {
2463 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
2464 size_t verdaux_offset = offset + verdef->vd_aux;
2465 offset += verdef->vd_next;
2466
2467 if (verdef->vd_version != 1) {
2468 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
2469 i, verdef->vd_version, si->get_realpath());
2470 return false;
2471 }
2472
2473 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
2474 // "this is the version of the file itself. It must not be used for
2475 // matching a symbol. It can be used to match references."
2476 //
2477 // http://www.akkadia.org/drepper/symbol-versioning
2478 continue;
2479 }
2480
2481 if (verdef->vd_cnt == 0) {
2482 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
2483 return false;
2484 }
2485
2486 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
2487
2488 if (functor(i, verdef, verdaux) == true) {
2489 break;
2490 }
2491 }
2492
2493 return true;
2494}
2495
2496bool find_verdef_version_index(const soinfo* si, const version_info* vi, ElfW(Versym)* versym) {
2497 if (vi == nullptr) {
2498 *versym = kVersymNotNeeded;
2499 return true;
2500 }
2501
2502 *versym = kVersymGlobal;
2503
2504 return for_each_verdef(si,
2505 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2506 if (verdef->vd_hash == vi->elf_hash &&
2507 strcmp(vi->name, si->get_string(verdaux->vda_name)) == 0) {
2508 *versym = verdef->vd_ndx;
2509 return true;
2510 }
2511
2512 return false;
2513 }
2514 );
2515}
2516
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002517bool VersionTracker::init_verdef(const soinfo* si_from) {
2518 return for_each_verdef(si_from,
2519 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2520 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2521 si_from->get_string(verdaux->vda_name), si_from);
2522 return false;
2523 }
2524 );
2525}
2526
2527bool VersionTracker::init(const soinfo* si_from) {
2528 if (!si_from->has_min_version(2)) {
2529 return true;
2530 }
2531
2532 return init_verneed(si_from) && init_verdef(si_from);
2533}
2534
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002535// TODO (dimitry): Methods below need to be moved out of soinfo
2536// and in more isolated file in order minimize dependencies on
2537// unnecessary object in the linker binary. Consider making them
2538// independent from soinfo (?).
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002539bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2540 const char* sym_name, const version_info** vi) {
2541 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2542 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2543
2544 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2545 *vi = version_tracker.get_version_info(sym_ver);
2546
2547 if (*vi == nullptr) {
2548 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002549 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002550 return false;
2551 }
2552 } else {
2553 // there is no version info
2554 *vi = nullptr;
2555 }
2556
2557 return true;
2558}
2559
Dimitry Ivanov576a3752016-08-09 06:58:55 -07002560#if !defined(__mips__)
2561#if defined(USE_RELA)
2562static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2563 return rela->r_addend;
2564}
2565#else
2566static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
2567 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2568 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
2569 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2570 }
2571 return 0;
2572}
2573#endif
2574
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002575template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002576bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2577 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002578 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2579 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002580 if (rel == nullptr) {
2581 return false;
2582 }
2583
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002584 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2585 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2586
2587 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002588 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002589 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002590 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002591
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002592 DEBUG("Processing \"%s\" relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002593 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002594 continue;
2595 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002596
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002597 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002598 soinfo* lsi = nullptr;
2599
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002600 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002601 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002602 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002603
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002604 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2605 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002606 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002607
2608 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2609 return false;
2610 }
2611
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002612 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002613 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002614 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002615 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002616 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002617 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002618 }
2619
2620 /* IHI0044C AAELF 4.5.1.1:
2621
2622 Libraries are not searched to resolve weak references.
2623 It is not an error for a weak reference to remain unsatisfied.
2624
2625 During linking, the value of an undefined weak reference is:
2626 - Zero if the relocation type is absolute
2627 - The address of the place if the relocation is pc-relative
2628 - The address of nominal base address if the relocation
2629 type is base-relative.
2630 */
2631
2632 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002633 case R_GENERIC_JUMP_SLOT:
2634 case R_GENERIC_GLOB_DAT:
2635 case R_GENERIC_RELATIVE:
2636 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002637#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002638 case R_AARCH64_ABS64:
2639 case R_AARCH64_ABS32:
2640 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002641#elif defined(__x86_64__)
2642 case R_X86_64_32:
2643 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002644#elif defined(__arm__)
2645 case R_ARM_ABS32:
2646#elif defined(__i386__)
2647 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002648#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002649 /*
2650 * The sym_addr was initialized to be zero above, or the relocation
2651 * code below does not care about value of sym_addr.
2652 * No need to do anything.
2653 */
2654 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002655#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002656 case R_X86_64_PC32:
2657 sym_addr = reloc;
2658 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002659#elif defined(__i386__)
2660 case R_386_PC32:
2661 sym_addr = reloc;
2662 break;
2663#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002664 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002665 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002666 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002667 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002668 } else { // We got a definition.
2669#if !defined(__LP64__)
2670 // When relocating dso with text_relocation .text segment is
2671 // not executable. We need to restore elf flags before resolving
2672 // STT_GNU_IFUNC symbol.
2673 bool protect_segments = has_text_relocations &&
2674 lsi == this &&
2675 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2676 if (protect_segments) {
2677 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2678 DL_ERR("can't protect segments for \"%s\": %s",
2679 get_realpath(), strerror(errno));
2680 return false;
2681 }
2682 }
2683#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002684 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002685#if !defined(__LP64__)
2686 if (protect_segments) {
2687 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2688 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2689 get_realpath(), strerror(errno));
2690 return false;
2691 }
2692 }
2693#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002694 }
2695 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002696 }
2697
2698 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002699 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002700 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002701 MARK(rel->r_offset);
2702 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2703 reinterpret_cast<void*>(reloc),
2704 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2705
2706 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002707 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002708 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002709 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002710 MARK(rel->r_offset);
2711 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2712 reinterpret_cast<void*>(reloc),
2713 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2714 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002715 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002716 case R_GENERIC_RELATIVE:
2717 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002718 MARK(rel->r_offset);
2719 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2720 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002721 reinterpret_cast<void*>(load_bias + addend));
2722 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002723 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002724 case R_GENERIC_IRELATIVE:
2725 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002726 MARK(rel->r_offset);
2727 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2728 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002729 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002730 {
2731#if !defined(__LP64__)
2732 // When relocating dso with text_relocation .text segment is
2733 // not executable. We need to restore elf flags for this
2734 // particular call.
2735 if (has_text_relocations) {
2736 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2737 DL_ERR("can't protect segments for \"%s\": %s",
2738 get_realpath(), strerror(errno));
2739 return false;
2740 }
2741 }
2742#endif
2743 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2744#if !defined(__LP64__)
2745 // Unprotect it afterwards...
2746 if (has_text_relocations) {
2747 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2748 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2749 get_realpath(), strerror(errno));
2750 return false;
2751 }
2752 }
2753#endif
2754 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2755 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002756 break;
2757
2758#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002759 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002760 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002761 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002762 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002763 reloc, sym_addr + addend, sym_name);
2764 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002765 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002766 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002767 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002768 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002769 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002770 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002771 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002772 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2773 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002774 if ((min_value <= (sym_addr + addend)) &&
2775 ((sym_addr + addend) <= max_value)) {
2776 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002777 } else {
2778 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002779 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002780 return false;
2781 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002782 }
2783 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002784 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002785 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002786 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002787 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002788 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002789 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002790 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2791 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002792 if ((min_value <= (sym_addr + addend)) &&
2793 ((sym_addr + addend) <= max_value)) {
2794 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002795 } else {
2796 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002797 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002798 return false;
2799 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002800 }
2801 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002802 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002803 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002804 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002805 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002806 reloc, sym_addr + addend, rel->r_offset, sym_name);
2807 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002808 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002809 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002810 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002811 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002812 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002813 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002814 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002815 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2816 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002817 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2818 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2819 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002820 } else {
2821 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002822 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002823 return false;
2824 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002825 }
2826 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002827 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002828 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002829 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002830 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002831 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002832 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002833 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2834 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002835 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2836 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2837 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002838 } else {
2839 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002840 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002841 return false;
2842 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002843 }
2844 break;
2845
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002846 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002847 /*
2848 * ET_EXEC is not supported so this should not happen.
2849 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002850 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002851 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002852 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002853 * R_AARCH64_COPY may only appear in executable objects where e_type is
2854 * set to ET_EXEC.
2855 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002856 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002857 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002858 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002859 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002860 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002861 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002862 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002863 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002864 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002865 break;
2866#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002867 case R_X86_64_32:
2868 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002869 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002870 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2871 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002872 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002873 break;
2874 case R_X86_64_64:
2875 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002876 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002877 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2878 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002879 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002880 break;
2881 case R_X86_64_PC32:
2882 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002883 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002884 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2885 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2886 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002887 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002888 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002889#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002890 case R_ARM_ABS32:
2891 count_relocation(kRelocAbsolute);
2892 MARK(rel->r_offset);
2893 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2894 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2895 break;
2896 case R_ARM_REL32:
2897 count_relocation(kRelocRelative);
2898 MARK(rel->r_offset);
2899 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2900 reloc, sym_addr, rel->r_offset, sym_name);
2901 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2902 break;
2903 case R_ARM_COPY:
2904 /*
2905 * ET_EXEC is not supported so this should not happen.
2906 *
2907 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2908 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002909 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002910 * R_ARM_COPY may only appear in executable objects where e_type is
2911 * set to ET_EXEC.
2912 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002913 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002914 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002915#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002916 case R_386_32:
2917 count_relocation(kRelocRelative);
2918 MARK(rel->r_offset);
2919 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2920 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2921 break;
2922 case R_386_PC32:
2923 count_relocation(kRelocRelative);
2924 MARK(rel->r_offset);
2925 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2926 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2927 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2928 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002929#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002930 default:
2931 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002932 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002933 }
2934 }
2935 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002936}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002937#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002938
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002939// An empty list of soinfos
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002940static soinfo_list_t g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002941
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002942bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002943 /* Extract dynamic section */
2944 ElfW(Word) dynamic_flags = 0;
2945 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002946
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002947 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002948 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002949 if (!relocating_linker) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002950 INFO("[ Linking \"%s\" ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002951 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002952 }
2953
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002954 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002955 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002956 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002957 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002958 return false;
2959 } else {
2960 if (!relocating_linker) {
2961 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002962 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002963 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002964
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002965#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002966 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2967 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002968#endif
2969
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002970 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002971 // Note that: "Except for the DT_NULL element at the end of the array,
2972 // and the relative order of DT_NEEDED elements, entries may appear in any order."
2973 //
2974 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002975 uint32_t needed_count = 0;
2976 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2977 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2978 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2979 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002980 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002981 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002982 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002983
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002984 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002985 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2986 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2987 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2988 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002989 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002990
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002991 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002992 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002993 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002994 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2995 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002996
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002997 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002998 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002999 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003000 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
3001 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003002
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003003 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003004 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003005 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003006 return false;
3007 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003008 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003009
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003010 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003011 break;
3012
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003013 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003014 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003015 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003016
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003017 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003018 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003019 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003020
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003021 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003022 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003023 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003024
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003025 case DT_SYMENT:
3026 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003027 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
3028 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003029 return false;
3030 }
3031 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003032
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003033 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003034#if defined(USE_RELA)
3035 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003036 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003037 return false;
3038 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003039#else
3040 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003041 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003042 return false;
3043 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003044#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003045 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003046
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003047 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003048#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003049 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003050#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003051 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003052#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003053 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003054
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003055 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003056#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003057 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003058#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003059 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003060#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003061 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003062
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003063 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003064#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003065 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003066 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003067#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003068 // Ignore for other platforms... (because RTLD_LAZY is not supported)
3069 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003070
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003071 case DT_DEBUG:
3072 // Set the DT_DEBUG entry to the address of _r_debug for GDB
3073 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08003074// FIXME: not working currently for N64
3075// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003076// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08003077// read-only, but the DYNAMIC header claims it is writable.
3078#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003079 if ((dynamic_flags & PF_W) != 0) {
3080 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
3081 }
Chris Dearman99186652014-02-06 20:36:51 -08003082#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08003083 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003084#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003085 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003086 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003087 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003088
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003089 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003090 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003091 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003092
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003093 case DT_ANDROID_RELA:
3094 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3095 break;
3096
3097 case DT_ANDROID_RELASZ:
3098 android_relocs_size_ = d->d_un.d_val;
3099 break;
3100
3101 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003102 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003103 return false;
3104
3105 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003106 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003107 return false;
3108
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003109 case DT_RELAENT:
3110 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003111 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003112 return false;
3113 }
3114 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003115
3116 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003117 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003118 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003119
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003120 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003121 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003122 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003123
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003124 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003125 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003126 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003127
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003128#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003129 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003130 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003131 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003132
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003133 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003134 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003135 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003136
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003137 case DT_RELENT:
3138 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003139 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003140 return false;
3141 }
3142 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003143
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003144 case DT_ANDROID_REL:
3145 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3146 break;
3147
3148 case DT_ANDROID_RELSZ:
3149 android_relocs_size_ = d->d_un.d_val;
3150 break;
3151
3152 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003153 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003154 return false;
3155
3156 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003157 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003158 return false;
3159
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003160 // "Indicates that all RELATIVE relocations have been concatenated together,
3161 // and specifies the RELATIVE relocation count."
3162 //
3163 // TODO: Spec also mentions that this can be used to optimize relocation process;
3164 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003165 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003166 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003167
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003168 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003169 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003170 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003171
3172 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003173 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003174 return false;
3175
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003176#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003177 case DT_INIT:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003178 init_func_ = reinterpret_cast<linker_ctor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003179 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003180 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003181
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003182 case DT_FINI:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003183 fini_func_ = reinterpret_cast<linker_dtor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003184 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003185 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003186
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003187 case DT_INIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003188 init_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003189 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003190 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003191
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003192 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003193 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003194 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003195
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003196 case DT_FINI_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003197 fini_array_ = reinterpret_cast<linker_dtor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003198 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003199 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003200
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003201 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003202 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003203 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003204
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003205 case DT_PREINIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003206 preinit_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003207 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003208 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003209
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003210 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003211 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003212 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003213
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003214 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003215#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003216 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003217 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003218#else
3219 has_text_relocations = true;
3220 break;
3221#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003222
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003223 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003224 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003225 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003226
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003227 case DT_NEEDED:
3228 ++needed_count;
3229 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003230
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003231 case DT_FLAGS:
3232 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003233#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003234 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003235 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003236#else
3237 has_text_relocations = true;
3238#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003239 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003240 if (d->d_un.d_val & DF_SYMBOLIC) {
3241 has_DT_SYMBOLIC = true;
3242 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003243 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003244
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003245 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003246 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003247
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003248 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003249 DL_WARN("\"%s\" has unsupported flags DT_FLAGS_1=%p", get_realpath(), reinterpret_cast<void*>(d->d_un.d_val));
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003250 }
3251 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003252#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003253 case DT_MIPS_RLD_MAP:
3254 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3255 {
3256 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3257 *dp = &_r_debug;
3258 }
3259 break;
Lazar Trsic83b44a92016-04-06 13:39:17 +02003260 case DT_MIPS_RLD_MAP_REL:
3261 // Set the DT_MIPS_RLD_MAP_REL entry to the address of _r_debug for GDB.
Raghu Gandham68815722014-12-18 19:12:19 -08003262 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003263 r_debug** dp = reinterpret_cast<r_debug**>(
3264 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003265 *dp = &_r_debug;
3266 }
3267 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003268
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003269 case DT_MIPS_RLD_VERSION:
3270 case DT_MIPS_FLAGS:
3271 case DT_MIPS_BASE_ADDRESS:
3272 case DT_MIPS_UNREFEXTNO:
3273 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003274
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003275 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003276 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003277 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003278
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003279 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003280 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003281 break;
3282
3283 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003284 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003285 break;
3286#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003287 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3288 case DT_BIND_NOW:
3289 break;
3290
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003291 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003292 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3293 break;
3294
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003295 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003296 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3297 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003298 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003299 verdef_cnt_ = d->d_un.d_val;
3300 break;
3301
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003302 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003303 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3304 break;
3305
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003306 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003307 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003308 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003309
Evgenii Stepanov68650822015-06-10 13:38:39 -07003310 case DT_RUNPATH:
3311 // this is parsed after we have strtab initialized (see below).
3312 break;
3313
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003314 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003315 if (!relocating_linker) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003316 DL_WARN("\"%s\" unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003317 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3318 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003319 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003320 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003321 }
3322
Duane Sandbc425c72015-06-01 16:29:14 -07003323#if defined(__mips__) && !defined(__LP64__)
3324 if (!mips_check_and_adjust_fp_modes()) {
3325 return false;
3326 }
3327#endif
3328
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003329 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003330 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003331
3332 // Sanity checks.
3333 if (relocating_linker && needed_count != 0) {
3334 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3335 return false;
3336 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003337 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003338 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003339 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003340 return false;
3341 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003342 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003343 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003344 return false;
3345 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003346 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003347 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003348 return false;
3349 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003350
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003351 // second pass - parse entries relying on strtab
3352 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003353 switch (d->d_tag) {
3354 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003355 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003356 break;
3357 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003358 set_dt_runpath(get_string(d->d_un.d_val));
3359 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003360 }
3361 }
3362
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003363 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003364 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003365 // because they can't find dt_needed library by soname.
3366 // This workaround should keep them working. (applies only
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003367 // for apps targeting sdk version < M). Make an exception for
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003368 // the main executable and linker; they do not need to have dt_soname
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003369 if (soname_ == nullptr &&
3370 this != solist_get_somain() &&
3371 (flags_ & FLAG_LINKER) == 0 &&
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003372 get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003373 soname_ = basename(realpath_.c_str());
3374 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3375 get_realpath(), soname_);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003376 // Don't call add_dlwarning because a missing DT_SONAME isn't important enough to show in the UI
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003377 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003378 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003379}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003380
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003381bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3382 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003383
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003384 local_group_root_ = local_group.front();
3385 if (local_group_root_ == nullptr) {
3386 local_group_root_ = this;
3387 }
3388
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003389 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3390 target_sdk_version_ = get_application_target_sdk_version();
3391 }
3392
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003393 VersionTracker version_tracker;
3394
3395 if (!version_tracker.init(this)) {
3396 return false;
3397 }
3398
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003399#if !defined(__LP64__)
3400 if (has_text_relocations) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003401 // Fail if app is targeting M or above.
Adrian DCbf3c0f42016-08-27 15:09:36 +02003402#if defined(TARGET_NEEDS_PLATFORM_TEXT_RELOCATIONS)
3403 if (get_application_target_sdk_version() != __ANDROID_API__
3404 && get_application_target_sdk_version() >= __ANDROID_API_M__) {
3405#else
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003406 if (get_application_target_sdk_version() >= __ANDROID_API_M__) {
Adrian DCbf3c0f42016-08-27 15:09:36 +02003407#endif
Elliott Hughes763f6e12017-04-10 09:52:33 -07003408 DL_ERR_AND_LOG("\"%s\" has text relocations (https://android.googlesource.com/platform/"
3409 "bionic/+/master/android-changes-for-ndk-developers.md#Text-Relocations-"
3410 "Enforced-for-API-level-23)", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003411 return false;
3412 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003413 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003414 // phdr_table_protect_segments() after all of them are applied.
Elliott Hughes763f6e12017-04-10 09:52:33 -07003415 DL_WARN("\"%s\" has text relocations (https://android.googlesource.com/platform/"
3416 "bionic/+/master/android-changes-for-ndk-developers.md#Text-Relocations-Enforced-"
3417 "for-API-level-23)", get_realpath());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003418 add_dlwarning(get_realpath(), "text relocations");
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003419 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3420 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003421 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003422 return false;
3423 }
3424 }
3425#endif
3426
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003427 if (android_relocs_ != nullptr) {
3428 // check signature
3429 if (android_relocs_size_ > 3 &&
3430 android_relocs_[0] == 'A' &&
3431 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003432 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003433 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003434 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003435
3436 bool relocated = false;
3437 const uint8_t* packed_relocs = android_relocs_ + 4;
3438 const size_t packed_relocs_size = android_relocs_size_ - 4;
3439
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003440 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003441 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003442 packed_reloc_iterator<sleb128_decoder>(
3443 sleb128_decoder(packed_relocs, packed_relocs_size)),
3444 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003445
3446 if (!relocated) {
3447 return false;
3448 }
3449 } else {
3450 DL_ERR("bad android relocation header.");
3451 return false;
3452 }
3453 }
3454
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003455#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003456 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003457 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003458 if (!relocate(version_tracker,
3459 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003460 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003461 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003462 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003463 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003464 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003465 if (!relocate(version_tracker,
3466 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003467 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003468 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003469 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003470#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003471 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003472 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003473 if (!relocate(version_tracker,
3474 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003475 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003476 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003477 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003478 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003479 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003480 if (!relocate(version_tracker,
3481 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003482 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003483 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003484 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003485#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003486
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003487#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003488 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003489 return false;
3490 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003491#endif
3492
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003493 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003494
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003495#if !defined(__LP64__)
3496 if (has_text_relocations) {
3497 // All relocations are done, we can protect our segments back to read-only.
3498 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3499 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003500 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003501 return false;
3502 }
3503 }
3504#endif
3505
Mingwei Shibe910522015-11-12 07:02:14 +00003506 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
3507 // itself --- it can't make system calls yet, and will have to call protect_relro later.
3508 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003509 return false;
3510 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003511
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003512 /* Handle serializing/sharing the RELRO segment */
3513 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3514 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3515 extinfo->relro_fd) < 0) {
3516 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003517 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003518 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003519 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003520 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3521 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3522 extinfo->relro_fd) < 0) {
3523 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003524 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003525 return false;
3526 }
3527 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003528
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003529 notify_gdb_of_load(this);
3530 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003531}
3532
Mingwei Shibe910522015-11-12 07:02:14 +00003533bool soinfo::protect_relro() {
3534 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3535 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
3536 get_realpath(), strerror(errno));
3537 return false;
3538 }
3539 return true;
3540}
3541
Jiyong Park34a4acd2017-05-20 01:01:24 +09003542static std::vector<android_namespace_t*> init_default_namespace_no_config(bool is_asan) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003543 g_default_namespace.set_isolated(false);
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003544 auto default_ld_paths = is_asan ? kAsanDefaultLdPaths : kDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003545
neo.chae2589f9d2016-10-04 11:00:27 +09003546 char real_path[PATH_MAX];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003547 std::vector<std::string> ld_default_paths;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003548 for (size_t i = 0; default_ld_paths[i] != nullptr; ++i) {
3549 if (realpath(default_ld_paths[i], real_path) != nullptr) {
neo.chae2589f9d2016-10-04 11:00:27 +09003550 ld_default_paths.push_back(real_path);
3551 } else {
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003552 ld_default_paths.push_back(default_ld_paths[i]);
neo.chae2589f9d2016-10-04 11:00:27 +09003553 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003554 }
3555
3556 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Jiyong Park34a4acd2017-05-20 01:01:24 +09003557
3558 std::vector<android_namespace_t*> namespaces;
3559 namespaces.push_back(&g_default_namespace);
3560 return namespaces;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003561}
3562
Jiyong Park34a4acd2017-05-20 01:01:24 +09003563std::vector<android_namespace_t*> init_default_namespaces(const char* executable_path) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003564 g_default_namespace.set_name("(default)");
3565
3566 soinfo* somain = solist_get_somain();
3567
3568 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3569 somain->load_bias);
3570 const char* bname = basename(interp);
3571
3572 g_is_asan = bname != nullptr &&
3573 (strcmp(bname, "linker_asan") == 0 ||
3574 strcmp(bname, "linker_asan64") == 0);
3575
3576 const Config* config = nullptr;
3577
3578 std::string error_msg;
3579
Jiyong Park34a4acd2017-05-20 01:01:24 +09003580 const char* config_file = kLdConfigFilePath;
3581#ifdef USE_LD_CONFIG_FILE
3582 // This is a debugging/testing only feature. Must not be available on
3583 // production builds.
3584 const char* ld_config_file = getenv("LD_CONFIG_FILE");
3585 if (ld_config_file != nullptr && file_exists(ld_config_file)) {
3586 config_file = ld_config_file;
3587 }
3588#endif
3589
3590 if (!Config::read_binary_config(config_file,
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003591 executable_path,
3592 g_is_asan,
3593 &config,
3594 &error_msg)) {
3595 if (!error_msg.empty()) {
3596 DL_WARN("error reading config file \"%s\" for \"%s\" (will use default configuration): %s",
Jiyong Park34a4acd2017-05-20 01:01:24 +09003597 config_file,
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003598 executable_path,
3599 error_msg.c_str());
3600 }
3601 config = nullptr;
3602 }
3603
3604 if (config == nullptr) {
Jiyong Park34a4acd2017-05-20 01:01:24 +09003605 return init_default_namespace_no_config(g_is_asan);
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003606 }
3607
3608 const auto& namespace_configs = config->namespace_configs();
3609 std::unordered_map<std::string, android_namespace_t*> namespaces;
3610
3611 // 1. Initialize default namespace
3612 const NamespaceConfig* default_ns_config = config->default_namespace_config();
3613
3614 g_default_namespace.set_isolated(default_ns_config->isolated());
3615 g_default_namespace.set_default_library_paths(default_ns_config->search_paths());
3616 g_default_namespace.set_permitted_paths(default_ns_config->permitted_paths());
3617
3618 namespaces[default_ns_config->name()] = &g_default_namespace;
Justin Yun6cd39892017-07-07 16:21:53 +09003619 if (default_ns_config->visible()) {
3620 g_exported_namespaces[default_ns_config->name()] = &g_default_namespace;
3621 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003622
3623 // 2. Initialize other namespaces
3624
3625 for (auto& ns_config : namespace_configs) {
3626 if (namespaces.find(ns_config->name()) != namespaces.end()) {
3627 continue;
3628 }
3629
3630 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
3631 ns->set_name(ns_config->name());
3632 ns->set_isolated(ns_config->isolated());
3633 ns->set_default_library_paths(ns_config->search_paths());
3634 ns->set_permitted_paths(ns_config->permitted_paths());
3635
3636 namespaces[ns_config->name()] = ns;
Jiyong Park01de74e2017-04-03 23:10:37 +09003637 if (ns_config->visible()) {
3638 g_exported_namespaces[ns_config->name()] = ns;
3639 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003640 }
3641
3642 // 3. Establish links between namespaces
3643 for (auto& ns_config : namespace_configs) {
3644 auto it_from = namespaces.find(ns_config->name());
3645 CHECK(it_from != namespaces.end());
3646 android_namespace_t* namespace_from = it_from->second;
3647 for (const NamespaceLinkConfig& ns_link : ns_config->links()) {
3648 auto it_to = namespaces.find(ns_link.ns_name());
3649 CHECK(it_to != namespaces.end());
3650 android_namespace_t* namespace_to = it_to->second;
3651 link_namespaces(namespace_from, namespace_to, ns_link.shared_libs().c_str());
3652 }
3653 }
3654 // we can no longer rely on the fact that libdl.so is part of default namespace
3655 // this is why we want to add ld-android.so to all namespaces from ld.config.txt
3656 soinfo* ld_android_so = solist_get_head();
3657 for (auto it : namespaces) {
3658 it.second->add_soinfo(ld_android_so);
Jiyong Park34a4acd2017-05-20 01:01:24 +09003659 // somain and ld_preloads are added to these namespaces after LD_PRELOAD libs are linked
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003660 }
3661
3662 set_application_target_sdk_version(config->target_sdk_version());
Jiyong Park34a4acd2017-05-20 01:01:24 +09003663
3664 std::vector<android_namespace_t*> created_namespaces;
3665 created_namespaces.reserve(namespaces.size());
3666 for (auto kv : namespaces) {
3667 created_namespaces.push_back(kv.second);
3668 }
3669 return created_namespaces;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003670}
Jiyong Park01de74e2017-04-03 23:10:37 +09003671
3672// This function finds a namespace exported in ld.config.txt by its name.
3673// A namespace can be exported by setting .visible property to true.
3674android_namespace_t* get_exported_namespace(const char* name) {
3675 if (name == nullptr) {
3676 return nullptr;
3677 }
3678 auto it = g_exported_namespaces.find(std::string(name));
3679 if (it == g_exported_namespaces.end()) {
3680 return nullptr;
3681 }
3682 return it->second;
3683}