blob: 6826396bacc68b619c33a45f8ee57672e7946c15 [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"
Ryan Pricharde5e69e02019-01-01 18:53:48 -080068#include "linker_tls.h"
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070069#include "linker_utils.h"
tony.ys_liub4474402015-07-29 18:00:22 +080070
Ryan Pricharde5e69e02019-01-01 18:53:48 -080071#include "private/bionic_globals.h"
dimitryfe1b27c2017-08-11 14:43:21 +020072#include "android-base/macros.h"
Elliott Hughes939a7e02015-12-04 15:27:46 -080073#include "android-base/strings.h"
Dimitry Ivanovb996d602016-07-11 18:11:39 -070074#include "android-base/stringprintf.h"
Simon Baldwinaef71952015-01-16 13:22:54 +000075#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076
dimitry06016f22018-01-05 11:39:28 +010077static std::unordered_map<void*, size_t> g_dso_handle_counters;
78
Jiyong Park25bedfd2019-05-16 21:00:39 +090079static bool g_anonymous_namespace_set = false;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -070080static android_namespace_t* g_anonymous_namespace = &g_default_namespace;
Jiyong Park01de74e2017-04-03 23:10:37 +090081static std::unordered_map<std::string, android_namespace_t*> g_exported_namespaces;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070082
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070083static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
84static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020085
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070086static LinkerTypeAllocator<android_namespace_t> g_namespace_allocator;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -070087static LinkerTypeAllocator<LinkedListEntry<android_namespace_t>> g_namespace_list_allocator;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070088
dimitryfe1b27c2017-08-11 14:43:21 +020089static const char* const kLdConfigArchFilePath = "/system/etc/ld.config." ABI_STRING ".txt";
90
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080091static const char* const kLdConfigFilePath = "/system/etc/ld.config.txt";
Logan Chienbcfe3cf2018-05-08 20:17:26 +080092static const char* const kLdConfigVndkLiteFilePath = "/system/etc/ld.config.vndk_lite.txt";
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080093
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070094#if defined(__LP64__)
Victor Chang6cb719f2019-02-06 17:19:10 +000095static const char* const kSystemLibDir = "/system/lib64";
96static const char* const kOdmLibDir = "/odm/lib64";
97static const char* const kVendorLibDir = "/vendor/lib64";
98static const char* const kAsanSystemLibDir = "/data/asan/system/lib64";
99static const char* const kAsanOdmLibDir = "/data/asan/odm/lib64";
100static const char* const kAsanVendorLibDir = "/data/asan/vendor/lib64";
101static const char* const kRuntimeApexLibDir = "/apex/com.android.runtime/lib64";
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700102#else
Victor Chang6cb719f2019-02-06 17:19:10 +0000103static const char* const kSystemLibDir = "/system/lib";
104static const char* const kOdmLibDir = "/odm/lib";
105static const char* const kVendorLibDir = "/vendor/lib";
106static const char* const kAsanSystemLibDir = "/data/asan/system/lib";
107static const char* const kAsanOdmLibDir = "/data/asan/odm/lib";
108static const char* const kAsanVendorLibDir = "/data/asan/vendor/lib";
109static const char* const kRuntimeApexLibDir = "/apex/com.android.runtime/lib";
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700110#endif
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700111
Vishwath Mohan4113def2017-03-29 15:31:34 -0700112static const char* const kAsanLibDirPrefix = "/data/asan";
113
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700114static const char* const kDefaultLdPaths[] = {
115 kSystemLibDir,
Alin Jerpelea074a9fd2017-09-25 17:47:49 +0200116 kOdmLibDir,
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700117 kVendorLibDir,
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700118 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700119};
David Bartleybc3a5c22009-06-02 18:27:28 -0700120
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700121static const char* const kAsanDefaultLdPaths[] = {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700122 kAsanSystemLibDir,
123 kSystemLibDir,
Alin Jerpelea074a9fd2017-09-25 17:47:49 +0200124 kAsanOdmLibDir,
125 kOdmLibDir,
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700126 kAsanVendorLibDir,
127 kVendorLibDir,
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700128 nullptr
129};
130
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700131// Is ASAN enabled?
132static bool g_is_asan = false;
133
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700134static CFIShadowWriter g_cfi_shadow;
135
136CFIShadowWriter* get_cfi_shadow() {
137 return &g_cfi_shadow;
138}
139
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700140static bool is_system_library(const std::string& realpath) {
141 for (const auto& dir : g_default_namespace.get_default_library_paths()) {
142 if (file_is_in_dir(realpath, dir)) {
143 return true;
144 }
145 }
146 return false;
147}
148
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700149// Checks if the file exists and not a directory.
150static bool file_exists(const char* path) {
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700151 struct stat s;
152
153 if (stat(path, &s) != 0) {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700154 return false;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700155 }
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700156
157 return S_ISREG(s.st_mode);
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700158}
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700159
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800160static std::string resolve_soname(const std::string& name) {
161 // We assume that soname equals to basename here
162
163 // TODO(dimitry): consider having honest absolute-path -> soname resolution
164 // note that since we might end up refusing to load this library because
165 // it is not in shared libs list we need to get the soname without actually loading
166 // the library.
167 //
168 // On the other hand there are several places where we already assume that
169 // soname == basename in particular for any not-loaded library mentioned
170 // in DT_NEEDED list.
171 return basename(name.c_str());
172}
173
174static bool maybe_accessible_via_namespace_links(android_namespace_t* ns, const char* name) {
175 std::string soname = resolve_soname(name);
176 for (auto& ns_link : ns->linked_namespaces()) {
177 if (ns_link.is_accessible(soname.c_str())) {
178 return true;
179 }
180 }
181
182 return false;
183}
184
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700185// TODO(dimitry): The grey-list is a workaround for http://b/26394120 ---
186// gradually remove libraries from this list until it is gone.
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800187static bool is_greylisted(android_namespace_t* ns, const char* name, const soinfo* needed_by) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700188 static const char* const kLibraryGreyList[] = {
189 "libandroid_runtime.so",
190 "libbinder.so",
191 "libcrypto.so",
192 "libcutils.so",
193 "libexpat.so",
194 "libgui.so",
195 "libmedia.so",
196 "libnativehelper.so",
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700197 "libssl.so",
198 "libstagefright.so",
199 "libsqlite.so",
200 "libui.so",
201 "libutils.so",
202 "libvorbisidec.so",
203 nullptr
204 };
205
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800206 // If you're targeting N, you don't get the greylist.
Elliott Hughes9e27e582017-03-23 17:42:49 -0700207 if (g_greylist_disabled || get_application_target_sdk_version() >= __ANDROID_API_N__) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700208 return false;
209 }
210
211 // if the library needed by a system library - implicitly assume it
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800212 // is greylisted unless it is in the list of shared libraries for one or
213 // more linked namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700214 if (needed_by != nullptr && is_system_library(needed_by->get_realpath())) {
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800215 return !maybe_accessible_via_namespace_links(ns, name);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700216 }
217
218 // if this is an absolute path - make sure it points to /system/lib(64)
219 if (name[0] == '/' && dirname(name) == kSystemLibDir) {
220 // and reduce the path to basename
221 name = basename(name);
222 }
223
224 for (size_t i = 0; kLibraryGreyList[i] != nullptr; ++i) {
225 if (strcmp(name, kLibraryGreyList[i]) == 0) {
226 return true;
227 }
228 }
229
230 return false;
231}
232// END OF WORKAROUND
233
Victor Chang6cb719f2019-02-06 17:19:10 +0000234// Workaround for dlopen(/system/lib(64)/<soname>) when .so is in /apex. http://b/121248172
235/**
236 * Translate /system path to /apex path if needed
237 * The workaround should work only when targetSdkVersion < Q.
238 *
239 * param out_name_to_apex pointing to /apex path
240 * return true if translation is needed
241 */
242static bool translateSystemPathToApexPath(const char* name, std::string* out_name_to_apex) {
243 static const char* const kSystemToRuntimeApexLibs[] = {
244 "libicuuc.so",
245 "libicui18n.so",
246 };
247 // New mapping for new apex should be added below
248
249 // Nothing to do if target sdk version is Q or above
250 if (get_application_target_sdk_version() >= __ANDROID_API_Q__) {
251 return false;
252 }
253
254 // If the path isn't /system/lib, there's nothing to do.
255 if (name == nullptr || dirname(name) != kSystemLibDir) {
256 return false;
257 }
258
259 const char* base_name = basename(name);
260
261 for (const char* soname : kSystemToRuntimeApexLibs) {
262 if (strcmp(base_name, soname) == 0) {
263 *out_name_to_apex = std::string(kRuntimeApexLibDir) + "/" + base_name;
264 return true;
265 }
266 }
267
268 return false;
269}
270// End Workaround for dlopen(/system/lib/<soname>) when .so is in /apex.
271
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700272static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800273
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700275struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700276 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700277};
278
279static linker_stats_t linker_stats;
280
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800281void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700282 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700283}
Vic Yang542db792019-07-25 10:39:27 -0700284
285void print_linker_stats() {
Vic Yang521ad072019-07-25 11:05:33 -0700286 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol (%d cached)",
287 g_argv[0],
Vic Yang542db792019-07-25 10:39:27 -0700288 linker_stats.count[kRelocAbsolute],
289 linker_stats.count[kRelocRelative],
290 linker_stats.count[kRelocCopy],
Vic Yang521ad072019-07-25 11:05:33 -0700291 linker_stats.count[kRelocSymbol],
292 linker_stats.count[kRelocSymbolCached]);
Vic Yang542db792019-07-25 10:39:27 -0700293}
Elliott Hughesbedfe382012-08-14 14:07:59 -0700294#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800295void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700296}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800297#endif
298
299#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800300uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800301#endif
302
Elliott Hughesbedfe382012-08-14 14:07:59 -0700303static void notify_gdb_of_load(soinfo* info) {
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800304 if (info->is_linker() || info->is_main_executable()) {
305 // gdb already knows about the linker and the main executable.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700306 return;
307 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800308
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800309 link_map* map = &(info->link_map_head);
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000310
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800311 map->l_addr = info->load_bias;
312 // link_map l_name field is not const.
313 map->l_name = const_cast<char*>(info->get_realpath());
314 map->l_ld = info->dynamic;
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000315
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800316 CHECK(map->l_name != nullptr);
317 CHECK(map->l_name[0] != '\0');
318
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800319 notify_gdb_of_load(map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700320}
321
Elliott Hughesbedfe382012-08-14 14:07:59 -0700322static void notify_gdb_of_unload(soinfo* info) {
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800323 notify_gdb_of_unload(&(info->link_map_head));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800324}
325
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700326LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
327 return g_soinfo_links_allocator.alloc();
328}
329
330void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
331 g_soinfo_links_allocator.free(entry);
332}
333
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700334LinkedListEntry<android_namespace_t>* NamespaceListAllocator::alloc() {
335 return g_namespace_list_allocator.alloc();
336}
337
338void NamespaceListAllocator::free(LinkedListEntry<android_namespace_t>* entry) {
339 g_namespace_list_allocator.free(entry);
340}
341
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700342soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
Ryan Prichard8f639a42018-10-01 23:10:05 -0700343 const struct stat* file_stat, off64_t file_offset,
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700344 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700345 if (strlen(name) >= PATH_MAX) {
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700346 async_safe_fatal("library name \"%s\" too long", name);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200347 }
348
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700349 TRACE("name %s: allocating soinfo for ns=%p", name, ns);
350
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700351 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
352 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700353
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700354 solist_add_soinfo(si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200355
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700356 si->generate_handle();
357 ns->add_soinfo(si);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700358
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700359 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200360 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800361}
362
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800363static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700364 if (si == nullptr) {
365 return;
366 }
367
368 if (si->base != 0 && si->size != 0) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800369 if (!si->is_mapped_by_caller()) {
370 munmap(reinterpret_cast<void*>(si->base), si->size);
371 } else {
372 // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE
373 mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE,
374 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
375 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700376 }
377
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700378 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700379
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700380 if (!solist_remove_soinfo(si)) {
dimitry965d06d2017-11-28 16:03:07 +0100381 async_safe_fatal("soinfo=%p is not in soinfo_list (double unload?)", si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700382 }
Elliott Hughes46882792012-08-03 16:49:39 -0700383
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700384 // clear links to/from si
385 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700386
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700387 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700388 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800389}
390
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700391static void parse_path(const char* path, const char* delimiters,
392 std::vector<std::string>* resolved_paths) {
393 std::vector<std::string> paths;
394 split_path(path, delimiters, &paths);
395 resolve_paths(paths, resolved_paths);
396}
397
Elliott Hughescade4c32012-12-20 14:42:14 -0800398static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700399 std::vector<std::string> ld_libary_paths;
400 parse_path(path, ":", &ld_libary_paths);
401 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800402}
403
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700404static bool realpath_fd(int fd, std::string* realpath) {
Vic Yangd8bef672019-05-15 21:10:22 -0700405 // proc_self_fd needs to be large enough to hold "/proc/self/fd/" plus an
406 // integer, plus the NULL terminator.
407 char proc_self_fd[32];
408 // We want to statically allocate this large buffer so that we don't grow
409 // the stack by too much.
410 static char buf[PATH_MAX];
411
412 async_safe_format_buffer(proc_self_fd, sizeof(proc_self_fd), "/proc/self/fd/%d", fd);
413 auto length = readlink(proc_self_fd, buf, sizeof(buf));
414 if (length == -1) {
Tom Cherry66bc4282018-11-08 13:40:52 -0800415 if (!is_first_stage_init()) {
Vic Yangd8bef672019-05-15 21:10:22 -0700416 PRINT("readlink(\"%s\") failed: %s [fd=%d]", proc_self_fd, strerror(errno), fd);
Tom Cherry66bc4282018-11-08 13:40:52 -0800417 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700418 return false;
419 }
420
Vic Yangd8bef672019-05-15 21:10:22 -0700421 realpath->assign(buf, length);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700422 return true;
423}
424
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700425#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700426
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700427// For a given PC, find the .so that it belongs to.
428// Returns the base address of the .ARM.exidx section
429// for that .so, and the number of 8-byte entries
430// in that section (via *pcount).
431//
432// Intended to be called by libc's __gnu_Unwind_Find_exidx().
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800433_Unwind_Ptr do_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Peter Collingbourneb39cb3c2019-03-01 13:12:49 -0800434 if (soinfo* si = find_containing_library(reinterpret_cast<void*>(pc))) {
435 *pcount = si->ARM_exidx_count;
436 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700437 }
438 *pcount = 0;
Elliott Hughesf2c6ad62017-04-21 10:25:56 -0700439 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800440}
Elliott Hughes46882792012-08-03 16:49:39 -0700441
Christopher Ferris24053a42013-08-19 17:45:09 -0700442#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700443
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700444// Here, we only have to provide a callback to iterate across all the
445// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700446int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700447 int rv = 0;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700448 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700449 dl_phdr_info dl_info;
450 dl_info.dlpi_addr = si->link_map_head.l_addr;
451 dl_info.dlpi_name = si->link_map_head.l_name;
452 dl_info.dlpi_phdr = si->phdr;
453 dl_info.dlpi_phnum = si->phnum;
454 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
455 if (rv != 0) {
456 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800457 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700458 }
459 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800460}
Elliott Hughes46882792012-08-03 16:49:39 -0700461
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800462
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700463bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700464 soinfo** si_found_in, const soinfo_list_t& global_group,
465 const soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800466 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700467 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700468
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700469 /* "This element's presence in a shared object library alters the dynamic linker's
470 * symbol resolution algorithm for references within the library. Instead of starting
471 * a symbol search with the executable file, the dynamic linker starts from the shared
472 * object itself. If the shared object fails to supply the referenced symbol, the
473 * dynamic linker then searches the executable file and other shared objects as usual."
474 *
475 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
476 *
477 * Note that this is unlikely since static linker avoids generating
478 * relocations for -Bsymbolic linked dynamic executables.
479 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700480 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700481 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700482 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
483 return false;
484 }
485
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700486 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700487 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700488 }
489 }
490
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700491 // 1. Look for it in global_group
492 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700493 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700494 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700495 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700496 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700497 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
498 error = true;
499 return false;
500 }
501
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700502 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700503 *si_found_in = global_si;
504 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700505 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700506
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700507 return true;
508 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700509
510 if (error) {
511 return false;
512 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700513 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700514
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700515 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700516 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700517 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700518 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700519 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700520 // we already did this - skip
521 return true;
522 }
523
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700524 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700525 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700526 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
527 error = true;
528 return false;
529 }
530
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700531 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700532 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700533 return false;
534 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700535
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700536 return true;
537 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700538
539 if (error) {
540 return false;
541 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700542 }
543
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700544 if (s != nullptr) {
545 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
546 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700547 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
548 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700549 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700550 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700551
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700552 *symbol = s;
553 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700554}
555
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700556ProtectedDataGuard::ProtectedDataGuard() {
557 if (ref_count_++ == 0) {
558 protect_data(PROT_READ | PROT_WRITE);
559 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700560
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700561 if (ref_count_ == 0) { // overflow
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700562 async_safe_fatal("Too many nested calls to dlopen()");
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800563 }
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800564}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800565
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800566ProtectedDataGuard::~ProtectedDataGuard() {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700567 if (--ref_count_ == 0) {
568 protect_data(PROT_READ);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800569 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700570}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800571
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700572void ProtectedDataGuard::protect_data(int protection) {
573 g_soinfo_allocator.protect_all(protection);
574 g_soinfo_links_allocator.protect_all(protection);
575 g_namespace_allocator.protect_all(protection);
576 g_namespace_list_allocator.protect_all(protection);
577}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800578
579size_t ProtectedDataGuard::ref_count_ = 0;
580
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700581// Each size has it's own allocator.
582template<size_t size>
583class SizeBasedAllocator {
584 public:
585 static void* alloc() {
586 return allocator_.alloc();
587 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700588
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700589 static void free(void* ptr) {
590 allocator_.free(ptr);
591 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700592
Vic Yangbb7e1232019-01-29 20:23:16 -0800593 static void purge() {
594 allocator_.purge();
595 }
596
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700597 private:
598 static LinkerBlockAllocator allocator_;
599};
600
601template<size_t size>
602LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
603
604template<typename T>
605class TypeBasedAllocator {
606 public:
607 static T* alloc() {
608 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
609 }
610
611 static void free(T* ptr) {
612 SizeBasedAllocator<sizeof(T)>::free(ptr);
613 }
Vic Yangbb7e1232019-01-29 20:23:16 -0800614
615 static void purge() {
616 SizeBasedAllocator<sizeof(T)>::purge();
617 }
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700618};
619
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700620class LoadTask {
621 public:
622 struct deleter_t {
623 void operator()(LoadTask* t) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700624 t->~LoadTask();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700625 TypeBasedAllocator<LoadTask>::free(t);
626 }
627 };
628
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700629 static deleter_t deleter;
630
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800631 static LoadTask* create(const char* name,
632 soinfo* needed_by,
Jiyong Park02586a22017-05-20 01:01:24 +0900633 android_namespace_t* start_from,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700634 std::unordered_map<const soinfo*, ElfReader>* readers_map) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700635 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
Jiyong Park02586a22017-05-20 01:01:24 +0900636 return new (ptr) LoadTask(name, needed_by, start_from, readers_map);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700637 }
638
639 const char* get_name() const {
640 return name_;
641 }
642
643 soinfo* get_needed_by() const {
644 return needed_by_;
645 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700646
647 soinfo* get_soinfo() const {
648 return si_;
649 }
650
651 void set_soinfo(soinfo* si) {
652 si_ = si;
653 }
654
655 off64_t get_file_offset() const {
656 return file_offset_;
657 }
658
659 void set_file_offset(off64_t offset) {
660 file_offset_ = offset;
661 }
662
663 int get_fd() const {
664 return fd_;
665 }
666
667 void set_fd(int fd, bool assume_ownership) {
Martin Stjernholmde853ff2019-01-17 00:18:44 +0000668 if (fd_ != -1 && close_fd_) {
669 close(fd_);
670 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700671 fd_ = fd;
672 close_fd_ = assume_ownership;
673 }
674
675 const android_dlextinfo* get_extinfo() const {
676 return extinfo_;
677 }
678
679 void set_extinfo(const android_dlextinfo* extinfo) {
680 extinfo_ = extinfo;
681 }
682
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700683 bool is_dt_needed() const {
684 return is_dt_needed_;
685 }
686
687 void set_dt_needed(bool is_dt_needed) {
688 is_dt_needed_ = is_dt_needed;
689 }
690
Jiyong Park02586a22017-05-20 01:01:24 +0900691 // returns the namespace from where we need to start loading this.
692 const android_namespace_t* get_start_from() const {
693 return start_from_;
694 }
695
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700696 const ElfReader& get_elf_reader() const {
697 CHECK(si_ != nullptr);
698 return (*elf_readers_map_)[si_];
699 }
700
701 ElfReader& get_elf_reader() {
702 CHECK(si_ != nullptr);
703 return (*elf_readers_map_)[si_];
704 }
705
706 std::unordered_map<const soinfo*, ElfReader>* get_readers_map() {
707 return elf_readers_map_;
708 }
709
710 bool read(const char* realpath, off64_t file_size) {
711 ElfReader& elf_reader = get_elf_reader();
712 return elf_reader.Read(realpath, fd_, file_offset_, file_size);
713 }
714
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400715 bool load(address_space_params* address_space) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700716 ElfReader& elf_reader = get_elf_reader();
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400717 if (!elf_reader.Load(address_space)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700718 return false;
719 }
720
721 si_->base = elf_reader.load_start();
722 si_->size = elf_reader.load_size();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800723 si_->set_mapped_by_caller(elf_reader.is_mapped_by_caller());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700724 si_->load_bias = elf_reader.load_bias();
725 si_->phnum = elf_reader.phdr_count();
726 si_->phdr = elf_reader.loaded_phdr();
727
728 return true;
729 }
730
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700731 private:
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800732 LoadTask(const char* name,
733 soinfo* needed_by,
Jiyong Park02586a22017-05-20 01:01:24 +0900734 android_namespace_t* start_from,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700735 std::unordered_map<const soinfo*, ElfReader>* readers_map)
736 : name_(name), needed_by_(needed_by), si_(nullptr),
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700737 fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map),
Jiyong Park02586a22017-05-20 01:01:24 +0900738 is_dt_needed_(false), start_from_(start_from) {}
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700739
740 ~LoadTask() {
741 if (fd_ != -1 && close_fd_) {
742 close(fd_);
743 }
744 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700745
746 const char* name_;
747 soinfo* needed_by_;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700748 soinfo* si_;
749 const android_dlextinfo* extinfo_;
750 int fd_;
751 bool close_fd_;
752 off64_t file_offset_;
753 std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_;
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700754 // TODO(dimitry): needed by workaround for http://b/26394120 (the grey-list)
755 bool is_dt_needed_;
756 // END OF WORKAROUND
Jiyong Park02586a22017-05-20 01:01:24 +0900757 const android_namespace_t* const start_from_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700758
759 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
760};
761
Ningsheng Jiane93be992014-09-16 15:22:10 +0800762LoadTask::deleter_t LoadTask::deleter;
763
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700764template <typename T>
765using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
766
767typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700768typedef linked_list_t<const char> StringLinkedList;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700769typedef std::vector<LoadTask*> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700770
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800771enum walk_action_result_t : uint32_t {
772 kWalkStop = 0,
773 kWalkContinue = 1,
774 kWalkSkip = 2
775};
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700776
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700777// This function walks down the tree of soinfo dependencies
778// in breadth-first order and
779// * calls action(soinfo* si) for each node, and
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800780// * terminates walk if action returns kWalkStop
781// * skips children of the node if action
782// return kWalkSkip
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700783//
784// walk_dependencies_tree returns false if walk was terminated
785// by the action and true otherwise.
786template<typename F>
dimitry965d06d2017-11-28 16:03:07 +0100787static bool walk_dependencies_tree(soinfo* root_soinfo, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700788 SoinfoLinkedList visit_list;
789 SoinfoLinkedList visited;
790
dimitry965d06d2017-11-28 16:03:07 +0100791 visit_list.push_back(root_soinfo);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700792
793 soinfo* si;
794 while ((si = visit_list.pop_front()) != nullptr) {
795 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700796 continue;
797 }
798
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800799 walk_action_result_t result = action(si);
800
801 if (result == kWalkStop) {
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700802 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700803 }
804
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700805 visited.push_back(si);
806
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800807 if (result != kWalkSkip) {
808 si->get_children().for_each([&](soinfo* child) {
809 visit_list.push_back(child);
810 });
811 }
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700812 }
813
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700814 return true;
815}
816
817
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800818static const ElfW(Sym)* dlsym_handle_lookup(android_namespace_t* ns,
819 soinfo* root,
820 soinfo* skip_until,
821 soinfo** found,
822 SymbolName& symbol_name,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800823 const version_info* vi) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700824 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700825 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700826
dimitry965d06d2017-11-28 16:03:07 +0100827 walk_dependencies_tree(root, [&](soinfo* current_soinfo) {
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700828 if (skip_lookup) {
829 skip_lookup = current_soinfo != skip_until;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800830 return kWalkContinue;
831 }
832
833 if (!ns->is_accessible(current_soinfo)) {
834 return kWalkSkip;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700835 }
836
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800837 if (!current_soinfo->find_symbol_by_name(symbol_name, vi, &result)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700838 result = nullptr;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800839 return kWalkStop;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700840 }
841
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700842 if (result != nullptr) {
843 *found = current_soinfo;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800844 return kWalkStop;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700845 }
846
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800847 return kWalkContinue;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700848 });
849
850 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800851}
852
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800853static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
854 const char* name,
855 const version_info* vi,
856 soinfo** found,
857 soinfo* caller,
858 void* handle);
859
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700860// This is used by dlsym(3). It performs symbol lookup only within the
861// specified soinfo object and its dependencies in breadth first order.
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800862static const ElfW(Sym)* dlsym_handle_lookup(soinfo* si,
863 soinfo** found,
864 const char* name,
865 const version_info* vi) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700866 // According to man dlopen(3) and posix docs in the case when si is handle
867 // of the main executable we need to search not only in the executable and its
868 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
869 //
870 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
871 // libraries and they are loaded in breath-first (correct) order we can just execute
872 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700873 if (si == solist_get_somain()) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800874 return dlsym_linear_lookup(&g_default_namespace, name, vi, found, nullptr, RTLD_DEFAULT);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700875 }
876
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700877 SymbolName symbol_name(name);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800878 // note that the namespace is not the namespace associated with caller_addr
879 // we use ns associated with root si intentionally here. Using caller_ns
880 // causes problems when user uses dlopen_ext to open a library in the separate
881 // namespace and then calls dlsym() on the handle.
882 return dlsym_handle_lookup(si->get_primary_namespace(), si, nullptr, found, symbol_name, vi);
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700883}
884
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800885/* This is used by dlsym(3) to performs a global symbol lookup. If the
886 start value is null (for RTLD_DEFAULT), the search starts at the
887 beginning of the global solist. Otherwise the search starts at the
888 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700889 */
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800890static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
891 const char* name,
892 const version_info* vi,
893 soinfo** found,
894 soinfo* caller,
895 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800896 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800897
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700898 auto& soinfo_list = ns->soinfo_list();
899 auto start = soinfo_list.begin();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700900
901 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -0700902 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700903 return nullptr;
904 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700905 auto it = soinfo_list.find(caller);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700906 CHECK (it != soinfo_list.end());
907 start = ++it;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700908 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800909 }
910
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700911 const ElfW(Sym)* s = nullptr;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700912 for (auto it = start, end = soinfo_list.end(); it != end; ++it) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700913 soinfo* si = *it;
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700914 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800915 // if the library is opened by application with target api level < M.
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700916 // See http://b/21565766
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800917 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 &&
918 si->get_target_sdk_version() >= __ANDROID_API_M__) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700919 continue;
920 }
921
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800922 if (!si->find_symbol_by_name(symbol_name, vi, &s)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700923 return nullptr;
924 }
925
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700926 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800927 *found = si;
928 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600929 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800930 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600931
dimitry153168c2018-02-20 16:51:41 +0100932 // If not found - use dlsym_handle_lookup for caller's local_group
933 if (s == nullptr && caller != nullptr) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800934 soinfo* local_group_root = caller->get_local_group_root();
935
936 return dlsym_handle_lookup(local_group_root->get_primary_namespace(),
937 local_group_root,
938 (handle == RTLD_NEXT) ? caller : nullptr,
939 found,
940 symbol_name,
941 vi);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700942 }
943
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700944 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700945 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
946 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800947 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800948
Elliott Hughescade4c32012-12-20 14:42:14 -0800949 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800950}
951
Kito Chengfa8c05d2013-03-12 14:58:06 +0800952soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800953 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700954 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Peter Collingbourneb39cb3c2019-03-01 13:12:49 -0800955 if (address < si->base || address - si->base >= si->size) {
956 continue;
957 }
958 ElfW(Addr) vaddr = address - si->load_bias;
959 for (size_t i = 0; i != si->phnum; ++i) {
960 const ElfW(Phdr)* phdr = &si->phdr[i];
961 if (phdr->p_type != PT_LOAD) {
962 continue;
963 }
964 if (vaddr >= phdr->p_vaddr && vaddr < phdr->p_vaddr + phdr->p_memsz) {
965 return si;
966 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600967 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800968 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700969 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600970}
971
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700972class ZipArchiveCache {
973 public:
974 ZipArchiveCache() {}
975 ~ZipArchiveCache();
976
977 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
978 private:
979 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
980
981 std::unordered_map<std::string, ZipArchiveHandle> cache_;
982};
983
984bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
985 std::string key(zip_path);
986
987 auto it = cache_.find(key);
988 if (it != cache_.end()) {
989 *handle = it->second;
990 return true;
991 }
992
993 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
994 if (fd == -1) {
995 return false;
996 }
997
998 if (OpenArchiveFd(fd, "", handle) != 0) {
999 // invalid zip-file (?)
Ryan Prichard0adf09b2018-10-01 18:35:46 -07001000 CloseArchive(*handle);
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001001 return false;
1002 }
1003
1004 cache_[key] = *handle;
1005 return true;
1006}
1007
1008ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -07001009 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001010 CloseArchive(it.second);
1011 }
1012}
1013
1014static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001015 const char* const input_path,
1016 off64_t* file_offset, std::string* realpath) {
1017 std::string normalized_path;
1018 if (!normalize_path(input_path, &normalized_path)) {
1019 return -1;
1020 }
1021
1022 const char* const path = normalized_path.c_str();
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001023 TRACE("Trying zip file open from path \"%s\" -> normalized \"%s\"", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +00001024
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001025 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +00001026 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001027 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +00001028 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001029 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +00001030 if (separator == nullptr) {
1031 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -07001032 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001033
1034 char buf[512];
1035 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
1036 PRINT("Warning: ignoring very long library path: %s", path);
1037 return -1;
1038 }
1039
1040 buf[separator - path] = '\0';
1041
1042 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001043 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +00001044 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1045 if (fd == -1) {
1046 return -1;
1047 }
1048
1049 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001050 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001051 // invalid zip-file (?)
1052 close(fd);
1053 return -1;
1054 }
1055
Simon Baldwinaef71952015-01-16 13:22:54 +00001056 ZipEntry entry;
1057
Elliott Hughesb51bb502019-05-03 22:45:41 -07001058 if (FindEntry(handle, file_path, &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001059 // Entry was not found.
1060 close(fd);
1061 return -1;
1062 }
1063
1064 // Check if it is properly stored
1065 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
1066 close(fd);
1067 return -1;
1068 }
1069
1070 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001071
1072 if (realpath_fd(fd, realpath)) {
1073 *realpath += separator;
1074 } else {
Tom Cherry66bc4282018-11-08 13:40:52 -08001075 if (!is_first_stage_init()) {
1076 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1077 normalized_path.c_str());
1078 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001079 *realpath = normalized_path;
1080 }
1081
Simon Baldwinaef71952015-01-16 13:22:54 +00001082 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001083}
1084
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001085static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001086 int n = async_safe_format_buffer(buf, buf_size, "%s/%s", path, name);
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001087 if (n < 0 || n >= static_cast<int>(buf_size)) {
1088 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1089 return false;
1090 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001091
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001092 return true;
1093}
1094
Ryan Prichard8f639a42018-10-01 23:10:05 -07001095static int open_library_at_path(ZipArchiveCache* zip_archive_cache,
1096 const char* path, off64_t* file_offset,
1097 std::string* realpath) {
1098 int fd = -1;
1099 if (strstr(path, kZipFileSeparator) != nullptr) {
1100 fd = open_library_in_zipfile(zip_archive_cache, path, file_offset, realpath);
1101 }
1102
1103 if (fd == -1) {
1104 fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC));
1105 if (fd != -1) {
1106 *file_offset = 0;
1107 if (!realpath_fd(fd, realpath)) {
Tom Cherry66bc4282018-11-08 13:40:52 -08001108 if (!is_first_stage_init()) {
1109 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1110 path);
1111 }
Ryan Prichard8f639a42018-10-01 23:10:05 -07001112 *realpath = path;
1113 }
1114 }
1115 }
1116
1117 return fd;
1118}
1119
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001120static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1121 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001122 const std::vector<std::string>& paths,
1123 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001124 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001125 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001126 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001127 continue;
1128 }
1129
Ryan Prichard8f639a42018-10-01 23:10:05 -07001130 int fd = open_library_at_path(zip_archive_cache, buf, file_offset, realpath);
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001131 if (fd != -1) {
1132 return fd;
1133 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001134 }
1135
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001136 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001137}
1138
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001139static int open_library(android_namespace_t* ns,
1140 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001141 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001142 off64_t* file_offset, std::string* realpath) {
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001143 TRACE("[ opening %s from namespace %s ]", name, ns->get_name());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001144
Elliott Hughes124fae92012-10-31 14:20:03 -07001145 // 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 -07001146 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001147 int fd = -1;
1148
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001149 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001150 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1151 }
1152
1153 if (fd == -1) {
1154 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001155 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001156 *file_offset = 0;
1157 if (!realpath_fd(fd, realpath)) {
Tom Cherry66bc4282018-11-08 13:40:52 -08001158 if (!is_first_stage_init()) {
1159 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1160 name);
1161 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001162 *realpath = name;
1163 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001164 }
1165 }
1166
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001167 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001168 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001169
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001170 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1171 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 -07001172 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001173 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001174 // Check if the library is accessible
1175 if (fd != -1 && !ns->is_accessible(*realpath)) {
Vic Yang48b69112018-10-24 14:14:26 -07001176 close(fd);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001177 fd = -1;
1178 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001179 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001180
Peter Collingbourneea11be02019-04-30 16:05:13 -07001181#if !defined(__ANDROID_APEX__)
1182 if (fd == -1) {
1183 std::vector<std::string> bootstrap_paths = { std::string(kSystemLibDir) + "/bootstrap" };
1184 fd = open_library_on_paths(zip_archive_cache, name, file_offset, bootstrap_paths, realpath);
1185 }
1186#endif
1187
Elliott Hughes124fae92012-10-31 14:20:03 -07001188 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001189 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001190 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001191
Elliott Hughes124fae92012-10-31 14:20:03 -07001192 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001193}
1194
Ryan Prichard8f639a42018-10-01 23:10:05 -07001195int open_executable(const char* path, off64_t* file_offset, std::string* realpath) {
1196 ZipArchiveCache zip_archive_cache;
1197 return open_library_at_path(&zip_archive_cache, path, file_offset, realpath);
1198}
1199
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001200const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001201#if !defined(__LP64__)
1202 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001203 int app_target_api_level = get_application_target_sdk_version();
1204 if (app_target_api_level < __ANDROID_API_M__) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001205 const char* bname = basename(dt_needed);
1206 if (bname != dt_needed) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001207 DL_WARN_documented_change(__ANDROID_API_M__,
1208 "invalid-dt_needed-entries-enforced-for-api-level-23",
1209 "library \"%s\" has invalid DT_NEEDED entry \"%s\"",
1210 sopath, dt_needed, app_target_api_level);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001211 add_dlwarning(sopath, "invalid DT_NEEDED entry", dt_needed);
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001212 }
1213
1214 return bname;
1215 }
1216#endif
1217 return dt_needed;
1218}
1219
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001220template<typename F>
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001221static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
1222 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1223 if (d->d_tag == DT_NEEDED) {
1224 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1225 }
1226 }
1227}
1228
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001229static bool find_loaded_library_by_inode(android_namespace_t* ns,
1230 const struct stat& file_stat,
1231 off64_t file_offset,
1232 bool search_linked_namespaces,
1233 soinfo** candidate) {
Vic Yang3ec16be2019-06-02 21:10:53 -07001234 if (file_stat.st_dev == 0 || file_stat.st_ino == 0) {
1235 *candidate = nullptr;
1236 return false;
1237 }
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001238
1239 auto predicate = [&](soinfo* si) {
Vic Yang3ec16be2019-06-02 21:10:53 -07001240 return si->get_st_ino() == file_stat.st_ino &&
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001241 si->get_st_dev() == file_stat.st_dev &&
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001242 si->get_file_offset() == file_offset;
1243 };
1244
1245 *candidate = ns->soinfo_list().find_if(predicate);
1246
1247 if (*candidate == nullptr && search_linked_namespaces) {
1248 for (auto& link : ns->linked_namespaces()) {
1249 android_namespace_t* linked_ns = link.linked_namespace();
1250 soinfo* si = linked_ns->soinfo_list().find_if(predicate);
1251
1252 if (si != nullptr && link.is_accessible(si->get_soname())) {
1253 *candidate = si;
1254 return true;
1255 }
1256 }
1257 }
1258
1259 return *candidate != nullptr;
1260}
1261
Evgenii Stepanov9e77a642017-07-27 14:55:44 -07001262static bool find_loaded_library_by_realpath(android_namespace_t* ns, const char* realpath,
1263 bool search_linked_namespaces, soinfo** candidate) {
1264 auto predicate = [&](soinfo* si) { return strcmp(realpath, si->get_realpath()) == 0; };
1265
1266 *candidate = ns->soinfo_list().find_if(predicate);
1267
1268 if (*candidate == nullptr && search_linked_namespaces) {
1269 for (auto& link : ns->linked_namespaces()) {
1270 android_namespace_t* linked_ns = link.linked_namespace();
1271 soinfo* si = linked_ns->soinfo_list().find_if(predicate);
1272
1273 if (si != nullptr && link.is_accessible(si->get_soname())) {
1274 *candidate = si;
1275 return true;
1276 }
1277 }
1278 }
1279
1280 return *candidate != nullptr;
1281}
1282
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001283static bool load_library(android_namespace_t* ns,
1284 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001285 LoadTaskList* load_tasks,
1286 int rtld_flags,
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001287 const std::string& realpath,
1288 bool search_linked_namespaces) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001289 off64_t file_offset = task->get_file_offset();
1290 const char* name = task->get_name();
1291 const android_dlextinfo* extinfo = task->get_extinfo();
1292
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001293 LD_LOG(kLogDlopen, "load_library(ns=%s, task=%s, flags=0x%x, realpath=%s)", ns->get_name(), name,
1294 rtld_flags, realpath.c_str());
1295
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001296 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001297 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001298 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001299 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001300 if (file_offset < 0) {
1301 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001302 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001303 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001304
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001305 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001306 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001307 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001308 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001309 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001310 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001311 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1312 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001313 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001314 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001315
1316 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001317 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1318 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001319 soinfo* si = nullptr;
1320 if (find_loaded_library_by_inode(ns, file_stat, file_offset, search_linked_namespaces, &si)) {
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001321 LD_LOG(kLogDlopen,
1322 "load_library(ns=%s, task=%s): Already loaded under different name/path \"%s\" - "
1323 "will return existing soinfo",
1324 ns->get_name(), name, si->get_realpath());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001325 task->set_soinfo(si);
1326 return true;
1327 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001328 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001329
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001330 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001331 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001332 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001333 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001334
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -07001335 struct statfs fs_stat;
1336 if (TEMP_FAILURE_RETRY(fstatfs(task->get_fd(), &fs_stat)) != 0) {
1337 DL_ERR("unable to fstatfs file for the library \"%s\": %s", name, strerror(errno));
1338 return false;
1339 }
1340
1341 // do not check accessibility using realpath if fd is located on tmpfs
1342 // this enables use of memfd_create() for apps
1343 if ((fs_stat.f_type != TMPFS_MAGIC) && (!ns->is_accessible(realpath))) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001344 // TODO(dimitry): workaround for http://b/26394120 - the grey-list
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001345
1346 // TODO(dimitry) before O release: add a namespace attribute to have this enabled
1347 // only for classloader-namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001348 const soinfo* needed_by = task->is_dt_needed() ? task->get_needed_by() : nullptr;
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001349 if (is_greylisted(ns, name, needed_by)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001350 // print warning only if needed by non-system library
1351 if (needed_by == nullptr || !is_system_library(needed_by->get_realpath())) {
1352 const soinfo* needed_or_dlopened_by = task->get_needed_by();
1353 const char* sopath = needed_or_dlopened_by == nullptr ? "(unknown)" :
1354 needed_or_dlopened_by->get_realpath();
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001355 DL_WARN_documented_change(__ANDROID_API_N__,
1356 "private-api-enforced-for-api-level-24",
1357 "library \"%s\" (\"%s\") needed or dlopened by \"%s\" "
1358 "is not accessible by namespace \"%s\"",
1359 name, realpath.c_str(), sopath, ns->get_name());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001360 add_dlwarning(sopath, "unauthorized access to", name);
1361 }
1362 } else {
1363 // do not load libraries if they are not accessible for the specified namespace.
1364 const char* needed_or_dlopened_by = task->get_needed_by() == nullptr ?
1365 "(unknown)" :
1366 task->get_needed_by()->get_realpath();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001367
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001368 DL_ERR("library \"%s\" needed or dlopened by \"%s\" is not accessible for the namespace \"%s\"",
1369 name, needed_or_dlopened_by, ns->get_name());
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001370
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001371 // do not print this if a library is in the list of shared libraries for linked namespaces
1372 if (!maybe_accessible_via_namespace_links(ns, name)) {
1373 PRINT("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the"
1374 " namespace: [name=\"%s\", ld_library_paths=\"%s\", default_library_paths=\"%s\","
1375 " permitted_paths=\"%s\"]",
1376 name, realpath.c_str(),
1377 needed_or_dlopened_by,
1378 ns->get_name(),
1379 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
1380 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
1381 android::base::Join(ns->get_permitted_paths(), ':').c_str());
1382 }
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001383 return false;
1384 }
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001385 }
1386
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001387 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001388 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001389 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001390 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001391
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001392 task->set_soinfo(si);
1393
1394 // Read the ELF header and some of the segments.
1395 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001396 soinfo_free(si);
1397 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001398 return false;
1399 }
1400
1401 // find and set DT_RUNPATH and dt_soname
1402 // Note that these field values are temporary and are
1403 // going to be overwritten on soinfo::prelink_image
1404 // with values from PT_LOAD segments.
1405 const ElfReader& elf_reader = task->get_elf_reader();
1406 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1407 if (d->d_tag == DT_RUNPATH) {
1408 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1409 }
1410 if (d->d_tag == DT_SONAME) {
1411 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1412 }
1413 }
1414
Dan Willemsen5038ef62018-10-21 17:45:04 -07001415#if !defined(__ANDROID__)
1416 // Bionic on the host currently uses some Android prebuilts, which don't set
1417 // DT_RUNPATH with any relative paths, so they can't find their dependencies.
1418 // b/118058804
1419 if (si->get_dt_runpath().empty()) {
1420 si->set_dt_runpath("$ORIGIN/../lib64:$ORIGIN/lib64");
1421 }
1422#endif
1423
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001424 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001425 LD_LOG(kLogDlopen, "load_library(ns=%s, task=%s): Adding DT_NEEDED task: %s",
1426 ns->get_name(), task->get_name(), name);
Jiyong Park02586a22017-05-20 01:01:24 +09001427 load_tasks->push_back(LoadTask::create(name, si, ns, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001428 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001429
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001430 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001431}
1432
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001433static bool load_library(android_namespace_t* ns,
1434 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001435 ZipArchiveCache* zip_archive_cache,
1436 LoadTaskList* load_tasks,
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001437 int rtld_flags,
1438 bool search_linked_namespaces) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001439 const char* name = task->get_name();
1440 soinfo* needed_by = task->get_needed_by();
1441 const android_dlextinfo* extinfo = task->get_extinfo();
1442
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001443 off64_t file_offset;
1444 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001445 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001446 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001447 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1448 file_offset = extinfo->library_fd_offset;
1449 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001450
1451 if (!realpath_fd(extinfo->library_fd, &realpath)) {
Tom Cherry66bc4282018-11-08 13:40:52 -08001452 if (!is_first_stage_init()) {
1453 PRINT(
1454 "warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1455 "Will use given name.",
1456 name);
1457 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001458 realpath = name;
1459 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001460
1461 task->set_fd(extinfo->library_fd, false);
1462 task->set_file_offset(file_offset);
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001463 return load_library(ns, task, load_tasks, rtld_flags, realpath, search_linked_namespaces);
Spencer Low0346ad72015-04-22 18:06:51 -07001464 }
1465
1466 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001467 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001468 if (fd == -1) {
1469 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001470 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001471 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001472
1473 task->set_fd(fd, true);
1474 task->set_file_offset(file_offset);
1475
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001476 return load_library(ns, task, load_tasks, rtld_flags, realpath, search_linked_namespaces);
Spencer Low0346ad72015-04-22 18:06:51 -07001477}
1478
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001479static bool find_loaded_library_by_soname(android_namespace_t* ns,
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001480 const char* name,
1481 soinfo** candidate) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001482 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001483 const char* soname = si->get_soname();
1484 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dimitry Ivanov3bd90612017-02-01 08:54:43 -08001485 *candidate = si;
1486 return false;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001487 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001488
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001489 return true;
1490 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001491}
1492
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001493// Returns true if library was found and false otherwise
1494static bool find_loaded_library_by_soname(android_namespace_t* ns,
1495 const char* name,
1496 bool search_linked_namespaces,
1497 soinfo** candidate) {
1498 *candidate = nullptr;
1499
1500 // Ignore filename with path.
1501 if (strchr(name, '/') != nullptr) {
1502 return false;
1503 }
1504
1505 bool found = find_loaded_library_by_soname(ns, name, candidate);
1506
1507 if (!found && search_linked_namespaces) {
1508 // if a library was not found - look into linked namespaces
1509 for (auto& link : ns->linked_namespaces()) {
1510 if (!link.is_accessible(name)) {
1511 continue;
1512 }
1513
1514 android_namespace_t* linked_ns = link.linked_namespace();
1515
1516 if (find_loaded_library_by_soname(linked_ns, name, candidate)) {
1517 return true;
1518 }
1519 }
1520 }
1521
1522 return found;
1523}
1524
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001525static bool find_library_in_linked_namespace(const android_namespace_link_t& namespace_link,
Jiyong Park02586a22017-05-20 01:01:24 +09001526 LoadTask* task) {
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001527 android_namespace_t* ns = namespace_link.linked_namespace();
1528
1529 soinfo* candidate;
1530 bool loaded = false;
1531
1532 std::string soname;
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001533 if (find_loaded_library_by_soname(ns, task->get_name(), false, &candidate)) {
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001534 loaded = true;
1535 soname = candidate->get_soname();
1536 } else {
1537 soname = resolve_soname(task->get_name());
1538 }
1539
1540 if (!namespace_link.is_accessible(soname.c_str())) {
1541 // the library is not accessible via namespace_link
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001542 LD_LOG(kLogDlopen,
1543 "find_library_in_linked_namespace(ns=%s, task=%s): Not accessible (soname=%s)",
1544 ns->get_name(), task->get_name(), soname.c_str());
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001545 return false;
1546 }
1547
1548 // if library is already loaded - return it
1549 if (loaded) {
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001550 LD_LOG(kLogDlopen, "find_library_in_linked_namespace(ns=%s, task=%s): Already loaded",
1551 ns->get_name(), task->get_name());
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001552 task->set_soinfo(candidate);
1553 return true;
1554 }
1555
Jiyong Park02586a22017-05-20 01:01:24 +09001556 // returning true with empty soinfo means that the library is okay to be
Logan Chien9ee45912018-01-18 12:05:09 +08001557 // loaded in the namespace but has not yet been loaded there before.
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001558 LD_LOG(kLogDlopen, "find_library_in_linked_namespace(ns=%s, task=%s): Ok to load", ns->get_name(),
1559 task->get_name());
Jiyong Park02586a22017-05-20 01:01:24 +09001560 task->set_soinfo(nullptr);
1561 return true;
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001562}
1563
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001564static bool find_library_internal(android_namespace_t* ns,
1565 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001566 ZipArchiveCache* zip_archive_cache,
1567 LoadTaskList* load_tasks,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001568 int rtld_flags,
1569 bool search_linked_namespaces) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001570 soinfo* candidate;
1571
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001572 if (find_loaded_library_by_soname(ns, task->get_name(), search_linked_namespaces, &candidate)) {
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001573 LD_LOG(kLogDlopen,
1574 "find_library_internal(ns=%s, task=%s): Already loaded (by soname): %s",
1575 ns->get_name(), task->get_name(), candidate->get_realpath());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001576 task->set_soinfo(candidate);
1577 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001578 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001579
1580 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001581 // of this fact is done by load_library.
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001582 TRACE("[ \"%s\" find_loaded_library_by_soname failed (*candidate=%s@%p). Trying harder... ]",
1583 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001584
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001585 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags, search_linked_namespaces)) {
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001586 return true;
1587 }
1588
Jiyong Parkefc503d2019-02-12 01:08:57 +09001589 // TODO(dimitry): workaround for http://b/26394120 (the grey-list)
1590 if (ns->is_greylist_enabled() && is_greylisted(ns, task->get_name(), task->get_needed_by())) {
1591 // For the libs in the greylist, switch to the default namespace and then
1592 // try the load again from there. The library could be loaded from the
1593 // default namespace or from another namespace (e.g. runtime) that is linked
1594 // from the default namespace.
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001595 LD_LOG(kLogDlopen,
1596 "find_library_internal(ns=%s, task=%s): Greylisted library - trying namespace %s",
1597 ns->get_name(), task->get_name(), g_default_namespace.get_name());
Jiyong Parkefc503d2019-02-12 01:08:57 +09001598 ns = &g_default_namespace;
1599 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags,
1600 search_linked_namespaces)) {
1601 return true;
1602 }
1603 }
1604 // END OF WORKAROUND
1605
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001606 if (search_linked_namespaces) {
1607 // if a library was not found - look into linked namespaces
dimitry8db36a52017-10-23 15:10:10 +02001608 // preserve current dlerror in the case it fails.
1609 DlErrorRestorer dlerror_restorer;
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001610 LD_LOG(kLogDlopen, "find_library_internal(ns=%s, task=%s): Trying %zu linked namespaces",
1611 ns->get_name(), task->get_name(), ns->linked_namespaces().size());
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001612 for (auto& linked_namespace : ns->linked_namespaces()) {
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001613 if (find_library_in_linked_namespace(linked_namespace, task)) {
Jiyong Park02586a22017-05-20 01:01:24 +09001614 if (task->get_soinfo() == nullptr) {
1615 // try to load the library - once namespace boundary is crossed
1616 // we need to load a library within separate load_group
1617 // to avoid using symbols from foreign namespace while.
1618 //
1619 // However, actual linking is deferred until when the global group
1620 // is fully identified and is applied to all namespaces.
1621 // Otherwise, the libs in the linked namespace won't get symbols from
1622 // the global group.
1623 if (load_library(linked_namespace.linked_namespace(), task, zip_archive_cache, load_tasks, rtld_flags, false)) {
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001624 LD_LOG(
1625 kLogDlopen, "find_library_internal(ns=%s, task=%s): Found in linked namespace %s",
1626 ns->get_name(), task->get_name(), linked_namespace.linked_namespace()->get_name());
Jiyong Park02586a22017-05-20 01:01:24 +09001627 return true;
1628 }
Jiyong Park02586a22017-05-20 01:01:24 +09001629 } else {
1630 // lib is already loaded
1631 return true;
1632 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001633 }
1634 }
1635 }
1636
1637 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001638}
1639
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001640static void soinfo_unload(soinfo* si);
1641
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001642static void shuffle(std::vector<LoadTask*>* v) {
Tom Cherry66bc4282018-11-08 13:40:52 -08001643 if (is_first_stage_init()) {
1644 // arc4random* is not available in first stage init because /dev/random
1645 // hasn't yet been created.
Jiyong Park31cd08f2018-06-01 19:18:56 +09001646 return;
1647 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001648 for (size_t i = 0, size = v->size(); i < size; ++i) {
1649 size_t n = size - i;
1650 size_t r = arc4random_uniform(n);
1651 std::swap((*v)[n-1], (*v)[r]);
1652 }
1653}
1654
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001655// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1656// not their transitive dependencies) as children of the start_with library.
1657// This is false when find_libraries is called for dlopen(), when newly loaded
1658// libraries must form a disjoint tree.
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001659bool find_libraries(android_namespace_t* ns,
1660 soinfo* start_with,
1661 const char* const library_names[],
1662 size_t library_names_count,
1663 soinfo* soinfos[],
1664 std::vector<soinfo*>* ld_preloads,
1665 size_t ld_preloads_count,
1666 int rtld_flags,
1667 const android_dlextinfo* extinfo,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001668 bool add_as_children,
Jiyong Park02586a22017-05-20 01:01:24 +09001669 bool search_linked_namespaces,
Jiyong Park02586a22017-05-20 01:01:24 +09001670 std::vector<android_namespace_t*>* namespaces) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001671 // Step 0: prepare.
dimitry965d06d2017-11-28 16:03:07 +01001672 std::unordered_map<const soinfo*, ElfReader> readers_map;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001673 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001674
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001675 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001676 const char* name = library_names[i];
Jiyong Park02586a22017-05-20 01:01:24 +09001677 load_tasks.push_back(LoadTask::create(name, start_with, ns, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001678 }
1679
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001680 // If soinfos array is null allocate one on stack.
1681 // The array is needed in case of failure; for example
1682 // when library_names[] = {libone.so, libtwo.so} and libone.so
1683 // is loaded correctly but libtwo.so failed for some reason.
1684 // In this case libone.so should be unloaded on return.
1685 // See also implementation of failure_guard below.
1686
1687 if (soinfos == nullptr) {
1688 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1689 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1690 memset(soinfos, 0, soinfos_size);
1691 }
1692
1693 // list of libraries to link - see step 2.
1694 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001695
Tom Cherryb8ab6182017-04-05 16:20:29 -07001696 auto scope_guard = android::base::make_scope_guard([&]() {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001697 for (LoadTask* t : load_tasks) {
1698 LoadTask::deleter(t);
1699 }
1700 });
1701
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001702 ZipArchiveCache zip_archive_cache;
1703
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001704 // Step 1: expand the list of load_tasks to include
1705 // all DT_NEEDED libraries (do not load them just yet)
1706 for (size_t i = 0; i<load_tasks.size(); ++i) {
1707 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07001708 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001709
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001710 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001711 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001712 task->set_dt_needed(is_dt_needed);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001713
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001714 LD_LOG(kLogDlopen, "find_libraries(ns=%s): task=%s, is_dt_needed=%d", ns->get_name(),
1715 task->get_name(), is_dt_needed);
1716
Jiyong Park02586a22017-05-20 01:01:24 +09001717 // Note: start from the namespace that is stored in the LoadTask. This namespace
1718 // is different from the current namespace when the LoadTask is for a transitive
1719 // dependency and the lib that created the LoadTask is not found in the
1720 // current namespace but in one of the linked namespace.
1721 if (!find_library_internal(const_cast<android_namespace_t*>(task->get_start_from()),
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001722 task,
1723 &zip_archive_cache,
1724 &load_tasks,
1725 rtld_flags,
1726 search_linked_namespaces || is_dt_needed)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001727 return false;
1728 }
1729
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001730 soinfo* si = task->get_soinfo();
1731
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001732 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001733 needed_by->add_child(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001734 }
1735
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001736 // When ld_preloads is not null, the first
1737 // ld_preloads_count libs are in fact ld_preloads.
1738 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001739 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001740 }
1741
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001742 if (soinfos_count < library_names_count) {
1743 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001744 }
1745 }
1746
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001747 // Step 2: Load libraries in random order (see b/24047022)
1748 LoadTaskList load_list;
1749 for (auto&& task : load_tasks) {
1750 soinfo* si = task->get_soinfo();
1751 auto pred = [&](const LoadTask* t) {
1752 return t->get_soinfo() == si;
1753 };
1754
1755 if (!si->is_linked() &&
1756 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
1757 load_list.push_back(task);
1758 }
1759 }
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -04001760 bool reserved_address_recursive = false;
1761 if (extinfo) {
1762 reserved_address_recursive = extinfo->flags & ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE;
1763 }
1764 if (!reserved_address_recursive) {
1765 // Shuffle the load order in the normal case, but not if we are loading all
1766 // the libraries to a reserved address range.
1767 shuffle(&load_list);
1768 }
1769
1770 // Set up address space parameters.
1771 address_space_params extinfo_params, default_params;
1772 size_t relro_fd_offset = 0;
1773 if (extinfo) {
1774 if (extinfo->flags & ANDROID_DLEXT_RESERVED_ADDRESS) {
1775 extinfo_params.start_addr = extinfo->reserved_addr;
1776 extinfo_params.reserved_size = extinfo->reserved_size;
1777 extinfo_params.must_use_address = true;
1778 } else if (extinfo->flags & ANDROID_DLEXT_RESERVED_ADDRESS_HINT) {
1779 extinfo_params.start_addr = extinfo->reserved_addr;
1780 extinfo_params.reserved_size = extinfo->reserved_size;
1781 }
1782 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001783
1784 for (auto&& task : load_list) {
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -04001785 address_space_params* address_space =
1786 (reserved_address_recursive || !task->is_dt_needed()) ? &extinfo_params : &default_params;
1787 if (!task->load(address_space)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001788 return false;
1789 }
1790 }
1791
1792 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
1793 for (auto&& task : load_tasks) {
1794 soinfo* si = task->get_soinfo();
1795 if (!si->is_linked() && !si->prelink_image()) {
1796 return false;
1797 }
Ryan Pricharde5e69e02019-01-01 18:53:48 -08001798 register_soinfo_tls(si);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001799 }
1800
Jiyong Park02586a22017-05-20 01:01:24 +09001801 // Step 4: Construct the global group. Note: DF_1_GLOBAL bit of a library is
1802 // determined at step 3.
1803
1804 // Step 4-1: DF_1_GLOBAL bit is force set for LD_PRELOADed libs because they
1805 // must be added to the global group
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001806 if (ld_preloads != nullptr) {
1807 for (auto&& si : *ld_preloads) {
1808 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
1809 }
1810 }
1811
Jiyong Park02586a22017-05-20 01:01:24 +09001812 // Step 4-2: Gather all DF_1_GLOBAL libs which were newly loaded during this
1813 // run. These will be the new member of the global group
1814 soinfo_list_t new_global_group_members;
1815 for (auto&& task : load_tasks) {
1816 soinfo* si = task->get_soinfo();
1817 if (!si->is_linked() && (si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1818 new_global_group_members.push_back(si);
1819 }
1820 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001821
Jiyong Park02586a22017-05-20 01:01:24 +09001822 // Step 4-3: Add the new global group members to all the linked namespaces
Jiyong Park01162f22017-10-16 15:31:09 +09001823 if (namespaces != nullptr) {
Jiyong Park02586a22017-05-20 01:01:24 +09001824 for (auto linked_ns : *namespaces) {
Jiyong Park01162f22017-10-16 15:31:09 +09001825 for (auto si : new_global_group_members) {
1826 if (si->get_primary_namespace() != linked_ns) {
1827 linked_ns->add_soinfo(si);
1828 si->add_secondary_namespace(linked_ns);
1829 }
Jiyong Park02586a22017-05-20 01:01:24 +09001830 }
1831 }
1832 }
1833
dimitry965d06d2017-11-28 16:03:07 +01001834 // Step 5: Collect roots of local_groups.
1835 // Whenever needed_by->si link crosses a namespace boundary it forms its own local_group.
1836 // Here we collect new roots to link them separately later on. Note that we need to avoid
1837 // collecting duplicates. Also the order is important. They need to be linked in the same
1838 // BFS order we link individual libraries.
1839 std::vector<soinfo*> local_group_roots;
1840 if (start_with != nullptr && add_as_children) {
1841 local_group_roots.push_back(start_with);
1842 } else {
1843 CHECK(soinfos_count == 1);
1844 local_group_roots.push_back(soinfos[0]);
1845 }
1846
Jiyong Park02586a22017-05-20 01:01:24 +09001847 for (auto&& task : load_tasks) {
1848 soinfo* si = task->get_soinfo();
dimitry965d06d2017-11-28 16:03:07 +01001849 soinfo* needed_by = task->get_needed_by();
1850 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
1851 android_namespace_t* needed_by_ns =
1852 is_dt_needed ? needed_by->get_primary_namespace() : ns;
1853
1854 if (!si->is_linked() && si->get_primary_namespace() != needed_by_ns) {
1855 auto it = std::find(local_group_roots.begin(), local_group_roots.end(), si);
1856 LD_LOG(kLogDlopen,
1857 "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",
1858 si->get_realpath(),
1859 si,
1860 si->get_primary_namespace()->get_name(),
1861 si->get_primary_namespace(),
1862 needed_by == nullptr ? "(nullptr)" : needed_by->get_realpath(),
1863 needed_by,
1864 ns->get_name(),
1865 ns,
1866 needed_by_ns->get_name(),
1867 needed_by_ns,
1868 it == local_group_roots.end() ? "yes" : "no");
1869
1870 if (it == local_group_roots.end()) {
1871 local_group_roots.push_back(si);
Jiyong Park02586a22017-05-20 01:01:24 +09001872 }
1873 }
1874 }
1875
dimitry965d06d2017-11-28 16:03:07 +01001876 // Step 6: Link all local groups
1877 for (auto root : local_group_roots) {
1878 soinfo_list_t local_group;
1879 android_namespace_t* local_group_ns = root->get_primary_namespace();
1880
1881 walk_dependencies_tree(root,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001882 [&] (soinfo* si) {
dimitry965d06d2017-11-28 16:03:07 +01001883 if (local_group_ns->is_accessible(si)) {
1884 local_group.push_back(si);
1885 return kWalkContinue;
1886 } else {
1887 return kWalkSkip;
1888 }
1889 });
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001890
dimitry965d06d2017-11-28 16:03:07 +01001891 soinfo_list_t global_group = local_group_ns->get_global_group();
1892 bool linked = local_group.visit([&](soinfo* si) {
Torne (Richard Coles)5d103742019-04-11 12:25:06 -04001893 // Even though local group may contain accessible soinfos from other namespaces
dimitry965d06d2017-11-28 16:03:07 +01001894 // we should avoid linking them (because if they are not linked -> they
1895 // are in the local_group_roots and will be linked later).
1896 if (!si->is_linked() && si->get_primary_namespace() == local_group_ns) {
Torne (Richard Coles)5d103742019-04-11 12:25:06 -04001897 const android_dlextinfo* link_extinfo = nullptr;
1898 if (si == soinfos[0] || reserved_address_recursive) {
1899 // Only forward extinfo for the first library unless the recursive
1900 // flag is set.
1901 link_extinfo = extinfo;
1902 }
1903 if (!si->link_image(global_group, local_group, link_extinfo, &relro_fd_offset) ||
dimitry965d06d2017-11-28 16:03:07 +01001904 !get_cfi_shadow()->AfterLoad(si, solist_get_head())) {
1905 return false;
1906 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001907 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001908
dimitry965d06d2017-11-28 16:03:07 +01001909 return true;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001910 });
Elliott Hughes27f18062017-11-29 18:47:42 +00001911
dimitry965d06d2017-11-28 16:03:07 +01001912 if (!linked) {
1913 return false;
1914 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001915 }
1916
dimitry965d06d2017-11-28 16:03:07 +01001917 // Step 7: Mark all load_tasks as linked and increment refcounts
1918 // for references between load_groups (at this point it does not matter if
1919 // referenced load_groups were loaded by previous dlopen or as part of this
1920 // one on step 6)
1921 if (start_with != nullptr && add_as_children) {
1922 start_with->set_linked();
1923 }
1924
1925 for (auto&& task : load_tasks) {
1926 soinfo* si = task->get_soinfo();
1927 si->set_linked();
1928 }
1929
1930 for (auto&& task : load_tasks) {
1931 soinfo* si = task->get_soinfo();
1932 soinfo* needed_by = task->get_needed_by();
1933 if (needed_by != nullptr &&
1934 needed_by != start_with &&
1935 needed_by->get_local_group_root() != si->get_local_group_root()) {
1936 si->increment_ref_count();
1937 }
1938 }
1939
1940
1941 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001942}
1943
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001944static soinfo* find_library(android_namespace_t* ns,
1945 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001946 const android_dlextinfo* extinfo,
1947 soinfo* needed_by) {
dimitry965d06d2017-11-28 16:03:07 +01001948 soinfo* si = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001949
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001950 if (name == nullptr) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001951 si = solist_get_somain();
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001952 } else if (!find_libraries(ns,
1953 needed_by,
1954 &name,
1955 1,
1956 &si,
1957 nullptr,
1958 0,
1959 rtld_flags,
1960 extinfo,
1961 false /* add_as_children */,
dimitry965d06d2017-11-28 16:03:07 +01001962 true /* search_linked_namespaces */)) {
1963 if (si != nullptr) {
1964 soinfo_unload(si);
1965 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001966 return nullptr;
1967 }
1968
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001969 si->increment_ref_count();
1970
Elliott Hughesd23736e2012-11-01 15:16:56 -07001971 return si;
1972}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001973
dimitry06016f22018-01-05 11:39:28 +01001974static void soinfo_unload_impl(soinfo* root) {
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07001975 ScopedTrace trace((std::string("unload ") + root->get_realpath()).c_str());
dimitry06016f22018-01-05 11:39:28 +01001976 bool is_linked = root->is_linked();
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07001977
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001978 if (!root->can_unload()) {
dimitryc92ce712017-10-27 14:12:53 +02001979 LD_LOG(kLogDlopen,
1980 "... dlclose(root=\"%s\"@%p) ... not unloading - the load group is flagged with NODELETE",
1981 root->get_realpath(),
1982 root);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001983 return;
1984 }
1985
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001986
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001987 soinfo_list_t unload_list;
dimitry965d06d2017-11-28 16:03:07 +01001988 unload_list.push_back(root);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001989
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001990 soinfo_list_t local_unload_list;
1991 soinfo_list_t external_unload_list;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001992 soinfo* si = nullptr;
1993
1994 while ((si = unload_list.pop_front()) != nullptr) {
1995 if (local_unload_list.contains(si)) {
1996 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001997 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001998
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001999 local_unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002000
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002001 if (si->has_min_version(0)) {
2002 soinfo* child = nullptr;
2003 while ((child = si->get_children().pop_front()) != nullptr) {
2004 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
2005 child->get_realpath(), child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002006
Dimitry Ivanovec90e242017-02-10 11:04:20 -08002007 child->get_parents().remove(si);
2008
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002009 if (local_unload_list.contains(child)) {
2010 continue;
2011 } else if (child->is_linked() && child->get_local_group_root() != root) {
2012 external_unload_list.push_back(child);
Dimitry Ivanovec90e242017-02-10 11:04:20 -08002013 } else if (child->get_parents().empty()) {
2014 unload_list.push_back(child);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002015 }
2016 }
2017 } else {
Christopher Ferris7a3681e2017-04-24 17:48:32 -07002018 async_safe_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002019 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002020 }
2021
2022 local_unload_list.for_each([](soinfo* si) {
dimitryc92ce712017-10-27 14:12:53 +02002023 LD_LOG(kLogDlopen,
2024 "... dlclose: calling destructors for \"%s\"@%p ... ",
2025 si->get_realpath(),
2026 si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002027 si->call_destructors();
dimitryc92ce712017-10-27 14:12:53 +02002028 LD_LOG(kLogDlopen,
2029 "... dlclose: calling destructors for \"%s\"@%p ... done",
2030 si->get_realpath(),
2031 si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002032 });
2033
2034 while ((si = local_unload_list.pop_front()) != nullptr) {
dimitryc92ce712017-10-27 14:12:53 +02002035 LD_LOG(kLogDlopen,
2036 "... dlclose: unloading \"%s\"@%p ...",
2037 si->get_realpath(),
2038 si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002039 notify_gdb_of_unload(si);
Ryan Pricharde5e69e02019-01-01 18:53:48 -08002040 unregister_soinfo_tls(si);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07002041 get_cfi_shadow()->BeforeUnload(si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002042 soinfo_free(si);
2043 }
2044
dimitry965d06d2017-11-28 16:03:07 +01002045 if (is_linked) {
2046 while ((si = external_unload_list.pop_front()) != nullptr) {
2047 LD_LOG(kLogDlopen,
2048 "... dlclose: unloading external reference \"%s\"@%p ...",
2049 si->get_realpath(),
2050 si);
2051 soinfo_unload(si);
2052 }
2053 } else {
2054 LD_LOG(kLogDlopen,
2055 "... dlclose: unload_si was not linked - not unloading external references ...");
Dmitriy Ivanova2547052014-11-18 12:03:09 -08002056 }
2057}
2058
dimitry06016f22018-01-05 11:39:28 +01002059static void soinfo_unload(soinfo* unload_si) {
2060 // Note that the library can be loaded but not linked;
2061 // in which case there is no root but we still need
2062 // to walk the tree and unload soinfos involved.
2063 //
2064 // This happens on unsuccessful dlopen, when one of
2065 // the DT_NEEDED libraries could not be linked/found.
2066 bool is_linked = unload_si->is_linked();
2067 soinfo* root = is_linked ? unload_si->get_local_group_root() : unload_si;
2068
2069 LD_LOG(kLogDlopen,
2070 "... dlclose(realpath=\"%s\"@%p) ... load group root is \"%s\"@%p",
2071 unload_si->get_realpath(),
2072 unload_si,
2073 root->get_realpath(),
2074 root);
2075
2076
2077 size_t ref_count = is_linked ? root->decrement_ref_count() : 0;
2078 if (ref_count > 0) {
2079 LD_LOG(kLogDlopen,
2080 "... dlclose(root=\"%s\"@%p) ... not unloading - decrementing ref_count to %zd",
2081 root->get_realpath(),
2082 root,
2083 ref_count);
2084 return;
2085 }
2086
2087 soinfo_unload_impl(root);
2088}
2089
2090void increment_dso_handle_reference_counter(void* dso_handle) {
2091 if (dso_handle == nullptr) {
2092 return;
2093 }
2094
2095 auto it = g_dso_handle_counters.find(dso_handle);
2096 if (it != g_dso_handle_counters.end()) {
2097 CHECK(++it->second != 0);
2098 } else {
2099 soinfo* si = find_containing_library(dso_handle);
2100 if (si != nullptr) {
2101 ProtectedDataGuard guard;
dimitry55547db2018-05-25 14:17:37 +02002102 si->increment_ref_count();
dimitry06016f22018-01-05 11:39:28 +01002103 } else {
2104 async_safe_fatal(
2105 "increment_dso_handle_reference_counter: Couldn't find soinfo by dso_handle=%p",
2106 dso_handle);
2107 }
2108 g_dso_handle_counters[dso_handle] = 1U;
2109 }
2110}
2111
2112void decrement_dso_handle_reference_counter(void* dso_handle) {
2113 if (dso_handle == nullptr) {
2114 return;
2115 }
2116
2117 auto it = g_dso_handle_counters.find(dso_handle);
2118 CHECK(it != g_dso_handle_counters.end());
2119 CHECK(it->second != 0);
2120
2121 if (--it->second == 0) {
2122 soinfo* si = find_containing_library(dso_handle);
2123 if (si != nullptr) {
2124 ProtectedDataGuard guard;
dimitry55547db2018-05-25 14:17:37 +02002125 soinfo_unload(si);
dimitry06016f22018-01-05 11:39:28 +01002126 } else {
2127 async_safe_fatal(
2128 "decrement_dso_handle_reference_counter: Couldn't find soinfo by dso_handle=%p",
2129 dso_handle);
2130 }
2131 g_dso_handle_counters.erase(it);
2132 }
2133}
2134
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002135static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
2136 if (sym_ver == nullptr) {
2137 return sym_name;
2138 }
2139
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002140 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002141}
2142
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002143static android_namespace_t* get_caller_namespace(soinfo* caller) {
2144 return caller != nullptr ? caller->get_primary_namespace() : g_anonymous_namespace;
2145}
2146
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002147void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002148 // Use basic string manipulation calls to avoid snprintf.
2149 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
2150 // When debug malloc is enabled, this call returns 0. This in turn causes
2151 // snprintf to do nothing, which causes libraries to fail to load.
2152 // See b/17302493 for further details.
2153 // Once the above bug is fixed, this code can be modified to use
2154 // snprintf again.
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08002155 const auto& default_ld_paths = g_default_namespace.get_default_library_paths();
2156
2157 size_t required_size = 0;
2158 for (const auto& path : default_ld_paths) {
2159 required_size += path.size() + 1;
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002160 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08002161
2162 if (buffer_size < required_size) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -07002163 async_safe_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
2164 "buffer len %zu, required len %zu", buffer_size, required_size);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002165 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08002166
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002167 char* end = buffer;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08002168 for (size_t i = 0; i < default_ld_paths.size(); ++i) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002169 if (i > 0) *end++ = ':';
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08002170 end = stpcpy(end, default_ld_paths[i].c_str());
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002171 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002172}
2173
Elliott Hughescade4c32012-12-20 14:42:14 -08002174void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08002175 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08002176}
2177
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002178static std::string android_dlextinfo_to_string(const android_dlextinfo* info) {
2179 if (info == nullptr) {
2180 return "(null)";
2181 }
2182
2183 return android::base::StringPrintf("[flags=0x%" PRIx64 ","
2184 " reserved_addr=%p,"
2185 " reserved_size=0x%zx,"
2186 " relro_fd=%d,"
2187 " library_fd=%d,"
2188 " library_fd_offset=0x%" PRIx64 ","
2189 " library_namespace=%s@%p]",
2190 info->flags,
2191 info->reserved_addr,
2192 info->reserved_size,
2193 info->relro_fd,
2194 info->library_fd,
2195 info->library_fd_offset,
2196 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
2197 (info->library_namespace != nullptr ?
2198 info->library_namespace->get_name() : "(null)") : "(n/a)",
2199 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
2200 info->library_namespace : nullptr);
2201}
2202
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08002203void* do_dlopen(const char* name, int flags,
2204 const android_dlextinfo* extinfo,
2205 const void* caller_addr) {
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -07002206 std::string trace_prefix = std::string("dlopen: ") + (name == nullptr ? "(nullptr)" : name);
2207 ScopedTrace trace(trace_prefix.c_str());
2208 ScopedTrace loading_trace((trace_prefix + " - loading and linking").c_str());
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002209 soinfo* const caller = find_containing_library(caller_addr);
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002210 android_namespace_t* ns = get_caller_namespace(caller);
2211
2212 LD_LOG(kLogDlopen,
Victor Chang6cb719f2019-02-06 17:19:10 +00002213 "dlopen(name=\"%s\", flags=0x%x, extinfo=%s, caller=\"%s\", caller_ns=%s@%p, targetSdkVersion=%i) ...",
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002214 name,
2215 flags,
2216 android_dlextinfo_to_string(extinfo).c_str(),
2217 caller == nullptr ? "(null)" : caller->get_realpath(),
2218 ns == nullptr ? "(null)" : ns->get_name(),
Victor Chang6cb719f2019-02-06 17:19:10 +00002219 ns,
2220 get_application_target_sdk_version());
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002221
Vic Yangbb7e1232019-01-29 20:23:16 -08002222 auto purge_guard = android::base::make_scope_guard([&]() { purge_unused_memory(); });
2223
Tom Cherryb8ab6182017-04-05 16:20:29 -07002224 auto failure_guard = android::base::make_scope_guard(
2225 [&]() { LD_LOG(kLogDlopen, "... dlopen failed: %s", linker_get_error_buffer()); });
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002226
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002227 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08002228 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002229 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08002230 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002231
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002232 if (extinfo != nullptr) {
2233 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
2234 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
2235 return nullptr;
2236 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002237
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002238 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07002239 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002240 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
2241 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002242 return nullptr;
2243 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002244
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002245 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
2246 if (extinfo->library_namespace == nullptr) {
2247 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
2248 return nullptr;
2249 }
2250 ns = extinfo->library_namespace;
2251 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00002252 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002253
Victor Chang6cb719f2019-02-06 17:19:10 +00002254 // Workaround for dlopen(/system/lib/<soname>) when .so is in /apex. http://b/121248172
2255 // The workaround works only when targetSdkVersion < Q.
2256 std::string name_to_apex;
2257 if (translateSystemPathToApexPath(name, &name_to_apex)) {
2258 const char* new_name = name_to_apex.c_str();
2259 LD_LOG(kLogDlopen, "dlopen considering translation from %s to APEX path %s",
2260 name,
2261 new_name);
2262 // Some APEXs could be optionally disabled. Only translate the path
2263 // when the old file is absent and the new file exists.
Victor Changaf12c942019-02-11 19:35:24 +00002264 // TODO(b/124218500): Re-enable it once app compat issue is resolved
2265 /*
Victor Chang6cb719f2019-02-06 17:19:10 +00002266 if (file_exists(name)) {
2267 LD_LOG(kLogDlopen, "dlopen %s exists, not translating", name);
Victor Changaf12c942019-02-11 19:35:24 +00002268 } else
2269 */
2270 if (!file_exists(new_name)) {
Victor Chang6cb719f2019-02-06 17:19:10 +00002271 LD_LOG(kLogDlopen, "dlopen %s does not exist, not translating",
2272 new_name);
2273 } else {
2274 LD_LOG(kLogDlopen, "dlopen translation accepted: using %s", new_name);
2275 name = new_name;
2276 }
2277 }
2278 // End Workaround for dlopen(/system/lib/<soname>) when .so is in /apex.
2279
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07002280 std::string asan_name_holder;
2281
2282 const char* translated_name = name;
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08002283 if (g_is_asan && translated_name != nullptr && translated_name[0] == '/') {
Evgenii Stepanov9e77a642017-07-27 14:55:44 -07002284 char original_path[PATH_MAX];
2285 if (realpath(name, original_path) != nullptr) {
2286 asan_name_holder = std::string(kAsanLibDirPrefix) + original_path;
Vishwath Mohan4113def2017-03-29 15:31:34 -07002287 if (file_exists(asan_name_holder.c_str())) {
Evgenii Stepanov9e77a642017-07-27 14:55:44 -07002288 soinfo* si = nullptr;
2289 if (find_loaded_library_by_realpath(ns, original_path, true, &si)) {
2290 PRINT("linker_asan dlopen NOT translating \"%s\" -> \"%s\": library already loaded", name,
2291 asan_name_holder.c_str());
2292 } else {
2293 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
2294 translated_name = asan_name_holder.c_str();
2295 }
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07002296 }
2297 }
2298 }
2299
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002300 ProtectedDataGuard guard;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07002301 soinfo* si = find_library(ns, translated_name, flags, extinfo, caller);
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -07002302 loading_trace.End();
2303
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002304 if (si != nullptr) {
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002305 void* handle = si->to_handle();
2306 LD_LOG(kLogDlopen,
Dimitry Ivanovae4a0c12016-11-21 10:44:35 -08002307 "... dlopen calling constructors: realpath=\"%s\", soname=\"%s\", handle=%p",
2308 si->get_realpath(), si->get_soname(), handle);
2309 si->call_constructors();
Tom Cherryb8ab6182017-04-05 16:20:29 -07002310 failure_guard.Disable();
Dimitry Ivanovae4a0c12016-11-21 10:44:35 -08002311 LD_LOG(kLogDlopen,
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002312 "... dlopen successful: realpath=\"%s\", soname=\"%s\", handle=%p",
2313 si->get_realpath(), si->get_soname(), handle);
2314 return handle;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002315 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002316
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002317 return nullptr;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002318}
2319
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002320int do_dladdr(const void* addr, Dl_info* info) {
2321 // Determine if this address can be found in any library currently mapped.
2322 soinfo* si = find_containing_library(addr);
2323 if (si == nullptr) {
2324 return 0;
2325 }
2326
2327 memset(info, 0, sizeof(Dl_info));
2328
2329 info->dli_fname = si->get_realpath();
2330 // Address at which the shared object is loaded.
2331 info->dli_fbase = reinterpret_cast<void*>(si->base);
2332
2333 // Determine if any symbol in the library contains the specified address.
2334 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
2335 if (sym != nullptr) {
2336 info->dli_sname = si->get_string(sym->st_name);
2337 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
2338 }
2339
2340 return 1;
2341}
2342
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002343static soinfo* soinfo_from_handle(void* handle) {
2344 if ((reinterpret_cast<uintptr_t>(handle) & 1) != 0) {
2345 auto it = g_soinfo_handles_map.find(reinterpret_cast<uintptr_t>(handle));
2346 if (it == g_soinfo_handles_map.end()) {
2347 return nullptr;
2348 } else {
2349 return it->second;
2350 }
2351 }
2352
2353 return static_cast<soinfo*>(handle);
2354}
2355
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08002356bool do_dlsym(void* handle,
2357 const char* sym_name,
2358 const char* sym_ver,
2359 const void* caller_addr,
2360 void** symbol) {
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07002361 ScopedTrace trace("dlsym");
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002362#if !defined(__LP64__)
2363 if (handle == nullptr) {
2364 DL_ERR("dlsym failed: library handle is null");
2365 return false;
2366 }
2367#endif
2368
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002369 soinfo* found = nullptr;
2370 const ElfW(Sym)* sym = nullptr;
2371 soinfo* caller = find_containing_library(caller_addr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002372 android_namespace_t* ns = get_caller_namespace(caller);
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002373 soinfo* si = nullptr;
2374 if (handle != RTLD_DEFAULT && handle != RTLD_NEXT) {
2375 si = soinfo_from_handle(handle);
2376 }
2377
2378 LD_LOG(kLogDlsym,
2379 "dlsym(handle=%p(\"%s\"), sym_name=\"%s\", sym_ver=\"%s\", caller=\"%s\", caller_ns=%s@%p) ...",
2380 handle,
2381 si != nullptr ? si->get_realpath() : "n/a",
2382 sym_name,
2383 sym_ver,
2384 caller == nullptr ? "(null)" : caller->get_realpath(),
2385 ns == nullptr ? "(null)" : ns->get_name(),
2386 ns);
2387
Tom Cherryb8ab6182017-04-05 16:20:29 -07002388 auto failure_guard = android::base::make_scope_guard(
2389 [&]() { LD_LOG(kLogDlsym, "... dlsym failed: %s", linker_get_error_buffer()); });
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002390
2391 if (sym_name == nullptr) {
2392 DL_ERR("dlsym failed: symbol name is null");
2393 return false;
2394 }
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002395
2396 version_info vi_instance;
2397 version_info* vi = nullptr;
2398
2399 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002400 vi_instance.name = sym_ver;
2401 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002402 vi = &vi_instance;
2403 }
2404
2405 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2406 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2407 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002408 if (si == nullptr) {
2409 DL_ERR("dlsym failed: invalid handle: %p", handle);
2410 return false;
2411 }
2412 sym = dlsym_handle_lookup(si, &found, sym_name, vi);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002413 }
2414
2415 if (sym != nullptr) {
2416 uint32_t bind = ELF_ST_BIND(sym->st_info);
Ryan Pricharde4d620b2019-04-01 17:42:14 -07002417 uint32_t type = ELF_ST_TYPE(sym->st_info);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002418
2419 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
Ryan Pricharde4d620b2019-04-01 17:42:14 -07002420 if (type == STT_TLS) {
2421 // For a TLS symbol, dlsym returns the address of the current thread's
2422 // copy of the symbol. This function may allocate a DTV and/or storage
2423 // for the source TLS module. (Allocating a DTV isn't necessary if the
2424 // symbol is part of static TLS, but it's simpler to reuse
2425 // __tls_get_addr.)
2426 soinfo_tls* tls_module = found->get_tls();
2427 if (tls_module == nullptr) {
2428 DL_ERR("TLS symbol \"%s\" in solib \"%s\" with no TLS segment",
2429 sym_name, found->get_realpath());
2430 return false;
2431 }
2432 const TlsIndex ti { tls_module->module_id, sym->st_value };
2433 *symbol = TLS_GET_ADDR(&ti);
2434 } else {
2435 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
2436 }
Tom Cherryb8ab6182017-04-05 16:20:29 -07002437 failure_guard.Disable();
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002438 LD_LOG(kLogDlsym,
2439 "... dlsym successful: sym_name=\"%s\", sym_ver=\"%s\", found in=\"%s\", address=%p",
2440 sym_name, sym_ver, found->get_soname(), *symbol);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002441 return true;
2442 }
2443
2444 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2445 return false;
2446 }
2447
2448 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2449 return false;
2450}
2451
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002452int do_dlclose(void* handle) {
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07002453 ScopedTrace trace("dlclose");
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002454 ProtectedDataGuard guard;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002455 soinfo* si = soinfo_from_handle(handle);
2456 if (si == nullptr) {
2457 DL_ERR("invalid handle: %p", handle);
2458 return -1;
2459 }
2460
dimitryc92ce712017-10-27 14:12:53 +02002461 LD_LOG(kLogDlopen,
2462 "dlclose(handle=%p, realpath=\"%s\"@%p) ...",
2463 handle,
2464 si->get_realpath(),
2465 si);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002466 soinfo_unload(si);
dimitryc92ce712017-10-27 14:12:53 +02002467 LD_LOG(kLogDlopen,
2468 "dlclose(handle=%p) ... done",
2469 handle);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002470 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002471}
2472
Jiyong Park25bedfd2019-05-16 21:00:39 +09002473// Make ns as the anonymous namespace that is a namespace used when
2474// we fail to determine the caller address (e.g., call from mono-jited code)
2475// Since there can be multiple anonymous namespace in a process, subsequent
2476// call to this function causes an error.
2477static bool set_anonymous_namespace(android_namespace_t* ns) {
2478 if (!g_anonymous_namespace_set && ns != nullptr) {
2479 CHECK(ns->is_also_used_as_anonymous());
2480 g_anonymous_namespace = ns;
2481 g_anonymous_namespace_set = true;
2482 return true;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002483 }
Jiyong Park25bedfd2019-05-16 21:00:39 +09002484 return false;
2485}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002486
Jiyong Park25bedfd2019-05-16 21:00:39 +09002487// TODO(b/130388701) remove this. Currently, this is used only for testing
2488// where we don't have classloader namespace.
2489bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002490 ProtectedDataGuard guard;
2491
Jiyong Park25bedfd2019-05-16 21:00:39 +09002492 // Test-only feature: we need to change the anonymous namespace multiple times
2493 // while the test is running.
2494 g_anonymous_namespace_set = false;
2495
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002496 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002497 // When the caller is nullptr - create_namespace will take global group
2498 // from the anonymous namespace, which is fine because anonymous namespace
2499 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002500 android_namespace_t* anon_ns =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002501 create_namespace(nullptr,
2502 "(anonymous)",
2503 nullptr,
2504 library_search_path,
Jiyong Park25bedfd2019-05-16 21:00:39 +09002505 ANDROID_NAMESPACE_TYPE_ISOLATED |
2506 ANDROID_NAMESPACE_TYPE_ALSO_USED_AS_ANONYMOUS,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002507 nullptr,
2508 &g_default_namespace);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002509
Jiyong Park25bedfd2019-05-16 21:00:39 +09002510 CHECK(anon_ns != nullptr);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002511
2512 if (!link_namespaces(anon_ns, &g_default_namespace, shared_lib_sonames)) {
Jiyong Park25bedfd2019-05-16 21:00:39 +09002513 // TODO: delete anon_ns
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002514 return false;
2515 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08002516
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002517 return true;
2518}
2519
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002520static void add_soinfos_to_namespace(const soinfo_list_t& soinfos, android_namespace_t* ns) {
2521 ns->add_soinfos(soinfos);
2522 for (auto si : soinfos) {
2523 si->add_secondary_namespace(ns);
2524 }
2525}
2526
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002527android_namespace_t* create_namespace(const void* caller_addr,
2528 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002529 const char* ld_library_path,
2530 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002531 uint64_t type,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002532 const char* permitted_when_isolated_path,
2533 android_namespace_t* parent_namespace) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002534 if (parent_namespace == nullptr) {
Dimitry Ivanov52408632016-05-23 10:31:11 -07002535 // if parent_namespace is nullptr -> set it to the caller namespace
2536 soinfo* caller_soinfo = find_containing_library(caller_addr);
2537
2538 parent_namespace = caller_soinfo != nullptr ?
2539 caller_soinfo->get_primary_namespace() :
2540 g_anonymous_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002541 }
2542
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002543 ProtectedDataGuard guard;
2544 std::vector<std::string> ld_library_paths;
2545 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002546 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002547
2548 parse_path(ld_library_path, ":", &ld_library_paths);
2549 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002550 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002551
2552 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2553 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002554 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Jiyong Park37b91af2017-05-05 22:07:05 +09002555 ns->set_greylist_enabled((type & ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED) != 0);
Jiyong Park25bedfd2019-05-16 21:00:39 +09002556 ns->set_also_used_as_anonymous((type & ANDROID_NAMESPACE_TYPE_ALSO_USED_AS_ANONYMOUS) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002557
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002558 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07002559 // append parent namespace paths.
2560 std::copy(parent_namespace->get_ld_library_paths().begin(),
2561 parent_namespace->get_ld_library_paths().end(),
2562 back_inserter(ld_library_paths));
2563
2564 std::copy(parent_namespace->get_default_library_paths().begin(),
2565 parent_namespace->get_default_library_paths().end(),
2566 back_inserter(default_library_paths));
2567
2568 std::copy(parent_namespace->get_permitted_paths().begin(),
2569 parent_namespace->get_permitted_paths().end(),
2570 back_inserter(permitted_paths));
2571
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002572 // If shared - clone the parent namespace
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002573 add_soinfos_to_namespace(parent_namespace->soinfo_list(), ns);
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07002574 // and copy parent namespace links
2575 for (auto& link : parent_namespace->linked_namespaces()) {
Logan Chien9ee45912018-01-18 12:05:09 +08002576 ns->add_linked_namespace(link.linked_namespace(), link.shared_lib_sonames(),
2577 link.allow_all_shared_libs());
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07002578 }
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002579 } else {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002580 // If not shared - copy only the shared group
Jiyong Park02586a22017-05-20 01:01:24 +09002581 add_soinfos_to_namespace(parent_namespace->get_shared_group(), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002582 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002583
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07002584 ns->set_ld_library_paths(std::move(ld_library_paths));
2585 ns->set_default_library_paths(std::move(default_library_paths));
2586 ns->set_permitted_paths(std::move(permitted_paths));
2587
Jiyong Park25bedfd2019-05-16 21:00:39 +09002588 if (ns->is_also_used_as_anonymous() && !set_anonymous_namespace(ns)) {
2589 DL_ERR("failed to set namespace: [name=\"%s\", ld_library_path=\"%s\", default_library_paths=\"%s\""
2590 " permitted_paths=\"%s\"] as the anonymous namespace",
2591 ns->get_name(),
2592 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
2593 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
2594 android::base::Join(ns->get_permitted_paths(), ':').c_str());
2595 return nullptr;
2596 }
2597
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002598 return ns;
2599}
2600
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002601bool link_namespaces(android_namespace_t* namespace_from,
2602 android_namespace_t* namespace_to,
2603 const char* shared_lib_sonames) {
2604 if (namespace_to == nullptr) {
2605 namespace_to = &g_default_namespace;
2606 }
2607
2608 if (namespace_from == nullptr) {
2609 DL_ERR("error linking namespaces: namespace_from is null.");
2610 return false;
2611 }
2612
2613 if (shared_lib_sonames == nullptr || shared_lib_sonames[0] == '\0') {
2614 DL_ERR("error linking namespaces \"%s\"->\"%s\": the list of shared libraries is empty.",
2615 namespace_from->get_name(), namespace_to->get_name());
2616 return false;
2617 }
2618
2619 auto sonames = android::base::Split(shared_lib_sonames, ":");
2620 std::unordered_set<std::string> sonames_set(sonames.begin(), sonames.end());
2621
2622 ProtectedDataGuard guard;
Logan Chien9ee45912018-01-18 12:05:09 +08002623 namespace_from->add_linked_namespace(namespace_to, sonames_set, false);
2624
2625 return true;
2626}
2627
2628bool link_namespaces_all_libs(android_namespace_t* namespace_from,
2629 android_namespace_t* namespace_to) {
2630 if (namespace_from == nullptr) {
2631 DL_ERR("error linking namespaces: namespace_from is null.");
2632 return false;
2633 }
2634
2635 if (namespace_to == nullptr) {
2636 DL_ERR("error linking namespaces: namespace_to is null.");
2637 return false;
2638 }
2639
2640 ProtectedDataGuard guard;
2641 namespace_from->add_linked_namespace(namespace_to, std::unordered_set<std::string>(), true);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002642
2643 return true;
2644}
2645
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002646ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
Elliott Hughes90f96b92019-05-09 15:56:39 -07002647 if (g_is_ldd) return 0;
2648
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002649 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2650 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2651 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002652 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2653 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002654
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002655 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002656}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002657
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002658const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2659 if (source_symver < 2 ||
2660 source_symver >= version_infos.size() ||
2661 version_infos[source_symver].name == nullptr) {
2662 return nullptr;
2663 }
2664
2665 return &version_infos[source_symver];
2666}
2667
2668void VersionTracker::add_version_info(size_t source_index,
2669 ElfW(Word) elf_hash,
2670 const char* ver_name,
2671 const soinfo* target_si) {
2672 if (source_index >= version_infos.size()) {
2673 version_infos.resize(source_index+1);
2674 }
2675
2676 version_infos[source_index].elf_hash = elf_hash;
2677 version_infos[source_index].name = ver_name;
2678 version_infos[source_index].target_si = target_si;
2679}
2680
2681bool VersionTracker::init_verneed(const soinfo* si_from) {
2682 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2683
2684 if (verneed_ptr == 0) {
2685 return true;
2686 }
2687
2688 size_t verneed_cnt = si_from->get_verneed_cnt();
2689
2690 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2691 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2692 size_t vernaux_offset = offset + verneed->vn_aux;
2693 offset += verneed->vn_next;
2694
2695 if (verneed->vn_version != 1) {
2696 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2697 return false;
2698 }
2699
2700 const char* target_soname = si_from->get_string(verneed->vn_file);
2701 // find it in dependencies
2702 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002703 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002704 });
2705
2706 if (target_si == nullptr) {
2707 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002708 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002709 return false;
2710 }
2711
2712 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2713 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2714 vernaux_offset += vernaux->vna_next;
2715
2716 const ElfW(Word) elf_hash = vernaux->vna_hash;
2717 const char* ver_name = si_from->get_string(vernaux->vna_name);
2718 ElfW(Half) source_index = vernaux->vna_other;
2719
2720 add_version_info(source_index, elf_hash, ver_name, target_si);
2721 }
2722 }
2723
2724 return true;
2725}
2726
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002727template <typename F>
2728static bool for_each_verdef(const soinfo* si, F functor) {
2729 if (!si->has_min_version(2)) {
2730 return true;
2731 }
2732
2733 uintptr_t verdef_ptr = si->get_verdef_ptr();
2734 if (verdef_ptr == 0) {
2735 return true;
2736 }
2737
2738 size_t offset = 0;
2739
2740 size_t verdef_cnt = si->get_verdef_cnt();
2741 for (size_t i = 0; i<verdef_cnt; ++i) {
2742 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
2743 size_t verdaux_offset = offset + verdef->vd_aux;
2744 offset += verdef->vd_next;
2745
2746 if (verdef->vd_version != 1) {
2747 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
2748 i, verdef->vd_version, si->get_realpath());
2749 return false;
2750 }
2751
2752 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
2753 // "this is the version of the file itself. It must not be used for
2754 // matching a symbol. It can be used to match references."
2755 //
2756 // http://www.akkadia.org/drepper/symbol-versioning
2757 continue;
2758 }
2759
2760 if (verdef->vd_cnt == 0) {
2761 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
2762 return false;
2763 }
2764
2765 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
2766
2767 if (functor(i, verdef, verdaux) == true) {
2768 break;
2769 }
2770 }
2771
2772 return true;
2773}
2774
2775bool find_verdef_version_index(const soinfo* si, const version_info* vi, ElfW(Versym)* versym) {
2776 if (vi == nullptr) {
2777 *versym = kVersymNotNeeded;
2778 return true;
2779 }
2780
2781 *versym = kVersymGlobal;
2782
2783 return for_each_verdef(si,
2784 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2785 if (verdef->vd_hash == vi->elf_hash &&
2786 strcmp(vi->name, si->get_string(verdaux->vda_name)) == 0) {
2787 *versym = verdef->vd_ndx;
2788 return true;
2789 }
2790
2791 return false;
2792 }
2793 );
2794}
2795
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002796bool VersionTracker::init_verdef(const soinfo* si_from) {
2797 return for_each_verdef(si_from,
2798 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2799 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2800 si_from->get_string(verdaux->vda_name), si_from);
2801 return false;
2802 }
2803 );
2804}
2805
2806bool VersionTracker::init(const soinfo* si_from) {
2807 if (!si_from->has_min_version(2)) {
2808 return true;
2809 }
2810
2811 return init_verneed(si_from) && init_verdef(si_from);
2812}
2813
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002814// TODO (dimitry): Methods below need to be moved out of soinfo
2815// and in more isolated file in order minimize dependencies on
2816// unnecessary object in the linker binary. Consider making them
2817// independent from soinfo (?).
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002818bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2819 const char* sym_name, const version_info** vi) {
2820 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2821 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2822
2823 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2824 *vi = version_tracker.get_version_info(sym_ver);
2825
2826 if (*vi == nullptr) {
2827 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002828 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002829 return false;
2830 }
2831 } else {
2832 // there is no version info
2833 *vi = nullptr;
2834 }
2835
2836 return true;
2837}
2838
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002839void soinfo::apply_relr_reloc(ElfW(Addr) offset) {
2840 ElfW(Addr) address = offset + load_bias;
2841 *reinterpret_cast<ElfW(Addr)*>(address) += load_bias;
2842}
2843
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002844// Process relocations in SHT_RELR section (experimental).
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002845// Details of the encoding are described in this post:
2846// https://groups.google.com/d/msg/generic-abi/bX460iggiKg/Pi9aSwwABgAJ
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002847bool soinfo::relocate_relr() {
2848 ElfW(Relr)* begin = relr_;
2849 ElfW(Relr)* end = relr_ + relr_count_;
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002850 constexpr size_t wordsize = sizeof(ElfW(Addr));
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002851
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002852 ElfW(Addr) base = 0;
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002853 for (ElfW(Relr)* current = begin; current < end; ++current) {
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002854 ElfW(Relr) entry = *current;
2855 ElfW(Addr) offset;
2856
2857 if ((entry&1) == 0) {
2858 // Even entry: encodes the offset for next relocation.
2859 offset = static_cast<ElfW(Addr)>(entry);
2860 apply_relr_reloc(offset);
2861 // Set base offset for subsequent bitmap entries.
2862 base = offset + wordsize;
2863 continue;
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002864 }
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002865
2866 // Odd entry: encodes bitmap for relocations starting at base.
2867 offset = base;
2868 while (entry != 0) {
2869 entry >>= 1;
2870 if ((entry&1) != 0) {
2871 apply_relr_reloc(offset);
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002872 }
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002873 offset += wordsize;
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002874 }
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002875
2876 // Advance base offset by 63 words for 64-bit platforms,
2877 // or 31 words for 32-bit platforms.
2878 base += (8*wordsize - 1) * wordsize;
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002879 }
2880 return true;
2881}
2882
Dimitry Ivanov576a3752016-08-09 06:58:55 -07002883#if !defined(__mips__)
2884#if defined(USE_RELA)
2885static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2886 return rela->r_addend;
2887}
2888#else
2889static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
2890 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
Ryan Prichardfb8730d2019-01-15 00:11:37 -08002891 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE ||
2892 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_TLS_DTPREL ||
2893 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_TLS_TPREL) {
Dimitry Ivanov576a3752016-08-09 06:58:55 -07002894 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2895 }
2896 return 0;
2897}
2898#endif
2899
Ryan Prichardfb8730d2019-01-15 00:11:37 -08002900static bool is_tls_reloc(ElfW(Word) type) {
2901 switch (type) {
2902 case R_GENERIC_TLS_DTPMOD:
2903 case R_GENERIC_TLS_DTPREL:
2904 case R_GENERIC_TLS_TPREL:
2905 case R_GENERIC_TLSDESC:
2906 return true;
2907 default:
2908 return false;
2909 }
2910}
2911
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002912template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002913bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2914 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Ryan Prichardfb8730d2019-01-15 00:11:37 -08002915 const size_t tls_tp_base = __libc_shared_globals()->static_tls_layout.offset_thread_pointer();
Ryan Prichardffaae702019-01-23 17:47:10 -08002916 std::vector<std::pair<TlsDescriptor*, size_t>> deferred_tlsdesc_relocs;
Ryan Prichardfb8730d2019-01-15 00:11:37 -08002917
Vic Yang521ad072019-07-25 11:05:33 -07002918 struct {
2919 // Cache key
2920 ElfW(Word) sym;
2921
2922 // Cache value
2923 const ElfW(Sym)* s;
2924 soinfo* lsi;
2925 } symbol_lookup_cache;
2926
2927 symbol_lookup_cache.sym = 0;
2928
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002929 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2930 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002931 if (rel == nullptr) {
2932 return false;
2933 }
2934
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002935 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2936 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2937
2938 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002939 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002940 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002941 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002942
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002943 DEBUG("Processing \"%s\" relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002944 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002945 continue;
2946 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002947
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002948 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002949 soinfo* lsi = nullptr;
2950
Ryan Prichardfb8730d2019-01-15 00:11:37 -08002951 if (sym == 0) {
Ryan Prichard3b463cf2019-01-22 22:49:22 -08002952 // By convention in ld.bfd and lld, an omitted symbol on a TLS relocation
2953 // is a reference to the current module.
2954 if (is_tls_reloc(type)) {
2955 lsi = this;
2956 }
Ryan Prichardfb8730d2019-01-15 00:11:37 -08002957 } else if (ELF_ST_BIND(symtab_[sym].st_info) == STB_LOCAL && is_tls_reloc(type)) {
2958 // In certain situations, the Gold linker accesses a TLS symbol using a
2959 // relocation to an STB_LOCAL symbol in .dynsym of either STT_SECTION or
2960 // STT_TLS type. Bionic doesn't support these relocations, so issue an
2961 // error. References:
2962 // - https://groups.google.com/d/topic/generic-abi/dJ4_Y78aQ2M/discussion
2963 // - https://sourceware.org/bugzilla/show_bug.cgi?id=17699
2964 s = &symtab_[sym];
2965 sym_name = get_string(s->st_name);
2966 DL_ERR("unexpected TLS reference to local symbol \"%s\": "
2967 "sym type %d, rel type %u (idx %zu of \"%s\")",
2968 sym_name, ELF_ST_TYPE(s->st_info), type, idx, get_realpath());
2969 return false;
2970 } else {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002971 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002972
Vic Yang521ad072019-07-25 11:05:33 -07002973 if (sym == symbol_lookup_cache.sym) {
2974 s = symbol_lookup_cache.s;
2975 lsi = symbol_lookup_cache.lsi;
2976 count_relocation(kRelocSymbolCached);
2977 } else {
2978 const version_info* vi = nullptr;
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002979
Vic Yang521ad072019-07-25 11:05:33 -07002980 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2981 return false;
2982 }
2983
2984 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2985 return false;
2986 }
2987
2988 symbol_lookup_cache.sym = sym;
2989 symbol_lookup_cache.s = s;
2990 symbol_lookup_cache.lsi = lsi;
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002991 }
2992
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002993 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002994 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002995 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002996 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002997 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002998 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002999 }
3000
3001 /* IHI0044C AAELF 4.5.1.1:
3002
3003 Libraries are not searched to resolve weak references.
3004 It is not an error for a weak reference to remain unsatisfied.
3005
3006 During linking, the value of an undefined weak reference is:
3007 - Zero if the relocation type is absolute
3008 - The address of the place if the relocation is pc-relative
3009 - The address of nominal base address if the relocation
3010 type is base-relative.
3011 */
3012
3013 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08003014 case R_GENERIC_JUMP_SLOT:
3015 case R_GENERIC_GLOB_DAT:
3016 case R_GENERIC_RELATIVE:
3017 case R_GENERIC_IRELATIVE:
Ryan Prichardfb8730d2019-01-15 00:11:37 -08003018 case R_GENERIC_TLS_DTPMOD:
3019 case R_GENERIC_TLS_DTPREL:
3020 case R_GENERIC_TLS_TPREL:
3021 case R_GENERIC_TLSDESC:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003022#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003023 case R_AARCH64_ABS64:
3024 case R_AARCH64_ABS32:
3025 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08003026#elif defined(__x86_64__)
3027 case R_X86_64_32:
3028 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003029#elif defined(__arm__)
3030 case R_ARM_ABS32:
3031#elif defined(__i386__)
3032 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08003033#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003034 /*
3035 * The sym_addr was initialized to be zero above, or the relocation
3036 * code below does not care about value of sym_addr.
3037 * No need to do anything.
3038 */
3039 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08003040#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00003041 case R_X86_64_PC32:
3042 sym_addr = reloc;
3043 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003044#elif defined(__i386__)
3045 case R_386_PC32:
3046 sym_addr = reloc;
3047 break;
3048#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003049 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003050 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08003051 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003052 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07003053 } else { // We got a definition.
3054#if !defined(__LP64__)
3055 // When relocating dso with text_relocation .text segment is
3056 // not executable. We need to restore elf flags before resolving
3057 // STT_GNU_IFUNC symbol.
3058 bool protect_segments = has_text_relocations &&
3059 lsi == this &&
3060 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
3061 if (protect_segments) {
3062 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3063 DL_ERR("can't protect segments for \"%s\": %s",
3064 get_realpath(), strerror(errno));
3065 return false;
3066 }
3067 }
3068#endif
Ryan Prichardfb8730d2019-01-15 00:11:37 -08003069 if (is_tls_reloc(type)) {
3070 if (ELF_ST_TYPE(s->st_info) != STT_TLS) {
3071 DL_ERR("reference to non-TLS symbol \"%s\" from TLS relocation in \"%s\"",
3072 sym_name, get_realpath());
3073 return false;
3074 }
Ryan Prichard3b463cf2019-01-22 22:49:22 -08003075 if (lsi->get_tls() == nullptr) {
3076 DL_ERR("TLS relocation refers to symbol \"%s\" in solib \"%s\" with no TLS segment",
3077 sym_name, lsi->get_realpath());
3078 return false;
3079 }
Ryan Prichardfb8730d2019-01-15 00:11:37 -08003080 sym_addr = s->st_value;
3081 } else {
3082 if (ELF_ST_TYPE(s->st_info) == STT_TLS) {
3083 DL_ERR("reference to TLS symbol \"%s\" from non-TLS relocation in \"%s\"",
3084 sym_name, get_realpath());
3085 return false;
3086 }
3087 sym_addr = lsi->resolve_symbol_address(s);
Elliott Hughes9724e932018-03-23 18:46:07 -07003088 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07003089#if !defined(__LP64__)
3090 if (protect_segments) {
3091 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3092 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
3093 get_realpath(), strerror(errno));
3094 return false;
3095 }
3096 }
3097#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003098 }
3099 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003100 }
3101
3102 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08003103 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003104 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003105 MARK(rel->r_offset);
3106 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
3107 reinterpret_cast<void*>(reloc),
3108 reinterpret_cast<void*>(sym_addr + addend), sym_name);
3109
3110 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003111 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08003112 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003113 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003114 MARK(rel->r_offset);
3115 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
3116 reinterpret_cast<void*>(reloc),
3117 reinterpret_cast<void*>(sym_addr + addend), sym_name);
3118 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003119 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08003120 case R_GENERIC_RELATIVE:
3121 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003122 MARK(rel->r_offset);
3123 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
3124 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003125 reinterpret_cast<void*>(load_bias + addend));
3126 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08003127 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08003128 case R_GENERIC_IRELATIVE:
3129 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003130 MARK(rel->r_offset);
3131 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
3132 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003133 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07003134 {
3135#if !defined(__LP64__)
3136 // When relocating dso with text_relocation .text segment is
3137 // not executable. We need to restore elf flags for this
3138 // particular call.
3139 if (has_text_relocations) {
3140 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3141 DL_ERR("can't protect segments for \"%s\": %s",
3142 get_realpath(), strerror(errno));
3143 return false;
3144 }
3145 }
3146#endif
3147 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
3148#if !defined(__LP64__)
3149 // Unprotect it afterwards...
3150 if (has_text_relocations) {
3151 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3152 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
3153 get_realpath(), strerror(errno));
3154 return false;
3155 }
3156 }
3157#endif
3158 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
3159 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08003160 break;
Ryan Prichardfb8730d2019-01-15 00:11:37 -08003161 case R_GENERIC_TLS_TPREL:
3162 count_relocation(kRelocRelative);
3163 MARK(rel->r_offset);
3164 {
3165 ElfW(Addr) tpoff = 0;
Ryan Prichardfb8730d2019-01-15 00:11:37 -08003166 if (lsi == nullptr) {
3167 // Unresolved weak relocation. Leave tpoff at 0 to resolve
3168 // &weak_tls_symbol to __get_tls().
Ryan Prichard3b463cf2019-01-22 22:49:22 -08003169 } else {
3170 CHECK(lsi->get_tls() != nullptr); // We rejected a missing TLS segment above.
3171 const TlsModule& mod = get_tls_module(lsi->get_tls()->module_id);
Ryan Prichardbf427f42019-01-17 16:37:11 -08003172 if (mod.static_offset != SIZE_MAX) {
3173 tpoff += mod.static_offset - tls_tp_base;
Ryan Prichardfb8730d2019-01-15 00:11:37 -08003174 } else {
3175 DL_ERR("TLS symbol \"%s\" in dlopened \"%s\" referenced from \"%s\" using IE access model",
3176 sym_name, lsi->get_realpath(), get_realpath());
3177 return false;
3178 }
Ryan Prichardfb8730d2019-01-15 00:11:37 -08003179 }
3180 tpoff += sym_addr + addend;
3181 TRACE_TYPE(RELO, "RELO TLS_TPREL %16p <- %16p %s\n",
3182 reinterpret_cast<void*>(reloc),
3183 reinterpret_cast<void*>(tpoff), sym_name);
3184 *reinterpret_cast<ElfW(Addr)*>(reloc) = tpoff;
3185 }
3186 break;
Ryan Prichard3b463cf2019-01-22 22:49:22 -08003187 case R_GENERIC_TLS_DTPMOD:
3188 count_relocation(kRelocRelative);
3189 MARK(rel->r_offset);
3190 {
3191 size_t module_id = 0;
3192 if (lsi == nullptr) {
3193 // Unresolved weak relocation. Evaluate the module ID to 0.
3194 } else {
3195 CHECK(lsi->get_tls() != nullptr); // We rejected a missing TLS segment above.
3196 module_id = lsi->get_tls()->module_id;
3197 }
3198 TRACE_TYPE(RELO, "RELO TLS_DTPMOD %16p <- %zu %s\n",
3199 reinterpret_cast<void*>(reloc), module_id, sym_name);
3200 *reinterpret_cast<ElfW(Addr)*>(reloc) = module_id;
3201 }
3202 break;
3203 case R_GENERIC_TLS_DTPREL:
3204 count_relocation(kRelocRelative);
3205 MARK(rel->r_offset);
3206 TRACE_TYPE(RELO, "RELO TLS_DTPREL %16p <- %16p %s\n",
3207 reinterpret_cast<void*>(reloc),
3208 reinterpret_cast<void*>(sym_addr + addend), sym_name);
3209 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
3210 break;
Ryan Prichard3b463cf2019-01-22 22:49:22 -08003211
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08003212#if defined(__aarch64__)
Ryan Prichardffaae702019-01-23 17:47:10 -08003213 // Bionic currently only implements TLSDESC for arm64. This implementation should work with
3214 // other architectures, as long as the resolver functions are implemented.
3215 case R_GENERIC_TLSDESC:
3216 count_relocation(kRelocRelative);
3217 MARK(rel->r_offset);
3218 {
3219 TlsDescriptor* desc = reinterpret_cast<TlsDescriptor*>(reloc);
3220 if (lsi == nullptr) {
3221 // Unresolved weak relocation.
3222 desc->func = tlsdesc_resolver_unresolved_weak;
3223 desc->arg = addend;
3224 TRACE_TYPE(RELO, "RELO TLSDESC %16p <- unresolved weak 0x%zx %s\n",
3225 reinterpret_cast<void*>(reloc), static_cast<size_t>(addend), sym_name);
3226 } else {
3227 CHECK(lsi->get_tls() != nullptr); // We rejected a missing TLS segment above.
3228 size_t module_id = lsi->get_tls()->module_id;
3229 const TlsModule& mod = get_tls_module(module_id);
3230 if (mod.static_offset != SIZE_MAX) {
3231 desc->func = tlsdesc_resolver_static;
3232 desc->arg = mod.static_offset - tls_tp_base + sym_addr + addend;
3233 TRACE_TYPE(RELO, "RELO TLSDESC %16p <- static (0x%zx - 0x%zx + 0x%zx + 0x%zx) %s\n",
3234 reinterpret_cast<void*>(reloc), mod.static_offset, tls_tp_base,
3235 static_cast<size_t>(sym_addr), static_cast<size_t>(addend), sym_name);
3236 } else {
3237 tlsdesc_args_.push_back({
3238 .generation = mod.first_generation,
3239 .index.module_id = module_id,
3240 .index.offset = sym_addr + addend,
3241 });
3242 // Defer the TLSDESC relocation until the address of the TlsDynamicResolverArg object
3243 // is finalized.
3244 deferred_tlsdesc_relocs.push_back({ desc, tlsdesc_args_.size() - 1 });
3245 const TlsDynamicResolverArg& desc_arg = tlsdesc_args_.back();
3246 TRACE_TYPE(RELO, "RELO TLSDESC %16p <- dynamic (gen %zu, mod %zu, off %zu) %s",
3247 reinterpret_cast<void*>(reloc), desc_arg.generation,
3248 desc_arg.index.module_id, desc_arg.index.offset, sym_name);
3249 }
3250 }
3251 }
3252 break;
Ryan Prichardb0e2dd02019-03-11 17:06:02 -07003253#endif // defined(__aarch64__)
Ryan Prichardffaae702019-01-23 17:47:10 -08003254
3255#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003256 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003257 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003258 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003259 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003260 reloc, sym_addr + addend, sym_name);
3261 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003262 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003263 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003264 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003265 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003266 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003267 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003268 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003269 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
3270 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003271 if ((min_value <= (sym_addr + addend)) &&
3272 ((sym_addr + addend) <= max_value)) {
3273 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003274 } else {
3275 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003276 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003277 return false;
3278 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003279 }
3280 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003281 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003282 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003283 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003284 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003285 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003286 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003287 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
3288 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003289 if ((min_value <= (sym_addr + addend)) &&
3290 ((sym_addr + addend) <= max_value)) {
3291 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003292 } else {
3293 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003294 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003295 return false;
3296 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003297 }
3298 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003299 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003300 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003301 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003302 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003303 reloc, sym_addr + addend, rel->r_offset, sym_name);
3304 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003305 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003306 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003307 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003308 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003309 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003310 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003311 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003312 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
3313 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003314 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
3315 ((sym_addr + addend - rel->r_offset) <= max_value)) {
3316 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003317 } else {
3318 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003319 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003320 return false;
3321 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003322 }
3323 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003324 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003325 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003326 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003327 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003328 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003329 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003330 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
3331 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003332 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
3333 ((sym_addr + addend - rel->r_offset) <= max_value)) {
3334 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003335 } else {
3336 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003337 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003338 return false;
3339 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003340 }
3341 break;
3342
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003343 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07003344 /*
3345 * ET_EXEC is not supported so this should not happen.
3346 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003347 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07003348 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003349 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07003350 * R_AARCH64_COPY may only appear in executable objects where e_type is
3351 * set to ET_EXEC.
3352 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003353 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08003354 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003355#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003356 case R_X86_64_32:
3357 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003358 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003359 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
3360 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09003361 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003362 break;
3363 case R_X86_64_64:
3364 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003365 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003366 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
3367 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09003368 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003369 break;
3370 case R_X86_64_PC32:
3371 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003372 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003373 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
3374 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
3375 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09003376 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003377 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003378#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003379 case R_ARM_ABS32:
3380 count_relocation(kRelocAbsolute);
3381 MARK(rel->r_offset);
3382 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
3383 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
3384 break;
3385 case R_ARM_REL32:
3386 count_relocation(kRelocRelative);
3387 MARK(rel->r_offset);
3388 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
3389 reloc, sym_addr, rel->r_offset, sym_name);
3390 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
3391 break;
3392 case R_ARM_COPY:
3393 /*
3394 * ET_EXEC is not supported so this should not happen.
3395 *
3396 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
3397 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003398 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003399 * R_ARM_COPY may only appear in executable objects where e_type is
3400 * set to ET_EXEC.
3401 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003402 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08003403 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003404#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003405 case R_386_32:
3406 count_relocation(kRelocRelative);
3407 MARK(rel->r_offset);
3408 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
3409 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
3410 break;
3411 case R_386_PC32:
3412 count_relocation(kRelocRelative);
3413 MARK(rel->r_offset);
3414 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
3415 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
3416 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
3417 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003418#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003419 default:
3420 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003421 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003422 }
3423 }
Ryan Prichardffaae702019-01-23 17:47:10 -08003424
Ryan Prichardb0e2dd02019-03-11 17:06:02 -07003425#if defined(__aarch64__)
3426 // Bionic currently only implements TLSDESC for arm64.
Ryan Prichardffaae702019-01-23 17:47:10 -08003427 for (const std::pair<TlsDescriptor*, size_t>& pair : deferred_tlsdesc_relocs) {
3428 TlsDescriptor* desc = pair.first;
3429 desc->func = tlsdesc_resolver_dynamic;
3430 desc->arg = reinterpret_cast<size_t>(&tlsdesc_args_[pair.second]);
3431 }
Ryan Prichardb0e2dd02019-03-11 17:06:02 -07003432#endif
Ryan Prichardffaae702019-01-23 17:47:10 -08003433
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003434 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003435}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08003436#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003437
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07003438// An empty list of soinfos
Dimitry Ivanovb943f302016-08-03 16:00:10 -07003439static soinfo_list_t g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003440
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003441bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08003442 /* Extract dynamic section */
3443 ElfW(Word) dynamic_flags = 0;
3444 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07003445
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003446 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003447 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003448 if (!relocating_linker) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003449 INFO("[ Linking \"%s\" ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003450 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003451 }
3452
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003453 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003454 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003455 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003456 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003457 return false;
3458 } else {
3459 if (!relocating_linker) {
3460 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003461 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003462 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003463
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003464#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003465 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
3466 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003467#endif
3468
Ryan Pricharde5e69e02019-01-01 18:53:48 -08003469 TlsSegment tls_segment;
Ryan Prichard19883502019-01-16 23:13:38 -08003470 if (__bionic_get_tls_segment(phdr, phnum, load_bias, &tls_segment)) {
3471 if (!__bionic_check_tls_alignment(&tls_segment.alignment)) {
3472 if (!relocating_linker) {
3473 DL_ERR("TLS segment alignment in \"%s\" is not a power of 2: %zu",
3474 get_realpath(), tls_segment.alignment);
3475 }
3476 return false;
3477 }
Ryan Pricharde5e69e02019-01-01 18:53:48 -08003478 tls_ = std::make_unique<soinfo_tls>();
3479 tls_->segment = tls_segment;
3480 }
3481
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003482 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003483 // Note that: "Except for the DT_NULL element at the end of the array,
3484 // and the relative order of DT_NEEDED elements, entries may appear in any order."
3485 //
3486 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003487 uint32_t needed_count = 0;
3488 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
3489 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
3490 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3491 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003492 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003493 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003494 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003495
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003496 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003497 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
3498 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
3499 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
3500 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003501 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003502
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003503 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003504 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003505 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003506 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
3507 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003508
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003509 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003510 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003511 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003512 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
3513 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003514
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003515 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003516 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003517 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003518 return false;
3519 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003520 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003521
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003522 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003523 break;
3524
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003525 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003526 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003527 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003528
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003529 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003530 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003531 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003532
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003533 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003534 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003535 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003536
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003537 case DT_SYMENT:
3538 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003539 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
3540 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003541 return false;
3542 }
3543 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003544
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003545 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003546#if defined(USE_RELA)
3547 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003548 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003549 return false;
3550 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003551#else
3552 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003553 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003554 return false;
3555 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003556#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003557 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003558
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003559 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003560#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003561 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003562#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003563 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003564#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003565 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003566
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003567 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003568#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003569 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003570#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003571 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003572#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003573 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003574
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003575 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003576#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003577 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003578 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003579#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003580 // Ignore for other platforms... (because RTLD_LAZY is not supported)
3581 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003582
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003583 case DT_DEBUG:
3584 // Set the DT_DEBUG entry to the address of _r_debug for GDB
3585 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08003586// FIXME: not working currently for N64
3587// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003588// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08003589// read-only, but the DYNAMIC header claims it is writable.
3590#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003591 if ((dynamic_flags & PF_W) != 0) {
3592 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
3593 }
Chris Dearman99186652014-02-06 20:36:51 -08003594#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08003595 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003596#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003597 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003598 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003599 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003600
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003601 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003602 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003603 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003604
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003605 case DT_ANDROID_RELA:
3606 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3607 break;
3608
3609 case DT_ANDROID_RELASZ:
3610 android_relocs_size_ = d->d_un.d_val;
3611 break;
3612
3613 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003614 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003615 return false;
3616
3617 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003618 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003619 return false;
3620
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003621 case DT_RELAENT:
3622 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003623 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003624 return false;
3625 }
3626 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003627
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08003628 // Ignored (see DT_RELCOUNT comments for details).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003629 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003630 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003631
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003632 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003633 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003634 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003635
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003636 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003637 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003638 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003639
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003640#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003641 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003642 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003643 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003644
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003645 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003646 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003647 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003648
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003649 case DT_RELENT:
3650 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003651 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003652 return false;
3653 }
3654 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003655
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003656 case DT_ANDROID_REL:
3657 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3658 break;
3659
3660 case DT_ANDROID_RELSZ:
3661 android_relocs_size_ = d->d_un.d_val;
3662 break;
3663
3664 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003665 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003666 return false;
3667
3668 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003669 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003670 return false;
3671
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003672 // "Indicates that all RELATIVE relocations have been concatenated together,
3673 // and specifies the RELATIVE relocation count."
3674 //
3675 // TODO: Spec also mentions that this can be used to optimize relocation process;
3676 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003677 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003678 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003679
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003680 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003681 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003682 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003683
3684 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003685 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003686 return false;
3687
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003688#endif
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08003689 case DT_RELR:
3690 relr_ = reinterpret_cast<ElfW(Relr)*>(load_bias + d->d_un.d_ptr);
3691 break;
3692
3693 case DT_RELRSZ:
3694 relr_count_ = d->d_un.d_val / sizeof(ElfW(Relr));
3695 break;
3696
3697 case DT_RELRENT:
3698 if (d->d_un.d_val != sizeof(ElfW(Relr))) {
3699 DL_ERR("invalid DT_RELRENT: %zd", static_cast<size_t>(d->d_un.d_val));
3700 return false;
3701 }
3702 break;
3703
3704 // Ignored (see DT_RELCOUNT comments for details).
3705 case DT_RELRCOUNT:
3706 break;
3707
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003708 case DT_INIT:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003709 init_func_ = reinterpret_cast<linker_ctor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003710 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003711 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003712
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003713 case DT_FINI:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003714 fini_func_ = reinterpret_cast<linker_dtor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003715 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003716 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003717
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003718 case DT_INIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003719 init_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003720 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003721 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003722
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003723 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003724 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003725 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003726
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003727 case DT_FINI_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003728 fini_array_ = reinterpret_cast<linker_dtor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003729 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003730 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003731
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003732 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003733 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003734 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003735
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003736 case DT_PREINIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003737 preinit_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003738 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003739 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003740
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003741 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003742 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003743 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003744
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003745 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003746#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003747 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003748 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003749#else
3750 has_text_relocations = true;
3751 break;
3752#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003753
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003754 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003755 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003756 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003757
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003758 case DT_NEEDED:
3759 ++needed_count;
3760 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003761
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003762 case DT_FLAGS:
3763 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003764#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003765 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003766 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003767#else
3768 has_text_relocations = true;
3769#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003770 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003771 if (d->d_un.d_val & DF_SYMBOLIC) {
3772 has_DT_SYMBOLIC = true;
3773 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003774 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003775
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003776 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003777 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003778
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003779 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003780 DL_WARN("Warning: \"%s\" has unsupported flags DT_FLAGS_1=%p "
3781 "(ignoring unsupported flags)",
3782 get_realpath(), reinterpret_cast<void*>(d->d_un.d_val));
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003783 }
3784 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003785#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003786 case DT_MIPS_RLD_MAP:
3787 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3788 {
3789 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3790 *dp = &_r_debug;
3791 }
3792 break;
Lazar Trsic83b44a92016-04-06 13:39:17 +02003793 case DT_MIPS_RLD_MAP_REL:
3794 // Set the DT_MIPS_RLD_MAP_REL entry to the address of _r_debug for GDB.
Raghu Gandham68815722014-12-18 19:12:19 -08003795 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003796 r_debug** dp = reinterpret_cast<r_debug**>(
3797 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003798 *dp = &_r_debug;
3799 }
3800 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003801
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003802 case DT_MIPS_RLD_VERSION:
3803 case DT_MIPS_FLAGS:
3804 case DT_MIPS_BASE_ADDRESS:
3805 case DT_MIPS_UNREFEXTNO:
3806 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003807
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003808 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003809 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003810 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003811
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003812 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003813 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003814 break;
3815
3816 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003817 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003818 break;
3819#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003820 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3821 case DT_BIND_NOW:
3822 break;
3823
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003824 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003825 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3826 break;
3827
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003828 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003829 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3830 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003831 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003832 verdef_cnt_ = d->d_un.d_val;
3833 break;
3834
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003835 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003836 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3837 break;
3838
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003839 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003840 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003841 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003842
Evgenii Stepanov68650822015-06-10 13:38:39 -07003843 case DT_RUNPATH:
3844 // this is parsed after we have strtab initialized (see below).
3845 break;
3846
Ryan Prichardea41ab02019-01-23 16:33:03 -08003847 case DT_TLSDESC_GOT:
3848 case DT_TLSDESC_PLT:
3849 // These DT entries are used for lazy TLSDESC relocations. Bionic
3850 // resolves everything eagerly, so these can be ignored.
3851 break;
3852
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003853 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003854 if (!relocating_linker) {
Elliott Hughes6eae4cc2017-08-30 09:02:33 -07003855 const char* tag_name;
3856 if (d->d_tag == DT_RPATH) {
3857 tag_name = "DT_RPATH";
3858 } else if (d->d_tag == DT_ENCODING) {
3859 tag_name = "DT_ENCODING";
3860 } else if (d->d_tag >= DT_LOOS && d->d_tag <= DT_HIOS) {
3861 tag_name = "unknown OS-specific";
3862 } else if (d->d_tag >= DT_LOPROC && d->d_tag <= DT_HIPROC) {
3863 tag_name = "unknown processor-specific";
3864 } else {
3865 tag_name = "unknown";
3866 }
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003867 DL_WARN("Warning: \"%s\" unused DT entry: %s (type %p arg %p) (ignoring)",
Elliott Hughes6eae4cc2017-08-30 09:02:33 -07003868 get_realpath(),
3869 tag_name,
3870 reinterpret_cast<void*>(d->d_tag),
3871 reinterpret_cast<void*>(d->d_un.d_val));
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003872 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003873 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003874 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003875 }
3876
Duane Sandbc425c72015-06-01 16:29:14 -07003877#if defined(__mips__) && !defined(__LP64__)
3878 if (!mips_check_and_adjust_fp_modes()) {
3879 return false;
3880 }
3881#endif
3882
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003883 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003884 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003885
3886 // Sanity checks.
3887 if (relocating_linker && needed_count != 0) {
3888 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3889 return false;
3890 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003891 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003892 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003893 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003894 return false;
3895 }
Yi Kong32bc0fc2018-08-02 17:31:13 -07003896 if (strtab_ == nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003897 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003898 return false;
3899 }
Yi Kong32bc0fc2018-08-02 17:31:13 -07003900 if (symtab_ == nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003901 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003902 return false;
3903 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003904
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003905 // second pass - parse entries relying on strtab
3906 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003907 switch (d->d_tag) {
3908 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003909 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003910 break;
3911 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003912 set_dt_runpath(get_string(d->d_un.d_val));
3913 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003914 }
3915 }
3916
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003917 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003918 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003919 // because they can't find dt_needed library by soname.
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003920 // This workaround should keep them working. (Applies only
3921 // for apps targeting sdk version < M.) Make an exception for
3922 // the main executable and linker; they do not need to have dt_soname.
3923 // TODO: >= O the linker doesn't need this workaround.
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003924 if (soname_ == nullptr &&
3925 this != solist_get_somain() &&
3926 (flags_ & FLAG_LINKER) == 0 &&
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003927 get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003928 soname_ = basename(realpath_.c_str());
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003929 DL_WARN_documented_change(__ANDROID_API_M__,
3930 "missing-soname-enforced-for-api-level-23",
3931 "\"%s\" has no DT_SONAME (will use %s instead)",
3932 get_realpath(), soname_);
3933
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003934 // 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 -07003935 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003936 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003937}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003938
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003939bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -04003940 const android_dlextinfo* extinfo, size_t* relro_fd_offset) {
dimitry965d06d2017-11-28 16:03:07 +01003941 if (is_image_linked()) {
3942 // already linked.
3943 return true;
3944 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003945
Elliott Hughes90f96b92019-05-09 15:56:39 -07003946 if (g_is_ldd && !is_main_executable()) {
3947 async_safe_format_fd(STDOUT_FILENO, "\t%s => %s (%p)\n", get_soname(),
3948 get_realpath(), reinterpret_cast<void*>(base));
3949 }
3950
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003951 local_group_root_ = local_group.front();
3952 if (local_group_root_ == nullptr) {
3953 local_group_root_ = this;
3954 }
3955
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003956 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3957 target_sdk_version_ = get_application_target_sdk_version();
3958 }
3959
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003960 VersionTracker version_tracker;
3961
3962 if (!version_tracker.init(this)) {
3963 return false;
3964 }
3965
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003966#if !defined(__LP64__)
3967 if (has_text_relocations) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003968 // Fail if app is targeting M or above.
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003969 int app_target_api_level = get_application_target_sdk_version();
3970 if (app_target_api_level >= __ANDROID_API_M__) {
Elliott Hughes763f6e12017-04-10 09:52:33 -07003971 DL_ERR_AND_LOG("\"%s\" has text relocations (https://android.googlesource.com/platform/"
3972 "bionic/+/master/android-changes-for-ndk-developers.md#Text-Relocations-"
3973 "Enforced-for-API-level-23)", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003974 return false;
3975 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003976 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003977 // phdr_table_protect_segments() after all of them are applied.
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003978 DL_WARN_documented_change(__ANDROID_API_M__,
3979 "Text-Relocations-Enforced-for-API-level-23",
3980 "\"%s\" has text relocations",
3981 get_realpath());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003982 add_dlwarning(get_realpath(), "text relocations");
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003983 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003984 DL_ERR("can't unprotect loadable segments for \"%s\": %s", get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003985 return false;
3986 }
3987 }
3988#endif
3989
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003990 if (android_relocs_ != nullptr) {
3991 // check signature
3992 if (android_relocs_size_ > 3 &&
3993 android_relocs_[0] == 'A' &&
3994 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003995 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003996 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003997 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003998
3999 bool relocated = false;
4000 const uint8_t* packed_relocs = android_relocs_ + 4;
4001 const size_t packed_relocs_size = android_relocs_size_ - 4;
4002
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07004003 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07004004 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07004005 packed_reloc_iterator<sleb128_decoder>(
4006 sleb128_decoder(packed_relocs, packed_relocs_size)),
4007 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08004008
4009 if (!relocated) {
4010 return false;
4011 }
4012 } else {
4013 DL_ERR("bad android relocation header.");
4014 return false;
4015 }
4016 }
4017
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08004018 if (relr_ != nullptr) {
4019 DEBUG("[ relocating %s relr ]", get_realpath());
4020 if (!relocate_relr()) {
4021 return false;
4022 }
4023 }
4024
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07004025#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004026 if (rela_ != nullptr) {
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08004027 DEBUG("[ relocating %s rela ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07004028 if (!relocate(version_tracker,
4029 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004030 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07004031 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004032 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004033 if (plt_rela_ != nullptr) {
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08004034 DEBUG("[ relocating %s plt rela ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07004035 if (!relocate(version_tracker,
4036 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004037 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004038 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004039 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07004040#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004041 if (rel_ != nullptr) {
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08004042 DEBUG("[ relocating %s rel ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07004043 if (!relocate(version_tracker,
4044 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004045 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004046 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004047 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004048 if (plt_rel_ != nullptr) {
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08004049 DEBUG("[ relocating %s plt rel ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07004050 if (!relocate(version_tracker,
4051 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004052 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07004053 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004054 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07004055#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07004056
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07004057#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07004058 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004059 return false;
4060 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07004061#endif
4062
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004063 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004064
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00004065#if !defined(__LP64__)
4066 if (has_text_relocations) {
4067 // All relocations are done, we can protect our segments back to read-only.
4068 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
4069 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004070 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00004071 return false;
4072 }
4073 }
4074#endif
4075
Mingwei Shibe910522015-11-12 07:02:14 +00004076 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
4077 // itself --- it can't make system calls yet, and will have to call protect_relro later.
4078 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004079 return false;
4080 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08004081
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004082 /* Handle serializing/sharing the RELRO segment */
4083 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
4084 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
Torne (Richard Coles)fa9f7f22019-04-02 17:04:42 -04004085 extinfo->relro_fd, relro_fd_offset) < 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004086 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004087 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004088 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00004089 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004090 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
4091 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -04004092 extinfo->relro_fd, relro_fd_offset) < 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004093 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004094 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004095 return false;
4096 }
4097 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00004098
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004099 notify_gdb_of_load(this);
dimitry965d06d2017-11-28 16:03:07 +01004100 set_image_linked();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004101 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004102}
4103
Mingwei Shibe910522015-11-12 07:02:14 +00004104bool soinfo::protect_relro() {
4105 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
4106 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
4107 get_realpath(), strerror(errno));
4108 return false;
4109 }
4110 return true;
4111}
4112
Jiyong Park02586a22017-05-20 01:01:24 +09004113static std::vector<android_namespace_t*> init_default_namespace_no_config(bool is_asan) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004114 g_default_namespace.set_isolated(false);
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08004115 auto default_ld_paths = is_asan ? kAsanDefaultLdPaths : kDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004116
neo.chae2589f9d2016-10-04 11:00:27 +09004117 char real_path[PATH_MAX];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004118 std::vector<std::string> ld_default_paths;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08004119 for (size_t i = 0; default_ld_paths[i] != nullptr; ++i) {
4120 if (realpath(default_ld_paths[i], real_path) != nullptr) {
neo.chae2589f9d2016-10-04 11:00:27 +09004121 ld_default_paths.push_back(real_path);
4122 } else {
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08004123 ld_default_paths.push_back(default_ld_paths[i]);
neo.chae2589f9d2016-10-04 11:00:27 +09004124 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004125 }
4126
4127 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Jiyong Park02586a22017-05-20 01:01:24 +09004128
4129 std::vector<android_namespace_t*> namespaces;
4130 namespaces.push_back(&g_default_namespace);
4131 return namespaces;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08004132}
4133
Roland Levillaindc25f6c2019-07-02 16:40:19 +01004134// return /apex/<name>/etc/ld.config.txt from /apex/<name>/bin/*
Jiyong Park35833412018-10-26 14:45:21 +09004135static std::string get_ld_config_file_apex_path(const char* executable_path) {
4136 std::vector<std::string> paths = android::base::Split(executable_path, "/");
Roland Levillaindc25f6c2019-07-02 16:40:19 +01004137 if (paths.size() >= 5 && paths[1] == "apex" && paths[3] == "bin") {
Jiyong Park35833412018-10-26 14:45:21 +09004138 return std::string("/apex/") + paths[2] + "/etc/ld.config.txt";
4139 }
4140 return "";
4141}
4142
Logan Chien886b96e2018-05-08 17:52:18 +08004143static std::string get_ld_config_file_vndk_path() {
Logan Chienbcfe3cf2018-05-08 20:17:26 +08004144 if (android::base::GetBoolProperty("ro.vndk.lite", false)) {
4145 return kLdConfigVndkLiteFilePath;
4146 }
4147
Logan Chien886b96e2018-05-08 17:52:18 +08004148 std::string ld_config_file_vndk = kLdConfigFilePath;
4149 size_t insert_pos = ld_config_file_vndk.find_last_of('.');
4150 if (insert_pos == std::string::npos) {
4151 insert_pos = ld_config_file_vndk.length();
4152 }
4153 ld_config_file_vndk.insert(insert_pos, Config::get_vndk_version_string('.'));
4154 return ld_config_file_vndk;
4155}
4156
Jiyong Park35833412018-10-26 14:45:21 +09004157static std::string get_ld_config_file_path(const char* executable_path) {
Logan Chien886b96e2018-05-08 17:52:18 +08004158#ifdef USE_LD_CONFIG_FILE
4159 // This is a debugging/testing only feature. Must not be available on
4160 // production builds.
4161 const char* ld_config_file_env = getenv("LD_CONFIG_FILE");
4162 if (ld_config_file_env != nullptr && file_exists(ld_config_file_env)) {
4163 return ld_config_file_env;
4164 }
4165#endif
4166
Jiyong Park35833412018-10-26 14:45:21 +09004167 std::string path = get_ld_config_file_apex_path(executable_path);
4168 if (!path.empty()) {
4169 if (file_exists(path.c_str())) {
4170 return path;
4171 }
4172 DL_WARN("Warning: couldn't read config file \"%s\" for \"%s\"",
4173 path.c_str(), executable_path);
Logan Chien886b96e2018-05-08 17:52:18 +08004174 }
4175
Jiyong Park35833412018-10-26 14:45:21 +09004176 path = kLdConfigArchFilePath;
4177 if (file_exists(path.c_str())) {
4178 return path;
4179 }
4180
4181 path = get_ld_config_file_vndk_path();
4182 if (file_exists(path.c_str())) {
4183 return path;
Logan Chien886b96e2018-05-08 17:52:18 +08004184 }
4185
4186 return kLdConfigFilePath;
4187}
4188
Jiyong Park02586a22017-05-20 01:01:24 +09004189std::vector<android_namespace_t*> init_default_namespaces(const char* executable_path) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08004190 g_default_namespace.set_name("(default)");
4191
4192 soinfo* somain = solist_get_somain();
4193
4194 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
4195 somain->load_bias);
Dan Willemsen7ccc50d2017-09-18 21:28:14 -07004196 const char* bname = (interp != nullptr) ? basename(interp) : nullptr;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08004197
4198 g_is_asan = bname != nullptr &&
4199 (strcmp(bname, "linker_asan") == 0 ||
4200 strcmp(bname, "linker_asan64") == 0);
4201
4202 const Config* config = nullptr;
4203
4204 std::string error_msg;
4205
Jiyong Park35833412018-10-26 14:45:21 +09004206 std::string ld_config_file_path = get_ld_config_file_path(executable_path);
Jiyong Park02586a22017-05-20 01:01:24 +09004207
Martin Stjernholm95252ee2019-02-22 22:48:59 +00004208 INFO("[ Reading linker config \"%s\" ]", ld_config_file_path.c_str());
Logan Chien886b96e2018-05-08 17:52:18 +08004209 if (!Config::read_binary_config(ld_config_file_path.c_str(),
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08004210 executable_path,
4211 g_is_asan,
4212 &config,
4213 &error_msg)) {
4214 if (!error_msg.empty()) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -08004215 DL_WARN("Warning: couldn't read \"%s\" for \"%s\" (using default configuration instead): %s",
Logan Chien886b96e2018-05-08 17:52:18 +08004216 ld_config_file_path.c_str(),
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08004217 executable_path,
4218 error_msg.c_str());
4219 }
4220 config = nullptr;
4221 }
4222
4223 if (config == nullptr) {
Jiyong Park02586a22017-05-20 01:01:24 +09004224 return init_default_namespace_no_config(g_is_asan);
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08004225 }
4226
4227 const auto& namespace_configs = config->namespace_configs();
4228 std::unordered_map<std::string, android_namespace_t*> namespaces;
4229
4230 // 1. Initialize default namespace
4231 const NamespaceConfig* default_ns_config = config->default_namespace_config();
4232
4233 g_default_namespace.set_isolated(default_ns_config->isolated());
4234 g_default_namespace.set_default_library_paths(default_ns_config->search_paths());
4235 g_default_namespace.set_permitted_paths(default_ns_config->permitted_paths());
4236
4237 namespaces[default_ns_config->name()] = &g_default_namespace;
Justin Yun90de9f02017-07-07 16:21:53 +09004238 if (default_ns_config->visible()) {
4239 g_exported_namespaces[default_ns_config->name()] = &g_default_namespace;
4240 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08004241
4242 // 2. Initialize other namespaces
4243
4244 for (auto& ns_config : namespace_configs) {
4245 if (namespaces.find(ns_config->name()) != namespaces.end()) {
4246 continue;
4247 }
4248
4249 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
4250 ns->set_name(ns_config->name());
4251 ns->set_isolated(ns_config->isolated());
4252 ns->set_default_library_paths(ns_config->search_paths());
4253 ns->set_permitted_paths(ns_config->permitted_paths());
Vic Yang2d020e42019-01-12 21:03:25 -08004254 ns->set_whitelisted_libs(ns_config->whitelisted_libs());
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08004255
4256 namespaces[ns_config->name()] = ns;
Jiyong Park01de74e2017-04-03 23:10:37 +09004257 if (ns_config->visible()) {
4258 g_exported_namespaces[ns_config->name()] = ns;
4259 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08004260 }
4261
4262 // 3. Establish links between namespaces
4263 for (auto& ns_config : namespace_configs) {
4264 auto it_from = namespaces.find(ns_config->name());
4265 CHECK(it_from != namespaces.end());
4266 android_namespace_t* namespace_from = it_from->second;
4267 for (const NamespaceLinkConfig& ns_link : ns_config->links()) {
4268 auto it_to = namespaces.find(ns_link.ns_name());
4269 CHECK(it_to != namespaces.end());
4270 android_namespace_t* namespace_to = it_to->second;
Logan Chien9ee45912018-01-18 12:05:09 +08004271 if (ns_link.allow_all_shared_libs()) {
4272 link_namespaces_all_libs(namespace_from, namespace_to);
4273 } else {
4274 link_namespaces(namespace_from, namespace_to, ns_link.shared_libs().c_str());
4275 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08004276 }
4277 }
4278 // we can no longer rely on the fact that libdl.so is part of default namespace
4279 // this is why we want to add ld-android.so to all namespaces from ld.config.txt
4280 soinfo* ld_android_so = solist_get_head();
dimitry8b142562018-05-09 15:22:38 +02004281
4282 // we also need vdso to be available for all namespaces (if present)
4283 soinfo* vdso = solist_get_vdso();
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08004284 for (auto it : namespaces) {
4285 it.second->add_soinfo(ld_android_so);
dimitry8b142562018-05-09 15:22:38 +02004286 if (vdso != nullptr) {
4287 it.second->add_soinfo(vdso);
4288 }
Jiyong Park02586a22017-05-20 01:01:24 +09004289 // somain and ld_preloads are added to these namespaces after LD_PRELOAD libs are linked
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08004290 }
4291
4292 set_application_target_sdk_version(config->target_sdk_version());
Jiyong Park02586a22017-05-20 01:01:24 +09004293
4294 std::vector<android_namespace_t*> created_namespaces;
4295 created_namespaces.reserve(namespaces.size());
Chih-Hung Hsieh0218e922018-12-11 10:22:11 -08004296 for (const auto& kv : namespaces) {
Jiyong Park02586a22017-05-20 01:01:24 +09004297 created_namespaces.push_back(kv.second);
4298 }
4299 return created_namespaces;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08004300}
Jiyong Park01de74e2017-04-03 23:10:37 +09004301
4302// This function finds a namespace exported in ld.config.txt by its name.
4303// A namespace can be exported by setting .visible property to true.
4304android_namespace_t* get_exported_namespace(const char* name) {
4305 if (name == nullptr) {
4306 return nullptr;
4307 }
4308 auto it = g_exported_namespaces.find(std::string(name));
4309 if (it == g_exported_namespaces.end()) {
4310 return nullptr;
4311 }
4312 return it->second;
4313}
Vic Yangbb7e1232019-01-29 20:23:16 -08004314
4315void purge_unused_memory() {
4316 // For now, we only purge the memory used by LoadTask because we know those
4317 // are temporary objects.
4318 //
4319 // Purging other LinkerBlockAllocator hardly yields much because they hold
4320 // information about namespaces and opened libraries, which are not freed
4321 // when the control leaves the linker.
4322 //
4323 // Purging BionicAllocator may give us a few dirty pages back, but those pages
4324 // would be already zeroed out, so they compress easily in ZRAM. Therefore,
4325 // it is not worth munmap()'ing those pages.
4326 TypeBasedAllocator<LoadTask>::purge();
4327}