blob: 3560b4e631a43e7e3643fc1fb3b0ebd3a869620a [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
Logan Chienbcfe3cf2018-05-08 20:17:26 +080047#include <android-base/properties.h>
Tom Cherryb8ab6182017-04-05 16:20:29 -070048#include <android-base/scopeguard.h>
49
Christopher Ferris7a3681e2017-04-24 17:48:32 -070050#include <async_safe/log.h>
51
Elliott Hughes46882792012-08-03 16:49:39 -070052// Private C library headers.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053
54#include "linker.h"
Dmitriy Ivanovc9ce70d2015-03-10 15:30:26 -070055#include "linker_block_allocator.h"
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070056#include "linker_cfi.h"
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080057#include "linker_config.h"
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -080058#include "linker_gdb_support.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070059#include "linker_globals.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060#include "linker_debug.h"
Dimitry Ivanov769b33f2016-07-21 11:33:40 -070061#include "linker_dlwarning.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070062#include "linker_main.h"
Dimitry Ivanovb943f302016-08-03 16:00:10 -070063#include "linker_namespaces.h"
Dmitriy Ivanov18870d32015-04-22 13:10:04 -070064#include "linker_sleb128.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020065#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080066#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080067#include "linker_reloc_iterators.h"
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070068#include "linker_utils.h"
tony.ys_liub4474402015-07-29 18:00:22 +080069
dimitryfe1b27c2017-08-11 14:43:21 +020070#include "android-base/macros.h"
Elliott Hughes939a7e02015-12-04 15:27:46 -080071#include "android-base/strings.h"
Dimitry Ivanovb996d602016-07-11 18:11:39 -070072#include "android-base/stringprintf.h"
Simon Baldwinaef71952015-01-16 13:22:54 +000073#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074
dimitry06016f22018-01-05 11:39:28 +010075static std::unordered_map<void*, size_t> g_dso_handle_counters;
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
dimitryfe1b27c2017-08-11 14:43:21 +020086static const char* const kLdConfigArchFilePath = "/system/etc/ld.config." ABI_STRING ".txt";
87
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080088static const char* const kLdConfigFilePath = "/system/etc/ld.config.txt";
Logan Chienbcfe3cf2018-05-08 20:17:26 +080089static const char* const kLdConfigVndkLiteFilePath = "/system/etc/ld.config.vndk_lite.txt";
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080090
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070091#if defined(__LP64__)
Roland Levillaind2dd3792018-10-19 13:57:52 +010092static const char* const kArtApexLibDir = "/apex/com.android.runtime/lib64";
Dimitry Ivanova1446972017-03-17 00:08:23 +000093static const char* const kSystemLibDir = "/system/lib64";
Alin Jerpelea074a9fd2017-09-25 17:47:49 +020094static const char* const kOdmLibDir = "/odm/lib64";
Dimitry Ivanova1446972017-03-17 00:08:23 +000095static const char* const kVendorLibDir = "/vendor/lib64";
Vishwath Mohan4113def2017-03-29 15:31:34 -070096static const char* const kAsanSystemLibDir = "/data/asan/system/lib64";
Alin Jerpelea074a9fd2017-09-25 17:47:49 +020097static const char* const kAsanOdmLibDir = "/data/asan/odm/lib64";
Vishwath Mohan4113def2017-03-29 15:31:34 -070098static const char* const kAsanVendorLibDir = "/data/asan/vendor/lib64";
Elliott Hughes011bc0b2013-10-08 14:27:10 -070099#else
Roland Levillaind2dd3792018-10-19 13:57:52 +0100100static const char* const kArtApexLibDir = "/apex/com.android.runtime/lib";
Dimitry Ivanova1446972017-03-17 00:08:23 +0000101static const char* const kSystemLibDir = "/system/lib";
Alin Jerpelea074a9fd2017-09-25 17:47:49 +0200102static const char* const kOdmLibDir = "/odm/lib";
Dimitry Ivanova1446972017-03-17 00:08:23 +0000103static const char* const kVendorLibDir = "/vendor/lib";
Vishwath Mohan4113def2017-03-29 15:31:34 -0700104static const char* const kAsanSystemLibDir = "/data/asan/system/lib";
Alin Jerpelea074a9fd2017-09-25 17:47:49 +0200105static const char* const kAsanOdmLibDir = "/data/asan/odm/lib";
Vishwath Mohan4113def2017-03-29 15:31:34 -0700106static const char* const kAsanVendorLibDir = "/data/asan/vendor/lib";
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700107#endif
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700108
Vishwath Mohan4113def2017-03-29 15:31:34 -0700109static const char* const kAsanLibDirPrefix = "/data/asan";
110
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700111static const char* const kDefaultLdPaths[] = {
Roland Levillaind2dd3792018-10-19 13:57:52 +0100112 kArtApexLibDir,
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700113 kSystemLibDir,
Alin Jerpelea074a9fd2017-09-25 17:47:49 +0200114 kOdmLibDir,
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700115 kVendorLibDir,
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700116 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700117};
David Bartleybc3a5c22009-06-02 18:27:28 -0700118
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700119static const char* const kAsanDefaultLdPaths[] = {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700120 kAsanSystemLibDir,
Roland Levillaind2dd3792018-10-19 13:57:52 +0100121 kArtApexLibDir,
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700122 kSystemLibDir,
Alin Jerpelea074a9fd2017-09-25 17:47:49 +0200123 kAsanOdmLibDir,
124 kOdmLibDir,
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700125 kAsanVendorLibDir,
126 kVendorLibDir,
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700127 nullptr
128};
129
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700130// Is ASAN enabled?
131static bool g_is_asan = false;
132
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700133static CFIShadowWriter g_cfi_shadow;
134
135CFIShadowWriter* get_cfi_shadow() {
136 return &g_cfi_shadow;
137}
138
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700139static bool is_system_library(const std::string& realpath) {
140 for (const auto& dir : g_default_namespace.get_default_library_paths()) {
141 if (file_is_in_dir(realpath, dir)) {
142 return true;
143 }
144 }
145 return false;
146}
147
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700148// Checks if the file exists and not a directory.
149static bool file_exists(const char* path) {
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700150 struct stat s;
151
152 if (stat(path, &s) != 0) {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700153 return false;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700154 }
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700155
156 return S_ISREG(s.st_mode);
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700157}
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700158
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800159static std::string resolve_soname(const std::string& name) {
160 // We assume that soname equals to basename here
161
162 // TODO(dimitry): consider having honest absolute-path -> soname resolution
163 // note that since we might end up refusing to load this library because
164 // it is not in shared libs list we need to get the soname without actually loading
165 // the library.
166 //
167 // On the other hand there are several places where we already assume that
168 // soname == basename in particular for any not-loaded library mentioned
169 // in DT_NEEDED list.
170 return basename(name.c_str());
171}
172
173static bool maybe_accessible_via_namespace_links(android_namespace_t* ns, const char* name) {
174 std::string soname = resolve_soname(name);
175 for (auto& ns_link : ns->linked_namespaces()) {
176 if (ns_link.is_accessible(soname.c_str())) {
177 return true;
178 }
179 }
180
181 return false;
182}
183
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700184// TODO(dimitry): The grey-list is a workaround for http://b/26394120 ---
185// gradually remove libraries from this list until it is gone.
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800186static bool is_greylisted(android_namespace_t* ns, const char* name, const soinfo* needed_by) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700187 static const char* const kLibraryGreyList[] = {
188 "libandroid_runtime.so",
189 "libbinder.so",
190 "libcrypto.so",
191 "libcutils.so",
192 "libexpat.so",
193 "libgui.so",
194 "libmedia.so",
195 "libnativehelper.so",
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700196 "libssl.so",
197 "libstagefright.so",
198 "libsqlite.so",
199 "libui.so",
200 "libutils.so",
201 "libvorbisidec.so",
202 nullptr
203 };
204
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800205 // If you're targeting N, you don't get the greylist.
Elliott Hughes9e27e582017-03-23 17:42:49 -0700206 if (g_greylist_disabled || get_application_target_sdk_version() >= __ANDROID_API_N__) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700207 return false;
208 }
209
210 // if the library needed by a system library - implicitly assume it
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800211 // is greylisted unless it is in the list of shared libraries for one or
212 // more linked namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700213 if (needed_by != nullptr && is_system_library(needed_by->get_realpath())) {
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800214 return !maybe_accessible_via_namespace_links(ns, name);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700215 }
216
217 // if this is an absolute path - make sure it points to /system/lib(64)
218 if (name[0] == '/' && dirname(name) == kSystemLibDir) {
219 // and reduce the path to basename
220 name = basename(name);
221 }
222
223 for (size_t i = 0; kLibraryGreyList[i] != nullptr; ++i) {
224 if (strcmp(name, kLibraryGreyList[i]) == 0) {
225 return true;
226 }
227 }
228
229 return false;
230}
231// END OF WORKAROUND
232
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700233static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800234
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800235static bool g_anonymous_namespace_initialized;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700236
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700238struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700239 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700240};
241
242static linker_stats_t linker_stats;
243
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800244void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700245 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700246}
247#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800248void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700249}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800250#endif
251
252#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800253uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800254#endif
255
Elliott Hughesbedfe382012-08-14 14:07:59 -0700256static void notify_gdb_of_load(soinfo* info) {
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800257 if (info->is_linker() || info->is_main_executable()) {
258 // gdb already knows about the linker and the main executable.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700259 return;
260 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800261
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800262 link_map* map = &(info->link_map_head);
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000263
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800264 map->l_addr = info->load_bias;
265 // link_map l_name field is not const.
266 map->l_name = const_cast<char*>(info->get_realpath());
267 map->l_ld = info->dynamic;
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000268
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800269 CHECK(map->l_name != nullptr);
270 CHECK(map->l_name[0] != '\0');
271
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800272 notify_gdb_of_load(map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700273}
274
Elliott Hughesbedfe382012-08-14 14:07:59 -0700275static void notify_gdb_of_unload(soinfo* info) {
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800276 notify_gdb_of_unload(&(info->link_map_head));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800277}
278
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700279LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
280 return g_soinfo_links_allocator.alloc();
281}
282
283void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
284 g_soinfo_links_allocator.free(entry);
285}
286
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700287LinkedListEntry<android_namespace_t>* NamespaceListAllocator::alloc() {
288 return g_namespace_list_allocator.alloc();
289}
290
291void NamespaceListAllocator::free(LinkedListEntry<android_namespace_t>* entry) {
292 g_namespace_list_allocator.free(entry);
293}
294
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700295soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
Ryan Prichard8f639a42018-10-01 23:10:05 -0700296 const struct stat* file_stat, off64_t file_offset,
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700297 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700298 if (strlen(name) >= PATH_MAX) {
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700299 async_safe_fatal("library name \"%s\" too long", name);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200300 }
301
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700302 TRACE("name %s: allocating soinfo for ns=%p", name, ns);
303
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700304 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
305 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700306
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700307 solist_add_soinfo(si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200308
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700309 si->generate_handle();
310 ns->add_soinfo(si);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700311
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700312 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200313 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800314}
315
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800316static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700317 if (si == nullptr) {
318 return;
319 }
320
321 if (si->base != 0 && si->size != 0) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800322 if (!si->is_mapped_by_caller()) {
323 munmap(reinterpret_cast<void*>(si->base), si->size);
324 } else {
325 // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE
326 mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE,
327 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
328 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700329 }
330
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700331 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700332
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700333 if (!solist_remove_soinfo(si)) {
dimitry965d06d2017-11-28 16:03:07 +0100334 async_safe_fatal("soinfo=%p is not in soinfo_list (double unload?)", si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700335 }
Elliott Hughes46882792012-08-03 16:49:39 -0700336
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700337 // clear links to/from si
338 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700339
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700340 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700341 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800342}
343
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700344static void parse_path(const char* path, const char* delimiters,
345 std::vector<std::string>* resolved_paths) {
346 std::vector<std::string> paths;
347 split_path(path, delimiters, &paths);
348 resolve_paths(paths, resolved_paths);
349}
350
Elliott Hughescade4c32012-12-20 14:42:14 -0800351static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700352 std::vector<std::string> ld_libary_paths;
353 parse_path(path, ":", &ld_libary_paths);
354 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800355}
356
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700357static bool realpath_fd(int fd, std::string* realpath) {
358 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700359 async_safe_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700360 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700361 PRINT("readlink(\"%s\") failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700362 return false;
363 }
364
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700365 *realpath = &buf[0];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700366 return true;
367}
368
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700369#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700370
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700371// For a given PC, find the .so that it belongs to.
372// Returns the base address of the .ARM.exidx section
373// for that .so, and the number of 8-byte entries
374// in that section (via *pcount).
375//
376// Intended to be called by libc's __gnu_Unwind_Find_exidx().
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800377_Unwind_Ptr do_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700378 for (soinfo* si = solist_get_head(); si != 0; si = si->next) {
Elliott Hughesf2c6ad62017-04-21 10:25:56 -0700379 if ((pc >= si->base) && (pc < (si->base + si->size))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700380 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800381 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800382 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700383 }
384 *pcount = 0;
Elliott Hughesf2c6ad62017-04-21 10:25:56 -0700385 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800386}
Elliott Hughes46882792012-08-03 16:49:39 -0700387
Christopher Ferris24053a42013-08-19 17:45:09 -0700388#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700389
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700390// Here, we only have to provide a callback to iterate across all the
391// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700392int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700393 int rv = 0;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700394 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700395 dl_phdr_info dl_info;
396 dl_info.dlpi_addr = si->link_map_head.l_addr;
397 dl_info.dlpi_name = si->link_map_head.l_name;
398 dl_info.dlpi_phdr = si->phdr;
399 dl_info.dlpi_phnum = si->phnum;
400 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
401 if (rv != 0) {
402 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800403 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700404 }
405 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800406}
Elliott Hughes46882792012-08-03 16:49:39 -0700407
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800408
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700409bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700410 soinfo** si_found_in, const soinfo_list_t& global_group,
411 const soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800412 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700413 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700414
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700415 /* "This element's presence in a shared object library alters the dynamic linker's
416 * symbol resolution algorithm for references within the library. Instead of starting
417 * a symbol search with the executable file, the dynamic linker starts from the shared
418 * object itself. If the shared object fails to supply the referenced symbol, the
419 * dynamic linker then searches the executable file and other shared objects as usual."
420 *
421 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
422 *
423 * Note that this is unlikely since static linker avoids generating
424 * relocations for -Bsymbolic linked dynamic executables.
425 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700426 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700427 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700428 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
429 return false;
430 }
431
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700432 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700433 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700434 }
435 }
436
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700437 // 1. Look for it in global_group
438 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700439 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700440 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700441 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700442 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700443 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
444 error = true;
445 return false;
446 }
447
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700448 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700449 *si_found_in = global_si;
450 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700451 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700452
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700453 return true;
454 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700455
456 if (error) {
457 return false;
458 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700459 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700460
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700461 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700462 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700463 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700464 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700465 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700466 // we already did this - skip
467 return true;
468 }
469
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700470 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700471 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700472 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
473 error = true;
474 return false;
475 }
476
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700477 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700478 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700479 return false;
480 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700481
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700482 return true;
483 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700484
485 if (error) {
486 return false;
487 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700488 }
489
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700490 if (s != nullptr) {
491 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
492 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700493 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
494 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700495 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700496 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700497
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700498 *symbol = s;
499 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700500}
501
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700502ProtectedDataGuard::ProtectedDataGuard() {
503 if (ref_count_++ == 0) {
504 protect_data(PROT_READ | PROT_WRITE);
505 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700506
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700507 if (ref_count_ == 0) { // overflow
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700508 async_safe_fatal("Too many nested calls to dlopen()");
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800509 }
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800510}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800511
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800512ProtectedDataGuard::~ProtectedDataGuard() {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700513 if (--ref_count_ == 0) {
514 protect_data(PROT_READ);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800515 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700516}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800517
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700518void ProtectedDataGuard::protect_data(int protection) {
519 g_soinfo_allocator.protect_all(protection);
520 g_soinfo_links_allocator.protect_all(protection);
521 g_namespace_allocator.protect_all(protection);
522 g_namespace_list_allocator.protect_all(protection);
523}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800524
525size_t ProtectedDataGuard::ref_count_ = 0;
526
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700527// Each size has it's own allocator.
528template<size_t size>
529class SizeBasedAllocator {
530 public:
531 static void* alloc() {
532 return allocator_.alloc();
533 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700534
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700535 static void free(void* ptr) {
536 allocator_.free(ptr);
537 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700538
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700539 private:
540 static LinkerBlockAllocator allocator_;
541};
542
543template<size_t size>
544LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
545
546template<typename T>
547class TypeBasedAllocator {
548 public:
549 static T* alloc() {
550 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
551 }
552
553 static void free(T* ptr) {
554 SizeBasedAllocator<sizeof(T)>::free(ptr);
555 }
556};
557
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700558class LoadTask {
559 public:
560 struct deleter_t {
561 void operator()(LoadTask* t) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700562 t->~LoadTask();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700563 TypeBasedAllocator<LoadTask>::free(t);
564 }
565 };
566
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700567 static deleter_t deleter;
568
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800569 static LoadTask* create(const char* name,
570 soinfo* needed_by,
Jiyong Park02586a22017-05-20 01:01:24 +0900571 android_namespace_t* start_from,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700572 std::unordered_map<const soinfo*, ElfReader>* readers_map) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700573 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
Jiyong Park02586a22017-05-20 01:01:24 +0900574 return new (ptr) LoadTask(name, needed_by, start_from, readers_map);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700575 }
576
577 const char* get_name() const {
578 return name_;
579 }
580
581 soinfo* get_needed_by() const {
582 return needed_by_;
583 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700584
585 soinfo* get_soinfo() const {
586 return si_;
587 }
588
589 void set_soinfo(soinfo* si) {
590 si_ = si;
591 }
592
593 off64_t get_file_offset() const {
594 return file_offset_;
595 }
596
597 void set_file_offset(off64_t offset) {
598 file_offset_ = offset;
599 }
600
601 int get_fd() const {
602 return fd_;
603 }
604
605 void set_fd(int fd, bool assume_ownership) {
606 fd_ = fd;
607 close_fd_ = assume_ownership;
608 }
609
610 const android_dlextinfo* get_extinfo() const {
611 return extinfo_;
612 }
613
614 void set_extinfo(const android_dlextinfo* extinfo) {
615 extinfo_ = extinfo;
616 }
617
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700618 bool is_dt_needed() const {
619 return is_dt_needed_;
620 }
621
622 void set_dt_needed(bool is_dt_needed) {
623 is_dt_needed_ = is_dt_needed;
624 }
625
Jiyong Park02586a22017-05-20 01:01:24 +0900626 // returns the namespace from where we need to start loading this.
627 const android_namespace_t* get_start_from() const {
628 return start_from_;
629 }
630
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700631 const ElfReader& get_elf_reader() const {
632 CHECK(si_ != nullptr);
633 return (*elf_readers_map_)[si_];
634 }
635
636 ElfReader& get_elf_reader() {
637 CHECK(si_ != nullptr);
638 return (*elf_readers_map_)[si_];
639 }
640
641 std::unordered_map<const soinfo*, ElfReader>* get_readers_map() {
642 return elf_readers_map_;
643 }
644
645 bool read(const char* realpath, off64_t file_size) {
646 ElfReader& elf_reader = get_elf_reader();
647 return elf_reader.Read(realpath, fd_, file_offset_, file_size);
648 }
649
650 bool load() {
651 ElfReader& elf_reader = get_elf_reader();
652 if (!elf_reader.Load(extinfo_)) {
653 return false;
654 }
655
656 si_->base = elf_reader.load_start();
657 si_->size = elf_reader.load_size();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800658 si_->set_mapped_by_caller(elf_reader.is_mapped_by_caller());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700659 si_->load_bias = elf_reader.load_bias();
660 si_->phnum = elf_reader.phdr_count();
661 si_->phdr = elf_reader.loaded_phdr();
662
663 return true;
664 }
665
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700666 private:
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800667 LoadTask(const char* name,
668 soinfo* needed_by,
Jiyong Park02586a22017-05-20 01:01:24 +0900669 android_namespace_t* start_from,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700670 std::unordered_map<const soinfo*, ElfReader>* readers_map)
671 : name_(name), needed_by_(needed_by), si_(nullptr),
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700672 fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map),
Jiyong Park02586a22017-05-20 01:01:24 +0900673 is_dt_needed_(false), start_from_(start_from) {}
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700674
675 ~LoadTask() {
676 if (fd_ != -1 && close_fd_) {
677 close(fd_);
678 }
679 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700680
681 const char* name_;
682 soinfo* needed_by_;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700683 soinfo* si_;
684 const android_dlextinfo* extinfo_;
685 int fd_;
686 bool close_fd_;
687 off64_t file_offset_;
688 std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_;
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700689 // TODO(dimitry): needed by workaround for http://b/26394120 (the grey-list)
690 bool is_dt_needed_;
691 // END OF WORKAROUND
Jiyong Park02586a22017-05-20 01:01:24 +0900692 const android_namespace_t* const start_from_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700693
694 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
695};
696
Ningsheng Jiane93be992014-09-16 15:22:10 +0800697LoadTask::deleter_t LoadTask::deleter;
698
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700699template <typename T>
700using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
701
702typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700703typedef linked_list_t<const char> StringLinkedList;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700704typedef std::vector<LoadTask*> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700705
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800706enum walk_action_result_t : uint32_t {
707 kWalkStop = 0,
708 kWalkContinue = 1,
709 kWalkSkip = 2
710};
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700711
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700712// This function walks down the tree of soinfo dependencies
713// in breadth-first order and
714// * calls action(soinfo* si) for each node, and
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800715// * terminates walk if action returns kWalkStop
716// * skips children of the node if action
717// return kWalkSkip
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700718//
719// walk_dependencies_tree returns false if walk was terminated
720// by the action and true otherwise.
721template<typename F>
dimitry965d06d2017-11-28 16:03:07 +0100722static bool walk_dependencies_tree(soinfo* root_soinfo, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700723 SoinfoLinkedList visit_list;
724 SoinfoLinkedList visited;
725
dimitry965d06d2017-11-28 16:03:07 +0100726 visit_list.push_back(root_soinfo);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700727
728 soinfo* si;
729 while ((si = visit_list.pop_front()) != nullptr) {
730 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700731 continue;
732 }
733
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800734 walk_action_result_t result = action(si);
735
736 if (result == kWalkStop) {
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700737 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700738 }
739
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700740 visited.push_back(si);
741
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800742 if (result != kWalkSkip) {
743 si->get_children().for_each([&](soinfo* child) {
744 visit_list.push_back(child);
745 });
746 }
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700747 }
748
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700749 return true;
750}
751
752
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800753static const ElfW(Sym)* dlsym_handle_lookup(android_namespace_t* ns,
754 soinfo* root,
755 soinfo* skip_until,
756 soinfo** found,
757 SymbolName& symbol_name,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800758 const version_info* vi) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700759 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700760 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700761
dimitry965d06d2017-11-28 16:03:07 +0100762 walk_dependencies_tree(root, [&](soinfo* current_soinfo) {
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700763 if (skip_lookup) {
764 skip_lookup = current_soinfo != skip_until;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800765 return kWalkContinue;
766 }
767
768 if (!ns->is_accessible(current_soinfo)) {
769 return kWalkSkip;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700770 }
771
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800772 if (!current_soinfo->find_symbol_by_name(symbol_name, vi, &result)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700773 result = nullptr;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800774 return kWalkStop;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700775 }
776
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700777 if (result != nullptr) {
778 *found = current_soinfo;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800779 return kWalkStop;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700780 }
781
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800782 return kWalkContinue;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700783 });
784
785 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800786}
787
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800788static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
789 const char* name,
790 const version_info* vi,
791 soinfo** found,
792 soinfo* caller,
793 void* handle);
794
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700795// This is used by dlsym(3). It performs symbol lookup only within the
796// specified soinfo object and its dependencies in breadth first order.
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800797static const ElfW(Sym)* dlsym_handle_lookup(soinfo* si,
798 soinfo** found,
799 const char* name,
800 const version_info* vi) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700801 // According to man dlopen(3) and posix docs in the case when si is handle
802 // of the main executable we need to search not only in the executable and its
803 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
804 //
805 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
806 // libraries and they are loaded in breath-first (correct) order we can just execute
807 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700808 if (si == solist_get_somain()) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800809 return dlsym_linear_lookup(&g_default_namespace, name, vi, found, nullptr, RTLD_DEFAULT);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700810 }
811
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700812 SymbolName symbol_name(name);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800813 // note that the namespace is not the namespace associated with caller_addr
814 // we use ns associated with root si intentionally here. Using caller_ns
815 // causes problems when user uses dlopen_ext to open a library in the separate
816 // namespace and then calls dlsym() on the handle.
817 return dlsym_handle_lookup(si->get_primary_namespace(), si, nullptr, found, symbol_name, vi);
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700818}
819
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800820/* This is used by dlsym(3) to performs a global symbol lookup. If the
821 start value is null (for RTLD_DEFAULT), the search starts at the
822 beginning of the global solist. Otherwise the search starts at the
823 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700824 */
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800825static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
826 const char* name,
827 const version_info* vi,
828 soinfo** found,
829 soinfo* caller,
830 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800831 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800832
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700833 auto& soinfo_list = ns->soinfo_list();
834 auto start = soinfo_list.begin();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700835
836 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -0700837 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700838 return nullptr;
839 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700840 auto it = soinfo_list.find(caller);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700841 CHECK (it != soinfo_list.end());
842 start = ++it;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700843 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800844 }
845
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700846 const ElfW(Sym)* s = nullptr;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700847 for (auto it = start, end = soinfo_list.end(); it != end; ++it) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700848 soinfo* si = *it;
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700849 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800850 // if the library is opened by application with target api level < M.
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700851 // See http://b/21565766
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800852 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 &&
853 si->get_target_sdk_version() >= __ANDROID_API_M__) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700854 continue;
855 }
856
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800857 if (!si->find_symbol_by_name(symbol_name, vi, &s)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700858 return nullptr;
859 }
860
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700861 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800862 *found = si;
863 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600864 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800865 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600866
dimitry153168c2018-02-20 16:51:41 +0100867 // If not found - use dlsym_handle_lookup for caller's local_group
868 if (s == nullptr && caller != nullptr) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800869 soinfo* local_group_root = caller->get_local_group_root();
870
871 return dlsym_handle_lookup(local_group_root->get_primary_namespace(),
872 local_group_root,
873 (handle == RTLD_NEXT) ? caller : nullptr,
874 found,
875 symbol_name,
876 vi);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700877 }
878
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700879 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700880 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
881 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800882 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800883
Elliott Hughescade4c32012-12-20 14:42:14 -0800884 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800885}
886
Kito Chengfa8c05d2013-03-12 14:58:06 +0800887soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800888 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700889 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800890 if (address >= si->base && address - si->base < si->size) {
891 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600892 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800893 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700894 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600895}
896
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700897class ZipArchiveCache {
898 public:
899 ZipArchiveCache() {}
900 ~ZipArchiveCache();
901
902 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
903 private:
904 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
905
906 std::unordered_map<std::string, ZipArchiveHandle> cache_;
907};
908
909bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
910 std::string key(zip_path);
911
912 auto it = cache_.find(key);
913 if (it != cache_.end()) {
914 *handle = it->second;
915 return true;
916 }
917
918 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
919 if (fd == -1) {
920 return false;
921 }
922
923 if (OpenArchiveFd(fd, "", handle) != 0) {
924 // invalid zip-file (?)
Ryan Prichard0adf09b2018-10-01 18:35:46 -0700925 CloseArchive(*handle);
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700926 return false;
927 }
928
929 cache_[key] = *handle;
930 return true;
931}
932
933ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -0700934 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700935 CloseArchive(it.second);
936 }
937}
938
939static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700940 const char* const input_path,
941 off64_t* file_offset, std::string* realpath) {
942 std::string normalized_path;
943 if (!normalize_path(input_path, &normalized_path)) {
944 return -1;
945 }
946
947 const char* const path = normalized_path.c_str();
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700948 TRACE("Trying zip file open from path \"%s\" -> normalized \"%s\"", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +0000949
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700950 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +0000951 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700952 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +0000953 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700954 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +0000955 if (separator == nullptr) {
956 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -0700957 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000958
959 char buf[512];
960 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
961 PRINT("Warning: ignoring very long library path: %s", path);
962 return -1;
963 }
964
965 buf[separator - path] = '\0';
966
967 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700968 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +0000969 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
970 if (fd == -1) {
971 return -1;
972 }
973
974 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700975 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000976 // invalid zip-file (?)
977 close(fd);
978 return -1;
979 }
980
Simon Baldwinaef71952015-01-16 13:22:54 +0000981 ZipEntry entry;
982
Yusuke Sato56f40fb2015-06-25 14:56:07 -0700983 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000984 // Entry was not found.
985 close(fd);
986 return -1;
987 }
988
989 // Check if it is properly stored
990 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
991 close(fd);
992 return -1;
993 }
994
995 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700996
997 if (realpath_fd(fd, realpath)) {
998 *realpath += separator;
999 } else {
1000 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1001 normalized_path.c_str());
1002 *realpath = normalized_path;
1003 }
1004
Simon Baldwinaef71952015-01-16 13:22:54 +00001005 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001006}
1007
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001008static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001009 int n = async_safe_format_buffer(buf, buf_size, "%s/%s", path, name);
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001010 if (n < 0 || n >= static_cast<int>(buf_size)) {
1011 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1012 return false;
1013 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001014
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001015 return true;
1016}
1017
Ryan Prichard8f639a42018-10-01 23:10:05 -07001018static int open_library_at_path(ZipArchiveCache* zip_archive_cache,
1019 const char* path, off64_t* file_offset,
1020 std::string* realpath) {
1021 int fd = -1;
1022 if (strstr(path, kZipFileSeparator) != nullptr) {
1023 fd = open_library_in_zipfile(zip_archive_cache, path, file_offset, realpath);
1024 }
1025
1026 if (fd == -1) {
1027 fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC));
1028 if (fd != -1) {
1029 *file_offset = 0;
1030 if (!realpath_fd(fd, realpath)) {
1031 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", path);
1032 *realpath = path;
1033 }
1034 }
1035 }
1036
1037 return fd;
1038}
1039
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001040static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1041 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001042 const std::vector<std::string>& paths,
1043 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001044 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001045 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001046 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001047 continue;
1048 }
1049
Ryan Prichard8f639a42018-10-01 23:10:05 -07001050 int fd = open_library_at_path(zip_archive_cache, buf, file_offset, realpath);
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001051 if (fd != -1) {
1052 return fd;
1053 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001054 }
1055
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001056 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001057}
1058
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001059static int open_library(android_namespace_t* ns,
1060 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001061 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001062 off64_t* file_offset, std::string* realpath) {
Jiyong Park02586a22017-05-20 01:01:24 +09001063 TRACE("[ opening %s at namespace %s]", name, ns->get_name());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001064
Elliott Hughes124fae92012-10-31 14:20:03 -07001065 // 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 -07001066 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001067 int fd = -1;
1068
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001069 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001070 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1071 }
1072
1073 if (fd == -1) {
1074 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001075 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001076 *file_offset = 0;
1077 if (!realpath_fd(fd, realpath)) {
1078 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name);
1079 *realpath = name;
1080 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001081 }
1082 }
1083
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001084 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001085 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001086
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001087 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1088 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 -07001089 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001090 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001091 // Check if the library is accessible
1092 if (fd != -1 && !ns->is_accessible(*realpath)) {
Vic Yang48b69112018-10-24 14:14:26 -07001093 close(fd);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001094 fd = -1;
1095 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001096 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001097
Elliott Hughes124fae92012-10-31 14:20:03 -07001098 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001099 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001100 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001101
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001102 // TODO(dimitry): workaround for http://b/26394120 (the grey-list)
Jiyong Park37b91af2017-05-05 22:07:05 +09001103 if (fd == -1 && ns->is_greylist_enabled() && is_greylisted(ns, name, needed_by)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001104 // try searching for it on default_namespace default_library_path
1105 fd = open_library_on_paths(zip_archive_cache, name, file_offset,
1106 g_default_namespace.get_default_library_paths(), realpath);
1107 }
1108 // END OF WORKAROUND
1109
Elliott Hughes124fae92012-10-31 14:20:03 -07001110 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001111}
1112
Ryan Prichard8f639a42018-10-01 23:10:05 -07001113int open_executable(const char* path, off64_t* file_offset, std::string* realpath) {
1114 ZipArchiveCache zip_archive_cache;
1115 return open_library_at_path(&zip_archive_cache, path, file_offset, realpath);
1116}
1117
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001118const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001119#if !defined(__LP64__)
1120 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001121 int app_target_api_level = get_application_target_sdk_version();
1122 if (app_target_api_level < __ANDROID_API_M__) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001123 const char* bname = basename(dt_needed);
1124 if (bname != dt_needed) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001125 DL_WARN_documented_change(__ANDROID_API_M__,
1126 "invalid-dt_needed-entries-enforced-for-api-level-23",
1127 "library \"%s\" has invalid DT_NEEDED entry \"%s\"",
1128 sopath, dt_needed, app_target_api_level);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001129 add_dlwarning(sopath, "invalid DT_NEEDED entry", dt_needed);
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001130 }
1131
1132 return bname;
1133 }
1134#endif
1135 return dt_needed;
1136}
1137
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001138template<typename F>
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001139static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
1140 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1141 if (d->d_tag == DT_NEEDED) {
1142 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1143 }
1144 }
1145}
1146
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001147static bool find_loaded_library_by_inode(android_namespace_t* ns,
1148 const struct stat& file_stat,
1149 off64_t file_offset,
1150 bool search_linked_namespaces,
1151 soinfo** candidate) {
1152
1153 auto predicate = [&](soinfo* si) {
1154 return si->get_st_dev() != 0 &&
1155 si->get_st_ino() != 0 &&
1156 si->get_st_dev() == file_stat.st_dev &&
1157 si->get_st_ino() == file_stat.st_ino &&
1158 si->get_file_offset() == file_offset;
1159 };
1160
1161 *candidate = ns->soinfo_list().find_if(predicate);
1162
1163 if (*candidate == nullptr && search_linked_namespaces) {
1164 for (auto& link : ns->linked_namespaces()) {
1165 android_namespace_t* linked_ns = link.linked_namespace();
1166 soinfo* si = linked_ns->soinfo_list().find_if(predicate);
1167
1168 if (si != nullptr && link.is_accessible(si->get_soname())) {
1169 *candidate = si;
1170 return true;
1171 }
1172 }
1173 }
1174
1175 return *candidate != nullptr;
1176}
1177
Evgenii Stepanov9e77a642017-07-27 14:55:44 -07001178static bool find_loaded_library_by_realpath(android_namespace_t* ns, const char* realpath,
1179 bool search_linked_namespaces, soinfo** candidate) {
1180 auto predicate = [&](soinfo* si) { return strcmp(realpath, si->get_realpath()) == 0; };
1181
1182 *candidate = ns->soinfo_list().find_if(predicate);
1183
1184 if (*candidate == nullptr && search_linked_namespaces) {
1185 for (auto& link : ns->linked_namespaces()) {
1186 android_namespace_t* linked_ns = link.linked_namespace();
1187 soinfo* si = linked_ns->soinfo_list().find_if(predicate);
1188
1189 if (si != nullptr && link.is_accessible(si->get_soname())) {
1190 *candidate = si;
1191 return true;
1192 }
1193 }
1194 }
1195
1196 return *candidate != nullptr;
1197}
1198
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001199static bool load_library(android_namespace_t* ns,
1200 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001201 LoadTaskList* load_tasks,
1202 int rtld_flags,
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001203 const std::string& realpath,
1204 bool search_linked_namespaces) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001205 off64_t file_offset = task->get_file_offset();
1206 const char* name = task->get_name();
1207 const android_dlextinfo* extinfo = task->get_extinfo();
1208
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001209 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001210 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001211 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001212 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001213 if (file_offset < 0) {
1214 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001215 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001216 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001217
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001218 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001219 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001220 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001221 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001222 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001223 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001224 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1225 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001226 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001227 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001228
1229 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001230 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1231 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001232 soinfo* si = nullptr;
1233 if (find_loaded_library_by_inode(ns, file_stat, file_offset, search_linked_namespaces, &si)) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001234 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
1235 "will return existing soinfo", name, si->get_realpath());
1236 task->set_soinfo(si);
1237 return true;
1238 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001239 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001240
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001241 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001242 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001243 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001244 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001245
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -07001246 struct statfs fs_stat;
1247 if (TEMP_FAILURE_RETRY(fstatfs(task->get_fd(), &fs_stat)) != 0) {
1248 DL_ERR("unable to fstatfs file for the library \"%s\": %s", name, strerror(errno));
1249 return false;
1250 }
1251
1252 // do not check accessibility using realpath if fd is located on tmpfs
1253 // this enables use of memfd_create() for apps
1254 if ((fs_stat.f_type != TMPFS_MAGIC) && (!ns->is_accessible(realpath))) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001255 // TODO(dimitry): workaround for http://b/26394120 - the grey-list
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001256
1257 // TODO(dimitry) before O release: add a namespace attribute to have this enabled
1258 // only for classloader-namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001259 const soinfo* needed_by = task->is_dt_needed() ? task->get_needed_by() : nullptr;
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001260 if (is_greylisted(ns, name, needed_by)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001261 // print warning only if needed by non-system library
1262 if (needed_by == nullptr || !is_system_library(needed_by->get_realpath())) {
1263 const soinfo* needed_or_dlopened_by = task->get_needed_by();
1264 const char* sopath = needed_or_dlopened_by == nullptr ? "(unknown)" :
1265 needed_or_dlopened_by->get_realpath();
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001266 DL_WARN_documented_change(__ANDROID_API_N__,
1267 "private-api-enforced-for-api-level-24",
1268 "library \"%s\" (\"%s\") needed or dlopened by \"%s\" "
1269 "is not accessible by namespace \"%s\"",
1270 name, realpath.c_str(), sopath, ns->get_name());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001271 add_dlwarning(sopath, "unauthorized access to", name);
1272 }
1273 } else {
1274 // do not load libraries if they are not accessible for the specified namespace.
1275 const char* needed_or_dlopened_by = task->get_needed_by() == nullptr ?
1276 "(unknown)" :
1277 task->get_needed_by()->get_realpath();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001278
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001279 DL_ERR("library \"%s\" needed or dlopened by \"%s\" is not accessible for the namespace \"%s\"",
1280 name, needed_or_dlopened_by, ns->get_name());
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001281
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001282 // do not print this if a library is in the list of shared libraries for linked namespaces
1283 if (!maybe_accessible_via_namespace_links(ns, name)) {
1284 PRINT("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the"
1285 " namespace: [name=\"%s\", ld_library_paths=\"%s\", default_library_paths=\"%s\","
1286 " permitted_paths=\"%s\"]",
1287 name, realpath.c_str(),
1288 needed_or_dlopened_by,
1289 ns->get_name(),
1290 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
1291 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
1292 android::base::Join(ns->get_permitted_paths(), ':').c_str());
1293 }
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001294 return false;
1295 }
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001296 }
1297
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001298 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001299 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001300 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001301 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001302
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001303 task->set_soinfo(si);
1304
1305 // Read the ELF header and some of the segments.
1306 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001307 soinfo_free(si);
1308 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001309 return false;
1310 }
1311
1312 // find and set DT_RUNPATH and dt_soname
1313 // Note that these field values are temporary and are
1314 // going to be overwritten on soinfo::prelink_image
1315 // with values from PT_LOAD segments.
1316 const ElfReader& elf_reader = task->get_elf_reader();
1317 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1318 if (d->d_tag == DT_RUNPATH) {
1319 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1320 }
1321 if (d->d_tag == DT_SONAME) {
1322 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1323 }
1324 }
1325
Dan Willemsen5038ef62018-10-21 17:45:04 -07001326#if !defined(__ANDROID__)
1327 // Bionic on the host currently uses some Android prebuilts, which don't set
1328 // DT_RUNPATH with any relative paths, so they can't find their dependencies.
1329 // b/118058804
1330 if (si->get_dt_runpath().empty()) {
1331 si->set_dt_runpath("$ORIGIN/../lib64:$ORIGIN/lib64");
1332 }
1333#endif
1334
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001335 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
Jiyong Park02586a22017-05-20 01:01:24 +09001336 load_tasks->push_back(LoadTask::create(name, si, ns, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001337 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001338
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001339 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001340}
1341
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001342static bool load_library(android_namespace_t* ns,
1343 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001344 ZipArchiveCache* zip_archive_cache,
1345 LoadTaskList* load_tasks,
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001346 int rtld_flags,
1347 bool search_linked_namespaces) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001348 const char* name = task->get_name();
1349 soinfo* needed_by = task->get_needed_by();
1350 const android_dlextinfo* extinfo = task->get_extinfo();
1351
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001352 off64_t file_offset;
1353 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001354 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001355 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001356 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1357 file_offset = extinfo->library_fd_offset;
1358 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001359
1360 if (!realpath_fd(extinfo->library_fd, &realpath)) {
1361 PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1362 "Will use given name.", name);
1363 realpath = name;
1364 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001365
1366 task->set_fd(extinfo->library_fd, false);
1367 task->set_file_offset(file_offset);
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001368 return load_library(ns, task, load_tasks, rtld_flags, realpath, search_linked_namespaces);
Spencer Low0346ad72015-04-22 18:06:51 -07001369 }
1370
1371 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001372 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001373 if (fd == -1) {
1374 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001375 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001376 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001377
1378 task->set_fd(fd, true);
1379 task->set_file_offset(file_offset);
1380
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001381 return load_library(ns, task, load_tasks, rtld_flags, realpath, search_linked_namespaces);
Spencer Low0346ad72015-04-22 18:06:51 -07001382}
1383
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001384static bool find_loaded_library_by_soname(android_namespace_t* ns,
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001385 const char* name,
1386 soinfo** candidate) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001387 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001388 const char* soname = si->get_soname();
1389 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dimitry Ivanov3bd90612017-02-01 08:54:43 -08001390 *candidate = si;
1391 return false;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001392 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001393
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001394 return true;
1395 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001396}
1397
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001398// Returns true if library was found and false otherwise
1399static bool find_loaded_library_by_soname(android_namespace_t* ns,
1400 const char* name,
1401 bool search_linked_namespaces,
1402 soinfo** candidate) {
1403 *candidate = nullptr;
1404
1405 // Ignore filename with path.
1406 if (strchr(name, '/') != nullptr) {
1407 return false;
1408 }
1409
1410 bool found = find_loaded_library_by_soname(ns, name, candidate);
1411
1412 if (!found && search_linked_namespaces) {
1413 // if a library was not found - look into linked namespaces
1414 for (auto& link : ns->linked_namespaces()) {
1415 if (!link.is_accessible(name)) {
1416 continue;
1417 }
1418
1419 android_namespace_t* linked_ns = link.linked_namespace();
1420
1421 if (find_loaded_library_by_soname(linked_ns, name, candidate)) {
1422 return true;
1423 }
1424 }
1425 }
1426
1427 return found;
1428}
1429
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001430static bool find_library_in_linked_namespace(const android_namespace_link_t& namespace_link,
Jiyong Park02586a22017-05-20 01:01:24 +09001431 LoadTask* task) {
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001432 android_namespace_t* ns = namespace_link.linked_namespace();
1433
1434 soinfo* candidate;
1435 bool loaded = false;
1436
1437 std::string soname;
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001438 if (find_loaded_library_by_soname(ns, task->get_name(), false, &candidate)) {
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001439 loaded = true;
1440 soname = candidate->get_soname();
1441 } else {
1442 soname = resolve_soname(task->get_name());
1443 }
1444
1445 if (!namespace_link.is_accessible(soname.c_str())) {
1446 // the library is not accessible via namespace_link
1447 return false;
1448 }
1449
1450 // if library is already loaded - return it
1451 if (loaded) {
1452 task->set_soinfo(candidate);
1453 return true;
1454 }
1455
Jiyong Park02586a22017-05-20 01:01:24 +09001456 // returning true with empty soinfo means that the library is okay to be
Logan Chien9ee45912018-01-18 12:05:09 +08001457 // loaded in the namespace but has not yet been loaded there before.
Jiyong Park02586a22017-05-20 01:01:24 +09001458 task->set_soinfo(nullptr);
1459 return true;
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001460}
1461
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001462static bool find_library_internal(android_namespace_t* ns,
1463 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001464 ZipArchiveCache* zip_archive_cache,
1465 LoadTaskList* load_tasks,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001466 int rtld_flags,
1467 bool search_linked_namespaces) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001468 soinfo* candidate;
1469
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001470 if (find_loaded_library_by_soname(ns, task->get_name(), search_linked_namespaces, &candidate)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001471 task->set_soinfo(candidate);
1472 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001473 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001474
1475 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001476 // of this fact is done by load_library.
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001477 TRACE("[ \"%s\" find_loaded_library_by_soname failed (*candidate=%s@%p). Trying harder...]",
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001478 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001479
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001480 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags, search_linked_namespaces)) {
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001481 return true;
1482 }
1483
1484 if (search_linked_namespaces) {
1485 // if a library was not found - look into linked namespaces
dimitry8db36a52017-10-23 15:10:10 +02001486 // preserve current dlerror in the case it fails.
1487 DlErrorRestorer dlerror_restorer;
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001488 for (auto& linked_namespace : ns->linked_namespaces()) {
1489 if (find_library_in_linked_namespace(linked_namespace,
Jiyong Park02586a22017-05-20 01:01:24 +09001490 task)) {
1491 if (task->get_soinfo() == nullptr) {
1492 // try to load the library - once namespace boundary is crossed
1493 // we need to load a library within separate load_group
1494 // to avoid using symbols from foreign namespace while.
1495 //
1496 // However, actual linking is deferred until when the global group
1497 // is fully identified and is applied to all namespaces.
1498 // Otherwise, the libs in the linked namespace won't get symbols from
1499 // the global group.
1500 if (load_library(linked_namespace.linked_namespace(), task, zip_archive_cache, load_tasks, rtld_flags, false)) {
1501 return true;
1502 }
Jiyong Park02586a22017-05-20 01:01:24 +09001503 } else {
1504 // lib is already loaded
1505 return true;
1506 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001507 }
1508 }
1509 }
1510
1511 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001512}
1513
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001514static void soinfo_unload(soinfo* si);
1515
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001516static void shuffle(std::vector<LoadTask*>* v) {
Jiyong Park31cd08f2018-06-01 19:18:56 +09001517 if (is_init()) {
1518 // arc4random* is not available in init because /dev/random hasn't yet been
1519 // created.
1520 return;
1521 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001522 for (size_t i = 0, size = v->size(); i < size; ++i) {
1523 size_t n = size - i;
1524 size_t r = arc4random_uniform(n);
1525 std::swap((*v)[n-1], (*v)[r]);
1526 }
1527}
1528
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001529// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1530// not their transitive dependencies) as children of the start_with library.
1531// This is false when find_libraries is called for dlopen(), when newly loaded
1532// libraries must form a disjoint tree.
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001533bool find_libraries(android_namespace_t* ns,
1534 soinfo* start_with,
1535 const char* const library_names[],
1536 size_t library_names_count,
1537 soinfo* soinfos[],
1538 std::vector<soinfo*>* ld_preloads,
1539 size_t ld_preloads_count,
1540 int rtld_flags,
1541 const android_dlextinfo* extinfo,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001542 bool add_as_children,
Jiyong Park02586a22017-05-20 01:01:24 +09001543 bool search_linked_namespaces,
Jiyong Park02586a22017-05-20 01:01:24 +09001544 std::vector<android_namespace_t*>* namespaces) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001545 // Step 0: prepare.
dimitry965d06d2017-11-28 16:03:07 +01001546 std::unordered_map<const soinfo*, ElfReader> readers_map;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001547 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001548
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001549 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001550 const char* name = library_names[i];
Jiyong Park02586a22017-05-20 01:01:24 +09001551 load_tasks.push_back(LoadTask::create(name, start_with, ns, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001552 }
1553
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001554 // If soinfos array is null allocate one on stack.
1555 // The array is needed in case of failure; for example
1556 // when library_names[] = {libone.so, libtwo.so} and libone.so
1557 // is loaded correctly but libtwo.so failed for some reason.
1558 // In this case libone.so should be unloaded on return.
1559 // See also implementation of failure_guard below.
1560
1561 if (soinfos == nullptr) {
1562 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1563 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1564 memset(soinfos, 0, soinfos_size);
1565 }
1566
1567 // list of libraries to link - see step 2.
1568 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001569
Tom Cherryb8ab6182017-04-05 16:20:29 -07001570 auto scope_guard = android::base::make_scope_guard([&]() {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001571 for (LoadTask* t : load_tasks) {
1572 LoadTask::deleter(t);
1573 }
1574 });
1575
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001576 ZipArchiveCache zip_archive_cache;
1577
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001578 // Step 1: expand the list of load_tasks to include
1579 // all DT_NEEDED libraries (do not load them just yet)
1580 for (size_t i = 0; i<load_tasks.size(); ++i) {
1581 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07001582 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001583
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001584 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001585 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001586 task->set_dt_needed(is_dt_needed);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001587
Jiyong Park02586a22017-05-20 01:01:24 +09001588 // Note: start from the namespace that is stored in the LoadTask. This namespace
1589 // is different from the current namespace when the LoadTask is for a transitive
1590 // dependency and the lib that created the LoadTask is not found in the
1591 // current namespace but in one of the linked namespace.
1592 if (!find_library_internal(const_cast<android_namespace_t*>(task->get_start_from()),
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001593 task,
1594 &zip_archive_cache,
1595 &load_tasks,
1596 rtld_flags,
1597 search_linked_namespaces || is_dt_needed)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001598 return false;
1599 }
1600
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001601 soinfo* si = task->get_soinfo();
1602
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001603 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001604 needed_by->add_child(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001605 }
1606
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001607 // When ld_preloads is not null, the first
1608 // ld_preloads_count libs are in fact ld_preloads.
1609 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001610 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001611 }
1612
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001613 if (soinfos_count < library_names_count) {
1614 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001615 }
1616 }
1617
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001618 // Step 2: Load libraries in random order (see b/24047022)
1619 LoadTaskList load_list;
1620 for (auto&& task : load_tasks) {
1621 soinfo* si = task->get_soinfo();
1622 auto pred = [&](const LoadTask* t) {
1623 return t->get_soinfo() == si;
1624 };
1625
1626 if (!si->is_linked() &&
1627 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
1628 load_list.push_back(task);
1629 }
1630 }
1631 shuffle(&load_list);
1632
1633 for (auto&& task : load_list) {
1634 if (!task->load()) {
1635 return false;
1636 }
1637 }
1638
1639 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
1640 for (auto&& task : load_tasks) {
1641 soinfo* si = task->get_soinfo();
1642 if (!si->is_linked() && !si->prelink_image()) {
1643 return false;
1644 }
1645 }
1646
Jiyong Park02586a22017-05-20 01:01:24 +09001647 // Step 4: Construct the global group. Note: DF_1_GLOBAL bit of a library is
1648 // determined at step 3.
1649
1650 // Step 4-1: DF_1_GLOBAL bit is force set for LD_PRELOADed libs because they
1651 // must be added to the global group
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001652 if (ld_preloads != nullptr) {
1653 for (auto&& si : *ld_preloads) {
1654 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
1655 }
1656 }
1657
Jiyong Park02586a22017-05-20 01:01:24 +09001658 // Step 4-2: Gather all DF_1_GLOBAL libs which were newly loaded during this
1659 // run. These will be the new member of the global group
1660 soinfo_list_t new_global_group_members;
1661 for (auto&& task : load_tasks) {
1662 soinfo* si = task->get_soinfo();
1663 if (!si->is_linked() && (si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1664 new_global_group_members.push_back(si);
1665 }
1666 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001667
Jiyong Park02586a22017-05-20 01:01:24 +09001668 // Step 4-3: Add the new global group members to all the linked namespaces
Jiyong Park01162f22017-10-16 15:31:09 +09001669 if (namespaces != nullptr) {
Jiyong Park02586a22017-05-20 01:01:24 +09001670 for (auto linked_ns : *namespaces) {
Jiyong Park01162f22017-10-16 15:31:09 +09001671 for (auto si : new_global_group_members) {
1672 if (si->get_primary_namespace() != linked_ns) {
1673 linked_ns->add_soinfo(si);
1674 si->add_secondary_namespace(linked_ns);
1675 }
Jiyong Park02586a22017-05-20 01:01:24 +09001676 }
1677 }
1678 }
1679
dimitry965d06d2017-11-28 16:03:07 +01001680 // Step 5: Collect roots of local_groups.
1681 // Whenever needed_by->si link crosses a namespace boundary it forms its own local_group.
1682 // Here we collect new roots to link them separately later on. Note that we need to avoid
1683 // collecting duplicates. Also the order is important. They need to be linked in the same
1684 // BFS order we link individual libraries.
1685 std::vector<soinfo*> local_group_roots;
1686 if (start_with != nullptr && add_as_children) {
1687 local_group_roots.push_back(start_with);
1688 } else {
1689 CHECK(soinfos_count == 1);
1690 local_group_roots.push_back(soinfos[0]);
1691 }
1692
Jiyong Park02586a22017-05-20 01:01:24 +09001693 for (auto&& task : load_tasks) {
1694 soinfo* si = task->get_soinfo();
dimitry965d06d2017-11-28 16:03:07 +01001695 soinfo* needed_by = task->get_needed_by();
1696 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
1697 android_namespace_t* needed_by_ns =
1698 is_dt_needed ? needed_by->get_primary_namespace() : ns;
1699
1700 if (!si->is_linked() && si->get_primary_namespace() != needed_by_ns) {
1701 auto it = std::find(local_group_roots.begin(), local_group_roots.end(), si);
1702 LD_LOG(kLogDlopen,
1703 "Crossing namespace boundary (si=%s@%p, si_ns=%s@%p, needed_by=%s@%p, ns=%s@%p, needed_by_ns=%s@%p) adding to local_group_roots: %s",
1704 si->get_realpath(),
1705 si,
1706 si->get_primary_namespace()->get_name(),
1707 si->get_primary_namespace(),
1708 needed_by == nullptr ? "(nullptr)" : needed_by->get_realpath(),
1709 needed_by,
1710 ns->get_name(),
1711 ns,
1712 needed_by_ns->get_name(),
1713 needed_by_ns,
1714 it == local_group_roots.end() ? "yes" : "no");
1715
1716 if (it == local_group_roots.end()) {
1717 local_group_roots.push_back(si);
Jiyong Park02586a22017-05-20 01:01:24 +09001718 }
1719 }
1720 }
1721
dimitry965d06d2017-11-28 16:03:07 +01001722 // Step 6: Link all local groups
1723 for (auto root : local_group_roots) {
1724 soinfo_list_t local_group;
1725 android_namespace_t* local_group_ns = root->get_primary_namespace();
1726
1727 walk_dependencies_tree(root,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001728 [&] (soinfo* si) {
dimitry965d06d2017-11-28 16:03:07 +01001729 if (local_group_ns->is_accessible(si)) {
1730 local_group.push_back(si);
1731 return kWalkContinue;
1732 } else {
1733 return kWalkSkip;
1734 }
1735 });
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001736
dimitry965d06d2017-11-28 16:03:07 +01001737 soinfo_list_t global_group = local_group_ns->get_global_group();
1738 bool linked = local_group.visit([&](soinfo* si) {
1739 // Even though local group may contain accessible soinfos from other namesapces
1740 // we should avoid linking them (because if they are not linked -> they
1741 // are in the local_group_roots and will be linked later).
1742 if (!si->is_linked() && si->get_primary_namespace() == local_group_ns) {
1743 if (!si->link_image(global_group, local_group, extinfo) ||
1744 !get_cfi_shadow()->AfterLoad(si, solist_get_head())) {
1745 return false;
1746 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001747 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001748
dimitry965d06d2017-11-28 16:03:07 +01001749 return true;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001750 });
Elliott Hughes27f18062017-11-29 18:47:42 +00001751
dimitry965d06d2017-11-28 16:03:07 +01001752 if (!linked) {
1753 return false;
1754 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001755 }
1756
dimitry965d06d2017-11-28 16:03:07 +01001757 // Step 7: Mark all load_tasks as linked and increment refcounts
1758 // for references between load_groups (at this point it does not matter if
1759 // referenced load_groups were loaded by previous dlopen or as part of this
1760 // one on step 6)
1761 if (start_with != nullptr && add_as_children) {
1762 start_with->set_linked();
1763 }
1764
1765 for (auto&& task : load_tasks) {
1766 soinfo* si = task->get_soinfo();
1767 si->set_linked();
1768 }
1769
1770 for (auto&& task : load_tasks) {
1771 soinfo* si = task->get_soinfo();
1772 soinfo* needed_by = task->get_needed_by();
1773 if (needed_by != nullptr &&
1774 needed_by != start_with &&
1775 needed_by->get_local_group_root() != si->get_local_group_root()) {
1776 si->increment_ref_count();
1777 }
1778 }
1779
1780
1781 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001782}
1783
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001784static soinfo* find_library(android_namespace_t* ns,
1785 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001786 const android_dlextinfo* extinfo,
1787 soinfo* needed_by) {
dimitry965d06d2017-11-28 16:03:07 +01001788 soinfo* si = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001789
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001790 if (name == nullptr) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001791 si = solist_get_somain();
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001792 } else if (!find_libraries(ns,
1793 needed_by,
1794 &name,
1795 1,
1796 &si,
1797 nullptr,
1798 0,
1799 rtld_flags,
1800 extinfo,
1801 false /* add_as_children */,
dimitry965d06d2017-11-28 16:03:07 +01001802 true /* search_linked_namespaces */)) {
1803 if (si != nullptr) {
1804 soinfo_unload(si);
1805 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001806 return nullptr;
1807 }
1808
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001809 si->increment_ref_count();
1810
Elliott Hughesd23736e2012-11-01 15:16:56 -07001811 return si;
1812}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001813
dimitry06016f22018-01-05 11:39:28 +01001814static void soinfo_unload_impl(soinfo* root) {
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07001815 ScopedTrace trace((std::string("unload ") + root->get_realpath()).c_str());
dimitry06016f22018-01-05 11:39:28 +01001816 bool is_linked = root->is_linked();
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07001817
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001818 if (!root->can_unload()) {
dimitryc92ce712017-10-27 14:12:53 +02001819 LD_LOG(kLogDlopen,
1820 "... dlclose(root=\"%s\"@%p) ... not unloading - the load group is flagged with NODELETE",
1821 root->get_realpath(),
1822 root);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001823 return;
1824 }
1825
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001826
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001827 soinfo_list_t unload_list;
dimitry965d06d2017-11-28 16:03:07 +01001828 unload_list.push_back(root);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001829
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001830 soinfo_list_t local_unload_list;
1831 soinfo_list_t external_unload_list;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001832 soinfo* si = nullptr;
1833
1834 while ((si = unload_list.pop_front()) != nullptr) {
1835 if (local_unload_list.contains(si)) {
1836 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001837 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001838
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001839 local_unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001840
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001841 if (si->has_min_version(0)) {
1842 soinfo* child = nullptr;
1843 while ((child = si->get_children().pop_front()) != nullptr) {
1844 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
1845 child->get_realpath(), child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001846
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001847 child->get_parents().remove(si);
1848
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001849 if (local_unload_list.contains(child)) {
1850 continue;
1851 } else if (child->is_linked() && child->get_local_group_root() != root) {
1852 external_unload_list.push_back(child);
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001853 } else if (child->get_parents().empty()) {
1854 unload_list.push_back(child);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001855 }
1856 }
1857 } else {
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001858 async_safe_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001859 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001860 }
1861
1862 local_unload_list.for_each([](soinfo* si) {
dimitryc92ce712017-10-27 14:12:53 +02001863 LD_LOG(kLogDlopen,
1864 "... dlclose: calling destructors for \"%s\"@%p ... ",
1865 si->get_realpath(),
1866 si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001867 si->call_destructors();
dimitryc92ce712017-10-27 14:12:53 +02001868 LD_LOG(kLogDlopen,
1869 "... dlclose: calling destructors for \"%s\"@%p ... done",
1870 si->get_realpath(),
1871 si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001872 });
1873
1874 while ((si = local_unload_list.pop_front()) != nullptr) {
dimitryc92ce712017-10-27 14:12:53 +02001875 LD_LOG(kLogDlopen,
1876 "... dlclose: unloading \"%s\"@%p ...",
1877 si->get_realpath(),
1878 si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001879 notify_gdb_of_unload(si);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001880 get_cfi_shadow()->BeforeUnload(si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001881 soinfo_free(si);
1882 }
1883
dimitry965d06d2017-11-28 16:03:07 +01001884 if (is_linked) {
1885 while ((si = external_unload_list.pop_front()) != nullptr) {
1886 LD_LOG(kLogDlopen,
1887 "... dlclose: unloading external reference \"%s\"@%p ...",
1888 si->get_realpath(),
1889 si);
1890 soinfo_unload(si);
1891 }
1892 } else {
1893 LD_LOG(kLogDlopen,
1894 "... dlclose: unload_si was not linked - not unloading external references ...");
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001895 }
1896}
1897
dimitry06016f22018-01-05 11:39:28 +01001898static void soinfo_unload(soinfo* unload_si) {
1899 // Note that the library can be loaded but not linked;
1900 // in which case there is no root but we still need
1901 // to walk the tree and unload soinfos involved.
1902 //
1903 // This happens on unsuccessful dlopen, when one of
1904 // the DT_NEEDED libraries could not be linked/found.
1905 bool is_linked = unload_si->is_linked();
1906 soinfo* root = is_linked ? unload_si->get_local_group_root() : unload_si;
1907
1908 LD_LOG(kLogDlopen,
1909 "... dlclose(realpath=\"%s\"@%p) ... load group root is \"%s\"@%p",
1910 unload_si->get_realpath(),
1911 unload_si,
1912 root->get_realpath(),
1913 root);
1914
1915
1916 size_t ref_count = is_linked ? root->decrement_ref_count() : 0;
1917 if (ref_count > 0) {
1918 LD_LOG(kLogDlopen,
1919 "... dlclose(root=\"%s\"@%p) ... not unloading - decrementing ref_count to %zd",
1920 root->get_realpath(),
1921 root,
1922 ref_count);
1923 return;
1924 }
1925
1926 soinfo_unload_impl(root);
1927}
1928
1929void increment_dso_handle_reference_counter(void* dso_handle) {
1930 if (dso_handle == nullptr) {
1931 return;
1932 }
1933
1934 auto it = g_dso_handle_counters.find(dso_handle);
1935 if (it != g_dso_handle_counters.end()) {
1936 CHECK(++it->second != 0);
1937 } else {
1938 soinfo* si = find_containing_library(dso_handle);
1939 if (si != nullptr) {
1940 ProtectedDataGuard guard;
dimitry55547db2018-05-25 14:17:37 +02001941 si->increment_ref_count();
dimitry06016f22018-01-05 11:39:28 +01001942 } else {
1943 async_safe_fatal(
1944 "increment_dso_handle_reference_counter: Couldn't find soinfo by dso_handle=%p",
1945 dso_handle);
1946 }
1947 g_dso_handle_counters[dso_handle] = 1U;
1948 }
1949}
1950
1951void decrement_dso_handle_reference_counter(void* dso_handle) {
1952 if (dso_handle == nullptr) {
1953 return;
1954 }
1955
1956 auto it = g_dso_handle_counters.find(dso_handle);
1957 CHECK(it != g_dso_handle_counters.end());
1958 CHECK(it->second != 0);
1959
1960 if (--it->second == 0) {
1961 soinfo* si = find_containing_library(dso_handle);
1962 if (si != nullptr) {
1963 ProtectedDataGuard guard;
dimitry55547db2018-05-25 14:17:37 +02001964 soinfo_unload(si);
dimitry06016f22018-01-05 11:39:28 +01001965 } else {
1966 async_safe_fatal(
1967 "decrement_dso_handle_reference_counter: Couldn't find soinfo by dso_handle=%p",
1968 dso_handle);
1969 }
1970 g_dso_handle_counters.erase(it);
1971 }
1972}
1973
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001974static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
1975 if (sym_ver == nullptr) {
1976 return sym_name;
1977 }
1978
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001979 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001980}
1981
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001982static android_namespace_t* get_caller_namespace(soinfo* caller) {
1983 return caller != nullptr ? caller->get_primary_namespace() : g_anonymous_namespace;
1984}
1985
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001986void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001987 // Use basic string manipulation calls to avoid snprintf.
1988 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1989 // When debug malloc is enabled, this call returns 0. This in turn causes
1990 // snprintf to do nothing, which causes libraries to fail to load.
1991 // See b/17302493 for further details.
1992 // Once the above bug is fixed, this code can be modified to use
1993 // snprintf again.
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001994 const auto& default_ld_paths = g_default_namespace.get_default_library_paths();
1995
1996 size_t required_size = 0;
1997 for (const auto& path : default_ld_paths) {
1998 required_size += path.size() + 1;
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001999 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08002000
2001 if (buffer_size < required_size) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -07002002 async_safe_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
2003 "buffer len %zu, required len %zu", buffer_size, required_size);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002004 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08002005
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002006 char* end = buffer;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08002007 for (size_t i = 0; i < default_ld_paths.size(); ++i) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002008 if (i > 0) *end++ = ':';
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08002009 end = stpcpy(end, default_ld_paths[i].c_str());
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002010 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002011}
2012
Elliott Hughescade4c32012-12-20 14:42:14 -08002013void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08002014 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08002015}
2016
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002017static std::string android_dlextinfo_to_string(const android_dlextinfo* info) {
2018 if (info == nullptr) {
2019 return "(null)";
2020 }
2021
2022 return android::base::StringPrintf("[flags=0x%" PRIx64 ","
2023 " reserved_addr=%p,"
2024 " reserved_size=0x%zx,"
2025 " relro_fd=%d,"
2026 " library_fd=%d,"
2027 " library_fd_offset=0x%" PRIx64 ","
2028 " library_namespace=%s@%p]",
2029 info->flags,
2030 info->reserved_addr,
2031 info->reserved_size,
2032 info->relro_fd,
2033 info->library_fd,
2034 info->library_fd_offset,
2035 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
2036 (info->library_namespace != nullptr ?
2037 info->library_namespace->get_name() : "(null)") : "(n/a)",
2038 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
2039 info->library_namespace : nullptr);
2040}
2041
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08002042void* do_dlopen(const char* name, int flags,
2043 const android_dlextinfo* extinfo,
2044 const void* caller_addr) {
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -07002045 std::string trace_prefix = std::string("dlopen: ") + (name == nullptr ? "(nullptr)" : name);
2046 ScopedTrace trace(trace_prefix.c_str());
2047 ScopedTrace loading_trace((trace_prefix + " - loading and linking").c_str());
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002048 soinfo* const caller = find_containing_library(caller_addr);
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002049 android_namespace_t* ns = get_caller_namespace(caller);
2050
2051 LD_LOG(kLogDlopen,
2052 "dlopen(name=\"%s\", flags=0x%x, extinfo=%s, caller=\"%s\", caller_ns=%s@%p) ...",
2053 name,
2054 flags,
2055 android_dlextinfo_to_string(extinfo).c_str(),
2056 caller == nullptr ? "(null)" : caller->get_realpath(),
2057 ns == nullptr ? "(null)" : ns->get_name(),
2058 ns);
2059
Tom Cherryb8ab6182017-04-05 16:20:29 -07002060 auto failure_guard = android::base::make_scope_guard(
2061 [&]() { LD_LOG(kLogDlopen, "... dlopen failed: %s", linker_get_error_buffer()); });
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002062
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002063 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08002064 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002065 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08002066 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002067
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002068 if (extinfo != nullptr) {
2069 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
2070 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
2071 return nullptr;
2072 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002073
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002074 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07002075 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002076 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
2077 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002078 return nullptr;
2079 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002080
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002081 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
2082 if (extinfo->library_namespace == nullptr) {
2083 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
2084 return nullptr;
2085 }
2086 ns = extinfo->library_namespace;
2087 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00002088 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002089
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07002090 std::string asan_name_holder;
2091
2092 const char* translated_name = name;
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08002093 if (g_is_asan && translated_name != nullptr && translated_name[0] == '/') {
Evgenii Stepanov9e77a642017-07-27 14:55:44 -07002094 char original_path[PATH_MAX];
2095 if (realpath(name, original_path) != nullptr) {
2096 asan_name_holder = std::string(kAsanLibDirPrefix) + original_path;
Vishwath Mohan4113def2017-03-29 15:31:34 -07002097 if (file_exists(asan_name_holder.c_str())) {
Evgenii Stepanov9e77a642017-07-27 14:55:44 -07002098 soinfo* si = nullptr;
2099 if (find_loaded_library_by_realpath(ns, original_path, true, &si)) {
2100 PRINT("linker_asan dlopen NOT translating \"%s\" -> \"%s\": library already loaded", name,
2101 asan_name_holder.c_str());
2102 } else {
2103 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
2104 translated_name = asan_name_holder.c_str();
2105 }
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07002106 }
2107 }
2108 }
2109
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002110 ProtectedDataGuard guard;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07002111 soinfo* si = find_library(ns, translated_name, flags, extinfo, caller);
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -07002112 loading_trace.End();
2113
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002114 if (si != nullptr) {
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002115 void* handle = si->to_handle();
2116 LD_LOG(kLogDlopen,
Dimitry Ivanovae4a0c12016-11-21 10:44:35 -08002117 "... dlopen calling constructors: realpath=\"%s\", soname=\"%s\", handle=%p",
2118 si->get_realpath(), si->get_soname(), handle);
2119 si->call_constructors();
Tom Cherryb8ab6182017-04-05 16:20:29 -07002120 failure_guard.Disable();
Dimitry Ivanovae4a0c12016-11-21 10:44:35 -08002121 LD_LOG(kLogDlopen,
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002122 "... dlopen successful: realpath=\"%s\", soname=\"%s\", handle=%p",
2123 si->get_realpath(), si->get_soname(), handle);
2124 return handle;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002125 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002126
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002127 return nullptr;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002128}
2129
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002130int do_dladdr(const void* addr, Dl_info* info) {
2131 // Determine if this address can be found in any library currently mapped.
2132 soinfo* si = find_containing_library(addr);
2133 if (si == nullptr) {
2134 return 0;
2135 }
2136
2137 memset(info, 0, sizeof(Dl_info));
2138
2139 info->dli_fname = si->get_realpath();
2140 // Address at which the shared object is loaded.
2141 info->dli_fbase = reinterpret_cast<void*>(si->base);
2142
2143 // Determine if any symbol in the library contains the specified address.
2144 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
2145 if (sym != nullptr) {
2146 info->dli_sname = si->get_string(sym->st_name);
2147 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
2148 }
2149
2150 return 1;
2151}
2152
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002153static soinfo* soinfo_from_handle(void* handle) {
2154 if ((reinterpret_cast<uintptr_t>(handle) & 1) != 0) {
2155 auto it = g_soinfo_handles_map.find(reinterpret_cast<uintptr_t>(handle));
2156 if (it == g_soinfo_handles_map.end()) {
2157 return nullptr;
2158 } else {
2159 return it->second;
2160 }
2161 }
2162
2163 return static_cast<soinfo*>(handle);
2164}
2165
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08002166bool do_dlsym(void* handle,
2167 const char* sym_name,
2168 const char* sym_ver,
2169 const void* caller_addr,
2170 void** symbol) {
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07002171 ScopedTrace trace("dlsym");
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002172#if !defined(__LP64__)
2173 if (handle == nullptr) {
2174 DL_ERR("dlsym failed: library handle is null");
2175 return false;
2176 }
2177#endif
2178
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002179 soinfo* found = nullptr;
2180 const ElfW(Sym)* sym = nullptr;
2181 soinfo* caller = find_containing_library(caller_addr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002182 android_namespace_t* ns = get_caller_namespace(caller);
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002183 soinfo* si = nullptr;
2184 if (handle != RTLD_DEFAULT && handle != RTLD_NEXT) {
2185 si = soinfo_from_handle(handle);
2186 }
2187
2188 LD_LOG(kLogDlsym,
2189 "dlsym(handle=%p(\"%s\"), sym_name=\"%s\", sym_ver=\"%s\", caller=\"%s\", caller_ns=%s@%p) ...",
2190 handle,
2191 si != nullptr ? si->get_realpath() : "n/a",
2192 sym_name,
2193 sym_ver,
2194 caller == nullptr ? "(null)" : caller->get_realpath(),
2195 ns == nullptr ? "(null)" : ns->get_name(),
2196 ns);
2197
Tom Cherryb8ab6182017-04-05 16:20:29 -07002198 auto failure_guard = android::base::make_scope_guard(
2199 [&]() { LD_LOG(kLogDlsym, "... dlsym failed: %s", linker_get_error_buffer()); });
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002200
2201 if (sym_name == nullptr) {
2202 DL_ERR("dlsym failed: symbol name is null");
2203 return false;
2204 }
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002205
2206 version_info vi_instance;
2207 version_info* vi = nullptr;
2208
2209 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002210 vi_instance.name = sym_ver;
2211 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002212 vi = &vi_instance;
2213 }
2214
2215 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2216 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2217 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002218 if (si == nullptr) {
2219 DL_ERR("dlsym failed: invalid handle: %p", handle);
2220 return false;
2221 }
2222 sym = dlsym_handle_lookup(si, &found, sym_name, vi);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002223 }
2224
2225 if (sym != nullptr) {
2226 uint32_t bind = ELF_ST_BIND(sym->st_info);
2227
2228 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2229 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
Tom Cherryb8ab6182017-04-05 16:20:29 -07002230 failure_guard.Disable();
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002231 LD_LOG(kLogDlsym,
2232 "... dlsym successful: sym_name=\"%s\", sym_ver=\"%s\", found in=\"%s\", address=%p",
2233 sym_name, sym_ver, found->get_soname(), *symbol);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002234 return true;
2235 }
2236
2237 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2238 return false;
2239 }
2240
2241 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2242 return false;
2243}
2244
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002245int do_dlclose(void* handle) {
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07002246 ScopedTrace trace("dlclose");
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002247 ProtectedDataGuard guard;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002248 soinfo* si = soinfo_from_handle(handle);
2249 if (si == nullptr) {
2250 DL_ERR("invalid handle: %p", handle);
2251 return -1;
2252 }
2253
dimitryc92ce712017-10-27 14:12:53 +02002254 LD_LOG(kLogDlopen,
2255 "dlclose(handle=%p, realpath=\"%s\"@%p) ...",
2256 handle,
2257 si->get_realpath(),
2258 si);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002259 soinfo_unload(si);
dimitryc92ce712017-10-27 14:12:53 +02002260 LD_LOG(kLogDlopen,
2261 "dlclose(handle=%p) ... done",
2262 handle);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002263 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002264}
2265
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002266bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path) {
2267 if (g_anonymous_namespace_initialized) {
2268 DL_ERR("anonymous namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002269 return false;
2270 }
2271
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002272 ProtectedDataGuard guard;
2273
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002274 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002275 // When the caller is nullptr - create_namespace will take global group
2276 // from the anonymous namespace, which is fine because anonymous namespace
2277 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002278 android_namespace_t* anon_ns =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002279 create_namespace(nullptr,
2280 "(anonymous)",
2281 nullptr,
2282 library_search_path,
Dimitry Ivanovc9dced22017-03-27 15:42:17 -07002283 ANDROID_NAMESPACE_TYPE_ISOLATED,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002284 nullptr,
2285 &g_default_namespace);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002286
2287 if (anon_ns == nullptr) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002288 return false;
2289 }
2290
2291 if (!link_namespaces(anon_ns, &g_default_namespace, shared_lib_sonames)) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002292 return false;
2293 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08002294
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002295 g_anonymous_namespace = anon_ns;
Dimitry Ivanov3e0821d2017-03-07 11:02:10 -08002296 g_anonymous_namespace_initialized = true;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002297
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002298 return true;
2299}
2300
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002301static void add_soinfos_to_namespace(const soinfo_list_t& soinfos, android_namespace_t* ns) {
2302 ns->add_soinfos(soinfos);
2303 for (auto si : soinfos) {
2304 si->add_secondary_namespace(ns);
2305 }
2306}
2307
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002308android_namespace_t* create_namespace(const void* caller_addr,
2309 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002310 const char* ld_library_path,
2311 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002312 uint64_t type,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002313 const char* permitted_when_isolated_path,
2314 android_namespace_t* parent_namespace) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002315 if (parent_namespace == nullptr) {
Dimitry Ivanov52408632016-05-23 10:31:11 -07002316 // if parent_namespace is nullptr -> set it to the caller namespace
2317 soinfo* caller_soinfo = find_containing_library(caller_addr);
2318
2319 parent_namespace = caller_soinfo != nullptr ?
2320 caller_soinfo->get_primary_namespace() :
2321 g_anonymous_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002322 }
2323
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002324 ProtectedDataGuard guard;
2325 std::vector<std::string> ld_library_paths;
2326 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002327 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002328
2329 parse_path(ld_library_path, ":", &ld_library_paths);
2330 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002331 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002332
2333 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2334 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002335 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Jiyong Park37b91af2017-05-05 22:07:05 +09002336 ns->set_greylist_enabled((type & ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002337
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002338 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07002339 // append parent namespace paths.
2340 std::copy(parent_namespace->get_ld_library_paths().begin(),
2341 parent_namespace->get_ld_library_paths().end(),
2342 back_inserter(ld_library_paths));
2343
2344 std::copy(parent_namespace->get_default_library_paths().begin(),
2345 parent_namespace->get_default_library_paths().end(),
2346 back_inserter(default_library_paths));
2347
2348 std::copy(parent_namespace->get_permitted_paths().begin(),
2349 parent_namespace->get_permitted_paths().end(),
2350 back_inserter(permitted_paths));
2351
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002352 // If shared - clone the parent namespace
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002353 add_soinfos_to_namespace(parent_namespace->soinfo_list(), ns);
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07002354 // and copy parent namespace links
2355 for (auto& link : parent_namespace->linked_namespaces()) {
Logan Chien9ee45912018-01-18 12:05:09 +08002356 ns->add_linked_namespace(link.linked_namespace(), link.shared_lib_sonames(),
2357 link.allow_all_shared_libs());
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07002358 }
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002359 } else {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002360 // If not shared - copy only the shared group
Jiyong Park02586a22017-05-20 01:01:24 +09002361 add_soinfos_to_namespace(parent_namespace->get_shared_group(), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002362 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002363
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07002364 ns->set_ld_library_paths(std::move(ld_library_paths));
2365 ns->set_default_library_paths(std::move(default_library_paths));
2366 ns->set_permitted_paths(std::move(permitted_paths));
2367
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002368 return ns;
2369}
2370
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002371bool link_namespaces(android_namespace_t* namespace_from,
2372 android_namespace_t* namespace_to,
2373 const char* shared_lib_sonames) {
2374 if (namespace_to == nullptr) {
2375 namespace_to = &g_default_namespace;
2376 }
2377
2378 if (namespace_from == nullptr) {
2379 DL_ERR("error linking namespaces: namespace_from is null.");
2380 return false;
2381 }
2382
2383 if (shared_lib_sonames == nullptr || shared_lib_sonames[0] == '\0') {
2384 DL_ERR("error linking namespaces \"%s\"->\"%s\": the list of shared libraries is empty.",
2385 namespace_from->get_name(), namespace_to->get_name());
2386 return false;
2387 }
2388
2389 auto sonames = android::base::Split(shared_lib_sonames, ":");
2390 std::unordered_set<std::string> sonames_set(sonames.begin(), sonames.end());
2391
2392 ProtectedDataGuard guard;
Logan Chien9ee45912018-01-18 12:05:09 +08002393 namespace_from->add_linked_namespace(namespace_to, sonames_set, false);
2394
2395 return true;
2396}
2397
2398bool link_namespaces_all_libs(android_namespace_t* namespace_from,
2399 android_namespace_t* namespace_to) {
2400 if (namespace_from == nullptr) {
2401 DL_ERR("error linking namespaces: namespace_from is null.");
2402 return false;
2403 }
2404
2405 if (namespace_to == nullptr) {
2406 DL_ERR("error linking namespaces: namespace_to is null.");
2407 return false;
2408 }
2409
2410 ProtectedDataGuard guard;
2411 namespace_from->add_linked_namespace(namespace_to, std::unordered_set<std::string>(), true);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002412
2413 return true;
2414}
2415
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002416ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002417 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2418 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2419 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002420 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2421 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002422
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002423 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002424}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002425
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002426const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2427 if (source_symver < 2 ||
2428 source_symver >= version_infos.size() ||
2429 version_infos[source_symver].name == nullptr) {
2430 return nullptr;
2431 }
2432
2433 return &version_infos[source_symver];
2434}
2435
2436void VersionTracker::add_version_info(size_t source_index,
2437 ElfW(Word) elf_hash,
2438 const char* ver_name,
2439 const soinfo* target_si) {
2440 if (source_index >= version_infos.size()) {
2441 version_infos.resize(source_index+1);
2442 }
2443
2444 version_infos[source_index].elf_hash = elf_hash;
2445 version_infos[source_index].name = ver_name;
2446 version_infos[source_index].target_si = target_si;
2447}
2448
2449bool VersionTracker::init_verneed(const soinfo* si_from) {
2450 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2451
2452 if (verneed_ptr == 0) {
2453 return true;
2454 }
2455
2456 size_t verneed_cnt = si_from->get_verneed_cnt();
2457
2458 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2459 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2460 size_t vernaux_offset = offset + verneed->vn_aux;
2461 offset += verneed->vn_next;
2462
2463 if (verneed->vn_version != 1) {
2464 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2465 return false;
2466 }
2467
2468 const char* target_soname = si_from->get_string(verneed->vn_file);
2469 // find it in dependencies
2470 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002471 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002472 });
2473
2474 if (target_si == nullptr) {
2475 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002476 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002477 return false;
2478 }
2479
2480 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2481 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2482 vernaux_offset += vernaux->vna_next;
2483
2484 const ElfW(Word) elf_hash = vernaux->vna_hash;
2485 const char* ver_name = si_from->get_string(vernaux->vna_name);
2486 ElfW(Half) source_index = vernaux->vna_other;
2487
2488 add_version_info(source_index, elf_hash, ver_name, target_si);
2489 }
2490 }
2491
2492 return true;
2493}
2494
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002495template <typename F>
2496static bool for_each_verdef(const soinfo* si, F functor) {
2497 if (!si->has_min_version(2)) {
2498 return true;
2499 }
2500
2501 uintptr_t verdef_ptr = si->get_verdef_ptr();
2502 if (verdef_ptr == 0) {
2503 return true;
2504 }
2505
2506 size_t offset = 0;
2507
2508 size_t verdef_cnt = si->get_verdef_cnt();
2509 for (size_t i = 0; i<verdef_cnt; ++i) {
2510 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
2511 size_t verdaux_offset = offset + verdef->vd_aux;
2512 offset += verdef->vd_next;
2513
2514 if (verdef->vd_version != 1) {
2515 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
2516 i, verdef->vd_version, si->get_realpath());
2517 return false;
2518 }
2519
2520 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
2521 // "this is the version of the file itself. It must not be used for
2522 // matching a symbol. It can be used to match references."
2523 //
2524 // http://www.akkadia.org/drepper/symbol-versioning
2525 continue;
2526 }
2527
2528 if (verdef->vd_cnt == 0) {
2529 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
2530 return false;
2531 }
2532
2533 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
2534
2535 if (functor(i, verdef, verdaux) == true) {
2536 break;
2537 }
2538 }
2539
2540 return true;
2541}
2542
2543bool find_verdef_version_index(const soinfo* si, const version_info* vi, ElfW(Versym)* versym) {
2544 if (vi == nullptr) {
2545 *versym = kVersymNotNeeded;
2546 return true;
2547 }
2548
2549 *versym = kVersymGlobal;
2550
2551 return for_each_verdef(si,
2552 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2553 if (verdef->vd_hash == vi->elf_hash &&
2554 strcmp(vi->name, si->get_string(verdaux->vda_name)) == 0) {
2555 *versym = verdef->vd_ndx;
2556 return true;
2557 }
2558
2559 return false;
2560 }
2561 );
2562}
2563
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002564bool VersionTracker::init_verdef(const soinfo* si_from) {
2565 return for_each_verdef(si_from,
2566 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2567 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2568 si_from->get_string(verdaux->vda_name), si_from);
2569 return false;
2570 }
2571 );
2572}
2573
2574bool VersionTracker::init(const soinfo* si_from) {
2575 if (!si_from->has_min_version(2)) {
2576 return true;
2577 }
2578
2579 return init_verneed(si_from) && init_verdef(si_from);
2580}
2581
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002582// TODO (dimitry): Methods below need to be moved out of soinfo
2583// and in more isolated file in order minimize dependencies on
2584// unnecessary object in the linker binary. Consider making them
2585// independent from soinfo (?).
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002586bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2587 const char* sym_name, const version_info** vi) {
2588 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2589 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2590
2591 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2592 *vi = version_tracker.get_version_info(sym_ver);
2593
2594 if (*vi == nullptr) {
2595 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002596 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002597 return false;
2598 }
2599 } else {
2600 // there is no version info
2601 *vi = nullptr;
2602 }
2603
2604 return true;
2605}
2606
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002607void soinfo::apply_relr_reloc(ElfW(Addr) offset) {
2608 ElfW(Addr) address = offset + load_bias;
2609 *reinterpret_cast<ElfW(Addr)*>(address) += load_bias;
2610}
2611
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002612// Process relocations in SHT_RELR section (experimental).
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002613// Details of the encoding are described in this post:
2614// https://groups.google.com/d/msg/generic-abi/bX460iggiKg/Pi9aSwwABgAJ
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002615bool soinfo::relocate_relr() {
2616 ElfW(Relr)* begin = relr_;
2617 ElfW(Relr)* end = relr_ + relr_count_;
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002618 constexpr size_t wordsize = sizeof(ElfW(Addr));
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002619
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002620 ElfW(Addr) base = 0;
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002621 for (ElfW(Relr)* current = begin; current < end; ++current) {
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002622 ElfW(Relr) entry = *current;
2623 ElfW(Addr) offset;
2624
2625 if ((entry&1) == 0) {
2626 // Even entry: encodes the offset for next relocation.
2627 offset = static_cast<ElfW(Addr)>(entry);
2628 apply_relr_reloc(offset);
2629 // Set base offset for subsequent bitmap entries.
2630 base = offset + wordsize;
2631 continue;
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002632 }
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002633
2634 // Odd entry: encodes bitmap for relocations starting at base.
2635 offset = base;
2636 while (entry != 0) {
2637 entry >>= 1;
2638 if ((entry&1) != 0) {
2639 apply_relr_reloc(offset);
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002640 }
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002641 offset += wordsize;
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002642 }
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002643
2644 // Advance base offset by 63 words for 64-bit platforms,
2645 // or 31 words for 32-bit platforms.
2646 base += (8*wordsize - 1) * wordsize;
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002647 }
2648 return true;
2649}
2650
Dimitry Ivanov576a3752016-08-09 06:58:55 -07002651#if !defined(__mips__)
2652#if defined(USE_RELA)
2653static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2654 return rela->r_addend;
2655}
2656#else
2657static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
2658 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2659 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
2660 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2661 }
2662 return 0;
2663}
2664#endif
2665
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002666template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002667bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2668 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002669 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2670 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002671 if (rel == nullptr) {
2672 return false;
2673 }
2674
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002675 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2676 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2677
2678 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002679 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002680 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002681 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002682
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002683 DEBUG("Processing \"%s\" relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002684 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002685 continue;
2686 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002687
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002688 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002689 soinfo* lsi = nullptr;
2690
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002691 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002692 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002693 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002694
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002695 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2696 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002697 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002698
2699 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2700 return false;
2701 }
2702
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002703 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002704 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002705 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002706 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002707 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002708 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002709 }
2710
2711 /* IHI0044C AAELF 4.5.1.1:
2712
2713 Libraries are not searched to resolve weak references.
2714 It is not an error for a weak reference to remain unsatisfied.
2715
2716 During linking, the value of an undefined weak reference is:
2717 - Zero if the relocation type is absolute
2718 - The address of the place if the relocation is pc-relative
2719 - The address of nominal base address if the relocation
2720 type is base-relative.
2721 */
2722
2723 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002724 case R_GENERIC_JUMP_SLOT:
2725 case R_GENERIC_GLOB_DAT:
2726 case R_GENERIC_RELATIVE:
2727 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002728#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002729 case R_AARCH64_ABS64:
2730 case R_AARCH64_ABS32:
2731 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002732#elif defined(__x86_64__)
2733 case R_X86_64_32:
2734 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002735#elif defined(__arm__)
2736 case R_ARM_ABS32:
2737#elif defined(__i386__)
2738 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002739#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002740 /*
2741 * The sym_addr was initialized to be zero above, or the relocation
2742 * code below does not care about value of sym_addr.
2743 * No need to do anything.
2744 */
2745 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002746#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002747 case R_X86_64_PC32:
2748 sym_addr = reloc;
2749 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002750#elif defined(__i386__)
2751 case R_386_PC32:
2752 sym_addr = reloc;
2753 break;
2754#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002755 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002756 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002757 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002758 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002759 } else { // We got a definition.
2760#if !defined(__LP64__)
2761 // When relocating dso with text_relocation .text segment is
2762 // not executable. We need to restore elf flags before resolving
2763 // STT_GNU_IFUNC symbol.
2764 bool protect_segments = has_text_relocations &&
2765 lsi == this &&
2766 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2767 if (protect_segments) {
2768 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2769 DL_ERR("can't protect segments for \"%s\": %s",
2770 get_realpath(), strerror(errno));
2771 return false;
2772 }
2773 }
2774#endif
Elliott Hughes9724e932018-03-23 18:46:07 -07002775 if (ELF_ST_TYPE(s->st_info) == STT_TLS) {
2776 DL_ERR("unsupported ELF TLS symbol \"%s\" referenced by \"%s\"",
2777 sym_name, get_realpath());
2778 return false;
2779 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002780 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002781#if !defined(__LP64__)
2782 if (protect_segments) {
2783 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2784 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2785 get_realpath(), strerror(errno));
2786 return false;
2787 }
2788 }
2789#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002790 }
2791 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002792 }
2793
2794 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002795 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002796 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002797 MARK(rel->r_offset);
2798 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2799 reinterpret_cast<void*>(reloc),
2800 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2801
2802 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002803 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002804 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002805 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002806 MARK(rel->r_offset);
2807 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2808 reinterpret_cast<void*>(reloc),
2809 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2810 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002811 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002812 case R_GENERIC_RELATIVE:
2813 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002814 MARK(rel->r_offset);
2815 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2816 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002817 reinterpret_cast<void*>(load_bias + addend));
2818 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002819 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002820 case R_GENERIC_IRELATIVE:
2821 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002822 MARK(rel->r_offset);
2823 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2824 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002825 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002826 {
2827#if !defined(__LP64__)
2828 // When relocating dso with text_relocation .text segment is
2829 // not executable. We need to restore elf flags for this
2830 // particular call.
2831 if (has_text_relocations) {
2832 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2833 DL_ERR("can't protect segments for \"%s\": %s",
2834 get_realpath(), strerror(errno));
2835 return false;
2836 }
2837 }
2838#endif
2839 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2840#if !defined(__LP64__)
2841 // Unprotect it afterwards...
2842 if (has_text_relocations) {
2843 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2844 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2845 get_realpath(), strerror(errno));
2846 return false;
2847 }
2848 }
2849#endif
2850 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2851 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002852 break;
2853
2854#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002855 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002856 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002857 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002858 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002859 reloc, sym_addr + addend, sym_name);
2860 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002861 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002862 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002863 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002864 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002865 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002866 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002867 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002868 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2869 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002870 if ((min_value <= (sym_addr + addend)) &&
2871 ((sym_addr + addend) <= max_value)) {
2872 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002873 } else {
2874 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002875 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002876 return false;
2877 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002878 }
2879 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002880 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002881 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002882 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002883 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002884 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002885 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002886 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2887 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002888 if ((min_value <= (sym_addr + addend)) &&
2889 ((sym_addr + addend) <= max_value)) {
2890 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002891 } else {
2892 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002893 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002894 return false;
2895 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002896 }
2897 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002898 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002899 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002900 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002901 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002902 reloc, sym_addr + addend, rel->r_offset, sym_name);
2903 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002904 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002905 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002906 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002907 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002908 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002909 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002910 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002911 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2912 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002913 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2914 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2915 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002916 } else {
2917 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002918 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002919 return false;
2920 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002921 }
2922 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002923 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002924 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002925 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002926 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002927 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002928 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002929 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2930 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002931 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2932 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2933 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002934 } else {
2935 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002936 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002937 return false;
2938 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002939 }
2940 break;
2941
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002942 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002943 /*
2944 * ET_EXEC is not supported so this should not happen.
2945 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002946 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002947 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002948 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002949 * R_AARCH64_COPY may only appear in executable objects where e_type is
2950 * set to ET_EXEC.
2951 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002952 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002953 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002954 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002955 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002956 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002957 break;
Ryan Prichardc1c8a182018-04-24 18:43:19 -07002958 case R_AARCH64_TLSDESC:
2959 TRACE_TYPE(RELO, "RELO TLSDESC *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002960 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002961 break;
2962#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002963 case R_X86_64_32:
2964 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002965 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002966 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2967 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002968 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002969 break;
2970 case R_X86_64_64:
2971 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002972 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002973 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2974 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002975 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002976 break;
2977 case R_X86_64_PC32:
2978 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002979 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002980 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2981 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2982 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002983 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002984 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002985#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002986 case R_ARM_ABS32:
2987 count_relocation(kRelocAbsolute);
2988 MARK(rel->r_offset);
2989 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2990 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2991 break;
2992 case R_ARM_REL32:
2993 count_relocation(kRelocRelative);
2994 MARK(rel->r_offset);
2995 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2996 reloc, sym_addr, rel->r_offset, sym_name);
2997 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2998 break;
2999 case R_ARM_COPY:
3000 /*
3001 * ET_EXEC is not supported so this should not happen.
3002 *
3003 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
3004 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003005 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003006 * R_ARM_COPY may only appear in executable objects where e_type is
3007 * set to ET_EXEC.
3008 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003009 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08003010 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003011#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003012 case R_386_32:
3013 count_relocation(kRelocRelative);
3014 MARK(rel->r_offset);
3015 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
3016 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
3017 break;
3018 case R_386_PC32:
3019 count_relocation(kRelocRelative);
3020 MARK(rel->r_offset);
3021 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
3022 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
3023 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
3024 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003025#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003026 default:
3027 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003028 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003029 }
3030 }
3031 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003032}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08003033#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003034
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07003035// An empty list of soinfos
Dimitry Ivanovb943f302016-08-03 16:00:10 -07003036static soinfo_list_t g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003037
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003038bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08003039 /* Extract dynamic section */
3040 ElfW(Word) dynamic_flags = 0;
3041 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07003042
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003043 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003044 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003045 if (!relocating_linker) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003046 INFO("[ Linking \"%s\" ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003047 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003048 }
3049
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003050 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003051 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003052 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003053 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003054 return false;
3055 } else {
3056 if (!relocating_linker) {
3057 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003058 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003059 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003060
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003061#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003062 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
3063 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003064#endif
3065
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003066 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003067 // Note that: "Except for the DT_NULL element at the end of the array,
3068 // and the relative order of DT_NEEDED elements, entries may appear in any order."
3069 //
3070 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003071 uint32_t needed_count = 0;
3072 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
3073 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
3074 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3075 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003076 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003077 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003078 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003079
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003080 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003081 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
3082 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
3083 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
3084 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003085 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003086
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003087 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003088 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003089 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003090 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
3091 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003092
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003093 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003094 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003095 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003096 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
3097 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003098
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003099 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003100 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003101 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003102 return false;
3103 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003104 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003105
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003106 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003107 break;
3108
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003109 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003110 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003111 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003112
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003113 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003114 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003115 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003116
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003117 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003118 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003119 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003120
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003121 case DT_SYMENT:
3122 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003123 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
3124 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003125 return false;
3126 }
3127 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003128
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003129 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003130#if defined(USE_RELA)
3131 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003132 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003133 return false;
3134 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003135#else
3136 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003137 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003138 return false;
3139 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003140#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003141 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003142
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003143 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003144#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003145 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003146#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003147 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003148#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003149 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003150
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003151 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003152#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003153 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003154#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003155 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003156#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003157 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003158
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003159 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003160#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003161 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003162 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003163#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003164 // Ignore for other platforms... (because RTLD_LAZY is not supported)
3165 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003166
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003167 case DT_DEBUG:
3168 // Set the DT_DEBUG entry to the address of _r_debug for GDB
3169 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08003170// FIXME: not working currently for N64
3171// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003172// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08003173// read-only, but the DYNAMIC header claims it is writable.
3174#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003175 if ((dynamic_flags & PF_W) != 0) {
3176 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
3177 }
Chris Dearman99186652014-02-06 20:36:51 -08003178#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08003179 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003180#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003181 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003182 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003183 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003184
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003185 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003186 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003187 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003188
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003189 case DT_ANDROID_RELA:
3190 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3191 break;
3192
3193 case DT_ANDROID_RELASZ:
3194 android_relocs_size_ = d->d_un.d_val;
3195 break;
3196
3197 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003198 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003199 return false;
3200
3201 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003202 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003203 return false;
3204
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003205 case DT_RELAENT:
3206 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003207 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003208 return false;
3209 }
3210 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003211
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08003212 // Ignored (see DT_RELCOUNT comments for details).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003213 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003214 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003215
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003216 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003217 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003218 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003219
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003220 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003221 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003222 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003223
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003224#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003225 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003226 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003227 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003228
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003229 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003230 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003231 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003232
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003233 case DT_RELENT:
3234 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003235 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003236 return false;
3237 }
3238 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003239
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003240 case DT_ANDROID_REL:
3241 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3242 break;
3243
3244 case DT_ANDROID_RELSZ:
3245 android_relocs_size_ = d->d_un.d_val;
3246 break;
3247
3248 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003249 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003250 return false;
3251
3252 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003253 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003254 return false;
3255
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003256 // "Indicates that all RELATIVE relocations have been concatenated together,
3257 // and specifies the RELATIVE relocation count."
3258 //
3259 // TODO: Spec also mentions that this can be used to optimize relocation process;
3260 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003261 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003262 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003263
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003264 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003265 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003266 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003267
3268 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003269 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003270 return false;
3271
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003272#endif
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08003273 case DT_RELR:
3274 relr_ = reinterpret_cast<ElfW(Relr)*>(load_bias + d->d_un.d_ptr);
3275 break;
3276
3277 case DT_RELRSZ:
3278 relr_count_ = d->d_un.d_val / sizeof(ElfW(Relr));
3279 break;
3280
3281 case DT_RELRENT:
3282 if (d->d_un.d_val != sizeof(ElfW(Relr))) {
3283 DL_ERR("invalid DT_RELRENT: %zd", static_cast<size_t>(d->d_un.d_val));
3284 return false;
3285 }
3286 break;
3287
3288 // Ignored (see DT_RELCOUNT comments for details).
3289 case DT_RELRCOUNT:
3290 break;
3291
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003292 case DT_INIT:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003293 init_func_ = reinterpret_cast<linker_ctor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003294 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003295 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003296
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003297 case DT_FINI:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003298 fini_func_ = reinterpret_cast<linker_dtor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003299 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003300 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003301
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003302 case DT_INIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003303 init_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003304 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003305 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003306
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003307 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003308 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003309 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003310
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003311 case DT_FINI_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003312 fini_array_ = reinterpret_cast<linker_dtor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003313 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003314 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003315
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003316 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003317 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003318 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003319
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003320 case DT_PREINIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003321 preinit_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003322 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003323 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003324
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003325 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003326 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003327 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003328
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003329 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003330#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003331 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003332 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003333#else
3334 has_text_relocations = true;
3335 break;
3336#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003337
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003338 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003339 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003340 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003341
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003342 case DT_NEEDED:
3343 ++needed_count;
3344 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003345
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003346 case DT_FLAGS:
3347 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003348#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003349 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003350 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003351#else
3352 has_text_relocations = true;
3353#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003354 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003355 if (d->d_un.d_val & DF_SYMBOLIC) {
3356 has_DT_SYMBOLIC = true;
3357 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003358 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003359
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003360 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003361 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003362
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003363 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003364 DL_WARN("Warning: \"%s\" has unsupported flags DT_FLAGS_1=%p "
3365 "(ignoring unsupported flags)",
3366 get_realpath(), reinterpret_cast<void*>(d->d_un.d_val));
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003367 }
3368 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003369#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003370 case DT_MIPS_RLD_MAP:
3371 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3372 {
3373 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3374 *dp = &_r_debug;
3375 }
3376 break;
Lazar Trsic83b44a92016-04-06 13:39:17 +02003377 case DT_MIPS_RLD_MAP_REL:
3378 // Set the DT_MIPS_RLD_MAP_REL entry to the address of _r_debug for GDB.
Raghu Gandham68815722014-12-18 19:12:19 -08003379 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003380 r_debug** dp = reinterpret_cast<r_debug**>(
3381 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003382 *dp = &_r_debug;
3383 }
3384 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003385
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003386 case DT_MIPS_RLD_VERSION:
3387 case DT_MIPS_FLAGS:
3388 case DT_MIPS_BASE_ADDRESS:
3389 case DT_MIPS_UNREFEXTNO:
3390 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003391
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003392 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003393 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003394 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003395
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003396 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003397 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003398 break;
3399
3400 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003401 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003402 break;
3403#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003404 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3405 case DT_BIND_NOW:
3406 break;
3407
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003408 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003409 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3410 break;
3411
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003412 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003413 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3414 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003415 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003416 verdef_cnt_ = d->d_un.d_val;
3417 break;
3418
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003419 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003420 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3421 break;
3422
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003423 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003424 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003425 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003426
Evgenii Stepanov68650822015-06-10 13:38:39 -07003427 case DT_RUNPATH:
3428 // this is parsed after we have strtab initialized (see below).
3429 break;
3430
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003431 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003432 if (!relocating_linker) {
Elliott Hughes9724e932018-03-23 18:46:07 -07003433 if (d->d_tag == DT_TLSDESC_GOT || d->d_tag == DT_TLSDESC_PLT) {
3434 DL_ERR("unsupported ELF TLS DT entry in \"%s\"", get_realpath());
3435 return false;
3436 }
3437
Elliott Hughes6eae4cc2017-08-30 09:02:33 -07003438 const char* tag_name;
3439 if (d->d_tag == DT_RPATH) {
3440 tag_name = "DT_RPATH";
3441 } else if (d->d_tag == DT_ENCODING) {
3442 tag_name = "DT_ENCODING";
3443 } else if (d->d_tag >= DT_LOOS && d->d_tag <= DT_HIOS) {
3444 tag_name = "unknown OS-specific";
3445 } else if (d->d_tag >= DT_LOPROC && d->d_tag <= DT_HIPROC) {
3446 tag_name = "unknown processor-specific";
3447 } else {
3448 tag_name = "unknown";
3449 }
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003450 DL_WARN("Warning: \"%s\" unused DT entry: %s (type %p arg %p) (ignoring)",
Elliott Hughes6eae4cc2017-08-30 09:02:33 -07003451 get_realpath(),
3452 tag_name,
3453 reinterpret_cast<void*>(d->d_tag),
3454 reinterpret_cast<void*>(d->d_un.d_val));
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003455 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003456 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003457 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003458 }
3459
Duane Sandbc425c72015-06-01 16:29:14 -07003460#if defined(__mips__) && !defined(__LP64__)
3461 if (!mips_check_and_adjust_fp_modes()) {
3462 return false;
3463 }
3464#endif
3465
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003466 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003467 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003468
3469 // Sanity checks.
3470 if (relocating_linker && needed_count != 0) {
3471 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3472 return false;
3473 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003474 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003475 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003476 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003477 return false;
3478 }
Yi Kong32bc0fc2018-08-02 17:31:13 -07003479 if (strtab_ == nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003480 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003481 return false;
3482 }
Yi Kong32bc0fc2018-08-02 17:31:13 -07003483 if (symtab_ == nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003484 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003485 return false;
3486 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003487
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003488 // second pass - parse entries relying on strtab
3489 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003490 switch (d->d_tag) {
3491 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003492 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003493 break;
3494 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003495 set_dt_runpath(get_string(d->d_un.d_val));
3496 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003497 }
3498 }
3499
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003500 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003501 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003502 // because they can't find dt_needed library by soname.
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003503 // This workaround should keep them working. (Applies only
3504 // for apps targeting sdk version < M.) Make an exception for
3505 // the main executable and linker; they do not need to have dt_soname.
3506 // TODO: >= O the linker doesn't need this workaround.
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003507 if (soname_ == nullptr &&
3508 this != solist_get_somain() &&
3509 (flags_ & FLAG_LINKER) == 0 &&
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003510 get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003511 soname_ = basename(realpath_.c_str());
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003512 DL_WARN_documented_change(__ANDROID_API_M__,
3513 "missing-soname-enforced-for-api-level-23",
3514 "\"%s\" has no DT_SONAME (will use %s instead)",
3515 get_realpath(), soname_);
3516
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003517 // 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 -07003518 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003519 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003520}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003521
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003522bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3523 const android_dlextinfo* extinfo) {
dimitry965d06d2017-11-28 16:03:07 +01003524 if (is_image_linked()) {
3525 // already linked.
3526 return true;
3527 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003528
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003529 local_group_root_ = local_group.front();
3530 if (local_group_root_ == nullptr) {
3531 local_group_root_ = this;
3532 }
3533
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003534 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3535 target_sdk_version_ = get_application_target_sdk_version();
3536 }
3537
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003538 VersionTracker version_tracker;
3539
3540 if (!version_tracker.init(this)) {
3541 return false;
3542 }
3543
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003544#if !defined(__LP64__)
3545 if (has_text_relocations) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003546 // Fail if app is targeting M or above.
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003547 int app_target_api_level = get_application_target_sdk_version();
3548 if (app_target_api_level >= __ANDROID_API_M__) {
Elliott Hughes763f6e12017-04-10 09:52:33 -07003549 DL_ERR_AND_LOG("\"%s\" has text relocations (https://android.googlesource.com/platform/"
3550 "bionic/+/master/android-changes-for-ndk-developers.md#Text-Relocations-"
3551 "Enforced-for-API-level-23)", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003552 return false;
3553 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003554 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003555 // phdr_table_protect_segments() after all of them are applied.
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003556 DL_WARN_documented_change(__ANDROID_API_M__,
3557 "Text-Relocations-Enforced-for-API-level-23",
3558 "\"%s\" has text relocations",
3559 get_realpath());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003560 add_dlwarning(get_realpath(), "text relocations");
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003561 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003562 DL_ERR("can't unprotect loadable segments for \"%s\": %s", get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003563 return false;
3564 }
3565 }
3566#endif
3567
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003568 if (android_relocs_ != nullptr) {
3569 // check signature
3570 if (android_relocs_size_ > 3 &&
3571 android_relocs_[0] == 'A' &&
3572 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003573 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003574 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003575 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003576
3577 bool relocated = false;
3578 const uint8_t* packed_relocs = android_relocs_ + 4;
3579 const size_t packed_relocs_size = android_relocs_size_ - 4;
3580
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003581 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003582 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003583 packed_reloc_iterator<sleb128_decoder>(
3584 sleb128_decoder(packed_relocs, packed_relocs_size)),
3585 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003586
3587 if (!relocated) {
3588 return false;
3589 }
3590 } else {
3591 DL_ERR("bad android relocation header.");
3592 return false;
3593 }
3594 }
3595
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08003596 if (relr_ != nullptr) {
3597 DEBUG("[ relocating %s relr ]", get_realpath());
3598 if (!relocate_relr()) {
3599 return false;
3600 }
3601 }
3602
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003603#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003604 if (rela_ != nullptr) {
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08003605 DEBUG("[ relocating %s rela ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003606 if (!relocate(version_tracker,
3607 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003608 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003609 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003610 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003611 if (plt_rela_ != nullptr) {
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08003612 DEBUG("[ relocating %s plt rela ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003613 if (!relocate(version_tracker,
3614 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003615 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003616 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003617 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003618#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003619 if (rel_ != nullptr) {
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08003620 DEBUG("[ relocating %s rel ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003621 if (!relocate(version_tracker,
3622 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003623 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003624 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003625 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003626 if (plt_rel_ != nullptr) {
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08003627 DEBUG("[ relocating %s plt rel ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003628 if (!relocate(version_tracker,
3629 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003630 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003631 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003632 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003633#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003634
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003635#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003636 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003637 return false;
3638 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003639#endif
3640
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003641 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003642
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003643#if !defined(__LP64__)
3644 if (has_text_relocations) {
3645 // All relocations are done, we can protect our segments back to read-only.
3646 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3647 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003648 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003649 return false;
3650 }
3651 }
3652#endif
3653
Mingwei Shibe910522015-11-12 07:02:14 +00003654 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
3655 // itself --- it can't make system calls yet, and will have to call protect_relro later.
3656 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003657 return false;
3658 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003659
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003660 /* Handle serializing/sharing the RELRO segment */
3661 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3662 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3663 extinfo->relro_fd) < 0) {
3664 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003665 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003666 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003667 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003668 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3669 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3670 extinfo->relro_fd) < 0) {
3671 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003672 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003673 return false;
3674 }
3675 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003676
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003677 notify_gdb_of_load(this);
dimitry965d06d2017-11-28 16:03:07 +01003678 set_image_linked();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003679 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003680}
3681
Mingwei Shibe910522015-11-12 07:02:14 +00003682bool soinfo::protect_relro() {
3683 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3684 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
3685 get_realpath(), strerror(errno));
3686 return false;
3687 }
3688 return true;
3689}
3690
Jiyong Park02586a22017-05-20 01:01:24 +09003691static std::vector<android_namespace_t*> init_default_namespace_no_config(bool is_asan) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003692 g_default_namespace.set_isolated(false);
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003693 auto default_ld_paths = is_asan ? kAsanDefaultLdPaths : kDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003694
neo.chae2589f9d2016-10-04 11:00:27 +09003695 char real_path[PATH_MAX];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003696 std::vector<std::string> ld_default_paths;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003697 for (size_t i = 0; default_ld_paths[i] != nullptr; ++i) {
3698 if (realpath(default_ld_paths[i], real_path) != nullptr) {
neo.chae2589f9d2016-10-04 11:00:27 +09003699 ld_default_paths.push_back(real_path);
3700 } else {
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003701 ld_default_paths.push_back(default_ld_paths[i]);
neo.chae2589f9d2016-10-04 11:00:27 +09003702 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003703 }
3704
3705 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Jiyong Park02586a22017-05-20 01:01:24 +09003706
3707 std::vector<android_namespace_t*> namespaces;
3708 namespaces.push_back(&g_default_namespace);
3709 return namespaces;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003710}
3711
Jiyong Park35833412018-10-26 14:45:21 +09003712// return /apex/<name>/etc/ld.config.txt from /apex/<name>/bin/<exec>
3713static std::string get_ld_config_file_apex_path(const char* executable_path) {
3714 std::vector<std::string> paths = android::base::Split(executable_path, "/");
3715 if (paths.size() == 5 && paths[1] == "apex" && paths[3] == "bin") {
3716 return std::string("/apex/") + paths[2] + "/etc/ld.config.txt";
3717 }
3718 return "";
3719}
3720
Logan Chien886b96e2018-05-08 17:52:18 +08003721static std::string get_ld_config_file_vndk_path() {
Logan Chienbcfe3cf2018-05-08 20:17:26 +08003722 if (android::base::GetBoolProperty("ro.vndk.lite", false)) {
3723 return kLdConfigVndkLiteFilePath;
3724 }
3725
Logan Chien886b96e2018-05-08 17:52:18 +08003726 std::string ld_config_file_vndk = kLdConfigFilePath;
3727 size_t insert_pos = ld_config_file_vndk.find_last_of('.');
3728 if (insert_pos == std::string::npos) {
3729 insert_pos = ld_config_file_vndk.length();
3730 }
3731 ld_config_file_vndk.insert(insert_pos, Config::get_vndk_version_string('.'));
3732 return ld_config_file_vndk;
3733}
3734
Jiyong Park35833412018-10-26 14:45:21 +09003735static std::string get_ld_config_file_path(const char* executable_path) {
Logan Chien886b96e2018-05-08 17:52:18 +08003736#ifdef USE_LD_CONFIG_FILE
3737 // This is a debugging/testing only feature. Must not be available on
3738 // production builds.
3739 const char* ld_config_file_env = getenv("LD_CONFIG_FILE");
3740 if (ld_config_file_env != nullptr && file_exists(ld_config_file_env)) {
3741 return ld_config_file_env;
3742 }
3743#endif
3744
Jiyong Park35833412018-10-26 14:45:21 +09003745 std::string path = get_ld_config_file_apex_path(executable_path);
3746 if (!path.empty()) {
3747 if (file_exists(path.c_str())) {
3748 return path;
3749 }
3750 DL_WARN("Warning: couldn't read config file \"%s\" for \"%s\"",
3751 path.c_str(), executable_path);
Logan Chien886b96e2018-05-08 17:52:18 +08003752 }
3753
Jiyong Park35833412018-10-26 14:45:21 +09003754 path = kLdConfigArchFilePath;
3755 if (file_exists(path.c_str())) {
3756 return path;
3757 }
3758
3759 path = get_ld_config_file_vndk_path();
3760 if (file_exists(path.c_str())) {
3761 return path;
Logan Chien886b96e2018-05-08 17:52:18 +08003762 }
3763
3764 return kLdConfigFilePath;
3765}
3766
Jiyong Park02586a22017-05-20 01:01:24 +09003767std::vector<android_namespace_t*> init_default_namespaces(const char* executable_path) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003768 g_default_namespace.set_name("(default)");
3769
3770 soinfo* somain = solist_get_somain();
3771
3772 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3773 somain->load_bias);
Dan Willemsen7ccc50d2017-09-18 21:28:14 -07003774 const char* bname = (interp != nullptr) ? basename(interp) : nullptr;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003775
3776 g_is_asan = bname != nullptr &&
3777 (strcmp(bname, "linker_asan") == 0 ||
3778 strcmp(bname, "linker_asan64") == 0);
3779
3780 const Config* config = nullptr;
3781
3782 std::string error_msg;
3783
Jiyong Park35833412018-10-26 14:45:21 +09003784 std::string ld_config_file_path = get_ld_config_file_path(executable_path);
Jiyong Park02586a22017-05-20 01:01:24 +09003785
Logan Chien886b96e2018-05-08 17:52:18 +08003786 if (!Config::read_binary_config(ld_config_file_path.c_str(),
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003787 executable_path,
3788 g_is_asan,
3789 &config,
3790 &error_msg)) {
3791 if (!error_msg.empty()) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003792 DL_WARN("Warning: couldn't read \"%s\" for \"%s\" (using default configuration instead): %s",
Logan Chien886b96e2018-05-08 17:52:18 +08003793 ld_config_file_path.c_str(),
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003794 executable_path,
3795 error_msg.c_str());
3796 }
3797 config = nullptr;
3798 }
3799
3800 if (config == nullptr) {
Jiyong Park02586a22017-05-20 01:01:24 +09003801 return init_default_namespace_no_config(g_is_asan);
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003802 }
3803
3804 const auto& namespace_configs = config->namespace_configs();
3805 std::unordered_map<std::string, android_namespace_t*> namespaces;
3806
3807 // 1. Initialize default namespace
3808 const NamespaceConfig* default_ns_config = config->default_namespace_config();
3809
3810 g_default_namespace.set_isolated(default_ns_config->isolated());
3811 g_default_namespace.set_default_library_paths(default_ns_config->search_paths());
3812 g_default_namespace.set_permitted_paths(default_ns_config->permitted_paths());
3813
3814 namespaces[default_ns_config->name()] = &g_default_namespace;
Justin Yun90de9f02017-07-07 16:21:53 +09003815 if (default_ns_config->visible()) {
3816 g_exported_namespaces[default_ns_config->name()] = &g_default_namespace;
3817 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003818
3819 // 2. Initialize other namespaces
3820
3821 for (auto& ns_config : namespace_configs) {
3822 if (namespaces.find(ns_config->name()) != namespaces.end()) {
3823 continue;
3824 }
3825
3826 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
3827 ns->set_name(ns_config->name());
3828 ns->set_isolated(ns_config->isolated());
3829 ns->set_default_library_paths(ns_config->search_paths());
3830 ns->set_permitted_paths(ns_config->permitted_paths());
3831
3832 namespaces[ns_config->name()] = ns;
Jiyong Park01de74e2017-04-03 23:10:37 +09003833 if (ns_config->visible()) {
3834 g_exported_namespaces[ns_config->name()] = ns;
3835 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003836 }
3837
3838 // 3. Establish links between namespaces
3839 for (auto& ns_config : namespace_configs) {
3840 auto it_from = namespaces.find(ns_config->name());
3841 CHECK(it_from != namespaces.end());
3842 android_namespace_t* namespace_from = it_from->second;
3843 for (const NamespaceLinkConfig& ns_link : ns_config->links()) {
3844 auto it_to = namespaces.find(ns_link.ns_name());
3845 CHECK(it_to != namespaces.end());
3846 android_namespace_t* namespace_to = it_to->second;
Logan Chien9ee45912018-01-18 12:05:09 +08003847 if (ns_link.allow_all_shared_libs()) {
3848 link_namespaces_all_libs(namespace_from, namespace_to);
3849 } else {
3850 link_namespaces(namespace_from, namespace_to, ns_link.shared_libs().c_str());
3851 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003852 }
3853 }
3854 // we can no longer rely on the fact that libdl.so is part of default namespace
3855 // this is why we want to add ld-android.so to all namespaces from ld.config.txt
3856 soinfo* ld_android_so = solist_get_head();
dimitry8b142562018-05-09 15:22:38 +02003857
3858 // we also need vdso to be available for all namespaces (if present)
3859 soinfo* vdso = solist_get_vdso();
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003860 for (auto it : namespaces) {
3861 it.second->add_soinfo(ld_android_so);
dimitry8b142562018-05-09 15:22:38 +02003862 if (vdso != nullptr) {
3863 it.second->add_soinfo(vdso);
3864 }
Jiyong Park02586a22017-05-20 01:01:24 +09003865 // somain and ld_preloads are added to these namespaces after LD_PRELOAD libs are linked
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003866 }
3867
3868 set_application_target_sdk_version(config->target_sdk_version());
Jiyong Park02586a22017-05-20 01:01:24 +09003869
3870 std::vector<android_namespace_t*> created_namespaces;
3871 created_namespaces.reserve(namespaces.size());
3872 for (auto kv : namespaces) {
3873 created_namespaces.push_back(kv.second);
3874 }
3875 return created_namespaces;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003876}
Jiyong Park01de74e2017-04-03 23:10:37 +09003877
3878// This function finds a namespace exported in ld.config.txt by its name.
3879// A namespace can be exported by setting .visible property to true.
3880android_namespace_t* get_exported_namespace(const char* name) {
3881 if (name == nullptr) {
3882 return nullptr;
3883 }
3884 auto it = g_exported_namespaces.find(std::string(name));
3885 if (it == g_exported_namespaces.end()) {
3886 return nullptr;
3887 }
3888 return it->second;
3889}