blob: 27b812d25a2ae26722bba91eedc1de3372f31faf [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002 * Copyright (C) 2008 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Dmitriy Ivanov19133522015-06-02 17:36:54 -070029#include <android/api-level.h>
Elliott Hughes46882792012-08-03 16:49:39 -070030#include <errno.h>
31#include <fcntl.h>
Elliott Hughes0266ae52014-02-10 17:46:57 -080032#include <inttypes.h>
Elliott Hughes46882792012-08-03 16:49:39 -070033#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
Elliott Hughes46882792012-08-03 16:49:39 -070037#include <sys/mman.h>
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -080038#include <sys/param.h>
Elliott Hughes46882792012-08-03 16:49:39 -070039#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070041#include <new>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070042#include <string>
Dmitriy Ivanovb4827502015-09-28 16:38:31 -070043#include <unordered_map>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070044#include <vector>
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070045
Elliott Hughes46882792012-08-03 16:49:39 -070046// Private C library headers.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -070047#include "private/ScopeGuard.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080048
49#include "linker.h"
Dmitriy Ivanovc9ce70d2015-03-10 15:30:26 -070050#include "linker_block_allocator.h"
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070051#include "linker_cfi.h"
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080052#include "linker_config.h"
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -080053#include "linker_gdb_support.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070054#include "linker_globals.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055#include "linker_debug.h"
Dimitry Ivanov769b33f2016-07-21 11:33:40 -070056#include "linker_dlwarning.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070057#include "linker_main.h"
Dimitry Ivanovb943f302016-08-03 16:00:10 -070058#include "linker_namespaces.h"
Dmitriy Ivanov18870d32015-04-22 13:10:04 -070059#include "linker_sleb128.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020060#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080061#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080062#include "linker_reloc_iterators.h"
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070063#include "linker_utils.h"
tony.ys_liub4474402015-07-29 18:00:22 +080064
Elliott Hughes939a7e02015-12-04 15:27:46 -080065#include "android-base/strings.h"
Dimitry Ivanovb996d602016-07-11 18:11:39 -070066#include "android-base/stringprintf.h"
Simon Baldwinaef71952015-01-16 13:22:54 +000067#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068
Elliott Hughes1801db32015-06-08 18:04:00 -070069// Override macros to use C++ style casts.
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080070#undef ELF_ST_TYPE
71#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
72
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -070073static android_namespace_t* g_anonymous_namespace = &g_default_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070074
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070075static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
76static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020077
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070078static LinkerTypeAllocator<android_namespace_t> g_namespace_allocator;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -070079static LinkerTypeAllocator<LinkedListEntry<android_namespace_t>> g_namespace_list_allocator;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070080
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080081static const char* const kLdConfigFilePath = "/system/etc/ld.config.txt";
82
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070083#if defined(__LP64__)
Jiyong Park1daa18e2017-01-31 17:32:46 +090084static const char* const kSystemLibDir = "/system/lib64";
85static const char* const kSystemNdkLibDir = "/system/lib64/ndk";
86static const char* const kSystemVndkLibDir = "/system/lib64/vndk";
87static const char* const kSystemVndkExtLibDir = "/system/lib64/vndk-ext";
88static const char* const kVendorSpHalLibDir = "/vendor/lib64/sameprocess";
89static const char* const kVendorLibDir = "/vendor/lib64";
90static const char* const kAsanSystemLibDir = "/data/lib64";
91static const char* const kAsanSystemNdkLibDir = "/data/lib64/ndk";
92static const char* const kAsanSystemVndkLibDir = "/data/lib64/vndk";
93static const char* const kAsanSystemVndkExtLibDir = "/data/lib64/vndk-ext";
94static const char* const kAsanVendorSpHalLibDir = "/data/vendor/lib64/sameprocess";
95static const char* const kAsanVendorLibDir = "/data/vendor/lib64";
Elliott Hughes011bc0b2013-10-08 14:27:10 -070096#else
Jiyong Park1daa18e2017-01-31 17:32:46 +090097static const char* const kSystemLibDir = "/system/lib";
98static const char* const kSystemNdkLibDir = "/system/lib/ndk";
99static const char* const kSystemVndkLibDir = "/system/lib/vndk";
100static const char* const kSystemVndkExtLibDir = "/system/lib/vndk-ext";
101static const char* const kVendorSpHalLibDir = "/vendor/lib/sameprocess";
102static const char* const kVendorLibDir = "/vendor/lib";
103static const char* const kAsanSystemLibDir = "/data/lib";
104static const char* const kAsanSystemNdkLibDir = "/data/lib/ndk";
105static const char* const kAsanSystemVndkLibDir = "/data/lib/vndk";
106static const char* const kAsanSystemVndkExtLibDir = "/data/lib/vndk-ext";
107static const char* const kAsanVendorSpHalLibDir = "/data/vendor/lib/sameprocess";
108static const char* const kAsanVendorLibDir = "/data/vendor/lib";
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700109#endif
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700110
111static const char* const kDefaultLdPaths[] = {
112 kSystemLibDir,
Jiyong Park1daa18e2017-01-31 17:32:46 +0900113 kSystemNdkLibDir,
114 kSystemVndkExtLibDir,
115 kSystemVndkLibDir,
116 kVendorSpHalLibDir,
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700117 kVendorLibDir,
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700118 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700119};
David Bartleybc3a5c22009-06-02 18:27:28 -0700120
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700121static const char* const kAsanDefaultLdPaths[] = {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700122 kAsanSystemLibDir,
123 kSystemLibDir,
Jiyong Park1daa18e2017-01-31 17:32:46 +0900124 kAsanSystemNdkLibDir,
125 kSystemNdkLibDir,
126 kAsanSystemVndkExtLibDir,
127 kSystemVndkExtLibDir,
128 kAsanSystemVndkLibDir,
129 kSystemVndkLibDir,
130 kAsanVendorSpHalLibDir,
131 kVendorSpHalLibDir,
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700132 kAsanVendorLibDir,
133 kVendorLibDir,
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700134 nullptr
135};
136
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700137// Is ASAN enabled?
138static bool g_is_asan = false;
139
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700140static CFIShadowWriter g_cfi_shadow;
141
142CFIShadowWriter* get_cfi_shadow() {
143 return &g_cfi_shadow;
144}
145
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700146static bool is_system_library(const std::string& realpath) {
147 for (const auto& dir : g_default_namespace.get_default_library_paths()) {
148 if (file_is_in_dir(realpath, dir)) {
149 return true;
150 }
151 }
152 return false;
153}
154
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700155// Checks if the file exists and not a directory.
156static bool file_exists(const char* path) {
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700157 struct stat s;
158
159 if (stat(path, &s) != 0) {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700160 return false;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700161 }
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700162
163 return S_ISREG(s.st_mode);
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700164}
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700165
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800166static std::string resolve_soname(const std::string& name) {
167 // We assume that soname equals to basename here
168
169 // TODO(dimitry): consider having honest absolute-path -> soname resolution
170 // note that since we might end up refusing to load this library because
171 // it is not in shared libs list we need to get the soname without actually loading
172 // the library.
173 //
174 // On the other hand there are several places where we already assume that
175 // soname == basename in particular for any not-loaded library mentioned
176 // in DT_NEEDED list.
177 return basename(name.c_str());
178}
179
180static bool maybe_accessible_via_namespace_links(android_namespace_t* ns, const char* name) {
181 std::string soname = resolve_soname(name);
182 for (auto& ns_link : ns->linked_namespaces()) {
183 if (ns_link.is_accessible(soname.c_str())) {
184 return true;
185 }
186 }
187
188 return false;
189}
190
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700191// TODO(dimitry): The grey-list is a workaround for http://b/26394120 ---
192// gradually remove libraries from this list until it is gone.
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800193static bool is_greylisted(android_namespace_t* ns, const char* name, const soinfo* needed_by) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700194 static const char* const kLibraryGreyList[] = {
195 "libandroid_runtime.so",
196 "libbinder.so",
197 "libcrypto.so",
198 "libcutils.so",
199 "libexpat.so",
200 "libgui.so",
201 "libmedia.so",
202 "libnativehelper.so",
203 "libskia.so",
204 "libssl.so",
205 "libstagefright.so",
206 "libsqlite.so",
207 "libui.so",
208 "libutils.so",
209 "libvorbisidec.so",
210 nullptr
211 };
212
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800213 // If you're targeting N, you don't get the greylist.
214 if (get_application_target_sdk_version() >= __ANDROID_API_N__) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700215 return false;
216 }
217
218 // if the library needed by a system library - implicitly assume it
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800219 // is greylisted unless it is in the list of shared libraries for one or
220 // more linked namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700221 if (needed_by != nullptr && is_system_library(needed_by->get_realpath())) {
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800222 return !maybe_accessible_via_namespace_links(ns, name);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700223 }
224
225 // if this is an absolute path - make sure it points to /system/lib(64)
226 if (name[0] == '/' && dirname(name) == kSystemLibDir) {
227 // and reduce the path to basename
228 name = basename(name);
229 }
230
231 for (size_t i = 0; kLibraryGreyList[i] != nullptr; ++i) {
232 if (strcmp(name, kLibraryGreyList[i]) == 0) {
233 return true;
234 }
235 }
236
237 return false;
238}
239// END OF WORKAROUND
240
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700241static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800242
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800243static bool g_anonymous_namespace_initialized;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700244
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800245#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700246struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700247 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700248};
249
250static linker_stats_t linker_stats;
251
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800252void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700253 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700254}
255#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800256void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700257}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800258#endif
259
260#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800261uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800262#endif
263
Elliott Hughesbedfe382012-08-14 14:07:59 -0700264static void notify_gdb_of_load(soinfo* info) {
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800265 if (info->is_linker() || info->is_main_executable()) {
266 // gdb already knows about the linker and the main executable.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700267 return;
268 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800269
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800270 link_map* map = &(info->link_map_head);
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000271
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800272 map->l_addr = info->load_bias;
273 // link_map l_name field is not const.
274 map->l_name = const_cast<char*>(info->get_realpath());
275 map->l_ld = info->dynamic;
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000276
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800277 CHECK(map->l_name != nullptr);
278 CHECK(map->l_name[0] != '\0');
279
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800280 notify_gdb_of_load(map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700281}
282
Elliott Hughesbedfe382012-08-14 14:07:59 -0700283static void notify_gdb_of_unload(soinfo* info) {
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800284 notify_gdb_of_unload(&(info->link_map_head));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800285}
286
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700287LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
288 return g_soinfo_links_allocator.alloc();
289}
290
291void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
292 g_soinfo_links_allocator.free(entry);
293}
294
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700295LinkedListEntry<android_namespace_t>* NamespaceListAllocator::alloc() {
296 return g_namespace_list_allocator.alloc();
297}
298
299void NamespaceListAllocator::free(LinkedListEntry<android_namespace_t>* entry) {
300 g_namespace_list_allocator.free(entry);
301}
302
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700303soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
304 struct stat* file_stat, off64_t file_offset,
305 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700306 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200307 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700308 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200309 }
310
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700311 TRACE("name %s: allocating soinfo for ns=%p", name, ns);
312
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700313 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
314 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700315
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700316 solist_add_soinfo(si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200317
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700318 si->generate_handle();
319 ns->add_soinfo(si);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700320
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700321 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200322 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800323}
324
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800325static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700326 if (si == nullptr) {
327 return;
328 }
329
330 if (si->base != 0 && si->size != 0) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800331 if (!si->is_mapped_by_caller()) {
332 munmap(reinterpret_cast<void*>(si->base), si->size);
333 } else {
334 // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE
335 mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE,
336 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
337 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700338 }
339
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700340 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700341
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700342 if (!solist_remove_soinfo(si)) {
343 // TODO (dimitry): revisit this - for now preserving the logic
344 // but it does not look right, abort if soinfo is not in the list instead?
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700345 return;
346 }
Elliott Hughes46882792012-08-03 16:49:39 -0700347
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700348 // clear links to/from si
349 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700350
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700351 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700352 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800353}
354
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700355static void parse_path(const char* path, const char* delimiters,
356 std::vector<std::string>* resolved_paths) {
357 std::vector<std::string> paths;
358 split_path(path, delimiters, &paths);
359 resolve_paths(paths, resolved_paths);
360}
361
Elliott Hughescade4c32012-12-20 14:42:14 -0800362static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700363 std::vector<std::string> ld_libary_paths;
364 parse_path(path, ":", &ld_libary_paths);
365 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800366}
367
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700368static bool realpath_fd(int fd, std::string* realpath) {
369 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700370 __libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700371 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700372 PRINT("readlink(\"%s\") failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700373 return false;
374 }
375
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700376 *realpath = &buf[0];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700377 return true;
378}
379
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700380#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700381
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700382// For a given PC, find the .so that it belongs to.
383// Returns the base address of the .ARM.exidx section
384// for that .so, and the number of 8-byte entries
385// in that section (via *pcount).
386//
387// Intended to be called by libc's __gnu_Unwind_Find_exidx().
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800388_Unwind_Ptr do_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800389 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800390
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700391 for (soinfo* si = solist_get_head(); si != 0; si = si->next) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700392 if ((addr >= si->base) && (addr < (si->base + si->size))) {
393 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800394 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800395 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700396 }
397 *pcount = 0;
398 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800399}
Elliott Hughes46882792012-08-03 16:49:39 -0700400
Christopher Ferris24053a42013-08-19 17:45:09 -0700401#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700402
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700403// Here, we only have to provide a callback to iterate across all the
404// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700405int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700406 int rv = 0;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700407 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700408 dl_phdr_info dl_info;
409 dl_info.dlpi_addr = si->link_map_head.l_addr;
410 dl_info.dlpi_name = si->link_map_head.l_name;
411 dl_info.dlpi_phdr = si->phdr;
412 dl_info.dlpi_phnum = si->phnum;
413 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
414 if (rv != 0) {
415 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800416 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700417 }
418 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800419}
Elliott Hughes46882792012-08-03 16:49:39 -0700420
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800421
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700422bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700423 soinfo** si_found_in, const soinfo_list_t& global_group,
424 const soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800425 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700426 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700427
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700428 /* "This element's presence in a shared object library alters the dynamic linker's
429 * symbol resolution algorithm for references within the library. Instead of starting
430 * a symbol search with the executable file, the dynamic linker starts from the shared
431 * object itself. If the shared object fails to supply the referenced symbol, the
432 * dynamic linker then searches the executable file and other shared objects as usual."
433 *
434 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
435 *
436 * Note that this is unlikely since static linker avoids generating
437 * relocations for -Bsymbolic linked dynamic executables.
438 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700439 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700440 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700441 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
442 return false;
443 }
444
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700445 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700446 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700447 }
448 }
449
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700450 // 1. Look for it in global_group
451 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700452 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700453 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700454 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700455 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700456 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
457 error = true;
458 return false;
459 }
460
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700461 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700462 *si_found_in = global_si;
463 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700464 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700465
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700466 return true;
467 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700468
469 if (error) {
470 return false;
471 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700472 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700473
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700474 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700475 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700476 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700477 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700478 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700479 // we already did this - skip
480 return true;
481 }
482
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700483 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700484 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700485 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
486 error = true;
487 return false;
488 }
489
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700490 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700491 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700492 return false;
493 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700494
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700495 return true;
496 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700497
498 if (error) {
499 return false;
500 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700501 }
502
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700503 if (s != nullptr) {
504 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
505 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700506 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
507 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700508 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700509 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700510
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700511 *symbol = s;
512 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700513}
514
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700515ProtectedDataGuard::ProtectedDataGuard() {
516 if (ref_count_++ == 0) {
517 protect_data(PROT_READ | PROT_WRITE);
518 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700519
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700520 if (ref_count_ == 0) { // overflow
521 __libc_fatal("Too many nested calls to dlopen()");
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800522 }
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800523}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800524
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800525ProtectedDataGuard::~ProtectedDataGuard() {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700526 if (--ref_count_ == 0) {
527 protect_data(PROT_READ);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800528 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700529}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800530
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700531void ProtectedDataGuard::protect_data(int protection) {
532 g_soinfo_allocator.protect_all(protection);
533 g_soinfo_links_allocator.protect_all(protection);
534 g_namespace_allocator.protect_all(protection);
535 g_namespace_list_allocator.protect_all(protection);
536}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800537
538size_t ProtectedDataGuard::ref_count_ = 0;
539
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700540// Each size has it's own allocator.
541template<size_t size>
542class SizeBasedAllocator {
543 public:
544 static void* alloc() {
545 return allocator_.alloc();
546 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700547
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700548 static void free(void* ptr) {
549 allocator_.free(ptr);
550 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700551
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700552 private:
553 static LinkerBlockAllocator allocator_;
554};
555
556template<size_t size>
557LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
558
559template<typename T>
560class TypeBasedAllocator {
561 public:
562 static T* alloc() {
563 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
564 }
565
566 static void free(T* ptr) {
567 SizeBasedAllocator<sizeof(T)>::free(ptr);
568 }
569};
570
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700571class LoadTask {
572 public:
573 struct deleter_t {
574 void operator()(LoadTask* t) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700575 t->~LoadTask();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700576 TypeBasedAllocator<LoadTask>::free(t);
577 }
578 };
579
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700580 static deleter_t deleter;
581
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800582 static LoadTask* create(const char* name,
583 soinfo* needed_by,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700584 std::unordered_map<const soinfo*, ElfReader>* readers_map) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700585 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700586 return new (ptr) LoadTask(name, needed_by, readers_map);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700587 }
588
589 const char* get_name() const {
590 return name_;
591 }
592
593 soinfo* get_needed_by() const {
594 return needed_by_;
595 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700596
597 soinfo* get_soinfo() const {
598 return si_;
599 }
600
601 void set_soinfo(soinfo* si) {
602 si_ = si;
603 }
604
605 off64_t get_file_offset() const {
606 return file_offset_;
607 }
608
609 void set_file_offset(off64_t offset) {
610 file_offset_ = offset;
611 }
612
613 int get_fd() const {
614 return fd_;
615 }
616
617 void set_fd(int fd, bool assume_ownership) {
618 fd_ = fd;
619 close_fd_ = assume_ownership;
620 }
621
622 const android_dlextinfo* get_extinfo() const {
623 return extinfo_;
624 }
625
626 void set_extinfo(const android_dlextinfo* extinfo) {
627 extinfo_ = extinfo;
628 }
629
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700630 bool is_dt_needed() const {
631 return is_dt_needed_;
632 }
633
634 void set_dt_needed(bool is_dt_needed) {
635 is_dt_needed_ = is_dt_needed;
636 }
637
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700638 const ElfReader& get_elf_reader() const {
639 CHECK(si_ != nullptr);
640 return (*elf_readers_map_)[si_];
641 }
642
643 ElfReader& get_elf_reader() {
644 CHECK(si_ != nullptr);
645 return (*elf_readers_map_)[si_];
646 }
647
648 std::unordered_map<const soinfo*, ElfReader>* get_readers_map() {
649 return elf_readers_map_;
650 }
651
652 bool read(const char* realpath, off64_t file_size) {
653 ElfReader& elf_reader = get_elf_reader();
654 return elf_reader.Read(realpath, fd_, file_offset_, file_size);
655 }
656
657 bool load() {
658 ElfReader& elf_reader = get_elf_reader();
659 if (!elf_reader.Load(extinfo_)) {
660 return false;
661 }
662
663 si_->base = elf_reader.load_start();
664 si_->size = elf_reader.load_size();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800665 si_->set_mapped_by_caller(elf_reader.is_mapped_by_caller());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700666 si_->load_bias = elf_reader.load_bias();
667 si_->phnum = elf_reader.phdr_count();
668 si_->phdr = elf_reader.loaded_phdr();
669
670 return true;
671 }
672
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700673 private:
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800674 LoadTask(const char* name,
675 soinfo* needed_by,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700676 std::unordered_map<const soinfo*, ElfReader>* readers_map)
677 : name_(name), needed_by_(needed_by), si_(nullptr),
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700678 fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map),
679 is_dt_needed_(false) {}
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700680
681 ~LoadTask() {
682 if (fd_ != -1 && close_fd_) {
683 close(fd_);
684 }
685 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700686
687 const char* name_;
688 soinfo* needed_by_;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700689 soinfo* si_;
690 const android_dlextinfo* extinfo_;
691 int fd_;
692 bool close_fd_;
693 off64_t file_offset_;
694 std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_;
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700695 // TODO(dimitry): needed by workaround for http://b/26394120 (the grey-list)
696 bool is_dt_needed_;
697 // END OF WORKAROUND
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700698
699 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
700};
701
Ningsheng Jiane93be992014-09-16 15:22:10 +0800702LoadTask::deleter_t LoadTask::deleter;
703
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700704template <typename T>
705using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
706
707typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700708typedef linked_list_t<const char> StringLinkedList;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700709typedef std::vector<LoadTask*> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700710
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800711enum walk_action_result_t : uint32_t {
712 kWalkStop = 0,
713 kWalkContinue = 1,
714 kWalkSkip = 2
715};
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700716
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700717// This function walks down the tree of soinfo dependencies
718// in breadth-first order and
719// * calls action(soinfo* si) for each node, and
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800720// * terminates walk if action returns kWalkStop
721// * skips children of the node if action
722// return kWalkSkip
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700723//
724// walk_dependencies_tree returns false if walk was terminated
725// by the action and true otherwise.
726template<typename F>
727static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700728 SoinfoLinkedList visit_list;
729 SoinfoLinkedList visited;
730
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700731 for (size_t i = 0; i < root_soinfos_size; ++i) {
732 visit_list.push_back(root_soinfos[i]);
733 }
734
735 soinfo* si;
736 while ((si = visit_list.pop_front()) != nullptr) {
737 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700738 continue;
739 }
740
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800741 walk_action_result_t result = action(si);
742
743 if (result == kWalkStop) {
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700744 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700745 }
746
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700747 visited.push_back(si);
748
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800749 if (result != kWalkSkip) {
750 si->get_children().for_each([&](soinfo* child) {
751 visit_list.push_back(child);
752 });
753 }
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700754 }
755
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700756 return true;
757}
758
759
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800760static const ElfW(Sym)* dlsym_handle_lookup(android_namespace_t* ns,
761 soinfo* root,
762 soinfo* skip_until,
763 soinfo** found,
764 SymbolName& symbol_name,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800765 const version_info* vi) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700766 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700767 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700768
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700769 walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) {
770 if (skip_lookup) {
771 skip_lookup = current_soinfo != skip_until;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800772 return kWalkContinue;
773 }
774
775 if (!ns->is_accessible(current_soinfo)) {
776 return kWalkSkip;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700777 }
778
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800779 if (!current_soinfo->find_symbol_by_name(symbol_name, vi, &result)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700780 result = nullptr;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800781 return kWalkStop;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700782 }
783
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700784 if (result != nullptr) {
785 *found = current_soinfo;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800786 return kWalkStop;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700787 }
788
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800789 return kWalkContinue;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700790 });
791
792 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800793}
794
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800795static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
796 const char* name,
797 const version_info* vi,
798 soinfo** found,
799 soinfo* caller,
800 void* handle);
801
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700802// This is used by dlsym(3). It performs symbol lookup only within the
803// specified soinfo object and its dependencies in breadth first order.
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800804static const ElfW(Sym)* dlsym_handle_lookup(soinfo* si,
805 soinfo** found,
806 const char* name,
807 const version_info* vi) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700808 // According to man dlopen(3) and posix docs in the case when si is handle
809 // of the main executable we need to search not only in the executable and its
810 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
811 //
812 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
813 // libraries and they are loaded in breath-first (correct) order we can just execute
814 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700815 if (si == solist_get_somain()) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800816 return dlsym_linear_lookup(&g_default_namespace, name, vi, found, nullptr, RTLD_DEFAULT);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700817 }
818
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700819 SymbolName symbol_name(name);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800820 // note that the namespace is not the namespace associated with caller_addr
821 // we use ns associated with root si intentionally here. Using caller_ns
822 // causes problems when user uses dlopen_ext to open a library in the separate
823 // namespace and then calls dlsym() on the handle.
824 return dlsym_handle_lookup(si->get_primary_namespace(), si, nullptr, found, symbol_name, vi);
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700825}
826
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800827/* This is used by dlsym(3) to performs a global symbol lookup. If the
828 start value is null (for RTLD_DEFAULT), the search starts at the
829 beginning of the global solist. Otherwise the search starts at the
830 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700831 */
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800832static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
833 const char* name,
834 const version_info* vi,
835 soinfo** found,
836 soinfo* caller,
837 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800838 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800839
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700840 auto& soinfo_list = ns->soinfo_list();
841 auto start = soinfo_list.begin();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700842
843 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -0700844 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700845 return nullptr;
846 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700847 auto it = soinfo_list.find(caller);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700848 CHECK (it != soinfo_list.end());
849 start = ++it;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700850 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800851 }
852
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700853 const ElfW(Sym)* s = nullptr;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700854 for (auto it = start, end = soinfo_list.end(); it != end; ++it) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700855 soinfo* si = *it;
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700856 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800857 // if the library is opened by application with target api level < M.
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700858 // See http://b/21565766
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800859 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 &&
860 si->get_target_sdk_version() >= __ANDROID_API_M__) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700861 continue;
862 }
863
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800864 if (!si->find_symbol_by_name(symbol_name, vi, &s)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700865 return nullptr;
866 }
867
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700868 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800869 *found = si;
870 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600871 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800872 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600873
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700874 // If not found - use dlsym_handle_lookup for caller's
875 // local_group unless it is part of the global group in which
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700876 // case we already did it.
877 if (s == nullptr && caller != nullptr &&
878 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800879 soinfo* local_group_root = caller->get_local_group_root();
880
881 return dlsym_handle_lookup(local_group_root->get_primary_namespace(),
882 local_group_root,
883 (handle == RTLD_NEXT) ? caller : nullptr,
884 found,
885 symbol_name,
886 vi);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700887 }
888
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700889 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700890 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
891 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800892 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800893
Elliott Hughescade4c32012-12-20 14:42:14 -0800894 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800895}
896
Kito Chengfa8c05d2013-03-12 14:58:06 +0800897soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800898 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700899 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800900 if (address >= si->base && address - si->base < si->size) {
901 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600902 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800903 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700904 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600905}
906
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700907class ZipArchiveCache {
908 public:
909 ZipArchiveCache() {}
910 ~ZipArchiveCache();
911
912 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
913 private:
914 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
915
916 std::unordered_map<std::string, ZipArchiveHandle> cache_;
917};
918
919bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
920 std::string key(zip_path);
921
922 auto it = cache_.find(key);
923 if (it != cache_.end()) {
924 *handle = it->second;
925 return true;
926 }
927
928 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
929 if (fd == -1) {
930 return false;
931 }
932
933 if (OpenArchiveFd(fd, "", handle) != 0) {
934 // invalid zip-file (?)
Yabin Cui722072d2016-03-21 17:10:12 -0700935 CloseArchive(handle);
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700936 close(fd);
937 return false;
938 }
939
940 cache_[key] = *handle;
941 return true;
942}
943
944ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -0700945 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700946 CloseArchive(it.second);
947 }
948}
949
950static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700951 const char* const input_path,
952 off64_t* file_offset, std::string* realpath) {
953 std::string normalized_path;
954 if (!normalize_path(input_path, &normalized_path)) {
955 return -1;
956 }
957
958 const char* const path = normalized_path.c_str();
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700959 TRACE("Trying zip file open from path \"%s\" -> normalized \"%s\"", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +0000960
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700961 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +0000962 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700963 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +0000964 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700965 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +0000966 if (separator == nullptr) {
967 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -0700968 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000969
970 char buf[512];
971 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
972 PRINT("Warning: ignoring very long library path: %s", path);
973 return -1;
974 }
975
976 buf[separator - path] = '\0';
977
978 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700979 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +0000980 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
981 if (fd == -1) {
982 return -1;
983 }
984
985 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700986 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000987 // invalid zip-file (?)
988 close(fd);
989 return -1;
990 }
991
Simon Baldwinaef71952015-01-16 13:22:54 +0000992 ZipEntry entry;
993
Yusuke Sato56f40fb2015-06-25 14:56:07 -0700994 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000995 // Entry was not found.
996 close(fd);
997 return -1;
998 }
999
1000 // Check if it is properly stored
1001 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
1002 close(fd);
1003 return -1;
1004 }
1005
1006 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001007
1008 if (realpath_fd(fd, realpath)) {
1009 *realpath += separator;
1010 } else {
1011 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1012 normalized_path.c_str());
1013 *realpath = normalized_path;
1014 }
1015
Simon Baldwinaef71952015-01-16 13:22:54 +00001016 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001017}
1018
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001019static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
1020 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
1021 if (n < 0 || n >= static_cast<int>(buf_size)) {
1022 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1023 return false;
1024 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001025
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001026 return true;
1027}
1028
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001029static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1030 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001031 const std::vector<std::string>& paths,
1032 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001033 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001034 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001035 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001036 continue;
1037 }
1038
1039 int fd = -1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001040 if (strstr(buf, kZipFileSeparator) != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001041 fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath);
Simon Baldwinaef71952015-01-16 13:22:54 +00001042 }
1043
1044 if (fd == -1) {
1045 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1046 if (fd != -1) {
1047 *file_offset = 0;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001048 if (!realpath_fd(fd, realpath)) {
1049 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
1050 *realpath = buf;
1051 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001052 }
1053 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001054
1055 if (fd != -1) {
1056 return fd;
1057 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001058 }
1059
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001060 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001061}
1062
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001063static int open_library(android_namespace_t* ns,
1064 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001065 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001066 off64_t* file_offset, std::string* realpath) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001067 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001068
Elliott Hughes124fae92012-10-31 14:20:03 -07001069 // 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 -07001070 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001071 int fd = -1;
1072
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001073 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001074 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1075 }
1076
1077 if (fd == -1) {
1078 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001079 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001080 *file_offset = 0;
1081 if (!realpath_fd(fd, realpath)) {
1082 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name);
1083 *realpath = name;
1084 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001085 }
1086 }
1087
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001088 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001089 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001090
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001091 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1092 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 -07001093 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001094 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001095 // Check if the library is accessible
1096 if (fd != -1 && !ns->is_accessible(*realpath)) {
1097 fd = -1;
1098 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001099 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001100
Elliott Hughes124fae92012-10-31 14:20:03 -07001101 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001102 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001103 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001104
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001105 // TODO(dimitry): workaround for http://b/26394120 (the grey-list)
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001106 if (fd == -1 && ns != &g_default_namespace && is_greylisted(ns, name, needed_by)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001107 // try searching for it on default_namespace default_library_path
1108 fd = open_library_on_paths(zip_archive_cache, name, file_offset,
1109 g_default_namespace.get_default_library_paths(), realpath);
1110 }
1111 // END OF WORKAROUND
1112
Elliott Hughes124fae92012-10-31 14:20:03 -07001113 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001114}
1115
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001116const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001117#if !defined(__LP64__)
1118 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Elliott Hughes5bc78c82016-11-16 11:35:43 -08001119 if (get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001120 const char* bname = basename(dt_needed);
1121 if (bname != dt_needed) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001122 DL_WARN("library \"%s\" has invalid DT_NEEDED entry \"%s\"", sopath, dt_needed);
1123 add_dlwarning(sopath, "invalid DT_NEEDED entry", dt_needed);
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001124 }
1125
1126 return bname;
1127 }
1128#endif
1129 return dt_needed;
1130}
1131
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001132template<typename F>
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001133static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
1134 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1135 if (d->d_tag == DT_NEEDED) {
1136 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1137 }
1138 }
1139}
1140
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001141static bool load_library(android_namespace_t* ns,
1142 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001143 LoadTaskList* load_tasks,
1144 int rtld_flags,
1145 const std::string& realpath) {
1146 off64_t file_offset = task->get_file_offset();
1147 const char* name = task->get_name();
1148 const android_dlextinfo* extinfo = task->get_extinfo();
1149
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001150 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001151 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001152 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001153 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001154 if (file_offset < 0) {
1155 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001156 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001157 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001158
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001159 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001160 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001161 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001162 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001163 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001164 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001165 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1166 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001167 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001168 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001169
1170 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001171 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1172 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001173 auto predicate = [&](soinfo* si) {
1174 return si->get_st_dev() != 0 &&
1175 si->get_st_ino() != 0 &&
1176 si->get_st_dev() == file_stat.st_dev &&
1177 si->get_st_ino() == file_stat.st_ino &&
1178 si->get_file_offset() == file_offset;
1179 };
1180
1181 soinfo* si = ns->soinfo_list().find_if(predicate);
1182
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001183 if (si != nullptr) {
1184 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
1185 "will return existing soinfo", name, si->get_realpath());
1186 task->set_soinfo(si);
1187 return true;
1188 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001189 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001190
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001191 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001192 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001193 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001194 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001195
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001196 if (!ns->is_accessible(realpath)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001197 // TODO(dimitry): workaround for http://b/26394120 - the grey-list
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001198
1199 // TODO(dimitry) before O release: add a namespace attribute to have this enabled
1200 // only for classloader-namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001201 const soinfo* needed_by = task->is_dt_needed() ? task->get_needed_by() : nullptr;
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001202 if (is_greylisted(ns, name, needed_by)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001203 // print warning only if needed by non-system library
1204 if (needed_by == nullptr || !is_system_library(needed_by->get_realpath())) {
1205 const soinfo* needed_or_dlopened_by = task->get_needed_by();
1206 const char* sopath = needed_or_dlopened_by == nullptr ? "(unknown)" :
1207 needed_or_dlopened_by->get_realpath();
1208 DL_WARN("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the namespace \"%s\""
1209 " - the access is temporarily granted as a workaround for http://b/26394120, note that the access"
1210 " will be removed in future releases of Android.",
1211 name, realpath.c_str(), sopath, ns->get_name());
1212 add_dlwarning(sopath, "unauthorized access to", name);
1213 }
1214 } else {
1215 // do not load libraries if they are not accessible for the specified namespace.
1216 const char* needed_or_dlopened_by = task->get_needed_by() == nullptr ?
1217 "(unknown)" :
1218 task->get_needed_by()->get_realpath();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001219
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001220 DL_ERR("library \"%s\" needed or dlopened by \"%s\" is not accessible for the namespace \"%s\"",
1221 name, needed_or_dlopened_by, ns->get_name());
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001222
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001223 // do not print this if a library is in the list of shared libraries for linked namespaces
1224 if (!maybe_accessible_via_namespace_links(ns, name)) {
1225 PRINT("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the"
1226 " namespace: [name=\"%s\", ld_library_paths=\"%s\", default_library_paths=\"%s\","
1227 " permitted_paths=\"%s\"]",
1228 name, realpath.c_str(),
1229 needed_or_dlopened_by,
1230 ns->get_name(),
1231 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
1232 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
1233 android::base::Join(ns->get_permitted_paths(), ':').c_str());
1234 }
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001235 return false;
1236 }
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001237 }
1238
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001239 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001240 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001241 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001242 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001243
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001244 task->set_soinfo(si);
1245
1246 // Read the ELF header and some of the segments.
1247 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001248 soinfo_free(si);
1249 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001250 return false;
1251 }
1252
1253 // find and set DT_RUNPATH and dt_soname
1254 // Note that these field values are temporary and are
1255 // going to be overwritten on soinfo::prelink_image
1256 // with values from PT_LOAD segments.
1257 const ElfReader& elf_reader = task->get_elf_reader();
1258 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1259 if (d->d_tag == DT_RUNPATH) {
1260 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1261 }
1262 if (d->d_tag == DT_SONAME) {
1263 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1264 }
1265 }
1266
1267 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
1268 load_tasks->push_back(LoadTask::create(name, si, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001269 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001270
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001271 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001272}
1273
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001274static bool load_library(android_namespace_t* ns,
1275 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001276 ZipArchiveCache* zip_archive_cache,
1277 LoadTaskList* load_tasks,
1278 int rtld_flags) {
1279 const char* name = task->get_name();
1280 soinfo* needed_by = task->get_needed_by();
1281 const android_dlextinfo* extinfo = task->get_extinfo();
1282
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001283 off64_t file_offset;
1284 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001285 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001286 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001287 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1288 file_offset = extinfo->library_fd_offset;
1289 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001290
1291 if (!realpath_fd(extinfo->library_fd, &realpath)) {
1292 PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1293 "Will use given name.", name);
1294 realpath = name;
1295 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001296
1297 task->set_fd(extinfo->library_fd, false);
1298 task->set_file_offset(file_offset);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001299 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001300 }
1301
1302 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001303 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001304 if (fd == -1) {
1305 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001306 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001307 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001308
1309 task->set_fd(fd, true);
1310 task->set_file_offset(file_offset);
1311
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001312 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001313}
1314
Dimitry Ivanov3bd90612017-02-01 08:54:43 -08001315// Returns true if library was found and false otherwise
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001316static bool find_loaded_library_by_soname(android_namespace_t* ns,
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001317 const char* name, soinfo** candidate) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001318 *candidate = nullptr;
1319
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001320 // Ignore filename with path.
1321 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001322 return false;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001323 }
1324
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001325 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001326 const char* soname = si->get_soname();
1327 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dimitry Ivanov3bd90612017-02-01 08:54:43 -08001328 *candidate = si;
1329 return false;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001330 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001331
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001332 return true;
1333 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001334}
1335
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001336static bool find_library_in_linked_namespace(const android_namespace_link_t& namespace_link,
1337 LoadTask* task,
1338 int rtld_flags) {
1339 android_namespace_t* ns = namespace_link.linked_namespace();
1340
1341 soinfo* candidate;
1342 bool loaded = false;
1343
1344 std::string soname;
1345 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
1346 loaded = true;
1347 soname = candidate->get_soname();
1348 } else {
1349 soname = resolve_soname(task->get_name());
1350 }
1351
1352 if (!namespace_link.is_accessible(soname.c_str())) {
1353 // the library is not accessible via namespace_link
1354 return false;
1355 }
1356
1357 // if library is already loaded - return it
1358 if (loaded) {
1359 task->set_soinfo(candidate);
1360 return true;
1361 }
1362
1363 // try to load the library - once namespace boundary is crossed
1364 // we need to load a library within separate load_group
1365 // to avoid using symbols from foreign namespace while.
1366 //
1367 // All symbols during relocation should be resolved within a
1368 // namespace to preserve library locality to a namespace.
1369 const char* name = task->get_name();
1370 if (find_libraries(ns,
1371 task->get_needed_by(),
1372 &name,
1373 1,
1374 &candidate,
1375 nullptr /* ld_preloads */,
1376 0 /* ld_preload_count*/,
1377 rtld_flags,
1378 nullptr /* extinfo*/,
1379 false /* add_as_children */,
1380 false /* search_linked_namespaces */)) {
1381 task->set_soinfo(candidate);
1382 return true;
1383 }
1384
1385 return false;
1386}
1387
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001388static bool find_library_internal(android_namespace_t* ns,
1389 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001390 ZipArchiveCache* zip_archive_cache,
1391 LoadTaskList* load_tasks,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001392 int rtld_flags,
1393 bool search_linked_namespaces) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001394 soinfo* candidate;
1395
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001396 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001397 task->set_soinfo(candidate);
1398 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001399 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001400
1401 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001402 // of this fact is done by load_library.
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001403 TRACE("[ \"%s\" find_loaded_library_by_soname failed (*candidate=%s@%p). Trying harder...]",
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001404 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001405
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001406 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags)) {
1407 return true;
1408 }
1409
1410 if (search_linked_namespaces) {
1411 // if a library was not found - look into linked namespaces
1412 for (auto& linked_namespace : ns->linked_namespaces()) {
1413 if (find_library_in_linked_namespace(linked_namespace,
1414 task,
1415 rtld_flags)) {
1416 return true;
1417 }
1418 }
1419 }
1420
1421 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001422}
1423
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001424static void soinfo_unload(soinfo* si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001425static void soinfo_unload(soinfo* soinfos[], size_t count);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001426
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001427// TODO: this is slightly unusual way to construct
1428// the global group for relocation. Not every RTLD_GLOBAL
1429// library is included in this group for backwards-compatibility
1430// reasons.
1431//
1432// This group consists of the main executable, LD_PRELOADs
1433// and libraries with the DF_1_GLOBAL flag set.
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001434static soinfo_list_t make_global_group(android_namespace_t* ns) {
1435 soinfo_list_t global_group;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001436 ns->soinfo_list().for_each([&](soinfo* si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001437 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1438 global_group.push_back(si);
1439 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001440 });
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001441
1442 return global_group;
1443}
1444
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001445// This function provides a list of libraries to be shared
1446// by the namespace. For the default namespace this is the global
1447// group (see make_global_group). For all others this is a group
1448// of RTLD_GLOBAL libraries (which includes the global group from
1449// the default namespace).
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001450static soinfo_list_t get_shared_group(android_namespace_t* ns) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001451 if (ns == &g_default_namespace) {
1452 return make_global_group(ns);
1453 }
1454
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001455 soinfo_list_t shared_group;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001456 ns->soinfo_list().for_each([&](soinfo* si) {
1457 if ((si->get_rtld_flags() & RTLD_GLOBAL) != 0) {
1458 shared_group.push_back(si);
1459 }
1460 });
1461
1462 return shared_group;
1463}
1464
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001465static void shuffle(std::vector<LoadTask*>* v) {
1466 for (size_t i = 0, size = v->size(); i < size; ++i) {
1467 size_t n = size - i;
1468 size_t r = arc4random_uniform(n);
1469 std::swap((*v)[n-1], (*v)[r]);
1470 }
1471}
1472
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001473// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1474// not their transitive dependencies) as children of the start_with library.
1475// This is false when find_libraries is called for dlopen(), when newly loaded
1476// libraries must form a disjoint tree.
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001477bool find_libraries(android_namespace_t* ns,
1478 soinfo* start_with,
1479 const char* const library_names[],
1480 size_t library_names_count,
1481 soinfo* soinfos[],
1482 std::vector<soinfo*>* ld_preloads,
1483 size_t ld_preloads_count,
1484 int rtld_flags,
1485 const android_dlextinfo* extinfo,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001486 bool add_as_children,
1487 bool search_linked_namespaces) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001488 // Step 0: prepare.
1489 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001490 std::unordered_map<const soinfo*, ElfReader> readers_map;
1491
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001492 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001493 const char* name = library_names[i];
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001494 load_tasks.push_back(LoadTask::create(name, start_with, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001495 }
1496
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001497 // Construct global_group.
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001498 soinfo_list_t global_group = make_global_group(ns);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001499
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001500 // If soinfos array is null allocate one on stack.
1501 // The array is needed in case of failure; for example
1502 // when library_names[] = {libone.so, libtwo.so} and libone.so
1503 // is loaded correctly but libtwo.so failed for some reason.
1504 // In this case libone.so should be unloaded on return.
1505 // See also implementation of failure_guard below.
1506
1507 if (soinfos == nullptr) {
1508 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1509 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1510 memset(soinfos, 0, soinfos_size);
1511 }
1512
1513 // list of libraries to link - see step 2.
1514 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001515
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001516 auto scope_guard = make_scope_guard([&]() {
1517 for (LoadTask* t : load_tasks) {
1518 LoadTask::deleter(t);
1519 }
1520 });
1521
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001522 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001523 // Housekeeping
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001524 soinfo_unload(soinfos, soinfos_count);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001525 });
1526
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001527 ZipArchiveCache zip_archive_cache;
1528
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001529 // Step 1: expand the list of load_tasks to include
1530 // all DT_NEEDED libraries (do not load them just yet)
1531 for (size_t i = 0; i<load_tasks.size(); ++i) {
1532 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07001533 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001534
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001535 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001536 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001537 task->set_dt_needed(is_dt_needed);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001538
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001539 if (!find_library_internal(ns,
1540 task,
1541 &zip_archive_cache,
1542 &load_tasks,
1543 rtld_flags,
1544 search_linked_namespaces || is_dt_needed)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001545 return false;
1546 }
1547
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001548 soinfo* si = task->get_soinfo();
1549
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001550 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001551 needed_by->add_child(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001552
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001553 if (si->is_linked()) {
1554 si->increment_ref_count();
1555 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001556 }
1557
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001558 // When ld_preloads is not null, the first
1559 // ld_preloads_count libs are in fact ld_preloads.
1560 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001561 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001562 }
1563
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001564 if (soinfos_count < library_names_count) {
1565 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001566 }
1567 }
1568
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001569 // Step 2: Load libraries in random order (see b/24047022)
1570 LoadTaskList load_list;
1571 for (auto&& task : load_tasks) {
1572 soinfo* si = task->get_soinfo();
1573 auto pred = [&](const LoadTask* t) {
1574 return t->get_soinfo() == si;
1575 };
1576
1577 if (!si->is_linked() &&
1578 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
1579 load_list.push_back(task);
1580 }
1581 }
1582 shuffle(&load_list);
1583
1584 for (auto&& task : load_list) {
1585 if (!task->load()) {
1586 return false;
1587 }
1588 }
1589
1590 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
1591 for (auto&& task : load_tasks) {
1592 soinfo* si = task->get_soinfo();
1593 if (!si->is_linked() && !si->prelink_image()) {
1594 return false;
1595 }
1596 }
1597
1598 // Step 4: Add LD_PRELOADed libraries to the global group for
1599 // future runs. There is no need to explicitly add them to
1600 // the global group for this run because they are going to
1601 // appear in the local group in the correct order.
1602 if (ld_preloads != nullptr) {
1603 for (auto&& si : *ld_preloads) {
1604 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
1605 }
1606 }
1607
1608
1609 // Step 5: link libraries.
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001610 soinfo_list_t local_group;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001611 walk_dependencies_tree(
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001612 (start_with != nullptr && add_as_children) ? &start_with : soinfos,
1613 (start_with != nullptr && add_as_children) ? 1 : soinfos_count,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001614 [&] (soinfo* si) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001615 if (ns->is_accessible(si)) {
1616 local_group.push_back(si);
1617 return kWalkContinue;
1618 } else {
1619 return kWalkSkip;
1620 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001621 });
1622
1623 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001624 if (!si->is_linked()) {
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001625 if (!si->link_image(global_group, local_group, extinfo) ||
1626 !get_cfi_shadow()->AfterLoad(si, solist_get_head())) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001627 return false;
1628 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001629 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001630
1631 return true;
1632 });
1633
1634 if (linked) {
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001635 local_group.for_each([](soinfo* si) {
1636 if (!si->is_linked()) {
1637 si->set_linked();
1638 }
1639 });
1640
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001641 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001642 }
1643
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001644 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001645}
1646
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001647static soinfo* find_library(android_namespace_t* ns,
1648 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001649 const android_dlextinfo* extinfo,
1650 soinfo* needed_by) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001651 soinfo* si;
1652
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001653 if (name == nullptr) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001654 si = solist_get_somain();
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001655 } else if (!find_libraries(ns,
1656 needed_by,
1657 &name,
1658 1,
1659 &si,
1660 nullptr,
1661 0,
1662 rtld_flags,
1663 extinfo,
1664 false /* add_as_children */,
1665 true /* search_linked_namespaces */)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001666 return nullptr;
1667 }
1668
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001669 si->increment_ref_count();
1670
Elliott Hughesd23736e2012-11-01 15:16:56 -07001671 return si;
1672}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001673
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001674static void soinfo_unload(soinfo* root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001675 if (root->is_linked()) {
1676 root = root->get_local_group_root();
1677 }
1678
1679 if (!root->can_unload()) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001680 TRACE("not unloading \"%s\" - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001681 return;
1682 }
1683
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001684 soinfo_unload(&root, 1);
1685}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001686
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001687static void soinfo_unload(soinfo* soinfos[], size_t count) {
1688 // Note that the library can be loaded but not linked;
1689 // in which case there is no root but we still need
1690 // to walk the tree and unload soinfos involved.
1691 //
1692 // This happens on unsuccessful dlopen, when one of
1693 // the DT_NEEDED libraries could not be linked/found.
1694 if (count == 0) {
1695 return;
1696 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001697
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001698 soinfo_list_t unload_list;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001699 for (size_t i = 0; i < count; ++i) {
1700 soinfo* si = soinfos[i];
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001701
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001702 if (si->can_unload()) {
1703 size_t ref_count = si->is_linked() ? si->decrement_ref_count() : 0;
1704 if (ref_count == 0) {
1705 unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001706 } else {
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001707 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
1708 si->get_realpath(), ref_count);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001709 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001710 } else {
1711 TRACE("not unloading '%s' - the binary is flagged with NODELETE", si->get_realpath());
1712 return;
1713 }
1714 }
1715
1716 // This is used to identify soinfos outside of the load-group
1717 // note that we cannot have > 1 in the array and have any of them
1718 // linked. This is why we can safely use the first one.
1719 soinfo* root = soinfos[0];
1720
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001721 soinfo_list_t local_unload_list;
1722 soinfo_list_t external_unload_list;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001723 soinfo* si = nullptr;
1724
1725 while ((si = unload_list.pop_front()) != nullptr) {
1726 if (local_unload_list.contains(si)) {
1727 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001728 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001729
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001730 local_unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001731
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001732 if (si->has_min_version(0)) {
1733 soinfo* child = nullptr;
1734 while ((child = si->get_children().pop_front()) != nullptr) {
1735 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
1736 child->get_realpath(), child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001737
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001738 child->get_parents().remove(si);
1739
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001740 if (local_unload_list.contains(child)) {
1741 continue;
1742 } else if (child->is_linked() && child->get_local_group_root() != root) {
1743 external_unload_list.push_back(child);
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001744 } else if (child->get_parents().empty()) {
1745 unload_list.push_back(child);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001746 }
1747 }
1748 } else {
1749#if !defined(__work_around_b_24465209__)
1750 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
1751#else
1752 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
1753 for_each_dt_needed(si, [&] (const char* library_name) {
1754 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
1755 si->get_realpath(), library_name);
1756
1757 soinfo* needed = find_library(si->get_primary_namespace(),
1758 library_name, RTLD_NOLOAD, nullptr, nullptr);
1759
1760 if (needed != nullptr) {
1761 // Not found: for example if symlink was deleted between dlopen and dlclose
1762 // Since we cannot really handle errors at this point - print and continue.
1763 PRINT("warning: couldn't find %s needed by %s on unload.",
1764 library_name, si->get_realpath());
1765 return;
1766 } else if (local_unload_list.contains(needed)) {
1767 // already visited
1768 return;
1769 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
1770 // external group
1771 external_unload_list.push_back(needed);
1772 } else {
1773 // local group
1774 unload_list.push_front(needed);
1775 }
1776 });
1777#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001778 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001779 }
1780
1781 local_unload_list.for_each([](soinfo* si) {
1782 si->call_destructors();
1783 });
1784
1785 while ((si = local_unload_list.pop_front()) != nullptr) {
1786 notify_gdb_of_unload(si);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001787 get_cfi_shadow()->BeforeUnload(si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001788 soinfo_free(si);
1789 }
1790
1791 while ((si = external_unload_list.pop_front()) != nullptr) {
1792 soinfo_unload(si);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001793 }
1794}
1795
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001796static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
1797 if (sym_ver == nullptr) {
1798 return sym_name;
1799 }
1800
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001801 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001802}
1803
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001804static android_namespace_t* get_caller_namespace(soinfo* caller) {
1805 return caller != nullptr ? caller->get_primary_namespace() : g_anonymous_namespace;
1806}
1807
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001808void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001809 // Use basic string manipulation calls to avoid snprintf.
1810 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1811 // When debug malloc is enabled, this call returns 0. This in turn causes
1812 // snprintf to do nothing, which causes libraries to fail to load.
1813 // See b/17302493 for further details.
1814 // Once the above bug is fixed, this code can be modified to use
1815 // snprintf again.
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001816 const auto& default_ld_paths = g_default_namespace.get_default_library_paths();
1817
1818 size_t required_size = 0;
1819 for (const auto& path : default_ld_paths) {
1820 required_size += path.size() + 1;
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001821 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001822
1823 if (buffer_size < required_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001824 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001825 "buffer len %zu, required len %zu", buffer_size, required_size);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001826 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001827
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001828 char* end = buffer;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001829 for (size_t i = 0; i < default_ld_paths.size(); ++i) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001830 if (i > 0) *end++ = ':';
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001831 end = stpcpy(end, default_ld_paths[i].c_str());
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001832 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001833}
1834
Elliott Hughescade4c32012-12-20 14:42:14 -08001835void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08001836 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08001837}
1838
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001839static std::string android_dlextinfo_to_string(const android_dlextinfo* info) {
1840 if (info == nullptr) {
1841 return "(null)";
1842 }
1843
1844 return android::base::StringPrintf("[flags=0x%" PRIx64 ","
1845 " reserved_addr=%p,"
1846 " reserved_size=0x%zx,"
1847 " relro_fd=%d,"
1848 " library_fd=%d,"
1849 " library_fd_offset=0x%" PRIx64 ","
1850 " library_namespace=%s@%p]",
1851 info->flags,
1852 info->reserved_addr,
1853 info->reserved_size,
1854 info->relro_fd,
1855 info->library_fd,
1856 info->library_fd_offset,
1857 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
1858 (info->library_namespace != nullptr ?
1859 info->library_namespace->get_name() : "(null)") : "(n/a)",
1860 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
1861 info->library_namespace : nullptr);
1862}
1863
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08001864void* do_dlopen(const char* name, int flags,
1865 const android_dlextinfo* extinfo,
1866 const void* caller_addr) {
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -07001867 std::string trace_prefix = std::string("dlopen: ") + (name == nullptr ? "(nullptr)" : name);
1868 ScopedTrace trace(trace_prefix.c_str());
1869 ScopedTrace loading_trace((trace_prefix + " - loading and linking").c_str());
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001870 soinfo* const caller = find_containing_library(caller_addr);
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001871 android_namespace_t* ns = get_caller_namespace(caller);
1872
1873 LD_LOG(kLogDlopen,
1874 "dlopen(name=\"%s\", flags=0x%x, extinfo=%s, caller=\"%s\", caller_ns=%s@%p) ...",
1875 name,
1876 flags,
1877 android_dlextinfo_to_string(extinfo).c_str(),
1878 caller == nullptr ? "(null)" : caller->get_realpath(),
1879 ns == nullptr ? "(null)" : ns->get_name(),
1880 ns);
1881
1882 auto failure_guard = make_scope_guard([&]() {
1883 LD_LOG(kLogDlopen, "... dlopen failed: %s", linker_get_error_buffer());
1884 });
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001885
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001886 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001887 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001888 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001889 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001890
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001891 if (extinfo != nullptr) {
1892 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1893 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1894 return nullptr;
1895 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07001896
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001897 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001898 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001899 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
1900 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001901 return nullptr;
1902 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07001903
1904 if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 &&
1905 (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) {
1906 DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not "
1907 "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT");
1908 return nullptr;
1909 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001910
1911 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
1912 if (extinfo->library_namespace == nullptr) {
1913 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
1914 return nullptr;
1915 }
1916 ns = extinfo->library_namespace;
1917 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001918 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001919
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001920 std::string asan_name_holder;
1921
1922 const char* translated_name = name;
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001923 if (g_is_asan && translated_name != nullptr && translated_name[0] == '/') {
1924 char translated_path[PATH_MAX];
1925 if (realpath(translated_name, translated_path) != nullptr) {
Evgenii Stepanov5b715002016-10-03 15:09:28 -07001926 if (file_is_under_dir(translated_path, kSystemLibDir)) {
1927 asan_name_holder = std::string(kAsanSystemLibDir) + "/" +
1928 (translated_path + strlen(kSystemLibDir) + 1);
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001929 if (file_exists(asan_name_holder.c_str())) {
1930 translated_name = asan_name_holder.c_str();
1931 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
1932 }
Evgenii Stepanov5b715002016-10-03 15:09:28 -07001933 } else if (file_is_under_dir(translated_path, kVendorLibDir)) {
1934 asan_name_holder = std::string(kAsanVendorLibDir) + "/" +
1935 (translated_path + strlen(kVendorLibDir) + 1);
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001936 if (file_exists(asan_name_holder.c_str())) {
1937 translated_name = asan_name_holder.c_str();
1938 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
1939 }
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001940 }
1941 }
1942 }
1943
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001944 ProtectedDataGuard guard;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001945 soinfo* si = find_library(ns, translated_name, flags, extinfo, caller);
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -07001946 loading_trace.End();
1947
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001948 if (si != nullptr) {
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001949 void* handle = si->to_handle();
1950 LD_LOG(kLogDlopen,
Dimitry Ivanovae4a0c12016-11-21 10:44:35 -08001951 "... dlopen calling constructors: realpath=\"%s\", soname=\"%s\", handle=%p",
1952 si->get_realpath(), si->get_soname(), handle);
1953 si->call_constructors();
1954 failure_guard.disable();
1955 LD_LOG(kLogDlopen,
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001956 "... dlopen successful: realpath=\"%s\", soname=\"%s\", handle=%p",
1957 si->get_realpath(), si->get_soname(), handle);
1958 return handle;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001959 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001960
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001961 return nullptr;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001962}
1963
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001964int do_dladdr(const void* addr, Dl_info* info) {
1965 // Determine if this address can be found in any library currently mapped.
1966 soinfo* si = find_containing_library(addr);
1967 if (si == nullptr) {
1968 return 0;
1969 }
1970
1971 memset(info, 0, sizeof(Dl_info));
1972
1973 info->dli_fname = si->get_realpath();
1974 // Address at which the shared object is loaded.
1975 info->dli_fbase = reinterpret_cast<void*>(si->base);
1976
1977 // Determine if any symbol in the library contains the specified address.
1978 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
1979 if (sym != nullptr) {
1980 info->dli_sname = si->get_string(sym->st_name);
1981 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
1982 }
1983
1984 return 1;
1985}
1986
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001987static soinfo* soinfo_from_handle(void* handle) {
1988 if ((reinterpret_cast<uintptr_t>(handle) & 1) != 0) {
1989 auto it = g_soinfo_handles_map.find(reinterpret_cast<uintptr_t>(handle));
1990 if (it == g_soinfo_handles_map.end()) {
1991 return nullptr;
1992 } else {
1993 return it->second;
1994 }
1995 }
1996
1997 return static_cast<soinfo*>(handle);
1998}
1999
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08002000bool do_dlsym(void* handle,
2001 const char* sym_name,
2002 const char* sym_ver,
2003 const void* caller_addr,
2004 void** symbol) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002005#if !defined(__LP64__)
2006 if (handle == nullptr) {
2007 DL_ERR("dlsym failed: library handle is null");
2008 return false;
2009 }
2010#endif
2011
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002012 soinfo* found = nullptr;
2013 const ElfW(Sym)* sym = nullptr;
2014 soinfo* caller = find_containing_library(caller_addr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002015 android_namespace_t* ns = get_caller_namespace(caller);
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002016 soinfo* si = nullptr;
2017 if (handle != RTLD_DEFAULT && handle != RTLD_NEXT) {
2018 si = soinfo_from_handle(handle);
2019 }
2020
2021 LD_LOG(kLogDlsym,
2022 "dlsym(handle=%p(\"%s\"), sym_name=\"%s\", sym_ver=\"%s\", caller=\"%s\", caller_ns=%s@%p) ...",
2023 handle,
2024 si != nullptr ? si->get_realpath() : "n/a",
2025 sym_name,
2026 sym_ver,
2027 caller == nullptr ? "(null)" : caller->get_realpath(),
2028 ns == nullptr ? "(null)" : ns->get_name(),
2029 ns);
2030
2031 auto failure_guard = make_scope_guard([&]() {
2032 LD_LOG(kLogDlsym, "... dlsym failed: %s", linker_get_error_buffer());
2033 });
2034
2035 if (sym_name == nullptr) {
2036 DL_ERR("dlsym failed: symbol name is null");
2037 return false;
2038 }
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002039
2040 version_info vi_instance;
2041 version_info* vi = nullptr;
2042
2043 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002044 vi_instance.name = sym_ver;
2045 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002046 vi = &vi_instance;
2047 }
2048
2049 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2050 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2051 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002052 if (si == nullptr) {
2053 DL_ERR("dlsym failed: invalid handle: %p", handle);
2054 return false;
2055 }
2056 sym = dlsym_handle_lookup(si, &found, sym_name, vi);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002057 }
2058
2059 if (sym != nullptr) {
2060 uint32_t bind = ELF_ST_BIND(sym->st_info);
2061
2062 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2063 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002064 failure_guard.disable();
2065 LD_LOG(kLogDlsym,
2066 "... dlsym successful: sym_name=\"%s\", sym_ver=\"%s\", found in=\"%s\", address=%p",
2067 sym_name, sym_ver, found->get_soname(), *symbol);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002068 return true;
2069 }
2070
2071 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2072 return false;
2073 }
2074
2075 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2076 return false;
2077}
2078
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002079int do_dlclose(void* handle) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002080 ProtectedDataGuard guard;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002081 soinfo* si = soinfo_from_handle(handle);
2082 if (si == nullptr) {
2083 DL_ERR("invalid handle: %p", handle);
2084 return -1;
2085 }
2086
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002087 soinfo_unload(si);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002088 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002089}
2090
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002091bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path) {
2092 if (g_anonymous_namespace_initialized) {
2093 DL_ERR("anonymous namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002094 return false;
2095 }
2096
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002097 ProtectedDataGuard guard;
2098
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002099 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002100 // When the caller is nullptr - create_namespace will take global group
2101 // from the anonymous namespace, which is fine because anonymous namespace
2102 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002103 android_namespace_t* anon_ns =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002104 create_namespace(nullptr,
2105 "(anonymous)",
2106 nullptr,
2107 library_search_path,
2108 // TODO (dimitry): change to isolated eventually.
2109 ANDROID_NAMESPACE_TYPE_REGULAR,
2110 nullptr,
2111 &g_default_namespace);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002112
2113 if (anon_ns == nullptr) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002114 return false;
2115 }
2116
2117 if (!link_namespaces(anon_ns, &g_default_namespace, shared_lib_sonames)) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002118 return false;
2119 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08002120
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002121 g_anonymous_namespace = anon_ns;
Dimitry Ivanov3e0821d2017-03-07 11:02:10 -08002122 g_anonymous_namespace_initialized = true;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002123
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002124 return true;
2125}
2126
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002127static void add_soinfos_to_namespace(const soinfo_list_t& soinfos, android_namespace_t* ns) {
2128 ns->add_soinfos(soinfos);
2129 for (auto si : soinfos) {
2130 si->add_secondary_namespace(ns);
2131 }
2132}
2133
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002134android_namespace_t* create_namespace(const void* caller_addr,
2135 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002136 const char* ld_library_path,
2137 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002138 uint64_t type,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002139 const char* permitted_when_isolated_path,
2140 android_namespace_t* parent_namespace) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002141 if (parent_namespace == nullptr) {
Dimitry Ivanov52408632016-05-23 10:31:11 -07002142 // if parent_namespace is nullptr -> set it to the caller namespace
2143 soinfo* caller_soinfo = find_containing_library(caller_addr);
2144
2145 parent_namespace = caller_soinfo != nullptr ?
2146 caller_soinfo->get_primary_namespace() :
2147 g_anonymous_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002148 }
2149
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002150 ProtectedDataGuard guard;
2151 std::vector<std::string> ld_library_paths;
2152 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002153 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002154
2155 parse_path(ld_library_path, ":", &ld_library_paths);
2156 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002157 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002158
2159 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2160 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002161 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002162 ns->set_ld_library_paths(std::move(ld_library_paths));
2163 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002164 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002165
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002166 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002167 // If shared - clone the parent namespace
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002168 add_soinfos_to_namespace(parent_namespace->soinfo_list(), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002169 } else {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002170 // If not shared - copy only the shared group
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002171 add_soinfos_to_namespace(get_shared_group(parent_namespace), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002172 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002173
2174 return ns;
2175}
2176
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002177bool link_namespaces(android_namespace_t* namespace_from,
2178 android_namespace_t* namespace_to,
2179 const char* shared_lib_sonames) {
2180 if (namespace_to == nullptr) {
2181 namespace_to = &g_default_namespace;
2182 }
2183
2184 if (namespace_from == nullptr) {
2185 DL_ERR("error linking namespaces: namespace_from is null.");
2186 return false;
2187 }
2188
2189 if (shared_lib_sonames == nullptr || shared_lib_sonames[0] == '\0') {
2190 DL_ERR("error linking namespaces \"%s\"->\"%s\": the list of shared libraries is empty.",
2191 namespace_from->get_name(), namespace_to->get_name());
2192 return false;
2193 }
2194
2195 auto sonames = android::base::Split(shared_lib_sonames, ":");
2196 std::unordered_set<std::string> sonames_set(sonames.begin(), sonames.end());
2197
2198 ProtectedDataGuard guard;
2199 namespace_from->add_linked_namespace(namespace_to, sonames_set);
2200
2201 return true;
2202}
2203
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002204ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002205 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2206 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2207 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002208 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2209 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002210
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002211 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002212}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002213
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002214const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2215 if (source_symver < 2 ||
2216 source_symver >= version_infos.size() ||
2217 version_infos[source_symver].name == nullptr) {
2218 return nullptr;
2219 }
2220
2221 return &version_infos[source_symver];
2222}
2223
2224void VersionTracker::add_version_info(size_t source_index,
2225 ElfW(Word) elf_hash,
2226 const char* ver_name,
2227 const soinfo* target_si) {
2228 if (source_index >= version_infos.size()) {
2229 version_infos.resize(source_index+1);
2230 }
2231
2232 version_infos[source_index].elf_hash = elf_hash;
2233 version_infos[source_index].name = ver_name;
2234 version_infos[source_index].target_si = target_si;
2235}
2236
2237bool VersionTracker::init_verneed(const soinfo* si_from) {
2238 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2239
2240 if (verneed_ptr == 0) {
2241 return true;
2242 }
2243
2244 size_t verneed_cnt = si_from->get_verneed_cnt();
2245
2246 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2247 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2248 size_t vernaux_offset = offset + verneed->vn_aux;
2249 offset += verneed->vn_next;
2250
2251 if (verneed->vn_version != 1) {
2252 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2253 return false;
2254 }
2255
2256 const char* target_soname = si_from->get_string(verneed->vn_file);
2257 // find it in dependencies
2258 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002259 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002260 });
2261
2262 if (target_si == nullptr) {
2263 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002264 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002265 return false;
2266 }
2267
2268 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2269 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2270 vernaux_offset += vernaux->vna_next;
2271
2272 const ElfW(Word) elf_hash = vernaux->vna_hash;
2273 const char* ver_name = si_from->get_string(vernaux->vna_name);
2274 ElfW(Half) source_index = vernaux->vna_other;
2275
2276 add_version_info(source_index, elf_hash, ver_name, target_si);
2277 }
2278 }
2279
2280 return true;
2281}
2282
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002283template <typename F>
2284static bool for_each_verdef(const soinfo* si, F functor) {
2285 if (!si->has_min_version(2)) {
2286 return true;
2287 }
2288
2289 uintptr_t verdef_ptr = si->get_verdef_ptr();
2290 if (verdef_ptr == 0) {
2291 return true;
2292 }
2293
2294 size_t offset = 0;
2295
2296 size_t verdef_cnt = si->get_verdef_cnt();
2297 for (size_t i = 0; i<verdef_cnt; ++i) {
2298 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
2299 size_t verdaux_offset = offset + verdef->vd_aux;
2300 offset += verdef->vd_next;
2301
2302 if (verdef->vd_version != 1) {
2303 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
2304 i, verdef->vd_version, si->get_realpath());
2305 return false;
2306 }
2307
2308 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
2309 // "this is the version of the file itself. It must not be used for
2310 // matching a symbol. It can be used to match references."
2311 //
2312 // http://www.akkadia.org/drepper/symbol-versioning
2313 continue;
2314 }
2315
2316 if (verdef->vd_cnt == 0) {
2317 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
2318 return false;
2319 }
2320
2321 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
2322
2323 if (functor(i, verdef, verdaux) == true) {
2324 break;
2325 }
2326 }
2327
2328 return true;
2329}
2330
2331bool find_verdef_version_index(const soinfo* si, const version_info* vi, ElfW(Versym)* versym) {
2332 if (vi == nullptr) {
2333 *versym = kVersymNotNeeded;
2334 return true;
2335 }
2336
2337 *versym = kVersymGlobal;
2338
2339 return for_each_verdef(si,
2340 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2341 if (verdef->vd_hash == vi->elf_hash &&
2342 strcmp(vi->name, si->get_string(verdaux->vda_name)) == 0) {
2343 *versym = verdef->vd_ndx;
2344 return true;
2345 }
2346
2347 return false;
2348 }
2349 );
2350}
2351
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002352bool VersionTracker::init_verdef(const soinfo* si_from) {
2353 return for_each_verdef(si_from,
2354 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2355 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2356 si_from->get_string(verdaux->vda_name), si_from);
2357 return false;
2358 }
2359 );
2360}
2361
2362bool VersionTracker::init(const soinfo* si_from) {
2363 if (!si_from->has_min_version(2)) {
2364 return true;
2365 }
2366
2367 return init_verneed(si_from) && init_verdef(si_from);
2368}
2369
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002370// TODO (dimitry): Methods below need to be moved out of soinfo
2371// and in more isolated file in order minimize dependencies on
2372// unnecessary object in the linker binary. Consider making them
2373// independent from soinfo (?).
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002374bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2375 const char* sym_name, const version_info** vi) {
2376 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2377 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2378
2379 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2380 *vi = version_tracker.get_version_info(sym_ver);
2381
2382 if (*vi == nullptr) {
2383 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002384 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002385 return false;
2386 }
2387 } else {
2388 // there is no version info
2389 *vi = nullptr;
2390 }
2391
2392 return true;
2393}
2394
Dimitry Ivanov576a3752016-08-09 06:58:55 -07002395#if !defined(__mips__)
2396#if defined(USE_RELA)
2397static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2398 return rela->r_addend;
2399}
2400#else
2401static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
2402 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2403 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
2404 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2405 }
2406 return 0;
2407}
2408#endif
2409
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002410template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002411bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2412 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002413 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2414 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002415 if (rel == nullptr) {
2416 return false;
2417 }
2418
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002419 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2420 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2421
2422 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002423 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002424 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002425 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002426
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002427 DEBUG("Processing \"%s\" relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002428 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002429 continue;
2430 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002431
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002432 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002433 soinfo* lsi = nullptr;
2434
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002435 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002436 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002437 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002438
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002439 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2440 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002441 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002442
2443 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2444 return false;
2445 }
2446
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002447 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002448 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002449 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002450 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002451 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002452 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002453 }
2454
2455 /* IHI0044C AAELF 4.5.1.1:
2456
2457 Libraries are not searched to resolve weak references.
2458 It is not an error for a weak reference to remain unsatisfied.
2459
2460 During linking, the value of an undefined weak reference is:
2461 - Zero if the relocation type is absolute
2462 - The address of the place if the relocation is pc-relative
2463 - The address of nominal base address if the relocation
2464 type is base-relative.
2465 */
2466
2467 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002468 case R_GENERIC_JUMP_SLOT:
2469 case R_GENERIC_GLOB_DAT:
2470 case R_GENERIC_RELATIVE:
2471 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002472#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002473 case R_AARCH64_ABS64:
2474 case R_AARCH64_ABS32:
2475 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002476#elif defined(__x86_64__)
2477 case R_X86_64_32:
2478 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002479#elif defined(__arm__)
2480 case R_ARM_ABS32:
2481#elif defined(__i386__)
2482 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002483#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002484 /*
2485 * The sym_addr was initialized to be zero above, or the relocation
2486 * code below does not care about value of sym_addr.
2487 * No need to do anything.
2488 */
2489 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002490#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002491 case R_X86_64_PC32:
2492 sym_addr = reloc;
2493 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002494#elif defined(__i386__)
2495 case R_386_PC32:
2496 sym_addr = reloc;
2497 break;
2498#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002499 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002500 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002501 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002502 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002503 } else { // We got a definition.
2504#if !defined(__LP64__)
2505 // When relocating dso with text_relocation .text segment is
2506 // not executable. We need to restore elf flags before resolving
2507 // STT_GNU_IFUNC symbol.
2508 bool protect_segments = has_text_relocations &&
2509 lsi == this &&
2510 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2511 if (protect_segments) {
2512 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2513 DL_ERR("can't protect segments for \"%s\": %s",
2514 get_realpath(), strerror(errno));
2515 return false;
2516 }
2517 }
2518#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002519 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002520#if !defined(__LP64__)
2521 if (protect_segments) {
2522 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2523 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2524 get_realpath(), strerror(errno));
2525 return false;
2526 }
2527 }
2528#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002529 }
2530 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002531 }
2532
2533 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002534 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002535 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002536 MARK(rel->r_offset);
2537 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2538 reinterpret_cast<void*>(reloc),
2539 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2540
2541 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002542 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002543 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002544 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002545 MARK(rel->r_offset);
2546 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2547 reinterpret_cast<void*>(reloc),
2548 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2549 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002550 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002551 case R_GENERIC_RELATIVE:
2552 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002553 MARK(rel->r_offset);
2554 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2555 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002556 reinterpret_cast<void*>(load_bias + addend));
2557 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002558 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002559 case R_GENERIC_IRELATIVE:
2560 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002561 MARK(rel->r_offset);
2562 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2563 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002564 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002565 {
2566#if !defined(__LP64__)
2567 // When relocating dso with text_relocation .text segment is
2568 // not executable. We need to restore elf flags for this
2569 // particular call.
2570 if (has_text_relocations) {
2571 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2572 DL_ERR("can't protect segments for \"%s\": %s",
2573 get_realpath(), strerror(errno));
2574 return false;
2575 }
2576 }
2577#endif
2578 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2579#if !defined(__LP64__)
2580 // Unprotect it afterwards...
2581 if (has_text_relocations) {
2582 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2583 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2584 get_realpath(), strerror(errno));
2585 return false;
2586 }
2587 }
2588#endif
2589 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2590 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002591 break;
2592
2593#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002594 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002595 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002596 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002597 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002598 reloc, sym_addr + addend, sym_name);
2599 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002600 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002601 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002602 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002603 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002604 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002605 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002606 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002607 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2608 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002609 if ((min_value <= (sym_addr + addend)) &&
2610 ((sym_addr + addend) <= max_value)) {
2611 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002612 } else {
2613 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002614 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002615 return false;
2616 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002617 }
2618 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002619 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002620 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002621 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002622 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002623 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002624 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002625 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2626 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002627 if ((min_value <= (sym_addr + addend)) &&
2628 ((sym_addr + addend) <= max_value)) {
2629 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002630 } else {
2631 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002632 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002633 return false;
2634 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002635 }
2636 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002637 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002638 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002639 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002640 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002641 reloc, sym_addr + addend, rel->r_offset, sym_name);
2642 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002643 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002644 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002645 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002646 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002647 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002648 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002649 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002650 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2651 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002652 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2653 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2654 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002655 } else {
2656 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002657 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002658 return false;
2659 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002660 }
2661 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002662 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002663 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002664 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002665 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002666 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002667 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002668 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2669 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002670 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2671 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2672 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002673 } else {
2674 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002675 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002676 return false;
2677 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002678 }
2679 break;
2680
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002681 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002682 /*
2683 * ET_EXEC is not supported so this should not happen.
2684 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002685 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002686 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002687 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002688 * R_AARCH64_COPY may only appear in executable objects where e_type is
2689 * set to ET_EXEC.
2690 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002691 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002692 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002693 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002694 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002695 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002696 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002697 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002698 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002699 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002700 break;
2701#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002702 case R_X86_64_32:
2703 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002704 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002705 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2706 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002707 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002708 break;
2709 case R_X86_64_64:
2710 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002711 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002712 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2713 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002714 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002715 break;
2716 case R_X86_64_PC32:
2717 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002718 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002719 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2720 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2721 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002722 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002723 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002724#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002725 case R_ARM_ABS32:
2726 count_relocation(kRelocAbsolute);
2727 MARK(rel->r_offset);
2728 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2729 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2730 break;
2731 case R_ARM_REL32:
2732 count_relocation(kRelocRelative);
2733 MARK(rel->r_offset);
2734 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2735 reloc, sym_addr, rel->r_offset, sym_name);
2736 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2737 break;
2738 case R_ARM_COPY:
2739 /*
2740 * ET_EXEC is not supported so this should not happen.
2741 *
2742 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2743 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002744 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002745 * R_ARM_COPY may only appear in executable objects where e_type is
2746 * set to ET_EXEC.
2747 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002748 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002749 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002750#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002751 case R_386_32:
2752 count_relocation(kRelocRelative);
2753 MARK(rel->r_offset);
2754 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2755 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2756 break;
2757 case R_386_PC32:
2758 count_relocation(kRelocRelative);
2759 MARK(rel->r_offset);
2760 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2761 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2762 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2763 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002764#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002765 default:
2766 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002767 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002768 }
2769 }
2770 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002771}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002772#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002773
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002774// An empty list of soinfos
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002775static soinfo_list_t g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002776
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002777bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002778 /* Extract dynamic section */
2779 ElfW(Word) dynamic_flags = 0;
2780 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002781
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002782 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002783 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002784 if (!relocating_linker) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002785 INFO("[ Linking \"%s\" ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002786 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002787 }
2788
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002789 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002790 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002791 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002792 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002793 return false;
2794 } else {
2795 if (!relocating_linker) {
2796 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002797 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002798 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002799
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002800#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002801 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2802 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002803#endif
2804
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002805 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002806 // Note that: "Except for the DT_NULL element at the end of the array,
2807 // and the relative order of DT_NEEDED elements, entries may appear in any order."
2808 //
2809 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002810 uint32_t needed_count = 0;
2811 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2812 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2813 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2814 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002815 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002816 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002817 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002818
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002819 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002820 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2821 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2822 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2823 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002824 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002825
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002826 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002827 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002828 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002829 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2830 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002831
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002832 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002833 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002834 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002835 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
2836 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002837
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002838 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002839 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002840 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002841 return false;
2842 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002843 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002844
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002845 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002846 break;
2847
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002848 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002849 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002850 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002851
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002852 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002853 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002854 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002855
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002856 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002857 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002858 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002859
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002860 case DT_SYMENT:
2861 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002862 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
2863 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002864 return false;
2865 }
2866 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002867
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002868 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002869#if defined(USE_RELA)
2870 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002871 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002872 return false;
2873 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002874#else
2875 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002876 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002877 return false;
2878 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002879#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002880 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002881
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002882 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002883#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002884 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002885#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002886 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002887#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002888 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002889
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002890 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002891#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002892 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002893#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002894 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002895#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002896 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002897
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002898 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002899#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002900 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002901 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002902#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002903 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2904 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002905
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002906 case DT_DEBUG:
2907 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2908 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002909// FIXME: not working currently for N64
2910// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002911// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002912// read-only, but the DYNAMIC header claims it is writable.
2913#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002914 if ((dynamic_flags & PF_W) != 0) {
2915 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2916 }
Chris Dearman99186652014-02-06 20:36:51 -08002917#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002918 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002919#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002920 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002921 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002922 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002923
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002924 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002925 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002926 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002927
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002928 case DT_ANDROID_RELA:
2929 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2930 break;
2931
2932 case DT_ANDROID_RELASZ:
2933 android_relocs_size_ = d->d_un.d_val;
2934 break;
2935
2936 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002937 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002938 return false;
2939
2940 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002941 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002942 return false;
2943
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002944 case DT_RELAENT:
2945 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002946 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002947 return false;
2948 }
2949 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002950
2951 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002952 case DT_RELACOUNT:
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_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002956 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002957 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002958
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002959 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002960 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002961 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002962
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002963#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002964 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002965 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002966 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002967
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002968 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002969 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002970 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002971
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002972 case DT_RELENT:
2973 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002974 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002975 return false;
2976 }
2977 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002978
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002979 case DT_ANDROID_REL:
2980 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2981 break;
2982
2983 case DT_ANDROID_RELSZ:
2984 android_relocs_size_ = d->d_un.d_val;
2985 break;
2986
2987 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002988 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002989 return false;
2990
2991 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002992 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002993 return false;
2994
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002995 // "Indicates that all RELATIVE relocations have been concatenated together,
2996 // and specifies the RELATIVE relocation count."
2997 //
2998 // TODO: Spec also mentions that this can be used to optimize relocation process;
2999 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003000 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003001 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003002
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003003 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003004 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003005 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003006
3007 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003008 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003009 return false;
3010
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003011#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003012 case DT_INIT:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003013 init_func_ = reinterpret_cast<linker_ctor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003014 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003015 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003016
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003017 case DT_FINI:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003018 fini_func_ = reinterpret_cast<linker_dtor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003019 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003020 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003021
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003022 case DT_INIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003023 init_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003024 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003025 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003026
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003027 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003028 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003029 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003030
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003031 case DT_FINI_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003032 fini_array_ = reinterpret_cast<linker_dtor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003033 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003034 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003035
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003036 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003037 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003038 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003039
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003040 case DT_PREINIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003041 preinit_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003042 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003043 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003044
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003045 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003046 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003047 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003048
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003049 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003050#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003051 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003052 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003053#else
3054 has_text_relocations = true;
3055 break;
3056#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003057
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003058 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003059 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003060 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003061
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003062 case DT_NEEDED:
3063 ++needed_count;
3064 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003065
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003066 case DT_FLAGS:
3067 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003068#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003069 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003070 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003071#else
3072 has_text_relocations = true;
3073#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003074 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003075 if (d->d_un.d_val & DF_SYMBOLIC) {
3076 has_DT_SYMBOLIC = true;
3077 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003078 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003079
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003080 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003081 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003082
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003083 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003084 DL_WARN("\"%s\" has unsupported flags DT_FLAGS_1=%p", get_realpath(), reinterpret_cast<void*>(d->d_un.d_val));
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003085 }
3086 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003087#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003088 case DT_MIPS_RLD_MAP:
3089 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3090 {
3091 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3092 *dp = &_r_debug;
3093 }
3094 break;
Lazar Trsic83b44a92016-04-06 13:39:17 +02003095 case DT_MIPS_RLD_MAP_REL:
3096 // Set the DT_MIPS_RLD_MAP_REL entry to the address of _r_debug for GDB.
Raghu Gandham68815722014-12-18 19:12:19 -08003097 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003098 r_debug** dp = reinterpret_cast<r_debug**>(
3099 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003100 *dp = &_r_debug;
3101 }
3102 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003103
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003104 case DT_MIPS_RLD_VERSION:
3105 case DT_MIPS_FLAGS:
3106 case DT_MIPS_BASE_ADDRESS:
3107 case DT_MIPS_UNREFEXTNO:
3108 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003109
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003110 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003111 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003112 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003113
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003114 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003115 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003116 break;
3117
3118 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003119 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003120 break;
3121#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003122 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3123 case DT_BIND_NOW:
3124 break;
3125
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003126 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003127 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3128 break;
3129
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003130 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003131 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3132 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003133 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003134 verdef_cnt_ = d->d_un.d_val;
3135 break;
3136
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003137 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003138 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3139 break;
3140
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003141 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003142 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003143 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003144
Evgenii Stepanov68650822015-06-10 13:38:39 -07003145 case DT_RUNPATH:
3146 // this is parsed after we have strtab initialized (see below).
3147 break;
3148
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003149 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003150 if (!relocating_linker) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003151 DL_WARN("\"%s\" unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003152 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3153 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003154 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003155 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003156 }
3157
Duane Sandbc425c72015-06-01 16:29:14 -07003158#if defined(__mips__) && !defined(__LP64__)
3159 if (!mips_check_and_adjust_fp_modes()) {
3160 return false;
3161 }
3162#endif
3163
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003164 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003165 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003166
3167 // Sanity checks.
3168 if (relocating_linker && needed_count != 0) {
3169 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3170 return false;
3171 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003172 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003173 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003174 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003175 return false;
3176 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003177 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003178 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003179 return false;
3180 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003181 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003182 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003183 return false;
3184 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003185
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003186 // second pass - parse entries relying on strtab
3187 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003188 switch (d->d_tag) {
3189 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003190 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003191 break;
3192 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003193 set_dt_runpath(get_string(d->d_un.d_val));
3194 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003195 }
3196 }
3197
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003198 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003199 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003200 // because they can't find dt_needed library by soname.
3201 // This workaround should keep them working. (applies only
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003202 // for apps targeting sdk version < M). Make an exception for
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003203 // the main executable and linker; they do not need to have dt_soname
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003204 if (soname_ == nullptr &&
3205 this != solist_get_somain() &&
3206 (flags_ & FLAG_LINKER) == 0 &&
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003207 get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003208 soname_ = basename(realpath_.c_str());
3209 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3210 get_realpath(), soname_);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003211 // 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 -07003212 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003213 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003214}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003215
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003216bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3217 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003218
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003219 local_group_root_ = local_group.front();
3220 if (local_group_root_ == nullptr) {
3221 local_group_root_ = this;
3222 }
3223
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003224 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3225 target_sdk_version_ = get_application_target_sdk_version();
3226 }
3227
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003228 VersionTracker version_tracker;
3229
3230 if (!version_tracker.init(this)) {
3231 return false;
3232 }
3233
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003234#if !defined(__LP64__)
3235 if (has_text_relocations) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003236 // Fail if app is targeting M or above.
3237 if (get_application_target_sdk_version() >= __ANDROID_API_M__) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003238 DL_ERR_AND_LOG("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003239 return false;
3240 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003241 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003242 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003243 DL_WARN("\"%s\" has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003244 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003245 add_dlwarning(get_realpath(), "text relocations");
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003246 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3247 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003248 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003249 return false;
3250 }
3251 }
3252#endif
3253
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003254 if (android_relocs_ != nullptr) {
3255 // check signature
3256 if (android_relocs_size_ > 3 &&
3257 android_relocs_[0] == 'A' &&
3258 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003259 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003260 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003261 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003262
3263 bool relocated = false;
3264 const uint8_t* packed_relocs = android_relocs_ + 4;
3265 const size_t packed_relocs_size = android_relocs_size_ - 4;
3266
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003267 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003268 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003269 packed_reloc_iterator<sleb128_decoder>(
3270 sleb128_decoder(packed_relocs, packed_relocs_size)),
3271 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003272
3273 if (!relocated) {
3274 return false;
3275 }
3276 } else {
3277 DL_ERR("bad android relocation header.");
3278 return false;
3279 }
3280 }
3281
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003282#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003283 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003284 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003285 if (!relocate(version_tracker,
3286 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003287 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003288 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003289 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003290 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003291 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003292 if (!relocate(version_tracker,
3293 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003294 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003295 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003296 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003297#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003298 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003299 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003300 if (!relocate(version_tracker,
3301 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003302 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003303 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003304 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003305 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003306 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003307 if (!relocate(version_tracker,
3308 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003309 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003310 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003311 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003312#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003313
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003314#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003315 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003316 return false;
3317 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003318#endif
3319
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003320 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003321
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003322#if !defined(__LP64__)
3323 if (has_text_relocations) {
3324 // All relocations are done, we can protect our segments back to read-only.
3325 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3326 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003327 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003328 return false;
3329 }
3330 }
3331#endif
3332
Mingwei Shibe910522015-11-12 07:02:14 +00003333 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
3334 // itself --- it can't make system calls yet, and will have to call protect_relro later.
3335 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003336 return false;
3337 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003338
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003339 /* Handle serializing/sharing the RELRO segment */
3340 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3341 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3342 extinfo->relro_fd) < 0) {
3343 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003344 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003345 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003346 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003347 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3348 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3349 extinfo->relro_fd) < 0) {
3350 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003351 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003352 return false;
3353 }
3354 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003355
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003356 notify_gdb_of_load(this);
3357 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003358}
3359
Mingwei Shibe910522015-11-12 07:02:14 +00003360bool soinfo::protect_relro() {
3361 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3362 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
3363 get_realpath(), strerror(errno));
3364 return false;
3365 }
3366 return true;
3367}
3368
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003369static void init_default_namespace_no_config(bool is_asan) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003370 g_default_namespace.set_isolated(false);
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003371 auto default_ld_paths = is_asan ? kAsanDefaultLdPaths : kDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003372
neo.chae2589f9d2016-10-04 11:00:27 +09003373 char real_path[PATH_MAX];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003374 std::vector<std::string> ld_default_paths;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003375 for (size_t i = 0; default_ld_paths[i] != nullptr; ++i) {
3376 if (realpath(default_ld_paths[i], real_path) != nullptr) {
neo.chae2589f9d2016-10-04 11:00:27 +09003377 ld_default_paths.push_back(real_path);
3378 } else {
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003379 ld_default_paths.push_back(default_ld_paths[i]);
neo.chae2589f9d2016-10-04 11:00:27 +09003380 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003381 }
3382
3383 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08003384}
3385
3386void init_default_namespace(const char* executable_path) {
3387 g_default_namespace.set_name("(default)");
3388
3389 soinfo* somain = solist_get_somain();
3390
3391 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3392 somain->load_bias);
3393 const char* bname = basename(interp);
3394
3395 g_is_asan = bname != nullptr &&
3396 (strcmp(bname, "linker_asan") == 0 ||
3397 strcmp(bname, "linker_asan64") == 0);
3398
3399 const Config* config = nullptr;
3400
3401 std::string error_msg;
3402
3403 if (!Config::read_binary_config(kLdConfigFilePath,
3404 executable_path,
3405 g_is_asan,
3406 &config,
3407 &error_msg)) {
3408 if (!error_msg.empty()) {
3409 DL_WARN("error reading config file \"%s\" for \"%s\" (will use default configuration): %s",
3410 kLdConfigFilePath,
3411 executable_path,
3412 error_msg.c_str());
3413 }
3414 config = nullptr;
3415 }
3416
3417 if (config == nullptr) {
3418 init_default_namespace_no_config(g_is_asan);
3419 return;
3420 }
3421
3422 const auto& namespace_configs = config->namespace_configs();
3423 std::unordered_map<std::string, android_namespace_t*> namespaces;
3424
3425 // 1. Initialize default namespace
3426 const NamespaceConfig* default_ns_config = config->default_namespace_config();
3427
3428 g_default_namespace.set_isolated(default_ns_config->isolated());
3429 g_default_namespace.set_default_library_paths(default_ns_config->search_paths());
3430 g_default_namespace.set_permitted_paths(default_ns_config->permitted_paths());
3431
3432 namespaces[default_ns_config->name()] = &g_default_namespace;
3433
3434 // 2. Initialize other namespaces
3435
3436 for (auto& ns_config : namespace_configs) {
3437 if (namespaces.find(ns_config->name()) != namespaces.end()) {
3438 continue;
3439 }
3440
3441 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
3442 ns->set_name(ns_config->name());
3443 ns->set_isolated(ns_config->isolated());
3444 ns->set_default_library_paths(ns_config->search_paths());
3445 ns->set_permitted_paths(ns_config->permitted_paths());
3446
3447 namespaces[ns_config->name()] = ns;
3448 }
3449
3450 // 3. Establish links between namespaces
3451 for (auto& ns_config : namespace_configs) {
3452 auto it_from = namespaces.find(ns_config->name());
3453 CHECK(it_from != namespaces.end());
3454 android_namespace_t* namespace_from = it_from->second;
3455 for (const NamespaceLinkConfig& ns_link : ns_config->links()) {
3456 auto it_to = namespaces.find(ns_link.ns_name());
3457 CHECK(it_to != namespaces.end());
3458 android_namespace_t* namespace_to = it_to->second;
3459 link_namespaces(namespace_from, namespace_to, ns_link.shared_libs().c_str());
3460 }
3461 }
3462 // we can no longer rely on the fact that libdl.so is part of default namespace
3463 // this is why we want to add ld-android.so to all namespaces from ld.config.txt
3464 soinfo* ld_android_so = solist_get_head();
3465 for (auto it : namespaces) {
3466 it.second->add_soinfo(ld_android_so);
3467 // TODO (dimitry): somain and ld_preloads should probably be added to all of these namespaces too?
3468 }
3469
3470 set_application_target_sdk_version(config->target_sdk_version());
3471}