blob: 03b08b69539b2d198697fb8d877a764252d860d5 [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>
Christopher Ferris7a3681e2017-04-24 17:48:32 -070049#include <async_safe/log.h>
Ryan Pricharda2e83ab2019-08-16 17:25:43 -070050#include <bionic/pthread_internal.h>
Christopher Ferris7a3681e2017-04-24 17:48:32 -070051
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"
Ryan Prichard339ecef2020-01-02 16:36:06 -080066#include "linker_relocate.h"
Ryan Pricharde5e69e02019-01-01 18:53:48 -080067#include "linker_tls.h"
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070068#include "linker_utils.h"
tony.ys_liub4474402015-07-29 18:00:22 +080069
Peter Collingbournee9491952019-10-28 10:57:26 -070070#include "private/bionic_call_ifunc_resolver.h"
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
Ryan Pricharda2e83ab2019-08-16 17:25:43 -070089static uint64_t g_module_load_counter = 0;
90static uint64_t g_module_unload_counter = 0;
91
dimitryfe1b27c2017-08-11 14:43:21 +020092static const char* const kLdConfigArchFilePath = "/system/etc/ld.config." ABI_STRING ".txt";
93
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080094static const char* const kLdConfigFilePath = "/system/etc/ld.config.txt";
Roland Levillain09bde952019-10-29 19:17:49 +000095static const char* const kLdConfigVndkLiteFilePath = "/system/etc/ld.config.vndk_lite.txt";
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080096
Kiyoung Kim1a2460d2019-11-22 14:38:25 +090097static const char* const kLdGeneratedConfigFilePath = "/linkerconfig/ld.config.txt";
Kiyoung Kim35e3b4b2019-08-07 19:24:13 +090098
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070099#if defined(__LP64__)
Victor Chang6cb719f2019-02-06 17:19:10 +0000100static const char* const kSystemLibDir = "/system/lib64";
101static const char* const kOdmLibDir = "/odm/lib64";
102static const char* const kVendorLibDir = "/vendor/lib64";
103static const char* const kAsanSystemLibDir = "/data/asan/system/lib64";
104static const char* const kAsanOdmLibDir = "/data/asan/odm/lib64";
105static const char* const kAsanVendorLibDir = "/data/asan/vendor/lib64";
Martin Stjernholm6d5277d2019-07-05 22:38:07 +0100106static const char* const kArtApexLibDir = "/apex/com.android.art/lib64";
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700107#else
Victor Chang6cb719f2019-02-06 17:19:10 +0000108static const char* const kSystemLibDir = "/system/lib";
109static const char* const kOdmLibDir = "/odm/lib";
110static const char* const kVendorLibDir = "/vendor/lib";
111static const char* const kAsanSystemLibDir = "/data/asan/system/lib";
112static const char* const kAsanOdmLibDir = "/data/asan/odm/lib";
113static const char* const kAsanVendorLibDir = "/data/asan/vendor/lib";
Martin Stjernholm6d5277d2019-07-05 22:38:07 +0100114static const char* const kArtApexLibDir = "/apex/com.android.art/lib";
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700115#endif
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700116
Vishwath Mohan4113def2017-03-29 15:31:34 -0700117static const char* const kAsanLibDirPrefix = "/data/asan";
118
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700119static const char* const kDefaultLdPaths[] = {
120 kSystemLibDir,
Alin Jerpelea074a9fd2017-09-25 17:47:49 +0200121 kOdmLibDir,
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700122 kVendorLibDir,
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700123 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700124};
David Bartleybc3a5c22009-06-02 18:27:28 -0700125
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700126static const char* const kAsanDefaultLdPaths[] = {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700127 kAsanSystemLibDir,
128 kSystemLibDir,
Alin Jerpelea074a9fd2017-09-25 17:47:49 +0200129 kAsanOdmLibDir,
130 kOdmLibDir,
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700131 kAsanVendorLibDir,
132 kVendorLibDir,
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700133 nullptr
134};
135
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700136// Is ASAN enabled?
137static bool g_is_asan = false;
138
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700139static CFIShadowWriter g_cfi_shadow;
140
141CFIShadowWriter* get_cfi_shadow() {
142 return &g_cfi_shadow;
143}
144
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700145static bool is_system_library(const std::string& realpath) {
146 for (const auto& dir : g_default_namespace.get_default_library_paths()) {
147 if (file_is_in_dir(realpath, dir)) {
148 return true;
149 }
150 }
151 return false;
152}
153
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700154// Checks if the file exists and not a directory.
155static bool file_exists(const char* path) {
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700156 struct stat s;
157
158 if (stat(path, &s) != 0) {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700159 return false;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700160 }
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700161
162 return S_ISREG(s.st_mode);
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700163}
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700164
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800165static std::string resolve_soname(const std::string& name) {
166 // We assume that soname equals to basename here
167
168 // TODO(dimitry): consider having honest absolute-path -> soname resolution
169 // note that since we might end up refusing to load this library because
170 // it is not in shared libs list we need to get the soname without actually loading
171 // the library.
172 //
173 // On the other hand there are several places where we already assume that
174 // soname == basename in particular for any not-loaded library mentioned
175 // in DT_NEEDED list.
176 return basename(name.c_str());
177}
178
179static bool maybe_accessible_via_namespace_links(android_namespace_t* ns, const char* name) {
180 std::string soname = resolve_soname(name);
181 for (auto& ns_link : ns->linked_namespaces()) {
182 if (ns_link.is_accessible(soname.c_str())) {
183 return true;
184 }
185 }
186
187 return false;
188}
189
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700190// TODO(dimitry): The grey-list is a workaround for http://b/26394120 ---
191// gradually remove libraries from this list until it is gone.
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800192static bool is_greylisted(android_namespace_t* ns, const char* name, const soinfo* needed_by) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700193 static const char* const kLibraryGreyList[] = {
194 "libandroid_runtime.so",
195 "libbinder.so",
196 "libcrypto.so",
197 "libcutils.so",
198 "libexpat.so",
199 "libgui.so",
200 "libmedia.so",
201 "libnativehelper.so",
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700202 "libssl.so",
203 "libstagefright.so",
204 "libsqlite.so",
205 "libui.so",
206 "libutils.so",
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700207 nullptr
208 };
209
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800210 // If you're targeting N, you don't get the greylist.
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800211 if (g_greylist_disabled || get_application_target_sdk_version() >= 24) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700212 return false;
213 }
214
215 // if the library needed by a system library - implicitly assume it
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800216 // is greylisted unless it is in the list of shared libraries for one or
217 // more linked namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700218 if (needed_by != nullptr && is_system_library(needed_by->get_realpath())) {
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800219 return !maybe_accessible_via_namespace_links(ns, name);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700220 }
221
222 // if this is an absolute path - make sure it points to /system/lib(64)
223 if (name[0] == '/' && dirname(name) == kSystemLibDir) {
224 // and reduce the path to basename
225 name = basename(name);
226 }
227
228 for (size_t i = 0; kLibraryGreyList[i] != nullptr; ++i) {
229 if (strcmp(name, kLibraryGreyList[i]) == 0) {
230 return true;
231 }
232 }
233
234 return false;
235}
236// END OF WORKAROUND
237
Victor Chang6cb719f2019-02-06 17:19:10 +0000238// Workaround for dlopen(/system/lib(64)/<soname>) when .so is in /apex. http://b/121248172
239/**
240 * Translate /system path to /apex path if needed
241 * The workaround should work only when targetSdkVersion < Q.
242 *
243 * param out_name_to_apex pointing to /apex path
244 * return true if translation is needed
245 */
246static bool translateSystemPathToApexPath(const char* name, std::string* out_name_to_apex) {
Martin Stjernholm6d5277d2019-07-05 22:38:07 +0100247 static const char* const kSystemToArtApexLibs[] = {
Victor Chang6cb719f2019-02-06 17:19:10 +0000248 "libicuuc.so",
249 "libicui18n.so",
250 };
251 // New mapping for new apex should be added below
252
253 // Nothing to do if target sdk version is Q or above
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800254 if (get_application_target_sdk_version() >= 29) {
Victor Chang6cb719f2019-02-06 17:19:10 +0000255 return false;
256 }
257
258 // If the path isn't /system/lib, there's nothing to do.
259 if (name == nullptr || dirname(name) != kSystemLibDir) {
260 return false;
261 }
262
263 const char* base_name = basename(name);
264
Martin Stjernholm6d5277d2019-07-05 22:38:07 +0100265 for (const char* soname : kSystemToArtApexLibs) {
Victor Chang6cb719f2019-02-06 17:19:10 +0000266 if (strcmp(base_name, soname) == 0) {
Martin Stjernholm6d5277d2019-07-05 22:38:07 +0100267 *out_name_to_apex = std::string(kArtApexLibDir) + "/" + base_name;
Victor Chang6cb719f2019-02-06 17:19:10 +0000268 return true;
269 }
270 }
271
272 return false;
273}
274// End Workaround for dlopen(/system/lib/<soname>) when .so is in /apex.
275
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700276static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800277
Elliott Hughesbedfe382012-08-14 14:07:59 -0700278static void notify_gdb_of_load(soinfo* info) {
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800279 if (info->is_linker() || info->is_main_executable()) {
280 // gdb already knows about the linker and the main executable.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700281 return;
282 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800283
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800284 link_map* map = &(info->link_map_head);
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000285
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800286 map->l_addr = info->load_bias;
287 // link_map l_name field is not const.
288 map->l_name = const_cast<char*>(info->get_realpath());
289 map->l_ld = info->dynamic;
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000290
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800291 CHECK(map->l_name != nullptr);
292 CHECK(map->l_name[0] != '\0');
293
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800294 notify_gdb_of_load(map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700295}
296
Elliott Hughesbedfe382012-08-14 14:07:59 -0700297static void notify_gdb_of_unload(soinfo* info) {
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800298 notify_gdb_of_unload(&(info->link_map_head));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800299}
300
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700301LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
302 return g_soinfo_links_allocator.alloc();
303}
304
305void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
306 g_soinfo_links_allocator.free(entry);
307}
308
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700309LinkedListEntry<android_namespace_t>* NamespaceListAllocator::alloc() {
310 return g_namespace_list_allocator.alloc();
311}
312
313void NamespaceListAllocator::free(LinkedListEntry<android_namespace_t>* entry) {
314 g_namespace_list_allocator.free(entry);
315}
316
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700317soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
Ryan Prichard8f639a42018-10-01 23:10:05 -0700318 const struct stat* file_stat, off64_t file_offset,
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700319 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700320 if (strlen(name) >= PATH_MAX) {
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700321 async_safe_fatal("library name \"%s\" too long", name);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200322 }
323
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700324 TRACE("name %s: allocating soinfo for ns=%p", name, ns);
325
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700326 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
327 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700328
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700329 solist_add_soinfo(si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200330
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700331 si->generate_handle();
332 ns->add_soinfo(si);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700333
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700334 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200335 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800336}
337
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800338static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700339 if (si == nullptr) {
340 return;
341 }
342
343 if (si->base != 0 && si->size != 0) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800344 if (!si->is_mapped_by_caller()) {
345 munmap(reinterpret_cast<void*>(si->base), si->size);
346 } else {
347 // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE
348 mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE,
349 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
350 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700351 }
352
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700353 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700354
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700355 if (!solist_remove_soinfo(si)) {
dimitry965d06d2017-11-28 16:03:07 +0100356 async_safe_fatal("soinfo=%p is not in soinfo_list (double unload?)", si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700357 }
Elliott Hughes46882792012-08-03 16:49:39 -0700358
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700359 // clear links to/from si
360 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700361
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700362 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700363 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800364}
365
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700366static void parse_path(const char* path, const char* delimiters,
367 std::vector<std::string>* resolved_paths) {
368 std::vector<std::string> paths;
369 split_path(path, delimiters, &paths);
370 resolve_paths(paths, resolved_paths);
371}
372
Elliott Hughescade4c32012-12-20 14:42:14 -0800373static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700374 std::vector<std::string> ld_libary_paths;
375 parse_path(path, ":", &ld_libary_paths);
376 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800377}
378
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700379static bool realpath_fd(int fd, std::string* realpath) {
Vic Yangd8bef672019-05-15 21:10:22 -0700380 // proc_self_fd needs to be large enough to hold "/proc/self/fd/" plus an
381 // integer, plus the NULL terminator.
382 char proc_self_fd[32];
383 // We want to statically allocate this large buffer so that we don't grow
384 // the stack by too much.
385 static char buf[PATH_MAX];
386
387 async_safe_format_buffer(proc_self_fd, sizeof(proc_self_fd), "/proc/self/fd/%d", fd);
388 auto length = readlink(proc_self_fd, buf, sizeof(buf));
389 if (length == -1) {
Tom Cherry66bc4282018-11-08 13:40:52 -0800390 if (!is_first_stage_init()) {
Vic Yangd8bef672019-05-15 21:10:22 -0700391 PRINT("readlink(\"%s\") failed: %s [fd=%d]", proc_self_fd, strerror(errno), fd);
Tom Cherry66bc4282018-11-08 13:40:52 -0800392 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700393 return false;
394 }
395
Vic Yangd8bef672019-05-15 21:10:22 -0700396 realpath->assign(buf, length);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700397 return true;
398}
399
Ryan Pricharda2e83ab2019-08-16 17:25:43 -0700400// Returns the address of the current thread's copy of a TLS module. If the current thread doesn't
401// have a copy yet, allocate one on-demand if should_alloc is true, and return nullptr otherwise.
402static inline void* get_tls_block_for_this_thread(const soinfo_tls* si_tls, bool should_alloc) {
403 const TlsModule& tls_mod = get_tls_module(si_tls->module_id);
404 if (tls_mod.static_offset != SIZE_MAX) {
405 const StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout;
406 char* static_tls = reinterpret_cast<char*>(__get_bionic_tcb()) - layout.offset_bionic_tcb();
407 return static_tls + tls_mod.static_offset;
408 } else if (should_alloc) {
409 const TlsIndex ti { si_tls->module_id, 0 };
410 return TLS_GET_ADDR(&ti);
411 } else {
412 TlsDtv* dtv = __get_tcb_dtv(__get_bionic_tcb());
413 if (dtv->generation < tls_mod.first_generation) return nullptr;
414 return dtv->modules[__tls_module_id_to_idx(si_tls->module_id)];
415 }
416}
417
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700418#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700419
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700420// For a given PC, find the .so that it belongs to.
421// Returns the base address of the .ARM.exidx section
422// for that .so, and the number of 8-byte entries
423// in that section (via *pcount).
424//
425// Intended to be called by libc's __gnu_Unwind_Find_exidx().
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800426_Unwind_Ptr do_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Peter Collingbourneb39cb3c2019-03-01 13:12:49 -0800427 if (soinfo* si = find_containing_library(reinterpret_cast<void*>(pc))) {
428 *pcount = si->ARM_exidx_count;
429 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700430 }
431 *pcount = 0;
Elliott Hughesf2c6ad62017-04-21 10:25:56 -0700432 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800433}
Elliott Hughes46882792012-08-03 16:49:39 -0700434
Christopher Ferris24053a42013-08-19 17:45:09 -0700435#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700436
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700437// Here, we only have to provide a callback to iterate across all the
438// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700439int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700440 int rv = 0;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700441 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700442 dl_phdr_info dl_info;
443 dl_info.dlpi_addr = si->link_map_head.l_addr;
444 dl_info.dlpi_name = si->link_map_head.l_name;
445 dl_info.dlpi_phdr = si->phdr;
446 dl_info.dlpi_phnum = si->phnum;
Ryan Pricharda2e83ab2019-08-16 17:25:43 -0700447 dl_info.dlpi_adds = g_module_load_counter;
448 dl_info.dlpi_subs = g_module_unload_counter;
449 if (soinfo_tls* tls_module = si->get_tls()) {
450 dl_info.dlpi_tls_modid = tls_module->module_id;
451 dl_info.dlpi_tls_data = get_tls_block_for_this_thread(tls_module, /*should_alloc=*/false);
452 } else {
453 dl_info.dlpi_tls_modid = 0;
454 dl_info.dlpi_tls_data = nullptr;
455 }
456
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700457 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
458 if (rv != 0) {
459 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800460 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700461 }
462 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800463}
Elliott Hughes46882792012-08-03 16:49:39 -0700464
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700465ProtectedDataGuard::ProtectedDataGuard() {
466 if (ref_count_++ == 0) {
467 protect_data(PROT_READ | PROT_WRITE);
468 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700469
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700470 if (ref_count_ == 0) { // overflow
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700471 async_safe_fatal("Too many nested calls to dlopen()");
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800472 }
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800473}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800474
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800475ProtectedDataGuard::~ProtectedDataGuard() {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700476 if (--ref_count_ == 0) {
477 protect_data(PROT_READ);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800478 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700479}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800480
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700481void ProtectedDataGuard::protect_data(int protection) {
482 g_soinfo_allocator.protect_all(protection);
483 g_soinfo_links_allocator.protect_all(protection);
484 g_namespace_allocator.protect_all(protection);
485 g_namespace_list_allocator.protect_all(protection);
486}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800487
488size_t ProtectedDataGuard::ref_count_ = 0;
489
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700490// Each size has it's own allocator.
491template<size_t size>
492class SizeBasedAllocator {
493 public:
494 static void* alloc() {
495 return allocator_.alloc();
496 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700497
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700498 static void free(void* ptr) {
499 allocator_.free(ptr);
500 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700501
Vic Yangbb7e1232019-01-29 20:23:16 -0800502 static void purge() {
503 allocator_.purge();
504 }
505
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700506 private:
507 static LinkerBlockAllocator allocator_;
508};
509
510template<size_t size>
511LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
512
513template<typename T>
514class TypeBasedAllocator {
515 public:
516 static T* alloc() {
517 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
518 }
519
520 static void free(T* ptr) {
521 SizeBasedAllocator<sizeof(T)>::free(ptr);
522 }
Vic Yangbb7e1232019-01-29 20:23:16 -0800523
524 static void purge() {
525 SizeBasedAllocator<sizeof(T)>::purge();
526 }
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700527};
528
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700529class LoadTask {
530 public:
531 struct deleter_t {
532 void operator()(LoadTask* t) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700533 t->~LoadTask();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700534 TypeBasedAllocator<LoadTask>::free(t);
535 }
536 };
537
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700538 static deleter_t deleter;
539
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800540 static LoadTask* create(const char* name,
541 soinfo* needed_by,
Jiyong Park02586a22017-05-20 01:01:24 +0900542 android_namespace_t* start_from,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700543 std::unordered_map<const soinfo*, ElfReader>* readers_map) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700544 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
Jiyong Park02586a22017-05-20 01:01:24 +0900545 return new (ptr) LoadTask(name, needed_by, start_from, readers_map);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700546 }
547
548 const char* get_name() const {
549 return name_;
550 }
551
552 soinfo* get_needed_by() const {
553 return needed_by_;
554 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700555
556 soinfo* get_soinfo() const {
557 return si_;
558 }
559
560 void set_soinfo(soinfo* si) {
561 si_ = si;
562 }
563
564 off64_t get_file_offset() const {
565 return file_offset_;
566 }
567
568 void set_file_offset(off64_t offset) {
569 file_offset_ = offset;
570 }
571
572 int get_fd() const {
573 return fd_;
574 }
575
576 void set_fd(int fd, bool assume_ownership) {
Martin Stjernholmde853ff2019-01-17 00:18:44 +0000577 if (fd_ != -1 && close_fd_) {
578 close(fd_);
579 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700580 fd_ = fd;
581 close_fd_ = assume_ownership;
582 }
583
584 const android_dlextinfo* get_extinfo() const {
585 return extinfo_;
586 }
587
588 void set_extinfo(const android_dlextinfo* extinfo) {
589 extinfo_ = extinfo;
590 }
591
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700592 bool is_dt_needed() const {
593 return is_dt_needed_;
594 }
595
596 void set_dt_needed(bool is_dt_needed) {
597 is_dt_needed_ = is_dt_needed;
598 }
599
Jiyong Park02586a22017-05-20 01:01:24 +0900600 // returns the namespace from where we need to start loading this.
601 const android_namespace_t* get_start_from() const {
602 return start_from_;
603 }
604
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700605 const ElfReader& get_elf_reader() const {
606 CHECK(si_ != nullptr);
607 return (*elf_readers_map_)[si_];
608 }
609
610 ElfReader& get_elf_reader() {
611 CHECK(si_ != nullptr);
612 return (*elf_readers_map_)[si_];
613 }
614
615 std::unordered_map<const soinfo*, ElfReader>* get_readers_map() {
616 return elf_readers_map_;
617 }
618
619 bool read(const char* realpath, off64_t file_size) {
620 ElfReader& elf_reader = get_elf_reader();
621 return elf_reader.Read(realpath, fd_, file_offset_, file_size);
622 }
623
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400624 bool load(address_space_params* address_space) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700625 ElfReader& elf_reader = get_elf_reader();
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400626 if (!elf_reader.Load(address_space)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700627 return false;
628 }
629
630 si_->base = elf_reader.load_start();
631 si_->size = elf_reader.load_size();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800632 si_->set_mapped_by_caller(elf_reader.is_mapped_by_caller());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700633 si_->load_bias = elf_reader.load_bias();
634 si_->phnum = elf_reader.phdr_count();
635 si_->phdr = elf_reader.loaded_phdr();
636
637 return true;
638 }
639
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700640 private:
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800641 LoadTask(const char* name,
642 soinfo* needed_by,
Jiyong Park02586a22017-05-20 01:01:24 +0900643 android_namespace_t* start_from,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700644 std::unordered_map<const soinfo*, ElfReader>* readers_map)
645 : name_(name), needed_by_(needed_by), si_(nullptr),
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700646 fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map),
Jiyong Park02586a22017-05-20 01:01:24 +0900647 is_dt_needed_(false), start_from_(start_from) {}
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700648
649 ~LoadTask() {
650 if (fd_ != -1 && close_fd_) {
651 close(fd_);
652 }
653 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700654
655 const char* name_;
656 soinfo* needed_by_;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700657 soinfo* si_;
658 const android_dlextinfo* extinfo_;
659 int fd_;
660 bool close_fd_;
661 off64_t file_offset_;
662 std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_;
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700663 // TODO(dimitry): needed by workaround for http://b/26394120 (the grey-list)
664 bool is_dt_needed_;
665 // END OF WORKAROUND
Jiyong Park02586a22017-05-20 01:01:24 +0900666 const android_namespace_t* const start_from_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700667
668 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
669};
670
Ningsheng Jiane93be992014-09-16 15:22:10 +0800671LoadTask::deleter_t LoadTask::deleter;
672
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700673template <typename T>
674using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
675
676typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700677typedef linked_list_t<const char> StringLinkedList;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700678typedef std::vector<LoadTask*> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700679
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800680enum walk_action_result_t : uint32_t {
681 kWalkStop = 0,
682 kWalkContinue = 1,
683 kWalkSkip = 2
684};
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700685
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700686// This function walks down the tree of soinfo dependencies
687// in breadth-first order and
688// * calls action(soinfo* si) for each node, and
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800689// * terminates walk if action returns kWalkStop
690// * skips children of the node if action
691// return kWalkSkip
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700692//
693// walk_dependencies_tree returns false if walk was terminated
694// by the action and true otherwise.
695template<typename F>
dimitry965d06d2017-11-28 16:03:07 +0100696static bool walk_dependencies_tree(soinfo* root_soinfo, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700697 SoinfoLinkedList visit_list;
698 SoinfoLinkedList visited;
699
dimitry965d06d2017-11-28 16:03:07 +0100700 visit_list.push_back(root_soinfo);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700701
702 soinfo* si;
703 while ((si = visit_list.pop_front()) != nullptr) {
704 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700705 continue;
706 }
707
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800708 walk_action_result_t result = action(si);
709
710 if (result == kWalkStop) {
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700711 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700712 }
713
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700714 visited.push_back(si);
715
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800716 if (result != kWalkSkip) {
717 si->get_children().for_each([&](soinfo* child) {
718 visit_list.push_back(child);
719 });
720 }
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700721 }
722
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700723 return true;
724}
725
726
Ryan Prichard272458e2020-01-23 16:07:27 -0800727static const ElfW(Sym)* dlsym_handle_lookup_impl(android_namespace_t* ns,
728 soinfo* root,
729 soinfo* skip_until,
730 soinfo** found,
731 SymbolName& symbol_name,
732 const version_info* vi) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700733 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700734 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700735
dimitry965d06d2017-11-28 16:03:07 +0100736 walk_dependencies_tree(root, [&](soinfo* current_soinfo) {
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700737 if (skip_lookup) {
738 skip_lookup = current_soinfo != skip_until;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800739 return kWalkContinue;
740 }
741
742 if (!ns->is_accessible(current_soinfo)) {
743 return kWalkSkip;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700744 }
745
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800746 result = current_soinfo->find_symbol_by_name(symbol_name, vi);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700747 if (result != nullptr) {
748 *found = current_soinfo;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800749 return kWalkStop;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700750 }
751
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800752 return kWalkContinue;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700753 });
754
755 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800756}
757
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800758/* This is used by dlsym(3) to performs a global symbol lookup. If the
759 start value is null (for RTLD_DEFAULT), the search starts at the
760 beginning of the global solist. Otherwise the search starts at the
761 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700762 */
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800763static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
764 const char* name,
765 const version_info* vi,
766 soinfo** found,
767 soinfo* caller,
768 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800769 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800770
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700771 auto& soinfo_list = ns->soinfo_list();
772 auto start = soinfo_list.begin();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700773
774 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -0700775 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700776 return nullptr;
777 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700778 auto it = soinfo_list.find(caller);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700779 CHECK (it != soinfo_list.end());
780 start = ++it;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700781 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800782 }
783
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700784 const ElfW(Sym)* s = nullptr;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700785 for (auto it = start, end = soinfo_list.end(); it != end; ++it) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700786 soinfo* si = *it;
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700787 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800788 // if the library is opened by application with target api level < M.
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700789 // See http://b/21565766
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800790 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 && si->get_target_sdk_version() >= 23) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700791 continue;
792 }
793
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800794 s = si->find_symbol_by_name(symbol_name, vi);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700795 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800796 *found = si;
797 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600798 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800799 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600800
Ryan Prichard272458e2020-01-23 16:07:27 -0800801 // If not found - use dlsym_handle_lookup_impl for caller's local_group
dimitry153168c2018-02-20 16:51:41 +0100802 if (s == nullptr && caller != nullptr) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800803 soinfo* local_group_root = caller->get_local_group_root();
804
Ryan Prichard272458e2020-01-23 16:07:27 -0800805 return dlsym_handle_lookup_impl(local_group_root->get_primary_namespace(),
806 local_group_root,
807 (handle == RTLD_NEXT) ? caller : nullptr,
808 found,
809 symbol_name,
810 vi);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700811 }
812
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700813 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700814 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
815 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800816 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800817
Elliott Hughescade4c32012-12-20 14:42:14 -0800818 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800819}
820
Ryan Prichard272458e2020-01-23 16:07:27 -0800821// This is used by dlsym(3). It performs symbol lookup only within the
822// specified soinfo object and its dependencies in breadth first order.
823static const ElfW(Sym)* dlsym_handle_lookup(soinfo* si,
824 soinfo** found,
825 const char* name,
826 const version_info* vi) {
827 // According to man dlopen(3) and posix docs in the case when si is handle
828 // of the main executable we need to search not only in the executable and its
829 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
830 //
831 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
832 // libraries and they are loaded in breath-first (correct) order we can just execute
833 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
834 if (si == solist_get_somain()) {
835 return dlsym_linear_lookup(&g_default_namespace, name, vi, found, nullptr, RTLD_DEFAULT);
836 }
837
838 SymbolName symbol_name(name);
839 // note that the namespace is not the namespace associated with caller_addr
840 // we use ns associated with root si intentionally here. Using caller_ns
841 // causes problems when user uses dlopen_ext to open a library in the separate
842 // namespace and then calls dlsym() on the handle.
843 return dlsym_handle_lookup_impl(si->get_primary_namespace(), si, nullptr, found, symbol_name, vi);
844}
845
Kito Chengfa8c05d2013-03-12 14:58:06 +0800846soinfo* find_containing_library(const void* p) {
Peter Collingbourne191ecdc2019-08-07 19:06:00 -0700847 // Addresses within a library may be tagged if they point to globals. Untag
848 // them so that the bounds check succeeds.
849 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(untag_address(p));
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700850 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Peter Collingbourneb39cb3c2019-03-01 13:12:49 -0800851 if (address < si->base || address - si->base >= si->size) {
852 continue;
853 }
854 ElfW(Addr) vaddr = address - si->load_bias;
855 for (size_t i = 0; i != si->phnum; ++i) {
856 const ElfW(Phdr)* phdr = &si->phdr[i];
857 if (phdr->p_type != PT_LOAD) {
858 continue;
859 }
860 if (vaddr >= phdr->p_vaddr && vaddr < phdr->p_vaddr + phdr->p_memsz) {
861 return si;
862 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600863 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800864 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700865 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600866}
867
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700868class ZipArchiveCache {
869 public:
870 ZipArchiveCache() {}
871 ~ZipArchiveCache();
872
873 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
874 private:
875 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
876
877 std::unordered_map<std::string, ZipArchiveHandle> cache_;
878};
879
880bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
881 std::string key(zip_path);
882
883 auto it = cache_.find(key);
884 if (it != cache_.end()) {
885 *handle = it->second;
886 return true;
887 }
888
889 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
890 if (fd == -1) {
891 return false;
892 }
893
894 if (OpenArchiveFd(fd, "", handle) != 0) {
895 // invalid zip-file (?)
Ryan Prichard0adf09b2018-10-01 18:35:46 -0700896 CloseArchive(*handle);
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700897 return false;
898 }
899
900 cache_[key] = *handle;
901 return true;
902}
903
904ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -0700905 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700906 CloseArchive(it.second);
907 }
908}
909
910static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700911 const char* const input_path,
912 off64_t* file_offset, std::string* realpath) {
913 std::string normalized_path;
914 if (!normalize_path(input_path, &normalized_path)) {
915 return -1;
916 }
917
918 const char* const path = normalized_path.c_str();
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700919 TRACE("Trying zip file open from path \"%s\" -> normalized \"%s\"", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +0000920
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700921 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +0000922 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700923 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +0000924 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700925 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +0000926 if (separator == nullptr) {
927 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -0700928 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000929
930 char buf[512];
931 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
932 PRINT("Warning: ignoring very long library path: %s", path);
933 return -1;
934 }
935
936 buf[separator - path] = '\0';
937
938 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700939 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +0000940 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
941 if (fd == -1) {
942 return -1;
943 }
944
945 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700946 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000947 // invalid zip-file (?)
948 close(fd);
949 return -1;
950 }
951
Simon Baldwinaef71952015-01-16 13:22:54 +0000952 ZipEntry entry;
953
Elliott Hughesb51bb502019-05-03 22:45:41 -0700954 if (FindEntry(handle, file_path, &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000955 // Entry was not found.
956 close(fd);
957 return -1;
958 }
959
960 // Check if it is properly stored
961 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
962 close(fd);
963 return -1;
964 }
965
966 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700967
968 if (realpath_fd(fd, realpath)) {
969 *realpath += separator;
970 } else {
Tom Cherry66bc4282018-11-08 13:40:52 -0800971 if (!is_first_stage_init()) {
972 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
973 normalized_path.c_str());
974 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700975 *realpath = normalized_path;
976 }
977
Simon Baldwinaef71952015-01-16 13:22:54 +0000978 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800979}
980
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700981static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700982 int n = async_safe_format_buffer(buf, buf_size, "%s/%s", path, name);
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700983 if (n < 0 || n >= static_cast<int>(buf_size)) {
984 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
985 return false;
986 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000987
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700988 return true;
989}
990
Ryan Prichard8f639a42018-10-01 23:10:05 -0700991static int open_library_at_path(ZipArchiveCache* zip_archive_cache,
992 const char* path, off64_t* file_offset,
993 std::string* realpath) {
994 int fd = -1;
995 if (strstr(path, kZipFileSeparator) != nullptr) {
996 fd = open_library_in_zipfile(zip_archive_cache, path, file_offset, realpath);
997 }
998
999 if (fd == -1) {
1000 fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC));
1001 if (fd != -1) {
1002 *file_offset = 0;
1003 if (!realpath_fd(fd, realpath)) {
Tom Cherry66bc4282018-11-08 13:40:52 -08001004 if (!is_first_stage_init()) {
1005 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1006 path);
1007 }
Ryan Prichard8f639a42018-10-01 23:10:05 -07001008 *realpath = path;
1009 }
1010 }
1011 }
1012
1013 return fd;
1014}
1015
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001016static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1017 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001018 const std::vector<std::string>& paths,
1019 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001020 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001021 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001022 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001023 continue;
1024 }
1025
Ryan Prichard8f639a42018-10-01 23:10:05 -07001026 int fd = open_library_at_path(zip_archive_cache, buf, file_offset, realpath);
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001027 if (fd != -1) {
1028 return fd;
1029 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001030 }
1031
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001032 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001033}
1034
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001035static int open_library(android_namespace_t* ns,
1036 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001037 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001038 off64_t* file_offset, std::string* realpath) {
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001039 TRACE("[ opening %s from namespace %s ]", name, ns->get_name());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001040
Elliott Hughes124fae92012-10-31 14:20:03 -07001041 // 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 -07001042 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001043 int fd = -1;
1044
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001045 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001046 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1047 }
1048
1049 if (fd == -1) {
1050 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001051 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001052 *file_offset = 0;
1053 if (!realpath_fd(fd, realpath)) {
Tom Cherry66bc4282018-11-08 13:40:52 -08001054 if (!is_first_stage_init()) {
1055 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1056 name);
1057 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001058 *realpath = name;
1059 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001060 }
1061 }
1062
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001063 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001064 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001065
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001066 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1067 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 -07001068 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001069 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001070 // Check if the library is accessible
1071 if (fd != -1 && !ns->is_accessible(*realpath)) {
Vic Yang48b69112018-10-24 14:14:26 -07001072 close(fd);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001073 fd = -1;
1074 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001075 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001076
Peter Collingbourneea11be02019-04-30 16:05:13 -07001077#if !defined(__ANDROID_APEX__)
1078 if (fd == -1) {
1079 std::vector<std::string> bootstrap_paths = { std::string(kSystemLibDir) + "/bootstrap" };
1080 fd = open_library_on_paths(zip_archive_cache, name, file_offset, bootstrap_paths, realpath);
1081 }
1082#endif
1083
Elliott Hughes124fae92012-10-31 14:20:03 -07001084 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001085 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001086 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001087
Elliott Hughes124fae92012-10-31 14:20:03 -07001088 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001089}
1090
Ryan Prichard8f639a42018-10-01 23:10:05 -07001091int open_executable(const char* path, off64_t* file_offset, std::string* realpath) {
1092 ZipArchiveCache zip_archive_cache;
1093 return open_library_at_path(&zip_archive_cache, path, file_offset, realpath);
1094}
1095
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001096const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001097#if !defined(__LP64__)
1098 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001099 int app_target_api_level = get_application_target_sdk_version();
Elliott Hughes95c6cd72019-12-20 13:26:14 -08001100 if (app_target_api_level < 23) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001101 const char* bname = basename(dt_needed);
1102 if (bname != dt_needed) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -08001103 DL_WARN_documented_change(23,
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001104 "invalid-dt_needed-entries-enforced-for-api-level-23",
1105 "library \"%s\" has invalid DT_NEEDED entry \"%s\"",
1106 sopath, dt_needed, app_target_api_level);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001107 add_dlwarning(sopath, "invalid DT_NEEDED entry", dt_needed);
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001108 }
1109
1110 return bname;
1111 }
1112#endif
1113 return dt_needed;
1114}
1115
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001116template<typename F>
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001117static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
1118 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1119 if (d->d_tag == DT_NEEDED) {
1120 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1121 }
1122 }
1123}
1124
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001125static bool find_loaded_library_by_inode(android_namespace_t* ns,
1126 const struct stat& file_stat,
1127 off64_t file_offset,
1128 bool search_linked_namespaces,
1129 soinfo** candidate) {
Vic Yang3ec16be2019-06-02 21:10:53 -07001130 if (file_stat.st_dev == 0 || file_stat.st_ino == 0) {
1131 *candidate = nullptr;
1132 return false;
1133 }
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001134
1135 auto predicate = [&](soinfo* si) {
Vic Yang3ec16be2019-06-02 21:10:53 -07001136 return si->get_st_ino() == file_stat.st_ino &&
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001137 si->get_st_dev() == file_stat.st_dev &&
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001138 si->get_file_offset() == file_offset;
1139 };
1140
1141 *candidate = ns->soinfo_list().find_if(predicate);
1142
1143 if (*candidate == nullptr && search_linked_namespaces) {
1144 for (auto& link : ns->linked_namespaces()) {
1145 android_namespace_t* linked_ns = link.linked_namespace();
1146 soinfo* si = linked_ns->soinfo_list().find_if(predicate);
1147
1148 if (si != nullptr && link.is_accessible(si->get_soname())) {
1149 *candidate = si;
1150 return true;
1151 }
1152 }
1153 }
1154
1155 return *candidate != nullptr;
1156}
1157
Evgenii Stepanov9e77a642017-07-27 14:55:44 -07001158static bool find_loaded_library_by_realpath(android_namespace_t* ns, const char* realpath,
1159 bool search_linked_namespaces, soinfo** candidate) {
1160 auto predicate = [&](soinfo* si) { return strcmp(realpath, si->get_realpath()) == 0; };
1161
1162 *candidate = ns->soinfo_list().find_if(predicate);
1163
1164 if (*candidate == nullptr && search_linked_namespaces) {
1165 for (auto& link : ns->linked_namespaces()) {
1166 android_namespace_t* linked_ns = link.linked_namespace();
1167 soinfo* si = linked_ns->soinfo_list().find_if(predicate);
1168
1169 if (si != nullptr && link.is_accessible(si->get_soname())) {
1170 *candidate = si;
1171 return true;
1172 }
1173 }
1174 }
1175
1176 return *candidate != nullptr;
1177}
1178
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001179static bool load_library(android_namespace_t* ns,
1180 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001181 LoadTaskList* load_tasks,
1182 int rtld_flags,
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001183 const std::string& realpath,
1184 bool search_linked_namespaces) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001185 off64_t file_offset = task->get_file_offset();
1186 const char* name = task->get_name();
1187 const android_dlextinfo* extinfo = task->get_extinfo();
1188
Martin Stjernholm9fe38262019-08-27 17:08:45 +01001189 LD_LOG(kLogDlopen,
1190 "load_library(ns=%s, task=%s, flags=0x%x, realpath=%s, search_linked_namespaces=%d)",
1191 ns->get_name(), name, rtld_flags, realpath.c_str(), search_linked_namespaces);
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001192
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001193 if ((file_offset % PAGE_SIZE) != 0) {
Martin Stjernholm9fe38262019-08-27 17:08:45 +01001194 DL_OPEN_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001195 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001196 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001197 if (file_offset < 0) {
Martin Stjernholm9fe38262019-08-27 17:08:45 +01001198 DL_OPEN_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001199 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001200 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001201
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001202 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001203 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Martin Stjernholm9fe38262019-08-27 17:08:45 +01001204 DL_OPEN_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001205 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001206 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001207 if (file_offset >= file_stat.st_size) {
Martin Stjernholm9fe38262019-08-27 17:08:45 +01001208 DL_OPEN_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001209 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001210 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001211 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001212
1213 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001214 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1215 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001216 soinfo* si = nullptr;
1217 if (find_loaded_library_by_inode(ns, file_stat, file_offset, search_linked_namespaces, &si)) {
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001218 LD_LOG(kLogDlopen,
1219 "load_library(ns=%s, task=%s): Already loaded under different name/path \"%s\" - "
1220 "will return existing soinfo",
1221 ns->get_name(), name, si->get_realpath());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001222 task->set_soinfo(si);
1223 return true;
1224 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001225 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001226
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001227 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Martin Stjernholm9fe38262019-08-27 17:08:45 +01001228 DL_OPEN_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001229 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001230 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001231
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -07001232 struct statfs fs_stat;
1233 if (TEMP_FAILURE_RETRY(fstatfs(task->get_fd(), &fs_stat)) != 0) {
Martin Stjernholm9fe38262019-08-27 17:08:45 +01001234 DL_OPEN_ERR("unable to fstatfs file for the library \"%s\": %s", name, strerror(errno));
Dimitry Ivanovbf34ba32017-04-21 13:12:05 -07001235 return false;
1236 }
1237
1238 // do not check accessibility using realpath if fd is located on tmpfs
1239 // this enables use of memfd_create() for apps
1240 if ((fs_stat.f_type != TMPFS_MAGIC) && (!ns->is_accessible(realpath))) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001241 // TODO(dimitry): workaround for http://b/26394120 - the grey-list
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001242
1243 // TODO(dimitry) before O release: add a namespace attribute to have this enabled
1244 // only for classloader-namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001245 const soinfo* needed_by = task->is_dt_needed() ? task->get_needed_by() : nullptr;
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001246 if (is_greylisted(ns, name, needed_by)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001247 // print warning only if needed by non-system library
1248 if (needed_by == nullptr || !is_system_library(needed_by->get_realpath())) {
1249 const soinfo* needed_or_dlopened_by = task->get_needed_by();
1250 const char* sopath = needed_or_dlopened_by == nullptr ? "(unknown)" :
1251 needed_or_dlopened_by->get_realpath();
Elliott Hughes95c6cd72019-12-20 13:26:14 -08001252 DL_WARN_documented_change(24,
Elliott Hughes9076b0c2018-02-28 11:29:45 -08001253 "private-api-enforced-for-api-level-24",
1254 "library \"%s\" (\"%s\") needed or dlopened by \"%s\" "
1255 "is not accessible by namespace \"%s\"",
1256 name, realpath.c_str(), sopath, ns->get_name());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001257 add_dlwarning(sopath, "unauthorized access to", name);
1258 }
1259 } else {
1260 // do not load libraries if they are not accessible for the specified namespace.
1261 const char* needed_or_dlopened_by = task->get_needed_by() == nullptr ?
1262 "(unknown)" :
1263 task->get_needed_by()->get_realpath();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001264
Martin Stjernholm9fe38262019-08-27 17:08:45 +01001265 DL_OPEN_ERR("library \"%s\" needed or dlopened by \"%s\" is not accessible for the namespace \"%s\"",
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001266 name, needed_or_dlopened_by, ns->get_name());
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001267
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001268 // do not print this if a library is in the list of shared libraries for linked namespaces
1269 if (!maybe_accessible_via_namespace_links(ns, name)) {
1270 PRINT("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the"
1271 " namespace: [name=\"%s\", ld_library_paths=\"%s\", default_library_paths=\"%s\","
1272 " permitted_paths=\"%s\"]",
1273 name, realpath.c_str(),
1274 needed_or_dlopened_by,
1275 ns->get_name(),
1276 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
1277 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
1278 android::base::Join(ns->get_permitted_paths(), ':').c_str());
1279 }
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001280 return false;
1281 }
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001282 }
1283
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001284 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001285 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001286 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001287 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001288
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001289 task->set_soinfo(si);
1290
1291 // Read the ELF header and some of the segments.
1292 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001293 soinfo_free(si);
1294 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001295 return false;
1296 }
1297
1298 // find and set DT_RUNPATH and dt_soname
1299 // Note that these field values are temporary and are
1300 // going to be overwritten on soinfo::prelink_image
1301 // with values from PT_LOAD segments.
1302 const ElfReader& elf_reader = task->get_elf_reader();
1303 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1304 if (d->d_tag == DT_RUNPATH) {
1305 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1306 }
1307 if (d->d_tag == DT_SONAME) {
1308 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1309 }
1310 }
1311
Dan Willemsen5038ef62018-10-21 17:45:04 -07001312#if !defined(__ANDROID__)
1313 // Bionic on the host currently uses some Android prebuilts, which don't set
1314 // DT_RUNPATH with any relative paths, so they can't find their dependencies.
1315 // b/118058804
1316 if (si->get_dt_runpath().empty()) {
1317 si->set_dt_runpath("$ORIGIN/../lib64:$ORIGIN/lib64");
1318 }
1319#endif
1320
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001321 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001322 LD_LOG(kLogDlopen, "load_library(ns=%s, task=%s): Adding DT_NEEDED task: %s",
1323 ns->get_name(), task->get_name(), name);
Jiyong Park02586a22017-05-20 01:01:24 +09001324 load_tasks->push_back(LoadTask::create(name, si, ns, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001325 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001326
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001327 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001328}
1329
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001330static bool load_library(android_namespace_t* ns,
1331 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001332 ZipArchiveCache* zip_archive_cache,
1333 LoadTaskList* load_tasks,
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001334 int rtld_flags,
1335 bool search_linked_namespaces) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001336 const char* name = task->get_name();
1337 soinfo* needed_by = task->get_needed_by();
1338 const android_dlextinfo* extinfo = task->get_extinfo();
1339
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001340 off64_t file_offset;
1341 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001342 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001343 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001344 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1345 file_offset = extinfo->library_fd_offset;
1346 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001347
1348 if (!realpath_fd(extinfo->library_fd, &realpath)) {
Tom Cherry66bc4282018-11-08 13:40:52 -08001349 if (!is_first_stage_init()) {
1350 PRINT(
1351 "warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1352 "Will use given name.",
1353 name);
1354 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001355 realpath = name;
1356 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001357
1358 task->set_fd(extinfo->library_fd, false);
1359 task->set_file_offset(file_offset);
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001360 return load_library(ns, task, load_tasks, rtld_flags, realpath, search_linked_namespaces);
Spencer Low0346ad72015-04-22 18:06:51 -07001361 }
1362
Martin Stjernholm9fe38262019-08-27 17:08:45 +01001363 LD_LOG(kLogDlopen,
1364 "load_library(ns=%s, task=%s, flags=0x%x, search_linked_namespaces=%d): calling "
1365 "open_library with realpath=%s",
1366 ns->get_name(), name, rtld_flags, search_linked_namespaces, realpath.c_str());
1367
Spencer Low0346ad72015-04-22 18:06:51 -07001368 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001369 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001370 if (fd == -1) {
Josh Gao16269572019-10-29 13:41:00 -07001371 if (task->is_dt_needed()) {
1372 if (needed_by->is_main_executable()) {
1373 DL_OPEN_ERR("library \"%s\" not found: needed by main executable", name);
1374 } else {
1375 DL_OPEN_ERR("library \"%s\" not found: needed by %s in namespace %s", name,
1376 needed_by->get_realpath(), task->get_start_from()->get_name());
1377 }
1378 } else {
1379 DL_OPEN_ERR("library \"%s\" not found", name);
1380 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001381 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001382 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001383
1384 task->set_fd(fd, true);
1385 task->set_file_offset(file_offset);
1386
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001387 return load_library(ns, task, load_tasks, rtld_flags, realpath, search_linked_namespaces);
Spencer Low0346ad72015-04-22 18:06:51 -07001388}
1389
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001390static bool find_loaded_library_by_soname(android_namespace_t* ns,
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001391 const char* name,
1392 soinfo** candidate) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001393 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001394 const char* soname = si->get_soname();
1395 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dimitry Ivanov3bd90612017-02-01 08:54:43 -08001396 *candidate = si;
1397 return false;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001398 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001399
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001400 return true;
1401 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001402}
1403
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001404// Returns true if library was found and false otherwise
1405static bool find_loaded_library_by_soname(android_namespace_t* ns,
1406 const char* name,
1407 bool search_linked_namespaces,
1408 soinfo** candidate) {
1409 *candidate = nullptr;
1410
1411 // Ignore filename with path.
1412 if (strchr(name, '/') != nullptr) {
1413 return false;
1414 }
1415
1416 bool found = find_loaded_library_by_soname(ns, name, candidate);
1417
1418 if (!found && search_linked_namespaces) {
1419 // if a library was not found - look into linked namespaces
1420 for (auto& link : ns->linked_namespaces()) {
1421 if (!link.is_accessible(name)) {
1422 continue;
1423 }
1424
1425 android_namespace_t* linked_ns = link.linked_namespace();
1426
1427 if (find_loaded_library_by_soname(linked_ns, name, candidate)) {
1428 return true;
1429 }
1430 }
1431 }
1432
1433 return found;
1434}
1435
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001436static bool find_library_in_linked_namespace(const android_namespace_link_t& namespace_link,
Jiyong Park02586a22017-05-20 01:01:24 +09001437 LoadTask* task) {
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001438 android_namespace_t* ns = namespace_link.linked_namespace();
1439
1440 soinfo* candidate;
1441 bool loaded = false;
1442
1443 std::string soname;
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001444 if (find_loaded_library_by_soname(ns, task->get_name(), false, &candidate)) {
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001445 loaded = true;
1446 soname = candidate->get_soname();
1447 } else {
1448 soname = resolve_soname(task->get_name());
1449 }
1450
1451 if (!namespace_link.is_accessible(soname.c_str())) {
1452 // the library is not accessible via namespace_link
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001453 LD_LOG(kLogDlopen,
1454 "find_library_in_linked_namespace(ns=%s, task=%s): Not accessible (soname=%s)",
1455 ns->get_name(), task->get_name(), soname.c_str());
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001456 return false;
1457 }
1458
1459 // if library is already loaded - return it
1460 if (loaded) {
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001461 LD_LOG(kLogDlopen, "find_library_in_linked_namespace(ns=%s, task=%s): Already loaded",
1462 ns->get_name(), task->get_name());
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001463 task->set_soinfo(candidate);
1464 return true;
1465 }
1466
Jiyong Park02586a22017-05-20 01:01:24 +09001467 // returning true with empty soinfo means that the library is okay to be
Logan Chien9ee45912018-01-18 12:05:09 +08001468 // loaded in the namespace but has not yet been loaded there before.
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001469 LD_LOG(kLogDlopen, "find_library_in_linked_namespace(ns=%s, task=%s): Ok to load", ns->get_name(),
1470 task->get_name());
Jiyong Park02586a22017-05-20 01:01:24 +09001471 task->set_soinfo(nullptr);
1472 return true;
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001473}
1474
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001475static bool find_library_internal(android_namespace_t* ns,
1476 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001477 ZipArchiveCache* zip_archive_cache,
1478 LoadTaskList* load_tasks,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001479 int rtld_flags,
1480 bool search_linked_namespaces) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001481 soinfo* candidate;
1482
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001483 if (find_loaded_library_by_soname(ns, task->get_name(), search_linked_namespaces, &candidate)) {
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001484 LD_LOG(kLogDlopen,
1485 "find_library_internal(ns=%s, task=%s): Already loaded (by soname): %s",
1486 ns->get_name(), task->get_name(), candidate->get_realpath());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001487 task->set_soinfo(candidate);
1488 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001489 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001490
1491 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001492 // of this fact is done by load_library.
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001493 TRACE("[ \"%s\" find_loaded_library_by_soname failed (*candidate=%s@%p). Trying harder... ]",
1494 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001495
Dimitry Ivanovd3e7d082017-03-27 14:11:02 -07001496 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags, search_linked_namespaces)) {
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001497 return true;
1498 }
1499
Jiyong Parkefc503d2019-02-12 01:08:57 +09001500 // TODO(dimitry): workaround for http://b/26394120 (the grey-list)
1501 if (ns->is_greylist_enabled() && is_greylisted(ns, task->get_name(), task->get_needed_by())) {
1502 // For the libs in the greylist, switch to the default namespace and then
1503 // try the load again from there. The library could be loaded from the
1504 // default namespace or from another namespace (e.g. runtime) that is linked
1505 // from the default namespace.
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001506 LD_LOG(kLogDlopen,
1507 "find_library_internal(ns=%s, task=%s): Greylisted library - trying namespace %s",
1508 ns->get_name(), task->get_name(), g_default_namespace.get_name());
Jiyong Parkefc503d2019-02-12 01:08:57 +09001509 ns = &g_default_namespace;
1510 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags,
1511 search_linked_namespaces)) {
1512 return true;
1513 }
1514 }
1515 // END OF WORKAROUND
1516
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001517 if (search_linked_namespaces) {
1518 // if a library was not found - look into linked namespaces
dimitry8db36a52017-10-23 15:10:10 +02001519 // preserve current dlerror in the case it fails.
1520 DlErrorRestorer dlerror_restorer;
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001521 LD_LOG(kLogDlopen, "find_library_internal(ns=%s, task=%s): Trying %zu linked namespaces",
1522 ns->get_name(), task->get_name(), ns->linked_namespaces().size());
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001523 for (auto& linked_namespace : ns->linked_namespaces()) {
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001524 if (find_library_in_linked_namespace(linked_namespace, task)) {
Jiyong Park02586a22017-05-20 01:01:24 +09001525 if (task->get_soinfo() == nullptr) {
1526 // try to load the library - once namespace boundary is crossed
1527 // we need to load a library within separate load_group
1528 // to avoid using symbols from foreign namespace while.
1529 //
1530 // However, actual linking is deferred until when the global group
1531 // is fully identified and is applied to all namespaces.
1532 // Otherwise, the libs in the linked namespace won't get symbols from
1533 // the global group.
1534 if (load_library(linked_namespace.linked_namespace(), task, zip_archive_cache, load_tasks, rtld_flags, false)) {
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001535 LD_LOG(
1536 kLogDlopen, "find_library_internal(ns=%s, task=%s): Found in linked namespace %s",
1537 ns->get_name(), task->get_name(), linked_namespace.linked_namespace()->get_name());
Jiyong Park02586a22017-05-20 01:01:24 +09001538 return true;
1539 }
Jiyong Park02586a22017-05-20 01:01:24 +09001540 } else {
1541 // lib is already loaded
1542 return true;
1543 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001544 }
1545 }
1546 }
1547
1548 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001549}
1550
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001551static void soinfo_unload(soinfo* si);
1552
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001553static void shuffle(std::vector<LoadTask*>* v) {
Tom Cherry66bc4282018-11-08 13:40:52 -08001554 if (is_first_stage_init()) {
1555 // arc4random* is not available in first stage init because /dev/random
1556 // hasn't yet been created.
Jiyong Park31cd08f2018-06-01 19:18:56 +09001557 return;
1558 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001559 for (size_t i = 0, size = v->size(); i < size; ++i) {
1560 size_t n = size - i;
1561 size_t r = arc4random_uniform(n);
1562 std::swap((*v)[n-1], (*v)[r]);
1563 }
1564}
1565
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001566// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1567// not their transitive dependencies) as children of the start_with library.
1568// This is false when find_libraries is called for dlopen(), when newly loaded
1569// libraries must form a disjoint tree.
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001570bool find_libraries(android_namespace_t* ns,
1571 soinfo* start_with,
1572 const char* const library_names[],
1573 size_t library_names_count,
1574 soinfo* soinfos[],
1575 std::vector<soinfo*>* ld_preloads,
1576 size_t ld_preloads_count,
1577 int rtld_flags,
1578 const android_dlextinfo* extinfo,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001579 bool add_as_children,
Jiyong Park02586a22017-05-20 01:01:24 +09001580 bool search_linked_namespaces,
Jiyong Park02586a22017-05-20 01:01:24 +09001581 std::vector<android_namespace_t*>* namespaces) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001582 // Step 0: prepare.
dimitry965d06d2017-11-28 16:03:07 +01001583 std::unordered_map<const soinfo*, ElfReader> readers_map;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001584 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001585
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001586 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001587 const char* name = library_names[i];
Jiyong Park02586a22017-05-20 01:01:24 +09001588 load_tasks.push_back(LoadTask::create(name, start_with, ns, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001589 }
1590
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001591 // If soinfos array is null allocate one on stack.
1592 // The array is needed in case of failure; for example
1593 // when library_names[] = {libone.so, libtwo.so} and libone.so
1594 // is loaded correctly but libtwo.so failed for some reason.
1595 // In this case libone.so should be unloaded on return.
1596 // See also implementation of failure_guard below.
1597
1598 if (soinfos == nullptr) {
1599 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1600 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1601 memset(soinfos, 0, soinfos_size);
1602 }
1603
1604 // list of libraries to link - see step 2.
1605 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001606
Tom Cherryb8ab6182017-04-05 16:20:29 -07001607 auto scope_guard = android::base::make_scope_guard([&]() {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001608 for (LoadTask* t : load_tasks) {
1609 LoadTask::deleter(t);
1610 }
1611 });
1612
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001613 ZipArchiveCache zip_archive_cache;
1614
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001615 // Step 1: expand the list of load_tasks to include
1616 // all DT_NEEDED libraries (do not load them just yet)
1617 for (size_t i = 0; i<load_tasks.size(); ++i) {
1618 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07001619 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001620
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001621 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001622 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001623 task->set_dt_needed(is_dt_needed);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001624
Martin Stjernholm95252ee2019-02-22 22:48:59 +00001625 LD_LOG(kLogDlopen, "find_libraries(ns=%s): task=%s, is_dt_needed=%d", ns->get_name(),
1626 task->get_name(), is_dt_needed);
1627
Jiyong Park02586a22017-05-20 01:01:24 +09001628 // Note: start from the namespace that is stored in the LoadTask. This namespace
1629 // is different from the current namespace when the LoadTask is for a transitive
1630 // dependency and the lib that created the LoadTask is not found in the
1631 // current namespace but in one of the linked namespace.
1632 if (!find_library_internal(const_cast<android_namespace_t*>(task->get_start_from()),
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001633 task,
1634 &zip_archive_cache,
1635 &load_tasks,
1636 rtld_flags,
1637 search_linked_namespaces || is_dt_needed)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001638 return false;
1639 }
1640
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001641 soinfo* si = task->get_soinfo();
1642
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001643 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001644 needed_by->add_child(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001645 }
1646
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001647 // When ld_preloads is not null, the first
1648 // ld_preloads_count libs are in fact ld_preloads.
1649 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001650 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001651 }
1652
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001653 if (soinfos_count < library_names_count) {
1654 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001655 }
1656 }
1657
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001658 // Step 2: Load libraries in random order (see b/24047022)
1659 LoadTaskList load_list;
1660 for (auto&& task : load_tasks) {
1661 soinfo* si = task->get_soinfo();
1662 auto pred = [&](const LoadTask* t) {
1663 return t->get_soinfo() == si;
1664 };
1665
1666 if (!si->is_linked() &&
1667 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
1668 load_list.push_back(task);
1669 }
1670 }
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -04001671 bool reserved_address_recursive = false;
1672 if (extinfo) {
1673 reserved_address_recursive = extinfo->flags & ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE;
1674 }
1675 if (!reserved_address_recursive) {
1676 // Shuffle the load order in the normal case, but not if we are loading all
1677 // the libraries to a reserved address range.
1678 shuffle(&load_list);
1679 }
1680
1681 // Set up address space parameters.
1682 address_space_params extinfo_params, default_params;
1683 size_t relro_fd_offset = 0;
1684 if (extinfo) {
1685 if (extinfo->flags & ANDROID_DLEXT_RESERVED_ADDRESS) {
1686 extinfo_params.start_addr = extinfo->reserved_addr;
1687 extinfo_params.reserved_size = extinfo->reserved_size;
1688 extinfo_params.must_use_address = true;
1689 } else if (extinfo->flags & ANDROID_DLEXT_RESERVED_ADDRESS_HINT) {
1690 extinfo_params.start_addr = extinfo->reserved_addr;
1691 extinfo_params.reserved_size = extinfo->reserved_size;
1692 }
1693 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001694
1695 for (auto&& task : load_list) {
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -04001696 address_space_params* address_space =
1697 (reserved_address_recursive || !task->is_dt_needed()) ? &extinfo_params : &default_params;
1698 if (!task->load(address_space)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001699 return false;
1700 }
1701 }
1702
1703 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
1704 for (auto&& task : load_tasks) {
1705 soinfo* si = task->get_soinfo();
1706 if (!si->is_linked() && !si->prelink_image()) {
1707 return false;
1708 }
Ryan Pricharde5e69e02019-01-01 18:53:48 -08001709 register_soinfo_tls(si);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001710 }
1711
Jiyong Park02586a22017-05-20 01:01:24 +09001712 // Step 4: Construct the global group. Note: DF_1_GLOBAL bit of a library is
1713 // determined at step 3.
1714
1715 // Step 4-1: DF_1_GLOBAL bit is force set for LD_PRELOADed libs because they
1716 // must be added to the global group
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001717 if (ld_preloads != nullptr) {
1718 for (auto&& si : *ld_preloads) {
1719 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
1720 }
1721 }
1722
Jiyong Park02586a22017-05-20 01:01:24 +09001723 // Step 4-2: Gather all DF_1_GLOBAL libs which were newly loaded during this
1724 // run. These will be the new member of the global group
1725 soinfo_list_t new_global_group_members;
1726 for (auto&& task : load_tasks) {
1727 soinfo* si = task->get_soinfo();
1728 if (!si->is_linked() && (si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1729 new_global_group_members.push_back(si);
1730 }
1731 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001732
Jiyong Park02586a22017-05-20 01:01:24 +09001733 // Step 4-3: Add the new global group members to all the linked namespaces
Jiyong Park01162f22017-10-16 15:31:09 +09001734 if (namespaces != nullptr) {
Jiyong Park02586a22017-05-20 01:01:24 +09001735 for (auto linked_ns : *namespaces) {
Jiyong Park01162f22017-10-16 15:31:09 +09001736 for (auto si : new_global_group_members) {
1737 if (si->get_primary_namespace() != linked_ns) {
1738 linked_ns->add_soinfo(si);
1739 si->add_secondary_namespace(linked_ns);
1740 }
Jiyong Park02586a22017-05-20 01:01:24 +09001741 }
1742 }
1743 }
1744
dimitry965d06d2017-11-28 16:03:07 +01001745 // Step 5: Collect roots of local_groups.
1746 // Whenever needed_by->si link crosses a namespace boundary it forms its own local_group.
1747 // Here we collect new roots to link them separately later on. Note that we need to avoid
1748 // collecting duplicates. Also the order is important. They need to be linked in the same
1749 // BFS order we link individual libraries.
1750 std::vector<soinfo*> local_group_roots;
1751 if (start_with != nullptr && add_as_children) {
1752 local_group_roots.push_back(start_with);
1753 } else {
1754 CHECK(soinfos_count == 1);
1755 local_group_roots.push_back(soinfos[0]);
1756 }
1757
Jiyong Park02586a22017-05-20 01:01:24 +09001758 for (auto&& task : load_tasks) {
1759 soinfo* si = task->get_soinfo();
dimitry965d06d2017-11-28 16:03:07 +01001760 soinfo* needed_by = task->get_needed_by();
1761 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
1762 android_namespace_t* needed_by_ns =
1763 is_dt_needed ? needed_by->get_primary_namespace() : ns;
1764
1765 if (!si->is_linked() && si->get_primary_namespace() != needed_by_ns) {
1766 auto it = std::find(local_group_roots.begin(), local_group_roots.end(), si);
1767 LD_LOG(kLogDlopen,
1768 "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",
1769 si->get_realpath(),
1770 si,
1771 si->get_primary_namespace()->get_name(),
1772 si->get_primary_namespace(),
1773 needed_by == nullptr ? "(nullptr)" : needed_by->get_realpath(),
1774 needed_by,
1775 ns->get_name(),
1776 ns,
1777 needed_by_ns->get_name(),
1778 needed_by_ns,
1779 it == local_group_roots.end() ? "yes" : "no");
1780
1781 if (it == local_group_roots.end()) {
1782 local_group_roots.push_back(si);
Jiyong Park02586a22017-05-20 01:01:24 +09001783 }
1784 }
1785 }
1786
dimitry965d06d2017-11-28 16:03:07 +01001787 // Step 6: Link all local groups
1788 for (auto root : local_group_roots) {
1789 soinfo_list_t local_group;
1790 android_namespace_t* local_group_ns = root->get_primary_namespace();
1791
1792 walk_dependencies_tree(root,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001793 [&] (soinfo* si) {
dimitry965d06d2017-11-28 16:03:07 +01001794 if (local_group_ns->is_accessible(si)) {
1795 local_group.push_back(si);
1796 return kWalkContinue;
1797 } else {
1798 return kWalkSkip;
1799 }
1800 });
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001801
dimitry965d06d2017-11-28 16:03:07 +01001802 soinfo_list_t global_group = local_group_ns->get_global_group();
Ryan Prichard339ecef2020-01-02 16:36:06 -08001803 SymbolLookupList lookup_list(global_group, local_group);
1804 soinfo* local_group_root = local_group.front();
1805
dimitry965d06d2017-11-28 16:03:07 +01001806 bool linked = local_group.visit([&](soinfo* si) {
Torne (Richard Coles)5d103742019-04-11 12:25:06 -04001807 // Even though local group may contain accessible soinfos from other namespaces
dimitry965d06d2017-11-28 16:03:07 +01001808 // we should avoid linking them (because if they are not linked -> they
1809 // are in the local_group_roots and will be linked later).
1810 if (!si->is_linked() && si->get_primary_namespace() == local_group_ns) {
Torne (Richard Coles)5d103742019-04-11 12:25:06 -04001811 const android_dlextinfo* link_extinfo = nullptr;
1812 if (si == soinfos[0] || reserved_address_recursive) {
1813 // Only forward extinfo for the first library unless the recursive
1814 // flag is set.
1815 link_extinfo = extinfo;
1816 }
Peter Collingbourne191ecdc2019-08-07 19:06:00 -07001817 if (__libc_shared_globals()->load_hook) {
1818 __libc_shared_globals()->load_hook(si->load_bias, si->phdr, si->phnum);
1819 }
Ryan Prichard339ecef2020-01-02 16:36:06 -08001820 lookup_list.set_dt_symbolic_lib(si->has_DT_SYMBOLIC ? si : nullptr);
1821 if (!si->link_image(lookup_list, local_group_root, link_extinfo, &relro_fd_offset) ||
dimitry965d06d2017-11-28 16:03:07 +01001822 !get_cfi_shadow()->AfterLoad(si, solist_get_head())) {
1823 return false;
1824 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001825 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001826
dimitry965d06d2017-11-28 16:03:07 +01001827 return true;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001828 });
Elliott Hughes27f18062017-11-29 18:47:42 +00001829
dimitry965d06d2017-11-28 16:03:07 +01001830 if (!linked) {
1831 return false;
1832 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001833 }
1834
dimitry965d06d2017-11-28 16:03:07 +01001835 // Step 7: Mark all load_tasks as linked and increment refcounts
1836 // for references between load_groups (at this point it does not matter if
1837 // referenced load_groups were loaded by previous dlopen or as part of this
1838 // one on step 6)
1839 if (start_with != nullptr && add_as_children) {
1840 start_with->set_linked();
1841 }
1842
1843 for (auto&& task : load_tasks) {
1844 soinfo* si = task->get_soinfo();
1845 si->set_linked();
1846 }
1847
1848 for (auto&& task : load_tasks) {
1849 soinfo* si = task->get_soinfo();
1850 soinfo* needed_by = task->get_needed_by();
1851 if (needed_by != nullptr &&
1852 needed_by != start_with &&
1853 needed_by->get_local_group_root() != si->get_local_group_root()) {
1854 si->increment_ref_count();
1855 }
1856 }
1857
1858
1859 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001860}
1861
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001862static soinfo* find_library(android_namespace_t* ns,
1863 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001864 const android_dlextinfo* extinfo,
1865 soinfo* needed_by) {
dimitry965d06d2017-11-28 16:03:07 +01001866 soinfo* si = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001867
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001868 if (name == nullptr) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001869 si = solist_get_somain();
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001870 } else if (!find_libraries(ns,
1871 needed_by,
1872 &name,
1873 1,
1874 &si,
1875 nullptr,
1876 0,
1877 rtld_flags,
1878 extinfo,
1879 false /* add_as_children */,
dimitry965d06d2017-11-28 16:03:07 +01001880 true /* search_linked_namespaces */)) {
1881 if (si != nullptr) {
1882 soinfo_unload(si);
1883 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001884 return nullptr;
1885 }
1886
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001887 si->increment_ref_count();
1888
Elliott Hughesd23736e2012-11-01 15:16:56 -07001889 return si;
1890}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001891
dimitry06016f22018-01-05 11:39:28 +01001892static void soinfo_unload_impl(soinfo* root) {
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07001893 ScopedTrace trace((std::string("unload ") + root->get_realpath()).c_str());
dimitry06016f22018-01-05 11:39:28 +01001894 bool is_linked = root->is_linked();
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07001895
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001896 if (!root->can_unload()) {
dimitryc92ce712017-10-27 14:12:53 +02001897 LD_LOG(kLogDlopen,
1898 "... dlclose(root=\"%s\"@%p) ... not unloading - the load group is flagged with NODELETE",
1899 root->get_realpath(),
1900 root);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001901 return;
1902 }
1903
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001904
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001905 soinfo_list_t unload_list;
dimitry965d06d2017-11-28 16:03:07 +01001906 unload_list.push_back(root);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001907
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001908 soinfo_list_t local_unload_list;
1909 soinfo_list_t external_unload_list;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001910 soinfo* si = nullptr;
1911
1912 while ((si = unload_list.pop_front()) != nullptr) {
1913 if (local_unload_list.contains(si)) {
1914 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001915 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001916
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001917 local_unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001918
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001919 if (si->has_min_version(0)) {
1920 soinfo* child = nullptr;
1921 while ((child = si->get_children().pop_front()) != nullptr) {
1922 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
1923 child->get_realpath(), child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001924
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001925 child->get_parents().remove(si);
1926
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001927 if (local_unload_list.contains(child)) {
1928 continue;
1929 } else if (child->is_linked() && child->get_local_group_root() != root) {
1930 external_unload_list.push_back(child);
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001931 } else if (child->get_parents().empty()) {
1932 unload_list.push_back(child);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001933 }
1934 }
1935 } else {
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001936 async_safe_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001937 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001938 }
1939
1940 local_unload_list.for_each([](soinfo* si) {
dimitryc92ce712017-10-27 14:12:53 +02001941 LD_LOG(kLogDlopen,
1942 "... dlclose: calling destructors for \"%s\"@%p ... ",
1943 si->get_realpath(),
1944 si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001945 si->call_destructors();
dimitryc92ce712017-10-27 14:12:53 +02001946 LD_LOG(kLogDlopen,
1947 "... dlclose: calling destructors for \"%s\"@%p ... done",
1948 si->get_realpath(),
1949 si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001950 });
1951
1952 while ((si = local_unload_list.pop_front()) != nullptr) {
dimitryc92ce712017-10-27 14:12:53 +02001953 LD_LOG(kLogDlopen,
1954 "... dlclose: unloading \"%s\"@%p ...",
1955 si->get_realpath(),
1956 si);
Ryan Pricharda2e83ab2019-08-16 17:25:43 -07001957 ++g_module_unload_counter;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001958 notify_gdb_of_unload(si);
Ryan Pricharde5e69e02019-01-01 18:53:48 -08001959 unregister_soinfo_tls(si);
Peter Collingbourne65332082019-08-05 16:16:14 -07001960 if (__libc_shared_globals()->unload_hook) {
1961 __libc_shared_globals()->unload_hook(si->load_bias, si->phdr, si->phnum);
1962 }
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001963 get_cfi_shadow()->BeforeUnload(si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001964 soinfo_free(si);
1965 }
1966
dimitry965d06d2017-11-28 16:03:07 +01001967 if (is_linked) {
1968 while ((si = external_unload_list.pop_front()) != nullptr) {
1969 LD_LOG(kLogDlopen,
1970 "... dlclose: unloading external reference \"%s\"@%p ...",
1971 si->get_realpath(),
1972 si);
1973 soinfo_unload(si);
1974 }
1975 } else {
1976 LD_LOG(kLogDlopen,
1977 "... dlclose: unload_si was not linked - not unloading external references ...");
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001978 }
1979}
1980
dimitry06016f22018-01-05 11:39:28 +01001981static void soinfo_unload(soinfo* unload_si) {
1982 // Note that the library can be loaded but not linked;
1983 // in which case there is no root but we still need
1984 // to walk the tree and unload soinfos involved.
1985 //
1986 // This happens on unsuccessful dlopen, when one of
1987 // the DT_NEEDED libraries could not be linked/found.
1988 bool is_linked = unload_si->is_linked();
1989 soinfo* root = is_linked ? unload_si->get_local_group_root() : unload_si;
1990
1991 LD_LOG(kLogDlopen,
1992 "... dlclose(realpath=\"%s\"@%p) ... load group root is \"%s\"@%p",
1993 unload_si->get_realpath(),
1994 unload_si,
1995 root->get_realpath(),
1996 root);
1997
1998
1999 size_t ref_count = is_linked ? root->decrement_ref_count() : 0;
2000 if (ref_count > 0) {
2001 LD_LOG(kLogDlopen,
2002 "... dlclose(root=\"%s\"@%p) ... not unloading - decrementing ref_count to %zd",
2003 root->get_realpath(),
2004 root,
2005 ref_count);
2006 return;
2007 }
2008
2009 soinfo_unload_impl(root);
2010}
2011
2012void increment_dso_handle_reference_counter(void* dso_handle) {
2013 if (dso_handle == nullptr) {
2014 return;
2015 }
2016
2017 auto it = g_dso_handle_counters.find(dso_handle);
2018 if (it != g_dso_handle_counters.end()) {
2019 CHECK(++it->second != 0);
2020 } else {
2021 soinfo* si = find_containing_library(dso_handle);
2022 if (si != nullptr) {
2023 ProtectedDataGuard guard;
dimitry55547db2018-05-25 14:17:37 +02002024 si->increment_ref_count();
dimitry06016f22018-01-05 11:39:28 +01002025 } else {
2026 async_safe_fatal(
2027 "increment_dso_handle_reference_counter: Couldn't find soinfo by dso_handle=%p",
2028 dso_handle);
2029 }
2030 g_dso_handle_counters[dso_handle] = 1U;
2031 }
2032}
2033
2034void decrement_dso_handle_reference_counter(void* dso_handle) {
2035 if (dso_handle == nullptr) {
2036 return;
2037 }
2038
2039 auto it = g_dso_handle_counters.find(dso_handle);
2040 CHECK(it != g_dso_handle_counters.end());
2041 CHECK(it->second != 0);
2042
2043 if (--it->second == 0) {
2044 soinfo* si = find_containing_library(dso_handle);
2045 if (si != nullptr) {
2046 ProtectedDataGuard guard;
dimitry55547db2018-05-25 14:17:37 +02002047 soinfo_unload(si);
dimitry06016f22018-01-05 11:39:28 +01002048 } else {
2049 async_safe_fatal(
2050 "decrement_dso_handle_reference_counter: Couldn't find soinfo by dso_handle=%p",
2051 dso_handle);
2052 }
2053 g_dso_handle_counters.erase(it);
2054 }
2055}
2056
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002057static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
2058 if (sym_ver == nullptr) {
2059 return sym_name;
2060 }
2061
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002062 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002063}
2064
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002065static android_namespace_t* get_caller_namespace(soinfo* caller) {
2066 return caller != nullptr ? caller->get_primary_namespace() : g_anonymous_namespace;
2067}
2068
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002069void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002070 // Use basic string manipulation calls to avoid snprintf.
2071 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
2072 // When debug malloc is enabled, this call returns 0. This in turn causes
2073 // snprintf to do nothing, which causes libraries to fail to load.
2074 // See b/17302493 for further details.
2075 // Once the above bug is fixed, this code can be modified to use
2076 // snprintf again.
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08002077 const auto& default_ld_paths = g_default_namespace.get_default_library_paths();
2078
2079 size_t required_size = 0;
2080 for (const auto& path : default_ld_paths) {
2081 required_size += path.size() + 1;
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002082 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08002083
2084 if (buffer_size < required_size) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -07002085 async_safe_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
2086 "buffer len %zu, required len %zu", buffer_size, required_size);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002087 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08002088
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002089 char* end = buffer;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08002090 for (size_t i = 0; i < default_ld_paths.size(); ++i) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002091 if (i > 0) *end++ = ':';
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08002092 end = stpcpy(end, default_ld_paths[i].c_str());
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002093 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002094}
2095
Elliott Hughescade4c32012-12-20 14:42:14 -08002096void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08002097 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08002098}
2099
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002100static std::string android_dlextinfo_to_string(const android_dlextinfo* info) {
2101 if (info == nullptr) {
2102 return "(null)";
2103 }
2104
2105 return android::base::StringPrintf("[flags=0x%" PRIx64 ","
2106 " reserved_addr=%p,"
2107 " reserved_size=0x%zx,"
2108 " relro_fd=%d,"
2109 " library_fd=%d,"
2110 " library_fd_offset=0x%" PRIx64 ","
2111 " library_namespace=%s@%p]",
2112 info->flags,
2113 info->reserved_addr,
2114 info->reserved_size,
2115 info->relro_fd,
2116 info->library_fd,
2117 info->library_fd_offset,
2118 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
2119 (info->library_namespace != nullptr ?
2120 info->library_namespace->get_name() : "(null)") : "(n/a)",
2121 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
2122 info->library_namespace : nullptr);
2123}
2124
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08002125void* do_dlopen(const char* name, int flags,
2126 const android_dlextinfo* extinfo,
2127 const void* caller_addr) {
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -07002128 std::string trace_prefix = std::string("dlopen: ") + (name == nullptr ? "(nullptr)" : name);
2129 ScopedTrace trace(trace_prefix.c_str());
2130 ScopedTrace loading_trace((trace_prefix + " - loading and linking").c_str());
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002131 soinfo* const caller = find_containing_library(caller_addr);
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002132 android_namespace_t* ns = get_caller_namespace(caller);
2133
2134 LD_LOG(kLogDlopen,
Victor Chang6cb719f2019-02-06 17:19:10 +00002135 "dlopen(name=\"%s\", flags=0x%x, extinfo=%s, caller=\"%s\", caller_ns=%s@%p, targetSdkVersion=%i) ...",
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002136 name,
2137 flags,
2138 android_dlextinfo_to_string(extinfo).c_str(),
2139 caller == nullptr ? "(null)" : caller->get_realpath(),
2140 ns == nullptr ? "(null)" : ns->get_name(),
Victor Chang6cb719f2019-02-06 17:19:10 +00002141 ns,
2142 get_application_target_sdk_version());
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002143
Vic Yangbb7e1232019-01-29 20:23:16 -08002144 auto purge_guard = android::base::make_scope_guard([&]() { purge_unused_memory(); });
2145
Tom Cherryb8ab6182017-04-05 16:20:29 -07002146 auto failure_guard = android::base::make_scope_guard(
2147 [&]() { LD_LOG(kLogDlopen, "... dlopen failed: %s", linker_get_error_buffer()); });
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002148
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002149 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Martin Stjernholm9fe38262019-08-27 17:08:45 +01002150 DL_OPEN_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002151 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08002152 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002153
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002154 if (extinfo != nullptr) {
2155 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
Martin Stjernholm9fe38262019-08-27 17:08:45 +01002156 DL_OPEN_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002157 return nullptr;
2158 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002159
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002160 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07002161 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Martin Stjernholm9fe38262019-08-27 17:08:45 +01002162 DL_OPEN_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002163 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002164 return nullptr;
2165 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002166
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002167 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
2168 if (extinfo->library_namespace == nullptr) {
Martin Stjernholm9fe38262019-08-27 17:08:45 +01002169 DL_OPEN_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002170 return nullptr;
2171 }
2172 ns = extinfo->library_namespace;
2173 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00002174 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002175
Victor Chang6cb719f2019-02-06 17:19:10 +00002176 // Workaround for dlopen(/system/lib/<soname>) when .so is in /apex. http://b/121248172
2177 // The workaround works only when targetSdkVersion < Q.
2178 std::string name_to_apex;
2179 if (translateSystemPathToApexPath(name, &name_to_apex)) {
2180 const char* new_name = name_to_apex.c_str();
2181 LD_LOG(kLogDlopen, "dlopen considering translation from %s to APEX path %s",
2182 name,
2183 new_name);
2184 // Some APEXs could be optionally disabled. Only translate the path
2185 // when the old file is absent and the new file exists.
Victor Changaf12c942019-02-11 19:35:24 +00002186 // TODO(b/124218500): Re-enable it once app compat issue is resolved
2187 /*
Victor Chang6cb719f2019-02-06 17:19:10 +00002188 if (file_exists(name)) {
2189 LD_LOG(kLogDlopen, "dlopen %s exists, not translating", name);
Victor Changaf12c942019-02-11 19:35:24 +00002190 } else
2191 */
2192 if (!file_exists(new_name)) {
Victor Chang6cb719f2019-02-06 17:19:10 +00002193 LD_LOG(kLogDlopen, "dlopen %s does not exist, not translating",
2194 new_name);
2195 } else {
2196 LD_LOG(kLogDlopen, "dlopen translation accepted: using %s", new_name);
2197 name = new_name;
2198 }
2199 }
2200 // End Workaround for dlopen(/system/lib/<soname>) when .so is in /apex.
2201
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07002202 std::string asan_name_holder;
2203
2204 const char* translated_name = name;
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08002205 if (g_is_asan && translated_name != nullptr && translated_name[0] == '/') {
Evgenii Stepanov9e77a642017-07-27 14:55:44 -07002206 char original_path[PATH_MAX];
2207 if (realpath(name, original_path) != nullptr) {
2208 asan_name_holder = std::string(kAsanLibDirPrefix) + original_path;
Vishwath Mohan4113def2017-03-29 15:31:34 -07002209 if (file_exists(asan_name_holder.c_str())) {
Evgenii Stepanov9e77a642017-07-27 14:55:44 -07002210 soinfo* si = nullptr;
2211 if (find_loaded_library_by_realpath(ns, original_path, true, &si)) {
2212 PRINT("linker_asan dlopen NOT translating \"%s\" -> \"%s\": library already loaded", name,
2213 asan_name_holder.c_str());
2214 } else {
2215 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
2216 translated_name = asan_name_holder.c_str();
2217 }
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07002218 }
2219 }
2220 }
2221
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002222 ProtectedDataGuard guard;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07002223 soinfo* si = find_library(ns, translated_name, flags, extinfo, caller);
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -07002224 loading_trace.End();
2225
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002226 if (si != nullptr) {
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002227 void* handle = si->to_handle();
2228 LD_LOG(kLogDlopen,
Dimitry Ivanovae4a0c12016-11-21 10:44:35 -08002229 "... dlopen calling constructors: realpath=\"%s\", soname=\"%s\", handle=%p",
2230 si->get_realpath(), si->get_soname(), handle);
2231 si->call_constructors();
Tom Cherryb8ab6182017-04-05 16:20:29 -07002232 failure_guard.Disable();
Dimitry Ivanovae4a0c12016-11-21 10:44:35 -08002233 LD_LOG(kLogDlopen,
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002234 "... dlopen successful: realpath=\"%s\", soname=\"%s\", handle=%p",
2235 si->get_realpath(), si->get_soname(), handle);
2236 return handle;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002237 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002238
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002239 return nullptr;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002240}
2241
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002242int do_dladdr(const void* addr, Dl_info* info) {
2243 // Determine if this address can be found in any library currently mapped.
2244 soinfo* si = find_containing_library(addr);
2245 if (si == nullptr) {
2246 return 0;
2247 }
2248
2249 memset(info, 0, sizeof(Dl_info));
2250
2251 info->dli_fname = si->get_realpath();
2252 // Address at which the shared object is loaded.
2253 info->dli_fbase = reinterpret_cast<void*>(si->base);
2254
2255 // Determine if any symbol in the library contains the specified address.
2256 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
2257 if (sym != nullptr) {
2258 info->dli_sname = si->get_string(sym->st_name);
2259 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
2260 }
2261
2262 return 1;
2263}
2264
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002265static soinfo* soinfo_from_handle(void* handle) {
2266 if ((reinterpret_cast<uintptr_t>(handle) & 1) != 0) {
2267 auto it = g_soinfo_handles_map.find(reinterpret_cast<uintptr_t>(handle));
2268 if (it == g_soinfo_handles_map.end()) {
2269 return nullptr;
2270 } else {
2271 return it->second;
2272 }
2273 }
2274
2275 return static_cast<soinfo*>(handle);
2276}
2277
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08002278bool do_dlsym(void* handle,
2279 const char* sym_name,
2280 const char* sym_ver,
2281 const void* caller_addr,
2282 void** symbol) {
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07002283 ScopedTrace trace("dlsym");
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002284#if !defined(__LP64__)
2285 if (handle == nullptr) {
Martin Stjernholm9fe38262019-08-27 17:08:45 +01002286 DL_SYM_ERR("dlsym failed: library handle is null");
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002287 return false;
2288 }
2289#endif
2290
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002291 soinfo* found = nullptr;
2292 const ElfW(Sym)* sym = nullptr;
2293 soinfo* caller = find_containing_library(caller_addr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002294 android_namespace_t* ns = get_caller_namespace(caller);
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002295 soinfo* si = nullptr;
2296 if (handle != RTLD_DEFAULT && handle != RTLD_NEXT) {
2297 si = soinfo_from_handle(handle);
2298 }
2299
2300 LD_LOG(kLogDlsym,
2301 "dlsym(handle=%p(\"%s\"), sym_name=\"%s\", sym_ver=\"%s\", caller=\"%s\", caller_ns=%s@%p) ...",
2302 handle,
2303 si != nullptr ? si->get_realpath() : "n/a",
2304 sym_name,
2305 sym_ver,
2306 caller == nullptr ? "(null)" : caller->get_realpath(),
2307 ns == nullptr ? "(null)" : ns->get_name(),
2308 ns);
2309
Tom Cherryb8ab6182017-04-05 16:20:29 -07002310 auto failure_guard = android::base::make_scope_guard(
2311 [&]() { LD_LOG(kLogDlsym, "... dlsym failed: %s", linker_get_error_buffer()); });
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002312
2313 if (sym_name == nullptr) {
Martin Stjernholm9fe38262019-08-27 17:08:45 +01002314 DL_SYM_ERR("dlsym failed: symbol name is null");
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002315 return false;
2316 }
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002317
2318 version_info vi_instance;
2319 version_info* vi = nullptr;
2320
2321 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002322 vi_instance.name = sym_ver;
2323 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002324 vi = &vi_instance;
2325 }
2326
2327 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2328 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2329 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002330 if (si == nullptr) {
Martin Stjernholm9fe38262019-08-27 17:08:45 +01002331 DL_SYM_ERR("dlsym failed: invalid handle: %p", handle);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002332 return false;
2333 }
2334 sym = dlsym_handle_lookup(si, &found, sym_name, vi);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002335 }
2336
2337 if (sym != nullptr) {
2338 uint32_t bind = ELF_ST_BIND(sym->st_info);
Ryan Pricharde4d620b2019-04-01 17:42:14 -07002339 uint32_t type = ELF_ST_TYPE(sym->st_info);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002340
2341 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
Ryan Pricharde4d620b2019-04-01 17:42:14 -07002342 if (type == STT_TLS) {
2343 // For a TLS symbol, dlsym returns the address of the current thread's
Ryan Pricharda2e83ab2019-08-16 17:25:43 -07002344 // copy of the symbol.
2345 const soinfo_tls* tls_module = found->get_tls();
Ryan Pricharde4d620b2019-04-01 17:42:14 -07002346 if (tls_module == nullptr) {
Martin Stjernholm9fe38262019-08-27 17:08:45 +01002347 DL_SYM_ERR("TLS symbol \"%s\" in solib \"%s\" with no TLS segment",
2348 sym_name, found->get_realpath());
Ryan Pricharde4d620b2019-04-01 17:42:14 -07002349 return false;
2350 }
Ryan Pricharda2e83ab2019-08-16 17:25:43 -07002351 void* tls_block = get_tls_block_for_this_thread(tls_module, /*should_alloc=*/true);
2352 *symbol = static_cast<char*>(tls_block) + sym->st_value;
Ryan Pricharde4d620b2019-04-01 17:42:14 -07002353 } else {
2354 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
2355 }
Tom Cherryb8ab6182017-04-05 16:20:29 -07002356 failure_guard.Disable();
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002357 LD_LOG(kLogDlsym,
2358 "... dlsym successful: sym_name=\"%s\", sym_ver=\"%s\", found in=\"%s\", address=%p",
2359 sym_name, sym_ver, found->get_soname(), *symbol);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002360 return true;
2361 }
2362
Martin Stjernholm9fe38262019-08-27 17:08:45 +01002363 DL_SYM_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002364 return false;
2365 }
2366
Martin Stjernholm9fe38262019-08-27 17:08:45 +01002367 DL_SYM_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002368 return false;
2369}
2370
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002371int do_dlclose(void* handle) {
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07002372 ScopedTrace trace("dlclose");
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002373 ProtectedDataGuard guard;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002374 soinfo* si = soinfo_from_handle(handle);
2375 if (si == nullptr) {
Martin Stjernholm9fe38262019-08-27 17:08:45 +01002376 DL_OPEN_ERR("invalid handle: %p", handle);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002377 return -1;
2378 }
2379
dimitryc92ce712017-10-27 14:12:53 +02002380 LD_LOG(kLogDlopen,
2381 "dlclose(handle=%p, realpath=\"%s\"@%p) ...",
2382 handle,
2383 si->get_realpath(),
2384 si);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002385 soinfo_unload(si);
dimitryc92ce712017-10-27 14:12:53 +02002386 LD_LOG(kLogDlopen,
2387 "dlclose(handle=%p) ... done",
2388 handle);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002389 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002390}
2391
Jiyong Park25bedfd2019-05-16 21:00:39 +09002392// Make ns as the anonymous namespace that is a namespace used when
2393// we fail to determine the caller address (e.g., call from mono-jited code)
2394// Since there can be multiple anonymous namespace in a process, subsequent
2395// call to this function causes an error.
2396static bool set_anonymous_namespace(android_namespace_t* ns) {
2397 if (!g_anonymous_namespace_set && ns != nullptr) {
2398 CHECK(ns->is_also_used_as_anonymous());
2399 g_anonymous_namespace = ns;
2400 g_anonymous_namespace_set = true;
2401 return true;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002402 }
Jiyong Park25bedfd2019-05-16 21:00:39 +09002403 return false;
2404}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002405
Jiyong Park25bedfd2019-05-16 21:00:39 +09002406// TODO(b/130388701) remove this. Currently, this is used only for testing
2407// where we don't have classloader namespace.
2408bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002409 ProtectedDataGuard guard;
2410
Jiyong Park25bedfd2019-05-16 21:00:39 +09002411 // Test-only feature: we need to change the anonymous namespace multiple times
2412 // while the test is running.
2413 g_anonymous_namespace_set = false;
2414
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002415 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002416 // When the caller is nullptr - create_namespace will take global group
2417 // from the anonymous namespace, which is fine because anonymous namespace
2418 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002419 android_namespace_t* anon_ns =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002420 create_namespace(nullptr,
2421 "(anonymous)",
2422 nullptr,
2423 library_search_path,
Jiyong Park25bedfd2019-05-16 21:00:39 +09002424 ANDROID_NAMESPACE_TYPE_ISOLATED |
2425 ANDROID_NAMESPACE_TYPE_ALSO_USED_AS_ANONYMOUS,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002426 nullptr,
2427 &g_default_namespace);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002428
Jiyong Park25bedfd2019-05-16 21:00:39 +09002429 CHECK(anon_ns != nullptr);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002430
2431 if (!link_namespaces(anon_ns, &g_default_namespace, shared_lib_sonames)) {
Jiyong Park25bedfd2019-05-16 21:00:39 +09002432 // TODO: delete anon_ns
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002433 return false;
2434 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08002435
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002436 return true;
2437}
2438
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002439static void add_soinfos_to_namespace(const soinfo_list_t& soinfos, android_namespace_t* ns) {
2440 ns->add_soinfos(soinfos);
2441 for (auto si : soinfos) {
2442 si->add_secondary_namespace(ns);
2443 }
2444}
2445
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002446android_namespace_t* create_namespace(const void* caller_addr,
2447 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002448 const char* ld_library_path,
2449 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002450 uint64_t type,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002451 const char* permitted_when_isolated_path,
2452 android_namespace_t* parent_namespace) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002453 if (parent_namespace == nullptr) {
Dimitry Ivanov52408632016-05-23 10:31:11 -07002454 // if parent_namespace is nullptr -> set it to the caller namespace
2455 soinfo* caller_soinfo = find_containing_library(caller_addr);
2456
2457 parent_namespace = caller_soinfo != nullptr ?
2458 caller_soinfo->get_primary_namespace() :
2459 g_anonymous_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002460 }
2461
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002462 ProtectedDataGuard guard;
2463 std::vector<std::string> ld_library_paths;
2464 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002465 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002466
2467 parse_path(ld_library_path, ":", &ld_library_paths);
2468 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002469 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002470
2471 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2472 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002473 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Jiyong Park37b91af2017-05-05 22:07:05 +09002474 ns->set_greylist_enabled((type & ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED) != 0);
Jiyong Park25bedfd2019-05-16 21:00:39 +09002475 ns->set_also_used_as_anonymous((type & ANDROID_NAMESPACE_TYPE_ALSO_USED_AS_ANONYMOUS) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002476
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002477 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07002478 // append parent namespace paths.
2479 std::copy(parent_namespace->get_ld_library_paths().begin(),
2480 parent_namespace->get_ld_library_paths().end(),
2481 back_inserter(ld_library_paths));
2482
2483 std::copy(parent_namespace->get_default_library_paths().begin(),
2484 parent_namespace->get_default_library_paths().end(),
2485 back_inserter(default_library_paths));
2486
2487 std::copy(parent_namespace->get_permitted_paths().begin(),
2488 parent_namespace->get_permitted_paths().end(),
2489 back_inserter(permitted_paths));
2490
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002491 // If shared - clone the parent namespace
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002492 add_soinfos_to_namespace(parent_namespace->soinfo_list(), ns);
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07002493 // and copy parent namespace links
2494 for (auto& link : parent_namespace->linked_namespaces()) {
Logan Chien9ee45912018-01-18 12:05:09 +08002495 ns->add_linked_namespace(link.linked_namespace(), link.shared_lib_sonames(),
2496 link.allow_all_shared_libs());
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07002497 }
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002498 } else {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002499 // If not shared - copy only the shared group
Jiyong Park02586a22017-05-20 01:01:24 +09002500 add_soinfos_to_namespace(parent_namespace->get_shared_group(), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002501 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002502
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -07002503 ns->set_ld_library_paths(std::move(ld_library_paths));
2504 ns->set_default_library_paths(std::move(default_library_paths));
2505 ns->set_permitted_paths(std::move(permitted_paths));
2506
Jiyong Park25bedfd2019-05-16 21:00:39 +09002507 if (ns->is_also_used_as_anonymous() && !set_anonymous_namespace(ns)) {
2508 DL_ERR("failed to set namespace: [name=\"%s\", ld_library_path=\"%s\", default_library_paths=\"%s\""
2509 " permitted_paths=\"%s\"] as the anonymous namespace",
2510 ns->get_name(),
2511 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
2512 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
2513 android::base::Join(ns->get_permitted_paths(), ':').c_str());
2514 return nullptr;
2515 }
2516
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002517 return ns;
2518}
2519
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002520bool link_namespaces(android_namespace_t* namespace_from,
2521 android_namespace_t* namespace_to,
2522 const char* shared_lib_sonames) {
2523 if (namespace_to == nullptr) {
2524 namespace_to = &g_default_namespace;
2525 }
2526
2527 if (namespace_from == nullptr) {
2528 DL_ERR("error linking namespaces: namespace_from is null.");
2529 return false;
2530 }
2531
2532 if (shared_lib_sonames == nullptr || shared_lib_sonames[0] == '\0') {
2533 DL_ERR("error linking namespaces \"%s\"->\"%s\": the list of shared libraries is empty.",
2534 namespace_from->get_name(), namespace_to->get_name());
2535 return false;
2536 }
2537
2538 auto sonames = android::base::Split(shared_lib_sonames, ":");
2539 std::unordered_set<std::string> sonames_set(sonames.begin(), sonames.end());
2540
2541 ProtectedDataGuard guard;
Logan Chien9ee45912018-01-18 12:05:09 +08002542 namespace_from->add_linked_namespace(namespace_to, sonames_set, false);
2543
2544 return true;
2545}
2546
2547bool link_namespaces_all_libs(android_namespace_t* namespace_from,
2548 android_namespace_t* namespace_to) {
2549 if (namespace_from == nullptr) {
2550 DL_ERR("error linking namespaces: namespace_from is null.");
2551 return false;
2552 }
2553
2554 if (namespace_to == nullptr) {
2555 DL_ERR("error linking namespaces: namespace_to is null.");
2556 return false;
2557 }
2558
2559 ProtectedDataGuard guard;
2560 namespace_from->add_linked_namespace(namespace_to, std::unordered_set<std::string>(), true);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002561
2562 return true;
2563}
2564
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002565ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
Elliott Hughes90f96b92019-05-09 15:56:39 -07002566 if (g_is_ldd) return 0;
2567
Peter Collingbournee9491952019-10-28 10:57:26 -07002568 ElfW(Addr) ifunc_addr = __bionic_call_ifunc_resolver(resolver_addr);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002569 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
Peter Collingbournee9491952019-10-28 10:57:26 -07002570 reinterpret_cast<void *>(resolver_addr), reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002571
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002572 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002573}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002574
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002575const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2576 if (source_symver < 2 ||
2577 source_symver >= version_infos.size() ||
2578 version_infos[source_symver].name == nullptr) {
2579 return nullptr;
2580 }
2581
2582 return &version_infos[source_symver];
2583}
2584
2585void VersionTracker::add_version_info(size_t source_index,
2586 ElfW(Word) elf_hash,
2587 const char* ver_name,
2588 const soinfo* target_si) {
2589 if (source_index >= version_infos.size()) {
2590 version_infos.resize(source_index+1);
2591 }
2592
2593 version_infos[source_index].elf_hash = elf_hash;
2594 version_infos[source_index].name = ver_name;
2595 version_infos[source_index].target_si = target_si;
2596}
2597
2598bool VersionTracker::init_verneed(const soinfo* si_from) {
2599 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2600
2601 if (verneed_ptr == 0) {
2602 return true;
2603 }
2604
2605 size_t verneed_cnt = si_from->get_verneed_cnt();
2606
2607 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2608 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2609 size_t vernaux_offset = offset + verneed->vn_aux;
2610 offset += verneed->vn_next;
2611
2612 if (verneed->vn_version != 1) {
2613 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2614 return false;
2615 }
2616
2617 const char* target_soname = si_from->get_string(verneed->vn_file);
2618 // find it in dependencies
2619 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002620 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002621 });
2622
2623 if (target_si == nullptr) {
2624 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002625 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002626 return false;
2627 }
2628
2629 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2630 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2631 vernaux_offset += vernaux->vna_next;
2632
2633 const ElfW(Word) elf_hash = vernaux->vna_hash;
2634 const char* ver_name = si_from->get_string(vernaux->vna_name);
2635 ElfW(Half) source_index = vernaux->vna_other;
2636
2637 add_version_info(source_index, elf_hash, ver_name, target_si);
2638 }
2639 }
2640
2641 return true;
2642}
2643
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002644template <typename F>
2645static bool for_each_verdef(const soinfo* si, F functor) {
2646 if (!si->has_min_version(2)) {
2647 return true;
2648 }
2649
2650 uintptr_t verdef_ptr = si->get_verdef_ptr();
2651 if (verdef_ptr == 0) {
2652 return true;
2653 }
2654
2655 size_t offset = 0;
2656
2657 size_t verdef_cnt = si->get_verdef_cnt();
2658 for (size_t i = 0; i<verdef_cnt; ++i) {
2659 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
2660 size_t verdaux_offset = offset + verdef->vd_aux;
2661 offset += verdef->vd_next;
2662
2663 if (verdef->vd_version != 1) {
2664 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
2665 i, verdef->vd_version, si->get_realpath());
2666 return false;
2667 }
2668
2669 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
2670 // "this is the version of the file itself. It must not be used for
2671 // matching a symbol. It can be used to match references."
2672 //
2673 // http://www.akkadia.org/drepper/symbol-versioning
2674 continue;
2675 }
2676
2677 if (verdef->vd_cnt == 0) {
2678 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
2679 return false;
2680 }
2681
2682 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
2683
2684 if (functor(i, verdef, verdaux) == true) {
2685 break;
2686 }
2687 }
2688
2689 return true;
2690}
2691
Ryan Prichard0e12cce2020-01-02 14:59:11 -08002692ElfW(Versym) find_verdef_version_index(const soinfo* si, const version_info* vi) {
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002693 if (vi == nullptr) {
Ryan Prichard0e12cce2020-01-02 14:59:11 -08002694 return kVersymNotNeeded;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002695 }
2696
Ryan Prichard0e12cce2020-01-02 14:59:11 -08002697 ElfW(Versym) result = kVersymGlobal;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002698
Ryan Prichard0e12cce2020-01-02 14:59:11 -08002699 if (!for_each_verdef(si,
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002700 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2701 if (verdef->vd_hash == vi->elf_hash &&
2702 strcmp(vi->name, si->get_string(verdaux->vda_name)) == 0) {
Ryan Prichard0e12cce2020-01-02 14:59:11 -08002703 result = verdef->vd_ndx;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002704 return true;
2705 }
2706
2707 return false;
2708 }
Ryan Prichard0e12cce2020-01-02 14:59:11 -08002709 )) {
2710 // verdef should have already been validated in prelink_image.
2711 async_safe_fatal("invalid verdef after prelinking: %s, %s",
2712 si->get_realpath(), linker_get_error_buffer());
2713 }
2714
2715 return result;
2716}
2717
2718// Validate the library's verdef section. On error, returns false and invokes DL_ERR.
2719bool validate_verdef_section(const soinfo* si) {
2720 return for_each_verdef(si,
2721 [&](size_t, const ElfW(Verdef)*, const ElfW(Verdaux)*) {
2722 return false;
2723 });
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002724}
2725
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002726bool VersionTracker::init_verdef(const soinfo* si_from) {
2727 return for_each_verdef(si_from,
2728 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2729 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2730 si_from->get_string(verdaux->vda_name), si_from);
2731 return false;
2732 }
2733 );
2734}
2735
2736bool VersionTracker::init(const soinfo* si_from) {
2737 if (!si_from->has_min_version(2)) {
2738 return true;
2739 }
2740
2741 return init_verneed(si_from) && init_verdef(si_from);
2742}
2743
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002744// TODO (dimitry): Methods below need to be moved out of soinfo
2745// and in more isolated file in order minimize dependencies on
2746// unnecessary object in the linker binary. Consider making them
2747// independent from soinfo (?).
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002748bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2749 const char* sym_name, const version_info** vi) {
2750 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2751 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2752
2753 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2754 *vi = version_tracker.get_version_info(sym_ver);
2755
2756 if (*vi == nullptr) {
2757 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002758 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002759 return false;
2760 }
2761 } else {
2762 // there is no version info
2763 *vi = nullptr;
2764 }
2765
2766 return true;
2767}
2768
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002769void soinfo::apply_relr_reloc(ElfW(Addr) offset) {
2770 ElfW(Addr) address = offset + load_bias;
2771 *reinterpret_cast<ElfW(Addr)*>(address) += load_bias;
2772}
2773
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002774// Process relocations in SHT_RELR section (experimental).
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002775// Details of the encoding are described in this post:
2776// https://groups.google.com/d/msg/generic-abi/bX460iggiKg/Pi9aSwwABgAJ
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002777bool soinfo::relocate_relr() {
2778 ElfW(Relr)* begin = relr_;
2779 ElfW(Relr)* end = relr_ + relr_count_;
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002780 constexpr size_t wordsize = sizeof(ElfW(Addr));
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002781
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002782 ElfW(Addr) base = 0;
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002783 for (ElfW(Relr)* current = begin; current < end; ++current) {
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002784 ElfW(Relr) entry = *current;
2785 ElfW(Addr) offset;
2786
2787 if ((entry&1) == 0) {
2788 // Even entry: encodes the offset for next relocation.
2789 offset = static_cast<ElfW(Addr)>(entry);
2790 apply_relr_reloc(offset);
2791 // Set base offset for subsequent bitmap entries.
2792 base = offset + wordsize;
2793 continue;
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002794 }
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002795
2796 // Odd entry: encodes bitmap for relocations starting at base.
2797 offset = base;
2798 while (entry != 0) {
2799 entry >>= 1;
2800 if ((entry&1) != 0) {
2801 apply_relr_reloc(offset);
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002802 }
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002803 offset += wordsize;
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002804 }
Rahul Chaudhryf16b6592018-01-25 15:34:15 -08002805
2806 // Advance base offset by 63 words for 64-bit platforms,
2807 // or 31 words for 32-bit platforms.
2808 base += (8*wordsize - 1) * wordsize;
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002809 }
2810 return true;
2811}
2812
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002813// An empty list of soinfos
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002814static soinfo_list_t g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002815
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002816bool soinfo::prelink_image() {
Ryan Prichardae320cd2019-12-10 12:39:05 -08002817 if (flags_ & FLAG_PRELINKED) return true;
Ningsheng Jiane93be992014-09-16 15:22:10 +08002818 /* Extract dynamic section */
2819 ElfW(Word) dynamic_flags = 0;
2820 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002821
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002822 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002823 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002824 if (!relocating_linker) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002825 INFO("[ Linking \"%s\" ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002826 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002827 }
2828
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002829 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002830 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002831 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002832 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002833 return false;
2834 } else {
2835 if (!relocating_linker) {
2836 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002837 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002838 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002839
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002840#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002841 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2842 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002843#endif
2844
Ryan Pricharde5e69e02019-01-01 18:53:48 -08002845 TlsSegment tls_segment;
Ryan Prichard19883502019-01-16 23:13:38 -08002846 if (__bionic_get_tls_segment(phdr, phnum, load_bias, &tls_segment)) {
2847 if (!__bionic_check_tls_alignment(&tls_segment.alignment)) {
2848 if (!relocating_linker) {
2849 DL_ERR("TLS segment alignment in \"%s\" is not a power of 2: %zu",
2850 get_realpath(), tls_segment.alignment);
2851 }
2852 return false;
2853 }
Ryan Pricharde5e69e02019-01-01 18:53:48 -08002854 tls_ = std::make_unique<soinfo_tls>();
2855 tls_->segment = tls_segment;
2856 }
2857
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002858 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002859 // Note that: "Except for the DT_NULL element at the end of the array,
2860 // and the relative order of DT_NEEDED elements, entries may appear in any order."
2861 //
2862 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002863 uint32_t needed_count = 0;
2864 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2865 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2866 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2867 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002868 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002869 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002870 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002871
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002872 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002873 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2874 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2875 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2876 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002877 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002878
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002879 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002880 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002881 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002882 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2883 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002884
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002885 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002886 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002887 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002888 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
2889 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002890
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002891 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002892 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002893 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002894 return false;
2895 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002896 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002897
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002898 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002899 break;
2900
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002901 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002902 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002903 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002904
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002905 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002906 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002907 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002908
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002909 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002910 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002911 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002912
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002913 case DT_SYMENT:
2914 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002915 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
2916 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002917 return false;
2918 }
2919 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002920
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002921 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002922#if defined(USE_RELA)
2923 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002924 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002925 return false;
2926 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002927#else
2928 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002929 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002930 return false;
2931 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002932#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002933 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002934
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002935 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002936#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002937 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002938#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002939 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002940#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002941 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002942
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002943 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002944#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002945 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002946#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002947 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002948#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002949 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002950
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002951 case DT_PLTGOT:
Elliott Hughes14798932020-02-13 15:40:07 -08002952 // Ignored (because RTLD_LAZY is not supported).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002953 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002954
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002955 case DT_DEBUG:
2956 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2957 // if the dynamic table is writable
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002958 if ((dynamic_flags & PF_W) != 0) {
2959 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2960 }
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002961 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002962#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002963 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002964 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002965 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002966
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002967 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002968 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002969 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002970
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002971 case DT_ANDROID_RELA:
2972 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2973 break;
2974
2975 case DT_ANDROID_RELASZ:
2976 android_relocs_size_ = d->d_un.d_val;
2977 break;
2978
2979 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002980 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002981 return false;
2982
2983 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002984 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002985 return false;
2986
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002987 case DT_RELAENT:
2988 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002989 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002990 return false;
2991 }
2992 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002993
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08002994 // Ignored (see DT_RELCOUNT comments for details).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002995 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002996 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002997
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002998 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002999 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003000 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003001
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003002 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003003 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003004 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003005
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003006#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003007 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003008 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003009 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003010
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003011 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003012 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003013 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003014
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003015 case DT_RELENT:
3016 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003017 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003018 return false;
3019 }
3020 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003021
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003022 case DT_ANDROID_REL:
3023 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3024 break;
3025
3026 case DT_ANDROID_RELSZ:
3027 android_relocs_size_ = d->d_un.d_val;
3028 break;
3029
3030 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003031 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003032 return false;
3033
3034 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003035 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003036 return false;
3037
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003038 // "Indicates that all RELATIVE relocations have been concatenated together,
3039 // and specifies the RELATIVE relocation count."
3040 //
3041 // TODO: Spec also mentions that this can be used to optimize relocation process;
3042 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003043 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003044 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003045
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003046 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003047 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003048 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003049
3050 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003051 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003052 return false;
3053
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003054#endif
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08003055 case DT_RELR:
Elliott Hughes6663f552020-01-24 14:36:10 -08003056 case DT_ANDROID_RELR:
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08003057 relr_ = reinterpret_cast<ElfW(Relr)*>(load_bias + d->d_un.d_ptr);
3058 break;
3059
3060 case DT_RELRSZ:
Elliott Hughes6663f552020-01-24 14:36:10 -08003061 case DT_ANDROID_RELRSZ:
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08003062 relr_count_ = d->d_un.d_val / sizeof(ElfW(Relr));
3063 break;
3064
3065 case DT_RELRENT:
Elliott Hughes6663f552020-01-24 14:36:10 -08003066 case DT_ANDROID_RELRENT:
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08003067 if (d->d_un.d_val != sizeof(ElfW(Relr))) {
3068 DL_ERR("invalid DT_RELRENT: %zd", static_cast<size_t>(d->d_un.d_val));
3069 return false;
3070 }
3071 break;
3072
3073 // Ignored (see DT_RELCOUNT comments for details).
Elliott Hughes6663f552020-01-24 14:36:10 -08003074 // There is no DT_RELRCOUNT specifically because it would only be ignored.
3075 case DT_ANDROID_RELRCOUNT:
Rahul Chaudhryb7feec72017-12-19 15:25:23 -08003076 break;
3077
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003078 case DT_INIT:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003079 init_func_ = reinterpret_cast<linker_ctor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003080 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003081 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003082
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003083 case DT_FINI:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003084 fini_func_ = reinterpret_cast<linker_dtor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003085 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003086 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003087
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003088 case DT_INIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003089 init_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003090 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003091 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003092
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003093 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003094 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003095 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003096
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003097 case DT_FINI_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003098 fini_array_ = reinterpret_cast<linker_dtor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003099 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003100 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003101
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003102 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003103 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003104 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003105
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003106 case DT_PREINIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003107 preinit_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003108 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003109 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003110
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003111 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003112 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003113 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003114
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003115 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003116#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003117 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003118 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003119#else
3120 has_text_relocations = true;
3121 break;
3122#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003123
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003124 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003125 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003126 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003127
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003128 case DT_NEEDED:
3129 ++needed_count;
3130 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003131
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003132 case DT_FLAGS:
3133 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003134#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003135 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003136 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003137#else
3138 has_text_relocations = true;
3139#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003140 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003141 if (d->d_un.d_val & DF_SYMBOLIC) {
3142 has_DT_SYMBOLIC = true;
3143 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003144 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003145
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003146 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003147 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003148
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003149 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003150 DL_WARN("Warning: \"%s\" has unsupported flags DT_FLAGS_1=%p "
3151 "(ignoring unsupported flags)",
3152 get_realpath(), reinterpret_cast<void*>(d->d_un.d_val));
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003153 }
3154 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003155
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003156 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3157 case DT_BIND_NOW:
3158 break;
3159
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003160 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003161 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3162 break;
3163
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003164 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003165 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3166 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003167 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003168 verdef_cnt_ = d->d_un.d_val;
3169 break;
3170
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003171 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003172 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3173 break;
3174
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003175 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003176 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003177 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003178
Evgenii Stepanov68650822015-06-10 13:38:39 -07003179 case DT_RUNPATH:
3180 // this is parsed after we have strtab initialized (see below).
3181 break;
3182
Ryan Prichardea41ab02019-01-23 16:33:03 -08003183 case DT_TLSDESC_GOT:
3184 case DT_TLSDESC_PLT:
3185 // These DT entries are used for lazy TLSDESC relocations. Bionic
3186 // resolves everything eagerly, so these can be ignored.
3187 break;
3188
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003189 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003190 if (!relocating_linker) {
Elliott Hughes6eae4cc2017-08-30 09:02:33 -07003191 const char* tag_name;
3192 if (d->d_tag == DT_RPATH) {
3193 tag_name = "DT_RPATH";
3194 } else if (d->d_tag == DT_ENCODING) {
3195 tag_name = "DT_ENCODING";
3196 } else if (d->d_tag >= DT_LOOS && d->d_tag <= DT_HIOS) {
3197 tag_name = "unknown OS-specific";
3198 } else if (d->d_tag >= DT_LOPROC && d->d_tag <= DT_HIPROC) {
3199 tag_name = "unknown processor-specific";
3200 } else {
3201 tag_name = "unknown";
3202 }
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003203 DL_WARN("Warning: \"%s\" unused DT entry: %s (type %p arg %p) (ignoring)",
Elliott Hughes6eae4cc2017-08-30 09:02:33 -07003204 get_realpath(),
3205 tag_name,
3206 reinterpret_cast<void*>(d->d_tag),
3207 reinterpret_cast<void*>(d->d_un.d_val));
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003208 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003209 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003210 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003211 }
3212
3213 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003214 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003215
3216 // Sanity checks.
3217 if (relocating_linker && needed_count != 0) {
3218 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3219 return false;
3220 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003221 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003222 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003223 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003224 return false;
3225 }
Yi Kong32bc0fc2018-08-02 17:31:13 -07003226 if (strtab_ == nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003227 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003228 return false;
3229 }
Yi Kong32bc0fc2018-08-02 17:31:13 -07003230 if (symtab_ == nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003231 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003232 return false;
3233 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003234
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003235 // second pass - parse entries relying on strtab
3236 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003237 switch (d->d_tag) {
3238 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003239 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003240 break;
3241 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003242 set_dt_runpath(get_string(d->d_un.d_val));
3243 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003244 }
3245 }
3246
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003247 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003248 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003249 // because they can't find dt_needed library by soname.
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003250 // This workaround should keep them working. (Applies only
3251 // for apps targeting sdk version < M.) Make an exception for
3252 // the main executable and linker; they do not need to have dt_soname.
3253 // TODO: >= O the linker doesn't need this workaround.
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003254 if (soname_ == nullptr &&
3255 this != solist_get_somain() &&
3256 (flags_ & FLAG_LINKER) == 0 &&
Elliott Hughes95c6cd72019-12-20 13:26:14 -08003257 get_application_target_sdk_version() < 23) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003258 soname_ = basename(realpath_.c_str());
Elliott Hughes95c6cd72019-12-20 13:26:14 -08003259 DL_WARN_documented_change(23,
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003260 "missing-soname-enforced-for-api-level-23",
3261 "\"%s\" has no DT_SONAME (will use %s instead)",
3262 get_realpath(), soname_);
3263
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003264 // 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 -07003265 }
Ryan Prichardae320cd2019-12-10 12:39:05 -08003266
Ryan Prichard0e12cce2020-01-02 14:59:11 -08003267 // Validate each library's verdef section once, so we don't have to validate
3268 // it each time we look up a symbol with a version.
3269 if (!validate_verdef_section(this)) return false;
3270
Ryan Prichardae320cd2019-12-10 12:39:05 -08003271 flags_ |= FLAG_PRELINKED;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003272 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003273}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003274
Ryan Prichard339ecef2020-01-02 16:36:06 -08003275bool soinfo::link_image(const SymbolLookupList& lookup_list, soinfo* local_group_root,
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -04003276 const android_dlextinfo* extinfo, size_t* relro_fd_offset) {
dimitry965d06d2017-11-28 16:03:07 +01003277 if (is_image_linked()) {
3278 // already linked.
3279 return true;
3280 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003281
Elliott Hughes90f96b92019-05-09 15:56:39 -07003282 if (g_is_ldd && !is_main_executable()) {
3283 async_safe_format_fd(STDOUT_FILENO, "\t%s => %s (%p)\n", get_soname(),
3284 get_realpath(), reinterpret_cast<void*>(base));
3285 }
3286
Ryan Prichard339ecef2020-01-02 16:36:06 -08003287 local_group_root_ = local_group_root;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003288 if (local_group_root_ == nullptr) {
3289 local_group_root_ = this;
3290 }
3291
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003292 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3293 target_sdk_version_ = get_application_target_sdk_version();
3294 }
3295
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003296#if !defined(__LP64__)
3297 if (has_text_relocations) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003298 // Fail if app is targeting M or above.
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003299 int app_target_api_level = get_application_target_sdk_version();
Elliott Hughes95c6cd72019-12-20 13:26:14 -08003300 if (app_target_api_level >= 23) {
Elliott Hughes763f6e12017-04-10 09:52:33 -07003301 DL_ERR_AND_LOG("\"%s\" has text relocations (https://android.googlesource.com/platform/"
3302 "bionic/+/master/android-changes-for-ndk-developers.md#Text-Relocations-"
3303 "Enforced-for-API-level-23)", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003304 return false;
3305 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003306 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003307 // phdr_table_protect_segments() after all of them are applied.
Elliott Hughes95c6cd72019-12-20 13:26:14 -08003308 DL_WARN_documented_change(23,
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003309 "Text-Relocations-Enforced-for-API-level-23",
3310 "\"%s\" has text relocations",
3311 get_realpath());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003312 add_dlwarning(get_realpath(), "text relocations");
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003313 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003314 DL_ERR("can't unprotect loadable segments for \"%s\": %s", get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003315 return false;
3316 }
3317 }
3318#endif
3319
Ryan Prichard339ecef2020-01-02 16:36:06 -08003320 if (!relocate(lookup_list)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003321 return false;
3322 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003323
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003324 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003325
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003326#if !defined(__LP64__)
3327 if (has_text_relocations) {
3328 // All relocations are done, we can protect our segments back to read-only.
3329 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3330 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003331 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003332 return false;
3333 }
3334 }
3335#endif
3336
Mingwei Shibe910522015-11-12 07:02:14 +00003337 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
3338 // itself --- it can't make system calls yet, and will have to call protect_relro later.
3339 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003340 return false;
3341 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003342
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003343 /* Handle serializing/sharing the RELRO segment */
3344 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3345 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
Torne (Richard Coles)fa9f7f22019-04-02 17:04:42 -04003346 extinfo->relro_fd, relro_fd_offset) < 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003347 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003348 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003349 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003350 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003351 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3352 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -04003353 extinfo->relro_fd, relro_fd_offset) < 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003354 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003355 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003356 return false;
3357 }
3358 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003359
Ryan Pricharda2e83ab2019-08-16 17:25:43 -07003360 ++g_module_load_counter;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003361 notify_gdb_of_load(this);
dimitry965d06d2017-11-28 16:03:07 +01003362 set_image_linked();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003363 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003364}
3365
Mingwei Shibe910522015-11-12 07:02:14 +00003366bool soinfo::protect_relro() {
3367 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3368 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
3369 get_realpath(), strerror(errno));
3370 return false;
3371 }
3372 return true;
3373}
3374
Jiyong Park02586a22017-05-20 01:01:24 +09003375static std::vector<android_namespace_t*> init_default_namespace_no_config(bool is_asan) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003376 g_default_namespace.set_isolated(false);
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003377 auto default_ld_paths = is_asan ? kAsanDefaultLdPaths : kDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003378
neo.chae2589f9d2016-10-04 11:00:27 +09003379 char real_path[PATH_MAX];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003380 std::vector<std::string> ld_default_paths;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003381 for (size_t i = 0; default_ld_paths[i] != nullptr; ++i) {
3382 if (realpath(default_ld_paths[i], real_path) != nullptr) {
neo.chae2589f9d2016-10-04 11:00:27 +09003383 ld_default_paths.push_back(real_path);
3384 } else {
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003385 ld_default_paths.push_back(default_ld_paths[i]);
neo.chae2589f9d2016-10-04 11:00:27 +09003386 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003387 }
3388
3389 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Jiyong Park02586a22017-05-20 01:01:24 +09003390
3391 std::vector<android_namespace_t*> namespaces;
3392 namespaces.push_back(&g_default_namespace);
3393 return namespaces;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003394}
3395
Roland Levillain57e6b862020-02-12 17:34:20 +00003396// Given an `executable_path` starting with "/apex/<name>/bin/, return
3397// "/linkerconfig/<name>/ld.config.txt" (or "/apex/<name>/etc/ld.config.txt", if
3398// the former does not exist).
Jiyong Park35833412018-10-26 14:45:21 +09003399static std::string get_ld_config_file_apex_path(const char* executable_path) {
3400 std::vector<std::string> paths = android::base::Split(executable_path, "/");
Roland Levillaindc25f6c2019-07-02 16:40:19 +01003401 if (paths.size() >= 5 && paths[1] == "apex" && paths[3] == "bin") {
Kiyoung Kimfb587442020-01-29 16:31:54 +09003402 // Check auto-generated ld.config.txt first
3403 std::string generated_apex_config = "/linkerconfig/" + paths[2] + "/ld.config.txt";
3404 if (file_exists(generated_apex_config.c_str())) {
3405 return generated_apex_config;
3406 }
3407
Jiyong Park35833412018-10-26 14:45:21 +09003408 return std::string("/apex/") + paths[2] + "/etc/ld.config.txt";
3409 }
3410 return "";
3411}
3412
Logan Chien886b96e2018-05-08 17:52:18 +08003413static std::string get_ld_config_file_vndk_path() {
Roland Levillain09bde952019-10-29 19:17:49 +00003414 if (android::base::GetBoolProperty("ro.vndk.lite", false)) {
3415 return kLdConfigVndkLiteFilePath;
Logan Chienbcfe3cf2018-05-08 20:17:26 +08003416 }
3417
Roland Levillain09bde952019-10-29 19:17:49 +00003418 std::string ld_config_file_vndk = kLdConfigFilePath;
3419 size_t insert_pos = ld_config_file_vndk.find_last_of('.');
3420 if (insert_pos == std::string::npos) {
3421 insert_pos = ld_config_file_vndk.length();
3422 }
3423 ld_config_file_vndk.insert(insert_pos, Config::get_vndk_version_string('.'));
3424 return ld_config_file_vndk;
Logan Chien886b96e2018-05-08 17:52:18 +08003425}
3426
Jiyong Park35833412018-10-26 14:45:21 +09003427static std::string get_ld_config_file_path(const char* executable_path) {
Logan Chien886b96e2018-05-08 17:52:18 +08003428#ifdef USE_LD_CONFIG_FILE
3429 // This is a debugging/testing only feature. Must not be available on
3430 // production builds.
3431 const char* ld_config_file_env = getenv("LD_CONFIG_FILE");
3432 if (ld_config_file_env != nullptr && file_exists(ld_config_file_env)) {
3433 return ld_config_file_env;
3434 }
3435#endif
3436
Jiyong Park35833412018-10-26 14:45:21 +09003437 std::string path = get_ld_config_file_apex_path(executable_path);
3438 if (!path.empty()) {
3439 if (file_exists(path.c_str())) {
3440 return path;
3441 }
3442 DL_WARN("Warning: couldn't read config file \"%s\" for \"%s\"",
3443 path.c_str(), executable_path);
Logan Chien886b96e2018-05-08 17:52:18 +08003444 }
3445
Evgeny Eltsinff43ec02019-12-16 19:22:07 +01003446 path = kLdConfigArchFilePath;
3447 if (file_exists(path.c_str())) {
3448 return path;
3449 }
3450
Kiyoung Kimef04c802019-12-13 12:19:56 +09003451 // Use generated linker config if flag is set
3452 // TODO(b/138920271) Do not check property once it is confirmed as stable
3453 if (android::base::GetBoolProperty("sys.linker.use_generated_config", true)) {
3454 if (file_exists(kLdGeneratedConfigFilePath)) {
3455 return kLdGeneratedConfigFilePath;
3456 } else {
Kiyoung Kim19f95dd2019-12-16 19:38:33 +09003457 // TODO(b/146386369) : Adjust log level and add more condition to log only when necessary
3458 INFO("Warning: failed to find generated linker configuration from \"%s\"",
3459 kLdGeneratedConfigFilePath);
Kiyoung Kimef04c802019-12-13 12:19:56 +09003460 }
3461 }
3462
Jiyong Park35833412018-10-26 14:45:21 +09003463 path = get_ld_config_file_vndk_path();
Roland Levillain09bde952019-10-29 19:17:49 +00003464 if (file_exists(path.c_str())) {
Jiyong Park35833412018-10-26 14:45:21 +09003465 return path;
Logan Chien886b96e2018-05-08 17:52:18 +08003466 }
3467
3468 return kLdConfigFilePath;
3469}
3470
Jiyong Park02586a22017-05-20 01:01:24 +09003471std::vector<android_namespace_t*> init_default_namespaces(const char* executable_path) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003472 g_default_namespace.set_name("(default)");
3473
3474 soinfo* somain = solist_get_somain();
3475
3476 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3477 somain->load_bias);
Dan Willemsen7ccc50d2017-09-18 21:28:14 -07003478 const char* bname = (interp != nullptr) ? basename(interp) : nullptr;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003479
3480 g_is_asan = bname != nullptr &&
3481 (strcmp(bname, "linker_asan") == 0 ||
3482 strcmp(bname, "linker_asan64") == 0);
3483
3484 const Config* config = nullptr;
3485
3486 std::string error_msg;
3487
Jiyong Park35833412018-10-26 14:45:21 +09003488 std::string ld_config_file_path = get_ld_config_file_path(executable_path);
Jiyong Park02586a22017-05-20 01:01:24 +09003489
Martin Stjernholm95252ee2019-02-22 22:48:59 +00003490 INFO("[ Reading linker config \"%s\" ]", ld_config_file_path.c_str());
Logan Chien886b96e2018-05-08 17:52:18 +08003491 if (!Config::read_binary_config(ld_config_file_path.c_str(),
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003492 executable_path,
3493 g_is_asan,
3494 &config,
3495 &error_msg)) {
3496 if (!error_msg.empty()) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -08003497 DL_WARN("Warning: couldn't read \"%s\" for \"%s\" (using default configuration instead): %s",
Logan Chien886b96e2018-05-08 17:52:18 +08003498 ld_config_file_path.c_str(),
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003499 executable_path,
3500 error_msg.c_str());
3501 }
3502 config = nullptr;
3503 }
3504
3505 if (config == nullptr) {
Jiyong Park02586a22017-05-20 01:01:24 +09003506 return init_default_namespace_no_config(g_is_asan);
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003507 }
3508
3509 const auto& namespace_configs = config->namespace_configs();
3510 std::unordered_map<std::string, android_namespace_t*> namespaces;
3511
3512 // 1. Initialize default namespace
3513 const NamespaceConfig* default_ns_config = config->default_namespace_config();
3514
3515 g_default_namespace.set_isolated(default_ns_config->isolated());
3516 g_default_namespace.set_default_library_paths(default_ns_config->search_paths());
3517 g_default_namespace.set_permitted_paths(default_ns_config->permitted_paths());
3518
3519 namespaces[default_ns_config->name()] = &g_default_namespace;
Justin Yun90de9f02017-07-07 16:21:53 +09003520 if (default_ns_config->visible()) {
3521 g_exported_namespaces[default_ns_config->name()] = &g_default_namespace;
3522 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003523
3524 // 2. Initialize other namespaces
3525
3526 for (auto& ns_config : namespace_configs) {
3527 if (namespaces.find(ns_config->name()) != namespaces.end()) {
3528 continue;
3529 }
3530
3531 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
3532 ns->set_name(ns_config->name());
3533 ns->set_isolated(ns_config->isolated());
3534 ns->set_default_library_paths(ns_config->search_paths());
3535 ns->set_permitted_paths(ns_config->permitted_paths());
Vic Yang2d020e42019-01-12 21:03:25 -08003536 ns->set_whitelisted_libs(ns_config->whitelisted_libs());
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003537
3538 namespaces[ns_config->name()] = ns;
Jiyong Park01de74e2017-04-03 23:10:37 +09003539 if (ns_config->visible()) {
3540 g_exported_namespaces[ns_config->name()] = ns;
3541 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003542 }
3543
3544 // 3. Establish links between namespaces
3545 for (auto& ns_config : namespace_configs) {
3546 auto it_from = namespaces.find(ns_config->name());
3547 CHECK(it_from != namespaces.end());
3548 android_namespace_t* namespace_from = it_from->second;
3549 for (const NamespaceLinkConfig& ns_link : ns_config->links()) {
3550 auto it_to = namespaces.find(ns_link.ns_name());
3551 CHECK(it_to != namespaces.end());
3552 android_namespace_t* namespace_to = it_to->second;
Logan Chien9ee45912018-01-18 12:05:09 +08003553 if (ns_link.allow_all_shared_libs()) {
3554 link_namespaces_all_libs(namespace_from, namespace_to);
3555 } else {
3556 link_namespaces(namespace_from, namespace_to, ns_link.shared_libs().c_str());
3557 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003558 }
3559 }
3560 // we can no longer rely on the fact that libdl.so is part of default namespace
3561 // this is why we want to add ld-android.so to all namespaces from ld.config.txt
3562 soinfo* ld_android_so = solist_get_head();
dimitry8b142562018-05-09 15:22:38 +02003563
3564 // we also need vdso to be available for all namespaces (if present)
3565 soinfo* vdso = solist_get_vdso();
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003566 for (auto it : namespaces) {
NIEJuhue2871bd2019-12-20 18:51:21 +08003567 if (it.second != &g_default_namespace) {
3568 it.second->add_soinfo(ld_android_so);
3569 if (vdso != nullptr) {
3570 it.second->add_soinfo(vdso);
3571 }
3572 // somain and ld_preloads are added to these namespaces after LD_PRELOAD libs are linked
dimitry8b142562018-05-09 15:22:38 +02003573 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003574 }
3575
3576 set_application_target_sdk_version(config->target_sdk_version());
Jiyong Park02586a22017-05-20 01:01:24 +09003577
3578 std::vector<android_namespace_t*> created_namespaces;
3579 created_namespaces.reserve(namespaces.size());
Chih-Hung Hsieh0218e922018-12-11 10:22:11 -08003580 for (const auto& kv : namespaces) {
Jiyong Park02586a22017-05-20 01:01:24 +09003581 created_namespaces.push_back(kv.second);
3582 }
3583 return created_namespaces;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003584}
Jiyong Park01de74e2017-04-03 23:10:37 +09003585
3586// This function finds a namespace exported in ld.config.txt by its name.
3587// A namespace can be exported by setting .visible property to true.
3588android_namespace_t* get_exported_namespace(const char* name) {
3589 if (name == nullptr) {
3590 return nullptr;
3591 }
3592 auto it = g_exported_namespaces.find(std::string(name));
3593 if (it == g_exported_namespaces.end()) {
3594 return nullptr;
3595 }
3596 return it->second;
3597}
Vic Yangbb7e1232019-01-29 20:23:16 -08003598
3599void purge_unused_memory() {
3600 // For now, we only purge the memory used by LoadTask because we know those
3601 // are temporary objects.
3602 //
3603 // Purging other LinkerBlockAllocator hardly yields much because they hold
3604 // information about namespaces and opened libraries, which are not freed
3605 // when the control leaves the linker.
3606 //
3607 // Purging BionicAllocator may give us a few dirty pages back, but those pages
3608 // would be already zeroed out, so they compress easily in ZRAM. Therefore,
3609 // it is not worth munmap()'ing those pages.
3610 TypeBasedAllocator<LoadTask>::purge();
3611}